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"
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 (sd, table)
48 struct option_list *ol = ((struct option_list *)
49 xmalloc (sizeof (struct option_list)));
51 /* Note: The list is constructed in the reverse order we're called so
52 later calls will override earlier ones (in case that ever happens).
53 This is the intended behaviour. */
54 ol->next = STATE_OPTIONS (sd);
56 STATE_OPTIONS (sd) = ol;
61 /* Standard option table.
62 Modules may specify additional ones.
63 The caller of sim_parse_args may also specify additional options
64 by calling sim_add_option_table first. */
66 static DECLARE_OPTION_HANDLER (standard_option_handler);
68 /* FIXME: We shouldn't print in --help output options that aren't usable.
69 Some fine tuning will be necessary. One can either move less general
70 options to another table or use a HAVE_FOO macro to ifdef out unavailable
73 /* ??? One might want to conditionally compile out the entries that
74 aren't enabled. There's a distinction, however, between options a
75 simulator can't support and options that haven't been configured in.
76 Certainly options a simulator can't support shouldn't appear in the
77 output of --help. Whether the same thing applies to options that haven't
78 been configured in or not isn't something I can get worked up over.
79 [Note that conditionally compiling them out might simply involve moving
80 the option to another table.]
81 If you decide to conditionally compile them out as well, delete this
82 comment and add a comment saying that that is the rule. */
84 #define OPTION_DEBUG_INSN (OPTION_START + 0)
85 #define OPTION_DEBUG_FILE (OPTION_START + 1)
86 #define OPTION_DO_COMMAND (OPTION_START + 2)
88 static const OPTION standard_options[] =
90 { {"verbose", no_argument, NULL, 'v'},
91 'v', NULL, "Verbose output",
92 standard_option_handler },
94 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
95 { {"endian", required_argument, NULL, 'E'},
96 'E', "big|little", "Set endianness",
97 standard_option_handler },
100 { {"debug", no_argument, NULL, 'D'},
101 'D', NULL, "Print debugging messages",
102 standard_option_handler },
103 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
104 '\0', NULL, "Print instruction debugging messages",
105 standard_option_handler },
106 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
107 '\0', "FILE NAME", "Specify debugging output file",
108 standard_option_handler },
110 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
111 { {"h8300h", no_argument, NULL, 'h'},
112 'h', NULL, "Indicate the CPU is h8/300h or h8/300s",
113 standard_option_handler },
116 #ifdef SIM_HAVE_FLATMEM
117 { {"mem-size", required_argument, NULL, 'm'},
118 'm', "MEMORY SIZE", "Specify memory size",
119 standard_option_handler },
122 { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
123 '\0', "COMMAND", ""/*undocumented*/,
124 standard_option_handler },
126 { {"help", no_argument, NULL, 'H'},
127 'H', NULL, "Print help information",
128 standard_option_handler },
130 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
134 standard_option_handler (sd, opt, arg, is_command)
145 STATE_VERBOSE_P (sd) = 1;
148 #ifdef SIM_HAVE_BIENDIAN
150 if (strcmp (arg, "big") == 0)
152 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
154 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
157 /* FIXME:wip: Need to set something in STATE_CONFIG. */
158 current_target_byte_order = BIG_ENDIAN;
160 else if (strcmp (arg, "little") == 0)
162 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
164 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
167 /* FIXME:wip: Need to set something in STATE_CONFIG. */
168 current_target_byte_order = LITTLE_ENDIAN;
172 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
180 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
183 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
184 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
185 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
189 case OPTION_DEBUG_INSN :
191 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
194 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
195 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
199 case OPTION_DEBUG_FILE :
201 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
204 FILE *f = fopen (arg, "w");
208 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
211 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
212 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
216 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
222 #ifdef SIM_HAVE_FLATMEM
225 unsigned long ul = strtol (arg, NULL, 0);
226 /* 16384: some minimal amount */
227 if (! isdigit (arg[0]) || ul < 16384)
229 sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
232 STATE_MEM_SIZE (sd) = ul;
237 case OPTION_DO_COMMAND:
238 sim_do_command (sd, arg);
243 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
245 /* FIXME: 'twould be nice to do something similar if gdb. */
252 /* Add the standard option list to the simulator. */
255 standard_install (SIM_DESC sd)
257 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
258 if (sim_add_option_table (sd, standard_options) != SIM_RC_OK)
263 /* Return non-zero if arg is a duplicate argument.
264 If ARG is NULL, initialize. */
266 #define ARG_HASH_SIZE 97
267 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
274 static char **arg_table = NULL;
278 if (arg_table == NULL)
279 arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
280 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
284 hash = ARG_HASH (arg);
285 while (arg_table[hash] != NULL)
287 if (strcmp (arg, arg_table[hash]) == 0)
289 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
290 don't check if the table is full. */
291 if (++hash == ARG_HASH_SIZE)
294 arg_table[hash] = arg;
298 /* Called by sim_open to parse the arguments. */
301 sim_parse_args (sd, argv)
305 int i, argc, num_opts;
306 char *p, *short_options;
307 /* The `val' option struct entry is dynamically assigned for options that
308 only come in the long form. ORIG_VAL is used to get the original value
310 unsigned char *orig_val;
311 struct option *lp, *long_options;
312 const struct option_list *ol;
314 OPTION_HANDLER **handlers;
316 /* Count the number of arguments. */
317 for (argc = 0; argv[argc] != NULL; ++argc)
320 /* Count the number of options. */
322 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
323 for (opt = ol->options; opt->opt.name != NULL; ++opt)
326 /* Initialize duplicate argument checker. */
327 (void) dup_arg_p (NULL);
329 /* Build the option table for getopt. */
330 long_options = (struct option *) alloca ((num_opts + 1) * sizeof (struct option));
332 short_options = (char *) alloca (num_opts * 3 + 1);
334 #if 0 /* ??? necessary anymore? */
335 /* Set '+' as first char so argument permutation isn't done. This is done
336 to workaround a problem with invoking getopt_long in run.c.: optind gets
337 decremented when the program name is reached. */
340 handlers = (OPTION_HANDLER **) alloca (256 * sizeof (OPTION_HANDLER *));
341 memset (handlers, 0, 256 * sizeof (OPTION_HANDLER *));
342 orig_val = (unsigned char *) alloca (256);
343 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
344 for (opt = ol->options; opt->opt.name != NULL; ++opt)
346 if (dup_arg_p (opt->opt.name))
348 if (opt->shortopt != 0)
350 *p++ = opt->shortopt;
351 if (opt->opt.has_arg == required_argument)
353 else if (opt->opt.has_arg == optional_argument)
354 { *p++ = ':'; *p++ = ':'; }
357 /* Dynamically assign `val' numbers for long options that don't have
358 a short option equivalent. */
359 if (OPTION_LONG_ONLY_P (opt->opt.val))
361 handlers[(unsigned char) lp->val] = opt->handler;
362 orig_val[(unsigned char) lp->val] = opt->opt.val;
368 /* Ensure getopt is initialized. */
374 optc = getopt_long (argc, argv, short_options, long_options, &longind);
377 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
378 STATE_PROG_ARGV (sd) = argv + optind;
384 if ((*handlers[optc]) (sd, orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
391 /* Print help messages for the options. */
397 const struct option_list *ol;
400 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
401 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
404 /* Initialize duplicate argument checker. */
405 (void) dup_arg_p (NULL);
407 sim_io_printf (sd, "Options:\n");
408 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
409 for (opt = ol->options; opt->opt.name != NULL; ++opt)
414 if (dup_arg_p (opt->opt.name))
417 if (opt->doc == NULL)
420 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
423 sim_io_printf (sd, " ");
431 if (o->shortopt != '\0')
433 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
434 len += (comma ? 2 : 0) + 2;
437 if (o->opt.has_arg == optional_argument)
439 sim_io_printf (sd, "[%s]", o->arg);
440 len += 1 + strlen (o->arg) + 1;
444 sim_io_printf (sd, " %s", o->arg);
445 len += 1 + strlen (o->arg);
452 while (o->opt.name != NULL && o->doc == NULL);
458 if (o->doc_name != NULL)
464 sim_io_printf (sd, "%s--%s",
467 len += ((comma ? 2 : 0)
472 if (o->opt.has_arg == optional_argument)
474 sim_io_printf (sd, " [%s]", o->arg);
475 len += 2 + strlen (o->arg) + 1;
479 sim_io_printf (sd, " %s", o->arg);
480 len += 1 + strlen (o->arg);
487 while (o->opt.name != NULL && o->doc == NULL);
491 sim_io_printf (sd, "\n");
495 for (; len < 30; len++)
496 sim_io_printf (sd, " ");
498 sim_io_printf (sd, "%s\n", opt->doc);
501 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
503 sim_io_printf (sd, "\n");
504 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
505 sim_io_printf (sd, " Note: Very few simulators support this.\n");
513 sim_args_command (sd, cmd)
517 /* something to do? */
519 return SIM_RC_OK; /* FIXME - perhaphs help would be better */
523 /* user specified -<opt> ... form? */
524 char **argv = buildargv (cmd);
525 SIM_RC rc = sim_parse_args (sd, argv);
531 /* user specified <opt> form? */
532 const struct option_list *ol;
534 char **argv = buildargv (cmd);
535 /* most recent option match */
536 const OPTION *matching_opt = NULL;
537 int matching_argi = -1;
538 if (argv [0] != NULL)
539 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
540 for (opt = ol->options; opt->opt.name != NULL; ++opt)
543 const char *name = opt->opt.name;
544 while (strncmp (name, argv [argi], strlen (argv [argi])) == 0)
546 name = &name [strlen (argv[argi])];
549 /* leading match ...<a-b-c>-d-e-f - continue search */
550 name ++; /* skip `-' */
554 else if (name [0] == '\0')
556 /* exact match ...<a-b-c-d-e-f> - better than before? */
557 if (argi > matching_argi)
559 matching_argi = argi;
568 if (matching_opt != NULL)
570 switch (matching_opt->opt.has_arg)
573 if (argv [matching_argi + 1] == NULL)
574 matching_opt->handler (sd, matching_opt->opt.val,
575 NULL, 1/*is_command*/);
577 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
578 matching_opt->opt.name);
580 case optional_argument:
581 if (argv [matching_argi + 1] == NULL)
582 matching_opt->handler (sd, matching_opt->opt.val,
583 NULL, 1/*is_command*/);
584 else if (argv [matching_argi + 2] == NULL)
585 matching_opt->handler (sd, matching_opt->opt.val,
586 argv [matching_argi + 1], 1/*is_command*/);
588 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
589 matching_opt->opt.name);
591 case required_argument:
592 if (argv [matching_argi + 1] == NULL)
593 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
594 matching_opt->opt.name);
595 else if (argv [matching_argi + 2] == NULL)
596 matching_opt->handler (sd, matching_opt->opt.val,
597 argv [matching_argi + 1], 1/*is_command*/);
599 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
600 matching_opt->opt.name);
606 /* didn't find anything that remotly matched */