sim: convert to bfd_endian
[deliverable/binutils-gdb.git] / sim / common / sim-options.c
index 94d0de4bd7dcb02cece16b9edc59e2b274a4bcec..e25275695fa5fc6618c6a8a61249572bf57b3e43 100644 (file)
@@ -1,22 +1,21 @@
 /* Simulator option handling.
 /* Simulator option handling.
-   Copyright (C) 1996, 1997 Free Software Foundation, Inc.
+   Copyright (C) 1996-2016 Free Software Foundation, Inc.
    Contributed by Cygnus Support.
 
 This file is part of GDB, the GNU debugger.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
    Contributed by Cygnus Support.
 
 This file is part of GDB, the GNU debugger.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
-any later version.
+the Free Software Foundation; either version 3 of the License, or
+(at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License along
-with this program; if not, write to the Free Software Foundation, Inc.,
-59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
+You should have received a copy of the GNU General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "sim-main.h"
 #ifdef HAVE_STRING_H
 
 #include "sim-main.h"
 #ifdef HAVE_STRING_H
@@ -34,6 +33,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "sim-options.h"
 #include "sim-io.h"
 #include "sim-assert.h"
 #include "sim-options.h"
 #include "sim-io.h"
 #include "sim-assert.h"
+#include "version.h"
 
 #include "bfd.h"
 
 
 #include "bfd.h"
 
@@ -100,34 +100,24 @@ typedef enum {
   OPTION_ENVIRONMENT,
   OPTION_ALIGNMENT,
   OPTION_VERBOSE,
   OPTION_ENVIRONMENT,
   OPTION_ALIGNMENT,
   OPTION_VERBOSE,
-#if defined (SIM_HAVE_BIENDIAN)
   OPTION_ENDIAN,
   OPTION_ENDIAN,
-#endif
   OPTION_DEBUG,
   OPTION_DEBUG,
-#ifdef SIM_HAVE_FLATMEM
-  OPTION_MEM_SIZE,
-#endif
   OPTION_HELP,
   OPTION_HELP,
-#ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir.  */
-  OPTION_H8300H,
-  OPTION_H8300S,
-  OPTION_H8300SX,
-#endif
+  OPTION_VERSION,
   OPTION_LOAD_LMA,
   OPTION_LOAD_VMA,
   OPTION_LOAD_LMA,
   OPTION_LOAD_VMA,
