cli-decode.c revision 1.1.1.8 1 1.1 christos /* Handle lists of commands, their decoding and documentation, for GDB.
2 1.1 christos
3 1.1.1.8 christos Copyright (C) 1986-2023 Free Software Foundation, Inc.
4 1.1 christos
5 1.1 christos This program is free software; you can redistribute it and/or modify
6 1.1 christos it under the terms of the GNU General Public License as published by
7 1.1 christos the Free Software Foundation; either version 3 of the License, or
8 1.1 christos (at your option) any later version.
9 1.1 christos
10 1.1 christos This program is distributed in the hope that it will be useful,
11 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
12 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 1.1 christos GNU General Public License for more details.
14 1.1 christos
15 1.1 christos You should have received a copy of the GNU General Public License
16 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 1.1 christos
18 1.1 christos #include "defs.h"
19 1.1 christos #include "symtab.h"
20 1.1 christos #include <ctype.h>
21 1.1.1.8 christos #include "gdbsupport/gdb_regex.h"
22 1.1 christos #include "completer.h"
23 1.1 christos #include "ui-out.h"
24 1.1 christos #include "cli/cli-cmds.h"
25 1.1 christos #include "cli/cli-decode.h"
26 1.1.1.7 christos #include "cli/cli-style.h"
27 1.1.1.7 christos #include "gdbsupport/gdb_optional.h"
28 1.1 christos
29 1.1 christos /* Prototypes for local functions. */
30 1.1 christos
31 1.1 christos static void undef_cmd_error (const char *, const char *);
32 1.1 christos
33 1.1.1.8 christos static cmd_list_element::aliases_list_type delete_cmd
34 1.1.1.8 christos (const char *name, cmd_list_element **list, cmd_list_element **prehook,
35 1.1.1.8 christos cmd_list_element **prehookee, cmd_list_element **posthook,
36 1.1.1.8 christos cmd_list_element **posthookee);
37 1.1 christos
38 1.1 christos static struct cmd_list_element *find_cmd (const char *command,
39 1.1 christos int len,
40 1.1 christos struct cmd_list_element *clist,
41 1.1 christos int ignore_help_classes,
42 1.1 christos int *nfound);
43 1.1 christos
44 1.1.1.7 christos static void help_cmd_list (struct cmd_list_element *list,
45 1.1.1.7 christos enum command_class theclass,
46 1.1.1.7 christos bool recurse,
47 1.1.1.7 christos struct ui_file *stream);
48 1.1.1.7 christos
49 1.1 christos static void help_all (struct ui_file *stream);
50 1.1 christos
51 1.1.1.8 christos static int lookup_cmd_composition_1 (const char *text,
52 1.1.1.8 christos struct cmd_list_element **alias,
53 1.1.1.8 christos struct cmd_list_element **prefix_cmd,
54 1.1.1.8 christos struct cmd_list_element **cmd,
55 1.1.1.8 christos struct cmd_list_element *cur_list);
56 1.1.1.8 christos
57 1.1.1.8 christos /* Look up a command whose 'subcommands' field is SUBCOMMANDS. Return the
58 1.1.1.8 christos command if found, otherwise return NULL. */
59 1.1 christos
60 1.1 christos static struct cmd_list_element *
61 1.1.1.8 christos lookup_cmd_with_subcommands (cmd_list_element **subcommands,
62 1.1.1.8 christos cmd_list_element *list)
63 1.1 christos {
64 1.1 christos struct cmd_list_element *p = NULL;
65 1.1 christos
66 1.1 christos for (p = list; p != NULL; p = p->next)
67 1.1 christos {
68 1.1 christos struct cmd_list_element *q;
69 1.1 christos
70 1.1.1.8 christos if (!p->is_prefix ())
71 1.1 christos continue;
72 1.1.1.8 christos
73 1.1.1.8 christos else if (p->subcommands == subcommands)
74 1.1.1.7 christos {
75 1.1.1.7 christos /* If we found an alias, we must return the aliased
76 1.1.1.7 christos command. */
77 1.1.1.8 christos return p->is_alias () ? p->alias_target : p;
78 1.1.1.7 christos }
79 1.1 christos
80 1.1.1.8 christos q = lookup_cmd_with_subcommands (subcommands, *(p->subcommands));
81 1.1 christos if (q != NULL)
82 1.1 christos return q;
83 1.1 christos }
84 1.1 christos
85 1.1 christos return NULL;
86 1.1 christos }
87 1.1 christos
88 1.1 christos static void
89 1.1.1.8 christos print_help_for_command (const cmd_list_element &c,
90 1.1.1.7 christos bool recurse, struct ui_file *stream);
91 1.1 christos
92 1.1 christos static void
93 1.1.1.8 christos do_simple_func (const char *args, int from_tty, cmd_list_element *c)
94 1.1 christos {
95 1.1.1.8 christos c->function.simple_func (args, from_tty);
96 1.1 christos }
97 1.1 christos
98 1.1.1.6 christos static void
99 1.1.1.8 christos set_cmd_simple_func (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
100 1.1 christos {
101 1.1.1.8 christos if (simple_func == NULL)
102 1.1 christos cmd->func = NULL;
103 1.1 christos else
104 1.1.1.8 christos cmd->func = do_simple_func;
105 1.1 christos
106 1.1.1.8 christos cmd->function.simple_func = simple_func;
107 1.1 christos }
108 1.1 christos
109 1.1 christos int
110 1.1.1.8 christos cmd_simple_func_eq (struct cmd_list_element *cmd, cmd_simple_func_ftype *simple_func)
111 1.1 christos {
112 1.1.1.8 christos return (cmd->func == do_simple_func
113 1.1.1.8 christos && cmd->function.simple_func == simple_func);
114 1.1 christos }
115 1.1 christos
116 1.1 christos void
117 1.1 christos set_cmd_completer (struct cmd_list_element *cmd, completer_ftype *completer)
118 1.1 christos {
119 1.1 christos cmd->completer = completer; /* Ok. */
120 1.1 christos }
121 1.1 christos
122 1.1.1.2 christos /* See definition in commands.h. */
123 1.1.1.2 christos
124 1.1.1.2 christos void
125 1.1.1.2 christos set_cmd_completer_handle_brkchars (struct cmd_list_element *cmd,
126 1.1.1.6 christos completer_handle_brkchars_ftype *func)
127 1.1.1.2 christos {
128 1.1.1.6 christos cmd->completer_handle_brkchars = func;
129 1.1.1.2 christos }
130 1.1.1.2 christos
131 1.1.1.8 christos std::string
132 1.1.1.8 christos cmd_list_element::prefixname () const
133 1.1.1.8 christos {
134 1.1.1.8 christos if (!this->is_prefix ())
135 1.1.1.8 christos /* Not a prefix command. */
136 1.1.1.8 christos return "";
137 1.1.1.8 christos
138 1.1.1.8 christos std::string prefixname;
139 1.1.1.8 christos if (this->prefix != nullptr)
140 1.1.1.8 christos prefixname = this->prefix->prefixname ();
141 1.1.1.8 christos
142 1.1.1.8 christos prefixname += this->name;
143 1.1.1.8 christos prefixname += " ";
144 1.1.1.8 christos
145 1.1.1.8 christos return prefixname;
146 1.1.1.8 christos }
147 1.1.1.8 christos
148 1.1.1.8 christos /* See cli/cli-decode.h. */
149 1.1.1.8 christos
150 1.1.1.8 christos std::vector<std::string>
151 1.1.1.8 christos cmd_list_element::command_components () const
152 1.1.1.8 christos {
153 1.1.1.8 christos std::vector<std::string> result;
154 1.1.1.8 christos
155 1.1.1.8 christos if (this->prefix != nullptr)
156 1.1.1.8 christos result = this->prefix->command_components ();
157 1.1.1.8 christos
158 1.1.1.8 christos result.emplace_back (std::string (this->name));
159 1.1.1.8 christos return result;
160 1.1.1.8 christos }
161 1.1.1.8 christos
162 1.1 christos /* Add element named NAME.
163 1.1 christos Space for NAME and DOC must be allocated by the caller.
164 1.1 christos CLASS is the top level category into which commands are broken down
165 1.1 christos for "help" purposes.
166 1.1 christos FUN should be the function to execute the command;
167 1.1 christos it will get a character string as argument, with leading
168 1.1 christos and trailing blanks already eliminated.
169 1.1 christos
170 1.1 christos DOC is a documentation string for the command.
171 1.1 christos Its first line should be a complete sentence.
172 1.1 christos It should start with ? for a command that is an abbreviation
173 1.1 christos or with * for a command that most users don't need to know about.
174 1.1 christos
175 1.1 christos Add this command to command list *LIST.
176 1.1 christos
177 1.1 christos Returns a pointer to the added command (not necessarily the head
178 1.1 christos of *LIST). */
179 1.1 christos
180 1.1.1.6 christos static struct cmd_list_element *
181 1.1.1.6 christos do_add_cmd (const char *name, enum command_class theclass,
182 1.1.1.6 christos const char *doc, struct cmd_list_element **list)
183 1.1 christos {
184 1.1.1.6 christos struct cmd_list_element *c = new struct cmd_list_element (name, theclass,
185 1.1.1.6 christos doc);
186 1.1 christos
187 1.1 christos /* Turn each alias of the old command into an alias of the new
188 1.1 christos command. */
189 1.1 christos c->aliases = delete_cmd (name, list, &c->hook_pre, &c->hookee_pre,
190 1.1 christos &c->hook_post, &c->hookee_post);
191 1.1.1.8 christos
192 1.1.1.8 christos for (cmd_list_element &alias : c->aliases)
193 1.1.1.8 christos alias.alias_target = c;
194 1.1.1.8 christos
195 1.1 christos if (c->hook_pre)
196 1.1 christos c->hook_pre->hookee_pre = c;
197 1.1.1.8 christos
198 1.1 christos if (c->hookee_pre)
199 1.1 christos c->hookee_pre->hook_pre = c;
200 1.1.1.8 christos
201 1.1 christos if (c->hook_post)
202 1.1 christos c->hook_post->hookee_post = c;
203 1.1.1.8 christos
204 1.1 christos if (c->hookee_post)
205 1.1 christos c->hookee_post->hook_post = c;
206 1.1 christos
207 1.1 christos if (*list == NULL || strcmp ((*list)->name, name) >= 0)
208 1.1 christos {
209 1.1 christos c->next = *list;
210 1.1 christos *list = c;
211 1.1 christos }
212 1.1 christos else
213 1.1 christos {
214 1.1.1.8 christos cmd_list_element *p = *list;
215 1.1 christos while (p->next && strcmp (p->next->name, name) <= 0)
216 1.1 christos {
217 1.1 christos p = p->next;
218 1.1 christos }
219 1.1 christos c->next = p->next;
220 1.1 christos p->next = c;
221 1.1 christos }
222 1.1 christos
223 1.1.1.7 christos /* Search the prefix cmd of C, and assigns it to C->prefix.
224 1.1.1.7 christos See also add_prefix_cmd and update_prefix_field_of_prefixed_commands. */
225 1.1.1.8 christos cmd_list_element *prefixcmd = lookup_cmd_with_subcommands (list, cmdlist);
226 1.1.1.7 christos c->prefix = prefixcmd;
227 1.1.1.7 christos
228 1.1.1.7 christos
229 1.1 christos return c;
230 1.1 christos }
231 1.1 christos
232 1.1.1.6 christos struct cmd_list_element *
233 1.1.1.6 christos add_cmd (const char *name, enum command_class theclass,
234 1.1.1.6 christos const char *doc, struct cmd_list_element **list)
235 1.1.1.6 christos {
236 1.1.1.6 christos cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
237 1.1.1.6 christos result->func = NULL;
238 1.1.1.8 christos result->function.simple_func = NULL;
239 1.1.1.6 christos return result;
240 1.1.1.6 christos }
241 1.1.1.6 christos
242 1.1.1.6 christos struct cmd_list_element *
243 1.1.1.6 christos add_cmd (const char *name, enum command_class theclass,
244 1.1.1.8 christos cmd_simple_func_ftype *fun,
245 1.1.1.6 christos const char *doc, struct cmd_list_element **list)
246 1.1.1.6 christos {
247 1.1.1.6 christos cmd_list_element *result = do_add_cmd (name, theclass, doc, list);
248 1.1.1.8 christos set_cmd_simple_func (result, fun);
249 1.1.1.6 christos return result;
250 1.1.1.6 christos }
251 1.1.1.6 christos
252 1.1.1.6 christos /* Add an element with a suppress notification to the LIST of commands. */
253 1.1.1.6 christos
254 1.1.1.6 christos struct cmd_list_element *
255 1.1.1.6 christos add_cmd_suppress_notification (const char *name, enum command_class theclass,
256 1.1.1.8 christos cmd_simple_func_ftype *fun, const char *doc,
257 1.1.1.6 christos struct cmd_list_element **list,
258 1.1.1.8 christos bool *suppress_notification)
259 1.1.1.6 christos {
260 1.1.1.6 christos struct cmd_list_element *element;
261 1.1.1.6 christos
262 1.1.1.6 christos element = add_cmd (name, theclass, fun, doc, list);
263 1.1.1.6 christos element->suppress_notification = suppress_notification;
264 1.1.1.6 christos
265 1.1.1.6 christos return element;
266 1.1.1.6 christos }
267 1.1.1.6 christos
268 1.1.1.6 christos
269 1.1 christos /* Deprecates a command CMD.
270 1.1 christos REPLACEMENT is the name of the command which should be used in
271 1.1 christos place of this command, or NULL if no such command exists.
272 1.1 christos
273 1.1 christos This function does not check to see if command REPLACEMENT exists
274 1.1 christos since gdb may not have gotten around to adding REPLACEMENT when
275 1.1 christos this function is called.
276 1.1 christos
277 1.1 christos Returns a pointer to the deprecated command. */
278 1.1 christos
279 1.1 christos struct cmd_list_element *
280 1.1.1.2 christos deprecate_cmd (struct cmd_list_element *cmd, const char *replacement)
281 1.1 christos {
282 1.1.1.2 christos cmd->cmd_deprecated = 1;
283 1.1.1.2 christos cmd->deprecated_warn_user = 1;
284 1.1 christos
285 1.1 christos if (replacement != NULL)
286 1.1 christos cmd->replacement = replacement;
287 1.1 christos else
288 1.1 christos cmd->replacement = NULL;
289 1.1 christos
290 1.1 christos return cmd;
291 1.1 christos }
292 1.1 christos
293 1.1 christos struct cmd_list_element *
294 1.1.1.8 christos add_alias_cmd (const char *name, cmd_list_element *target,
295 1.1.1.5 christos enum command_class theclass, int abbrev_flag,
296 1.1.1.5 christos struct cmd_list_element **list)
297 1.1 christos {
298 1.1.1.8 christos gdb_assert (target != nullptr);
299 1.1 christos
300 1.1.1.8 christos struct cmd_list_element *c = add_cmd (name, theclass, target->doc, list);
301 1.1 christos
302 1.1.1.8 christos /* If TARGET->DOC can be freed, we should make another copy. */
303 1.1.1.8 christos if (target->doc_allocated)
304 1.1 christos {
305 1.1.1.8 christos c->doc = xstrdup (target->doc);
306 1.1.1.2 christos c->doc_allocated = 1;
307 1.1 christos }
308 1.1 christos /* NOTE: Both FUNC and all the FUNCTIONs need to be copied. */
309 1.1.1.8 christos c->func = target->func;
310 1.1.1.8 christos c->function = target->function;
311 1.1.1.8 christos c->subcommands = target->subcommands;
312 1.1.1.8 christos c->allow_unknown = target->allow_unknown;
313 1.1 christos c->abbrev_flag = abbrev_flag;
314 1.1.1.8 christos c->alias_target = target;
315 1.1.1.8 christos target->aliases.push_front (*c);
316 1.1 christos
317 1.1 christos return c;
318 1.1 christos }
319 1.1 christos
320 1.1.1.7 christos /* Update the prefix field of all sub-commands of the prefix command C.
321 1.1.1.7 christos We must do this when a prefix command is defined as the GDB init sequence
322 1.1.1.7 christos does not guarantee that a prefix command is created before its sub-commands.
323 1.1.1.7 christos For example, break-catch-sig.c initialization runs before breakpoint.c
324 1.1.1.7 christos initialization, but it is breakpoint.c that creates the "catch" command used
325 1.1.1.7 christos by the "catch signal" command created by break-catch-sig.c. */
326 1.1.1.7 christos
327 1.1.1.7 christos static void
328 1.1.1.7 christos update_prefix_field_of_prefixed_commands (struct cmd_list_element *c)
329 1.1.1.7 christos {
330 1.1.1.8 christos for (cmd_list_element *p = *c->subcommands; p != NULL; p = p->next)
331 1.1.1.7 christos {
332 1.1.1.7 christos p->prefix = c;
333 1.1.1.7 christos
334 1.1.1.7 christos /* We must recursively update the prefix field to cover
335 1.1.1.7 christos e.g. 'info auto-load libthread-db' where the creation
336 1.1.1.7 christos order was:
337 1.1.1.8 christos libthread-db
338 1.1.1.8 christos auto-load
339 1.1.1.8 christos info
340 1.1.1.7 christos In such a case, when 'auto-load' was created by do_add_cmd,
341 1.1.1.8 christos the 'libthread-db' prefix field could not be updated, as the
342 1.1.1.7 christos 'auto-load' command was not yet reachable by
343 1.1.1.8 christos lookup_cmd_for_subcommands (list, cmdlist)
344 1.1.1.7 christos that searches from the top level 'cmdlist'. */
345 1.1.1.8 christos if (p->is_prefix ())
346 1.1.1.7 christos update_prefix_field_of_prefixed_commands (p);
347 1.1.1.7 christos }
348 1.1.1.7 christos }
349 1.1.1.7 christos
350 1.1.1.7 christos
351 1.1 christos /* Like add_cmd but adds an element for a command prefix: a name that
352 1.1 christos should be followed by a subcommand to be looked up in another
353 1.1.1.8 christos command list. SUBCOMMANDS should be the address of the variable
354 1.1 christos containing that list. */
355 1.1 christos
356 1.1 christos struct cmd_list_element *
357 1.1.1.3 christos add_prefix_cmd (const char *name, enum command_class theclass,
358 1.1.1.8 christos cmd_simple_func_ftype *fun,
359 1.1.1.8 christos const char *doc, struct cmd_list_element **subcommands,
360 1.1.1.8 christos int allow_unknown, struct cmd_list_element **list)
361 1.1 christos {
362 1.1.1.3 christos struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
363 1.1 christos
364 1.1.1.8 christos c->subcommands = subcommands;
365 1.1 christos c->allow_unknown = allow_unknown;
366 1.1 christos
367 1.1.1.7 christos /* Now that prefix command C is defined, we need to set the prefix field
368 1.1.1.7 christos of all prefixed commands that were defined before C itself was defined. */
369 1.1.1.7 christos update_prefix_field_of_prefixed_commands (c);
370 1.1 christos
371 1.1 christos return c;
372 1.1 christos }
373 1.1 christos
374 1.1.1.7 christos /* A helper function for add_basic_prefix_cmd. This is a command
375 1.1.1.7 christos function that just forwards to help_list. */
376 1.1.1.7 christos
377 1.1.1.7 christos static void
378 1.1.1.7 christos do_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
379 1.1.1.7 christos {
380 1.1.1.7 christos /* Look past all aliases. */
381 1.1.1.8 christos while (c->is_alias ())
382 1.1.1.8 christos c = c->alias_target;
383 1.1.1.7 christos
384 1.1.1.8 christos help_list (*c->subcommands, c->prefixname ().c_str (),
385 1.1.1.8 christos all_commands, gdb_stdout);
386 1.1.1.7 christos }
387 1.1.1.7 christos
388 1.1.1.7 christos /* See command.h. */
389 1.1.1.7 christos
390 1.1.1.7 christos struct cmd_list_element *
391 1.1.1.7 christos add_basic_prefix_cmd (const char *name, enum command_class theclass,
392 1.1.1.8 christos const char *doc, struct cmd_list_element **subcommands,
393 1.1.1.8 christos int allow_unknown, struct cmd_list_element **list)
394 1.1.1.7 christos {
395 1.1.1.7 christos struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
396 1.1.1.8 christos doc, subcommands,
397 1.1.1.7 christos allow_unknown, list);
398 1.1.1.8 christos cmd->func = do_prefix_cmd;
399 1.1.1.7 christos return cmd;
400 1.1.1.7 christos }
401 1.1.1.7 christos
402 1.1.1.7 christos /* A helper function for add_show_prefix_cmd. This is a command
403 1.1.1.7 christos function that just forwards to cmd_show_list. */
404 1.1.1.7 christos
405 1.1.1.7 christos static void
406 1.1.1.7 christos do_show_prefix_cmd (const char *args, int from_tty, struct cmd_list_element *c)
407 1.1.1.7 christos {
408 1.1.1.8 christos cmd_show_list (*c->subcommands, from_tty);
409 1.1.1.7 christos }
410 1.1.1.7 christos
411 1.1.1.7 christos /* See command.h. */
412 1.1.1.7 christos
413 1.1.1.7 christos struct cmd_list_element *
414 1.1.1.7 christos add_show_prefix_cmd (const char *name, enum command_class theclass,
415 1.1.1.8 christos const char *doc, struct cmd_list_element **subcommands,
416 1.1.1.8 christos int allow_unknown, struct cmd_list_element **list)
417 1.1.1.7 christos {
418 1.1.1.7 christos struct cmd_list_element *cmd = add_prefix_cmd (name, theclass, nullptr,
419 1.1.1.8 christos doc, subcommands,
420 1.1.1.7 christos allow_unknown, list);
421 1.1.1.8 christos cmd->func = do_show_prefix_cmd;
422 1.1.1.7 christos return cmd;
423 1.1.1.7 christos }
424 1.1.1.7 christos
425 1.1.1.8 christos /* See command.h. */
426 1.1.1.8 christos
427 1.1.1.8 christos set_show_commands
428 1.1.1.8 christos add_setshow_prefix_cmd (const char *name, command_class theclass,
429 1.1.1.8 christos const char *set_doc, const char *show_doc,
430 1.1.1.8 christos cmd_list_element **set_subcommands_list,
431 1.1.1.8 christos cmd_list_element **show_subcommands_list,
432 1.1.1.8 christos cmd_list_element **set_list,
433 1.1.1.8 christos cmd_list_element **show_list)
434 1.1.1.8 christos {
435 1.1.1.8 christos set_show_commands cmds;
436 1.1.1.8 christos
437 1.1.1.8 christos cmds.set = add_basic_prefix_cmd (name, theclass, set_doc,
438 1.1.1.8 christos set_subcommands_list, 0,
439 1.1.1.8 christos set_list);
440 1.1.1.8 christos cmds.show = add_show_prefix_cmd (name, theclass, show_doc,
441 1.1.1.8 christos show_subcommands_list, 0,
442 1.1.1.8 christos show_list);
443 1.1.1.8 christos
444 1.1.1.8 christos return cmds;
445 1.1.1.8 christos }
446 1.1.1.8 christos
447 1.1.1.6 christos /* Like ADD_PREFIX_CMD but sets the suppress_notification pointer on the
448 1.1.1.6 christos new command list element. */
449 1.1.1.6 christos
450 1.1.1.6 christos struct cmd_list_element *
451 1.1.1.6 christos add_prefix_cmd_suppress_notification
452 1.1.1.8 christos (const char *name, enum command_class theclass,
453 1.1.1.8 christos cmd_simple_func_ftype *fun,
454 1.1.1.8 christos const char *doc, struct cmd_list_element **subcommands,
455 1.1.1.8 christos int allow_unknown, struct cmd_list_element **list,
456 1.1.1.8 christos bool *suppress_notification)
457 1.1.1.6 christos {
458 1.1.1.6 christos struct cmd_list_element *element
459 1.1.1.8 christos = add_prefix_cmd (name, theclass, fun, doc, subcommands,
460 1.1.1.8 christos allow_unknown, list);
461 1.1.1.6 christos element->suppress_notification = suppress_notification;
462 1.1.1.6 christos return element;
463 1.1.1.6 christos }
464 1.1.1.6 christos
465 1.1 christos /* Like add_prefix_cmd but sets the abbrev_flag on the new command. */
466 1.1 christos
467 1.1 christos struct cmd_list_element *
468 1.1.1.3 christos add_abbrev_prefix_cmd (const char *name, enum command_class theclass,
469 1.1.1.8 christos cmd_simple_func_ftype *fun, const char *doc,
470 1.1.1.8 christos struct cmd_list_element **subcommands,
471 1.1 christos int allow_unknown, struct cmd_list_element **list)
472 1.1 christos {
473 1.1.1.3 christos struct cmd_list_element *c = add_cmd (name, theclass, fun, doc, list);
474 1.1 christos
475 1.1.1.8 christos c->subcommands = subcommands;
476 1.1 christos c->allow_unknown = allow_unknown;
477 1.1 christos c->abbrev_flag = 1;
478 1.1 christos return c;
479 1.1 christos }
480 1.1 christos
481 1.1.1.8 christos /* This is an empty "simple func". */
482 1.1 christos void
483 1.1.1.6 christos not_just_help_class_command (const char *args, int from_tty)
484 1.1 christos {
485 1.1 christos }
486 1.1 christos
487 1.1.1.8 christos /* This is an empty cmd func. */
488 1.1 christos
489 1.1 christos static void
490 1.1.1.8 christos empty_func (const char *args, int from_tty, cmd_list_element *c)
491 1.1 christos {
492 1.1 christos }
493 1.1 christos
494 1.1 christos /* Add element named NAME to command list LIST (the list for set/show
495 1.1 christos or some sublist thereof).
496 1.1 christos TYPE is set_cmd or show_cmd.
497 1.1 christos CLASS is as in add_cmd.
498 1.1 christos VAR_TYPE is the kind of thing we are setting.
499 1.1 christos VAR is address of the variable being controlled by this command.
500 1.1.1.8 christos SET_SETTING_FUNC is a pointer to an optional function callback used to set
501 1.1.1.8 christos the setting value.
502 1.1.1.8 christos GET_SETTING_FUNC is a pointer to an optional function callback used to get
503 1.1.1.8 christos the setting value.
504 1.1 christos DOC is the documentation string. */
505 1.1 christos
506 1.1 christos static struct cmd_list_element *
507 1.1 christos add_set_or_show_cmd (const char *name,
508 1.1 christos enum cmd_types type,
509 1.1.1.3 christos enum command_class theclass,
510 1.1 christos var_types var_type,
511 1.1.1.8 christos const setting::erased_args &arg,
512 1.1.1.2 christos const char *doc,
513 1.1 christos struct cmd_list_element **list)
514 1.1 christos {
515 1.1.1.6 christos struct cmd_list_element *c = add_cmd (name, theclass, doc, list);
516 1.1 christos
517 1.1 christos gdb_assert (type == set_cmd || type == show_cmd);
518 1.1 christos c->type = type;
519 1.1.1.8 christos c->var.emplace (var_type, arg);
520 1.1.1.8 christos
521 1.1 christos /* This needs to be something besides NULL so that this isn't
522 1.1 christos treated as a help class. */
523 1.1.1.8 christos c->func = empty_func;
524 1.1 christos return c;
525 1.1 christos }
526 1.1 christos
527 1.1 christos /* Add element named NAME to both the command SET_LIST and SHOW_LIST.
528 1.1 christos CLASS is as in add_cmd. VAR_TYPE is the kind of thing we are
529 1.1 christos setting. VAR is address of the variable being controlled by this
530 1.1.1.8 christos command. If nullptr is given as VAR, then both SET_SETTING_FUNC and
531 1.1.1.8 christos GET_SETTING_FUNC must be provided. SET_SETTING_FUNC and GET_SETTING_FUNC are
532 1.1.1.8 christos callbacks used to access and modify the underlying property, whatever its
533 1.1.1.8 christos storage is. SET_FUNC and SHOW_FUNC are the callback functions (if non-NULL).
534 1.1.1.8 christos SET_DOC, SHOW_DOC and HELP_DOC are the documentation strings.
535 1.1.1.8 christos
536 1.1.1.8 christos Return the newly created set and show commands. */
537 1.1.1.8 christos
538 1.1.1.8 christos static set_show_commands
539 1.1.1.8 christos add_setshow_cmd_full_erased (const char *name,
540 1.1.1.8 christos enum command_class theclass,
541 1.1.1.8 christos var_types var_type,
542 1.1.1.8 christos const setting::erased_args &args,
543 1.1.1.8 christos const char *set_doc, const char *show_doc,
544 1.1.1.8 christos const char *help_doc,
545 1.1.1.8 christos cmd_func_ftype *set_func,
546 1.1.1.8 christos show_value_ftype *show_func,
547 1.1.1.8 christos struct cmd_list_element **set_list,
548 1.1.1.8 christos struct cmd_list_element **show_list)
549 1.1 christos {
550 1.1 christos struct cmd_list_element *set;
551 1.1 christos struct cmd_list_element *show;
552 1.1.1.8 christos gdb::unique_xmalloc_ptr<char> full_set_doc;
553 1.1.1.8 christos gdb::unique_xmalloc_ptr<char> full_show_doc;
554 1.1 christos
555 1.1 christos if (help_doc != NULL)
556 1.1 christos {
557 1.1 christos full_set_doc = xstrprintf ("%s\n%s", set_doc, help_doc);
558 1.1 christos full_show_doc = xstrprintf ("%s\n%s", show_doc, help_doc);
559 1.1 christos }
560 1.1 christos else
561 1.1 christos {
562 1.1.1.8 christos full_set_doc = make_unique_xstrdup (set_doc);
563 1.1.1.8 christos full_show_doc = make_unique_xstrdup (show_doc);
564 1.1 christos }
565 1.1.1.8 christos set = add_set_or_show_cmd (name, set_cmd, theclass, var_type, args,
566 1.1.1.8 christos full_set_doc.release (), set_list);
567 1.1.1.2 christos set->doc_allocated = 1;
568 1.1 christos
569 1.1 christos if (set_func != NULL)
570 1.1.1.8 christos set->func = set_func;
571 1.1 christos
572 1.1.1.8 christos show = add_set_or_show_cmd (name, show_cmd, theclass, var_type, args,
573 1.1.1.8 christos full_show_doc.release (), show_list);
574 1.1.1.2 christos show->doc_allocated = 1;
575 1.1 christos show->show_value_func = show_func;
576 1.1.1.7 christos /* Disable the default symbol completer. Doesn't make much sense
577 1.1.1.7 christos for the "show" command to complete on anything. */
578 1.1.1.7 christos set_cmd_completer (show, nullptr);
579 1.1 christos
580 1.1.1.8 christos return {set, show};
581 1.1.1.8 christos }
582 1.1.1.8 christos
583 1.1.1.8 christos template<typename T>
584 1.1.1.8 christos static set_show_commands
585 1.1.1.8 christos add_setshow_cmd_full (const char *name,
586 1.1.1.8 christos enum command_class theclass,
587 1.1.1.8 christos var_types var_type, T *var,
588 1.1.1.8 christos const char *set_doc, const char *show_doc,
589 1.1.1.8 christos const char *help_doc,
590 1.1.1.8 christos typename setting_func_types<T>::set set_setting_func,
591 1.1.1.8 christos typename setting_func_types<T>::get get_setting_func,
592 1.1.1.8 christos cmd_func_ftype *set_func,
593 1.1.1.8 christos show_value_ftype *show_func,
594 1.1.1.8 christos struct cmd_list_element **set_list,
595 1.1.1.8 christos struct cmd_list_element **show_list)
596 1.1.1.8 christos {
597 1.1.1.8 christos auto erased_args
598 1.1.1.8 christos = setting::erase_args (var_type, var,
599 1.1.1.8 christos set_setting_func, get_setting_func);
600 1.1.1.8 christos
601 1.1.1.8 christos return add_setshow_cmd_full_erased (name,
602 1.1.1.8 christos theclass,
603 1.1.1.8 christos var_type, erased_args,
604 1.1.1.8 christos set_doc, show_doc,
605 1.1.1.8 christos help_doc,
606 1.1.1.8 christos set_func,
607 1.1.1.8 christos show_func,
608 1.1.1.8 christos set_list,
609 1.1.1.8 christos show_list);
610 1.1 christos }
611 1.1 christos
612 1.1 christos /* Add element named NAME to command list LIST (the list for set or
613 1.1 christos some sublist thereof). CLASS is as in add_cmd. ENUMLIST is a list
614 1.1 christos of strings which may follow NAME. VAR is address of the variable
615 1.1 christos which will contain the matching string (from ENUMLIST). */
616 1.1 christos
617 1.1.1.8 christos set_show_commands
618 1.1 christos add_setshow_enum_cmd (const char *name,
619 1.1.1.3 christos enum command_class theclass,
620 1.1 christos const char *const *enumlist,
621 1.1 christos const char **var,
622 1.1 christos const char *set_doc,
623 1.1 christos const char *show_doc,
624 1.1 christos const char *help_doc,
625 1.1.1.8 christos cmd_func_ftype *set_func,
626 1.1 christos show_value_ftype *show_func,
627 1.1 christos struct cmd_list_element **set_list,
628 1.1.1.8 christos struct cmd_list_element **show_list)
629 1.1.1.8 christos {
630 1.1.1.8 christos /* We require *VAR to be initialized before this call, and
631 1.1.1.8 christos furthermore it must be == to one of the values in ENUMLIST. */
632 1.1.1.8 christos gdb_assert (var != nullptr && *var != nullptr);
633 1.1.1.8 christos for (int i = 0; ; ++i)
634 1.1.1.8 christos {
635 1.1.1.8 christos gdb_assert (enumlist[i] != nullptr);
636 1.1.1.8 christos if (*var == enumlist[i])
637 1.1.1.8 christos break;
638 1.1.1.8 christos }
639 1.1.1.8 christos
640 1.1.1.8 christos set_show_commands commands
641 1.1.1.8 christos = add_setshow_cmd_full<const char *> (name, theclass, var_enum, var,
642 1.1.1.8 christos set_doc, show_doc, help_doc,
643 1.1.1.8 christos nullptr, nullptr, set_func,
644 1.1.1.8 christos show_func, set_list, show_list);
645 1.1.1.8 christos commands.set->enums = enumlist;
646 1.1.1.8 christos return commands;
647 1.1.1.8 christos }
648 1.1.1.8 christos
649 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
650 1.1.1.8 christos to a global storage buffer. */
651 1.1.1.8 christos
652 1.1.1.8 christos set_show_commands
653 1.1.1.8 christos add_setshow_enum_cmd (const char *name, command_class theclass,
654 1.1.1.8 christos const char *const *enumlist, const char *set_doc,
655 1.1.1.8 christos const char *show_doc, const char *help_doc,
656 1.1.1.8 christos setting_func_types<const char *>::set set_func,
657 1.1.1.8 christos setting_func_types<const char *>::get get_func,
658 1.1.1.8 christos show_value_ftype *show_func,
659 1.1.1.8 christos cmd_list_element **set_list,
660 1.1.1.8 christos cmd_list_element **show_list)
661 1.1 christos {
662 1.1.1.8 christos auto cmds = add_setshow_cmd_full<const char *> (name, theclass, var_enum,
663 1.1.1.8 christos nullptr, set_doc, show_doc,
664 1.1.1.8 christos help_doc, set_func, get_func,
665 1.1.1.8 christos nullptr, show_func, set_list,
666 1.1.1.8 christos show_list);
667 1.1 christos
668 1.1.1.8 christos cmds.set->enums = enumlist;
669 1.1.1.6 christos
670 1.1.1.8 christos return cmds;
671 1.1 christos }
672 1.1 christos
673 1.1.1.7 christos /* See cli-decode.h. */
674 1.1 christos const char * const auto_boolean_enums[] = { "on", "off", "auto", NULL };
675 1.1 christos
676 1.1 christos /* Add an auto-boolean command named NAME to both the set and show
677 1.1 christos command list lists. CLASS is as in add_cmd. VAR is address of the
678 1.1 christos variable which will contain the value. DOC is the documentation
679 1.1 christos string. FUNC is the corresponding callback. */
680 1.1.1.8 christos
681 1.1.1.8 christos set_show_commands
682 1.1 christos add_setshow_auto_boolean_cmd (const char *name,
683 1.1.1.3 christos enum command_class theclass,
684 1.1 christos enum auto_boolean *var,
685 1.1 christos const char *set_doc, const char *show_doc,
686 1.1 christos const char *help_doc,
687 1.1.1.8 christos cmd_func_ftype *set_func,
688 1.1 christos show_value_ftype *show_func,
689 1.1 christos struct cmd_list_element **set_list,
690 1.1 christos struct cmd_list_element **show_list)
691 1.1 christos {
692 1.1.1.8 christos set_show_commands commands
693 1.1.1.8 christos = add_setshow_cmd_full<enum auto_boolean> (name, theclass, var_auto_boolean,
694 1.1.1.8 christos var, set_doc, show_doc, help_doc,
695 1.1.1.8 christos nullptr, nullptr, set_func,
696 1.1.1.8 christos show_func, set_list, show_list);
697 1.1.1.8 christos
698 1.1.1.8 christos commands.set->enums = auto_boolean_enums;
699 1.1.1.8 christos
700 1.1.1.8 christos return commands;
701 1.1.1.8 christos }
702 1.1.1.8 christos
703 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
704 1.1.1.8 christos to a global storage buffer. */
705 1.1.1.8 christos
706 1.1.1.8 christos set_show_commands
707 1.1.1.8 christos add_setshow_auto_boolean_cmd (const char *name, command_class theclass,
708 1.1.1.8 christos const char *set_doc, const char *show_doc,
709 1.1.1.8 christos const char *help_doc,
710 1.1.1.8 christos setting_func_types<enum auto_boolean>::set set_func,
711 1.1.1.8 christos setting_func_types<enum auto_boolean>::get get_func,
712 1.1.1.8 christos show_value_ftype *show_func,
713 1.1.1.8 christos cmd_list_element **set_list,
714 1.1.1.8 christos cmd_list_element **show_list)
715 1.1.1.8 christos {
716 1.1.1.8 christos auto cmds = add_setshow_cmd_full<enum auto_boolean> (name, theclass,
717 1.1.1.8 christos var_auto_boolean,
718 1.1.1.8 christos nullptr, set_doc,
719 1.1.1.8 christos show_doc, help_doc,
720 1.1.1.8 christos set_func, get_func,
721 1.1.1.8 christos nullptr, show_func,
722 1.1.1.8 christos set_list, show_list);
723 1.1.1.8 christos
724 1.1.1.8 christos cmds.set->enums = auto_boolean_enums;
725 1.1 christos
726 1.1.1.8 christos return cmds;
727 1.1 christos }
728 1.1 christos
729 1.1.1.7 christos /* See cli-decode.h. */
730 1.1.1.7 christos const char * const boolean_enums[] = { "on", "off", NULL };
731 1.1.1.7 christos
732 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
733 1.1 christos list for set/show or some sublist thereof). CLASS is as in
734 1.1 christos add_cmd. VAR is address of the variable which will contain the
735 1.1.1.7 christos value. SET_DOC and SHOW_DOC are the documentation strings.
736 1.1.1.7 christos Returns the new command element. */
737 1.1.1.7 christos
738 1.1.1.8 christos set_show_commands
739 1.1.1.7 christos add_setshow_boolean_cmd (const char *name, enum command_class theclass, bool *var,
740 1.1 christos const char *set_doc, const char *show_doc,
741 1.1 christos const char *help_doc,
742 1.1.1.8 christos cmd_func_ftype *set_func,
743 1.1 christos show_value_ftype *show_func,
744 1.1 christos struct cmd_list_element **set_list,
745 1.1 christos struct cmd_list_element **show_list)
746 1.1 christos {
747 1.1.1.8 christos set_show_commands commands
748 1.1.1.8 christos = add_setshow_cmd_full<bool> (name, theclass, var_boolean, var,
749 1.1.1.8 christos set_doc, show_doc, help_doc,
750 1.1.1.8 christos nullptr, nullptr, set_func, show_func,
751 1.1.1.8 christos set_list, show_list);
752 1.1 christos
753 1.1.1.8 christos commands.set->enums = boolean_enums;
754 1.1.1.7 christos
755 1.1.1.8 christos return commands;
756 1.1.1.8 christos }
757 1.1.1.8 christos
758 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
759 1.1.1.8 christos to a global storage buffer. */
760 1.1.1.8 christos
761 1.1.1.8 christos set_show_commands
762 1.1.1.8 christos add_setshow_boolean_cmd (const char *name, command_class theclass,
763 1.1.1.8 christos const char *set_doc, const char *show_doc,
764 1.1.1.8 christos const char *help_doc,
765 1.1.1.8 christos setting_func_types<bool>::set set_func,
766 1.1.1.8 christos setting_func_types<bool>::get get_func,
767 1.1.1.8 christos show_value_ftype *show_func,
768 1.1.1.8 christos cmd_list_element **set_list,
769 1.1.1.8 christos cmd_list_element **show_list)
770 1.1.1.8 christos {
771 1.1.1.8 christos auto cmds = add_setshow_cmd_full<bool> (name, theclass, var_boolean, nullptr,
772 1.1.1.8 christos set_doc, show_doc, help_doc,
773 1.1.1.8 christos set_func, get_func, nullptr,
774 1.1.1.8 christos show_func, set_list, show_list);
775 1.1.1.8 christos
776 1.1.1.8 christos cmds.set->enums = boolean_enums;
777 1.1.1.8 christos
778 1.1.1.8 christos return cmds;
779 1.1 christos }
780 1.1 christos
781 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
782 1.1 christos list for set/show or some sublist thereof). */
783 1.1.1.8 christos
784 1.1.1.8 christos set_show_commands
785 1.1.1.3 christos add_setshow_filename_cmd (const char *name, enum command_class theclass,
786 1.1.1.8 christos std::string *var,
787 1.1 christos const char *set_doc, const char *show_doc,
788 1.1 christos const char *help_doc,
789 1.1.1.8 christos cmd_func_ftype *set_func,
790 1.1 christos show_value_ftype *show_func,
791 1.1 christos struct cmd_list_element **set_list,
792 1.1 christos struct cmd_list_element **show_list)
793 1.1 christos {
794 1.1.1.8 christos set_show_commands commands
795 1.1.1.8 christos = add_setshow_cmd_full<std::string> (name, theclass, var_filename, var,
796 1.1.1.8 christos set_doc, show_doc, help_doc,
797 1.1.1.8 christos nullptr, nullptr, set_func,
798 1.1.1.8 christos show_func, set_list, show_list);
799 1.1.1.8 christos
800 1.1.1.8 christos set_cmd_completer (commands.set, filename_completer);
801 1.1.1.8 christos
802 1.1.1.8 christos return commands;
803 1.1.1.8 christos }
804 1.1.1.8 christos
805 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
806 1.1.1.8 christos to a global storage buffer. */
807 1.1.1.8 christos
808 1.1.1.8 christos set_show_commands
809 1.1.1.8 christos add_setshow_filename_cmd (const char *name, command_class theclass,
810 1.1.1.8 christos const char *set_doc, const char *show_doc,
811 1.1.1.8 christos const char *help_doc,
812 1.1.1.8 christos setting_func_types<std::string>::set set_func,
813 1.1.1.8 christos setting_func_types<std::string>::get get_func,
814 1.1.1.8 christos show_value_ftype *show_func,
815 1.1.1.8 christos cmd_list_element **set_list,
816 1.1.1.8 christos cmd_list_element **show_list)
817 1.1.1.8 christos {
818 1.1.1.8 christos auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_filename,
819 1.1.1.8 christos nullptr, set_doc, show_doc,
820 1.1.1.8 christos help_doc, set_func, get_func,
821 1.1.1.8 christos nullptr, show_func, set_list,
822 1.1.1.8 christos show_list);
823 1.1 christos
824 1.1.1.8 christos set_cmd_completer (cmds.set, filename_completer);
825 1.1.1.8 christos
826 1.1.1.8 christos return cmds;
827 1.1 christos }
828 1.1 christos
829 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
830 1.1 christos list for set/show or some sublist thereof). */
831 1.1.1.8 christos
832 1.1.1.8 christos set_show_commands
833 1.1.1.3 christos add_setshow_string_cmd (const char *name, enum command_class theclass,
834 1.1.1.8 christos std::string *var,
835 1.1 christos const char *set_doc, const char *show_doc,
836 1.1 christos const char *help_doc,
837 1.1.1.8 christos cmd_func_ftype *set_func,
838 1.1 christos show_value_ftype *show_func,
839 1.1 christos struct cmd_list_element **set_list,
840 1.1 christos struct cmd_list_element **show_list)
841 1.1 christos {
842 1.1.1.8 christos set_show_commands commands
843 1.1.1.8 christos = add_setshow_cmd_full<std::string> (name, theclass, var_string, var,
844 1.1.1.8 christos set_doc, show_doc, help_doc,
845 1.1.1.8 christos nullptr, nullptr, set_func,
846 1.1.1.8 christos show_func, set_list, show_list);
847 1.1.1.8 christos
848 1.1.1.8 christos /* Disable the default symbol completer. */
849 1.1.1.8 christos set_cmd_completer (commands.set, nullptr);
850 1.1.1.8 christos
851 1.1.1.8 christos return commands;
852 1.1.1.8 christos }
853 1.1.1.7 christos
854 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
855 1.1.1.8 christos to a global storage buffer. */
856 1.1.1.8 christos
857 1.1.1.8 christos set_show_commands
858 1.1.1.8 christos add_setshow_string_cmd (const char *name, command_class theclass,
859 1.1.1.8 christos const char *set_doc, const char *show_doc,
860 1.1.1.8 christos const char *help_doc,
861 1.1.1.8 christos setting_func_types<std::string>::set set_func,
862 1.1.1.8 christos setting_func_types<std::string>::get get_func,
863 1.1.1.8 christos show_value_ftype *show_func,
864 1.1.1.8 christos cmd_list_element **set_list,
865 1.1.1.8 christos cmd_list_element **show_list)
866 1.1.1.8 christos {
867 1.1.1.8 christos auto cmds = add_setshow_cmd_full<std::string> (name, theclass, var_string,
868 1.1.1.8 christos nullptr, set_doc, show_doc,
869 1.1.1.8 christos help_doc, set_func, get_func,
870 1.1.1.8 christos nullptr, show_func, set_list,
871 1.1.1.8 christos show_list);
872 1.1.1.7 christos
873 1.1.1.7 christos /* Disable the default symbol completer. */
874 1.1.1.8 christos set_cmd_completer (cmds.set, nullptr);
875 1.1.1.8 christos
876 1.1.1.8 christos return cmds;
877 1.1 christos }
878 1.1 christos
879 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
880 1.1 christos list for set/show or some sublist thereof). */
881 1.1.1.8 christos
882 1.1.1.8 christos set_show_commands
883 1.1.1.3 christos add_setshow_string_noescape_cmd (const char *name, enum command_class theclass,
884 1.1.1.8 christos std::string *var,
885 1.1 christos const char *set_doc, const char *show_doc,
886 1.1 christos const char *help_doc,
887 1.1.1.8 christos cmd_func_ftype *set_func,
888 1.1 christos show_value_ftype *show_func,
889 1.1 christos struct cmd_list_element **set_list,
890 1.1 christos struct cmd_list_element **show_list)
891 1.1 christos {
892 1.1.1.8 christos set_show_commands commands
893 1.1.1.8 christos = add_setshow_cmd_full<std::string> (name, theclass, var_string_noescape,
894 1.1.1.8 christos var, set_doc, show_doc, help_doc,
895 1.1.1.8 christos nullptr, nullptr, set_func, show_func,
896 1.1.1.8 christos set_list, show_list);
897 1.1.1.8 christos
898 1.1.1.8 christos /* Disable the default symbol completer. */
899 1.1.1.8 christos set_cmd_completer (commands.set, nullptr);
900 1.1 christos
901 1.1.1.8 christos return commands;
902 1.1.1.8 christos }
903 1.1.1.8 christos
904 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
905 1.1.1.8 christos to a global storage buffer. */
906 1.1.1.8 christos
907 1.1.1.8 christos set_show_commands
908 1.1.1.8 christos add_setshow_string_noescape_cmd (const char *name, command_class theclass,
909 1.1.1.8 christos const char *set_doc, const char *show_doc,
910 1.1.1.8 christos const char *help_doc,
911 1.1.1.8 christos setting_func_types<std::string>::set set_func,
912 1.1.1.8 christos setting_func_types<std::string>::get get_func,
913 1.1.1.8 christos show_value_ftype *show_func,
914 1.1.1.8 christos cmd_list_element **set_list,
915 1.1.1.8 christos cmd_list_element **show_list)
916 1.1.1.8 christos {
917 1.1.1.8 christos auto cmds = add_setshow_cmd_full<std::string> (name, theclass,
918 1.1.1.8 christos var_string_noescape, nullptr,
919 1.1.1.8 christos set_doc, show_doc, help_doc,
920 1.1.1.8 christos set_func, get_func,
921 1.1.1.8 christos nullptr, show_func, set_list,
922 1.1.1.8 christos show_list);
923 1.1.1.7 christos
924 1.1.1.7 christos /* Disable the default symbol completer. */
925 1.1.1.8 christos set_cmd_completer (cmds.set, nullptr);
926 1.1.1.7 christos
927 1.1.1.8 christos return cmds;
928 1.1 christos }
929 1.1 christos
930 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
931 1.1 christos list for set/show or some sublist thereof). */
932 1.1.1.8 christos
933 1.1.1.8 christos set_show_commands
934 1.1.1.3 christos add_setshow_optional_filename_cmd (const char *name, enum command_class theclass,
935 1.1.1.8 christos std::string *var,
936 1.1 christos const char *set_doc, const char *show_doc,
937 1.1 christos const char *help_doc,
938 1.1.1.8 christos cmd_func_ftype *set_func,
939 1.1 christos show_value_ftype *show_func,
940 1.1 christos struct cmd_list_element **set_list,
941 1.1 christos struct cmd_list_element **show_list)
942 1.1 christos {
943 1.1.1.8 christos set_show_commands commands
944 1.1.1.8 christos = add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
945 1.1.1.8 christos var, set_doc, show_doc, help_doc,
946 1.1.1.8 christos nullptr, nullptr, set_func, show_func,
947 1.1.1.8 christos set_list, show_list);
948 1.1.1.8 christos
949 1.1.1.8 christos set_cmd_completer (commands.set, filename_completer);
950 1.1.1.8 christos
951 1.1.1.8 christos return commands;
952 1.1.1.8 christos }
953 1.1.1.8 christos
954 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
955 1.1.1.8 christos to a global storage buffer. */
956 1.1.1.8 christos
957 1.1.1.8 christos set_show_commands
958 1.1.1.8 christos add_setshow_optional_filename_cmd (const char *name, command_class theclass,
959 1.1.1.8 christos const char *set_doc, const char *show_doc,
960 1.1.1.8 christos const char *help_doc,
961 1.1.1.8 christos setting_func_types<std::string>::set set_func,
962 1.1.1.8 christos setting_func_types<std::string>::get get_func,
963 1.1.1.8 christos show_value_ftype *show_func,
964 1.1.1.8 christos cmd_list_element **set_list,
965 1.1.1.8 christos cmd_list_element **show_list)
966 1.1.1.8 christos {
967 1.1.1.8 christos auto cmds =
968 1.1.1.8 christos add_setshow_cmd_full<std::string> (name, theclass, var_optional_filename,
969 1.1.1.8 christos nullptr, set_doc, show_doc, help_doc,
970 1.1.1.8 christos set_func, get_func, nullptr, show_func,
971 1.1.1.8 christos set_list,show_list);
972 1.1 christos
973 1.1.1.8 christos set_cmd_completer (cmds.set, filename_completer);
974 1.1.1.8 christos
975 1.1.1.8 christos return cmds;
976 1.1 christos }
977 1.1 christos
978 1.1 christos /* Completes on literal "unlimited". Used by integer commands that
979 1.1 christos support a special "unlimited" value. */
980 1.1 christos
981 1.1.1.6 christos static void
982 1.1 christos integer_unlimited_completer (struct cmd_list_element *ignore,
983 1.1.1.6 christos completion_tracker &tracker,
984 1.1 christos const char *text, const char *word)
985 1.1 christos {
986 1.1 christos static const char * const keywords[] =
987 1.1 christos {
988 1.1 christos "unlimited",
989 1.1 christos NULL,
990 1.1 christos };
991 1.1 christos
992 1.1.1.8 christos if (*text == '\0')
993 1.1.1.8 christos tracker.add_completion (make_unique_xstrdup ("NUMBER"));
994 1.1.1.6 christos complete_on_enum (tracker, keywords, text, word);
995 1.1 christos }
996 1.1 christos
997 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
998 1.1 christos list for set/show or some sublist thereof). CLASS is as in
999 1.1 christos add_cmd. VAR is address of the variable which will contain the
1000 1.1 christos value. SET_DOC and SHOW_DOC are the documentation strings. This
1001 1.1 christos function is only used in Python API. Please don't use it elsewhere. */
1002 1.1.1.8 christos
1003 1.1.1.8 christos set_show_commands
1004 1.1.1.3 christos add_setshow_integer_cmd (const char *name, enum command_class theclass,
1005 1.1 christos int *var,
1006 1.1 christos const char *set_doc, const char *show_doc,
1007 1.1 christos const char *help_doc,
1008 1.1.1.8 christos cmd_func_ftype *set_func,
1009 1.1 christos show_value_ftype *show_func,
1010 1.1 christos struct cmd_list_element **set_list,
1011 1.1 christos struct cmd_list_element **show_list)
1012 1.1 christos {
1013 1.1.1.8 christos set_show_commands commands
1014 1.1.1.8 christos = add_setshow_cmd_full<int> (name, theclass, var_integer, var,
1015 1.1.1.8 christos set_doc, show_doc, help_doc,
1016 1.1.1.8 christos nullptr, nullptr, set_func,
1017 1.1.1.8 christos show_func, set_list, show_list);
1018 1.1 christos
1019 1.1.1.8 christos set_cmd_completer (commands.set, integer_unlimited_completer);
1020 1.1 christos
1021 1.1.1.8 christos return commands;
1022 1.1.1.8 christos }
1023 1.1.1.8 christos
1024 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
1025 1.1.1.8 christos to a global storage buffer. */
1026 1.1.1.8 christos
1027 1.1.1.8 christos set_show_commands
1028 1.1.1.8 christos add_setshow_integer_cmd (const char *name, command_class theclass,
1029 1.1.1.8 christos const char *set_doc, const char *show_doc,
1030 1.1.1.8 christos const char *help_doc,
1031 1.1.1.8 christos setting_func_types<int>::set set_func,
1032 1.1.1.8 christos setting_func_types<int>::get get_func,
1033 1.1.1.8 christos show_value_ftype *show_func,
1034 1.1.1.8 christos cmd_list_element **set_list,
1035 1.1.1.8 christos cmd_list_element **show_list)
1036 1.1.1.8 christos {
1037 1.1.1.8 christos auto cmds = add_setshow_cmd_full<int> (name, theclass, var_integer, nullptr,
1038 1.1.1.8 christos set_doc, show_doc, help_doc, set_func,
1039 1.1.1.8 christos get_func, nullptr, show_func, set_list,
1040 1.1.1.8 christos show_list);
1041 1.1.1.8 christos
1042 1.1.1.8 christos set_cmd_completer (cmds.set, integer_unlimited_completer);
1043 1.1.1.8 christos
1044 1.1.1.8 christos return cmds;
1045 1.1 christos }
1046 1.1 christos
1047 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
1048 1.1 christos list for set/show or some sublist thereof). CLASS is as in
1049 1.1 christos add_cmd. VAR is address of the variable which will contain the
1050 1.1 christos value. SET_DOC and SHOW_DOC are the documentation strings. */
1051 1.1.1.8 christos
1052 1.1.1.8 christos set_show_commands
1053 1.1.1.3 christos add_setshow_uinteger_cmd (const char *name, enum command_class theclass,
1054 1.1 christos unsigned int *var,
1055 1.1 christos const char *set_doc, const char *show_doc,
1056 1.1 christos const char *help_doc,
1057 1.1.1.8 christos cmd_func_ftype *set_func,
1058 1.1 christos show_value_ftype *show_func,
1059 1.1 christos struct cmd_list_element **set_list,
1060 1.1 christos struct cmd_list_element **show_list)
1061 1.1 christos {
1062 1.1.1.8 christos set_show_commands commands
1063 1.1.1.8 christos = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger, var,
1064 1.1.1.8 christos set_doc, show_doc, help_doc,
1065 1.1.1.8 christos nullptr, nullptr, set_func,
1066 1.1.1.8 christos show_func, set_list, show_list);
1067 1.1.1.8 christos
1068 1.1.1.8 christos set_cmd_completer (commands.set, integer_unlimited_completer);
1069 1.1 christos
1070 1.1.1.8 christos return commands;
1071 1.1.1.8 christos }
1072 1.1.1.8 christos
1073 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
1074 1.1.1.8 christos to a global storage buffer. */
1075 1.1.1.8 christos
1076 1.1.1.8 christos set_show_commands
1077 1.1.1.8 christos add_setshow_uinteger_cmd (const char *name, command_class theclass,
1078 1.1.1.8 christos const char *set_doc, const char *show_doc,
1079 1.1.1.8 christos const char *help_doc,
1080 1.1.1.8 christos setting_func_types<unsigned int>::set set_func,
1081 1.1.1.8 christos setting_func_types<unsigned int>::get get_func,
1082 1.1.1.8 christos show_value_ftype *show_func,
1083 1.1.1.8 christos cmd_list_element **set_list,
1084 1.1.1.8 christos cmd_list_element **show_list)
1085 1.1.1.8 christos {
1086 1.1.1.8 christos auto cmds = add_setshow_cmd_full<unsigned int> (name, theclass, var_uinteger,
1087 1.1.1.8 christos nullptr, set_doc, show_doc,
1088 1.1.1.8 christos help_doc, set_func, get_func,
1089 1.1.1.8 christos nullptr, show_func, set_list,
1090 1.1.1.8 christos show_list);
1091 1.1.1.8 christos
1092 1.1.1.8 christos set_cmd_completer (cmds.set, integer_unlimited_completer);
1093 1.1 christos
1094 1.1.1.8 christos return cmds;
1095 1.1 christos }
1096 1.1 christos
1097 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
1098 1.1 christos list for set/show or some sublist thereof). CLASS is as in
1099 1.1 christos add_cmd. VAR is address of the variable which will contain the
1100 1.1 christos value. SET_DOC and SHOW_DOC are the documentation strings. */
1101 1.1.1.8 christos
1102 1.1.1.8 christos set_show_commands
1103 1.1.1.3 christos add_setshow_zinteger_cmd (const char *name, enum command_class theclass,
1104 1.1 christos int *var,
1105 1.1 christos const char *set_doc, const char *show_doc,
1106 1.1 christos const char *help_doc,
1107 1.1.1.8 christos cmd_func_ftype *set_func,
1108 1.1 christos show_value_ftype *show_func,
1109 1.1 christos struct cmd_list_element **set_list,
1110 1.1 christos struct cmd_list_element **show_list)
1111 1.1 christos {
1112 1.1.1.8 christos return add_setshow_cmd_full<int> (name, theclass, var_zinteger, var,
1113 1.1.1.8 christos set_doc, show_doc, help_doc,
1114 1.1.1.8 christos nullptr, nullptr, set_func,
1115 1.1.1.8 christos show_func, set_list, show_list);
1116 1.1 christos }
1117 1.1 christos
1118 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
1119 1.1.1.8 christos to a global storage buffer. */
1120 1.1.1.8 christos
1121 1.1.1.8 christos set_show_commands
1122 1.1.1.8 christos add_setshow_zinteger_cmd (const char *name, command_class theclass,
1123 1.1.1.8 christos const char *set_doc, const char *show_doc,
1124 1.1.1.8 christos const char *help_doc,
1125 1.1.1.8 christos setting_func_types<int>::set set_func,
1126 1.1.1.8 christos setting_func_types<int>::get get_func,
1127 1.1.1.8 christos show_value_ftype *show_func,
1128 1.1.1.8 christos cmd_list_element **set_list,
1129 1.1.1.8 christos cmd_list_element **show_list)
1130 1.1.1.8 christos {
1131 1.1.1.8 christos return add_setshow_cmd_full<int> (name, theclass, var_zinteger, nullptr,
1132 1.1.1.8 christos set_doc, show_doc, help_doc, set_func,
1133 1.1.1.8 christos get_func, nullptr, show_func, set_list,
1134 1.1.1.8 christos show_list);
1135 1.1.1.8 christos }
1136 1.1.1.8 christos
1137 1.1.1.8 christos set_show_commands
1138 1.1 christos add_setshow_zuinteger_unlimited_cmd (const char *name,
1139 1.1.1.3 christos enum command_class theclass,
1140 1.1 christos int *var,
1141 1.1 christos const char *set_doc,
1142 1.1 christos const char *show_doc,
1143 1.1 christos const char *help_doc,
1144 1.1.1.8 christos cmd_func_ftype *set_func,
1145 1.1 christos show_value_ftype *show_func,
1146 1.1 christos struct cmd_list_element **set_list,
1147 1.1 christos struct cmd_list_element **show_list)
1148 1.1 christos {
1149 1.1.1.8 christos set_show_commands commands
1150 1.1.1.8 christos = add_setshow_cmd_full<int> (name, theclass, var_zuinteger_unlimited, var,
1151 1.1.1.8 christos set_doc, show_doc, help_doc, nullptr,
1152 1.1.1.8 christos nullptr, set_func, show_func, set_list,
1153 1.1.1.8 christos show_list);
1154 1.1.1.8 christos
1155 1.1.1.8 christos set_cmd_completer (commands.set, integer_unlimited_completer);
1156 1.1 christos
1157 1.1.1.8 christos return commands;
1158 1.1.1.8 christos }
1159 1.1.1.8 christos
1160 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
1161 1.1.1.8 christos to a global storage buffer. */
1162 1.1.1.8 christos
1163 1.1.1.8 christos set_show_commands
1164 1.1.1.8 christos add_setshow_zuinteger_unlimited_cmd (const char *name, command_class theclass,
1165 1.1.1.8 christos const char *set_doc, const char *show_doc,
1166 1.1.1.8 christos const char *help_doc,
1167 1.1.1.8 christos setting_func_types<int>::set set_func,
1168 1.1.1.8 christos setting_func_types<int>::get get_func,
1169 1.1.1.8 christos show_value_ftype *show_func,
1170 1.1.1.8 christos cmd_list_element **set_list,
1171 1.1.1.8 christos cmd_list_element **show_list)
1172 1.1.1.8 christos {
1173 1.1.1.8 christos auto cmds
1174 1.1.1.8 christos = add_setshow_cmd_full<int> (name, theclass, var_zuinteger_unlimited,
1175 1.1.1.8 christos nullptr, set_doc, show_doc, help_doc, set_func,
1176 1.1.1.8 christos get_func, nullptr, show_func, set_list,
1177 1.1.1.8 christos show_list);
1178 1.1.1.8 christos
1179 1.1.1.8 christos set_cmd_completer (cmds.set, integer_unlimited_completer);
1180 1.1 christos
1181 1.1.1.8 christos return cmds;
1182 1.1 christos }
1183 1.1 christos
1184 1.1 christos /* Add element named NAME to both the set and show command LISTs (the
1185 1.1 christos list for set/show or some sublist thereof). CLASS is as in
1186 1.1 christos add_cmd. VAR is address of the variable which will contain the
1187 1.1 christos value. SET_DOC and SHOW_DOC are the documentation strings. */
1188 1.1.1.8 christos
1189 1.1.1.8 christos set_show_commands
1190 1.1.1.3 christos add_setshow_zuinteger_cmd (const char *name, enum command_class theclass,
1191 1.1 christos unsigned int *var,
1192 1.1 christos const char *set_doc, const char *show_doc,
1193 1.1 christos const char *help_doc,
1194 1.1.1.8 christos cmd_func_ftype *set_func,
1195 1.1 christos show_value_ftype *show_func,
1196 1.1 christos struct cmd_list_element **set_list,
1197 1.1 christos struct cmd_list_element **show_list)
1198 1.1 christos {
1199 1.1.1.8 christos return add_setshow_cmd_full<unsigned int> (name, theclass, var_zuinteger,
1200 1.1.1.8 christos var, set_doc, show_doc, help_doc,
1201 1.1.1.8 christos nullptr, nullptr, set_func,
1202 1.1.1.8 christos show_func, set_list, show_list);
1203 1.1 christos }
1204 1.1 christos
1205 1.1.1.8 christos /* Same as above but using a getter and a setter function instead of a pointer
1206 1.1.1.8 christos to a global storage buffer. */
1207 1.1 christos
1208 1.1.1.8 christos set_show_commands
1209 1.1.1.8 christos add_setshow_zuinteger_cmd (const char *name, command_class theclass,
1210 1.1.1.8 christos const char *set_doc, const char *show_doc,
1211 1.1.1.8 christos const char *help_doc,
1212 1.1.1.8 christos setting_func_types<unsigned int>::set set_func,
1213 1.1.1.8 christos setting_func_types<unsigned int>::get get_func,
1214 1.1.1.8 christos show_value_ftype *show_func,
1215 1.1.1.8 christos cmd_list_element **set_list,
1216 1.1.1.8 christos cmd_list_element **show_list)
1217 1.1.1.8 christos {
1218 1.1.1.8 christos return add_setshow_cmd_full<unsigned int> (name, theclass, var_zuinteger,
1219 1.1.1.8 christos nullptr, set_doc, show_doc,
1220 1.1.1.8 christos help_doc, set_func, get_func,
1221 1.1.1.8 christos nullptr, show_func, set_list,
1222 1.1.1.8 christos show_list);
1223 1.1.1.8 christos }
1224 1.1.1.8 christos
1225 1.1.1.8 christos /* Remove the command named NAME from the command list. Return the list
1226 1.1.1.8 christos commands which were aliased to the deleted command. The various *HOOKs are
1227 1.1.1.8 christos set to the pre- and post-hook commands for the deleted command. If the
1228 1.1.1.8 christos command does not have a hook, the corresponding out parameter is set to
1229 1.1.1.8 christos NULL. */
1230 1.1.1.8 christos
1231 1.1.1.8 christos static cmd_list_element::aliases_list_type
1232 1.1 christos delete_cmd (const char *name, struct cmd_list_element **list,
1233 1.1 christos struct cmd_list_element **prehook,
1234 1.1 christos struct cmd_list_element **prehookee,
1235 1.1 christos struct cmd_list_element **posthook,
1236 1.1 christos struct cmd_list_element **posthookee)
1237 1.1 christos {
1238 1.1 christos struct cmd_list_element *iter;
1239 1.1 christos struct cmd_list_element **previous_chain_ptr;
1240 1.1.1.8 christos cmd_list_element::aliases_list_type aliases;
1241 1.1 christos
1242 1.1 christos *prehook = NULL;
1243 1.1 christos *prehookee = NULL;
1244 1.1 christos *posthook = NULL;
1245 1.1 christos *posthookee = NULL;
1246 1.1 christos previous_chain_ptr = list;
1247 1.1 christos
1248 1.1 christos for (iter = *previous_chain_ptr; iter; iter = *previous_chain_ptr)
1249 1.1 christos {
1250 1.1 christos if (strcmp (iter->name, name) == 0)
1251 1.1 christos {
1252 1.1 christos if (iter->destroyer)
1253 1.1.1.8 christos iter->destroyer (iter, iter->context ());
1254 1.1.1.8 christos
1255 1.1 christos if (iter->hookee_pre)
1256 1.1 christos iter->hookee_pre->hook_pre = 0;
1257 1.1 christos *prehook = iter->hook_pre;
1258 1.1 christos *prehookee = iter->hookee_pre;
1259 1.1 christos if (iter->hookee_post)
1260 1.1 christos iter->hookee_post->hook_post = 0;
1261 1.1 christos *posthook = iter->hook_post;
1262 1.1 christos *posthookee = iter->hookee_post;
1263 1.1 christos
1264 1.1 christos /* Update the link. */
1265 1.1 christos *previous_chain_ptr = iter->next;
1266 1.1 christos
1267 1.1.1.8 christos aliases = std::move (iter->aliases);
1268 1.1 christos
1269 1.1 christos /* If this command was an alias, remove it from the list of
1270 1.1 christos aliases. */
1271 1.1.1.8 christos if (iter->is_alias ())
1272 1.1 christos {
1273 1.1.1.8 christos auto it = iter->alias_target->aliases.iterator_to (*iter);
1274 1.1.1.8 christos iter->alias_target->aliases.erase (it);
1275 1.1 christos }
1276 1.1 christos
1277 1.1.1.6 christos delete iter;
1278 1.1 christos
1279 1.1 christos /* We won't see another command with the same name. */
1280 1.1 christos break;
1281 1.1 christos }
1282 1.1 christos else
1283 1.1 christos previous_chain_ptr = &iter->next;
1284 1.1 christos }
1285 1.1 christos
1286 1.1 christos return aliases;
1287 1.1 christos }
1288 1.1 christos
1289 1.1 christos /* Shorthands to the commands above. */
1291 1.1 christos
1292 1.1 christos /* Add an element to the list of info subcommands. */
1293 1.1 christos
1294 1.1.1.8 christos struct cmd_list_element *
1295 1.1 christos add_info (const char *name, cmd_simple_func_ftype *fun, const char *doc)
1296 1.1.1.4 christos {
1297 1.1 christos return add_cmd (name, class_info, fun, doc, &infolist);
1298 1.1 christos }
1299 1.1 christos
1300 1.1 christos /* Add an alias to the list of info subcommands. */
1301 1.1.1.8 christos
1302 1.1.1.8 christos cmd_list_element *
1303 1.1 christos add_info_alias (const char *name, cmd_list_element *target, int abbrev_flag)
1304 1.1.1.8 christos {
1305 1.1 christos return add_alias_cmd (name, target, class_run, abbrev_flag, &infolist);
1306 1.1 christos }
1307 1.1 christos
1308 1.1 christos /* Add an element to the list of commands. */
1309 1.1 christos
1310 1.1.1.6 christos struct cmd_list_element *
1311 1.1.1.8 christos add_com (const char *name, enum command_class theclass,
1312 1.1.1.2 christos cmd_simple_func_ftype *fun,
1313 1.1 christos const char *doc)
1314 1.1.1.3 christos {
1315 1.1 christos return add_cmd (name, theclass, fun, doc, &cmdlist);
1316 1.1 christos }
1317 1.1.1.7 christos
1318 1.1.1.7 christos /* Add an alias or abbreviation command to the list of commands.
1319 1.1.1.7 christos For aliases predefined by GDB (such as bt), THECLASS must be
1320 1.1.1.7 christos different of class_alias, as class_alias is used to identify
1321 1.1 christos user defined aliases. */
1322 1.1.1.8 christos
1323 1.1.1.8 christos cmd_list_element *
1324 1.1.1.8 christos add_com_alias (const char *name, cmd_list_element *target,
1325 1.1 christos command_class theclass, int abbrev_flag)
1326 1.1.1.8 christos {
1327 1.1 christos return add_alias_cmd (name, target, theclass, abbrev_flag, &cmdlist);
1328 1.1.1.4 christos }
1329 1.1.1.4 christos
1330 1.1.1.4 christos /* Add an element with a suppress notification to the list of commands. */
1331 1.1.1.4 christos
1332 1.1.1.4 christos struct cmd_list_element *
1333 1.1.1.8 christos add_com_suppress_notification (const char *name, enum command_class theclass,
1334 1.1.1.8 christos cmd_simple_func_ftype *fun, const char *doc,
1335 1.1.1.4 christos bool *suppress_notification)
1336 1.1.1.6 christos {
1337 1.1.1.6 christos return add_cmd_suppress_notification (name, theclass, fun, doc,
1338 1.1.1.4 christos &cmdlist, suppress_notification);
1339 1.1.1.4 christos }
1340 1.1.1.7 christos
1341 1.1.1.7 christos /* Print the prefix of C followed by name of C in title style. */
1342 1.1.1.7 christos
1343 1.1.1.8 christos static void
1344 1.1.1.7 christos fput_command_name_styled (const cmd_list_element &c, struct ui_file *stream)
1345 1.1.1.8 christos {
1346 1.1.1.8 christos std::string prefixname
1347 1.1.1.8 christos = c.prefix == nullptr ? "" : c.prefix->prefixname ();
1348 1.1.1.8 christos
1349 1.1.1.8 christos fprintf_styled (stream, title_style.style (), "%s%s",
1350 1.1.1.8 christos prefixname.c_str (), c.name);
1351 1.1.1.7 christos }
1352 1.1.1.8 christos
1353 1.1.1.8 christos /* True if ALIAS has a user-defined documentation. */
1354 1.1.1.8 christos
1355 1.1.1.8 christos static bool
1356 1.1.1.8 christos user_documented_alias (const cmd_list_element &alias)
1357 1.1.1.8 christos {
1358 1.1.1.8 christos gdb_assert (alias.is_alias ());
1359 1.1.1.8 christos /* Alias is user documented if it has an allocated documentation
1360 1.1.1.8 christos that differs from the aliased command. */
1361 1.1.1.8 christos return (alias.doc_allocated
1362 1.1.1.7 christos && strcmp (alias.doc, alias.alias_target->doc) != 0);
1363 1.1.1.7 christos }
1364 1.1.1.7 christos
1365 1.1.1.7 christos /* Print the definition of alias C using title style for alias
1366 1.1.1.7 christos and aliased command. */
1367 1.1.1.7 christos
1368 1.1.1.8 christos static void
1369 1.1.1.7 christos fput_alias_definition_styled (const cmd_list_element &c,
1370 1.1.1.7 christos struct ui_file *stream)
1371 1.1.1.8 christos {
1372 1.1.1.8 christos gdb_assert (c.is_alias ());
1373 1.1.1.7 christos gdb_puts (" alias ", stream);
1374 1.1.1.8 christos fput_command_name_styled (c, stream);
1375 1.1.1.8 christos gdb_printf (stream, " = ");
1376 1.1.1.8 christos fput_command_name_styled (*c.alias_target, stream);
1377 1.1.1.7 christos gdb_printf (stream, " %s\n", c.default_args.c_str ());
1378 1.1.1.7 christos }
1379 1.1.1.8 christos
1380 1.1.1.8 christos /* Print the definition of CMD aliases not deprecated and having default args
1381 1.1.1.7 christos and not specifically documented by the user. */
1382 1.1.1.7 christos
1383 1.1.1.8 christos static void
1384 1.1.1.7 christos fput_aliases_definition_styled (const cmd_list_element &cmd,
1385 1.1.1.7 christos struct ui_file *stream)
1386 1.1.1.8 christos {
1387 1.1.1.8 christos for (const cmd_list_element &alias : cmd.aliases)
1388 1.1.1.8 christos if (!alias.cmd_deprecated
1389 1.1.1.8 christos && !user_documented_alias (alias)
1390 1.1.1.8 christos && !alias.default_args.empty ())
1391 1.1.1.7 christos fput_alias_definition_styled (alias, stream);
1392 1.1.1.7 christos }
1393 1.1.1.8 christos
1394 1.1.1.8 christos /* If C has one or more aliases, style print the name of C and the name of its
1395 1.1.1.7 christos aliases not documented specifically by the user, separated by commas.
1396 1.1.1.7 christos If ALWAYS_FPUT_C_NAME, print the name of C even if it has no aliases.
1397 1.1.1.7 christos If one or more names are printed, POSTFIX is printed after the last name.
1398 1.1.1.7 christos */
1399 1.1.1.7 christos
1400 1.1.1.8 christos static void
1401 1.1.1.7 christos fput_command_names_styled (const cmd_list_element &c,
1402 1.1.1.7 christos bool always_fput_c_name, const char *postfix,
1403 1.1.1.7 christos struct ui_file *stream)
1404 1.1.1.8 christos {
1405 1.1.1.8 christos /* First, check if we are going to print something. That is, either if
1406 1.1.1.8 christos ALWAYS_FPUT_C_NAME is true or if there exists at least one non-deprecated
1407 1.1.1.8 christos alias not documented specifically by the user. */
1408 1.1.1.8 christos
1409 1.1.1.8 christos auto print_alias = [] (const cmd_list_element &alias)
1410 1.1.1.8 christos {
1411 1.1.1.8 christos return !alias.cmd_deprecated && !user_documented_alias (alias);
1412 1.1.1.8 christos };
1413 1.1.1.8 christos
1414 1.1.1.8 christos bool print_something = always_fput_c_name;
1415 1.1.1.8 christos if (!print_something)
1416 1.1.1.8 christos for (const cmd_list_element &alias : c.aliases)
1417 1.1.1.8 christos {
1418 1.1.1.8 christos if (!print_alias (alias))
1419 1.1.1.8 christos continue;
1420 1.1.1.8 christos
1421 1.1.1.8 christos print_something = true;
1422 1.1.1.8 christos break;
1423 1.1.1.8 christos }
1424 1.1.1.8 christos
1425 1.1.1.7 christos if (print_something)
1426 1.1.1.8 christos fput_command_name_styled (c, stream);
1427 1.1.1.8 christos
1428 1.1.1.7 christos for (const cmd_list_element &alias : c.aliases)
1429 1.1.1.8 christos {
1430 1.1.1.8 christos if (!print_alias (alias))
1431 1.1.1.8 christos continue;
1432 1.1.1.8 christos
1433 1.1.1.8 christos gdb_puts (", ", stream);
1434 1.1.1.8 christos stream->wrap_here (3);
1435 1.1.1.7 christos fput_command_name_styled (alias, stream);
1436 1.1.1.8 christos }
1437 1.1.1.8 christos
1438 1.1.1.8 christos if (print_something)
1439 1.1.1.7 christos gdb_puts (postfix, stream);
1440 1.1.1.7 christos }
1441 1.1.1.7 christos
1442 1.1.1.7 christos /* If VERBOSE, print the full help for command C and highlight the
1443 1.1.1.7 christos documentation parts matching HIGHLIGHT,
1444 1.1.1.7 christos otherwise print only one-line help for command C. */
1445 1.1.1.7 christos
1446 1.1.1.8 christos static void
1447 1.1.1.7 christos print_doc_of_command (const cmd_list_element &c, const char *prefix,
1448 1.1.1.7 christos bool verbose, compiled_regex &highlight,
1449 1.1.1.7 christos struct ui_file *stream)
1450 1.1.1.7 christos {
1451 1.1.1.7 christos /* When printing the full documentation, add a line to separate
1452 1.1.1.7 christos this documentation from the previous command help, in the likely
1453 1.1.1.7 christos case that apropos finds several commands. */
1454 1.1.1.8 christos if (verbose)
1455 1.1.1.7 christos gdb_puts ("\n", stream);
1456 1.1.1.7 christos
1457 1.1.1.7 christos fput_command_names_styled (c, true,
1458 1.1.1.7 christos verbose ? "" : " -- ", stream);
1459 1.1.1.7 christos if (verbose)
1460 1.1.1.8 christos {
1461 1.1.1.7 christos gdb_puts ("\n", stream);
1462 1.1.1.8 christos fput_aliases_definition_styled (c, stream);
1463 1.1.1.8 christos fputs_highlighted (c.doc, highlight, stream);
1464 1.1.1.7 christos gdb_puts ("\n", stream);
1465 1.1.1.7 christos }
1466 1.1.1.7 christos else
1467 1.1.1.8 christos {
1468 1.1.1.8 christos print_doc_line (stream, c.doc, false);
1469 1.1.1.7 christos gdb_puts ("\n", stream);
1470 1.1.1.7 christos fput_aliases_definition_styled (c, stream);
1471 1.1.1.7 christos }
1472 1.1.1.7 christos }
1473 1.1 christos
1474 1.1 christos /* Recursively walk the commandlist structures, and print out the
1475 1.1 christos documentation of commands that match our regex in either their
1476 1.1.1.7 christos name, or their documentation.
1477 1.1.1.7 christos If VERBOSE, prints the complete documentation and highlight the
1478 1.1.1.7 christos documentation parts matching REGEX, otherwise prints only
1479 1.1 christos the first line.
1480 1.1.1.7 christos */
1481 1.1.1.7 christos void
1482 1.1 christos apropos_cmd (struct ui_file *stream,
1483 1.1.1.7 christos struct cmd_list_element *commandlist,
1484 1.1 christos bool verbose, compiled_regex ®ex, const char *prefix)
1485 1.1 christos {
1486 1.1 christos struct cmd_list_element *c;
1487 1.1 christos int returnvalue;
1488 1.1 christos
1489 1.1 christos /* Walk through the commands. */
1490 1.1 christos for (c=commandlist;c;c=c->next)
1491 1.1.1.8 christos {
1492 1.1.1.7 christos if (c->is_alias () && !user_documented_alias (*c))
1493 1.1.1.8 christos {
1494 1.1.1.8 christos /* Command aliases/abbreviations not specifically documented by the
1495 1.1.1.8 christos user are skipped to ensure we print the doc of a command only once,
1496 1.1.1.7 christos when encountering the aliased command. */
1497 1.1.1.7 christos continue;
1498 1.1.1.7 christos }
1499 1.1 christos
1500 1.1 christos returnvalue = -1; /* Needed to avoid double printing. */
1501 1.1 christos if (c->name != NULL)
1502 1.1.1.6 christos {
1503 1.1.1.6 christos size_t name_len = strlen (c->name);
1504 1.1 christos
1505 1.1.1.6 christos /* Try to match against the name. */
1506 1.1 christos returnvalue = regex.search (c->name, name_len, 0, name_len, NULL);
1507 1.1.1.8 christos if (returnvalue >= 0)
1508 1.1.1.7 christos print_doc_of_command (*c, prefix, verbose, regex, stream);
1509 1.1.1.7 christos
1510 1.1.1.8 christos /* Try to match against the name of the aliases. */
1511 1.1 christos for (const cmd_list_element &alias : c->aliases)
1512 1.1.1.8 christos {
1513 1.1.1.8 christos name_len = strlen (alias.name);
1514 1.1.1.7 christos returnvalue = regex.search (alias.name, name_len, 0, name_len, NULL);
1515 1.1.1.8 christos if (returnvalue >= 0)
1516 1.1.1.8 christos {
1517 1.1.1.8 christos print_doc_of_command (*c, prefix, verbose, regex, stream);
1518 1.1.1.8 christos break;
1519 1.1 christos }
1520 1.1 christos }
1521 1.1 christos }
1522 1.1 christos if (c->doc != NULL && returnvalue < 0)
1523 1.1.1.6 christos {
1524 1.1.1.6 christos size_t doc_len = strlen (c->doc);
1525 1.1 christos
1526 1.1.1.6 christos /* Try to match against documentation. */
1527 1.1.1.8 christos if (regex.search (c->doc, doc_len, 0, doc_len, NULL) >= 0)
1528 1.1 christos print_doc_of_command (*c, prefix, verbose, regex, stream);
1529 1.1.1.7 christos }
1530 1.1.1.8 christos /* Check if this command has subcommands. */
1531 1.1 christos if (c->is_prefix ())
1532 1.1 christos {
1533 1.1 christos /* Recursively call ourselves on the subcommand list,
1534 1.1.1.8 christos passing the right prefix in. */
1535 1.1.1.8 christos apropos_cmd (stream, *c->subcommands, verbose, regex,
1536 1.1 christos c->prefixname ().c_str ());
1537 1.1 christos }
1538 1.1 christos }
1539 1.1 christos }
1540 1.1 christos
1541 1.1 christos /* This command really has to deal with two things:
1542 1.1 christos 1) I want documentation on *this string* (usually called by
1543 1.1 christos "help commandname").
1544 1.1 christos
1545 1.1 christos 2) I want documentation on *this list* (usually called by giving a
1546 1.1 christos command that requires subcommands. Also called by saying just
1547 1.1 christos "help".)
1548 1.1.1.7 christos
1549 1.1 christos I am going to split this into two separate commands, help_cmd and
1550 1.1 christos help_list. */
1551 1.1 christos
1552 1.1.1.2 christos void
1553 1.1 christos help_cmd (const char *command, struct ui_file *stream)
1554 1.1.1.7 christos {
1555 1.1 christos struct cmd_list_element *c, *alias, *prefix_cmd, *c_cmd;
1556 1.1 christos
1557 1.1 christos if (!command)
1558 1.1 christos {
1559 1.1 christos help_list (cmdlist, "", all_classes, stream);
1560 1.1 christos return;
1561 1.1 christos }
1562 1.1 christos
1563 1.1 christos if (strcmp (command, "all") == 0)
1564 1.1 christos {
1565 1.1 christos help_all (stream);
1566 1.1 christos return;
1567 1.1 christos }
1568 1.1.1.7 christos
1569 1.1.1.7 christos const char *orig_command = command;
1570 1.1 christos c = lookup_cmd (&command, cmdlist, "", NULL, 0, 0);
1571 1.1 christos
1572 1.1 christos if (c == 0)
1573 1.1 christos return;
1574 1.1.1.7 christos
1575 1.1.1.7 christos lookup_cmd_composition (orig_command, &alias, &prefix_cmd, &c_cmd);
1576 1.1 christos
1577 1.1.1.8 christos /* There are three cases here.
1578 1.1 christos If c->subcommands is nonzero, we have a prefix command.
1579 1.1 christos Print its documentation, then list its subcommands.
1580 1.1 christos
1581 1.1 christos If c->func is non NULL, we really have a command. Print its
1582 1.1 christos documentation and return.
1583 1.1 christos
1584 1.1 christos If c->func is NULL, we have a class name. Print its
1585 1.1 christos documentation (as if it were a command) and then set class to the
1586 1.1 christos number of this class so that the commands in the class will be
1587 1.1 christos listed. */
1588 1.1.1.8 christos
1589 1.1.1.8 christos if (alias == nullptr || !user_documented_alias (*alias))
1590 1.1.1.8 christos {
1591 1.1.1.8 christos /* Case of a normal command, or an alias not explictly
1592 1.1.1.8 christos documented by the user. */
1593 1.1.1.8 christos /* If the user asked 'help somecommand' and there is no alias,
1594 1.1.1.8 christos the false indicates to not output the (single) command name. */
1595 1.1.1.8 christos fput_command_names_styled (*c, false, "\n", stream);
1596 1.1.1.8 christos fput_aliases_definition_styled (*c, stream);
1597 1.1.1.8 christos gdb_puts (c->doc, stream);
1598 1.1.1.8 christos }
1599 1.1.1.8 christos else
1600 1.1.1.8 christos {
1601 1.1.1.8 christos /* Case of an alias explictly documented by the user.
1602 1.1.1.8 christos Only output the alias definition and its explicit documentation. */
1603 1.1.1.8 christos fput_alias_definition_styled (*alias, stream);
1604 1.1.1.8 christos fput_command_names_styled (*alias, false, "\n", stream);
1605 1.1.1.8 christos gdb_puts (alias->doc, stream);
1606 1.1.1.8 christos }
1607 1.1 christos gdb_puts ("\n", stream);
1608 1.1.1.8 christos
1609 1.1 christos if (!c->is_prefix () && !c->is_command_class_help ())
1610 1.1.1.8 christos return;
1611 1.1.1.8 christos
1612 1.1 christos gdb_printf (stream, "\n");
1613 1.1 christos
1614 1.1.1.8 christos /* If this is a prefix command, print it's subcommands. */
1615 1.1.1.8 christos if (c->is_prefix ())
1616 1.1.1.8 christos help_list (*c->subcommands, c->prefixname ().c_str (),
1617 1.1 christos all_commands, stream);
1618 1.1 christos
1619 1.1.1.8 christos /* If this is a class name, print all of the commands in the class. */
1620 1.1.1.3 christos if (c->is_command_class_help ())
1621 1.1 christos help_list (cmdlist, "", c->theclass, stream);
1622 1.1 christos
1623 1.1.1.8 christos if (c->hook_pre || c->hook_post)
1624 1.1.1.8 christos gdb_printf (stream,
1625 1.1 christos "\nThis command has a hook (or hooks) defined:\n");
1626 1.1 christos
1627 1.1.1.8 christos if (c->hook_pre)
1628 1.1.1.8 christos gdb_printf (stream,
1629 1.1.1.8 christos "\tThis command is run after : %s (pre hook)\n",
1630 1.1 christos c->hook_pre->name);
1631 1.1.1.8 christos if (c->hook_post)
1632 1.1.1.8 christos gdb_printf (stream,
1633 1.1.1.8 christos "\tThis command is run before : %s (post hook)\n",
1634 1.1 christos c->hook_post->name);
1635 1.1 christos }
1636 1.1 christos
1637 1.1 christos /*
1638 1.1 christos * Get a specific kind of help on a command list.
1639 1.1 christos *
1640 1.1 christos * LIST is the list.
1641 1.1 christos * CMDTYPE is the prefix to use in the title string.
1642 1.1 christos * CLASS is the class with which to list the nodes of this list (see
1643 1.1 christos * documentation for help_cmd_list below), As usual, ALL_COMMANDS for
1644 1.1 christos * everything, ALL_CLASSES for just classes, and non-negative for only things
1645 1.1 christos * in a specific class.
1646 1.1 christos * and STREAM is the output stream on which to print things.
1647 1.1 christos * If you call this routine with a class >= 0, it recurses.
1648 1.1 christos */
1649 1.1.1.2 christos void
1650 1.1.1.3 christos help_list (struct cmd_list_element *list, const char *cmdtype,
1651 1.1 christos enum command_class theclass, struct ui_file *stream)
1652 1.1 christos {
1653 1.1 christos int len;
1654 1.1 christos char *cmdtype1, *cmdtype2;
1655 1.1 christos
1656 1.1 christos /* If CMDTYPE is "foo ", CMDTYPE1 gets " foo" and CMDTYPE2 gets "foo sub".
1657 1.1 christos */
1658 1.1 christos len = strlen (cmdtype);
1659 1.1 christos cmdtype1 = (char *) alloca (len + 1);
1660 1.1 christos cmdtype1[0] = 0;
1661 1.1 christos cmdtype2 = (char *) alloca (len + 4);
1662 1.1 christos cmdtype2[0] = 0;
1663 1.1 christos if (len)
1664 1.1 christos {
1665 1.1.1.6 christos cmdtype1[0] = ' ';
1666 1.1 christos memcpy (cmdtype1 + 1, cmdtype, len - 1);
1667 1.1.1.6 christos cmdtype1[len] = 0;
1668 1.1 christos memcpy (cmdtype2, cmdtype, len - 1);
1669 1.1 christos strcpy (cmdtype2 + len - 1, " sub");
1670 1.1 christos }
1671 1.1.1.3 christos
1672 1.1.1.8 christos if (theclass == all_classes)
1673 1.1 christos gdb_printf (stream, "List of classes of %scommands:\n\n", cmdtype2);
1674 1.1.1.8 christos else
1675 1.1 christos gdb_printf (stream, "List of %scommands:\n\n", cmdtype2);
1676 1.1.1.7 christos
1677 1.1 christos help_cmd_list (list, theclass, theclass >= 0, stream);
1678 1.1.1.3 christos
1679 1.1 christos if (theclass == all_classes)
1680 1.1.1.8 christos {
1681 1.1 christos gdb_printf (stream, "\n\
1682 1.1.1.8 christos Type \"help%s\" followed by a class name for a list of commands in ",
1683 1.1.1.8 christos cmdtype1);
1684 1.1.1.8 christos stream->wrap_here (0);
1685 1.1 christos gdb_printf (stream, "that class.");
1686 1.1.1.8 christos
1687 1.1 christos gdb_printf (stream, "\n\
1688 1.1 christos Type \"help all\" for the list of all commands.");
1689 1.1 christos }
1690 1.1.1.8 christos
1691 1.1.1.8 christos gdb_printf (stream, "\nType \"help%s\" followed by %scommand name ",
1692 1.1.1.8 christos cmdtype1, cmdtype2);
1693 1.1.1.8 christos stream->wrap_here (0);
1694 1.1.1.8 christos gdb_puts ("for ", stream);
1695 1.1.1.8 christos stream->wrap_here (0);
1696 1.1.1.8 christos gdb_puts ("full ", stream);
1697 1.1.1.8 christos stream->wrap_here (0);
1698 1.1.1.8 christos gdb_puts ("documentation.\n", stream);
1699 1.1.1.8 christos gdb_puts ("Type \"apropos word\" to search "
1700 1.1.1.8 christos "for commands related to \"word\".\n", stream);
1701 1.1.1.8 christos gdb_puts ("Type \"apropos -v word\" for full documentation", stream);
1702 1.1.1.8 christos stream->wrap_here (0);
1703 1.1.1.8 christos gdb_puts (" of commands related to \"word\".\n", stream);
1704 1.1.1.8 christos gdb_puts ("Command name abbreviations are allowed if unambiguous.\n",
1705 1.1 christos stream);
1706 1.1 christos }
1707 1.1 christos
1708 1.1 christos static void
1709 1.1 christos help_all (struct ui_file *stream)
1710 1.1 christos {
1711 1.1 christos struct cmd_list_element *c;
1712 1.1 christos int seen_unclassified = 0;
1713 1.1 christos
1714 1.1 christos for (c = cmdlist; c; c = c->next)
1715 1.1 christos {
1716 1.1.1.8 christos if (c->abbrev_flag)
1717 1.1 christos continue;
1718 1.1 christos /* If this is a class name, print all of the commands in the
1719 1.1 christos class. */
1720 1.1.1.8 christos
1721 1.1 christos if (c->is_command_class_help ())
1722 1.1.1.8 christos {
1723 1.1.1.7 christos gdb_printf (stream, "\nCommand class: %s\n\n", c->name);
1724 1.1 christos help_cmd_list (cmdlist, c->theclass, true, stream);
1725 1.1 christos }
1726 1.1 christos }
1727 1.1 christos
1728 1.1 christos /* While it's expected that all commands are in some class,
1729 1.1 christos as a safety measure, we'll print commands outside of any
1730 1.1 christos class at the end. */
1731 1.1 christos
1732 1.1 christos for (c = cmdlist; c; c = c->next)
1733 1.1 christos {
1734 1.1.1.8 christos if (c->abbrev_flag)
1735 1.1 christos continue;
1736 1.1.1.3 christos
1737 1.1 christos if (c->theclass == no_class)
1738 1.1 christos {
1739 1.1 christos if (!seen_unclassified)
1740 1.1.1.8 christos {
1741 1.1 christos gdb_printf (stream, "\nUnclassified commands\n\n");
1742 1.1 christos seen_unclassified = 1;
1743 1.1.1.8 christos }
1744 1.1 christos print_help_for_command (*c, true, stream);
1745 1.1 christos }
1746 1.1 christos }
1747 1.1 christos
1748 1.1 christos }
1749 1.1.1.7 christos
1750 1.1.1.7 christos /* See cli-decode.h. */
1751 1.1 christos
1752 1.1.1.7 christos void
1753 1.1.1.7 christos print_doc_line (struct ui_file *stream, const char *str,
1754 1.1 christos bool for_value_prefix)
1755 1.1 christos {
1756 1.1 christos static char *line_buffer = 0;
1757 1.1.1.2 christos static int line_size;
1758 1.1 christos const char *p;
1759 1.1 christos
1760 1.1 christos if (!line_buffer)
1761 1.1 christos {
1762 1.1 christos line_size = 80;
1763 1.1 christos line_buffer = (char *) xmalloc (line_size);
1764 1.1 christos }
1765 1.1.1.7 christos
1766 1.1 christos /* Searches for the first end of line or the end of STR. */
1767 1.1.1.7 christos p = str;
1768 1.1 christos while (*p && *p != '\n')
1769 1.1 christos p++;
1770 1.1 christos if (p - str > line_size - 1)
1771 1.1 christos {
1772 1.1 christos line_size = p - str + 1;
1773 1.1 christos xfree (line_buffer);
1774 1.1 christos line_buffer = (char *) xmalloc (line_size);
1775 1.1 christos }
1776 1.1.1.7 christos strncpy (line_buffer, str, p - str);
1777 1.1.1.7 christos if (for_value_prefix)
1778 1.1.1.7 christos {
1779 1.1.1.7 christos if (islower (line_buffer[0]))
1780 1.1.1.7 christos line_buffer[0] = toupper (line_buffer[0]);
1781 1.1.1.7 christos gdb_assert (p > str);
1782 1.1.1.7 christos if (line_buffer[p - str - 1] == '.')
1783 1.1.1.7 christos line_buffer[p - str - 1] = '\0';
1784 1.1.1.7 christos else
1785 1.1.1.7 christos line_buffer[p - str] = '\0';
1786 1.1.1.7 christos }
1787 1.1.1.7 christos else
1788 1.1.1.8 christos line_buffer[p - str] = '\0';
1789 1.1 christos gdb_puts (line_buffer, stream);
1790 1.1 christos }
1791 1.1 christos
1792 1.1 christos /* Print one-line help for command C.
1793 1.1 christos If RECURSE is non-zero, also print one-line descriptions
1794 1.1 christos of all prefixed subcommands. */
1795 1.1.1.8 christos static void
1796 1.1.1.7 christos print_help_for_command (const cmd_list_element &c,
1797 1.1 christos bool recurse, struct ui_file *stream)
1798 1.1.1.7 christos {
1799 1.1.1.8 christos fput_command_names_styled (c, true, " -- ", stream);
1800 1.1.1.8 christos print_doc_line (stream, c.doc, false);
1801 1.1.1.8 christos gdb_puts ("\n", stream);
1802 1.1.1.7 christos if (!c.default_args.empty ())
1803 1.1.1.7 christos fput_alias_definition_styled (c, stream);
1804 1.1.1.7 christos fput_aliases_definition_styled (c, stream);
1805 1.1 christos
1806 1.1.1.8 christos if (recurse
1807 1.1.1.8 christos && c.is_prefix ()
1808 1.1 christos && c.abbrev_flag == 0)
1809 1.1 christos /* Subcommands of a prefix command typically have 'all_commands'
1810 1.1 christos as class. If we pass CLASS to recursive invocation,
1811 1.1.1.8 christos most often we won't see anything. */
1812 1.1 christos help_cmd_list (*c.subcommands, all_commands, true, stream);
1813 1.1 christos }
1814 1.1 christos
1815 1.1 christos /*
1816 1.1 christos * Implement a help command on command list LIST.
1817 1.1 christos * RECURSE should be non-zero if this should be done recursively on
1818 1.1 christos * all sublists of LIST.
1819 1.1.1.4 christos * STREAM is the stream upon which the output should be written.
1820 1.1 christos * THECLASS should be:
1821 1.1 christos * A non-negative class number to list only commands in that
1822 1.1 christos * ALL_COMMANDS to list all commands in list.
1823 1.1 christos * ALL_CLASSES to list all classes in list.
1824 1.1.1.7 christos *
1825 1.1.1.7 christos * Note that aliases are only shown when THECLASS is class_alias.
1826 1.1.1.7 christos * In the other cases, the aliases will be shown together with their
1827 1.1.1.7 christos * aliased command.
1828 1.1 christos *
1829 1.1 christos * Note that RECURSE will be active on *all* sublists, not just the
1830 1.1 christos * ones selected by the criteria above (ie. the selection mechanism
1831 1.1 christos * is at the low level, not the high-level).
1832 1.1.1.7 christos */
1833 1.1.1.7 christos
1834 1.1.1.3 christos static void
1835 1.1.1.7 christos help_cmd_list (struct cmd_list_element *list, enum command_class theclass,
1836 1.1 christos bool recurse, struct ui_file *stream)
1837 1.1 christos {
1838 1.1 christos struct cmd_list_element *c;
1839 1.1 christos
1840 1.1.1.4 christos for (c = list; c; c = c->next)
1841 1.1.1.7 christos {
1842 1.1.1.7 christos if (c->abbrev_flag == 1 || c->cmd_deprecated)
1843 1.1.1.7 christos {
1844 1.1.1.7 christos /* Do not show abbreviations or deprecated commands. */
1845 1.1.1.7 christos continue;
1846 1.1.1.7 christos }
1847 1.1.1.8 christos
1848 1.1.1.7 christos if (c->is_alias () && theclass != class_alias)
1849 1.1.1.7 christos {
1850 1.1.1.7 christos /* Do not show an alias, unless specifically showing the
1851 1.1.1.7 christos list of aliases: for all other classes, an alias is
1852 1.1.1.7 christos shown (if needed) together with its aliased command. */
1853 1.1.1.7 christos continue;
1854 1.1.1.7 christos }
1855 1.1.1.7 christos
1856 1.1.1.8 christos if (theclass == all_commands
1857 1.1.1.8 christos || (theclass == all_classes && c->is_command_class_help ())
1858 1.1.1.7 christos || (theclass == c->theclass && !c->is_command_class_help ()))
1859 1.1.1.7 christos {
1860 1.1.1.8 christos /* show C when
1861 1.1.1.7 christos - showing all commands
1862 1.1.1.7 christos - showing all classes and C is a help class
1863 1.1.1.7 christos - showing commands of THECLASS and C is not the help class */
1864 1.1.1.7 christos
1865 1.1.1.7 christos /* If we show the class_alias and C is an alias, do not recurse,
1866 1.1.1.7 christos as this would show the (possibly very long) not very useful
1867 1.1.1.7 christos list of sub-commands of the aliased command. */
1868 1.1.1.8 christos print_help_for_command
1869 1.1.1.8 christos (*c,
1870 1.1.1.7 christos recurse && (theclass != class_alias || !c->is_alias ()),
1871 1.1.1.7 christos stream);
1872 1.1.1.7 christos continue;
1873 1.1.1.7 christos }
1874 1.1.1.7 christos
1875 1.1.1.7 christos if (recurse
1876 1.1.1.8 christos && (theclass == class_user || theclass == class_alias)
1877 1.1.1.7 christos && c->is_prefix ())
1878 1.1.1.7 christos {
1879 1.1.1.8 christos /* User-defined commands or aliases may be subcommands. */
1880 1.1.1.7 christos help_cmd_list (*c->subcommands, theclass, recurse, stream);
1881 1.1.1.7 christos continue;
1882 1.1.1.7 christos }
1883 1.1.1.7 christos
1884 1.1.1.7 christos /* Do not show C or recurse on C, e.g. because C does not belong to
1885 1.1 christos THECLASS or because C is a help class. */
1886 1.1 christos }
1887 1.1 christos }
1888 1.1 christos
1889 1.1 christos
1891 1.1 christos /* Search the input clist for 'command'. Return the command if
1892 1.1 christos found (or NULL if not), and return the number of commands
1893 1.1 christos found in nfound. */
1894 1.1 christos
1895 1.1 christos static struct cmd_list_element *
1896 1.1 christos find_cmd (const char *command, int len, struct cmd_list_element *clist,
1897 1.1 christos int ignore_help_classes, int *nfound)
1898 1.1 christos {
1899 1.1.1.4 christos struct cmd_list_element *found, *c;
1900 1.1 christos
1901 1.1 christos found = NULL;
1902 1.1 christos *nfound = 0;
1903 1.1.1.8 christos for (c = clist; c; c = c->next)
1904 1.1 christos if (!strncmp (command, c->name, len)
1905 1.1 christos && (!ignore_help_classes || !c->is_command_class_help ()))
1906 1.1 christos {
1907 1.1 christos found = c;
1908 1.1 christos (*nfound)++;
1909 1.1 christos if (c->name[len] == '\0')
1910 1.1 christos {
1911 1.1 christos *nfound = 1;
1912 1.1 christos break;
1913 1.1 christos }
1914 1.1 christos }
1915 1.1 christos return found;
1916 1.1.1.5 christos }
1917 1.1.1.5 christos
1918 1.1.1.5 christos /* Return the length of command name in TEXT. */
1919 1.1 christos
1920 1.1 christos int
1921 1.1 christos find_command_name_length (const char *text)
1922 1.1 christos {
1923 1.1 christos const char *p = text;
1924 1.1 christos
1925 1.1 christos /* Treating underscores as part of command words is important
1926 1.1 christos so that "set args_foo()" doesn't get interpreted as
1927 1.1 christos "set args _foo()". */
1928 1.1 christos /* Some characters are only used for TUI specific commands.
1929 1.1 christos However, they are always allowed for the sake of consistency.
1930 1.1 christos
1931 1.1 christos Note that this is larger than the character set allowed when
1932 1.1.1.7 christos creating user-defined commands. */
1933 1.1 christos
1934 1.1.1.7 christos /* Recognize the single character commands so that, e.g., "!ls"
1935 1.1 christos works as expected. */
1936 1.1 christos if (*p == '!' || *p == '|')
1937 1.1.1.7 christos return 1;
1938 1.1 christos
1939 1.1.1.3 christos while (valid_cmd_char_p (*p)
1940 1.1 christos /* Characters used by TUI specific commands. */
1941 1.1 christos || *p == '+' || *p == '<' || *p == '>' || *p == '$')
1942 1.1 christos p++;
1943 1.1 christos
1944 1.1 christos return p - text;
1945 1.1.1.7 christos }
1946 1.1 christos
1947 1.1.1.7 christos /* See command.h. */
1948 1.1.1.7 christos
1949 1.1.1.7 christos bool
1950 1.1.1.7 christos valid_cmd_char_p (int c)
1951 1.1.1.7 christos {
1952 1.1.1.7 christos /* Alas "42" is a legitimate user-defined command.
1953 1.1.1.7 christos In the interests of not breaking anything we preserve that. */
1954 1.1.1.7 christos
1955 1.1.1.7 christos return isalnum (c) || c == '-' || c == '_' || c == '.';
1956 1.1.1.7 christos }
1957 1.1.1.7 christos
1958 1.1.1.7 christos /* See command.h. */
1959 1.1 christos
1960 1.1 christos bool
1961 1.1 christos valid_user_defined_cmd_name_p (const char *name)
1962 1.1 christos {
1963 1.1 christos const char *p;
1964 1.1.1.7 christos
1965 1.1 christos if (*name == '\0')
1966 1.1 christos return false;
1967 1.1 christos
1968 1.1.1.7 christos for (p = name; *p != '\0'; ++p)
1969 1.1 christos {
1970 1.1 christos if (valid_cmd_char_p (*p))
1971 1.1.1.7 christos ; /* Ok. */
1972 1.1 christos else
1973 1.1 christos return false;
1974 1.1.1.7 christos }
1975 1.1 christos
1976 1.1 christos return true;
1977 1.1.1.8 christos }
1978 1.1 christos
1979 1.1 christos /* See command.h. */
1980 1.1 christos
1981 1.1.1.7 christos struct cmd_list_element *
1982 1.1.1.8 christos lookup_cmd_1 (const char **text, struct cmd_list_element *clist,
1983 1.1 christos struct cmd_list_element **result_list, std::string *default_args,
1984 1.1 christos int ignore_help_classes, bool lookup_for_completion_p)
1985 1.1.1.6 christos {
1986 1.1 christos char *command;
1987 1.1.1.7 christos int len, nfound;
1988 1.1 christos struct cmd_list_element *found, *c;
1989 1.1 christos bool found_alias = false;
1990 1.1 christos const char *line = *text;
1991 1.1 christos
1992 1.1 christos while (**text == ' ' || **text == '\t')
1993 1.1 christos (*text)++;
1994 1.1 christos
1995 1.1 christos /* Identify the name of the command. */
1996 1.1 christos len = find_command_name_length (*text);
1997 1.1 christos
1998 1.1 christos /* If nothing but whitespace, return 0. */
1999 1.1 christos if (len == 0)
2000 1.1 christos return 0;
2001 1.1 christos
2002 1.1 christos /* *text and p now bracket the first command word to lookup (and
2003 1.1 christos it's length is len). We copy this into a local temporary. */
2004 1.1 christos
2005 1.1 christos
2006 1.1 christos command = (char *) alloca (len + 1);
2007 1.1 christos memcpy (command, *text, len);
2008 1.1 christos command[len] = '\0';
2009 1.1 christos
2010 1.1 christos /* Look it up. */
2011 1.1 christos found = 0;
2012 1.1 christos nfound = 0;
2013 1.1 christos found = find_cmd (command, len, clist, ignore_help_classes, &nfound);
2014 1.1 christos
2015 1.1 christos /* If nothing matches, we have a simple failure. */
2016 1.1 christos if (nfound == 0)
2017 1.1 christos return 0;
2018 1.1 christos
2019 1.1.1.7 christos if (nfound > 1)
2020 1.1 christos {
2021 1.1 christos if (result_list != nullptr)
2022 1.1 christos /* Will be modified in calling routine
2023 1.1.1.7 christos if we know what the prefix command is. */
2024 1.1.1.7 christos *result_list = 0;
2025 1.1 christos if (default_args != nullptr)
2026 1.1 christos *default_args = std::string ();
2027 1.1 christos return CMD_LIST_AMBIGUOUS; /* Ambiguous. */
2028 1.1 christos }
2029 1.1 christos
2030 1.1 christos /* We've matched something on this list. Move text pointer forward. */
2031 1.1 christos
2032 1.1.1.8 christos *text += len;
2033 1.1 christos
2034 1.1 christos if (found->is_alias ())
2035 1.1 christos {
2036 1.1 christos /* We drop the alias (abbreviation) in favor of the command it
2037 1.1 christos is pointing to. If the alias is deprecated, though, we need to
2038 1.1 christos warn the user about it before we drop it. Note that while we
2039 1.1 christos are warning about the alias, we may also warn about the command
2040 1.1.1.7 christos itself and we will adjust the appropriate DEPRECATED_WARN_USER
2041 1.1.1.8 christos flags. */
2042 1.1.1.8 christos
2043 1.1.1.8 christos if (found->deprecated_warn_user && !lookup_for_completion_p)
2044 1.1.1.7 christos deprecated_cmd_warning (line, clist);
2045 1.1.1.7 christos
2046 1.1.1.7 christos
2047 1.1.1.7 christos /* Return the default_args of the alias, not the default_args
2048 1.1.1.7 christos of the command it is pointing to. */
2049 1.1.1.8 christos if (default_args != nullptr)
2050 1.1.1.7 christos *default_args = found->default_args;
2051 1.1 christos found = found->alias_target;
2052 1.1 christos found_alias = true;
2053 1.1 christos }
2054 1.1.1.8 christos /* If we found a prefix command, keep looking. */
2055 1.1 christos
2056 1.1.1.8 christos if (found->is_prefix ())
2057 1.1.1.8 christos {
2058 1.1 christos c = lookup_cmd_1 (text, *found->subcommands, result_list, default_args,
2059 1.1 christos ignore_help_classes, lookup_for_completion_p);
2060 1.1 christos if (!c)
2061 1.1.1.7 christos {
2062 1.1 christos /* Didn't find anything; this is as far as we got. */
2063 1.1.1.7 christos if (result_list != nullptr)
2064 1.1.1.7 christos *result_list = clist;
2065 1.1 christos if (!found_alias && default_args != nullptr)
2066 1.1 christos *default_args = found->default_args;
2067 1.1 christos return found;
2068 1.1 christos }
2069 1.1 christos else if (c == CMD_LIST_AMBIGUOUS)
2070 1.1 christos {
2071 1.1 christos /* We've gotten this far properly, but the next step is
2072 1.1.1.7 christos ambiguous. We need to set the result list to the best
2073 1.1 christos we've found (if an inferior hasn't already set it). */
2074 1.1.1.8 christos if (result_list != nullptr)
2075 1.1.1.8 christos if (!*result_list)
2076 1.1.1.8 christos /* This used to say *result_list = *found->subcommands.
2077 1.1.1.8 christos If that was correct, need to modify the documentation
2078 1.1 christos at the top of this function to clarify what is
2079 1.1.1.7 christos supposed to be going on. */
2080 1.1.1.7 christos *result_list = found;
2081 1.1.1.7 christos /* For ambiguous commands, do not return any default_args args. */
2082 1.1 christos if (default_args != nullptr)
2083 1.1 christos *default_args = std::string ();
2084 1.1 christos return c;
2085 1.1 christos }
2086 1.1 christos else
2087 1.1 christos {
2088 1.1 christos /* We matched! */
2089 1.1 christos return c;
2090 1.1 christos }
2091 1.1 christos }
2092 1.1.1.7 christos else
2093 1.1 christos {
2094 1.1.1.7 christos if (result_list != nullptr)
2095 1.1.1.7 christos *result_list = clist;
2096 1.1 christos if (!found_alias && default_args != nullptr)
2097 1.1 christos *default_args = found->default_args;
2098 1.1 christos return found;
2099 1.1 christos }
2100 1.1 christos }
2101 1.1 christos
2102 1.1 christos /* All this hair to move the space to the front of cmdtype */
2103 1.1 christos
2104 1.1 christos static void
2105 1.1 christos undef_cmd_error (const char *cmdtype, const char *q)
2106 1.1 christos {
2107 1.1 christos error (_("Undefined %scommand: \"%s\". Try \"help%s%.*s\"."),
2108 1.1 christos cmdtype,
2109 1.1 christos q,
2110 1.1 christos *cmdtype ? " " : "",
2111 1.1 christos (int) strlen (cmdtype) - 1,
2112 1.1 christos cmdtype);
2113 1.1 christos }
2114 1.1 christos
2115 1.1.1.7 christos /* Look up the contents of *LINE as a command in the command list LIST.
2116 1.1.1.7 christos LIST is a chain of struct cmd_list_element's.
2117 1.1.1.7 christos If it is found, return the struct cmd_list_element for that command,
2118 1.1.1.7 christos update *LINE to point after the command name, at the first argument
2119 1.1.1.7 christos and update *DEFAULT_ARGS (if DEFAULT_ARGS is not null) to the default
2120 1.1.1.7 christos args to prepend to the user provided args when running the command.
2121 1.1.1.7 christos Note that if the found cmd_list_element is found via an alias,
2122 1.1 christos the default args of the alias are returned.
2123 1.1 christos
2124 1.1 christos If not found, call error if ALLOW_UNKNOWN is zero
2125 1.1 christos otherwise (or if error returns) return zero.
2126 1.1 christos Call error if specified command is ambiguous,
2127 1.1 christos unless ALLOW_UNKNOWN is negative.
2128 1.1.1.7 christos CMDTYPE precedes the word "command" in the error message.
2129 1.1 christos
2130 1.1 christos If IGNORE_HELP_CLASSES is nonzero, ignore any command list
2131 1.1 christos elements which are actually help classes rather than commands (i.e.
2132 1.1 christos the function field of the struct cmd_list_element is 0). */
2133 1.1.1.5 christos
2134 1.1.1.5 christos struct cmd_list_element *
2135 1.1.1.7 christos lookup_cmd (const char **line, struct cmd_list_element *list,
2136 1.1 christos const char *cmdtype,
2137 1.1 christos std::string *default_args,
2138 1.1 christos int allow_unknown, int ignore_help_classes)
2139 1.1 christos {
2140 1.1 christos struct cmd_list_element *last_list = 0;
2141 1.1 christos struct cmd_list_element *c;
2142 1.1 christos
2143 1.1 christos /* Note: Do not remove trailing whitespace here because this
2144 1.1 christos would be wrong for complete_command. Jim Kingdon */
2145 1.1 christos
2146 1.1 christos if (!*line)
2147 1.1.1.7 christos error (_("Lack of needed %scommand"), cmdtype);
2148 1.1 christos
2149 1.1 christos c = lookup_cmd_1 (line, list, &last_list, default_args, ignore_help_classes);
2150 1.1 christos
2151 1.1 christos if (!c)
2152 1.1 christos {
2153 1.1 christos if (!allow_unknown)
2154 1.1 christos {
2155 1.1 christos char *q;
2156 1.1 christos int len = find_command_name_length (*line);
2157 1.1 christos
2158 1.1 christos q = (char *) alloca (len + 1);
2159 1.1 christos strncpy (q, *line, len);
2160 1.1 christos q[len] = '\0';
2161 1.1 christos undef_cmd_error (cmdtype, q);
2162 1.1 christos }
2163 1.1 christos else
2164 1.1 christos return 0;
2165 1.1 christos }
2166 1.1.1.8 christos else if (c == CMD_LIST_AMBIGUOUS)
2167 1.1.1.8 christos {
2168 1.1 christos /* Ambigous. Local values should be off subcommands or called
2169 1.1 christos values. */
2170 1.1.1.8 christos int local_allow_unknown = (last_list ? last_list->allow_unknown :
2171 1.1.1.8 christos allow_unknown);
2172 1.1 christos std::string local_cmdtype
2173 1.1.1.8 christos = last_list ? last_list->prefixname () : cmdtype;
2174 1.1 christos struct cmd_list_element *local_list =
2175 1.1 christos (last_list ? *(last_list->subcommands) : list);
2176 1.1 christos
2177 1.1 christos if (local_allow_unknown < 0)
2178 1.1 christos {
2179 1.1 christos if (last_list)
2180 1.1 christos return last_list; /* Found something. */
2181 1.1 christos else
2182 1.1 christos return 0; /* Found nothing. */
2183 1.1 christos }
2184 1.1 christos else
2185 1.1 christos {
2186 1.1 christos /* Report as error. */
2187 1.1 christos int amb_len;
2188 1.1 christos char ambbuf[100];
2189 1.1 christos
2190 1.1 christos for (amb_len = 0;
2191 1.1 christos ((*line)[amb_len] && (*line)[amb_len] != ' '
2192 1.1 christos && (*line)[amb_len] != '\t');
2193 1.1 christos amb_len++)
2194 1.1 christos ;
2195 1.1 christos
2196 1.1 christos ambbuf[0] = 0;
2197 1.1 christos for (c = local_list; c; c = c->next)
2198 1.1 christos if (!strncmp (*line, c->name, amb_len))
2199 1.1 christos {
2200 1.1 christos if (strlen (ambbuf) + strlen (c->name) + 6
2201 1.1 christos < (int) sizeof ambbuf)
2202 1.1 christos {
2203 1.1 christos if (strlen (ambbuf))
2204 1.1 christos strcat (ambbuf, ", ");
2205 1.1 christos strcat (ambbuf, c->name);
2206 1.1 christos }
2207 1.1 christos else
2208 1.1 christos {
2209 1.1 christos strcat (ambbuf, "..");
2210 1.1 christos break;
2211 1.1.1.8 christos }
2212 1.1.1.8 christos }
2213 1.1 christos error (_("Ambiguous %scommand \"%s\": %s."),
2214 1.1 christos local_cmdtype.c_str (), *line, ambbuf);
2215 1.1 christos }
2216 1.1 christos }
2217 1.1 christos else
2218 1.1.1.8 christos {
2219 1.1 christos if (c->type == set_cmd && **line != '\0' && !isspace (**line))
2220 1.1 christos error (_("Argument must be preceded by space."));
2221 1.1.1.8 christos
2222 1.1 christos /* We've got something. It may still not be what the caller
2223 1.1 christos wants (if this command *needs* a subcommand). */
2224 1.1 christos while (**line == ' ' || **line == '\t')
2225 1.1.1.8 christos (*line)++;
2226 1.1.1.8 christos
2227 1.1 christos if (c->is_prefix () && **line && !c->allow_unknown)
2228 1.1 christos undef_cmd_error (c->prefixname ().c_str (), *line);
2229 1.1 christos
2230 1.1 christos /* Seems to be what he wants. Return it. */
2231 1.1 christos return c;
2232 1.1 christos }
2233 1.1 christos return 0;
2234 1.1.1.8 christos }
2235 1.1.1.8 christos
2236 1.1.1.8 christos /* See command.h. */
2237 1.1.1.8 christos
2238 1.1.1.8 christos struct cmd_list_element *
2239 1.1.1.8 christos lookup_cmd_exact (const char *name,
2240 1.1.1.8 christos struct cmd_list_element *list,
2241 1.1.1.8 christos bool ignore_help_classes)
2242 1.1.1.8 christos {
2243 1.1.1.8 christos const char *tem = name;
2244 1.1.1.8 christos struct cmd_list_element *cmd = lookup_cmd (&tem, list, "", NULL, -1,
2245 1.1.1.8 christos ignore_help_classes);
2246 1.1.1.8 christos if (cmd != nullptr && strcmp (name, cmd->name) != 0)
2247 1.1.1.8 christos cmd = nullptr;
2248 1.1.1.8 christos return cmd;
2249 1.1 christos }
2250 1.1 christos
2251 1.1 christos /* We are here presumably because an alias or command in TEXT is
2252 1.1 christos deprecated and a warning message should be generated. This
2253 1.1 christos function decodes TEXT and potentially generates a warning message
2254 1.1 christos as outlined below.
2255 1.1 christos
2256 1.1 christos Example for 'set endian big' which has a fictitious alias 'seb'.
2257 1.1 christos
2258 1.1 christos If alias wasn't used in TEXT, and the command is deprecated:
2259 1.1 christos "warning: 'set endian big' is deprecated."
2260 1.1 christos
2261 1.1 christos If alias was used, and only the alias is deprecated:
2262 1.1 christos "warning: 'seb' an alias for the command 'set endian big' is deprecated."
2263 1.1 christos
2264 1.1 christos If alias was used and command is deprecated (regardless of whether
2265 1.1 christos the alias itself is deprecated:
2266 1.1 christos
2267 1.1 christos "warning: 'set endian big' (seb) is deprecated."
2268 1.1 christos
2269 1.1 christos After the message has been sent, clear the appropriate flags in the
2270 1.1 christos command and/or the alias so the user is no longer bothered.
2271 1.1 christos
2272 1.1.1.8 christos */
2273 1.1 christos void
2274 1.1.1.8 christos deprecated_cmd_warning (const char *text, struct cmd_list_element *list)
2275 1.1.1.8 christos {
2276 1.1 christos struct cmd_list_element *alias = nullptr;
2277 1.1.1.8 christos struct cmd_list_element *cmd = nullptr;
2278 1.1.1.8 christos
2279 1.1.1.8 christos /* Return if text doesn't evaluate to a command. We place this lookup
2280 1.1.1.8 christos within its own scope so that the PREFIX_CMD local is not visible
2281 1.1.1.8 christos later in this function. The value returned in PREFIX_CMD is based on
2282 1.1.1.8 christos the prefix found in TEXT, and is our case this prefix can be missing
2283 1.1.1.8 christos in some situations (when LIST is not the global CMDLIST).
2284 1.1.1.8 christos
2285 1.1.1.8 christos It is better for our purposes to use the prefix commands directly from
2286 1.1.1.8 christos the ALIAS and CMD results. */
2287 1.1.1.8 christos {
2288 1.1.1.8 christos struct cmd_list_element *prefix_cmd = nullptr;
2289 1.1.1.8 christos if (!lookup_cmd_composition_1 (text, &alias, &prefix_cmd, &cmd, list))
2290 1.1 christos return;
2291 1.1.1.8 christos }
2292 1.1.1.8 christos
2293 1.1.1.8 christos /* Return if nothing is deprecated. */
2294 1.1 christos if (!((alias != nullptr ? alias->deprecated_warn_user : 0)
2295 1.1 christos || cmd->deprecated_warn_user))
2296 1.1.1.8 christos return;
2297 1.1.1.8 christos
2298 1.1.1.8 christos /* Join command prefix (if any) and the command name. */
2299 1.1.1.8 christos std::string tmp_cmd_str;
2300 1.1.1.8 christos if (cmd->prefix != nullptr)
2301 1.1.1.8 christos tmp_cmd_str += cmd->prefix->prefixname ();
2302 1.1.1.8 christos tmp_cmd_str += std::string (cmd->name);
2303 1.1.1.8 christos
2304 1.1.1.8 christos /* Display the appropriate first line, this warns that the thing the user
2305 1.1.1.8 christos entered is deprecated. */
2306 1.1.1.8 christos if (alias != nullptr)
2307 1.1.1.8 christos {
2308 1.1.1.8 christos /* Join the alias prefix (if any) and the alias name. */
2309 1.1.1.8 christos std::string tmp_alias_str;
2310 1.1.1.8 christos if (alias->prefix != nullptr)
2311 1.1.1.8 christos tmp_alias_str += alias->prefix->prefixname ();
2312 1.1.1.8 christos tmp_alias_str += std::string (alias->name);
2313 1.1.1.8 christos
2314 1.1.1.8 christos if (cmd->cmd_deprecated)
2315 1.1.1.8 christos gdb_printf (_("Warning: command '%ps' (%ps) is deprecated.\n"),
2316 1.1.1.8 christos styled_string (title_style.style (),
2317 1.1.1.8 christos tmp_cmd_str.c_str ()),
2318 1.1 christos styled_string (title_style.style (),
2319 1.1.1.8 christos tmp_alias_str.c_str ()));
2320 1.1.1.8 christos else
2321 1.1.1.8 christos gdb_printf (_("Warning: '%ps', an alias for the command '%ps', "
2322 1.1.1.8 christos "is deprecated.\n"),
2323 1.1.1.8 christos styled_string (title_style.style (),
2324 1.1.1.8 christos tmp_alias_str.c_str ()),
2325 1.1 christos styled_string (title_style.style (),
2326 1.1.1.8 christos tmp_cmd_str.c_str ()));
2327 1.1.1.8 christos }
2328 1.1.1.8 christos else
2329 1.1.1.8 christos gdb_printf (_("Warning: command '%ps' is deprecated.\n"),
2330 1.1.1.8 christos styled_string (title_style.style (),
2331 1.1.1.8 christos tmp_cmd_str.c_str ()));
2332 1.1.1.8 christos
2333 1.1.1.8 christos /* Now display a second line indicating what the user should use instead.
2334 1.1.1.8 christos If it is only the alias that is deprecated, we want to indicate the
2335 1.1.1.8 christos new alias, otherwise we'll indicate the new command. */
2336 1.1.1.8 christos const char *replacement;
2337 1.1.1.8 christos if (alias != nullptr && !cmd->cmd_deprecated)
2338 1.1.1.8 christos replacement = alias->replacement;
2339 1.1.1.8 christos else
2340 1.1.1.8 christos replacement = cmd->replacement;
2341 1.1.1.8 christos if (replacement != nullptr)
2342 1.1.1.8 christos gdb_printf (_("Use '%ps'.\n\n"),
2343 1.1.1.8 christos styled_string (title_style.style (),
2344 1.1.1.8 christos replacement));
2345 1.1 christos else
2346 1.1 christos gdb_printf (_("No alternative known.\n\n"));
2347 1.1.1.8 christos
2348 1.1.1.2 christos /* We've warned you, now we'll keep quiet. */
2349 1.1.1.2 christos if (alias != nullptr)
2350 1.1 christos alias->deprecated_warn_user = 0;
2351 1.1 christos cmd->deprecated_warn_user = 0;
2352 1.1.1.8 christos }
2353 1.1 christos
2354 1.1.1.7 christos /* Look up the contents of TEXT as a command in the command list CUR_LIST.
2355 1.1.1.7 christos Return 1 on success, 0 on failure.
2356 1.1.1.7 christos
2357 1.1.1.7 christos If TEXT refers to an alias, *ALIAS will point to that alias.
2358 1.1.1.7 christos
2359 1.1.1.7 christos If TEXT is a subcommand (i.e. one that is preceded by a prefix
2360 1.1.1.7 christos command) set *PREFIX_CMD.
2361 1.1.1.7 christos
2362 1.1.1.7 christos Set *CMD to point to the command TEXT indicates.
2363 1.1 christos
2364 1.1.1.7 christos If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2365 1.1 christos exist, they are NULL when we return.
2366 1.1 christos
2367 1.1.1.8 christos */
2368 1.1.1.8 christos
2369 1.1.1.8 christos static int
2370 1.1.1.8 christos lookup_cmd_composition_1 (const char *text,
2371 1.1.1.8 christos struct cmd_list_element **alias,
2372 1.1.1.8 christos struct cmd_list_element **prefix_cmd,
2373 1.1.1.8 christos struct cmd_list_element **cmd,
2374 1.1.1.8 christos struct cmd_list_element *cur_list)
2375 1.1.1.8 christos {
2376 1.1.1.8 christos *alias = nullptr;
2377 1.1.1.7 christos *prefix_cmd = cur_list->prefix;
2378 1.1.1.7 christos *cmd = nullptr;
2379 1.1.1.7 christos
2380 1.1.1.8 christos text = skip_spaces (text);
2381 1.1.1.8 christos
2382 1.1 christos /* Go through as many command lists as we need to, to find the command
2383 1.1.1.7 christos TEXT refers to. */
2384 1.1 christos while (1)
2385 1.1.1.8 christos {
2386 1.1.1.7 christos /* Identify the name of the command. */
2387 1.1 christos int len = find_command_name_length (text);
2388 1.1 christos
2389 1.1 christos /* If nothing but whitespace, return. */
2390 1.1.1.7 christos if (len == 0)
2391 1.1.1.7 christos return 0;
2392 1.1.1.7 christos
2393 1.1.1.8 christos /* TEXT is the start of the first command word to lookup (and
2394 1.1.1.7 christos it's length is LEN). We copy this into a local temporary. */
2395 1.1 christos std::string command (text, len);
2396 1.1.1.8 christos
2397 1.1.1.8 christos /* Look it up. */
2398 1.1.1.7 christos int nfound = 0;
2399 1.1.1.8 christos *cmd = find_cmd (command.c_str (), len, cur_list, 1, &nfound);
2400 1.1.1.8 christos
2401 1.1.1.8 christos /* We only handle the case where a single command was found. */
2402 1.1 christos if (*cmd == CMD_LIST_AMBIGUOUS || *cmd == nullptr)
2403 1.1 christos return 0;
2404 1.1.1.8 christos else
2405 1.1 christos {
2406 1.1.1.8 christos if ((*cmd)->is_alias ())
2407 1.1.1.8 christos {
2408 1.1 christos /* If the command was actually an alias, we note that an
2409 1.1.1.8 christos alias was used (by assigning *ALIAS) and we set *CMD. */
2410 1.1 christos *alias = *cmd;
2411 1.1 christos *cmd = (*cmd)->alias_target;
2412 1.1.1.7 christos }
2413 1.1.1.7 christos }
2414 1.1.1.7 christos
2415 1.1.1.7 christos text += len;
2416 1.1.1.8 christos text = skip_spaces (text);
2417 1.1.1.8 christos
2418 1.1.1.8 christos if ((*cmd)->is_prefix () && *text != '\0')
2419 1.1.1.8 christos {
2420 1.1.1.8 christos cur_list = *(*cmd)->subcommands;
2421 1.1 christos *prefix_cmd = *cmd;
2422 1.1 christos }
2423 1.1 christos else
2424 1.1 christos return 1;
2425 1.1 christos }
2426 1.1.1.8 christos }
2427 1.1.1.8 christos
2428 1.1.1.8 christos /* Look up the contents of TEXT as a command in the command list 'cmdlist'.
2429 1.1.1.8 christos Return 1 on success, 0 on failure.
2430 1.1.1.8 christos
2431 1.1.1.8 christos If TEXT refers to an alias, *ALIAS will point to that alias.
2432 1.1.1.8 christos
2433 1.1.1.8 christos If TEXT is a subcommand (i.e. one that is preceded by a prefix
2434 1.1.1.8 christos command) set *PREFIX_CMD.
2435 1.1.1.8 christos
2436 1.1.1.8 christos Set *CMD to point to the command TEXT indicates.
2437 1.1.1.8 christos
2438 1.1.1.8 christos If any of *ALIAS, *PREFIX_CMD, or *CMD cannot be determined or do not
2439 1.1.1.8 christos exist, they are NULL when we return.
2440 1.1.1.8 christos
2441 1.1.1.8 christos */
2442 1.1.1.8 christos
2443 1.1.1.8 christos int
2444 1.1.1.8 christos lookup_cmd_composition (const char *text,
2445 1.1.1.8 christos struct cmd_list_element **alias,
2446 1.1.1.8 christos struct cmd_list_element **prefix_cmd,
2447 1.1.1.8 christos struct cmd_list_element **cmd)
2448 1.1.1.8 christos {
2449 1.1.1.8 christos return lookup_cmd_composition_1 (text, alias, prefix_cmd, cmd, cmdlist);
2450 1.1 christos }
2451 1.1 christos
2452 1.1 christos /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2453 1.1 christos
2454 1.1 christos /* Return a vector of char pointers which point to the different
2455 1.1 christos possible completions in LIST of TEXT.
2456 1.1 christos
2457 1.1 christos WORD points in the same buffer as TEXT, and completions should be
2458 1.1 christos returned relative to this position. For example, suppose TEXT is
2459 1.1 christos "foo" and we want to complete to "foobar". If WORD is "oo", return
2460 1.1.1.6 christos "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2461 1.1 christos
2462 1.1.1.6 christos void
2463 1.1 christos complete_on_cmdlist (struct cmd_list_element *list,
2464 1.1 christos completion_tracker &tracker,
2465 1.1 christos const char *text, const char *word,
2466 1.1 christos int ignore_help_classes)
2467 1.1 christos {
2468 1.1 christos struct cmd_list_element *ptr;
2469 1.1 christos int textlen = strlen (text);
2470 1.1 christos int pass;
2471 1.1 christos int saw_deprecated_match = 0;
2472 1.1 christos
2473 1.1 christos /* We do one or two passes. In the first pass, we skip deprecated
2474 1.1 christos commands. If we see no matching commands in the first pass, and
2475 1.1.1.6 christos if we did happen to see a matching deprecated command, we do
2476 1.1 christos another loop to collect those. */
2477 1.1.1.6 christos for (pass = 0; pass < 2; ++pass)
2478 1.1.1.6 christos {
2479 1.1 christos bool got_matches = false;
2480 1.1 christos
2481 1.1 christos for (ptr = list; ptr; ptr = ptr->next)
2482 1.1.1.8 christos if (!strncmp (ptr->name, text, textlen)
2483 1.1.1.8 christos && !ptr->abbrev_flag
2484 1.1 christos && (!ignore_help_classes || !ptr->is_command_class_help ()
2485 1.1 christos || ptr->is_prefix ()))
2486 1.1 christos {
2487 1.1.1.2 christos if (pass == 0)
2488 1.1 christos {
2489 1.1 christos if (ptr->cmd_deprecated)
2490 1.1 christos {
2491 1.1 christos saw_deprecated_match = 1;
2492 1.1 christos continue;
2493 1.1 christos }
2494 1.1.1.6 christos }
2495 1.1.1.6 christos
2496 1.1.1.6 christos tracker.add_completion
2497 1.1 christos (make_completion_match_str (ptr->name, text, word));
2498 1.1.1.6 christos got_matches = true;
2499 1.1.1.6 christos }
2500 1.1.1.6 christos
2501 1.1.1.6 christos if (got_matches)
2502 1.1 christos break;
2503 1.1 christos
2504 1.1 christos /* If we saw no matching deprecated commands in the first pass,
2505 1.1 christos just bail out. */
2506 1.1 christos if (!saw_deprecated_match)
2507 1.1 christos break;
2508 1.1 christos }
2509 1.1 christos }
2510 1.1 christos
2511 1.1.1.6 christos /* Helper function for SYMBOL_COMPLETION_FUNCTION. */
2512 1.1 christos
2513 1.1 christos /* Add the different possible completions in ENUMLIST of TEXT.
2514 1.1 christos
2515 1.1 christos WORD points in the same buffer as TEXT, and completions should be
2516 1.1 christos returned relative to this position. For example, suppose TEXT is "foo"
2517 1.1 christos and we want to complete to "foobar". If WORD is "oo", return
2518 1.1.1.6 christos "oobar"; if WORD is "baz/foo", return "baz/foobar". */
2519 1.1.1.6 christos
2520 1.1.1.6 christos void
2521 1.1 christos complete_on_enum (completion_tracker &tracker,
2522 1.1 christos const char *const *enumlist,
2523 1.1 christos const char *text, const char *word)
2524 1.1 christos {
2525 1.1 christos int textlen = strlen (text);
2526 1.1 christos int i;
2527 1.1 christos const char *name;
2528 1.1 christos
2529 1.1.1.6 christos for (i = 0; (name = enumlist[i]) != NULL; i++)
2530 1.1 christos if (strncmp (name, text, textlen) == 0)
2531 1.1 christos tracker.add_completion (make_completion_match_str (name, text, word));
2532 1.1 christos }
2533 1.1 christos
2534 1.1.1.6 christos /* Call the command function. */
2535 1.1 christos void
2536 1.1.1.8 christos cmd_func (struct cmd_list_element *cmd, const char *args, int from_tty)
2537 1.1.1.4 christos {
2538 1.1.1.8 christos if (!cmd->is_command_class_help ())
2539 1.1.1.4 christos {
2540 1.1.1.4 christos gdb::optional<scoped_restore_tmpl<bool>> restore_suppress;
2541 1.1.1.8 christos
2542 1.1.1.4 christos if (cmd->suppress_notification != NULL)
2543 1.1.1.8 christos restore_suppress.emplace (cmd->suppress_notification, true);
2544 1.1.1.4 christos
2545 1.1 christos cmd->func (args, from_tty, cmd);
2546 1.1 christos }
2547 1.1 christos else
2548 1.1.1.2 christos error (_("Invalid command"));
2549 1.1.1.2 christos }
2550 1.1.1.2 christos
2551 1.1.1.2 christos int
2552 1.1.1.8 christos cli_user_command_p (struct cmd_list_element *cmd)
2553 1.1.1.2 christos {
2554 return cmd->theclass == class_user && cmd->func == do_simple_func;
2555 }
2556