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. */
120 static const OPTION standard_options
[] =
122 { {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
123 'v', NULL
, "Verbose output",
124 standard_option_handler
},
126 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
127 { {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
128 'E', "big|little", "Set endianness",
129 standard_option_handler
},
132 #ifdef SIM_HAVE_ENVIRONMENT
133 /* This option isn't supported unless all choices are supported in keeping
134 with the goal of not printing in --help output things the simulator can't
135 do [as opposed to things that just haven't been configured in]. */
136 { {"environment", required_argument
, NULL
, OPTION_ENVIRONMENT
},
137 '\0', "user|virtual|operating", "Set running environment",
138 standard_option_handler
},
141 { {"alignment", required_argument
, NULL
, OPTION_ALIGNMENT
},
142 '\0', "strict|nonstrict|forced", "Set memory access alignment",
143 standard_option_handler
},
145 { {"debug", no_argument
, NULL
, OPTION_DEBUG
},
146 'D', NULL
, "Print debugging messages",
147 standard_option_handler
},
148 { {"debug-insn", no_argument
, NULL
, OPTION_DEBUG_INSN
},
149 '\0', NULL
, "Print instruction debugging messages",
150 standard_option_handler
},
151 { {"debug-file", required_argument
, NULL
, OPTION_DEBUG_FILE
},
152 '\0', "FILE NAME", "Specify debugging output file",
153 standard_option_handler
},
155 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
156 { {"h8300h", no_argument
, NULL
, OPTION_H8300H
},
157 'h', NULL
, "Indicate the CPU is h8/300h",
158 standard_option_handler
},
159 { {"h8300s", no_argument
, NULL
, OPTION_H8300S
},
160 'S', NULL
, "Indicate the CPU is h8/300s",
161 standard_option_handler
},
162 { {"h8300sx", no_argument
, NULL
, OPTION_H8300SX
},
163 'x', NULL
, "Indicate the CPU is h8/300sx",
164 standard_option_handler
},
167 #ifdef SIM_HAVE_FLATMEM
168 { {"mem-size", required_argument
, NULL
, OPTION_MEM_SIZE
},
169 'm', "MEMORY SIZE", "Specify memory size",
170 standard_option_handler
},
173 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
174 '\0', "COMMAND", ""/*undocumented*/,
175 standard_option_handler
},
177 { {"help", no_argument
, NULL
, OPTION_HELP
},
178 'H', NULL
, "Print help information",
179 standard_option_handler
},
181 { {"architecture", required_argument
, NULL
, OPTION_ARCHITECTURE
},
182 '\0', "MACHINE", "Specify the architecture to use",
183 standard_option_handler
},
184 { {"architecture-info", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
185 '\0', NULL
, "List supported architectures",
186 standard_option_handler
},
187 { {"info-architecture", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
189 standard_option_handler
},
191 { {"target", required_argument
, NULL
, OPTION_TARGET
},
192 '\0', "BFDNAME", "Specify the object-code format for the object files",
193 standard_option_handler
},
195 #ifdef SIM_HANDLES_LMA
196 { {"load-lma", no_argument
, NULL
, OPTION_LOAD_LMA
},
199 "Use VMA or LMA addresses when loading image (default LMA)",
201 "Use VMA or LMA addresses when loading image (default VMA)",
203 standard_option_handler
, "load-{lma,vma}" },
204 { {"load-vma", no_argument
, NULL
, OPTION_LOAD_VMA
},
205 '\0', NULL
, "", standard_option_handler
, "" },
208 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
212 standard_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
213 char *arg
, int is_command
)
217 switch ((STANDARD_OPTIONS
) opt
)
220 STATE_VERBOSE_P (sd
) = 1;
223 #ifdef SIM_HAVE_BIENDIAN
225 if (strcmp (arg
, "big") == 0)
227 if (WITH_TARGET_BYTE_ORDER
== LITTLE_ENDIAN
)
229 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
232 /* FIXME:wip: Need to set something in STATE_CONFIG. */
233 current_target_byte_order
= BIG_ENDIAN
;
235 else if (strcmp (arg
, "little") == 0)
237 if (WITH_TARGET_BYTE_ORDER
== BIG_ENDIAN
)
239 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
242 /* FIXME:wip: Need to set something in STATE_CONFIG. */
243 current_target_byte_order
= LITTLE_ENDIAN
;
247 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
:
378 unsigned long ul
= strtol (arg
, NULL
, 0);
379 /* 16384: some minimal amount */
380 if (! isdigit (arg
[0]) || ul
< 16384)
382 sim_io_eprintf (sd
, "Invalid memory size `%s'", arg
);
385 STATE_MEM_SIZE (sd
) = ul
;
390 case OPTION_DO_COMMAND
:
391 sim_do_command (sd
, arg
);
394 case OPTION_ARCHITECTURE
:
396 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
399 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
402 STATE_ARCHITECTURE (sd
) = ap
;
406 case OPTION_ARCHITECTURE_INFO
:
408 const char **list
= bfd_arch_list();
412 sim_io_printf (sd
, "Possible architectures:");
413 for (lp
= list
; *lp
!= NULL
; lp
++)
414 sim_io_printf (sd
, " %s", *lp
);
415 sim_io_printf (sd
, "\n");
422 STATE_TARGET (sd
) = xstrdup (arg
);
426 case OPTION_LOAD_LMA
:
428 STATE_LOAD_AT_LMA_P (sd
) = 1;
432 case OPTION_LOAD_VMA
:
434 STATE_LOAD_AT_LMA_P (sd
) = 0;
439 sim_print_help (sd
, is_command
);
440 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
442 /* FIXME: 'twould be nice to do something similar if gdb. */
449 /* Add the standard option list to the simulator. */
452 standard_install (SIM_DESC sd
)
454 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
455 if (sim_add_option_table (sd
, NULL
, standard_options
) != SIM_RC_OK
)
457 #ifdef SIM_HANDLES_LMA
458 STATE_LOAD_AT_LMA_P (sd
) = SIM_HANDLES_LMA
;
463 /* Return non-zero if arg is a duplicate argument.
464 If ARG is NULL, initialize. */
466 #define ARG_HASH_SIZE 97
467 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
474 static char **arg_table
= NULL
;
478 if (arg_table
== NULL
)
479 arg_table
= (char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
480 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
484 hash
= ARG_HASH (arg
);
485 while (arg_table
[hash
] != NULL
)
487 if (strcmp (arg
, arg_table
[hash
]) == 0)
489 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
490 don't check if the table is full. */
491 if (++hash
== ARG_HASH_SIZE
)
494 arg_table
[hash
] = arg
;
498 /* Called by sim_open to parse the arguments. */
501 sim_parse_args (sd
, argv
)
505 int c
, i
, argc
, num_opts
;
506 char *p
, *short_options
;
507 /* The `val' option struct entry is dynamically assigned for options that
508 only come in the long form. ORIG_VAL is used to get the original value
511 struct option
*lp
, *long_options
;
512 const struct option_list
*ol
;
514 OPTION_HANDLER
**handlers
;
516 SIM_RC result
= SIM_RC_OK
;
518 /* Count the number of arguments. */
519 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
522 /* Count the number of options. */
524 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
525 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
527 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
528 for (ol
= CPU_OPTIONS (STATE_CPU (sd
, i
)); ol
!= NULL
; ol
= ol
->next
)
529 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
532 /* Initialize duplicate argument checker. */
533 (void) dup_arg_p (NULL
);
535 /* Build the option table for getopt. */
537 long_options
= NZALLOC (struct option
, num_opts
+ 1);
539 short_options
= NZALLOC (char, num_opts
* 3 + 1);
541 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
542 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
543 opt_cpu
= NZALLOC (sim_cpu
*, OPTION_START
+ num_opts
);
545 /* Set '+' as first char so argument permutation isn't done. This
546 is done to stop getopt_long returning options that appear after
547 the target program. Such options should be passed unchanged into
548 the program image. */
551 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
552 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
554 if (dup_arg_p (opt
->opt
.name
))
556 if (opt
->shortopt
!= 0)
558 *p
++ = opt
->shortopt
;
559 if (opt
->opt
.has_arg
== required_argument
)
561 else if (opt
->opt
.has_arg
== optional_argument
)
562 { *p
++ = ':'; *p
++ = ':'; }
563 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
564 if (opt
->opt
.val
!= 0)
565 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
567 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
569 if (opt
->opt
.name
!= NULL
)
572 /* Dynamically assign `val' numbers for long options. */
574 handlers
[lp
->val
] = opt
->handler
;
575 orig_val
[lp
->val
] = opt
->opt
.val
;
576 opt_cpu
[lp
->val
] = NULL
;
581 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
583 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
584 for (ol
= CPU_OPTIONS (cpu
); ol
!= NULL
; ol
= ol
->next
)
585 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
587 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
588 on the need for dup_arg_p checking. Maybe in the future it'll be
589 needed so this is just commented out, and not deleted. */
590 if (dup_arg_p (opt
->opt
.name
))
593 /* Don't allow short versions of cpu specific options for now. */
594 if (opt
->shortopt
!= 0)
596 sim_io_eprintf (sd
, "internal error, short cpu specific option");
597 result
= SIM_RC_FAIL
;
600 if (opt
->opt
.name
!= NULL
)
604 /* Prepend --<cpuname>- to the option. */
605 asprintf (&name
, "%s-%s", CPU_NAME (cpu
), lp
->name
);
607 /* Dynamically assign `val' numbers for long options. */
609 handlers
[lp
->val
] = opt
->handler
;
610 orig_val
[lp
->val
] = opt
->opt
.val
;
611 opt_cpu
[lp
->val
] = cpu
;
617 /* Terminate the short and long option lists. */
621 /* Ensure getopt is initialized. */
628 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
631 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
632 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
637 result
= SIM_RC_FAIL
;
641 if ((*handlers
[optc
]) (sd
, opt_cpu
[optc
], orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
643 result
= SIM_RC_FAIL
;
648 zfree (long_options
);
649 zfree (short_options
);
656 /* Utility of sim_print_help to print a list of option tables. */
659 print_help (SIM_DESC sd
, sim_cpu
*cpu
, const struct option_list
*ol
, int is_command
)
663 for ( ; ol
!= NULL
; ol
= ol
->next
)
664 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
666 const int indent
= 30;
670 if (dup_arg_p (opt
->opt
.name
))
673 if (opt
->doc
== NULL
)
676 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
679 sim_io_printf (sd
, " ");
684 /* list any short options (aliases) for the current OPT */
690 if (o
->shortopt
!= '\0')
692 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
693 len
+= (comma
? 2 : 0) + 2;
696 if (o
->opt
.has_arg
== optional_argument
)
698 sim_io_printf (sd
, "[%s]", o
->arg
);
699 len
+= 1 + strlen (o
->arg
) + 1;
703 sim_io_printf (sd
, " %s", o
->arg
);
704 len
+= 1 + strlen (o
->arg
);
711 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
714 /* list any long options (aliases) for the current OPT */
719 const char *cpu_prefix
= cpu
? CPU_NAME (cpu
) : NULL
;
720 if (o
->doc_name
!= NULL
)
726 sim_io_printf (sd
, "%s%s%s%s%s",
728 is_command
? "" : "--",
729 cpu
? cpu_prefix
: "",
732 len
+= ((comma
? 2 : 0)
733 + (is_command
? 0 : 2)
737 if (o
->opt
.has_arg
== optional_argument
)
739 sim_io_printf (sd
, "[=%s]", o
->arg
);
740 len
+= 2 + strlen (o
->arg
) + 1;
744 sim_io_printf (sd
, " %s", o
->arg
);
745 len
+= 1 + strlen (o
->arg
);
752 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
756 sim_io_printf (sd
, "\n%*s", indent
, "");
759 sim_io_printf (sd
, "%*s", indent
- len
, "");
761 /* print the description, word wrap long lines */
763 const char *chp
= opt
->doc
;
764 unsigned doc_width
= 80 - indent
;
765 while (strlen (chp
) >= doc_width
) /* some slack */
767 const char *end
= chp
+ doc_width
- 1;
768 while (end
> chp
&& !isspace (*end
))
771 end
= chp
+ doc_width
- 1;
772 sim_io_printf (sd
, "%.*s\n%*s", end
- chp
, chp
, indent
, "");
774 while (isspace (*chp
) && *chp
!= '\0')
777 sim_io_printf (sd
, "%s\n", chp
);
782 /* Print help messages for the options. */
785 sim_print_help (sd
, is_command
)
789 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
790 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
793 /* Initialize duplicate argument checker. */
794 (void) dup_arg_p (NULL
);
796 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
797 sim_io_printf (sd
, "Options:\n");
799 sim_io_printf (sd
, "Commands:\n");
801 print_help (sd
, NULL
, STATE_OPTIONS (sd
), is_command
);
802 sim_io_printf (sd
, "\n");
804 /* Print cpu-specific options. */
808 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
810 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
811 if (CPU_OPTIONS (cpu
) == NULL
)
813 sim_io_printf (sd
, "CPU %s specific options:\n", CPU_NAME (cpu
));
814 print_help (sd
, cpu
, CPU_OPTIONS (cpu
), is_command
);
815 sim_io_printf (sd
, "\n");
819 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
820 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
821 sim_io_printf (sd
, " may not be applicable\n");
823 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
825 sim_io_printf (sd
, "\n");
826 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
827 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
831 /* Utility of sim_args_command to find the closest match for a command.
832 Commands that have "-" in them can be specified as separate words.
833 e.g. sim memory-region 0x800000,0x4000
834 or sim memory region 0x800000,0x4000
835 If CPU is non-null, use its option table list, otherwise use the main one.
836 *PARGI is where to start looking in ARGV. It is updated to point past
839 static const OPTION
*
840 find_match (SIM_DESC sd
, sim_cpu
*cpu
, char *argv
[], int *pargi
)
842 const struct option_list
*ol
;
844 /* most recent option match */
845 const OPTION
*matching_opt
= NULL
;
846 int matching_argi
= -1;
849 ol
= CPU_OPTIONS (cpu
);
851 ol
= STATE_OPTIONS (sd
);
853 /* Skip passed elements specified by *PARGI. */
856 for ( ; ol
!= NULL
; ol
= ol
->next
)
857 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
860 const char *name
= opt
->opt
.name
;
863 while (argv
[argi
] != NULL
864 && strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
866 name
= &name
[strlen (argv
[argi
])];
869 /* leading match ...<a-b-c>-d-e-f - continue search */
870 name
++; /* skip `-' */
874 else if (name
[0] == '\0')
876 /* exact match ...<a-b-c-d-e-f> - better than before? */
877 if (argi
> matching_argi
)
879 matching_argi
= argi
;
889 *pargi
= matching_argi
;
894 sim_args_command (SIM_DESC sd
, char *cmd
)
896 /* something to do? */
898 return SIM_RC_OK
; /* FIXME - perhaphs help would be better */
902 /* user specified -<opt> ... form? */
903 char **argv
= buildargv (cmd
);
904 SIM_RC rc
= sim_parse_args (sd
, argv
);
910 char **argv
= buildargv (cmd
);
911 const OPTION
*matching_opt
= NULL
;
915 if (argv
[0] == NULL
)
916 return SIM_RC_OK
; /* FIXME - perhaphs help would be better */
918 /* First check for a cpu selector. */
920 char *cpu_name
= xstrdup (argv
[0]);
921 char *hyphen
= strchr (cpu_name
, '-');
924 cpu
= sim_cpu_lookup (sd
, cpu_name
);
927 /* If <cpuname>-<command>, point argv[0] at <command>. */
931 argv
[0] += hyphen
- cpu_name
+ 1;
935 matching_opt
= find_match (sd
, cpu
, argv
, &matching_argi
);
936 /* If hyphen found restore argv[0]. */
938 argv
[0] -= hyphen
- cpu_name
+ 1;
943 /* If that failed, try the main table. */
944 if (matching_opt
== NULL
)
947 matching_opt
= find_match (sd
, NULL
, argv
, &matching_argi
);
950 if (matching_opt
!= NULL
)
952 switch (matching_opt
->opt
.has_arg
)
955 if (argv
[matching_argi
+ 1] == NULL
)
956 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
957 NULL
, 1/*is_command*/);
959 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
960 matching_opt
->opt
.name
);
962 case optional_argument
:
963 if (argv
[matching_argi
+ 1] == NULL
)
964 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
965 NULL
, 1/*is_command*/);
966 else if (argv
[matching_argi
+ 2] == NULL
)
967 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
968 argv
[matching_argi
+ 1], 1/*is_command*/);
970 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
971 matching_opt
->opt
.name
);
973 case required_argument
:
974 if (argv
[matching_argi
+ 1] == NULL
)
975 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
976 matching_opt
->opt
.name
);
977 else if (argv
[matching_argi
+ 2] == NULL
)
978 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
979 argv
[matching_argi
+ 1], 1/*is_command*/);
981 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
982 matching_opt
->opt
.name
);
991 /* didn't find anything that remotly matched */