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)
91 #define OPTION_ARCHITECTURE_INFO (OPTION_START + 5)
93 static const OPTION standard_options
[] =
95 { {"verbose", no_argument
, NULL
, 'v'},
96 'v', NULL
, "Verbose output",
97 standard_option_handler
},
99 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
100 { {"endian", required_argument
, NULL
, 'E'},
101 'E', "big|little", "Set endianness",
102 standard_option_handler
},
105 { {"debug", no_argument
, NULL
, 'D'},
106 'D', NULL
, "Print debugging messages",
107 standard_option_handler
},
108 { {"debug-insn", no_argument
, NULL
, OPTION_DEBUG_INSN
},
109 '\0', NULL
, "Print instruction debugging messages",
110 standard_option_handler
},
111 { {"debug-file", required_argument
, NULL
, OPTION_DEBUG_FILE
},
112 '\0', "FILE NAME", "Specify debugging output file",
113 standard_option_handler
},
115 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
116 { {"h8300h", no_argument
, NULL
, 'h'},
117 'h', NULL
, "Indicate the CPU is h8/300h or h8/300s",
118 standard_option_handler
},
121 #ifdef SIM_HAVE_FLATMEM
122 { {"mem-size", required_argument
, NULL
, 'm'},
123 'm', "MEMORY SIZE", "Specify memory size",
124 standard_option_handler
},
127 { {"do-command", required_argument
, NULL
, OPTION_DO_COMMAND
},
128 '\0', "COMMAND", ""/*undocumented*/,
129 standard_option_handler
},
131 { {"help", no_argument
, NULL
, 'H'},
132 'H', NULL
, "Print help information",
133 standard_option_handler
},
135 { {"architecture", required_argument
, NULL
, OPTION_ARCHITECTURE
},
136 '\0', "MACHINE", "Specify the architecture to use",
137 standard_option_handler
},
138 { {"architecture-info", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
139 '\0', NULL
, "List supported architectures",
140 standard_option_handler
},
141 { {"info-architecture", no_argument
, NULL
, OPTION_ARCHITECTURE_INFO
},
143 standard_option_handler
},
145 { {"target", required_argument
, NULL
, OPTION_TARGET
},
146 '\0', "BFDNAME", "Specify the object-code format for the object files",
147 standard_option_handler
},
149 { {NULL
, no_argument
, NULL
, 0}, '\0', NULL
, NULL
, NULL
}
153 standard_option_handler (sd
, opt
, arg
, is_command
)
164 STATE_VERBOSE_P (sd
) = 1;
167 #ifdef SIM_HAVE_BIENDIAN
169 if (strcmp (arg
, "big") == 0)
171 if (WITH_TARGET_BYTE_ORDER
== LITTLE_ENDIAN
)
173 sim_io_eprintf (sd
, "Simulator compiled for little endian only.\n");
176 /* FIXME:wip: Need to set something in STATE_CONFIG. */
177 current_target_byte_order
= BIG_ENDIAN
;
179 else if (strcmp (arg
, "little") == 0)
181 if (WITH_TARGET_BYTE_ORDER
== BIG_ENDIAN
)
183 sim_io_eprintf (sd
, "Simulator compiled for big endian only.\n");
186 /* FIXME:wip: Need to set something in STATE_CONFIG. */
187 current_target_byte_order
= LITTLE_ENDIAN
;
191 sim_io_eprintf (sd
, "Invalid endian specification `%s'\n", arg
);
199 sim_io_eprintf (sd
, "Debugging not compiled in, `-D' ignored\n");
202 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
203 for (i
= 0; i
< MAX_DEBUG_VALUES
; ++i
)
204 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[i
] = 1;
208 case OPTION_DEBUG_INSN
:
210 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-insn' ignored\n");
213 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
214 CPU_DEBUG_FLAGS (STATE_CPU (sd
, n
))[DEBUG_INSN_IDX
] = 1;
218 case OPTION_DEBUG_FILE
:
220 sim_io_eprintf (sd
, "Debugging not compiled in, `--debug-file' ignored\n");
223 FILE *f
= fopen (arg
, "w");
227 sim_io_eprintf (sd
, "Unable to open debug output file `%s'\n", arg
);
230 for (n
= 0; n
< MAX_NR_PROCESSORS
; ++n
)
231 CPU_DEBUG_FILE (STATE_CPU (sd
, n
)) = f
;
235 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
241 #ifdef SIM_HAVE_FLATMEM
244 unsigned long ul
= strtol (arg
, NULL
, 0);
245 /* 16384: some minimal amount */
246 if (! isdigit (arg
[0]) || ul
< 16384)
248 sim_io_eprintf (sd
, "Invalid memory size `%s'", arg
);
251 STATE_MEM_SIZE (sd
) = ul
;
256 case OPTION_DO_COMMAND
:
257 sim_do_command (sd
, arg
);
260 case OPTION_ARCHITECTURE
:
262 const struct bfd_arch_info
*ap
= bfd_scan_arch (arg
);
265 sim_io_eprintf (sd
, "Architecture `%s' unknown\n", arg
);
268 STATE_ARCHITECTURE (sd
) = ap
;
272 case OPTION_ARCHITECTURE_INFO
:
274 const char **list
= bfd_arch_list();
278 sim_io_printf (sd
, "Valid architectures:");
279 for (lp
= list
; *lp
!= NULL
; lp
++)
280 sim_io_printf (sd
, " %s", *lp
);
281 sim_io_printf (sd
, "\n");
288 STATE_TARGET (sd
) = xstrdup (arg
);
293 sim_print_help (sd
, is_command
);
294 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
296 /* FIXME: 'twould be nice to do something similar if gdb. */
303 /* Add the standard option list to the simulator. */
306 standard_install (SIM_DESC sd
)
308 SIM_ASSERT (STATE_MAGIC (sd
) == SIM_MAGIC_NUMBER
);
309 if (sim_add_option_table (sd
, standard_options
) != SIM_RC_OK
)
314 /* Return non-zero if arg is a duplicate argument.
315 If ARG is NULL, initialize. */
317 #define ARG_HASH_SIZE 97
318 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
325 static char **arg_table
= NULL
;
329 if (arg_table
== NULL
)
330 arg_table
= (char **) xmalloc (ARG_HASH_SIZE
* sizeof (char *));
331 memset (arg_table
, 0, ARG_HASH_SIZE
* sizeof (char *));
335 hash
= ARG_HASH (arg
);
336 while (arg_table
[hash
] != NULL
)
338 if (strcmp (arg
, arg_table
[hash
]) == 0)
340 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
341 don't check if the table is full. */
342 if (++hash
== ARG_HASH_SIZE
)
345 arg_table
[hash
] = arg
;
349 /* Called by sim_open to parse the arguments. */
352 sim_parse_args (sd
, argv
)
356 int i
, argc
, num_opts
;
357 char *p
, *short_options
;
358 /* The `val' option struct entry is dynamically assigned for options that
359 only come in the long form. ORIG_VAL is used to get the original value
361 unsigned char *orig_val
;
362 struct option
*lp
, *long_options
;
363 const struct option_list
*ol
;
365 OPTION_HANDLER
**handlers
;
367 /* Count the number of arguments. */
368 for (argc
= 0; argv
[argc
] != NULL
; ++argc
)
371 /* Count the number of options. */
373 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
374 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
377 /* Initialize duplicate argument checker. */
378 (void) dup_arg_p (NULL
);
380 /* Build the option table for getopt. */
381 long_options
= (struct option
*) alloca ((num_opts
+ 1) * sizeof (struct option
));
383 short_options
= (char *) alloca (num_opts
* 3 + 1);
385 #if 0 /* ??? necessary anymore? */
386 /* Set '+' as first char so argument permutation isn't done. This is done
387 to workaround a problem with invoking getopt_long in run.c.: optind gets
388 decremented when the program name is reached. */
391 handlers
= (OPTION_HANDLER
**) alloca (256 * sizeof (OPTION_HANDLER
*));
392 memset (handlers
, 0, 256 * sizeof (OPTION_HANDLER
*));
393 orig_val
= (unsigned char *) alloca (256);
394 for (i
= OPTION_START
, ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
395 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
397 if (dup_arg_p (opt
->opt
.name
))
399 if (opt
->shortopt
!= 0)
401 *p
++ = opt
->shortopt
;
402 if (opt
->opt
.has_arg
== required_argument
)
404 else if (opt
->opt
.has_arg
== optional_argument
)
405 { *p
++ = ':'; *p
++ = ':'; }
408 /* Dynamically assign `val' numbers for long options that don't have
409 a short option equivalent. */
410 if (OPTION_LONG_ONLY_P (opt
->opt
.val
))
412 handlers
[(unsigned char) lp
->val
] = opt
->handler
;
413 orig_val
[(unsigned char) lp
->val
] = opt
->opt
.val
;
419 /* Ensure getopt is initialized. */
425 optc
= getopt_long (argc
, argv
, short_options
, long_options
, &longind
);
428 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
429 STATE_PROG_ARGV (sd
) = dupargv (argv
+ optind
);
435 if ((*handlers
[optc
]) (sd
, orig_val
[optc
], optarg
, 0/*!is_command*/) == SIM_RC_FAIL
)
442 /* Print help messages for the options. */
445 sim_print_help (sd
, is_command
)
449 const struct option_list
*ol
;
452 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
453 sim_io_printf (sd
, "Usage: %s [options] program [program args]\n",
456 /* Initialize duplicate argument checker. */
457 (void) dup_arg_p (NULL
);
459 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
460 sim_io_printf (sd
, "Options:\n");
462 sim_io_printf (sd
, "Commands:\n");
464 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
465 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
470 if (dup_arg_p (opt
->opt
.name
))
473 if (opt
->doc
== NULL
)
476 if (opt
->doc_name
!= NULL
&& opt
->doc_name
[0] == '\0')
479 sim_io_printf (sd
, " ");
489 if (o
->shortopt
!= '\0')
491 sim_io_printf (sd
, "%s-%c", comma
? ", " : "", o
->shortopt
);
492 len
+= (comma
? 2 : 0) + 2;
495 if (o
->opt
.has_arg
== optional_argument
)
497 sim_io_printf (sd
, "[%s]", o
->arg
);
498 len
+= 1 + strlen (o
->arg
) + 1;
502 sim_io_printf (sd
, " %s", o
->arg
);
503 len
+= 1 + strlen (o
->arg
);
510 while (o
->opt
.name
!= NULL
&& o
->doc
== NULL
);
517 if (o
->doc_name
!= NULL
)
523 sim_io_printf (sd
, "%s%s%s",
525 is_command
? "" : "--",
527 len
+= ((comma
? 2 : 0)
528 + (is_command
? 0 : 2)
532 if (o
->opt
.has_arg
== optional_argument
)
534 sim_io_printf (sd
, " [%s]", o
->arg
);
535 len
+= 2 + strlen (o
->arg
) + 1;
539 sim_io_printf (sd
, " %s", o
->arg
);
540 len
+= 1 + strlen (o
->arg
);
547 while (o
->opt
.name
!= NULL
&& o
->doc
== NULL
);
551 sim_io_printf (sd
, "\n");
555 for (; len
< 30; len
++)
556 sim_io_printf (sd
, " ");
558 sim_io_printf (sd
, "%s\n", opt
->doc
);
561 sim_io_printf (sd
, "\n");
562 sim_io_printf (sd
, "Note: Depending on the simulator configuration some %ss\n",
563 STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
? "option" : "command");
564 sim_io_printf (sd
, " may not be applicable\n");
566 if (STATE_OPEN_KIND (sd
) == SIM_OPEN_STANDALONE
)
568 sim_io_printf (sd
, "\n");
569 sim_io_printf (sd
, "program args Arguments to pass to simulated program.\n");
570 sim_io_printf (sd
, " Note: Very few simulators support this.\n");
578 sim_args_command (sd
, cmd
)
582 /* something to do? */
584 return SIM_RC_OK
; /* FIXME - perhaphs help would be better */
588 /* user specified -<opt> ... form? */
589 char **argv
= buildargv (cmd
);
590 SIM_RC rc
= sim_parse_args (sd
, argv
);
596 /* user specified <opt> form? */
597 const struct option_list
*ol
;
599 char **argv
= buildargv (cmd
);
600 /* most recent option match */
601 const OPTION
*matching_opt
= NULL
;
602 int matching_argi
= -1;
603 if (argv
[0] != NULL
)
604 for (ol
= STATE_OPTIONS (sd
); ol
!= NULL
; ol
= ol
->next
)
605 for (opt
= ol
->options
; opt
->opt
.name
!= NULL
; ++opt
)
608 const char *name
= opt
->opt
.name
;
609 while (strncmp (name
, argv
[argi
], strlen (argv
[argi
])) == 0)
611 name
= &name
[strlen (argv
[argi
])];
614 /* leading match ...<a-b-c>-d-e-f - continue search */
615 name
++; /* skip `-' */
619 else if (name
[0] == '\0')
621 /* exact match ...<a-b-c-d-e-f> - better than before? */
622 if (argi
> matching_argi
)
624 matching_argi
= argi
;
633 if (matching_opt
!= NULL
)
635 switch (matching_opt
->opt
.has_arg
)
638 if (argv
[matching_argi
+ 1] == NULL
)
639 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
640 NULL
, 1/*is_command*/);
642 sim_io_eprintf (sd
, "Command `%s' takes no arguments\n",
643 matching_opt
->opt
.name
);
645 case optional_argument
:
646 if (argv
[matching_argi
+ 1] == NULL
)
647 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
648 NULL
, 1/*is_command*/);
649 else if (argv
[matching_argi
+ 2] == NULL
)
650 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
651 argv
[matching_argi
+ 1], 1/*is_command*/);
653 sim_io_eprintf (sd
, "Command `%s' requires no more than one argument\n",
654 matching_opt
->opt
.name
);
656 case required_argument
:
657 if (argv
[matching_argi
+ 1] == NULL
)
658 sim_io_eprintf (sd
, "Command `%s' requires an argument\n",
659 matching_opt
->opt
.name
);
660 else if (argv
[matching_argi
+ 2] == NULL
)
661 matching_opt
->handler (sd
, matching_opt
->opt
.val
,
662 argv
[matching_argi
+ 1], 1/*is_command*/);
664 sim_io_eprintf (sd
, "Command `%s' requires only one argument\n",
665 matching_opt
->opt
.name
);
671 /* didn't find anything that remotly matched */