source: mainline/uspace/app/bdsh/compl.c@ b5746a2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since b5746a2 was 1d6dd2a, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 8 years ago

Remove unnecessary includes from <stdio.h>.

  • Property mode set to 100644
File size: 9.5 KB
Line 
1/*
2 * Copyright (c) 2011 Jiri Svoboda
3 * Copyright (c) 2011 Martin Sucha
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * - The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#include <stdbool.h>
31#include <dirent.h>
32#include <errno.h>
33#include <macros.h>
34#include <stdlib.h>
35#include <vfs/vfs.h>
36#include <str.h>
37
38#include "cmds/cmds.h"
39#include "compl.h"
40#include "exec.h"
41#include "tok.h"
42
43static errno_t compl_init(wchar_t *text, size_t pos, size_t *cstart, void **state);
44static errno_t compl_get_next(void *state, char **compl);
45static void compl_fini(void *state);
46
47/** Bdsh implementation of completion ops. */
48tinput_compl_ops_t compl_ops = {
49 .init = compl_init,
50 .get_next = compl_get_next,
51 .fini = compl_fini
52};
53
54/** Completion state object.
55 *
56 * The state object contains 'iterators' for modules, builtins and
57 * executables in directories.
58 */
59typedef struct {
60 /** String prefix which we are trying to complete. */
61 char *prefix;
62 /** Length of string prefix (number of characters) */
63 size_t prefix_len;
64
65 /** Pointer inside list of modules */
66 module_t *module;
67 /** Pointer inside list of builtins */
68 builtin_t *builtin;
69
70 /** Pointer inside list of directories */
71 const char **path;
72 /** If not @c NULL, should be freed in the end. */
73 const char **path_list;
74 /** Current open directory */
75 DIR *dir;
76
77 char *last_compl;
78
79 /**
80 * @c true if we are completing a command, @c false if we are
81 * completing an argument
82 */
83 bool is_command;
84} compl_t;
85
86/** Init completion.
87 *
88 * Set up iterators in completion object, based on current token.
89 */
90static errno_t compl_init(wchar_t *text, size_t pos, size_t *cstart, void **state)
91{
92 compl_t *cs = NULL;
93 char *stext = NULL;
94 char *prefix = NULL;
95 char *dirname = NULL;
96 errno_t retval;
97
98 token_t *tokens = calloc(WORD_MAX, sizeof(token_t));
99 if (tokens == NULL) {
100 retval = ENOMEM;
101 goto error;
102 }
103
104 size_t pref_size;
105 char *rpath_sep;
106 static const char *dirlist_arg[] = { ".", NULL };
107 tokenizer_t tok;
108 ssize_t current_token;
109 size_t tokens_length;
110
111 cs = calloc(1, sizeof(compl_t));
112 if (!cs) {
113 retval = ENOMEM;
114 goto error;
115 }
116
117 /* Convert text buffer to string */
118 stext = wstr_to_astr(text);
119 if (stext == NULL) {
120 retval = ENOMEM;
121 goto error;
122 }
123
124 /* Tokenize the input string */
125 retval = tok_init(&tok, stext, tokens, WORD_MAX);
126 if (retval != EOK) {
127 goto error;
128 }
129
130 retval = tok_tokenize(&tok, &tokens_length);
131 if (retval != EOK) {
132 goto error;
133 }
134
135 /* Find the current token */
136 for (current_token = 0; current_token < (ssize_t) tokens_length;
137 current_token++) {
138 token_t *t = &tokens[current_token];
139 size_t end = t->char_start + t->char_length;
140
141 /*
142 * Check if the caret lies inside the token or immediately
143 * after it
144 */
145 if (t->char_start <= pos && pos <= end) {
146 break;
147 }
148 }
149
150 if (tokens_length == 0)
151 current_token = -1;
152
153 if ((current_token >= 0) && (tokens[current_token].type != TOKTYPE_SPACE))
154 *cstart = tokens[current_token].char_start;
155 else
156 *cstart = pos;
157
158 /*
159 * Extract the prefix being completed
160 * XXX: handle strings, etc.
161 */
162 pref_size = str_lsize(stext, pos - *cstart);
163 prefix = malloc(pref_size + 1);
164 if (prefix == NULL) {
165 retval = ENOMEM;
166 goto error;
167 }
168 prefix[pref_size] = 0;
169
170 if (current_token >= 0) {
171 str_ncpy(prefix, pref_size + 1, stext +
172 tokens[current_token].byte_start, pref_size);
173 }
174
175 /*
176 * Determine if the token being completed is a command or argument.
177 * We look at the previous token. If there is none or it is a pipe
178 * ('|'), it is a command, otherwise it is an argument.
179 */
180
181 /* Skip any whitespace before current token */
182 ssize_t prev_token = current_token - 1;
183 if ((prev_token >= 0) && (tokens[prev_token].type == TOKTYPE_SPACE))
184 prev_token--;
185
186 /*
187 * It is a command if it is the first token or if it immediately
188 * follows a pipe token.
189 */
190 if ((prev_token < 0) || (tokens[prev_token].type == TOKTYPE_SPACE))
191 cs->is_command = true;
192 else
193 cs->is_command = false;
194
195 rpath_sep = str_rchr(prefix, '/');
196 if (rpath_sep != NULL) {
197 /* Extract path. For path beginning with '/' keep the '/'. */
198 dirname = str_ndup(prefix, max(1, rpath_sep - prefix));
199 if (dirname == NULL) {
200 retval = ENOMEM;
201 goto error;
202 }
203
204 /* Extract name prefix */
205 cs->prefix = str_dup(rpath_sep + 1);
206 if (cs->prefix == NULL) {
207 retval = ENOMEM;
208 goto error;
209 }
210 *cstart += rpath_sep + 1 - prefix;
211 free(prefix);
212 prefix = NULL;
213
214 cs->path_list = malloc(sizeof(char *) * 2);
215 if (cs->path_list == NULL) {
216 retval = ENOMEM;
217 goto error;
218 }
219 cs->path_list[0] = dirname;
220 cs->path_list[1] = NULL;
221 cs->path = cs->path_list;
222
223 } else if (cs->is_command) {
224 /* Command without path */
225 cs->module = modules;
226 cs->builtin = builtins;
227 cs->prefix = prefix;
228 cs->path = &search_dir[0];
229 } else {
230 /* Argument without path */
231 cs->prefix = prefix;
232 cs->path = &dirlist_arg[0];
233 }
234
235 cs->prefix_len = str_length(cs->prefix);
236
237 tok_fini(&tok);
238
239 *state = cs;
240 return EOK;
241
242error:
243 /* Error cleanup */
244
245 tok_fini(&tok);
246
247 if (cs != NULL && cs->path_list != NULL) {
248 size_t i = 0;
249 while (cs->path_list[i] != NULL) {
250 free(cs->path_list[i]);
251 ++i;
252 }
253 free(cs->path_list);
254 }
255
256 if ((cs != NULL) && (cs->prefix != NULL))
257 free(cs->prefix);
258
259 if (dirname != NULL)
260 free(dirname);
261
262 if (prefix != NULL)
263 free(prefix);
264
265 if (stext != NULL)
266 free(stext);
267
268 if (cs != NULL)
269 free(cs);
270
271 if (tokens != NULL)
272 free(tokens);
273
274 return retval;
275}
276
277/** Determine if completion matches the required prefix.
278 *
279 * Required prefix is stored in @a cs->prefix.
280 *
281 * @param cs Completion state object
282 * @param compl Completion string
283 * @return @c true when @a compl matches, @c false otherwise
284 */
285static bool compl_match_prefix(compl_t *cs, const char *compl)
286{
287 return str_lcmp(compl, cs->prefix, cs->prefix_len) == 0;
288}
289
290/** Get next match. */
291static errno_t compl_get_next(void *state, char **compl)
292{
293 compl_t *cs = (compl_t *) state;
294 struct dirent *dent;
295
296 *compl = NULL;
297
298 if (cs->last_compl != NULL) {
299 free(cs->last_compl);
300 cs->last_compl = NULL;
301 }
302
303 /* Modules */
304 if (cs->module != NULL) {
305 while (*compl == NULL && cs->module->name != NULL) {
306 if (compl_match_prefix(cs, cs->module->name)) {
307 asprintf(compl, "%s ", cs->module->name);
308 cs->last_compl = *compl;
309 if (*compl == NULL)
310 return ENOMEM;
311 }
312 cs->module++;
313 }
314 }
315
316 /* Builtins */
317 if (cs->builtin != NULL) {
318 while (*compl == NULL && cs->builtin->name != NULL) {
319 if (compl_match_prefix(cs, cs->builtin->name)) {
320 asprintf(compl, "%s ", cs->builtin->name);
321 cs->last_compl = *compl;
322 if (*compl == NULL)
323 return ENOMEM;
324 }
325 cs->builtin++;
326 }
327 }
328
329 /* Files and directories. We scan entries from a set of directories. */
330 if (cs->path != NULL) {
331 while (*compl == NULL) {
332 /* Open next directory */
333 while (cs->dir == NULL) {
334 if (*cs->path == NULL)
335 break;
336
337 cs->dir = opendir(*cs->path);
338
339 /* Skip directories that we fail to open. */
340 if (cs->dir == NULL)
341 cs->path++;
342 }
343
344 /* If it was the last one, we are done */
345 if (cs->dir == NULL)
346 break;
347
348 /* Read next directory entry */
349 dent = readdir(cs->dir);
350 if (dent == NULL) {
351 /* Error. Close directory, go to next one */
352 closedir(cs->dir);
353 cs->dir = NULL;
354 cs->path++;
355 continue;
356 }
357
358 if (compl_match_prefix(cs, dent->d_name)) {
359 /* Construct pathname */
360 char *ent_path;
361 asprintf(&ent_path, "%s/%s", *cs->path, dent->d_name);
362 struct stat ent_stat;
363 if (vfs_stat_path(ent_path, &ent_stat) != EOK) {
364 /* Error */
365 free(ent_path);
366 continue;
367 }
368
369 free(ent_path);
370
371 /* If completing command, do not match directories. */
372 if (!ent_stat.is_directory || !cs->is_command) {
373 asprintf(compl, "%s%c", dent->d_name,
374 ent_stat.is_directory ? '/' : ' ');
375 cs->last_compl = *compl;
376 if (*compl == NULL)
377 return ENOMEM;
378 }
379 }
380 }
381 }
382
383 if (*compl == NULL)
384 return ENOENT;
385
386 return EOK;
387}
388
389/** Finish completion operation. */
390static void compl_fini(void *state)
391{
392 compl_t *cs = (compl_t *) state;
393
394 if (cs->path_list != NULL) {
395 size_t i = 0;
396 while (cs->path_list[i] != NULL) {
397 free(cs->path_list[i]);
398 ++i;
399 }
400 free(cs->path_list);
401 }
402
403 if (cs->last_compl != NULL)
404 free(cs->last_compl);
405 if (cs->dir != NULL)
406 closedir(cs->dir);
407
408 free(cs->prefix);
409 free(cs);
410}
Note: See TracBrowser for help on using the repository browser.