]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - sim/common/sim-options.c
Copyright year update in most files of the GDB Project.
[thirdparty/binutils-gdb.git] / sim / common / sim-options.c
CommitLineData
c906108c 1/* Simulator option handling.
c5a57081
JB
2 Copyright (C) 1996-1997, 2004, 2007-2012 Free Software Foundation,
3 Inc.
c906108c
SS
4 Contributed by Cygnus Support.
5
6This file is part of GDB, the GNU debugger.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
4744ac1b
JB
10the Free Software Foundation; either version 3 of the License, or
11(at your option) any later version.
c906108c
SS
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
4744ac1b
JB
18You should have received a copy of the GNU General Public License
19along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c
SS
20
21#include "sim-main.h"
22#ifdef HAVE_STRING_H
23#include <string.h>
24#else
25#ifdef HAVE_STRINGS_H
26#include <strings.h>
27#endif
28#endif
29#ifdef HAVE_STDLIB_H
30#include <stdlib.h>
31#endif
32#include <ctype.h>
33#include "libiberty.h"
34#include "sim-options.h"
35#include "sim-io.h"
36#include "sim-assert.h"
37
38#include "bfd.h"
39
40/* Add a set of options to the simulator.
41 TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
42 This is intended to be called by modules in their `install' handler. */
43
44SIM_RC
45sim_add_option_table (SIM_DESC sd, sim_cpu *cpu, const OPTION *table)
46{
47 struct option_list *ol = ((struct option_list *)
48 xmalloc (sizeof (struct option_list)));
49
50 /* Note: The list is constructed in the reverse order we're called so
51 later calls will override earlier ones (in case that ever happens).
52 This is the intended behaviour. */
53
54 if (cpu)
55 {
56 ol->next = CPU_OPTIONS (cpu);
57 ol->options = table;
58 CPU_OPTIONS (cpu) = ol;
59 }
60 else
61 {
62 ol->next = STATE_OPTIONS (sd);
63 ol->options = table;
64 STATE_OPTIONS (sd) = ol;
65 }
66
67 return SIM_RC_OK;
68}
69
70/* Standard option table.
71 Modules may specify additional ones.
72 The caller of sim_parse_args may also specify additional options
73 by calling sim_add_option_table first. */
74
75static DECLARE_OPTION_HANDLER (standard_option_handler);
76
77/* FIXME: We shouldn't print in --help output options that aren't usable.
78 Some fine tuning will be necessary. One can either move less general
79 options to another table or use a HAVE_FOO macro to ifdef out unavailable
80 options. */
81
82/* ??? One might want to conditionally compile out the entries that
83 aren't enabled. There's a distinction, however, between options a
84 simulator can't support and options that haven't been configured in.
85 Certainly options a simulator can't support shouldn't appear in the
86 output of --help. Whether the same thing applies to options that haven't
87 been configured in or not isn't something I can get worked up over.
88 [Note that conditionally compiling them out might simply involve moving
89 the option to another table.]
90 If you decide to conditionally compile them out as well, delete this
91 comment and add a comment saying that that is the rule. */
92
93typedef enum {
94 OPTION_DEBUG_INSN = OPTION_START,
95 OPTION_DEBUG_FILE,
96 OPTION_DO_COMMAND,
97 OPTION_ARCHITECTURE,
98 OPTION_TARGET,
99 OPTION_ARCHITECTURE_INFO,
100 OPTION_ENVIRONMENT,
101 OPTION_ALIGNMENT,
102 OPTION_VERBOSE,
c906108c 103 OPTION_ENDIAN,
c906108c
SS
104 OPTION_DEBUG,
105#ifdef SIM_HAVE_FLATMEM
106 OPTION_MEM_SIZE,
107#endif
108 OPTION_HELP,
109#ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
828c9ae6 110 OPTION_H8300H,
a8cdafbd 111 OPTION_H8300S,
828c9ae6 112 OPTION_H8300SX,
c906108c 113#endif
43e526b9
JM
114 OPTION_LOAD_LMA,
115 OPTION_LOAD_VMA,
027e2a04 116 OPTION_SYSROOT
c906108c
SS
117} STANDARD_OPTIONS;
118
119static const OPTION standard_options[] =
120{
121 { {"verbose", no_argument, NULL, OPTION_VERBOSE},
122 'v', NULL, "Verbose output",
21cf617c 123 standard_option_handler, NULL },
c906108c 124
c906108c
SS
125 { {"endian", required_argument, NULL, OPTION_ENDIAN},
126 'E', "big|little", "Set endianness",
21cf617c 127 standard_option_handler, NULL },
c906108c
SS
128
129#ifdef SIM_HAVE_ENVIRONMENT
130 /* This option isn't supported unless all choices are supported in keeping
131 with the goal of not printing in --help output things the simulator can't
132 do [as opposed to things that just haven't been configured in]. */
133 { {"environment", required_argument, NULL, OPTION_ENVIRONMENT},
134 '\0', "user|virtual|operating", "Set running environment",
135 standard_option_handler },
136#endif
137
138 { {"alignment", required_argument, NULL, OPTION_ALIGNMENT},
139 '\0', "strict|nonstrict|forced", "Set memory access alignment",
140 standard_option_handler },
141
142 { {"debug", no_argument, NULL, OPTION_DEBUG},
143 'D', NULL, "Print debugging messages",
144 standard_option_handler },
145 { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
146 '\0', NULL, "Print instruction debugging messages",
147 standard_option_handler },
148 { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
149 '\0', "FILE NAME", "Specify debugging output file",
150 standard_option_handler },
151
152#ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir. */
828c9ae6 153 { {"h8300h", no_argument, NULL, OPTION_H8300H},
a69146da 154 'h', NULL, "Indicate the CPU is H8/300H",
a8cdafbd
AV
155 standard_option_handler },
156 { {"h8300s", no_argument, NULL, OPTION_H8300S},
a69146da 157 'S', NULL, "Indicate the CPU is H8S",
c906108c 158 standard_option_handler },
828c9ae6 159 { {"h8300sx", no_argument, NULL, OPTION_H8300SX},
a69146da 160 'x', NULL, "Indicate the CPU is H8SX",
828c9ae6 161 standard_option_handler },
c906108c
SS
162#endif
163
164#ifdef SIM_HAVE_FLATMEM
165 { {"mem-size", required_argument, NULL, OPTION_MEM_SIZE},
f40f1a01
NC
166 'm', "<size>[in bytes, Kb (k suffix), Mb (m suffix) or Gb (g suffix)]",
167 "Specify memory size", standard_option_handler },
c906108c
SS
168#endif
169
170 { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
171 '\0', "COMMAND", ""/*undocumented*/,
172 standard_option_handler },
173
174 { {"help", no_argument, NULL, OPTION_HELP},
175 'H', NULL, "Print help information",
176 standard_option_handler },
177
178 { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
179 '\0', "MACHINE", "Specify the architecture to use",
180 standard_option_handler },
181 { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
182 '\0', NULL, "List supported architectures",
183 standard_option_handler },
184 { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
185 '\0', NULL, NULL,
186 standard_option_handler },
187
188 { {"target", required_argument, NULL, OPTION_TARGET},
189 '\0', "BFDNAME", "Specify the object-code format for the object files",
190 standard_option_handler },
191
43e526b9
JM
192#ifdef SIM_HANDLES_LMA
193 { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA},
194 '\0', NULL,
195#if SIM_HANDLES_LMA
196 "Use VMA or LMA addresses when loading image (default LMA)",
197#else
198 "Use VMA or LMA addresses when loading image (default VMA)",
199#endif
200 standard_option_handler, "load-{lma,vma}" },
201 { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA},
202 '\0', NULL, "", standard_option_handler, "" },
203#endif
204
027e2a04
HPN
205 { {"sysroot", required_argument, NULL, OPTION_SYSROOT},
206 '\0', "SYSROOT",
207 "Root for system calls with absolute file-names and cwd at start",
21cf617c 208 standard_option_handler, NULL },
027e2a04 209
21cf617c 210 { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
c906108c
SS
211};
212
213static SIM_RC
214standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
215 char *arg, int is_command)
216{
217 int i,n;
218
219 switch ((STANDARD_OPTIONS) opt)
220 {
221 case OPTION_VERBOSE:
222 STATE_VERBOSE_P (sd) = 1;
223 break;
224
c906108c
SS
225 case OPTION_ENDIAN:
226 if (strcmp (arg, "big") == 0)
227 {
228 if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
229 {
230 sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
231 return SIM_RC_FAIL;
232 }
233 /* FIXME:wip: Need to set something in STATE_CONFIG. */
234 current_target_byte_order = BIG_ENDIAN;
235 }
236 else if (strcmp (arg, "little") == 0)
237 {
238 if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
239 {
240 sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
241 return SIM_RC_FAIL;
242 }
243 /* FIXME:wip: Need to set something in STATE_CONFIG. */
244 current_target_byte_order = LITTLE_ENDIAN;
245 }
246 else
247 {
248 sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
249 return SIM_RC_FAIL;
250 }
251 break;
c906108c
SS
252
253 case OPTION_ENVIRONMENT:
254 if (strcmp (arg, "user") == 0)
255 STATE_ENVIRONMENT (sd) = USER_ENVIRONMENT;
256 else if (strcmp (arg, "virtual") == 0)
257 STATE_ENVIRONMENT (sd) = VIRTUAL_ENVIRONMENT;
258 else if (strcmp (arg, "operating") == 0)
259 STATE_ENVIRONMENT (sd) = OPERATING_ENVIRONMENT;
260 else
261 {
262 sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg);
263 return SIM_RC_FAIL;
264 }
265 if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
266 && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd))
267 {
3dd68605 268 const char *type;
c906108c
SS
269 switch (WITH_ENVIRONMENT)
270 {
271 case USER_ENVIRONMENT: type = "user"; break;
272 case VIRTUAL_ENVIRONMENT: type = "virtual"; break;
273 case OPERATING_ENVIRONMENT: type = "operating"; break;
274 }
275 sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n",
276 type);
277 return SIM_RC_FAIL;
278 }
279 break;
280
281 case OPTION_ALIGNMENT:
282 if (strcmp (arg, "strict") == 0)
283 {
284 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT)
285 {
286 current_alignment = STRICT_ALIGNMENT;
287 break;
288 }
289 }
290 else if (strcmp (arg, "nonstrict") == 0)
291 {
292 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT)
293 {
294 current_alignment = NONSTRICT_ALIGNMENT;
295 break;
296 }
297 }
298 else if (strcmp (arg, "forced") == 0)
299 {
300 if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT)
301 {
302 current_alignment = FORCED_ALIGNMENT;
303 break;
304 }
305 }
306 else
307 {
308 sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg);
309 return SIM_RC_FAIL;
310 }
311 switch (WITH_ALIGNMENT)
312 {
313 case STRICT_ALIGNMENT:
314 sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n");
315 break;
316 case NONSTRICT_ALIGNMENT:
317 sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n");
318 break;
319 case FORCED_ALIGNMENT:
320 sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n");
321 break;
322 }
323 return SIM_RC_FAIL;
324
325 case OPTION_DEBUG:
326 if (! WITH_DEBUG)
327 sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
328 else
329 {
330 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
331 for (i = 0; i < MAX_DEBUG_VALUES; ++i)
332 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
333 }
334 break;
335
336 case OPTION_DEBUG_INSN :
337 if (! WITH_DEBUG)
338 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
339 else
340 {
341 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
342 CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
343 }
344 break;
345
346 case OPTION_DEBUG_FILE :
347 if (! WITH_DEBUG)
348 sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
349 else
350 {
351 FILE *f = fopen (arg, "w");
352
353 if (f == NULL)
354 {
355 sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
356 return SIM_RC_FAIL;
357 }
358 for (n = 0; n < MAX_NR_PROCESSORS; ++n)
359 CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
360 }
361 break;
362
363#ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir. */
828c9ae6
MS
364 case OPTION_H8300H:
365 set_h8300h (bfd_mach_h8300h);
a8cdafbd
AV
366 break;
367 case OPTION_H8300S:
828c9ae6
MS
368 set_h8300h (bfd_mach_h8300s);
369 break;
370 case OPTION_H8300SX:
371 set_h8300h (bfd_mach_h8300sx);
c906108c
SS
372 break;
373#endif
374
375#ifdef SIM_HAVE_FLATMEM
376 case OPTION_MEM_SIZE:
377 {
f40f1a01
NC
378 char * endp;
379 unsigned long ul = strtol (arg, &endp, 0);
380
381 switch (* endp)
382 {
383 case 'k': case 'K': size <<= 10; break;
384 case 'm': case 'M': size <<= 20; break;
385 case 'g': case 'G': size <<= 30; break;
386 case ' ': case '\0': case '\t': break;
387 default:
388 if (ul > 0)
389 sim_io_eprintf (sd, "Ignoring strange character at end of memory size: %c\n", * endp);
390 break;
391 }
392
c906108c
SS
393 /* 16384: some minimal amount */
394 if (! isdigit (arg[0]) || ul < 16384)
395 {
396 sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
397 return SIM_RC_FAIL;
398 }
399 STATE_MEM_SIZE (sd) = ul;
400 }
401 break;
402#endif
403
404 case OPTION_DO_COMMAND:
405 sim_do_command (sd, arg);
406 break;
407
408 case OPTION_ARCHITECTURE:
409 {
410 const struct bfd_arch_info *ap = bfd_scan_arch (arg);
411 if (ap == NULL)
412 {
413 sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
414 return SIM_RC_FAIL;
415 }
416 STATE_ARCHITECTURE (sd) = ap;
417 break;
418 }
419
420 case OPTION_ARCHITECTURE_INFO:
421 {
34b47c38 422 const char **list = bfd_arch_list ();
c906108c
SS
423 const char **lp;
424 if (list == NULL)
425 abort ();
426 sim_io_printf (sd, "Possible architectures:");
427 for (lp = list; *lp != NULL; lp++)
428 sim_io_printf (sd, " %s", *lp);
429 sim_io_printf (sd, "\n");
430 free (list);
431 break;
432 }
433
434 case OPTION_TARGET:
435 {
436 STATE_TARGET (sd) = xstrdup (arg);
437 break;
438 }
439
43e526b9
JM
440 case OPTION_LOAD_LMA:
441 {
442 STATE_LOAD_AT_LMA_P (sd) = 1;
443 break;
444 }
445
446 case OPTION_LOAD_VMA:
447 {
448 STATE_LOAD_AT_LMA_P (sd) = 0;
449 break;
450 }
451
c906108c
SS
452 case OPTION_HELP:
453 sim_print_help (sd, is_command);
454 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
455 exit (0);
456 /* FIXME: 'twould be nice to do something similar if gdb. */
457 break;
027e2a04
HPN
458
459 case OPTION_SYSROOT:
460 /* Don't leak memory in the odd event that there's lots of
440db575
MF
461 --sysroot=... options. We treat "" specially since this
462 is the statically initialized value and cannot free it. */
463 if (simulator_sysroot[0] != '\0')
027e2a04 464 free (simulator_sysroot);
440db575
MF
465 if (arg[0] != '\0')
466 simulator_sysroot = xstrdup (arg);
467 else
468 simulator_sysroot = "";
027e2a04 469 break;
c906108c
SS
470 }
471
472 return SIM_RC_OK;
473}
474
475/* Add the standard option list to the simulator. */
476
477SIM_RC
478standard_install (SIM_DESC sd)
479{
480 SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
481 if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK)
482 return SIM_RC_FAIL;
43e526b9
JM
483#ifdef SIM_HANDLES_LMA
484 STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA;
485#endif
c906108c
SS
486 return SIM_RC_OK;
487}
488
489/* Return non-zero if arg is a duplicate argument.
490 If ARG is NULL, initialize. */
491
492#define ARG_HASH_SIZE 97
493#define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
494
495static int
e377ca52 496dup_arg_p (const char *arg)
c906108c
SS
497{
498 int hash;
e377ca52 499 static const char **arg_table = NULL;
c906108c
SS
500
501 if (arg == NULL)
502 {
503 if (arg_table == NULL)
e377ca52 504 arg_table = (const char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
c906108c
SS
505 memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
506 return 0;
507 }
508
509 hash = ARG_HASH (arg);
510 while (arg_table[hash] != NULL)
511 {
512 if (strcmp (arg, arg_table[hash]) == 0)
513 return 1;
514 /* We assume there won't be more than ARG_HASH_SIZE arguments so we
515 don't check if the table is full. */
516 if (++hash == ARG_HASH_SIZE)
517 hash = 0;
518 }
519 arg_table[hash] = arg;
520 return 0;
521}
028f6515 522
c906108c
SS
523/* Called by sim_open to parse the arguments. */
524
525SIM_RC
dc416615 526sim_parse_args (SIM_DESC sd, char **argv)
c906108c
SS
527{
528 int c, i, argc, num_opts;
529 char *p, *short_options;
530 /* The `val' option struct entry is dynamically assigned for options that
531 only come in the long form. ORIG_VAL is used to get the original value
532 back. */
533 int *orig_val;
534 struct option *lp, *long_options;
535 const struct option_list *ol;
536 const OPTION *opt;
537 OPTION_HANDLER **handlers;
538 sim_cpu **opt_cpu;
7c070881 539 SIM_RC result = SIM_RC_OK;
c906108c
SS
540
541 /* Count the number of arguments. */
542 for (argc = 0; argv[argc] != NULL; ++argc)
543 continue;
544
545 /* Count the number of options. */
546 num_opts = 0;
547 for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
548 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
549 ++num_opts;
550 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
551 for (ol = CPU_OPTIONS (STATE_CPU (sd, i)); ol != NULL; ol = ol->next)
552 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
553 ++num_opts;
554
555 /* Initialize duplicate argument checker. */
556 (void) dup_arg_p (NULL);
557
558 /* Build the option table for getopt. */
559
560 long_options = NZALLOC (struct option, num_opts + 1);
561 lp = long_options;
562 short_options = NZALLOC (char, num_opts * 3 + 1);
563 p = short_options;
564 handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts);
565 orig_val = NZALLOC (int, OPTION_START + num_opts);
566 opt_cpu = NZALLOC (sim_cpu *, OPTION_START + num_opts);
567
568 /* Set '+' as first char so argument permutation isn't done. This
569 is done to stop getopt_long returning options that appear after
570 the target program. Such options should be passed unchanged into
571 the program image. */
572 *p++ = '+';
573
574 for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
575 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
576 {
577 if (dup_arg_p (opt->opt.name))
578 continue;
579 if (opt->shortopt != 0)
580 {
581 *p++ = opt->shortopt;
582 if (opt->opt.has_arg == required_argument)
583 *p++ = ':';
584 else if (opt->opt.has_arg == optional_argument)
585 { *p++ = ':'; *p++ = ':'; }
586 handlers[(unsigned char) opt->shortopt] = opt->handler;
587 if (opt->opt.val != 0)
588 orig_val[(unsigned char) opt->shortopt] = opt->opt.val;
589 else
590 orig_val[(unsigned char) opt->shortopt] = opt->shortopt;
591 }
592 if (opt->opt.name != NULL)
593 {
594 *lp = opt->opt;
595 /* Dynamically assign `val' numbers for long options. */
596 lp->val = i++;
597 handlers[lp->val] = opt->handler;
598 orig_val[lp->val] = opt->opt.val;
599 opt_cpu[lp->val] = NULL;
600 ++lp;
601 }
602 }
603
604 for (c = 0; c < MAX_NR_PROCESSORS; ++c)
605 {
606 sim_cpu *cpu = STATE_CPU (sd, c);
607 for (ol = CPU_OPTIONS (cpu); ol != NULL; ol = ol->next)
608 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
609 {
610#if 0 /* Each option is prepended with --<cpuname>- so this greatly cuts down
611 on the need for dup_arg_p checking. Maybe in the future it'll be
612 needed so this is just commented out, and not deleted. */
613 if (dup_arg_p (opt->opt.name))
614 continue;
615#endif
616 /* Don't allow short versions of cpu specific options for now. */
617 if (opt->shortopt != 0)
618 {
619 sim_io_eprintf (sd, "internal error, short cpu specific option");
7c070881
SC
620 result = SIM_RC_FAIL;
621 break;
c906108c
SS
622 }
623 if (opt->opt.name != NULL)
624 {
625 char *name;
626 *lp = opt->opt;
627 /* Prepend --<cpuname>- to the option. */
39a3ae0a
MF
628 if (asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name) < 0)
629 {
630 sim_io_eprintf (sd, "internal error, out of memory");
631 result = SIM_RC_FAIL;
632 break;
633 }
c906108c
SS
634 lp->name = name;
635 /* Dynamically assign `val' numbers for long options. */
636 lp->val = i++;
637 handlers[lp->val] = opt->handler;
638 orig_val[lp->val] = opt->opt.val;
639 opt_cpu[lp->val] = cpu;
640 ++lp;
641 }
642 }
643 }
028f6515 644
c906108c
SS
645 /* Terminate the short and long option lists. */
646 *p = 0;
647 lp->name = NULL;
648
649 /* Ensure getopt is initialized. */
650 optind = 0;
651
652 while (1)
653 {
654 int longind, optc;
655
656 optc = getopt_long (argc, argv, short_options, long_options, &longind);
657 if (optc == -1)
658 {
659 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
660 STATE_PROG_ARGV (sd) = dupargv (argv + optind);
661 break;
662 }
663 if (optc == '?')
7c070881
SC
664 {
665 result = SIM_RC_FAIL;
666 break;
667 }
c906108c
SS
668
669 if ((*handlers[optc]) (sd, opt_cpu[optc], orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
7c070881
SC
670 {
671 result = SIM_RC_FAIL;
672 break;
673 }
c906108c
SS
674 }
675
d79fe0d6
MF
676 free (long_options);
677 free (short_options);
678 free (handlers);
679 free (opt_cpu);
680 free (orig_val);
7c070881 681 return result;
c906108c
SS
682}
683
684/* Utility of sim_print_help to print a list of option tables. */
685
686static void
687print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_command)
688{
689 const OPTION *opt;
690
691 for ( ; ol != NULL; ol = ol->next)
692 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
693 {
694 const int indent = 30;
695 int comma, len;
696 const OPTION *o;
697
698 if (dup_arg_p (opt->opt.name))
699 continue;
700
701 if (opt->doc == NULL)
702 continue;
703
704 if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
705 continue;
706
707 sim_io_printf (sd, " ");
708
709 comma = 0;
710 len = 2;
711
712 /* list any short options (aliases) for the current OPT */
713 if (!is_command)
714 {
715 o = opt;
716 do
717 {
718 if (o->shortopt != '\0')
719 {
720 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
721 len += (comma ? 2 : 0) + 2;
722 if (o->arg != NULL)
723 {
724 if (o->opt.has_arg == optional_argument)
725 {
726 sim_io_printf (sd, "[%s]", o->arg);
727 len += 1 + strlen (o->arg) + 1;
728 }
729 else
730 {
731 sim_io_printf (sd, " %s", o->arg);
732 len += 1 + strlen (o->arg);
733 }
734 }
735 comma = 1;
736 }
737 ++o;
738 }
739 while (OPTION_VALID_P (o) && o->doc == NULL);
740 }
028f6515 741
c906108c
SS
742 /* list any long options (aliases) for the current OPT */
743 o = opt;
744 do
745 {
746 const char *name;
747 const char *cpu_prefix = cpu ? CPU_NAME (cpu) : NULL;
748 if (o->doc_name != NULL)
749 name = o->doc_name;
750 else
751 name = o->opt.name;
752 if (name != NULL)
753 {
754 sim_io_printf (sd, "%s%s%s%s%s",
755 comma ? ", " : "",
756 is_command ? "" : "--",
757 cpu ? cpu_prefix : "",
758 cpu ? "-" : "",
759 name);
760 len += ((comma ? 2 : 0)
761 + (is_command ? 0 : 2)
762 + strlen (name));
763 if (o->arg != NULL)
764 {
765 if (o->opt.has_arg == optional_argument)
766 {
c4093a6a 767 sim_io_printf (sd, "[=%s]", o->arg);
c906108c
SS
768 len += 2 + strlen (o->arg) + 1;
769 }
770 else
771 {
772 sim_io_printf (sd, " %s", o->arg);
773 len += 1 + strlen (o->arg);
774 }
775 }
776 comma = 1;
777 }
778 ++o;
779 }
780 while (OPTION_VALID_P (o) && o->doc == NULL);
781
782 if (len >= indent)
783 {
784 sim_io_printf (sd, "\n%*s", indent, "");
785 }
786 else
787 sim_io_printf (sd, "%*s", indent - len, "");
788
789 /* print the description, word wrap long lines */
790 {
791 const char *chp = opt->doc;
792 unsigned doc_width = 80 - indent;
793 while (strlen (chp) >= doc_width) /* some slack */
794 {
795 const char *end = chp + doc_width - 1;
796 while (end > chp && !isspace (*end))
797 end --;
798 if (end == chp)
799 end = chp + doc_width - 1;
e158f0a0
AC
800 /* The cast should be ok - its distances between to
801 points in a string. */
802 sim_io_printf (sd, "%.*s\n%*s", (int) (end - chp), chp, indent,
803 "");
c906108c
SS
804 chp = end;
805 while (isspace (*chp) && *chp != '\0')
806 chp++;
807 }
808 sim_io_printf (sd, "%s\n", chp);
809 }
810 }
811}
812
813/* Print help messages for the options. */
814
815void
dc416615 816sim_print_help (SIM_DESC sd, int is_command)
c906108c
SS
817{
818 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
819 sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
820 STATE_MY_NAME (sd));
821
822 /* Initialize duplicate argument checker. */
823 (void) dup_arg_p (NULL);
824
825 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
826 sim_io_printf (sd, "Options:\n");
827 else
828 sim_io_printf (sd, "Commands:\n");
829
830 print_help (sd, NULL, STATE_OPTIONS (sd), is_command);
831 sim_io_printf (sd, "\n");
832
833 /* Print cpu-specific options. */
834 {
835 int i;
836
837 for (i = 0; i < MAX_NR_PROCESSORS; ++i)
838 {
839 sim_cpu *cpu = STATE_CPU (sd, i);
840 if (CPU_OPTIONS (cpu) == NULL)
841 continue;
842 sim_io_printf (sd, "CPU %s specific options:\n", CPU_NAME (cpu));
843 print_help (sd, cpu, CPU_OPTIONS (cpu), is_command);
844 sim_io_printf (sd, "\n");
845 }
846 }
847
848 sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
849 STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
850 sim_io_printf (sd, " may not be applicable\n");
851
852 if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
853 {
854 sim_io_printf (sd, "\n");
855 sim_io_printf (sd, "program args Arguments to pass to simulated program.\n");
856 sim_io_printf (sd, " Note: Very few simulators support this.\n");
857 }
858}
859
860/* Utility of sim_args_command to find the closest match for a command.
861 Commands that have "-" in them can be specified as separate words.
862 e.g. sim memory-region 0x800000,0x4000
863 or sim memory region 0x800000,0x4000
864 If CPU is non-null, use its option table list, otherwise use the main one.
865 *PARGI is where to start looking in ARGV. It is updated to point past
866 the found option. */
867
868static const OPTION *
869find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi)
870{
871 const struct option_list *ol;
872 const OPTION *opt;
873 /* most recent option match */
874 const OPTION *matching_opt = NULL;
875 int matching_argi = -1;
876
877 if (cpu)
878 ol = CPU_OPTIONS (cpu);
879 else
880 ol = STATE_OPTIONS (sd);
881
882 /* Skip passed elements specified by *PARGI. */
883 argv += *pargi;
884
885 for ( ; ol != NULL; ol = ol->next)
886 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
887 {
888 int argi = 0;
889 const char *name = opt->opt.name;
890 if (name == NULL)
891 continue;
892 while (argv [argi] != NULL
893 && strncmp (name, argv [argi], strlen (argv [argi])) == 0)
894 {
895 name = &name [strlen (argv[argi])];
896 if (name [0] == '-')
897 {
898 /* leading match ...<a-b-c>-d-e-f - continue search */
899 name ++; /* skip `-' */
900 argi ++;
901 continue;
902 }
903 else if (name [0] == '\0')
904 {
905 /* exact match ...<a-b-c-d-e-f> - better than before? */
906 if (argi > matching_argi)
907 {
908 matching_argi = argi;
909 matching_opt = opt;
910 }
911 break;
912 }
913 else
914 break;
915 }
916 }
917
918 *pargi = matching_argi;
919 return matching_opt;
920}
921
56a9aa1d
MF
922static char **
923complete_option_list (char **ret, size_t *cnt, const struct option_list *ol,
924 char *text, char *word)
925{
926 const OPTION *opt = NULL;
927 int argi;
928 size_t len = strlen (word);
929
930 for ( ; ol != NULL; ol = ol->next)
931 for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
932 {
933 const char *name = opt->opt.name;
934
935 /* A long option to match against? */
936 if (!name)
937 continue;
938
939 /* Does this option actually match? */
940 if (strncmp (name, word, len))
941 continue;
942
943 ret = xrealloc (ret, ++*cnt * sizeof (ret[0]));
944 ret[*cnt - 2] = xstrdup (name);
945 }
946
947 return ret;
948}
949
950/* All leading text is stored in @text, while the current word being
951 completed is stored in @word. Trailing text of @word is not. */
5592f70e 952
56a9aa1d
MF
953char **
954sim_complete_command (SIM_DESC sd, char *text, char *word)
955{
956 char **ret = NULL;
957 size_t cnt = 1;
958 sim_cpu *cpu;
959
960 /* Only complete first word for now. */
961 if (text != word)
962 return ret;
963
964 cpu = STATE_CPU (sd, 0);
965 if (cpu)
966 ret = complete_option_list (ret, &cnt, CPU_OPTIONS (cpu), text, word);
967 ret = complete_option_list (ret, &cnt, STATE_OPTIONS (sd), text, word);
968
969 if (ret)
970 ret[cnt - 1] = NULL;
971 return ret;
972}
973
c906108c
SS
974SIM_RC
975sim_args_command (SIM_DESC sd, char *cmd)
976{
977 /* something to do? */
978 if (cmd == NULL)
109d3db3 979 return SIM_RC_OK; /* FIXME - perhaps help would be better */
028f6515 980
c906108c
SS
981 if (cmd [0] == '-')
982 {
983 /* user specified -<opt> ... form? */
984 char **argv = buildargv (cmd);
985 SIM_RC rc = sim_parse_args (sd, argv);
986 freeargv (argv);
987 return rc;
988 }
989 else
990 {
991 char **argv = buildargv (cmd);
992 const OPTION *matching_opt = NULL;
993 int matching_argi;
994 sim_cpu *cpu;
995
996 if (argv [0] == NULL)
109d3db3 997 return SIM_RC_OK; /* FIXME - perhaps help would be better */
c906108c
SS
998
999 /* First check for a cpu selector. */
1000 {
1001 char *cpu_name = xstrdup (argv[0]);
1002 char *hyphen = strchr (cpu_name, '-');
1003 if (hyphen)
1004 *hyphen = 0;
1005 cpu = sim_cpu_lookup (sd, cpu_name);
1006 if (cpu)
1007 {
1008 /* If <cpuname>-<command>, point argv[0] at <command>. */
1009 if (hyphen)
1010 {
1011 matching_argi = 0;
1012 argv[0] += hyphen - cpu_name + 1;
1013 }
1014 else
1015 matching_argi = 1;
1016 matching_opt = find_match (sd, cpu, argv, &matching_argi);
1017 /* If hyphen found restore argv[0]. */
1018 if (hyphen)
1019 argv[0] -= hyphen - cpu_name + 1;
1020 }
1021 free (cpu_name);
1022 }
1023
1024 /* If that failed, try the main table. */
1025 if (matching_opt == NULL)
1026 {
1027 matching_argi = 0;
1028 matching_opt = find_match (sd, NULL, argv, &matching_argi);
1029 }
1030
1031 if (matching_opt != NULL)
1032 {
1033 switch (matching_opt->opt.has_arg)
1034 {
1035 case no_argument:
1036 if (argv [matching_argi + 1] == NULL)
1037 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1038 NULL, 1/*is_command*/);
1039 else
1040 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
1041 matching_opt->opt.name);
1042 break;
1043 case optional_argument:
1044 if (argv [matching_argi + 1] == NULL)
1045 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1046 NULL, 1/*is_command*/);
1047 else if (argv [matching_argi + 2] == NULL)
1048 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1049 argv [matching_argi + 1], 1/*is_command*/);
1050 else
1051 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
1052 matching_opt->opt.name);
1053 break;
1054 case required_argument:
1055 if (argv [matching_argi + 1] == NULL)
1056 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
1057 matching_opt->opt.name);
1058 else if (argv [matching_argi + 2] == NULL)
1059 matching_opt->handler (sd, cpu, matching_opt->opt.val,
1060 argv [matching_argi + 1], 1/*is_command*/);
1061 else
1062 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
1063 matching_opt->opt.name);
1064 }
1065 freeargv (argv);
1066 return SIM_RC_OK;
1067 }
1068
1069 freeargv (argv);
1070 }
028f6515 1071
c906108c
SS
1072 /* didn't find anything that remotly matched */
1073 return SIM_RC_FAIL;
1074}