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