nm: Add --quiet to suppress "no symbols" diagnostic
[binutils-gdb.git] / gdb / cli / cli-option.c
blobab76f194c3d98b245459025139f44f1b642f8cda
1 /* CLI options framework, for GDB.
3 Copyright (C) 2017-2021 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "defs.h"
21 #include "cli/cli-option.h"
22 #include "cli/cli-decode.h"
23 #include "cli/cli-utils.h"
24 #include "cli/cli-setshow.h"
25 #include "command.h"
26 #include <vector>
28 namespace gdb {
29 namespace option {
31 /* An option's value. Which field is active depends on the option's
32 type. */
33 union option_value
35 /* For var_boolean options. */
36 bool boolean;
38 /* For var_uinteger options. */
39 unsigned int uinteger;
41 /* For var_zuinteger_unlimited options. */
42 int integer;
44 /* For var_enum options. */
45 const char *enumeration;
47 /* For var_string options. This is malloc-allocated. */
48 char *string;
51 /* Holds an options definition and its value. */
52 struct option_def_and_value
54 /* The option definition. */
55 const option_def &option;
57 /* A context. */
58 void *ctx;
60 /* The option's value, if any. */
61 gdb::optional<option_value> value;
63 /* Constructor. */
64 option_def_and_value (const option_def &option_, void *ctx_,
65 gdb::optional<option_value> &&value_ = {})
66 : option (option_),
67 ctx (ctx_),
68 value (std::move (value_))
70 clear_value (option_, value_);
73 /* Move constructor. Need this because for some types the values
74 are allocated on the heap. */
75 option_def_and_value (option_def_and_value &&rval)
76 : option (rval.option),
77 ctx (rval.ctx),
78 value (std::move (rval.value))
80 clear_value (rval.option, rval.value);
83 DISABLE_COPY_AND_ASSIGN (option_def_and_value);
85 ~option_def_and_value ()
87 if (value.has_value ())
89 if (option.type == var_string)
90 xfree (value->string);
94 private:
96 /* Clear the option_value, without releasing it. This is used after
97 the value has been moved to some other option_def_and_value
98 instance. This is needed because for some types the value is
99 allocated on the heap, so we must clear the pointer in the
100 source, to avoid a double free. */
101 static void clear_value (const option_def &option,
102 gdb::optional<option_value> &value)
104 if (value.has_value ())
106 if (option.type == var_string)
107 value->string = nullptr;
112 static void save_option_value_in_ctx (gdb::optional<option_def_and_value> &ov);
114 /* Info passed around when handling completion. */
115 struct parse_option_completion_info
117 /* The completion word. */
118 const char *word;
120 /* The tracker. */
121 completion_tracker &tracker;
124 /* If ARGS starts with "-", look for a "--" delimiter. If one is
125 found, then interpret everything up until the "--" as command line
126 options. Otherwise, interpret unknown input as the beginning of
127 the command's operands. */
129 static const char *
130 find_end_options_delimiter (const char *args)
132 if (args[0] == '-')
134 const char *p = args;
136 p = skip_spaces (p);
137 while (*p)
139 if (check_for_argument (&p, "--"))
140 return p;
141 else
142 p = skip_to_space (p);
143 p = skip_spaces (p);
147 return nullptr;
150 /* Complete TEXT/WORD on all options in OPTIONS_GROUP. */
152 static void
153 complete_on_options (gdb::array_view<const option_def_group> options_group,
154 completion_tracker &tracker,
155 const char *text, const char *word)
157 size_t textlen = strlen (text);
158 for (const auto &grp : options_group)
159 for (const auto &opt : grp.options)
160 if (strncmp (opt.name, text, textlen) == 0)
162 tracker.add_completion
163 (make_completion_match_str (opt.name, text, word));
167 /* See cli-option.h. */
169 void
170 complete_on_all_options (completion_tracker &tracker,
171 gdb::array_view<const option_def_group> options_group)
173 static const char opt[] = "-";
174 complete_on_options (options_group, tracker, opt + 1, opt);
177 /* Parse ARGS, guided by OPTIONS_GROUP. HAVE_DELIMITER is true if the
178 whole ARGS line included the "--" options-terminator delimiter. */
180 static gdb::optional<option_def_and_value>
181 parse_option (gdb::array_view<const option_def_group> options_group,
182 process_options_mode mode,
183 bool have_delimiter,
184 const char **args,
185 parse_option_completion_info *completion = nullptr)
187 if (*args == nullptr)
188 return {};
189 else if (**args != '-')
191 if (have_delimiter)
192 error (_("Unrecognized option at: %s"), *args);
193 return {};
195 else if (check_for_argument (args, "--"))
196 return {};
198 /* Skip the initial '-'. */
199 const char *arg = *args + 1;
201 const char *after = skip_to_space (arg);
202 size_t len = after - arg;
203 const option_def *match = nullptr;
204 void *match_ctx = nullptr;
206 for (const auto &grp : options_group)
208 for (const auto &o : grp.options)
210 if (strncmp (o.name, arg, len) == 0)
212 if (match != nullptr)
214 if (completion != nullptr && arg[len] == '\0')
216 complete_on_options (options_group,
217 completion->tracker,
218 arg, completion->word);
219 return {};
222 error (_("Ambiguous option at: -%s"), arg);
225 match = &o;
226 match_ctx = grp.ctx;
228 if ((isspace (arg[len]) || arg[len] == '\0')
229 && strlen (o.name) == len)
230 break; /* Exact match. */
235 if (match == nullptr)
237 if (have_delimiter || mode != PROCESS_OPTIONS_UNKNOWN_IS_OPERAND)
238 error (_("Unrecognized option at: %s"), *args);
240 return {};
243 if (completion != nullptr && arg[len] == '\0')
245 complete_on_options (options_group, completion->tracker,
246 arg, completion->word);
247 return {};
250 *args += 1 + len;
251 *args = skip_spaces (*args);
252 if (completion != nullptr)
253 completion->word = *args;
255 switch (match->type)
257 case var_boolean:
259 if (!match->have_argument)
261 option_value val;
262 val.boolean = true;
263 return option_def_and_value {*match, match_ctx, val};
266 const char *val_str = *args;
267 int res;
269 if (**args == '\0' && completion != nullptr)
271 /* Complete on both "on/off" and more options. */
273 if (mode == PROCESS_OPTIONS_REQUIRE_DELIMITER)
275 complete_on_enum (completion->tracker,
276 boolean_enums, val_str, val_str);
277 complete_on_all_options (completion->tracker, options_group);
279 return option_def_and_value {*match, match_ctx};
281 else if (**args == '-')
283 /* Treat:
284 "cmd -boolean-option -another-opt..."
286 "cmd -boolean-option on -another-opt..."
288 res = 1;
290 else if (**args == '\0')
292 /* Treat:
293 (1) "cmd -boolean-option "
295 (1) "cmd -boolean-option on"
297 res = 1;
299 else
301 res = parse_cli_boolean_value (args);
302 if (res < 0)
304 const char *end = skip_to_space (*args);
305 if (completion != nullptr)
307 if (*end == '\0')
309 complete_on_enum (completion->tracker,
310 boolean_enums, val_str, val_str);
311 return option_def_and_value {*match, match_ctx};
315 if (have_delimiter)
316 error (_("Value given for `-%s' is not a boolean: %.*s"),
317 match->name, (int) (end - val_str), val_str);
318 /* The user didn't separate options from operands
319 using "--", so treat this unrecognized value as the
320 start of the operands. This makes "frame apply all
321 -past-main CMD" work. */
322 return option_def_and_value {*match, match_ctx};
324 else if (completion != nullptr && **args == '\0')
326 /* While "cmd -boolean [TAB]" only offers "on" and
327 "off", the boolean option actually accepts "1",
328 "yes", etc. as boolean values. We complete on all
329 of those instead of BOOLEAN_ENUMS here to make
330 these work:
332 "p -object 1[TAB]" -> "p -object 1 "
333 "p -object ye[TAB]" -> "p -object yes "
335 Etc. Note that it's important that the space is
336 auto-appended. Otherwise, if we only completed on
337 on/off here, then it might look to the user like
338 "1" isn't valid, like:
339 "p -object 1[TAB]" -> "p -object 1" (i.e., nothing happens).
341 static const char *const all_boolean_enums[] = {
342 "on", "off",
343 "yes", "no",
344 "enable", "disable",
345 "0", "1",
346 nullptr,
348 complete_on_enum (completion->tracker, all_boolean_enums,
349 val_str, val_str);
350 return {};
354 option_value val;
355 val.boolean = res;
356 return option_def_and_value {*match, match_ctx, val};
358 case var_uinteger:
359 case var_zuinteger_unlimited:
361 if (completion != nullptr)
363 if (**args == '\0')
365 /* Convenience to let the user know what the option
366 can accept. Note there's no common prefix between
367 the strings on purpose, so that readline doesn't do
368 a partial match. */
369 completion->tracker.add_completion
370 (make_unique_xstrdup ("NUMBER"));
371 completion->tracker.add_completion
372 (make_unique_xstrdup ("unlimited"));
373 return {};
375 else if (startswith ("unlimited", *args))
377 completion->tracker.add_completion
378 (make_unique_xstrdup ("unlimited"));
379 return {};
383 if (match->type == var_zuinteger_unlimited)
385 option_value val;
386 val.integer = parse_cli_var_zuinteger_unlimited (args, false);
387 return option_def_and_value {*match, match_ctx, val};
389 else
391 option_value val;
392 val.uinteger = parse_cli_var_uinteger (match->type, args, false);
393 return option_def_and_value {*match, match_ctx, val};
396 case var_enum:
398 if (completion != nullptr)
400 const char *after_arg = skip_to_space (*args);
401 if (*after_arg == '\0')
403 complete_on_enum (completion->tracker,
404 match->enums, *args, *args);
405 if (completion->tracker.have_completions ())
406 return {};
408 /* If we don't have completions, let the
409 non-completion path throw on invalid enum value
410 below, so that completion processing stops. */
414 if (check_for_argument (args, "--"))
416 /* Treat e.g., "backtrace -entry-values --" as if there
417 was no argument after "-entry-values". This makes
418 parse_cli_var_enum throw an error with a suggestion of
419 what are the valid options. */
420 args = nullptr;
423 option_value val;
424 val.enumeration = parse_cli_var_enum (args, match->enums);
425 return option_def_and_value {*match, match_ctx, val};
427 case var_string:
429 if (check_for_argument (args, "--"))
431 /* Treat e.g., "maint test-options -string --" as if there
432 was no argument after "-string". */
433 error (_("-%s requires an argument"), match->name);
436 const char *arg_start = *args;
437 std::string str = extract_string_maybe_quoted (args);
438 if (*args == arg_start)
439 error (_("-%s requires an argument"), match->name);
441 option_value val;
442 val.string = xstrdup (str.c_str ());
443 return option_def_and_value {*match, match_ctx, val};
446 default:
447 /* Not yet. */
448 gdb_assert_not_reached (_("option type not supported"));
451 return {};
454 /* See cli-option.h. */
456 bool
457 complete_options (completion_tracker &tracker,
458 const char **args,
459 process_options_mode mode,
460 gdb::array_view<const option_def_group> options_group)
462 const char *text = *args;
464 tracker.set_use_custom_word_point (true);
466 const char *delimiter = find_end_options_delimiter (text);
467 bool have_delimiter = delimiter != nullptr;
469 if (text[0] == '-' && (!have_delimiter || *delimiter == '\0'))
471 parse_option_completion_info completion_info {nullptr, tracker};
473 while (1)
475 *args = skip_spaces (*args);
476 completion_info.word = *args;
478 if (strcmp (*args, "-") == 0)
480 complete_on_options (options_group, tracker, *args + 1,
481 completion_info.word);
483 else if (strcmp (*args, "--") == 0)
485 tracker.add_completion (make_unique_xstrdup (*args));
487 else if (**args == '-')
489 gdb::optional<option_def_and_value> ov
490 = parse_option (options_group, mode, have_delimiter,
491 args, &completion_info);
492 if (!ov && !tracker.have_completions ())
494 tracker.advance_custom_word_point_by (*args - text);
495 return mode == PROCESS_OPTIONS_REQUIRE_DELIMITER;
498 if (ov
499 && ov->option.type == var_boolean
500 && !ov->value.has_value ())
502 /* Looked like a boolean option, but we failed to
503 parse the value. If this command requires a
504 delimiter, this value can't be the start of the
505 operands, so return true. Otherwise, if the
506 command doesn't require a delimiter return false
507 so that the caller tries to complete on the
508 operand. */
509 tracker.advance_custom_word_point_by (*args - text);
510 return mode == PROCESS_OPTIONS_REQUIRE_DELIMITER;
513 /* If we parsed an option with an argument, and reached
514 the end of the input string with no trailing space,
515 return true, so that our callers don't try to
516 complete anything by themselves. E.g., this makes it
517 so that with:
519 (gdb) frame apply all -limit 10[TAB]
521 we don't try to complete on command names. */
522 if (ov
523 && !tracker.have_completions ()
524 && **args == '\0'
525 && *args > text && !isspace ((*args)[-1]))
527 tracker.advance_custom_word_point_by
528 (*args - text);
529 return true;
532 /* If the caller passed in a context, then it is
533 interested in the option argument values. */
534 if (ov && ov->ctx != nullptr)
535 save_option_value_in_ctx (ov);
537 else
539 tracker.advance_custom_word_point_by
540 (completion_info.word - text);
542 /* If the command requires a delimiter, but we haven't
543 seen one, then return true, so that the caller
544 doesn't try to complete on whatever follows options,
545 which for these commands should only be done if
546 there's a delimiter. */
547 if (mode == PROCESS_OPTIONS_REQUIRE_DELIMITER
548 && !have_delimiter)
550 /* If we reached the end of the input string, then
551 offer all options, since that's all the user can
552 type (plus "--"). */
553 if (completion_info.word[0] == '\0')
554 complete_on_all_options (tracker, options_group);
555 return true;
557 else
558 return false;
561 if (tracker.have_completions ())
563 tracker.advance_custom_word_point_by
564 (completion_info.word - text);
565 return true;
569 else if (delimiter != nullptr)
571 tracker.advance_custom_word_point_by (delimiter - text);
572 *args = delimiter;
573 return false;
576 return false;
579 /* Save the parsed value in the option's context. */
581 static void
582 save_option_value_in_ctx (gdb::optional<option_def_and_value> &ov)
584 switch (ov->option.type)
586 case var_boolean:
588 bool value = ov->value.has_value () ? ov->value->boolean : true;
589 *ov->option.var_address.boolean (ov->option, ov->ctx) = value;
591 break;
592 case var_uinteger:
593 *ov->option.var_address.uinteger (ov->option, ov->ctx)
594 = ov->value->uinteger;
595 break;
596 case var_zuinteger_unlimited:
597 *ov->option.var_address.integer (ov->option, ov->ctx)
598 = ov->value->integer;
599 break;
600 case var_enum:
601 *ov->option.var_address.enumeration (ov->option, ov->ctx)
602 = ov->value->enumeration;
603 break;
604 case var_string:
605 *ov->option.var_address.string (ov->option, ov->ctx)
606 = ov->value->string;
607 ov->value->string = nullptr;
608 break;
609 default:
610 gdb_assert_not_reached ("unhandled option type");
614 /* See cli-option.h. */
616 bool
617 process_options (const char **args,
618 process_options_mode mode,
619 gdb::array_view<const option_def_group> options_group)
621 if (*args == nullptr)
622 return false;
624 /* If ARGS starts with "-", look for a "--" sequence. If one is
625 found, then interpret everything up until the "--" as
626 'gdb::option'-style command line options. Otherwise, interpret
627 ARGS as possibly the command's operands. */
628 bool have_delimiter = find_end_options_delimiter (*args) != nullptr;
630 if (mode == PROCESS_OPTIONS_REQUIRE_DELIMITER && !have_delimiter)
631 return false;
633 bool processed_any = false;
635 while (1)
637 *args = skip_spaces (*args);
639 auto ov = parse_option (options_group, mode, have_delimiter, args);
640 if (!ov)
642 if (processed_any)
643 return true;
644 return false;
647 processed_any = true;
649 save_option_value_in_ctx (ov);
653 /* Helper for build_help. Return a fragment of a help string showing
654 OPT's possible values. Returns NULL if OPT doesn't take an
655 argument. */
657 static const char *
658 get_val_type_str (const option_def &opt, std::string &buffer)
660 if (!opt.have_argument)
661 return nullptr;
663 switch (opt.type)
665 case var_boolean:
666 return "[on|off]";
667 case var_uinteger:
668 case var_zuinteger_unlimited:
669 return "NUMBER|unlimited";
670 case var_enum:
672 buffer = "";
673 for (size_t i = 0; opt.enums[i] != nullptr; i++)
675 if (i != 0)
676 buffer += "|";
677 buffer += opt.enums[i];
679 return buffer.c_str ();
681 case var_string:
682 return "STRING";
683 default:
684 return nullptr;
688 /* Helper for build_help. Appends an indented version of DOC into
689 HELP. */
691 static void
692 append_indented_doc (const char *doc, std::string &help)
694 const char *p = doc;
695 const char *n = strchr (p, '\n');
697 while (n != nullptr)
699 help += " ";
700 help.append (p, n - p + 1);
701 p = n + 1;
702 n = strchr (p, '\n');
704 help += " ";
705 help += p;
708 /* Fill HELP with an auto-generated "help" string fragment for
709 OPTIONS. */
711 static void
712 build_help_option (gdb::array_view<const option_def> options,
713 std::string &help)
715 std::string buffer;
717 for (const auto &o : options)
719 if (o.set_doc == nullptr)
720 continue;
722 help += " -";
723 help += o.name;
725 const char *val_type_str = get_val_type_str (o, buffer);
726 if (val_type_str != nullptr)
728 help += ' ';
729 help += val_type_str;
731 help += "\n";
732 append_indented_doc (o.set_doc, help);
733 if (o.help_doc != nullptr)
735 help += "\n";
736 append_indented_doc (o.help_doc, help);
741 /* See cli-option.h. */
743 std::string
744 build_help (const char *help_tmpl,
745 gdb::array_view<const option_def_group> options_group)
747 bool need_newlines = false;
748 std::string help_str;
750 const char *p = strstr (help_tmpl, "%OPTIONS%");
751 help_str.assign (help_tmpl, p);
753 for (const auto &grp : options_group)
754 for (const auto &opt : grp.options)
756 if (need_newlines)
757 help_str += "\n\n";
758 else
759 need_newlines = true;
760 build_help_option (opt, help_str);
763 p += strlen ("%OPTIONS%");
764 help_str.append (p);
766 return help_str;
769 /* See cli-option.h. */
771 void
772 add_setshow_cmds_for_options (command_class cmd_class,
773 void *data,
774 gdb::array_view<const option_def> options,
775 struct cmd_list_element **set_list,
776 struct cmd_list_element **show_list)
778 for (const auto &option : options)
780 if (option.type == var_boolean)
782 add_setshow_boolean_cmd (option.name, cmd_class,
783 option.var_address.boolean (option, data),
784 option.set_doc, option.show_doc,
785 option.help_doc,
786 nullptr, option.show_cmd_cb,
787 set_list, show_list);
789 else if (option.type == var_uinteger)
791 add_setshow_uinteger_cmd (option.name, cmd_class,
792 option.var_address.uinteger (option, data),
793 option.set_doc, option.show_doc,
794 option.help_doc,
795 nullptr, option.show_cmd_cb,
796 set_list, show_list);
798 else if (option.type == var_zuinteger_unlimited)
800 add_setshow_zuinteger_unlimited_cmd
801 (option.name, cmd_class,
802 option.var_address.integer (option, data),
803 option.set_doc, option.show_doc,
804 option.help_doc,
805 nullptr, option.show_cmd_cb,
806 set_list, show_list);
808 else if (option.type == var_enum)
810 add_setshow_enum_cmd (option.name, cmd_class,
811 option.enums,
812 option.var_address.enumeration (option, data),
813 option.set_doc, option.show_doc,
814 option.help_doc,
815 nullptr, option.show_cmd_cb,
816 set_list, show_list);
818 else if (option.type == var_string)
820 add_setshow_string_cmd (option.name, cmd_class,
821 option.var_address.string (option, data),
822 option.set_doc, option.show_doc,
823 option.help_doc,
824 nullptr, option.show_cmd_cb,
825 set_list, show_list);
827 else
828 gdb_assert_not_reached (_("option type not handled"));
832 } /* namespace option */
833 } /* namespace gdb */