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