Changes in uspace/app/bdsh/compl.c [5935c079:41ff85b] in mainline
- File:
-
- 1 edited
-
uspace/app/bdsh/compl.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
uspace/app/bdsh/compl.c
r5935c079 r41ff85b 1 1 /* 2 2 * Copyright (c) 2011 Jiri Svoboda 3 * Copyright (c) 2011 Martin Sucha4 3 * All rights reserved. 5 4 * … … 38 37 #include "compl.h" 39 38 #include "exec.h" 40 #include "tok.h"41 39 42 40 static int compl_init(wchar_t *text, size_t pos, size_t *cstart, void **state); … … 90 88 { 91 89 compl_t *cs = NULL; 90 size_t p; 91 size_t pref_size; 92 92 char *stext = NULL; 93 93 char *prefix = NULL; 94 94 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;104 95 char *rpath_sep; 105 96 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 110 99 cs = calloc(1, sizeof(compl_t)); 111 100 if (!cs) { … … 113 102 goto error; 114 103 } 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 116 114 /* Convert text buffer to string */ 117 stext = wstr_to_astr(text );115 stext = wstr_to_astr(text + *cstart); 118 116 if (stext == NULL) { 119 117 retval = ENOMEM; 120 118 goto error; 121 119 } 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 */ 161 122 pref_size = str_lsize(stext, pos - *cstart); 162 123 prefix = malloc(pref_size + 1); … … 165 126 goto error; 166 127 } 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); 173 130 174 131 /* … … 176 133 * We look at the previous token. If there is none or it is a pipe 177 134 * ('|'), it is a command, otherwise it is an argument. 135 * XXX Again we should use the standard tokenizer/parser. 178 136 */ 179 137 180 138 /* 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 185 142 /* 186 143 * It is a command if it is the first token or if it immediately 187 144 * follows a pipe token. 188 145 */ 189 if ( (prev_token < 0) || (tokens[prev_token].type == TOKTYPE_SPACE))146 if (p == 0 || text[p - 1] == '|') 190 147 cs->is_command = true; 191 148 else … … 232 189 233 190 cs->prefix_len = str_length(cs->prefix); 234 235 tok_fini(&tok);236 191 237 192 *state = cs; … … 240 195 error: 241 196 /* Error cleanup */ 242 243 tok_fini(&tok);244 197 245 198 if (cs != NULL && cs->path_list != NULL) { … … 262 215 if (cs != NULL) 263 216 free(cs); 264 if (tokens != NULL)265 free(tokens);266 217 267 218 return retval;
Note:
See TracChangeset
for help on using the changeset viewer.
