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