+  OPTION_SYSROOT
 } STANDARD_OPTIONS;
 
 static const OPTION standard_options[] =
 {
   { {"verbose", no_argument, NULL, OPTION_VERBOSE},
       'v', NULL, "Verbose output",
 } STANDARD_OPTIONS;
 
 static const OPTION standard_options[] =
 {
   { {"verbose", no_argument, NULL, OPTION_VERBOSE},
       'v', NULL, "Verbose output",
-      standard_option_handler },
+      standard_option_handler, NULL },
 
 
-#if defined (SIM_HAVE_BIENDIAN) /* ??? && WITH_TARGET_BYTE_ORDER == 0 */
   { {"endian", required_argument, NULL, OPTION_ENDIAN},
       'E', "big|little", "Set endianness",
   { {"endian", required_argument, NULL, OPTION_ENDIAN},
       'E', "big|little", "Set endianness",
-      standard_option_handler },
-#endif
+      standard_option_handler, NULL },
 
 #ifdef SIM_HAVE_ENVIRONMENT
   /* This option isn't supported unless all choices are supported in keeping
 
 #ifdef SIM_HAVE_ENVIRONMENT
   /* This option isn't supported unless all choices are supported in keeping
@@ -152,24 +142,6 @@ static const OPTION standard_options[] =
       '\0', "FILE NAME", "Specify debugging output file",
       standard_option_handler },
 
       '\0', "FILE NAME", "Specify debugging output file",
       standard_option_handler },
 
-#ifdef SIM_H8300 /* FIXME: Should be movable to h8300 dir.  */
-  { {"h8300h", no_argument, NULL, OPTION_H8300H},
-      'h', NULL, "Indicate the CPU is H8/300H",
-      standard_option_handler },
-  { {"h8300s", no_argument, NULL, OPTION_H8300S},
-      'S', NULL, "Indicate the CPU is H8S",
-      standard_option_handler },
-  { {"h8300sx", no_argument, NULL, OPTION_H8300SX},
-      'x', NULL, "Indicate the CPU is H8SX",
-      standard_option_handler },
-#endif
-
-#ifdef SIM_HAVE_FLATMEM
-  { {"mem-size", required_argument, NULL, OPTION_MEM_SIZE},
-      'm', "MEMORY SIZE", "Specify memory size",
-      standard_option_handler },
-#endif
-
   { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
       '\0', "COMMAND", ""/*undocumented*/,
       standard_option_handler },
   { {"do-command", required_argument, NULL, OPTION_DO_COMMAND},
       '\0', "COMMAND", ""/*undocumented*/,
       standard_option_handler },
@@ -177,6 +149,9 @@ static const OPTION standard_options[] =
   { {"help", no_argument, NULL, OPTION_HELP},
       'H', NULL, "Print help information",
       standard_option_handler },
   { {"help", no_argument, NULL, OPTION_HELP},
       'H', NULL, "Print help information",
       standard_option_handler },
+  { {"version", no_argument, NULL, OPTION_VERSION},
+      '\0', NULL, "Print version information",
+      standard_option_handler },
 
   { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
       '\0', "MACHINE", "Specify the architecture to use",
 
   { {"architecture", required_argument, NULL, OPTION_ARCHITECTURE},
       '\0', "MACHINE", "Specify the architecture to use",
@@ -192,20 +167,19 @@ static const OPTION standard_options[] =
       '\0', "BFDNAME", "Specify the object-code format for the object files",
       standard_option_handler },
 
       '\0', "BFDNAME", "Specify the object-code format for the object files",
       standard_option_handler },
 
-#ifdef SIM_HANDLES_LMA
   { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA},
       '\0', NULL,
   { {"load-lma", no_argument, NULL, OPTION_LOAD_LMA},
       '\0', NULL,
-#if SIM_HANDLES_LMA
     "Use VMA or LMA addresses when loading image (default LMA)",
     "Use VMA or LMA addresses when loading image (default LMA)",
-#else
-    "Use VMA or LMA addresses when loading image (default VMA)",
-#endif
       standard_option_handler, "load-{lma,vma}" },
   { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA},
       '\0', NULL, "", standard_option_handler,  "" },
       standard_option_handler, "load-{lma,vma}" },
   { {"load-vma", no_argument, NULL, OPTION_LOAD_VMA},
       '\0', NULL, "", standard_option_handler,  "" },
-#endif
 
 
-  { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL }
+  { {"sysroot", required_argument, NULL, OPTION_SYSROOT},
+      '\0', "SYSROOT",
+    "Root for system calls with absolute file-names and cwd at start",
+      standard_option_handler, NULL },
+
+  { {NULL, no_argument, NULL, 0}, '\0', NULL, NULL, NULL, NULL }
 };
 
 static SIM_RC
 };
 
 static SIM_RC
@@ -220,27 +194,26 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
       STATE_VERBOSE_P (sd) = 1;
       break;
 
       STATE_VERBOSE_P (sd) = 1;
       break;
 
