1 /* Simulator option handling.
2 Copyright (C) 1996-2019 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/>. */
34 #include "libiberty.h"
35 #include "sim-options.h"
37 #include "sim-assert.h"
42 /* Add a set of options to the simulator.
43 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
44 This is intended to be called by modules in their `install' handler. */
47 sim_add_option_table (SIM_DESC sd
, sim_cpu
*cpu
, const OPTION
*table
)
49 struct option_list
*ol
= ((struct option_list
*)
50 xmalloc (sizeof (struct option_list
)));
52 /* Note: The list is constructed in the reverse order we're called so
53 later calls will override earlier ones (in case that ever happens).
54 This is the intended behaviour. */
58 ol
->next
= CPU_OPTIONS (cpu
);
60 CPU_OPTIONS (cpu
) = ol
;
64 ol
->next
= STATE_OPTIONS (sd
);
66 STATE_OPTIONS (sd
) = ol
;
72 /* Standard option table.
73 Modules may specify additional ones.
74 The caller of sim_parse_args may also specify additional options
75 by calling sim_add_option_table first. */
77 static DECLARE_OPTION_HANDLER (standard_option_handler
);
79 /* FIXME: We shouldn't print in --help output options that aren't usable.
80 Some fine tuning will be necessary. One can either move less general
81 options to another table or use a HAVE_FOO macro to ifdef out unavailable
84 /* ??? One might want to conditionally compile out the entries that
85 aren't enabled. There's a distinction, however, between options a
86 simulator can't support and options that haven't been configured in.
87 Certainly options a simulator can't support shouldn't appear in the
88 output of --help. Whether the same thing applies to options that haven't
89 been configured in or not isn't something I can get worked up over.
90 [Note that conditionally compiling them out might simply involve moving
91 the option to another table.]
92 If you decide to conditionally compile them out as well, delete this
93 comment and add a comment saying that that is the rule. */
96 OPTION_DEBUG_INSN
= OPTION_START
,
101 OPTION_ARCHITECTURE_INFO
,
114 static const OPTION standard_options
[] =
116 { {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
117 'v', NULL
, "Verbose output",
118 standard_option_handler
, NULL
},
120 { {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
121 'E', "big|little", "Set endianness",
122 standard_option_handler
, NULL
},
124 /* This option isn't supported unless all choices are supported in keeping
125 with the goal of not printing in --help output things the simulator can't
126 do [as opposed to things that just haven't been configured in]. */
127 { {"environment", required_argument
, NULL
, OPTION_ENVIRONMENT
},
128 '\0', "user|virtual|operating", "Set running environment",
129 standard_option_handler
},
131 { {"alignment", required_argument
, NULL
, OPTION_ALIGNMENT
},
132 '\0', "strict|nonstrict|forced", "Set memory access alignment",
133 standard_option_handler
},
135 { {"debug", no_argument
, NULL
, OPTION_DEBUG
},
136 'D', NULL
, "Print debugging messages",
137 standard_option_handler
},
138 { {"debug-insn", no_argument
, NULL
, OPTION_DEBUG_INSN
},
139 '\0', NULL
, "Print instruction debugging messages",
140 standard_option_handler
},
141 { {"debug-file", required_argument
, NULL
, OPTION_DEBUG_FILE
},
142 '\0', "FILE NAME", "Specify debugging output file",
143 standard_option_handler
},
145 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
146 '\0', "COMMAND", ""/*undocumented*/,
147 standard_option_handler
},
149 { {"help", no_argument
, NULL
, OPTION_HELP
},
150 'H', NULL
, "Print help information",
151 standard_option_handler
},
152 { {"version", no_argument
, NULL
, OPTION_VERSION
},
153 '\0', NULL
, "Print version information",
154 standard_option_handler
},
156 { {"architecture", required_argument
, NULL
, OPTION_ARCHITECTURE
},
157 '\0', "MACHINE", "Specify the architecture to use",
158 standard_option_handler
},
159 { {"architecture-info", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
160 '\0', NULL
, "List supported architectures",
161 standard_option_handler
},
162 { {"info-architecture", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
164 standard_option_handler
},
166 { {"target", required_argument
, NULL
, OPTION_TARGET
},
167 '\0', "BFDNAME", "Specify the object-code format for the object files",
168 standard_option_handler
},
170 { {"load-lma", no_argument
, NULL
, OPTION_LOAD_LMA
},
172 "Use VMA or LMA addresses when loading image (default LMA)",
173 standard_option_handler
, "load-{lma,vma}" },
174 { {"load-vma", no_argument
, NULL
, OPTION_LOAD_VMA
},
175 '\0', NULL
, "", standard_option_handler
, "" },
177 { {"sysroot", required_argument
, NULL
, OPTION_SYSROOT
},
179 "Root for system calls with absolute file-names and cwd at start",
180 standard_option_handler
, NULL
},
182 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
186 standard_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
187 char *arg
, int is_command
)
191 switch ((STANDARD_OPTIONS
) opt
)
194 STATE_VERBOSE_P (sd
) = 1;
198 if (strcmp (arg
, "big") == 0)
200 if (WITH_TARGET_BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
202 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
205 /* FIXME:wip: Need to set something in STATE_CONFIG. */
206 current_target_byte_order
= BFD_ENDIAN_BIG
;
208 else if (strcmp (arg
, "little") == 0)
210 if (WITH_TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
212 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
215 /* FIXME:wip: Need to set something in STATE_CONFIG. */
216 current_target_byte_order
= BFD_ENDIAN_LITTLE
;
220 sim_io_eprintf (sd
, "Invalid endian specification `%s'\n", arg
);
225 case OPTION_ENVIRONMENT
:
226 if (strcmp (arg
, "user") == 0)
227 STATE_ENVIRONMENT (sd
) = USER_ENVIRONMENT
;
228 else if (strcmp (arg
, "virtual") == 0)
229 STATE_ENVIRONMENT (sd
) = VIRTUAL_ENVIRONMENT
;
230 else if (strcmp (arg
, "operating") == 0)
231 STATE_ENVIRONMENT (sd
) = OPERATING_ENVIRONMENT
;
234 sim_io_eprintf (sd
, "Invalid environment specification `%s'\n", arg
);
237 if (WITH_ENVIRONMENT
!= ALL_ENVIRONMENT
238 && WITH_ENVIRONMENT
!= STATE_ENVIRONMENT (sd
))
241 switch (WITH_ENVIRONMENT
)
243 case USER_ENVIRONMENT
: type
= "user"; break;
244 case VIRTUAL_ENVIRONMENT
: type
= "virtual"; break;
245 case OPERATING_ENVIRONMENT
: type
= "operating"; break;
247 sim_io_eprintf (sd
, "Simulator compiled for the %s environment only.\n",
253 case OPTION_ALIGNMENT
:
254 if (strcmp (arg
, "strict") == 0)
256 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== STRICT_ALIGNMENT
)
258 current_alignment
= STRICT_ALIGNMENT
;
262 else if (strcmp (arg
, "nonstrict") == 0)
264 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== NONSTRICT_ALIGNMENT
)
266 current_alignment
= NONSTRICT_ALIGNMENT
;
270 else if (strcmp (arg
, "forced") == 0)
272 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== FORCED_ALIGNMENT
)
274 current_alignment
= FORCED_ALIGNMENT
;
280 sim_io_eprintf (sd
, "Invalid alignment specification `%s'\n", arg
);
283 switch (WITH_ALIGNMENT
)
285 case STRICT_ALIGNMENT
:
286 sim_io_eprintf (sd
, "Simulator compiled for strict alignment only.\n");
288 case NONSTRICT_ALIGNMENT
:
289 sim_io_eprintf (sd
, "Simulator compiled for nonstrict alignment only.\n");
291 case FORCED_ALIGNMENT
:
292 sim_io_eprintf (sd
, "Simulator compiled for forced alignment only.\n");
299 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
302 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
303 for (i
= 0; i
< MAX_DEBUG_VALUES
; ++i
)
304 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
308 case OPTION_DEBUG_INSN
:
310 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
313 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
314 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
318 case OPTION_DEBUG_FILE
:
320 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
323 FILE *f
= fopen (arg
, "w");
327 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
330 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
331 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
335 case OPTION_DO_COMMAND
:
336 sim_do_command (sd
, arg
);
339 case OPTION_ARCHITECTURE
:
341 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
344 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
347 STATE_ARCHITECTURE (sd
) = ap
;
351 case OPTION_ARCHITECTURE_INFO
:
353 const char **list
= bfd_arch_list ();
357 sim_io_printf (sd
, "Possible architectures:");
358 for (lp
= list
; *lp
!= NULL
; lp
++)
359 sim_io_printf (sd
, " %s", *lp
);
360 sim_io_printf (sd
, "\n");
367 STATE_TARGET (sd
) = xstrdup (arg
);
371 case OPTION_LOAD_LMA
:
373 STATE_LOAD_AT_LMA_P (sd
) = 1;
377 case OPTION_LOAD_VMA
:
379 STATE_LOAD_AT_LMA_P (sd
) = 0;
384 sim_print_help (sd
, is_command
);
385 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
387 /* FIXME: 'twould be nice to do something similar if gdb. */
391 sim_io_printf (sd
, "GNU simulator %s%s\n", PKGVERSION
, version
);
392 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
397 /* Don't leak memory in the odd event that there's lots of
398 --sysroot=... options. We treat "" specially since this
399 is the statically initialized value and cannot free it. */
400 if (simulator_sysroot
[0] != '\0')
401 free (simulator_sysroot
);
403 simulator_sysroot
= xstrdup (arg
);
405 simulator_sysroot
= "";
412 /* Add the standard option list to the simulator. */
415 standard_install (SIM_DESC sd
)
417 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
418 if (sim_add_option_table (sd
, NULL
, standard_options
) != SIM_RC_OK
)
420 STATE_LOAD_AT_LMA_P (sd
) = 1;
424 /* Return non-zero if arg is a duplicate argument.
425 If ARG is NULL, initialize. */
427 #define ARG_HASH_SIZE 97
428 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
431 dup_arg_p (const char *arg
)
434 static const char **arg_table
= NULL
;
438 if (arg_table
== NULL
)
439 arg_table
= (const char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
440 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
444 hash
= ARG_HASH (arg
);
445 while (arg_table
[hash
] != NULL
)
447 if (strcmp (arg
, arg_table
[hash
]) == 0)
449 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
450 don't check if the table is full. */
451 if (++hash
== ARG_HASH_SIZE
)
454 arg_table
[hash
] = arg
;
458 /* Called by sim_open to parse the arguments. */
461 sim_parse_args (SIM_DESC sd
, char * const *argv
)
463 int c
, i
, argc
, num_opts
, save_opterr
;
464 char *p
, *short_options
;
465 /* The `val' option struct entry is dynamically assigned for options that
466 only come in the long form. ORIG_VAL is used to get the original value
469 struct option
*lp
, *long_options
;
470 const struct option_list
*ol
;
472 OPTION_HANDLER
**handlers
;
474 SIM_RC result
= SIM_RC_OK
;
476 /* Count the number of arguments. */
477 argc
= countargv (argv
);
479 /* Count the number of options. */
481 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
482 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
484 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
485 for (ol
= CPU_OPTIONS (STATE_CPU (sd
, i
)); ol
!= NULL
; ol
= ol
->next
)
486 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
489 /* Initialize duplicate argument checker. */
490 (void) dup_arg_p (NULL
);
492 /* Build the option table for getopt. */
494 long_options
= NZALLOC (struct option
, num_opts
+ 1);
496 short_options
= NZALLOC (char, num_opts
* 3 + 1);
498 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
499 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
500 opt_cpu
= NZALLOC (sim_cpu
*, OPTION_START
+ num_opts
);
502 /* Set '+' as first char so argument permutation isn't done. This
503 is done to stop getopt_long returning options that appear after
504 the target program. Such options should be passed unchanged into
505 the program image. */
508 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
509 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
511 if (dup_arg_p (opt
->opt
.name
))
513 if (opt
->shortopt
!= 0)
515 *p
++ = opt
->shortopt
;
516 if (opt
->opt
.has_arg
== required_argument
)
518 else if (opt
->opt
.has_arg
== optional_argument
)
519 { *p
++ = ':'; *p
++ = ':'; }
520 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
521 if (opt
->opt
.val
!= 0)
522 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
524 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
526 if (opt
->opt
.name
!= NULL
)
529 /* Dynamically assign `val' numbers for long options. */
531 handlers
[lp
->val
] = opt
->handler
;
532 orig_val
[lp
->val
] = opt
->opt
.val
;
533 opt_cpu
[lp
->val
] = NULL
;
538 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
540 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
541 for (ol
= CPU_OPTIONS (cpu
); ol
!= NULL
; ol
= ol
->next
)
542 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
544 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
545 on the need for dup_arg_p checking. Maybe in the future it'll be
546 needed so this is just commented out, and not deleted. */
547 if (dup_arg_p (opt
->opt
.name
))
550 /* Don't allow short versions of cpu specific options for now. */
551 if (opt
->shortopt
!= 0)
553 sim_io_eprintf (sd
, "internal error, short cpu specific option");
554 result
= SIM_RC_FAIL
;
557 if (opt
->opt
.name
!= NULL
)
561 /* Prepend --<cpuname>- to the option. */
562 if (asprintf (&name
, "%s-%s", CPU_NAME (cpu
), lp
->name
) < 0)
564 sim_io_eprintf (sd
, "internal error, out of memory");
565 result
= SIM_RC_FAIL
;
569 /* Dynamically assign `val' numbers for long options. */
571 handlers
[lp
->val
] = opt
->handler
;
572 orig_val
[lp
->val
] = opt
->opt
.val
;
573 opt_cpu
[lp
->val
] = cpu
;
579 /* Terminate the short and long option lists. */
583 /* Ensure getopt is initialized. */
586 /* Do not lot getopt throw errors for us. But don't mess with the state for
587 any callers higher up by saving/restoring it. */
588 save_opterr
= opterr
;
595 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
598 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
599 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
604 /* If getopt rejects a short option, optopt is set to the bad char.
605 If it rejects a long option, we have to look at optind. In the
606 short option case, argv could be multiple short options. */
612 sprintf (optbuf
, "-%c", optopt
);
616 badopt
= argv
[optind
- 1];
619 "%s: unrecognized option '%s'\n"
620 "Use --help for a complete list of options.\n",
621 STATE_MY_NAME (sd
), badopt
);
623 result
= SIM_RC_FAIL
;
627 if ((*handlers
[optc
]) (sd
, opt_cpu
[optc
], orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
629 result
= SIM_RC_FAIL
;
634 opterr
= save_opterr
;
637 free (short_options
);
644 /* Utility of sim_print_help to print a list of option tables. */
647 print_help (SIM_DESC sd
, sim_cpu
*cpu
, const struct option_list
*ol
, int is_command
)
651 for ( ; ol
!= NULL
; ol
= ol
->next
)
652 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
654 const int indent
= 30;
658 if (dup_arg_p (opt
->opt
.name
))
661 if (opt
->doc
== NULL
)
664 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
667 sim_io_printf (sd
, " ");
672 /* list any short options (aliases) for the current OPT */
678 if (o
->shortopt
!= '\0')
680 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
681 len
+= (comma
? 2 : 0) + 2;
684 if (o
->opt
.has_arg
== optional_argument
)
686 sim_io_printf (sd
, "[%s]", o
->arg
);
687 len
+= 1 + strlen (o
->arg
) + 1;
691 sim_io_printf (sd
, " %s", o
->arg
);
692 len
+= 1 + strlen (o
->arg
);
699 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
702 /* list any long options (aliases) for the current OPT */
707 const char *cpu_prefix
= cpu
? CPU_NAME (cpu
) : NULL
;
708 if (o
->doc_name
!= NULL
)
714 sim_io_printf (sd
, "%s%s%s%s%s",
716 is_command
? "" : "--",
717 cpu
? cpu_prefix
: "",
720 len
+= ((comma
? 2 : 0)
721 + (is_command
? 0 : 2)
725 if (o
->opt
.has_arg
== optional_argument
)
727 sim_io_printf (sd
, "[=%s]", o
->arg
);
728 len
+= 2 + strlen (o
->arg
) + 1;
732 sim_io_printf (sd
, " %s", o
->arg
);
733 len
+= 1 + strlen (o
->arg
);
740 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
744 sim_io_printf (sd
, "\n%*s", indent
, "");
747 sim_io_printf (sd
, "%*s", indent
- len
, "");
749 /* print the description, word wrap long lines */
751 const char *chp
= opt
->doc
;
752 unsigned doc_width
= 80 - indent
;
753 while (strlen (chp
) >= doc_width
) /* some slack */
755 const char *end
= chp
+ doc_width
- 1;
756 while (end
> chp
&& !isspace (*end
))
759 end
= chp
+ doc_width
- 1;
760 /* The cast should be ok - its distances between to
761 points in a string. */
762 sim_io_printf (sd
, "%.*s\n%*s", (int) (end
- chp
), chp
, indent
,
765 while (isspace (*chp
) && *chp
!= '\0')
768 sim_io_printf (sd
, "%s\n", chp
);
773 /* Print help messages for the options. */
776 sim_print_help (SIM_DESC sd
, int is_command
)
778 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
779 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
782 /* Initialize duplicate argument checker. */
783 (void) dup_arg_p (NULL
);
785 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
786 sim_io_printf (sd
, "Options:\n");
788 sim_io_printf (sd
, "Commands:\n");
790 print_help (sd
, NULL
, STATE_OPTIONS (sd
), is_command
);
791 sim_io_printf (sd
, "\n");
793 /* Print cpu-specific options. */
797 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
799 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
800 if (CPU_OPTIONS (cpu
) == NULL
)
802 sim_io_printf (sd
, "CPU %s specific options:\n", CPU_NAME (cpu
));
803 print_help (sd
, cpu
, CPU_OPTIONS (cpu
), is_command
);
804 sim_io_printf (sd
, "\n");
808 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
809 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
810 sim_io_printf (sd
, " may not be applicable\n");
812 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
814 sim_io_printf (sd
, "\n");
815 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
816 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
820 /* Utility of sim_args_command to find the closest match for a command.
821 Commands that have "-" in them can be specified as separate words.
822 e.g. sim memory-region 0x800000,0x4000
823 or sim memory region 0x800000,0x4000
824 If CPU is non-null, use its option table list, otherwise use the main one.
825 *PARGI is where to start looking in ARGV. It is updated to point past
828 static const OPTION
*
829 find_match (SIM_DESC sd
, sim_cpu
*cpu
, char *argv
[], int *pargi
)
831 const struct option_list
*ol
;
833 /* most recent option match */
834 const OPTION
*matching_opt
= NULL
;
835 int matching_argi
= -1;
838 ol
= CPU_OPTIONS (cpu
);
840 ol
= STATE_OPTIONS (sd
);
842 /* Skip passed elements specified by *PARGI. */
845 for ( ; ol
!= NULL
; ol
= ol
->next
)
846 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
849 const char *name
= opt
->opt
.name
;
852 while (argv
[argi
] != NULL
853 && strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
855 name
= &name
[strlen (argv
[argi
])];
858 /* leading match ...<a-b-c>-d-e-f - continue search */
859 name
++; /* skip `-' */
863 else if (name
[0] == '\0')
865 /* exact match ...<a-b-c-d-e-f> - better than before? */
866 if (argi
> matching_argi
)
868 matching_argi
= argi
;
878 *pargi
= matching_argi
;
883 complete_option_list (char **ret
, size_t *cnt
, const struct option_list
*ol
,
884 const char *text
, const char *word
)
886 const OPTION
*opt
= NULL
;
888 size_t len
= strlen (word
);
890 for ( ; ol
!= NULL
; ol
= ol
->next
)
891 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
893 const char *name
= opt
->opt
.name
;
895 /* A long option to match against? */
899 /* Does this option actually match? */
900 if (strncmp (name
, word
, len
))
903 ret
= xrealloc (ret
, ++*cnt
* sizeof (ret
[0]));
904 ret
[*cnt
- 2] = xstrdup (name
);
910 /* All leading text is stored in @text, while the current word being
911 completed is stored in @word. Trailing text of @word is not. */
914 sim_complete_command (SIM_DESC sd
, const char *text
, const char *word
)
920 /* Only complete first word for now. */
924 cpu
= STATE_CPU (sd
, 0);
926 ret
= complete_option_list (ret
, &cnt
, CPU_OPTIONS (cpu
), text
, word
);
927 ret
= complete_option_list (ret
, &cnt
, STATE_OPTIONS (sd
), text
, word
);
935 sim_args_command (SIM_DESC sd
, const char *cmd
)
937 /* something to do? */
939 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
943 /* user specified -<opt> ... form? */
944 char **argv
= buildargv (cmd
);
945 SIM_RC rc
= sim_parse_args (sd
, argv
);
951 char **argv
= buildargv (cmd
);
952 const OPTION
*matching_opt
= NULL
;
956 if (argv
[0] == NULL
)
959 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
962 /* First check for a cpu selector. */
964 char *cpu_name
= xstrdup (argv
[0]);
965 char *hyphen
= strchr (cpu_name
, '-');
968 cpu
= sim_cpu_lookup (sd
, cpu_name
);
971 /* If <cpuname>-<command>, point argv[0] at <command>. */
975 argv
[0] += hyphen
- cpu_name
+ 1;
979 matching_opt
= find_match (sd
, cpu
, argv
, &matching_argi
);
980 /* If hyphen found restore argv[0]. */
982 argv
[0] -= hyphen
- cpu_name
+ 1;
987 /* If that failed, try the main table. */
988 if (matching_opt
== NULL
)
991 matching_opt
= find_match (sd
, NULL
, argv
, &matching_argi
);
994 if (matching_opt
!= NULL
)
996 switch (matching_opt
->opt
.has_arg
)
999 if (argv
[matching_argi
+ 1] == NULL
)
1000 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1001 NULL
, 1/*is_command*/);
1003 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
1004 matching_opt
->opt
.name
);
1006 case optional_argument
:
1007 if (argv
[matching_argi
+ 1] == NULL
)
1008 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1009 NULL
, 1/*is_command*/);
1010 else if (argv
[matching_argi
+ 2] == NULL
)
1011 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1012 argv
[matching_argi
+ 1], 1/*is_command*/);
1014 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
1015 matching_opt
->opt
.name
);
1017 case required_argument
:
1018 if (argv
[matching_argi
+ 1] == NULL
)
1019 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
1020 matching_opt
->opt
.name
);
1021 else if (argv
[matching_argi
+ 2] == NULL
)
1022 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1023 argv
[matching_argi
+ 1], 1/*is_command*/);
1025 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
1026 matching_opt
->opt
.name
);
1035 /* didn't find anything that remotly matched */