1 /* Simulator option handling.
2 Copyright (C) 1996-2014 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
,
104 #ifdef SIM_HAVE_FLATMEM
108 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
118 static const OPTION standard_options
[] =
120 { {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
121 'v', NULL
, "Verbose output",
122 standard_option_handler
, NULL
},
124 { {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
125 'E', "big|little", "Set endianness",
126 standard_option_handler
, NULL
},
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_H8300H
},
153 'h', NULL
, "Indicate the CPU is H8/300H",
154 standard_option_handler
},
155 { {"h8300s", no_argument
, NULL
, OPTION_H8300S
},
156 'S', NULL
, "Indicate the CPU is H8S",
157 standard_option_handler
},
158 { {"h8300sx", no_argument
, NULL
, OPTION_H8300SX
},
159 'x', NULL
, "Indicate the CPU is H8SX",
160 standard_option_handler
},
163 #ifdef SIM_HAVE_FLATMEM
164 { {"mem-size", required_argument
, NULL
, OPTION_MEM_SIZE
},
165 'm', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
166 "Specify memory size", standard_option_handler
},
169 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
170 '\0', "COMMAND", ""/*undocumented*/,
171 standard_option_handler
},
173 { {"help", no_argument
, NULL
, OPTION_HELP
},
174 'H', NULL
, "Print help information",
175 standard_option_handler
},
177 { {"architecture", required_argument
, NULL
, OPTION_ARCHITECTURE
},
178 '\0', "MACHINE", "Specify the architecture to use",
179 standard_option_handler
},
180 { {"architecture-info", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
181 '\0', NULL
, "List supported architectures",
182 standard_option_handler
},
183 { {"info-architecture", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
185 standard_option_handler
},
187 { {"target", required_argument
, NULL
, OPTION_TARGET
},
188 '\0', "BFDNAME", "Specify the object-code format for the object files",
189 standard_option_handler
},
191 #ifdef SIM_HANDLES_LMA
192 { {"load-lma", no_argument
, NULL
, OPTION_LOAD_LMA
},
195 "Use VMA or LMA addresses when loading image (default LMA)",
197 "Use VMA or LMA addresses when loading image (default VMA)",
199 standard_option_handler
, "load-{lma,vma}" },
200 { {"load-vma", no_argument
, NULL
, OPTION_LOAD_VMA
},
201 '\0', NULL
, "", standard_option_handler
, "" },
204 { {"sysroot", required_argument
, NULL
, OPTION_SYSROOT
},
206 "Root for system calls with absolute file-names and cwd at start",
207 standard_option_handler
, NULL
},
209 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
213 standard_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
214 char *arg
, int is_command
)
218 switch ((STANDARD_OPTIONS
) opt
)
221 STATE_VERBOSE_P (sd
) = 1;
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
);
252 case OPTION_ENVIRONMENT
:
253 if (strcmp (arg
, "user") == 0)
254 STATE_ENVIRONMENT (sd
) = USER_ENVIRONMENT
;
255 else if (strcmp (arg
, "virtual") == 0)
256 STATE_ENVIRONMENT (sd
) = VIRTUAL_ENVIRONMENT
;
257 else if (strcmp (arg
, "operating") == 0)
258 STATE_ENVIRONMENT (sd
) = OPERATING_ENVIRONMENT
;
261 sim_io_eprintf (sd
, "Invalid environment specification `%s'\n", arg
);
264 if (WITH_ENVIRONMENT
!= ALL_ENVIRONMENT
265 && WITH_ENVIRONMENT
!= STATE_ENVIRONMENT (sd
))
268 switch (WITH_ENVIRONMENT
)
270 case USER_ENVIRONMENT
: type
= "user"; break;
271 case VIRTUAL_ENVIRONMENT
: type
= "virtual"; break;
272 case OPERATING_ENVIRONMENT
: type
= "operating"; break;
274 sim_io_eprintf (sd
, "Simulator compiled for the %s environment only.\n",
280 case OPTION_ALIGNMENT
:
281 if (strcmp (arg
, "strict") == 0)
283 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== STRICT_ALIGNMENT
)
285 current_alignment
= STRICT_ALIGNMENT
;
289 else if (strcmp (arg
, "nonstrict") == 0)
291 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== NONSTRICT_ALIGNMENT
)
293 current_alignment
= NONSTRICT_ALIGNMENT
;
297 else if (strcmp (arg
, "forced") == 0)
299 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== FORCED_ALIGNMENT
)
301 current_alignment
= FORCED_ALIGNMENT
;
307 sim_io_eprintf (sd
, "Invalid alignment specification `%s'\n", arg
);
310 switch (WITH_ALIGNMENT
)
312 case STRICT_ALIGNMENT
:
313 sim_io_eprintf (sd
, "Simulator compiled for strict alignment only.\n");
315 case NONSTRICT_ALIGNMENT
:
316 sim_io_eprintf (sd
, "Simulator compiled for nonstrict alignment only.\n");
318 case FORCED_ALIGNMENT
:
319 sim_io_eprintf (sd
, "Simulator compiled for forced alignment only.\n");
326 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
329 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
330 for (i
= 0; i
< MAX_DEBUG_VALUES
; ++i
)
331 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
335 case OPTION_DEBUG_INSN
:
337 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
340 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
341 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
345 case OPTION_DEBUG_FILE
:
347 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
350 FILE *f
= fopen (arg
, "w");
354 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
357 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
358 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
362 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
364 set_h8300h (bfd_mach_h8300h
);
367 set_h8300h (bfd_mach_h8300s
);
370 set_h8300h (bfd_mach_h8300sx
);
374 #ifdef SIM_HAVE_FLATMEM
375 case OPTION_MEM_SIZE
:
378 unsigned long ul
= strtol (arg
, &endp
, 0);
382 case 'k': case 'K': size
<<= 10; break;
383 case 'm': case 'M': size
<<= 20; break;
384 case 'g': case 'G': size
<<= 30; break;
385 case ' ': case '\0': case '\t': break;
388 sim_io_eprintf (sd
, "Ignoring strange character at end of memory size: %c\n", * endp
);
392 /* 16384: some minimal amount */
393 if (! isdigit (arg
[0]) || ul
< 16384)
395 sim_io_eprintf (sd
, "Invalid memory size `%s'", arg
);
398 STATE_MEM_SIZE (sd
) = ul
;
403 case OPTION_DO_COMMAND
:
404 sim_do_command (sd
, arg
);
407 case OPTION_ARCHITECTURE
:
409 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
412 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
415 STATE_ARCHITECTURE (sd
) = ap
;
419 case OPTION_ARCHITECTURE_INFO
:
421 const char **list
= bfd_arch_list ();
425 sim_io_printf (sd
, "Possible architectures:");
426 for (lp
= list
; *lp
!= NULL
; lp
++)
427 sim_io_printf (sd
, " %s", *lp
);
428 sim_io_printf (sd
, "\n");
435 STATE_TARGET (sd
) = xstrdup (arg
);
439 case OPTION_LOAD_LMA
:
441 STATE_LOAD_AT_LMA_P (sd
) = 1;
445 case OPTION_LOAD_VMA
:
447 STATE_LOAD_AT_LMA_P (sd
) = 0;
452 sim_print_help (sd
, is_command
);
453 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
455 /* FIXME: 'twould be nice to do something similar if gdb. */
459 /* Don't leak memory in the odd event that there's lots of
460 --sysroot=... options. We treat "" specially since this
461 is the statically initialized value and cannot free it. */
462 if (simulator_sysroot
[0] != '\0')
463 free (simulator_sysroot
);
465 simulator_sysroot
= xstrdup (arg
);
467 simulator_sysroot
= "";
474 /* Add the standard option list to the simulator. */
477 standard_install (SIM_DESC sd
)
479 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
480 if (sim_add_option_table (sd
, NULL
, standard_options
) != SIM_RC_OK
)
482 #ifdef SIM_HANDLES_LMA
483 STATE_LOAD_AT_LMA_P (sd
) = SIM_HANDLES_LMA
;
488 /* Return non-zero if arg is a duplicate argument.
489 If ARG is NULL, initialize. */
491 #define ARG_HASH_SIZE 97
492 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
495 dup_arg_p (const char *arg
)
498 static const char **arg_table
= NULL
;
502 if (arg_table
== NULL
)
503 arg_table
= (const char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
504 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
508 hash
= ARG_HASH (arg
);
509 while (arg_table
[hash
] != NULL
)
511 if (strcmp (arg
, arg_table
[hash
]) == 0)
513 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
514 don't check if the table is full. */
515 if (++hash
== ARG_HASH_SIZE
)
518 arg_table
[hash
] = arg
;
522 /* Called by sim_open to parse the arguments. */
525 sim_parse_args (SIM_DESC sd
, char **argv
)
527 int c
, i
, argc
, num_opts
;
528 char *p
, *short_options
;
529 /* The `val' option struct entry is dynamically assigned for options that
530 only come in the long form. ORIG_VAL is used to get the original value
533 struct option
*lp
, *long_options
;
534 const struct option_list
*ol
;
536 OPTION_HANDLER
**handlers
;
538 SIM_RC result
= SIM_RC_OK
;
540 /* Count the number of arguments. */
541 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
544 /* Count the number of options. */
546 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
547 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
549 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
550 for (ol
= CPU_OPTIONS (STATE_CPU (sd
, i
)); ol
!= NULL
; ol
= ol
->next
)
551 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
554 /* Initialize duplicate argument checker. */
555 (void) dup_arg_p (NULL
);
557 /* Build the option table for getopt. */
559 long_options
= NZALLOC (struct option
, num_opts
+ 1);
561 short_options
= NZALLOC (char, num_opts
* 3 + 1);
563 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
564 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
565 opt_cpu
= NZALLOC (sim_cpu
*, OPTION_START
+ num_opts
);
567 /* Set '+' as first char so argument permutation isn't done. This
568 is done to stop getopt_long returning options that appear after
569 the target program. Such options should be passed unchanged into
570 the program image. */
573 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
574 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
576 if (dup_arg_p (opt
->opt
.name
))
578 if (opt
->shortopt
!= 0)
580 *p
++ = opt
->shortopt
;
581 if (opt
->opt
.has_arg
== required_argument
)
583 else if (opt
->opt
.has_arg
== optional_argument
)
584 { *p
++ = ':'; *p
++ = ':'; }
585 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
586 if (opt
->opt
.val
!= 0)
587 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
589 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
591 if (opt
->opt
.name
!= NULL
)
594 /* Dynamically assign `val' numbers for long options. */
596 handlers
[lp
->val
] = opt
->handler
;
597 orig_val
[lp
->val
] = opt
->opt
.val
;
598 opt_cpu
[lp
->val
] = NULL
;
603 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
605 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
606 for (ol
= CPU_OPTIONS (cpu
); ol
!= NULL
; ol
= ol
->next
)
607 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
609 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
610 on the need for dup_arg_p checking. Maybe in the future it'll be
611 needed so this is just commented out, and not deleted. */
612 if (dup_arg_p (opt
->opt
.name
))
615 /* Don't allow short versions of cpu specific options for now. */
616 if (opt
->shortopt
!= 0)
618 sim_io_eprintf (sd
, "internal error, short cpu specific option");
619 result
= SIM_RC_FAIL
;
622 if (opt
->opt
.name
!= NULL
)
626 /* Prepend --<cpuname>- to the option. */
627 if (asprintf (&name
, "%s-%s", CPU_NAME (cpu
), lp
->name
) < 0)
629 sim_io_eprintf (sd
, "internal error, out of memory");
630 result
= SIM_RC_FAIL
;
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
;
676 free (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 (SIM_DESC sd
, int is_command
)
817 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
818 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
821 /* Initialize duplicate argument checker. */
822 (void) dup_arg_p (NULL
);
824 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
825 sim_io_printf (sd
, "Options:\n");
827 sim_io_printf (sd
, "Commands:\n");
829 print_help (sd
, NULL
, STATE_OPTIONS (sd
), is_command
);
830 sim_io_printf (sd
, "\n");
832 /* Print cpu-specific options. */
836 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
838 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
839 if (CPU_OPTIONS (cpu
) == NULL
)
841 sim_io_printf (sd
, "CPU %s specific options:\n", CPU_NAME (cpu
));
842 print_help (sd
, cpu
, CPU_OPTIONS (cpu
), is_command
);
843 sim_io_printf (sd
, "\n");
847 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
848 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
849 sim_io_printf (sd
, " may not be applicable\n");
851 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
853 sim_io_printf (sd
, "\n");
854 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
855 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
859 /* Utility of sim_args_command to find the closest match for a command.
860 Commands that have "-" in them can be specified as separate words.
861 e.g. sim memory-region 0x800000,0x4000
862 or sim memory region 0x800000,0x4000
863 If CPU is non-null, use its option table list, otherwise use the main one.
864 *PARGI is where to start looking in ARGV. It is updated to point past
867 static const OPTION
*
868 find_match (SIM_DESC sd
, sim_cpu
*cpu
, char *argv
[], int *pargi
)
870 const struct option_list
*ol
;
872 /* most recent option match */
873 const OPTION
*matching_opt
= NULL
;
874 int matching_argi
= -1;
877 ol
= CPU_OPTIONS (cpu
);
879 ol
= STATE_OPTIONS (sd
);
881 /* Skip passed elements specified by *PARGI. */
884 for ( ; ol
!= NULL
; ol
= ol
->next
)
885 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
888 const char *name
= opt
->opt
.name
;
891 while (argv
[argi
] != NULL
892 && strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
894 name
= &name
[strlen (argv
[argi
])];
897 /* leading match ...<a-b-c>-d-e-f - continue search */
898 name
++; /* skip `-' */
902 else if (name
[0] == '\0')
904 /* exact match ...<a-b-c-d-e-f> - better than before? */
905 if (argi
> matching_argi
)
907 matching_argi
= argi
;
917 *pargi
= matching_argi
;
922 complete_option_list (char **ret
, size_t *cnt
, const struct option_list
*ol
,
923 const char *text
, const char *word
)
925 const OPTION
*opt
= NULL
;
927 size_t len
= strlen (word
);
929 for ( ; ol
!= NULL
; ol
= ol
->next
)
930 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
932 const char *name
= opt
->opt
.name
;
934 /* A long option to match against? */
938 /* Does this option actually match? */
939 if (strncmp (name
, word
, len
))
942 ret
= xrealloc (ret
, ++*cnt
* sizeof (ret
[0]));
943 ret
[*cnt
- 2] = xstrdup (name
);
949 /* All leading text is stored in @text, while the current word being
950 completed is stored in @word. Trailing text of @word is not. */
953 sim_complete_command (SIM_DESC sd
, const char *text
, const char *word
)
959 /* Only complete first word for now. */
963 cpu
= STATE_CPU (sd
, 0);
965 ret
= complete_option_list (ret
, &cnt
, CPU_OPTIONS (cpu
), text
, word
);
966 ret
= complete_option_list (ret
, &cnt
, STATE_OPTIONS (sd
), text
, word
);
974 sim_args_command (SIM_DESC sd
, char *cmd
)
976 /* something to do? */
978 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
982 /* user specified -<opt> ... form? */
983 char **argv
= buildargv (cmd
);
984 SIM_RC rc
= sim_parse_args (sd
, argv
);
990 char **argv
= buildargv (cmd
);
991 const OPTION
*matching_opt
= NULL
;
995 if (argv
[0] == NULL
)
996 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
998 /* First check for a cpu selector. */
1000 char *cpu_name
= xstrdup (argv
[0]);
1001 char *hyphen
= strchr (cpu_name
, '-');
1004 cpu
= sim_cpu_lookup (sd
, cpu_name
);
1007 /* If <cpuname>-<command>, point argv[0] at <command>. */
1011 argv
[0] += hyphen
- cpu_name
+ 1;
1015 matching_opt
= find_match (sd
, cpu
, argv
, &matching_argi
);
1016 /* If hyphen found restore argv[0]. */
1018 argv
[0] -= hyphen
- cpu_name
+ 1;
1023 /* If that failed, try the main table. */
1024 if (matching_opt
== NULL
)
1027 matching_opt
= find_match (sd
, NULL
, argv
, &matching_argi
);
1030 if (matching_opt
!= NULL
)
1032 switch (matching_opt
->opt
.has_arg
)
1035 if (argv
[matching_argi
+ 1] == NULL
)
1036 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1037 NULL
, 1/*is_command*/);
1039 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
1040 matching_opt
->opt
.name
);
1042 case optional_argument
:
1043 if (argv
[matching_argi
+ 1] == NULL
)
1044 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1045 NULL
, 1/*is_command*/);
1046 else if (argv
[matching_argi
+ 2] == NULL
)
1047 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1048 argv
[matching_argi
+ 1], 1/*is_command*/);
1050 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
1051 matching_opt
->opt
.name
);
1053 case required_argument
:
1054 if (argv
[matching_argi
+ 1] == NULL
)
1055 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
1056 matching_opt
->opt
.name
);
1057 else if (argv
[matching_argi
+ 2] == NULL
)
1058 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1059 argv
[matching_argi
+ 1], 1/*is_command*/);
1061 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
1062 matching_opt
->opt
.name
);
1071 /* didn't find anything that remotly matched */