-#ifdef SIM_HAVE_BIENDIAN
     case OPTION_ENDIAN:
       if (strcmp (arg, "big") == 0)
        {
     case OPTION_ENDIAN:
       if (strcmp (arg, "big") == 0)
        {
-         if (WITH_TARGET_BYTE_ORDER == LITTLE_ENDIAN)
+         if (WITH_TARGET_BYTE_ORDER == BFD_ENDIAN_LITTLE)
            {
              sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
              return SIM_RC_FAIL;
            }
          /* FIXME:wip: Need to set something in STATE_CONFIG.  */
            {
              sim_io_eprintf (sd, "Simulator compiled for little endian only.\n");
              return SIM_RC_FAIL;
            }
          /* FIXME:wip: Need to set something in STATE_CONFIG.  */
-         current_target_byte_order = BIG_ENDIAN;
+         current_target_byte_order = BFD_ENDIAN_BIG;
        }
       else if (strcmp (arg, "little") == 0)
        {
        }
       else if (strcmp (arg, "little") == 0)
        {
-         if (WITH_TARGET_BYTE_ORDER == BIG_ENDIAN)
+         if (WITH_TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
            {
              sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
              return SIM_RC_FAIL;
            }
          /* FIXME:wip: Need to set something in STATE_CONFIG.  */
            {
              sim_io_eprintf (sd, "Simulator compiled for big endian only.\n");
              return SIM_RC_FAIL;
            }
          /* FIXME:wip: Need to set something in STATE_CONFIG.  */
-         current_target_byte_order = LITTLE_ENDIAN;
+         current_target_byte_order = BFD_ENDIAN_LITTLE;
        }
       else
        {
        }
       else
        {
@@ -248,7 +221,6 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
          return SIM_RC_FAIL;
        }
       break;
          return SIM_RC_FAIL;
        }
       break;
-#endif
 
     case OPTION_ENVIRONMENT:
       if (strcmp (arg, "user") == 0)
 
     case OPTION_ENVIRONMENT:
       if (strcmp (arg, "user") == 0)
@@ -265,7 +237,7 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
       if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
          && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd))
        {
       if (WITH_ENVIRONMENT != ALL_ENVIRONMENT
          && WITH_ENVIRONMENT != STATE_ENVIRONMENT (sd))
        {
-         char *type;
+         const char *type;
          switch (WITH_ENVIRONMENT)
            {
            case USER_ENVIRONMENT: type = "user"; break;
          switch (WITH_ENVIRONMENT)
            {
            case USER_ENVIRONMENT: type = "user"; break;
@@ -360,33 +332,6 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
        }
       break;
 
        }
       break;
 
-#ifdef SIM_H8300 /* FIXME: Can be moved to h8300 dir.  */
-    case OPTION_H8300H:
-      set_h8300h (bfd_mach_h8300h);
-      break;
-    case OPTION_H8300S:
-      set_h8300h (bfd_mach_h8300s);
-      break;
-    case OPTION_H8300SX:
-      set_h8300h (bfd_mach_h8300sx);
-      break;
-#endif
-
-#ifdef SIM_HAVE_FLATMEM
-    case OPTION_MEM_SIZE:
-      {
-       unsigned long ul = strtol (arg, NULL, 0);
-       /* 16384: some minimal amount */
-       if (! isdigit (arg[0]) || ul < 16384)
-         {
-           sim_io_eprintf (sd, "Invalid memory size `%s'", arg);
-           return SIM_RC_FAIL;
-         }
-       STATE_MEM_SIZE (sd) = ul;
-      }
-      break;
-#endif
-
     case OPTION_DO_COMMAND:
       sim_do_command (sd, arg);
       break;
     case OPTION_DO_COMMAND:
       sim_do_command (sd, arg);
       break;
@@ -405,7 +350,7 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
 
     case OPTION_ARCHITECTURE_INFO:
       {
 
     case OPTION_ARCHITECTURE_INFO:
       {
-       const char **list = bfd_arch_list();
+       const char **list = bfd_arch_list ();
        const char **lp;
        if (list == NULL)
          abort ();
        const char **lp;
        if (list == NULL)
          abort ();
@@ -441,6 +386,24 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
        exit (0);
       /* FIXME: 'twould be nice to do something similar if gdb.  */
       break;
        exit (0);
       /* FIXME: 'twould be nice to do something similar if gdb.  */
       break;
+
+    case OPTION_VERSION:
+      sim_io_printf (sd, "GNU simulator %s%s\n", PKGVERSION, version);
+      if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
+       exit (0);
+      break;
+
+    case OPTION_SYSROOT:
+      /* Don't leak memory in the odd event that there's lots of
+        --sysroot=... options.  We treat "" specially since this
+        is the statically initialized value and cannot free it.  */
+      if (simulator_sysroot[0] != '\0')
+       free (simulator_sysroot);
+      if (arg[0] != '\0')
+       simulator_sysroot = xstrdup (arg);
+      else
+       simulator_sysroot = "";
+      break;
     }
 
   return SIM_RC_OK;
     }
 
   return SIM_RC_OK;
@@ -454,9 +417,7 @@ standard_install (SIM_DESC sd)
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
   if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK)
     return SIM_RC_FAIL;
   SIM_ASSERT (STATE_MAGIC (sd) == SIM_MAGIC_NUMBER);
   if (sim_add_option_table (sd, NULL, standard_options) != SIM_RC_OK)
     return SIM_RC_FAIL;
-#ifdef SIM_HANDLES_LMA
-  STATE_LOAD_AT_LMA_P (sd) = SIM_HANDLES_LMA;
-#endif
+  STATE_LOAD_AT_LMA_P (sd) = 1;
   return SIM_RC_OK;
 }
 
   return SIM_RC_OK;
 }
 
