Ignore:
File:
1 edited

Legend:

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

    ra35b458 r5a6cc679  
    5252#include <macros.h>
    5353#include <debug.h>
    54 #include <halt.h>
     54#include <func.h>
    5555#include <str.h>
    5656#include <sysinfo/sysinfo.h>
     
    9898{
    9999        unsigned int i;
    100 
     100       
    101101        cmd_init();
    102102        for (i = 0; i < KCONSOLE_HISTORY; i++)
     
    114114{
    115115        spinlock_lock(&cmd_lock);
    116 
     116       
    117117        /*
    118118         * Make sure the command is not already listed.
     
    124124                        return false;
    125125                }
    126 
     126               
    127127                /* Avoid deadlock. */
    128128                if (hlp < cmd) {
     
    133133                        spinlock_lock(&hlp->lock);
    134134                }
    135 
     135               
    136136                if (str_cmp(hlp->name, cmd->name) == 0) {
    137137                        /* The command is already there. */
     
    141141                        return false;
    142142                }
    143 
     143               
    144144                spinlock_unlock(&hlp->lock);
    145145                spinlock_unlock(&cmd->lock);
    146146        }
    147 
     147       
    148148        /*
    149149         * Now the command can be added.
    150150         */
    151151        list_append(&cmd->link, &cmd_list);
    152 
     152       
    153153        spinlock_unlock(&cmd_lock);
    154154        return true;
     
    168168        link_t **startpos = (link_t**) ctx;
    169169        size_t namelen = str_length(name);
    170 
     170       
    171171        spinlock_lock(&cmd_lock);
    172 
     172       
    173173        if (*startpos == NULL)
    174174                *startpos = cmd_list.head.next;
    175 
     175       
    176176        for (; *startpos != &cmd_list.head; *startpos = (*startpos)->next) {
    177177                cmd_info_t *hlp = list_get_instance(*startpos, cmd_info_t, link);
    178 
     178               
    179179                const char *curname = hlp->name;
    180180                if (str_length(curname) < namelen)
    181181                        continue;
    182 
     182               
    183183                if (str_lcmp(curname, name, namelen) == 0) {
    184184                        *startpos = (*startpos)->next;
    185185                        if (h)
    186186                                *h = hlp->description;
    187 
     187                       
    188188                        spinlock_unlock(&cmd_lock);
    189189                        return (curname + str_lsize(curname, namelen));
    190190                }
    191191        }
    192 
     192       
    193193        spinlock_unlock(&cmd_lock);
    194194        return NULL;
     
    207207{
    208208        const char *name = input;
    209 
     209       
    210210        size_t found = 0;
    211 
     211       
    212212        /*
    213213         * Maximum Match Length: Length of longest matching common
     
    223223        size_t total_hints_shown = 0;
    224224        bool continue_showing_hints = true;
    225 
     225       
    226226        output[0] = 0;
    227 
     227       
    228228        while ((hint = hints_enum(name, NULL, &pos))) {
    229229                if ((found == 0) || (str_length(hint) > str_length(output)))
    230230                        str_cpy(output, MAX_CMDLINE, hint);
    231 
     231               
    232232                found++;
    233233        }
    234 
     234       
    235235        /*
    236236         * If the number of possible completions is more than MAX_TAB_HINTS,
     
    242242                    console_prompt_display_all_hints(indev, found);
    243243        }
    244 
     244       
    245245        if ((found > 1) && (str_length(output) != 0)) {
    246246                printf("\n");
    247247                pos = NULL;
    248248                while ((hint = hints_enum(name, &help, &pos))) {
    249 
     249                       
    250250                        if (continue_showing_hints) {
    251251                                if (help)
     
    253253                                else
    254254                                        printf("%s%s\n", name, hint);
    255 
     255                               
    256256                                --hints_to_show;
    257257                                ++total_hints_shown;
    258 
     258                               
    259259                                if ((hints_to_show == 0) && (total_hints_shown != found)) {
    260260                                        /* Ask user to continue */
     
    263263                                }
    264264                        }
    265 
     265                       
    266266                        for (max_match_len_tmp = 0;
    267267                            (output[max_match_len_tmp] ==
    268268                            hint[max_match_len_tmp]) &&
    269269                            (max_match_len_tmp < max_match_len); ++max_match_len_tmp);
    270 
     270                       
    271271                        max_match_len = max_match_len_tmp;
    272272                }
    273 
     273               
    274274                /* Keep only the characters common in all completions */
    275275                output[max_match_len] = 0;
    276276        }
    277 
     277       
    278278        if (found > 0)
    279279                str_cpy(input, size, output);
    280 
     280       
    281281        free(output);
    282282        return found;
     
    288288        size_t end;
    289289        char *tmp;
    290 
     290       
    291291        while (isspace(cmdline[start]))
    292292                start++;
    293 
     293       
    294294        end = start + 1;
    295 
     295       
    296296        while (!isspace(cmdline[end]))
    297297                end++;
    298 
     298       
    299299        tmp = malloc(STR_BOUNDS(end - start + 1), 0);
    300 
     300       
    301301        wstr_to_str(tmp, end - start + 1, &cmdline[start]);
    302 
     302       
    303303        spinlock_lock(&cmd_lock);
    304 
     304       
    305305        list_foreach(cmd_list, link, cmd_info_t, hlp) {
    306306                spinlock_lock(&hlp->lock);
    307 
     307               
    308308                if (str_cmp(hlp->name, tmp) == 0) {
    309309                        spinlock_unlock(&hlp->lock);
     
    312312                        return hlp;
    313313                }
    314 
     314               
    315315                spinlock_unlock(&hlp->lock);
    316316        }
    317 
     317       
    318318        free(tmp);
    319319        spinlock_unlock(&cmd_lock);
    320 
     320       
    321321        return NULL;
    322322}
     
    325325{
    326326        printf("%s> ", prompt);
    327 
     327       
    328328        size_t position = 0;
    329329        wchar_t *current = history[history_pos];
    330330        current[0] = 0;
    331331        char *tmp = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
    332 
     332       
    333333        while (true) {
    334334                wchar_t ch = indev_pop_character(indev);
    335 
     335               
    336336                if (ch == '\n') {
    337337                        /* Enter */
     
    339339                        break;
    340340                }
    341 
     341               
    342342                if (ch == '\b') {
    343343                        /* Backspace */
    344344                        if (position == 0)
    345345                                continue;
    346 
     346                       
    347347                        if (wstr_remove(current, position - 1)) {
    348348                                position--;
     
    353353                        }
    354354                }
    355 
     355               
    356356                if (ch == '\t') {
    357357                        /* Tab completion */
    358 
     358                       
    359359                        /* Move to the end of the word */
    360360                        for (; (current[position] != 0) && (!isspace(current[position]));
    361361                            position++)
    362362                                putchar(current[position]);
    363 
    364 
     363                       
     364                       
    365365                        /*
    366366                         * Find the beginning of the word
     
    376376                                    (beg > 0) && (!isspace(current[beg]));
    377377                                    beg--);
    378 
     378                               
    379379                                if (isspace(current[beg]))
    380380                                        beg++;
    381 
     381                               
    382382                                wstr_to_str(tmp, position - beg + 1, current + beg);
    383383                        }
    384 
     384                       
    385385                        /* Count which argument number are we tabbing (narg=0 is cmd) */
    386386                        bool sp = false;
     
    394394                                        sp = false;
    395395                        }
    396 
     396                       
    397397                        if (narg && isspace(current[0]))
    398398                                narg--;
    399 
     399                       
    400400                        int found;
    401401                        if (narg == 0) {
     
    411411                                    cmd->hints_enum);
    412412                        }
    413 
     413                       
    414414                        if (found == 0)
    415415                                continue;
     
    424424                                if (!wstr_linsert(current, ch, position + i, MAX_CMDLINE))
    425425                                        break;
    426 
     426                               
    427427                                i++;
    428428                        }
    429 
     429                       
    430430                        if (found > 1) {
    431431                                /* No unique hint, list was printed */
     
    436436                                continue;
    437437                        }
    438 
     438                       
    439439                        /* We have a hint */
    440 
     440                       
    441441                        printf("%ls", current + position);
    442442                        position += str_length(tmp);
    443443                        print_cc('\b', wstr_length(current) - position);
    444 
     444                       
    445445                        if (position == wstr_length(current)) {
    446446                                /* Insert a space after the last completed argument */
     
    452452                        continue;
    453453                }
    454 
     454               
    455455                if (ch == U_LEFT_ARROW) {
    456456                        /* Left */
     
    461461                        continue;
    462462                }
    463 
     463               
    464464                if (ch == U_RIGHT_ARROW) {
    465465                        /* Right */
     
    470470                        continue;
    471471                }
    472 
     472               
    473473                if ((ch == U_UP_ARROW) || (ch == U_DOWN_ARROW)) {
    474474                        /* Up, down */
     
    476476                        print_cc(' ', wstr_length(current));
    477477                        print_cc('\b', wstr_length(current));
    478 
     478                       
    479479                        if (ch == U_UP_ARROW) {
    480480                                /* Up */
     
    493493                        continue;
    494494                }
    495 
     495               
    496496                if (ch == U_HOME_ARROW) {
    497497                        /* Home */
     
    500500                        continue;
    501501                }
    502 
     502               
    503503                if (ch == U_END_ARROW) {
    504504                        /* End */
     
    507507                        continue;
    508508                }
    509 
     509               
    510510                if (ch == U_DELETE) {
    511511                        /* Delete */
    512512                        if (position == wstr_length(current))
    513513                                continue;
    514 
     514                       
    515515                        if (wstr_remove(current, position)) {
    516516                                printf("%ls ", current + position);
     
    519519                        continue;
    520520                }
    521 
     521               
    522522                if (wstr_linsert(current, ch, position, MAX_CMDLINE)) {
    523523                        printf("%ls", current + position);
     
    526526                }
    527527        }
    528 
     528       
    529529        if (wstr_length(current) > 0) {
    530530                history_pos++;
    531531                history_pos = history_pos % KCONSOLE_HISTORY;
    532532        }
    533 
     533       
    534534        free(tmp);
    535535        return current;
     
    546546        bool isaddr = false;
    547547        bool isptr = false;
    548 
     548       
    549549        /* If we get a name, try to find it in symbol table */
    550550        if (text[0] == '&') {
     
    557557                len--;
    558558        }
    559 
     559       
    560560        if ((text[0] < '0') || (text[0] > '9')) {
    561561                char symname[MAX_SYMBOL_NAME];
    562562                str_ncpy(symname, MAX_SYMBOL_NAME, text, len + 1);
    563 
     563               
    564564                uintptr_t symaddr;
    565565                errno_t rc = symtab_addr_lookup(symname, &symaddr);
     
    611611                }
    612612        }
    613 
     613       
    614614        return true;
    615615}
     
    635635        assert(start != NULL);
    636636        assert(end != NULL);
    637 
     637       
    638638        bool found_start = false;
    639639        size_t offset = *start;
    640640        size_t prev = *start;
    641641        wchar_t ch;
    642 
     642       
    643643        while ((ch = str_decode(cmdline, &offset, size)) != 0) {
    644644                if (!found_start) {
     
    651651                                break;
    652652                }
    653 
     653               
    654654                prev = offset;
    655655        }
    656656        *end = prev;
    657 
     657       
    658658        return found_start;
    659659}
     
    676676        }
    677677        spinlock_lock(&cmd_lock);
    678 
     678       
    679679        cmd_info_t *cmd = NULL;
    680 
     680       
    681681        list_foreach(cmd_list, link, cmd_info_t, hlp) {
    682682                spinlock_lock(&hlp->lock);
    683 
     683               
    684684                if (str_lcmp(hlp->name, cmdline + start,
    685685                    max(str_length(hlp->name),
     
    688688                        break;
    689689                }
    690 
     690               
    691691                spinlock_unlock(&hlp->lock);
    692692        }
    693 
     693       
    694694        spinlock_unlock(&cmd_lock);
    695 
     695       
    696696        if (!cmd) {
    697697                /* Unknown command. */
     
    699699                return NULL;
    700700        }
    701 
     701       
    702702        /* cmd == hlp is locked */
    703 
     703       
    704704        /*
    705705         * The command line must be further analyzed and
     
    708708         * structure.
    709709         */
    710 
     710       
    711711        bool error = false;
    712712        size_t i;
    713713        for (i = 0; i < cmd->argc; i++) {
    714714                char *buf;
    715 
     715               
    716716                start = end;
    717717                if (!parse_argument(cmdline, size, &start, &end)) {
     
    721721                                continue;
    722722                        }
    723 
     723                       
    724724                        printf("Too few arguments.\n");
    725725                        spinlock_unlock(&cmd->lock);
    726726                        return NULL;
    727727                }
    728 
     728               
    729729                switch (cmd->argv[i].type) {
    730730                case ARG_TYPE_STRING:
     
    767767                }
    768768        }
    769 
     769       
    770770        if (error) {
    771771                spinlock_unlock(&cmd->lock);
    772772                return NULL;
    773773        }
    774 
     774       
    775775        start = end;
    776776        if (parse_argument(cmdline, size, &start, &end)) {
     
    779779                return NULL;
    780780        }
    781 
     781       
    782782        spinlock_unlock(&cmd->lock);
    783783        return cmd;
     
    798798                return;
    799799        }
    800 
     800       
    801801        if (msg)
    802802                printf("%s", msg);
    803 
     803       
    804804        if (kcon)
    805805                indev_pop_character(stdin);
    806806        else
    807807                printf("Type \"exit\" to leave the console.\n");
    808 
     808       
    809809        char *cmdline = malloc(STR_BOUNDS(MAX_CMDLINE), 0);
    810810        while (true) {
     
    813813                if (!len)
    814814                        continue;
    815 
     815               
    816816                wstr_to_str(cmdline, STR_BOUNDS(MAX_CMDLINE), tmp);
    817 
     817               
    818818                if ((!kcon) && (len == 4) && (str_lcmp(cmdline, "exit", 4) == 0))
    819819                        break;
    820 
     820               
    821821                cmd_info_t *cmd_info = parse_cmdline(cmdline, STR_BOUNDS(MAX_CMDLINE));
    822822                if (!cmd_info)
    823823                        continue;
    824 
     824               
    825825                (void) cmd_info->func(cmd_info->argv);
    826826        }
Note: See TracChangeset for help on using the changeset viewer.