1 /* Simulator option handling.
2 Copyright (C) 1996, 1997, 2004, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
6 This file is part of GDB, the GNU debugger.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
33 #include "libiberty.h"
34 #include "sim-options.h"
36 #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,
105 #ifdef SIM_HAVE_FLATMEM
109 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
119 static const OPTION standard_options[] =
121 { {"verbose", no_argument, NULL, OPTION_VERBOSE},
122 'v', NULL, "Verbose output",
123 standard_option_handler },
125 { {"endian", required_argument, NULL, OPTION_ENDIAN},
126 'E', "big|little", "Set endianness",
127 standard_option_handler },
129 #ifdef SIM_HAVE_ENVIRONMENT
130 /* This option isn't supported unless all choices are supported in keeping
131 with the goal of not printing in --help output things the simulator can't
132 do [as opposed to things that just haven't been configured in]. */
133 { {"environment", required_argument, NULL, OPTION_ENVIRONMENT},
134 '\0', "user|virtual|operating", "Set running environment",
135 standard_option_handler },
138 { {"alignment", required_argument, NULL, OPTION_ALIGNMENT},
139 '\0', "strict|nonstrict|forced", "Set memory access alignment",
140 standard_option_handler },
142 { {"debug", no_argument, NULL, OPTION_DEBUG},
143 'D', NULL, "Print debugging messages",
144 standard_option_handler },
145 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
146 '\0', NULL, "Print instruction debugging messages",
147 standard_option_handler },
148 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
149 '\0', "FILE NAME", "Specify debugging output file",
150 standard_option_handler },
152 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
153 { {"h8300h", no_argument, NULL, OPTION_H8300H},
154 'h', NULL, "Indicate the CPU is H8/300H",
155 standard_option_handler },
156 { {"h8300s", no_argument, NULL, OPTION_H8300S},
157 'S', NULL, "Indicate the CPU is H8S",
158 standard_option_handler },
159 { {"h8300sx", no_argument, NULL, OPTION_H8300SX},
160 'x', NULL, "Indicate the CPU is H8SX",
161 standard_option_handler },
164 #ifdef SIM_HAVE_FLATMEM
165 { {"mem-size", required_argument, NULL, OPTION_MEM_SIZE},
166 'm', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
167 "Specify memory size", standard_option_handler },
170 { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
171 '\0', "COMMAND", ""/*undocumented*/,
172 standard_option_handler },
174 { {"help", no_argument, NULL, OPTION_HELP},
175 'H', NULL, "Print help information",
176 standard_option_handler },
178 { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
179 '\0', "MACHINE", "Specify the architecture to use",
180 standard_option_handler },
181 { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
182 '\0', NULL, "List supported architectures",
183 standard_option_handler },
184 { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
186 standard_option_handler },
188 { {"target", required_argument, NULL, OPTION_TARGET},
189 '\0', "BFDNAME", "Specify the object-code format for the object files",
190 standard_option_handler },
192 #ifdef SIM_HANDLES_LMA
193 { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA},
196 "Use VMA or LMA addresses when loading image (default LMA)",
198 "Use VMA or LMA addresses when loading image (default VMA)",
200 standard_option_handler, "load-{lma,vma}" },
201 { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA},
202 '\0', NULL, "", standard_option_handler, "" },
205 { {"sysroot", required_argument, NULL, OPTION_SYSROOT},
207 "Root for system calls with absolute file-names and cwd at start",
208 standard_option_handler },
210 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
214 standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
215 char *arg, int is_command)
219 switch ((STANDARD_OPTIONS) opt)
222 STATE_VERBOSE_P (sd) = 1;
226 if (strcmp (arg, "big") == 0)
228 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
230 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
233 /* FIXME:wip: Need to set something in STATE_CONFIG. */
234 current_target_byte_order = BIG_ENDIAN;
236 else if (strcmp (arg, "little") == 0)
238 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
240 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
243 /* FIXME:wip: Need to set something in STATE_CONFIG. */
244 current_target_byte_order = LITTLE_ENDIAN;
248 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
253 case OPTION_ENVIRONMENT:
254 if (strcmp (arg, "user") == 0)
255 STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT;
256 else if (strcmp (arg, "virtual") == 0)
257 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
258 else if (strcmp (arg, "operating") == 0)
259 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
262 sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg);
265 if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
266 && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd))
269 switch (WITH_ENVIRONMENT)
271 case USER_ENVIRONMENT: type = "user"; break;
272 case VIRTUAL_ENVIRONMENT: type = "virtual"; break;
273 case OPERATING_ENVIRONMENT: type = "operating"; break;
275 sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n",
281 case OPTION_ALIGNMENT:
282 if (strcmp (arg, "strict") == 0)
284 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT)
286 current_alignment = STRICT_ALIGNMENT;
290 else if (strcmp (arg, "nonstrict") == 0)
292 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT)
294 current_alignment = NONSTRICT_ALIGNMENT;
298 else if (strcmp (arg, "forced") == 0)
300 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT)
302 current_alignment = FORCED_ALIGNMENT;
308 sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg);
311 switch (WITH_ALIGNMENT)
313 case STRICT_ALIGNMENT:
314 sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n");
316 case NONSTRICT_ALIGNMENT:
317 sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n");
319 case FORCED_ALIGNMENT:
320 sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n");
327 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
330 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
331 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
332 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
336 case OPTION_DEBUG_INSN :
338 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
341 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
342 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
346 case OPTION_DEBUG_FILE :
348 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
351 FILE *f = fopen (arg, "w");
355 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
358 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
359 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
363 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
365 set_h8300h (bfd_mach_h8300h);
368 set_h8300h (bfd_mach_h8300s);
371 set_h8300h (bfd_mach_h8300sx);
375 #ifdef SIM_HAVE_FLATMEM
376 case OPTION_MEM_SIZE:
379 unsigned long ul = strtol (arg, &endp, 0);
383 case 'k': case 'K': size <<= 10; break;
384 case 'm': case 'M': size <<= 20; break;
385 case 'g': case 'G': size <<= 30; break;
386 case ' ': case '\0': case '\t': break;
389 sim_io_eprintf (sd, "Ignoring strange character at end of memory size: %c\n", * endp);
393 /* 16384: some minimal amount */
394 if (! isdigit (arg[0]) || ul < 16384)
396 sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
399 STATE_MEM_SIZE (sd) = ul;
404 case OPTION_DO_COMMAND:
405 sim_do_command (sd, arg);
408 case OPTION_ARCHITECTURE:
410 const struct bfd_arch_info *ap = bfd_scan_arch (arg);
413 sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
416 STATE_ARCHITECTURE (sd) = ap;
420 case OPTION_ARCHITECTURE_INFO:
422 const char **list = bfd_arch_list();
426 sim_io_printf (sd, "Possible architectures:");
427 for (lp = list; *lp != NULL; lp++)
428 sim_io_printf (sd, " %s", *lp);
429 sim_io_printf (sd, "\n");
436 STATE_TARGET (sd) = xstrdup (arg);
440 case OPTION_LOAD_LMA:
442 STATE_LOAD_AT_LMA_P (sd) = 1;
446 case OPTION_LOAD_VMA:
448 STATE_LOAD_AT_LMA_P (sd) = 0;
453 sim_print_help (sd, is_command);
454 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
456 /* FIXME: 'twould be nice to do something similar if gdb. */
460 /* Don't leak memory in the odd event that there's lots of
461 --sysroot=... options. */
462 if (simulator_sysroot[0] != '\0' && arg[0] != '\0')
463 free (simulator_sysroot);
464 simulator_sysroot = xstrdup (arg);
471 /* Add the standard option list to the simulator. */
474 standard_install (SIM_DESC sd)
476 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
477 if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK)
479 #ifdef SIM_HANDLES_LMA
480 STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA;
485 /* Return non-zero if arg is a duplicate argument.
486 If ARG is NULL, initialize. */
488 #define ARG_HASH_SIZE 97
489 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
496 static char **arg_table = NULL;
500 if (arg_table == NULL)
501 arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
502 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
506 hash = ARG_HASH (arg);
507 while (arg_table[hash] != NULL)
509 if (strcmp (arg, arg_table[hash]) == 0)
511 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
512 don't check if the table is full. */
513 if (++hash == ARG_HASH_SIZE)
516 arg_table[hash] = arg;
520 /* Called by sim_open to parse the arguments. */
523 sim_parse_args (sd, argv)
527 int c, i, argc, num_opts;
528 char *p, *short_options;
529 /* The `val' option struct entry is dynamically assigned for options that
530 only come in the long form. ORIG_VAL is used to get the original value
533 struct option *lp, *long_options;
534 const struct option_list *ol;
536 OPTION_HANDLER **handlers;
538 SIM_RC result = SIM_RC_OK;
540 /* Count the number of arguments. */
541 for (argc = 0; argv[argc] != NULL; ++argc)
544 /* Count the number of options. */
546 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
547 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
549 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
550 for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next)
551 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
554 /* Initialize duplicate argument checker. */
555 (void) dup_arg_p (NULL);
557 /* Build the option table for getopt. */
559 long_options = NZALLOC (struct option, num_opts + 1);
561 short_options = NZALLOC (char, num_opts * 3 + 1);
563 handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts);
564 orig_val = NZALLOC (int, OPTION_START + num_opts);
565 opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts);
567 /* Set '+' as first char so argument permutation isn't done. This
568 is done to stop getopt_long returning options that appear after
569 the target program. Such options should be passed unchanged into
570 the program image. */
573 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
574 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
576 if (dup_arg_p (opt->opt.name))
578 if (opt->shortopt != 0)
580 *p++ = opt->shortopt;
581 if (opt->opt.has_arg == required_argument)
583 else if (opt->opt.has_arg == optional_argument)
584 { *p++ = ':'; *p++ = ':'; }
585 handlers[(unsigned char) opt->shortopt] = opt->handler;
586 if (opt->opt.val != 0)
587 orig_val[(unsigned char) opt->shortopt] = opt->opt.val;
589 orig_val[(unsigned char) opt->shortopt] = opt->shortopt;
591 if (opt->opt.name != NULL)
594 /* Dynamically assign `val' numbers for long options. */
596 handlers[lp->val] = opt->handler;
597 orig_val[lp->val] = opt->opt.val;
598 opt_cpu[lp->val] = NULL;
603 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
605 sim_cpu *cpu = STATE_CPU (sd, c);
606 for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next)
607 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
609 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
610 on the need for dup_arg_p checking. Maybe in the future it'll be
611 needed so this is just commented out, and not deleted. */
612 if (dup_arg_p (opt->opt.name))
615 /* Don't allow short versions of cpu specific options for now. */
616 if (opt->shortopt != 0)
618 sim_io_eprintf (sd, "internal error, short cpu specific option");
619 result = SIM_RC_FAIL;
622 if (opt->opt.name != NULL)
626 /* Prepend --<cpuname>- to the option. */
627 asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name);
629 /* Dynamically assign `val' numbers for long options. */
631 handlers[lp->val] = opt->handler;
632 orig_val[lp->val] = opt->opt.val;
633 opt_cpu[lp->val] = cpu;
639 /* Terminate the short and long option lists. */
643 /* Ensure getopt is initialized. */
650 optc = getopt_long (argc, argv, short_options, long_options, &longind);
653 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
654 STATE_PROG_ARGV (sd) = dupargv (argv + optind);
659 result = SIM_RC_FAIL;
663 if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
665 result = SIM_RC_FAIL;
670 zfree (long_options);
671 zfree (short_options);
678 /* Utility of sim_print_help to print a list of option tables. */
681 print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command)
685 for ( ; ol != NULL; ol = ol->next)
686 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
688 const int indent = 30;
692 if (dup_arg_p (opt->opt.name))
695 if (opt->doc == NULL)
698 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
701 sim_io_printf (sd, " ");
706 /* list any short options (aliases) for the current OPT */
712 if (o->shortopt != '\0')
714 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
715 len += (comma ? 2 : 0) + 2;
718 if (o->opt.has_arg == optional_argument)
720 sim_io_printf (sd, "[%s]", o->arg);
721 len += 1 + strlen (o->arg) + 1;
725 sim_io_printf (sd, " %s", o->arg);
726 len += 1 + strlen (o->arg);
733 while (OPTION_VALID_P (o) && o->doc == NULL);
736 /* list any long options (aliases) for the current OPT */
741 const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL;
742 if (o->doc_name != NULL)
748 sim_io_printf (sd, "%s%s%s%s%s",
750 is_command ? "" : "--",
751 cpu ? cpu_prefix : "",
754 len += ((comma ? 2 : 0)
755 + (is_command ? 0 : 2)
759 if (o->opt.has_arg == optional_argument)
761 sim_io_printf (sd, "[=%s]", o->arg);
762 len += 2 + strlen (o->arg) + 1;
766 sim_io_printf (sd, " %s", o->arg);
767 len += 1 + strlen (o->arg);
774 while (OPTION_VALID_P (o) && o->doc == NULL);
778 sim_io_printf (sd, "\n%*s", indent, "");
781 sim_io_printf (sd, "%*s", indent - len, "");
783 /* print the description, word wrap long lines */
785 const char *chp = opt->doc;
786 unsigned doc_width = 80 - indent;
787 while (strlen (chp) >= doc_width) /* some slack */
789 const char *end = chp + doc_width - 1;
790 while (end > chp && !isspace (*end))
793 end = chp + doc_width - 1;
794 /* The cast should be ok - its distances between to
795 points in a string. */
796 sim_io_printf (sd, "%.*s\n%*s", (int) (end - chp), chp, indent,
799 while (isspace (*chp) && *chp != '\0')
802 sim_io_printf (sd, "%s\n", chp);
807 /* Print help messages for the options. */
810 sim_print_help (sd, is_command)
814 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
815 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
818 /* Initialize duplicate argument checker. */
819 (void) dup_arg_p (NULL);
821 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
822 sim_io_printf (sd, "Options:\n");
824 sim_io_printf (sd, "Commands:\n");
826 print_help (sd, NULL, STATE_OPTIONS (sd), is_command);
827 sim_io_printf (sd, "\n");
829 /* Print cpu-specific options. */
833 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
835 sim_cpu *cpu = STATE_CPU (sd, i);
836 if (CPU_OPTIONS (cpu) == NULL)
838 sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu));
839 print_help (sd, cpu, CPU_OPTIONS (cpu), is_command);
840 sim_io_printf (sd, "\n");
844 sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
845 STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
846 sim_io_printf (sd, " may not be applicable\n");
848 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
850 sim_io_printf (sd, "\n");
851 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
852 sim_io_printf (sd, " Note: Very few simulators support this.\n");
856 /* Utility of sim_args_command to find the closest match for a command.
857 Commands that have "-" in them can be specified as separate words.
858 e.g. sim memory-region 0x800000,0x4000
859 or sim memory region 0x800000,0x4000
860 If CPU is non-null, use its option table list, otherwise use the main one.
861 *PARGI is where to start looking in ARGV. It is updated to point past
864 static const OPTION *
865 find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi)
867 const struct option_list *ol;
869 /* most recent option match */
870 const OPTION *matching_opt = NULL;
871 int matching_argi = -1;
874 ol = CPU_OPTIONS (cpu);
876 ol = STATE_OPTIONS (sd);
878 /* Skip passed elements specified by *PARGI. */
881 for ( ; ol != NULL; ol = ol->next)
882 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
885 const char *name = opt->opt.name;
888 while (argv [argi] != NULL
889 && strncmp (name, argv [argi], strlen (argv [argi])) == 0)
891 name = &name [strlen (argv[argi])];
894 /* leading match ...<a-b-c>-d-e-f - continue search */
895 name ++; /* skip `-' */
899 else if (name [0] == '\0')
901 /* exact match ...<a-b-c-d-e-f> - better than before? */
902 if (argi > matching_argi)
904 matching_argi = argi;
914 *pargi = matching_argi;
919 sim_args_command (SIM_DESC sd, char *cmd)
921 /* something to do? */
923 return SIM_RC_OK; /* FIXME - perhaps help would be better */
927 /* user specified -<opt> ... form? */
928 char **argv = buildargv (cmd);
929 SIM_RC rc = sim_parse_args (sd, argv);
935 char **argv = buildargv (cmd);
936 const OPTION *matching_opt = NULL;
940 if (argv [0] == NULL)
941 return SIM_RC_OK; /* FIXME - perhaps help would be better */
943 /* First check for a cpu selector. */
945 char *cpu_name = xstrdup (argv[0]);
946 char *hyphen = strchr (cpu_name, '-');
949 cpu = sim_cpu_lookup (sd, cpu_name);
952 /* If <cpuname>-<command>, point argv[0] at <command>. */
956 argv[0] += hyphen - cpu_name + 1;
960 matching_opt = find_match (sd, cpu, argv, &matching_argi);
961 /* If hyphen found restore argv[0]. */
963 argv[0] -= hyphen - cpu_name + 1;
968 /* If that failed, try the main table. */
969 if (matching_opt == NULL)
972 matching_opt = find_match (sd, NULL, argv, &matching_argi);
975 if (matching_opt != NULL)
977 switch (matching_opt->opt.has_arg)
980 if (argv [matching_argi + 1] == NULL)
981 matching_opt->handler (sd, cpu, matching_opt->opt.val,
982 NULL, 1/*is_command*/);
984 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
985 matching_opt->opt.name);
987 case optional_argument:
988 if (argv [matching_argi + 1] == NULL)
989 matching_opt->handler (sd, cpu, matching_opt->opt.val,
990 NULL, 1/*is_command*/);
991 else if (argv [matching_argi + 2] == NULL)
992 matching_opt->handler (sd, cpu, matching_opt->opt.val,
993 argv [matching_argi + 1], 1/*is_command*/);
995 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
996 matching_opt->opt.name);
998 case required_argument:
999 if (argv [matching_argi + 1] == NULL)
1000 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
1001 matching_opt->opt.name);
1002 else if (argv [matching_argi + 2] == NULL)
1003 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1004 argv [matching_argi + 1], 1/*is_command*/);
1006 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
1007 matching_opt->opt.name);
1016 /* didn't find anything that remotly matched */