Rev 3366 | Rev 3378 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 3366 | Rev 3377 | ||
|---|---|---|---|
| Line 54... | Line 54... | ||
| 54 | 54 | ||
| 55 | /* These are not exposed, even to builtins */ |
55 | /* These are not exposed, even to builtins */ |
| 56 | static int cli_init(cliuser_t *usr); |
56 | static int cli_init(cliuser_t *usr); |
| 57 | static void cli_finit(cliuser_t *usr); |
57 | static void cli_finit(cliuser_t *usr); |
| 58 | 58 | ||
| 59 | /* (re)allocates memory to store the current working directory, gets |
- | |
| 60 | * and updates the current working directory, formats the prompt |
- | |
| 61 | * string */ |
- | |
| 62 | unsigned int cli_set_prompt(cliuser_t *usr) |
- | |
| 63 | { |
- | |
| 64 | usr->prompt = (char *) realloc(usr->prompt, PATH_MAX); |
- | |
| 65 | if (NULL == usr->prompt) { |
- | |
| 66 | cli_error(CL_ENOMEM, "Can not allocate prompt"); |
- | |
| 67 | return 1; |
- | |
| 68 | } |
- | |
| 69 | memset(usr->prompt, 0, sizeof(usr->prompt)); |
- | |
| 70 | - | ||
| 71 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
- | |
| 72 | if (NULL == usr->cwd) { |
- | |
| 73 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
- | |
| 74 | return 1; |
- | |
| 75 | } |
- | |
| 76 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
- | |
| 77 | - | ||
| 78 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
- | |
| 79 | - | ||
| 80 | if (NULL == usr->cwd) |
- | |
| 81 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
- | |
| 82 | - | ||
| 83 | if (1 < cli_psprintf(&usr->prompt, "%s # ", usr->cwd)) { |
- | |
| 84 | cli_error(cli_errno, "Failed to set prompt"); |
- | |
| 85 | return 1; |
- | |
| 86 | } |
- | |
| 87 | - | ||
| 88 | return 0; |
- | |
| 89 | } |
- | |
| 90 | - | ||
| 91 | /* Constructor */ |
59 | /* Constructor */ |
| 92 | static int cli_init(cliuser_t *usr) |
60 | static int cli_init(cliuser_t *usr) |
| 93 | { |
61 | { |
| 94 | usr->line = (char *) NULL; |
62 | usr->line = (char *) NULL; |
| 95 | usr->name = "root"; |
63 | usr->name = "root"; |
| Line 122... | Line 90... | ||
| 122 | 90 | ||
| 123 | printf("Welcome to %s - %s\nType `help' at any time for usage information.\n", |
91 | printf("Welcome to %s - %s\nType `help' at any time for usage information.\n", |
| 124 | progname, PACKAGE_STRING); |
92 | progname, PACKAGE_STRING); |
| 125 | 93 | ||
| 126 | while (!cli_quit) { |
94 | while (!cli_quit) { |
| 127 | cli_set_prompt(&usr); |
- | |
| 128 | get_input(&usr); |
95 | get_input(&usr); |
| 129 | if (NULL != usr.line) { |
96 | if (NULL != usr.line) { |
| 130 | ret = tok_input(&usr); |
97 | ret = tok_input(&usr); |
| 131 | usr.lasterr = ret; |
98 | usr.lasterr = ret; |
| 132 | } |
99 | } |