Rev 625 | Rev 640 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 625 | Rev 635 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Jakub Jermar |
2 | * Copyright (C) 2005 Jakub Jermar |
3 | * All rights reserved. |
3 | * 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 |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <console/kconsole.h> |
29 | #include <console/kconsole.h> |
30 | #include <console/console.h> |
30 | #include <console/console.h> |
31 | #include <console/chardev.h> |
31 | #include <console/chardev.h> |
32 | #include <console/cmd.h> |
32 | #include <console/cmd.h> |
33 | #include <print.h> |
33 | #include <print.h> |
34 | #include <panic.h> |
34 | #include <panic.h> |
35 | #include <typedefs.h> |
35 | #include <typedefs.h> |
36 | #include <arch/types.h> |
36 | #include <arch/types.h> |
37 | #include <list.h> |
37 | #include <list.h> |
38 | #include <arch.h> |
38 | #include <arch.h> |
39 | #include <macros.h> |
39 | #include <macros.h> |
40 | #include <debug.h> |
40 | #include <debug.h> |
41 | #include <func.h> |
41 | #include <func.h> |
42 | #include <symtab.h> |
42 | #include <symtab.h> |
43 | #include <macros.h> |
43 | #include <macros.h> |
44 | 44 | ||
45 | /** Simple kernel console. |
45 | /** Simple kernel console. |
46 | * |
46 | * |
47 | * The console is realized by kernel thread kconsole. |
47 | * The console is realized by kernel thread kconsole. |
48 | * It doesn't understand any useful command on its own, |
48 | * It doesn't understand any useful command on its own, |
49 | * but makes it possible for other kernel subsystems to |
49 | * but makes it possible for other kernel subsystems to |
50 | * register their own commands. |
50 | * register their own commands. |
51 | */ |
51 | */ |
52 | 52 | ||
53 | /** Locking. |
53 | /** Locking. |
54 | * |
54 | * |
55 | * There is a list of cmd_info_t structures. This list |
55 | * There is a list of cmd_info_t structures. This list |
56 | * is protected by cmd_lock spinlock. Note that specially |
56 | * is protected by cmd_lock spinlock. Note that specially |
57 | * the link elements of cmd_info_t are protected by |
57 | * the link elements of cmd_info_t are protected by |
58 | * this lock. |
58 | * this lock. |
59 | * |
59 | * |
60 | * Each cmd_info_t also has its own lock, which protects |
60 | * Each cmd_info_t also has its own lock, which protects |
61 | * all elements thereof except the link element. |
61 | * all elements thereof except the link element. |
62 | * |
62 | * |
63 | * cmd_lock must be acquired before any cmd_info lock. |
63 | * cmd_lock must be acquired before any cmd_info lock. |
64 | * When locking two cmd info structures, structure with |
64 | * When locking two cmd info structures, structure with |
65 | * lower address must be locked first. |
65 | * lower address must be locked first. |
66 | */ |
66 | */ |
67 | 67 | ||
68 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
68 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
69 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
69 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
70 | 70 | ||
71 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
71 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
72 | static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end); |
72 | static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end); |
73 | static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
73 | static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
74 | 74 | ||
75 | /** Initialize kconsole data structures. */ |
75 | /** Initialize kconsole data structures. */ |
76 | void kconsole_init(void) |
76 | void kconsole_init(void) |
77 | { |
77 | { |
78 | int i; |
78 | int i; |
79 | 79 | ||
80 | cmd_init(); |
80 | cmd_init(); |
81 | for (i=0; i<KCONSOLE_HISTORY; i++) |
81 | for (i=0; i<KCONSOLE_HISTORY; i++) |
82 | history[i][0] = '\0'; |
82 | history[i][0] = '\0'; |
83 | } |
83 | } |
84 | 84 | ||
85 | 85 | ||
86 | /** Register kconsole command. |
86 | /** Register kconsole command. |
87 | * |
87 | * |
88 | * @param cmd Structure describing the command. |
88 | * @param cmd Structure describing the command. |
89 | * |
89 | * |
90 | * @return 0 on failure, 1 on success. |
90 | * @return 0 on failure, 1 on success. |
91 | */ |
91 | */ |
92 | int cmd_register(cmd_info_t *cmd) |
92 | int cmd_register(cmd_info_t *cmd) |
93 | { |
93 | { |
94 | link_t *cur; |
94 | link_t *cur; |
95 | 95 | ||
96 | spinlock_lock(&cmd_lock); |
96 | spinlock_lock(&cmd_lock); |
97 | 97 | ||
98 | /* |
98 | /* |
99 | * Make sure the command is not already listed. |
99 | * Make sure the command is not already listed. |
100 | */ |
100 | */ |
101 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
101 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
102 | cmd_info_t *hlp; |
102 | cmd_info_t *hlp; |
103 | 103 | ||
104 | hlp = list_get_instance(cur, cmd_info_t, link); |
104 | hlp = list_get_instance(cur, cmd_info_t, link); |
105 | 105 | ||
106 | if (hlp == cmd) { |
106 | if (hlp == cmd) { |
107 | /* The command is already there. */ |
107 | /* The command is already there. */ |
108 | spinlock_unlock(&cmd_lock); |
108 | spinlock_unlock(&cmd_lock); |
109 | return 0; |
109 | return 0; |
110 | } |
110 | } |
111 | 111 | ||
112 | /* Avoid deadlock. */ |
112 | /* Avoid deadlock. */ |
113 | if (hlp < cmd) { |
113 | if (hlp < cmd) { |
114 | spinlock_lock(&hlp->lock); |
114 | spinlock_lock(&hlp->lock); |
115 | spinlock_lock(&cmd->lock); |
115 | spinlock_lock(&cmd->lock); |
116 | } else { |
116 | } else { |
117 | spinlock_lock(&cmd->lock); |
117 | spinlock_lock(&cmd->lock); |
118 | spinlock_lock(&hlp->lock); |
118 | spinlock_lock(&hlp->lock); |
119 | } |
119 | } |
120 | 120 | if ((strncmp(hlp->name, |
|
121 | if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) { |
121 | cmd->name, max(strlen(cmd->name), |
- | 122 | strlen(hlp->name))) == 0)) { |
|
122 | /* The command is already there. */ |
123 | /* The command is already there. */ |
123 | spinlock_unlock(&hlp->lock); |
124 | spinlock_unlock(&hlp->lock); |
124 | spinlock_unlock(&cmd->lock); |
125 | spinlock_unlock(&cmd->lock); |
125 | spinlock_unlock(&cmd_lock); |
126 | spinlock_unlock(&cmd_lock); |
126 | return 0; |
127 | return 0; |
127 | } |
128 | } |
128 | 129 | ||
129 | spinlock_unlock(&hlp->lock); |
130 | spinlock_unlock(&hlp->lock); |
130 | spinlock_unlock(&cmd->lock); |
131 | spinlock_unlock(&cmd->lock); |
131 | } |
132 | } |
132 | 133 | ||
133 | /* |
134 | /* |
134 | * Now the command can be added. |
135 | * Now the command can be added. |
135 | */ |
136 | */ |
136 | list_append(&cmd->link, &cmd_head); |
137 | list_append(&cmd->link, &cmd_head); |
137 | 138 | ||
138 | spinlock_unlock(&cmd_lock); |
139 | spinlock_unlock(&cmd_lock); |
139 | return 1; |
140 | return 1; |
140 | } |
141 | } |
141 | 142 | ||
- | 143 | /** Print count times a character */ |
|
142 | static void rdln_print_c(char ch, int count) |
144 | static void rdln_print_c(char ch, int count) |
143 | { |
145 | { |
144 | int i; |
146 | int i; |
145 | for (i=0;i<count;i++) |
147 | for (i=0;i<count;i++) |
146 | putchar(ch); |
148 | putchar(ch); |
147 | } |
149 | } |
148 | 150 | ||
- | 151 | /** Insert character to string */ |
|
149 | static void insert_char(char *str, char ch, int pos) |
152 | static void insert_char(char *str, char ch, int pos) |
150 | { |
153 | { |
151 | int i; |
154 | int i; |
152 | 155 | ||
153 | for (i=strlen(str);i > pos; i--) |
156 | for (i=strlen(str);i > pos; i--) |
154 | str[i] = str[i-1]; |
157 | str[i] = str[i-1]; |
155 | str[pos] = ch; |
158 | str[pos] = ch; |
156 | } |
159 | } |
157 | 160 | ||
- | 161 | /** Try to find a command begenning with prefix */ |
|
158 | static const char * cmdtab_search_one(const char *name,link_t **startpos) |
162 | static const char * cmdtab_search_one(const char *name,link_t **startpos) |
159 | { |
163 | { |
160 | int namelen = strlen(name); |
164 | int namelen = strlen(name); |
161 | const char *curname; |
165 | const char *curname; |
162 | 166 | ||
163 | spinlock_lock(&cmd_lock); |
167 | spinlock_lock(&cmd_lock); |
164 | 168 | ||
165 | if (!*startpos) |
169 | if (!*startpos) |
166 | *startpos = cmd_head.next; |
170 | *startpos = cmd_head.next; |
167 | 171 | ||
168 | for (;*startpos != &cmd_head;*startpos = (*startpos)->next) { |
172 | for (;*startpos != &cmd_head;*startpos = (*startpos)->next) { |
169 | cmd_info_t *hlp; |
173 | cmd_info_t *hlp; |
170 | hlp = list_get_instance(*startpos, cmd_info_t, link); |
174 | hlp = list_get_instance(*startpos, cmd_info_t, link); |
171 | 175 | ||
172 | curname = hlp->name; |
176 | curname = hlp->name; |
173 | if (strlen(curname) < namelen) |
177 | if (strlen(curname) < namelen) |
174 | continue; |
178 | continue; |
175 | if (strncmp(curname, name, namelen) == 0) { |
179 | if (strncmp(curname, name, namelen) == 0) { |
176 | spinlock_unlock(&cmd_lock); |
180 | spinlock_unlock(&cmd_lock); |
177 | return curname+namelen; |
181 | return curname+namelen; |
178 | } |
182 | } |
179 | } |
183 | } |
180 | spinlock_unlock(&cmd_lock); |
184 | spinlock_unlock(&cmd_lock); |
181 | return NULL; |
185 | return NULL; |
182 | } |
186 | } |
183 | 187 | ||
184 | 188 | ||
185 | /** Command completion of the commands |
189 | /** Command completion of the commands |
186 | * |
190 | * |
187 | * @param name - string to match, changed to hint on exit |
191 | * @param name - string to match, changed to hint on exit |
188 | * @return number of found matches |
192 | * @return number of found matches |
189 | */ |
193 | */ |
190 | static int cmdtab_compl(char *name) |
194 | static int cmdtab_compl(char *name) |
191 | { |
195 | { |
192 | char output[MAX_SYMBOL_NAME+1]; |
196 | char output[MAX_SYMBOL_NAME+1]; |
193 | link_t *startpos = NULL; |
197 | link_t *startpos = NULL; |
194 | const char *foundtxt; |
198 | const char *foundtxt; |
195 | int found = 0; |
199 | int found = 0; |
196 | int i; |
200 | int i; |
197 | 201 | ||
198 | output[0] = '\0'; |
202 | output[0] = '\0'; |
199 | while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
203 | while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
200 | startpos = startpos->next; |
204 | startpos = startpos->next; |
201 | if (!found) |
205 | if (!found) |
202 | strncpy(output, foundtxt, strlen(foundtxt)+1); |
206 | strncpy(output, foundtxt, strlen(foundtxt)+1); |
203 | else { |
207 | else { |
204 | for (i=0; output[i] && foundtxt[i] && output[i]==foundtxt[i]; i++) |
208 | for (i=0; output[i] && foundtxt[i] && output[i]==foundtxt[i]; i++) |
205 | ; |
209 | ; |
206 | output[i] = '\0'; |
210 | output[i] = '\0'; |
207 | } |
211 | } |
208 | found++; |
212 | found++; |
209 | } |
213 | } |
210 | if (!found) |
214 | if (!found) |
211 | return 0; |
215 | return 0; |
212 | 216 | ||
213 | if (found > 1 && !strlen(output)) { |
217 | if (found > 1 && !strlen(output)) { |
214 | printf("\n"); |
218 | printf("\n"); |
215 | startpos = NULL; |
219 | startpos = NULL; |
216 | while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
220 | while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
217 | cmd_info_t *hlp; |
221 | cmd_info_t *hlp; |
218 | hlp = list_get_instance(startpos, cmd_info_t, link); |
222 | hlp = list_get_instance(startpos, cmd_info_t, link); |
219 | printf("%s - %s\n", hlp->name, hlp->description); |
223 | printf("%s - %s\n", hlp->name, hlp->description); |
220 | startpos = startpos->next; |
224 | startpos = startpos->next; |
221 | } |
225 | } |
222 | } |
226 | } |
223 | strncpy(name, output, MAX_SYMBOL_NAME); |
227 | strncpy(name, output, MAX_SYMBOL_NAME); |
224 | return found; |
228 | return found; |
225 | 229 | ||
226 | } |
230 | } |
227 | 231 | ||
228 | static char * clever_readline(const char *prompt, chardev_t *input) |
232 | static char * clever_readline(const char *prompt, chardev_t *input) |
229 | { |
233 | { |
230 | static int histposition = 0; |
234 | static int histposition = 0; |
231 | 235 | ||
232 | char tmp[MAX_CMDLINE+1]; |
236 | char tmp[MAX_CMDLINE+1]; |
233 | int curlen = 0, position = 0; |
237 | int curlen = 0, position = 0; |
234 | char *current = history[histposition]; |
238 | char *current = history[histposition]; |
235 | int i; |
239 | int i; |
236 | char mod; /* Command Modifier */ |
240 | char mod; /* Command Modifier */ |
237 | char c; |
241 | char c; |
238 | 242 | ||
239 | printf("%s> ", prompt); |
243 | printf("%s> ", prompt); |
240 | while (1) { |
244 | while (1) { |
241 | c = _getc(input); |
245 | c = _getc(input); |
242 | if (c == '\n') { |
246 | if (c == '\n') { |
243 | putchar(c); |
247 | putchar(c); |
244 | break; |
248 | break; |
245 | } if (c == '\b') { /* Backspace */ |
249 | } if (c == '\b') { /* Backspace */ |
246 | if (position == 0) |
250 | if (position == 0) |
247 | continue; |
251 | continue; |
248 | for (i=position; i<curlen;i++) |
252 | for (i=position; i<curlen;i++) |
249 | current[i-1] = current[i]; |
253 | current[i-1] = current[i]; |
250 | curlen--; |
254 | curlen--; |
251 | position--; |
255 | position--; |
252 | putchar('\b'); |
256 | putchar('\b'); |
253 | for (i=position;i<curlen;i++) |
257 | for (i=position;i<curlen;i++) |
254 | putchar(current[i]); |
258 | putchar(current[i]); |
255 | putchar(' '); |
259 | putchar(' '); |
256 | rdln_print_c('\b',curlen-position+1); |
260 | rdln_print_c('\b',curlen-position+1); |
257 | continue; |
261 | continue; |
258 | } |
262 | } |
259 | if (c == '\t') { /* Tabulator */ |
263 | if (c == '\t') { /* Tabulator */ |
260 | int found; |
264 | int found; |
261 | 265 | ||
262 | /* Move to the end of the word */ |
266 | /* Move to the end of the word */ |
263 | for (;position<curlen && current[position]!=' ';position++) |
267 | for (;position<curlen && current[position]!=' ';position++) |
264 | putchar(current[position]); |
268 | putchar(current[position]); |
265 | /* Copy to tmp last word */ |
269 | /* Copy to tmp last word */ |
266 | for (i=position-1;i >= 0 && current[i]!=' ' ;i--) |
270 | for (i=position-1;i >= 0 && current[i]!=' ' ;i--) |
267 | ; |
271 | ; |
268 | /* If word begins with * or &, skip it */ |
272 | /* If word begins with * or &, skip it */ |
269 | if (tmp[0] == '*' || tmp[0] == '&') |
273 | if (tmp[0] == '*' || tmp[0] == '&') |
270 | for (i=1;tmp[i];i++) |
274 | for (i=1;tmp[i];i++) |
271 | tmp[i-1] = tmp[i]; |
275 | tmp[i-1] = tmp[i]; |
272 | i++; /* I is at the start of the word */ |
276 | i++; /* I is at the start of the word */ |
273 | strncpy(tmp, current+i, position-i+1); |
277 | strncpy(tmp, current+i, position-i+1); |
274 | 278 | ||
275 | if (i==0) { /* Command completion */ |
279 | if (i==0) { /* Command completion */ |
276 | found = cmdtab_compl(tmp); |
280 | found = cmdtab_compl(tmp); |
277 | } else { /* Symtab completion */ |
281 | } else { /* Symtab completion */ |
278 | found = symtab_compl(tmp); |
282 | found = symtab_compl(tmp); |
279 | } |
283 | } |
280 | 284 | ||
281 | if (found == 0) |
285 | if (found == 0) |
282 | continue; |
286 | continue; |
283 | for (i=0;tmp[i] && curlen < MAX_CMDLINE;i++,curlen++) |
287 | for (i=0;tmp[i] && curlen < MAX_CMDLINE;i++,curlen++) |
284 | insert_char(current, tmp[i], i+position); |
288 | insert_char(current, tmp[i], i+position); |
285 | 289 | ||
286 | if (strlen(tmp) || found==1) { /* If we have a hint */ |
290 | if (strlen(tmp) || found==1) { /* If we have a hint */ |
287 | for (i=position;i<curlen;i++) |
291 | for (i=position;i<curlen;i++) |
288 | putchar(current[i]); |
292 | putchar(current[i]); |
289 | position += strlen(tmp); |
293 | position += strlen(tmp); |
290 | /* Add space to end */ |
294 | /* Add space to end */ |
291 | if (found == 1 && position == curlen && \ |
295 | if (found == 1 && position == curlen && \ |
292 | curlen < MAX_CMDLINE) { |
296 | curlen < MAX_CMDLINE) { |
293 | current[position] = ' '; |
297 | current[position] = ' '; |
294 | curlen++; |
298 | curlen++; |
295 | position++; |
299 | position++; |
296 | putchar(' '); |
300 | putchar(' '); |
297 | } |
301 | } |
298 | } else { /* No hint, table was printed */ |
302 | } else { /* No hint, table was printed */ |
299 | printf("%s> ", prompt); |
303 | printf("%s> ", prompt); |
300 | for (i=0; i<curlen;i++) |
304 | for (i=0; i<curlen;i++) |
301 | putchar(current[i]); |
305 | putchar(current[i]); |
302 | position += strlen(tmp); |
306 | position += strlen(tmp); |
303 | } |
307 | } |
304 | rdln_print_c('\b', curlen-position); |
308 | rdln_print_c('\b', curlen-position); |
305 | continue; |
309 | continue; |
306 | } |
310 | } |
307 | if (c == 0x1b) { /* Special command */ |
311 | if (c == 0x1b) { /* Special command */ |
308 | mod = _getc(input); |
312 | mod = _getc(input); |
309 | c = _getc(input); |
313 | c = _getc(input); |
310 | 314 | ||
311 | if (mod != 0x5b && mod != 0x4f) |
315 | if (mod != 0x5b && mod != 0x4f) |
312 | continue; |
316 | continue; |
313 | 317 | ||
314 | if (c == 0x33 && _getc(input) == 0x7e) { |
318 | if (c == 0x33 && _getc(input) == 0x7e) { |
315 | /* Delete */ |
319 | /* Delete */ |
316 | if (position == curlen) |
320 | if (position == curlen) |
317 | continue; |
321 | continue; |
318 | for (i=position+1; i<curlen;i++) { |
322 | for (i=position+1; i<curlen;i++) { |
319 | putchar(current[i]); |
323 | putchar(current[i]); |
320 | current[i-1] = current[i]; |
324 | current[i-1] = current[i]; |
321 | } |
325 | } |
322 | putchar(' '); |
326 | putchar(' '); |
323 | rdln_print_c('\b',curlen-position); |
327 | rdln_print_c('\b',curlen-position); |
324 | curlen--; |
328 | curlen--; |
325 | } |
329 | } |
326 | else if (c == 0x48) { /* Home */ |
330 | else if (c == 0x48) { /* Home */ |
327 | rdln_print_c('\b',position); |
331 | rdln_print_c('\b',position); |
328 | position = 0; |
332 | position = 0; |
329 | } |
333 | } |
330 | else if (c == 0x46) { /* End */ |
334 | else if (c == 0x46) { /* End */ |
331 | for (i=position;i<curlen;i++) |
335 | for (i=position;i<curlen;i++) |
332 | putchar(current[i]); |
336 | putchar(current[i]); |
333 | position = curlen; |
337 | position = curlen; |
334 | } |
338 | } |
335 | else if (c == 0x44) { /* Left */ |
339 | else if (c == 0x44) { /* Left */ |
336 | if (position > 0) { |
340 | if (position > 0) { |
337 | putchar('\b'); |
341 | putchar('\b'); |
338 | position--; |
342 | position--; |
339 | } |
343 | } |
340 | continue; |
344 | continue; |
341 | } |
345 | } |
342 | else if (c == 0x43) { /* Right */ |
346 | else if (c == 0x43) { /* Right */ |
343 | if (position < curlen) { |
347 | if (position < curlen) { |
344 | putchar(current[position]); |
348 | putchar(current[position]); |
345 | position++; |
349 | position++; |
346 | } |
350 | } |
347 | continue; |
351 | continue; |
348 | } |
352 | } |
349 | else if (c == 0x41 || c == 0x42) { |
353 | else if (c == 0x41 || c == 0x42) { |
350 | /* Up,down */ |
354 | /* Up,down */ |
351 | rdln_print_c('\b',position); |
355 | rdln_print_c('\b',position); |
352 | rdln_print_c(' ',curlen); |
356 | rdln_print_c(' ',curlen); |
353 | rdln_print_c('\b',curlen); |
357 | rdln_print_c('\b',curlen); |
354 | if (c == 0x41) /* Up */ |
358 | if (c == 0x41) /* Up */ |
355 | histposition--; |
359 | histposition--; |
356 | else |
360 | else |
357 | histposition++; |
361 | histposition++; |
358 | if (histposition < 0) |
362 | if (histposition < 0) |
359 | histposition = KCONSOLE_HISTORY -1 ; |
363 | histposition = KCONSOLE_HISTORY -1 ; |
360 | else |
364 | else |
361 | histposition = histposition % KCONSOLE_HISTORY; |
365 | histposition = histposition % KCONSOLE_HISTORY; |
362 | current = history[histposition]; |
366 | current = history[histposition]; |
363 | printf("%s", current); |
367 | printf("%s", current); |
364 | curlen = strlen(current); |
368 | curlen = strlen(current); |
365 | position = curlen; |
369 | position = curlen; |
366 | continue; |
370 | continue; |
367 | } |
371 | } |
368 | continue; |
372 | continue; |
369 | } |
373 | } |
370 | if (curlen >= MAX_CMDLINE) |
374 | if (curlen >= MAX_CMDLINE) |
371 | continue; |
375 | continue; |
372 | 376 | ||
373 | insert_char(current, c, position); |
377 | insert_char(current, c, position); |
374 | 378 | ||
375 | curlen++; |
379 | curlen++; |
376 | for (i=position;i<curlen;i++) |
380 | for (i=position;i<curlen;i++) |
377 | putchar(current[i]); |
381 | putchar(current[i]); |
378 | position++; |
382 | position++; |
379 | rdln_print_c('\b',curlen-position); |
383 | rdln_print_c('\b',curlen-position); |
380 | } |
384 | } |
381 | if (curlen) { |
385 | if (curlen) { |
382 | histposition++; |
386 | histposition++; |
383 | histposition = histposition % KCONSOLE_HISTORY; |
387 | histposition = histposition % KCONSOLE_HISTORY; |
384 | } |
388 | } |
385 | current[curlen] = '\0'; |
389 | current[curlen] = '\0'; |
386 | return current; |
390 | return current; |
387 | } |
391 | } |
388 | 392 | ||
389 | /** Kernel console managing thread. |
393 | /** Kernel console managing thread. |
390 | * |
394 | * |
391 | * @param arg Not used. |
395 | * @param arg Not used. |
392 | */ |
396 | */ |
393 | void kconsole(void *prompt) |
397 | void kconsole(void *prompt) |
394 | { |
398 | { |
395 | cmd_info_t *cmd_info; |
399 | cmd_info_t *cmd_info; |
396 | count_t len; |
400 | count_t len; |
397 | char *cmdline; |
401 | char *cmdline; |
398 | 402 | ||
399 | if (!stdin) { |
403 | if (!stdin) { |
400 | printf("%s: no stdin\n", __FUNCTION__); |
404 | printf("%s: no stdin\n", __FUNCTION__); |
401 | return; |
405 | return; |
402 | } |
406 | } |
403 | 407 | ||
404 | while (true) { |
408 | while (true) { |
405 | cmdline = clever_readline(prompt, stdin); |
409 | cmdline = clever_readline(prompt, stdin); |
406 | len = strlen(cmdline); |
410 | len = strlen(cmdline); |
407 | if (!len) |
411 | if (!len) |
408 | continue; |
412 | continue; |
409 | cmd_info = parse_cmdline(cmdline, len); |
413 | cmd_info = parse_cmdline(cmdline, len); |
410 | if (!cmd_info) |
414 | if (!cmd_info) |
411 | continue; |
415 | continue; |
412 | if (strncmp(cmd_info->name,"exit", \ |
416 | if (strncmp(cmd_info->name,"exit", \ |
413 | min(strlen(cmd_info->name),5)) == 0) |
417 | min(strlen(cmd_info->name),5)) == 0) |
414 | break; |
418 | break; |
415 | (void) cmd_info->func(cmd_info->argv); |
419 | (void) cmd_info->func(cmd_info->argv); |
416 | } |
420 | } |
417 | } |
421 | } |
418 | 422 | ||
419 | static int parse_int_arg(char *text, size_t len, __native *result) |
423 | static int parse_int_arg(char *text, size_t len, __native *result) |
420 | { |
424 | { |
421 | char symname[MAX_SYMBOL_NAME]; |
425 | char symname[MAX_SYMBOL_NAME]; |
422 | __address symaddr; |
426 | __address symaddr; |
423 | bool isaddr = false; |
427 | bool isaddr = false; |
424 | bool isptr = false; |
428 | bool isptr = false; |
425 | 429 | ||
426 | /* If we get a name, try to find it in symbol table */ |
430 | /* If we get a name, try to find it in symbol table */ |
427 | if (text[0] < '0' || text[0] > '9') { |
431 | if (text[0] < '0' || text[0] > '9') { |
428 | if (text[0] == '&') { |
432 | if (text[0] == '&') { |
429 | isaddr = true; |
433 | isaddr = true; |
430 | text++;len--; |
434 | text++;len--; |
431 | } else if (text[0] == '*') { |
435 | } else if (text[0] == '*') { |
432 | isptr = true; |
436 | isptr = true; |
433 | text++;len--; |
437 | text++;len--; |
434 | } |
438 | } |
435 | strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME)); |
439 | strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME)); |
436 | symaddr = get_symbol_addr(symname); |
440 | symaddr = get_symbol_addr(symname); |
437 | if (!symaddr) { |
441 | if (!symaddr) { |
438 | printf("Symbol %s not found.\n",symname); |
442 | printf("Symbol %s not found.\n",symname); |
439 | return -1; |
443 | return -1; |
440 | } |
444 | } |
441 | if (symaddr == (__address) -1) { |
445 | if (symaddr == (__address) -1) { |
442 | printf("Duplicate symbol %s.\n",symname); |
446 | printf("Duplicate symbol %s.\n",symname); |
443 | symtab_print_search(symname); |
447 | symtab_print_search(symname); |
444 | return -1; |
448 | return -1; |
445 | } |
449 | } |
446 | if (isaddr) |
450 | if (isaddr) |
447 | *result = (__native)symaddr; |
451 | *result = (__native)symaddr; |
448 | else if (isptr) |
452 | else if (isptr) |
449 | *result = **((__native **)symaddr); |
453 | *result = **((__native **)symaddr); |
450 | else |
454 | else |
451 | *result = *((__native *)symaddr); |
455 | *result = *((__native *)symaddr); |
452 | } else /* It's a number - convert it */ |
456 | } else /* It's a number - convert it */ |
453 | *result = atoi(text); |
457 | *result = atoi(text); |
454 | return 0; |
458 | return 0; |
455 | } |
459 | } |
456 | 460 | ||
457 | /** Parse command line. |
461 | /** Parse command line. |
458 | * |
462 | * |
459 | * @param cmdline Command line as read from input device. |
463 | * @param cmdline Command line as read from input device. |
460 | * @param len Command line length. |
464 | * @param len Command line length. |
461 | * |
465 | * |
462 | * @return Structure describing the command. |
466 | * @return Structure describing the command. |
463 | */ |
467 | */ |
464 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
468 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
465 | { |
469 | { |
466 | index_t start = 0, end = 0; |
470 | index_t start = 0, end = 0; |
467 | cmd_info_t *cmd = NULL; |
471 | cmd_info_t *cmd = NULL; |
468 | link_t *cur; |
472 | link_t *cur; |
469 | int i; |
473 | int i; |
470 | 474 | ||
471 | if (!parse_argument(cmdline, len, &start, &end)) { |
475 | if (!parse_argument(cmdline, len, &start, &end)) { |
472 | /* Command line did not contain alphanumeric word. */ |
476 | /* Command line did not contain alphanumeric word. */ |
473 | return NULL; |
477 | return NULL; |
474 | } |
478 | } |
475 | 479 | ||
476 | spinlock_lock(&cmd_lock); |
480 | spinlock_lock(&cmd_lock); |
477 | 481 | ||
478 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
482 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
479 | cmd_info_t *hlp; |
483 | cmd_info_t *hlp; |
480 | 484 | ||
481 | hlp = list_get_instance(cur, cmd_info_t, link); |
485 | hlp = list_get_instance(cur, cmd_info_t, link); |
482 | spinlock_lock(&hlp->lock); |
486 | spinlock_lock(&hlp->lock); |
483 | 487 | ||
484 | if (strncmp(hlp->name, &cmdline[start], strlen(hlp->name)) == 0) { |
488 | if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name), |
- | 489 | end-start+1)) == 0) { |
|
485 | cmd = hlp; |
490 | cmd = hlp; |
486 | break; |
491 | break; |
487 | } |
492 | } |
488 | 493 | ||
489 | spinlock_unlock(&hlp->lock); |
494 | spinlock_unlock(&hlp->lock); |
490 | } |
495 | } |
491 | 496 | ||
492 | spinlock_unlock(&cmd_lock); |
497 | spinlock_unlock(&cmd_lock); |
493 | 498 | ||
494 | if (!cmd) { |
499 | if (!cmd) { |
495 | /* Unknown command. */ |
500 | /* Unknown command. */ |
496 | printf("Unknown command.\n"); |
501 | printf("Unknown command.\n"); |
497 | return NULL; |
502 | return NULL; |
498 | } |
503 | } |
499 | 504 | ||
500 | /* cmd == hlp is locked */ |
505 | /* cmd == hlp is locked */ |
501 | 506 | ||
502 | /* |
507 | /* |
503 | * The command line must be further analyzed and |
508 | * The command line must be further analyzed and |
504 | * the parameters therefrom must be matched and |
509 | * the parameters therefrom must be matched and |
505 | * converted to those specified in the cmd info |
510 | * converted to those specified in the cmd info |
506 | * structure. |
511 | * structure. |
507 | */ |
512 | */ |
508 | 513 | ||
509 | for (i = 0; i < cmd->argc; i++) { |
514 | for (i = 0; i < cmd->argc; i++) { |
510 | char *buf; |
515 | char *buf; |
511 | start = end + 1; |
516 | start = end + 1; |
512 | if (!parse_argument(cmdline, len, &start, &end)) { |
517 | if (!parse_argument(cmdline, len, &start, &end)) { |
513 | printf("Too few arguments.\n"); |
518 | printf("Too few arguments.\n"); |
514 | spinlock_unlock(&cmd->lock); |
519 | spinlock_unlock(&cmd->lock); |
515 | return NULL; |
520 | return NULL; |
516 | } |
521 | } |
517 | 522 | ||
518 | switch (cmd->argv[i].type) { |
523 | switch (cmd->argv[i].type) { |
519 | case ARG_TYPE_STRING: |
524 | case ARG_TYPE_STRING: |
520 | buf = cmd->argv[i].buffer; |
525 | buf = cmd->argv[i].buffer; |
521 | strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len)); |
526 | strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len)); |
522 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; |
527 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; |
523 | break; |
528 | break; |
524 | case ARG_TYPE_INT: |
529 | case ARG_TYPE_INT: |
525 | if (parse_int_arg(cmdline+start, end-start+1, |
530 | if (parse_int_arg(cmdline+start, end-start+1, |
526 | &cmd->argv[i].intval)) |
531 | &cmd->argv[i].intval)) |
527 | return NULL; |
532 | return NULL; |
528 | break; |
533 | break; |
529 | case ARG_TYPE_VAR: |
534 | case ARG_TYPE_VAR: |
530 | if (start != end && cmdline[start] == '"' && cmdline[end] == '"') { |
535 | if (start != end && cmdline[start] == '"' && cmdline[end] == '"') { |
531 | buf = cmd->argv[i].buffer; |
536 | buf = cmd->argv[i].buffer; |
532 | strncpy(buf, (const char *) &cmdline[start+1], |
537 | strncpy(buf, (const char *) &cmdline[start+1], |
533 | min((end-start), cmd->argv[i].len)); |
538 | min((end-start), cmd->argv[i].len)); |
534 | buf[min((end - start), cmd->argv[i].len - 1)] = '\0'; |
539 | buf[min((end - start), cmd->argv[i].len - 1)] = '\0'; |
535 | cmd->argv[i].intval = (__native) buf; |
540 | cmd->argv[i].intval = (__native) buf; |
536 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
541 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
537 | } else if (!parse_int_arg(cmdline+start, end-start+1, |
542 | } else if (!parse_int_arg(cmdline+start, end-start+1, |
538 | &cmd->argv[i].intval)) |
543 | &cmd->argv[i].intval)) |
539 | cmd->argv[i].vartype = ARG_TYPE_INT; |
544 | cmd->argv[i].vartype = ARG_TYPE_INT; |
540 | else { |
545 | else { |
541 | printf("Unrecognized variable argument.\n"); |
546 | printf("Unrecognized variable argument.\n"); |
542 | return NULL; |
547 | return NULL; |
543 | } |
548 | } |
544 | break; |
549 | break; |
545 | case ARG_TYPE_INVALID: |
550 | case ARG_TYPE_INVALID: |
546 | default: |
551 | default: |
547 | printf("invalid argument type\n"); |
552 | printf("invalid argument type\n"); |
548 | return NULL; |
553 | return NULL; |
549 | break; |
554 | break; |
550 | } |
555 | } |
551 | } |
556 | } |
552 | 557 | ||
553 | start = end + 1; |
558 | start = end + 1; |
554 | if (parse_argument(cmdline, len, &start, &end)) { |
559 | if (parse_argument(cmdline, len, &start, &end)) { |
555 | printf("Too many arguments.\n"); |
560 | printf("Too many arguments.\n"); |
556 | spinlock_unlock(&cmd->lock); |
561 | spinlock_unlock(&cmd->lock); |
557 | return NULL; |
562 | return NULL; |
558 | } |
563 | } |
559 | 564 | ||
560 | spinlock_unlock(&cmd->lock); |
565 | spinlock_unlock(&cmd->lock); |
561 | return cmd; |
566 | return cmd; |
562 | } |
567 | } |
563 | 568 | ||
564 | /** Parse argument. |
569 | /** Parse argument. |
565 | * |
570 | * |
566 | * Find start and end positions of command line argument. |
571 | * Find start and end positions of command line argument. |
567 | * |
572 | * |
568 | * @param cmdline Command line as read from the input device. |
573 | * @param cmdline Command line as read from the input device. |
569 | * @param len Number of characters in cmdline. |
574 | * @param len Number of characters in cmdline. |
570 | * @param start On entry, 'start' contains pointer to the index |
575 | * @param start On entry, 'start' contains pointer to the index |
571 | * of first unprocessed character of cmdline. |
576 | * of first unprocessed character of cmdline. |
572 | * On successful exit, it marks beginning of the next argument. |
577 | * On successful exit, it marks beginning of the next argument. |
573 | * @param end Undefined on entry. On exit, 'end' points to the last character |
578 | * @param end Undefined on entry. On exit, 'end' points to the last character |
574 | * of the next argument. |
579 | * of the next argument. |
575 | * |
580 | * |
576 | * @return false on failure, true on success. |
581 | * @return false on failure, true on success. |
577 | */ |
582 | */ |
578 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
583 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
579 | { |
584 | { |
580 | int i; |
585 | int i; |
581 | bool found_start = false; |
586 | bool found_start = false; |
582 | 587 | ||
583 | ASSERT(start != NULL); |
588 | ASSERT(start != NULL); |
584 | ASSERT(end != NULL); |
589 | ASSERT(end != NULL); |
585 | 590 | ||
586 | for (i = *start; i < len; i++) { |
591 | for (i = *start; i < len; i++) { |
587 | if (!found_start) { |
592 | if (!found_start) { |
588 | if (is_white(cmdline[i])) |
593 | if (is_white(cmdline[i])) |
589 | (*start)++; |
594 | (*start)++; |
590 | else |
595 | else |
591 | found_start = true; |
596 | found_start = true; |
592 | } else { |
597 | } else { |
593 | if (is_white(cmdline[i])) |
598 | if (is_white(cmdline[i])) |
594 | break; |
599 | break; |
595 | } |
600 | } |
596 | } |
601 | } |
597 | *end = i - 1; |
602 | *end = i - 1; |
598 | 603 | ||
599 | return found_start; |
604 | return found_start; |
600 | } |
605 | } |
601 | 606 |