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