Ignore:
File:
1 edited

Legend:

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

    r1558d85 rfeeac0d  
    5858#include <errno.h>
    5959#include <putchar.h>
    60 #include <mm/slab.h>
     60#include <str.h>
    6161
    6262/** Simple kernel console.
     
    164164
    165165/** Try to find a command beginning with prefix */
    166 const char *cmdtab_enum(const char *name, const char **h, void **ctx)
    167 {
    168         link_t **startpos = (link_t**)ctx;
     166NO_TRACE static const char *cmdtab_search_one(const char *name,
     167    link_t **startpos)
     168{
    169169        size_t namelen = str_length(name);
    170170       
     
    182182               
    183183                if (str_lcmp(curname, name, namelen) == 0) {
    184                         *startpos = (*startpos)->next;
    185                         if (h) {
    186                                 *h = hlp->description;
    187                         }
    188184                        spinlock_unlock(&cmd_lock);
    189185                        return (curname + str_lsize(curname, namelen));
     
    203199 *
    204200 */
    205 NO_TRACE static int cmdtab_compl(char *input, size_t size, indev_t *indev,
    206     hints_enum_func_t hints_enum)
     201NO_TRACE static int cmdtab_compl(char *input, size_t size, indev_t *indev)
    207202{
    208203        const char *name = input;
     
    216211        size_t max_match_len = size;
    217212        size_t max_match_len_tmp = size;
    218         void *pos = NULL;
     213        size_t input_len = str_length(input);
     214        link_t *pos = NULL;
    219215        const char *hint;
    220         const char *help;
    221216        char *output = malloc(MAX_CMDLINE, 0);
    222217        size_t hints_to_show = MAX_TAB_HINTS - 1;
     
    226221        output[0] = 0;
    227222       
    228         while ((hint = hints_enum(name, NULL, &pos))) {
    229                 if ((found == 0) || (str_length(hint) > str_length(output)))
     223        while ((hint = cmdtab_search_one(name, &pos))) {
     224                if ((found == 0) || (str_length(output) > str_length(hint)))
    230225                        str_cpy(output, MAX_CMDLINE, hint);
    231226               
     227                pos = pos->next;
    232228                found++;
    233229        }
     
    246242                printf("\n");
    247243                pos = NULL;
    248                 while ((hint = hints_enum(name, &help, &pos))) {
     244                while (cmdtab_search_one(name, &pos)) {
     245                        cmd_info_t *hlp = list_get_instance(pos, cmd_info_t, link);
    249246                       
    250247                        if (continue_showing_hints) {
    251                                
    252                                 if (help)
    253                                         printf("%s%s (%s)\n", name, hint, help);
    254                                 else
    255                                         printf("%s%s\n", name, hint);
    256                                
     248                                printf("%s (%s)\n", hlp->name, hlp->description);
    257249                                --hints_to_show;
    258250                                ++total_hints_shown;
     
    265257                        }
    266258                       
     259                        pos = pos->next;
     260                       
    267261                        for (max_match_len_tmp = 0;
    268262                            (output[max_match_len_tmp] ==
    269                             hint[max_match_len_tmp]) &&
     263                            hlp->name[input_len + max_match_len_tmp]) &&
    270264                            (max_match_len_tmp < max_match_len); ++max_match_len_tmp);
    271265                       
     
    282276        free(output);
    283277        return found;
    284 }
    285 
    286 NO_TRACE static cmd_info_t *parse_cmd(const wchar_t *cmdline)
    287 {
    288         size_t start = 0;
    289         size_t end;
    290         char *tmp;
    291        
    292         while (isspace(cmdline[start]))
    293                 start++;
    294         end = start + 1;
    295         while (!isspace(cmdline[end]))
    296                 end++;
    297        
    298         tmp = malloc(STR_BOUNDS(end - start + 1), 0);
    299        
    300         wstr_to_str(tmp, end - start + 1, &cmdline[start]);
    301        
    302         spinlock_lock(&cmd_lock);
    303        
    304         list_foreach(cmd_list, link, cmd_info_t, hlp) {
    305                 spinlock_lock(&hlp->lock);
    306                
    307                 if (str_cmp(hlp->name, tmp) == 0) {
    308                         spinlock_unlock(&hlp->lock);
    309                         spinlock_unlock(&cmd_lock);
    310                         free(tmp);
    311                         return hlp;
    312                 }
    313                
    314                 spinlock_unlock(&hlp->lock);
    315         }
    316        
    317         free(tmp);
    318         spinlock_unlock(&cmd_lock);
    319        
    320         return NULL;
    321278}
    322279
     
    361318                                putchar(current[position]);
    362319                       
     320                        if (position == 0)
     321                                continue;
    363322                       
    364323                        /*
     
    367326                         */
    368327                        size_t beg;
    369                         unsigned narg = 0;
    370                         if (position == 0) {
    371                                 tmp[0] = '\0';
    372                                 beg = 0;
     328                        for (beg = position - 1; (beg > 0) && (!isspace(current[beg]));
     329                            beg--);
     330                       
     331                        if (isspace(current[beg]))
     332                                beg++;
     333                       
     334                        wstr_to_str(tmp, position - beg + 1, current + beg);
     335                       
     336                        int found;
     337                        if (beg == 0) {
     338                                /* Command completion */
     339                                found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev);
    373340                        } else {
    374                                 for (beg = position - 1;
    375                                     (beg > 0) && (!isspace(current[beg]));
    376                                     beg--) {
    377                                         ;
    378                                 }
    379                                
    380                                 if (isspace(current[beg]))
    381                                         beg++;
    382                                
    383                                 wstr_to_str(tmp, position - beg + 1, current + beg);
    384                         }
    385                        
    386                         /* Count which argument number are we tabbing (narg=0 is cmd) */
    387                         bool sp = false;
    388                         for (; beg > 0; beg--) {
    389                                 if (isspace(current[beg])) {
    390                                         if (!sp) {
    391                                                 narg++;
    392                                                 sp = true;
    393                                         }
    394                                 } else
    395                                         sp = false;
    396                         }
    397                         if (narg && isspace(current[0]))
    398                                 narg--;
    399 
    400                         int found;
    401                         if (narg == 0) {
    402                                 /* Command completion */
    403                                 found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev,
    404                                     cmdtab_enum);
    405                         } else {
    406                                 /* Arguments completion */
    407                                 cmd_info_t *cmd = parse_cmd(current);
    408                                 if (!cmd || !cmd->hints_enum || cmd->argc < narg)
    409                                         continue;
    410                                 found = cmdtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev,
    411                                     cmd->hints_enum);
     341                                /* Symbol completion */
     342                                found = symtab_compl(tmp, STR_BOUNDS(MAX_CMDLINE), indev);
    412343                        }
    413344                       
Note: See TracChangeset for help on using the changeset viewer.