Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/bdsh/compl.c

    re14a103 r41ff85b  
    11/*
    22 * Copyright (c) 2011 Jiri Svoboda
    3  * Copyright (c) 2011 Martin Sucha
    43 * All rights reserved.
    54 *
     
    3837#include "compl.h"
    3938#include "exec.h"
    40 #include "tok.h"
    4139
    4240static int compl_init(wchar_t *text, size_t pos, size_t *cstart, void **state);
     
    9088{
    9189        compl_t *cs = NULL;
     90        size_t p;
    9291        size_t pref_size;
    9392        char *stext = NULL;
     
    9796        static const char *dirlist_arg[] = { ".", NULL };
    9897        int retval;
    99         tokenizer_t tok;
    100         token_t tokens[WORD_MAX];
    101         unsigned int current_token;
    102         size_t tokens_length;
    10398
    10499        cs = calloc(1, sizeof(compl_t));
     
    108103        }
    109104
     105        /*
     106         * Copy token pointed to by caret from start up to the caret.
     107         * XXX Ideally we would use the standard tokenizer.
     108         */
     109        p = pos;
     110        while (p > 0 && text[p - 1] != (wchar_t) ' ')
     111                --p;
     112        *cstart = p;
     113
    110114        /* Convert text buffer to string */
    111         stext = wstr_to_astr(text);
     115        stext = wstr_to_astr(text + *cstart);
    112116        if (stext == NULL) {
    113117                retval = ENOMEM;
    114118                goto error;
    115119        }
    116        
    117         /* Tokenize the input string */
    118         retval = tok_init(&tok, stext, tokens, WORD_MAX);
    119         if (retval != EOK) {
    120                 goto error;
    121         }
    122        
    123         retval = tok_tokenize(&tok, &tokens_length);
    124         if (retval != EOK) {
    125                 goto error;
    126         }
    127        
    128         /* Find the current token */
    129         for (current_token = 0; current_token < tokens_length; current_token++) {
    130                 token_t *t = &tokens[current_token];
    131                 size_t end = t->char_start + t->char_length;
    132                 /* Check if the caret lies inside the token or immediately
    133                  * after it
    134                  */
    135                 if (t->char_start <= pos && pos <= end) {
    136                         break;
    137                 }
    138         }
    139        
    140         if (tokens[current_token].type != TOKTYPE_SPACE) {
    141                 *cstart = tokens[current_token].char_start;
    142         }
    143         else {
    144                 *cstart = pos;
    145         }
    146        
    147         /* Extract the prefix being completed
    148          * XXX: handle strings, etc.
    149          */
     120
     121        /* Extract the prefix being completed */
    150122        pref_size = str_lsize(stext, pos - *cstart);
    151123        prefix = malloc(pref_size + 1);
     
    155127        }
    156128
    157         str_ncpy(prefix, pref_size + 1, stext +
    158             tokens[current_token].byte_start, pref_size);
     129        str_ncpy(prefix, pref_size + 1, stext, pref_size);
    159130
    160131        /*
     
    162133         * We look at the previous token. If there is none or it is a pipe
    163134         * ('|'), it is a command, otherwise it is an argument.
     135         * XXX Again we should use the standard tokenizer/parser.
    164136         */
    165137
    166138        /* Skip any whitespace before current token */
    167         int prev_token = current_token - 1;
    168         if (prev_token != -1 && tokens[prev_token].type == TOKTYPE_SPACE) {
    169                 prev_token--;
    170         }
     139        while (p > 0 && text[p - 1] == (wchar_t) ' ')
     140                --p;
    171141
    172142        /*
     
    174144         * follows a pipe token.
    175145         */
    176         if (prev_token == -1 || tokens[prev_token].type == TOKTYPE_SPACE)
     146        if (p == 0 || text[p - 1] == '|')
    177147                cs->is_command = true;
    178148        else
     
    219189
    220190        cs->prefix_len = str_length(cs->prefix);
    221        
    222         tok_fini(&tok);
    223191
    224192        *state = cs;
     
    227195error:
    228196        /* Error cleanup */
    229        
    230         tok_fini(&tok);
    231197
    232198        if (cs != NULL && cs->path_list != NULL) {
Note: See TracChangeset for help on using the changeset viewer.