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