Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Handle lists of commands, their decoding and documentation, for GDB. |
8926118c AC |
2 | |
3 | Copyright 1986, 1989, 1990, 1991, 1998, 2000, 2001, 2002 Free | |
4 | Software Foundation, Inc. | |
c906108c | 5 | |
c5aa993b JM |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
c906108c | 10 | |
c5aa993b JM |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
c906108c | 15 | |
c5aa993b JM |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
c906108c SS |
20 | |
21 | #include "defs.h" | |
c906108c | 22 | #include "symtab.h" |
c906108c | 23 | #include <ctype.h> |
f77b92bf | 24 | #include "gdb_regex.h" |
d318976c | 25 | |
8b93c638 | 26 | #include "ui-out.h" |
c906108c | 27 | |
d318976c FN |
28 | #include "cli/cli-cmds.h" |
29 | #include "cli/cli-decode.h" | |
53a5351d | 30 | |
c906108c SS |
31 | /* Prototypes for local functions */ |
32 | ||
a14ed312 | 33 | static void undef_cmd_error (char *, char *); |
c906108c | 34 | |
a14ed312 KB |
35 | static struct cmd_list_element *find_cmd (char *command, |
36 | int len, | |
37 | struct cmd_list_element *clist, | |
38 | int ignore_help_classes, | |
39 | int *nfound); | |
6837a0a2 | 40 | |
c85871a3 | 41 | static void help_all (struct ui_file *stream); |
d318976c | 42 | \f |
c906108c SS |
43 | /* Add element named NAME. |
44 | CLASS is the top level category into which commands are broken down | |
45 | for "help" purposes. | |
46 | FUN should be the function to execute the command; | |
47 | it will get a character string as argument, with leading | |
48 | and trailing blanks already eliminated. | |
49 | ||
50 | DOC is a documentation string for the command. | |
51 | Its first line should be a complete sentence. | |
52 | It should start with ? for a command that is an abbreviation | |
53 | or with * for a command that most users don't need to know about. | |
54 | ||
55 | Add this command to command list *LIST. | |
56 | ||
57 | Returns a pointer to the added command (not necessarily the head | |
58 | of *LIST). */ | |
59 | ||
60 | struct cmd_list_element * | |
af1c1752 KB |
61 | add_cmd (char *name, enum command_class class, void (*fun) (char *, int), |
62 | char *doc, struct cmd_list_element **list) | |
c906108c SS |
63 | { |
64 | register struct cmd_list_element *c | |
c5aa993b | 65 | = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element)); |
c906108c SS |
66 | struct cmd_list_element *p; |
67 | ||
68 | delete_cmd (name, list); | |
69 | ||
494b7ec9 | 70 | if (*list == NULL || strcmp ((*list)->name, name) >= 0) |
c906108c SS |
71 | { |
72 | c->next = *list; | |
73 | *list = c; | |
74 | } | |
75 | else | |
76 | { | |
77 | p = *list; | |
494b7ec9 | 78 | while (p->next && strcmp (p->next->name, name) <= 0) |
c5aa993b JM |
79 | { |
80 | p = p->next; | |
81 | } | |
c906108c SS |
82 | c->next = p->next; |
83 | p->next = c; | |
84 | } | |
85 | ||
86 | c->name = name; | |
87 | c->class = class; | |
88 | c->function.cfunc = fun; | |
89 | c->doc = doc; | |
56382845 FN |
90 | c->flags = 0; |
91 | c->replacement = NULL; | |
47724592 | 92 | c->pre_show_hook = NULL; |
73bc900d FN |
93 | c->hook_pre = NULL; |
94 | c->hook_post = NULL; | |
95 | c->hook_in = 0; | |
c906108c SS |
96 | c->prefixlist = NULL; |
97 | c->prefixname = NULL; | |
98 | c->allow_unknown = 0; | |
99 | c->abbrev_flag = 0; | |
100 | c->completer = make_symbol_completion_list; | |
101 | c->type = not_set_cmd; | |
102 | c->var = NULL; | |
103 | c->var_type = var_boolean; | |
104 | c->enums = NULL; | |
105 | c->user_commands = NULL; | |
73bc900d FN |
106 | c->hookee_pre = NULL; |
107 | c->hookee_post = NULL; | |
c906108c SS |
108 | c->cmd_pointer = NULL; |
109 | ||
110 | return c; | |
111 | } | |
112 | ||
69da3468 FN |
113 | /* Same as above, except that the abbrev_flag is set. */ |
114 | /* Note: Doesn't seem to be used anywhere currently. */ | |
115 | ||
116 | struct cmd_list_element * | |
117 | add_abbrev_cmd (char *name, enum command_class class, void (*fun) (char *, int), | |
118 | char *doc, struct cmd_list_element **list) | |
119 | { | |
120 | register struct cmd_list_element *c | |
121 | = add_cmd (name, class, fun, doc, list); | |
122 | ||
123 | c->abbrev_flag = 1; | |
124 | return c; | |
125 | } | |
56382845 FN |
126 | |
127 | /* Deprecates a command CMD. | |
128 | REPLACEMENT is the name of the command which should be used in place | |
129 | of this command, or NULL if no such command exists. | |
130 | ||
131 | This function does not check to see if command REPLACEMENT exists | |
132 | since gdb may not have gotten around to adding REPLACEMENT when this | |
133 | function is called. | |
134 | ||
135 | Returns a pointer to the deprecated command. */ | |
136 | ||
137 | struct cmd_list_element * | |
fba45db2 | 138 | deprecate_cmd (struct cmd_list_element *cmd, char *replacement) |
56382845 FN |
139 | { |
140 | cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER); | |
141 | ||
142 | if (replacement != NULL) | |
143 | cmd->replacement = replacement; | |
144 | else | |
145 | cmd->replacement = NULL; | |
146 | ||
147 | return cmd; | |
148 | } | |
149 | ||
c906108c | 150 | struct cmd_list_element * |
fba45db2 KB |
151 | add_alias_cmd (char *name, char *oldname, enum command_class class, |
152 | int abbrev_flag, struct cmd_list_element **list) | |
c906108c SS |
153 | { |
154 | /* Must do this since lookup_cmd tries to side-effect its first arg */ | |
155 | char *copied_name; | |
156 | register struct cmd_list_element *old; | |
157 | register struct cmd_list_element *c; | |
158 | copied_name = (char *) alloca (strlen (oldname) + 1); | |
159 | strcpy (copied_name, oldname); | |
c5aa993b | 160 | old = lookup_cmd (&copied_name, *list, "", 1, 1); |
c906108c SS |
161 | |
162 | if (old == 0) | |
163 | { | |
164 | delete_cmd (name, list); | |
165 | return 0; | |
166 | } | |
167 | ||
168 | c = add_cmd (name, class, old->function.cfunc, old->doc, list); | |
169 | c->prefixlist = old->prefixlist; | |
170 | c->prefixname = old->prefixname; | |
171 | c->allow_unknown = old->allow_unknown; | |
172 | c->abbrev_flag = abbrev_flag; | |
173 | c->cmd_pointer = old; | |
174 | return c; | |
175 | } | |
176 | ||
177 | /* Like add_cmd but adds an element for a command prefix: | |
178 | a name that should be followed by a subcommand to be looked up | |
179 | in another command list. PREFIXLIST should be the address | |
180 | of the variable containing that list. */ | |
181 | ||
182 | struct cmd_list_element * | |
af1c1752 KB |
183 | add_prefix_cmd (char *name, enum command_class class, void (*fun) (char *, int), |
184 | char *doc, struct cmd_list_element **prefixlist, | |
185 | char *prefixname, int allow_unknown, | |
186 | struct cmd_list_element **list) | |
c906108c SS |
187 | { |
188 | register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list); | |
189 | c->prefixlist = prefixlist; | |
190 | c->prefixname = prefixname; | |
191 | c->allow_unknown = allow_unknown; | |
192 | return c; | |
193 | } | |
194 | ||
195 | /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */ | |
c5aa993b | 196 | |
c906108c | 197 | struct cmd_list_element * |
af1c1752 KB |
198 | add_abbrev_prefix_cmd (char *name, enum command_class class, |
199 | void (*fun) (char *, int), char *doc, | |
200 | struct cmd_list_element **prefixlist, char *prefixname, | |
201 | int allow_unknown, struct cmd_list_element **list) | |
c906108c SS |
202 | { |
203 | register struct cmd_list_element *c = add_cmd (name, class, fun, doc, list); | |
204 | c->prefixlist = prefixlist; | |
205 | c->prefixname = prefixname; | |
206 | c->allow_unknown = allow_unknown; | |
207 | c->abbrev_flag = 1; | |
208 | return c; | |
209 | } | |
210 | ||
211 | /* This is an empty "cfunc". */ | |
212 | void | |
fba45db2 | 213 | not_just_help_class_command (char *args, int from_tty) |
c906108c SS |
214 | { |
215 | } | |
216 | ||
217 | /* This is an empty "sfunc". */ | |
a14ed312 | 218 | static void empty_sfunc (char *, int, struct cmd_list_element *); |
c906108c SS |
219 | |
220 | static void | |
fba45db2 | 221 | empty_sfunc (char *args, int from_tty, struct cmd_list_element *c) |
c906108c SS |
222 | { |
223 | } | |
224 | ||
225 | /* Add element named NAME to command list LIST (the list for set | |
226 | or some sublist thereof). | |
227 | CLASS is as in add_cmd. | |
228 | VAR_TYPE is the kind of thing we are setting. | |
229 | VAR is address of the variable being controlled by this command. | |
230 | DOC is the documentation string. */ | |
231 | ||
232 | struct cmd_list_element * | |
1ed2a135 AC |
233 | add_set_cmd (char *name, |
234 | enum command_class class, | |
235 | var_types var_type, | |
236 | void *var, | |
237 | char *doc, | |
238 | struct cmd_list_element **list) | |
c906108c SS |
239 | { |
240 | struct cmd_list_element *c | |
c5aa993b | 241 | = add_cmd (name, class, NO_FUNCTION, doc, list); |
c906108c SS |
242 | |
243 | c->type = set_cmd; | |
244 | c->var_type = var_type; | |
245 | c->var = var; | |
246 | /* This needs to be something besides NO_FUNCTION so that this isn't | |
247 | treated as a help class. */ | |
248 | c->function.sfunc = empty_sfunc; | |
249 | return c; | |
250 | } | |
251 | ||
252 | /* Add element named NAME to command list LIST (the list for set | |
253 | or some sublist thereof). | |
254 | CLASS is as in add_cmd. | |
255 | ENUMLIST is a list of strings which may follow NAME. | |
256 | VAR is address of the variable which will contain the matching string | |
c5aa993b | 257 | (from ENUMLIST). |
c906108c SS |
258 | DOC is the documentation string. */ |
259 | ||
260 | struct cmd_list_element * | |
1ed2a135 AC |
261 | add_set_enum_cmd (char *name, |
262 | enum command_class class, | |
53904c9e AC |
263 | const char *enumlist[], |
264 | const char **var, | |
1ed2a135 AC |
265 | char *doc, |
266 | struct cmd_list_element **list) | |
c906108c SS |
267 | { |
268 | struct cmd_list_element *c | |
c5aa993b | 269 | = add_set_cmd (name, class, var_enum, var, doc, list); |
c906108c SS |
270 | c->enums = enumlist; |
271 | ||
272 | return c; | |
273 | } | |
274 | ||
97c3646f AC |
275 | /* Add element named NAME to command list LIST (the list for set |
276 | or some sublist thereof). | |
277 | CLASS is as in add_cmd. | |
278 | VAR is address of the variable which will contain the value. | |
279 | DOC is the documentation string. */ | |
280 | struct cmd_list_element * | |
281 | add_set_auto_boolean_cmd (char *name, | |
282 | enum command_class class, | |
283 | enum cmd_auto_boolean *var, | |
284 | char *doc, | |
285 | struct cmd_list_element **list) | |
286 | { | |
287 | static const char *auto_boolean_enums[] = { "on", "off", "auto", NULL }; | |
288 | struct cmd_list_element *c; | |
289 | c = add_set_cmd (name, class, var_auto_boolean, var, doc, list); | |
290 | c->enums = auto_boolean_enums; | |
291 | return c; | |
292 | } | |
293 | ||
f3796e26 AC |
294 | /* Add element named NAME to command list LIST (the list for set |
295 | or some sublist thereof). | |
296 | CLASS is as in add_cmd. | |
297 | VAR is address of the variable which will contain the value. | |
298 | DOC is the documentation string. */ | |
299 | struct cmd_list_element * | |
300 | add_set_boolean_cmd (char *name, | |
301 | enum command_class class, | |
302 | int *var, | |
303 | char *doc, | |
304 | struct cmd_list_element **list) | |
305 | { | |
306 | static const char *boolean_enums[] = { "on", "off", NULL }; | |
307 | struct cmd_list_element *c; | |
308 | c = add_set_cmd (name, class, var_boolean, var, doc, list); | |
309 | c->enums = boolean_enums; | |
310 | return c; | |
311 | } | |
312 | ||
c906108c SS |
313 | /* Where SETCMD has already been added, add the corresponding show |
314 | command to LIST and return a pointer to the added command (not | |
315 | necessarily the head of LIST). */ | |
316 | struct cmd_list_element * | |
fba45db2 KB |
317 | add_show_from_set (struct cmd_list_element *setcmd, |
318 | struct cmd_list_element **list) | |
c906108c SS |
319 | { |
320 | struct cmd_list_element *showcmd = | |
c5aa993b | 321 | (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element)); |
c906108c SS |
322 | struct cmd_list_element *p; |
323 | ||
324 | memcpy (showcmd, setcmd, sizeof (struct cmd_list_element)); | |
325 | delete_cmd (showcmd->name, list); | |
326 | showcmd->type = show_cmd; | |
c5aa993b | 327 | |
c906108c SS |
328 | /* Replace "set " at start of docstring with "show ". */ |
329 | if (setcmd->doc[0] == 'S' && setcmd->doc[1] == 'e' | |
330 | && setcmd->doc[2] == 't' && setcmd->doc[3] == ' ') | |
331 | showcmd->doc = concat ("Show ", setcmd->doc + 4, NULL); | |
332 | else | |
333 | fprintf_unfiltered (gdb_stderr, "GDB internal error: Bad docstring for set command\n"); | |
c5aa993b | 334 | |
494b7ec9 | 335 | if (*list == NULL || strcmp ((*list)->name, showcmd->name) >= 0) |
c906108c SS |
336 | { |
337 | showcmd->next = *list; | |
338 | *list = showcmd; | |
339 | } | |
340 | else | |
341 | { | |
342 | p = *list; | |
494b7ec9 | 343 | while (p->next && strcmp (p->next->name, showcmd->name) <= 0) |
c5aa993b JM |
344 | { |
345 | p = p->next; | |
346 | } | |
c906108c SS |
347 | showcmd->next = p->next; |
348 | p->next = showcmd; | |
349 | } | |
350 | ||
351 | return showcmd; | |
352 | } | |
353 | ||
354 | /* Remove the command named NAME from the command list. */ | |
355 | ||
356 | void | |
fba45db2 | 357 | delete_cmd (char *name, struct cmd_list_element **list) |
c906108c SS |
358 | { |
359 | register struct cmd_list_element *c; | |
360 | struct cmd_list_element *p; | |
361 | ||
362 | while (*list && STREQ ((*list)->name, name)) | |
363 | { | |
73bc900d FN |
364 | if ((*list)->hookee_pre) |
365 | (*list)->hookee_pre->hook_pre = 0; /* Hook slips out of its mouth */ | |
366 | if ((*list)->hookee_post) | |
367 | (*list)->hookee_post->hook_post = 0; /* Hook slips out of its bottom */ | |
c906108c | 368 | p = (*list)->next; |
b8c9b27d | 369 | xfree (* list); |
c906108c SS |
370 | *list = p; |
371 | } | |
372 | ||
373 | if (*list) | |
374 | for (c = *list; c->next;) | |
375 | { | |
376 | if (STREQ (c->next->name, name)) | |
377 | { | |
73bc900d FN |
378 | if (c->next->hookee_pre) |
379 | c->next->hookee_pre->hook_pre = 0; /* hooked cmd gets away. */ | |
380 | if (c->next->hookee_post) | |
381 | c->next->hookee_post->hook_post = 0; /* remove post hook */ | |
382 | /* :( no fishing metaphore */ | |
c906108c | 383 | p = c->next->next; |
b8c9b27d | 384 | xfree (c->next); |
c906108c SS |
385 | c->next = p; |
386 | } | |
387 | else | |
388 | c = c->next; | |
389 | } | |
390 | } | |
d318976c FN |
391 | \f |
392 | /* Shorthands to the commands above. */ | |
393 | ||
394 | /* Add an element to the list of info subcommands. */ | |
395 | ||
396 | struct cmd_list_element * | |
397 | add_info (char *name, void (*fun) (char *, int), char *doc) | |
398 | { | |
399 | return add_cmd (name, no_class, fun, doc, &infolist); | |
400 | } | |
401 | ||
402 | /* Add an alias to the list of info subcommands. */ | |
403 | ||
404 | struct cmd_list_element * | |
405 | add_info_alias (char *name, char *oldname, int abbrev_flag) | |
406 | { | |
407 | return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist); | |
408 | } | |
409 | ||
410 | /* Add an element to the list of commands. */ | |
411 | ||
412 | struct cmd_list_element * | |
413 | add_com (char *name, enum command_class class, void (*fun) (char *, int), | |
414 | char *doc) | |
415 | { | |
416 | return add_cmd (name, class, fun, doc, &cmdlist); | |
417 | } | |
418 | ||
419 | /* Add an alias or abbreviation command to the list of commands. */ | |
420 | ||
421 | struct cmd_list_element * | |
422 | add_com_alias (char *name, char *oldname, enum command_class class, | |
423 | int abbrev_flag) | |
424 | { | |
425 | return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist); | |
426 | } | |
427 | \f | |
6837a0a2 DB |
428 | /* Recursively walk the commandlist structures, and print out the |
429 | documentation of commands that match our regex in either their | |
430 | name, or their documentation. | |
431 | */ | |
d318976c FN |
432 | void |
433 | apropos_cmd (struct ui_file *stream, struct cmd_list_element *commandlist, | |
6837a0a2 DB |
434 | struct re_pattern_buffer *regex, char *prefix) |
435 | { | |
436 | register struct cmd_list_element *c; | |
437 | int returnvalue=1; /*Needed to avoid double printing*/ | |
438 | /* Walk through the commands */ | |
439 | for (c=commandlist;c;c=c->next) | |
440 | { | |
441 | if (c->name != NULL) | |
442 | { | |
443 | /* Try to match against the name*/ | |
444 | returnvalue=re_search(regex,c->name,strlen(c->name),0,strlen(c->name),NULL); | |
445 | if (returnvalue >= 0) | |
446 | { | |
447 | /* Stolen from help_cmd_list. We don't directly use | |
448 | * help_cmd_list because it doesn't let us print out | |
449 | * single commands | |
450 | */ | |
451 | fprintf_filtered (stream, "%s%s -- ", prefix, c->name); | |
452 | print_doc_line (stream, c->doc); | |
453 | fputs_filtered ("\n", stream); | |
454 | returnvalue=0; /*Set this so we don't print it again.*/ | |
455 | } | |
456 | } | |
457 | if (c->doc != NULL && returnvalue != 0) | |
458 | { | |
459 | /* Try to match against documentation */ | |
460 | if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0) | |
461 | { | |
462 | /* Stolen from help_cmd_list. We don't directly use | |
463 | * help_cmd_list because it doesn't let us print out | |
464 | * single commands | |
465 | */ | |
466 | fprintf_filtered (stream, "%s%s -- ", prefix, c->name); | |
467 | print_doc_line (stream, c->doc); | |
468 | fputs_filtered ("\n", stream); | |
469 | } | |
470 | } | |
471 | /* Check if this command has subcommands */ | |
472 | if (c->prefixlist != NULL) | |
473 | { | |
474 | /* Recursively call ourselves on the subcommand list, | |
475 | passing the right prefix in. | |
476 | */ | |
d318976c | 477 | apropos_cmd (stream,*c->prefixlist,regex,c->prefixname); |
6837a0a2 DB |
478 | } |
479 | } | |
480 | } | |
c906108c SS |
481 | |
482 | /* This command really has to deal with two things: | |
483 | * 1) I want documentation on *this string* (usually called by | |
484 | * "help commandname"). | |
485 | * 2) I want documentation on *this list* (usually called by | |
486 | * giving a command that requires subcommands. Also called by saying | |
487 | * just "help".) | |
488 | * | |
489 | * I am going to split this into two seperate comamnds, help_cmd and | |
490 | * help_list. | |
491 | */ | |
492 | ||
493 | void | |
fba45db2 | 494 | help_cmd (char *command, struct ui_file *stream) |
c906108c SS |
495 | { |
496 | struct cmd_list_element *c; | |
497 | extern struct cmd_list_element *cmdlist; | |
498 | ||
499 | if (!command) | |
500 | { | |
501 | help_list (cmdlist, "", all_classes, stream); | |
502 | return; | |
503 | } | |
504 | ||
49a5a3a3 GM |
505 | if (strcmp (command, "all") == 0) |
506 | { | |
507 | help_all (stream); | |
508 | return; | |
509 | } | |
510 | ||
c906108c SS |
511 | c = lookup_cmd (&command, cmdlist, "", 0, 0); |
512 | ||
513 | if (c == 0) | |
514 | return; | |
515 | ||
516 | /* There are three cases here. | |
517 | If c->prefixlist is nonzero, we have a prefix command. | |
518 | Print its documentation, then list its subcommands. | |
c5aa993b | 519 | |
c906108c SS |
520 | If c->function is nonzero, we really have a command. |
521 | Print its documentation and return. | |
c5aa993b | 522 | |
c906108c SS |
523 | If c->function is zero, we have a class name. |
524 | Print its documentation (as if it were a command) | |
525 | and then set class to the number of this class | |
526 | so that the commands in the class will be listed. */ | |
527 | ||
528 | fputs_filtered (c->doc, stream); | |
529 | fputs_filtered ("\n", stream); | |
530 | ||
531 | if (c->prefixlist == 0 && c->function.cfunc != NULL) | |
532 | return; | |
533 | fprintf_filtered (stream, "\n"); | |
534 | ||
535 | /* If this is a prefix command, print it's subcommands */ | |
536 | if (c->prefixlist) | |
537 | help_list (*c->prefixlist, c->prefixname, all_commands, stream); | |
538 | ||
539 | /* If this is a class name, print all of the commands in the class */ | |
540 | if (c->function.cfunc == NULL) | |
541 | help_list (cmdlist, "", c->class, stream); | |
542 | ||
73bc900d FN |
543 | if (c->hook_pre || c->hook_post) |
544 | fprintf_filtered (stream, | |
545 | "\nThis command has a hook (or hooks) defined:\n"); | |
546 | ||
547 | if (c->hook_pre) | |
548 | fprintf_filtered (stream, | |
549 | "\tThis command is run after : %s (pre hook)\n", | |
550 | c->hook_pre->name); | |
551 | if (c->hook_post) | |
552 | fprintf_filtered (stream, | |
553 | "\tThis command is run before : %s (post hook)\n", | |
554 | c->hook_post->name); | |
c906108c SS |
555 | } |
556 | ||
557 | /* | |
558 | * Get a specific kind of help on a command list. | |
559 | * | |
560 | * LIST is the list. | |
561 | * CMDTYPE is the prefix to use in the title string. | |
562 | * CLASS is the class with which to list the nodes of this list (see | |
563 | * documentation for help_cmd_list below), As usual, ALL_COMMANDS for | |
564 | * everything, ALL_CLASSES for just classes, and non-negative for only things | |
565 | * in a specific class. | |
566 | * and STREAM is the output stream on which to print things. | |
567 | * If you call this routine with a class >= 0, it recurses. | |
568 | */ | |
569 | void | |
fba45db2 KB |
570 | help_list (struct cmd_list_element *list, char *cmdtype, |
571 | enum command_class class, struct ui_file *stream) | |
c906108c SS |
572 | { |
573 | int len; | |
574 | char *cmdtype1, *cmdtype2; | |
c5aa993b | 575 | |
c906108c SS |
576 | /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub" */ |
577 | len = strlen (cmdtype); | |
578 | cmdtype1 = (char *) alloca (len + 1); | |
579 | cmdtype1[0] = 0; | |
580 | cmdtype2 = (char *) alloca (len + 4); | |
581 | cmdtype2[0] = 0; | |
582 | if (len) | |
583 | { | |
584 | cmdtype1[0] = ' '; | |
585 | strncpy (cmdtype1 + 1, cmdtype, len - 1); | |
586 | cmdtype1[len] = 0; | |
587 | strncpy (cmdtype2, cmdtype, len - 1); | |
588 | strcpy (cmdtype2 + len - 1, " sub"); | |
589 | } | |
590 | ||
591 | if (class == all_classes) | |
592 | fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2); | |
593 | else | |
594 | fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2); | |
595 | ||
c5aa993b | 596 | help_cmd_list (list, class, cmdtype, (int) class >= 0, stream); |
c906108c SS |
597 | |
598 | if (class == all_classes) | |
599 | fprintf_filtered (stream, "\n\ | |
600 | Type \"help%s\" followed by a class name for a list of commands in that class.", | |
c5aa993b | 601 | cmdtype1); |
c906108c SS |
602 | |
603 | fprintf_filtered (stream, "\n\ | |
604 | Type \"help%s\" followed by %scommand name for full documentation.\n\ | |
605 | Command name abbreviations are allowed if unambiguous.\n", | |
c5aa993b | 606 | cmdtype1, cmdtype2); |
c906108c | 607 | } |
c5aa993b | 608 | |
49a5a3a3 | 609 | static void |
c85871a3 | 610 | help_all (struct ui_file *stream) |
49a5a3a3 GM |
611 | { |
612 | struct cmd_list_element *c; | |
613 | extern struct cmd_list_element *cmdlist; | |
614 | ||
615 | for (c = cmdlist; c; c = c->next) | |
616 | { | |
617 | if (c->abbrev_flag) | |
618 | continue; | |
619 | /* If this is a prefix command, print it's subcommands */ | |
620 | if (c->prefixlist) | |
621 | help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 0, stream); | |
622 | ||
623 | /* If this is a class name, print all of the commands in the class */ | |
624 | else if (c->function.cfunc == NULL) | |
625 | help_cmd_list (cmdlist, c->class, "", 0, stream); | |
626 | } | |
627 | } | |
628 | ||
c906108c | 629 | /* Print only the first line of STR on STREAM. */ |
d318976c | 630 | void |
fba45db2 | 631 | print_doc_line (struct ui_file *stream, char *str) |
c906108c SS |
632 | { |
633 | static char *line_buffer = 0; | |
634 | static int line_size; | |
635 | register char *p; | |
636 | ||
637 | if (!line_buffer) | |
638 | { | |
639 | line_size = 80; | |
640 | line_buffer = (char *) xmalloc (line_size); | |
641 | } | |
642 | ||
643 | p = str; | |
644 | while (*p && *p != '\n' && *p != '.' && *p != ',') | |
645 | p++; | |
646 | if (p - str > line_size - 1) | |
647 | { | |
648 | line_size = p - str + 1; | |
b8c9b27d | 649 | xfree (line_buffer); |
c906108c SS |
650 | line_buffer = (char *) xmalloc (line_size); |
651 | } | |
652 | strncpy (line_buffer, str, p - str); | |
653 | line_buffer[p - str] = '\0'; | |
654 | if (islower (line_buffer[0])) | |
655 | line_buffer[0] = toupper (line_buffer[0]); | |
8b93c638 | 656 | ui_out_text (uiout, line_buffer); |
c906108c SS |
657 | } |
658 | ||
659 | /* | |
660 | * Implement a help command on command list LIST. | |
661 | * RECURSE should be non-zero if this should be done recursively on | |
662 | * all sublists of LIST. | |
663 | * PREFIX is the prefix to print before each command name. | |
664 | * STREAM is the stream upon which the output should be written. | |
665 | * CLASS should be: | |
c5aa993b | 666 | * A non-negative class number to list only commands in that |
c906108c | 667 | * class. |
c5aa993b JM |
668 | * ALL_COMMANDS to list all commands in list. |
669 | * ALL_CLASSES to list all classes in list. | |
c906108c SS |
670 | * |
671 | * Note that RECURSE will be active on *all* sublists, not just the | |
672 | * ones selected by the criteria above (ie. the selection mechanism | |
673 | * is at the low level, not the high-level). | |
674 | */ | |
675 | void | |
fba45db2 KB |
676 | help_cmd_list (struct cmd_list_element *list, enum command_class class, |
677 | char *prefix, int recurse, struct ui_file *stream) | |
c906108c SS |
678 | { |
679 | register struct cmd_list_element *c; | |
680 | ||
681 | for (c = list; c; c = c->next) | |
682 | { | |
683 | if (c->abbrev_flag == 0 && | |
684 | (class == all_commands | |
c5aa993b JM |
685 | || (class == all_classes && c->function.cfunc == NULL) |
686 | || (class == c->class && c->function.cfunc != NULL))) | |
c906108c SS |
687 | { |
688 | fprintf_filtered (stream, "%s%s -- ", prefix, c->name); | |
689 | print_doc_line (stream, c->doc); | |
690 | fputs_filtered ("\n", stream); | |
691 | } | |
692 | if (recurse | |
693 | && c->prefixlist != 0 | |
694 | && c->abbrev_flag == 0) | |
695 | help_cmd_list (*c->prefixlist, class, c->prefixname, 1, stream); | |
696 | } | |
697 | } | |
c906108c | 698 | \f |
c5aa993b | 699 | |
c906108c SS |
700 | /* Search the input clist for 'command'. Return the command if |
701 | found (or NULL if not), and return the number of commands | |
702 | found in nfound */ | |
703 | ||
704 | static struct cmd_list_element * | |
fba45db2 KB |
705 | find_cmd (char *command, int len, struct cmd_list_element *clist, |
706 | int ignore_help_classes, int *nfound) | |
c906108c SS |
707 | { |
708 | struct cmd_list_element *found, *c; | |
709 | ||
c5aa993b | 710 | found = (struct cmd_list_element *) NULL; |
c906108c SS |
711 | *nfound = 0; |
712 | for (c = clist; c; c = c->next) | |
713 | if (!strncmp (command, c->name, len) | |
c5aa993b | 714 | && (!ignore_help_classes || c->function.cfunc)) |
c906108c | 715 | { |
c5aa993b JM |
716 | found = c; |
717 | (*nfound)++; | |
718 | if (c->name[len] == '\0') | |
719 | { | |
720 | *nfound = 1; | |
721 | break; | |
722 | } | |
c906108c SS |
723 | } |
724 | return found; | |
725 | } | |
726 | ||
727 | /* This routine takes a line of TEXT and a CLIST in which to start the | |
728 | lookup. When it returns it will have incremented the text pointer past | |
729 | the section of text it matched, set *RESULT_LIST to point to the list in | |
730 | which the last word was matched, and will return a pointer to the cmd | |
731 | list element which the text matches. It will return NULL if no match at | |
732 | all was possible. It will return -1 (cast appropriately, ick) if ambigous | |
733 | matches are possible; in this case *RESULT_LIST will be set to point to | |
734 | the list in which there are ambiguous choices (and *TEXT will be set to | |
735 | the ambiguous text string). | |
736 | ||
737 | If the located command was an abbreviation, this routine returns the base | |
738 | command of the abbreviation. | |
739 | ||
740 | It does no error reporting whatsoever; control will always return | |
741 | to the superior routine. | |
742 | ||
743 | In the case of an ambiguous return (-1), *RESULT_LIST will be set to point | |
744 | at the prefix_command (ie. the best match) *or* (special case) will be NULL | |
745 | if no prefix command was ever found. For example, in the case of "info a", | |
746 | "info" matches without ambiguity, but "a" could be "args" or "address", so | |
747 | *RESULT_LIST is set to the cmd_list_element for "info". So in this case | |
748 | RESULT_LIST should not be interpeted as a pointer to the beginning of a | |
749 | list; it simply points to a specific command. In the case of an ambiguous | |
750 | return *TEXT is advanced past the last non-ambiguous prefix (e.g. | |
751 | "info t" can be "info types" or "info target"; upon return *TEXT has been | |
752 | advanced past "info "). | |
753 | ||
754 | If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise | |
755 | affect the operation). | |
756 | ||
757 | This routine does *not* modify the text pointed to by TEXT. | |
c5aa993b | 758 | |
c906108c SS |
759 | If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which |
760 | are actually help classes rather than commands (i.e. the function field of | |
761 | the struct cmd_list_element is NULL). */ | |
762 | ||
763 | struct cmd_list_element * | |
fba45db2 KB |
764 | lookup_cmd_1 (char **text, struct cmd_list_element *clist, |
765 | struct cmd_list_element **result_list, int ignore_help_classes) | |
c906108c SS |
766 | { |
767 | char *p, *command; | |
768 | int len, tmp, nfound; | |
769 | struct cmd_list_element *found, *c; | |
56382845 | 770 | char *line = *text; |
c906108c SS |
771 | |
772 | while (**text == ' ' || **text == '\t') | |
773 | (*text)++; | |
774 | ||
775 | /* Treating underscores as part of command words is important | |
776 | so that "set args_foo()" doesn't get interpreted as | |
777 | "set args _foo()". */ | |
778 | for (p = *text; | |
c5aa993b | 779 | *p && (isalnum (*p) || *p == '-' || *p == '_' || |
c906108c SS |
780 | (tui_version && |
781 | (*p == '+' || *p == '<' || *p == '>' || *p == '$')) || | |
782 | (xdb_commands && (*p == '!' || *p == '/' || *p == '?'))); | |
783 | p++) | |
784 | ; | |
785 | ||
786 | /* If nothing but whitespace, return 0. */ | |
787 | if (p == *text) | |
788 | return 0; | |
c5aa993b | 789 | |
c906108c SS |
790 | len = p - *text; |
791 | ||
792 | /* *text and p now bracket the first command word to lookup (and | |
793 | it's length is len). We copy this into a local temporary */ | |
794 | ||
795 | ||
796 | command = (char *) alloca (len + 1); | |
797 | for (tmp = 0; tmp < len; tmp++) | |
798 | { | |
799 | char x = (*text)[tmp]; | |
800 | command[tmp] = x; | |
801 | } | |
802 | command[len] = '\0'; | |
803 | ||
804 | /* Look it up. */ | |
805 | found = 0; | |
806 | nfound = 0; | |
c5aa993b | 807 | found = find_cmd (command, len, clist, ignore_help_classes, &nfound); |
c906108c SS |
808 | |
809 | /* | |
c5aa993b JM |
810 | ** We didn't find the command in the entered case, so lower case it |
811 | ** and search again. | |
812 | */ | |
c906108c SS |
813 | if (!found || nfound == 0) |
814 | { | |
815 | for (tmp = 0; tmp < len; tmp++) | |
c5aa993b JM |
816 | { |
817 | char x = command[tmp]; | |
818 | command[tmp] = isupper (x) ? tolower (x) : x; | |
819 | } | |
820 | found = find_cmd (command, len, clist, ignore_help_classes, &nfound); | |
c906108c SS |
821 | } |
822 | ||
823 | /* If nothing matches, we have a simple failure. */ | |
824 | if (nfound == 0) | |
825 | return 0; | |
826 | ||
827 | if (nfound > 1) | |
828 | { | |
829 | if (result_list != NULL) | |
830 | /* Will be modified in calling routine | |
831 | if we know what the prefix command is. */ | |
c5aa993b JM |
832 | *result_list = 0; |
833 | return (struct cmd_list_element *) -1; /* Ambiguous. */ | |
c906108c SS |
834 | } |
835 | ||
836 | /* We've matched something on this list. Move text pointer forward. */ | |
837 | ||
838 | *text = p; | |
839 | ||
c906108c | 840 | if (found->cmd_pointer) |
56382845 FN |
841 | { |
842 | /* We drop the alias (abbreviation) in favor of the command it is | |
843 | pointing to. If the alias is deprecated, though, we need to | |
844 | warn the user about it before we drop it. Note that while we | |
845 | are warning about the alias, we may also warn about the command | |
846 | itself and we will adjust the appropriate DEPRECATED_WARN_USER | |
847 | flags */ | |
848 | ||
849 | if (found->flags & DEPRECATED_WARN_USER) | |
850 | deprecated_cmd_warning (&line); | |
851 | found = found->cmd_pointer; | |
852 | } | |
c906108c SS |
853 | /* If we found a prefix command, keep looking. */ |
854 | ||
855 | if (found->prefixlist) | |
856 | { | |
857 | c = lookup_cmd_1 (text, *found->prefixlist, result_list, | |
858 | ignore_help_classes); | |
859 | if (!c) | |
860 | { | |
861 | /* Didn't find anything; this is as far as we got. */ | |
862 | if (result_list != NULL) | |
863 | *result_list = clist; | |
864 | return found; | |
865 | } | |
866 | else if (c == (struct cmd_list_element *) -1) | |
867 | { | |
868 | /* We've gotten this far properly, but the next step | |
869 | is ambiguous. We need to set the result list to the best | |
870 | we've found (if an inferior hasn't already set it). */ | |
871 | if (result_list != NULL) | |
872 | if (!*result_list) | |
873 | /* This used to say *result_list = *found->prefixlist | |
c5aa993b JM |
874 | If that was correct, need to modify the documentation |
875 | at the top of this function to clarify what is supposed | |
876 | to be going on. */ | |
c906108c SS |
877 | *result_list = found; |
878 | return c; | |
879 | } | |
880 | else | |
881 | { | |
882 | /* We matched! */ | |
883 | return c; | |
884 | } | |
885 | } | |
886 | else | |
887 | { | |
888 | if (result_list != NULL) | |
889 | *result_list = clist; | |
890 | return found; | |
891 | } | |
892 | } | |
893 | ||
894 | /* All this hair to move the space to the front of cmdtype */ | |
895 | ||
896 | static void | |
fba45db2 | 897 | undef_cmd_error (char *cmdtype, char *q) |
c906108c SS |
898 | { |
899 | error ("Undefined %scommand: \"%s\". Try \"help%s%.*s\".", | |
c5aa993b JM |
900 | cmdtype, |
901 | q, | |
902 | *cmdtype ? " " : "", | |
903 | strlen (cmdtype) - 1, | |
904 | cmdtype); | |
c906108c SS |
905 | } |
906 | ||
907 | /* Look up the contents of *LINE as a command in the command list LIST. | |
908 | LIST is a chain of struct cmd_list_element's. | |
909 | If it is found, return the struct cmd_list_element for that command | |
910 | and update *LINE to point after the command name, at the first argument. | |
911 | If not found, call error if ALLOW_UNKNOWN is zero | |
912 | otherwise (or if error returns) return zero. | |
913 | Call error if specified command is ambiguous, | |
914 | unless ALLOW_UNKNOWN is negative. | |
915 | CMDTYPE precedes the word "command" in the error message. | |
916 | ||
917 | If INGNORE_HELP_CLASSES is nonzero, ignore any command list | |
918 | elements which are actually help classes rather than commands (i.e. | |
919 | the function field of the struct cmd_list_element is 0). */ | |
920 | ||
921 | struct cmd_list_element * | |
fba45db2 KB |
922 | lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype, |
923 | int allow_unknown, int ignore_help_classes) | |
c906108c SS |
924 | { |
925 | struct cmd_list_element *last_list = 0; | |
926 | struct cmd_list_element *c = | |
c5aa993b | 927 | lookup_cmd_1 (line, list, &last_list, ignore_help_classes); |
c64601c7 FN |
928 | |
929 | /* Note: Do not remove trailing whitespace here because this | |
930 | would be wrong for complete_command. Jim Kingdon */ | |
c5aa993b | 931 | |
c906108c SS |
932 | if (!c) |
933 | { | |
934 | if (!allow_unknown) | |
935 | { | |
936 | if (!*line) | |
937 | error ("Lack of needed %scommand", cmdtype); | |
938 | else | |
939 | { | |
940 | char *p = *line, *q; | |
941 | ||
c5aa993b | 942 | while (isalnum (*p) || *p == '-') |
c906108c SS |
943 | p++; |
944 | ||
945 | q = (char *) alloca (p - *line + 1); | |
946 | strncpy (q, *line, p - *line); | |
947 | q[p - *line] = '\0'; | |
948 | undef_cmd_error (cmdtype, q); | |
949 | } | |
950 | } | |
951 | else | |
952 | return 0; | |
953 | } | |
954 | else if (c == (struct cmd_list_element *) -1) | |
955 | { | |
956 | /* Ambigous. Local values should be off prefixlist or called | |
c5aa993b | 957 | values. */ |
c906108c SS |
958 | int local_allow_unknown = (last_list ? last_list->allow_unknown : |
959 | allow_unknown); | |
960 | char *local_cmdtype = last_list ? last_list->prefixname : cmdtype; | |
961 | struct cmd_list_element *local_list = | |
c5aa993b JM |
962 | (last_list ? *(last_list->prefixlist) : list); |
963 | ||
c906108c SS |
964 | if (local_allow_unknown < 0) |
965 | { | |
966 | if (last_list) | |
967 | return last_list; /* Found something. */ | |
968 | else | |
969 | return 0; /* Found nothing. */ | |
970 | } | |
971 | else | |
972 | { | |
973 | /* Report as error. */ | |
974 | int amb_len; | |
975 | char ambbuf[100]; | |
976 | ||
977 | for (amb_len = 0; | |
978 | ((*line)[amb_len] && (*line)[amb_len] != ' ' | |
979 | && (*line)[amb_len] != '\t'); | |
980 | amb_len++) | |
981 | ; | |
c5aa993b | 982 | |
c906108c SS |
983 | ambbuf[0] = 0; |
984 | for (c = local_list; c; c = c->next) | |
985 | if (!strncmp (*line, c->name, amb_len)) | |
986 | { | |
c5aa993b | 987 | if (strlen (ambbuf) + strlen (c->name) + 6 < (int) sizeof ambbuf) |
c906108c SS |
988 | { |
989 | if (strlen (ambbuf)) | |
990 | strcat (ambbuf, ", "); | |
991 | strcat (ambbuf, c->name); | |
992 | } | |
993 | else | |
994 | { | |
995 | strcat (ambbuf, ".."); | |
996 | break; | |
997 | } | |
998 | } | |
999 | error ("Ambiguous %scommand \"%s\": %s.", local_cmdtype, | |
1000 | *line, ambbuf); | |
1001 | return 0; /* lint */ | |
1002 | } | |
1003 | } | |
1004 | else | |
1005 | { | |
1006 | /* We've got something. It may still not be what the caller | |
1007 | wants (if this command *needs* a subcommand). */ | |
1008 | while (**line == ' ' || **line == '\t') | |
1009 | (*line)++; | |
1010 | ||
1011 | if (c->prefixlist && **line && !c->allow_unknown) | |
1012 | undef_cmd_error (c->prefixname, *line); | |
1013 | ||
1014 | /* Seems to be what he wants. Return it. */ | |
1015 | return c; | |
1016 | } | |
1017 | return 0; | |
1018 | } | |
c5aa993b | 1019 | |
56382845 FN |
1020 | /* We are here presumably because an alias or command in *TEXT is |
1021 | deprecated and a warning message should be generated. This function | |
1022 | decodes *TEXT and potentially generates a warning message as outlined | |
1023 | below. | |
1024 | ||
1025 | Example for 'set endian big' which has a fictitious alias 'seb'. | |
1026 | ||
1027 | If alias wasn't used in *TEXT, and the command is deprecated: | |
1028 | "warning: 'set endian big' is deprecated." | |
1029 | ||
1030 | If alias was used, and only the alias is deprecated: | |
1031 | "warning: 'seb' an alias for the command 'set endian big' is deprecated." | |
1032 | ||
1033 | If alias was used and command is deprecated (regardless of whether the | |
1034 | alias itself is deprecated: | |
1035 | ||
1036 | "warning: 'set endian big' (seb) is deprecated." | |
1037 | ||
1038 | After the message has been sent, clear the appropriate flags in the | |
1039 | command and/or the alias so the user is no longer bothered. | |
1040 | ||
1041 | */ | |
1042 | void | |
1043 | deprecated_cmd_warning (char **text) | |
1044 | { | |
1045 | struct cmd_list_element *alias = NULL; | |
1046 | struct cmd_list_element *prefix_cmd = NULL; | |
1047 | struct cmd_list_element *cmd = NULL; | |
1048 | struct cmd_list_element *c; | |
1049 | char *type; | |
1050 | ||
1051 | if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd)) | |
1052 | /* return if text doesn't evaluate to a command */ | |
1053 | return; | |
1054 | ||
1055 | if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0) | |
1056 | || (cmd->flags & DEPRECATED_WARN_USER) ) ) | |
1057 | /* return if nothing is deprecated */ | |
1058 | return; | |
1059 | ||
1060 | printf_filtered ("Warning:"); | |
1061 | ||
1062 | if (alias && !(cmd->flags & CMD_DEPRECATED)) | |
1063 | printf_filtered (" '%s', an alias for the", alias->name); | |
1064 | ||
1065 | printf_filtered (" command '"); | |
1066 | ||
1067 | if (prefix_cmd) | |
1068 | printf_filtered ("%s", prefix_cmd->prefixname); | |
1069 | ||
1070 | printf_filtered ("%s", cmd->name); | |
1071 | ||
1072 | if (alias && (cmd->flags & CMD_DEPRECATED)) | |
1073 | printf_filtered ("' (%s) is deprecated.\n", alias->name); | |
1074 | else | |
1075 | printf_filtered ("' is deprecated.\n"); | |
1076 | ||
1077 | ||
1078 | /* if it is only the alias that is deprecated, we want to indicate the | |
1079 | new alias, otherwise we'll indicate the new command */ | |
1080 | ||
1081 | if (alias && !(cmd->flags & CMD_DEPRECATED)) | |
1082 | { | |
1083 | if (alias->replacement) | |
1084 | printf_filtered ("Use '%s'.\n\n", alias->replacement); | |
1085 | else | |
1086 | printf_filtered ("No alternative known.\n\n"); | |
1087 | } | |
1088 | else | |
1089 | { | |
1090 | if (cmd->replacement) | |
1091 | printf_filtered ("Use '%s'.\n\n", cmd->replacement); | |
1092 | else | |
1093 | printf_filtered ("No alternative known.\n\n"); | |
1094 | } | |
1095 | ||
1096 | /* We've warned you, now we'll keep quiet */ | |
1097 | if (alias) | |
1098 | alias->flags &= ~DEPRECATED_WARN_USER; | |
1099 | ||
1100 | cmd->flags &= ~DEPRECATED_WARN_USER; | |
1101 | } | |
1102 | ||
1103 | ||
1104 | ||
1105 | /* Look up the contents of LINE as a command in the command list 'cmdlist'. | |
1106 | Return 1 on success, 0 on failure. | |
1107 | ||
1108 | If LINE refers to an alias, *alias will point to that alias. | |
1109 | ||
1110 | If LINE is a postfix command (i.e. one that is preceeded by a prefix | |
1111 | command) set *prefix_cmd. | |
1112 | ||
1113 | Set *cmd to point to the command LINE indicates. | |
1114 | ||
1115 | If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not | |
1116 | exist, they are NULL when we return. | |
1117 | ||
1118 | */ | |
1119 | int | |
1120 | lookup_cmd_composition (char *text, | |
1121 | struct cmd_list_element **alias, | |
1122 | struct cmd_list_element **prefix_cmd, | |
1123 | struct cmd_list_element **cmd) | |
1124 | { | |
1125 | char *p, *command; | |
1126 | int len, tmp, nfound; | |
1127 | struct cmd_list_element *cur_list; | |
1128 | struct cmd_list_element *prev_cmd; | |
1129 | *alias = NULL; | |
1130 | *prefix_cmd = NULL; | |
1131 | *cmd = NULL; | |
1132 | ||
1133 | cur_list = cmdlist; | |
1134 | ||
1135 | while (1) | |
1136 | { | |
1137 | /* Go through as many command lists as we need to | |
1138 | to find the command TEXT refers to. */ | |
1139 | ||
1140 | prev_cmd = *cmd; | |
1141 | ||
1142 | while (*text == ' ' || *text == '\t') | |
1143 | (text)++; | |
1144 | ||
1145 | /* Treating underscores as part of command words is important | |
1146 | so that "set args_foo()" doesn't get interpreted as | |
1147 | "set args _foo()". */ | |
1148 | for (p = text; | |
1149 | *p && (isalnum (*p) || *p == '-' || *p == '_' || | |
1150 | (tui_version && | |
1151 | (*p == '+' || *p == '<' || *p == '>' || *p == '$')) || | |
1152 | (xdb_commands && (*p == '!' || *p == '/' || *p == '?'))); | |
1153 | p++) | |
1154 | ; | |
1155 | ||
1156 | /* If nothing but whitespace, return. */ | |
1157 | if (p == text) | |
1158 | return 0; | |
1159 | ||
1160 | len = p - text; | |
1161 | ||
1162 | /* text and p now bracket the first command word to lookup (and | |
1163 | it's length is len). We copy this into a local temporary */ | |
1164 | ||
1165 | command = (char *) alloca (len + 1); | |
1166 | for (tmp = 0; tmp < len; tmp++) | |
1167 | { | |
1168 | char x = text[tmp]; | |
1169 | command[tmp] = x; | |
1170 | } | |
1171 | command[len] = '\0'; | |
1172 | ||
1173 | /* Look it up. */ | |
1174 | *cmd = 0; | |
1175 | nfound = 0; | |
1176 | *cmd = find_cmd (command, len, cur_list, 1, &nfound); | |
1177 | ||
1178 | /* We didn't find the command in the entered case, so lower case it | |
1179 | and search again. | |
1180 | */ | |
1181 | if (!*cmd || nfound == 0) | |
1182 | { | |
1183 | for (tmp = 0; tmp < len; tmp++) | |
1184 | { | |
1185 | char x = command[tmp]; | |
1186 | command[tmp] = isupper (x) ? tolower (x) : x; | |
1187 | } | |
1188 | *cmd = find_cmd (command, len, cur_list, 1, &nfound); | |
1189 | } | |
1190 | ||
1191 | if (*cmd == (struct cmd_list_element *) -1) | |
1192 | { | |
1193 | return 0; /* ambiguous */ | |
1194 | } | |
1195 | ||
1196 | if (*cmd == NULL) | |
1197 | return 0; /* nothing found */ | |
1198 | else | |
1199 | { | |
1200 | if ((*cmd)->cmd_pointer) | |
1201 | { | |
1202 | /* cmd was actually an alias, we note that an alias was used | |
1203 | (by assigning *alais) and we set *cmd. | |
1204 | */ | |
1205 | *alias = *cmd; | |
1206 | *cmd = (*cmd)->cmd_pointer; | |
1207 | } | |
1208 | *prefix_cmd = prev_cmd; | |
1209 | } | |
1210 | if ((*cmd)->prefixlist) | |
1211 | cur_list = *(*cmd)->prefixlist; | |
1212 | else | |
1213 | return 1; | |
1214 | ||
1215 | text = p; | |
1216 | } | |
1217 | } | |
1218 | ||
c906108c SS |
1219 | /* Helper function for SYMBOL_COMPLETION_FUNCTION. */ |
1220 | ||
1221 | /* Return a vector of char pointers which point to the different | |
1222 | possible completions in LIST of TEXT. | |
1223 | ||
1224 | WORD points in the same buffer as TEXT, and completions should be | |
1225 | returned relative to this position. For example, suppose TEXT is "foo" | |
1226 | and we want to complete to "foobar". If WORD is "oo", return | |
1227 | "oobar"; if WORD is "baz/foo", return "baz/foobar". */ | |
1228 | ||
1229 | char ** | |
fba45db2 | 1230 | complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word) |
c906108c SS |
1231 | { |
1232 | struct cmd_list_element *ptr; | |
1233 | char **matchlist; | |
1234 | int sizeof_matchlist; | |
1235 | int matches; | |
1236 | int textlen = strlen (text); | |
1237 | ||
1238 | sizeof_matchlist = 10; | |
1239 | matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *)); | |
1240 | matches = 0; | |
1241 | ||
1242 | for (ptr = list; ptr; ptr = ptr->next) | |
1243 | if (!strncmp (ptr->name, text, textlen) | |
1244 | && !ptr->abbrev_flag | |
1245 | && (ptr->function.cfunc | |
1246 | || ptr->prefixlist)) | |
1247 | { | |
1248 | if (matches == sizeof_matchlist) | |
1249 | { | |
1250 | sizeof_matchlist *= 2; | |
c5aa993b | 1251 | matchlist = (char **) xrealloc ((char *) matchlist, |
c906108c SS |
1252 | (sizeof_matchlist |
1253 | * sizeof (char *))); | |
1254 | } | |
1255 | ||
c5aa993b | 1256 | matchlist[matches] = (char *) |
c906108c SS |
1257 | xmalloc (strlen (word) + strlen (ptr->name) + 1); |
1258 | if (word == text) | |
1259 | strcpy (matchlist[matches], ptr->name); | |
1260 | else if (word > text) | |
1261 | { | |
1262 | /* Return some portion of ptr->name. */ | |
1263 | strcpy (matchlist[matches], ptr->name + (word - text)); | |
1264 | } | |
1265 | else | |
1266 | { | |
1267 | /* Return some of text plus ptr->name. */ | |
1268 | strncpy (matchlist[matches], word, text - word); | |
1269 | matchlist[matches][text - word] = '\0'; | |
1270 | strcat (matchlist[matches], ptr->name); | |
1271 | } | |
1272 | ++matches; | |
1273 | } | |
1274 | ||
1275 | if (matches == 0) | |
1276 | { | |
b8c9b27d | 1277 | xfree (matchlist); |
c906108c SS |
1278 | matchlist = 0; |
1279 | } | |
1280 | else | |
1281 | { | |
c5aa993b JM |
1282 | matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1) |
1283 | * sizeof (char *))); | |
c906108c SS |
1284 | matchlist[matches] = (char *) 0; |
1285 | } | |
1286 | ||
1287 | return matchlist; | |
1288 | } | |
1289 | ||
1290 | /* Helper function for SYMBOL_COMPLETION_FUNCTION. */ | |
1291 | ||
1292 | /* Return a vector of char pointers which point to the different | |
1293 | possible completions in CMD of TEXT. | |
1294 | ||
1295 | WORD points in the same buffer as TEXT, and completions should be | |
1296 | returned relative to this position. For example, suppose TEXT is "foo" | |
1297 | and we want to complete to "foobar". If WORD is "oo", return | |
1298 | "oobar"; if WORD is "baz/foo", return "baz/foobar". */ | |
1299 | ||
1300 | char ** | |
53904c9e AC |
1301 | complete_on_enum (const char *enumlist[], |
1302 | char *text, | |
1303 | char *word) | |
c906108c SS |
1304 | { |
1305 | char **matchlist; | |
1306 | int sizeof_matchlist; | |
1307 | int matches; | |
1308 | int textlen = strlen (text); | |
1309 | int i; | |
53904c9e | 1310 | const char *name; |
c906108c SS |
1311 | |
1312 | sizeof_matchlist = 10; | |
1313 | matchlist = (char **) xmalloc (sizeof_matchlist * sizeof (char *)); | |
1314 | matches = 0; | |
1315 | ||
1316 | for (i = 0; (name = enumlist[i]) != NULL; i++) | |
1317 | if (strncmp (name, text, textlen) == 0) | |
1318 | { | |
1319 | if (matches == sizeof_matchlist) | |
1320 | { | |
1321 | sizeof_matchlist *= 2; | |
c5aa993b | 1322 | matchlist = (char **) xrealloc ((char *) matchlist, |
c906108c SS |
1323 | (sizeof_matchlist |
1324 | * sizeof (char *))); | |
1325 | } | |
1326 | ||
c5aa993b | 1327 | matchlist[matches] = (char *) |
c906108c SS |
1328 | xmalloc (strlen (word) + strlen (name) + 1); |
1329 | if (word == text) | |
1330 | strcpy (matchlist[matches], name); | |
1331 | else if (word > text) | |
1332 | { | |
1333 | /* Return some portion of name. */ | |
1334 | strcpy (matchlist[matches], name + (word - text)); | |
1335 | } | |
1336 | else | |
1337 | { | |
1338 | /* Return some of text plus name. */ | |
1339 | strncpy (matchlist[matches], word, text - word); | |
1340 | matchlist[matches][text - word] = '\0'; | |
1341 | strcat (matchlist[matches], name); | |
1342 | } | |
1343 | ++matches; | |
1344 | } | |
1345 | ||
1346 | if (matches == 0) | |
1347 | { | |
b8c9b27d | 1348 | xfree (matchlist); |
c906108c SS |
1349 | matchlist = 0; |
1350 | } | |
1351 | else | |
1352 | { | |
c5aa993b JM |
1353 | matchlist = (char **) xrealloc ((char *) matchlist, ((matches + 1) |
1354 | * sizeof (char *))); | |
c906108c SS |
1355 | matchlist[matches] = (char *) 0; |
1356 | } | |
1357 | ||
1358 | return matchlist; | |
1359 | } | |
1360 |