]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/command.c
gdb-2.8.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 <stdio.h>
108
109 #ifdef sparc
110 #include <alloca.h>
111 #endif
112
113 extern char *xmalloc ();
114
115 static char *savestring ();
116
117 /* Add element named NAME to command list *LIST.
118 FUN should be the function to execute the command;
119 it will get a character string as argument, with leading
120 and trailing blanks already eliminated.
121
122 DOC is a documentation string for the command.
123 Its first line should be a complete sentence.
124 It should start with ? for a command that is an abbreviation
125 or with * for a command that most users don't need to know about. */
126
127 struct cmd_list_element *
128 add_cmd (name, class, fun, doc, list)
129 char *name;
130 int class;
131 void (*fun) ();
132 char *doc;
133 struct cmd_list_element **list;
134 {
135 register struct cmd_list_element *c
136 = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element));
137
138 delete_cmd (name, list);
139 c->next = *list;
140 c->name = savestring (name, strlen (name));
141 c->class = class;
142 c->function = fun;
143 c->doc = doc;
144 c->prefixlist = 0;
145 c->allow_unknown = 0;
146 c->abbrev_flag = 0;
147 c->aux = 0;
148 *list = c;
149 return c;
150 }
151
152 struct cmd_list_element *
153 add_alias_cmd (name, oldname, class, abbrev_flag, list)
154 char *name;
155 char *oldname;
156 int class;
157 int abbrev_flag;
158 struct cmd_list_element **list;
159 {
160 /* Must do this since lookup_cmd tries to side-effect its first arg */
161 char *copied_name;
162 register struct cmd_list_element *old;
163 register struct cmd_list_element *c;
164 copied_name = (char *) alloca (strlen (oldname) + 1);
165 strcpy (copied_name, oldname);
166 old = lookup_cmd (&copied_name, *list, 0, 1);
167
168 if (old == 0)
169 {
170 delete_cmd (name, list);
171 return 0;
172 }
173
174 c = add_cmd (name, class, old->function, old->doc, list);
175 c->prefixlist = old->prefixlist;
176 c->prefixname = old->prefixname;
177 c->allow_unknown = old->allow_unknown;
178 c->abbrev_flag = abbrev_flag;
179 c->aux = old->aux;
180 return c;
181 }
182
183 /* Like add_prefix_cmd but adds an element for a command prefix:
184 a name that should be followed by a subcommand to be looked up
185 in another command list. PREFIXLIST should be the address
186 of the variable containing that list. */
187
188 struct cmd_list_element *
189 add_prefix_cmd (name, class, fun, doc, prefixlist, prefixname,
190 allow_unknown, list)
191 char *name;
192 int class;
193 void (*fun) ();
194 char *doc;
195 struct cmd_list_element **prefixlist;
196 char *prefixname;
197 int allow_unknown;
198 struct cmd_list_element **list;
199 {
200 register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list);
201 c->prefixlist = prefixlist;
202 c->prefixname = prefixname;
203 c->allow_unknown = allow_unknown;
204 return c;
205 }
206
207 /* Remove the command named NAME from the command list. */
208
209 void
210 delete_cmd (name, list)
211 char *name;
212 struct cmd_list_element **list;
213 {
214 register struct cmd_list_element *c;
215
216 while (*list && !strcmp ((*list)->name, name))
217 {
218 *list = (*list)->next;
219 }
220
221 if (*list)
222 for (c = *list; c->next;)
223 {
224 if (!strcmp (c->next->name, name))
225 c->next = c->next->next;
226 else
227 c = c->next;
228 }
229 }
230
231 /* Implement a help command on command list LIST.
232 COMMAND is the argument given (a command from the list to document)
233 or zero for no arg (describe briefly all the commands in the list).
234 CMDTYPE is a string to use in the error message if command COMMAND
235 is not found in the list. */
236
237 /* CLASS should be -1 to list all commands in LIST,
238 or a nonnegative class number value to list just commands in that class,
239 or -2 to list the classes themselves. */
240
241 void
242 help_cmd (command, list, cmdtype, class, stream)
243 char *command;
244 struct cmd_list_element *list;
245 char *cmdtype;
246 int class;
247 FILE *stream;
248 {
249 register struct cmd_list_element *c;
250 register char *p;
251 register int ncmds;
252 struct cmdvec { struct cmd_list_element *cmd; int class; };
253 register struct cmdvec *cmdvec;
254 char *cmdtype1, *cmdtype2;
255 int len;
256
257 if (command)
258 {
259 c = lookup_cmd (&command, list, cmdtype, 0);
260 if (c == 0)
261 return;
262
263 /* There are three cases here.
264 If c->prefixlist is nonzer, we have a prefix command.
265 Print its documentation, then list its subcommands.
266
267 If c->function is nonzero, we really have a command.
268 Print its documentation and return.
269
270 If c->function is zero, we have a class name.
271 Print its documentation (as if it were a command)
272 and then set class to he number of this class
273 so that the commands in the class will be listed. */
274
275 p = c->doc;
276 fprintf (stream, "%s\n", p);
277 if (c->function != 0 && c->prefixlist == 0)
278 return;
279 fputc ('\n', stream);
280 if (c->prefixlist)
281 {
282 list = *c->prefixlist;
283 class = 0;
284 cmdtype = c->prefixname;
285 }
286 else
287 class = c->class;
288 }
289
290 /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */
291 len = strlen (cmdtype);
292 cmdtype1 = (char *) alloca (len + 1);
293 cmdtype1[0] = 0;
294 cmdtype2 = (char *) alloca (len + 4);
295 cmdtype2[0] = 0;
296 if (len)
297 {
298 cmdtype1[0] = ' ';
299 strncpy (cmdtype1 + 1, cmdtype, len - 1);
300 cmdtype1[len] = 0;
301 strncpy (cmdtype2, cmdtype, len - 1);
302 strcpy (cmdtype2 + len - 1, " sub");
303 }
304
305 if (class == -2)
306 fprintf (stream, "List of classes of %scommands:\n\n", cmdtype2);
307 else
308 fprintf (stream, "List of %scommands:\n\n", cmdtype2);
309
310 for (c = list; c; c = c->next)
311 {
312 if (c->abbrev_flag == 0
313 && (class == -1 /* Listing all */
314 || (c->class == class && c->function != 0) /* Listing one class */
315 || (class == -2 && c->function == 0))) /* Listing the classes */
316 {
317 fprintf (stream, "%s -- ", c->name);
318 /* Print just first line of documentation. */
319 p = c->doc;
320 while (*p && *p != '\n') p++;
321 fwrite (c->doc, 1, p - c->doc, stream);
322 fputc ('\n', stream);
323 }
324 }
325
326 if (class == -2)
327 fprintf (stream, "\n\
328 Type \"help%s\" followed by a class name for a list of commands in that class.",
329 cmdtype1);
330
331 fprintf (stream, "\n\
332 Type \"help%s\" followed by %scommand name for full documentation.\n\
333 Command name abbreviations are allowed if unambiguous.\n",
334 cmdtype1, cmdtype2);
335 }
336 \f
337 /* Look up the contents of *LINE as a command in the command list LIST.
338 LIST is a chain of struct cmd_list_element's.
339 If it is found, return the struct cmd_list_element for that command
340 and update *LINE to point after the command name, at the first argument.
341 If not found, call error if ALLOW_UNKNOWN is zero
342 otherwise (or if error returns) return zero.
343 Call error if specified command is ambiguous,
344 unless ALLOW_UNKNOWN is negative.
345 CMDTYPE precedes the word "command" in the error message. */
346
347 struct cmd_list_element *
348 lookup_cmd (line, list, cmdtype, allow_unknown)
349 char **line;
350 struct cmd_list_element *list;
351 char *cmdtype;
352 int allow_unknown;
353 {
354 register char *p;
355 register struct cmd_list_element *c, *found;
356 int nfound;
357 char ambbuf[100];
358
359 /* Skip leading whitespace. */
360
361 while (**line == ' ' || **line == '\t')
362 (*line)++;
363
364 /* Clear out trailing whitespace. */
365
366 p = *line + strlen (*line);
367 while (p != *line && (p[-1] == ' ' || p[-1] == '\t'))
368 p--;
369 *p = 0;
370
371 /* Find end of command name. */
372
373 p = *line;
374 while (*p == '-'
375 || (*p >= 'a' && *p <= 'z')
376 || (*p >= 'A' && *p <= 'Z')
377 || (*p >= '0' && *p <= '9'))
378 {
379 if (*p >= 'A' && *p <= 'Z')
380 *p += 'a' - 'A';
381 p++;
382 }
383
384 /* Look up the command name.
385 If exact match, keep that.
386 Otherwise, take command abbreviated, if unique. */
387
388 found = 0;
389 nfound = 0;
390 for (c = list; c; c = c->next)
391 {
392 if (!strncmp (*line, c->name, p - *line))
393 {
394 found = c;
395 nfound++;
396 if (c->name[p - *line] == 0)
397 {
398 nfound = 1;
399 break;
400 }
401 }
402 }
403
404 /* Report error for undefined command name. */
405
406 if (nfound != 1)
407 {
408 if (nfound > 1 && allow_unknown >= 0)
409 {
410 *p = 0;
411 ambbuf[0] = 0;
412 for (c = list; c; c = c->next)
413 if (!strncmp (*line, c->name, p - *line))
414 {
415 if (strlen (ambbuf) + strlen (c->name) + 6 < sizeof ambbuf)
416 {
417 if (strlen (ambbuf))
418 strcat (ambbuf, ", ");
419 strcat (ambbuf, c->name);
420 }
421 else
422 {
423 strcat (ambbuf, "..");
424 break;
425 }
426 }
427 error ("Ambiguous %scommand \"%s\": %s.", cmdtype, *line, ambbuf);
428 }
429 else if (!allow_unknown)
430 {
431 *p = 0;
432 error ("Undefined %scommand: \"%s\".", cmdtype, *line);
433 }
434 return 0;
435 }
436
437 /* Skip whitespace before the argument. */
438
439 while (*p == ' ' || *p == '\t') p++;
440 *line = p;
441
442 if (found->prefixlist && *p)
443 {
444 c = lookup_cmd (line, *found->prefixlist, found->prefixname,
445 found->allow_unknown);
446 if (c)
447 return c;
448 }
449
450 return found;
451 }
452
453 /* Make a copy of the string at PTR with SIZE characters
454 (and add a null character at the end in the copy).
455 Uses malloc to get the space. Returns the address of the copy. */
456
457 static char *
458 savestring (ptr, size)
459 char *ptr;
460 int size;
461 {
462 register char *p = (char *) xmalloc (size + 1);
463 bcopy (ptr, p, size);
464 p[size] = 0;
465 return p;
466 }