source: mainline/uspace/app/bdsh/compl.c@ 41ff85b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 41ff85b was 41ff85b, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Skip directories that cannot be opened when completing. Fixes a bug where
the shell whould enter an endless loop if you tried to complete a file
residing in a nonexistent directory (or directory that cannot be opened).

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