Rev 3366 | Rev 3425 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3366 | Rev 3372 | ||
---|---|---|---|
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 | * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved |
3 | * Copyright (c) 2008, Jiri Svoboda - All Rights Reserved |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are met: |
6 | * modification, are permitted provided that the following conditions are met: |
7 | * |
7 | * |
8 | * Redistributions of source code must retain the above copyright notice, this |
8 | * Redistributions of source code must retain the above copyright notice, this |
9 | * list of conditions and the following disclaimer. |
9 | * list of conditions and the following disclaimer. |
10 | * |
10 | * |
11 | * Redistributions in binary form must reproduce the above copyright notice, |
11 | * Redistributions in binary form must reproduce the above copyright notice, |
12 | * this list of conditions and the following disclaimer in the documentation |
12 | * this list of conditions and the following disclaimer in the documentation |
13 | * and/or other materials provided with the distribution. |
13 | * and/or other materials provided with the distribution. |
14 | * |
14 | * |
15 | * Neither the name of the original program's authors nor the names of its |
15 | * Neither the name of the original program's authors nor the names of its |
16 | * contributors may be used to endorse or promote products derived from this |
16 | * contributors may be used to endorse or promote products derived from this |
17 | * software without specific prior written permission. |
17 | * software without specific prior written permission. |
18 | * |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
29 | * POSSIBILITY OF SUCH DAMAGE. |
29 | * POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
30 | */ |
31 | 31 | ||
32 | #include <stdio.h> |
32 | #include <stdio.h> |
33 | #include <stdlib.h> |
33 | #include <stdlib.h> |
34 | #include <string.h> |
34 | #include <string.h> |
35 | #include <io/stream.h> |
35 | #include <io/stream.h> |
36 | 36 | ||
37 | #include "config.h" |
37 | #include "config.h" |
38 | #include "util.h" |
38 | #include "util.h" |
39 | #include "scli.h" |
39 | #include "scli.h" |
40 | #include "input.h" |
40 | #include "input.h" |
41 | #include "errors.h" |
41 | #include "errors.h" |
42 | #include "exec.h" |
42 | #include "exec.h" |
43 | 43 | ||
44 | extern volatile unsigned int cli_interactive; |
44 | extern volatile unsigned int cli_interactive; |
45 | 45 | ||
- | 46 | /* Not exposed in input.h */ |
|
- | 47 | static void cli_restricted(char *); |
|
- | 48 | static void read_line(char *, int); |
|
- | 49 | ||
46 | /* More than a macro than anything */ |
50 | /* More than a macro than anything */ |
47 | void cli_restricted(char *cmd) |
51 | static void cli_restricted(char *cmd) |
48 | { |
52 | { |
49 | printf("%s is not available in %s mode\n", cmd, |
53 | printf("%s is not available in %s mode\n", cmd, |
50 | cli_interactive ? "interactive" : "non-interactive"); |
54 | cli_interactive ? "interactive" : "non-interactive"); |
51 | 55 | ||
52 | return; |
56 | return; |
53 | } |
57 | } |
54 | 58 | ||
55 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
59 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
56 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
60 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
57 | * the handler */ |
61 | * the handler */ |
58 | int tok_input(cliuser_t *usr) |
62 | int tok_input(cliuser_t *usr) |
59 | { |
63 | { |
60 | char *cmd[WORD_MAX]; |
64 | char *cmd[WORD_MAX]; |
61 | int n = 0, i = 0; |
65 | int n = 0, i = 0; |
62 | int rc = 0; |
66 | int rc = 0; |
63 | char *tmp; |
67 | char *tmp; |
64 | 68 | ||
65 | if (NULL == usr->line) |
69 | if (NULL == usr->line) |
66 | return CL_EFAIL; |
70 | return CL_EFAIL; |
67 | 71 | ||
68 | tmp = cli_strdup(usr->line); |
72 | tmp = cli_strdup(usr->line); |
69 | 73 | ||
70 | /* Break up what the user typed, space delimited */ |
74 | /* Break up what the user typed, space delimited */ |
71 | 75 | ||
72 | /* TODO: Protect things in quotes / ticks, expand wildcards */ |
76 | /* TODO: Protect things in quotes / ticks, expand wildcards */ |
73 | cmd[n] = cli_strtok(tmp, " "); |
77 | cmd[n] = cli_strtok(tmp, " "); |
74 | while (cmd[n] && n < WORD_MAX) { |
78 | while (cmd[n] && n < WORD_MAX) { |
75 | cmd[++n] = cli_strtok(NULL, " "); |
79 | cmd[++n] = cli_strtok(NULL, " "); |
76 | } |
80 | } |
77 | 81 | ||
78 | /* We have rubbish */ |
82 | /* We have rubbish */ |
79 | if (NULL == cmd[0]) { |
83 | if (NULL == cmd[0]) { |
80 | rc = CL_ENOENT; |
84 | rc = CL_ENOENT; |
81 | goto finit; |
85 | goto finit; |
82 | } |
86 | } |
83 | 87 | ||
84 | /* Check what kind of command argv[0] might be, TODO: move this to |
- | |
85 | * a function */ |
88 | /* Its a builtin command */ |
86 | if ((i = (is_builtin(cmd[0]))) > -1) { |
89 | if ((i = (is_builtin(cmd[0]))) > -1) { |
- | 90 | /* Its not available in this mode, see what try_exec() thinks */ |
|
87 | if (builtin_is_restricted(i)) { |
91 | if (builtin_is_restricted(i)) { |
88 | rc = try_exec(cmd[0], cmd); |
92 | rc = try_exec(cmd[0], cmd); |
89 | if (rc) |
93 | if (rc) |
- | 94 | /* No external matching it could be found, tell the |
|
- | 95 | * user that the command does exist, but is not |
|
- | 96 | * available in this mode. */ |
|
90 | cli_restricted(cmd[0]); |
97 | cli_restricted(cmd[0]); |
91 | goto finit; |
98 | goto finit; |
92 | } |
99 | } |
- | 100 | /* Its a builtin, its available, run it */ |
|
93 | rc = run_builtin(i, cmd, usr); |
101 | rc = run_builtin(i, cmd, usr); |
94 | goto finit; |
102 | goto finit; |
- | 103 | /* We repeat the same dance for modules */ |
|
95 | } else if ((i = (is_module(cmd[0]))) > -1) { |
104 | } else if ((i = (is_module(cmd[0]))) > -1) { |
96 | if (module_is_restricted(i)) { |
105 | if (module_is_restricted(i)) { |
97 | rc = try_exec(cmd[0], cmd); |
106 | rc = try_exec(cmd[0], cmd); |
98 | if (rc) |
107 | if (rc) |
99 | cli_restricted(cmd[0]); |
108 | cli_restricted(cmd[0]); |
100 | goto finit; |
109 | goto finit; |
101 | } |
110 | } |
102 | rc = run_module(i, cmd); |
111 | rc = run_module(i, cmd); |
103 | goto finit; |
112 | goto finit; |
104 | } else { |
113 | } else { |
- | 114 | /* Its not a module or builtin, restricted or otherwise. |
|
- | 115 | * See what try_exec() thinks of it and just pass its return |
|
- | 116 | * value back to the caller */ |
|
105 | rc = try_exec(cmd[0], cmd); |
117 | rc = try_exec(cmd[0], cmd); |
106 | goto finit; |
118 | goto finit; |
107 | } |
119 | } |
108 | 120 | ||
109 | finit: |
121 | finit: |
110 | if (NULL != usr->line) { |
122 | if (NULL != usr->line) { |
111 | free(usr->line); |
123 | free(usr->line); |
112 | usr->line = (char *) NULL; |
124 | usr->line = (char *) NULL; |
113 | } |
125 | } |
114 | if (NULL != tmp) |
126 | if (NULL != tmp) |
115 | free(tmp); |
127 | free(tmp); |
116 | 128 | ||
117 | return rc; |
129 | return rc; |
118 | } |
130 | } |
119 | 131 | ||
120 | /* Borrowed from Jiri Svoboda's 'cli' uspace app */ |
132 | /* Borrowed from Jiri Svoboda's 'cli' uspace app */ |
121 | void read_line(char *buffer, int n) |
133 | static void read_line(char *buffer, int n) |
122 | { |
134 | { |
123 | char c; |
135 | char c; |
124 | int chars; |
136 | int chars; |
125 | 137 | ||
126 | chars = 0; |
138 | chars = 0; |
127 | while (chars < n - 1) { |
139 | while (chars < n - 1) { |
128 | c = getchar(); |
140 | c = getchar(); |
129 | if (c < 0) |
141 | if (c < 0) |
130 | return; |
142 | return; |
131 | if (c == '\n') |
143 | if (c == '\n') |
132 | break; |
144 | break; |
133 | if (c == '\b') { |
145 | if (c == '\b') { |
134 | if (chars > 0) { |
146 | if (chars > 0) { |
135 | putchar('\b'); |
147 | putchar('\b'); |
136 | --chars; |
148 | --chars; |
137 | } |
149 | } |
138 | continue; |
150 | continue; |
139 | } |
151 | } |
140 | putchar(c); |
152 | putchar(c); |
141 | buffer[chars++] = c; |
153 | buffer[chars++] = c; |
142 | } |
154 | } |
143 | putchar('\n'); |
155 | putchar('\n'); |
144 | buffer[chars] = '\0'; |
156 | buffer[chars] = '\0'; |
145 | } |
157 | } |
146 | 158 | ||
- | 159 | /* TODO: |
|
- | 160 | * Implement something like editline() / readline(), if even |
|
- | 161 | * just for command history and making arrows work. */ |
|
147 | void get_input(cliuser_t *usr) |
162 | void get_input(cliuser_t *usr) |
148 | { |
163 | { |
149 | char line[INPUT_MAX]; |
164 | char line[INPUT_MAX]; |
150 | size_t len = 0; |
165 | size_t len = 0; |
151 | 166 | ||
152 | printf("%s", usr->prompt); |
167 | printf("%s", usr->prompt); |
153 | read_line(line, INPUT_MAX); |
168 | read_line(line, INPUT_MAX); |
154 | len = strlen(line); |
169 | len = strlen(line); |
155 | /* Make sure we don't have rubbish or a C/R happy user */ |
170 | /* Make sure we don't have rubbish or a C/R happy user */ |
156 | if (len == 0 || line[0] == '\n') |
171 | if (len == 0 || line[0] == '\n') |
157 | return; |
172 | return; |
158 | usr->line = cli_strdup(line); |
173 | usr->line = cli_strdup(line); |
159 | 174 | ||
160 | return; |
175 | return; |
161 | } |
176 | } |
162 | 177 | ||
163 | 178 |