@@ -467,16 +428,15 @@ standard_install (SIM_DESC sd)
 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
 
 static int
 #define ARG_HASH(a) ((256 * (unsigned char) a[0] + (unsigned char) a[1]) % ARG_HASH_SIZE)
 
 static int
-dup_arg_p (arg)
-     char *arg;
+dup_arg_p (const char *arg)
 {
   int hash;
 {
   int hash;
-  static char **arg_table = NULL;
+  static const char **arg_table = NULL;
 
   if (arg == NULL)
     {
       if (arg_table == NULL)
 
   if (arg == NULL)
     {
       if (arg_table == NULL)
-       arg_table = (char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
+       arg_table = (const char **) xmalloc (ARG_HASH_SIZE * sizeof (char *));
       memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
       return 0;
     }
       memset (arg_table, 0, ARG_HASH_SIZE * sizeof (char *));
       return 0;
     }
@@ -494,13 +454,11 @@ dup_arg_p (arg)
   arg_table[hash] = arg;
   return 0;
 }
   arg_table[hash] = arg;
   return 0;
 }
-     
+
 /* Called by sim_open to parse the arguments.  */
 
 SIM_RC
 /* Called by sim_open to parse the arguments.  */
 
 SIM_RC
-sim_parse_args (sd, argv)
-     SIM_DESC sd;
-     char **argv;
+sim_parse_args (SIM_DESC sd, char **argv)
 {
   int c, i, argc, num_opts;
   char *p, *short_options;
 {
   int c, i, argc, num_opts;
   char *p, *short_options;
@@ -602,7 +560,12 @@ sim_parse_args (sd, argv)
                char *name;
                *lp = opt->opt;
                /* Prepend --<cpuname>- to the option.  */
                char *name;
                *lp = opt->opt;
                /* Prepend --<cpuname>- to the option.  */
-               asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name);
+               if (asprintf (&name, "%s-%s", CPU_NAME (cpu), lp->name) < 0)
+                 {
+                   sim_io_eprintf (sd, "internal error, out of memory");
+                   result = SIM_RC_FAIL;
+                   break;
+                 }
                lp->name = name;
                /* Dynamically assign `val' numbers for long options. */
                lp->val = i++;
                lp->name = name;
                /* Dynamically assign `val' numbers for long options. */
                lp->val = i++;
@@ -613,7 +576,7 @@ sim_parse_args (sd, argv)
              }
          }
     }
              }
          }
     }
-           
+
   /* Terminate the short and long option lists.  */
   *p = 0;
   lp->name = NULL;
   /* Terminate the short and long option lists.  */
   *p = 0;
   lp->name = NULL;
@@ -645,11 +608,11 @@ sim_parse_args (sd, argv)
        }
     }
 
        }
     }
 
-  zfree (long_options);
-  zfree (short_options);
-  zfree (handlers);
-  zfree (opt_cpu);
-  zfree (orig_val);
+  free (long_options);
+  free (short_options);
+  free (handlers);
+  free (opt_cpu);
+  free (orig_val);
   return result;
 }
 
   return result;
 }
 
@@ -710,7 +673,7 @@ print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_comm
              }
            while (OPTION_VALID_P (o) && o->doc == NULL);
          }
              }
            while (OPTION_VALID_P (o) && o->doc == NULL);
          }
