7b4ac7e1 |
1 | /* Library for reading command lines and decoding commands. |
4187119d |
2 | Copyright (C) 1986, 1989 Free Software Foundation, Inc. |
7b4ac7e1 |
3 | |
4187119d |
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 1, or (at your option) |
7 | 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ |
7b4ac7e1 |
17 | |
18 | #include "command.h" |
e91b87a3 |
19 | #include "defs.h" |
7b4ac7e1 |
20 | #include <stdio.h> |
4187119d |
21 | #include <ctype.h> |
bb7592f0 |
22 | |
7b4ac7e1 |
23 | extern char *xmalloc (); |
24 | |
7b4ac7e1 |
25 | /* Add element named NAME to command list *LIST. |
26 | FUN should be the function to execute the command; |
27 | it will get a character string as argument, with leading |
28 | and trailing blanks already eliminated. |
29 | |
30 | DOC is a documentation string for the command. |
31 | Its first line should be a complete sentence. |
32 | It should start with ? for a command that is an abbreviation |
33 | or with * for a command that most users don't need to know about. */ |
34 | |
35 | struct cmd_list_element * |
36 | add_cmd (name, class, fun, doc, list) |
37 | char *name; |
38 | int class; |
39 | void (*fun) (); |
40 | char *doc; |
41 | struct cmd_list_element **list; |
42 | { |
43 | register struct cmd_list_element *c |
44 | = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element)); |
45 | |
46 | delete_cmd (name, list); |
47 | c->next = *list; |
48 | c->name = savestring (name, strlen (name)); |
49 | c->class = class; |
50 | c->function = fun; |
51 | c->doc = doc; |
52 | c->prefixlist = 0; |
53 | c->allow_unknown = 0; |
54 | c->abbrev_flag = 0; |
55 | c->aux = 0; |
56 | *list = c; |
57 | return c; |
58 | } |
59 | |
e91b87a3 |
60 | /* Same as above, except that the abbrev_flag is set. */ |
61 | |
62 | struct cmd_list_element * |
63 | add_abbrev_cmd (name, class, fun, doc, list) |
64 | char *name; |
65 | int class; |
66 | void (*fun) (); |
67 | char *doc; |
68 | struct cmd_list_element **list; |
69 | { |
70 | register struct cmd_list_element *c |
71 | = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element)); |
72 | |
73 | delete_cmd (name, list); |
74 | c->next = *list; |
75 | c->name = savestring (name, strlen (name)); |
76 | c->class = class; |
77 | c->function = fun; |
78 | c->doc = doc; |
79 | c->prefixlist = 0; |
80 | c->allow_unknown = 0; |
81 | c->abbrev_flag = 1; |
82 | c->aux = 0; |
83 | *list = c; |
84 | return c; |
85 | } |
86 | |
7b4ac7e1 |
87 | struct cmd_list_element * |
88 | add_alias_cmd (name, oldname, class, abbrev_flag, list) |
89 | char *name; |
90 | char *oldname; |
91 | int class; |
92 | int abbrev_flag; |
93 | struct cmd_list_element **list; |
94 | { |
95 | /* Must do this since lookup_cmd tries to side-effect its first arg */ |
96 | char *copied_name; |
97 | register struct cmd_list_element *old; |
98 | register struct cmd_list_element *c; |
99 | copied_name = (char *) alloca (strlen (oldname) + 1); |
100 | strcpy (copied_name, oldname); |
4187119d |
101 | old = lookup_cmd (&copied_name, *list, 0, 1, 1); |
7b4ac7e1 |
102 | |
103 | if (old == 0) |
104 | { |
105 | delete_cmd (name, list); |
106 | return 0; |
107 | } |
108 | |
109 | c = add_cmd (name, class, old->function, old->doc, list); |
110 | c->prefixlist = old->prefixlist; |
111 | c->prefixname = old->prefixname; |
112 | c->allow_unknown = old->allow_unknown; |
113 | c->abbrev_flag = abbrev_flag; |
114 | c->aux = old->aux; |
115 | return c; |
116 | } |
117 | |
e91b87a3 |
118 | /* Like add_cmd but adds an element for a command prefix: |
7b4ac7e1 |
119 | a name that should be followed by a subcommand to be looked up |
120 | in another command list. PREFIXLIST should be the address |
121 | of the variable containing that list. */ |
122 | |
123 | struct cmd_list_element * |
124 | add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname, |
125 | allow_unknown, list) |
126 | char *name; |
127 | int class; |
128 | void (*fun) (); |
129 | char *doc; |
130 | struct cmd_list_element **prefixlist; |
131 | char *prefixname; |
132 | int allow_unknown; |
133 | struct cmd_list_element **list; |
134 | { |
135 | register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list); |
136 | c->prefixlist = prefixlist; |
137 | c->prefixname = prefixname; |
138 | c->allow_unknown = allow_unknown; |
139 | return c; |
140 | } |
141 | |
e91b87a3 |
142 | /* Like add_prefix_cmd butsets the abbrev_flag on the new command. */ |
143 | |
144 | struct cmd_list_element * |
145 | add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname, |
146 | allow_unknown, list) |
147 | char *name; |
148 | int class; |
149 | void (*fun) (); |
150 | char *doc; |
151 | struct cmd_list_element **prefixlist; |
152 | char *prefixname; |
153 | int allow_unknown; |
154 | struct cmd_list_element **list; |
155 | { |
156 | register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list); |
157 | c->prefixlist = prefixlist; |
158 | c->prefixname = prefixname; |
159 | c->allow_unknown = allow_unknown; |
160 | c->abbrev_flag = 1; |
161 | return c; |
162 | } |
163 | |
7b4ac7e1 |
164 | /* Remove the command named NAME from the command list. */ |
165 | |
166 | void |
167 | delete_cmd (name, list) |
168 | char *name; |
169 | struct cmd_list_element **list; |
170 | { |
171 | register struct cmd_list_element *c; |
172 | |
173 | while (*list && !strcmp ((*list)->name, name)) |
174 | { |
175 | *list = (*list)->next; |
176 | } |
177 | |
178 | if (*list) |
179 | for (c = *list; c->next;) |
180 | { |
181 | if (!strcmp (c->next->name, name)) |
182 | c->next = c->next->next; |
183 | else |
184 | c = c->next; |
185 | } |
186 | } |
187 | |
e91b87a3 |
188 | void help_cmd (), help_list (), help_cmd_list (); |
7b4ac7e1 |
189 | |
e91b87a3 |
190 | /* This command really has to deal with two things: |
191 | * 1) I want documentation on *this string* (usually called by |
192 | * "help commandname"). |
193 | * 2) I want documentation on *this list* (usually called by |
194 | * giving a command that requires subcommands. Also called by saying |
195 | * just "help".) |
196 | * |
197 | * I am going to split this into two seperate comamnds, help_cmd and |
198 | * help_list. |
199 | */ |
7b4ac7e1 |
200 | |
201 | void |
e91b87a3 |
202 | help_cmd (command, stream) |
7b4ac7e1 |
203 | char *command; |
7b4ac7e1 |
204 | FILE *stream; |
205 | { |
e91b87a3 |
206 | struct cmd_list_element *c; |
207 | extern struct cmd_list_element *cmdlist; |
7b4ac7e1 |
208 | |
e91b87a3 |
209 | if (!command) |
7b4ac7e1 |
210 | { |
e91b87a3 |
211 | help_list (cmdlist, "", -2, stream); |
212 | return; |
7b4ac7e1 |
213 | } |
214 | |
4187119d |
215 | c = lookup_cmd (&command, cmdlist, "", 0, 0); |
e91b87a3 |
216 | |
217 | if (c == 0) |
218 | return; |
219 | |
220 | /* There are three cases here. |
221 | If c->prefixlist is nonzer, we have a prefix command. |
222 | Print its documentation, then list its subcommands. |
223 | |
224 | If c->function is nonzero, we really have a command. |
225 | Print its documentation and return. |
226 | |
227 | If c->function is zero, we have a class name. |
228 | Print its documentation (as if it were a command) |
229 | and then set class to he number of this class |
230 | so that the commands in the class will be listed. */ |
231 | |
4187119d |
232 | fputs_filtered (c->doc, stream); |
233 | fputs_filtered ("\n", stream); |
234 | |
e91b87a3 |
235 | if (c->prefixlist == 0 && c->function != 0) |
236 | return; |
4187119d |
237 | fprintf_filtered (stream, "\n"); |
e91b87a3 |
238 | |
239 | /* If this is a prefix command, print it's subcommands */ |
240 | if (c->prefixlist) |
241 | help_list (*c->prefixlist, c->prefixname, -1, stream); |
242 | |
243 | /* If this is a class name, print all of the commands in the class */ |
244 | if (c->function == 0) |
245 | help_list (cmdlist, "", c->class, stream); |
246 | } |
247 | |
248 | /* |
249 | * Get a specific kind of help on a command list. |
250 | * |
251 | * LIST is the list. |
252 | * CMDTYPE is the prefix to use in the title string. |
253 | * CLASS is the class with which to list the nodes of this list (see |
254 | * documentation for help_cmd_list below), As usual, -1 for |
255 | * everything, -2 for just classes, and non-negative for only things |
256 | * in a specific class. |
257 | * and STREAM is the output stream on which to print things. |
258 | * If you call this routine with a class >= 0, it recurses. |
259 | */ |
260 | void |
261 | help_list (list, cmdtype, class, stream) |
262 | struct cmd_list_element *list; |
263 | char *cmdtype; |
264 | int class; |
265 | FILE *stream; |
266 | { |
267 | int len; |
268 | char *cmdtype1, *cmdtype2; |
269 | |
7b4ac7e1 |
270 | /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */ |
271 | len = strlen (cmdtype); |
272 | cmdtype1 = (char *) alloca (len + 1); |
273 | cmdtype1[0] = 0; |
274 | cmdtype2 = (char *) alloca (len + 4); |
275 | cmdtype2[0] = 0; |
276 | if (len) |
277 | { |
278 | cmdtype1[0] = ' '; |
279 | strncpy (cmdtype1 + 1, cmdtype, len - 1); |
280 | cmdtype1[len] = 0; |
281 | strncpy (cmdtype2, cmdtype, len - 1); |
282 | strcpy (cmdtype2 + len - 1, " sub"); |
283 | } |
284 | |
285 | if (class == -2) |
4187119d |
286 | fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2); |
7b4ac7e1 |
287 | else |
4187119d |
288 | fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2); |
7b4ac7e1 |
289 | |
e91b87a3 |
290 | help_cmd_list (list, class, cmdtype, (class >= 0), stream); |
7b4ac7e1 |
291 | |
292 | if (class == -2) |
4187119d |
293 | fprintf_filtered (stream, "\n\ |
7b4ac7e1 |
294 | Type \"help%s\" followed by a class name for a list of commands in that class.", |
295 | cmdtype1); |
296 | |
4187119d |
297 | fprintf_filtered (stream, "\n\ |
7b4ac7e1 |
298 | Type \"help%s\" followed by %scommand name for full documentation.\n\ |
299 | Command name abbreviations are allowed if unambiguous.\n", |
300 | cmdtype1, cmdtype2); |
301 | } |
e91b87a3 |
302 | |
303 | |
304 | /* |
305 | * Implement a help command on command list LIST. |
306 | * RECURSE should be non-zero if this should be done recursively on |
307 | * all sublists of LIST. |
308 | * PREFIX is the prefix to print before each command name. |
309 | * STREAM is the stream upon which the output should be written. |
310 | * CLASS should be: |
311 | * A non-negative class number to list only commands in that |
312 | * class. |
313 | * -1 to list all commands in list. |
314 | * -2 to list all classes in list. |
315 | * |
316 | * Note that RECURSE will be active on *all* sublists, not just the |
317 | * ones seclected by the criteria above (ie. the selection mechanism |
318 | * is at the low level, not the high-level). |
319 | */ |
320 | void |
321 | help_cmd_list (list, class, prefix, recurse, stream) |
322 | struct cmd_list_element *list; |
323 | int class; |
324 | char *prefix; |
325 | int recurse; |
326 | FILE *stream; |
327 | { |
328 | register struct cmd_list_element *c; |
329 | register char *p; |
4187119d |
330 | static char *line_buffer = 0; |
331 | static int line_size; |
332 | |
333 | if (!line_buffer) |
334 | { |
335 | line_size = 80; |
336 | line_buffer = (char *) xmalloc (line_size); |
337 | } |
e91b87a3 |
338 | |
339 | for (c = list; c; c = c->next) |
340 | { |
341 | if (c->abbrev_flag == 0 && |
342 | (class == -1 |
343 | || (class == -2 && c->function == 0) |
344 | || (class == c->class && c->function != 0))) |
345 | { |
4187119d |
346 | fprintf_filtered (stream, "%s%s -- ", prefix, c->name); |
e91b87a3 |
347 | /* Print just the first line */ |
348 | p = c->doc; |
349 | while (*p && *p != '\n') p++; |
4187119d |
350 | if (p - c->doc > line_size - 1) |
351 | { |
352 | line_size = p - c->doc + 1; |
353 | free (line_buffer); |
354 | line_buffer = (char *) xmalloc (line_size); |
355 | } |
356 | strncpy (line_buffer, c->doc, p - c->doc); |
357 | line_buffer[p - c->doc] = '\0'; |
358 | fputs_filtered (line_buffer, stream); |
359 | fputs_filtered ("\n", stream); |
e91b87a3 |
360 | } |
361 | if (recurse |
362 | && c->prefixlist != 0 |
363 | && c->abbrev_flag == 0) |
364 | help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream); |
365 | } |
366 | } |
7b4ac7e1 |
367 | \f |
4187119d |
368 | /* This routine takes a line of TEXT and a CLIST in which to |
369 | start the lookup. When it returns it will have incremented the text |
370 | pointer past the section of text it matched, set *RESULT_LIST to |
371 | the list in which the last word was matched, and will return the |
372 | cmd list element which the text matches. It will return 0 if no |
373 | match at all was possible. It will return -1 if ambigous matches are |
374 | possible; in this case *RESULT_LIST will be set to the list in which |
375 | there are ambiguous choices (and text will be set to the ambiguous |
376 | text string). |
377 | |
378 | It does no error reporting whatsoever; control will always return |
379 | to the superior routine. |
380 | |
381 | In the case of an ambiguous return (-1), *RESULT_LIST will be set to |
382 | point at the prefix_command (ie. the best match) *or* (special |
383 | case) will be 0 if no prefix command was ever found. For example, |
384 | in the case of "info a", "info" matches without ambiguity, but "a" |
385 | could be "args" or "address", so *RESULT_LIST is set to |
386 | the cmd_list_element for "info". So in this case |
387 | result list should not be interpeted as a pointer to the beginning |
388 | of a list; it simply points to a specific command. |
389 | |
390 | This routine does *not* modify the text pointed to by TEXT. |
391 | |
392 | If INGNORE_HELP_CLASSES is nonzero, ignore any command list |
393 | elements which are actually help classes rather than commands (i.e. |
394 | the function field of the struct cmd_list_element is 0). */ |
395 | |
396 | struct cmd_list_element * |
397 | lookup_cmd_1 (text, clist, result_list, ignore_help_classes) |
398 | char **text; |
399 | struct cmd_list_element *clist, **result_list; |
400 | int ignore_help_classes; |
401 | { |
402 | char *p, *command; |
403 | int len, tmp, nfound; |
404 | struct cmd_list_element *found, *c; |
405 | |
406 | while (**text == ' ' || **text == '\t') |
407 | (*text)++; |
408 | |
409 | /* Treating underscores as part of command words is important |
410 | so that "set args_foo()" doesn't get interpreted as |
411 | "set args _foo()". */ |
412 | for (p = *text; |
413 | *p && (isalnum(*p) || *p == '-' || *p == '_'); |
414 | p++) |
415 | ; |
416 | |
417 | /* If nothing but whitespace, return 0. */ |
418 | if (p == *text) |
419 | return 0; |
420 | |
421 | len = p - *text; |
422 | |
423 | /* *text and p now bracket the first command word to lookup (and |
424 | it's length is len). We copy this into a local temporary, |
425 | converting to lower case as we go. */ |
426 | |
427 | command = (char *) alloca (len + 1); |
428 | for (tmp = 0; tmp < len; tmp++) |
429 | { |
430 | char x = (*text)[tmp]; |
431 | command[tmp] = (x >= 'A' && x <= 'Z') ? x - 'A' + 'a' : x; |
432 | } |
433 | command[len] = '\0'; |
434 | |
435 | /* Look it up. */ |
436 | found = 0; |
437 | nfound = 0; |
438 | for (c = clist; c; c = c->next) |
439 | if (!strncmp (command, c->name, len) |
440 | && (!ignore_help_classes || c->function)) |
441 | { |
442 | found = c; |
443 | nfound++; |
444 | if (c->name[len] == '\0') |
445 | { |
446 | nfound = 1; |
447 | break; |
448 | } |
449 | } |
450 | |
451 | /* If nothing matches, we have a simple failure. */ |
452 | if (nfound == 0) |
453 | return 0; |
454 | |
455 | if (nfound > 1) |
456 | { |
457 | *result_list = 0; /* Will be modified in calling routine |
458 | if we know what the prefix command is. |
459 | */ |
460 | return (struct cmd_list_element *) -1; /* Ambiguous. */ |
461 | } |
462 | |
463 | /* We've matched something on this list. Move text pointer forward. */ |
464 | |
465 | *text = p; |
466 | if (found->prefixlist) |
467 | { |
468 | c = lookup_cmd_1 (text, *found->prefixlist, result_list, |
469 | ignore_help_classes); |
470 | if (!c) |
471 | { |
472 | /* Didn't find anything; this is as far as we got. */ |
473 | *result_list = clist; |
474 | return found; |
475 | } |
476 | else if (c == (struct cmd_list_element *) -1) |
477 | { |
478 | /* We've gotten this far properley, but the next step |
479 | is ambiguous. We need to set the result list to the best |
480 | we've found (if an inferior hasn't already set it). */ |
481 | if (!*result_list) |
482 | /* This used to say *result_list = *found->prefixlist |
483 | If that was correct, need to modify the documentation |
484 | at the top of this function to clarify what is supposed |
485 | to be going on. */ |
486 | *result_list = found; |
487 | return c; |
488 | } |
489 | else |
490 | { |
491 | /* We matched! */ |
492 | return c; |
493 | } |
494 | } |
495 | else |
496 | { |
497 | *result_list = clist; |
498 | return found; |
499 | } |
500 | } |
501 | |
502 | /* Look up the contents of *LINE as a command in the command list LIST. |
503 | LIST is a chain of struct cmd_list_element's. |
504 | If it is found, return the struct cmd_list_element for that command |
505 | and update *LINE to point after the command name, at the first argument. |
506 | If not found, call error if ALLOW_UNKNOWN is zero |
507 | otherwise (or if error returns) return zero. |
508 | Call error if specified command is ambiguous, |
509 | unless ALLOW_UNKNOWN is negative. |
510 | CMDTYPE precedes the word "command" in the error message. |
511 | |
512 | If INGNORE_HELP_CLASSES is nonzero, ignore any command list |
513 | elements which are actually help classes rather than commands (i.e. |
514 | the function field of the struct cmd_list_element is 0). */ |
515 | |
516 | struct cmd_list_element * |
517 | lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes) |
518 | char **line; |
519 | struct cmd_list_element *list; |
520 | char *cmdtype; |
521 | int allow_unknown; |
522 | int ignore_help_classes; |
523 | { |
524 | struct cmd_list_element *last_list = 0; |
525 | struct cmd_list_element *c = |
526 | lookup_cmd_1 (line, list, &last_list, ignore_help_classes); |
527 | char *ptr = (*line) + strlen (*line) - 1; |
528 | |
529 | /* Clear off trailing whitespace. */ |
530 | while (ptr >= *line && (*ptr == ' ' || *ptr == '\t')) |
531 | ptr--; |
532 | *(ptr + 1) = '\0'; |
533 | |
534 | if (!c) |
535 | { |
536 | if (!allow_unknown) |
537 | { |
538 | if (!*line) |
539 | error ("Lack of needed %scommand", cmdtype); |
540 | else |
541 | { |
542 | char *p = *line, *q; |
543 | |
544 | while (isalnum(*p) || *p == '-') |
545 | p++; |
546 | |
547 | q = (char *) alloca (p - *line + 1); |
548 | strncpy (q, *line, p - *line); |
549 | q[p-*line] = '\0'; |
550 | |
551 | error ("Undefined %scommand: \"%s\".", cmdtype, q); |
552 | } |
553 | } |
554 | else |
555 | return 0; |
556 | } |
557 | else if (c == (struct cmd_list_element *) -1) |
558 | { |
559 | /* Ambigous. Local values should be off prefixlist or called |
560 | values. */ |
561 | int local_allow_unknown = (last_list ? last_list->allow_unknown : |
562 | allow_unknown); |
563 | char *local_cmdtype = last_list ? last_list->prefixname : cmdtype; |
564 | struct cmd_list_element *local_list = |
565 | (last_list ? *(last_list->prefixlist) : list); |
566 | |
567 | if (local_allow_unknown < 0) |
568 | { |
569 | if (last_list) |
570 | return last_list; /* Found something. */ |
571 | else |
572 | return 0; /* Found nothing. */ |
573 | } |
574 | else |
575 | { |
576 | /* Report as error. */ |
577 | int amb_len; |
578 | char ambbuf[100]; |
579 | |
580 | for (amb_len = 0; |
581 | ((*line)[amb_len] && (*line)[amb_len] != ' ' |
582 | && (*line)[amb_len] != '\t'); |
583 | amb_len++) |
584 | ; |
585 | |
586 | ambbuf[0] = 0; |
587 | for (c = local_list; c; c = c->next) |
588 | if (!strncmp (*line, c->name, amb_len)) |
589 | { |
590 | if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf) |
591 | { |
592 | if (strlen (ambbuf)) |
593 | strcat (ambbuf, ", "); |
594 | strcat (ambbuf, c->name); |
595 | } |
596 | else |
597 | { |
598 | strcat (ambbuf, ".."); |
599 | break; |
600 | } |
601 | } |
602 | error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype, |
603 | *line, ambbuf); |
604 | } |
605 | } |
606 | else |
607 | { |
608 | /* We've got something. It may still not be what the caller |
609 | wants (if this command *needs* a subcommand). */ |
610 | while (**line == ' ' || **line == '\t') |
611 | (*line)++; |
612 | |
613 | if (c->prefixlist && **line && !c->allow_unknown) |
614 | error ("Undefined %scommand: \"%s\".", c->prefixname, *line); |
615 | |
616 | /* Seems to be what he wants. Return it. */ |
617 | return c; |
618 | } |
619 | } |
620 | |
621 | #if 0 |
7b4ac7e1 |
622 | /* Look up the contents of *LINE as a command in the command list LIST. |
623 | LIST is a chain of struct cmd_list_element's. |
624 | If it is found, return the struct cmd_list_element for that command |
625 | and update *LINE to point after the command name, at the first argument. |
626 | If not found, call error if ALLOW_UNKNOWN is zero |
627 | otherwise (or if error returns) return zero. |
628 | Call error if specified command is ambiguous, |
629 | unless ALLOW_UNKNOWN is negative. |
630 | CMDTYPE precedes the word "command" in the error message. */ |
631 | |
632 | struct cmd_list_element * |
633 | lookup_cmd (line, list, cmdtype, allow_unknown) |
634 | char **line; |
635 | struct cmd_list_element *list; |
636 | char *cmdtype; |
637 | int allow_unknown; |
638 | { |
639 | register char *p; |
640 | register struct cmd_list_element *c, *found; |
641 | int nfound; |
642 | char ambbuf[100]; |
e91b87a3 |
643 | char *processed_cmd; |
644 | int i, cmd_len; |
7b4ac7e1 |
645 | |
646 | /* Skip leading whitespace. */ |
647 | |
648 | while (**line == ' ' || **line == '\t') |
649 | (*line)++; |
650 | |
651 | /* Clear out trailing whitespace. */ |
652 | |
653 | p = *line + strlen (*line); |
654 | while (p != *line && (p[-1] == ' ' || p[-1] == '\t')) |
655 | p--; |
656 | *p = 0; |
657 | |
658 | /* Find end of command name. */ |
659 | |
660 | p = *line; |
4187119d |
661 | while (*p == '-' |
662 | || (*p >= 'a' && *p <= 'z') |
663 | || (*p >= 'A' && *p <= 'Z') |
664 | || (*p >= '0' && *p <= '9')) |
e91b87a3 |
665 | p++; |
7b4ac7e1 |
666 | |
667 | /* Look up the command name. |
668 | If exact match, keep that. |
e91b87a3 |
669 | Otherwise, take command abbreviated, if unique. Note that (in my |
670 | opinion) a null string does *not* indicate ambiguity; simply the |
671 | end of the argument. */ |
672 | |
673 | if (p == *line) |
674 | { |
675 | if (!allow_unknown) |
676 | error ("Lack of needed %scommand", cmdtype); |
677 | return 0; |
678 | } |
679 | |
680 | /* Copy over to a local buffer, converting to lowercase on the way. |
681 | This is in case the command being parsed is a subcommand which |
682 | doesn't match anything, and that's ok. We want the original |
683 | untouched for the routine of the original command. */ |
684 | |
685 | processed_cmd = (char *) alloca (p - *line + 1); |
686 | for (cmd_len = 0; cmd_len < p - *line; cmd_len++) |
687 | { |
688 | char x = (*line)[cmd_len]; |
689 | if (x >= 'A' && x <= 'Z') |
690 | processed_cmd[cmd_len] = x - 'A' + 'a'; |
691 | else |
692 | processed_cmd[cmd_len] = x; |
693 | } |
694 | processed_cmd[cmd_len] = '\0'; |
7b4ac7e1 |
695 | |
e91b87a3 |
696 | /* Check all possibilities in the current command list. */ |
7b4ac7e1 |
697 | found = 0; |
698 | nfound = 0; |
699 | for (c = list; c; c = c->next) |
700 | { |
e91b87a3 |
701 | if (!strncmp (processed_cmd, c->name, cmd_len)) |
7b4ac7e1 |
702 | { |
703 | found = c; |
704 | nfound++; |
e91b87a3 |
705 | if (c->name[cmd_len] == 0) |
7b4ac7e1 |
706 | { |
707 | nfound = 1; |
708 | break; |
709 | } |
710 | } |
711 | } |
712 | |
713 | /* Report error for undefined command name. */ |
714 | |
715 | if (nfound != 1) |
716 | { |
717 | if (nfound > 1 && allow_unknown >= 0) |
718 | { |
7b4ac7e1 |
719 | ambbuf[0] = 0; |
720 | for (c = list; c; c = c->next) |
e91b87a3 |
721 | if (!strncmp (processed_cmd, c->name, cmd_len)) |
7b4ac7e1 |
722 | { |
723 | if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf) |
724 | { |
725 | if (strlen (ambbuf)) |
726 | strcat (ambbuf, ", "); |
727 | strcat (ambbuf, c->name); |
728 | } |
729 | else |
730 | { |
731 | strcat (ambbuf, ".."); |
732 | break; |
733 | } |
734 | } |
e91b87a3 |
735 | error ("Ambiguous %scommand \"%s\": %s.", cmdtype, |
736 | processed_cmd, ambbuf); |
7b4ac7e1 |
737 | } |
738 | else if (!allow_unknown) |
e91b87a3 |
739 | error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd); |
7b4ac7e1 |
740 | return 0; |
741 | } |
742 | |
743 | /* Skip whitespace before the argument. */ |
744 | |
745 | while (*p == ' ' || *p == '\t') p++; |
746 | *line = p; |
747 | |
748 | if (found->prefixlist && *p) |
749 | { |
750 | c = lookup_cmd (line, *found->prefixlist, found->prefixname, |
751 | found->allow_unknown); |
752 | if (c) |
753 | return c; |
754 | } |
755 | |
756 | return found; |
757 | } |
4187119d |
758 | #endif |
759 | |
760 | /* Helper function for SYMBOL_COMPLETION_FUNCTION. */ |
761 | |
762 | /* Return a vector of char pointers which point to the different |
763 | possible completions in LIST of TEXT. */ |
764 | |
765 | char ** |
766 | complete_on_cmdlist (list, text) |
767 | struct cmd_list_element *list; |
768 | char *text; |
769 | { |
770 | struct cmd_list_element *ptr; |
771 | char **matchlist; |
772 | int sizeof_matchlist; |
773 | int matches; |
774 | int textlen = strlen (text); |
775 | |
776 | sizeof_matchlist = 10; |
777 | matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *)); |
778 | matches = 0; |
779 | |
780 | for (ptr = list; ptr; ptr = ptr->next) |
781 | if (!strncmp (ptr->name, text, textlen) |
782 | && !ptr->abbrev_flag |
783 | && (ptr->function |
784 | || ptr->prefixlist)) |
785 | { |
786 | if (matches == sizeof_matchlist) |
787 | { |
788 | sizeof_matchlist *= 2; |
789 | matchlist = (char **) xrealloc (matchlist, |
790 | (sizeof_matchlist |
791 | * sizeof (char *))); |
792 | } |
793 | |
794 | matchlist[matches] = (char *) |
795 | xmalloc (strlen (ptr->name) + 1); |
796 | strcpy (matchlist[matches++], ptr->name); |
797 | } |
798 | |
799 | if (matches == 0) |
800 | { |
801 | free (matchlist); |
802 | matchlist = 0; |
803 | } |
804 | else |
805 | { |
806 | matchlist = (char **) xrealloc (matchlist, ((matches + 1) |
807 | * sizeof (char *))); |
808 | matchlist[matches] = (char *) 0; |
809 | } |
810 | |
811 | return matchlist; |
812 | } |
7b4ac7e1 |
813 | |
e91b87a3 |
814 | static void |
815 | shell_escape (arg, from_tty) |
816 | char *arg; |
817 | int from_tty; |
818 | { |
819 | int rc, status, pid; |
820 | char *p, *user_shell; |
821 | extern char *rindex (); |
822 | |
823 | if ((user_shell = (char *) getenv ("SHELL")) == NULL) |
824 | user_shell = "/bin/sh"; |
825 | |
826 | /* Get the name of the shell for arg0 */ |
827 | if ((p = rindex (user_shell, '/')) == NULL) |
828 | p = user_shell; |
829 | else |
830 | p++; /* Get past '/' */ |
831 | |
832 | if ((pid = fork()) == 0) |
833 | { |
834 | if (!arg) |
835 | execl (user_shell, p, 0); |
836 | else |
837 | execl (user_shell, p, "-c", arg, 0); |
7b4ac7e1 |
838 | |
e91b87a3 |
839 | fprintf (stderr, "Exec of shell failed\n"); |
840 | exit (0); |
841 | } |
842 | |
843 | if (pid != -1) |
844 | while ((rc = wait (&status)) != pid && rc != -1) |
845 | ; |
846 | else |
847 | error ("Fork failed"); |
848 | } |
849 | |
850 | void |
851 | _initialize_command () |
7b4ac7e1 |
852 | { |
e91b87a3 |
853 | add_com ("shell", class_support, shell_escape, |
854 | "Execute the rest of the line as a shell command. \n\ |
855 | With no arguments, run an inferior shell."); |
7b4ac7e1 |
856 | } |