Ignore:
Timestamp:
2012-07-10T12:01:07Z (12 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
f0d7bd9
Parents:
b4ca0a9c
git-author:
Sandeep Kumar <> (2012-07-10 12:01:07)
git-committer:
Vojtech Horky <vojtechhorky@…> (2012-07-10 12:01:07)
Message:

Symbol table completion for kernel console (#50)

Kernel console command arguments are tab-completed with words from symbol
table. Thanks Sandeep Kumar.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/console/kconsole.c

    rb4ca0a9c r1e01a35  
    201201 *
    202202 */
    203 NO_TRACE static int cmdtab_compl(char *input, size_t size)
     203NO_TRACE static int cmdtab_compl(char *input, size_t size, indev_t * indev)
    204204{
    205205        const char *name = input;
    206206       
    207207        size_t found = 0;
     208        /* Maximum Match Length : Length of longest matching common substring in
     209           case more than one match is found */
     210        size_t max_match_len = size;
     211        size_t max_match_len_tmp = size;
     212        size_t input_len = str_length(input);
    208213        link_t *pos = NULL;
    209214        const char *hint;
    210215        char *output = malloc(MAX_CMDLINE, 0);
     216        char display = 'y';
     217        size_t hints_to_show = MAX_TAB_HINTS - 1;
     218        size_t total_hints_shown = 0;
     219        char continue_showing_hints = 'y';
    211220       
    212221        output[0] = 0;
     
    218227                pos = pos->next;
    219228                found++;
     229        }
     230       
     231        /* If possible completions are more than MAX_TAB_HINTS, ask user whether to display them or not. */
     232        if (found > MAX_TAB_HINTS) {
     233                printf("\nDisplay all %zu possibilities? (y or n)", found);
     234                do {
     235                        display = indev_pop_character(indev);
     236                } while (display != 'y' && display != 'n' && display != 'Y' && display != 'N');
    220237        }
    221238       
     
    225242                while (cmdtab_search_one(name, &pos)) {
    226243                        cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link);
    227                         printf("%s (%s)\n", hlp->name, hlp->description);
     244
     245                        if (display == 'y' || display == 'Y') { /* We are still showing hints */
     246                                printf("%s (%s)\n", hlp->name, hlp->description);
     247                                --hints_to_show;
     248                                ++total_hints_shown;
     249
     250                                if (hints_to_show == 0 && total_hints_shown != found) { /* Time to ask user to continue */
     251                                        printf("--More--");
     252                                        do {
     253                                                continue_showing_hints = indev_pop_character(indev);
     254                                                if (continue_showing_hints == 'y' || continue_showing_hints == 'Y'
     255                                                                || continue_showing_hints == ' ') {
     256                                                        hints_to_show = MAX_TAB_HINTS - 1; /* Display a full page again */
     257                                                        break;
     258                                                }
     259
     260                                                if (continue_showing_hints == 'n' || continue_showing_hints == 'N'
     261                                                                || continue_showing_hints == 'q' || continue_showing_hints == 'Q') {
     262                                                        display = 'n'; /* Stop displaying hints */
     263                                                        break;
     264                                                }
     265
     266                                                if (continue_showing_hints == '\n') {
     267                                                        hints_to_show = 1; /* Show one more hint */
     268                                                        break;
     269                                                }
     270                                        } while (1);
     271
     272                                        printf("\r         \r"); /* Delete the --More-- option */
     273                                }
     274                        }
     275
    228276                        pos = pos->next;
    229                 }
     277                        for(max_match_len_tmp = 0; output[max_match_len_tmp] == hlp->name[input_len + max_match_len_tmp]
     278                                        && max_match_len_tmp < max_match_len; ++max_match_len_tmp);
     279                        max_match_len = max_match_len_tmp;
     280                }
     281                /* keep only the characters common in all completions */
     282                output[max_match_len] = 0;
    230283        }
    231284       
     
    294347                        if (beg == 0) {
    295348                                /* Command completion */
    296                                 found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
     349                                found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev);
    297350                        } else {
    298351                                /* Symbol completion */
    299                                 found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE));
     352                                found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev);
    300353                        }
    301354                       
    302355                        if (found == 0)
    303356                                continue;
    304                        
    305                         if (found > 1) {
    306                                 /* No unique hint, list was printed */
    307                                 printf("%s> ", prompt);
    308                                 printf("%ls", current);
    309                                 print_cc('\b', wstr_length(current) - position);
    310                                 continue;
    311                         }
    312                        
    313                         /* We have a hint */
    314                        
     357
     358                        /* We have hints, may be many. In case of more than one hint,
     359                           tmp will contain the common prefix. */
    315360                        size_t off = 0;
    316361                        size_t i = 0;
     
    320365                                i++;
    321366                        }
     367                       
     368                        if (found > 1) {
     369                                /* No unique hint, list was printed */
     370                                printf("%s> ", prompt);
     371                                printf("%ls", current);
     372                                position += str_length(tmp);
     373                                print_cc('\b', wstr_length(current) - position);
     374                                continue;
     375                        }
     376                       
     377                        /* We have a hint */
    322378                       
    323379                        printf("%ls", current + position);
     
    540596/** Parse command line.
    541597 *
    542  * @param cmdline Command line as read from input device. 
     598 * @param cmdline Command line as read from input device.
    543599 * @param size    Size (in bytes) of the string.
    544600 *
Note: See TracChangeset for help on using the changeset viewer.