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 3 of the License, or
10 (at your option) any later version.
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
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32 #include "libiberty.h"
33 #include "sim-options.h"
35 #include "sim-assert.h"
39 /* Add a set of options to the simulator.
40 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
41 This is intended to be called by modules in their `install' handler. */
44 sim_add_option_table (SIM_DESC sd
, sim_cpu
*cpu
, const OPTION
*table
)
46 struct option_list
*ol
= ((struct option_list
*)
47 xmalloc (sizeof (struct option_list
)));
49 /* Note: The list is constructed in the reverse order we're called so
50 later calls will override earlier ones (in case that ever happens).
51 This is the intended behaviour. */
55 ol
->next
= CPU_OPTIONS (cpu
);
57 CPU_OPTIONS (cpu
) = ol
;
61 ol
->next
= STATE_OPTIONS (sd
);
63 STATE_OPTIONS (sd
) = ol
;
69 /* Standard option table.
70 Modules may specify additional ones.
71 The caller of sim_parse_args may also specify additional options
72 by calling sim_add_option_table first. */
74 static DECLARE_OPTION_HANDLER (standard_option_handler
);
76 /* FIXME: We shouldn't print in --help output options that aren't usable.
77 Some fine tuning will be necessary. One can either move less general
78 options to another table or use a HAVE_FOO macro to ifdef out unavailable
81 /* ??? One might want to conditionally compile out the entries that
82 aren't enabled. There's a distinction, however, between options a
83 simulator can't support and options that haven't been configured in.
84 Certainly options a simulator can't support shouldn't appear in the
85 output of --help. Whether the same thing applies to options that haven't
86 been configured in or not isn't something I can get worked up over.
87 [Note that conditionally compiling them out might simply involve moving
88 the option to another table.]
89 If you decide to conditionally compile them out as well, delete this
90 comment and add a comment saying that that is the rule. */
93 OPTION_DEBUG_INSN
= OPTION_START
,
98 OPTION_ARCHITECTURE_INFO
,
102 #if defined (SIM_HAVE_BIENDIAN)
106 #ifdef SIM_HAVE_FLATMEM
110 #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 H8S",
161 standard_option_handler
},
162 { {"h8300sx", no_argument
, NULL
, OPTION_H8300SX
},
163 'x', NULL
, "Indicate the CPU is H8SX",
164 standard_option_handler
},
167 #ifdef SIM_HAVE_FLATMEM
168 { {"mem-size", required_argument
, NULL
, OPTION_MEM_SIZE
},
169 'm', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
170 "Specify memory size", 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 { {"sysroot", required_argument
, NULL
, OPTION_SYSROOT
},
210 "Root for system calls with absolute file-names and cwd at start",
211 standard_option_handler
},
213 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
217 standard_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
218 char *arg
, int is_command
)
222 switch ((STANDARD_OPTIONS
) opt
)
225 STATE_VERBOSE_P (sd
) = 1;
228 #ifdef SIM_HAVE_BIENDIAN
230 if (strcmp (arg
, "big") == 0)
232 if (WITH_TARGET_BYTE_ORDER
== LITTLE_ENDIAN
)
234 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
237 /* FIXME:wip: Need to set something in STATE_CONFIG. */
238 current_target_byte_order
= BIG_ENDIAN
;
240 else if (strcmp (arg
, "little") == 0)
242 if (WITH_TARGET_BYTE_ORDER
== BIG_ENDIAN
)
244 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
247 /* FIXME:wip: Need to set something in STATE_CONFIG. */
248 current_target_byte_order
= LITTLE_ENDIAN
;
252 sim_io_eprintf (sd
, "Invalid endian specification `%s'\n", arg
);
258 case OPTION_ENVIRONMENT
:
259 if (strcmp (arg
, "user") == 0)
260 STATE_ENVIRONMENT (sd
) = USER_ENVIRONMENT
;
261 else if (strcmp (arg
, "virtual") == 0)
262 STATE_ENVIRONMENT (sd
) = VIRTUAL_ENVIRONMENT
;
263 else if (strcmp (arg
, "operating") == 0)
264 STATE_ENVIRONMENT (sd
) = OPERATING_ENVIRONMENT
;
267 sim_io_eprintf (sd
, "Invalid environment specification `%s'\n", arg
);
270 if (WITH_ENVIRONMENT
!= ALL_ENVIRONMENT
271 && WITH_ENVIRONMENT
!= STATE_ENVIRONMENT (sd
))
274 switch (WITH_ENVIRONMENT
)
276 case USER_ENVIRONMENT
: type
= "user"; break;
277 case VIRTUAL_ENVIRONMENT
: type
= "virtual"; break;
278 case OPERATING_ENVIRONMENT
: type
= "operating"; break;
280 sim_io_eprintf (sd
, "Simulator compiled for the %s environment only.\n",
286 case OPTION_ALIGNMENT
:
287 if (strcmp (arg
, "strict") == 0)
289 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== STRICT_ALIGNMENT
)
291 current_alignment
= STRICT_ALIGNMENT
;
295 else if (strcmp (arg
, "nonstrict") == 0)
297 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== NONSTRICT_ALIGNMENT
)
299 current_alignment
= NONSTRICT_ALIGNMENT
;
303 else if (strcmp (arg
, "forced") == 0)
305 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== FORCED_ALIGNMENT
)
307 current_alignment
= FORCED_ALIGNMENT
;
313 sim_io_eprintf (sd
, "Invalid alignment specification `%s'\n", arg
);
316 switch (WITH_ALIGNMENT
)
318 case STRICT_ALIGNMENT
:
319 sim_io_eprintf (sd
, "Simulator compiled for strict alignment only.\n");
321 case NONSTRICT_ALIGNMENT
:
322 sim_io_eprintf (sd
, "Simulator compiled for nonstrict alignment only.\n");
324 case FORCED_ALIGNMENT
:
325 sim_io_eprintf (sd
, "Simulator compiled for forced alignment only.\n");
332 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
335 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
336 for (i
= 0; i
< MAX_DEBUG_VALUES
; ++i
)
337 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
341 case OPTION_DEBUG_INSN
:
343 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
346 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
347 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
351 case OPTION_DEBUG_FILE
:
353 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
356 FILE *f
= fopen (arg
, "w");
360 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
363 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
364 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
368 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
370 set_h8300h (bfd_mach_h8300h
);
373 set_h8300h (bfd_mach_h8300s
);
376 set_h8300h (bfd_mach_h8300sx
);
380 #ifdef SIM_HAVE_FLATMEM
381 case OPTION_MEM_SIZE
:
384 unsigned long ul
= strtol (arg
, &endp
, 0);
388 case 'k': case 'K': size
<<= 10; break;
389 case 'm': case 'M': size
<<= 20; break;
390 case 'g': case 'G': size
<<= 30; break;
391 case ' ': case '\0': case '\t': break;
394 sim_io_eprintf (sd
, "Ignoring strange character at end of memory size: %c\n", * endp
);
398 /* 16384: some minimal amount */
399 if (! isdigit (arg
[0]) || ul
< 16384)
401 sim_io_eprintf (sd
, "Invalid memory size `%s'", arg
);
404 STATE_MEM_SIZE (sd
) = ul
;
409 case OPTION_DO_COMMAND
:
410 sim_do_command (sd
, arg
);
413 case OPTION_ARCHITECTURE
:
415 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
418 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
421 STATE_ARCHITECTURE (sd
) = ap
;
425 case OPTION_ARCHITECTURE_INFO
:
427 const char **list
= bfd_arch_list();
431 sim_io_printf (sd
, "Possible architectures:");
432 for (lp
= list
; *lp
!= NULL
; lp
++)
433 sim_io_printf (sd
, " %s", *lp
);
434 sim_io_printf (sd
, "\n");
441 STATE_TARGET (sd
) = xstrdup (arg
);
445 case OPTION_LOAD_LMA
:
447 STATE_LOAD_AT_LMA_P (sd
) = 1;
451 case OPTION_LOAD_VMA
:
453 STATE_LOAD_AT_LMA_P (sd
) = 0;
458 sim_print_help (sd
, is_command
);
459 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
461 /* FIXME: 'twould be nice to do something similar if gdb. */
465 /* Don't leak memory in the odd event that there's lots of
466 --sysroot=... options. */
467 if (simulator_sysroot
[0] != '\0' && arg
[0] != '\0')
468 free (simulator_sysroot
);
469 simulator_sysroot
= xstrdup (arg
);
476 /* Add the standard option list to the simulator. */
479 standard_install (SIM_DESC sd
)
481 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
482 if (sim_add_option_table (sd
, NULL
, standard_options
) != SIM_RC_OK
)
484 #ifdef SIM_HANDLES_LMA
485 STATE_LOAD_AT_LMA_P (sd
) = SIM_HANDLES_LMA
;
490 /* Return non-zero if arg is a duplicate argument.
491 If ARG is NULL, initialize. */
493 #define ARG_HASH_SIZE 97
494 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
501 static char **arg_table
= NULL
;
505 if (arg_table
== NULL
)
506 arg_table
= (char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
507 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
511 hash
= ARG_HASH (arg
);
512 while (arg_table
[hash
] != NULL
)
514 if (strcmp (arg
, arg_table
[hash
]) == 0)
516 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
517 don't check if the table is full. */
518 if (++hash
== ARG_HASH_SIZE
)
521 arg_table
[hash
] = arg
;
525 /* Called by sim_open to parse the arguments. */
528 sim_parse_args (sd
, argv
)
532 int c
, i
, argc
, num_opts
;
533 char *p
, *short_options
;
534 /* The `val' option struct entry is dynamically assigned for options that
535 only come in the long form. ORIG_VAL is used to get the original value
538 struct option
*lp
, *long_options
;
539 const struct option_list
*ol
;
541 OPTION_HANDLER
**handlers
;
543 SIM_RC result
= SIM_RC_OK
;
545 /* Count the number of arguments. */
546 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
549 /* Count the number of options. */
551 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
552 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
554 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
555 for (ol
= CPU_OPTIONS (STATE_CPU (sd
, i
)); ol
!= NULL
; ol
= ol
->next
)
556 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
559 /* Initialize duplicate argument checker. */
560 (void) dup_arg_p (NULL
);
562 /* Build the option table for getopt. */
564 long_options
= NZALLOC (struct option
, num_opts
+ 1);
566 short_options
= NZALLOC (char, num_opts
* 3 + 1);
568 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
569 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
570 opt_cpu
= NZALLOC (sim_cpu
*, OPTION_START
+ num_opts
);
572 /* Set '+' as first char so argument permutation isn't done. This
573 is done to stop getopt_long returning options that appear after
574 the target program. Such options should be passed unchanged into
575 the program image. */
578 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
579 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
581 if (dup_arg_p (opt
->opt
.name
))
583 if (opt
->shortopt
!= 0)
585 *p
++ = opt
->shortopt
;
586 if (opt
->opt
.has_arg
== required_argument
)
588 else if (opt
->opt
.has_arg
== optional_argument
)
589 { *p
++ = ':'; *p
++ = ':'; }
590 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
591 if (opt
->opt
.val
!= 0)
592 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
594 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
596 if (opt
->opt
.name
!= NULL
)
599 /* Dynamically assign `val' numbers for long options. */
601 handlers
[lp
->val
] = opt
->handler
;
602 orig_val
[lp
->val
] = opt
->opt
.val
;
603 opt_cpu
[lp
->val
] = NULL
;
608 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
610 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
611 for (ol
= CPU_OPTIONS (cpu
); ol
!= NULL
; ol
= ol
->next
)
612 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
614 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
615 on the need for dup_arg_p checking. Maybe in the future it'll be
616 needed so this is just commented out, and not deleted. */
617 if (dup_arg_p (opt
->opt
.name
))
620 /* Don't allow short versions of cpu specific options for now. */
621 if (opt
->shortopt
!= 0)
623 sim_io_eprintf (sd
, "internal error, short cpu specific option");
624 result
= SIM_RC_FAIL
;
627 if (opt
->opt
.name
!= NULL
)
631 /* Prepend --<cpuname>- to the option. */
632 asprintf (&name
, "%s-%s", CPU_NAME (cpu
), lp
->name
);
634 /* Dynamically assign `val' numbers for long options. */
636 handlers
[lp
->val
] = opt
->handler
;
637 orig_val
[lp
->val
] = opt
->opt
.val
;
638 opt_cpu
[lp
->val
] = cpu
;
644 /* Terminate the short and long option lists. */
648 /* Ensure getopt is initialized. */
655 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
658 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
659 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
664 result
= SIM_RC_FAIL
;
668 if ((*handlers
[optc
]) (sd
, opt_cpu
[optc
], orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
670 result
= SIM_RC_FAIL
;
675 zfree (long_options
);
676 zfree (short_options
);
683 /* Utility of sim_print_help to print a list of option tables. */
686 print_help (SIM_DESC sd
, sim_cpu
*cpu
, const struct option_list
*ol
, int is_command
)
690 for ( ; ol
!= NULL
; ol
= ol
->next
)
691 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
693 const int indent
= 30;
697 if (dup_arg_p (opt
->opt
.name
))
700 if (opt
->doc
== NULL
)
703 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
706 sim_io_printf (sd
, " ");
711 /* list any short options (aliases) for the current OPT */
717 if (o
->shortopt
!= '\0')
719 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
720 len
+= (comma
? 2 : 0) + 2;
723 if (o
->opt
.has_arg
== optional_argument
)
725 sim_io_printf (sd
, "[%s]", o
->arg
);
726 len
+= 1 + strlen (o
->arg
) + 1;
730 sim_io_printf (sd
, " %s", o
->arg
);
731 len
+= 1 + strlen (o
->arg
);
738 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
741 /* list any long options (aliases) for the current OPT */
746 const char *cpu_prefix
= cpu
? CPU_NAME (cpu
) : NULL
;
747 if (o
->doc_name
!= NULL
)
753 sim_io_printf (sd
, "%s%s%s%s%s",
755 is_command
? "" : "--",
756 cpu
? cpu_prefix
: "",
759 len
+= ((comma
? 2 : 0)
760 + (is_command
? 0 : 2)
764 if (o
->opt
.has_arg
== optional_argument
)
766 sim_io_printf (sd
, "[=%s]", o
->arg
);
767 len
+= 2 + strlen (o
->arg
) + 1;
771 sim_io_printf (sd
, " %s", o
->arg
);
772 len
+= 1 + strlen (o
->arg
);
779 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
783 sim_io_printf (sd
, "\n%*s", indent
, "");
786 sim_io_printf (sd
, "%*s", indent
- len
, "");
788 /* print the description, word wrap long lines */
790 const char *chp
= opt
->doc
;
791 unsigned doc_width
= 80 - indent
;
792 while (strlen (chp
) >= doc_width
) /* some slack */
794 const char *end
= chp
+ doc_width
- 1;
795 while (end
> chp
&& !isspace (*end
))
798 end
= chp
+ doc_width
- 1;
799 /* The cast should be ok - its distances between to
800 points in a string. */
801 sim_io_printf (sd
, "%.*s\n%*s", (int) (end
- chp
), chp
, indent
,
804 while (isspace (*chp
) && *chp
!= '\0')
807 sim_io_printf (sd
, "%s\n", chp
);
812 /* Print help messages for the options. */
815 sim_print_help (sd
, is_command
)
819 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
820 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
823 /* Initialize duplicate argument checker. */
824 (void) dup_arg_p (NULL
);
826 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
827 sim_io_printf (sd
, "Options:\n");
829 sim_io_printf (sd
, "Commands:\n");
831 print_help (sd
, NULL
, STATE_OPTIONS (sd
), is_command
);
832 sim_io_printf (sd
, "\n");
834 /* Print cpu-specific options. */
838 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
840 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
841 if (CPU_OPTIONS (cpu
) == NULL
)
843 sim_io_printf (sd
, "CPU %s specific options:\n", CPU_NAME (cpu
));
844 print_help (sd
, cpu
, CPU_OPTIONS (cpu
), is_command
);
845 sim_io_printf (sd
, "\n");
849 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
850 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
851 sim_io_printf (sd
, " may not be applicable\n");
853 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
855 sim_io_printf (sd
, "\n");
856 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
857 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
861 /* Utility of sim_args_command to find the closest match for a command.
862 Commands that have "-" in them can be specified as separate words.
863 e.g. sim memory-region 0x800000,0x4000
864 or sim memory region 0x800000,0x4000
865 If CPU is non-null, use its option table list, otherwise use the main one.
866 *PARGI is where to start looking in ARGV. It is updated to point past
869 static const OPTION
*
870 find_match (SIM_DESC sd
, sim_cpu
*cpu
, char *argv
[], int *pargi
)
872 const struct option_list
*ol
;
874 /* most recent option match */
875 const OPTION
*matching_opt
= NULL
;
876 int matching_argi
= -1;
879 ol
= CPU_OPTIONS (cpu
);
881 ol
= STATE_OPTIONS (sd
);
883 /* Skip passed elements specified by *PARGI. */
886 for ( ; ol
!= NULL
; ol
= ol
->next
)
887 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
890 const char *name
= opt
->opt
.name
;
893 while (argv
[argi
] != NULL
894 && strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
896 name
= &name
[strlen (argv
[argi
])];
899 /* leading match ...<a-b-c>-d-e-f - continue search */
900 name
++; /* skip `-' */
904 else if (name
[0] == '\0')
906 /* exact match ...<a-b-c-d-e-f> - better than before? */
907 if (argi
> matching_argi
)
909 matching_argi
= argi
;
919 *pargi
= matching_argi
;
924 sim_args_command (SIM_DESC sd
, char *cmd
)
926 /* something to do? */
928 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
932 /* user specified -<opt> ... form? */
933 char **argv
= buildargv (cmd
);
934 SIM_RC rc
= sim_parse_args (sd
, argv
);
940 char **argv
= buildargv (cmd
);
941 const OPTION
*matching_opt
= NULL
;
945 if (argv
[0] == NULL
)
946 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
948 /* First check for a cpu selector. */
950 char *cpu_name
= xstrdup (argv
[0]);
951 char *hyphen
= strchr (cpu_name
, '-');
954 cpu
= sim_cpu_lookup (sd
, cpu_name
);
957 /* If <cpuname>-<command>, point argv[0] at <command>. */
961 argv
[0] += hyphen
- cpu_name
+ 1;
965 matching_opt
= find_match (sd
, cpu
, argv
, &matching_argi
);
966 /* If hyphen found restore argv[0]. */
968 argv
[0] -= hyphen
- cpu_name
+ 1;
973 /* If that failed, try the main table. */
974 if (matching_opt
== NULL
)
977 matching_opt
= find_match (sd
, NULL
, argv
, &matching_argi
);
980 if (matching_opt
!= NULL
)
982 switch (matching_opt
->opt
.has_arg
)
985 if (argv
[matching_argi
+ 1] == NULL
)
986 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
987 NULL
, 1/*is_command*/);
989 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
990 matching_opt
->opt
.name
);
992 case optional_argument
:
993 if (argv
[matching_argi
+ 1] == NULL
)
994 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
995 NULL
, 1/*is_command*/);
996 else if (argv
[matching_argi
+ 2] == NULL
)
997 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
998 argv
[matching_argi
+ 1], 1/*is_command*/);
1000 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
1001 matching_opt
->opt
.name
);
1003 case required_argument
:
1004 if (argv
[matching_argi
+ 1] == NULL
)
1005 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
1006 matching_opt
->opt
.name
);
1007 else if (argv
[matching_argi
+ 2] == NULL
)
1008 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1009 argv
[matching_argi
+ 1], 1/*is_command*/);
1011 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
1012 matching_opt
->opt
.name
);
1021 /* didn't find anything that remotly matched */