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