Rev 3403 | Rev 4341 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3403 | Rev 4338 | ||
---|---|---|---|
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 | #include <console.h> |
|
36 | 37 | ||
37 | #include "config.h" |
38 | #include "config.h" |
38 | #include "util.h" |
39 | #include "util.h" |
39 | #include "scli.h" |
40 | #include "scli.h" |
40 | #include "input.h" |
41 | #include "input.h" |
41 | #include "errors.h" |
42 | #include "errors.h" |
42 | #include "exec.h" |
43 | #include "exec.h" |
43 | 44 | ||
44 | extern volatile unsigned int cli_interactive; |
45 | extern volatile unsigned int cli_interactive; |
45 | 46 | ||
46 | /* Not exposed in input.h */ |
47 | /* Not exposed in input.h */ |
47 | static void cli_restricted(char *); |
48 | static void cli_restricted(char *); |
48 | static void read_line(char *, int); |
49 | static void read_line(char *, int); |
49 | 50 | ||
50 | /* More than a macro than anything */ |
51 | /* More than a macro than anything */ |
51 | static void cli_restricted(char *cmd) |
52 | static void cli_restricted(char *cmd) |
52 | { |
53 | { |
53 | printf("%s is not available in %s mode\n", cmd, |
54 | printf("%s is not available in %s mode\n", cmd, |
54 | cli_interactive ? "interactive" : "non-interactive"); |
55 | cli_interactive ? "interactive" : "non-interactive"); |
55 | 56 | ||
56 | return; |
57 | return; |
57 | } |
58 | } |
58 | 59 | ||
59 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
60 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
60 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
61 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
61 | * the handler */ |
62 | * the handler */ |
62 | int tok_input(cliuser_t *usr) |
63 | int tok_input(cliuser_t *usr) |
63 | { |
64 | { |
64 | char *cmd[WORD_MAX]; |
65 | char *cmd[WORD_MAX]; |
65 | int n = 0, i = 0; |
66 | int n = 0, i = 0; |
66 | int rc = 0; |
67 | int rc = 0; |
67 | char *tmp; |
68 | char *tmp; |
68 | 69 | ||
69 | if (NULL == usr->line) |
70 | if (NULL == usr->line) |
70 | return CL_EFAIL; |
71 | return CL_EFAIL; |
71 | 72 | ||
72 | tmp = cli_strdup(usr->line); |
73 | tmp = cli_strdup(usr->line); |
73 | 74 | ||
74 | /* Break up what the user typed, space delimited */ |
75 | /* Break up what the user typed, space delimited */ |
75 | 76 | ||
76 | /* TODO: Protect things in quotes / ticks, expand wildcards */ |
77 | /* TODO: Protect things in quotes / ticks, expand wildcards */ |
77 | cmd[n] = cli_strtok(tmp, " "); |
78 | cmd[n] = cli_strtok(tmp, " "); |
78 | while (cmd[n] && n < WORD_MAX) { |
79 | while (cmd[n] && n < WORD_MAX) { |
79 | cmd[++n] = cli_strtok(NULL, " "); |
80 | cmd[++n] = cli_strtok(NULL, " "); |
80 | } |
81 | } |
81 | 82 | ||
82 | /* We have rubbish */ |
83 | /* We have rubbish */ |
83 | if (NULL == cmd[0]) { |
84 | if (NULL == cmd[0]) { |
84 | rc = CL_ENOENT; |
85 | rc = CL_ENOENT; |
85 | goto finit; |
86 | goto finit; |
86 | } |
87 | } |
87 | 88 | ||
88 | /* Its a builtin command */ |
89 | /* Its a builtin command */ |
89 | if ((i = (is_builtin(cmd[0]))) > -1) { |
90 | if ((i = (is_builtin(cmd[0]))) > -1) { |
90 | /* Its not available in this mode, see what try_exec() thinks */ |
91 | /* Its not available in this mode, see what try_exec() thinks */ |
91 | if (builtin_is_restricted(i)) { |
92 | if (builtin_is_restricted(i)) { |
92 | rc = try_exec(cmd[0], cmd); |
93 | rc = try_exec(cmd[0], cmd); |
93 | if (rc) |
94 | if (rc) |
94 | /* No external matching it could be found, tell the |
95 | /* No external matching it could be found, tell the |
95 | * user that the command does exist, but is not |
96 | * user that the command does exist, but is not |
96 | * available in this mode. */ |
97 | * available in this mode. */ |
97 | cli_restricted(cmd[0]); |
98 | cli_restricted(cmd[0]); |
98 | goto finit; |
99 | goto finit; |
99 | } |
100 | } |
100 | /* Its a builtin, its available, run it */ |
101 | /* Its a builtin, its available, run it */ |
101 | rc = run_builtin(i, cmd, usr); |
102 | rc = run_builtin(i, cmd, usr); |
102 | goto finit; |
103 | goto finit; |
103 | /* We repeat the same dance for modules */ |
104 | /* We repeat the same dance for modules */ |
104 | } else if ((i = (is_module(cmd[0]))) > -1) { |
105 | } else if ((i = (is_module(cmd[0]))) > -1) { |
105 | if (module_is_restricted(i)) { |
106 | if (module_is_restricted(i)) { |
106 | rc = try_exec(cmd[0], cmd); |
107 | rc = try_exec(cmd[0], cmd); |
107 | if (rc) |
108 | if (rc) |
108 | cli_restricted(cmd[0]); |
109 | cli_restricted(cmd[0]); |
109 | goto finit; |
110 | goto finit; |
110 | } |
111 | } |
111 | rc = run_module(i, cmd); |
112 | rc = run_module(i, cmd); |
112 | goto finit; |
113 | goto finit; |
113 | } else { |
114 | } else { |
114 | /* Its not a module or builtin, restricted or otherwise. |
115 | /* Its not a module or builtin, restricted or otherwise. |
115 | * See what try_exec() thinks of it and just pass its return |
116 | * See what try_exec() thinks of it and just pass its return |
116 | * value back to the caller */ |
117 | * value back to the caller */ |
117 | rc = try_exec(cmd[0], cmd); |
118 | rc = try_exec(cmd[0], cmd); |
118 | goto finit; |
119 | goto finit; |
119 | } |
120 | } |
120 | 121 | ||
121 | finit: |
122 | finit: |
122 | if (NULL != usr->line) { |
123 | if (NULL != usr->line) { |
123 | free(usr->line); |
124 | free(usr->line); |
124 | usr->line = (char *) NULL; |
125 | usr->line = (char *) NULL; |
125 | } |
126 | } |
126 | if (NULL != tmp) |
127 | if (NULL != tmp) |
127 | free(tmp); |
128 | free(tmp); |
128 | 129 | ||
129 | return rc; |
130 | return rc; |
130 | } |
131 | } |
131 | 132 | ||
132 | /* Borrowed from Jiri Svoboda's 'cli' uspace app */ |
133 | /* Borrowed from Jiri Svoboda's 'cli' uspace app */ |
133 | static void read_line(char *buffer, int n) |
134 | static void read_line(char *buffer, int n) |
134 | { |
135 | { |
135 | char c; |
136 | char c; |
136 | int chars; |
137 | int chars; |
137 | 138 | ||
138 | chars = 0; |
139 | chars = 0; |
139 | while (chars < n - 1) { |
140 | while (chars < n - 1) { |
140 | c = getchar(); |
141 | c = getchar(); |
141 | if (c < 0) |
142 | if (c < 0) |
142 | return; |
143 | return; |
143 | if (c == '\n') |
144 | if (c == '\n') |
144 | break; |
145 | break; |
145 | if (c == '\b') { |
146 | if (c == '\b') { |
146 | if (chars > 0) { |
147 | if (chars > 0) { |
147 | putchar('\b'); |
148 | putchar('\b'); |
148 | --chars; |
149 | --chars; |
149 | } |
150 | } |
150 | continue; |
151 | continue; |
151 | } |
152 | } |
152 | putchar(c); |
153 | putchar(c); |
153 | buffer[chars++] = c; |
154 | buffer[chars++] = c; |
154 | } |
155 | } |
155 | putchar('\n'); |
156 | putchar('\n'); |
156 | buffer[chars] = '\0'; |
157 | buffer[chars] = '\0'; |
157 | } |
158 | } |
158 | 159 | ||
159 | /* TODO: |
160 | /* TODO: |
160 | * Implement something like editline() / readline(), if even |
161 | * Implement something like editline() / readline(), if even |
161 | * just for command history and making arrows work. */ |
162 | * just for command history and making arrows work. */ |
162 | void get_input(cliuser_t *usr) |
163 | void get_input(cliuser_t *usr) |
163 | { |
164 | { |
164 | char line[INPUT_MAX]; |
165 | char line[INPUT_MAX]; |
165 | size_t len = 0; |
166 | size_t len = 0; |
166 | 167 | ||
- | 168 | console_set_style(STYLE_EMPHASIS); |
|
167 | printf("%s", usr->prompt); |
169 | printf("%s", usr->prompt); |
- | 170 | console_set_style(STYLE_NORMAL); |
|
- | 171 | ||
168 | read_line(line, INPUT_MAX); |
172 | read_line(line, INPUT_MAX); |
169 | len = strlen(line); |
173 | len = strlen(line); |
170 | /* Make sure we don't have rubbish or a C/R happy user */ |
174 | /* Make sure we don't have rubbish or a C/R happy user */ |
171 | if (len == 0 || line[0] == '\n') |
175 | if (len == 0 || line[0] == '\n') |
172 | return; |
176 | return; |
173 | usr->line = cli_strdup(line); |
177 | usr->line = cli_strdup(line); |
174 | 178 | ||
175 | return; |
179 | return; |
176 | } |
180 | } |
177 | 181 | ||
178 | 182 |