Rev 3265 | Rev 3275 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3265 | Rev 3269 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
2 | * All rights reserved. |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: |
5 | * modification, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * Redistributions of source code must retain the above copyright notice, this |
7 | * Redistributions of source code must retain the above copyright notice, this |
8 | * list of conditions and the following disclaimer. |
8 | * list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
11 | * this list of conditions and the following disclaimer in the documentation |
11 | * this list of conditions and the following disclaimer in the documentation |
12 | * and/or other materials provided with the distribution. |
12 | * and/or other materials provided with the distribution. |
13 | * |
13 | * |
14 | * Neither the name of the original program's authors nor the names of its |
14 | * Neither the name of the original program's authors nor the names of its |
15 | * contributors may be used to endorse or promote products derived from this |
15 | * contributors may be used to endorse or promote products derived from this |
16 | * software without specific prior written permission. |
16 | * software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | #include <stdio.h> |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
32 | #include <stdlib.h> |
33 | #include <string.h> |
33 | #include <string.h> |
34 | #include <unistd.h> |
34 | #include <unistd.h> |
35 | #include "config.h" |
35 | #include "config.h" |
36 | #include "scli.h" |
36 | #include "scli.h" |
37 | #include "input.h" |
37 | #include "input.h" |
38 | #include "util.h" |
38 | #include "util.h" |
39 | #include "errors.h" |
39 | #include "errors.h" |
40 | #include "cmds/cmds.h" |
40 | #include "cmds/cmds.h" |
41 | 41 | ||
42 | #ifndef HELENOS |
42 | #ifndef HELENOS |
43 | #include <linux/limits.h> |
43 | #include <linux/limits.h> |
44 | #endif |
44 | #endif |
45 | 45 | ||
46 | /* See scli.h */ |
46 | /* See scli.h */ |
47 | static cliuser_t usr; |
47 | static cliuser_t usr; |
48 | 48 | ||
49 | /* Modified by the 'quit' module, which is compiled before this */ |
49 | /* Modified by the 'quit' module, which is compiled before this */ |
50 | extern unsigned int cli_quit; |
50 | extern unsigned int cli_quit; |
51 | 51 | ||
52 | /* Globals that are modified during start-up that modules/builtins should |
52 | /* Globals that are modified during start-up that modules/builtins should |
53 | * be aware of. */ |
53 | * be aware of. */ |
54 | volatile unsigned int cli_interactive = 1; |
54 | volatile unsigned int cli_interactive = 1; |
55 | volatile unsigned int cli_verbocity = 1; |
55 | volatile unsigned int cli_verbocity = 1; |
56 | 56 | ||
57 | /* The official name of this program |
57 | /* The official name of this program |
58 | * (change to your liking in configure.ac and re-run autoconf) */ |
58 | * (change to your liking in configure.ac and re-run autoconf) */ |
59 | const char *progname = PACKAGE_NAME; |
59 | const char *progname = PACKAGE_NAME; |
60 | 60 | ||
61 | /* (re)allocates memory to store the current working directory, gets |
61 | /* (re)allocates memory to store the current working directory, gets |
62 | * and updates the current working directory, formats the prompt string */ |
62 | * and updates the current working directory, formats the prompt string */ |
63 | int cli_set_prompt(cliuser_t *usr) |
63 | int cli_set_prompt(cliuser_t *usr) |
64 | { |
64 | { |
65 | usr->prompt = (char *) realloc(usr->prompt, PATH_MAX); |
65 | usr->prompt = (char *) realloc(usr->prompt, PATH_MAX); |
66 | if (NULL == usr->prompt) { |
66 | if (NULL == usr->prompt) { |
67 | cli_error(CL_ENOMEM, "Can not allocate prompt"); |
67 | cli_error(CL_ENOMEM, "Can not allocate prompt"); |
68 | return 1; |
68 | return 1; |
69 | } |
69 | } |
70 | memset(usr->prompt, 0, sizeof(usr->prompt)); |
70 | memset(usr->prompt, 0, sizeof(usr->prompt)); |
71 | 71 | ||
72 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
72 | usr->cwd = (char *) realloc(usr->cwd, PATH_MAX); |
73 | if (NULL == usr->cwd) { |
73 | if (NULL == usr->cwd) { |
74 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
74 | cli_error(CL_ENOMEM, "Can not allocate cwd"); |
75 | return 1; |
75 | return 1; |
76 | } |
76 | } |
77 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
77 | memset(usr->cwd, 0, sizeof(usr->cwd)); |
78 | 78 | ||
79 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
79 | usr->cwd = getcwd(usr->cwd, PATH_MAX - 1); |
80 | if (NULL == usr->cwd) |
80 | if (NULL == usr->cwd) |
81 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
81 | snprintf(usr->cwd, PATH_MAX, "(unknown)"); |
82 | 82 | ||
83 | if (usr->lasterr != 0) |
83 | if (usr->lasterr != 0) |
84 | snprintf(usr->prompt, |
84 | snprintf(usr->prompt, |
85 | PATH_MAX, |
85 | PATH_MAX, |
86 | "[%lld] %s # ", |
86 | "[%lld] %s # ", |
87 | usr->lasterr, usr->cwd); |
87 | usr->lasterr, usr->cwd); |
88 | else |
88 | else |
89 | snprintf(usr->prompt, |
89 | snprintf(usr->prompt, |
90 | PATH_MAX, |
90 | PATH_MAX, |
91 | "%s # ", |
91 | "%s # ", |
92 | usr->cwd); |
92 | usr->cwd); |
93 | 93 | ||
94 | return 0; |
94 | return 0; |
95 | } |
95 | } |
96 | 96 | ||
97 | int cli_init(cliuser_t *usr) |
97 | int cli_init(cliuser_t *usr) |
98 | { |
98 | { |
99 | usr->line = (char *) NULL; |
99 | usr->line = (char *) NULL; |
100 | usr->name = "root"; |
100 | usr->name = "root"; |
101 | usr->home = "/"; |
101 | usr->home = "/"; |
102 | usr->cwd = (char *) NULL; |
102 | usr->cwd = (char *) NULL; |
103 | usr->prompt = (char *) NULL; |
103 | usr->prompt = (char *) NULL; |
104 | chdir(usr->home); |
104 | chdir(usr->home); |
105 | usr->lasterr = 0; |
105 | usr->lasterr = 0; |
106 | return (int) cli_set_prompt(usr); |
106 | return (int) cli_set_prompt(usr); |
107 | } |
107 | } |
108 | 108 | ||
109 | /* Destructor */ |
109 | /* Destructor */ |
110 | void cli_finit(cliuser_t *usr) |
110 | void cli_finit(cliuser_t *usr) |
111 | { |
111 | { |
112 | if (NULL != usr->line) |
112 | if (NULL != usr->line) |
113 | free(usr->line); |
113 | free(usr->line); |
114 | if (NULL != usr->prompt) |
114 | if (NULL != usr->prompt) |
115 | free(usr->prompt); |
115 | free(usr->prompt); |
116 | if (NULL != usr->cwd) |
116 | if (NULL != usr->cwd) |
117 | free(usr->cwd); |
117 | free(usr->cwd); |
118 | } |
118 | } |
119 | 119 | ||
120 | int main(int argc, char *argv[]) |
120 | int main(int argc, char *argv[]) |
121 | { |
121 | { |
122 | int ret = 0; |
122 | int ret = 0; |
123 | int i = 0; |
123 | int i = 0; |
124 | 124 | ||
125 | if (cli_init(&usr)) |
125 | if (cli_init(&usr)) |
126 | exit(EXIT_FAILURE); |
126 | exit(EXIT_FAILURE); |
127 | 127 | ||
128 | while (!cli_quit) { |
128 | while (!cli_quit) { |
129 | cli_set_prompt(&usr); |
129 | cli_set_prompt(&usr); |
130 | get_input(&usr); |
130 | get_input(&usr); |
131 | if (NULL != usr.line) { |
131 | if (NULL != usr.line) { |
- | 132 | cli_verbose("Sending %s to tokenizer", usr.line); |
|
132 | ret = tok_input(&usr); |
133 | ret = tok_input(&usr); |
133 | usr.lasterr = ret; |
134 | usr.lasterr = ret; |
134 | } |
135 | } |
135 | i++; |
136 | i++; |
136 | } |
137 | } |
137 | goto finit; |
138 | goto finit; |
138 | 139 | ||
139 | finit: |
140 | finit: |
140 | cli_finit(&usr); |
141 | cli_finit(&usr); |
141 | return ret; |
142 | return ret; |
142 | } |
143 | } |
143 | 144 |