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