1 /* Simulator option handling.
2 Copyright (C) 1996-2015 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 #ifdef SIM_HANDLES_LMA
171 { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA},
174 "Use VMA or LMA addresses when loading image (default LMA)",
176 "Use VMA or LMA addresses when loading image (default VMA)",
178 standard_option_handler, "load-{lma,vma}" },
179 { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA},
180 '\0', NULL, "", standard_option_handler, "" },
183 { {"sysroot", required_argument, NULL, OPTION_SYSROOT},
185 "Root for system calls with absolute file-names and cwd at start",
186 standard_option_handler, NULL },
188 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
192 standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
193 char *arg, int is_command)
197 switch ((STANDARD_OPTIONS) opt)
200 STATE_VERBOSE_P (sd) = 1;
204 if (strcmp (arg, "big") == 0)
206 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
208 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
211 /* FIXME:wip: Need to set something in STATE_CONFIG. */
212 current_target_byte_order = BIG_ENDIAN;
214 else if (strcmp (arg, "little") == 0)
216 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
218 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
221 /* FIXME:wip: Need to set something in STATE_CONFIG. */
222 current_target_byte_order = LITTLE_ENDIAN;
226 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
231 case OPTION_ENVIRONMENT:
232 if (strcmp (arg, "user") == 0)
233 STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT;
234 else if (strcmp (arg, "virtual") == 0)
235 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
236 else if (strcmp (arg, "operating") == 0)
237 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
240 sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg);
243 if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
244 && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd))
247 switch (WITH_ENVIRONMENT)
249 case USER_ENVIRONMENT: type = "user"; break;
250 case VIRTUAL_ENVIRONMENT: type = "virtual"; break;
251 case OPERATING_ENVIRONMENT: type = "operating"; break;
253 sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n",
259 case OPTION_ALIGNMENT:
260 if (strcmp (arg, "strict") == 0)
262 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT)
264 current_alignment = STRICT_ALIGNMENT;
268 else if (strcmp (arg, "nonstrict") == 0)
270 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT)
272 current_alignment = NONSTRICT_ALIGNMENT;
276 else if (strcmp (arg, "forced") == 0)
278 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT)
280 current_alignment = FORCED_ALIGNMENT;
286 sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg);
289 switch (WITH_ALIGNMENT)
291 case STRICT_ALIGNMENT:
292 sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n");
294 case NONSTRICT_ALIGNMENT:
295 sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n");
297 case FORCED_ALIGNMENT:
298 sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n");
305 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
308 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
309 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
310 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
314 case OPTION_DEBUG_INSN :
316 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
319 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
320 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
324 case OPTION_DEBUG_FILE :
326 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
329 FILE *f = fopen (arg, "w");
333 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
336 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
337 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
341 case OPTION_DO_COMMAND:
342 sim_do_command (sd, arg);
345 case OPTION_ARCHITECTURE:
347 const struct bfd_arch_info *ap = bfd_scan_arch (arg);
350 sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
353 STATE_ARCHITECTURE (sd) = ap;
357 case OPTION_ARCHITECTURE_INFO:
359 const char **list = bfd_arch_list ();
363 sim_io_printf (sd, "Possible architectures:");
364 for (lp = list; *lp != NULL; lp++)
365 sim_io_printf (sd, " %s", *lp);
366 sim_io_printf (sd, "\n");
373 STATE_TARGET (sd) = xstrdup (arg);
377 case OPTION_LOAD_LMA:
379 STATE_LOAD_AT_LMA_P (sd) = 1;
383 case OPTION_LOAD_VMA:
385 STATE_LOAD_AT_LMA_P (sd) = 0;
390 sim_print_help (sd, is_command);
391 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
393 /* FIXME: 'twould be nice to do something similar if gdb. */
397 sim_io_printf (sd, "GNU simulator %s%s\n", PKGVERSION, version);
398 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
403 /* Don't leak memory in the odd event that there's lots of
404 --sysroot=... options. We treat "" specially since this
405 is the statically initialized value and cannot free it. */
406 if (simulator_sysroot[0] != '\0')
407 free (simulator_sysroot);
409 simulator_sysroot = xstrdup (arg);
411 simulator_sysroot = "";
418 /* Add the standard option list to the simulator. */
421 standard_install (SIM_DESC sd)
423 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
424 if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK)
426 #ifdef SIM_HANDLES_LMA
427 STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA;
432 /* Return non-zero if arg is a duplicate argument.
433 If ARG is NULL, initialize. */
435 #define ARG_HASH_SIZE 97
436 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
439 dup_arg_p (const char *arg)
442 static const char **arg_table = NULL;
446 if (arg_table == NULL)
447 arg_table = (const char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
448 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
452 hash = ARG_HASH (arg);
453 while (arg_table[hash] != NULL)
455 if (strcmp (arg, arg_table[hash]) == 0)
457 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
458 don't check if the table is full. */
459 if (++hash == ARG_HASH_SIZE)
462 arg_table[hash] = arg;
466 /* Called by sim_open to parse the arguments. */
469 sim_parse_args (SIM_DESC sd, char **argv)
471 int c, i, argc, num_opts;
472 char *p, *short_options;
473 /* The `val' option struct entry is dynamically assigned for options that
474 only come in the long form. ORIG_VAL is used to get the original value
477 struct option *lp, *long_options;
478 const struct option_list *ol;
480 OPTION_HANDLER **handlers;
482 SIM_RC result = SIM_RC_OK;
484 /* Count the number of arguments. */
485 for (argc = 0; argv[argc] != NULL; ++argc)
488 /* Count the number of options. */
490 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
491 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
493 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
494 for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next)
495 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
498 /* Initialize duplicate argument checker. */
499 (void) dup_arg_p (NULL);
501 /* Build the option table for getopt. */
503 long_options = NZALLOC (struct option, num_opts + 1);
505 short_options = NZALLOC (char, num_opts * 3 + 1);
507 handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts);
508 orig_val = NZALLOC (int, OPTION_START + num_opts);
509 opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts);
511 /* Set '+' as first char so argument permutation isn't done. This
512 is done to stop getopt_long returning options that appear after
513 the target program. Such options should be passed unchanged into
514 the program image. */
517 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
518 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
520 if (dup_arg_p (opt->opt.name))
522 if (opt->shortopt != 0)
524 *p++ = opt->shortopt;
525 if (opt->opt.has_arg == required_argument)
527 else if (opt->opt.has_arg == optional_argument)
528 { *p++ = ':'; *p++ = ':'; }
529 handlers[(unsigned char) opt->shortopt] = opt->handler;
530 if (opt->opt.val != 0)
531 orig_val[(unsigned char) opt->shortopt] = opt->opt.val;
533 orig_val[(unsigned char) opt->shortopt] = opt->shortopt;
535 if (opt->opt.name != NULL)
538 /* Dynamically assign `val' numbers for long options. */
540 handlers[lp->val] = opt->handler;
541 orig_val[lp->val] = opt->opt.val;
542 opt_cpu[lp->val] = NULL;
547 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
549 sim_cpu *cpu = STATE_CPU (sd, c);
550 for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next)
551 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
553 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
554 on the need for dup_arg_p checking. Maybe in the future it'll be
555 needed so this is just commented out, and not deleted. */
556 if (dup_arg_p (opt->opt.name))
559 /* Don't allow short versions of cpu specific options for now. */
560 if (opt->shortopt != 0)
562 sim_io_eprintf (sd, "internal error, short cpu specific option");
563 result = SIM_RC_FAIL;
566 if (opt->opt.name != NULL)
570 /* Prepend --<cpuname>- to the option. */
571 if (asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name) < 0)
573 sim_io_eprintf (sd, "internal error, out of memory");
574 result = SIM_RC_FAIL;
578 /* Dynamically assign `val' numbers for long options. */
580 handlers[lp->val] = opt->handler;
581 orig_val[lp->val] = opt->opt.val;
582 opt_cpu[lp->val] = cpu;
588 /* Terminate the short and long option lists. */
592 /* Ensure getopt is initialized. */
599 optc = getopt_long (argc, argv, short_options, long_options, &longind);
602 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
603 STATE_PROG_ARGV (sd) = dupargv (argv + optind);
608 result = SIM_RC_FAIL;
612 if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
614 result = SIM_RC_FAIL;
620 free (short_options);
627 /* Utility of sim_print_help to print a list of option tables. */
630 print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command)
634 for ( ; ol != NULL; ol = ol->next)
635 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
637 const int indent = 30;
641 if (dup_arg_p (opt->opt.name))
644 if (opt->doc == NULL)
647 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
650 sim_io_printf (sd, " ");
655 /* list any short options (aliases) for the current OPT */
661 if (o->shortopt != '\0')
663 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
664 len += (comma ? 2 : 0) + 2;
667 if (o->opt.has_arg == optional_argument)
669 sim_io_printf (sd, "[%s]", o->arg);
670 len += 1 + strlen (o->arg) + 1;
674 sim_io_printf (sd, " %s", o->arg);
675 len += 1 + strlen (o->arg);
682 while (OPTION_VALID_P (o) && o->doc == NULL);
685 /* list any long options (aliases) for the current OPT */
690 const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL;
691 if (o->doc_name != NULL)
697 sim_io_printf (sd, "%s%s%s%s%s",
699 is_command ? "" : "--",
700 cpu ? cpu_prefix : "",
703 len += ((comma ? 2 : 0)
704 + (is_command ? 0 : 2)
708 if (o->opt.has_arg == optional_argument)
710 sim_io_printf (sd, "[=%s]", o->arg);
711 len += 2 + strlen (o->arg) + 1;
715 sim_io_printf (sd, " %s", o->arg);
716 len += 1 + strlen (o->arg);
723 while (OPTION_VALID_P (o) && o->doc == NULL);
727 sim_io_printf (sd, "\n%*s", indent, "");
730 sim_io_printf (sd, "%*s", indent - len, "");
732 /* print the description, word wrap long lines */
734 const char *chp = opt->doc;
735 unsigned doc_width = 80 - indent;
736 while (strlen (chp) >= doc_width) /* some slack */
738 const char *end = chp + doc_width - 1;
739 while (end > chp && !isspace (*end))
742 end = chp + doc_width - 1;
743 /* The cast should be ok - its distances between to
744 points in a string. */
745 sim_io_printf (sd, "%.*s\n%*s", (int) (end - chp), chp, indent,
748 while (isspace (*chp) && *chp != '\0')
751 sim_io_printf (sd, "%s\n", chp);
756 /* Print help messages for the options. */
759 sim_print_help (SIM_DESC sd, int is_command)
761 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
762 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
765 /* Initialize duplicate argument checker. */
766 (void) dup_arg_p (NULL);
768 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
769 sim_io_printf (sd, "Options:\n");
771 sim_io_printf (sd, "Commands:\n");
773 print_help (sd, NULL, STATE_OPTIONS (sd), is_command);
774 sim_io_printf (sd, "\n");
776 /* Print cpu-specific options. */
780 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
782 sim_cpu *cpu = STATE_CPU (sd, i);
783 if (CPU_OPTIONS (cpu) == NULL)
785 sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu));
786 print_help (sd, cpu, CPU_OPTIONS (cpu), is_command);
787 sim_io_printf (sd, "\n");
791 sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
792 STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
793 sim_io_printf (sd, " may not be applicable\n");
795 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
797 sim_io_printf (sd, "\n");
798 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
799 sim_io_printf (sd, " Note: Very few simulators support this.\n");
803 /* Utility of sim_args_command to find the closest match for a command.
804 Commands that have "-" in them can be specified as separate words.
805 e.g. sim memory-region 0x800000,0x4000
806 or sim memory region 0x800000,0x4000
807 If CPU is non-null, use its option table list, otherwise use the main one.
808 *PARGI is where to start looking in ARGV. It is updated to point past
811 static const OPTION *
812 find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi)
814 const struct option_list *ol;
816 /* most recent option match */
817 const OPTION *matching_opt = NULL;
818 int matching_argi = -1;
821 ol = CPU_OPTIONS (cpu);
823 ol = STATE_OPTIONS (sd);
825 /* Skip passed elements specified by *PARGI. */
828 for ( ; ol != NULL; ol = ol->next)
829 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
832 const char *name = opt->opt.name;
835 while (argv [argi] != NULL
836 && strncmp (name, argv [argi], strlen (argv [argi])) == 0)
838 name = &name [strlen (argv[argi])];
841 /* leading match ...<a-b-c>-d-e-f - continue search */
842 name ++; /* skip `-' */
846 else if (name [0] == '\0')
848 /* exact match ...<a-b-c-d-e-f> - better than before? */
849 if (argi > matching_argi)
851 matching_argi = argi;
861 *pargi = matching_argi;
866 complete_option_list (char **ret, size_t *cnt, const struct option_list *ol,
867 const char *text, const char *word)
869 const OPTION *opt = NULL;
871 size_t len = strlen (word);
873 for ( ; ol != NULL; ol = ol->next)
874 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
876 const char *name = opt->opt.name;
878 /* A long option to match against? */
882 /* Does this option actually match? */
883 if (strncmp (name, word, len))
886 ret = xrealloc (ret, ++*cnt * sizeof (ret[0]));
887 ret[*cnt - 2] = xstrdup (name);
893 /* All leading text is stored in @text, while the current word being
894 completed is stored in @word. Trailing text of @word is not. */
897 sim_complete_command (SIM_DESC sd, const char *text, const char *word)
903 /* Only complete first word for now. */
907 cpu = STATE_CPU (sd, 0);
909 ret = complete_option_list (ret, &cnt, CPU_OPTIONS (cpu), text, word);
910 ret = complete_option_list (ret, &cnt, STATE_OPTIONS (sd), text, word);
918 sim_args_command (SIM_DESC sd, const char *cmd)
920 /* something to do? */
922 return SIM_RC_OK; /* FIXME - perhaps help would be better */
926 /* user specified -<opt> ... form? */
927 char **argv = buildargv (cmd);
928 SIM_RC rc = sim_parse_args (sd, argv);
934 char **argv = buildargv (cmd);
935 const OPTION *matching_opt = NULL;
939 if (argv [0] == NULL)
942 return SIM_RC_OK; /* FIXME - perhaps help would be better */
945 /* First check for a cpu selector. */
947 char *cpu_name = xstrdup (argv[0]);
948 char *hyphen = strchr (cpu_name, '-');
951 cpu = sim_cpu_lookup (sd, cpu_name);
954 /* If <cpuname>-<command>, point argv[0] at <command>. */
958 argv[0] += hyphen - cpu_name + 1;
962 matching_opt = find_match (sd, cpu, argv, &matching_argi);
963 /* If hyphen found restore argv[0]. */
965 argv[0] -= hyphen - cpu_name + 1;
970 /* If that failed, try the main table. */
971 if (matching_opt == NULL)
974 matching_opt = find_match (sd, NULL, argv, &matching_argi);
977 if (matching_opt != NULL)
979 switch (matching_opt->opt.has_arg)
982 if (argv [matching_argi + 1] == NULL)
983 matching_opt->handler (sd, cpu, matching_opt->opt.val,
984 NULL, 1/*is_command*/);
986 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
987 matching_opt->opt.name);
989 case optional_argument:
990 if (argv [matching_argi + 1] == NULL)
991 matching_opt->handler (sd, cpu, matching_opt->opt.val,
992 NULL, 1/*is_command*/);
993 else if (argv [matching_argi + 2] == NULL)
994 matching_opt->handler (sd, cpu, matching_opt->opt.val,
995 argv [matching_argi + 1], 1/*is_command*/);
997 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
998 matching_opt->opt.name);
1000 case required_argument:
1001 if (argv [matching_argi + 1] == NULL)
1002 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
1003 matching_opt->opt.name);
1004 else if (argv [matching_argi + 2] == NULL)
1005 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1006 argv [matching_argi + 1], 1/*is_command*/);
1008 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
1009 matching_opt->opt.name);
1018 /* didn't find anything that remotly matched */