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