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