File engine/cmds.c added (mode: 100644) (index 0000000..dca5975) |
|
1 |
|
/* vifm |
|
2 |
|
* Copyright (C) 2011 xaizek. |
|
3 |
|
* |
|
4 |
|
* This program is free software; you can redistribute it and/or modify |
|
5 |
|
* it under the terms of the GNU General Public License as published by |
|
6 |
|
* the Free Software Foundation; either version 2 of the License, or |
|
7 |
|
* (at your option) any later version. |
|
8 |
|
* |
|
9 |
|
* This program is distributed in the hope that it will be useful, |
|
10 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
|
* GNU General Public License for more details. |
|
13 |
|
* |
|
14 |
|
* You should have received a copy of the GNU General Public License |
|
15 |
|
* along with this program; if not, write to the Free Software |
|
16 |
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
17 |
|
*/ |
|
18 |
|
|
|
19 |
|
#include "cmds.h" |
|
20 |
|
|
|
21 |
|
#include <assert.h> /* assert() */ |
|
22 |
|
#include <ctype.h> /* isalnum() isalpha() isdigit() isspace() */ |
|
23 |
|
#include <stddef.h> /* NULL size_t */ |
|
24 |
|
#include <stdio.h> |
|
25 |
|
#include <stdlib.h> /* calloc() malloc() free() realloc() */ |
|
26 |
|
#include <string.h> /* strdup() */ |
|
27 |
|
|
|
28 |
|
#include "../compat/reallocarray.h" |
|
29 |
|
#include "../utils/darray.h" |
|
30 |
|
#include "../utils/macros.h" |
|
31 |
|
#include "../utils/str.h" |
|
32 |
|
#include "../utils/string_array.h" |
|
33 |
|
#include "../utils/test_helpers.h" |
|
34 |
|
#include "../utils/utils.h" |
|
35 |
|
#include "completion.h" |
|
36 |
|
|
|
37 |
|
#define MAX_CMD_RECURSION 16 |
|
38 |
|
#define MAX_CMD_NAME_LEN 256 |
|
39 |
|
#define INVALID_MARK -4096 |
|
40 |
|
|
|
41 |
|
typedef struct |
|
42 |
|
{ |
|
43 |
|
cmd_t head; |
|
44 |
|
cmd_add_t user_cmd_handler; |
|
45 |
|
cmd_handler command_handler; |
|
46 |
|
int custom_cmd_count; /* Number of non-builtin commands. */ |
|
47 |
|
} |
|
48 |
|
inner_t; |
|
49 |
|
|
|
50 |
|
/* List of characters, which are treated as range separators. */ |
|
51 |
|
static const char *RANGE_SEPARATORS = ",;"; |
|
52 |
|
|
|
53 |
|
static inner_t *inner; |
|
54 |
|
static cmds_conf_t *cmds_conf; |
|
55 |
|
|
|
56 |
|
static const char * correct_limit(const char cmd[], cmd_info_t *cmd_info); |
|
57 |
|
static int udf_is_ambiguous(const char name[]); |
|
58 |
|
static const char * parse_tail(cmd_t *cur, const char cmd[], |
|
59 |
|
cmd_info_t *cmd_info); |
|
60 |
|
static const char * get_cmd_name(const char cmd[], char buf[], size_t buf_len); |
|
61 |
|
static void init_cmd_info(cmd_info_t *cmd_info); |
|
62 |
|
static const char * skip_prefix_commands(const char cmd[]); |
|
63 |
|
static cmd_t * find_cmd(const char name[]); |
|
64 |
|
static cmd_t * find_cmd_advance(cmd_t *cmd, const char name[]); |
|
65 |
|
static int find_cmd_match(cmd_t *cmd, const char name[]); |
|
66 |
|
static const char * parse_range(const char cmd[], cmd_info_t *cmd_info); |
|
67 |
|
static const char * parse_range_elem(const char cmd[], cmd_info_t *cmd_info, |
|
68 |
|
char last_sep); |
|
69 |
|
static int complete_cmd_args(cmd_t *cur, const char args[], |
|
70 |
|
cmd_info_t *cmd_info, void *arg); |
|
71 |
|
static void complete_cmd_name(const char cmd_name[], int user_only); |
|
72 |
|
TSTATIC int add_builtin_cmd(const char name[], CMD_TYPE type, |
|
73 |
|
const cmd_add_t *conf); |
|
74 |
|
static int is_builtin_like_name_ok(const char name[]); |
|
75 |
|
static int comclear_cmd(const cmd_info_t *cmd_info); |
|
76 |
|
static void remove_commands(CMD_TYPE type); |
|
77 |
|
static int command_cmd(const cmd_info_t *cmd_info); |
|
78 |
|
static void init_command_flags(cmd_t *cmd, int flags); |
|
79 |
|
static const char * get_user_cmd_name(const char cmd[], char buf[], |
|
80 |
|
size_t buf_len); |
|
81 |
|
static int is_valid_udc_name(const char name[]); |
|
82 |
|
static cmd_t * insert_cmd(cmd_t *after); |
|
83 |
|
static int delcommand_cmd(const cmd_info_t *cmd_info); |
|
84 |
|
TSTATIC char ** dispatch_line(const char args[], int *count, char sep, |
|
85 |
|
int regexp, int quotes, int noescaping, int comments, int *last_arg, |
|
86 |
|
int (**positions)[2]); |
|
87 |
|
static int is_separator(char c, char sep); |
|
88 |
|
|
|
89 |
|
void |
|
90 |
|
vle_cmds_init(int udf, cmds_conf_t *conf) |
|
91 |
|
{ |
|
92 |
|
static cmd_add_t commands[] = { |
|
93 |
|
{ |
|
94 |
|
.name = "comclear", .abbr = "comc", .handler = comclear_cmd, |
|
95 |
|
.id = COMCLEAR_CMD_ID, |
|
96 |
|
.descr = "remove all user-defined :commands", |
|
97 |
|
.flags = 0, |
|
98 |
|
.min_args = 0, .max_args = 0, |
|
99 |
|
}, { |
|
100 |
|
.name = "command", .abbr = "com", .handler = command_cmd, |
|
101 |
|
.id = COMMAND_CMD_ID, |
|
102 |
|
.descr = "display/define user-defined :command", |
|
103 |
|
.flags = HAS_EMARK, |
|
104 |
|
.min_args = 0, .max_args = NOT_DEF, |
|
105 |
|
}, { |
|
106 |
|
.name = "delcommand", .abbr = "delc", .handler = delcommand_cmd, |
|
107 |
|
.id = DELCOMMAND_CMD_ID, |
|
108 |
|
.descr = "undefine user-defined :command", |
|
109 |
|
.flags = HAS_EMARK, |
|
110 |
|
.min_args = 1, .max_args = 1, |
|
111 |
|
} |
|
112 |
|
}; |
|
113 |
|
|
|
114 |
|
cmds_conf = conf; |
|
115 |
|
inner = conf->inner; |
|
116 |
|
|
|
117 |
|
if(inner == NULL) |
|
118 |
|
{ |
|
119 |
|
assert(conf->complete_line != NULL); |
|
120 |
|
assert(conf->complete_args != NULL); |
|
121 |
|
assert(conf->swap_range != NULL); |
|
122 |
|
assert(conf->resolve_mark != NULL); |
|
123 |
|
assert(conf->expand_macros != NULL); |
|
124 |
|
assert(conf->expand_envvars != NULL); |
|
125 |
|
assert(conf->post != NULL); |
|
126 |
|
assert(conf->select_range != NULL); |
|
127 |
|
assert(conf->skip_at_beginning != NULL); |
|
128 |
|
conf->inner = calloc(1, sizeof(inner_t)); |
|
129 |
|
assert(conf->inner != NULL); |
|
130 |
|
inner = conf->inner; |
|
131 |
|
|
|
132 |
|
if(udf) |
|
133 |
|
vle_cmds_add(commands, ARRAY_LEN(commands)); |
|
134 |
|
} |
|
135 |
|
} |
|
136 |
|
|
|
137 |
|
void |
|
138 |
|
vle_cmds_reset(void) |
|
139 |
|
{ |
|
140 |
|
if(inner == NULL) |
|
141 |
|
{ |
|
142 |
|
return; |
|
143 |
|
} |
|
144 |
|
|
|
145 |
|
cmd_t *cur = inner->head.next; |
|
146 |
|
|
|
147 |
|
while(cur != NULL) |
|
148 |
|
{ |
|
149 |
|
cmd_t *next = cur->next; |
|
150 |
|
free(cur->cmd); |
|
151 |
|
free(cur->name); |
|
152 |
|
free(cur); |
|
153 |
|
cur = next; |
|
154 |
|
} |
|
155 |
|
|
|
156 |
|
inner->head.next = NULL; |
|
157 |
|
inner->user_cmd_handler.handler = NULL; |
|
158 |
|
|
|
159 |
|
free(inner); |
|
160 |
|
inner = NULL; |
|
161 |
|
cmds_conf->inner = NULL; |
|
162 |
|
} |
|
163 |
|
|
|
164 |
|
void |
|
165 |
|
vle_cmds_clear(void) |
|
166 |
|
{ |
|
167 |
|
remove_commands(USER_CMD); |
|
168 |
|
remove_commands(FOREIGN_CMD); |
|
169 |
|
} |
|
170 |
|
|
|
171 |
|
int |
|
172 |
|
vle_cmds_run(const char cmd[]) |
|
173 |
|
{ |
|
174 |
|
cmd_info_t cmd_info; |
|
175 |
|
char cmd_name[MAX_CMD_NAME_LEN + 1]; |
|
176 |
|
cmd_t *cur; |
|
177 |
|
const char *args; |
|
178 |
|
int execution_code; |
|
179 |
|
size_t last_arg_len; |
|
180 |
|
char *last_arg; |
|
181 |
|
int last_end = 0; |
|
182 |
|
cmds_conf_t *cc = cmds_conf; |
|
183 |
|
|
|
184 |
|
init_cmd_info(&cmd_info); |
|
185 |
|
cmd = parse_range(cmd, &cmd_info); |
|
186 |
|
if(cmd == NULL) |
|
187 |
|
{ |
|
188 |
|
if(cmd_info.end == INVALID_MARK) |
|
189 |
|
return CMDS_ERR_INVALID_RANGE; |
|
190 |
|
else |
|
191 |
|
return CMDS_ERR_INVALID_CMD; |
|
192 |
|
} |
|
193 |
|
|
|
194 |
|
if(*cmd != '\0' && cmd_info.end < cmd_info.begin) |
|
195 |
|
{ |
|
196 |
|
int t; |
|
197 |
|
|
|
198 |
|
if(!cc->swap_range()) |
|
199 |
|
return CMDS_ERR_INVALID_RANGE; |
|
200 |
|
|
|
201 |
|
t = cmd_info.end; |
|
202 |
|
cmd_info.end = cmd_info.begin; |
|
203 |
|
cmd_info.begin = t; |
|
204 |
|
} |
|
205 |
|
|
|
206 |
|
cmd = get_cmd_name(cmd, cmd_name, sizeof(cmd_name)); |
|
207 |
|
if(udf_is_ambiguous(cmd_name)) |
|
208 |
|
return CMDS_ERR_UDF_IS_AMBIGUOUS; |
|
209 |
|
|
|
210 |
|
cur = find_cmd(cmd_name); |
|
211 |
|
if(cur == NULL) |
|
212 |
|
return CMDS_ERR_INVALID_CMD; |
|
213 |
|
|
|
214 |
|
args = parse_tail(cur, cmd, &cmd_info); |
|
215 |
|
|
|
216 |
|
cmd_info.raw_args = strdup(args); |
|
217 |
|
|
|
218 |
|
/* Set background flag and remove background mark from raw arguments, when |
|
219 |
|
* command supports backgrounding. */ |
|
220 |
|
last_arg = vle_cmds_last_arg(cmd_info.raw_args, cur->quote, &last_arg_len); |
|
221 |
|
if(cur->bg && *last_arg == '&' && *vle_cmds_at_arg(last_arg + 1) == '\0') |
|
222 |
|
{ |
|
223 |
|
cmd_info.bg = 1; |
|
224 |
|
*last_arg = '\0'; |
|
225 |
|
} |
|
226 |
|
|
|
227 |
|
if(cur->select) |
|
228 |
|
{ |
|
229 |
|
cc->select_range(cur->id, &cmd_info); |
|
230 |
|
} |
|
231 |
|
|
|
232 |
|
if(cur->macros_for_cmd || cur->macros_for_shell) |
|
233 |
|
{ |
|
234 |
|
cmd_info.args = cc->expand_macros(cmd_info.raw_args, cur->macros_for_shell, |
|
235 |
|
&cmd_info.usr1, &cmd_info.usr2); |
|
236 |
|
} |
|
237 |
|
if(cur->envvars) |
|
238 |
|
{ |
|
239 |
|
char *const p = cmd_info.args; |
|
240 |
|
cmd_info.args = cc->expand_envvars(p ? p : cmd_info.raw_args); |
|
241 |
|
free(p); |
|
242 |
|
} |
|
243 |
|
if(cmd_info.args == NULL) |
|
244 |
|
{ |
|
245 |
|
cmd_info.args = strdup(cmd_info.raw_args); |
|
246 |
|
} |
|
247 |
|
|
|
248 |
|
cmd_info.argv = dispatch_line(cmd_info.args, &cmd_info.argc, cmd_info.sep, |
|
249 |
|
cur->regexp, cur->quote, cur->noescaping, cur->comment, NULL, |
|
250 |
|
&cmd_info.argvp); |
|
251 |
|
if(cmd_info.argc > 0) |
|
252 |
|
{ |
|
253 |
|
last_end = cmd_info.argvp[cmd_info.argc - 1][1]; |
|
254 |
|
} |
|
255 |
|
cmd_info.args[last_end] = '\0'; |
|
256 |
|
|
|
257 |
|
/* TODO: extract a method that would check all these error conditions. */ |
|
258 |
|
if((cmd_info.begin != NOT_DEF || cmd_info.end != NOT_DEF) && !cur->range) |
|
259 |
|
{ |
|
260 |
|
execution_code = CMDS_ERR_NO_RANGE_ALLOWED; |
|
261 |
|
} |
|
262 |
|
else if(cmd_info.argc < 0) |
|
263 |
|
{ |
|
264 |
|
execution_code = CMDS_ERR_INVALID_ARG; |
|
265 |
|
} |
|
266 |
|
else if(cmd_info.emark && !cur->emark) |
|
267 |
|
{ |
|
268 |
|
execution_code = CMDS_ERR_NO_BANG_ALLOWED; |
|
269 |
|
} |
|
270 |
|
else if(cmd_info.qmark && !cur->qmark) |
|
271 |
|
{ |
|
272 |
|
execution_code = CMDS_ERR_NO_QMARK_ALLOWED; |
|
273 |
|
} |
|
274 |
|
else if(cmd_info.qmark && !cur->args_after_qmark && *cmd_info.args != '\0') |
|
275 |
|
{ |
|
276 |
|
execution_code = CMDS_ERR_TRAILING_CHARS; |
|
277 |
|
} |
|
278 |
|
else if(cmd_info.argc < cur->min_args) |
|
279 |
|
{ |
|
280 |
|
execution_code = CMDS_ERR_TOO_FEW_ARGS; |
|
281 |
|
} |
|
282 |
|
else if(cmd_info.argc > cur->max_args && cur->max_args != NOT_DEF) |
|
283 |
|
{ |
|
284 |
|
execution_code = CMDS_ERR_TRAILING_CHARS; |
|
285 |
|
} |
|
286 |
|
else if(cur->passed > MAX_CMD_RECURSION) |
|
287 |
|
{ |
|
288 |
|
execution_code = CMDS_ERR_LOOP; |
|
289 |
|
} |
|
290 |
|
else |
|
291 |
|
{ |
|
292 |
|
++cur->passed; |
|
293 |
|
|
|
294 |
|
if(cur->type == USER_CMD) |
|
295 |
|
{ |
|
296 |
|
cmd_info.user_cmd = cur->name; |
|
297 |
|
cmd_info.user_action = cur->cmd; |
|
298 |
|
execution_code = inner->user_cmd_handler.handler(&cmd_info); |
|
299 |
|
} |
|
300 |
|
else |
|
301 |
|
{ |
|
302 |
|
cmd_info.user_data = cur->user_data; |
|
303 |
|
execution_code = cur->handler(&cmd_info); |
|
304 |
|
} |
|
305 |
|
|
|
306 |
|
cc->post(cur->id); |
|
307 |
|
if(--cur->passed == 0 && cur->deleted) |
|
308 |
|
{ |
|
309 |
|
free(cur); |
|
310 |
|
} |
|
311 |
|
} |
|
312 |
|
|
|
313 |
|
free(cmd_info.raw_args); |
|
314 |
|
free(cmd_info.args); |
|
315 |
|
free_string_array(cmd_info.argv, cmd_info.argc); |
|
316 |
|
free(cmd_info.argvp); |
|
317 |
|
|
|
318 |
|
return execution_code; |
|
319 |
|
} |
|
320 |
|
|
|
321 |
|
/* Applies limit shifts and ensures that its value is not out of bounds. |
|
322 |
|
* Returns pointer to part of string after parsed piece. */ |
|
323 |
|
static const char * |
|
324 |
|
correct_limit(const char cmd[], cmd_info_t *cmd_info) |
|
325 |
|
{ |
|
326 |
|
cmd_info->count = (cmd_info->end == NOT_DEF) ? 1 : (cmd_info->end + 1); |
|
327 |
|
|
|
328 |
|
while(*cmd == '+' || *cmd == '-') |
|
329 |
|
{ |
|
330 |
|
int n = 1; |
|
331 |
|
int plus = *cmd == '+'; |
|
332 |
|
cmd++; |
|
333 |
|
if(isdigit(*cmd)) |
|
334 |
|
{ |
|
335 |
|
char *p; |
|
336 |
|
n = strtol(cmd, &p, 10); |
|
337 |
|
cmd = p; |
|
338 |
|
} |
|
339 |
|
|
|
340 |
|
if(plus) |
|
341 |
|
{ |
|
342 |
|
cmd_info->end += n; |
|
343 |
|
cmd_info->count += n; |
|
344 |
|
} |
|
345 |
|
else |
|
346 |
|
{ |
|
347 |
|
cmd_info->end -= n; |
|
348 |
|
cmd_info->count -= n; |
|
349 |
|
} |
|
350 |
|
} |
|
351 |
|
|
|
352 |
|
if(cmd_info->end < 0) |
|
353 |
|
cmd_info->end = 0; |
|
354 |
|
if(cmd_info->end > cmds_conf->end) |
|
355 |
|
cmd_info->end = cmds_conf->end; |
|
356 |
|
|
|
357 |
|
return cmd; |
|
358 |
|
} |
|
359 |
|
|
|
360 |
|
static int |
|
361 |
|
udf_is_ambiguous(const char name[]) |
|
362 |
|
{ |
|
363 |
|
size_t len; |
|
364 |
|
int count; |
|
365 |
|
cmd_t *cur; |
|
366 |
|
|
|
367 |
|
len = strlen(name); |
|
368 |
|
count = 0; |
|
369 |
|
cur = inner->head.next; |
|
370 |
|
while(cur != NULL) |
|
371 |
|
{ |
|
372 |
|
int cmp; |
|
373 |
|
|
|
374 |
|
cmp = strncmp(cur->name, name, len); |
|
375 |
|
if(cmp == 0) |
|
376 |
|
{ |
|
377 |
|
if(cur->name[len] == '\0') |
|
378 |
|
return 0; |
|
379 |
|
if(cur->type == USER_CMD) |
|
380 |
|
{ |
|
381 |
|
char c = cur->name[strlen(cur->name) - 1]; |
|
382 |
|
if(c != '!' && c != '?') |
|
383 |
|
count++; |
|
384 |
|
} |
|
385 |
|
} |
|
386 |
|
else if(cmp > 0) |
|
387 |
|
{ |
|
388 |
|
break; |
|
389 |
|
} |
|
390 |
|
|
|
391 |
|
cur = cur->next; |
|
392 |
|
} |
|
393 |
|
return (count > 1); |
|
394 |
|
} |
|
395 |
|
|
|
396 |
|
static const char * |
|
397 |
|
parse_tail(cmd_t *cur, const char cmd[], cmd_info_t *cmd_info) |
|
398 |
|
{ |
|
399 |
|
if(*cmd == '!' && (!cur->cust_sep || cur->emark)) |
|
400 |
|
{ |
|
401 |
|
cmd_info->emark = 1; |
|
402 |
|
cmd++; |
|
403 |
|
} |
|
404 |
|
else if(*cmd == '?' && (!cur->cust_sep || cur->qmark)) |
|
405 |
|
{ |
|
406 |
|
cmd_info->qmark = 1; |
|
407 |
|
cmd++; |
|
408 |
|
} |
|
409 |
|
|
|
410 |
|
if(*cmd != '\0' && !isspace(*cmd)) |
|
411 |
|
{ |
|
412 |
|
if(cur->cust_sep) |
|
413 |
|
{ |
|
414 |
|
cmd_info->sep = isspace(*cmd) ? ' ' : *cmd; |
|
415 |
|
} |
|
416 |
|
return cmd; |
|
417 |
|
} |
|
418 |
|
|
|
419 |
|
while(is_separator(*cmd, cmd_info->sep)) |
|
420 |
|
{ |
|
421 |
|
++cmd; |
|
422 |
|
} |
|
423 |
|
|
|
424 |
|
return cmd; |
|
425 |
|
} |
|
426 |
|
|
|
427 |
|
int |
|
428 |
|
vle_cmds_identify(const char cmd[]) |
|
429 |
|
{ |
|
430 |
|
cmd_info_t info; |
|
431 |
|
const cmd_t *const c = vle_cmds_parse(cmd, &info); |
|
432 |
|
return (c == NULL ? -1 : c->id); |
|
433 |
|
} |
|
434 |
|
|
|
435 |
|
const char * |
|
436 |
|
vle_cmds_args(const char cmd[]) |
|
437 |
|
{ |
|
438 |
|
cmd_info_t info = {}; |
|
439 |
|
(void)vle_cmds_parse(cmd, &info); |
|
440 |
|
return info.raw_args; |
|
441 |
|
} |
|
442 |
|
|
|
443 |
|
/* Initializes command info structure with reasonable defaults. */ |
|
444 |
|
static void |
|
445 |
|
init_cmd_info(cmd_info_t *cmd_info) |
|
446 |
|
{ |
|
447 |
|
cmd_info->begin = NOT_DEF; |
|
448 |
|
cmd_info->end = NOT_DEF; |
|
449 |
|
cmd_info->count = NOT_DEF; |
|
450 |
|
cmd_info->emark = 0; |
|
451 |
|
cmd_info->qmark = 0; |
|
452 |
|
cmd_info->raw_args = NULL; |
|
453 |
|
cmd_info->args = NULL; |
|
454 |
|
cmd_info->argc = 0; |
|
455 |
|
cmd_info->argv = NULL; |
|
456 |
|
cmd_info->user_cmd = NULL; |
|
457 |
|
cmd_info->user_action = NULL; |
|
458 |
|
cmd_info->user_data = NULL; |
|
459 |
|
cmd_info->sep = ' '; |
|
460 |
|
cmd_info->bg = 0; |
|
461 |
|
cmd_info->usr1 = 0; |
|
462 |
|
cmd_info->usr2 = 0; |
|
463 |
|
} |
|
464 |
|
|
|
465 |
|
const cmd_t * |
|
466 |
|
vle_cmds_parse(const char cmd[], cmd_info_t *info) |
|
467 |
|
{ |
|
468 |
|
cmd_info_t cmd_info; |
|
469 |
|
char cmd_name[MAX_CMD_NAME_LEN + 1]; |
|
470 |
|
cmd_t *c; |
|
471 |
|
|
|
472 |
|
init_cmd_info(&cmd_info); |
|
473 |
|
|
|
474 |
|
cmd = parse_range(cmd, &cmd_info); |
|
475 |
|
if(cmd == NULL) |
|
476 |
|
{ |
|
477 |
|
return NULL; |
|
478 |
|
} |
|
479 |
|
|
|
480 |
|
cmd = get_cmd_name(cmd, cmd_name, sizeof(cmd_name)); |
|
481 |
|
c = find_cmd(cmd_name); |
|
482 |
|
if(c == NULL) |
|
483 |
|
{ |
|
484 |
|
return NULL; |
|
485 |
|
} |
|
486 |
|
|
|
487 |
|
cmd_info.raw_args = (char *)parse_tail(c, cmd, &cmd_info); |
|
488 |
|
|
|
489 |
|
*info = cmd_info; |
|
490 |
|
return c; |
|
491 |
|
} |
|
492 |
|
|
|
493 |
|
int |
|
494 |
|
vle_cmds_complete(const char cmd[], void *arg) |
|
495 |
|
{ |
|
496 |
|
cmd_info_t cmd_info; |
|
497 |
|
const char *begin, *cmd_name_pos; |
|
498 |
|
size_t prefix_len; |
|
499 |
|
|
|
500 |
|
begin = cmd; |
|
501 |
|
cmd = skip_prefix_commands(begin); |
|
502 |
|
prefix_len = cmd - begin; |
|
503 |
|
|
|
504 |
|
init_cmd_info(&cmd_info); |
|
505 |
|
|
|
506 |
|
cmd_name_pos = parse_range(cmd, &cmd_info); |
|
507 |
|
if(cmd_name_pos != NULL) |
|
508 |
|
{ |
|
509 |
|
char cmd_name[MAX_CMD_NAME_LEN + 1]; |
|
510 |
|
const char *args; |
|
511 |
|
cmd_t *cur; |
|
512 |
|
|
|
513 |
|
args = get_cmd_name(cmd_name_pos, cmd_name, sizeof(cmd_name)); |
|
514 |
|
cur = find_cmd(cmd_name); |
|
515 |
|
|
|
516 |
|
if(*args == '\0' && strcmp(cmd_name, "!") != 0) |
|
517 |
|
{ |
|
518 |
|
complete_cmd_name(cmd_name, 0); |
|
519 |
|
|
|
520 |
|
if(vle_compl_get_count() == 0) |
|
521 |
|
{ |
|
522 |
|
prefix_len += cmds_conf->complete_line(cmd_name, arg); |
|
523 |
|
} |
|
524 |
|
else |
|
525 |
|
{ |
|
526 |
|
vle_compl_add_last_match(cmd_name); |
|
527 |
|
prefix_len += cmd_name_pos - cmd; |
|
528 |
|
} |
|
529 |
|
} |
|
530 |
|
else if(cur == NULL || cur->name[0] == '\0') |
|
531 |
|
{ |
|
532 |
|
/* Handle empty command specially here. */ |
|
533 |
|
prefix_len += cmds_conf->complete_line(cmd, arg); |
|
534 |
|
} |
|
535 |
|
else |
|
536 |
|
{ |
|
537 |
|
prefix_len += args - cmd; |
|
538 |
|
cmd_info.user_data = cur->user_data; |
|
539 |
|
prefix_len += complete_cmd_args(cur, args, &cmd_info, arg); |
|
540 |
|
} |
|
541 |
|
} |
|
542 |
|
|
|
543 |
|
return prefix_len; |
|
544 |
|
} |
|
545 |
|
|
|
546 |
|
/* Skips prefix commands (which can be followed by an arbitrary command) at the |
|
547 |
|
* beginning of command-line. */ |
|
548 |
|
static const char * |
|
549 |
|
skip_prefix_commands(const char cmd[]) |
|
550 |
|
{ |
|
551 |
|
cmd_info_t cmd_info; |
|
552 |
|
const char *cmd_name_pos; |
|
553 |
|
|
|
554 |
|
init_cmd_info(&cmd_info); |
|
555 |
|
|
|
556 |
|
cmd_name_pos = parse_range(cmd, &cmd_info); |
|
557 |
|
if(cmd_name_pos != NULL) |
|
558 |
|
{ |
|
559 |
|
char cmd_name[MAX_CMD_NAME_LEN + 1]; |
|
560 |
|
const char *args; |
|
561 |
|
cmd_t *cur; |
|
562 |
|
|
|
563 |
|
args = get_cmd_name(cmd_name_pos, cmd_name, sizeof(cmd_name)); |
|
564 |
|
cur = find_cmd(cmd_name); |
|
565 |
|
while(cur != NULL && *args != '\0') |
|
566 |
|
{ |
|
567 |
|
int offset = cmds_conf->skip_at_beginning(cur->id, args); |
|
568 |
|
if(offset >= 0) |
|
569 |
|
{ |
|
570 |
|
int delta = (args - cmd) + offset; |
|
571 |
|
cmd += delta; |
|
572 |
|
init_cmd_info(&cmd_info); |
|
573 |
|
cmd_name_pos = parse_range(cmd, &cmd_info); |
|
574 |
|
if(cmd_name_pos == NULL) |
|
575 |
|
{ |
|
576 |
|
break; |
|
577 |
|
} |
|
578 |
|
args = get_cmd_name(cmd_name_pos, cmd_name, sizeof(cmd_name)); |
|
579 |
|
} |
|
580 |
|
else |
|
581 |
|
{ |
|
582 |
|
break; |
|
583 |
|
} |
|
584 |
|
cur = find_cmd(cmd_name); |
|
585 |
|
} |
|
586 |
|
} |
|
587 |
|
return cmd; |
|
588 |
|
} |
|
589 |
|
|
|
590 |
|
/* Looks up a command by its name. */ |
|
591 |
|
static cmd_t * |
|
592 |
|
find_cmd(const char name[]) |
|
593 |
|
{ |
|
594 |
|
cmd_t *const cmd = find_cmd_advance(inner->head.next, name); |
|
595 |
|
return (find_cmd_match(cmd, name) ? cmd : NULL); |
|
596 |
|
} |
|
597 |
|
|
|
598 |
|
/* Advances to the first command whose name is not less than the parameter. |
|
599 |
|
* Returns advanced values (could be unchanged or NULL). */ |
|
600 |
|
static cmd_t * |
|
601 |
|
find_cmd_advance(cmd_t *cmd, const char name[]) |
|
602 |
|
{ |
|
603 |
|
while(cmd != NULL && strcmp(cmd->name, name) < 0) |
|
604 |
|
{ |
|
605 |
|
cmd = cmd->next; |
|
606 |
|
} |
|
607 |
|
return cmd; |
|
608 |
|
} |
|
609 |
|
|
|
610 |
|
/* Checks that command search was a success. Returns non-zero if so, otherwise |
|
611 |
|
* zero is returned. */ |
|
612 |
|
static int |
|
613 |
|
find_cmd_match(cmd_t *cmd, const char name[]) |
|
614 |
|
{ |
|
615 |
|
if(cmd == NULL) |
|
616 |
|
{ |
|
617 |
|
return 0; |
|
618 |
|
} |
|
619 |
|
|
|
620 |
|
if(name[0] == '\0') |
|
621 |
|
{ |
|
622 |
|
return (cmd->name[0] == '\0'); |
|
623 |
|
} |
|
624 |
|
|
|
625 |
|
return (strncmp(name, cmd->name, strlen(name)) == 0); |
|
626 |
|
} |
|
627 |
|
|
|
628 |
|
/* Parses whole command range (e.g. "<val>;+<val>,,-<val>"). Returns advanced |
|
629 |
|
* value of cmd when parsing is successful, otherwise NULL is returned. */ |
|
630 |
|
static const char * |
|
631 |
|
parse_range(const char cmd[], cmd_info_t *cmd_info) |
|
632 |
|
{ |
|
633 |
|
char last_sep; |
|
634 |
|
|
|
635 |
|
cmd = vle_cmds_at_arg(cmd); |
|
636 |
|
|
|
637 |
|
if(isalpha(*cmd) || *cmd == '!' || *cmd == '\0') |
|
638 |
|
{ |
|
639 |
|
return cmd; |
|
640 |
|
} |
|
641 |
|
|
|
642 |
|
last_sep = '\0'; |
|
643 |
|
while(*cmd != '\0') |
|
644 |
|
{ |
|
645 |
|
cmd_info->begin = cmd_info->end; |
|
646 |
|
|
|
647 |
|
cmd = parse_range_elem(cmd, cmd_info, last_sep); |
|
648 |
|
if(cmd == NULL) |
|
649 |
|
{ |
|
650 |
|
return NULL; |
|
651 |
|
} |
|
652 |
|
|
|
653 |
|
cmd = correct_limit(cmd, cmd_info); |
|
654 |
|
|
|
655 |
|
if(cmd_info->begin == NOT_DEF) |
|
656 |
|
{ |
|
657 |
|
cmd_info->begin = cmd_info->end; |
|
658 |
|
} |
|
659 |
|
|
|
660 |
|
cmd = vle_cmds_at_arg(cmd); |
|
661 |
|
|
|
662 |
|
if(!char_is_one_of(RANGE_SEPARATORS, *cmd)) |
|
663 |
|
{ |
|
664 |
|
break; |
|
665 |
|
} |
|
666 |
|
|
|
667 |
|
last_sep = *cmd; |
|
668 |
|
cmd++; |
|
669 |
|
|
|
670 |
|
cmd = vle_cmds_at_arg(cmd); |
|
671 |
|
} |
|
672 |
|
|
|
673 |
|
return cmd; |
|
674 |
|
} |
|
675 |
|
|
|
676 |
|
/* Parses single element of a command range (e.g. any of <el> in |
|
677 |
|
* "<el>;<el>,<el>"). Returns advanced value of cmd when parsing is successful, |
|
678 |
|
* otherwise NULL is returned. */ |
|
679 |
|
static const char * |
|
680 |
|
parse_range_elem(const char cmd[], cmd_info_t *cmd_info, char last_sep) |
|
681 |
|
{ |
|
682 |
|
if(cmd[0] == '%') |
|
683 |
|
{ |
|
684 |
|
cmd_info->begin = cmds_conf->begin; |
|
685 |
|
cmd_info->end = cmds_conf->end; |
|
686 |
|
cmd++; |
|
687 |
|
} |
|
688 |
|
else if(cmd[0] == '$') |
|
689 |
|
{ |
|
690 |
|
cmd_info->end = cmds_conf->end; |
|
691 |
|
cmd++; |
|
692 |
|
} |
|
693 |
|
else if(cmd[0] == '.') |
|
694 |
|
{ |
|
695 |
|
cmd_info->end = cmds_conf->current; |
|
696 |
|
cmd++; |
|
697 |
|
} |
|
698 |
|
else if(char_is_one_of(RANGE_SEPARATORS, *cmd)) |
|
699 |
|
{ |
|
700 |
|
cmd_info->end = cmds_conf->current; |
|
701 |
|
} |
|
702 |
|
else if(isalpha(*cmd)) |
|
703 |
|
{ |
|
704 |
|
cmd_info->end = cmds_conf->current; |
|
705 |
|
} |
|
706 |
|
else if(isdigit(*cmd)) |
|
707 |
|
{ |
|
708 |
|
char *p; |
|
709 |
|
cmd_info->end = strtol(cmd, &p, 10) - 1; |
|
710 |
|
if(cmd_info->end < cmds_conf->begin) |
|
711 |
|
cmd_info->end = cmds_conf->begin; |
|
712 |
|
cmd = p; |
|
713 |
|
} |
|
714 |
|
else if(*cmd == '\'') |
|
715 |
|
{ |
|
716 |
|
char mark; |
|
717 |
|
cmd++; |
|
718 |
|
mark = *cmd++; |
|
719 |
|
cmd_info->end = cmds_conf->resolve_mark(mark); |
|
720 |
|
if(cmd_info->end < 0) |
|
721 |
|
{ |
|
722 |
|
cmd_info->end = INVALID_MARK; |
|
723 |
|
return NULL; |
|
724 |
|
} |
|
725 |
|
} |
|
726 |
|
else if(*cmd == '+' || *cmd == '-') |
|
727 |
|
{ |
|
728 |
|
/* Do nothing after semicolon, because in this case +/- are adjusting not |
|
729 |
|
* base current cursor position, but base end of the range. */ |
|
730 |
|
if(last_sep != ';') |
|
731 |
|
{ |
|
732 |
|
cmd_info->end = cmds_conf->current; |
|
733 |
|
} |
|
734 |
|
} |
|
735 |
|
else |
|
736 |
|
{ |
|
737 |
|
return NULL; |
|
738 |
|
} |
|
739 |
|
|
|
740 |
|
return cmd; |
|
741 |
|
} |
|
742 |
|
|
|
743 |
|
static const char * |
|
744 |
|
get_cmd_name(const char cmd[], char buf[], size_t buf_len) |
|
745 |
|
{ |
|
746 |
|
assert(buf_len != 0 && "The buffer is expected to be of size > 0."); |
|
747 |
|
|
|
748 |
|
if(cmd[0] == '!') |
|
749 |
|
{ |
|
750 |
|
strcpy(buf, "!"); |
|
751 |
|
cmd = vle_cmds_at_arg(cmd + 1); |
|
752 |
|
return cmd; |
|
753 |
|
} |
|
754 |
|
|
|
755 |
|
cmd_t *c = inner->head.next; |
|
756 |
|
|
|
757 |
|
const char *t = cmd; |
|
758 |
|
if(isalpha(*t)) |
|
759 |
|
++t; |
|
760 |
|
while(isalnum(*t)) |
|
761 |
|
{ |
|
762 |
|
if(isdigit(*t)) |
|
763 |
|
{ |
|
764 |
|
size_t len = MIN((size_t)(t - cmd), buf_len - 1); |
|
765 |
|
copy_str(buf, len + 1, cmd); |
|
766 |
|
|
|
767 |
|
c = find_cmd_advance(c, buf); |
|
768 |
|
if(find_cmd_match(c, buf) && c->name[len] != *t) |
|
769 |
|
{ |
|
770 |
|
break; |
|
771 |
|
} |
|
772 |
|
} |
|
773 |
|
++t; |
|
774 |
|
} |
|
775 |
|
|
|
776 |
|
size_t len = MIN((size_t)(t - cmd), buf_len - 1); |
|
777 |
|
strncpy(buf, cmd, len); |
|
778 |
|
buf[len] = '\0'; |
|
779 |
|
if(*t == '?' || *t == '!') |
|
780 |
|
{ |
|
781 |
|
int cmp; |
|
782 |
|
cmd_t *cur; |
|
783 |
|
|
|
784 |
|
cur = inner->head.next; |
|
785 |
|
while(cur != NULL && (cmp = strncmp(cur->name, buf, len)) <= 0) |
|
786 |
|
{ |
|
787 |
|
if(cmp == 0) |
|
788 |
|
{ |
|
789 |
|
/* Complete match for a builtin with a custom separator. */ |
|
790 |
|
if(cur->cust_sep && cur->name[len] == '\0') |
|
791 |
|
{ |
|
792 |
|
strncpy(buf, cur->name, buf_len); |
|
793 |
|
break; |
|
794 |
|
} |
|
795 |
|
/* Check for user-defined command that ends with the char ('!' or |
|
796 |
|
* '?', see above). */ |
|
797 |
|
if(cur->type == USER_CMD && cur->name[strlen(cur->name) - 1] == *t) |
|
798 |
|
{ |
|
799 |
|
strncpy(buf, cur->name, buf_len); |
|
800 |
|
break; |
|
801 |
|
} |
|
802 |
|
/* Or builtin abbreviation that supports the mark. */ |
|
803 |
|
if(cur->type == BUILTIN_ABBR && |
|
804 |
|
((*t == '!' && cur->emark) || (*t == '?' && cur->qmark))) |
|
805 |
|
{ |
|
806 |
|
strncpy(buf, cur->name, buf_len); |
|
807 |
|
break; |
|
808 |
|
} |
|
809 |
|
} |
|
810 |
|
cur = cur->next; |
|
811 |
|
} |
|
812 |
|
|
|
813 |
|
if(cur != NULL && cur->type == USER_CMD && |
|
814 |
|
strncmp(cur->name, buf, len) == 0) |
|
815 |
|
{ |
|
816 |
|
/* For user-defined commands, the char is part of the name. */ |
|
817 |
|
++t; |
|
818 |
|
} |
|
819 |
|
} |
|
820 |
|
|
|
821 |
|
return t; |
|
822 |
|
} |
|
823 |
|
|
|
824 |
|
/* Completes command arguments. Returns offset at which completion was done. */ |
|
825 |
|
static int |
|
826 |
|
complete_cmd_args(cmd_t *cur, const char args[], cmd_info_t *cmd_info, |
|
827 |
|
void *arg) |
|
828 |
|
{ |
|
829 |
|
const char *tmp_args = args; |
|
830 |
|
int result = 0; |
|
831 |
|
|
|
832 |
|
if(cur->id >= NO_COMPLETION_BOUNDARY && cur->id < 0) |
|
833 |
|
return 0; |
|
834 |
|
|
|
835 |
|
args = parse_tail(cur, tmp_args, cmd_info); |
|
836 |
|
args = vle_cmds_at_arg(args); |
|
837 |
|
result += args - tmp_args; |
|
838 |
|
|
|
839 |
|
if(cur->id == COMMAND_CMD_ID || cur->id == DELCOMMAND_CMD_ID) |
|
840 |
|
{ |
|
841 |
|
const char *arg; |
|
842 |
|
|
|
843 |
|
arg = strrchr(args, ' '); |
|
844 |
|
arg = (arg == NULL) ? args : (arg + 1); |
|
845 |
|
|
|
846 |
|
complete_cmd_name(arg, 1); |
|
847 |
|
vle_compl_add_last_match(arg); |
|
848 |
|
|
|
849 |
|
result += arg - args; |
|
850 |
|
} |
|
851 |
|
else |
|
852 |
|
{ |
|
853 |
|
int argc; |
|
854 |
|
char **argv; |
|
855 |
|
int (*argvp)[2]; |
|
856 |
|
int last_arg = 0; |
|
857 |
|
|
|
858 |
|
argv = dispatch_line(args, &argc, ' ', 0, 1, 0, 0, &last_arg, &argvp); |
|
859 |
|
|
|
860 |
|
cmd_info->args = (char *)args; |
|
861 |
|
cmd_info->argc = argc; |
|
862 |
|
cmd_info->argv = argv; |
|
863 |
|
result += cmds_conf->complete_args(cur->id, cmd_info, last_arg, arg); |
|
864 |
|
|
|
865 |
|
free_string_array(argv, argc); |
|
866 |
|
free(argvp); |
|
867 |
|
} |
|
868 |
|
return result; |
|
869 |
|
} |
|
870 |
|
|
|
871 |
|
static void |
|
872 |
|
complete_cmd_name(const char cmd_name[], int user_only) |
|
873 |
|
{ |
|
874 |
|
cmd_t *cur; |
|
875 |
|
size_t len; |
|
876 |
|
|
|
877 |
|
cur = inner->head.next; |
|
878 |
|
while(cur != NULL && strcmp(cur->name, cmd_name) < 0) |
|
879 |
|
cur = cur->next; |
|
880 |
|
|
|
881 |
|
len = strlen(cmd_name); |
|
882 |
|
while(cur != NULL && strncmp(cur->name, cmd_name, len) == 0) |
|
883 |
|
{ |
|
884 |
|
if(cur->type == BUILTIN_ABBR) |
|
885 |
|
; |
|
886 |
|
else if(cur->type != USER_CMD && user_only) |
|
887 |
|
; |
|
888 |
|
else if(cur->name[0] == '\0') |
|
889 |
|
; |
|
890 |
|
else if(cur->type == USER_CMD && cur->descr == NULL) |
|
891 |
|
vle_compl_add_match(cur->name, cur->cmd); |
|
892 |
|
else |
|
893 |
|
vle_compl_add_match(cur->name, cur->descr); |
|
894 |
|
cur = cur->next; |
|
895 |
|
} |
|
896 |
|
} |
|
897 |
|
|
|
898 |
|
void |
|
899 |
|
vle_cmds_add(const cmd_add_t cmds[], int count) |
|
900 |
|
{ |
|
901 |
|
int i; |
|
902 |
|
for(i = 0; i < count; ++i) |
|
903 |
|
{ |
|
904 |
|
int ret_code; |
|
905 |
|
assert(cmds[i].min_args >= 0); |
|
906 |
|
assert(cmds[i].max_args == NOT_DEF || |
|
907 |
|
cmds[i].min_args <= cmds[i].max_args); |
|
908 |
|
ret_code = add_builtin_cmd(cmds[i].name, BUILTIN_CMD, &cmds[i]); |
|
909 |
|
assert(ret_code == 0); |
|
910 |
|
if(cmds[i].abbr != NULL) |
|
911 |
|
{ |
|
912 |
|
size_t full_len, short_len; |
|
913 |
|
char buf[strlen(cmds[i].name) + 1]; |
|
914 |
|
assert(starts_with(cmds[i].name, cmds[i].abbr) && |
|
915 |
|
"Abbreviation is consistent with full command"); |
|
916 |
|
strcpy(buf, cmds[i].name); |
|
917 |
|
full_len = strlen(buf); |
|
918 |
|
short_len = strlen(cmds[i].abbr); |
|
919 |
|
while(full_len > short_len) |
|
920 |
|
{ |
|
921 |
|
buf[--full_len] = '\0'; |
|
922 |
|
ret_code = add_builtin_cmd(buf, BUILTIN_ABBR, &cmds[i]); |
|
923 |
|
assert(ret_code == 0); |
|
924 |
|
} |
|
925 |
|
} |
|
926 |
|
(void)ret_code; |
|
927 |
|
} |
|
928 |
|
} |
|
929 |
|
|
|
930 |
|
int |
|
931 |
|
vle_cmds_add_foreign(const cmd_add_t *cmd) |
|
932 |
|
{ |
|
933 |
|
if(cmd->min_args < 0) |
|
934 |
|
{ |
|
935 |
|
return 1; |
|
936 |
|
} |
|
937 |
|
|
|
938 |
|
if(cmd->max_args != NOT_DEF && cmd->min_args > cmd->max_args) |
|
939 |
|
{ |
|
940 |
|
return 1; |
|
941 |
|
} |
|
942 |
|
|
|
943 |
|
if(!is_valid_udc_name(cmd->name)) |
|
944 |
|
{ |
|
945 |
|
return CMDS_ERR_INCORRECT_NAME; |
|
946 |
|
} |
|
947 |
|
|
|
948 |
|
int failure = (add_builtin_cmd(cmd->name, FOREIGN_CMD, cmd) != 0); |
|
949 |
|
if(!failure) |
|
950 |
|
{ |
|
951 |
|
++inner->custom_cmd_count; |
|
952 |
|
} |
|
953 |
|
return failure; |
|
954 |
|
} |
|
955 |
|
|
|
956 |
|
/* Returns non-zero on error */ |
|
957 |
|
TSTATIC int |
|
958 |
|
add_builtin_cmd(const char name[], CMD_TYPE type, const cmd_add_t *conf) |
|
959 |
|
{ |
|
960 |
|
int cmp; |
|
961 |
|
cmd_t *new; |
|
962 |
|
cmd_t *cur = &inner->head; |
|
963 |
|
|
|
964 |
|
if(strcmp(name, "<USERCMD>") == 0) |
|
965 |
|
{ |
|
966 |
|
if(inner->user_cmd_handler.handler != NULL) |
|
967 |
|
return -1; |
|
968 |
|
inner->user_cmd_handler = *conf; |
|
969 |
|
return 0; |
|
970 |
|
} |
|
971 |
|
|
|
972 |
|
if(!is_builtin_like_name_ok(name)) |
|
973 |
|
{ |
|
974 |
|
return -1; |
|
975 |
|
} |
|
976 |
|
|
|
977 |
|
cmp = -1; |
|
978 |
|
while(cur->next != NULL && (cmp = strcmp(cur->next->name, name)) < 0) |
|
979 |
|
{ |
|
980 |
|
cur = cur->next; |
|
981 |
|
} |
|
982 |
|
|
|
983 |
|
/* Command with the same name already exists. */ |
|
984 |
|
if(cmp == 0) |
|
985 |
|
{ |
|
986 |
|
if(strncmp(name, "command", strlen(name)) == 0) |
|
987 |
|
{ |
|
988 |
|
inner->command_handler = conf->handler; |
|
989 |
|
return 0; |
|
990 |
|
} |
|
991 |
|
return -1; |
|
992 |
|
} |
|
993 |
|
|
|
994 |
|
new = insert_cmd(cur); |
|
995 |
|
if(new == NULL) |
|
996 |
|
{ |
|
997 |
|
return -1; |
|
998 |
|
} |
|
999 |
|
|
|
1000 |
|
new->name = strdup(name); |
|
1001 |
|
new->descr = conf->descr; |
|
1002 |
|
new->id = conf->id; |
|
1003 |
|
new->user_data = conf->user_data; |
|
1004 |
|
new->handler = conf->handler; |
|
1005 |
|
new->type = type; |
|
1006 |
|
new->passed = 0; |
|
1007 |
|
new->cmd = NULL; |
|
1008 |
|
new->min_args = conf->min_args; |
|
1009 |
|
new->max_args = conf->max_args; |
|
1010 |
|
new->deleted = 0; |
|
1011 |
|
init_command_flags(new, conf->flags); |
|
1012 |
|
|
|
1013 |
|
return 0; |
|
1014 |
|
} |
|
1015 |
|
|
|
1016 |
|
/* Checks validity of a name for a builtin-like command. Returns non-zero if |
|
1017 |
|
* it's valid and zero otherwise. */ |
|
1018 |
|
static int |
|
1019 |
|
is_builtin_like_name_ok(const char name[]) |
|
1020 |
|
{ |
|
1021 |
|
if(name[0] == '\0' || strcmp(name, "!") == 0) |
|
1022 |
|
{ |
|
1023 |
|
return 1; |
|
1024 |
|
} |
|
1025 |
|
|
|
1026 |
|
if(!isalpha(name[0])) |
|
1027 |
|
{ |
|
1028 |
|
return 0; |
|
1029 |
|
} |
|
1030 |
|
|
|
1031 |
|
int i; |
|
1032 |
|
for(i = 1; name[i] != '\0'; ++i) |
|
1033 |
|
{ |
|
1034 |
|
if(!isalnum(name[i])) |
|
1035 |
|
{ |
|
1036 |
|
return 0; |
|
1037 |
|
} |
|
1038 |
|
} |
|
1039 |
|
|
|
1040 |
|
return 1; |
|
1041 |
|
} |
|
1042 |
|
|
|
1043 |
|
/* Implements :comclear builtin command provided by this unit. */ |
|
1044 |
|
static int |
|
1045 |
|
comclear_cmd(const cmd_info_t *cmd_info) |
|
1046 |
|
{ |
|
1047 |
|
remove_commands(USER_CMD); |
|
1048 |
|
return 0; |
|
1049 |
|
} |
|
1050 |
|
|
|
1051 |
|
/* Removes commands of the specified type. */ |
|
1052 |
|
static void |
|
1053 |
|
remove_commands(CMD_TYPE type) |
|
1054 |
|
{ |
|
1055 |
|
cmd_t *cur = &inner->head; |
|
1056 |
|
|
|
1057 |
|
while(cur->next != NULL) |
|
1058 |
|
{ |
|
1059 |
|
if(cur->next->type == type) |
|
1060 |
|
{ |
|
1061 |
|
cmd_t *this = cur->next; |
|
1062 |
|
cur->next = this->next; |
|
1063 |
|
|
|
1064 |
|
free(this->cmd); |
|
1065 |
|
free(this->name); |
|
1066 |
|
if(this->passed == 0) |
|
1067 |
|
{ |
|
1068 |
|
free(this); |
|
1069 |
|
} |
|
1070 |
|
else |
|
1071 |
|
{ |
|
1072 |
|
this->deleted = 1; |
|
1073 |
|
} |
|
1074 |
|
} |
|
1075 |
|
else |
|
1076 |
|
{ |
|
1077 |
|
cur = cur->next; |
|
1078 |
|
} |
|
1079 |
|
} |
|
1080 |
|
inner->custom_cmd_count = 0; |
|
1081 |
|
} |
|
1082 |
|
|
|
1083 |
|
/* Implements :command builtin command mostly provided by this unit. */ |
|
1084 |
|
static int |
|
1085 |
|
command_cmd(const cmd_info_t *cmd_info) |
|
1086 |
|
{ |
|
1087 |
|
if(cmd_info->argc < 2) |
|
1088 |
|
{ |
|
1089 |
|
if(inner->command_handler == NULL) |
|
1090 |
|
{ |
|
1091 |
|
return CMDS_ERR_TOO_FEW_ARGS; |
|
1092 |
|
} |
|
1093 |
|
return inner->command_handler(cmd_info); |
|
1094 |
|
} |
|
1095 |
|
|
|
1096 |
|
char name[MAX_CMD_NAME_LEN + 1]; |
|
1097 |
|
const char *body = get_user_cmd_name(cmd_info->args, name, sizeof(name)); |
|
1098 |
|
body = vle_cmds_at_arg(body); |
|
1099 |
|
|
|
1100 |
|
return vle_cmds_add_user(name, body, NULL, cmd_info->emark); |
|
1101 |
|
} |
|
1102 |
|
|
|
1103 |
|
int |
|
1104 |
|
vle_cmds_add_user(const char name[], const char body[], const char descr[], |
|
1105 |
|
int overwrite) |
|
1106 |
|
{ |
|
1107 |
|
if(body[0] == '\0') |
|
1108 |
|
{ |
|
1109 |
|
return CMDS_ERR_TOO_FEW_ARGS; |
|
1110 |
|
} |
|
1111 |
|
if(!is_valid_udc_name(name)) |
|
1112 |
|
{ |
|
1113 |
|
return CMDS_ERR_INCORRECT_NAME; |
|
1114 |
|
} |
|
1115 |
|
|
|
1116 |
|
size_t len = strlen(name); |
|
1117 |
|
int has_emark = (len > 0 && name[len - 1] == '!'); |
|
1118 |
|
int has_qmark = (len > 0 && name[len - 1] == '?'); |
|
1119 |
|
|
|
1120 |
|
int cmp = -1; |
|
1121 |
|
cmd_t *cur = &inner->head; |
|
1122 |
|
while(cur->next != NULL && (cmp = strcmp(cur->next->name, name)) < 0) |
|
1123 |
|
{ |
|
1124 |
|
int builtin_like = (cur->next->type == BUILTIN_CMD) |
|
1125 |
|
|| (cur->next->type == FOREIGN_CMD); |
|
1126 |
|
if(has_emark && builtin_like && cur->next->emark && |
|
1127 |
|
strncmp(name, cur->next->name, len - 1) == 0) |
|
1128 |
|
{ |
|
1129 |
|
cmp = 0; |
|
1130 |
|
break; |
|
1131 |
|
} |
|
1132 |
|
if(has_qmark && builtin_like && cur->next->qmark && |
|
1133 |
|
strncmp(name, cur->next->name, len - 1) == 0) |
|
1134 |
|
{ |
|
1135 |
|
cmp = 0; |
|
1136 |
|
break; |
|
1137 |
|
} |
|
1138 |
|
cur = cur->next; |
|
1139 |
|
} |
|
1140 |
|
|
|
1141 |
|
cmd_t *new; |
|
1142 |
|
if(cmp == 0) |
|
1143 |
|
{ |
|
1144 |
|
cur = cur->next; |
|
1145 |
|
if(cur->type == BUILTIN_CMD || cur->type == FOREIGN_CMD) |
|
1146 |
|
return CMDS_ERR_NO_BUILTIN_REDEFINE; |
|
1147 |
|
if(!overwrite) |
|
1148 |
|
return CMDS_ERR_NEED_BANG; |
|
1149 |
|
free(cur->name); |
|
1150 |
|
free(cur->cmd); |
|
1151 |
|
new = cur; |
|
1152 |
|
} |
|
1153 |
|
else |
|
1154 |
|
{ |
|
1155 |
|
if((new = insert_cmd(cur)) == NULL) |
|
1156 |
|
{ |
|
1157 |
|
return CMDS_ERR_NO_MEM; |
|
1158 |
|
} |
|
1159 |
|
} |
|
1160 |
|
|
|
1161 |
|
new->name = strdup(name); |
|
1162 |
|
new->descr = descr; |
|
1163 |
|
new->id = USER_CMD_ID; |
|
1164 |
|
new->type = USER_CMD; |
|
1165 |
|
new->passed = 0; |
|
1166 |
|
new->cmd = strdup(body); |
|
1167 |
|
new->min_args = inner->user_cmd_handler.min_args; |
|
1168 |
|
new->max_args = inner->user_cmd_handler.max_args; |
|
1169 |
|
new->deleted = 0; |
|
1170 |
|
init_command_flags(new, inner->user_cmd_handler.flags); |
|
1171 |
|
|
|
1172 |
|
++inner->custom_cmd_count; |
|
1173 |
|
return 0; |
|
1174 |
|
} |
|
1175 |
|
|
|
1176 |
|
/* Initializes flag fields of *cmd from set of HAS_* flags. */ |
|
1177 |
|
static void |
|
1178 |
|
init_command_flags(cmd_t *cmd, int flags) |
|
1179 |
|
{ |
|
1180 |
|
assert((flags & (HAS_RAW_ARGS | HAS_REGEXP_ARGS)) != |
|
1181 |
|
(HAS_RAW_ARGS | HAS_REGEXP_ARGS) && "Wrong flags combination."); |
|
1182 |
|
assert((flags & (HAS_RAW_ARGS | HAS_QUOTED_ARGS)) != |
|
1183 |
|
(HAS_RAW_ARGS | HAS_QUOTED_ARGS) && "Wrong flags combination."); |
|
1184 |
|
assert((flags & (HAS_QMARK_NO_ARGS | HAS_QMARK_WITH_ARGS)) != |
|
1185 |
|
(HAS_QMARK_NO_ARGS | HAS_QMARK_WITH_ARGS) && "Wrong flags combination."); |
|
1186 |
|
assert((flags & (HAS_MACROS_FOR_CMD | HAS_MACROS_FOR_SHELL)) != |
|
1187 |
|
(HAS_MACROS_FOR_CMD | HAS_MACROS_FOR_SHELL) && |
|
1188 |
|
"Wrong flags combination."); |
|
1189 |
|
|
|
1190 |
|
cmd->range = ((flags & HAS_RANGE) != 0); |
|
1191 |
|
cmd->cust_sep = ((flags & HAS_CUST_SEP) != 0); |
|
1192 |
|
cmd->emark = ((flags & HAS_EMARK) != 0); |
|
1193 |
|
cmd->envvars = ((flags & HAS_ENVVARS) != 0); |
|
1194 |
|
cmd->select = ((flags & HAS_SELECTION_SCOPE) != 0); |
|
1195 |
|
cmd->bg = ((flags & HAS_BG_FLAG) != 0); |
|
1196 |
|
cmd->regexp = ((flags & HAS_REGEXP_ARGS) != 0); |
|
1197 |
|
cmd->quote = ((flags & HAS_QUOTED_ARGS) != 0); |
|
1198 |
|
cmd->noescaping = ((flags & HAS_RAW_ARGS) != 0); |
|
1199 |
|
cmd->comment = ((flags & HAS_COMMENT) != 0); |
|
1200 |
|
cmd->qmark = ((flags & (HAS_QMARK_NO_ARGS | HAS_QMARK_WITH_ARGS)) != 0); |
|
1201 |
|
cmd->args_after_qmark = ((flags & HAS_QMARK_WITH_ARGS) != 0); |
|
1202 |
|
cmd->macros_for_cmd = ((flags & HAS_MACROS_FOR_CMD) != 0); |
|
1203 |
|
cmd->macros_for_shell = ((flags & HAS_MACROS_FOR_SHELL) != 0); |
|
1204 |
|
} |
|
1205 |
|
|
|
1206 |
|
static const char * |
|
1207 |
|
get_user_cmd_name(const char cmd[], char buf[], size_t buf_len) |
|
1208 |
|
{ |
|
1209 |
|
const char *t; |
|
1210 |
|
size_t len; |
|
1211 |
|
|
|
1212 |
|
t = vle_cmds_past_arg(cmd); |
|
1213 |
|
|
|
1214 |
|
len = MIN((size_t)(t - cmd), buf_len - 1); |
|
1215 |
|
strncpy(buf, cmd, len); |
|
1216 |
|
buf[len] = '\0'; |
|
1217 |
|
return t; |
|
1218 |
|
} |
|
1219 |
|
|
|
1220 |
|
/* Checks that the name is a valid name for a user-defined command. */ |
|
1221 |
|
static int |
|
1222 |
|
is_valid_udc_name(const char name[]) |
|
1223 |
|
{ |
|
1224 |
|
assert(name[0] != '\0' && "Command name can't be empty"); |
|
1225 |
|
|
|
1226 |
|
if(strcmp(name, "!") == 0) |
|
1227 |
|
return 0; |
|
1228 |
|
if(strcmp(name, "?") == 0) |
|
1229 |
|
return 0; |
|
1230 |
|
if(!isalpha(name[0])) |
|
1231 |
|
return 0; |
|
1232 |
|
if(strlen(name) >= MAX_CMD_NAME_LEN) |
|
1233 |
|
return 0; |
|
1234 |
|
|
|
1235 |
|
char cmd_name[MAX_CMD_NAME_LEN]; |
|
1236 |
|
char *p = cmd_name; |
|
1237 |
|
|
|
1238 |
|
cmd_t *cmd = inner->head.next; |
|
1239 |
|
|
|
1240 |
|
while(name[0] != '\0') |
|
1241 |
|
{ |
|
1242 |
|
*p++ = *name; |
|
1243 |
|
|
|
1244 |
|
if(!isalnum(name[0])) |
|
1245 |
|
{ |
|
1246 |
|
if(name[1] != '\0') |
|
1247 |
|
return 0; |
|
1248 |
|
else if(name[0] != '!' && name[0] != '?') |
|
1249 |
|
return 0; |
|
1250 |
|
} |
|
1251 |
|
else if(isdigit(name[1])) |
|
1252 |
|
{ |
|
1253 |
|
*p = '\0'; |
|
1254 |
|
cmd = find_cmd_advance(cmd, cmd_name); |
|
1255 |
|
|
|
1256 |
|
if(find_cmd_match(cmd, cmd_name)) |
|
1257 |
|
{ |
|
1258 |
|
if(cmd->type == BUILTIN_CMD || cmd->type == BUILTIN_ABBR) |
|
1259 |
|
{ |
|
1260 |
|
return 0; |
|
1261 |
|
} |
|
1262 |
|
|
|
1263 |
|
const int name_len = p - cmd_name; |
|
1264 |
|
if(cmd->name[name_len] == '\0' || isalpha(cmd->name[name_len])) |
|
1265 |
|
{ |
|
1266 |
|
return 0; |
|
1267 |
|
} |
|
1268 |
|
} |
|
1269 |
|
} |
|
1270 |
|
|
|
1271 |
|
++name; |
|
1272 |
|
} |
|
1273 |
|
|
|
1274 |
|
*p = '\0'; |
|
1275 |
|
|
|
1276 |
|
/* Builtins with custom separator have higher priority. Disallow registering |
|
1277 |
|
* user-defined commands which will never be called. */ |
|
1278 |
|
if(name[-1] == '!' || name[-1] == '?') |
|
1279 |
|
{ |
|
1280 |
|
cmd_name[strlen(cmd_name) - 1] = '\0'; |
|
1281 |
|
} |
|
1282 |
|
|
|
1283 |
|
cmd = find_cmd_advance(cmd, cmd_name); |
|
1284 |
|
if(find_cmd_match(cmd, cmd_name)) |
|
1285 |
|
{ |
|
1286 |
|
if(cmd->cust_sep && strcmp(cmd->name, cmd_name) == 0) |
|
1287 |
|
return 0; |
|
1288 |
|
if(isdigit(cmd->name[strlen(cmd_name)])) |
|
1289 |
|
return 0; |
|
1290 |
|
} |
|
1291 |
|
|
|
1292 |
|
return 1; |
|
1293 |
|
} |
|
1294 |
|
|
|
1295 |
|
/* Allocates new command node and inserts it after the specified one. Returns |
|
1296 |
|
* new uninitialized node or NULL on error. */ |
|
1297 |
|
static cmd_t * |
|
1298 |
|
insert_cmd(cmd_t *after) |
|
1299 |
|
{ |
|
1300 |
|
cmd_t *const new = malloc(sizeof(*new)); |
|
1301 |
|
if(new == NULL) |
|
1302 |
|
{ |
|
1303 |
|
return NULL; |
|
1304 |
|
} |
|
1305 |
|
|
|
1306 |
|
new->next = after->next; |
|
1307 |
|
after->next = new; |
|
1308 |
|
return new; |
|
1309 |
|
} |
|
1310 |
|
|
|
1311 |
|
/* Implements :command builtin command provided by this unit. */ |
|
1312 |
|
static int |
|
1313 |
|
delcommand_cmd(const cmd_info_t *cmd_info) |
|
1314 |
|
{ |
|
1315 |
|
return vle_cmds_del_user(cmd_info->argv[0]); |
|
1316 |
|
} |
|
1317 |
|
|
|
1318 |
|
int |
|
1319 |
|
vle_cmds_del_user(const char name[]) |
|
1320 |
|
{ |
|
1321 |
|
int cmp; |
|
1322 |
|
cmd_t *cur; |
|
1323 |
|
cmd_t *cmd; |
|
1324 |
|
|
|
1325 |
|
cmp = -1; |
|
1326 |
|
cur = &inner->head; |
|
1327 |
|
while(cur->next != NULL && (cmp = strcmp(cur->next->name, name)) < 0) |
|
1328 |
|
cur = cur->next; |
|
1329 |
|
|
|
1330 |
|
if(cur->next == NULL || cmp != 0) |
|
1331 |
|
return CMDS_ERR_NO_SUCH_UDF; |
|
1332 |
|
|
|
1333 |
|
cmd = cur->next; |
|
1334 |
|
cur->next = cmd->next; |
|
1335 |
|
free(cmd->name); |
|
1336 |
|
free(cmd->cmd); |
|
1337 |
|
free(cmd); |
|
1338 |
|
|
|
1339 |
|
inner->custom_cmd_count--; |
|
1340 |
|
return 0; |
|
1341 |
|
} |
|
1342 |
|
|
|
1343 |
|
char * |
|
1344 |
|
vle_cmds_last_arg(const char cmd[], int quotes, size_t *len) |
|
1345 |
|
{ |
|
1346 |
|
int argc; |
|
1347 |
|
char **argv; |
|
1348 |
|
int (*argvp)[2]; |
|
1349 |
|
int last_start = 0; |
|
1350 |
|
int last_end = 0; |
|
1351 |
|
|
|
1352 |
|
argv = dispatch_line(cmd, &argc, ' ', 0, quotes, 0, 0, NULL, &argvp); |
|
1353 |
|
|
|
1354 |
|
if(argc > 0) |
|
1355 |
|
{ |
|
1356 |
|
last_start = argvp[argc - 1][0]; |
|
1357 |
|
last_end = argvp[argc - 1][1]; |
|
1358 |
|
} |
|
1359 |
|
|
|
1360 |
|
*len = last_end - last_start; |
|
1361 |
|
free_string_array(argv, argc); |
|
1362 |
|
free(argvp); |
|
1363 |
|
return (char *)cmd + last_start; |
|
1364 |
|
} |
|
1365 |
|
|
|
1366 |
|
/* Splits argument string into array of strings. Non-zero noescaping means that |
|
1367 |
|
* unquoted arguments don't have escaping. Returns NULL if no arguments are |
|
1368 |
|
* found or an error occurred. Always sets *count (to negative value on |
|
1369 |
|
* unmatched quotes and to zero on all other errors). */ |
|
1370 |
|
TSTATIC char ** |
|
1371 |
|
dispatch_line(const char args[], int *count, char sep, int regexp, int quotes, |
|
1372 |
|
int noescaping, int comments, int *last_pos, int (**positions)[2]) |
|
1373 |
|
{ |
|
1374 |
|
char *cmdstr; |
|
1375 |
|
int len; |
|
1376 |
|
int i; |
|
1377 |
|
int st; |
|
1378 |
|
const char *args_beg; |
|
1379 |
|
char **params; |
|
1380 |
|
int (*argvp)[2] = NULL; |
|
1381 |
|
DA_INSTANCE(argvp); |
|
1382 |
|
|
|
1383 |
|
enum { BEGIN, NO_QUOTING, S_QUOTING, D_QUOTING, R_QUOTING, ARG, QARG } state; |
|
1384 |
|
|
|
1385 |
|
if(last_pos != NULL) |
|
1386 |
|
*last_pos = 0; |
|
1387 |
|
*positions = NULL; |
|
1388 |
|
|
|
1389 |
|
*count = 0; |
|
1390 |
|
params = NULL; |
|
1391 |
|
|
|
1392 |
|
args_beg = args; |
|
1393 |
|
if(sep == ' ') |
|
1394 |
|
{ |
|
1395 |
|
args = vle_cmds_at_arg(args); |
|
1396 |
|
} |
|
1397 |
|
cmdstr = strdup(args); |
|
1398 |
|
len = strlen(cmdstr); |
|
1399 |
|
for(i = 0, st = 0, state = BEGIN; i <= len; ++i) |
|
1400 |
|
{ |
|
1401 |
|
const int prev_state = state; |
|
1402 |
|
switch(state) |
|
1403 |
|
{ |
|
1404 |
|
case BEGIN: |
|
1405 |
|
if(sep == ' ' && cmdstr[i] == '\'' && quotes) |
|
1406 |
|
{ |
|
1407 |
|
st = i + 1; |
|
1408 |
|
state = S_QUOTING; |
|
1409 |
|
} |
|
1410 |
|
else if(cmdstr[i] == '"' && ((sep == ' ' && quotes) || |
|
1411 |
|
(comments && strchr(&cmdstr[i + 1], '"') == NULL))) |
|
1412 |
|
{ |
|
1413 |
|
st = i + 1; |
|
1414 |
|
state = D_QUOTING; |
|
1415 |
|
} |
|
1416 |
|
else if(sep == ' ' && cmdstr[i] == '/' && regexp) |
|
1417 |
|
{ |
|
1418 |
|
st = i + 1; |
|
1419 |
|
state = R_QUOTING; |
|
1420 |
|
} |
|
1421 |
|
else if(!is_separator(cmdstr[i], sep)) |
|
1422 |
|
{ |
|
1423 |
|
st = i; |
|
1424 |
|
state = NO_QUOTING; |
|
1425 |
|
|
|
1426 |
|
/* Omit escaped character from processign to account for possible case |
|
1427 |
|
* when it's a separator, which would lead to breaking argument into |
|
1428 |
|
* pieces. */ |
|
1429 |
|
if(!noescaping && cmdstr[i] == '\\' && cmdstr[i + 1] != '\0') |
|
1430 |
|
{ |
|
1431 |
|
++i; |
|
1432 |
|
} |
|
1433 |
|
} |
|
1434 |
|
else if(sep != ' ' && i > 0 && is_separator(cmdstr[i - 1], sep)) |
|
1435 |
|
{ |
|
1436 |
|
st = i--; |
|
1437 |
|
state = NO_QUOTING; |
|
1438 |
|
} |
|
1439 |
|
if(state != BEGIN && cmdstr[i] != '\0') |
|
1440 |
|
{ |
|
1441 |
|
if(DA_EXTEND(argvp) != NULL) |
|
1442 |
|
{ |
|
1443 |
|
DA_COMMIT(argvp); |
|
1444 |
|
argvp[DA_SIZE(argvp) - 1U][0] = i; |
|
1445 |
|
} |
|
1446 |
|
} |
|
1447 |
|
break; |
|
1448 |
|
case NO_QUOTING: |
|
1449 |
|
if(cmdstr[i] == '\0' || is_separator(cmdstr[i], sep)) |
|
1450 |
|
{ |
|
1451 |
|
state = ARG; |
|
1452 |
|
} |
|
1453 |
|
else if(!noescaping && cmdstr[i] == '\\' && cmdstr[i + 1] != '\0') |
|
1454 |
|
{ |
|
1455 |
|
++i; |
|
1456 |
|
} |
|
1457 |
|
break; |
|
1458 |
|
case S_QUOTING: |
|
1459 |
|
if(cmdstr[i] == '\'') |
|
1460 |
|
{ |
|
1461 |
|
if(cmdstr[i + 1] == '\'') |
|
1462 |
|
{ |
|
1463 |
|
++i; |
|
1464 |
|
} |
|
1465 |
|
else |
|
1466 |
|
{ |
|
1467 |
|
state = QARG; |
|
1468 |
|
} |
|
1469 |
|
} |
|
1470 |
|
break; |
|
1471 |
|
case D_QUOTING: |
|
1472 |
|
if(cmdstr[i] == '"') |
|
1473 |
|
{ |
|
1474 |
|
state = QARG; |
|
1475 |
|
} |
|
1476 |
|
else if(!noescaping && cmdstr[i] == '\\' && cmdstr[i + 1] != '\0') |
|
1477 |
|
{ |
|
1478 |
|
++i; |
|
1479 |
|
} |
|
1480 |
|
break; |
|
1481 |
|
case R_QUOTING: |
|
1482 |
|
if(cmdstr[i] == '/') |
|
1483 |
|
{ |
|
1484 |
|
state = QARG; |
|
1485 |
|
} |
|
1486 |
|
else if(!noescaping && cmdstr[i] == '\\' && cmdstr[i + 1] == '/') |
|
1487 |
|
{ |
|
1488 |
|
++i; |
|
1489 |
|
} |
|
1490 |
|
break; |
|
1491 |
|
|
|
1492 |
|
case ARG: |
|
1493 |
|
case QARG: |
|
1494 |
|
assert(0 && "Dispatch line state machine is broken"); |
|
1495 |
|
break; |
|
1496 |
|
} |
|
1497 |
|
if(state == ARG || state == QARG) |
|
1498 |
|
{ |
|
1499 |
|
/* Found another argument. */ |
|
1500 |
|
|
|
1501 |
|
const int end = (args - args_beg) + ((state == ARG) ? i : (i + 1)); |
|
1502 |
|
char *last_arg; |
|
1503 |
|
const char c = cmdstr[i]; |
|
1504 |
|
cmdstr[i] = '\0'; |
|
1505 |
|
|
|
1506 |
|
if(DA_SIZE(argvp) != 0U) |
|
1507 |
|
{ |
|
1508 |
|
argvp[DA_SIZE(argvp) - 1U][1] = end; |
|
1509 |
|
} |
|
1510 |
|
|
|
1511 |
|
*count = add_to_string_array(¶ms, *count, &cmdstr[st]); |
|
1512 |
|
if(*count == 0) |
|
1513 |
|
{ |
|
1514 |
|
break; |
|
1515 |
|
} |
|
1516 |
|
last_arg = params[*count - 1]; |
|
1517 |
|
|
|
1518 |
|
cmdstr[i] = c; |
|
1519 |
|
switch(prev_state) |
|
1520 |
|
{ |
|
1521 |
|
case NO_QUOTING: |
|
1522 |
|
if(!noescaping) |
|
1523 |
|
{ |
|
1524 |
|
unescape(last_arg, sep != ' '); |
|
1525 |
|
} |
|
1526 |
|
break; |
|
1527 |
|
case S_QUOTING: expand_squotes_escaping(last_arg); break; |
|
1528 |
|
case D_QUOTING: expand_dquotes_escaping(last_arg); break; |
|
1529 |
|
case R_QUOTING: unescape(last_arg, 1); break; |
|
1530 |
|
} |
|
1531 |
|
state = BEGIN; |
|
1532 |
|
} |
|
1533 |
|
} |
|
1534 |
|
|
|
1535 |
|
if(comments && state == D_QUOTING && strchr(&cmdstr[st], '"') == NULL) |
|
1536 |
|
{ |
|
1537 |
|
state = BEGIN; |
|
1538 |
|
--st; |
|
1539 |
|
if(DA_SIZE(argvp) != 0U) |
|
1540 |
|
{ |
|
1541 |
|
DA_REMOVE(argvp, &argvp[DA_SIZE(argvp) - 1U]); |
|
1542 |
|
} |
|
1543 |
|
} |
|
1544 |
|
|
|
1545 |
|
free(cmdstr); |
|
1546 |
|
|
|
1547 |
|
if(*count == 0 || (size_t)*count != DA_SIZE(argvp) || |
|
1548 |
|
(state != BEGIN && state != NO_QUOTING) || |
|
1549 |
|
put_into_string_array(¶ms, *count, NULL) != *count + 1) |
|
1550 |
|
{ |
|
1551 |
|
free(argvp); |
|
1552 |
|
free_string_array(params, *count); |
|
1553 |
|
*count = (state == S_QUOTING || state == D_QUOTING || state == R_QUOTING) |
|
1554 |
|
? -1 : 0; |
|
1555 |
|
return NULL; |
|
1556 |
|
} |
|
1557 |
|
|
|
1558 |
|
if(last_pos != NULL) |
|
1559 |
|
{ |
|
1560 |
|
*last_pos = (args - args_beg) + st; |
|
1561 |
|
} |
|
1562 |
|
|
|
1563 |
|
*positions = argvp; |
|
1564 |
|
return params; |
|
1565 |
|
} |
|
1566 |
|
|
|
1567 |
|
char ** |
|
1568 |
|
vle_cmds_list_udcs(void) |
|
1569 |
|
{ |
|
1570 |
|
char **p; |
|
1571 |
|
cmd_t *cur; |
|
1572 |
|
|
|
1573 |
|
char **const list = reallocarray(NULL, inner->custom_cmd_count*2 + 1, |
|
1574 |
|
sizeof(*list)); |
|
1575 |
|
if(list == NULL) |
|
1576 |
|
{ |
|
1577 |
|
return NULL; |
|
1578 |
|
} |
|
1579 |
|
|
|
1580 |
|
p = list; |
|
1581 |
|
|
|
1582 |
|
cur = inner->head.next; |
|
1583 |
|
while(cur != NULL) |
|
1584 |
|
{ |
|
1585 |
|
if(cur->type == USER_CMD) |
|
1586 |
|
{ |
|
1587 |
|
*p++ = strdup(cur->name); |
|
1588 |
|
*p++ = strdup(cur->descr == NULL ? cur->cmd : cur->descr); |
|
1589 |
|
} |
|
1590 |
|
else if(cur->type == FOREIGN_CMD) |
|
1591 |
|
{ |
|
1592 |
|
*p++ = strdup(cur->name); |
|
1593 |
|
*p++ = strdup(cur->descr); |
|
1594 |
|
} |
|
1595 |
|
cur = cur->next; |
|
1596 |
|
} |
|
1597 |
|
|
|
1598 |
|
*p = NULL; |
|
1599 |
|
|
|
1600 |
|
return list; |
|
1601 |
|
} |
|
1602 |
|
|
|
1603 |
|
char * |
|
1604 |
|
vle_cmds_print_udcs(const char beginning[]) |
|
1605 |
|
{ |
|
1606 |
|
size_t len; |
|
1607 |
|
cmd_t *cur; |
|
1608 |
|
char *content = NULL; |
|
1609 |
|
size_t content_len = 0; |
|
1610 |
|
|
|
1611 |
|
cur = inner->head.next; |
|
1612 |
|
len = strlen(beginning); |
|
1613 |
|
while(cur != NULL) |
|
1614 |
|
{ |
|
1615 |
|
void *ptr; |
|
1616 |
|
size_t new_size; |
|
1617 |
|
|
|
1618 |
|
if(strncmp(cur->name, beginning, len) != 0 || cur->type != USER_CMD) |
|
1619 |
|
{ |
|
1620 |
|
cur = cur->next; |
|
1621 |
|
continue; |
|
1622 |
|
} |
|
1623 |
|
|
|
1624 |
|
if(content == NULL) |
|
1625 |
|
{ |
|
1626 |
|
content = strdup("Command -- Action"); |
|
1627 |
|
content_len = strlen(content); |
|
1628 |
|
} |
|
1629 |
|
new_size = content_len + 1 + strlen(cur->name) + 10 + strlen(cur->cmd) + 1; |
|
1630 |
|
ptr = realloc(content, new_size); |
|
1631 |
|
if(ptr != NULL) |
|
1632 |
|
{ |
|
1633 |
|
content = ptr; |
|
1634 |
|
content_len += sprintf(content + content_len, "\n%-*s %s", 10, cur->name, |
|
1635 |
|
cur->cmd); |
|
1636 |
|
} |
|
1637 |
|
cur = cur->next; |
|
1638 |
|
} |
|
1639 |
|
|
|
1640 |
|
return content; |
|
1641 |
|
} |
|
1642 |
|
|
|
1643 |
|
char * |
|
1644 |
|
vle_cmds_past_arg(const char args[]) |
|
1645 |
|
{ |
|
1646 |
|
while(!is_separator(*args, ' ') && *args != '\0') |
|
1647 |
|
{ |
|
1648 |
|
++args; |
|
1649 |
|
} |
|
1650 |
|
return (char *)args; |
|
1651 |
|
} |
|
1652 |
|
|
|
1653 |
|
char * |
|
1654 |
|
vle_cmds_at_arg(const char args[]) |
|
1655 |
|
{ |
|
1656 |
|
while(is_separator(*args, ' ')) |
|
1657 |
|
{ |
|
1658 |
|
++args; |
|
1659 |
|
} |
|
1660 |
|
return (char *)args; |
|
1661 |
|
} |
|
1662 |
|
|
|
1663 |
|
char * |
|
1664 |
|
vle_cmds_next_arg(const char args[]) |
|
1665 |
|
{ |
|
1666 |
|
args = vle_cmds_past_arg(args); |
|
1667 |
|
return vle_cmds_at_arg(args); |
|
1668 |
|
} |
|
1669 |
|
|
|
1670 |
|
/* Checks whether character is command separator. Special case is space as a |
|
1671 |
|
* separator, which means that any reasonable whitespace can be used as |
|
1672 |
|
* separators (we don't won't to treat line feeds or similar characters as |
|
1673 |
|
* separators to allow their appearance as part of arguments). Returns non-zero |
|
1674 |
|
* if c is counted to be a separator, otherwise zero is returned. */ |
|
1675 |
|
static int |
|
1676 |
|
is_separator(char c, char sep) |
|
1677 |
|
{ |
|
1678 |
|
return (sep == ' ') ? (c == ' ' || c == '\t') : (c == sep); |
|
1679 |
|
} |
|
1680 |
|
|
|
1681 |
|
/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ |
|
1682 |
|
/* vim: set cinoptions+=t0 filetype=c : */ |
File engine/cmds.h added (mode: 100644) (index 0000000..3dc1142) |
|
1 |
|
/* vifm |
|
2 |
|
* Copyright (C) 2011 xaizek. |
|
3 |
|
* |
|
4 |
|
* This program is free software; you can redistribute it and/or modify |
|
5 |
|
* it under the terms of the GNU General Public License as published by |
|
6 |
|
* the Free Software Foundation; either version 2 of the License, or |
|
7 |
|
* (at your option) any later version. |
|
8 |
|
* |
|
9 |
|
* This program is distributed in the hope that it will be useful, |
|
10 |
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
|
* GNU General Public License for more details. |
|
13 |
|
* |
|
14 |
|
* You should have received a copy of the GNU General Public License |
|
15 |
|
* along with this program; if not, write to the Free Software |
|
16 |
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
|
17 |
|
*/ |
|
18 |
|
|
|
19 |
|
#ifndef VIFM__ENGINE__CMDS_H__ |
|
20 |
|
#define VIFM__ENGINE__CMDS_H__ |
|
21 |
|
|
|
22 |
|
#include <stddef.h> /* size_t */ |
|
23 |
|
|
|
24 |
|
#include "../utils/test_helpers.h" |
|
25 |
|
|
|
26 |
|
#ifdef __cplusplus |
|
27 |
|
extern "C" |
|
28 |
|
{ |
|
29 |
|
#endif |
|
30 |
|
|
|
31 |
|
/* Error codes returned by vle_cmds_run(). */ |
|
32 |
|
enum |
|
33 |
|
{ |
|
34 |
|
/* Error codes are negative integers. */ |
|
35 |
|
CMDS_ERR_LOOP = -128, /* Too deep recursion of commands. */ |
|
36 |
|
CMDS_ERR_NO_MEM, /* Not enough memory. */ |
|
37 |
|
CMDS_ERR_TOO_FEW_ARGS, /* Not enough arguments. */ |
|
38 |
|
CMDS_ERR_TRAILING_CHARS, /* Too many arguments. */ |
|
39 |
|
CMDS_ERR_INCORRECT_NAME, /* Bad name for a user-defined command. */ |
|
40 |
|
CMDS_ERR_NEED_BANG, /* Need enforcing to succeed. */ |
|
41 |
|
CMDS_ERR_NO_BUILTIN_REDEFINE, /* Can't shadow builtin command. */ |
|
42 |
|
CMDS_ERR_INVALID_CMD, /* Unknown command name. */ |
|
43 |
|
CMDS_ERR_NO_BANG_ALLOWED, /* The command doesn't support "!". */ |
|
44 |
|
CMDS_ERR_NO_RANGE_ALLOWED, /* The command doesn't take range. */ |
|
45 |
|
CMDS_ERR_NO_QMARK_ALLOWED, /* The command doesn't support "?". */ |
|
46 |
|
CMDS_ERR_INVALID_RANGE, /* Bad range. */ |
|
47 |
|
CMDS_ERR_NO_SUCH_UDF, /* Unknown name of a user-defined command. */ |
|
48 |
|
CMDS_ERR_UDF_IS_AMBIGUOUS, /* Calling user-defined command is ambiguous. */ |
|
49 |
|
CMDS_ERR_INVALID_ARG, /* Missing closing quote. */ |
|
50 |
|
CMDS_ERR_CUSTOM, /* Error defined by the client. */ |
|
51 |
|
}; |
|
52 |
|
|
|
53 |
|
/* Constants related to command ids. */ |
|
54 |
|
enum |
|
55 |
|
{ |
|
56 |
|
/* Builtin commands have negative ids. */ |
|
57 |
|
USER_CMD_ID = -256, /* <USERCMD> */ |
|
58 |
|
COMCLEAR_CMD_ID, /* :comc[lear] */ |
|
59 |
|
COMMAND_CMD_ID, /* :com[mand] */ |
|
60 |
|
DELCOMMAND_CMD_ID, /* :delc[ommand] */ |
|
61 |
|
|
|
62 |
|
/* Commands with ids in range [NO_COMPLETION_BOUNDARY; 0) are not |
|
63 |
|
* completed. */ |
|
64 |
|
NO_COMPLETION_BOUNDARY, |
|
65 |
|
}; |
|
66 |
|
|
|
67 |
|
/* Special values. */ |
|
68 |
|
enum |
|
69 |
|
{ |
|
70 |
|
/* Undefined maximum number of arguments for a command. */ |
|
71 |
|
NOT_DEF = -8192, |
|
72 |
|
}; |
|
73 |
|
|
|
74 |
|
/* Command type. */ |
|
75 |
|
typedef enum |
|
76 |
|
{ |
|
77 |
|
BUILTIN_ABBR, /* Abbreviated version of a builtin command (like `:com`). */ |
|
78 |
|
BUILTIN_CMD, /* Builtin command. */ |
|
79 |
|
FOREIGN_CMD, /* Externally registered builtin-like command. */ |
|
80 |
|
USER_CMD, /* User-defined command. */ |
|
81 |
|
} |
|
82 |
|
CMD_TYPE; |
|
83 |
|
|
|
84 |
|
/* Detailed information resulted from command parsing, which is passed to |
|
85 |
|
* command handler (cmd_handler). */ |
|
86 |
|
typedef struct cmd_info_t |
|
87 |
|
{ |
|
88 |
|
int begin, end; /* Parsed range of the command. They are either valid and |
|
89 |
|
define range with both boundaries included or equal to |
|
90 |
|
NOT_DEF if no range was given. */ |
|
91 |
|
int count; /* Parsed [count] of the command. */ |
|
92 |
|
int emark, qmark, bg; |
|
93 |
|
char sep; |
|
94 |
|
int usr1, usr2; |
|
95 |
|
|
|
96 |
|
char *raw_args; /* Arguments as they were passed in. */ |
|
97 |
|
char *args; /* Arguments after macro and envvar expansions. */ |
|
98 |
|
int argc; /* Number of arguments. */ |
|
99 |
|
char **argv; /* Values of arguments. */ |
|
100 |
|
int (*argvp)[2]; /* Start/end positions of arguments in args. */ |
|
101 |
|
|
|
102 |
|
/* For user defined commands. */ |
|
103 |
|
const char *user_cmd; /* Name of user defined command. */ |
|
104 |
|
const char *user_action; /* Body of user defined command. */ |
|
105 |
|
|
|
106 |
|
void *user_data; /* User data associated with the command or NULL. */ |
|
107 |
|
} |
|
108 |
|
cmd_info_t; |
|
109 |
|
|
|
110 |
|
/* Type of command handler. Shouldn't return negative numbers unless it's one |
|
111 |
|
* of CMDS_ERR_* constants. Either way the return value will be the return |
|
112 |
|
* value of vle_cmds_run() function. */ |
|
113 |
|
typedef int (*cmd_handler)(const cmd_info_t *cmd_info); |
|
114 |
|
|
|
115 |
|
/* Description of a command or an abbreviation. */ |
|
116 |
|
typedef struct cmd_t |
|
117 |
|
{ |
|
118 |
|
char *name; /* Name of the command. */ |
|
119 |
|
const char *descr; /* Brief description of the command. */ |
|
120 |
|
int id; /* Numeric identifier, positive, USER_CMD_ID or -1. */ |
|
121 |
|
CMD_TYPE type; /* Type of command described by this structure. */ |
|
122 |
|
int passed; /* Number of times this command was recursively called. */ |
|
123 |
|
|
|
124 |
|
void *user_data; /* User data associated with the command or NULL. */ |
|
125 |
|
cmd_handler handler; /* Handler for builtin commands. */ |
|
126 |
|
char *cmd; /* Command-line for user-defined commands. */ |
|
127 |
|
|
|
128 |
|
int min_args, max_args; /* Min and max number of arguments, can be NOT_DEF. */ |
|
129 |
|
|
|
130 |
|
unsigned int deleted : 1; /* Whether this command was deleted. */ |
|
131 |
|
unsigned int range : 1; /* Handles ranges. */ |
|
132 |
|
unsigned int cust_sep : 1; /* Custom separator of arguments. */ |
|
133 |
|
unsigned int emark : 1; /* Supports emark flag. */ |
|
134 |
|
unsigned int envvars : 1; /* Expand environment variables. */ |
|
135 |
|
unsigned int select : 1; /* Select files in a range. */ |
|
136 |
|
unsigned int bg : 1; /* Bg (can have " &" at the end). */ |
|
137 |
|
unsigned int noescaping : 1; /* Don't process \-escaping in unquoted |
|
138 |
|
args. */ |
|
139 |
|
unsigned int regexp : 1; /* Process /.../-arguments. */ |
|
140 |
|
unsigned int quote : 1; /* Process '- and "-quoted args. */ |
|
141 |
|
unsigned int comment : 1; /* Trailing comment is allowed. */ |
|
142 |
|
unsigned int qmark : 1; /* No args after qmark. */ |
|
143 |
|
unsigned int args_after_qmark : 1; /* Args after qmark are allowed. */ |
|
144 |
|
unsigned int macros_for_cmd : 1; /* Expand macros w/o special escaping. */ |
|
145 |
|
unsigned int macros_for_shell : 1; /* Expand macros with shell escaping. */ |
|
146 |
|
unsigned int : 0; /* Padding. */ |
|
147 |
|
|
|
148 |
|
struct cmd_t *next; /* Pointer to the next structure or NULL. */ |
|
149 |
|
} |
|
150 |
|
cmd_t; |
|
151 |
|
|
|
152 |
|
/* Possible flags for cmd_add_t::flags field. */ |
|
153 |
|
enum |
|
154 |
|
{ |
|
155 |
|
HAS_RANGE = 0x0001, /* Handles ranges. */ |
|
156 |
|
HAS_CUST_SEP = 0x0002, /* Custom separator of arguments. */ |
|
157 |
|
HAS_EMARK = 0x0004, /* Supports emark flag. */ |
|
158 |
|
HAS_ENVVARS = 0x0008, /* Expand environment variables. */ |
|
159 |
|
HAS_SELECTION_SCOPE = 0x0010, /* Select files in a range. */ |
|
160 |
|
HAS_BG_FLAG = 0x0020, /* Background (can have " &" at the end). */ |
|
161 |
|
HAS_COMMENT = 0x0040, /* Trailing comment is allowed. */ |
|
162 |
|
|
|
163 |
|
/* HAS_RAW_ARGS flag can't be combined with either of the other two, but those |
|
164 |
|
* two can be specified at the same time. */ |
|
165 |
|
HAS_RAW_ARGS = 0x0080, /* No special processing of arguments. */ |
|
166 |
|
HAS_REGEXP_ARGS = 0x0100, /* Process /.../-arguments. */ |
|
167 |
|
HAS_QUOTED_ARGS = 0x0200, /* Process '- and "-quoted args. */ |
|
168 |
|
|
|
169 |
|
/* Must be at most one of these. */ |
|
170 |
|
HAS_QMARK_NO_ARGS = 0x0400, /* No args after qmark. */ |
|
171 |
|
HAS_QMARK_WITH_ARGS = 0x0800, /* Args after qmark are allowed. */ |
|
172 |
|
|
|
173 |
|
/* Must be at most one of these. */ |
|
174 |
|
HAS_MACROS_FOR_CMD = 0x1000, /* Expand macros without special escaping. */ |
|
175 |
|
HAS_MACROS_FOR_SHELL = 0x2000, /* Expand macros with shell escaping. */ |
|
176 |
|
}; |
|
177 |
|
|
|
178 |
|
/* New commands specification for vle_cmds_add(). */ |
|
179 |
|
typedef struct |
|
180 |
|
{ |
|
181 |
|
const char *name; /* Full command name. */ |
|
182 |
|
const char *abbr; /* Command prefix (can be NULL). */ |
|
183 |
|
const char *descr; /* Brief description (stored as a pointer). */ |
|
184 |
|
void *user_data; /* User data for the handler or NULL. */ |
|
185 |
|
int id; /* Command id. Doesn't need to be unique. Negative |
|
186 |
|
value means absence of arg completion. Use, for |
|
187 |
|
example, -1 for all commands without completion. */ |
|
188 |
|
cmd_handler handler; /* Function invoked to run the command. */ |
|
189 |
|
int min_args, max_args; /* Minimum and maximum bounds on number of args. */ |
|
190 |
|
int flags; /* Set of HAS_* flags. */ |
|
191 |
|
} |
|
192 |
|
cmd_add_t; |
|
193 |
|
|
|
194 |
|
/* Configuration structure that's passed in from the outside. */ |
|
195 |
|
typedef struct |
|
196 |
|
{ |
|
197 |
|
void *inner; /* Should be NULL on first call of vle_cmds_init(). This is a |
|
198 |
|
pointer to internal data. */ |
|
199 |
|
|
|
200 |
|
int begin; /* The lowest valid number of the range. */ |
|
201 |
|
int current; /* Current position between [begin; end]. */ |
|
202 |
|
int end; /* The highest valid number of the range. */ |
|
203 |
|
|
|
204 |
|
/* Whole line completion function. arg is a user supplied value, which is |
|
205 |
|
* passed through. The functions should return completion offset. */ |
|
206 |
|
int (*complete_line)(const char cmd_line[], void *arg); |
|
207 |
|
|
|
208 |
|
/* Argument completion function. arg is a user supplied value, which is |
|
209 |
|
* passed through. The functions must not modify any strings passed to it. |
|
210 |
|
* The functions should return completion offset. */ |
|
211 |
|
int (*complete_args)(int id, const cmd_info_t *cmd_info, int arg_pos, |
|
212 |
|
void *arg); |
|
213 |
|
|
|
214 |
|
/* Asks user whether bounds of an inverted range should be swapped. Should |
|
215 |
|
* return non-zero if so and zero otherwise. */ |
|
216 |
|
int (*swap_range)(void); |
|
217 |
|
/* Resolves name of the mark to a position. Should return corresponding |
|
218 |
|
* position or value < 0 on error. */ |
|
219 |
|
int (*resolve_mark)(char mark); |
|
220 |
|
/* Expands macros in the passed string. Should return newly allocated |
|
221 |
|
* memory. */ |
|
222 |
|
char * (*expand_macros)(const char str[], int for_shell, int *usr1, |
|
223 |
|
int *usr2); |
|
224 |
|
/* Expands environment variables in the passed string. Should return newly |
|
225 |
|
* allocated memory. */ |
|
226 |
|
char * (*expand_envvars)(const char str[]); |
|
227 |
|
/* Called after successful processing of a command. */ |
|
228 |
|
void (*post)(int id); |
|
229 |
|
/* Called for commands with HAS_SELECTION_SCOPE flag. */ |
|
230 |
|
void (*select_range)(int id, const cmd_info_t *cmd_info); |
|
231 |
|
/* Called to determine whether a command at the front should be skipped for |
|
232 |
|
* the purposes of completion. Should return < 0 to do nothing, x to skip |
|
233 |
|
* command name and x chars. */ |
|
234 |
|
int (*skip_at_beginning)(int id, const char args[]); |
|
235 |
|
} |
|
236 |
|
cmds_conf_t; |
|
237 |
|
|
|
238 |
|
/* Initializes previously uninitialized instance of the unit and sets it as the |
|
239 |
|
* current one. The udf argument specifies whether user-defined commands are |
|
240 |
|
* allowed. The structure pointed to by cmds_conf_t should be filled before |
|
241 |
|
* calling this function. */ |
|
242 |
|
void vle_cmds_init(int udf, cmds_conf_t *cmds_conf); |
|
243 |
|
|
|
244 |
|
/* Resets state of the unit. */ |
|
245 |
|
void vle_cmds_reset(void); |
|
246 |
|
|
|
247 |
|
/* Clears (partially resets) state of the unit. */ |
|
248 |
|
void vle_cmds_clear(void); |
|
249 |
|
|
|
250 |
|
/* Executes a command. Returns one of CMDS_ERR_* codes or code returned by the |
|
251 |
|
* command handler. */ |
|
252 |
|
int vle_cmds_run(const char cmd[]); |
|
253 |
|
|
|
254 |
|
/* Parses command to fetch command and retrieve id associated with it. Returns |
|
255 |
|
* the id, -1 on error and USER_CMD_ID for all user defined commands. */ |
|
256 |
|
int vle_cmds_identify(const char cmd[]); |
|
257 |
|
|
|
258 |
|
/* Parses cmd to find beginning of arguments. Returns pointer within the cmd or |
|
259 |
|
* NULL if command is unknown or command-line is invalid. */ |
|
260 |
|
const char * vle_cmds_args(const char cmd[]); |
|
261 |
|
|
|
262 |
|
/* Breaks down passed command into its constituent parts. Returns pointer to |
|
263 |
|
* command's description or NULL on error. */ |
|
264 |
|
const cmd_t * vle_cmds_parse(const char cmd[], cmd_info_t *info); |
|
265 |
|
|
|
266 |
|
/* Performs completion of the command, either just command name or arguments of |
|
267 |
|
* some command. Returns offset in cmd, where completion elements should be |
|
268 |
|
* pasted. */ |
|
269 |
|
int vle_cmds_complete(const char cmd[], void *arg); |
|
270 |
|
|
|
271 |
|
/* Registers all commands in the array pointed to by cmds of length at least |
|
272 |
|
* count. */ |
|
273 |
|
void vle_cmds_add(const cmd_add_t cmds[], int count); |
|
274 |
|
|
|
275 |
|
/* Registers a foreign builtin-like command. Returns non-zero on error, |
|
276 |
|
* otherwise zero is returned. */ |
|
277 |
|
int vle_cmds_add_foreign(const cmd_add_t *cmd); |
|
278 |
|
|
|
279 |
|
/* Adds a new or updates an existing user command. Non-zero overwrite parameter |
|
280 |
|
* enables updating of existing command. Returns error code or zero on |
|
281 |
|
* success. */ |
|
282 |
|
int vle_cmds_add_user(const char name[], const char body[], const char descr[], |
|
283 |
|
int overwrite); |
|
284 |
|
|
|
285 |
|
/* Removes a user command if one exists. Returns error code or zero on |
|
286 |
|
* success. */ |
|
287 |
|
int vle_cmds_del_user(const char name[]); |
|
288 |
|
|
|
289 |
|
/* Finds the first character of the last argument in cmd. Returns pointer to |
|
290 |
|
* it. */ |
|
291 |
|
char * vle_cmds_last_arg(const char cmd[], int quotes, size_t *len); |
|
292 |
|
|
|
293 |
|
/* Lists user-defined commands as name-value pairs each in separate item of the |
|
294 |
|
* array. Last pair element is followed by a NULL. */ |
|
295 |
|
char ** vle_cmds_list_udcs(void); |
|
296 |
|
|
|
297 |
|
/* Prints a table that includes commands that start with the given prefix into |
|
298 |
|
* a multiline string (all lines except for the last one has new line |
|
299 |
|
* character). Returns the string or NULL if there are no command with that |
|
300 |
|
* prefix. */ |
|
301 |
|
char * vle_cmds_print_udcs(const char beginning[]); |
|
302 |
|
|
|
303 |
|
/* Skips at most one argument of the string. Returns pointer to the next |
|
304 |
|
* character after that argument, if any, otherwise pointer to |
|
305 |
|
* null-character. */ |
|
306 |
|
char * vle_cmds_past_arg(const char args[]); |
|
307 |
|
|
|
308 |
|
/* Skips argument separators in the string. Returns pointer to the first |
|
309 |
|
* character of the first argument, if any, otherwise pointer to |
|
310 |
|
* null-character. */ |
|
311 |
|
char * vle_cmds_at_arg(const char args[]); |
|
312 |
|
|
|
313 |
|
/* Advances to the next argument in the string. Returns pointer to the next |
|
314 |
|
* argument, if any, otherwise pointer to null-character. */ |
|
315 |
|
char * vle_cmds_next_arg(const char args[]); |
|
316 |
|
|
|
317 |
|
TSTATIC_DEFS( |
|
318 |
|
int add_builtin_cmd(const char name[], CMD_TYPE type, const cmd_add_t *conf); |
|
319 |
|
char ** dispatch_line(const char args[], int *count, char sep, int regexp, |
|
320 |
|
int quotes, int noescaping, int comments, int *last_arg, |
|
321 |
|
int (**positions)[2]); |
|
322 |
|
) |
|
323 |
|
|
|
324 |
|
#ifdef __cplusplus |
|
325 |
|
} |
|
326 |
|
#endif |
|
327 |
|
|
|
328 |
|
#endif /* VIFM__ENGINE__CMDS_H__ */ |
|
329 |
|
|
|
330 |
|
/* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ |
|
331 |
|
/* vim: set cinoptions+=t0 filetype=c : */ |