Rev 3268 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3268 | 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 | * 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 | 35 | ||
36 | #ifdef HELENOS |
36 | #ifdef HELENOS |
37 | #include <io/stream.h> |
37 | #include <io/stream.h> |
38 | #endif |
38 | #endif |
39 | 39 | ||
40 | #include "config.h" |
40 | #include "config.h" |
41 | #include "util.h" |
41 | #include "util.h" |
42 | #include "scli.h" |
42 | #include "scli.h" |
43 | #include "input.h" |
43 | #include "input.h" |
44 | #include "errors.h" |
44 | #include "errors.h" |
45 | #include "exec.h" |
45 | #include "exec.h" |
46 | 46 | ||
47 | extern volatile unsigned int cli_interactive; |
47 | extern volatile unsigned int cli_interactive; |
48 | 48 | ||
49 | /* More than a macro than anything */ |
49 | /* More than a macro than anything */ |
50 | void cli_restricted(char *cmd) |
50 | void cli_restricted(char *cmd) |
51 | { |
51 | { |
52 | cli_verbose("%s is not available in %s mode\n", cmd, |
52 | cli_verbose("%s is not available in %s mode\n", cmd, |
53 | cli_interactive ? "interactive" : "non-interactive"); |
53 | cli_interactive ? "interactive" : "non-interactive"); |
54 | 54 | ||
55 | return; |
55 | return; |
56 | } |
56 | } |
57 | 57 | ||
58 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
58 | /* Tokenizes input from console, sees if the first word is a built-in, if so |
59 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
59 | * invokes the built-in entry point (a[0]) passing all arguments in a[] to |
60 | * the handler */ |
60 | * the handler */ |
61 | int tok_input(cliuser_t *usr) |
61 | int tok_input(cliuser_t *usr) |
62 | { |
62 | { |
63 | char *cmd[WORD_MAX]; |
63 | char *cmd[WORD_MAX]; |
64 | int n = 0, i = 0; |
64 | int n = 0, i = 0; |
65 | int rc = 0; |
65 | int rc = 0; |
66 | char *tmp = (char *) NULL; |
66 | char *tmp; |
67 | 67 | ||
68 | if (NULL == usr->line) { |
68 | if (NULL == usr->line) |
69 | rc = CL_EFAIL; |
69 | return CL_EFAIL; |
70 | goto finit; |
- | |
71 | } |
- | |
72 | 70 | ||
73 | tmp = cli_strdup(usr->line); |
71 | tmp = cli_strdup(usr->line); |
74 | - | ||
- | 72 | cli_verbose("Tok: tmp = %s", tmp); |
|
75 | /* Break up what the user typed, space delimited */ |
73 | /* Break up what the user typed, space delimited */ |
76 | cmd[n] = cli_strtok(tmp, " "); |
74 | cmd[n] = cli_strtok(tmp, " "); |
77 | while (cmd[n] && n < WORD_MAX) { |
75 | while (cmd[n] && n < WORD_MAX) { |
78 | cmd[++n] = cli_strtok(NULL, " "); |
76 | cmd[++n] = cli_strtok(NULL, " "); |
79 | } |
77 | } |
80 | 78 | ||
- | 79 | for (n = 0; cmd[n] != NULL; n++) |
|
- | 80 | cli_verbose("arg[%d] => `%s'", n, cmd[n]); |
|
- | 81 | ||
81 | /* We have rubbish */ |
82 | /* We have rubbish */ |
82 | if (NULL == cmd[0]) { |
83 | if (NULL == cmd[0]) { |
83 | rc = CL_ENOENT; |
84 | rc = CL_ENOENT; |
84 | goto finit; |
85 | goto finit; |
85 | } |
86 | } |
86 | 87 | ||
87 | /* Check what kind of command argv[0] might be */ |
88 | /* Check what kind of command argv[0] might be */ |
88 | if ((i = (is_builtin(cmd[0]))) > -1) { |
89 | if ((i = (is_builtin(cmd[0]))) > -1) { |
89 | /* Its a builtin */ |
90 | /* Its a builtin */ |
90 | if (builtin_is_restricted(i)) { |
91 | if (builtin_is_restricted(i)) { |
91 | /* Try an external command matching argv[0] */ |
92 | /* Try an external command matching argv[0] */ |
92 | rc = try_exec(cmd[0], cmd); |
93 | rc = try_exec(cmd[0], cmd); |
93 | if (rc) |
94 | if (rc) |
94 | cli_restricted(cmd[0]); |
95 | cli_restricted(cmd[0]); |
95 | goto finit; |
96 | goto finit; |
96 | } |
97 | } |
97 | rc = run_builtin(i, cmd, usr); |
98 | rc = run_builtin(i, cmd, usr); |
98 | goto finit; |
99 | goto finit; |
99 | } else if ((i = (is_module(cmd[0]))) > -1) { |
100 | } else if ((i = (is_module(cmd[0]))) > -1) { |
100 | /* Its a module, it can't modify cliuser_t */ |
101 | /* Its a module, it can't modify cliuser_t */ |
101 | if (module_is_restricted(i)) { |
102 | if (module_is_restricted(i)) { |
102 | rc = try_exec(cmd[0], cmd); |
103 | rc = try_exec(cmd[0], cmd); |
103 | if (rc) |
104 | if (rc) |
104 | cli_restricted(cmd[0]); |
105 | cli_restricted(cmd[0]); |
105 | goto finit; |
106 | goto finit; |
106 | } |
107 | } |
107 | rc = run_module(i, cmd); |
108 | rc = run_module(i, cmd); |
108 | goto finit; |
109 | goto finit; |
109 | } else { |
110 | } else { |
110 | /* See what try_exec() thinks of it */ |
111 | /* See what try_exec() thinks of it */ |
111 | rc = try_exec(cmd[0], cmd); |
112 | rc = try_exec(cmd[0], cmd); |
112 | goto finit; |
113 | goto finit; |
113 | } |
114 | } |
114 | 115 | ||
115 | finit: |
116 | finit: |
- | 117 | /* Why is free complaining about these ? |
|
116 | if (NULL != usr->line) { |
118 | if (NULL != usr->line) { |
117 | free(usr->line); |
119 | free(usr->line); |
118 | usr->line = (char *) NULL; |
120 | usr->line = (char *) NULL; |
119 | } |
121 | } |
120 | if (NULL != tmp) |
122 | if (NULL != tmp) |
121 | free(tmp); |
123 | free(tmp); |
122 | 124 | */ |
|
123 | return rc; |
125 | return rc; |
124 | } |
126 | } |
125 | 127 | ||
126 | #ifdef HELENOS |
128 | #ifdef HELENOS |
127 | /* Borrowed from Jiri Svoboda's 'cli' uspace app */ |
129 | /* Borrowed from Jiri Svoboda's 'cli' uspace app */ |
128 | void read_line(char *buffer, int n) |
130 | void read_line(char *buffer, int n) |
129 | { |
131 | { |
130 | char c; |
132 | char c; |
131 | int chars; |
133 | int chars; |
132 | 134 | ||
133 | chars = 0; |
135 | chars = 0; |
134 | while (chars < n - 1) { |
136 | while (chars < n - 1) { |
135 | c = getchar(); |
137 | c = getchar(); |
136 | if (c < 0) |
138 | if (c < 0) |
137 | return; |
139 | return; |
138 | if (c == '\n') |
140 | if (c == '\n') |
139 | break; |
141 | break; |
140 | if (c == '\b') { |
142 | if (c == '\b') { |
141 | if (chars > 0) { |
143 | if (chars > 0) { |
142 | putchar('\b'); |
144 | putchar('\b'); |
143 | --chars; |
145 | --chars; |
144 | } |
146 | } |
145 | continue; |
147 | continue; |
146 | } |
148 | } |
147 | putchar(c); |
149 | putchar(c); |
148 | buffer[chars++] = c; |
150 | buffer[chars++] = c; |
149 | } |
151 | } |
150 | putchar('\n'); |
152 | putchar('\n'); |
151 | buffer[chars] = '\0'; |
153 | buffer[chars] = '\0'; |
152 | } |
154 | } |
153 | #endif |
155 | #endif |
154 | 156 | ||
155 | /* This is soupy, fix me */ |
157 | /* This is soupy, fix me */ |
156 | void get_input(cliuser_t *usr) |
158 | void get_input(cliuser_t *usr) |
157 | { |
159 | { |
158 | char line[INPUT_MAX]; |
160 | char line[INPUT_MAX]; |
159 | size_t len = 0; |
161 | size_t len = 0; |
160 | 162 | ||
161 | printf("%s", usr->prompt); |
163 | printf("%s", usr->prompt); |
162 | #ifdef HELENOS |
164 | #ifdef HELENOS |
163 | read_line(line, INPUT_MAX); |
165 | read_line(line, INPUT_MAX); |
164 | #else |
166 | #else |
165 | fgets(line, INPUT_MAX, stdin); |
167 | fgets(line, INPUT_MAX, stdin); |
166 | #endif |
168 | #endif |
167 | len = strlen(line); |
169 | len = strlen(line); |
168 | /* 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 */ |
169 | if (len == 0) |
171 | if (len == 0) |
170 | return; |
172 | return; |
171 | if (len == 1 && line[len-1] == '\n') |
173 | if (len == 1 && line[len-1] == '\n') |
172 | return; |
174 | return; |
173 | #ifndef HELENOS |
175 | #ifndef HELENOS |
174 | /* Null terminate line */ |
176 | /* Null terminate line */ |
175 | if (len > 0 && line[len-1] == '\n') |
177 | if (len > 0 && line[len-1] == '\n') |
176 | line[len-1] = '\0'; |
178 | line[len-1] = '\0'; |
177 | #endif |
179 | #endif |
178 | usr->line = cli_strdup(line); |
180 | usr->line = cli_strdup(line); |
179 | return; |
181 | return; |
180 | } |
182 | } |
181 | 183 | ||
182 | 184 |