1 /* Simulator option handling.
2 Copyright (C) 1996-2015 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,
107 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
117 static const OPTION standard_options[] =
119 { {"verbose", no_argument, NULL, OPTION_VERBOSE},
120 'v', NULL, "Verbose output",
121 standard_option_handler, NULL },
123 { {"endian", required_argument, NULL, OPTION_ENDIAN},
124 'E', "big|little", "Set endianness",
125 standard_option_handler, NULL },
127 #ifdef SIM_HAVE_ENVIRONMENT
128 /* This option isn't supported unless all choices are supported in keeping
129 with the goal of not printing in --help output things the simulator can't
130 do [as opposed to things that just haven't been configured in]. */
131 { {"environment", required_argument, NULL, OPTION_ENVIRONMENT},
132 '\0', "user|virtual|operating", "Set running environment",
133 standard_option_handler },
136 { {"alignment", required_argument, NULL, OPTION_ALIGNMENT},
137 '\0', "strict|nonstrict|forced", "Set memory access alignment",
138 standard_option_handler },
140 { {"debug", no_argument, NULL, OPTION_DEBUG},
141 'D', NULL, "Print debugging messages",
142 standard_option_handler },
143 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
144 '\0', NULL, "Print instruction debugging messages",
145 standard_option_handler },
146 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
147 '\0', "FILE NAME", "Specify debugging output file",
148 standard_option_handler },
150 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
151 { {"h8300h", no_argument, NULL, OPTION_H8300H},
152 'h', NULL, "Indicate the CPU is H8/300H",
153 standard_option_handler },
154 { {"h8300s", no_argument, NULL, OPTION_H8300S},
155 'S', NULL, "Indicate the CPU is H8S",
156 standard_option_handler },
157 { {"h8300sx", no_argument, NULL, OPTION_H8300SX},
158 'x', NULL, "Indicate the CPU is H8SX",
159 standard_option_handler },
162 { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
163 '\0', "COMMAND", ""/*undocumented*/,
164 standard_option_handler },
166 { {"help", no_argument, NULL, OPTION_HELP},
167 'H', NULL, "Print help information",
168 standard_option_handler },
169 { {"version", no_argument, NULL, OPTION_VERSION},
170 '\0', NULL, "Print version information",
171 standard_option_handler },
173 { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
174 '\0', "MACHINE", "Specify the architecture to use",
175 standard_option_handler },
176 { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
177 '\0', NULL, "List supported architectures",
178 standard_option_handler },
179 { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
181 standard_option_handler },
183 { {"target", required_argument, NULL, OPTION_TARGET},
184 '\0', "BFDNAME", "Specify the object-code format for the object files",
185 standard_option_handler },
187 #ifdef SIM_HANDLES_LMA
188 { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA},
191 "Use VMA or LMA addresses when loading image (default LMA)",
193 "Use VMA or LMA addresses when loading image (default VMA)",
195 standard_option_handler, "load-{lma,vma}" },
196 { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA},
197 '\0', NULL, "", standard_option_handler, "" },
200 { {"sysroot", required_argument, NULL, OPTION_SYSROOT},
202 "Root for system calls with absolute file-names and cwd at start",
203 standard_option_handler, NULL },
205 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
209 standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
210 char *arg, int is_command)
214 switch ((STANDARD_OPTIONS) opt)
217 STATE_VERBOSE_P (sd) = 1;
221 if (strcmp (arg, "big") == 0)
223 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
225 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
228 /* FIXME:wip: Need to set something in STATE_CONFIG. */
229 current_target_byte_order = BIG_ENDIAN;
231 else if (strcmp (arg, "little") == 0)
233 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
235 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
238 /* FIXME:wip: Need to set something in STATE_CONFIG. */
239 current_target_byte_order = LITTLE_ENDIAN;
243 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
248 case OPTION_ENVIRONMENT:
249 if (strcmp (arg, "user") == 0)
250 STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT;
251 else if (strcmp (arg, "virtual") == 0)
252 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
253 else if (strcmp (arg, "operating") == 0)
254 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
257 sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg);
260 if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
261 && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd))
264 switch (WITH_ENVIRONMENT)
266 case USER_ENVIRONMENT: type = "user"; break;
267 case VIRTUAL_ENVIRONMENT: type = "virtual"; break;
268 case OPERATING_ENVIRONMENT: type = "operating"; break;
270 sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n",
276 case OPTION_ALIGNMENT:
277 if (strcmp (arg, "strict") == 0)
279 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT)
281 current_alignment = STRICT_ALIGNMENT;
285 else if (strcmp (arg, "nonstrict") == 0)
287 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT)
289 current_alignment = NONSTRICT_ALIGNMENT;
293 else if (strcmp (arg, "forced") == 0)
295 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT)
297 current_alignment = FORCED_ALIGNMENT;
303 sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg);
306 switch (WITH_ALIGNMENT)
308 case STRICT_ALIGNMENT:
309 sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n");
311 case NONSTRICT_ALIGNMENT:
312 sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n");
314 case FORCED_ALIGNMENT:
315 sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n");
322 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
325 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
326 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
327 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
331 case OPTION_DEBUG_INSN :
333 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
336 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
337 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
341 case OPTION_DEBUG_FILE :
343 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
346 FILE *f = fopen (arg, "w");
350 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
353 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
354 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
358 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
360 set_h8300h (bfd_mach_h8300h);
363 set_h8300h (bfd_mach_h8300s);
366 set_h8300h (bfd_mach_h8300sx);
370 case OPTION_DO_COMMAND:
371 sim_do_command (sd, arg);
374 case OPTION_ARCHITECTURE:
376 const struct bfd_arch_info *ap = bfd_scan_arch (arg);
379 sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
382 STATE_ARCHITECTURE (sd) = ap;
386 case OPTION_ARCHITECTURE_INFO:
388 const char **list = bfd_arch_list ();
392 sim_io_printf (sd, "Possible architectures:");
393 for (lp = list; *lp != NULL; lp++)
394 sim_io_printf (sd, " %s", *lp);
395 sim_io_printf (sd, "\n");
402 STATE_TARGET (sd) = xstrdup (arg);
406 case OPTION_LOAD_LMA:
408 STATE_LOAD_AT_LMA_P (sd) = 1;
412 case OPTION_LOAD_VMA:
414 STATE_LOAD_AT_LMA_P (sd) = 0;
419 sim_print_help (sd, is_command);
420 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
422 /* FIXME: 'twould be nice to do something similar if gdb. */
426 sim_io_printf (sd, "GNU simulator %s%s\n", PKGVERSION, version);
427 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
432 /* Don't leak memory in the odd event that there's lots of
433 --sysroot=... options. We treat "" specially since this
434 is the statically initialized value and cannot free it. */
435 if (simulator_sysroot[0] != '\0')
436 free (simulator_sysroot);
438 simulator_sysroot = xstrdup (arg);
440 simulator_sysroot = "";
447 /* Add the standard option list to the simulator. */
450 standard_install (SIM_DESC sd)
452 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
453 if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK)
455 #ifdef SIM_HANDLES_LMA
456 STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA;
461 /* Return non-zero if arg is a duplicate argument.
462 If ARG is NULL, initialize. */
464 #define ARG_HASH_SIZE 97
465 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
468 dup_arg_p (const char *arg)
471 static const char **arg_table = NULL;
475 if (arg_table == NULL)
476 arg_table = (const char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
477 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
481 hash = ARG_HASH (arg);
482 while (arg_table[hash] != NULL)
484 if (strcmp (arg, arg_table[hash]) == 0)
486 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
487 don't check if the table is full. */
488 if (++hash == ARG_HASH_SIZE)
491 arg_table[hash] = arg;
495 /* Called by sim_open to parse the arguments. */
498 sim_parse_args (SIM_DESC sd, char **argv)
500 int c, i, argc, num_opts;
501 char *p, *short_options;
502 /* The `val' option struct entry is dynamically assigned for options that
503 only come in the long form. ORIG_VAL is used to get the original value
506 struct option *lp, *long_options;
507 const struct option_list *ol;
509 OPTION_HANDLER **handlers;
511 SIM_RC result = SIM_RC_OK;
513 /* Count the number of arguments. */
514 for (argc = 0; argv[argc] != NULL; ++argc)
517 /* Count the number of options. */
519 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
520 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
522 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
523 for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next)
524 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
527 /* Initialize duplicate argument checker. */
528 (void) dup_arg_p (NULL);
530 /* Build the option table for getopt. */
532 long_options = NZALLOC (struct option, num_opts + 1);
534 short_options = NZALLOC (char, num_opts * 3 + 1);
536 handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts);
537 orig_val = NZALLOC (int, OPTION_START + num_opts);
538 opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts);
540 /* Set '+' as first char so argument permutation isn't done. This
541 is done to stop getopt_long returning options that appear after
542 the target program. Such options should be passed unchanged into
543 the program image. */
546 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
547 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
549 if (dup_arg_p (opt->opt.name))
551 if (opt->shortopt != 0)
553 *p++ = opt->shortopt;
554 if (opt->opt.has_arg == required_argument)
556 else if (opt->opt.has_arg == optional_argument)
557 { *p++ = ':'; *p++ = ':'; }
558 handlers[(unsigned char) opt->shortopt] = opt->handler;
559 if (opt->opt.val != 0)
560 orig_val[(unsigned char) opt->shortopt] = opt->opt.val;
562 orig_val[(unsigned char) opt->shortopt] = opt->shortopt;
564 if (opt->opt.name != NULL)
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] = NULL;
576 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
578 sim_cpu *cpu = STATE_CPU (sd, c);
579 for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next)
580 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
582 #if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
583 on the need for dup_arg_p checking. Maybe in the future it'll be
584 needed so this is just commented out, and not deleted. */
585 if (dup_arg_p (opt->opt.name))
588 /* Don't allow short versions of cpu specific options for now. */
589 if (opt->shortopt != 0)
591 sim_io_eprintf (sd, "internal error, short cpu specific option");
592 result = SIM_RC_FAIL;
595 if (opt->opt.name != NULL)
599 /* Prepend --<cpuname>- to the option. */
600 if (asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name) < 0)
602 sim_io_eprintf (sd, "internal error, out of memory");
603 result = SIM_RC_FAIL;
607 /* Dynamically assign `val' numbers for long options. */
609 handlers[lp->val] = opt->handler;
610 orig_val[lp->val] = opt->opt.val;
611 opt_cpu[lp->val] = cpu;
617 /* Terminate the short and long option lists. */
621 /* Ensure getopt is initialized. */
628 optc = getopt_long (argc, argv, short_options, long_options, &longind);
631 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
632 STATE_PROG_ARGV (sd) = dupargv (argv + optind);
637 result = SIM_RC_FAIL;
641 if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
643 result = SIM_RC_FAIL;
649 free (short_options);
656 /* Utility of sim_print_help to print a list of option tables. */
659 print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command)
663 for ( ; ol != NULL; ol = ol->next)
664 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
666 const int indent = 30;
670 if (dup_arg_p (opt->opt.name))
673 if (opt->doc == NULL)
676 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
679 sim_io_printf (sd, " ");
684 /* list any short options (aliases) for the current OPT */
690 if (o->shortopt != '\0')
692 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
693 len += (comma ? 2 : 0) + 2;
696 if (o->opt.has_arg == optional_argument)
698 sim_io_printf (sd, "[%s]", o->arg);
699 len += 1 + strlen (o->arg) + 1;
703 sim_io_printf (sd, " %s", o->arg);
704 len += 1 + strlen (o->arg);
711 while (OPTION_VALID_P (o) && o->doc == NULL);
714 /* list any long options (aliases) for the current OPT */
719 const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL;
720 if (o->doc_name != NULL)
726 sim_io_printf (sd, "%s%s%s%s%s",
728 is_command ? "" : "--",
729 cpu ? cpu_prefix : "",
732 len += ((comma ? 2 : 0)
733 + (is_command ? 0 : 2)
737 if (o->opt.has_arg == optional_argument)
739 sim_io_printf (sd, "[=%s]", o->arg);
740 len += 2 + strlen (o->arg) + 1;
744 sim_io_printf (sd, " %s", o->arg);
745 len += 1 + strlen (o->arg);
752 while (OPTION_VALID_P (o) && o->doc == NULL);
756 sim_io_printf (sd, "\n%*s", indent, "");
759 sim_io_printf (sd, "%*s", indent - len, "");
761 /* print the description, word wrap long lines */
763 const char *chp = opt->doc;
764 unsigned doc_width = 80 - indent;
765 while (strlen (chp) >= doc_width) /* some slack */
767 const char *end = chp + doc_width - 1;
768 while (end > chp && !isspace (*end))
771 end = chp + doc_width - 1;
772 /* The cast should be ok - its distances between to
773 points in a string. */
774 sim_io_printf (sd, "%.*s\n%*s", (int) (end - chp), chp, indent,
777 while (isspace (*chp) && *chp != '\0')
780 sim_io_printf (sd, "%s\n", chp);
785 /* Print help messages for the options. */
788 sim_print_help (SIM_DESC sd, int is_command)
790 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
791 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
794 /* Initialize duplicate argument checker. */
795 (void) dup_arg_p (NULL);
797 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
798 sim_io_printf (sd, "Options:\n");
800 sim_io_printf (sd, "Commands:\n");
802 print_help (sd, NULL, STATE_OPTIONS (sd), is_command);
803 sim_io_printf (sd, "\n");
805 /* Print cpu-specific options. */
809 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
811 sim_cpu *cpu = STATE_CPU (sd, i);
812 if (CPU_OPTIONS (cpu) == NULL)
814 sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu));
815 print_help (sd, cpu, CPU_OPTIONS (cpu), is_command);
816 sim_io_printf (sd, "\n");
820 sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
821 STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
822 sim_io_printf (sd, " may not be applicable\n");
824 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
826 sim_io_printf (sd, "\n");
827 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
828 sim_io_printf (sd, " Note: Very few simulators support this.\n");
832 /* Utility of sim_args_command to find the closest match for a command.
833 Commands that have "-" in them can be specified as separate words.
834 e.g. sim memory-region 0x800000,0x4000
835 or sim memory region 0x800000,0x4000
836 If CPU is non-null, use its option table list, otherwise use the main one.
837 *PARGI is where to start looking in ARGV. It is updated to point past
840 static const OPTION *
841 find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi)
843 const struct option_list *ol;
845 /* most recent option match */
846 const OPTION *matching_opt = NULL;
847 int matching_argi = -1;
850 ol = CPU_OPTIONS (cpu);
852 ol = STATE_OPTIONS (sd);
854 /* Skip passed elements specified by *PARGI. */
857 for ( ; ol != NULL; ol = ol->next)
858 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
861 const char *name = opt->opt.name;
864 while (argv [argi] != NULL
865 && strncmp (name, argv [argi], strlen (argv [argi])) == 0)
867 name = &name [strlen (argv[argi])];
870 /* leading match ...<a-b-c>-d-e-f - continue search */
871 name ++; /* skip `-' */
875 else if (name [0] == '\0')
877 /* exact match ...<a-b-c-d-e-f> - better than before? */
878 if (argi > matching_argi)
880 matching_argi = argi;
890 *pargi = matching_argi;
895 complete_option_list (char **ret, size_t *cnt, const struct option_list *ol,
896 const char *text, const char *word)
898 const OPTION *opt = NULL;
900 size_t len = strlen (word);
902 for ( ; ol != NULL; ol = ol->next)
903 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
905 const char *name = opt->opt.name;
907 /* A long option to match against? */
911 /* Does this option actually match? */
912 if (strncmp (name, word, len))
915 ret = xrealloc (ret, ++*cnt * sizeof (ret[0]));
916 ret[*cnt - 2] = xstrdup (name);
922 /* All leading text is stored in @text, while the current word being
923 completed is stored in @word. Trailing text of @word is not. */
926 sim_complete_command (SIM_DESC sd, const char *text, const char *word)
932 /* Only complete first word for now. */
936 cpu = STATE_CPU (sd, 0);
938 ret = complete_option_list (ret, &cnt, CPU_OPTIONS (cpu), text, word);
939 ret = complete_option_list (ret, &cnt, STATE_OPTIONS (sd), text, word);
947 sim_args_command (SIM_DESC sd, const char *cmd)
949 /* something to do? */
951 return SIM_RC_OK; /* FIXME - perhaps help would be better */
955 /* user specified -<opt> ... form? */
956 char **argv = buildargv (cmd);
957 SIM_RC rc = sim_parse_args (sd, argv);
963 char **argv = buildargv (cmd);
964 const OPTION *matching_opt = NULL;
968 if (argv [0] == NULL)
971 return SIM_RC_OK; /* FIXME - perhaps help would be better */
974 /* First check for a cpu selector. */
976 char *cpu_name = xstrdup (argv[0]);
977 char *hyphen = strchr (cpu_name, '-');
980 cpu = sim_cpu_lookup (sd, cpu_name);
983 /* If <cpuname>-<command>, point argv[0] at <command>. */
987 argv[0] += hyphen - cpu_name + 1;
991 matching_opt = find_match (sd, cpu, argv, &matching_argi);
992 /* If hyphen found restore argv[0]. */
994 argv[0] -= hyphen - cpu_name + 1;
999 /* If that failed, try the main table. */
1000 if (matching_opt == NULL)
1003 matching_opt = find_match (sd, NULL, argv, &matching_argi);
1006 if (matching_opt != NULL)
1008 switch (matching_opt->opt.has_arg)
1011 if (argv [matching_argi + 1] == NULL)
1012 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1013 NULL, 1/*is_command*/);
1015 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
1016 matching_opt->opt.name);
1018 case optional_argument:
1019 if (argv [matching_argi + 1] == NULL)
1020 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1021 NULL, 1/*is_command*/);
1022 else if (argv [matching_argi + 2] == NULL)
1023 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1024 argv [matching_argi + 1], 1/*is_command*/);
1026 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
1027 matching_opt->opt.name);
1029 case required_argument:
1030 if (argv [matching_argi + 1] == NULL)
1031 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
1032 matching_opt->opt.name);
1033 else if (argv [matching_argi + 2] == NULL)
1034 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1035 argv [matching_argi + 1], 1/*is_command*/);
1037 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
1038 matching_opt->opt.name);
1047 /* didn't find anything that remotly matched */