1 /* Simulator option handling.
2 Copyright (C) 1996-2016 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
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/>. */
32 #include "libiberty.h"
33 #include "sim-options.h"
35 #include "sim-assert.h"
40 /* Add a set of options to the simulator.
41 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
42 This is intended to be called by modules in their `install' handler. */
45 sim_add_option_table (SIM_DESC sd, sim_cpu *cpu, const OPTION *table)
47 struct option_list *ol = ((struct option_list *)
48 xmalloc (sizeof (struct option_list)));
50 /* Note: The list is constructed in the reverse order we're called so
51 later calls will override earlier ones (in case that ever happens).
52 This is the intended behaviour. */
56 ol->next = CPU_OPTIONS (cpu);
58 CPU_OPTIONS (cpu) = ol;
62 ol->next = STATE_OPTIONS (sd);
64 STATE_OPTIONS (sd) = ol;
70 /* Standard option table.
71 Modules may specify additional ones.
72 The caller of sim_parse_args may also specify additional options
73 by calling sim_add_option_table first. */
75 static DECLARE_OPTION_HANDLER (standard_option_handler);
77 /* FIXME: We shouldn't print in --help output options that aren't usable.
78 Some fine tuning will be necessary. One can either move less general
79 options to another table or use a HAVE_FOO macro to ifdef out unavailable
82 /* ??? One might want to conditionally compile out the entries that
83 aren't enabled. There's a distinction, however, between options a
84 simulator can't support and options that haven't been configured in.
85 Certainly options a simulator can't support shouldn't appear in the
86 output of --help. Whether the same thing applies to options that haven't
87 been configured in or not isn't something I can get worked up over.
88 [Note that conditionally compiling them out might simply involve moving
89 the option to another table.]
90 If you decide to conditionally compile them out as well, delete this
91 comment and add a comment saying that that is the rule. */
94 OPTION_DEBUG_INSN = OPTION_START,
99 OPTION_ARCHITECTURE_INFO,
112 static const OPTION standard_options[] =
114 { {"verbose", no_argument, NULL, OPTION_VERBOSE},
115 'v', NULL, "Verbose output",
116 standard_option_handler, NULL },
118 { {"endian", required_argument, NULL, OPTION_ENDIAN},
119 'E', "big|little", "Set endianness",
120 standard_option_handler, NULL },
122 #ifdef SIM_HAVE_ENVIRONMENT
123 /* This option isn't supported unless all choices are supported in keeping
124 with the goal of not printing in --help output things the simulator can't
125 do [as opposed to things that just haven't been configured in]. */
126 { {"environment", required_argument, NULL, OPTION_ENVIRONMENT},
127 '\0', "user|virtual|operating", "Set running environment",
128 standard_option_handler },
131 { {"alignment", required_argument, NULL, OPTION_ALIGNMENT},
132 '\0', "strict|nonstrict|forced", "Set memory access alignment",
133 standard_option_handler },
135 { {"debug", no_argument, NULL, OPTION_DEBUG},
136 'D', NULL, "Print debugging messages",
137 standard_option_handler },
138 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
139 '\0', NULL, "Print instruction debugging messages",
140 standard_option_handler },
141 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
142 '\0', "FILE NAME", "Specify debugging output file",
143 standard_option_handler },
145 { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
146 '\0', "COMMAND", ""/*undocumented*/,
147 standard_option_handler },
149 { {"help", no_argument, NULL, OPTION_HELP},
150 'H', NULL, "Print help information",
151 standard_option_handler },
152 { {"version", no_argument, NULL, OPTION_VERSION},
153 '\0', NULL, "Print version information",
154 standard_option_handler },
156 { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
157 '\0', "MACHINE", "Specify the architecture to use",
158 standard_option_handler },
159 { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
160 '\0', NULL, "List supported architectures",
161 standard_option_handler },
162 { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
164 standard_option_handler },
166 { {"target", required_argument, NULL, OPTION_TARGET},
167 '\0', "BFDNAME", "Specify the object-code format for the object files",
168 standard_option_handler },
170 { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA},
172 "Use VMA or LMA addresses when loading image (default LMA)",
173 standard_option_handler, "load-{lma,vma}" },
174 { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA},
175 '\0', NULL, "", standard_option_handler, "" },
177 { {"sysroot", required_argument, NULL, OPTION_SYSROOT},
179 "Root for system calls with absolute file-names and cwd at start",
180 standard_option_handler, NULL },
182 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
186 standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
187 char *arg, int is_command)
191 switch ((STANDARD_OPTIONS) opt)
194 STATE_VERBOSE_P (sd) = 1;
198 if (strcmp (arg, "big") == 0)
200 if (WITH_TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
202 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
205 /* FIXME:wip: Need to set something in STATE_CONFIG. */
206 current_target_byte_order = BFD_ENDIAN_BIG;
208 else if (strcmp (arg, "little") == 0)
210 if (WITH_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
212 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
215 /* FIXME:wip: Need to set something in STATE_CONFIG. */
216 current_target_byte_order = BFD_ENDIAN_LITTLE;
220 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
225 case OPTION_ENVIRONMENT:
226 if (strcmp (arg, "user") == 0)
227 STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT;
228 else if (strcmp (arg, "virtual") == 0)
229 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
230 else if (strcmp (arg, "operating") == 0)
231 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
234 sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg);
237 if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
238 && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd))
241 switch (WITH_ENVIRONMENT)
243 case USER_ENVIRONMENT: type = "user"; break;
244 case VIRTUAL_ENVIRONMENT: type = "virtual"; break;
245 case OPERATING_ENVIRONMENT: type = "operating"; break;
247 sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n",
253 case OPTION_ALIGNMENT:
254 if (strcmp (arg, "strict") == 0)
256 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT)
258 current_alignment = STRICT_ALIGNMENT;
262 else if (strcmp (arg, "nonstrict") == 0)
264 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT)
266 current_alignment = NONSTRICT_ALIGNMENT;
270 else if (strcmp (arg, "forced") == 0)
272 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT)
274 current_alignment = FORCED_ALIGNMENT;
280 sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg);
283 switch (WITH_ALIGNMENT)
285 case STRICT_ALIGNMENT:
286 sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n");
288 case NONSTRICT_ALIGNMENT:
289 sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n");
291 case FORCED_ALIGNMENT:
292 sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n");
299 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
302 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
303 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
304 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
308 case OPTION_DEBUG_INSN :
310 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
313 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
314 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
318 case OPTION_DEBUG_FILE :
320 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
323 FILE *f = fopen (arg, "w");
327 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
330 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
331 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
335 case OPTION_DO_COMMAND:
336 sim_do_command (sd, arg);
339 case OPTION_ARCHITECTURE:
341 const struct bfd_arch_info *ap = bfd_scan_arch (arg);
344 sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
347 STATE_ARCHITECTURE (sd) = ap;
351 case OPTION_ARCHITECTURE_INFO:
353 const char **list = bfd_arch_list ();
357 sim_io_printf (sd, "Possible architectures:");
358 for (lp = list; *lp != NULL; lp++)
359 sim_io_printf (sd, " %s", *lp);
360 sim_io_printf (sd, "\n");
367 STATE_TARGET (sd) = xstrdup (arg);
371 case OPTION_LOAD_LMA:
373 STATE_LOAD_AT_LMA_P (sd) = 1;
377 case OPTION_LOAD_VMA:
379 STATE_LOAD_AT_LMA_P (sd) = 0;
384 sim_print_help (sd, is_command);
385 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
387 /* FIXME: 'twould be nice to do something similar if gdb. */
391 sim_io_printf (sd, "GNU simulator %s%s\n", PKGVERSION, version);
392 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
397 /* Don't leak memory in the odd event that there's lots of
398 --sysroot=... options. We treat "" specially since this
399 is the statically initialized value and cannot free it. */
400 if (simulator_sysroot[0] != '\0')
401 free (simulator_sysroot);
403 simulator_sysroot = xstrdup (arg);
405 simulator_sysroot = "";
412 /* Add the standard option list to the simulator. */
415 standard_install (SIM_DESC sd)
417 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
418 if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK)
420 STATE_LOAD_AT_LMA_P (sd) = 1;
424 /* Return non-zero if arg is a duplicate argument.
425 If ARG is NULL, initialize. */
427 #define ARG_HASH_SIZE 97
428 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
431 dup_arg_p (const char *arg)
434 static const char **arg_table = NULL;
438 if (arg_table == NULL)
439 arg_table = (const char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
440 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
444 hash = ARG_HASH (arg);
445 while (arg_table[hash] != NULL)
447 if (strcmp (arg, arg_table[hash]) == 0)
449 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
450 don't check if the table is full. */
451 if (++hash == ARG_HASH_SIZE)
454 arg_table[hash] = arg;
458 /* Called by sim_open to parse the arguments. */
461 sim_parse_args (SIM_DESC sd, char **argv)
463 int c, i, argc, num_opts;
464 char *p, *short_options;
465 /* The `val' option struct entry is dynamically assigned for options that
466 only come in the long form. ORIG_VAL is used to get the original value
469 struct option *lp, *long_options;
470 const struct option_list *ol;
472 OPTION_HANDLER **handlers;
474 SIM_RC result = SIM_RC_OK;
476 /* Count the number of arguments. */
477 for (argc = 0; argv[argc] != NULL; ++argc)
480 /* Count the number of options. */
482 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
483 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
485 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
486 for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next)
487 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
490 /* Initialize duplicate argument checker. */
491 (void) dup_arg_p (NULL);
493 /* Build the option table for getopt. */
495 long_options = NZALLOC (struct option, num_opts + 1);
497 short_options = NZALLOC (char, num_opts * 3 + 1);
499 handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts);
500 orig_val = NZALLOC (int, OPTION_START + num_opts);
501 opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts);
503 /* Set '+' as first char so argument permutation isn't done. This
504 is done to stop getopt_long returning options that appear after
505 the target program. Such options should be passed unchanged into
506 the program image. */
509 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
510 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
512 if (dup_arg_p (opt->opt.name))
514 if (opt->shortopt != 0)
516 *p++ = opt->shortopt;
517 if (opt->opt.has_arg == required_argument)
519 else if (opt->opt.has_arg == optional_argument)
520 { *p++ = ':'; *p++ = ':'; }
521 handlers[(unsigned char) opt->shortopt] = opt->handler;
522 if (opt->opt.val != 0)
523 orig_val[(unsigned char) opt->shortopt] = opt->opt.val;
525 orig_val[(unsigned char) opt->shortopt] = opt->shortopt;
527 if (opt->opt.name != NULL)
530 /* Dynamically assign `val' numbers for long options. */
532 handlers[lp->val] = opt->handler;
533 orig_val[lp->val] = opt->opt.val;
534 opt_cpu[lp->val] = NULL;
539 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
541 sim_cpu *cpu = STATE_CPU (sd, c);
542 for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next)
543 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
545 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
546 on the need for dup_arg_p checking. Maybe in the future it'll be
547 needed so this is just commented out, and not deleted. */
548 if (dup_arg_p (opt->opt.name))
551 /* Don't allow short versions of cpu specific options for now. */
552 if (opt->shortopt != 0)
554 sim_io_eprintf (sd, "internal error, short cpu specific option");
555 result = SIM_RC_FAIL;
558 if (opt->opt.name != NULL)
562 /* Prepend --<cpuname>- to the option. */
563 if (asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name) < 0)
565 sim_io_eprintf (sd, "internal error, out of memory");
566 result = SIM_RC_FAIL;
570 /* Dynamically assign `val' numbers for long options. */
572 handlers[lp->val] = opt->handler;
573 orig_val[lp->val] = opt->opt.val;
574 opt_cpu[lp->val] = cpu;
580 /* Terminate the short and long option lists. */
584 /* Ensure getopt is initialized. */
591 optc = getopt_long (argc, argv, short_options, long_options, &longind);
594 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
595 STATE_PROG_ARGV (sd) = dupargv (argv + optind);
600 result = SIM_RC_FAIL;
604 if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
606 result = SIM_RC_FAIL;
612 free (short_options);
619 /* Utility of sim_print_help to print a list of option tables. */
622 print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command)
626 for ( ; ol != NULL; ol = ol->next)
627 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
629 const int indent = 30;
633 if (dup_arg_p (opt->opt.name))
636 if (opt->doc == NULL)
639 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
642 sim_io_printf (sd, " ");
647 /* list any short options (aliases) for the current OPT */
653 if (o->shortopt != '\0')
655 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
656 len += (comma ? 2 : 0) + 2;
659 if (o->opt.has_arg == optional_argument)
661 sim_io_printf (sd, "[%s]", o->arg);
662 len += 1 + strlen (o->arg) + 1;
666 sim_io_printf (sd, " %s", o->arg);
667 len += 1 + strlen (o->arg);
674 while (OPTION_VALID_P (o) && o->doc == NULL);
677 /* list any long options (aliases) for the current OPT */
682 const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL;
683 if (o->doc_name != NULL)
689 sim_io_printf (sd, "%s%s%s%s%s",
691 is_command ? "" : "--",
692 cpu ? cpu_prefix : "",
695 len += ((comma ? 2 : 0)
696 + (is_command ? 0 : 2)
700 if (o->opt.has_arg == optional_argument)
702 sim_io_printf (sd, "[=%s]", o->arg);
703 len += 2 + strlen (o->arg) + 1;
707 sim_io_printf (sd, " %s", o->arg);
708 len += 1 + strlen (o->arg);
715 while (OPTION_VALID_P (o) && o->doc == NULL);
719 sim_io_printf (sd, "\n%*s", indent, "");
722 sim_io_printf (sd, "%*s", indent - len, "");
724 /* print the description, word wrap long lines */
726 const char *chp = opt->doc;
727 unsigned doc_width = 80 - indent;
728 while (strlen (chp) >= doc_width) /* some slack */
730 const char *end = chp + doc_width - 1;
731 while (end > chp && !isspace (*end))
734 end = chp + doc_width - 1;
735 /* The cast should be ok - its distances between to
736 points in a string. */
737 sim_io_printf (sd, "%.*s\n%*s", (int) (end - chp), chp, indent,
740 while (isspace (*chp) && *chp != '\0')
743 sim_io_printf (sd, "%s\n", chp);
748 /* Print help messages for the options. */
751 sim_print_help (SIM_DESC sd, int is_command)
753 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
754 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
757 /* Initialize duplicate argument checker. */
758 (void) dup_arg_p (NULL);
760 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
761 sim_io_printf (sd, "Options:\n");
763 sim_io_printf (sd, "Commands:\n");
765 print_help (sd, NULL, STATE_OPTIONS (sd), is_command);
766 sim_io_printf (sd, "\n");
768 /* Print cpu-specific options. */
772 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
774 sim_cpu *cpu = STATE_CPU (sd, i);
775 if (CPU_OPTIONS (cpu) == NULL)
777 sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu));
778 print_help (sd, cpu, CPU_OPTIONS (cpu), is_command);
779 sim_io_printf (sd, "\n");
783 sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
784 STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
785 sim_io_printf (sd, " may not be applicable\n");
787 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
789 sim_io_printf (sd, "\n");
790 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
791 sim_io_printf (sd, " Note: Very few simulators support this.\n");
795 /* Utility of sim_args_command to find the closest match for a command.
796 Commands that have "-" in them can be specified as separate words.
797 e.g. sim memory-region 0x800000,0x4000
798 or sim memory region 0x800000,0x4000
799 If CPU is non-null, use its option table list, otherwise use the main one.
800 *PARGI is where to start looking in ARGV. It is updated to point past
803 static const OPTION *
804 find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi)
806 const struct option_list *ol;
808 /* most recent option match */
809 const OPTION *matching_opt = NULL;
810 int matching_argi = -1;
813 ol = CPU_OPTIONS (cpu);
815 ol = STATE_OPTIONS (sd);
817 /* Skip passed elements specified by *PARGI. */
820 for ( ; ol != NULL; ol = ol->next)
821 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
824 const char *name = opt->opt.name;
827 while (argv [argi] != NULL
828 && strncmp (name, argv [argi], strlen (argv [argi])) == 0)
830 name = &name [strlen (argv[argi])];
833 /* leading match ...<a-b-c>-d-e-f - continue search */
834 name ++; /* skip `-' */
838 else if (name [0] == '\0')
840 /* exact match ...<a-b-c-d-e-f> - better than before? */
841 if (argi > matching_argi)
843 matching_argi = argi;
853 *pargi = matching_argi;
858 complete_option_list (char **ret, size_t *cnt, const struct option_list *ol,
859 const char *text, const char *word)
861 const OPTION *opt = NULL;
863 size_t len = strlen (word);
865 for ( ; ol != NULL; ol = ol->next)
866 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
868 const char *name = opt->opt.name;
870 /* A long option to match against? */
874 /* Does this option actually match? */
875 if (strncmp (name, word, len))
878 ret = xrealloc (ret, ++*cnt * sizeof (ret[0]));
879 ret[*cnt - 2] = xstrdup (name);
885 /* All leading text is stored in @text, while the current word being
886 completed is stored in @word. Trailing text of @word is not. */
889 sim_complete_command (SIM_DESC sd, const char *text, const char *word)
895 /* Only complete first word for now. */
899 cpu = STATE_CPU (sd, 0);
901 ret = complete_option_list (ret, &cnt, CPU_OPTIONS (cpu), text, word);
902 ret = complete_option_list (ret, &cnt, STATE_OPTIONS (sd), text, word);
910 sim_args_command (SIM_DESC sd, const char *cmd)
912 /* something to do? */
914 return SIM_RC_OK; /* FIXME - perhaps help would be better */
918 /* user specified -<opt> ... form? */
919 char **argv = buildargv (cmd);
920 SIM_RC rc = sim_parse_args (sd, argv);
926 char **argv = buildargv (cmd);
927 const OPTION *matching_opt = NULL;
931 if (argv [0] == NULL)
934 return SIM_RC_OK; /* FIXME - perhaps help would be better */
937 /* First check for a cpu selector. */
939 char *cpu_name = xstrdup (argv[0]);
940 char *hyphen = strchr (cpu_name, '-');
943 cpu = sim_cpu_lookup (sd, cpu_name);
946 /* If <cpuname>-<command>, point argv[0] at <command>. */
950 argv[0] += hyphen - cpu_name + 1;
954 matching_opt = find_match (sd, cpu, argv, &matching_argi);
955 /* If hyphen found restore argv[0]. */
957 argv[0] -= hyphen - cpu_name + 1;
962 /* If that failed, try the main table. */
963 if (matching_opt == NULL)
966 matching_opt = find_match (sd, NULL, argv, &matching_argi);
969 if (matching_opt != NULL)
971 switch (matching_opt->opt.has_arg)
974 if (argv [matching_argi + 1] == NULL)
975 matching_opt->handler (sd, cpu, matching_opt->opt.val,
976 NULL, 1/*is_command*/);
978 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
979 matching_opt->opt.name);
981 case optional_argument:
982 if (argv [matching_argi + 1] == NULL)
983 matching_opt->handler (sd, cpu, matching_opt->opt.val,
984 NULL, 1/*is_command*/);
985 else if (argv [matching_argi + 2] == NULL)
986 matching_opt->handler (sd, cpu, matching_opt->opt.val,
987 argv [matching_argi + 1], 1/*is_command*/);
989 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
990 matching_opt->opt.name);
992 case required_argument:
993 if (argv [matching_argi + 1] == NULL)
994 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
995 matching_opt->opt.name);
996 else if (argv [matching_argi + 2] == NULL)
997 matching_opt->handler (sd, cpu, matching_opt->opt.val,
998 argv [matching_argi + 1], 1/*is_command*/);
1000 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
1001 matching_opt->opt.name);
1010 /* didn't find anything that remotly matched */