Add function sim_args_command() which takes a `(gdb) sim <command>' and
[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 #include "libiberty.h"
30 #include "../libiberty/alloca-conf.h"
31 #include "sim-options.h"
32 #include "sim-io.h"
33
34 /* Add a set of options to the simulator.
35    TABLE is an array of OPTIONS terminated by a NULL `opt.name' entry.
36    This is intended to be called by modules in their `install' handler.  */
37
38 SIM_RC
39 sim_add_option_table (sd, table)
40      SIM_DESC sd;
41      const OPTION *table;
42 {
43   struct option_list *ol = ((struct option_list *)
44                             xmalloc (sizeof (struct option_list)));
45
46   /* Note: The list is constructed in the reverse order we're called so
47      later calls will override earlier ones (in case that ever happens).
48      This is the intended behaviour.  */
49   ol->next = STATE_OPTIONS (sd);
50   ol->options = table;
51   STATE_OPTIONS (sd) = ol;
52
53   return SIM_RC_OK;
54 }
55
56 /* Standard option table.
57    Modules may specify additional ones.
58    The caller of sim_parse_args may also specify additional options
59    by calling sim_add_option_table first.  */
60
61 static DECLARE_OPTION_HANDLER (standard_option_handler);
62
63 /* FIXME: We shouldn't print in --help output options that aren't usable.
64    Some fine tuning will be necessary.  One can either move less general
65    options to another table or use a HAVE_FOO macro to ifdef out unavailable
66    options.  */
67
68 /* ??? One might want to conditionally compile out the entries that
69    aren't enabled.  There's a distinction, however, between options a
70    simulator can't support and options that haven't been configured in.
71    Certainly options a simulator can't support shouldn't appear in the
72    output of --help.  Whether the same thing applies to options that haven't
73    been configured in or not isn't something I can get worked up over.
74    [Note that conditionally compiling them out might simply involve moving
75    the option to another table.]
76    If you decide to conditionally compile them out as well, delete this
77    comment and add a comment saying that that is the rule.  */
78
79 #define OPTION_DEBUG_INSN       (OPTION_START + 0)
80 #define OPTION_DEBUG_FILE       (OPTION_START + 1)
81 #define OPTION_DO_COMMAND       (OPTION_START + 2)
82
83 static const OPTION standard_options[] =
84 {
85   { {"verbose", no_argument, NULL, 'v'},
86       'v', NULL, "Verbose output",
87       standard_option_handler },
88
89 #if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
90   { {"endian", required_argument, NULL, 'E'},
91       'E', "big|little", "Set endianness",
92       standard_option_handler },
93 #endif
94
95   { {"debug", no_argument, NULL, 'D'},
96       'D', NULL, "Print debugging messages",
97       standard_option_handler },
98   { {"debug-insn", no_argument, NULL, OPTION_DEBUG_INSN},
99       '\0', NULL, "Print instruction debugging messages",
100       standard_option_handler },
101   { {"debug-file", required_argument, NULL, OPTION_DEBUG_FILE},
102       '\0', "FILE NAME", "Specify debugging output file",
103       standard_option_handler },
104
105 #ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir.  */
106   { {"h8300h", no_argument, NULL, 'h'},
107       'h', NULL, "Indicate the CPU is h8/300h or h8/300s",
108       standard_option_handler },
109 #endif
110
111 #ifdef SIM_HAVE_FLATMEM
112   { {"mem-size", required_argument, NULL, 'm'},
113       'm', "MEMORY SIZE", "Specify memory size",
114       standard_option_handler },
115 #endif
116
117   { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
118       '\0', "COMMAND", "Perform a builtin command",
119       standard_option_handler },
120
121   { {"help", no_argument, NULL, 'H'},
122       'H', NULL, "Print help information",
123       standard_option_handler },
124
125   { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
126 };
127
128 static SIM_RC
129 standard_option_handler (sd, opt, arg)
130      SIM_DESC sd;
131      int opt;
132      char *arg;
133 {
134   int i,n;
135
136   switch (opt)
137     {
138     case 'v' :
139       STATE_VERBOSE_P (sd) = 1;
140       break;
141
142 #ifdef SIM_HAVE_BIENDIAN
143     case 'E' :
144       if (strcmp (arg, "big") == 0)
145         {
146           if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
147             {
148               sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
149               return SIM_RC_FAIL;
150             }
151           /* FIXME:wip: Need to set something in STATE_CONFIG.  */
152         }
153       else if (strcmp (arg, "little") == 0)
154         {
155           if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
156             {
157               sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
158               return SIM_RC_FAIL;
159             }
160           /* FIXME:wip: Need to set something in STATE_CONFIG.  */
161         }
162       else
163         {
164           sim_io_eprintf (sd, "Invalid endian specification `%s'\n", arg);
165           return SIM_RC_FAIL;
166         }
167       break;
168 #endif
169
170     case 'D' :
171       if (! WITH_DEBUG)
172         sim_io_eprintf (sd, "Debugging not compiled in, `-D' ignored\n");
173       else
174         {
175           for (n = 0; n < MAX_NR_PROCESSORS; ++n)
176             for (i = 0; i < MAX_DEBUG_VALUES; ++i)
177               CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[i] = 1;
178         }
179       break;
180
181     case OPTION_DEBUG_INSN :
182       if (! WITH_DEBUG)
183         sim_io_eprintf (sd, "Debugging not compiled in, `--debug-insn' ignored\n");
184       else
185         {
186           for (n = 0; n < MAX_NR_PROCESSORS; ++n)
187             CPU_DEBUG_FLAGS (STATE_CPU (sd, n))[DEBUG_INSN_IDX] = 1;
188         }
189       break;
190
191     case OPTION_DEBUG_FILE :
192       if (! WITH_DEBUG)
193         sim_io_eprintf (sd, "Debugging not compiled in, `--debug-file' ignored\n");
194       else
195         {
196           FILE *f = fopen (arg, "w");
197
198           if (f == NULL)
199             {
200               sim_io_eprintf (sd, "Unable to open debug output file `%s'\n", arg);
201               return SIM_RC_FAIL;
202             }
203           for (n = 0; n < MAX_NR_PROCESSORS; ++n)
204             CPU_DEBUG_FILE (STATE_CPU (sd, n)) = f;
205         }
206       break;
207
208 #ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir.  */
209     case 'h' :
210       set_h8300h (1);
211       break;
212 #endif
213
214 #ifdef SIM_HAVE_FLATMEM
215     case 'm':
216       {
217         unsigned long ul = strtol (arg, NULL, 0);
218         /* 16384: some minimal amount */
219         if (! isdigit (arg[0]) || ul < 16384)
220           {
221             sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
222             return SIM_RC_FAIL;
223           }
224         STATE_MEM_SIZE (sd) = ul;
225       }
226       break;
227 #endif
228
229     case OPTION_DO_COMMAND:
230       sim_do_command (sd, arg);
231       break;
232
233     case 'H':
234       sim_print_help (sd);
235       if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
236         exit (0);
237       /* FIXME: 'twould be nice to do something similar if gdb.  */
238       break;
239     }
240
241   return SIM_RC_OK;
242 }
243
244 /* Add the standard option list to the simulator.  */
245
246 SIM_RC
247 standard_install (SIM_DESC sd)
248 {
249   if (sim_add_option_table (sd, standard_options) != SIM_RC_OK)
250     return SIM_RC_FAIL;
251   return SIM_RC_OK;
252 }
253
254 /* Return non-zero if arg is a duplicate argument.
255    If ARG is NULL, initialize.  */
256
257 #define ARG_HASH_SIZE 97
258 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
259
260 static int
261 dup_arg_p (arg)
262      char *arg;
263 {
264   int hash;
265   static char **arg_table = NULL;
266
267   if (arg == NULL)
268     {
269       if (arg_table == NULL)
270         arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
271       memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
272       return 0;
273     }
274
275   hash = ARG_HASH (arg);
276   while (arg_table[hash] != NULL)
277     {
278       if (strcmp (arg, arg_table[hash]) == 0)
279         return 1;
280       /* We assume there won't be more than ARG_HASH_SIZE arguments so we
281          don't check if the table is full.  */
282       if (++hash == ARG_HASH_SIZE)
283         hash = 0;
284     }
285   arg_table[hash] = arg;
286   return 0;
287 }
288      
289 /* Called by sim_open to parse the arguments.  */
290
291 SIM_RC
292 sim_parse_args (sd, argv)
293      SIM_DESC sd;
294      char **argv;
295 {
296   int i, argc, num_opts;
297   char *p, *short_options;
298   /* The `val' option struct entry is dynamically assigned for options that
299      only come in the long form.  ORIG_VAL is used to get the original value
300      back.  */
301   unsigned char *orig_val;
302   struct option *lp, *long_options;
303   const struct option_list *ol;
304   const OPTION *opt;
305   OPTION_HANDLER **handlers;
306
307   /* Count the number of arguments.  */
308   for (argc = 0; argv[argc] != NULL; ++argc)
309     continue;
310
311   /* Count the number of options.  */
312   num_opts = 0;
313   for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
314     for (opt = ol->options; opt->opt.name != NULL; ++opt)
315       ++num_opts;
316
317   /* Initialize duplicate argument checker.  */
318   (void) dup_arg_p (NULL);
319
320   /* Build the option table for getopt.  */
321   long_options = (struct option *) alloca ((num_opts + 1) * sizeof (struct option));
322   lp = long_options;
323   short_options = (char *) alloca (num_opts * 3 + 1);
324   p = short_options;
325 #if 0 /* ??? necessary anymore? */
326   /* Set '+' as first char so argument permutation isn't done.  This is done
327      to workaround a problem with invoking getopt_long in run.c.: optind gets
328      decremented when the program name is reached.  */
329   *p++ = '+';
330 #endif
331   handlers = (OPTION_HANDLER **) alloca (256 * sizeof (OPTION_HANDLER *));
332   memset (handlers, 0, 256 * sizeof (OPTION_HANDLER *));
333   orig_val = (unsigned char *) alloca (256);
334   for (i = OPTION_START, ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
335     for (opt = ol->options; opt->opt.name != NULL; ++opt)
336       {
337         if (dup_arg_p (opt->opt.name))
338           continue;
339         if (opt->shortopt != 0)
340           {
341             *p++ = opt->shortopt;
342             if (opt->opt.has_arg == required_argument)
343               *p++ = ':';
344             else if (opt->opt.has_arg == optional_argument)
345               { *p++ = ':'; *p++ = ':'; }
346           }
347         *lp = opt->opt;
348         /* Dynamically assign `val' numbers for long options that don't have
349            a short option equivalent.  */
350         if (OPTION_LONG_ONLY_P (opt->opt.val))
351           lp->val = i++;
352         handlers[(unsigned char) lp->val] = opt->handler;
353         orig_val[(unsigned char) lp->val] = opt->opt.val;
354         ++lp;
355       }
356   *p = 0;
357   lp->name = NULL;
358
359   /* Ensure getopt is initialized.  */
360   optind = 0;
361   while (1)
362     {
363       int longind, optc;
364
365       optc = getopt_long (argc, argv, short_options, long_options, &longind);
366       if (optc == -1)
367         {
368           if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
369             STATE_PROG_ARGV (sd) = argv + optind;
370           break;
371         }
372       if (optc == '?')
373         return SIM_RC_FAIL;
374
375       if ((*handlers[optc]) (sd, orig_val[optc], optarg) == SIM_RC_FAIL)
376         return SIM_RC_FAIL;
377     }
378
379   return SIM_RC_OK;
380 }
381
382 /* Print help messages for the options.  */
383
384 void
385 sim_print_help (sd)
386      SIM_DESC sd;
387 {
388   const struct option_list *ol;
389   const OPTION *opt;
390
391   if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
392     sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
393                    STATE_MY_NAME (sd));
394
395   /* Initialize duplicate argument checker.  */
396   (void) dup_arg_p (NULL);
397
398   sim_io_printf (sd, "Options:\n");
399   for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
400     for (opt = ol->options; opt->opt.name != NULL; ++opt)
401       {
402         int comma, len;
403         const OPTION *o;
404
405         if (dup_arg_p (opt->opt.name))
406           continue;
407
408         if (opt->doc == NULL)
409           continue;
410
411         sim_io_printf (sd, "  ");
412
413         comma = 0;
414         len = 2;
415
416         o = opt;
417         do
418           {
419             if (o->shortopt != '\0')
420               {
421                 sim_io_printf (sd, "%s-%c", comma ? ", " : "", o->shortopt);
422                 len += (comma ? 2 : 0) + 2;
423                 if (o->arg != NULL)
424                   {
425                     if (o->opt.has_arg == optional_argument)
426                       {
427                         sim_io_printf (sd, "[%s]", o->arg);
428                         len += 1 + strlen (o->arg) + 1;
429                       }
430                     else
431                       {
432                         sim_io_printf (sd, " %s", o->arg);
433                         len += 1 + strlen (o->arg);
434                       }
435                   }
436                 comma = 1;
437               }
438             ++o;
439           }
440         while (o->opt.name != NULL && o->doc == NULL);
441
442         o = opt;
443         do
444           {
445             if (o->opt.name != NULL)
446               {
447                 sim_io_printf (sd, "%s--%s",
448                                comma ? ", " : "",
449                                o->opt.name);
450                 len += ((comma ? 2 : 0)
451                         + 2
452                         + strlen (o->opt.name));
453                 if (o->arg != NULL)
454                   {
455                     if (o->opt.has_arg == optional_argument)
456                       {
457                         sim_io_printf (sd, " [%s]", o->arg);
458                         len += 2 + strlen (o->arg) + 1;
459                       }
460                     else
461                       {
462                         sim_io_printf (sd, " %s", o->arg);
463                         len += 1 + strlen (o->arg);
464                       }
465                   }
466                 comma = 1;
467               }
468             ++o;
469           }
470         while (o->opt.name != NULL && o->doc == NULL);
471
472         if (len >= 30)
473           {
474             sim_io_printf (sd, "\n");
475             len = 0;
476           }
477
478         for (; len < 30; len++)
479           sim_io_printf (sd, " ");
480
481         sim_io_printf (sd, "%s\n", opt->doc);
482       }
483
484   if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
485     {
486       sim_io_printf (sd, "\n");
487       sim_io_printf (sd, "program args    Arguments to pass to simulated program.\n");
488       sim_io_printf (sd, "                Note: Very few simulators support this.\n");
489     }
490 }
491
492
493
494
495 SIM_RC
496 sim_args_command (sd, cmd)
497      SIM_DESC sd;
498      char *cmd;
499 {
500   /* something to do? */
501   if (cmd == NULL)
502     return SIM_RC_OK; /* FIXME - perhaphs help would be better */
503   
504   if (cmd [0] == '-')
505     {
506       /* user specified -<opt> ... form? */
507       char **argv = buildargv (cmd);
508       SIM_RC rc = sim_parse_args (sd, argv);
509       freeargv (argv);
510       return rc;
511     }
512   else
513     {
514       /* user specified <opt> form? */
515       const struct option_list *ol;
516       const OPTION *opt;
517       char **argv = buildargv (cmd);
518       for (ol = STATE_OPTIONS (sd); ol != NULL; ol = ol->next)
519         for (opt = ol->options; opt->opt.name != NULL; ++opt)
520           {
521             if (strcmp (argv[0], opt->opt.name) == 0)
522               {
523                 switch (opt->opt.has_arg)
524                   {
525                   case no_argument:
526                     if (argv[1] == NULL)
527                       opt->handler (sd, opt->opt.val, NULL);
528                     else
529                       sim_io_eprintf (sd, "Command `%s' takes no arguments\n", opt->opt.name);
530                     break;
531                   case optional_argument:
532                     if (argv[1] == NULL)
533                       opt->handler (sd, opt->opt.val, NULL);
534                     else if (argv[2] == NULL)
535                       opt->handler (sd, opt->opt.val, argv[1]);
536                     else
537                       sim_io_eprintf (sd, "Command `%s' requires no more than one argument\n", opt->opt.name);
538                     break;
539                   case required_argument:
540                     if (argv[1] == NULL)
541                       sim_io_eprintf (sd, "Command `%s' requires an argument\n", opt->opt.name);
542                     else if (argv[2] == NULL)
543                       opt->handler (sd, opt->opt.val, argv[1]);
544                     else
545                       sim_io_eprintf (sd, "Command `%s' requires only one argument\n", opt->opt.name);
546                   }
547                 return SIM_RC_OK;
548               }
549           }
550     }
551
552   /* didn't find anything that matched */
553   return SIM_RC_FAIL;
554 }