1 /* Simulator option handling.
2 Copyright (C) 1996, 1997 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 2, or (at your option)
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 along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 #include "libiberty.h"
34 #include "../libiberty/alloca-conf.h"
35 #include "sim-options.h"
37 #include "sim-assert.h"
41 /* Add a set of options to the simulator.
42 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
43 This is intended to be called by modules in their `install' handler. */
46 sim_add_option_table (sd, table)
50 struct option_list *ol = ((struct option_list *)
51 xmalloc (sizeof (struct option_list)));
53 /* Note: The list is constructed in the reverse order we're called so
54 later calls will override earlier ones (in case that ever happens).
55 This is the intended behaviour. */
56 ol->next = STATE_OPTIONS (sd);
58 STATE_OPTIONS (sd) = ol;
63 /* Standard option table.
64 Modules may specify additional ones.
65 The caller of sim_parse_args may also specify additional options
66 by calling sim_add_option_table first. */
68 static DECLARE_OPTION_HANDLER (standard_option_handler);
70 /* FIXME: We shouldn't print in --help output options that aren't usable.
71 Some fine tuning will be necessary. One can either move less general
72 options to another table or use a HAVE_FOO macro to ifdef out unavailable
75 /* ??? One might want to conditionally compile out the entries that
76 aren't enabled. There's a distinction, however, between options a
77 simulator can't support and options that haven't been configured in.
78 Certainly options a simulator can't support shouldn't appear in the
79 output of --help. Whether the same thing applies to options that haven't
80 been configured in or not isn't something I can get worked up over.
81 [Note that conditionally compiling them out might simply involve moving
82 the option to another table.]
83 If you decide to conditionally compile them out as well, delete this
84 comment and add a comment saying that that is the rule. */
86 #define OPTION_DEBUG_INSN (OPTION_START + 0)
87 #define OPTION_DEBUG_FILE (OPTION_START + 1)
88 #define OPTION_DO_COMMAND (OPTION_START + 2)
89 #define OPTION_ARCHITECTURE (OPTION_START + 3)
90 #define OPTION_TARGET (OPTION_START + 4)
92 static const OPTION standard_options[] =
94 { {"verbose", no_argument, NULL, 'v'},
95 'v', NULL, "Verbose output",
96 standard_option_handler },
98 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
99 { {"endian", required_argument, NULL, 'E'},
100 'E', "big|little", "Set endianness",
101 standard_option_handler },
104 { {"debug", no_argument, NULL, 'D'},
105 'D', NULL, "Print debugging messages",
106 standard_option_handler },
107 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
108 '\0', NULL, "Print instruction debugging messages",
109 standard_option_handler },
110 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
111 '\0', "FILE NAME", "Specify debugging output file",
112 standard_option_handler },
114 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
115 { {"h8300h", no_argument, NULL, 'h'},
116 'h', NULL, "Indicate the CPU is h8/300h or h8/300s",
117 standard_option_handler },
120 #ifdef SIM_HAVE_FLATMEM
121 { {"mem-size", required_argument, NULL, 'm'},
122 'm', "MEMORY SIZE", "Specify memory size",
123 standard_option_handler },
126 { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
127 '\0', "COMMAND", ""/*undocumented*/,
128 standard_option_handler },
130 { {"help", no_argument, NULL, 'H'},
131 'H', NULL, "Print help information",
132 standard_option_handler },
134 { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
135 '\0', "MACHINE", "Specify the architecture to use",
136 standard_option_handler },
138 { {"target", required_argument, NULL, OPTION_TARGET},
139 '\0', "BFDNAME", "Specify the object-code format for the object files",
140 standard_option_handler },
142 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
146 standard_option_handler (sd, opt, arg, is_command)
157 STATE_VERBOSE_P (sd) = 1;
160 #ifdef SIM_HAVE_BIENDIAN
162 if (strcmp (arg, "big") == 0)
164 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
166 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
169 /* FIXME:wip: Need to set something in STATE_CONFIG. */
170 current_target_byte_order = BIG_ENDIAN;
172 else if (strcmp (arg, "little") == 0)
174 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
176 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
179 /* FIXME:wip: Need to set something in STATE_CONFIG. */
180 current_target_byte_order = LITTLE_ENDIAN;
184 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
192 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
195 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
196 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
197 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
201 case OPTION_DEBUG_INSN :
203 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
206 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
207 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
211 case OPTION_DEBUG_FILE :
213 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
216 FILE *f = fopen (arg, "w");
220 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
223 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
224 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
228 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
234 #ifdef SIM_HAVE_FLATMEM
237 unsigned long ul = strtol (arg, NULL, 0);
238 /* 16384: some minimal amount */
239 if (! isdigit (arg[0]) || ul < 16384)
241 sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
244 STATE_MEM_SIZE (sd) = ul;
249 case OPTION_DO_COMMAND:
250 sim_do_command (sd, arg);
253 case OPTION_ARCHITECTURE:
255 const struct bfd_arch_info *ap = bfd_scan_arch (arg);
258 sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
261 STATE_ARCHITECTURE (sd) = ap;
267 STATE_TARGET (sd) = xstrdup (arg);
272 sim_print_help (sd, is_command);
273 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
275 /* FIXME: 'twould be nice to do something similar if gdb. */
282 /* Add the standard option list to the simulator. */
285 standard_install (SIM_DESC sd)
287 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
288 if (sim_add_option_table (sd, standard_options) != SIM_RC_OK)
293 /* Return non-zero if arg is a duplicate argument.
294 If ARG is NULL, initialize. */
296 #define ARG_HASH_SIZE 97
297 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
304 static char **arg_table = NULL;
308 if (arg_table == NULL)
309 arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
310 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
314 hash = ARG_HASH (arg);
315 while (arg_table[hash] != NULL)
317 if (strcmp (arg, arg_table[hash]) == 0)
319 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
320 don't check if the table is full. */
321 if (++hash == ARG_HASH_SIZE)
324 arg_table[hash] = arg;
328 /* Called by sim_open to parse the arguments. */
331 sim_parse_args (sd, argv)
335 int i, argc, num_opts;
336 char *p, *short_options;
337 /* The `val' option struct entry is dynamically assigned for options that
338 only come in the long form. ORIG_VAL is used to get the original value
340 unsigned char *orig_val;
341 struct option *lp, *long_options;
342 const struct option_list *ol;
344 OPTION_HANDLER **handlers;
346 /* Count the number of arguments. */
347 for (argc = 0; argv[argc] != NULL; ++argc)
350 /* Count the number of options. */
352 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
353 for (opt = ol->options; opt->opt.name != NULL; ++opt)
356 /* Initialize duplicate argument checker. */
357 (void) dup_arg_p (NULL);
359 /* Build the option table for getopt. */
360 long_options = (struct option *) alloca ((num_opts + 1) * sizeof (struct option));
362 short_options = (char *) alloca (num_opts * 3 + 1);
364 #if 0 /* ??? necessary anymore? */
365 /* Set '+' as first char so argument permutation isn't done. This is done
366 to workaround a problem with invoking getopt_long in run.c.: optind gets
367 decremented when the program name is reached. */
370 handlers = (OPTION_HANDLER **) alloca (256 * sizeof (OPTION_HANDLER *));
371 memset (handlers, 0, 256 * sizeof (OPTION_HANDLER *));
372 orig_val = (unsigned char *) alloca (256);
373 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
374 for (opt = ol->options; opt->opt.name != NULL; ++opt)
376 if (dup_arg_p (opt->opt.name))
378 if (opt->shortopt != 0)
380 *p++ = opt->shortopt;
381 if (opt->opt.has_arg == required_argument)
383 else if (opt->opt.has_arg == optional_argument)
384 { *p++ = ':'; *p++ = ':'; }
387 /* Dynamically assign `val' numbers for long options that don't have
388 a short option equivalent. */
389 if (OPTION_LONG_ONLY_P (opt->opt.val))
391 handlers[(unsigned char) lp->val] = opt->handler;
392 orig_val[(unsigned char) lp->val] = opt->opt.val;
398 /* Ensure getopt is initialized. */
404 optc = getopt_long (argc, argv, short_options, long_options, &longind);
407 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
408 STATE_PROG_ARGV (sd) = sim_copy_argv (argv + optind);
414 if ((*handlers[optc]) (sd, orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
421 /* Print help messages for the options. */
424 sim_print_help (sd, is_command)
428 const struct option_list *ol;
431 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
432 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
435 /* Initialize duplicate argument checker. */
436 (void) dup_arg_p (NULL);
438 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
439 sim_io_printf (sd, "Options:\n");
441 sim_io_printf (sd, "Commands:\n");
443 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
444 for (opt = ol->options; opt->opt.name != NULL; ++opt)
449 if (dup_arg_p (opt->opt.name))
452 if (opt->doc == NULL)
455 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
458 sim_io_printf (sd, " ");
468 if (o->shortopt != '\0')
470 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
471 len += (comma ? 2 : 0) + 2;
474 if (o->opt.has_arg == optional_argument)
476 sim_io_printf (sd, "[%s]", o->arg);
477 len += 1 + strlen (o->arg) + 1;
481 sim_io_printf (sd, " %s", o->arg);
482 len += 1 + strlen (o->arg);
489 while (o->opt.name != NULL && o->doc == NULL);
496 if (o->doc_name != NULL)
502 sim_io_printf (sd, "%s%s%s",
504 is_command ? "" : "--",
506 len += ((comma ? 2 : 0)
507 + (is_command ? 0 : 2)
511 if (o->opt.has_arg == optional_argument)
513 sim_io_printf (sd, " [%s]", o->arg);
514 len += 2 + strlen (o->arg) + 1;
518 sim_io_printf (sd, " %s", o->arg);
519 len += 1 + strlen (o->arg);
526 while (o->opt.name != NULL && o->doc == NULL);
530 sim_io_printf (sd, "\n");
534 for (; len < 30; len++)
535 sim_io_printf (sd, " ");
537 sim_io_printf (sd, "%s\n", opt->doc);
540 sim_io_printf (sd, "\n");
541 sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
542 STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
543 sim_io_printf (sd, " may not be applicable\n");
545 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
547 sim_io_printf (sd, "\n");
548 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
549 sim_io_printf (sd, " Note: Very few simulators support this.\n");
557 sim_args_command (sd, cmd)
561 /* something to do? */
563 return SIM_RC_OK; /* FIXME - perhaphs help would be better */
567 /* user specified -<opt> ... form? */
568 char **argv = buildargv (cmd);
569 SIM_RC rc = sim_parse_args (sd, argv);
575 /* user specified <opt> form? */
576 const struct option_list *ol;
578 char **argv = buildargv (cmd);
579 /* most recent option match */
580 const OPTION *matching_opt = NULL;
581 int matching_argi = -1;
582 if (argv [0] != NULL)
583 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
584 for (opt = ol->options; opt->opt.name != NULL; ++opt)
587 const char *name = opt->opt.name;
588 while (strncmp (name, argv [argi], strlen (argv [argi])) == 0)
590 name = &name [strlen (argv[argi])];
593 /* leading match ...<a-b-c>-d-e-f - continue search */
594 name ++; /* skip `-' */
598 else if (name [0] == '\0')
600 /* exact match ...<a-b-c-d-e-f> - better than before? */
601 if (argi > matching_argi)
603 matching_argi = argi;
612 if (matching_opt != NULL)
614 switch (matching_opt->opt.has_arg)
617 if (argv [matching_argi + 1] == NULL)
618 matching_opt->handler (sd, matching_opt->opt.val,
619 NULL, 1/*is_command*/);
621 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
622 matching_opt->opt.name);
624 case optional_argument:
625 if (argv [matching_argi + 1] == NULL)
626 matching_opt->handler (sd, matching_opt->opt.val,
627 NULL, 1/*is_command*/);
628 else if (argv [matching_argi + 2] == NULL)
629 matching_opt->handler (sd, matching_opt->opt.val,
630 argv [matching_argi + 1], 1/*is_command*/);
632 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
633 matching_opt->opt.name);
635 case required_argument:
636 if (argv [matching_argi + 1] == NULL)
637 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
638 matching_opt->opt.name);
639 else if (argv [matching_argi + 2] == NULL)
640 matching_opt->handler (sd, matching_opt->opt.val,
641 argv [matching_argi + 1], 1/*is_command*/);
643 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
644 matching_opt->opt.name);
650 /* didn't find anything that remotly matched */