1 /* Simulator option handling.
2 Copyright (C) 1996, 1997, 2004 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', "MEMORY SIZE", "Specify memory size",
171 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
:
384 unsigned long ul
= strtol (arg
, NULL
, 0);
385 /* 16384: some minimal amount */
386 if (! isdigit (arg
[0]) || ul
< 16384)
388 sim_io_eprintf (sd
, "Invalid memory size `%s'", arg
);
391 STATE_MEM_SIZE (sd
) = ul
;
396 case OPTION_DO_COMMAND
:
397 sim_do_command (sd
, arg
);
400 case OPTION_ARCHITECTURE
:
402 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
405 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
408 STATE_ARCHITECTURE (sd
) = ap
;
412 case OPTION_ARCHITECTURE_INFO
:
414 const char **list
= bfd_arch_list();
418 sim_io_printf (sd
, "Possible architectures:");
419 for (lp
= list
; *lp
!= NULL
; lp
++)
420 sim_io_printf (sd
, " %s", *lp
);
421 sim_io_printf (sd
, "\n");
428 STATE_TARGET (sd
) = xstrdup (arg
);
432 case OPTION_LOAD_LMA
:
434 STATE_LOAD_AT_LMA_P (sd
) = 1;
438 case OPTION_LOAD_VMA
:
440 STATE_LOAD_AT_LMA_P (sd
) = 0;
445 sim_print_help (sd
, is_command
);
446 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
448 /* FIXME: 'twould be nice to do something similar if gdb. */
452 /* Don't leak memory in the odd event that there's lots of
453 --sysroot=... options. */
454 if (simulator_sysroot
[0] != '\0' && arg
[0] != '\0')
455 free (simulator_sysroot
);
456 simulator_sysroot
= xstrdup (arg
);
463 /* Add the standard option list to the simulator. */
466 standard_install (SIM_DESC sd
)
468 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
469 if (sim_add_option_table (sd
, NULL
, standard_options
) != SIM_RC_OK
)
471 #ifdef SIM_HANDLES_LMA
472 STATE_LOAD_AT_LMA_P (sd
) = SIM_HANDLES_LMA
;
477 /* Return non-zero if arg is a duplicate argument.
478 If ARG is NULL, initialize. */
480 #define ARG_HASH_SIZE 97
481 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
488 static char **arg_table
= NULL
;
492 if (arg_table
== NULL
)
493 arg_table
= (char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
494 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
498 hash
= ARG_HASH (arg
);
499 while (arg_table
[hash
] != NULL
)
501 if (strcmp (arg
, arg_table
[hash
]) == 0)
503 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
504 don't check if the table is full. */
505 if (++hash
== ARG_HASH_SIZE
)
508 arg_table
[hash
] = arg
;
512 /* Called by sim_open to parse the arguments. */
515 sim_parse_args (sd
, argv
)
519 int c
, i
, argc
, num_opts
;
520 char *p
, *short_options
;
521 /* The `val' option struct entry is dynamically assigned for options that
522 only come in the long form. ORIG_VAL is used to get the original value
525 struct option
*lp
, *long_options
;
526 const struct option_list
*ol
;
528 OPTION_HANDLER
**handlers
;
530 SIM_RC result
= SIM_RC_OK
;
532 /* Count the number of arguments. */
533 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
536 /* Count the number of options. */
538 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
539 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
541 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
542 for (ol
= CPU_OPTIONS (STATE_CPU (sd
, i
)); ol
!= NULL
; ol
= ol
->next
)
543 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
546 /* Initialize duplicate argument checker. */
547 (void) dup_arg_p (NULL
);
549 /* Build the option table for getopt. */
551 long_options
= NZALLOC (struct option
, num_opts
+ 1);
553 short_options
= NZALLOC (char, num_opts
* 3 + 1);
555 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
556 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
557 opt_cpu
= NZALLOC (sim_cpu
*, OPTION_START
+ num_opts
);
559 /* Set '+' as first char so argument permutation isn't done. This
560 is done to stop getopt_long returning options that appear after
561 the target program. Such options should be passed unchanged into
562 the program image. */
565 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
566 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
568 if (dup_arg_p (opt
->opt
.name
))
570 if (opt
->shortopt
!= 0)
572 *p
++ = opt
->shortopt
;
573 if (opt
->opt
.has_arg
== required_argument
)
575 else if (opt
->opt
.has_arg
== optional_argument
)
576 { *p
++ = ':'; *p
++ = ':'; }
577 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
578 if (opt
->opt
.val
!= 0)
579 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
581 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
583 if (opt
->opt
.name
!= NULL
)
586 /* Dynamically assign `val' numbers for long options. */
588 handlers
[lp
->val
] = opt
->handler
;
589 orig_val
[lp
->val
] = opt
->opt
.val
;
590 opt_cpu
[lp
->val
] = NULL
;
595 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
597 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
598 for (ol
= CPU_OPTIONS (cpu
); ol
!= NULL
; ol
= ol
->next
)
599 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
601 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
602 on the need for dup_arg_p checking. Maybe in the future it'll be
603 needed so this is just commented out, and not deleted. */
604 if (dup_arg_p (opt
->opt
.name
))
607 /* Don't allow short versions of cpu specific options for now. */
608 if (opt
->shortopt
!= 0)
610 sim_io_eprintf (sd
, "internal error, short cpu specific option");
611 result
= SIM_RC_FAIL
;
614 if (opt
->opt
.name
!= NULL
)
618 /* Prepend --<cpuname>- to the option. */
619 asprintf (&name
, "%s-%s", CPU_NAME (cpu
), lp
->name
);
621 /* Dynamically assign `val' numbers for long options. */
623 handlers
[lp
->val
] = opt
->handler
;
624 orig_val
[lp
->val
] = opt
->opt
.val
;
625 opt_cpu
[lp
->val
] = cpu
;
631 /* Terminate the short and long option lists. */
635 /* Ensure getopt is initialized. */
642 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
645 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
646 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
651 result
= SIM_RC_FAIL
;
655 if ((*handlers
[optc
]) (sd
, opt_cpu
[optc
], orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
657 result
= SIM_RC_FAIL
;
662 zfree (long_options
);
663 zfree (short_options
);
670 /* Utility of sim_print_help to print a list of option tables. */
673 print_help (SIM_DESC sd
, sim_cpu
*cpu
, const struct option_list
*ol
, int is_command
)
677 for ( ; ol
!= NULL
; ol
= ol
->next
)
678 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
680 const int indent
= 30;
684 if (dup_arg_p (opt
->opt
.name
))
687 if (opt
->doc
== NULL
)
690 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
693 sim_io_printf (sd
, " ");
698 /* list any short options (aliases) for the current OPT */
704 if (o
->shortopt
!= '\0')
706 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
707 len
+= (comma
? 2 : 0) + 2;
710 if (o
->opt
.has_arg
== optional_argument
)
712 sim_io_printf (sd
, "[%s]", o
->arg
);
713 len
+= 1 + strlen (o
->arg
) + 1;
717 sim_io_printf (sd
, " %s", o
->arg
);
718 len
+= 1 + strlen (o
->arg
);
725 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
728 /* list any long options (aliases) for the current OPT */
733 const char *cpu_prefix
= cpu
? CPU_NAME (cpu
) : NULL
;
734 if (o
->doc_name
!= NULL
)
740 sim_io_printf (sd
, "%s%s%s%s%s",
742 is_command
? "" : "--",
743 cpu
? cpu_prefix
: "",
746 len
+= ((comma
? 2 : 0)
747 + (is_command
? 0 : 2)
751 if (o
->opt
.has_arg
== optional_argument
)
753 sim_io_printf (sd
, "[=%s]", o
->arg
);
754 len
+= 2 + strlen (o
->arg
) + 1;
758 sim_io_printf (sd
, " %s", o
->arg
);
759 len
+= 1 + strlen (o
->arg
);
766 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
770 sim_io_printf (sd
, "\n%*s", indent
, "");
773 sim_io_printf (sd
, "%*s", indent
- len
, "");
775 /* print the description, word wrap long lines */
777 const char *chp
= opt
->doc
;
778 unsigned doc_width
= 80 - indent
;
779 while (strlen (chp
) >= doc_width
) /* some slack */
781 const char *end
= chp
+ doc_width
- 1;
782 while (end
> chp
&& !isspace (*end
))
785 end
= chp
+ doc_width
- 1;
786 /* The cast should be ok - its distances between to
787 points in a string. */
788 sim_io_printf (sd
, "%.*s\n%*s", (int) (end
- chp
), chp
, indent
,
791 while (isspace (*chp
) && *chp
!= '\0')
794 sim_io_printf (sd
, "%s\n", chp
);
799 /* Print help messages for the options. */
802 sim_print_help (sd
, is_command
)
806 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
807 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
810 /* Initialize duplicate argument checker. */
811 (void) dup_arg_p (NULL
);
813 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
814 sim_io_printf (sd
, "Options:\n");
816 sim_io_printf (sd
, "Commands:\n");
818 print_help (sd
, NULL
, STATE_OPTIONS (sd
), is_command
);
819 sim_io_printf (sd
, "\n");
821 /* Print cpu-specific options. */
825 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
827 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
828 if (CPU_OPTIONS (cpu
) == NULL
)
830 sim_io_printf (sd
, "CPU %s specific options:\n", CPU_NAME (cpu
));
831 print_help (sd
, cpu
, CPU_OPTIONS (cpu
), is_command
);
832 sim_io_printf (sd
, "\n");
836 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
837 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
838 sim_io_printf (sd
, " may not be applicable\n");
840 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
842 sim_io_printf (sd
, "\n");
843 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
844 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
848 /* Utility of sim_args_command to find the closest match for a command.
849 Commands that have "-" in them can be specified as separate words.
850 e.g. sim memory-region 0x800000,0x4000
851 or sim memory region 0x800000,0x4000
852 If CPU is non-null, use its option table list, otherwise use the main one.
853 *PARGI is where to start looking in ARGV. It is updated to point past
856 static const OPTION
*
857 find_match (SIM_DESC sd
, sim_cpu
*cpu
, char *argv
[], int *pargi
)
859 const struct option_list
*ol
;
861 /* most recent option match */
862 const OPTION
*matching_opt
= NULL
;
863 int matching_argi
= -1;
866 ol
= CPU_OPTIONS (cpu
);
868 ol
= STATE_OPTIONS (sd
);
870 /* Skip passed elements specified by *PARGI. */
873 for ( ; ol
!= NULL
; ol
= ol
->next
)
874 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
877 const char *name
= opt
->opt
.name
;
880 while (argv
[argi
] != NULL
881 && strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
883 name
= &name
[strlen (argv
[argi
])];
886 /* leading match ...<a-b-c>-d-e-f - continue search */
887 name
++; /* skip `-' */
891 else if (name
[0] == '\0')
893 /* exact match ...<a-b-c-d-e-f> - better than before? */
894 if (argi
> matching_argi
)
896 matching_argi
= argi
;
906 *pargi
= matching_argi
;
911 sim_args_command (SIM_DESC sd
, char *cmd
)
913 /* something to do? */
915 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
919 /* user specified -<opt> ... form? */
920 char **argv
= buildargv (cmd
);
921 SIM_RC rc
= sim_parse_args (sd
, argv
);
927 char **argv
= buildargv (cmd
);
928 const OPTION
*matching_opt
= NULL
;
932 if (argv
[0] == NULL
)
933 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
935 /* First check for a cpu selector. */
937 char *cpu_name
= xstrdup (argv
[0]);
938 char *hyphen
= strchr (cpu_name
, '-');
941 cpu
= sim_cpu_lookup (sd
, cpu_name
);
944 /* If <cpuname>-<command>, point argv[0] at <command>. */
948 argv
[0] += hyphen
- cpu_name
+ 1;
952 matching_opt
= find_match (sd
, cpu
, argv
, &matching_argi
);
953 /* If hyphen found restore argv[0]. */
955 argv
[0] -= hyphen
- cpu_name
+ 1;
960 /* If that failed, try the main table. */
961 if (matching_opt
== NULL
)
964 matching_opt
= find_match (sd
, NULL
, argv
, &matching_argi
);
967 if (matching_opt
!= NULL
)
969 switch (matching_opt
->opt
.has_arg
)
972 if (argv
[matching_argi
+ 1] == NULL
)
973 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
974 NULL
, 1/*is_command*/);
976 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
977 matching_opt
->opt
.name
);
979 case optional_argument
:
980 if (argv
[matching_argi
+ 1] == NULL
)
981 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
982 NULL
, 1/*is_command*/);
983 else if (argv
[matching_argi
+ 2] == NULL
)
984 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
985 argv
[matching_argi
+ 1], 1/*is_command*/);
987 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
988 matching_opt
->opt
.name
);
990 case required_argument
:
991 if (argv
[matching_argi
+ 1] == NULL
)
992 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
993 matching_opt
->opt
.name
);
994 else if (argv
[matching_argi
+ 2] == NULL
)
995 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
996 argv
[matching_argi
+ 1], 1/*is_command*/);
998 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
999 matching_opt
->opt
.name
);
1008 /* didn't find anything that remotly matched */