Changeset e14a103 in mainline for uspace/app/bdsh/compl.c


Ignore:
Timestamp:
2011-08-19T18:24:23Z (13 years ago)
Author:
Martin Sucha <sucha14@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
be61b8f
Parents:
5992e0e
Message:

Use tokenizer in bdsh completion

File:
1 edited

Legend:

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

    r5992e0e re14a103  
    11/*
    22 * Copyright (c) 2011 Jiri Svoboda
     3 * Copyright (c) 2011 Martin Sucha
    34 * All rights reserved.
    45 *
     
    3738#include "compl.h"
    3839#include "exec.h"
     40#include "tok.h"
    3941
    4042static int compl_init(wchar_t *text, size_t pos, size_t *cstart, void **state);
     
    8890{
    8991        compl_t *cs = NULL;
    90         size_t p;
    9192        size_t pref_size;
    9293        char *stext = NULL;
     
    9697        static const char *dirlist_arg[] = { ".", NULL };
    9798        int retval;
     99        tokenizer_t tok;
     100        token_t tokens[WORD_MAX];
     101        unsigned int current_token;
     102        size_t tokens_length;
    98103
    99104        cs = calloc(1, sizeof(compl_t));
     
    103108        }
    104109
    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 
    114110        /* Convert text buffer to string */
    115         stext = wstr_to_astr(text + *cstart);
     111        stext = wstr_to_astr(text);
    116112        if (stext == NULL) {
    117113                retval = ENOMEM;
    118114                goto error;
    119115        }
    120 
    121         /* Extract the prefix being completed */
     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         */
    122150        pref_size = str_lsize(stext, pos - *cstart);
    123151        prefix = malloc(pref_size + 1);
     
    127155        }
    128156
    129         str_ncpy(prefix, pref_size + 1, stext, pref_size);
     157        str_ncpy(prefix, pref_size + 1, stext +
     158            tokens[current_token].byte_start, pref_size);
    130159
    131160        /*
     
    133162         * We look at the previous token. If there is none or it is a pipe
    134163         * ('|'), it is a command, otherwise it is an argument.
    135          * XXX Again we should use the standard tokenizer/parser.
    136164         */
    137165
    138166        /* Skip any whitespace before current token */
    139         while (p > 0 && text[p - 1] == (wchar_t) ' ')
    140                 --p;
     167        int prev_token = current_token - 1;
     168        if (prev_token != -1 && tokens[prev_token].type == TOKTYPE_SPACE) {
     169                prev_token--;
     170        }
    141171
    142172        /*
     
    144174         * follows a pipe token.
    145175         */
    146         if (p == 0 || text[p - 1] == '|')
     176        if (prev_token == -1 || tokens[prev_token].type == TOKTYPE_SPACE)
    147177                cs->is_command = true;
    148178        else
     
    189219
    190220        cs->prefix_len = str_length(cs->prefix);
     221       
     222        tok_fini(&tok);
    191223
    192224        *state = cs;
     
    195227error:
    196228        /* Error cleanup */
     229       
     230        tok_fini(&tok);
    197231
    198232        if (cs != NULL && cs->path_list != NULL) {
Note: See TracChangeset for help on using the changeset viewer.