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