]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/command.c
gdb-3.1
[thirdparty/binutils-gdb.git] / gdb / command.c
1 /* Library for reading command lines and decoding commands.
2 Copyright (C) 1986 Free Software Foundation, Inc.
3
4 NO WARRANTY
5
6 BECAUSE THIS PROGRAM IS LICENSED FREE OF CHARGE, WE PROVIDE ABSOLUTELY
7 NO WARRANTY, TO THE EXTENT PERMITTED BY APPLICABLE STATE LAW. EXCEPT
8 WHEN OTHERWISE STATED IN WRITING, FREE SOFTWARE FOUNDATION, INC,
9 RICHARD M. STALLMAN AND/OR OTHER PARTIES PROVIDE THIS PROGRAM "AS IS"
10 WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
11 BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
12 FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY
13 AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
14 DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
15 CORRECTION.
16
17 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW WILL RICHARD M.
18 STALLMAN, THE FREE SOFTWARE FOUNDATION, INC., AND/OR ANY OTHER PARTY
19 WHO MAY MODIFY AND REDISTRIBUTE THIS PROGRAM AS PERMITTED BELOW, BE
20 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY LOST PROFITS, LOST MONIES, OR
21 OTHER SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
22 USE OR INABILITY TO USE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR
23 DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY THIRD PARTIES OR
24 A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS) THIS
25 PROGRAM, EVEN IF YOU HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
26 DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY.
27
28 GENERAL PUBLIC LICENSE TO COPY
29
30 1. You may copy and distribute verbatim copies of this source file
31 as you receive it, in any medium, provided that you conspicuously and
32 appropriately publish on each copy a valid copyright notice "Copyright
33 (C) 1986 Free Software Foundation, Inc."; and include following the
34 copyright notice a verbatim copy of the above disclaimer of warranty
35 and of this License. You may charge a distribution fee for the
36 physical act of transferring a copy.
37
38 2. You may modify your copy or copies of this source file or
39 any portion of it, and copy and distribute such modifications under
40 the terms of Paragraph 1 above, provided that you also do the following:
41
42 a) cause the modified files to carry prominent notices stating
43 that you changed the files and the date of any change; and
44
45 b) cause the whole of any work that you distribute or publish,
46 that in whole or in part contains or is a derivative of this
47 program or any part thereof, to be licensed at no charge to all
48 third parties on terms identical to those contained in this
49 License Agreement (except that you may choose to grant more extensive
50 warranty protection to some or all third parties, at your option).
51
52 c) You may charge a distribution fee for the physical act of
53 transferring a copy, and you may at your option offer warranty
54 protection in exchange for a fee.
55
56 Mere aggregation of another unrelated program with this program (or its
57 derivative) on a volume of a storage or distribution medium does not bring
58 the other program under the scope of these terms.
59
60 3. You may copy and distribute this program (or a portion or derivative
61 of it, under Paragraph 2) in object code or executable form under the terms
62 of Paragraphs 1 and 2 above provided that you also do one of the following:
63
64 a) accompany it with the complete corresponding machine-readable
65 source code, which must be distributed under the terms of
66 Paragraphs 1 and 2 above; or,
67
68 b) accompany it with a written offer, valid for at least three
69 years, to give any third party free (except for a nominal
70 shipping charge) a complete machine-readable copy of the
71 corresponding source code, to be distributed under the terms of
72 Paragraphs 1 and 2 above; or,
73
74 c) accompany it with the information you received as to where the
75 corresponding source code may be obtained. (This alternative is
76 allowed only for noncommercial distribution and only if you
77 received the program in object code or executable form alone.)
78
79 For an executable file, complete source code means all the source code for
80 all modules it contains; but, as a special exception, it need not include
81 source code for modules which are standard libraries that accompany the
82 operating system on which the executable file runs.
83
84 4. You may not copy, sublicense, distribute or transfer this program
85 except as expressly provided under this License Agreement. Any attempt
86 otherwise to copy, sublicense, distribute or transfer this program is void and
87 your rights to use the program under this License agreement shall be
88 automatically terminated. However, parties who have received computer
89 software programs from you with this License Agreement will not have
90 their licenses terminated so long as such parties remain in full compliance.
91
92 5. If you wish to incorporate parts of this program into other free
93 programs whose distribution conditions are different, write to the Free
94 Software Foundation at 675 Mass Ave, Cambridge, MA 02139. We have not yet
95 worked out a simple rule that can be stated here, but we will often permit
96 this. We will be guided by the two goals of preserving the free status of
97 all derivatives of our free software and of promoting the sharing and reuse of
98 software.
99
100
101 In other words, you are welcome to use, share and improve this program.
102 You are forbidden to forbid anyone else to use, share and improve
103 what you give them. Help stamp out software-hoarding! */
104
105
106 #include "command.h"
107 #include "defs.h"
108 #include <stdio.h>
109
110 #ifdef sparc
111 #include <alloca.h>
112 #endif
113
114 extern char *xmalloc ();
115
116 /* Add element named NAME to command list *LIST.
117 FUN should be the function to execute the command;
118 it will get a character string as argument, with leading
119 and trailing blanks already eliminated.
120
121 DOC is a documentation string for the command.
122 Its first line should be a complete sentence.
123 It should start with ? for a command that is an abbreviation
124 or with * for a command that most users don't need to know about. */
125
126 struct cmd_list_element *
127 add_cmd (name, class, fun, doc, list)
128 char *name;
129 int class;
130 void (*fun) ();
131 char *doc;
132 struct cmd_list_element **list;
133 {
134 register struct cmd_list_element *c
135 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
136
137 delete_cmd (name, list);
138 c->next = *list;
139 c->name = savestring (name, strlen (name));
140 c->class = class;
141 c->function = fun;
142 c->doc = doc;
143 c->prefixlist = 0;
144 c->allow_unknown = 0;
145 c->abbrev_flag = 0;
146 c->aux = 0;
147 *list = c;
148 return c;
149 }
150
151 /* Same as above, except that the abbrev_flag is set. */
152
153 struct cmd_list_element *
154 add_abbrev_cmd (name, class, fun, doc, list)
155 char *name;
156 int class;
157 void (*fun) ();
158 char *doc;
159 struct cmd_list_element **list;
160 {
161 register struct cmd_list_element *c
162 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
163
164 delete_cmd (name, list);
165 c->next = *list;
166 c->name = savestring (name, strlen (name));
167 c->class = class;
168 c->function = fun;
169 c->doc = doc;
170 c->prefixlist = 0;
171 c->allow_unknown = 0;
172 c->abbrev_flag = 1;
173 c->aux = 0;
174 *list = c;
175 return c;
176 }
177
178 struct cmd_list_element *
179 add_alias_cmd (name, oldname, class, abbrev_flag, list)
180 char *name;
181 char *oldname;
182 int class;
183 int abbrev_flag;
184 struct cmd_list_element **list;
185 {
186 /* Must do this since lookup_cmd tries to side-effect its first arg */
187 char *copied_name;
188 register struct cmd_list_element *old;
189 register struct cmd_list_element *c;
190 copied_name = (char *) alloca (strlen (oldname) + 1);
191 strcpy (copied_name, oldname);
192 old = lookup_cmd (&copied_name, *list, 0, 1);
193
194 if (old == 0)
195 {
196 delete_cmd (name, list);
197 return 0;
198 }
199
200 c = add_cmd (name, class, old->function, old->doc, list);
201 c->prefixlist = old->prefixlist;
202 c->prefixname = old->prefixname;
203 c->allow_unknown = old->allow_unknown;
204 c->abbrev_flag = abbrev_flag;
205 c->aux = old->aux;
206 return c;
207 }
208
209 /* Like add_cmd but adds an element for a command prefix:
210 a name that should be followed by a subcommand to be looked up
211 in another command list. PREFIXLIST should be the address
212 of the variable containing that list. */
213
214 struct cmd_list_element *
215 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
216 allow_unknown, list)
217 char *name;
218 int class;
219 void (*fun) ();
220 char *doc;
221 struct cmd_list_element **prefixlist;
222 char *prefixname;
223 int allow_unknown;
224 struct cmd_list_element **list;
225 {
226 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
227 c->prefixlist = prefixlist;
228 c->prefixname = prefixname;
229 c->allow_unknown = allow_unknown;
230 return c;
231 }
232
233 /* Like add_prefix_cmd butsets the abbrev_flag on the new command. */
234
235 struct cmd_list_element *
236 add_abbrev_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
237 allow_unknown, list)
238 char *name;
239 int class;
240 void (*fun) ();
241 char *doc;
242 struct cmd_list_element **prefixlist;
243 char *prefixname;
244 int allow_unknown;
245 struct cmd_list_element **list;
246 {
247 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
248 c->prefixlist = prefixlist;
249 c->prefixname = prefixname;
250 c->allow_unknown = allow_unknown;
251 c->abbrev_flag = 1;
252 return c;
253 }
254
255 /* Remove the command named NAME from the command list. */
256
257 void
258 delete_cmd (name, list)
259 char *name;
260 struct cmd_list_element **list;
261 {
262 register struct cmd_list_element *c;
263
264 while (*list && !strcmp ((*list)->name, name))
265 {
266 *list = (*list)->next;
267 }
268
269 if (*list)
270 for (c = *list; c->next;)
271 {
272 if (!strcmp (c->next->name, name))
273 c->next = c->next->next;
274 else
275 c = c->next;
276 }
277 }
278
279 void help_cmd (), help_list (), help_cmd_list ();
280
281 /* This command really has to deal with two things:
282 * 1) I want documentation on *this string* (usually called by
283 * "help commandname").
284 * 2) I want documentation on *this list* (usually called by
285 * giving a command that requires subcommands. Also called by saying
286 * just "help".)
287 *
288 * I am going to split this into two seperate comamnds, help_cmd and
289 * help_list.
290 */
291
292 void
293 help_cmd (command, stream)
294 char *command;
295 FILE *stream;
296 {
297 struct cmd_list_element *c;
298 extern struct cmd_list_element *cmdlist;
299
300 if (!command)
301 {
302 help_list (cmdlist, "", -2, stream);
303 return;
304 }
305
306 c = lookup_cmd (&command, cmdlist, "", 0);
307
308 if (c == 0)
309 return;
310
311 /* There are three cases here.
312 If c->prefixlist is nonzer, we have a prefix command.
313 Print its documentation, then list its subcommands.
314
315 If c->function is nonzero, we really have a command.
316 Print its documentation and return.
317
318 If c->function is zero, we have a class name.
319 Print its documentation (as if it were a command)
320 and then set class to he number of this class
321 so that the commands in the class will be listed. */
322
323 fprintf (stream, "%s\n", c->doc);
324 if (c->prefixlist == 0 && c->function != 0)
325 return;
326 fputc ('\n', stream);
327
328 /* If this is a prefix command, print it's subcommands */
329 if (c->prefixlist)
330 help_list (*c->prefixlist, c->prefixname, -1, stream);
331
332 /* If this is a class name, print all of the commands in the class */
333 if (c->function == 0)
334 help_list (cmdlist, "", c->class, stream);
335 }
336
337 /*
338 * Get a specific kind of help on a command list.
339 *
340 * LIST is the list.
341 * CMDTYPE is the prefix to use in the title string.
342 * CLASS is the class with which to list the nodes of this list (see
343 * documentation for help_cmd_list below), As usual, -1 for
344 * everything, -2 for just classes, and non-negative for only things
345 * in a specific class.
346 * and STREAM is the output stream on which to print things.
347 * If you call this routine with a class >= 0, it recurses.
348 */
349 void
350 help_list (list, cmdtype, class, stream)
351 struct cmd_list_element *list;
352 char *cmdtype;
353 int class;
354 FILE *stream;
355 {
356 int len;
357 char *cmdtype1, *cmdtype2;
358
359 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
360 len = strlen (cmdtype);
361 cmdtype1 = (char *) alloca (len + 1);
362 cmdtype1[0] = 0;
363 cmdtype2 = (char *) alloca (len + 4);
364 cmdtype2[0] = 0;
365 if (len)
366 {
367 cmdtype1[0] = ' ';
368 strncpy (cmdtype1 + 1, cmdtype, len - 1);
369 cmdtype1[len] = 0;
370 strncpy (cmdtype2, cmdtype, len - 1);
371 strcpy (cmdtype2 + len - 1, " sub");
372 }
373
374 if (class == -2)
375 fprintf (stream, "List of classes of %scommands:\n\n", cmdtype2);
376 else
377 fprintf (stream, "List of %scommands:\n\n", cmdtype2);
378
379 help_cmd_list (list, class, cmdtype, (class >= 0), stream);
380
381 if (class == -2)
382 fprintf (stream, "\n\
383 Type \"help%s\" followed by a class name for a list of commands in that class.",
384 cmdtype1);
385
386 fprintf (stream, "\n\
387 Type \"help%s\" followed by %scommand name for full documentation.\n\
388 Command name abbreviations are allowed if unambiguous.\n",
389 cmdtype1, cmdtype2);
390 }
391
392
393 /*
394 * Implement a help command on command list LIST.
395 * RECURSE should be non-zero if this should be done recursively on
396 * all sublists of LIST.
397 * PREFIX is the prefix to print before each command name.
398 * STREAM is the stream upon which the output should be written.
399 * CLASS should be:
400 * A non-negative class number to list only commands in that
401 * class.
402 * -1 to list all commands in list.
403 * -2 to list all classes in list.
404 *
405 * Note that RECURSE will be active on *all* sublists, not just the
406 * ones seclected by the criteria above (ie. the selection mechanism
407 * is at the low level, not the high-level).
408 */
409 void
410 help_cmd_list (list, class, prefix, recurse, stream)
411 struct cmd_list_element *list;
412 int class;
413 char *prefix;
414 int recurse;
415 FILE *stream;
416 {
417 register struct cmd_list_element *c;
418 register char *p;
419
420 for (c = list; c; c = c->next)
421 {
422 if (c->abbrev_flag == 0 &&
423 (class == -1
424 || (class == -2 && c->function == 0)
425 || (class == c->class && c->function != 0)))
426 {
427 fprintf (stream, "%s%s -- ", prefix, c->name);
428 /* Print just the first line */
429 p = c->doc;
430 while (*p && *p != '\n') p++;
431 fwrite (c->doc, 1, p - c->doc, stream);
432 fputc('\n', stream);
433 }
434 if (recurse
435 && c->prefixlist != 0
436 && c->abbrev_flag == 0)
437 help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream);
438 }
439 }
440 \f
441 /* Look up the contents of *LINE as a command in the command list LIST.
442 LIST is a chain of struct cmd_list_element's.
443 If it is found, return the struct cmd_list_element for that command
444 and update *LINE to point after the command name, at the first argument.
445 If not found, call error if ALLOW_UNKNOWN is zero
446 otherwise (or if error returns) return zero.
447 Call error if specified command is ambiguous,
448 unless ALLOW_UNKNOWN is negative.
449 CMDTYPE precedes the word "command" in the error message. */
450
451 struct cmd_list_element *
452 lookup_cmd (line, list, cmdtype, allow_unknown)
453 char **line;
454 struct cmd_list_element *list;
455 char *cmdtype;
456 int allow_unknown;
457 {
458 register char *p;
459 register struct cmd_list_element *c, *found;
460 int nfound;
461 char ambbuf[100];
462 char *processed_cmd;
463 int i, cmd_len;
464
465 /* Skip leading whitespace. */
466
467 while (**line == ' ' || **line == '\t')
468 (*line)++;
469
470 /* Clear out trailing whitespace. */
471
472 p = *line + strlen (*line);
473 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
474 p--;
475 *p = 0;
476
477 /* Find end of command name. */
478
479 p = *line;
480 if (*p == '!')
481 p++;
482 else while (*p == '-'
483 || (*p >= 'a' && *p <= 'z')
484 || (*p >= 'A' && *p <= 'Z')
485 || (*p >= '0' && *p <= '9'))
486 p++;
487
488 /* Look up the command name.
489 If exact match, keep that.
490 Otherwise, take command abbreviated, if unique. Note that (in my
491 opinion) a null string does *not* indicate ambiguity; simply the
492 end of the argument. */
493
494 if (p == *line)
495 {
496 if (!allow_unknown)
497 error ("Lack of needed %scommand", cmdtype);
498 return 0;
499 }
500
501 /* Copy over to a local buffer, converting to lowercase on the way.
502 This is in case the command being parsed is a subcommand which
503 doesn't match anything, and that's ok. We want the original
504 untouched for the routine of the original command. */
505
506 processed_cmd = (char *) alloca (p - *line + 1);
507 for (cmd_len = 0; cmd_len < p - *line; cmd_len++)
508 {
509 char x = (*line)[cmd_len];
510 if (x >= 'A' && x <= 'Z')
511 processed_cmd[cmd_len] = x - 'A' + 'a';
512 else
513 processed_cmd[cmd_len] = x;
514 }
515 processed_cmd[cmd_len] = '\0';
516
517 /* Check all possibilities in the current command list. */
518 found = 0;
519 nfound = 0;
520 for (c = list; c; c = c->next)
521 {
522 if (!strncmp (processed_cmd, c->name, cmd_len))
523 {
524 found = c;
525 nfound++;
526 if (c->name[cmd_len] == 0)
527 {
528 nfound = 1;
529 break;
530 }
531 }
532 }
533
534 /* Report error for undefined command name. */
535
536 if (nfound != 1)
537 {
538 if (nfound > 1 && allow_unknown >= 0)
539 {
540 ambbuf[0] = 0;
541 for (c = list; c; c = c->next)
542 if (!strncmp (processed_cmd, c->name, cmd_len))
543 {
544 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
545 {
546 if (strlen (ambbuf))
547 strcat (ambbuf, ", ");
548 strcat (ambbuf, c->name);
549 }
550 else
551 {
552 strcat (ambbuf, "..");
553 break;
554 }
555 }
556 error ("Ambiguous %scommand \"%s\": %s.", cmdtype,
557 processed_cmd, ambbuf);
558 }
559 else if (!allow_unknown)
560 error ("Undefined %scommand: \"%s\".", cmdtype, processed_cmd);
561 return 0;
562 }
563
564 /* Skip whitespace before the argument. */
565
566 while (*p == ' ' || *p == '\t') p++;
567 *line = p;
568
569 if (found->prefixlist && *p)
570 {
571 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
572 found->allow_unknown);
573 if (c)
574 return c;
575 }
576
577 return found;
578 }
579
580 static void
581 shell_escape (arg, from_tty)
582 char *arg;
583 int from_tty;
584 {
585 int rc, status, pid;
586 char *p, *user_shell;
587 extern char *rindex ();
588
589 if ((user_shell = (char *) getenv ("SHELL")) == NULL)
590 user_shell = "/bin/sh";
591
592 /* Get the name of the shell for arg0 */
593 if ((p = rindex (user_shell, '/')) == NULL)
594 p = user_shell;
595 else
596 p++; /* Get past '/' */
597
598 if ((pid = fork()) == 0)
599 {
600 if (!arg)
601 execl (user_shell, p, 0);
602 else
603 execl (user_shell, p, "-c", arg, 0);
604
605 fprintf (stderr, "Exec of shell failed\n");
606 exit (0);
607 }
608
609 if (pid != -1)
610 while ((rc = wait (&status)) != pid && rc != -1)
611 ;
612 else
613 error ("Fork failed");
614 }
615
616 void
617 _initialize_command ()
618 {
619 add_com ("shell", class_support, shell_escape,
620 "Execute the rest of the line as a shell command. \n\
621 With no arguments, run an inferior shell.");
622
623 add_com_alias ("!", "shell", class_support, 1);
624 }