Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Handle lists of commands, their decoding and documentation, for GDB. |
8926118c | 2 | |
28e7fd62 | 3 | Copyright (c) 1986-2013 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b JM |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 7 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 8 | (at your option) any later version. |
c906108c | 9 | |
c5aa993b JM |
10 | This program is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | GNU General Public License for more details. | |
c906108c | 14 | |
c5aa993b | 15 | You should have received a copy of the GNU General Public License |
a9762ec7 | 16 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
17 | |
18 | #include "defs.h" | |
c906108c | 19 | #include "symtab.h" |
c906108c | 20 | #include <ctype.h> |
f77b92bf | 21 | #include "gdb_regex.h" |
5f8a3188 | 22 | #include "gdb_string.h" |
f397e303 | 23 | #include "completer.h" |
8b93c638 | 24 | #include "ui-out.h" |
c906108c | 25 | |
d318976c FN |
26 | #include "cli/cli-cmds.h" |
27 | #include "cli/cli-decode.h" | |
53a5351d | 28 | |
6a83354a | 29 | #ifdef TUI |
ebcd3b23 | 30 | #include "tui/tui.h" /* For tui_active et al. */ |
6a83354a AC |
31 | #endif |
32 | ||
b2875cc0 AC |
33 | #include "gdb_assert.h" |
34 | ||
ebcd3b23 | 35 | /* Prototypes for local functions. */ |
c906108c | 36 | |
a14ed312 | 37 | static void undef_cmd_error (char *, char *); |
c906108c | 38 | |
b05dcbb7 | 39 | static struct cmd_list_element *delete_cmd (char *name, |
fad6eecd TT |
40 | struct cmd_list_element **list, |
41 | struct cmd_list_element **prehook, | |
42 | struct cmd_list_element **prehookee, | |
43 | struct cmd_list_element **posthook, | |
44 | struct cmd_list_element **posthookee); | |
b05dcbb7 | 45 | |
a14ed312 KB |
46 | static struct cmd_list_element *find_cmd (char *command, |
47 | int len, | |
48 | struct cmd_list_element *clist, | |
49 | int ignore_help_classes, | |
50 | int *nfound); | |
6837a0a2 | 51 | |
c85871a3 | 52 | static void help_all (struct ui_file *stream); |
6e381ba0 | 53 | |
5b9afe8a YQ |
54 | /* Look up a command whose 'prefixlist' is KEY. Return the command if found, |
55 | otherwise return NULL. */ | |
56 | ||
57 | static struct cmd_list_element * | |
58 | lookup_cmd_for_prefixlist (struct cmd_list_element **key, | |
59 | struct cmd_list_element *list) | |
60 | { | |
61 | struct cmd_list_element *p = NULL; | |
62 | ||
63 | for (p = list; p != NULL; p = p->next) | |
64 | { | |
65 | struct cmd_list_element *q; | |
66 | ||
67 | if (p->prefixlist == NULL) | |
68 | continue; | |
69 | else if (p->prefixlist == key) | |
70 | return p; | |
71 | ||
72 | q = lookup_cmd_for_prefixlist (key, *(p->prefixlist)); | |
73 | if (q != NULL) | |
74 | return q; | |
75 | } | |
76 | ||
77 | return NULL; | |
78 | } | |
79 | ||
80 | static void | |
81 | set_cmd_prefix (struct cmd_list_element *c, struct cmd_list_element **list) | |
82 | { | |
83 | struct cmd_list_element *p; | |
84 | ||
85 | /* Check to see if *LIST contains any element other than C. */ | |
86 | for (p = *list; p != NULL; p = p->next) | |
87 | if (p != c) | |
88 | break; | |
89 | ||
90 | if (p == NULL) | |
91 | { | |
92 | /* *SET_LIST only contains SET. */ | |
93 | p = lookup_cmd_for_prefixlist (list, setlist); | |
94 | ||
95 | c->prefix = p ? (p->cmd_pointer ? p->cmd_pointer : p) : p; | |
96 | } | |
97 | else | |
98 | c->prefix = p->prefix; | |
99 | } | |
100 | ||
6e381ba0 VP |
101 | static void |
102 | print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse, | |
103 | struct ui_file *stream); | |
104 | ||
d318976c | 105 | \f |
9f60d481 AC |
106 | /* Set the callback function for the specified command. For each both |
107 | the commands callback and func() are set. The latter set to a | |
108 | bounce function (unless cfunc / sfunc is NULL that is). */ | |
109 | ||
110 | static void | |
111 | do_cfunc (struct cmd_list_element *c, char *args, int from_tty) | |
112 | { | |
113 | c->function.cfunc (args, from_tty); /* Ok. */ | |
114 | } | |
115 | ||
116 | void | |
9773a94b | 117 | set_cmd_cfunc (struct cmd_list_element *cmd, cmd_cfunc_ftype *cfunc) |
9f60d481 AC |
118 | { |
119 | if (cfunc == NULL) | |
120 | cmd->func = NULL; | |
121 | else | |
122 | cmd->func = do_cfunc; | |
123 | cmd->function.cfunc = cfunc; /* Ok. */ | |
124 | } | |
125 | ||
126 | static void | |
127 | do_sfunc (struct cmd_list_element *c, char *args, int from_tty) | |
128 | { | |
129 | c->function.sfunc (args, from_tty, c); /* Ok. */ | |
130 | } | |
131 | ||
132 | void | |
9773a94b | 133 | set_cmd_sfunc (struct cmd_list_element *cmd, cmd_sfunc_ftype *sfunc) |
9f60d481 AC |
134 | { |
135 | if (sfunc == NULL) | |
136 | cmd->func = NULL; | |
137 | else | |
138 | cmd->func = do_sfunc; | |
139 | cmd->function.sfunc = sfunc; /* Ok. */ | |
140 | } | |
141 | ||
bbaca940 AC |
142 | int |
143 | cmd_cfunc_eq (struct cmd_list_element *cmd, | |
144 | void (*cfunc) (char *args, int from_tty)) | |
145 | { | |
146 | return cmd->func == do_cfunc && cmd->function.cfunc == cfunc; | |
147 | } | |
148 | ||
7d0766f3 AC |
149 | void |
150 | set_cmd_context (struct cmd_list_element *cmd, void *context) | |
151 | { | |
152 | cmd->context = context; | |
153 | } | |
154 | ||
155 | void * | |
156 | get_cmd_context (struct cmd_list_element *cmd) | |
157 | { | |
158 | return cmd->context; | |
159 | } | |
160 | ||
1868c04e AC |
161 | enum cmd_types |
162 | cmd_type (struct cmd_list_element *cmd) | |
163 | { | |
164 | return cmd->type; | |
165 | } | |
166 | ||
5ba2abeb | 167 | void |
625e8578 | 168 | set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer) |
5ba2abeb AC |
169 | { |
170 | cmd->completer = completer; /* Ok. */ | |
171 | } | |
172 | ||
c906108c | 173 | /* Add element named NAME. |
bc587a6b | 174 | Space for NAME and DOC must be allocated by the caller. |
c906108c SS |
175 | CLASS is the top level category into which commands are broken down |
176 | for "help" purposes. | |
177 | FUN should be the function to execute the command; | |
178 | it will get a character string as argument, with leading | |
179 | and trailing blanks already eliminated. | |
180 | ||
181 | DOC is a documentation string for the command. | |
182 | Its first line should be a complete sentence. | |
183 | It should start with ? for a command that is an abbreviation | |
184 | or with * for a command that most users don't need to know about. | |
185 | ||
ebcd3b23 | 186 | Add this command to command list *LIST. |
c906108c SS |
187 | |
188 | Returns a pointer to the added command (not necessarily the head | |
ebcd3b23 | 189 | of *LIST). */ |
c906108c SS |
190 | |
191 | struct cmd_list_element * | |
af1c1752 KB |
192 | add_cmd (char *name, enum command_class class, void (*fun) (char *, int), |
193 | char *doc, struct cmd_list_element **list) | |
c906108c | 194 | { |
d5b5ac79 | 195 | struct cmd_list_element *c |
cdb27c12 | 196 | = (struct cmd_list_element *) xmalloc (sizeof (struct cmd_list_element)); |
b05dcbb7 | 197 | struct cmd_list_element *p, *iter; |
c906108c | 198 | |
b05dcbb7 TT |
199 | /* Turn each alias of the old command into an alias of the new |
200 | command. */ | |
fad6eecd TT |
201 | c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre, |
202 | &c->hook_post, &c->hookee_post); | |
b05dcbb7 TT |
203 | for (iter = c->aliases; iter; iter = iter->alias_chain) |
204 | iter->cmd_pointer = c; | |
fad6eecd TT |
205 | if (c->hook_pre) |
206 | c->hook_pre->hookee_pre = c; | |
207 | if (c->hookee_pre) | |
208 | c->hookee_pre->hook_pre = c; | |
209 | if (c->hook_post) | |
210 | c->hook_post->hookee_post = c; | |
211 | if (c->hookee_post) | |
212 | c->hookee_post->hook_post = c; | |
c906108c | 213 | |
494b7ec9 | 214 | if (*list == NULL || strcmp ((*list)->name, name) >= 0) |
c906108c SS |
215 | { |
216 | c->next = *list; | |
217 | *list = c; | |
218 | } | |
219 | else | |
220 | { | |
221 | p = *list; | |
494b7ec9 | 222 | while (p->next && strcmp (p->next->name, name) <= 0) |
c5aa993b JM |
223 | { |
224 | p = p->next; | |
225 | } | |
c906108c SS |
226 | c->next = p->next; |
227 | p->next = c; | |
228 | } | |
229 | ||
230 | c->name = name; | |
231 | c->class = class; | |
9f60d481 | 232 | set_cmd_cfunc (c, fun); |
7d0766f3 | 233 | set_cmd_context (c, NULL); |
c906108c | 234 | c->doc = doc; |
56382845 FN |
235 | c->flags = 0; |
236 | c->replacement = NULL; | |
47724592 | 237 | c->pre_show_hook = NULL; |
73bc900d | 238 | c->hook_in = 0; |
c906108c SS |
239 | c->prefixlist = NULL; |
240 | c->prefixname = NULL; | |
241 | c->allow_unknown = 0; | |
5b9afe8a | 242 | c->prefix = NULL; |
c906108c | 243 | c->abbrev_flag = 0; |
d8906c6f TJB |
244 | set_cmd_completer (c, make_symbol_completion_list_fn); |
245 | c->destroyer = NULL; | |
c906108c SS |
246 | c->type = not_set_cmd; |
247 | c->var = NULL; | |
248 | c->var_type = var_boolean; | |
249 | c->enums = NULL; | |
250 | c->user_commands = NULL; | |
c906108c | 251 | c->cmd_pointer = NULL; |
b05dcbb7 | 252 | c->alias_chain = NULL; |
c906108c SS |
253 | |
254 | return c; | |
255 | } | |
256 | ||
56382845 | 257 | /* Deprecates a command CMD. |
ebcd3b23 MS |
258 | REPLACEMENT is the name of the command which should be used in |
259 | place of this command, or NULL if no such command exists. | |
56382845 FN |
260 | |
261 | This function does not check to see if command REPLACEMENT exists | |
ebcd3b23 MS |
262 | since gdb may not have gotten around to adding REPLACEMENT when |
263 | this function is called. | |
56382845 FN |
264 | |
265 | Returns a pointer to the deprecated command. */ | |
266 | ||
267 | struct cmd_list_element * | |
fba45db2 | 268 | deprecate_cmd (struct cmd_list_element *cmd, char *replacement) |
56382845 FN |
269 | { |
270 | cmd->flags |= (CMD_DEPRECATED | DEPRECATED_WARN_USER); | |
271 | ||
272 | if (replacement != NULL) | |
273 | cmd->replacement = replacement; | |
274 | else | |
275 | cmd->replacement = NULL; | |
276 | ||
277 | return cmd; | |
278 | } | |
279 | ||
c906108c | 280 | struct cmd_list_element * |
fba45db2 KB |
281 | add_alias_cmd (char *name, char *oldname, enum command_class class, |
282 | int abbrev_flag, struct cmd_list_element **list) | |
c906108c | 283 | { |
ebcd3b23 MS |
284 | /* Must do this since lookup_cmd tries to side-effect its first |
285 | arg. */ | |
c906108c | 286 | char *copied_name; |
d5b5ac79 AC |
287 | struct cmd_list_element *old; |
288 | struct cmd_list_element *c; | |
cdb27c12 | 289 | |
c906108c SS |
290 | copied_name = (char *) alloca (strlen (oldname) + 1); |
291 | strcpy (copied_name, oldname); | |
c5aa993b | 292 | old = lookup_cmd (&copied_name, *list, "", 1, 1); |
c906108c SS |
293 | |
294 | if (old == 0) | |
295 | { | |
fad6eecd TT |
296 | struct cmd_list_element *prehook, *prehookee, *posthook, *posthookee; |
297 | struct cmd_list_element *aliases = delete_cmd (name, list, | |
298 | &prehook, &prehookee, | |
299 | &posthook, &posthookee); | |
cdb27c12 | 300 | |
b05dcbb7 | 301 | /* If this happens, it means a programmer error somewhere. */ |
300d0284 | 302 | gdb_assert (!aliases && !prehook && !prehookee |
fad6eecd | 303 | && !posthook && ! posthookee); |
c906108c SS |
304 | return 0; |
305 | } | |
306 | ||
9f60d481 | 307 | c = add_cmd (name, class, NULL, old->doc, list); |
5bc81a00 PM |
308 | |
309 | /* If OLD->DOC can be freed, we should make another copy. */ | |
310 | if ((old->flags & DOC_ALLOCATED) != 0) | |
311 | { | |
312 | c->doc = xstrdup (old->doc); | |
313 | c->flags |= DOC_ALLOCATED; | |
314 | } | |
9f60d481 AC |
315 | /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */ |
316 | c->func = old->func; | |
317 | c->function = old->function; | |
c906108c SS |
318 | c->prefixlist = old->prefixlist; |
319 | c->prefixname = old->prefixname; | |
320 | c->allow_unknown = old->allow_unknown; | |
321 | c->abbrev_flag = abbrev_flag; | |
322 | c->cmd_pointer = old; | |
b05dcbb7 TT |
323 | c->alias_chain = old->aliases; |
324 | old->aliases = c; | |
5b9afe8a YQ |
325 | |
326 | set_cmd_prefix (c, list); | |
c906108c SS |
327 | return c; |
328 | } | |
329 | ||
ebcd3b23 MS |
330 | /* Like add_cmd but adds an element for a command prefix: a name that |
331 | should be followed by a subcommand to be looked up in another | |
332 | command list. PREFIXLIST should be the address of the variable | |
333 | containing that list. */ | |
c906108c SS |
334 | |
335 | struct cmd_list_element * | |
9a2b4c1b MS |
336 | add_prefix_cmd (char *name, enum command_class class, |
337 | void (*fun) (char *, int), | |
af1c1752 KB |
338 | char *doc, struct cmd_list_element **prefixlist, |
339 | char *prefixname, int allow_unknown, | |
340 | struct cmd_list_element **list) | |
c906108c | 341 | { |
d5b5ac79 | 342 | struct cmd_list_element *c = add_cmd (name, class, fun, doc, list); |
5b9afe8a | 343 | struct cmd_list_element *p; |
cdb27c12 | 344 | |
c906108c SS |
345 | c->prefixlist = prefixlist; |
346 | c->prefixname = prefixname; | |
347 | c->allow_unknown = allow_unknown; | |
5b9afe8a YQ |
348 | |
349 | if (list == &cmdlist) | |
350 | c->prefix = NULL; | |
351 | else | |
352 | set_cmd_prefix (c, list); | |
353 | ||
354 | /* Update the field 'prefix' of each cmd_list_element in *PREFIXLIST. */ | |
355 | for (p = *prefixlist; p != NULL; p = p->next) | |
356 | p->prefix = c; | |
357 | ||
c906108c SS |
358 | return c; |
359 | } | |
360 | ||
ebcd3b23 | 361 | /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */ |
c5aa993b | 362 | |
c906108c | 363 | struct cmd_list_element * |
af1c1752 KB |
364 | add_abbrev_prefix_cmd (char *name, enum command_class class, |
365 | void (*fun) (char *, int), char *doc, | |
366 | struct cmd_list_element **prefixlist, char *prefixname, | |
367 | int allow_unknown, struct cmd_list_element **list) | |
c906108c | 368 | { |
d5b5ac79 | 369 | struct cmd_list_element *c = add_cmd (name, class, fun, doc, list); |
cdb27c12 | 370 | |
c906108c SS |
371 | c->prefixlist = prefixlist; |
372 | c->prefixname = prefixname; | |
373 | c->allow_unknown = allow_unknown; | |
374 | c->abbrev_flag = 1; | |
375 | return c; | |
376 | } | |
377 | ||
378 | /* This is an empty "cfunc". */ | |
379 | void | |
fba45db2 | 380 | not_just_help_class_command (char *args, int from_tty) |
c906108c SS |
381 | { |
382 | } | |
383 | ||
384 | /* This is an empty "sfunc". */ | |
a14ed312 | 385 | static void empty_sfunc (char *, int, struct cmd_list_element *); |
c906108c SS |
386 | |
387 | static void | |
fba45db2 | 388 | empty_sfunc (char *args, int from_tty, struct cmd_list_element *c) |
c906108c SS |
389 | { |
390 | } | |
391 | ||
b2875cc0 | 392 | /* Add element named NAME to command list LIST (the list for set/show |
c906108c | 393 | or some sublist thereof). |
b2875cc0 | 394 | TYPE is set_cmd or show_cmd. |
c906108c SS |
395 | CLASS is as in add_cmd. |
396 | VAR_TYPE is the kind of thing we are setting. | |
397 | VAR is address of the variable being controlled by this command. | |
398 | DOC is the documentation string. */ | |
399 | ||
b2875cc0 AC |
400 | static struct cmd_list_element * |
401 | add_set_or_show_cmd (char *name, | |
402 | enum cmd_types type, | |
403 | enum command_class class, | |
404 | var_types var_type, | |
405 | void *var, | |
406 | char *doc, | |
407 | struct cmd_list_element **list) | |
c906108c | 408 | { |
e00d1dc8 | 409 | struct cmd_list_element *c = add_cmd (name, class, NULL, doc, list); |
cdb27c12 | 410 | |
b2875cc0 AC |
411 | gdb_assert (type == set_cmd || type == show_cmd); |
412 | c->type = type; | |
c906108c SS |
413 | c->var_type = var_type; |
414 | c->var = var; | |
e00d1dc8 | 415 | /* This needs to be something besides NULL so that this isn't |
c906108c | 416 | treated as a help class. */ |
9f60d481 | 417 | set_cmd_sfunc (c, empty_sfunc); |
c906108c SS |
418 | return c; |
419 | } | |
420 | ||
e707bbc2 AC |
421 | /* Add element named NAME to both the command SET_LIST and SHOW_LIST. |
422 | CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are | |
423 | setting. VAR is address of the variable being controlled by this | |
424 | command. SET_FUNC and SHOW_FUNC are the callback functions (if | |
3b64bf98 AC |
425 | non-NULL). SET_DOC, SHOW_DOC and HELP_DOC are the documentation |
426 | strings. PRINT the format string to print the value. SET_RESULT | |
427 | and SHOW_RESULT, if not NULL, are set to the resulting command | |
428 | structures. */ | |
e707bbc2 | 429 | |
b3f42336 | 430 | static void |
9f064c95 TT |
431 | add_setshow_cmd_full (char *name, |
432 | enum command_class class, | |
433 | var_types var_type, void *var, | |
3b64bf98 | 434 | const char *set_doc, const char *show_doc, |
335cca0d | 435 | const char *help_doc, |
3b64bf98 | 436 | cmd_sfunc_ftype *set_func, |
08546159 | 437 | show_value_ftype *show_func, |
9f064c95 TT |
438 | struct cmd_list_element **set_list, |
439 | struct cmd_list_element **show_list, | |
440 | struct cmd_list_element **set_result, | |
441 | struct cmd_list_element **show_result) | |
e707bbc2 AC |
442 | { |
443 | struct cmd_list_element *set; | |
444 | struct cmd_list_element *show; | |
be7d7357 AC |
445 | char *full_set_doc; |
446 | char *full_show_doc; | |
447 | ||
448 | if (help_doc != NULL) | |
449 | { | |
450 | full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc); | |
451 | full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc); | |
452 | } | |
453 | else | |
454 | { | |
455 | full_set_doc = xstrdup (set_doc); | |
456 | full_show_doc = xstrdup (show_doc); | |
457 | } | |
e707bbc2 | 458 | set = add_set_or_show_cmd (name, set_cmd, class, var_type, var, |
3b64bf98 | 459 | full_set_doc, set_list); |
5bc81a00 PM |
460 | set->flags |= DOC_ALLOCATED; |
461 | ||
e707bbc2 AC |
462 | if (set_func != NULL) |
463 | set_cmd_sfunc (set, set_func); | |
5b9afe8a YQ |
464 | |
465 | set_cmd_prefix (set, set_list); | |
466 | ||
e707bbc2 | 467 | show = add_set_or_show_cmd (name, show_cmd, class, var_type, var, |
3b64bf98 | 468 | full_show_doc, show_list); |
5bc81a00 | 469 | show->flags |= DOC_ALLOCATED; |
08546159 | 470 | show->show_value_func = show_func; |
9f064c95 TT |
471 | |
472 | if (set_result != NULL) | |
473 | *set_result = set; | |
474 | if (show_result != NULL) | |
475 | *show_result = show; | |
476 | } | |
477 | ||
1b295c3d AC |
478 | /* Add element named NAME to command list LIST (the list for set or |
479 | some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list | |
480 | of strings which may follow NAME. VAR is address of the variable | |
481 | which will contain the matching string (from ENUMLIST). */ | |
482 | ||
483 | void | |
484 | add_setshow_enum_cmd (char *name, | |
485 | enum command_class class, | |
40478521 | 486 | const char *const *enumlist, |
1b295c3d AC |
487 | const char **var, |
488 | const char *set_doc, | |
489 | const char *show_doc, | |
490 | const char *help_doc, | |
1b295c3d | 491 | cmd_sfunc_ftype *set_func, |
08546159 | 492 | show_value_ftype *show_func, |
1b295c3d | 493 | struct cmd_list_element **set_list, |
7376b4c2 | 494 | struct cmd_list_element **show_list) |
1b295c3d AC |
495 | { |
496 | struct cmd_list_element *c; | |
cdb27c12 | 497 | |
1b295c3d | 498 | add_setshow_cmd_full (name, class, var_enum, var, |
335cca0d | 499 | set_doc, show_doc, help_doc, |
1b295c3d AC |
500 | set_func, show_func, |
501 | set_list, show_list, | |
7376b4c2 | 502 | &c, NULL); |
1b295c3d AC |
503 | c->enums = enumlist; |
504 | } | |
505 | ||
5b9afe8a YQ |
506 | const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL }; |
507 | ||
e9e68a56 AC |
508 | /* Add an auto-boolean command named NAME to both the set and show |
509 | command list lists. CLASS is as in add_cmd. VAR is address of the | |
510 | variable which will contain the value. DOC is the documentation | |
511 | string. FUNC is the corresponding callback. */ | |
512 | void | |
513 | add_setshow_auto_boolean_cmd (char *name, | |
514 | enum command_class class, | |
515 | enum auto_boolean *var, | |
3b64bf98 | 516 | const char *set_doc, const char *show_doc, |
335cca0d | 517 | const char *help_doc, |
e9e68a56 | 518 | cmd_sfunc_ftype *set_func, |
08546159 | 519 | show_value_ftype *show_func, |
e9e68a56 AC |
520 | struct cmd_list_element **set_list, |
521 | struct cmd_list_element **show_list) | |
97c3646f | 522 | { |
97c3646f | 523 | struct cmd_list_element *c; |
cdb27c12 | 524 | |
9f064c95 | 525 | add_setshow_cmd_full (name, class, var_auto_boolean, var, |
2c5b56ce | 526 | set_doc, show_doc, help_doc, |
3b64bf98 | 527 | set_func, show_func, |
9f064c95 TT |
528 | set_list, show_list, |
529 | &c, NULL); | |
97c3646f | 530 | c->enums = auto_boolean_enums; |
97c3646f AC |
531 | } |
532 | ||
e707bbc2 AC |
533 | /* Add element named NAME to both the set and show command LISTs (the |
534 | list for set/show or some sublist thereof). CLASS is as in | |
535 | add_cmd. VAR is address of the variable which will contain the | |
ba3e8e46 | 536 | value. SET_DOC and SHOW_DOC are the documentation strings. */ |
e707bbc2 | 537 | void |
3b64bf98 AC |
538 | add_setshow_boolean_cmd (char *name, enum command_class class, int *var, |
539 | const char *set_doc, const char *show_doc, | |
335cca0d | 540 | const char *help_doc, |
e707bbc2 | 541 | cmd_sfunc_ftype *set_func, |
08546159 | 542 | show_value_ftype *show_func, |
e707bbc2 AC |
543 | struct cmd_list_element **set_list, |
544 | struct cmd_list_element **show_list) | |
f3796e26 AC |
545 | { |
546 | static const char *boolean_enums[] = { "on", "off", NULL }; | |
547 | struct cmd_list_element *c; | |
cdb27c12 | 548 | |
9f064c95 | 549 | add_setshow_cmd_full (name, class, var_boolean, var, |
2c5b56ce | 550 | set_doc, show_doc, help_doc, |
9f064c95 TT |
551 | set_func, show_func, |
552 | set_list, show_list, | |
553 | &c, NULL); | |
f3796e26 | 554 | c->enums = boolean_enums; |
f3796e26 AC |
555 | } |
556 | ||
b3f42336 AC |
557 | /* Add element named NAME to both the set and show command LISTs (the |
558 | list for set/show or some sublist thereof). */ | |
559 | void | |
560 | add_setshow_filename_cmd (char *name, enum command_class class, | |
561 | char **var, | |
562 | const char *set_doc, const char *show_doc, | |
335cca0d | 563 | const char *help_doc, |
b3f42336 | 564 | cmd_sfunc_ftype *set_func, |
08546159 | 565 | show_value_ftype *show_func, |
b3f42336 AC |
566 | struct cmd_list_element **set_list, |
567 | struct cmd_list_element **show_list) | |
568 | { | |
f397e303 | 569 | struct cmd_list_element *set_result; |
cdb27c12 | 570 | |
b3f42336 | 571 | add_setshow_cmd_full (name, class, var_filename, var, |
2c5b56ce | 572 | set_doc, show_doc, help_doc, |
b3f42336 AC |
573 | set_func, show_func, |
574 | set_list, show_list, | |
f397e303 AC |
575 | &set_result, NULL); |
576 | set_cmd_completer (set_result, filename_completer); | |
b3f42336 AC |
577 | } |
578 | ||
579 | /* Add element named NAME to both the set and show command LISTs (the | |
5efd5804 PA |
580 | list for set/show or some sublist thereof). */ |
581 | void | |
b3f42336 | 582 | add_setshow_string_cmd (char *name, enum command_class class, |
6df3bc68 MS |
583 | char **var, |
584 | const char *set_doc, const char *show_doc, | |
585 | const char *help_doc, | |
586 | cmd_sfunc_ftype *set_func, | |
587 | show_value_ftype *show_func, | |
588 | struct cmd_list_element **set_list, | |
589 | struct cmd_list_element **show_list) | |
b3f42336 AC |
590 | { |
591 | add_setshow_cmd_full (name, class, var_string, var, | |
2c5b56ce | 592 | set_doc, show_doc, help_doc, |
b3f42336 AC |
593 | set_func, show_func, |
594 | set_list, show_list, | |
5efd5804 | 595 | NULL, NULL); |
b3f42336 AC |
596 | } |
597 | ||
26c41df3 | 598 | /* Add element named NAME to both the set and show command LISTs (the |
5efd5804 | 599 | list for set/show or some sublist thereof). */ |
44478ab3 | 600 | struct cmd_list_element * |
26c41df3 AC |
601 | add_setshow_string_noescape_cmd (char *name, enum command_class class, |
602 | char **var, | |
603 | const char *set_doc, const char *show_doc, | |
604 | const char *help_doc, | |
605 | cmd_sfunc_ftype *set_func, | |
606 | show_value_ftype *show_func, | |
607 | struct cmd_list_element **set_list, | |
608 | struct cmd_list_element **show_list) | |
609 | { | |
44478ab3 TT |
610 | struct cmd_list_element *set_cmd; |
611 | ||
26c41df3 AC |
612 | add_setshow_cmd_full (name, class, var_string_noescape, var, |
613 | set_doc, show_doc, help_doc, | |
614 | set_func, show_func, | |
615 | set_list, show_list, | |
44478ab3 TT |
616 | &set_cmd, NULL); |
617 | return set_cmd; | |
26c41df3 AC |
618 | } |
619 | ||
b4b4ac0b AC |
620 | /* Add element named NAME to both the set and show command LISTs (the |
621 | list for set/show or some sublist thereof). */ | |
622 | void | |
623 | add_setshow_optional_filename_cmd (char *name, enum command_class class, | |
624 | char **var, | |
625 | const char *set_doc, const char *show_doc, | |
626 | const char *help_doc, | |
627 | cmd_sfunc_ftype *set_func, | |
628 | show_value_ftype *show_func, | |
629 | struct cmd_list_element **set_list, | |
630 | struct cmd_list_element **show_list) | |
631 | { | |
7f6a6314 PM |
632 | struct cmd_list_element *set_result; |
633 | ||
b4b4ac0b AC |
634 | add_setshow_cmd_full (name, class, var_optional_filename, var, |
635 | set_doc, show_doc, help_doc, | |
636 | set_func, show_func, | |
637 | set_list, show_list, | |
7f6a6314 PM |
638 | &set_result, NULL); |
639 | ||
640 | set_cmd_completer (set_result, filename_completer); | |
641 | ||
b4b4ac0b AC |
642 | } |
643 | ||
c0d88b1b AC |
644 | /* Add element named NAME to both the set and show command LISTs (the |
645 | list for set/show or some sublist thereof). CLASS is as in | |
646 | add_cmd. VAR is address of the variable which will contain the | |
6fc1c773 YQ |
647 | value. SET_DOC and SHOW_DOC are the documentation strings. This |
648 | function is only used in Python API. Please don't use it elsewhere. */ | |
5efd5804 | 649 | void |
c0d88b1b | 650 | add_setshow_integer_cmd (char *name, enum command_class class, |
47b667de | 651 | int *var, |
6df3bc68 MS |
652 | const char *set_doc, const char *show_doc, |
653 | const char *help_doc, | |
654 | cmd_sfunc_ftype *set_func, | |
655 | show_value_ftype *show_func, | |
656 | struct cmd_list_element **set_list, | |
657 | struct cmd_list_element **show_list) | |
c0d88b1b AC |
658 | { |
659 | add_setshow_cmd_full (name, class, var_integer, var, | |
660 | set_doc, show_doc, help_doc, | |
661 | set_func, show_func, | |
662 | set_list, show_list, | |
5efd5804 | 663 | NULL, NULL); |
c0d88b1b AC |
664 | } |
665 | ||
25d29d70 AC |
666 | /* Add element named NAME to both the set and show command LISTs (the |
667 | list for set/show or some sublist thereof). CLASS is as in | |
668 | add_cmd. VAR is address of the variable which will contain the | |
5efd5804 PA |
669 | value. SET_DOC and SHOW_DOC are the documentation strings. */ |
670 | void | |
3b64bf98 AC |
671 | add_setshow_uinteger_cmd (char *name, enum command_class class, |
672 | unsigned int *var, | |
673 | const char *set_doc, const char *show_doc, | |
335cca0d | 674 | const char *help_doc, |
25d29d70 | 675 | cmd_sfunc_ftype *set_func, |
08546159 | 676 | show_value_ftype *show_func, |
25d29d70 AC |
677 | struct cmd_list_element **set_list, |
678 | struct cmd_list_element **show_list) | |
679 | { | |
680 | add_setshow_cmd_full (name, class, var_uinteger, var, | |
2c5b56ce | 681 | set_doc, show_doc, help_doc, |
25d29d70 AC |
682 | set_func, show_func, |
683 | set_list, show_list, | |
5efd5804 | 684 | NULL, NULL); |
25d29d70 AC |
685 | } |
686 | ||
15170568 AC |
687 | /* Add element named NAME to both the set and show command LISTs (the |
688 | list for set/show or some sublist thereof). CLASS is as in | |
689 | add_cmd. VAR is address of the variable which will contain the | |
5efd5804 PA |
690 | value. SET_DOC and SHOW_DOC are the documentation strings. */ |
691 | void | |
3b64bf98 AC |
692 | add_setshow_zinteger_cmd (char *name, enum command_class class, |
693 | int *var, | |
694 | const char *set_doc, const char *show_doc, | |
335cca0d | 695 | const char *help_doc, |
15170568 | 696 | cmd_sfunc_ftype *set_func, |
08546159 | 697 | show_value_ftype *show_func, |
15170568 AC |
698 | struct cmd_list_element **set_list, |
699 | struct cmd_list_element **show_list) | |
700 | { | |
701 | add_setshow_cmd_full (name, class, var_zinteger, var, | |
2c5b56ce | 702 | set_doc, show_doc, help_doc, |
15170568 AC |
703 | set_func, show_func, |
704 | set_list, show_list, | |
5efd5804 | 705 | NULL, NULL); |
15170568 AC |
706 | } |
707 | ||
6fc1c773 YQ |
708 | void |
709 | add_setshow_zuinteger_unlimited_cmd (char *name, | |
710 | enum command_class class, | |
711 | unsigned int *var, | |
712 | const char *set_doc, | |
713 | const char *show_doc, | |
714 | const char *help_doc, | |
715 | cmd_sfunc_ftype *set_func, | |
716 | show_value_ftype *show_func, | |
717 | struct cmd_list_element **set_list, | |
718 | struct cmd_list_element **show_list) | |
719 | { | |
720 | add_setshow_cmd_full (name, class, var_zuinteger_unlimited, var, | |
721 | set_doc, show_doc, help_doc, | |
722 | set_func, show_func, | |
723 | set_list, show_list, | |
724 | NULL, NULL); | |
725 | } | |
726 | ||
1e8fb976 PA |
727 | /* Add element named NAME to both the set and show command LISTs (the |
728 | list for set/show or some sublist thereof). CLASS is as in | |
729 | add_cmd. VAR is address of the variable which will contain the | |
5efd5804 PA |
730 | value. SET_DOC and SHOW_DOC are the documentation strings. */ |
731 | void | |
1e8fb976 PA |
732 | add_setshow_zuinteger_cmd (char *name, enum command_class class, |
733 | unsigned int *var, | |
734 | const char *set_doc, const char *show_doc, | |
735 | const char *help_doc, | |
736 | cmd_sfunc_ftype *set_func, | |
737 | show_value_ftype *show_func, | |
738 | struct cmd_list_element **set_list, | |
739 | struct cmd_list_element **show_list) | |
740 | { | |
741 | add_setshow_cmd_full (name, class, var_zuinteger, var, | |
742 | set_doc, show_doc, help_doc, | |
743 | set_func, show_func, | |
744 | set_list, show_list, | |
5efd5804 | 745 | NULL, NULL); |
1e8fb976 PA |
746 | } |
747 | ||
b05dcbb7 TT |
748 | /* Remove the command named NAME from the command list. Return the |
749 | list commands which were aliased to the deleted command. If the | |
fad6eecd TT |
750 | command had no aliases, return NULL. The various *HOOKs are set to |
751 | the pre- and post-hook commands for the deleted command. If the | |
752 | command does not have a hook, the corresponding out parameter is | |
753 | set to NULL. */ | |
c906108c | 754 | |
b05dcbb7 | 755 | static struct cmd_list_element * |
fad6eecd TT |
756 | delete_cmd (char *name, struct cmd_list_element **list, |
757 | struct cmd_list_element **prehook, | |
758 | struct cmd_list_element **prehookee, | |
759 | struct cmd_list_element **posthook, | |
760 | struct cmd_list_element **posthookee) | |
c906108c | 761 | { |
b05dcbb7 TT |
762 | struct cmd_list_element *iter; |
763 | struct cmd_list_element **previous_chain_ptr; | |
764 | struct cmd_list_element *aliases = NULL; | |
c906108c | 765 | |
fad6eecd TT |
766 | *prehook = NULL; |
767 | *prehookee = NULL; | |
768 | *posthook = NULL; | |
769 | *posthookee = NULL; | |
b05dcbb7 TT |
770 | previous_chain_ptr = list; |
771 | ||
772 | for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr) | |
c906108c | 773 | { |
b05dcbb7 TT |
774 | if (strcmp (iter->name, name) == 0) |
775 | { | |
d8906c6f TJB |
776 | if (iter->destroyer) |
777 | iter->destroyer (iter, iter->context); | |
b05dcbb7 TT |
778 | if (iter->hookee_pre) |
779 | iter->hookee_pre->hook_pre = 0; | |
fad6eecd TT |
780 | *prehook = iter->hook_pre; |
781 | *prehookee = iter->hookee_pre; | |
b05dcbb7 TT |
782 | if (iter->hookee_post) |
783 | iter->hookee_post->hook_post = 0; | |
5bc81a00 PM |
784 | if (iter->doc && (iter->flags & DOC_ALLOCATED) != 0) |
785 | xfree (iter->doc); | |
fad6eecd TT |
786 | *posthook = iter->hook_post; |
787 | *posthookee = iter->hookee_post; | |
b05dcbb7 TT |
788 | |
789 | /* Update the link. */ | |
790 | *previous_chain_ptr = iter->next; | |
791 | ||
792 | aliases = iter->aliases; | |
793 | ||
794 | /* If this command was an alias, remove it from the list of | |
795 | aliases. */ | |
796 | if (iter->cmd_pointer) | |
797 | { | |
798 | struct cmd_list_element **prevp = &iter->cmd_pointer->aliases; | |
799 | struct cmd_list_element *a = *prevp; | |
800 | ||
801 | while (a != iter) | |
802 | { | |
803 | prevp = &a->alias_chain; | |
804 | a = *prevp; | |
805 | } | |
806 | *prevp = iter->alias_chain; | |
807 | } | |
808 | ||
809 | xfree (iter); | |
810 | ||
811 | /* We won't see another command with the same name. */ | |
812 | break; | |
813 | } | |
814 | else | |
815 | previous_chain_ptr = &iter->next; | |
c906108c SS |
816 | } |
817 | ||
b05dcbb7 | 818 | return aliases; |
c906108c | 819 | } |
d318976c | 820 | \f |
ebcd3b23 | 821 | /* Shorthands to the commands above. */ |
d318976c FN |
822 | |
823 | /* Add an element to the list of info subcommands. */ | |
824 | ||
825 | struct cmd_list_element * | |
826 | add_info (char *name, void (*fun) (char *, int), char *doc) | |
827 | { | |
828 | return add_cmd (name, no_class, fun, doc, &infolist); | |
829 | } | |
830 | ||
831 | /* Add an alias to the list of info subcommands. */ | |
832 | ||
833 | struct cmd_list_element * | |
834 | add_info_alias (char *name, char *oldname, int abbrev_flag) | |
835 | { | |
836 | return add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist); | |
837 | } | |
838 | ||
839 | /* Add an element to the list of commands. */ | |
840 | ||
841 | struct cmd_list_element * | |
842 | add_com (char *name, enum command_class class, void (*fun) (char *, int), | |
843 | char *doc) | |
844 | { | |
845 | return add_cmd (name, class, fun, doc, &cmdlist); | |
846 | } | |
847 | ||
848 | /* Add an alias or abbreviation command to the list of commands. */ | |
849 | ||
850 | struct cmd_list_element * | |
851 | add_com_alias (char *name, char *oldname, enum command_class class, | |
852 | int abbrev_flag) | |
853 | { | |
854 | return add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist); | |
855 | } | |
856 | \f | |
6837a0a2 DB |
857 | /* Recursively walk the commandlist structures, and print out the |
858 | documentation of commands that match our regex in either their | |
859 | name, or their documentation. | |
860 | */ | |
d318976c | 861 | void |
ebcd3b23 MS |
862 | apropos_cmd (struct ui_file *stream, |
863 | struct cmd_list_element *commandlist, | |
cdb27c12 | 864 | struct re_pattern_buffer *regex, char *prefix) |
6837a0a2 | 865 | { |
d5b5ac79 | 866 | struct cmd_list_element *c; |
4a98be19 | 867 | int returnvalue; |
cdb27c12 | 868 | |
ebcd3b23 | 869 | /* Walk through the commands. */ |
6837a0a2 DB |
870 | for (c=commandlist;c;c=c->next) |
871 | { | |
ebcd3b23 | 872 | returnvalue = -1; /* Needed to avoid double printing. */ |
6837a0a2 DB |
873 | if (c->name != NULL) |
874 | { | |
ebcd3b23 | 875 | /* Try to match against the name. */ |
cdb27c12 MS |
876 | returnvalue = re_search (regex, c->name, strlen(c->name), |
877 | 0, strlen (c->name), NULL); | |
6837a0a2 DB |
878 | if (returnvalue >= 0) |
879 | { | |
6e381ba0 VP |
880 | print_help_for_command (c, prefix, |
881 | 0 /* don't recurse */, stream); | |
6837a0a2 DB |
882 | } |
883 | } | |
4a98be19 | 884 | if (c->doc != NULL && returnvalue < 0) |
6837a0a2 | 885 | { |
ebcd3b23 | 886 | /* Try to match against documentation. */ |
6837a0a2 DB |
887 | if (re_search(regex,c->doc,strlen(c->doc),0,strlen(c->doc),NULL) >=0) |
888 | { | |
6e381ba0 VP |
889 | print_help_for_command (c, prefix, |
890 | 0 /* don't recurse */, stream); | |
6837a0a2 DB |
891 | } |
892 | } | |
ebcd3b23 MS |
893 | /* Check if this command has subcommands and is not an |
894 | abbreviation. We skip listing subcommands of abbreviations | |
895 | in order to avoid duplicates in the output. */ | |
5d2c29b8 | 896 | if (c->prefixlist != NULL && !c->abbrev_flag) |
6837a0a2 DB |
897 | { |
898 | /* Recursively call ourselves on the subcommand list, | |
ebcd3b23 | 899 | passing the right prefix in. */ |
d318976c | 900 | apropos_cmd (stream,*c->prefixlist,regex,c->prefixname); |
6837a0a2 DB |
901 | } |
902 | } | |
903 | } | |
c906108c SS |
904 | |
905 | /* This command really has to deal with two things: | |
ebcd3b23 MS |
906 | 1) I want documentation on *this string* (usually called by |
907 | "help commandname"). | |
908 | ||
909 | 2) I want documentation on *this list* (usually called by giving a | |
910 | command that requires subcommands. Also called by saying just | |
911 | "help".) | |
912 | ||
913 | I am going to split this into two seperate comamnds, help_cmd and | |
914 | help_list. */ | |
c906108c SS |
915 | |
916 | void | |
fba45db2 | 917 | help_cmd (char *command, struct ui_file *stream) |
c906108c SS |
918 | { |
919 | struct cmd_list_element *c; | |
920 | extern struct cmd_list_element *cmdlist; | |
921 | ||
922 | if (!command) | |
923 | { | |
924 | help_list (cmdlist, "", all_classes, stream); | |
925 | return; | |
926 | } | |
927 | ||
49a5a3a3 GM |
928 | if (strcmp (command, "all") == 0) |
929 | { | |
930 | help_all (stream); | |
931 | return; | |
932 | } | |
933 | ||
c906108c SS |
934 | c = lookup_cmd (&command, cmdlist, "", 0, 0); |
935 | ||
936 | if (c == 0) | |
937 | return; | |
938 | ||
939 | /* There are three cases here. | |
940 | If c->prefixlist is nonzero, we have a prefix command. | |
941 | Print its documentation, then list its subcommands. | |
c5aa993b | 942 | |
9f60d481 AC |
943 | If c->func is non NULL, we really have a command. Print its |
944 | documentation and return. | |
c5aa993b | 945 | |
9f60d481 AC |
946 | If c->func is NULL, we have a class name. Print its |
947 | documentation (as if it were a command) and then set class to the | |
948 | number of this class so that the commands in the class will be | |
949 | listed. */ | |
c906108c SS |
950 | |
951 | fputs_filtered (c->doc, stream); | |
952 | fputs_filtered ("\n", stream); | |
953 | ||
9f60d481 | 954 | if (c->prefixlist == 0 && c->func != NULL) |
c906108c SS |
955 | return; |
956 | fprintf_filtered (stream, "\n"); | |
957 | ||
ebcd3b23 | 958 | /* If this is a prefix command, print it's subcommands. */ |
c906108c SS |
959 | if (c->prefixlist) |
960 | help_list (*c->prefixlist, c->prefixname, all_commands, stream); | |
961 | ||
ebcd3b23 | 962 | /* If this is a class name, print all of the commands in the class. */ |
9f60d481 | 963 | if (c->func == NULL) |
c906108c SS |
964 | help_list (cmdlist, "", c->class, stream); |
965 | ||
73bc900d FN |
966 | if (c->hook_pre || c->hook_post) |
967 | fprintf_filtered (stream, | |
968 | "\nThis command has a hook (or hooks) defined:\n"); | |
969 | ||
970 | if (c->hook_pre) | |
327529e9 | 971 | fprintf_filtered (stream, |
72fe0832 | 972 | "\tThis command is run after : %s (pre hook)\n", |
73bc900d FN |
973 | c->hook_pre->name); |
974 | if (c->hook_post) | |
327529e9 | 975 | fprintf_filtered (stream, |
72fe0832 | 976 | "\tThis command is run before : %s (post hook)\n", |
73bc900d | 977 | c->hook_post->name); |
c906108c SS |
978 | } |
979 | ||
980 | /* | |
981 | * Get a specific kind of help on a command list. | |
982 | * | |
983 | * LIST is the list. | |
984 | * CMDTYPE is the prefix to use in the title string. | |
985 | * CLASS is the class with which to list the nodes of this list (see | |
986 | * documentation for help_cmd_list below), As usual, ALL_COMMANDS for | |
987 | * everything, ALL_CLASSES for just classes, and non-negative for only things | |
988 | * in a specific class. | |
989 | * and STREAM is the output stream on which to print things. | |
990 | * If you call this routine with a class >= 0, it recurses. | |
991 | */ | |
992 | void | |
fba45db2 KB |
993 | help_list (struct cmd_list_element *list, char *cmdtype, |
994 | enum command_class class, struct ui_file *stream) | |
c906108c SS |
995 | { |
996 | int len; | |
997 | char *cmdtype1, *cmdtype2; | |
c5aa993b | 998 | |
ebcd3b23 MS |
999 | /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub". |
1000 | */ | |
c906108c SS |
1001 | len = strlen (cmdtype); |
1002 | cmdtype1 = (char *) alloca (len + 1); | |
1003 | cmdtype1[0] = 0; | |
1004 | cmdtype2 = (char *) alloca (len + 4); | |
1005 | cmdtype2[0] = 0; | |
1006 | if (len) | |
1007 | { | |
1008 | cmdtype1[0] = ' '; | |
1009 | strncpy (cmdtype1 + 1, cmdtype, len - 1); | |
1010 | cmdtype1[len] = 0; | |
1011 | strncpy (cmdtype2, cmdtype, len - 1); | |
1012 | strcpy (cmdtype2 + len - 1, " sub"); | |
1013 | } | |
1014 | ||
1015 | if (class == all_classes) | |
1016 | fprintf_filtered (stream, "List of classes of %scommands:\n\n", cmdtype2); | |
1017 | else | |
1018 | fprintf_filtered (stream, "List of %scommands:\n\n", cmdtype2); | |
1019 | ||
c5aa993b | 1020 | help_cmd_list (list, class, cmdtype, (int) class >= 0, stream); |
c906108c SS |
1021 | |
1022 | if (class == all_classes) | |
de74f71f MS |
1023 | { |
1024 | fprintf_filtered (stream, "\n\ | |
1025 | Type \"help%s\" followed by a class name for a list of commands in ", | |
1026 | cmdtype1); | |
1027 | wrap_here (""); | |
1028 | fprintf_filtered (stream, "that class."); | |
6e381ba0 VP |
1029 | |
1030 | fprintf_filtered (stream, "\n\ | |
1031 | Type \"help all\" for the list of all commands."); | |
de74f71f | 1032 | } |
c906108c | 1033 | |
de74f71f | 1034 | fprintf_filtered (stream, "\nType \"help%s\" followed by %scommand name ", |
c5aa993b | 1035 | cmdtype1, cmdtype2); |
de74f71f MS |
1036 | wrap_here (""); |
1037 | fputs_filtered ("for ", stream); | |
1038 | wrap_here (""); | |
1039 | fputs_filtered ("full ", stream); | |
1040 | wrap_here (""); | |
1041 | fputs_filtered ("documentation.\n", stream); | |
6e381ba0 | 1042 | fputs_filtered ("Type \"apropos word\" to search " |
ebcd3b23 | 1043 | "for commands related to \"word\".\n", stream); |
de74f71f MS |
1044 | fputs_filtered ("Command name abbreviations are allowed if unambiguous.\n", |
1045 | stream); | |
c906108c | 1046 | } |
c5aa993b | 1047 | |
49a5a3a3 | 1048 | static void |
c85871a3 | 1049 | help_all (struct ui_file *stream) |
49a5a3a3 GM |
1050 | { |
1051 | struct cmd_list_element *c; | |
1052 | extern struct cmd_list_element *cmdlist; | |
6e381ba0 | 1053 | int seen_unclassified = 0; |
49a5a3a3 GM |
1054 | |
1055 | for (c = cmdlist; c; c = c->next) | |
1056 | { | |
1057 | if (c->abbrev_flag) | |
1058 | continue; | |
ebcd3b23 MS |
1059 | /* If this is a class name, print all of the commands in the |
1060 | class. */ | |
6e381ba0 VP |
1061 | |
1062 | if (c->func == NULL) | |
1063 | { | |
1064 | fprintf_filtered (stream, "\nCommand class: %s\n\n", c->name); | |
1065 | help_cmd_list (cmdlist, c->class, "", 1, stream); | |
1066 | } | |
49a5a3a3 | 1067 | } |
6e381ba0 VP |
1068 | |
1069 | /* While it's expected that all commands are in some class, | |
1070 | as a safety measure, we'll print commands outside of any | |
1071 | class at the end. */ | |
1072 | ||
1073 | for (c = cmdlist; c; c = c->next) | |
1074 | { | |
1075 | if (c->abbrev_flag) | |
1076 | continue; | |
1077 | ||
1078 | if (c->class == no_class) | |
1079 | { | |
1080 | if (!seen_unclassified) | |
1081 | { | |
1082 | fprintf_filtered (stream, "\nUnclassified commands\n\n"); | |
1083 | seen_unclassified = 1; | |
1084 | } | |
1085 | print_help_for_command (c, "", 1, stream); | |
1086 | } | |
1087 | } | |
1088 | ||
49a5a3a3 GM |
1089 | } |
1090 | ||
c906108c | 1091 | /* Print only the first line of STR on STREAM. */ |
d318976c | 1092 | void |
fba45db2 | 1093 | print_doc_line (struct ui_file *stream, char *str) |
c906108c SS |
1094 | { |
1095 | static char *line_buffer = 0; | |
1096 | static int line_size; | |
d5b5ac79 | 1097 | char *p; |
c906108c SS |
1098 | |
1099 | if (!line_buffer) | |
1100 | { | |
1101 | line_size = 80; | |
1102 | line_buffer = (char *) xmalloc (line_size); | |
1103 | } | |
1104 | ||
7afa1642 JK |
1105 | /* Keep printing '.' or ',' not followed by a whitespace for embedded strings |
1106 | like '.gdbinit'. */ | |
c906108c | 1107 | p = str; |
7afa1642 JK |
1108 | while (*p && *p != '\n' |
1109 | && ((*p != '.' && *p != ',') || (p[1] && !isspace (p[1])))) | |
c906108c SS |
1110 | p++; |
1111 | if (p - str > line_size - 1) | |
1112 | { | |
1113 | line_size = p - str + 1; | |
b8c9b27d | 1114 | xfree (line_buffer); |
c906108c SS |
1115 | line_buffer = (char *) xmalloc (line_size); |
1116 | } | |
1117 | strncpy (line_buffer, str, p - str); | |
1118 | line_buffer[p - str] = '\0'; | |
1119 | if (islower (line_buffer[0])) | |
1120 | line_buffer[0] = toupper (line_buffer[0]); | |
b6201d44 | 1121 | fputs_filtered (line_buffer, stream); |
c906108c SS |
1122 | } |
1123 | ||
6e381ba0 VP |
1124 | /* Print one-line help for command C. |
1125 | If RECURSE is non-zero, also print one-line descriptions | |
ebcd3b23 | 1126 | of all prefixed subcommands. */ |
6e381ba0 VP |
1127 | static void |
1128 | print_help_for_command (struct cmd_list_element *c, char *prefix, int recurse, | |
1129 | struct ui_file *stream) | |
1130 | { | |
1131 | fprintf_filtered (stream, "%s%s -- ", prefix, c->name); | |
1132 | print_doc_line (stream, c->doc); | |
1133 | fputs_filtered ("\n", stream); | |
1134 | ||
1135 | if (recurse | |
1136 | && c->prefixlist != 0 | |
1137 | && c->abbrev_flag == 0) | |
1138 | /* Subcommands of a prefix command typically have 'all_commands' | |
1139 | as class. If we pass CLASS to recursive invocation, | |
ebcd3b23 | 1140 | most often we won't see anything. */ |
6e381ba0 VP |
1141 | help_cmd_list (*c->prefixlist, all_commands, c->prefixname, 1, stream); |
1142 | } | |
1143 | ||
c906108c SS |
1144 | /* |
1145 | * Implement a help command on command list LIST. | |
1146 | * RECURSE should be non-zero if this should be done recursively on | |
1147 | * all sublists of LIST. | |
1148 | * PREFIX is the prefix to print before each command name. | |
1149 | * STREAM is the stream upon which the output should be written. | |
1150 | * CLASS should be: | |
c5aa993b | 1151 | * A non-negative class number to list only commands in that |
c906108c | 1152 | * class. |
c5aa993b JM |
1153 | * ALL_COMMANDS to list all commands in list. |
1154 | * ALL_CLASSES to list all classes in list. | |
c906108c SS |
1155 | * |
1156 | * Note that RECURSE will be active on *all* sublists, not just the | |
1157 | * ones selected by the criteria above (ie. the selection mechanism | |
1158 | * is at the low level, not the high-level). | |
1159 | */ | |
1160 | void | |
fba45db2 KB |
1161 | help_cmd_list (struct cmd_list_element *list, enum command_class class, |
1162 | char *prefix, int recurse, struct ui_file *stream) | |
c906108c | 1163 | { |
d5b5ac79 | 1164 | struct cmd_list_element *c; |
c906108c SS |
1165 | |
1166 | for (c = list; c; c = c->next) | |
6e381ba0 | 1167 | { |
5aafa1cc PM |
1168 | if (c->abbrev_flag == 0 |
1169 | && (class == all_commands | |
1170 | || (class == all_classes && c->func == NULL) | |
1171 | || (class == c->class && c->func != NULL))) | |
c906108c | 1172 | { |
6e381ba0 | 1173 | print_help_for_command (c, prefix, recurse, stream); |
c906108c | 1174 | } |
adb483fe DJ |
1175 | else if (c->abbrev_flag == 0 && recurse |
1176 | && class == class_user && c->prefixlist != NULL) | |
1177 | /* User-defined commands may be subcommands. */ | |
ebcd3b23 MS |
1178 | help_cmd_list (*c->prefixlist, class, c->prefixname, |
1179 | recurse, stream); | |
c906108c SS |
1180 | } |
1181 | } | |
c906108c | 1182 | \f |
c5aa993b | 1183 | |
c906108c SS |
1184 | /* Search the input clist for 'command'. Return the command if |
1185 | found (or NULL if not), and return the number of commands | |
ebcd3b23 | 1186 | found in nfound. */ |
c906108c SS |
1187 | |
1188 | static struct cmd_list_element * | |
fba45db2 KB |
1189 | find_cmd (char *command, int len, struct cmd_list_element *clist, |
1190 | int ignore_help_classes, int *nfound) | |
c906108c SS |
1191 | { |
1192 | struct cmd_list_element *found, *c; | |
1193 | ||
c5aa993b | 1194 | found = (struct cmd_list_element *) NULL; |
c906108c SS |
1195 | *nfound = 0; |
1196 | for (c = clist; c; c = c->next) | |
1197 | if (!strncmp (command, c->name, len) | |
9f60d481 | 1198 | && (!ignore_help_classes || c->func)) |
c906108c | 1199 | { |
c5aa993b JM |
1200 | found = c; |
1201 | (*nfound)++; | |
1202 | if (c->name[len] == '\0') | |
1203 | { | |
1204 | *nfound = 1; | |
1205 | break; | |
1206 | } | |
c906108c SS |
1207 | } |
1208 | return found; | |
1209 | } | |
1210 | ||
3386243e AS |
1211 | static int |
1212 | find_command_name_length (const char *text) | |
1213 | { | |
1214 | const char *p = text; | |
1215 | ||
1216 | /* Treating underscores as part of command words is important | |
1217 | so that "set args_foo()" doesn't get interpreted as | |
1218 | "set args _foo()". */ | |
ebcd3b23 MS |
1219 | /* Some characters are only used for TUI specific commands. |
1220 | However, they are always allowed for the sake of consistency. | |
1221 | ||
1222 | The XDB compatibility characters are only allowed when using the | |
1223 | right mode because they clash with other GDB commands - | |
1224 | specifically '/' is used as a suffix for print, examine and | |
1225 | display. | |
1226 | ||
1227 | Note that this is larger than the character set allowed when | |
1228 | creating user-defined commands. */ | |
1229 | ||
ed59ded5 DE |
1230 | /* Recognize '!' as a single character command so that, e.g., "!ls" |
1231 | works as expected. */ | |
1232 | if (*p == '!') | |
1233 | return 1; | |
1234 | ||
5aafa1cc | 1235 | while (isalnum (*p) || *p == '-' || *p == '_' |
3386243e | 1236 | /* Characters used by TUI specific commands. */ |
5aafa1cc | 1237 | || *p == '+' || *p == '<' || *p == '>' || *p == '$' |
3386243e | 1238 | /* Characters used for XDB compatibility. */ |
ed59ded5 | 1239 | || (xdb_commands && (*p == '/' || *p == '?'))) |
3386243e AS |
1240 | p++; |
1241 | ||
1242 | return p - text; | |
1243 | } | |
1244 | ||
5a56e9c5 DE |
1245 | /* Return TRUE if NAME is a valid user-defined command name. |
1246 | This is a stricter subset of all gdb commands, | |
1247 | see find_command_name_length. */ | |
1248 | ||
1249 | int | |
1250 | valid_user_defined_cmd_name_p (const char *name) | |
1251 | { | |
1252 | const char *p; | |
1253 | ||
1254 | if (*name == '\0') | |
1255 | return FALSE; | |
1256 | ||
1257 | /* Alas "42" is a legitimate user-defined command. | |
1258 | In the interests of not breaking anything we preserve that. */ | |
1259 | ||
1260 | for (p = name; *p != '\0'; ++p) | |
1261 | { | |
1262 | if (isalnum (*p) | |
1263 | || *p == '-' | |
1264 | || *p == '_') | |
1265 | ; /* Ok. */ | |
1266 | else | |
1267 | return FALSE; | |
1268 | } | |
1269 | ||
1270 | return TRUE; | |
1271 | } | |
1272 | ||
c906108c SS |
1273 | /* This routine takes a line of TEXT and a CLIST in which to start the |
1274 | lookup. When it returns it will have incremented the text pointer past | |
1275 | the section of text it matched, set *RESULT_LIST to point to the list in | |
1276 | which the last word was matched, and will return a pointer to the cmd | |
1277 | list element which the text matches. It will return NULL if no match at | |
1278 | all was possible. It will return -1 (cast appropriately, ick) if ambigous | |
1279 | matches are possible; in this case *RESULT_LIST will be set to point to | |
1280 | the list in which there are ambiguous choices (and *TEXT will be set to | |
1281 | the ambiguous text string). | |
1282 | ||
1283 | If the located command was an abbreviation, this routine returns the base | |
1284 | command of the abbreviation. | |
1285 | ||
1286 | It does no error reporting whatsoever; control will always return | |
1287 | to the superior routine. | |
1288 | ||
1289 | In the case of an ambiguous return (-1), *RESULT_LIST will be set to point | |
1290 | at the prefix_command (ie. the best match) *or* (special case) will be NULL | |
1291 | if no prefix command was ever found. For example, in the case of "info a", | |
1292 | "info" matches without ambiguity, but "a" could be "args" or "address", so | |
1293 | *RESULT_LIST is set to the cmd_list_element for "info". So in this case | |
1294 | RESULT_LIST should not be interpeted as a pointer to the beginning of a | |
1295 | list; it simply points to a specific command. In the case of an ambiguous | |
1296 | return *TEXT is advanced past the last non-ambiguous prefix (e.g. | |
1297 | "info t" can be "info types" or "info target"; upon return *TEXT has been | |
1298 | advanced past "info "). | |
1299 | ||
1300 | If RESULT_LIST is NULL, don't set *RESULT_LIST (but don't otherwise | |
1301 | affect the operation). | |
1302 | ||
1303 | This routine does *not* modify the text pointed to by TEXT. | |
c5aa993b | 1304 | |
c906108c SS |
1305 | If IGNORE_HELP_CLASSES is nonzero, ignore any command list elements which |
1306 | are actually help classes rather than commands (i.e. the function field of | |
1307 | the struct cmd_list_element is NULL). */ | |
1308 | ||
1309 | struct cmd_list_element * | |
fba45db2 KB |
1310 | lookup_cmd_1 (char **text, struct cmd_list_element *clist, |
1311 | struct cmd_list_element **result_list, int ignore_help_classes) | |
c906108c | 1312 | { |
3386243e | 1313 | char *command; |
c906108c SS |
1314 | int len, tmp, nfound; |
1315 | struct cmd_list_element *found, *c; | |
56382845 | 1316 | char *line = *text; |
c906108c SS |
1317 | |
1318 | while (**text == ' ' || **text == '\t') | |
1319 | (*text)++; | |
1320 | ||
3386243e AS |
1321 | /* Identify the name of the command. */ |
1322 | len = find_command_name_length (*text); | |
c906108c SS |
1323 | |
1324 | /* If nothing but whitespace, return 0. */ | |
3386243e | 1325 | if (len == 0) |
c906108c | 1326 | return 0; |
c5aa993b | 1327 | |
c906108c | 1328 | /* *text and p now bracket the first command word to lookup (and |
ebcd3b23 | 1329 | it's length is len). We copy this into a local temporary. */ |
c906108c SS |
1330 | |
1331 | ||
1332 | command = (char *) alloca (len + 1); | |
22ad7fee | 1333 | memcpy (command, *text, len); |
c906108c SS |
1334 | command[len] = '\0'; |
1335 | ||
1336 | /* Look it up. */ | |
1337 | found = 0; | |
1338 | nfound = 0; | |
c5aa993b | 1339 | found = find_cmd (command, len, clist, ignore_help_classes, &nfound); |
c906108c | 1340 | |
ebcd3b23 MS |
1341 | /* We didn't find the command in the entered case, so lower case it |
1342 | and search again. */ | |
c906108c SS |
1343 | if (!found || nfound == 0) |
1344 | { | |
1345 | for (tmp = 0; tmp < len; tmp++) | |
c5aa993b JM |
1346 | { |
1347 | char x = command[tmp]; | |
cdb27c12 | 1348 | |
c5aa993b JM |
1349 | command[tmp] = isupper (x) ? tolower (x) : x; |
1350 | } | |
1351 | found = find_cmd (command, len, clist, ignore_help_classes, &nfound); | |
c906108c SS |
1352 | } |
1353 | ||
1354 | /* If nothing matches, we have a simple failure. */ | |
1355 | if (nfound == 0) | |
1356 | return 0; | |
1357 | ||
1358 | if (nfound > 1) | |
1359 | { | |
1360 | if (result_list != NULL) | |
1361 | /* Will be modified in calling routine | |
1362 | if we know what the prefix command is. */ | |
c5aa993b | 1363 | *result_list = 0; |
1427fe5e | 1364 | return CMD_LIST_AMBIGUOUS; /* Ambiguous. */ |
c906108c SS |
1365 | } |
1366 | ||
ebcd3b23 | 1367 | /* We've matched something on this list. Move text pointer forward. */ |
c906108c | 1368 | |
3386243e | 1369 | *text += len; |
c906108c | 1370 | |
c906108c | 1371 | if (found->cmd_pointer) |
56382845 | 1372 | { |
ebcd3b23 MS |
1373 | /* We drop the alias (abbreviation) in favor of the command it |
1374 | is pointing to. If the alias is deprecated, though, we need to | |
56382845 FN |
1375 | warn the user about it before we drop it. Note that while we |
1376 | are warning about the alias, we may also warn about the command | |
1377 | itself and we will adjust the appropriate DEPRECATED_WARN_USER | |
ebcd3b23 | 1378 | flags. */ |
56382845 FN |
1379 | |
1380 | if (found->flags & DEPRECATED_WARN_USER) | |
938f5214 | 1381 | deprecated_cmd_warning (&line); |
56382845 FN |
1382 | found = found->cmd_pointer; |
1383 | } | |
c906108c SS |
1384 | /* If we found a prefix command, keep looking. */ |
1385 | ||
1386 | if (found->prefixlist) | |
1387 | { | |
1388 | c = lookup_cmd_1 (text, *found->prefixlist, result_list, | |
1389 | ignore_help_classes); | |
1390 | if (!c) | |
1391 | { | |
1392 | /* Didn't find anything; this is as far as we got. */ | |
1393 | if (result_list != NULL) | |
1394 | *result_list = clist; | |
1395 | return found; | |
1396 | } | |
1427fe5e | 1397 | else if (c == CMD_LIST_AMBIGUOUS) |
c906108c | 1398 | { |
ebcd3b23 MS |
1399 | /* We've gotten this far properly, but the next step is |
1400 | ambiguous. We need to set the result list to the best | |
c906108c SS |
1401 | we've found (if an inferior hasn't already set it). */ |
1402 | if (result_list != NULL) | |
1403 | if (!*result_list) | |
ebcd3b23 | 1404 | /* This used to say *result_list = *found->prefixlist. |
c5aa993b | 1405 | If that was correct, need to modify the documentation |
ebcd3b23 MS |
1406 | at the top of this function to clarify what is |
1407 | supposed to be going on. */ | |
c906108c SS |
1408 | *result_list = found; |
1409 | return c; | |
1410 | } | |
1411 | else | |
1412 | { | |
1413 | /* We matched! */ | |
1414 | return c; | |
1415 | } | |
1416 | } | |
1417 | else | |
1418 | { | |
1419 | if (result_list != NULL) | |
1420 | *result_list = clist; | |
1421 | return found; | |
1422 | } | |
1423 | } | |
1424 | ||
1425 | /* All this hair to move the space to the front of cmdtype */ | |
1426 | ||
1427 | static void | |
fba45db2 | 1428 | undef_cmd_error (char *cmdtype, char *q) |
c906108c | 1429 | { |
8a3fe4f8 | 1430 | error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."), |
c5aa993b JM |
1431 | cmdtype, |
1432 | q, | |
1433 | *cmdtype ? " " : "", | |
823ca731 | 1434 | (int) strlen (cmdtype) - 1, |
c5aa993b | 1435 | cmdtype); |
c906108c SS |
1436 | } |
1437 | ||
1438 | /* Look up the contents of *LINE as a command in the command list LIST. | |
1439 | LIST is a chain of struct cmd_list_element's. | |
1440 | If it is found, return the struct cmd_list_element for that command | |
1441 | and update *LINE to point after the command name, at the first argument. | |
1442 | If not found, call error if ALLOW_UNKNOWN is zero | |
1443 | otherwise (or if error returns) return zero. | |
1444 | Call error if specified command is ambiguous, | |
1445 | unless ALLOW_UNKNOWN is negative. | |
1446 | CMDTYPE precedes the word "command" in the error message. | |
1447 | ||
1448 | If INGNORE_HELP_CLASSES is nonzero, ignore any command list | |
1449 | elements which are actually help classes rather than commands (i.e. | |
1450 | the function field of the struct cmd_list_element is 0). */ | |
1451 | ||
1452 | struct cmd_list_element * | |
fba45db2 KB |
1453 | lookup_cmd (char **line, struct cmd_list_element *list, char *cmdtype, |
1454 | int allow_unknown, int ignore_help_classes) | |
c906108c SS |
1455 | { |
1456 | struct cmd_list_element *last_list = 0; | |
3cebf8d8 | 1457 | struct cmd_list_element *c; |
c64601c7 FN |
1458 | |
1459 | /* Note: Do not remove trailing whitespace here because this | |
1460 | would be wrong for complete_command. Jim Kingdon */ | |
c5aa993b | 1461 | |
3cebf8d8 MS |
1462 | if (!*line) |
1463 | error (_("Lack of needed %scommand"), cmdtype); | |
1464 | ||
1465 | c = lookup_cmd_1 (line, list, &last_list, ignore_help_classes); | |
1466 | ||
c906108c SS |
1467 | if (!c) |
1468 | { | |
1469 | if (!allow_unknown) | |
1470 | { | |
3cebf8d8 MS |
1471 | char *q; |
1472 | int len = find_command_name_length (*line); | |
c906108c | 1473 | |
3cebf8d8 MS |
1474 | q = (char *) alloca (len + 1); |
1475 | strncpy (q, *line, len); | |
1476 | q[len] = '\0'; | |
1477 | undef_cmd_error (cmdtype, q); | |
c906108c SS |
1478 | } |
1479 | else | |
1480 | return 0; | |
1481 | } | |
1427fe5e | 1482 | else if (c == CMD_LIST_AMBIGUOUS) |
c906108c SS |
1483 | { |
1484 | /* Ambigous. Local values should be off prefixlist or called | |
c5aa993b | 1485 | values. */ |
c906108c SS |
1486 | int local_allow_unknown = (last_list ? last_list->allow_unknown : |
1487 | allow_unknown); | |
1488 | char *local_cmdtype = last_list ? last_list->prefixname : cmdtype; | |
1489 | struct cmd_list_element *local_list = | |
cdb27c12 | 1490 | (last_list ? *(last_list->prefixlist) : list); |
c5aa993b | 1491 | |
c906108c SS |
1492 | if (local_allow_unknown < 0) |
1493 | { | |
1494 | if (last_list) | |
1495 | return last_list; /* Found something. */ | |
1496 | else | |
1497 | return 0; /* Found nothing. */ | |
1498 | } | |
1499 | else | |
1500 | { | |
1501 | /* Report as error. */ | |
1502 | int amb_len; | |
1503 | char ambbuf[100]; | |
1504 | ||
1505 | for (amb_len = 0; | |
1506 | ((*line)[amb_len] && (*line)[amb_len] != ' ' | |
1507 | && (*line)[amb_len] != '\t'); | |
1508 | amb_len++) | |
1509 | ; | |
c5aa993b | 1510 | |
c906108c SS |
1511 | ambbuf[0] = 0; |
1512 | for (c = local_list; c; c = c->next) | |
1513 | if (!strncmp (*line, c->name, amb_len)) | |
1514 | { | |
9a2b4c1b MS |
1515 | if (strlen (ambbuf) + strlen (c->name) + 6 |
1516 | < (int) sizeof ambbuf) | |
c906108c SS |
1517 | { |
1518 | if (strlen (ambbuf)) | |
1519 | strcat (ambbuf, ", "); | |
1520 | strcat (ambbuf, c->name); | |
1521 | } | |
1522 | else | |
1523 | { | |
1524 | strcat (ambbuf, ".."); | |
1525 | break; | |
1526 | } | |
1527 | } | |
8a3fe4f8 | 1528 | error (_("Ambiguous %scommand \"%s\": %s."), local_cmdtype, |
c906108c SS |
1529 | *line, ambbuf); |
1530 | return 0; /* lint */ | |
1531 | } | |
1532 | } | |
1533 | else | |
1534 | { | |
1535 | /* We've got something. It may still not be what the caller | |
1536 | wants (if this command *needs* a subcommand). */ | |
1537 | while (**line == ' ' || **line == '\t') | |
1538 | (*line)++; | |
1539 | ||
1540 | if (c->prefixlist && **line && !c->allow_unknown) | |
1541 | undef_cmd_error (c->prefixname, *line); | |
1542 | ||
1543 | /* Seems to be what he wants. Return it. */ | |
1544 | return c; | |
1545 | } | |
1546 | return 0; | |
1547 | } | |
c5aa993b | 1548 | |
ebcd3b23 MS |
1549 | /* We are here presumably because an alias or command in *TEXT is |
1550 | deprecated and a warning message should be generated. This | |
1551 | function decodes *TEXT and potentially generates a warning message | |
1552 | as outlined below. | |
56382845 FN |
1553 | |
1554 | Example for 'set endian big' which has a fictitious alias 'seb'. | |
1555 | ||
1556 | If alias wasn't used in *TEXT, and the command is deprecated: | |
1557 | "warning: 'set endian big' is deprecated." | |
1558 | ||
1559 | If alias was used, and only the alias is deprecated: | |
1560 | "warning: 'seb' an alias for the command 'set endian big' is deprecated." | |
1561 | ||
ebcd3b23 MS |
1562 | If alias was used and command is deprecated (regardless of whether |
1563 | the alias itself is deprecated: | |
56382845 FN |
1564 | |
1565 | "warning: 'set endian big' (seb) is deprecated." | |
1566 | ||
1567 | After the message has been sent, clear the appropriate flags in the | |
1568 | command and/or the alias so the user is no longer bothered. | |
1569 | ||
1570 | */ | |
1571 | void | |
1572 | deprecated_cmd_warning (char **text) | |
1573 | { | |
1574 | struct cmd_list_element *alias = NULL; | |
1575 | struct cmd_list_element *prefix_cmd = NULL; | |
1576 | struct cmd_list_element *cmd = NULL; | |
edefbb7c | 1577 | |
56382845 | 1578 | if (!lookup_cmd_composition (*text, &alias, &prefix_cmd, &cmd)) |
ebcd3b23 | 1579 | /* Return if text doesn't evaluate to a command. */ |
56382845 FN |
1580 | return; |
1581 | ||
1582 | if (!((alias ? (alias->flags & DEPRECATED_WARN_USER) : 0) | |
1583 | || (cmd->flags & DEPRECATED_WARN_USER) ) ) | |
ebcd3b23 | 1584 | /* Return if nothing is deprecated. */ |
56382845 FN |
1585 | return; |
1586 | ||
1587 | printf_filtered ("Warning:"); | |
1588 | ||
1589 | if (alias && !(cmd->flags & CMD_DEPRECATED)) | |
1590 | printf_filtered (" '%s', an alias for the", alias->name); | |
1591 | ||
1592 | printf_filtered (" command '"); | |
1593 | ||
1594 | if (prefix_cmd) | |
1595 | printf_filtered ("%s", prefix_cmd->prefixname); | |
1596 | ||
1597 | printf_filtered ("%s", cmd->name); | |
1598 | ||
1599 | if (alias && (cmd->flags & CMD_DEPRECATED)) | |
1600 | printf_filtered ("' (%s) is deprecated.\n", alias->name); | |
1601 | else | |
1602 | printf_filtered ("' is deprecated.\n"); | |
1603 | ||
1604 | ||
ebcd3b23 MS |
1605 | /* If it is only the alias that is deprecated, we want to indicate |
1606 | the new alias, otherwise we'll indicate the new command. */ | |
56382845 FN |
1607 | |
1608 | if (alias && !(cmd->flags & CMD_DEPRECATED)) | |
1609 | { | |
1610 | if (alias->replacement) | |
cdb27c12 | 1611 | printf_filtered ("Use '%s'.\n\n", alias->replacement); |
56382845 | 1612 | else |
cdb27c12 | 1613 | printf_filtered ("No alternative known.\n\n"); |
56382845 FN |
1614 | } |
1615 | else | |
1616 | { | |
1617 | if (cmd->replacement) | |
cdb27c12 | 1618 | printf_filtered ("Use '%s'.\n\n", cmd->replacement); |
56382845 | 1619 | else |
cdb27c12 | 1620 | printf_filtered ("No alternative known.\n\n"); |
56382845 FN |
1621 | } |
1622 | ||
ebcd3b23 | 1623 | /* We've warned you, now we'll keep quiet. */ |
56382845 FN |
1624 | if (alias) |
1625 | alias->flags &= ~DEPRECATED_WARN_USER; | |
1626 | ||
1627 | cmd->flags &= ~DEPRECATED_WARN_USER; | |
1628 | } | |
1629 | ||
1630 | ||
ebcd3b23 | 1631 | /* Look up the contents of LINE as a command in the command list 'cmdlist'. |
56382845 FN |
1632 | Return 1 on success, 0 on failure. |
1633 | ||
1634 | If LINE refers to an alias, *alias will point to that alias. | |
1635 | ||
177b42fe | 1636 | If LINE is a postfix command (i.e. one that is preceded by a prefix |
56382845 FN |
1637 | command) set *prefix_cmd. |
1638 | ||
1639 | Set *cmd to point to the command LINE indicates. | |
1640 | ||
1641 | If any of *alias, *prefix_cmd, or *cmd cannot be determined or do not | |
1642 | exist, they are NULL when we return. | |
1643 | ||
1644 | */ | |
1645 | int | |
1646 | lookup_cmd_composition (char *text, | |
1647 | struct cmd_list_element **alias, | |
1648 | struct cmd_list_element **prefix_cmd, | |
1649 | struct cmd_list_element **cmd) | |
1650 | { | |
3386243e | 1651 | char *command; |
56382845 FN |
1652 | int len, tmp, nfound; |
1653 | struct cmd_list_element *cur_list; | |
1654 | struct cmd_list_element *prev_cmd; | |
cdb27c12 | 1655 | |
56382845 FN |
1656 | *alias = NULL; |
1657 | *prefix_cmd = NULL; | |
1658 | *cmd = NULL; | |
1659 | ||
1660 | cur_list = cmdlist; | |
1661 | ||
1662 | while (1) | |
1663 | { | |
7a9dd1b2 | 1664 | /* Go through as many command lists as we need to, |
ebcd3b23 | 1665 | to find the command TEXT refers to. */ |
56382845 FN |
1666 | |
1667 | prev_cmd = *cmd; | |
1668 | ||
1669 | while (*text == ' ' || *text == '\t') | |
cdb27c12 | 1670 | (text)++; |
56382845 | 1671 | |
3386243e AS |
1672 | /* Identify the name of the command. */ |
1673 | len = find_command_name_length (text); | |
56382845 FN |
1674 | |
1675 | /* If nothing but whitespace, return. */ | |
3386243e AS |
1676 | if (len == 0) |
1677 | return 0; | |
56382845 | 1678 | |
cdb27c12 MS |
1679 | /* Text is the start of the first command word to lookup (and |
1680 | it's length is len). We copy this into a local temporary. */ | |
56382845 FN |
1681 | |
1682 | command = (char *) alloca (len + 1); | |
22ad7fee | 1683 | memcpy (command, text, len); |
56382845 FN |
1684 | command[len] = '\0'; |
1685 | ||
1686 | /* Look it up. */ | |
1687 | *cmd = 0; | |
1688 | nfound = 0; | |
1689 | *cmd = find_cmd (command, len, cur_list, 1, &nfound); | |
1690 | ||
ebcd3b23 MS |
1691 | /* We didn't find the command in the entered case, so lower case |
1692 | it and search again. | |
56382845 FN |
1693 | */ |
1694 | if (!*cmd || nfound == 0) | |
cdb27c12 MS |
1695 | { |
1696 | for (tmp = 0; tmp < len; tmp++) | |
1697 | { | |
1698 | char x = command[tmp]; | |
1699 | ||
1700 | command[tmp] = isupper (x) ? tolower (x) : x; | |
1701 | } | |
1702 | *cmd = find_cmd (command, len, cur_list, 1, &nfound); | |
1703 | } | |
56382845 | 1704 | |
1427fe5e | 1705 | if (*cmd == CMD_LIST_AMBIGUOUS) |
cdb27c12 MS |
1706 | { |
1707 | return 0; /* ambiguous */ | |
1708 | } | |
56382845 FN |
1709 | |
1710 | if (*cmd == NULL) | |
cdb27c12 | 1711 | return 0; /* nothing found */ |
56382845 | 1712 | else |
cdb27c12 MS |
1713 | { |
1714 | if ((*cmd)->cmd_pointer) | |
1715 | { | |
ebcd3b23 MS |
1716 | /* cmd was actually an alias, we note that an alias was |
1717 | used (by assigning *alais) and we set *cmd. */ | |
cdb27c12 MS |
1718 | *alias = *cmd; |
1719 | *cmd = (*cmd)->cmd_pointer; | |
1720 | } | |
1721 | *prefix_cmd = prev_cmd; | |
1722 | } | |
56382845 | 1723 | if ((*cmd)->prefixlist) |
cdb27c12 | 1724 | cur_list = *(*cmd)->prefixlist; |
56382845 | 1725 | else |
cdb27c12 | 1726 | return 1; |
56382845 | 1727 | |
3386243e | 1728 | text += len; |
56382845 FN |
1729 | } |
1730 | } | |
1731 | ||
c906108c SS |
1732 | /* Helper function for SYMBOL_COMPLETION_FUNCTION. */ |
1733 | ||
1734 | /* Return a vector of char pointers which point to the different | |
ebcd3b23 | 1735 | possible completions in LIST of TEXT. |
c906108c SS |
1736 | |
1737 | WORD points in the same buffer as TEXT, and completions should be | |
ebcd3b23 MS |
1738 | returned relative to this position. For example, suppose TEXT is |
1739 | "foo" and we want to complete to "foobar". If WORD is "oo", return | |
c906108c SS |
1740 | "oobar"; if WORD is "baz/foo", return "baz/foobar". */ |
1741 | ||
49c4e619 | 1742 | VEC (char_ptr) * |
ace21957 MF |
1743 | complete_on_cmdlist (struct cmd_list_element *list, char *text, char *word, |
1744 | int ignore_help_classes) | |
c906108c SS |
1745 | { |
1746 | struct cmd_list_element *ptr; | |
49c4e619 | 1747 | VEC (char_ptr) *matchlist = NULL; |
c906108c | 1748 | int textlen = strlen (text); |
3f172e24 TT |
1749 | int pass; |
1750 | int saw_deprecated_match = 0; | |
c906108c | 1751 | |
3f172e24 TT |
1752 | /* We do one or two passes. In the first pass, we skip deprecated |
1753 | commands. If we see no matching commands in the first pass, and | |
1754 | if we did happen to see a matching deprecated command, we do | |
1755 | another loop to collect those. */ | |
49c4e619 | 1756 | for (pass = 0; matchlist == 0 && pass < 2; ++pass) |
3f172e24 TT |
1757 | { |
1758 | for (ptr = list; ptr; ptr = ptr->next) | |
1759 | if (!strncmp (ptr->name, text, textlen) | |
1760 | && !ptr->abbrev_flag | |
ace21957 | 1761 | && (!ignore_help_classes || ptr->func |
3f172e24 | 1762 | || ptr->prefixlist)) |
c906108c | 1763 | { |
49c4e619 TT |
1764 | char *match; |
1765 | ||
3f172e24 TT |
1766 | if (pass == 0) |
1767 | { | |
1768 | if ((ptr->flags & CMD_DEPRECATED) != 0) | |
1769 | { | |
1770 | saw_deprecated_match = 1; | |
1771 | continue; | |
1772 | } | |
1773 | } | |
c906108c | 1774 | |
49c4e619 | 1775 | match = (char *) xmalloc (strlen (word) + strlen (ptr->name) + 1); |
3f172e24 | 1776 | if (word == text) |
49c4e619 | 1777 | strcpy (match, ptr->name); |
3f172e24 TT |
1778 | else if (word > text) |
1779 | { | |
1780 | /* Return some portion of ptr->name. */ | |
49c4e619 | 1781 | strcpy (match, ptr->name + (word - text)); |
3f172e24 TT |
1782 | } |
1783 | else | |
1784 | { | |
1785 | /* Return some of text plus ptr->name. */ | |
49c4e619 TT |
1786 | strncpy (match, word, text - word); |
1787 | match[text - word] = '\0'; | |
1788 | strcat (match, ptr->name); | |
3f172e24 | 1789 | } |
49c4e619 | 1790 | VEC_safe_push (char_ptr, matchlist, match); |
c906108c | 1791 | } |
3f172e24 TT |
1792 | /* If we saw no matching deprecated commands in the first pass, |
1793 | just bail out. */ | |
1794 | if (!saw_deprecated_match) | |
1795 | break; | |
1796 | } | |
c906108c | 1797 | |
c906108c SS |
1798 | return matchlist; |
1799 | } | |
1800 | ||
1801 | /* Helper function for SYMBOL_COMPLETION_FUNCTION. */ | |
1802 | ||
1803 | /* Return a vector of char pointers which point to the different | |
ebcd3b23 | 1804 | possible completions in CMD of TEXT. |
c906108c SS |
1805 | |
1806 | WORD points in the same buffer as TEXT, and completions should be | |
1807 | returned relative to this position. For example, suppose TEXT is "foo" | |
1808 | and we want to complete to "foobar". If WORD is "oo", return | |
1809 | "oobar"; if WORD is "baz/foo", return "baz/foobar". */ | |
1810 | ||
49c4e619 | 1811 | VEC (char_ptr) * |
40478521 | 1812 | complete_on_enum (const char *const *enumlist, |
53904c9e AC |
1813 | char *text, |
1814 | char *word) | |
c906108c | 1815 | { |
49c4e619 | 1816 | VEC (char_ptr) *matchlist = NULL; |
c906108c SS |
1817 | int textlen = strlen (text); |
1818 | int i; | |
53904c9e | 1819 | const char *name; |
c906108c | 1820 | |
c906108c SS |
1821 | for (i = 0; (name = enumlist[i]) != NULL; i++) |
1822 | if (strncmp (name, text, textlen) == 0) | |
1823 | { | |
49c4e619 | 1824 | char *match; |
c906108c | 1825 | |
49c4e619 | 1826 | match = (char *) xmalloc (strlen (word) + strlen (name) + 1); |
c906108c | 1827 | if (word == text) |
49c4e619 | 1828 | strcpy (match, name); |
c906108c SS |
1829 | else if (word > text) |
1830 | { | |
1831 | /* Return some portion of name. */ | |
49c4e619 | 1832 | strcpy (match, name + (word - text)); |
c906108c SS |
1833 | } |
1834 | else | |
1835 | { | |
1836 | /* Return some of text plus name. */ | |
49c4e619 TT |
1837 | strncpy (match, word, text - word); |
1838 | match[text - word] = '\0'; | |
1839 | strcat (match, name); | |
c906108c | 1840 | } |
49c4e619 | 1841 | VEC_safe_push (char_ptr, matchlist, match); |
c906108c SS |
1842 | } |
1843 | ||
c906108c SS |
1844 | return matchlist; |
1845 | } | |
1846 | ||
f436dd25 | 1847 | |
ebcd3b23 | 1848 | /* Check function pointer. */ |
f436dd25 MH |
1849 | int |
1850 | cmd_func_p (struct cmd_list_element *cmd) | |
1851 | { | |
1852 | return (cmd->func != NULL); | |
1853 | } | |
1854 | ||
1855 | ||
ebcd3b23 | 1856 | /* Call the command function. */ |
f436dd25 MH |
1857 | void |
1858 | cmd_func (struct cmd_list_element *cmd, char *args, int from_tty) | |
1859 | { | |
1860 | if (cmd_func_p (cmd)) | |
1861 | (*cmd->func) (cmd, args, from_tty); | |
1862 | else | |
8a3fe4f8 | 1863 | error (_("Invalid command")); |
f436dd25 | 1864 | } |