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
);
242 sim_print_help (sd
, is_command
);
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. */
394 sim_print_help (sd
, is_command
)
398 const struct option_list
*ol
;
401 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
402 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
405 /* Initialize duplicate argument checker. */
406 (void) dup_arg_p (NULL
);
408 sim_io_printf (sd
, "Options:\n");
409 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
410 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
415 if (dup_arg_p (opt
->opt
.name
))
418 if (opt
->doc
== NULL
)
421 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
424 sim_io_printf (sd
, " ");
434 if (o
->shortopt
!= '\0')
436 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
437 len
+= (comma
? 2 : 0) + 2;
440 if (o
->opt
.has_arg
== optional_argument
)
442 sim_io_printf (sd
, "[%s]", o
->arg
);
443 len
+= 1 + strlen (o
->arg
) + 1;
447 sim_io_printf (sd
, " %s", o
->arg
);
448 len
+= 1 + strlen (o
->arg
);
455 while (o
->opt
.name
!= NULL
&& o
->doc
== NULL
);
462 if (o
->doc_name
!= NULL
)
468 sim_io_printf (sd
, "%s%s%s",
470 is_command
? "" : "--",
472 len
+= ((comma
? 2 : 0)
473 + (is_command
? 0 : 2)
477 if (o
->opt
.has_arg
== optional_argument
)
479 sim_io_printf (sd
, " [%s]", o
->arg
);
480 len
+= 2 + strlen (o
->arg
) + 1;
484 sim_io_printf (sd
, " %s", o
->arg
);
485 len
+= 1 + strlen (o
->arg
);
492 while (o
->opt
.name
!= NULL
&& o
->doc
== NULL
);
496 sim_io_printf (sd
, "\n");
500 for (; len
< 30; len
++)
501 sim_io_printf (sd
, " ");
503 sim_io_printf (sd
, "%s\n", opt
->doc
);
506 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
508 sim_io_printf (sd
, "\n");
509 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
510 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
518 sim_args_command (sd
, cmd
)
522 /* something to do? */
524 return SIM_RC_OK
; /* FIXME - perhaphs help would be better */
528 /* user specified -<opt> ... form? */
529 char **argv
= buildargv (cmd
);
530 SIM_RC rc
= sim_parse_args (sd
, argv
);
536 /* user specified <opt> form? */
537 const struct option_list
*ol
;
539 char **argv
= buildargv (cmd
);
540 /* most recent option match */
541 const OPTION
*matching_opt
= NULL
;
542 int matching_argi
= -1;
543 if (argv
[0] != NULL
)
544 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
545 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
548 const char *name
= opt
->opt
.name
;
549 while (strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
551 name
= &name
[strlen (argv
[argi
])];
554 /* leading match ...<a-b-c>-d-e-f - continue search */
555 name
++; /* skip `-' */
559 else if (name
[0] == '\0')
561 /* exact match ...<a-b-c-d-e-f> - better than before? */
562 if (argi
> matching_argi
)
564 matching_argi
= argi
;
573 if (matching_opt
!= NULL
)
575 switch (matching_opt
->opt
.has_arg
)
578 if (argv
[matching_argi
+ 1] == NULL
)
579 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
580 NULL
, 1/*is_command*/);
582 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
583 matching_opt
->opt
.name
);
585 case optional_argument
:
586 if (argv
[matching_argi
+ 1] == NULL
)
587 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
588 NULL
, 1/*is_command*/);
589 else if (argv
[matching_argi
+ 2] == NULL
)
590 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
591 argv
[matching_argi
+ 1], 1/*is_command*/);
593 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
594 matching_opt
->opt
.name
);
596 case required_argument
:
597 if (argv
[matching_argi
+ 1] == NULL
)
598 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
599 matching_opt
->opt
.name
);
600 else if (argv
[matching_argi
+ 2] == NULL
)
601 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
602 argv
[matching_argi
+ 1], 1/*is_command*/);
604 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
605 matching_opt
->opt
.name
);
611 /* didn't find anything that remotly matched */