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