1 /* Simulator option handling.
2 Copyright (C) 1996, 1997, 2004, 2007 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 2, or (at your option)
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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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,
103 #if defined (SIM_HAVE_BIENDIAN)
107 #ifdef SIM_HAVE_FLATMEM
111 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
121 static const OPTION standard_options[] =
123 { {"verbose", no_argument, NULL, OPTION_VERBOSE},
124 'v', NULL, "Verbose output",
125 standard_option_handler },
127 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
128 { {"endian", required_argument, NULL, OPTION_ENDIAN},
129 'E', "big|little", "Set endianness",
130 standard_option_handler },
133 #ifdef SIM_HAVE_ENVIRONMENT
134 /* This option isn't supported unless all choices are supported in keeping
135 with the goal of not printing in --help output things the simulator can't
136 do [as opposed to things that just haven't been configured in]. */
137 { {"environment", required_argument, NULL, OPTION_ENVIRONMENT},
138 '\0', "user|virtual|operating", "Set running environment",
139 standard_option_handler },
142 { {"alignment", required_argument, NULL, OPTION_ALIGNMENT},
143 '\0', "strict|nonstrict|forced", "Set memory access alignment",
144 standard_option_handler },
146 { {"debug", no_argument, NULL, OPTION_DEBUG},
147 'D', NULL, "Print debugging messages",
148 standard_option_handler },
149 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
150 '\0', NULL, "Print instruction debugging messages",
151 standard_option_handler },
152 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
153 '\0', "FILE NAME", "Specify debugging output file",
154 standard_option_handler },
156 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
157 { {"h8300h", no_argument, NULL, OPTION_H8300H},
158 'h', NULL, "Indicate the CPU is H8/300H",
159 standard_option_handler },
160 { {"h8300s", no_argument, NULL, OPTION_H8300S},
161 'S', NULL, "Indicate the CPU is H8S",
162 standard_option_handler },
163 { {"h8300sx", no_argument, NULL, OPTION_H8300SX},
164 'x', NULL, "Indicate the CPU is H8SX",
165 standard_option_handler },
168 #ifdef SIM_HAVE_FLATMEM
169 { {"mem-size", required_argument, NULL, OPTION_MEM_SIZE},
170 'm', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
171 "Specify memory size", standard_option_handler },
174 { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
175 '\0', "COMMAND", ""/*undocumented*/,
176 standard_option_handler },
178 { {"help", no_argument, NULL, OPTION_HELP},
179 'H', NULL, "Print help information",
180 standard_option_handler },
182 { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
183 '\0', "MACHINE", "Specify the architecture to use",
184 standard_option_handler },
185 { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
186 '\0', NULL, "List supported architectures",
187 standard_option_handler },
188 { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
190 standard_option_handler },
192 { {"target", required_argument, NULL, OPTION_TARGET},
193 '\0', "BFDNAME", "Specify the object-code format for the object files",
194 standard_option_handler },
196 #ifdef SIM_HANDLES_LMA
197 { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA},
200 "Use VMA or LMA addresses when loading image (default LMA)",
202 "Use VMA or LMA addresses when loading image (default VMA)",
204 standard_option_handler, "load-{lma,vma}" },
205 { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA},
206 '\0', NULL, "", standard_option_handler, "" },
209 { {"sysroot", required_argument, NULL, OPTION_SYSROOT},
211 "Root for system calls with absolute file-names and cwd at start",
212 standard_option_handler },
214 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
218 standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
219 char *arg, int is_command)
223 switch ((STANDARD_OPTIONS) opt)
226 STATE_VERBOSE_P (sd) = 1;
229 #ifdef SIM_HAVE_BIENDIAN
231 if (strcmp (arg, "big") == 0)
233 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
235 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
238 /* FIXME:wip: Need to set something in STATE_CONFIG. */
239 current_target_byte_order = BIG_ENDIAN;
241 else if (strcmp (arg, "little") == 0)
243 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
245 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
248 /* FIXME:wip: Need to set something in STATE_CONFIG. */
249 current_target_byte_order = LITTLE_ENDIAN;
253 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
259 case OPTION_ENVIRONMENT:
260 if (strcmp (arg, "user") == 0)
261 STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT;
262 else if (strcmp (arg, "virtual") == 0)
263 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
264 else if (strcmp (arg, "operating") == 0)
265 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
268 sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg);
271 if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
272 && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd))
275 switch (WITH_ENVIRONMENT)
277 case USER_ENVIRONMENT: type = "user"; break;
278 case VIRTUAL_ENVIRONMENT: type = "virtual"; break;
279 case OPERATING_ENVIRONMENT: type = "operating"; break;
281 sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n",
287 case OPTION_ALIGNMENT:
288 if (strcmp (arg, "strict") == 0)
290 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT)
292 current_alignment = STRICT_ALIGNMENT;
296 else if (strcmp (arg, "nonstrict") == 0)
298 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT)
300 current_alignment = NONSTRICT_ALIGNMENT;
304 else if (strcmp (arg, "forced") == 0)
306 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT)
308 current_alignment = FORCED_ALIGNMENT;
314 sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg);
317 switch (WITH_ALIGNMENT)
319 case STRICT_ALIGNMENT:
320 sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n");
322 case NONSTRICT_ALIGNMENT:
323 sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n");
325 case FORCED_ALIGNMENT:
326 sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n");
333 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
336 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
337 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
338 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
342 case OPTION_DEBUG_INSN :
344 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
347 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
348 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
352 case OPTION_DEBUG_FILE :
354 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
357 FILE *f = fopen (arg, "w");
361 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
364 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
365 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
369 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
371 set_h8300h (bfd_mach_h8300h);
374 set_h8300h (bfd_mach_h8300s);
377 set_h8300h (bfd_mach_h8300sx);
381 #ifdef SIM_HAVE_FLATMEM
382 case OPTION_MEM_SIZE:
385 unsigned long ul = strtol (arg, &endp, 0);
389 case 'k': case 'K': size <<= 10; break;
390 case 'm': case 'M': size <<= 20; break;
391 case 'g': case 'G': size <<= 30; break;
392 case ' ': case '\0': case '\t': break;
395 sim_io_eprintf (sd, "Ignoring strange character at end of memory size: %c\n", * endp);
399 /* 16384: some minimal amount */
400 if (! isdigit (arg[0]) || ul < 16384)
402 sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
405 STATE_MEM_SIZE (sd) = ul;
410 case OPTION_DO_COMMAND:
411 sim_do_command (sd, arg);
414 case OPTION_ARCHITECTURE:
416 const struct bfd_arch_info *ap = bfd_scan_arch (arg);
419 sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
422 STATE_ARCHITECTURE (sd) = ap;
426 case OPTION_ARCHITECTURE_INFO:
428 const char **list = bfd_arch_list();
432 sim_io_printf (sd, "Possible architectures:");
433 for (lp = list; *lp != NULL; lp++)
434 sim_io_printf (sd, " %s", *lp);
435 sim_io_printf (sd, "\n");
442 STATE_TARGET (sd) = xstrdup (arg);
446 case OPTION_LOAD_LMA:
448 STATE_LOAD_AT_LMA_P (sd) = 1;
452 case OPTION_LOAD_VMA:
454 STATE_LOAD_AT_LMA_P (sd) = 0;
459 sim_print_help (sd, is_command);
460 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
462 /* FIXME: 'twould be nice to do something similar if gdb. */
466 /* Don't leak memory in the odd event that there's lots of
467 --sysroot=... options. */
468 if (simulator_sysroot[0] != '\0' && arg[0] != '\0')
469 free (simulator_sysroot);
470 simulator_sysroot = xstrdup (arg);
477 /* Add the standard option list to the simulator. */
480 standard_install (SIM_DESC sd)
482 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
483 if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK)
485 #ifdef SIM_HANDLES_LMA
486 STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA;
491 /* Return non-zero if arg is a duplicate argument.
492 If ARG is NULL, initialize. */
494 #define ARG_HASH_SIZE 97
495 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
502 static char **arg_table = NULL;
506 if (arg_table == NULL)
507 arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
508 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
512 hash = ARG_HASH (arg);
513 while (arg_table[hash] != NULL)
515 if (strcmp (arg, arg_table[hash]) == 0)
517 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
518 don't check if the table is full. */
519 if (++hash == ARG_HASH_SIZE)
522 arg_table[hash] = arg;
526 /* Called by sim_open to parse the arguments. */
529 sim_parse_args (sd, argv)
533 int c, i, argc, num_opts;
534 char *p, *short_options;
535 /* The `val' option struct entry is dynamically assigned for options that
536 only come in the long form. ORIG_VAL is used to get the original value
539 struct option *lp, *long_options;
540 const struct option_list *ol;
542 OPTION_HANDLER **handlers;
544 SIM_RC result = SIM_RC_OK;
546 /* Count the number of arguments. */
547 for (argc = 0; argv[argc] != NULL; ++argc)
550 /* Count the number of options. */
552 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
553 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
555 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
556 for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next)
557 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
560 /* Initialize duplicate argument checker. */
561 (void) dup_arg_p (NULL);
563 /* Build the option table for getopt. */
565 long_options = NZALLOC (struct option, num_opts + 1);
567 short_options = NZALLOC (char, num_opts * 3 + 1);
569 handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts);
570 orig_val = NZALLOC (int, OPTION_START + num_opts);
571 opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts);
573 /* Set '+' as first char so argument permutation isn't done. This
574 is done to stop getopt_long returning options that appear after
575 the target program. Such options should be passed unchanged into
576 the program image. */
579 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
580 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
582 if (dup_arg_p (opt->opt.name))
584 if (opt->shortopt != 0)
586 *p++ = opt->shortopt;
587 if (opt->opt.has_arg == required_argument)
589 else if (opt->opt.has_arg == optional_argument)
590 { *p++ = ':'; *p++ = ':'; }
591 handlers[(unsigned char) opt->shortopt] = opt->handler;
592 if (opt->opt.val != 0)
593 orig_val[(unsigned char) opt->shortopt] = opt->opt.val;
595 orig_val[(unsigned char) opt->shortopt] = opt->shortopt;
597 if (opt->opt.name != NULL)
600 /* Dynamically assign `val' numbers for long options. */
602 handlers[lp->val] = opt->handler;
603 orig_val[lp->val] = opt->opt.val;
604 opt_cpu[lp->val] = NULL;
609 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
611 sim_cpu *cpu = STATE_CPU (sd, c);
612 for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next)
613 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
615 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
616 on the need for dup_arg_p checking. Maybe in the future it'll be
617 needed so this is just commented out, and not deleted. */
618 if (dup_arg_p (opt->opt.name))
621 /* Don't allow short versions of cpu specific options for now. */
622 if (opt->shortopt != 0)
624 sim_io_eprintf (sd, "internal error, short cpu specific option");
625 result = SIM_RC_FAIL;
628 if (opt->opt.name != NULL)
632 /* Prepend --<cpuname>- to the option. */
633 asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name);
635 /* Dynamically assign `val' numbers for long options. */
637 handlers[lp->val] = opt->handler;
638 orig_val[lp->val] = opt->opt.val;
639 opt_cpu[lp->val] = cpu;
645 /* Terminate the short and long option lists. */
649 /* Ensure getopt is initialized. */
656 optc = getopt_long (argc, argv, short_options, long_options, &longind);
659 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
660 STATE_PROG_ARGV (sd) = dupargv (argv + optind);
665 result = SIM_RC_FAIL;
669 if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
671 result = SIM_RC_FAIL;
676 zfree (long_options);
677 zfree (short_options);
684 /* Utility of sim_print_help to print a list of option tables. */
687 print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command)
691 for ( ; ol != NULL; ol = ol->next)
692 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
694 const int indent = 30;
698 if (dup_arg_p (opt->opt.name))
701 if (opt->doc == NULL)
704 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
707 sim_io_printf (sd, " ");
712 /* list any short options (aliases) for the current OPT */
718 if (o->shortopt != '\0')
720 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
721 len += (comma ? 2 : 0) + 2;
724 if (o->opt.has_arg == optional_argument)
726 sim_io_printf (sd, "[%s]", o->arg);
727 len += 1 + strlen (o->arg) + 1;
731 sim_io_printf (sd, " %s", o->arg);
732 len += 1 + strlen (o->arg);
739 while (OPTION_VALID_P (o) && o->doc == NULL);
742 /* list any long options (aliases) for the current OPT */
747 const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL;
748 if (o->doc_name != NULL)
754 sim_io_printf (sd, "%s%s%s%s%s",
756 is_command ? "" : "--",
757 cpu ? cpu_prefix : "",
760 len += ((comma ? 2 : 0)
761 + (is_command ? 0 : 2)
765 if (o->opt.has_arg == optional_argument)
767 sim_io_printf (sd, "[=%s]", o->arg);
768 len += 2 + strlen (o->arg) + 1;
772 sim_io_printf (sd, " %s", o->arg);
773 len += 1 + strlen (o->arg);
780 while (OPTION_VALID_P (o) && o->doc == NULL);
784 sim_io_printf (sd, "\n%*s", indent, "");
787 sim_io_printf (sd, "%*s", indent - len, "");
789 /* print the description, word wrap long lines */
791 const char *chp = opt->doc;
792 unsigned doc_width = 80 - indent;
793 while (strlen (chp) >= doc_width) /* some slack */
795 const char *end = chp + doc_width - 1;
796 while (end > chp && !isspace (*end))
799 end = chp + doc_width - 1;
800 /* The cast should be ok - its distances between to
801 points in a string. */
802 sim_io_printf (sd, "%.*s\n%*s", (int) (end - chp), chp, indent,
805 while (isspace (*chp) && *chp != '\0')
808 sim_io_printf (sd, "%s\n", chp);
813 /* Print help messages for the options. */
816 sim_print_help (sd, is_command)
820 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
821 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
824 /* Initialize duplicate argument checker. */
825 (void) dup_arg_p (NULL);
827 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
828 sim_io_printf (sd, "Options:\n");
830 sim_io_printf (sd, "Commands:\n");
832 print_help (sd, NULL, STATE_OPTIONS (sd), is_command);
833 sim_io_printf (sd, "\n");
835 /* Print cpu-specific options. */
839 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
841 sim_cpu *cpu = STATE_CPU (sd, i);
842 if (CPU_OPTIONS (cpu) == NULL)
844 sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu));
845 print_help (sd, cpu, CPU_OPTIONS (cpu), is_command);
846 sim_io_printf (sd, "\n");
850 sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
851 STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
852 sim_io_printf (sd, " may not be applicable\n");
854 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
856 sim_io_printf (sd, "\n");
857 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
858 sim_io_printf (sd, " Note: Very few simulators support this.\n");
862 /* Utility of sim_args_command to find the closest match for a command.
863 Commands that have "-" in them can be specified as separate words.
864 e.g. sim memory-region 0x800000,0x4000
865 or sim memory region 0x800000,0x4000
866 If CPU is non-null, use its option table list, otherwise use the main one.
867 *PARGI is where to start looking in ARGV. It is updated to point past
870 static const OPTION *
871 find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi)
873 const struct option_list *ol;
875 /* most recent option match */
876 const OPTION *matching_opt = NULL;
877 int matching_argi = -1;
880 ol = CPU_OPTIONS (cpu);
882 ol = STATE_OPTIONS (sd);
884 /* Skip passed elements specified by *PARGI. */
887 for ( ; ol != NULL; ol = ol->next)
888 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
891 const char *name = opt->opt.name;
894 while (argv [argi] != NULL
895 && strncmp (name, argv [argi], strlen (argv [argi])) == 0)
897 name = &name [strlen (argv[argi])];
900 /* leading match ...<a-b-c>-d-e-f - continue search */
901 name ++; /* skip `-' */
905 else if (name [0] == '\0')
907 /* exact match ...<a-b-c-d-e-f> - better than before? */
908 if (argi > matching_argi)
910 matching_argi = argi;
920 *pargi = matching_argi;
925 sim_args_command (SIM_DESC sd, char *cmd)
927 /* something to do? */
929 return SIM_RC_OK; /* FIXME - perhaps help would be better */
933 /* user specified -<opt> ... form? */
934 char **argv = buildargv (cmd);
935 SIM_RC rc = sim_parse_args (sd, argv);
941 char **argv = buildargv (cmd);
942 const OPTION *matching_opt = NULL;
946 if (argv [0] == NULL)
947 return SIM_RC_OK; /* FIXME - perhaps help would be better */
949 /* First check for a cpu selector. */
951 char *cpu_name = xstrdup (argv[0]);
952 char *hyphen = strchr (cpu_name, '-');
955 cpu = sim_cpu_lookup (sd, cpu_name);
958 /* If <cpuname>-<command>, point argv[0] at <command>. */
962 argv[0] += hyphen - cpu_name + 1;
966 matching_opt = find_match (sd, cpu, argv, &matching_argi);
967 /* If hyphen found restore argv[0]. */
969 argv[0] -= hyphen - cpu_name + 1;
974 /* If that failed, try the main table. */
975 if (matching_opt == NULL)
978 matching_opt = find_match (sd, NULL, argv, &matching_argi);
981 if (matching_opt != NULL)
983 switch (matching_opt->opt.has_arg)
986 if (argv [matching_argi + 1] == NULL)
987 matching_opt->handler (sd, cpu, matching_opt->opt.val,
988 NULL, 1/*is_command*/);
990 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
991 matching_opt->opt.name);
993 case optional_argument:
994 if (argv [matching_argi + 1] == NULL)
995 matching_opt->handler (sd, cpu, matching_opt->opt.val,
996 NULL, 1/*is_command*/);
997 else if (argv [matching_argi + 2] == NULL)
998 matching_opt->handler (sd, cpu, matching_opt->opt.val,
999 argv [matching_argi + 1], 1/*is_command*/);
1001 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
1002 matching_opt->opt.name);
1004 case required_argument:
1005 if (argv [matching_argi + 1] == NULL)
1006 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
1007 matching_opt->opt.name);
1008 else if (argv [matching_argi + 2] == NULL)
1009 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1010 argv [matching_argi + 1], 1/*is_command*/);
1012 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
1013 matching_opt->opt.name);
1022 /* didn't find anything that remotly matched */