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