-       
+
        /* list any long options (aliases) for the current OPT */
        o = opt;
        do
        /* list any long options (aliases) for the current OPT */
        o = opt;
        do
@@ -785,9 +748,7 @@ print_help (SIM_DESC sd, sim_cpu *cpu, const struct option_list *ol, int is_comm
 /* Print help messages for the options.  */
 
 void
 /* Print help messages for the options.  */
 
 void
-sim_print_help (sd, is_command)
-     SIM_DESC sd;
-     int is_command;
+sim_print_help (SIM_DESC sd, int is_command)
 {
   if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
     sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
 {
   if (STATE_OPEN_KIND (sd) == SIM_OPEN_STANDALONE)
     sim_io_printf (sd, "Usage: %s [options] program [program args]\n",
@@ -893,13 +854,65 @@ find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi)
   return matching_opt;
 }
 
   return matching_opt;
 }
 
+static char **
+complete_option_list (char **ret, size_t *cnt, const struct option_list *ol,
+                     const char *text, const char *word)
+{
+  const OPTION *opt = NULL;
+  int argi;
+  size_t len = strlen (word);
+
+  for ( ; ol != NULL; ol = ol->next)
+    for (opt = ol->options; OPTION_VALID_P (opt); ++opt)
+      {
+       const char *name = opt->opt.name;
+
+       /* A long option to match against?  */
+       if (!name)
+         continue;
+
+       /* Does this option actually match?  */
+       if (strncmp (name, word, len))
+         continue;
+
+       ret = xrealloc (ret, ++*cnt * sizeof (ret[0]));
+       ret[*cnt - 2] = xstrdup (name);
+      }
+
+  return ret;
+}
+
+/* All leading text is stored in @text, while the current word being
+   completed is stored in @word.  Trailing text of @word is not.  */
+
+char **
+sim_complete_command (SIM_DESC sd, const char *text, const char *word)
+{
+  char **ret = NULL;
+  size_t cnt = 1;
+  sim_cpu *cpu;
+
+  /* Only complete first word for now.  */
+  if (text != word)
+    return ret;
+
+  cpu = STATE_CPU (sd, 0);
+  if (cpu)
+    ret = complete_option_list (ret, &cnt, CPU_OPTIONS (cpu), text, word);
+  ret = complete_option_list (ret, &cnt, STATE_OPTIONS (sd), text, word);
+
+  if (ret)
+    ret[cnt - 1] = NULL;
+  return ret;
+}
+
 SIM_RC
 SIM_RC
-sim_args_command (SIM_DESC sd, char *cmd)
+sim_args_command (SIM_DESC sd, const char *cmd)
 {
   /* something to do? */
   if (cmd == NULL)
 {
   /* something to do? */
   if (cmd == NULL)
-    return SIM_RC_OK; /* FIXME - perhaphs help would be better */
-  
+    return SIM_RC_OK; /* FIXME - perhaps help would be better */
+
   if (cmd [0] == '-')
     {
       /* user specified -<opt> ... form? */
   if (cmd [0] == '-')
     {
       /* user specified -<opt> ... form? */
@@ -916,7 +929,10 @@ sim_args_command (SIM_DESC sd, char *cmd)
       sim_cpu *cpu;
 
       if (argv [0] == NULL)
       sim_cpu *cpu;
 
       if (argv [0] == NULL)
-       return SIM_RC_OK; /* FIXME - perhaphs help would be better */
+       {
+         freeargv (argv);
+         return SIM_RC_OK; /* FIXME - perhaps help would be better */
+       }
 
       /* First check for a cpu selector.  */
       {
 
       /* First check for a cpu selector.  */
       {
@@ -990,7 +1006,7 @@ sim_args_command (SIM_DESC sd, char *cmd)
 
       freeargv (argv);
     }
 
       freeargv (argv);
     }
-      
+
   /* didn't find anything that remotly matched */
   return SIM_RC_FAIL;
 }
   /* didn't find anything that remotly matched */
   return SIM_RC_FAIL;
 }
This page took 0.02884 seconds and 4 git commands to generate.