Commit | Line | Data |
---|---|---|
dd3b648e RP |
1 | /* Library for reading command lines and decoding commands. |
2 | Copyright (C) 1986, 1989, 1990 Free Software Foundation, Inc. | |
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 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. */ | |
17 | ||
18 | #include "defs.h" | |
19 | #include "command.h" | |
20 | #include "symtab.h" | |
21 | #include "value.h" | |
22 | #include <stdio.h> | |
23 | #include <ctype.h> | |
24 | #include <string.h> | |
25 | ||
26 | extern char *getenv (); | |
27 | ||
28 | /* Add element named NAME to command list *LIST. | |
29 | FUN should be the function to execute the command; | |
30 | it will get a character string as argument, with leading | |
31 | and trailing blanks already eliminated. | |
32 | ||
33 | DOC is a documentation string for the command. | |
34 | Its first line should be a complete sentence. | |
35 | It should start with ? for a command that is an abbreviation | |
36 | or with * for a command that most users don't need to know about. */ | |
37 | ||
38 | struct cmd_list_element * | |
39 | add_cmd (name, class, fun, doc, list) | |
40 | char *name; | |
41 | enum command_class class; | |
42 | void (*fun) (); | |
43 | char *doc; | |
44 | struct cmd_list_element **list; | |
45 | { | |
46 | register struct cmd_list_element *c | |
47 | = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element)); | |
48 | ||
49 | delete_cmd (name, list); | |
50 | c->next = *list; | |
51 | c->name = name; | |
52 | c->class = class; | |
53 | c->function = fun; | |
54 | c->doc = doc; | |
55 | c->prefixlist = 0; | |
56 | c->prefixname = (char *)NULL; | |
57 | c->allow_unknown = 0; | |
58 | c->abbrev_flag = 0; | |
59 | c->aux = 0; | |
60 | c->type = not_set_cmd; | |
61 | c->completer = make_symbol_completion_list; | |
62 | c->var = 0; | |
63 | c->var_type = var_boolean; | |
64 | c->user_commands = 0; | |
65 | *list = c; | |
66 | return c; | |
67 | } | |
68 | ||
69 | /* Same as above, except that the abbrev_flag is set. */ | |
70 | ||
71 | struct cmd_list_element * | |
72 | add_abbrev_cmd (name, class, fun, doc, list) | |
73 | char *name; | |
74 | enum command_class class; | |
75 | void (*fun) (); | |
76 | char *doc; | |
77 | struct cmd_list_element **list; | |
78 | { | |
79 | register struct cmd_list_element *c | |
80 | = add_cmd (name, class, fun, doc, list); | |
81 | ||
82 | c->abbrev_flag = 1; | |
83 | return c; | |
84 | } | |
85 | ||
86 | struct cmd_list_element * | |
87 | add_alias_cmd (name, oldname, class, abbrev_flag, list) | |
88 | char *name; | |
89 | char *oldname; | |
90 | enum command_class class; | |
91 | int abbrev_flag; | |
92 | struct cmd_list_element **list; | |
93 | { | |
94 | /* Must do this since lookup_cmd tries to side-effect its first arg */ | |
95 | char *copied_name; | |
96 | register struct cmd_list_element *old; | |
97 | register struct cmd_list_element *c; | |
98 | copied_name = (char *) alloca (strlen (oldname) + 1); | |
99 | strcpy (copied_name, oldname); | |
100 | old = lookup_cmd (&copied_name, *list, "", 1, 1); | |
101 | ||
102 | if (old == 0) | |
103 | { | |
104 | delete_cmd (name, list); | |
105 | return 0; | |
106 | } | |
107 | ||
108 | c = add_cmd (name, class, old->function, old->doc, list); | |
109 | c->prefixlist = old->prefixlist; | |
110 | c->prefixname = old->prefixname; | |
111 | c->allow_unknown = old->allow_unknown; | |
112 | c->abbrev_flag = abbrev_flag; | |
113 | c->aux = old->aux; | |
114 | return c; | |
115 | } | |
116 | ||
117 | /* Like add_cmd but adds an element for a command prefix: | |
118 | a name that should be followed by a subcommand to be looked up | |
119 | in another command list. PREFIXLIST should be the address | |
120 | of the variable containing that list. */ | |
121 | ||
122 | struct cmd_list_element * | |
123 | add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname, | |
124 | allow_unknown, list) | |
125 | char *name; | |
126 | enum command_class class; | |
127 | void (*fun) (); | |
128 | char *doc; | |
129 | struct cmd_list_element **prefixlist; | |
130 | char *prefixname; | |
131 | int allow_unknown; | |
132 | struct cmd_list_element **list; | |
133 | { | |
134 | register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list); | |
135 | c->prefixlist = prefixlist; | |
136 | c->prefixname = prefixname; | |
137 | c->allow_unknown = allow_unknown; | |
138 | return c; | |
139 | } | |
140 | ||
141 | /* Like add_prefix_cmd butsets the abbrev_flag on the new command. */ | |
142 | ||
143 | struct cmd_list_element * | |
144 | add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname, | |
145 | allow_unknown, list) | |
146 | char *name; | |
147 | enum command_class class; | |
148 | void (*fun) (); | |
149 | char *doc; | |
150 | struct cmd_list_element **prefixlist; | |
151 | char *prefixname; | |
152 | int allow_unknown; | |
153 | struct cmd_list_element **list; | |
154 | { | |
155 | register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list); | |
156 | c->prefixlist = prefixlist; | |
157 | c->prefixname = prefixname; | |
158 | c->allow_unknown = allow_unknown; | |
159 | c->abbrev_flag = 1; | |
160 | return c; | |
161 | } | |
162 | ||
163 | void | |
164 | not_just_help_class_command (args, from_tty, c) | |
165 | char *args; | |
166 | int from_tty; | |
167 | struct cmd_list_element *c; | |
168 | { | |
169 | } | |
170 | ||
171 | /* Add element named NAME to command list LIST (the list for set | |
172 | or some sublist thereof). | |
173 | CLASS is as in add_cmd. | |
174 | VAR_TYPE is the kind of thing we are setting. | |
175 | VAR is address of the variable being controlled by this command. | |
176 | DOC is the documentation string. */ | |
177 | struct cmd_list_element * | |
178 | add_set_cmd (name, class, var_type, var, doc, list) | |
179 | char *name; | |
180 | enum command_class class; | |
181 | var_types var_type; | |
182 | char *var; | |
183 | char *doc; | |
184 | struct cmd_list_element **list; | |
185 | { | |
186 | /* For set/show, we have to call do_setshow_command | |
187 | differently than an ordinary function (take commandlist as | |
188 | well as arg), so the function field isn't helpful. However, | |
189 | function == NULL means that it's a help class, so set the function | |
190 | to not_just_help_class_command. */ | |
191 | struct cmd_list_element *c | |
192 | = add_cmd (name, class, not_just_help_class_command, doc, list); | |
193 | ||
194 | c->type = set_cmd; | |
195 | c->var_type = var_type; | |
196 | c->var = var; | |
197 | return c; | |
198 | } | |
199 | ||
200 | /* Where SETCMD has already been added, add the corresponding show | |
201 | command to LIST and return a pointer to it. */ | |
202 | struct cmd_list_element * | |
203 | add_show_from_set (setcmd, list) | |
204 | struct cmd_list_element *setcmd; | |
205 | struct cmd_list_element **list; | |
206 | { | |
207 | struct cmd_list_element *showcmd = | |
208 | (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element)); | |
209 | ||
210 | bcopy (setcmd, showcmd, sizeof (struct cmd_list_element)); | |
211 | delete_cmd (showcmd->name, list); | |
212 | showcmd->type = show_cmd; | |
213 | ||
214 | /* Replace "set " at start of docstring with "show ". */ | |
215 | if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e' | |
216 | && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ') | |
217 | showcmd->doc = concat ("Show ", setcmd->doc + 4, ""); | |
218 | else | |
219 | fprintf (stderr, "GDB internal error: Bad docstring for set command\n"); | |
220 | ||
221 | showcmd->next = *list; | |
222 | *list = showcmd; | |
223 | return showcmd; | |
224 | } | |
225 | ||
226 | /* Remove the command named NAME from the command list. */ | |
227 | ||
228 | void | |
229 | delete_cmd (name, list) | |
230 | char *name; | |
231 | struct cmd_list_element **list; | |
232 | { | |
233 | register struct cmd_list_element *c; | |
234 | struct cmd_list_element *p; | |
235 | ||
236 | while (*list && !strcmp ((*list)->name, name)) | |
237 | { | |
238 | p = (*list)->next; | |
239 | free (*list); | |
240 | *list = p; | |
241 | } | |
242 | ||
243 | if (*list) | |
244 | for (c = *list; c->next;) | |
245 | { | |
246 | if (!strcmp (c->next->name, name)) | |
247 | { | |
248 | p = c->next->next; | |
249 | free (c->next); | |
250 | c->next = p; | |
251 | } | |
252 | else | |
253 | c = c->next; | |
254 | } | |
255 | } | |
256 | ||
257 | void help_cmd (), help_list (), help_cmd_list (); | |
258 | ||
259 | /* This command really has to deal with two things: | |
260 | * 1) I want documentation on *this string* (usually called by | |
261 | * "help commandname"). | |
262 | * 2) I want documentation on *this list* (usually called by | |
263 | * giving a command that requires subcommands. Also called by saying | |
264 | * just "help".) | |
265 | * | |
266 | * I am going to split this into two seperate comamnds, help_cmd and | |
267 | * help_list. | |
268 | */ | |
269 | ||
270 | void | |
271 | help_cmd (command, stream) | |
272 | char *command; | |
273 | FILE *stream; | |
274 | { | |
275 | struct cmd_list_element *c; | |
276 | extern struct cmd_list_element *cmdlist; | |
277 | ||
278 | if (!command) | |
279 | { | |
280 | help_list (cmdlist, "", all_classes, stream); | |
281 | return; | |
282 | } | |
283 | ||
284 | c = lookup_cmd (&command, cmdlist, "", 0, 0); | |
285 | ||
286 | if (c == 0) | |
287 | return; | |
288 | ||
289 | /* There are three cases here. | |
290 | If c->prefixlist is nonzero, we have a prefix command. | |
291 | Print its documentation, then list its subcommands. | |
292 | ||
293 | If c->function is nonzero, we really have a command. | |
294 | Print its documentation and return. | |
295 | ||
296 | If c->function is zero, we have a class name. | |
297 | Print its documentation (as if it were a command) | |
298 | and then set class to the number of this class | |
299 | so that the commands in the class will be listed. */ | |
300 | ||
301 | fputs_filtered (c->doc, stream); | |
302 | fputs_filtered ("\n", stream); | |
303 | ||
304 | if (c->prefixlist == 0 && c->function != 0) | |
305 | return; | |
306 | fprintf_filtered (stream, "\n"); | |
307 | ||
308 | /* If this is a prefix command, print it's subcommands */ | |
309 | if (c->prefixlist) | |
310 | help_list (*c->prefixlist, c->prefixname, all_commands, stream); | |
311 | ||
312 | /* If this is a class name, print all of the commands in the class */ | |
313 | if (c->function == 0) | |
314 | help_list (cmdlist, "", c->class, stream); | |
315 | } | |
316 | ||
317 | /* | |
318 | * Get a specific kind of help on a command list. | |
319 | * | |
320 | * LIST is the list. | |
321 | * CMDTYPE is the prefix to use in the title string. | |
322 | * CLASS is the class with which to list the nodes of this list (see | |
323 | * documentation for help_cmd_list below), As usual, ALL_COMMANDS for | |
324 | * everything, ALL_CLASSES for just classes, and non-negative for only things | |
325 | * in a specific class. | |
326 | * and STREAM is the output stream on which to print things. | |
327 | * If you call this routine with a class >= 0, it recurses. | |
328 | */ | |
329 | void | |
330 | help_list (list, cmdtype, class, stream) | |
331 | struct cmd_list_element *list; | |
332 | char *cmdtype; | |
333 | enum command_class class; | |
334 | FILE *stream; | |
335 | { | |
336 | int len; | |
337 | char *cmdtype1, *cmdtype2; | |
338 | ||
339 | /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */ | |
340 | len = strlen (cmdtype); | |
341 | cmdtype1 = (char *) alloca (len + 1); | |
342 | cmdtype1[0] = 0; | |
343 | cmdtype2 = (char *) alloca (len + 4); | |
344 | cmdtype2[0] = 0; | |
345 | if (len) | |
346 | { | |
347 | cmdtype1[0] = ' '; | |
348 | strncpy (cmdtype1 + 1, cmdtype, len - 1); | |
349 | cmdtype1[len] = 0; | |
350 | strncpy (cmdtype2, cmdtype, len - 1); | |
351 | strcpy (cmdtype2 + len - 1, " sub"); | |
352 | } | |
353 | ||
354 | if (class == all_classes) | |
355 | fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2); | |
356 | else | |
357 | fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2); | |
358 | ||
359 | help_cmd_list (list, class, cmdtype, (int)class >= 0, stream); | |
360 | ||
361 | if (class == all_classes) | |
362 | fprintf_filtered (stream, "\n\ | |
363 | Type \"help%s\" followed by a class name for a list of commands in that class.", | |
364 | cmdtype1); | |
365 | ||
366 | fprintf_filtered (stream, "\n\ | |
367 | Type \"help%s\" followed by %scommand name for full documentation.\n\ | |
368 | Command name abbreviations are allowed if unambiguous.\n", | |
369 | cmdtype1, cmdtype2); | |
370 | } | |
371 | ||
372 | /* Print only the first line of STR on STREAM. */ | |
373 | static void | |
374 | print_doc_line (stream, str) | |
375 | FILE *stream; | |
376 | char *str; | |
377 | { | |
378 | static char *line_buffer = 0; | |
379 | static int line_size; | |
380 | register char *p; | |
381 | ||
382 | if (!line_buffer) | |
383 | { | |
384 | line_size = 80; | |
385 | line_buffer = (char *) xmalloc (line_size); | |
386 | } | |
387 | ||
388 | p = str; | |
389 | while (*p && *p != '\n' && *p != '.' && *p != ',') | |
390 | p++; | |
391 | if (p - str > line_size - 1) | |
392 | { | |
393 | line_size = p - str + 1; | |
394 | free (line_buffer); | |
395 | line_buffer = (char *) xmalloc (line_size); | |
396 | } | |
397 | strncpy (line_buffer, str, p - str); | |
398 | line_buffer[p - str] = '\0'; | |
399 | if (islower (line_buffer[0])) | |
400 | line_buffer[0] = toupper (line_buffer[0]); | |
401 | fputs_filtered (line_buffer, stream); | |
402 | } | |
403 | ||
404 | /* | |
405 | * Implement a help command on command list LIST. | |
406 | * RECURSE should be non-zero if this should be done recursively on | |
407 | * all sublists of LIST. | |
408 | * PREFIX is the prefix to print before each command name. | |
409 | * STREAM is the stream upon which the output should be written. | |
410 | * CLASS should be: | |
411 | * A non-negative class number to list only commands in that | |
412 | * class. | |
413 | * ALL_COMMANDS to list all commands in list. | |
414 | * ALL_CLASSES to list all classes in list. | |
415 | * | |
416 | * Note that RECURSE will be active on *all* sublists, not just the | |
417 | * ones seclected by the criteria above (ie. the selection mechanism | |
418 | * is at the low level, not the high-level). | |
419 | */ | |
420 | void | |
421 | help_cmd_list (list, class, prefix, recurse, stream) | |
422 | struct cmd_list_element *list; | |
423 | enum command_class class; | |
424 | char *prefix; | |
425 | int recurse; | |
426 | FILE *stream; | |
427 | { | |
428 | register struct cmd_list_element *c; | |
429 | ||
430 | for (c = list; c; c = c->next) | |
431 | { | |
432 | if (c->abbrev_flag == 0 && | |
433 | (class == all_commands | |
434 | || (class == all_classes && c->function == 0) | |
435 | || (class == c->class && c->function != 0))) | |
436 | { | |
437 | fprintf_filtered (stream, "%s%s -- ", prefix, c->name); | |
438 | print_doc_line (stream, c->doc); | |
439 | fputs_filtered ("\n", stream); | |
440 | } | |
441 | if (recurse | |
442 | && c->prefixlist != 0 | |
443 | && c->abbrev_flag == 0) | |
444 | help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream); | |
445 | } | |
446 | } | |
447 | \f | |
448 | /* This routine takes a line of TEXT and a CLIST in which to | |
449 | start the lookup. When it returns it will have incremented the text | |
450 | pointer past the section of text it matched, set *RESULT_LIST to | |
451 | the list in which the last word was matched, and will return the | |
452 | cmd list element which the text matches. It will return 0 if no | |
453 | match at all was possible. It will return -1 if ambigous matches are | |
454 | possible; in this case *RESULT_LIST will be set to the list in which | |
455 | there are ambiguous choices (and text will be set to the ambiguous | |
456 | text string). | |
457 | ||
458 | It does no error reporting whatsoever; control will always return | |
459 | to the superior routine. | |
460 | ||
461 | In the case of an ambiguous return (-1), *RESULT_LIST will be set to | |
462 | point at the prefix_command (ie. the best match) *or* (special | |
463 | case) will be 0 if no prefix command was ever found. For example, | |
464 | in the case of "info a", "info" matches without ambiguity, but "a" | |
465 | could be "args" or "address", so *RESULT_LIST is set to | |
466 | the cmd_list_element for "info". So in this case | |
467 | result list should not be interpeted as a pointer to the beginning | |
468 | of a list; it simply points to a specific command. | |
469 | ||
470 | If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise | |
471 | affect the operation). | |
472 | ||
473 | This routine does *not* modify the text pointed to by TEXT. | |
474 | ||
475 | If IGNORE_HELP_CLASSES is nonzero, ignore any command list | |
476 | elements which are actually help classes rather than commands (i.e. | |
477 | the function field of the struct cmd_list_element is 0). */ | |
478 | ||
479 | struct cmd_list_element * | |
480 | lookup_cmd_1 (text, clist, result_list, ignore_help_classes) | |
481 | char **text; | |
482 | struct cmd_list_element *clist, **result_list; | |
483 | int ignore_help_classes; | |
484 | { | |
485 | char *p, *command; | |
486 | int len, tmp, nfound; | |
487 | struct cmd_list_element *found, *c; | |
488 | ||
489 | while (**text == ' ' || **text == '\t') | |
490 | (*text)++; | |
491 | ||
492 | /* Treating underscores as part of command words is important | |
493 | so that "set args_foo()" doesn't get interpreted as | |
494 | "set args _foo()". */ | |
495 | for (p = *text; | |
496 | *p && (isalnum(*p) || *p == '-' || *p == '_'); | |
497 | p++) | |
498 | ; | |
499 | ||
500 | /* If nothing but whitespace, return 0. */ | |
501 | if (p == *text) | |
502 | return 0; | |
503 | ||
504 | len = p - *text; | |
505 | ||
506 | /* *text and p now bracket the first command word to lookup (and | |
507 | it's length is len). We copy this into a local temporary, | |
508 | converting to lower case as we go. */ | |
509 | ||
510 | command = (char *) alloca (len + 1); | |
511 | for (tmp = 0; tmp < len; tmp++) | |
512 | { | |
513 | char x = (*text)[tmp]; | |
514 | command[tmp] = (x >= 'A' && x <= 'Z') ? x - 'A' + 'a' : x; | |
515 | } | |
516 | command[len] = '\0'; | |
517 | ||
518 | /* Look it up. */ | |
519 | found = 0; | |
520 | nfound = 0; | |
521 | for (c = clist; c; c = c->next) | |
522 | if (!strncmp (command, c->name, len) | |
523 | && (!ignore_help_classes || c->function)) | |
524 | { | |
525 | found = c; | |
526 | nfound++; | |
527 | if (c->name[len] == '\0') | |
528 | { | |
529 | nfound = 1; | |
530 | break; | |
531 | } | |
532 | } | |
533 | ||
534 | /* If nothing matches, we have a simple failure. */ | |
535 | if (nfound == 0) | |
536 | return 0; | |
537 | ||
538 | if (nfound > 1) | |
539 | { | |
540 | if (result_list != NULL) | |
541 | /* Will be modified in calling routine | |
542 | if we know what the prefix command is. */ | |
543 | *result_list = 0; | |
544 | return (struct cmd_list_element *) -1; /* Ambiguous. */ | |
545 | } | |
546 | ||
547 | /* We've matched something on this list. Move text pointer forward. */ | |
548 | ||
549 | *text = p; | |
550 | if (found->prefixlist) | |
551 | { | |
552 | c = lookup_cmd_1 (text, *found->prefixlist, result_list, | |
553 | ignore_help_classes); | |
554 | if (!c) | |
555 | { | |
556 | /* Didn't find anything; this is as far as we got. */ | |
557 | if (result_list != NULL) | |
558 | *result_list = clist; | |
559 | return found; | |
560 | } | |
561 | else if (c == (struct cmd_list_element *) -1) | |
562 | { | |
563 | /* We've gotten this far properley, but the next step | |
564 | is ambiguous. We need to set the result list to the best | |
565 | we've found (if an inferior hasn't already set it). */ | |
566 | if (result_list != NULL) | |
567 | if (!*result_list) | |
568 | /* This used to say *result_list = *found->prefixlist | |
569 | If that was correct, need to modify the documentation | |
570 | at the top of this function to clarify what is supposed | |
571 | to be going on. */ | |
572 | *result_list = found; | |
573 | return c; | |
574 | } | |
575 | else | |
576 | { | |
577 | /* We matched! */ | |
578 | return c; | |
579 | } | |
580 | } | |
581 | else | |
582 | { | |
583 | if (result_list != NULL) | |
584 | *result_list = clist; | |
585 | return found; | |
586 | } | |
587 | } | |
588 | ||
589 | /* Look up the contents of *LINE as a command in the command list LIST. | |
590 | LIST is a chain of struct cmd_list_element's. | |
591 | If it is found, return the struct cmd_list_element for that command | |
592 | and update *LINE to point after the command name, at the first argument. | |
593 | If not found, call error if ALLOW_UNKNOWN is zero | |
594 | otherwise (or if error returns) return zero. | |
595 | Call error if specified command is ambiguous, | |
596 | unless ALLOW_UNKNOWN is negative. | |
597 | CMDTYPE precedes the word "command" in the error message. | |
598 | ||
599 | If INGNORE_HELP_CLASSES is nonzero, ignore any command list | |
600 | elements which are actually help classes rather than commands (i.e. | |
601 | the function field of the struct cmd_list_element is 0). */ | |
602 | ||
603 | struct cmd_list_element * | |
604 | lookup_cmd (line, list, cmdtype, allow_unknown, ignore_help_classes) | |
605 | char **line; | |
606 | struct cmd_list_element *list; | |
607 | char *cmdtype; | |
608 | int allow_unknown; | |
609 | int ignore_help_classes; | |
610 | { | |
611 | struct cmd_list_element *last_list = 0; | |
612 | struct cmd_list_element *c = | |
613 | lookup_cmd_1 (line, list, &last_list, ignore_help_classes); | |
614 | char *ptr = (*line) + strlen (*line) - 1; | |
615 | ||
616 | /* Clear off trailing whitespace. */ | |
617 | while (ptr >= *line && (*ptr == ' ' || *ptr == '\t')) | |
618 | ptr--; | |
619 | *(ptr + 1) = '\0'; | |
620 | ||
621 | if (!c) | |
622 | { | |
623 | if (!allow_unknown) | |
624 | { | |
625 | if (!*line) | |
626 | error ("Lack of needed %scommand", cmdtype); | |
627 | else | |
628 | { | |
629 | char *p = *line, *q; | |
630 | ||
631 | while (isalnum(*p) || *p == '-') | |
632 | p++; | |
633 | ||
634 | q = (char *) alloca (p - *line + 1); | |
635 | strncpy (q, *line, p - *line); | |
636 | q[p-*line] = '\0'; | |
637 | ||
638 | error ("Undefined %scommand: \"%s\".", cmdtype, q); | |
639 | } | |
640 | } | |
641 | else | |
642 | return 0; | |
643 | } | |
644 | else if (c == (struct cmd_list_element *) -1) | |
645 | { | |
646 | /* Ambigous. Local values should be off prefixlist or called | |
647 | values. */ | |
648 | int local_allow_unknown = (last_list ? last_list->allow_unknown : | |
649 | allow_unknown); | |
650 | char *local_cmdtype = last_list ? last_list->prefixname : cmdtype; | |
651 | struct cmd_list_element *local_list = | |
652 | (last_list ? *(last_list->prefixlist) : list); | |
653 | ||
654 | if (local_allow_unknown < 0) | |
655 | { | |
656 | if (last_list) | |
657 | return last_list; /* Found something. */ | |
658 | else | |
659 | return 0; /* Found nothing. */ | |
660 | } | |
661 | else | |
662 | { | |
663 | /* Report as error. */ | |
664 | int amb_len; | |
665 | char ambbuf[100]; | |
666 | ||
667 | for (amb_len = 0; | |
668 | ((*line)[amb_len] && (*line)[amb_len] != ' ' | |
669 | && (*line)[amb_len] != '\t'); | |
670 | amb_len++) | |
671 | ; | |
672 | ||
673 | ambbuf[0] = 0; | |
674 | for (c = local_list; c; c = c->next) | |
675 | if (!strncmp (*line, c->name, amb_len)) | |
676 | { | |
677 | if (strlen (ambbuf) + strlen (c->name) + 6 < (int)sizeof ambbuf) | |
678 | { | |
679 | if (strlen (ambbuf)) | |
680 | strcat (ambbuf, ", "); | |
681 | strcat (ambbuf, c->name); | |
682 | } | |
683 | else | |
684 | { | |
685 | strcat (ambbuf, ".."); | |
686 | break; | |
687 | } | |
688 | } | |
689 | error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype, | |
690 | *line, ambbuf); | |
691 | return 0; /* lint */ | |
692 | } | |
693 | } | |
694 | else | |
695 | { | |
696 | /* We've got something. It may still not be what the caller | |
697 | wants (if this command *needs* a subcommand). */ | |
698 | while (**line == ' ' || **line == '\t') | |
699 | (*line)++; | |
700 | ||
701 | if (c->prefixlist && **line && !c->allow_unknown) | |
702 | error ("Undefined %scommand: \"%s\".", c->prefixname, *line); | |
703 | ||
704 | /* Seems to be what he wants. Return it. */ | |
705 | return c; | |
706 | } | |
707 | return 0; | |
708 | } | |
709 | ||
710 | #if 0 | |
711 | /* Look up the contents of *LINE as a command in the command list LIST. | |
712 | LIST is a chain of struct cmd_list_element's. | |
713 | If it is found, return the struct cmd_list_element for that command | |
714 | and update *LINE to point after the command name, at the first argument. | |
715 | If not found, call error if ALLOW_UNKNOWN is zero | |
716 | otherwise (or if error returns) return zero. | |
717 | Call error if specified command is ambiguous, | |
718 | unless ALLOW_UNKNOWN is negative. | |
719 | CMDTYPE precedes the word "command" in the error message. */ | |
720 | ||
721 | struct cmd_list_element * | |
722 | lookup_cmd (line, list, cmdtype, allow_unknown) | |
723 | char **line; | |
724 | struct cmd_list_element *list; | |
725 | char *cmdtype; | |
726 | int allow_unknown; | |
727 | { | |
728 | register char *p; | |
729 | register struct cmd_list_element *c, *found; | |
730 | int nfound; | |
731 | char ambbuf[100]; | |
732 | char *processed_cmd; | |
733 | int i, cmd_len; | |
734 | ||
735 | /* Skip leading whitespace. */ | |
736 | ||
737 | while (**line == ' ' || **line == '\t') | |
738 | (*line)++; | |
739 | ||
740 | /* Clear out trailing whitespace. */ | |
741 | ||
742 | p = *line + strlen (*line); | |
743 | while (p != *line && (p[-1] == ' ' || p[-1] == '\t')) | |
744 | p--; | |
745 | *p = 0; | |
746 | ||
747 | /* Find end of command name. */ | |
748 | ||
749 | p = *line; | |
750 | while (*p == '-' | |
751 | || (*p >= 'a' && *p <= 'z') | |
752 | || (*p >= 'A' && *p <= 'Z') | |
753 | || (*p >= '0' && *p <= '9')) | |
754 | p++; | |
755 | ||
756 | /* Look up the command name. | |
757 | If exact match, keep that. | |
758 | Otherwise, take command abbreviated, if unique. Note that (in my | |
759 | opinion) a null string does *not* indicate ambiguity; simply the | |
760 | end of the argument. */ | |
761 | ||
762 | if (p == *line) | |
763 | { | |
764 | if (!allow_unknown) | |
765 | error ("Lack of needed %scommand", cmdtype); | |
766 | return 0; | |
767 | } | |
768 | ||
769 | /* Copy over to a local buffer, converting to lowercase on the way. | |
770 | This is in case the command being parsed is a subcommand which | |
771 | doesn't match anything, and that's ok. We want the original | |
772 | untouched for the routine of the original command. */ | |
773 | ||
774 | processed_cmd = (char *) alloca (p - *line + 1); | |
775 | for (cmd_len = 0; cmd_len < p - *line; cmd_len++) | |
776 | { | |
777 | char x = (*line)[cmd_len]; | |
778 | if (x >= 'A' && x <= 'Z') | |
779 | processed_cmd[cmd_len] = x - 'A' + 'a'; | |
780 | else | |
781 | processed_cmd[cmd_len] = x; | |
782 | } | |
783 | processed_cmd[cmd_len] = '\0'; | |
784 | ||
785 | /* Check all possibilities in the current command list. */ | |
786 | found = 0; | |
787 | nfound = 0; | |
788 | for (c = list; c; c = c->next) | |
789 | { | |
790 | if (!strncmp (processed_cmd, c->name, cmd_len)) | |
791 | { | |
792 | found = c; | |
793 | nfound++; | |
794 | if (c->name[cmd_len] == 0) | |
795 | { | |
796 | nfound = 1; | |
797 | break; | |
798 | } | |
799 | } | |
800 | } | |
801 | ||
802 | /* Report error for undefined command name. */ | |
803 | ||
804 | if (nfound != 1) | |
805 | { | |
806 | if (nfound > 1 && allow_unknown >= 0) | |
807 | { | |
808 | ambbuf[0] = 0; | |
809 | for (c = list; c; c = c->next) | |
810 | if (!strncmp (processed_cmd, c->name, cmd_len)) | |
811 | { | |
812 | if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf) | |
813 | { | |
814 | if (strlen (ambbuf)) | |
815 | strcat (ambbuf, ", "); | |
816 | strcat (ambbuf, c->name); | |
817 | } | |
818 | else | |
819 | { | |
820 | strcat (ambbuf, ".."); | |
821 | break; | |
822 | } | |
823 | } | |
824 | error ("Ambiguous %scommand \"%s\": %s.", cmdtype, | |
825 | processed_cmd, ambbuf); | |
826 | } | |
827 | else if (!allow_unknown) | |
828 | error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd); | |
829 | return 0; | |
830 | } | |
831 | ||
832 | /* Skip whitespace before the argument. */ | |
833 | ||
834 | while (*p == ' ' || *p == '\t') p++; | |
835 | *line = p; | |
836 | ||
837 | if (found->prefixlist && *p) | |
838 | { | |
839 | c = lookup_cmd (line, *found->prefixlist, found->prefixname, | |
840 | found->allow_unknown); | |
841 | if (c) | |
842 | return c; | |
843 | } | |
844 | ||
845 | return found; | |
846 | } | |
847 | #endif | |
848 | ||
849 | /* Helper function for SYMBOL_COMPLETION_FUNCTION. */ | |
850 | ||
851 | /* Return a vector of char pointers which point to the different | |
852 | possible completions in LIST of TEXT. */ | |
853 | ||
854 | char ** | |
855 | complete_on_cmdlist (list, text) | |
856 | struct cmd_list_element *list; | |
857 | char *text; | |
858 | { | |
859 | struct cmd_list_element *ptr; | |
860 | char **matchlist; | |
861 | int sizeof_matchlist; | |
862 | int matches; | |
863 | int textlen = strlen (text); | |
864 | ||
865 | sizeof_matchlist = 10; | |
866 | matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *)); | |
867 | matches = 0; | |
868 | ||
869 | for (ptr = list; ptr; ptr = ptr->next) | |
870 | if (!strncmp (ptr->name, text, textlen) | |
871 | && !ptr->abbrev_flag | |
872 | && (ptr->function | |
873 | || ptr->prefixlist)) | |
874 | { | |
875 | if (matches == sizeof_matchlist) | |
876 | { | |
877 | sizeof_matchlist *= 2; | |
878 | matchlist = (char **) xrealloc ((char *)matchlist, | |
879 | (sizeof_matchlist | |
880 | * sizeof (char *))); | |
881 | } | |
882 | ||
883 | matchlist[matches] = (char *) | |
884 | xmalloc (strlen (ptr->name) + 1); | |
885 | strcpy (matchlist[matches++], ptr->name); | |
886 | } | |
887 | ||
888 | if (matches == 0) | |
889 | { | |
890 | free (matchlist); | |
891 | matchlist = 0; | |
892 | } | |
893 | else | |
894 | { | |
895 | matchlist = (char **) xrealloc ((char *)matchlist, ((matches + 1) | |
896 | * sizeof (char *))); | |
897 | matchlist[matches] = (char *) 0; | |
898 | } | |
899 | ||
900 | return matchlist; | |
901 | } | |
902 | ||
903 | static int | |
904 | parse_binary_operation (arg) | |
905 | char *arg; | |
906 | { | |
907 | int length; | |
908 | ||
909 | if (!arg || !*arg) | |
910 | return 1; | |
911 | ||
912 | length = strlen (arg); | |
913 | ||
914 | while (arg[length - 1] == ' ' || arg[length - 1] == '\t') | |
915 | length--; | |
916 | ||
917 | if (!strncmp (arg, "on", length) | |
918 | || !strncmp (arg, "1", length) | |
919 | || !strncmp (arg, "yes", length)) | |
920 | return 1; | |
921 | else | |
922 | if (!strncmp (arg, "off", length) | |
923 | || !strncmp (arg, "0", length) | |
924 | || !strncmp (arg, "no", length)) | |
925 | return 0; | |
926 | else | |
927 | { | |
928 | error ("\"on\" or \"off\" expected."); | |
929 | return 0; | |
930 | } | |
931 | } | |
932 | ||
933 | /* Do a "set" or "show" command. ARG is NULL if no argument, or the text | |
934 | of the argument, and FROM_TTY is nonzero if this command is being entered | |
935 | directly by the user (i.e. these are just like any other | |
936 | command). C is the command list element for the command. */ | |
937 | void | |
938 | do_setshow_command (arg, from_tty, c) | |
939 | char *arg; | |
940 | int from_tty; | |
941 | struct cmd_list_element *c; | |
942 | { | |
943 | if (c->type == set_cmd) | |
944 | { | |
945 | switch (c->var_type) | |
946 | { | |
947 | case var_string: | |
948 | { | |
949 | char *new; | |
950 | char *p; | |
951 | char *q; | |
952 | int ch; | |
953 | ||
954 | if (arg == NULL) | |
955 | arg = ""; | |
956 | new = (char *) xmalloc (strlen (arg) + 2); | |
957 | p = arg; q = new; | |
958 | while (ch = *p++) | |
959 | { | |
960 | if (ch == '\\') | |
961 | { | |
962 | /* \ at end of argument is used after spaces | |
963 | so they won't be lost. */ | |
964 | if (*p == 0) | |
965 | break; | |
966 | ch = parse_escape (&p); | |
967 | if (ch == 0) | |
968 | break; /* C loses */ | |
969 | else if (ch > 0) | |
970 | *q++ = ch; | |
971 | } | |
972 | else | |
973 | *q++ = ch; | |
974 | } | |
975 | if (*(p - 1) != '\\') | |
976 | *q++ = ' '; | |
977 | *q++ = '\0'; | |
978 | new = (char *) xrealloc (new, q - new); | |
979 | if (*(char **)c->var != NULL) | |
980 | free (*(char **)c->var); | |
981 | *(char **) c->var = new; | |
982 | } | |
983 | break; | |
984 | case var_string_noescape: | |
985 | if (arg == NULL) | |
986 | arg = ""; | |
987 | if (*(char **)c->var != NULL) | |
988 | free (*(char **)c->var); | |
989 | *(char **) c->var = savestring (arg, strlen (arg)); | |
990 | break; | |
991 | case var_filename: | |
992 | if (arg == NULL) | |
993 | error_no_arg ("filename to set it to."); | |
994 | if (*(char **)c->var != NULL) | |
995 | free (*(char **)c->var); | |
996 | *(char **)c->var = tilde_expand (arg); | |
997 | break; | |
998 | case var_boolean: | |
999 | *(int *) c->var = parse_binary_operation (arg); | |
1000 | break; | |
1001 | case var_uinteger: | |
1002 | if (arg == NULL) | |
1003 | error_no_arg ("integer to set it to."); | |
1004 | *(int *) c->var = parse_and_eval_address (arg); | |
1005 | if (*(int *) c->var == 0) | |
1006 | *(int *) c->var = UINT_MAX; | |
1007 | break; | |
1008 | case var_zinteger: | |
1009 | if (arg == NULL) | |
1010 | error_no_arg ("integer to set it to."); | |
1011 | *(int *) c->var = parse_and_eval_address (arg); | |
1012 | break; | |
1013 | default: | |
1014 | error ("gdb internal error: bad var_type in do_setshow_command"); | |
1015 | } | |
1016 | } | |
1017 | else if (c->type == show_cmd) | |
1018 | { | |
1019 | /* Print doc minus "show" at start. */ | |
1020 | print_doc_line (stdout, c->doc + 5); | |
1021 | ||
1022 | fputs_filtered (" is ", stdout); | |
1023 | wrap_here (" "); | |
1024 | switch (c->var_type) | |
1025 | { | |
1026 | case var_string: | |
1027 | { | |
1028 | unsigned char *p; | |
1029 | fputs_filtered ("\"", stdout); | |
1030 | for (p = *(unsigned char **) c->var; *p != '\0'; p++) | |
1031 | printchar (*p, stdout, '"'); | |
1032 | fputs_filtered ("\"", stdout); | |
1033 | } | |
1034 | break; | |
1035 | case var_string_noescape: | |
1036 | case var_filename: | |
1037 | fputs_filtered ("\"", stdout); | |
1038 | fputs_filtered (*(char **) c->var, stdout); | |
1039 | fputs_filtered ("\"", stdout); | |
1040 | break; | |
1041 | case var_boolean: | |
1042 | fputs_filtered (*(int *) c->var ? "on" : "off", stdout); | |
1043 | break; | |
1044 | case var_uinteger: | |
1045 | if (*(unsigned int *) c->var == UINT_MAX) { | |
1046 | fputs_filtered ("unlimited", stdout); | |
1047 | break; | |
1048 | } | |
1049 | /* else fall through */ | |
1050 | case var_zinteger: | |
1051 | fprintf_filtered (stdout, "%d", *(unsigned int *) c->var); | |
1052 | break; | |
1053 | default: | |
1054 | error ("gdb internal error: bad var_type in do_setshow_command"); | |
1055 | } | |
1056 | fputs_filtered (".\n", stdout); | |
1057 | } | |
1058 | else | |
1059 | error ("gdb internal error: bad cmd_type in do_setshow_command"); | |
1060 | (*c->function) (NULL, from_tty, c); | |
1061 | } | |
1062 | ||
1063 | /* Show all the settings in a list of show commands. */ | |
1064 | ||
1065 | void | |
1066 | cmd_show_list (list, from_tty, prefix) | |
1067 | struct cmd_list_element *list; | |
1068 | int from_tty; | |
1069 | char *prefix; | |
1070 | { | |
1071 | for (; list != NULL; list = list->next) { | |
1072 | /* If we find a prefix, run its list, prefixing our output by its | |
1073 | prefix (with "show " skipped). */ | |
1074 | if (list->prefixlist && !list->abbrev_flag) | |
1075 | cmd_show_list (*list->prefixlist, from_tty, list->prefixname + 5); | |
1076 | if (list->type == show_cmd) | |
1077 | { | |
1078 | fputs_filtered (prefix, stdout); | |
1079 | fputs_filtered (list->name, stdout); | |
1080 | fputs_filtered (": ", stdout); | |
1081 | do_setshow_command ((char *)NULL, from_tty, list); | |
1082 | } | |
1083 | } | |
1084 | } | |
1085 | ||
1086 | static void | |
1087 | shell_escape (arg, from_tty) | |
1088 | char *arg; | |
1089 | int from_tty; | |
1090 | { | |
1091 | int rc, status, pid; | |
1092 | char *p, *user_shell; | |
1093 | extern char *rindex (); | |
1094 | ||
1095 | if ((user_shell = (char *) getenv ("SHELL")) == NULL) | |
1096 | user_shell = "/bin/sh"; | |
1097 | ||
1098 | /* Get the name of the shell for arg0 */ | |
1099 | if ((p = rindex (user_shell, '/')) == NULL) | |
1100 | p = user_shell; | |
1101 | else | |
1102 | p++; /* Get past '/' */ | |
1103 | ||
1104 | if ((pid = fork()) == 0) | |
1105 | { | |
1106 | if (!arg) | |
1107 | execl (user_shell, p, 0); | |
1108 | else | |
1109 | execl (user_shell, p, "-c", arg, 0); | |
1110 | ||
1111 | fprintf (stderr, "Exec of shell failed\n"); | |
1112 | exit (0); | |
1113 | } | |
1114 | ||
1115 | if (pid != -1) | |
1116 | while ((rc = wait (&status)) != pid && rc != -1) | |
1117 | ; | |
1118 | else | |
1119 | error ("Fork failed"); | |
1120 | } | |
1121 | ||
1122 | static void | |
1123 | make_command (arg, from_tty) | |
1124 | char *arg; | |
1125 | int from_tty; | |
1126 | { | |
1127 | char *p; | |
1128 | ||
1129 | if (arg == 0) | |
1130 | p = "make"; | |
1131 | else | |
1132 | { | |
1133 | p = xmalloc (sizeof("make ") + strlen(arg)); | |
1134 | strcpy (p, "make "); | |
1135 | strcpy (p + sizeof("make ")-1, arg); | |
1136 | } | |
1137 | ||
1138 | shell_escape (p, from_tty); | |
1139 | } | |
1140 | ||
1141 | void | |
1142 | _initialize_command () | |
1143 | { | |
1144 | add_com ("shell", class_support, shell_escape, | |
1145 | "Execute the rest of the line as a shell command. \n\ | |
1146 | With no arguments, run an inferior shell."); | |
1147 | ||
1148 | add_com ("make", class_support, make_command, | |
1149 | "Run the ``make'' program using the rest of the line as arguments."); | |
1150 | } |