* Make-common.in (SIM_ENVIRONMENT): New variable.
[external/binutils.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 (sd, table)
46      SIM_DESC sd;
47      const OPTION *table;
48 {
49   struct option_list *ol = ((struct option_list *)
50                             xmalloc (sizeof (struct option_list)));
51
52   /* Note: The list is constructed in the reverse order we're called so
53      later calls will override earlier ones (in case that ever happens).
54      This is the intended behaviour.  */
55   ol->next = STATE_OPTIONS (sd);
56   ol->options = table;
57   STATE_OPTIONS (sd) = ol;
58
59   return SIM_RC_OK;
60 }
61
62 /* Standard option table.
63    Modules may specify additional ones.
64    The caller of sim_parse_args may also specify additional options
65    by calling sim_add_option_table first.  */
66
67 static DECLARE_OPTION_HANDLER (standard_option_handler);
68
69 /* FIXME: We shouldn't print in --help output options that aren't usable.
70    Some fine tuning will be necessary.  One can either move less general
71    options to another table or use a HAVE_FOO macro to ifdef out unavailable
72    options.  */
73
74 /* ??? One might want to conditionally compile out the entries that
75    aren't enabled.  There's a distinction, however, between options a
76    simulator can't support and options that haven't been configured in.
77    Certainly options a simulator can't support shouldn't appear in the
78    output of --help.  Whether the same thing applies to options that haven't
79    been configured in or not isn't something I can get worked up over.
80    [Note that conditionally compiling them out might simply involve moving
81    the option to another table.]
82    If you decide to conditionally compile them out as well, delete this
83    comment and add a comment saying that that is the rule.  */
84
85 typedef enum {
86   OPTION_DEBUG_INSN = OPTION_START,
87   OPTION_DEBUG_FILE,
88   OPTION_DO_COMMAND,
89   OPTION_ARCHITECTURE,
90   OPTION_TARGET,
91   OPTION_ARCHITECTURE_INFO,
92   OPTION_ENVIRONMENT,
93   OPTION_ALIGNMENT,
94   OPTION_VERBOSE,
95 #if defined (SIM_HAVE_BIENDIAN)
96   OPTION_ENDIAN,
97 #endif
98   OPTION_DEBUG,
99 #ifdef SIM_HAVE_FLATMEM
100   OPTION_MEM_SIZE,
101 #endif
102   OPTION_HELP,
103 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir.  */
104   OPTION_H8300,
105 #endif
106 } STANDARD_OPTIONS;
107
108 static const OPTION standard_options[] =
109 {
110   { {"verbose", no_argument, NULL, OPTION_VERBOSE},
111       'v', NULL, "Verbose output",
112       standard_option_handler },
113
114 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
115   { {"endian", required_argument, NULL, OPTION_ENDIAN},
116       'E', "big|little", "Set endianness",
117       standard_option_handler },
118 #endif
119
120 #ifdef SIM_HAVE_ENVIRONMENT
121   /* This option isn't supported unless all choices are supported in keeping
122      with the goal of not printing in --help output things the simulator can't
123      do [as opposed to things that just haven't been configured in].  */
124   { {"environment", required_argument, NULL, OPTION_ENVIRONMENT},
125       '\0', "user|virtual|operating", "Set running environment",
126       standard_option_handler },
127 #endif
128
129   { {"alignment", required_argument, NULL, OPTION_ALIGNMENT},
130       '\0', "strict|nonstrict|forced", "Set memory access alignment",
131       standard_option_handler },
132
133   { {"debug", no_argument, NULL, OPTION_DEBUG},
134       'D', NULL, "Print debugging messages",
135       standard_option_handler },
136   { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
137       '\0', NULL, "Print instruction debugging messages",
138       standard_option_handler },
139   { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
140       '\0', "FILE NAME", "Specify debugging output file",
141       standard_option_handler },
142
143 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir.  */
144   { {"h8300h", no_argument, NULL, OPTION_H8300},
145       'h', NULL, "Indicate the CPU is h8/300h or h8/300s",
146       standard_option_handler },
147 #endif
148
149 #ifdef SIM_HAVE_FLATMEM
150   { {"mem-size", required_argument, NULL, OPTION_MEM_SIZE},
151       'm', "MEMORY SIZE", "Specify memory size",
152       standard_option_handler },
153 #endif
154
155   { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
156       '\0', "COMMAND", ""/*undocumented*/,
157       standard_option_handler },
158
159   { {"help", no_argument, NULL, OPTION_HELP},
160       'H', NULL, "Print help information",
161       standard_option_handler },
162
163   { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
164       '\0', "MACHINE", "Specify the architecture to use",
165       standard_option_handler },
166   { {"architecture-info", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
167       '\0', NULL, "List supported architectures",
168       standard_option_handler },
169   { {"info-architecture", no_argument, NULL, OPTION_ARCHITECTURE_INFO},
170       '\0', NULL, NULL,
171       standard_option_handler },
172
173   { {"target", required_argument, NULL, OPTION_TARGET},
174       '\0', "BFDNAME", "Specify the object-code format for the object files",
175       standard_option_handler },
176
177   { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
178 };
179
180 static SIM_RC
181 standard_option_handler (sd, opt, arg, is_command)
182      SIM_DESC sd;
183      int opt;
184      char *arg;
185      int is_command;
186 {
187   int i,n;
188
189   switch ((STANDARD_OPTIONS) opt)
190     {
191     case OPTION_VERBOSE:
192       STATE_VERBOSE_P (sd) = 1;
193       break;
194
195 #ifdef SIM_HAVE_BIENDIAN
196     case OPTION_ENDIAN:
197       if (strcmp (arg, "big") == 0)
198         {
199           if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
200             {
201               sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
202               return SIM_RC_FAIL;
203             }
204           /* FIXME:wip: Need to set something in STATE_CONFIG.  */
205           current_target_byte_order = BIG_ENDIAN;
206         }
207       else if (strcmp (arg, "little") == 0)
208         {
209           if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
210             {
211               sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
212               return SIM_RC_FAIL;
213             }
214           /* FIXME:wip: Need to set something in STATE_CONFIG.  */
215           current_target_byte_order = LITTLE_ENDIAN;
216         }
217       else
218         {
219           sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
220           return SIM_RC_FAIL;
221         }
222       break;
223 #endif
224
225     case OPTION_ENVIRONMENT:
226       if (strcmp (arg, "user") == 0)
227         current_environment = USER_ENVIRONMENT;
228       else if (strcmp (arg, "virtual") == 0)
229         current_environment = VIRTUAL_ENVIRONMENT;
230       else if (strcmp (arg, "operating") == 0)
231         current_environment = OPERATING_ENVIRONMENT;
232       else
233         {
234           sim_io_eprintf (sd, "Invalid environment specification `%s'\n", arg);
235           return SIM_RC_FAIL;
236         }
237       if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
238           && WITH_ENVIRONMENT != current_environment)
239         {
240           char *type;
241           switch (WITH_ENVIRONMENT)
242             {
243             case USER_ENVIRONMENT: type = "user"; break;
244             case VIRTUAL_ENVIRONMENT: type = "virtual"; break;
245             case OPERATING_ENVIRONMENT: type = "operating"; break;
246             }
247           sim_io_eprintf (sd, "Simulator compiled for the %s environment only.\n",
248                           type);
249           return SIM_RC_FAIL;
250         }
251       break;
252
253     case OPTION_ALIGNMENT:
254       if (strcmp (arg, "strict") == 0)
255         {
256           if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == STRICT_ALIGNMENT)
257             {
258               current_alignment = STRICT_ALIGNMENT;
259               break;
260             }
261         }
262       else if (strcmp (arg, "nonstrict") == 0)
263         {
264           if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == NONSTRICT_ALIGNMENT)
265             {
266               current_alignment = NONSTRICT_ALIGNMENT;
267               break;
268             }
269         }
270       else if (strcmp (arg, "forced") == 0)
271         {
272           if (WITH_ALIGNMENT == 0 || WITH_ALIGNMENT == FORCED_ALIGNMENT)
273             {
274               current_alignment = FORCED_ALIGNMENT;
275               break;
276             }
277         }
278       else
279         {
280           sim_io_eprintf (sd, "Invalid alignment specification `%s'\n", arg);
281           return SIM_RC_FAIL;
282         }
283       switch (WITH_ALIGNMENT)
284         {
285         case STRICT_ALIGNMENT:
286           sim_io_eprintf (sd, "Simulator compiled for strict alignment only.\n");
287           break;
288         case NONSTRICT_ALIGNMENT:
289           sim_io_eprintf (sd, "Simulator compiled for nonstrict alignment only.\n");
290           break;
291         case FORCED_ALIGNMENT:
292           sim_io_eprintf (sd, "Simulator compiled for forced alignment only.\n");
293           break;
294         }
295       return SIM_RC_FAIL;
296
297     case OPTION_DEBUG:
298       if (! WITH_DEBUG)
299         sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
300       else
301         {
302           for (n = 0; n < MAX_NR_PROCESSORS; ++n)
303             for (i = 0; i < MAX_DEBUG_VALUES; ++i)
304               CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
305         }
306       break;
307
308     case OPTION_DEBUG_INSN :
309       if (! WITH_DEBUG)
310         sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
311       else
312         {
313           for (n = 0; n < MAX_NR_PROCESSORS; ++n)
314             CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
315         }
316       break;
317
318     case OPTION_DEBUG_FILE :
319       if (! WITH_DEBUG)
320         sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
321       else
322         {
323           FILE *f = fopen (arg, "w");
324
325           if (f == NULL)
326             {
327               sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
328               return SIM_RC_FAIL;
329             }
330           for (n = 0; n < MAX_NR_PROCESSORS; ++n)
331             CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
332         }
333       break;
334
335 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir.  */
336     case OPTION_H8300:
337       set_h8300h (1);
338       break;
339 #endif
340
341 #ifdef SIM_HAVE_FLATMEM
342     case OPTION_MEM_SIZE:
343       {
344         unsigned long ul = strtol (arg, NULL, 0);
345         /* 16384: some minimal amount */
346         if (! isdigit (arg[0]) || ul < 16384)
347           {
348             sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
349             return SIM_RC_FAIL;
350           }
351         STATE_MEM_SIZE (sd) = ul;
352       }
353       break;
354 #endif
355
356     case OPTION_DO_COMMAND:
357       sim_do_command (sd, arg);
358       break;
359
360     case OPTION_ARCHITECTURE:
361       {
362         const struct bfd_arch_info *ap = bfd_scan_arch (arg);
363         if (ap == NULL)
364           {
365             sim_io_eprintf (sd, "Architecture `%s' unknown\n", arg);
366             return SIM_RC_FAIL;
367           }
368         STATE_ARCHITECTURE (sd) = ap;
369         break;
370       }
371
372     case OPTION_ARCHITECTURE_INFO:
373       {
374         const char **list = bfd_arch_list();
375         const char **lp;
376         if (list == NULL)
377           abort ();
378         sim_io_printf (sd, "Valid architectures:");
379         for (lp = list; *lp != NULL; lp++)
380           sim_io_printf (sd, " %s", *lp);
381         sim_io_printf (sd, "\n");
382         free (list);
383         break;
384       }
385
386     case OPTION_TARGET:
387       {
388         STATE_TARGET (sd) = xstrdup (arg);
389         break;
390       }
391
392     case OPTION_HELP:
393       sim_print_help (sd, is_command);
394       if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
395         exit (0);
396       /* FIXME: 'twould be nice to do something similar if gdb.  */
397       break;
398     }
399
400   return SIM_RC_OK;
401 }
402
403 /* Add the standard option list to the simulator.  */
404
405 SIM_RC
406 standard_install (SIM_DESC sd)
407 {
408   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
409   if (sim_add_option_table (sd, standard_options) != SIM_RC_OK)
410     return SIM_RC_FAIL;
411   return SIM_RC_OK;
412 }
413
414 /* Return non-zero if arg is a duplicate argument.
415    If ARG is NULL, initialize.  */
416
417 #define ARG_HASH_SIZE 97
418 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
419
420 static int
421 dup_arg_p (arg)
422      char *arg;
423 {
424   int hash;
425   static char **arg_table = NULL;
426
427   if (arg == NULL)
428     {
429       if (arg_table == NULL)
430         arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
431       memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
432       return 0;
433     }
434
435   hash = ARG_HASH (arg);
436   while (arg_table[hash] != NULL)
437     {
438       if (strcmp (arg, arg_table[hash]) == 0)
439         return 1;
440       /* We assume there won't be more than ARG_HASH_SIZE arguments so we
441          don't check if the table is full.  */
442       if (++hash == ARG_HASH_SIZE)
443         hash = 0;
444     }
445   arg_table[hash] = arg;
446   return 0;
447 }
448      
449 /* Called by sim_open to parse the arguments.  */
450
451 SIM_RC
452 sim_parse_args (sd, argv)
453      SIM_DESC sd;
454      char **argv;
455 {
456   int i, argc, num_opts;
457   char *p, *short_options;
458   /* The `val' option struct entry is dynamically assigned for options that
459      only come in the long form.  ORIG_VAL is used to get the original value
460      back.  */
461   int *orig_val;
462   struct option *lp, *long_options;
463   const struct option_list *ol;
464   const OPTION *opt;
465   OPTION_HANDLER **handlers;
466
467   /* Count the number of arguments.  */
468   for (argc = 0; argv[argc] != NULL; ++argc)
469     continue;
470
471   /* Count the number of options.  */
472   num_opts = 0;
473   for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
474     for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
475       ++num_opts;
476
477   /* Initialize duplicate argument checker.  */
478   (void) dup_arg_p (NULL);
479
480   /* Build the option table for getopt.  */
481   long_options = NZALLOC (struct option, num_opts + 1);
482   lp = long_options;
483   short_options = NZALLOC (char, num_opts * 3 + 1);
484   p = short_options;
485   handlers = NZALLOC (OPTION_HANDLER *, OPTION_START + num_opts);
486   orig_val = NZALLOC (int, OPTION_START + num_opts);
487   /* Set '+' as first char so argument permutation isn't done.  This
488      is done to stop getopt_long returning options that appear after
489      the target program.  Such options should be passed unchanged into
490      the program image. */
491   *p++ = '+';
492   for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
493     for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
494       {
495         if (dup_arg_p (opt->opt.name))
496           continue;
497         if (opt->shortopt != 0)
498           {
499             *p++ = opt->shortopt;
500             if (opt->opt.has_arg == required_argument)
501               *p++ = ':';
502             else if (opt->opt.has_arg == optional_argument)
503               { *p++ = ':'; *p++ = ':'; }
504             handlers[(unsigned char) opt->shortopt] = opt->handler;
505             if (opt->opt.val != 0)
506               orig_val[(unsigned char) opt->shortopt] = opt->opt.val;
507             else
508               orig_val[(unsigned char) opt->shortopt] = opt->shortopt;
509           }
510         if (opt->opt.name != NULL)
511           {
512             *lp = opt->opt;
513             /* Dynamically assign `val' numbers for long options. */
514             lp->val = i++;
515             handlers[lp->val] = opt->handler;
516             orig_val[lp->val] = opt->opt.val;
517             ++lp;
518           }
519       }
520   *p = 0;
521   lp->name = NULL;
522
523   /* Ensure getopt is initialized.  */
524   optind = 0;
525   while (1)
526     {
527       int longind, optc;
528
529       optc = getopt_long (argc, argv, short_options, long_options, &longind);
530       if (optc == -1)
531         {
532           if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
533             STATE_PROG_ARGV (sd) = dupargv (argv + optind);
534           break;
535         }
536       if (optc == '?')
537         return SIM_RC_FAIL;
538
539       if ((*handlers[optc]) (sd, orig_val[optc], optarg, 0/*!is_command*/) == SIM_RC_FAIL)
540         return SIM_RC_FAIL;
541     }
542
543   return SIM_RC_OK;
544 }
545
546 /* Print help messages for the options.  */
547
548 void
549 sim_print_help (sd, is_command)
550      SIM_DESC sd;
551      int is_command;
552 {
553   const struct option_list *ol;
554   const OPTION *opt;
555
556   if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
557     sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
558                    STATE_MY_NAME (sd));
559
560   /* Initialize duplicate argument checker.  */
561   (void) dup_arg_p (NULL);
562
563   if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
564     sim_io_printf (sd, "Options:\n");
565   else
566     sim_io_printf (sd, "Commands:\n");
567
568   for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
569     for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
570       {
571         const int indent = 30;
572         int comma, len;
573         const OPTION *o;
574
575         if (dup_arg_p (opt->opt.name))
576           continue;
577
578         if (opt->doc == NULL)
579           continue;
580
581         if (opt->doc_name != NULL && opt->doc_name [0] == '\0')
582           continue;
583
584         sim_io_printf (sd, "  ");
585
586         comma = 0;
587         len = 2;
588
589         /* list any short options (aliases) for the current OPT */
590         if (!is_command)
591           {
592             o = opt;
593             do
594               {
595                 if (o->shortopt != '\0')
596                   {
597                     sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
598                     len += (comma ? 2 : 0) + 2;
599                     if (o->arg != NULL)
600                       {
601                         if (o->opt.has_arg == optional_argument)
602                           {
603                             sim_io_printf (sd, "[%s]", o->arg);
604                             len += 1 + strlen (o->arg) + 1;
605                           }
606                         else
607                           {
608                             sim_io_printf (sd, " %s", o->arg);
609                             len += 1 + strlen (o->arg);
610                           }
611                       }
612                     comma = 1;
613                   }
614                 ++o;
615               }
616             while (OPTION_VALID_P (o) && o->doc == NULL);
617           }
618         
619         /* list any long options (aliases) for the current OPT */
620         o = opt;
621         do
622           {
623             const char *name;
624             if (o->doc_name != NULL)
625               name = o->doc_name;
626             else
627               name = o->opt.name;
628             if (name != NULL)
629               {
630                 sim_io_printf (sd, "%s%s%s",
631                                comma ? ", " : "",
632                                is_command ? "" : "--",
633                                name);
634                 len += ((comma ? 2 : 0)
635                         + (is_command ? 0 : 2)
636                         + strlen (name));
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 += 2 + 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         if (len >= indent)
657           {
658             sim_io_printf (sd, "\n%*s", indent, "");
659           }
660         else
661           sim_io_printf (sd, "%*s", indent - len, "");
662
663         /* print the description, word wrap long lines */
664         {
665           const char *chp = opt->doc;
666           unsigned doc_width = 80 - indent;
667           while (strlen (chp) >= doc_width) /* some slack */
668             {
669               const char *end = chp + doc_width - 1;
670               while (end > chp && !isspace (*end))
671                 end --;
672               if (end == chp)
673                 end = chp + doc_width - 1;
674               sim_io_printf (sd, "%.*s\n%*s", end - chp, chp, indent, "");
675               chp = end;
676               while (isspace (*chp) && *chp != '\0')
677                 chp++;
678             }
679           sim_io_printf (sd, "%s\n", chp);
680         }
681       }
682
683   sim_io_printf (sd, "\n");
684   sim_io_printf (sd, "Note: Depending on the simulator configuration some %ss\n",
685                  STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE ? "option" : "command");
686   sim_io_printf (sd, "      may not be applicable\n");
687
688   if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
689     {
690       sim_io_printf (sd, "\n");
691       sim_io_printf (sd, "program args    Arguments to pass to simulated program.\n");
692       sim_io_printf (sd, "                Note: Very few simulators support this.\n");
693     }
694 }
695
696
697
698
699 SIM_RC
700 sim_args_command (sd, cmd)
701      SIM_DESC sd;
702      char *cmd;
703 {
704   /* something to do? */
705   if (cmd == NULL)
706     return SIM_RC_OK; /* FIXME - perhaphs help would be better */
707   
708   if (cmd [0] == '-')
709     {
710       /* user specified -<opt> ... form? */
711       char **argv = buildargv (cmd);
712       SIM_RC rc = sim_parse_args (sd, argv);
713       freeargv (argv);
714       return rc;
715     }
716   else
717     {
718       /* user specified <opt> form? */
719       const struct option_list *ol;
720       const OPTION *opt;
721       char **argv = buildargv (cmd);
722       /* most recent option match */
723       const OPTION *matching_opt = NULL;
724       int matching_argi = -1;
725       if (argv [0] != NULL)
726         for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
727           for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
728             {
729               int argi = 0;
730               const char *name = opt->opt.name;
731               if (name == NULL)
732                 continue;
733               while (strncmp (name, argv [argi], strlen (argv [argi])) == 0)
734                 {
735                   name = &name [strlen (argv[argi])];
736                   if (name [0] == '-')
737                     {
738                       /* leading match ...<a-b-c>-d-e-f - continue search */
739                       name ++; /* skip `-' */
740                       argi ++;
741                       continue;
742                     }
743                   else if (name [0] == '\0')
744                     {
745                       /* exact match ...<a-b-c-d-e-f> - better than before? */
746                       if (argi > matching_argi)
747                         {
748                           matching_argi = argi;
749                           matching_opt = opt;
750                         }
751                       break;
752                     }
753                   else
754                     break;
755                 }
756             }
757       if (matching_opt != NULL)
758         {
759           switch (matching_opt->opt.has_arg)
760             {
761             case no_argument:
762               if (argv [matching_argi + 1] == NULL)
763                 matching_opt->handler (sd, matching_opt->opt.val,
764                                        NULL, 1/*is_command*/);
765               else
766                 sim_io_eprintf (sd, "Command `%s' takes no arguments\n",
767                                 matching_opt->opt.name);
768               break;
769             case optional_argument:
770               if (argv [matching_argi + 1] == NULL)
771                 matching_opt->handler (sd, matching_opt->opt.val,
772                                        NULL, 1/*is_command*/);
773               else if (argv [matching_argi + 2] == NULL)
774                 matching_opt->handler (sd, matching_opt->opt.val,
775                                        argv [matching_argi + 1], 1/*is_command*/);
776               else
777                 sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n",
778                                 matching_opt->opt.name);
779               break;
780             case required_argument:
781               if (argv [matching_argi + 1] == NULL)
782                 sim_io_eprintf (sd, "Command `%s' requires an argument\n",
783                                 matching_opt->opt.name);
784               else if (argv [matching_argi + 2] == NULL)
785                 matching_opt->handler (sd, matching_opt->opt.val,
786                                        argv [matching_argi + 1], 1/*is_command*/);
787               else
788                 sim_io_eprintf (sd, "Command `%s' requires only one argument\n",
789                                 matching_opt->opt.name);
790             }
791           return SIM_RC_OK;
792         }
793     }
794       
795   /* didn't find anything that remotly matched */
796   return SIM_RC_FAIL;
797 }