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