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