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