1 /* Simulator option handling.
2 Copyright (C) 1996, 1997 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. */
116 static const OPTION standard_options
[] =
118 { {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
119 'v', NULL
, "Verbose output",
120 standard_option_handler
},
122 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
123 { {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
124 'E', "big|little", "Set endianness",
125 standard_option_handler
},
128 #ifdef SIM_HAVE_ENVIRONMENT
129 /* This option isn't supported unless all choices are supported in keeping
130 with the goal of not printing in --help output things the simulator can't
131 do [as opposed to things that just haven't been configured in]. */
132 { {"environment", required_argument
, NULL
, OPTION_ENVIRONMENT
},
133 '\0', "user|virtual|operating", "Set running environment",
134 standard_option_handler
},
137 { {"alignment", required_argument
, NULL
, OPTION_ALIGNMENT
},
138 '\0', "strict|nonstrict|forced", "Set memory access alignment",
139 standard_option_handler
},
141 { {"debug", no_argument
, NULL
, OPTION_DEBUG
},
142 'D', NULL
, "Print debugging messages",
143 standard_option_handler
},
144 { {"debug-insn", no_argument
, NULL
, OPTION_DEBUG_INSN
},
145 '\0', NULL
, "Print instruction debugging messages",
146 standard_option_handler
},
147 { {"debug-file", required_argument
, NULL
, OPTION_DEBUG_FILE
},
148 '\0', "FILE NAME", "Specify debugging output file",
149 standard_option_handler
},
151 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
152 { {"h8300h", no_argument
, NULL
, OPTION_H8300
},
153 'h', NULL
, "Indicate the CPU is h8/300h or h8/300s",
154 standard_option_handler
},
157 #ifdef SIM_HAVE_FLATMEM
158 { {"mem-size", required_argument
, NULL
, OPTION_MEM_SIZE
},
159 'm', "MEMORY SIZE", "Specify memory size",
160 standard_option_handler
},
163 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
164 '\0', "COMMAND", ""/*undocumented*/,
165 standard_option_handler
},
167 { {"help", no_argument
, NULL
, OPTION_HELP
},
168 'H', NULL
, "Print help information",
169 standard_option_handler
},
171 { {"architecture", required_argument
, NULL
, OPTION_ARCHITECTURE
},
172 '\0', "MACHINE", "Specify the architecture to use",
173 standard_option_handler
},
174 { {"architecture-info", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
175 '\0', NULL
, "List supported architectures",
176 standard_option_handler
},
177 { {"info-architecture", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
179 standard_option_handler
},
181 { {"target", required_argument
, NULL
, OPTION_TARGET
},
182 '\0', "BFDNAME", "Specify the object-code format for the object files",
183 standard_option_handler
},
185 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
189 standard_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
190 char *arg
, int is_command
)
194 switch ((STANDARD_OPTIONS
) opt
)
197 STATE_VERBOSE_P (sd
) = 1;
200 #ifdef SIM_HAVE_BIENDIAN
202 if (strcmp (arg
, "big") == 0)
204 if (WITH_TARGET_BYTE_ORDER
== LITTLE_ENDIAN
)
206 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
209 /* FIXME:wip: Need to set something in STATE_CONFIG. */
210 current_target_byte_order
= BIG_ENDIAN
;
212 else if (strcmp (arg
, "little") == 0)
214 if (WITH_TARGET_BYTE_ORDER
== BIG_ENDIAN
)
216 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
219 /* FIXME:wip: Need to set something in STATE_CONFIG. */
220 current_target_byte_order
= LITTLE_ENDIAN
;
224 sim_io_eprintf (sd
, "Invalid endian specification `%s'\n", arg
);
230 case OPTION_ENVIRONMENT
:
231 if (strcmp (arg
, "user") == 0)
232 current_environment
= USER_ENVIRONMENT
;
233 else if (strcmp (arg
, "virtual") == 0)
234 current_environment
= VIRTUAL_ENVIRONMENT
;
235 else if (strcmp (arg
, "operating") == 0)
236 current_environment
= OPERATING_ENVIRONMENT
;
239 sim_io_eprintf (sd
, "Invalid environment specification `%s'\n", arg
);
242 if (WITH_ENVIRONMENT
!= ALL_ENVIRONMENT
243 && WITH_ENVIRONMENT
!= current_environment
)
246 switch (WITH_ENVIRONMENT
)
248 case USER_ENVIRONMENT
: type
= "user"; break;
249 case VIRTUAL_ENVIRONMENT
: type
= "virtual"; break;
250 case OPERATING_ENVIRONMENT
: type
= "operating"; break;
252 sim_io_eprintf (sd
, "Simulator compiled for the %s environment only.\n",
258 case OPTION_ALIGNMENT
:
259 if (strcmp (arg
, "strict") == 0)
261 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== STRICT_ALIGNMENT
)
263 current_alignment
= STRICT_ALIGNMENT
;
267 else if (strcmp (arg
, "nonstrict") == 0)
269 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== NONSTRICT_ALIGNMENT
)
271 current_alignment
= NONSTRICT_ALIGNMENT
;
275 else if (strcmp (arg
, "forced") == 0)
277 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== FORCED_ALIGNMENT
)
279 current_alignment
= FORCED_ALIGNMENT
;
285 sim_io_eprintf (sd
, "Invalid alignment specification `%s'\n", arg
);
288 switch (WITH_ALIGNMENT
)
290 case STRICT_ALIGNMENT
:
291 sim_io_eprintf (sd
, "Simulator compiled for strict alignment only.\n");
293 case NONSTRICT_ALIGNMENT
:
294 sim_io_eprintf (sd
, "Simulator compiled for nonstrict alignment only.\n");
296 case FORCED_ALIGNMENT
:
297 sim_io_eprintf (sd
, "Simulator compiled for forced alignment only.\n");
304 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
307 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
308 for (i
= 0; i
< MAX_DEBUG_VALUES
; ++i
)
309 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
313 case OPTION_DEBUG_INSN
:
315 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
318 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
319 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
323 case OPTION_DEBUG_FILE
:
325 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
328 FILE *f
= fopen (arg
, "w");
332 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
335 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
336 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
340 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
346 #ifdef SIM_HAVE_FLATMEM
347 case OPTION_MEM_SIZE
:
349 unsigned long ul
= strtol (arg
, NULL
, 0);
350 /* 16384: some minimal amount */
351 if (! isdigit (arg
[0]) || ul
< 16384)
353 sim_io_eprintf (sd
, "Invalid memory size `%s'", arg
);
356 STATE_MEM_SIZE (sd
) = ul
;
361 case OPTION_DO_COMMAND
:
362 sim_do_command (sd
, arg
);
365 case OPTION_ARCHITECTURE
:
367 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
370 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
373 STATE_ARCHITECTURE (sd
) = ap
;
377 case OPTION_ARCHITECTURE_INFO
:
379 const char **list
= bfd_arch_list();
383 sim_io_printf (sd
, "Possible architectures:");
384 for (lp
= list
; *lp
!= NULL
; lp
++)
385 sim_io_printf (sd
, " %s", *lp
);
386 sim_io_printf (sd
, "\n");
393 STATE_TARGET (sd
) = xstrdup (arg
);
398 sim_print_help (sd
, is_command
);
399 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
401 /* FIXME: 'twould be nice to do something similar if gdb. */
408 /* Add the standard option list to the simulator. */
411 standard_install (SIM_DESC sd
)
413 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
414 if (sim_add_option_table (sd
, NULL
, standard_options
) != SIM_RC_OK
)
419 /* Return non-zero if arg is a duplicate argument.
420 If ARG is NULL, initialize. */
422 #define ARG_HASH_SIZE 97
423 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
430 static char **arg_table
= NULL
;
434 if (arg_table
== NULL
)
435 arg_table
= (char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
436 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
440 hash
= ARG_HASH (arg
);
441 while (arg_table
[hash
] != NULL
)
443 if (strcmp (arg
, arg_table
[hash
]) == 0)
445 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
446 don't check if the table is full. */
447 if (++hash
== ARG_HASH_SIZE
)
450 arg_table
[hash
] = arg
;
454 /* Called by sim_open to parse the arguments. */
457 sim_parse_args (sd
, argv
)
461 int c
, i
, argc
, num_opts
;
462 char *p
, *short_options
;
463 /* The `val' option struct entry is dynamically assigned for options that
464 only come in the long form. ORIG_VAL is used to get the original value
467 struct option
*lp
, *long_options
;
468 const struct option_list
*ol
;
470 OPTION_HANDLER
**handlers
;
473 /* Count the number of arguments. */
474 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
477 /* Count the number of options. */
479 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
480 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
482 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
483 for (ol
= CPU_OPTIONS (STATE_CPU (sd
, i
)); ol
!= NULL
; ol
= ol
->next
)
484 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
487 /* Initialize duplicate argument checker. */
488 (void) dup_arg_p (NULL
);
490 /* Build the option table for getopt. */
492 long_options
= NZALLOC (struct option
, num_opts
+ 1);
494 short_options
= NZALLOC (char, num_opts
* 3 + 1);
496 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
497 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
498 opt_cpu
= NZALLOC (sim_cpu
*, OPTION_START
+ num_opts
);
500 /* Set '+' as first char so argument permutation isn't done. This
501 is done to stop getopt_long returning options that appear after
502 the target program. Such options should be passed unchanged into
503 the program image. */
506 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
507 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
509 if (dup_arg_p (opt
->opt
.name
))
511 if (opt
->shortopt
!= 0)
513 *p
++ = opt
->shortopt
;
514 if (opt
->opt
.has_arg
== required_argument
)
516 else if (opt
->opt
.has_arg
== optional_argument
)
517 { *p
++ = ':'; *p
++ = ':'; }
518 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
519 if (opt
->opt
.val
!= 0)
520 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
522 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
524 if (opt
->opt
.name
!= NULL
)
527 /* Dynamically assign `val' numbers for long options. */
529 handlers
[lp
->val
] = opt
->handler
;
530 orig_val
[lp
->val
] = opt
->opt
.val
;
531 opt_cpu
[lp
->val
] = NULL
;
536 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
538 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
539 for (ol
= CPU_OPTIONS (cpu
); ol
!= NULL
; ol
= ol
->next
)
540 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
542 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
543 on the need for dup_arg_p checking. Maybe in the future it'll be
544 needed so this is just commented out, and not deleted. */
545 if (dup_arg_p (opt
->opt
.name
))
548 /* Don't allow short versions of cpu specific options for now. */
549 if (opt
->shortopt
!= 0)
551 sim_io_eprintf (sd
, "internal error, short cpu specific option");
554 if (opt
->opt
.name
!= NULL
)
558 /* Prepend --<cpuname>- to the option. */
559 asprintf (&name
, "%s-%s", CPU_NAME (cpu
), lp
->name
);
561 /* Dynamically assign `val' numbers for long options. */
563 handlers
[lp
->val
] = opt
->handler
;
564 orig_val
[lp
->val
] = opt
->opt
.val
;
565 opt_cpu
[lp
->val
] = cpu
;
571 /* Terminate the short and long option lists. */
575 /* Ensure getopt is initialized. */
582 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
585 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
586 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
592 if ((*handlers
[optc
]) (sd
, opt_cpu
[optc
], orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
599 /* Utility of sim_print_help to print a list of option tables. */
602 print_help (SIM_DESC sd
, sim_cpu
*cpu
, const struct option_list
*ol
, int is_command
)
606 for ( ; ol
!= NULL
; ol
= ol
->next
)
607 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
609 const int indent
= 30;
613 if (dup_arg_p (opt
->opt
.name
))
616 if (opt
->doc
== NULL
)
619 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
622 sim_io_printf (sd
, " ");
627 /* list any short options (aliases) for the current OPT */
633 if (o
->shortopt
!= '\0')
635 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
636 len
+= (comma
? 2 : 0) + 2;
639 if (o
->opt
.has_arg
== optional_argument
)
641 sim_io_printf (sd
, "[%s]", o
->arg
);
642 len
+= 1 + strlen (o
->arg
) + 1;
646 sim_io_printf (sd
, " %s", o
->arg
);
647 len
+= 1 + strlen (o
->arg
);
654 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
657 /* list any long options (aliases) for the current OPT */
662 const char *cpu_prefix
= cpu
? CPU_NAME (cpu
) : NULL
;
663 if (o
->doc_name
!= NULL
)
669 sim_io_printf (sd
, "%s%s%s%s%s",
671 is_command
? "" : "--",
672 cpu
? cpu_prefix
: "",
675 len
+= ((comma
? 2 : 0)
676 + (is_command
? 0 : 2)
680 if (o
->opt
.has_arg
== optional_argument
)
682 sim_io_printf (sd
, " [%s]", o
->arg
);
683 len
+= 2 + strlen (o
->arg
) + 1;
687 sim_io_printf (sd
, " %s", o
->arg
);
688 len
+= 1 + strlen (o
->arg
);
695 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
699 sim_io_printf (sd
, "\n%*s", indent
, "");
702 sim_io_printf (sd
, "%*s", indent
- len
, "");
704 /* print the description, word wrap long lines */
706 const char *chp
= opt
->doc
;
707 unsigned doc_width
= 80 - indent
;
708 while (strlen (chp
) >= doc_width
) /* some slack */
710 const char *end
= chp
+ doc_width
- 1;
711 while (end
> chp
&& !isspace (*end
))
714 end
= chp
+ doc_width
- 1;
715 sim_io_printf (sd
, "%.*s\n%*s", end
- chp
, chp
, indent
, "");
717 while (isspace (*chp
) && *chp
!= '\0')
720 sim_io_printf (sd
, "%s\n", chp
);
725 /* Print help messages for the options. */
728 sim_print_help (sd
, is_command
)
732 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
733 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
736 /* Initialize duplicate argument checker. */
737 (void) dup_arg_p (NULL
);
739 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
740 sim_io_printf (sd
, "Options:\n");
742 sim_io_printf (sd
, "Commands:\n");
744 print_help (sd
, NULL
, STATE_OPTIONS (sd
), is_command
);
745 sim_io_printf (sd
, "\n");
747 /* Print cpu-specific options. */
751 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
753 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
754 if (CPU_OPTIONS (cpu
) == NULL
)
756 sim_io_printf (sd
, "CPU %s specific options:\n", CPU_NAME (cpu
));
757 print_help (sd
, cpu
, CPU_OPTIONS (cpu
), is_command
);
758 sim_io_printf (sd
, "\n");
762 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
763 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
764 sim_io_printf (sd
, " may not be applicable\n");
766 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
768 sim_io_printf (sd
, "\n");
769 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
770 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
774 /* Utility of sim_args_command to find the closest match for a command.
775 Commands that have "-" in them can be specified as separate words.
776 e.g. sim memory-region 0x800000,0x4000
777 or sim memory region 0x800000,0x4000
778 If CPU is non-null, use its option table list, otherwise use the main one.
779 *PARGI is where to start looking in ARGV. It is updated to point past
782 static const OPTION
*
783 find_match (SIM_DESC sd
, sim_cpu
*cpu
, char *argv
[], int *pargi
)
785 const struct option_list
*ol
;
787 /* most recent option match */
788 const OPTION
*matching_opt
= NULL
;
789 int matching_argi
= -1;
792 ol
= CPU_OPTIONS (cpu
);
794 ol
= STATE_OPTIONS (sd
);
796 /* Skip passed elements specified by *PARGI. */
799 for ( ; ol
!= NULL
; ol
= ol
->next
)
800 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
803 const char *name
= opt
->opt
.name
;
806 while (argv
[argi
] != NULL
807 && strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
809 name
= &name
[strlen (argv
[argi
])];
812 /* leading match ...<a-b-c>-d-e-f - continue search */
813 name
++; /* skip `-' */
817 else if (name
[0] == '\0')
819 /* exact match ...<a-b-c-d-e-f> - better than before? */
820 if (argi
> matching_argi
)
822 matching_argi
= argi
;
832 *pargi
= matching_argi
;
837 sim_args_command (SIM_DESC sd
, char *cmd
)
839 /* something to do? */
841 return SIM_RC_OK
; /* FIXME - perhaphs help would be better */
845 /* user specified -<opt> ... form? */
846 char **argv
= buildargv (cmd
);
847 SIM_RC rc
= sim_parse_args (sd
, argv
);
853 char **argv
= buildargv (cmd
);
854 const OPTION
*matching_opt
= NULL
;
858 if (argv
[0] == NULL
)
859 return SIM_RC_OK
; /* FIXME - perhaphs help would be better */
861 /* First check for a cpu selector. */
863 char *cpu_name
= xstrdup (argv
[0]);
864 char *hyphen
= strchr (cpu_name
, '-');
867 cpu
= sim_cpu_lookup (sd
, cpu_name
);
870 /* If <cpuname>-<command>, point argv[0] at <command>. */
874 argv
[0] += hyphen
- cpu_name
+ 1;
878 matching_opt
= find_match (sd
, cpu
, argv
, &matching_argi
);
879 /* If hyphen found restore argv[0]. */
881 argv
[0] -= hyphen
- cpu_name
+ 1;
886 /* If that failed, try the main table. */
887 if (matching_opt
== NULL
)
890 matching_opt
= find_match (sd
, NULL
, argv
, &matching_argi
);
893 if (matching_opt
!= NULL
)
895 switch (matching_opt
->opt
.has_arg
)
898 if (argv
[matching_argi
+ 1] == NULL
)
899 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
900 NULL
, 1/*is_command*/);
902 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
903 matching_opt
->opt
.name
);
905 case optional_argument
:
906 if (argv
[matching_argi
+ 1] == NULL
)
907 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
908 NULL
, 1/*is_command*/);
909 else if (argv
[matching_argi
+ 2] == NULL
)
910 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
911 argv
[matching_argi
+ 1], 1/*is_command*/);
913 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
914 matching_opt
->opt
.name
);
916 case required_argument
:
917 if (argv
[matching_argi
+ 1] == NULL
)
918 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
919 matching_opt
->opt
.name
);
920 else if (argv
[matching_argi
+ 2] == NULL
)
921 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
922 argv
[matching_argi
+ 1], 1/*is_command*/);
924 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
925 matching_opt
->opt
.name
);
934 /* didn't find anything that remotly matched */