1 /* Simulator option handling.
2 Copyright (C) 1996-2018 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"
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
,
112 static const OPTION standard_options
[] =
114 { {"verbose", no_argument
, NULL
, OPTION_VERBOSE
},
115 'v', NULL
, "Verbose output",
116 standard_option_handler
, NULL
},
118 { {"endian", required_argument
, NULL
, OPTION_ENDIAN
},
119 'E', "big|little", "Set endianness",
120 standard_option_handler
, NULL
},
122 /* This option isn't supported unless all choices are supported in keeping
123 with the goal of not printing in --help output things the simulator can't
124 do [as opposed to things that just haven't been configured in]. */
125 { {"environment", required_argument
, NULL
, OPTION_ENVIRONMENT
},
126 '\0', "user|virtual|operating", "Set running environment",
127 standard_option_handler
},
129 { {"alignment", required_argument
, NULL
, OPTION_ALIGNMENT
},
130 '\0', "strict|nonstrict|forced", "Set memory access alignment",
131 standard_option_handler
},
133 { {"debug", no_argument
, NULL
, OPTION_DEBUG
},
134 'D', NULL
, "Print debugging messages",
135 standard_option_handler
},
136 { {"debug-insn", no_argument
, NULL
, OPTION_DEBUG_INSN
},
137 '\0', NULL
, "Print instruction debugging messages",
138 standard_option_handler
},
139 { {"debug-file", required_argument
, NULL
, OPTION_DEBUG_FILE
},
140 '\0', "FILE NAME", "Specify debugging output file",
141 standard_option_handler
},
143 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
144 '\0', "COMMAND", ""/*undocumented*/,
145 standard_option_handler
},
147 { {"help", no_argument
, NULL
, OPTION_HELP
},
148 'H', NULL
, "Print help information",
149 standard_option_handler
},
150 { {"version", no_argument
, NULL
, OPTION_VERSION
},
151 '\0', NULL
, "Print version information",
152 standard_option_handler
},
154 { {"architecture", required_argument
, NULL
, OPTION_ARCHITECTURE
},
155 '\0', "MACHINE", "Specify the architecture to use",
156 standard_option_handler
},
157 { {"architecture-info", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
158 '\0', NULL
, "List supported architectures",
159 standard_option_handler
},
160 { {"info-architecture", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
162 standard_option_handler
},
164 { {"target", required_argument
, NULL
, OPTION_TARGET
},
165 '\0', "BFDNAME", "Specify the object-code format for the object files",
166 standard_option_handler
},
168 { {"load-lma", no_argument
, NULL
, OPTION_LOAD_LMA
},
170 "Use VMA or LMA addresses when loading image (default LMA)",
171 standard_option_handler
, "load-{lma,vma}" },
172 { {"load-vma", no_argument
, NULL
, OPTION_LOAD_VMA
},
173 '\0', NULL
, "", standard_option_handler
, "" },
175 { {"sysroot", required_argument
, NULL
, OPTION_SYSROOT
},
177 "Root for system calls with absolute file-names and cwd at start",
178 standard_option_handler
, NULL
},
180 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
, NULL
}
184 standard_option_handler (SIM_DESC sd
, sim_cpu
*cpu
, int opt
,
185 char *arg
, int is_command
)
189 switch ((STANDARD_OPTIONS
) opt
)
192 STATE_VERBOSE_P (sd
) = 1;
196 if (strcmp (arg
, "big") == 0)
198 if (WITH_TARGET_BYTE_ORDER
== BFD_ENDIAN_LITTLE
)
200 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
203 /* FIXME:wip: Need to set something in STATE_CONFIG. */
204 current_target_byte_order
= BFD_ENDIAN_BIG
;
206 else if (strcmp (arg
, "little") == 0)
208 if (WITH_TARGET_BYTE_ORDER
== BFD_ENDIAN_BIG
)
210 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
213 /* FIXME:wip: Need to set something in STATE_CONFIG. */
214 current_target_byte_order
= BFD_ENDIAN_LITTLE
;
218 sim_io_eprintf (sd
, "Invalid endian specification `%s'\n", arg
);
223 case OPTION_ENVIRONMENT
:
224 if (strcmp (arg
, "user") == 0)
225 STATE_ENVIRONMENT (sd
) = USER_ENVIRONMENT
;
226 else if (strcmp (arg
, "virtual") == 0)
227 STATE_ENVIRONMENT (sd
) = VIRTUAL_ENVIRONMENT
;
228 else if (strcmp (arg
, "operating") == 0)
229 STATE_ENVIRONMENT (sd
) = OPERATING_ENVIRONMENT
;
232 sim_io_eprintf (sd
, "Invalid environment specification `%s'\n", arg
);
235 if (WITH_ENVIRONMENT
!= ALL_ENVIRONMENT
236 && WITH_ENVIRONMENT
!= STATE_ENVIRONMENT (sd
))
239 switch (WITH_ENVIRONMENT
)
241 case USER_ENVIRONMENT
: type
= "user"; break;
242 case VIRTUAL_ENVIRONMENT
: type
= "virtual"; break;
243 case OPERATING_ENVIRONMENT
: type
= "operating"; break;
245 sim_io_eprintf (sd
, "Simulator compiled for the %s environment only.\n",
251 case OPTION_ALIGNMENT
:
252 if (strcmp (arg
, "strict") == 0)
254 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== STRICT_ALIGNMENT
)
256 current_alignment
= STRICT_ALIGNMENT
;
260 else if (strcmp (arg
, "nonstrict") == 0)
262 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== NONSTRICT_ALIGNMENT
)
264 current_alignment
= NONSTRICT_ALIGNMENT
;
268 else if (strcmp (arg
, "forced") == 0)
270 if (WITH_ALIGNMENT
== 0 || WITH_ALIGNMENT
== FORCED_ALIGNMENT
)
272 current_alignment
= FORCED_ALIGNMENT
;
278 sim_io_eprintf (sd
, "Invalid alignment specification `%s'\n", arg
);
281 switch (WITH_ALIGNMENT
)
283 case STRICT_ALIGNMENT
:
284 sim_io_eprintf (sd
, "Simulator compiled for strict alignment only.\n");
286 case NONSTRICT_ALIGNMENT
:
287 sim_io_eprintf (sd
, "Simulator compiled for nonstrict alignment only.\n");
289 case FORCED_ALIGNMENT
:
290 sim_io_eprintf (sd
, "Simulator compiled for forced alignment only.\n");
297 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
300 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
301 for (i
= 0; i
< MAX_DEBUG_VALUES
; ++i
)
302 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
306 case OPTION_DEBUG_INSN
:
308 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
311 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
312 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
316 case OPTION_DEBUG_FILE
:
318 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
321 FILE *f
= fopen (arg
, "w");
325 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
328 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
329 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
333 case OPTION_DO_COMMAND
:
334 sim_do_command (sd
, arg
);
337 case OPTION_ARCHITECTURE
:
339 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
342 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
345 STATE_ARCHITECTURE (sd
) = ap
;
349 case OPTION_ARCHITECTURE_INFO
:
351 const char **list
= bfd_arch_list ();
355 sim_io_printf (sd
, "Possible architectures:");
356 for (lp
= list
; *lp
!= NULL
; lp
++)
357 sim_io_printf (sd
, " %s", *lp
);
358 sim_io_printf (sd
, "\n");
365 STATE_TARGET (sd
) = xstrdup (arg
);
369 case OPTION_LOAD_LMA
:
371 STATE_LOAD_AT_LMA_P (sd
) = 1;
375 case OPTION_LOAD_VMA
:
377 STATE_LOAD_AT_LMA_P (sd
) = 0;
382 sim_print_help (sd
, is_command
);
383 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
385 /* FIXME: 'twould be nice to do something similar if gdb. */
389 sim_io_printf (sd
, "GNU simulator %s%s\n", PKGVERSION
, version
);
390 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
395 /* Don't leak memory in the odd event that there's lots of
396 --sysroot=... options. We treat "" specially since this
397 is the statically initialized value and cannot free it. */
398 if (simulator_sysroot
[0] != '\0')
399 free (simulator_sysroot
);
401 simulator_sysroot
= xstrdup (arg
);
403 simulator_sysroot
= "";
410 /* Add the standard option list to the simulator. */
413 standard_install (SIM_DESC sd
)
415 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
416 if (sim_add_option_table (sd
, NULL
, standard_options
) != SIM_RC_OK
)
418 STATE_LOAD_AT_LMA_P (sd
) = 1;
422 /* Return non-zero if arg is a duplicate argument.
423 If ARG is NULL, initialize. */
425 #define ARG_HASH_SIZE 97
426 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
429 dup_arg_p (const char *arg
)
432 static const char **arg_table
= NULL
;
436 if (arg_table
== NULL
)
437 arg_table
= (const char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
438 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
442 hash
= ARG_HASH (arg
);
443 while (arg_table
[hash
] != NULL
)
445 if (strcmp (arg
, arg_table
[hash
]) == 0)
447 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
448 don't check if the table is full. */
449 if (++hash
== ARG_HASH_SIZE
)
452 arg_table
[hash
] = arg
;
456 /* Called by sim_open to parse the arguments. */
459 sim_parse_args (SIM_DESC sd
, char * const *argv
)
461 int c
, i
, argc
, num_opts
, save_opterr
;
462 char *p
, *short_options
;
463 /* The `val' option struct entry is dynamically assigned for options that
464 only come in the long form. ORIG_VAL is used to get the original value
467 struct option
*lp
, *long_options
;
468 const struct option_list
*ol
;
470 OPTION_HANDLER
**handlers
;
472 SIM_RC result
= SIM_RC_OK
;
474 /* Count the number of arguments. */
475 argc
= countargv (argv
);
477 /* Count the number of options. */
479 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
480 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
482 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
483 for (ol
= CPU_OPTIONS (STATE_CPU (sd
, i
)); ol
!= NULL
; ol
= ol
->next
)
484 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
487 /* Initialize duplicate argument checker. */
488 (void) dup_arg_p (NULL
);
490 /* Build the option table for getopt. */
492 long_options
= NZALLOC (struct option
, num_opts
+ 1);
494 short_options
= NZALLOC (char, num_opts
* 3 + 1);
496 handlers
= NZALLOC (OPTION_HANDLER
*, OPTION_START
+ num_opts
);
497 orig_val
= NZALLOC (int, OPTION_START
+ num_opts
);
498 opt_cpu
= NZALLOC (sim_cpu
*, OPTION_START
+ num_opts
);
500 /* Set '+' as first char so argument permutation isn't done. This
501 is done to stop getopt_long returning options that appear after
502 the target program. Such options should be passed unchanged into
503 the program image. */
506 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
507 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
509 if (dup_arg_p (opt
->opt
.name
))
511 if (opt
->shortopt
!= 0)
513 *p
++ = opt
->shortopt
;
514 if (opt
->opt
.has_arg
== required_argument
)
516 else if (opt
->opt
.has_arg
== optional_argument
)
517 { *p
++ = ':'; *p
++ = ':'; }
518 handlers
[(unsigned char) opt
->shortopt
] = opt
->handler
;
519 if (opt
->opt
.val
!= 0)
520 orig_val
[(unsigned char) opt
->shortopt
] = opt
->opt
.val
;
522 orig_val
[(unsigned char) opt
->shortopt
] = opt
->shortopt
;
524 if (opt
->opt
.name
!= NULL
)
527 /* Dynamically assign `val' numbers for long options. */
529 handlers
[lp
->val
] = opt
->handler
;
530 orig_val
[lp
->val
] = opt
->opt
.val
;
531 opt_cpu
[lp
->val
] = NULL
;
536 for (c
= 0; c
< MAX_NR_PROCESSORS
; ++c
)
538 sim_cpu
*cpu
= STATE_CPU (sd
, c
);
539 for (ol
= CPU_OPTIONS (cpu
); ol
!= NULL
; ol
= ol
->next
)
540 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
542 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
543 on the need for dup_arg_p checking. Maybe in the future it'll be
544 needed so this is just commented out, and not deleted. */
545 if (dup_arg_p (opt
->opt
.name
))
548 /* Don't allow short versions of cpu specific options for now. */
549 if (opt
->shortopt
!= 0)
551 sim_io_eprintf (sd
, "internal error, short cpu specific option");
552 result
= SIM_RC_FAIL
;
555 if (opt
->opt
.name
!= NULL
)
559 /* Prepend --<cpuname>- to the option. */
560 if (asprintf (&name
, "%s-%s", CPU_NAME (cpu
), lp
->name
) < 0)
562 sim_io_eprintf (sd
, "internal error, out of memory");
563 result
= SIM_RC_FAIL
;
567 /* Dynamically assign `val' numbers for long options. */
569 handlers
[lp
->val
] = opt
->handler
;
570 orig_val
[lp
->val
] = opt
->opt
.val
;
571 opt_cpu
[lp
->val
] = cpu
;
577 /* Terminate the short and long option lists. */
581 /* Ensure getopt is initialized. */
584 /* Do not lot getopt throw errors for us. But don't mess with the state for
585 any callers higher up by saving/restoring it. */
586 save_opterr
= opterr
;
593 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
596 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
597 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
602 /* If getopt rejects a short option, optopt is set to the bad char.
603 If it rejects a long option, we have to look at optind. In the
604 short option case, argv could be multiple short options. */
610 sprintf (optbuf
, "-%c", optopt
);
614 badopt
= argv
[optind
- 1];
617 "%s: unrecognized option '%s'\n"
618 "Use --help for a complete list of options.\n",
619 STATE_MY_NAME (sd
), badopt
);
621 result
= SIM_RC_FAIL
;
625 if ((*handlers
[optc
]) (sd
, opt_cpu
[optc
], orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
627 result
= SIM_RC_FAIL
;
632 opterr
= save_opterr
;
635 free (short_options
);
642 /* Utility of sim_print_help to print a list of option tables. */
645 print_help (SIM_DESC sd
, sim_cpu
*cpu
, const struct option_list
*ol
, int is_command
)
649 for ( ; ol
!= NULL
; ol
= ol
->next
)
650 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
652 const int indent
= 30;
656 if (dup_arg_p (opt
->opt
.name
))
659 if (opt
->doc
== NULL
)
662 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
665 sim_io_printf (sd
, " ");
670 /* list any short options (aliases) for the current OPT */
676 if (o
->shortopt
!= '\0')
678 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
679 len
+= (comma
? 2 : 0) + 2;
682 if (o
->opt
.has_arg
== optional_argument
)
684 sim_io_printf (sd
, "[%s]", o
->arg
);
685 len
+= 1 + strlen (o
->arg
) + 1;
689 sim_io_printf (sd
, " %s", o
->arg
);
690 len
+= 1 + strlen (o
->arg
);
697 while (OPTION_VALID_P (o
) && o
->doc
== NULL
);
700 /* list any long options (aliases) for the current OPT */
705 const char *cpu_prefix
= cpu
? CPU_NAME (cpu
) : NULL
;
706 if (o
->doc_name
!= NULL
)
712 sim_io_printf (sd
, "%s%s%s%s%s",
714 is_command
? "" : "--",
715 cpu
? cpu_prefix
: "",
718 len
+= ((comma
? 2 : 0)
719 + (is_command
? 0 : 2)
723 if (o
->opt
.has_arg
== optional_argument
)
725 sim_io_printf (sd
, "[=%s]", o
->arg
);
726 len
+= 2 + 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
);
742 sim_io_printf (sd
, "\n%*s", indent
, "");
745 sim_io_printf (sd
, "%*s", indent
- len
, "");
747 /* print the description, word wrap long lines */
749 const char *chp
= opt
->doc
;
750 unsigned doc_width
= 80 - indent
;
751 while (strlen (chp
) >= doc_width
) /* some slack */
753 const char *end
= chp
+ doc_width
- 1;
754 while (end
> chp
&& !isspace (*end
))
757 end
= chp
+ doc_width
- 1;
758 /* The cast should be ok - its distances between to
759 points in a string. */
760 sim_io_printf (sd
, "%.*s\n%*s", (int) (end
- chp
), chp
, indent
,
763 while (isspace (*chp
) && *chp
!= '\0')
766 sim_io_printf (sd
, "%s\n", chp
);
771 /* Print help messages for the options. */
774 sim_print_help (SIM_DESC sd
, int is_command
)
776 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
777 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
780 /* Initialize duplicate argument checker. */
781 (void) dup_arg_p (NULL
);
783 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
784 sim_io_printf (sd
, "Options:\n");
786 sim_io_printf (sd
, "Commands:\n");
788 print_help (sd
, NULL
, STATE_OPTIONS (sd
), is_command
);
789 sim_io_printf (sd
, "\n");
791 /* Print cpu-specific options. */
795 for (i
= 0; i
< MAX_NR_PROCESSORS
; ++i
)
797 sim_cpu
*cpu
= STATE_CPU (sd
, i
);
798 if (CPU_OPTIONS (cpu
) == NULL
)
800 sim_io_printf (sd
, "CPU %s specific options:\n", CPU_NAME (cpu
));
801 print_help (sd
, cpu
, CPU_OPTIONS (cpu
), is_command
);
802 sim_io_printf (sd
, "\n");
806 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
807 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
808 sim_io_printf (sd
, " may not be applicable\n");
810 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
812 sim_io_printf (sd
, "\n");
813 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
814 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
818 /* Utility of sim_args_command to find the closest match for a command.
819 Commands that have "-" in them can be specified as separate words.
820 e.g. sim memory-region 0x800000,0x4000
821 or sim memory region 0x800000,0x4000
822 If CPU is non-null, use its option table list, otherwise use the main one.
823 *PARGI is where to start looking in ARGV. It is updated to point past
826 static const OPTION
*
827 find_match (SIM_DESC sd
, sim_cpu
*cpu
, char *argv
[], int *pargi
)
829 const struct option_list
*ol
;
831 /* most recent option match */
832 const OPTION
*matching_opt
= NULL
;
833 int matching_argi
= -1;
836 ol
= CPU_OPTIONS (cpu
);
838 ol
= STATE_OPTIONS (sd
);
840 /* Skip passed elements specified by *PARGI. */
843 for ( ; ol
!= NULL
; ol
= ol
->next
)
844 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
847 const char *name
= opt
->opt
.name
;
850 while (argv
[argi
] != NULL
851 && strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
853 name
= &name
[strlen (argv
[argi
])];
856 /* leading match ...<a-b-c>-d-e-f - continue search */
857 name
++; /* skip `-' */
861 else if (name
[0] == '\0')
863 /* exact match ...<a-b-c-d-e-f> - better than before? */
864 if (argi
> matching_argi
)
866 matching_argi
= argi
;
876 *pargi
= matching_argi
;
881 complete_option_list (char **ret
, size_t *cnt
, const struct option_list
*ol
,
882 const char *text
, const char *word
)
884 const OPTION
*opt
= NULL
;
886 size_t len
= strlen (word
);
888 for ( ; ol
!= NULL
; ol
= ol
->next
)
889 for (opt
= ol
->options
; OPTION_VALID_P (opt
); ++opt
)
891 const char *name
= opt
->opt
.name
;
893 /* A long option to match against? */
897 /* Does this option actually match? */
898 if (strncmp (name
, word
, len
))
901 ret
= xrealloc (ret
, ++*cnt
* sizeof (ret
[0]));
902 ret
[*cnt
- 2] = xstrdup (name
);
908 /* All leading text is stored in @text, while the current word being
909 completed is stored in @word. Trailing text of @word is not. */
912 sim_complete_command (SIM_DESC sd
, const char *text
, const char *word
)
918 /* Only complete first word for now. */
922 cpu
= STATE_CPU (sd
, 0);
924 ret
= complete_option_list (ret
, &cnt
, CPU_OPTIONS (cpu
), text
, word
);
925 ret
= complete_option_list (ret
, &cnt
, STATE_OPTIONS (sd
), text
, word
);
933 sim_args_command (SIM_DESC sd
, const char *cmd
)
935 /* something to do? */
937 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
941 /* user specified -<opt> ... form? */
942 char **argv
= buildargv (cmd
);
943 SIM_RC rc
= sim_parse_args (sd
, argv
);
949 char **argv
= buildargv (cmd
);
950 const OPTION
*matching_opt
= NULL
;
954 if (argv
[0] == NULL
)
957 return SIM_RC_OK
; /* FIXME - perhaps help would be better */
960 /* First check for a cpu selector. */
962 char *cpu_name
= xstrdup (argv
[0]);
963 char *hyphen
= strchr (cpu_name
, '-');
966 cpu
= sim_cpu_lookup (sd
, cpu_name
);
969 /* If <cpuname>-<command>, point argv[0] at <command>. */
973 argv
[0] += hyphen
- cpu_name
+ 1;
977 matching_opt
= find_match (sd
, cpu
, argv
, &matching_argi
);
978 /* If hyphen found restore argv[0]. */
980 argv
[0] -= hyphen
- cpu_name
+ 1;
985 /* If that failed, try the main table. */
986 if (matching_opt
== NULL
)
989 matching_opt
= find_match (sd
, NULL
, argv
, &matching_argi
);
992 if (matching_opt
!= NULL
)
994 switch (matching_opt
->opt
.has_arg
)
997 if (argv
[matching_argi
+ 1] == NULL
)
998 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
999 NULL
, 1/*is_command*/);
1001 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
1002 matching_opt
->opt
.name
);
1004 case optional_argument
:
1005 if (argv
[matching_argi
+ 1] == NULL
)
1006 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1007 NULL
, 1/*is_command*/);
1008 else if (argv
[matching_argi
+ 2] == NULL
)
1009 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1010 argv
[matching_argi
+ 1], 1/*is_command*/);
1012 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
1013 matching_opt
->opt
.name
);
1015 case required_argument
:
1016 if (argv
[matching_argi
+ 1] == NULL
)
1017 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
1018 matching_opt
->opt
.name
);
1019 else if (argv
[matching_argi
+ 2] == NULL
)
1020 matching_opt
->handler (sd
, cpu
, matching_opt
->opt
.val
,
1021 argv
[matching_argi
+ 1], 1/*is_command*/);
1023 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
1024 matching_opt
->opt
.name
);
1033 /* didn't find anything that remotly matched */