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