Rev 2089 | Rev 2113 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2089 | Rev 2108 | ||
---|---|---|---|
Line 77... | Line 77... | ||
77 | 77 | ||
78 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
78 | SPINLOCK_INITIALIZE(cmd_lock); /**< Lock protecting command list. */ |
79 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
79 | LIST_INITIALIZE(cmd_head); /**< Command list. */ |
80 | 80 | ||
81 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
81 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
82 | static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end); |
82 | static bool parse_argument(char *cmdline, size_t len, index_t *start, |
- | 83 | index_t *end); |
|
83 | static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
84 | static char history[KCONSOLE_HISTORY][MAX_CMDLINE] = {}; |
84 | 85 | ||
85 | /** Initialize kconsole data structures. */ |
86 | /** Initialize kconsole data structures. */ |
86 | void kconsole_init(void) |
87 | void kconsole_init(void) |
87 | { |
88 | { |
88 | int i; |
89 | int i; |
89 | 90 | ||
90 | cmd_init(); |
91 | cmd_init(); |
91 | for (i=0; i<KCONSOLE_HISTORY; i++) |
92 | for (i = 0; i < KCONSOLE_HISTORY; i++) |
92 | history[i][0] = '\0'; |
93 | history[i][0] = '\0'; |
93 | } |
94 | } |
94 | 95 | ||
95 | 96 | ||
96 | /** Register kconsole command. |
97 | /** Register kconsole command. |
Line 125... | Line 126... | ||
125 | spinlock_lock(&cmd->lock); |
126 | spinlock_lock(&cmd->lock); |
126 | } else { |
127 | } else { |
127 | spinlock_lock(&cmd->lock); |
128 | spinlock_lock(&cmd->lock); |
128 | spinlock_lock(&hlp->lock); |
129 | spinlock_lock(&hlp->lock); |
129 | } |
130 | } |
130 | if ((strncmp(hlp->name, |
- | |
131 | cmd->name, max(strlen(cmd->name), |
131 | if ((strncmp(hlp->name, cmd->name, max(strlen(cmd->name), |
132 | strlen(hlp->name))) == 0)) { |
132 | strlen(hlp->name))) == 0)) { |
133 | /* The command is already there. */ |
133 | /* The command is already there. */ |
134 | spinlock_unlock(&hlp->lock); |
134 | spinlock_unlock(&hlp->lock); |
135 | spinlock_unlock(&cmd->lock); |
135 | spinlock_unlock(&cmd->lock); |
136 | spinlock_unlock(&cmd_lock); |
136 | spinlock_unlock(&cmd_lock); |
137 | return 0; |
137 | return 0; |
Line 152... | Line 152... | ||
152 | 152 | ||
153 | /** Print count times a character */ |
153 | /** Print count times a character */ |
154 | static void rdln_print_c(char ch, int count) |
154 | static void rdln_print_c(char ch, int count) |
155 | { |
155 | { |
156 | int i; |
156 | int i; |
157 | for (i=0;i<count;i++) |
157 | for (i = 0; i < count; i++) |
158 | putchar(ch); |
158 | putchar(ch); |
159 | } |
159 | } |
160 | 160 | ||
161 | /** Insert character to string */ |
161 | /** Insert character to string */ |
162 | static void insert_char(char *str, char ch, int pos) |
162 | static void insert_char(char *str, char ch, int pos) |
163 | { |
163 | { |
164 | int i; |
164 | int i; |
165 | 165 | ||
166 | for (i=strlen(str);i > pos; i--) |
166 | for (i = strlen(str); i > pos; i--) |
167 | str[i] = str[i-1]; |
167 | str[i] = str[i - 1]; |
168 | str[pos] = ch; |
168 | str[pos] = ch; |
169 | } |
169 | } |
170 | 170 | ||
171 | /** Try to find a command beginning with prefix */ |
171 | /** Try to find a command beginning with prefix */ |
172 | static const char * cmdtab_search_one(const char *name,link_t **startpos) |
172 | static const char * cmdtab_search_one(const char *name,link_t **startpos) |
Line 177... | Line 177... | ||
177 | spinlock_lock(&cmd_lock); |
177 | spinlock_lock(&cmd_lock); |
178 | 178 | ||
179 | if (!*startpos) |
179 | if (!*startpos) |
180 | *startpos = cmd_head.next; |
180 | *startpos = cmd_head.next; |
181 | 181 | ||
182 | for (;*startpos != &cmd_head;*startpos = (*startpos)->next) { |
182 | for (; *startpos != &cmd_head; *startpos = (*startpos)->next) { |
183 | cmd_info_t *hlp; |
183 | cmd_info_t *hlp; |
184 | hlp = list_get_instance(*startpos, cmd_info_t, link); |
184 | hlp = list_get_instance(*startpos, cmd_info_t, link); |
185 | 185 | ||
186 | curname = hlp->name; |
186 | curname = hlp->name; |
187 | if (strlen(curname) < namelen) |
187 | if (strlen(curname) < namelen) |
Line 213... | Line 213... | ||
213 | while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
213 | while ((foundtxt = cmdtab_search_one(name, &startpos))) { |
214 | startpos = startpos->next; |
214 | startpos = startpos->next; |
215 | if (!found) |
215 | if (!found) |
216 | strncpy(output, foundtxt, strlen(foundtxt)+1); |
216 | strncpy(output, foundtxt, strlen(foundtxt)+1); |
217 | else { |
217 | else { |
218 | for (i=0; output[i] && foundtxt[i] && output[i]==foundtxt[i]; i++) |
218 | for (i = 0; output[i] && foundtxt[i] && |
- | 219 | output[i] == foundtxt[i]; i++) |
|
219 | ; |
220 | ; |
220 | output[i] = '\0'; |
221 | output[i] = '\0'; |
221 | } |
222 | } |
222 | found++; |
223 | found++; |
223 | } |
224 | } |
Line 257... | Line 258... | ||
257 | putchar(c); |
258 | putchar(c); |
258 | break; |
259 | break; |
259 | } if (c == '\b') { /* Backspace */ |
260 | } if (c == '\b') { /* Backspace */ |
260 | if (position == 0) |
261 | if (position == 0) |
261 | continue; |
262 | continue; |
262 | for (i=position; i<curlen;i++) |
263 | for (i = position; i < curlen; i++) |
263 | current[i-1] = current[i]; |
264 | current[i - 1] = current[i]; |
264 | curlen--; |
265 | curlen--; |
265 | position--; |
266 | position--; |
266 | putchar('\b'); |
267 | putchar('\b'); |
267 | for (i=position;i<curlen;i++) |
268 | for (i = position; i < curlen; i++) |
268 | putchar(current[i]); |
269 | putchar(current[i]); |
269 | putchar(' '); |
270 | putchar(' '); |
270 | rdln_print_c('\b',curlen-position+1); |
271 | rdln_print_c('\b', curlen - position + 1); |
271 | continue; |
272 | continue; |
272 | } |
273 | } |
273 | if (c == '\t') { /* Tabulator */ |
274 | if (c == '\t') { /* Tabulator */ |
274 | int found; |
275 | int found; |
275 | 276 | ||
276 | /* Move to the end of the word */ |
277 | /* Move to the end of the word */ |
277 | for (;position<curlen && current[position]!=' ';position++) |
278 | for (; position < curlen && current[position] != ' '; |
- | 279 | position++) |
|
278 | putchar(current[position]); |
280 | putchar(current[position]); |
279 | /* Copy to tmp last word */ |
281 | /* Copy to tmp last word */ |
280 | for (i=position-1;i >= 0 && current[i]!=' ' ;i--) |
282 | for (i = position - 1; i >= 0 && current[i] != ' '; i--) |
281 | ; |
283 | ; |
282 | /* If word begins with * or &, skip it */ |
284 | /* If word begins with * or &, skip it */ |
283 | if (tmp[0] == '*' || tmp[0] == '&') |
285 | if (tmp[0] == '*' || tmp[0] == '&') |
284 | for (i=1;tmp[i];i++) |
286 | for (i = 1; tmp[i]; i++) |
285 | tmp[i-1] = tmp[i]; |
287 | tmp[i - 1] = tmp[i]; |
286 | i++; /* I is at the start of the word */ |
288 | i++; /* I is at the start of the word */ |
287 | strncpy(tmp, current+i, position-i+1); |
289 | strncpy(tmp, current + i, position - i + 1); |
288 | 290 | ||
289 | if (i==0) { /* Command completion */ |
291 | if (i == 0) { /* Command completion */ |
290 | found = cmdtab_compl(tmp); |
292 | found = cmdtab_compl(tmp); |
291 | } else { /* Symtab completion */ |
293 | } else { /* Symtab completion */ |
292 | found = symtab_compl(tmp); |
294 | found = symtab_compl(tmp); |
293 | } |
295 | } |
294 | 296 | ||
295 | if (found == 0) |
297 | if (found == 0) |
296 | continue; |
298 | continue; |
297 | for (i=0;tmp[i] && curlen < MAX_CMDLINE;i++,curlen++) |
299 | for (i = 0; tmp[i] && curlen < MAX_CMDLINE; |
- | 300 | i++, curlen++) |
|
298 | insert_char(current, tmp[i], i+position); |
301 | insert_char(current, tmp[i], i + position); |
299 | 302 | ||
300 | if (strlen(tmp) || found==1) { /* If we have a hint */ |
303 | if (strlen(tmp) || found == 1) { /* If we have a hint */ |
301 | for (i=position;i<curlen;i++) |
304 | for (i = position; i < curlen; i++) |
302 | putchar(current[i]); |
305 | putchar(current[i]); |
303 | position += strlen(tmp); |
306 | position += strlen(tmp); |
304 | /* Add space to end */ |
307 | /* Add space to end */ |
305 | if (found == 1 && position == curlen && \ |
308 | if (found == 1 && position == curlen && |
306 | curlen < MAX_CMDLINE) { |
309 | curlen < MAX_CMDLINE) { |
307 | current[position] = ' '; |
310 | current[position] = ' '; |
308 | curlen++; |
311 | curlen++; |
309 | position++; |
312 | position++; |
310 | putchar(' '); |
313 | putchar(' '); |
311 | } |
314 | } |
312 | } else { /* No hint, table was printed */ |
315 | } else { /* No hint, table was printed */ |
313 | printf("%s> ", prompt); |
316 | printf("%s> ", prompt); |
314 | for (i=0; i<curlen;i++) |
317 | for (i = 0; i < curlen; i++) |
315 | putchar(current[i]); |
318 | putchar(current[i]); |
316 | position += strlen(tmp); |
319 | position += strlen(tmp); |
317 | } |
320 | } |
318 | rdln_print_c('\b', curlen-position); |
321 | rdln_print_c('\b', curlen - position); |
319 | continue; |
322 | continue; |
320 | } |
323 | } |
321 | if (c == 0x1b) { /* Special command */ |
324 | if (c == 0x1b) { /* Special command */ |
322 | mod = _getc(input); |
325 | mod = _getc(input); |
323 | c = _getc(input); |
326 | c = _getc(input); |
Line 327... | Line 330... | ||
327 | 330 | ||
328 | if (c == 0x33 && _getc(input) == 0x7e) { |
331 | if (c == 0x33 && _getc(input) == 0x7e) { |
329 | /* Delete */ |
332 | /* Delete */ |
330 | if (position == curlen) |
333 | if (position == curlen) |
331 | continue; |
334 | continue; |
332 | for (i=position+1; i<curlen;i++) { |
335 | for (i = position + 1; i < curlen; i++) { |
333 | putchar(current[i]); |
336 | putchar(current[i]); |
334 | current[i-1] = current[i]; |
337 | current[i - 1] = current[i]; |
335 | } |
338 | } |
336 | putchar(' '); |
339 | putchar(' '); |
337 | rdln_print_c('\b',curlen-position); |
340 | rdln_print_c('\b', curlen - position); |
338 | curlen--; |
341 | curlen--; |
339 | } |
- | |
340 | else if (c == 0x48) { /* Home */ |
342 | } else if (c == 0x48) { /* Home */ |
341 | rdln_print_c('\b',position); |
343 | rdln_print_c('\b', position); |
342 | position = 0; |
344 | position = 0; |
343 | } |
- | |
344 | else if (c == 0x46) { /* End */ |
345 | } else if (c == 0x46) { /* End */ |
345 | for (i=position;i<curlen;i++) |
346 | for (i = position; i < curlen; i++) |
346 | putchar(current[i]); |
347 | putchar(current[i]); |
347 | position = curlen; |
348 | position = curlen; |
348 | } |
- | |
349 | else if (c == 0x44) { /* Left */ |
349 | } else if (c == 0x44) { /* Left */ |
350 | if (position > 0) { |
350 | if (position > 0) { |
351 | putchar('\b'); |
351 | putchar('\b'); |
352 | position--; |
352 | position--; |
353 | } |
353 | } |
354 | continue; |
354 | continue; |
355 | } |
- | |
356 | else if (c == 0x43) { /* Right */ |
355 | } else if (c == 0x43) { /* Right */ |
357 | if (position < curlen) { |
356 | if (position < curlen) { |
358 | putchar(current[position]); |
357 | putchar(current[position]); |
359 | position++; |
358 | position++; |
360 | } |
359 | } |
361 | continue; |
360 | continue; |
362 | } |
- | |
363 | else if (c == 0x41 || c == 0x42) { |
361 | } else if (c == 0x41 || c == 0x42) { |
364 | /* Up,down */ |
362 | /* Up, down */ |
365 | rdln_print_c('\b',position); |
363 | rdln_print_c('\b', position); |
366 | rdln_print_c(' ',curlen); |
364 | rdln_print_c(' ', curlen); |
367 | rdln_print_c('\b',curlen); |
365 | rdln_print_c('\b', curlen); |
368 | if (c == 0x41) /* Up */ |
366 | if (c == 0x41) /* Up */ |
369 | histposition--; |
367 | histposition--; |
370 | else |
368 | else |
371 | histposition++; |
369 | histposition++; |
372 | if (histposition < 0) |
370 | if (histposition < 0) { |
373 | histposition = KCONSOLE_HISTORY -1 ; |
371 | histposition = KCONSOLE_HISTORY - 1; |
374 | else |
372 | } else { |
- | 373 | histposition = |
|
375 | histposition = histposition % KCONSOLE_HISTORY; |
374 | histposition % KCONSOLE_HISTORY; |
- | 375 | } |
|
376 | current = history[histposition]; |
376 | current = history[histposition]; |
377 | printf("%s", current); |
377 | printf("%s", current); |
378 | curlen = strlen(current); |
378 | curlen = strlen(current); |
379 | position = curlen; |
379 | position = curlen; |
380 | continue; |
380 | continue; |
Line 385... | Line 385... | ||
385 | continue; |
385 | continue; |
386 | 386 | ||
387 | insert_char(current, c, position); |
387 | insert_char(current, c, position); |
388 | 388 | ||
389 | curlen++; |
389 | curlen++; |
390 | for (i=position;i<curlen;i++) |
390 | for (i = position; i < curlen; i++) |
391 | putchar(current[i]); |
391 | putchar(current[i]); |
392 | position++; |
392 | position++; |
393 | rdln_print_c('\b',curlen-position); |
393 | rdln_print_c('\b',curlen - position); |
394 | } |
394 | } |
395 | if (curlen) { |
395 | if (curlen) { |
396 | histposition++; |
396 | histposition++; |
397 | histposition = histposition % KCONSOLE_HISTORY; |
397 | histposition = histposition % KCONSOLE_HISTORY; |
398 | } |
398 | } |
Line 421... | Line 421... | ||
421 | if (!len) |
421 | if (!len) |
422 | continue; |
422 | continue; |
423 | cmd_info = parse_cmdline(cmdline, len); |
423 | cmd_info = parse_cmdline(cmdline, len); |
424 | if (!cmd_info) |
424 | if (!cmd_info) |
425 | continue; |
425 | continue; |
426 | if (strncmp(cmd_info->name,"exit", \ |
426 | if (strncmp(cmd_info->name, "exit", |
427 | min(strlen(cmd_info->name),5)) == 0) |
427 | min(strlen(cmd_info->name), 5)) == 0) |
428 | break; |
428 | break; |
429 | (void) cmd_info->func(cmd_info->argv); |
429 | (void) cmd_info->func(cmd_info->argv); |
430 | } |
430 | } |
431 | } |
431 | } |
432 | 432 | ||
Line 438... | Line 438... | ||
438 | bool isptr = false; |
438 | bool isptr = false; |
439 | 439 | ||
440 | /* If we get a name, try to find it in symbol table */ |
440 | /* If we get a name, try to find it in symbol table */ |
441 | if (text[0] == '&') { |
441 | if (text[0] == '&') { |
442 | isaddr = true; |
442 | isaddr = true; |
443 | text++;len--; |
443 | text++; |
- | 444 | len--; |
|
444 | } else if (text[0] == '*') { |
445 | } else if (text[0] == '*') { |
445 | isptr = true; |
446 | isptr = true; |
446 | text++;len--; |
447 | text++; |
- | 448 | len--; |
|
447 | } |
449 | } |
448 | if (text[0] < '0' || text[0] > '9') { |
450 | if (text[0] < '0' || text[0] > '9') { |
449 | strncpy(symname, text, min(len+1, MAX_SYMBOL_NAME)); |
451 | strncpy(symname, text, min(len + 1, MAX_SYMBOL_NAME)); |
450 | symaddr = get_symbol_addr(symname); |
452 | symaddr = get_symbol_addr(symname); |
451 | if (!symaddr) { |
453 | if (!symaddr) { |
452 | printf("Symbol %s not found.\n",symname); |
454 | printf("Symbol %s not found.\n", symname); |
453 | return -1; |
455 | return -1; |
454 | } |
456 | } |
455 | if (symaddr == (uintptr_t) -1) { |
457 | if (symaddr == (uintptr_t) -1) { |
456 | printf("Duplicate symbol %s.\n",symname); |
458 | printf("Duplicate symbol %s.\n", symname); |
457 | symtab_print_search(symname); |
459 | symtab_print_search(symname); |
458 | return -1; |
460 | return -1; |
459 | } |
461 | } |
460 | if (isaddr) |
462 | if (isaddr) |
461 | *result = (unative_t)symaddr; |
463 | *result = (unative_t)symaddr; |
Line 499... | Line 501... | ||
499 | 501 | ||
500 | hlp = list_get_instance(cur, cmd_info_t, link); |
502 | hlp = list_get_instance(cur, cmd_info_t, link); |
501 | spinlock_lock(&hlp->lock); |
503 | spinlock_lock(&hlp->lock); |
502 | 504 | ||
503 | if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name), |
505 | if (strncmp(hlp->name, &cmdline[start], max(strlen(hlp->name), |
504 | end-start+1)) == 0) { |
506 | end - start + 1)) == 0) { |
505 | cmd = hlp; |
507 | cmd = hlp; |
506 | break; |
508 | break; |
507 | } |
509 | } |
508 | 510 | ||
509 | spinlock_unlock(&hlp->lock); |
511 | spinlock_unlock(&hlp->lock); |
Line 537... | Line 539... | ||
537 | 539 | ||
538 | error = 0; |
540 | error = 0; |
539 | switch (cmd->argv[i].type) { |
541 | switch (cmd->argv[i].type) { |
540 | case ARG_TYPE_STRING: |
542 | case ARG_TYPE_STRING: |
541 | buf = cmd->argv[i].buffer; |
543 | buf = cmd->argv[i].buffer; |
542 | strncpy(buf, (const char *) &cmdline[start], min((end - start) + 2, cmd->argv[i].len)); |
544 | strncpy(buf, (const char *) &cmdline[start], |
- | 545 | min((end - start) + 2, cmd->argv[i].len)); |
|
543 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; |
546 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; |
544 | break; |
547 | break; |
545 | case ARG_TYPE_INT: |
548 | case ARG_TYPE_INT: |
546 | if (parse_int_arg(cmdline+start, end-start+1, |
549 | if (parse_int_arg(cmdline + start, end - start + 1, |
547 | &cmd->argv[i].intval)) |
550 | &cmd->argv[i].intval)) |
548 | error = 1; |
551 | error = 1; |
549 | break; |
552 | break; |
550 | case ARG_TYPE_VAR: |
553 | case ARG_TYPE_VAR: |
551 | if (start != end && cmdline[start] == '"' && cmdline[end] == '"') { |
554 | if (start != end && cmdline[start] == '"' && |
- | 555 | cmdline[end] == '"') { |
|
552 | buf = cmd->argv[i].buffer; |
556 | buf = cmd->argv[i].buffer; |
553 | strncpy(buf, (const char *) &cmdline[start+1], |
557 | strncpy(buf, (const char *) &cmdline[start + 1], |
554 | min((end-start), cmd->argv[i].len)); |
558 | min((end-start), cmd->argv[i].len)); |
555 | buf[min((end - start), cmd->argv[i].len - 1)] = '\0'; |
559 | buf[min((end - start), cmd->argv[i].len - 1)] = |
- | 560 | '\0'; |
|
556 | cmd->argv[i].intval = (unative_t) buf; |
561 | cmd->argv[i].intval = (unative_t) buf; |
557 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
562 | cmd->argv[i].vartype = ARG_TYPE_STRING; |
558 | } else if (!parse_int_arg(cmdline+start, end-start+1, |
563 | } else if (!parse_int_arg(cmdline + start, end - start + 1, |
559 | &cmd->argv[i].intval)) |
564 | &cmd->argv[i].intval)) { |
560 | cmd->argv[i].vartype = ARG_TYPE_INT; |
565 | cmd->argv[i].vartype = ARG_TYPE_INT; |
561 | else { |
566 | } else { |
562 | printf("Unrecognized variable argument.\n"); |
567 | printf("Unrecognized variable argument.\n"); |
563 | error = 1; |
568 | error = 1; |
564 | } |
569 | } |
565 | break; |
570 | break; |
566 | case ARG_TYPE_INVALID: |
571 | case ARG_TYPE_INVALID: |