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. */
29 #include "libiberty.h"
30 #include "../libiberty/alloca-conf.h"
31 #include "sim-options.h"
34 /* Add a set of options to the simulator.
35 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
36 This is intended to be called by modules in their `install' handler. */
39 sim_add_option_table (sd
, table
)
43 struct option_list
*ol
= ((struct option_list
*)
44 xmalloc (sizeof (struct option_list
)));
46 /* Note: The list is constructed in the reverse order we're called so
47 later calls will override earlier ones (in case that ever happens).
48 This is the intended behaviour. */
49 ol
->next
= STATE_OPTIONS (sd
);
51 STATE_OPTIONS (sd
) = ol
;
56 /* Standard option table.
57 Modules may specify additional ones.
58 The caller of sim_parse_args may also specify additional options
59 by calling sim_add_option_table first. */
61 static DECLARE_OPTION_HANDLER (standard_option_handler
);
63 /* FIXME: We shouldn't print in --help output options that aren't usable.
64 Some fine tuning will be necessary. One can either move less general
65 options to another table or use a HAVE_FOO macro to ifdef out unavailable
68 /* ??? One might want to conditionally compile out the entries that
69 aren't enabled. There's a distinction, however, between options a
70 simulator can't support and options that haven't been configured in.
71 Certainly options a simulator can't support shouldn't appear in the
72 output of --help. Whether the same thing applies to options that haven't
73 been configured in or not isn't something I can get worked up over.
74 [Note that conditionally compiling them out might simply involve moving
75 the option to another table.]
76 If you decide to conditionally compile them out as well, delete this
77 comment and add a comment saying that that is the rule. */
79 #define OPTION_DEBUG_INSN (OPTION_START + 0)
80 #define OPTION_DEBUG_FILE (OPTION_START + 1)
81 #define OPTION_DO_COMMAND (OPTION_START + 2)
83 static const OPTION standard_options
[] =
85 { {"verbose", no_argument
, NULL
, 'v'},
86 'v', NULL
, "Verbose output",
87 standard_option_handler
},
89 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
90 { {"endian", required_argument
, NULL
, 'E'},
91 'E', "big|little", "Set endianness",
92 standard_option_handler
},
95 { {"debug", no_argument
, NULL
, 'D'},
96 'D', NULL
, "Print debugging messages",
97 standard_option_handler
},
98 { {"debug-insn", no_argument
, NULL
, OPTION_DEBUG_INSN
},
99 '\0', NULL
, "Print instruction debugging messages",
100 standard_option_handler
},
101 { {"debug-file", required_argument
, NULL
, OPTION_DEBUG_FILE
},
102 '\0', "FILE NAME", "Specify debugging output file",
103 standard_option_handler
},
105 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
106 { {"h8300h", no_argument
, NULL
, 'h'},
107 'h', NULL
, "Indicate the CPU is h8/300h or h8/300s",
108 standard_option_handler
},
111 #ifdef SIM_HAVE_FLATMEM
112 { {"mem-size", required_argument
, NULL
, 'm'},
113 'm', "MEMORY SIZE", "Specify memory size",
114 standard_option_handler
},
117 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
118 '\0', "COMMAND", "Perform a builtin command",
119 standard_option_handler
},
121 { {"help", no_argument
, NULL
, 'H'},
122 'H', NULL
, "Print help information",
123 standard_option_handler
},
125 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
129 standard_option_handler (sd
, opt
, arg
)
139 STATE_VERBOSE_P (sd
) = 1;
142 #ifdef SIM_HAVE_BIENDIAN
144 if (strcmp (arg
, "big") == 0)
146 if (WITH_TARGET_BYTE_ORDER
== LITTLE_ENDIAN
)
148 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
151 /* FIXME:wip: Need to set something in STATE_CONFIG. */
153 else if (strcmp (arg
, "little") == 0)
155 if (WITH_TARGET_BYTE_ORDER
== BIG_ENDIAN
)
157 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
160 /* FIXME:wip: Need to set something in STATE_CONFIG. */
164 sim_io_eprintf (sd
, "Invalid endian specification `%s'\n", arg
);
172 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
175 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
176 for (i
= 0; i
< MAX_DEBUG_VALUES
; ++i
)
177 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
181 case OPTION_DEBUG_INSN
:
183 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
186 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
187 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
191 case OPTION_DEBUG_FILE
:
193 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
196 FILE *f
= fopen (arg
, "w");
200 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
203 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
204 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
208 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
214 #ifdef SIM_HAVE_FLATMEM
217 unsigned long ul
= strtol (arg
, NULL
, 0);
218 /* 16384: some minimal amount */
219 if (! isdigit (arg
[0]) || ul
< 16384)
221 sim_io_eprintf (sd
, "Invalid memory size `%s'", arg
);
224 STATE_MEM_SIZE (sd
) = ul
;
229 case OPTION_DO_COMMAND
:
230 sim_do_command (sd
, arg
);
235 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
237 /* FIXME: 'twould be nice to do something similar if gdb. */
244 /* Add the standard option list to the simulator. */
247 standard_install (SIM_DESC sd
)
249 if (sim_add_option_table (sd
, standard_options
) != SIM_RC_OK
)
254 /* Return non-zero if arg is a duplicate argument.
255 If ARG is NULL, initialize. */
257 #define ARG_HASH_SIZE 97
258 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
265 static char **arg_table
= NULL
;
269 if (arg_table
== NULL
)
270 arg_table
= (char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
271 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
275 hash
= ARG_HASH (arg
);
276 while (arg_table
[hash
] != NULL
)
278 if (strcmp (arg
, arg_table
[hash
]) == 0)
280 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
281 don't check if the table is full. */
282 if (++hash
== ARG_HASH_SIZE
)
285 arg_table
[hash
] = arg
;
289 /* Called by sim_open to parse the arguments. */
292 sim_parse_args (sd
, argv
)
296 int i
, argc
, num_opts
;
297 char *p
, *short_options
;
298 /* The `val' option struct entry is dynamically assigned for options that
299 only come in the long form. ORIG_VAL is used to get the original value
301 unsigned char *orig_val
;
302 struct option
*lp
, *long_options
;
303 const struct option_list
*ol
;
305 OPTION_HANDLER
**handlers
;
307 /* Count the number of arguments. */
308 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
311 /* Count the number of options. */
313 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
314 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
317 /* Initialize duplicate argument checker. */
318 (void) dup_arg_p (NULL
);
320 /* Build the option table for getopt. */
321 long_options
= (struct option
*) alloca ((num_opts
+ 1) * sizeof (struct option
));
323 short_options
= (char *) alloca (num_opts
* 3 + 1);
325 #if 0 /* ??? necessary anymore? */
326 /* Set '+' as first char so argument permutation isn't done. This is done
327 to workaround a problem with invoking getopt_long in run.c.: optind gets
328 decremented when the program name is reached. */
331 handlers
= (OPTION_HANDLER
**) alloca (256 * sizeof (OPTION_HANDLER
*));
332 memset (handlers
, 0, 256 * sizeof (OPTION_HANDLER
*));
333 orig_val
= (unsigned char *) alloca (256);
334 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
335 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
337 if (dup_arg_p (opt
->opt
.name
))
339 if (opt
->shortopt
!= 0)
341 *p
++ = opt
->shortopt
;
342 if (opt
->opt
.has_arg
== required_argument
)
344 else if (opt
->opt
.has_arg
== optional_argument
)
345 { *p
++ = ':'; *p
++ = ':'; }
348 /* Dynamically assign `val' numbers for long options that don't have
349 a short option equivalent. */
350 if (OPTION_LONG_ONLY_P (opt
->opt
.val
))
352 handlers
[(unsigned char) lp
->val
] = opt
->handler
;
353 orig_val
[(unsigned char) lp
->val
] = opt
->opt
.val
;
359 /* Ensure getopt is initialized. */
365 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
368 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
369 STATE_PROG_ARGV (sd
) = argv
+ optind
;
375 if ((*handlers
[optc
]) (sd
, orig_val
[optc
], optarg
) == SIM_RC_FAIL
)
382 /* Print help messages for the options. */
388 const struct option_list
*ol
;
391 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
392 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
395 /* Initialize duplicate argument checker. */
396 (void) dup_arg_p (NULL
);
398 sim_io_printf (sd
, "Options:\n");
399 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
400 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
405 if (dup_arg_p (opt
->opt
.name
))
408 if (opt
->doc
== NULL
)
411 sim_io_printf (sd
, " ");
419 if (o
->shortopt
!= '\0')
421 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
422 len
+= (comma
? 2 : 0) + 2;
425 if (o
->opt
.has_arg
== optional_argument
)
427 sim_io_printf (sd
, "[%s]", o
->arg
);
428 len
+= 1 + strlen (o
->arg
) + 1;
432 sim_io_printf (sd
, " %s", o
->arg
);
433 len
+= 1 + strlen (o
->arg
);
440 while (o
->opt
.name
!= NULL
&& o
->doc
== NULL
);
445 if (o
->opt
.name
!= NULL
)
447 sim_io_printf (sd
, "%s--%s",
450 len
+= ((comma
? 2 : 0)
452 + strlen (o
->opt
.name
));
455 if (o
->opt
.has_arg
== optional_argument
)
457 sim_io_printf (sd
, " [%s]", o
->arg
);
458 len
+= 2 + strlen (o
->arg
) + 1;
462 sim_io_printf (sd
, " %s", o
->arg
);
463 len
+= 1 + strlen (o
->arg
);
470 while (o
->opt
.name
!= NULL
&& o
->doc
== NULL
);
474 sim_io_printf (sd
, "\n");
478 for (; len
< 30; len
++)
479 sim_io_printf (sd
, " ");
481 sim_io_printf (sd
, "%s\n", opt
->doc
);
484 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
486 sim_io_printf (sd
, "\n");
487 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
488 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
496 sim_args_command (sd
, cmd
)
500 /* something to do? */
502 return SIM_RC_OK
; /* FIXME - perhaphs help would be better */
506 /* user specified -<opt> ... form? */
507 char **argv
= buildargv (cmd
);
508 SIM_RC rc
= sim_parse_args (sd
, argv
);
514 /* user specified <opt> form? */
515 const struct option_list
*ol
;
517 char **argv
= buildargv (cmd
);
518 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
519 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
521 if (strcmp (argv
[0], opt
->opt
.name
) == 0)
523 switch (opt
->opt
.has_arg
)
527 opt
->handler (sd
, opt
->opt
.val
, NULL
);
529 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n", opt
->opt
.name
);
531 case optional_argument
:
533 opt
->handler (sd
, opt
->opt
.val
, NULL
);
534 else if (argv
[2] == NULL
)
535 opt
->handler (sd
, opt
->opt
.val
, argv
[1]);
537 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n", opt
->opt
.name
);
539 case required_argument
:
541 sim_io_eprintf (sd
, "Command `%s' requires an argument\n", opt
->opt
.name
);
542 else if (argv
[2] == NULL
)
543 opt
->handler (sd
, opt
->opt
.val
, argv
[1]);
545 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n", opt
->opt
.name
);
552 /* didn't find anything that matched */
This page took 0.04169 seconds and 4 git commands to generate.