Ignore:
File:
1 edited

Legend:

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

    r5935c079 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;
     91        size_t pref_size;
    9292        char *stext = NULL;
    9393        char *prefix = NULL;
    9494        char *dirname = NULL;
    95         int retval;
    96        
    97         token_t *tokens = calloc(WORD_MAX, sizeof(token_t));
    98         if (tokens == NULL) {
    99                 retval = ENOMEM;
    100                 goto error;
    101         }
    102        
    103         size_t pref_size;
    10495        char *rpath_sep;
    10596        static const char *dirlist_arg[] = { ".", NULL };
    106         tokenizer_t tok;
    107         ssize_t current_token;
    108         size_t tokens_length;
    109        
     97        int retval;
     98
    11099        cs = calloc(1, sizeof(compl_t));
    111100        if (!cs) {
     
    113102                goto error;
    114103        }
    115        
     104
     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
    116114        /* Convert text buffer to string */
    117         stext = wstr_to_astr(text);
     115        stext = wstr_to_astr(text + *cstart);
    118116        if (stext == NULL) {
    119117                retval = ENOMEM;
    120118                goto error;
    121119        }
    122        
    123         /* Tokenize the input string */
    124         retval = tok_init(&tok, stext, tokens, WORD_MAX);
    125         if (retval != EOK) {
    126                 goto error;
    127         }
    128        
    129         retval = tok_tokenize(&tok, &tokens_length);
    130         if (retval != EOK) {
    131                 goto error;
    132         }
    133        
    134         /* Find the current token */
    135         for (current_token = 0; current_token < (ssize_t) tokens_length;
    136             current_token++) {
    137                 token_t *t = &tokens[current_token];
    138                 size_t end = t->char_start + t->char_length;
    139                
    140                 /*
    141                  * Check if the caret lies inside the token or immediately
    142                  * after it
    143                  */
    144                 if (t->char_start <= pos && pos <= end) {
    145                         break;
    146                 }
    147         }
    148        
    149         if (tokens_length == 0)
    150                 current_token = -1;
    151        
    152         if ((current_token >= 0) && (tokens[current_token].type != TOKTYPE_SPACE))
    153                 *cstart = tokens[current_token].char_start;
    154         else
    155                 *cstart = pos;
    156        
    157         /*
    158          * Extract the prefix being completed
    159          * XXX: handle strings, etc.
    160          */
     120
     121        /* Extract the prefix being completed */
    161122        pref_size = str_lsize(stext, pos - *cstart);
    162123        prefix = malloc(pref_size + 1);
     
    165126                goto error;
    166127        }
    167         prefix[pref_size] = 0;
    168 
    169         if (current_token >= 0) {
    170                 str_ncpy(prefix, pref_size + 1, stext +
    171                     tokens[current_token].byte_start, pref_size);
    172         }
     128
     129        str_ncpy(prefix, pref_size + 1, stext, pref_size);
    173130
    174131        /*
     
    176133         * We look at the previous token. If there is none or it is a pipe
    177134         * ('|'), it is a command, otherwise it is an argument.
     135         * XXX Again we should use the standard tokenizer/parser.
    178136         */
    179137
    180138        /* Skip any whitespace before current token */
    181         ssize_t prev_token = current_token - 1;
    182         if ((prev_token >= 0) && (tokens[prev_token].type == TOKTYPE_SPACE))
    183                 prev_token--;
    184        
     139        while (p > 0 && text[p - 1] == (wchar_t) ' ')
     140                --p;
     141
    185142        /*
    186143         * It is a command if it is the first token or if it immediately
    187144         * follows a pipe token.
    188145         */
    189         if ((prev_token < 0) || (tokens[prev_token].type == TOKTYPE_SPACE))
     146        if (p == 0 || text[p - 1] == '|')
    190147                cs->is_command = true;
    191148        else
     
    232189
    233190        cs->prefix_len = str_length(cs->prefix);
    234        
    235         tok_fini(&tok);
    236191
    237192        *state = cs;
     
    240195error:
    241196        /* Error cleanup */
    242        
    243         tok_fini(&tok);
    244197
    245198        if (cs != NULL && cs->path_list != NULL) {
     
    262215        if (cs != NULL)
    263216                free(cs);
    264         if (tokens != NULL)
    265                 free(tokens);
    266217
    267218        return retval;
Note: See TracChangeset for help on using the changeset viewer.