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