]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - sim/common/sim-options.c
Update years in copyright notice for the GDB files.
[thirdparty/binutils-gdb.git] / sim / common / sim-options.c
index c01cce5426b3cb8bcf6d1036b52233625d9f06e4..73f84fb3dec02fdfd4d3927afa24282a47609751 100644 (file)
@@ -1,6 +1,5 @@
 /* Simulator option handling.
-   Copyright (C) 1996, 1997, 2004, 2007, 2008, 2009, 2010
-   Free Software Foundation, Inc.
+   Copyright (C) 1996-2013 Free Software Foundation, Inc.
    Contributed by Cygnus Support.
 
 This file is part of GDB, the GNU debugger.
@@ -419,7 +418,7 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
 
     case OPTION_ARCHITECTURE_INFO:
       {
-       const char **list = bfd_arch_list();
+       const char **list = bfd_arch_list ();
        const char **lp;
        if (list == NULL)
          abort ();
@@ -458,10 +457,14 @@ standard_option_handler (SIM_DESC sd, sim_cpu *cpu, int opt,
 
     case OPTION_SYSROOT:
       /* Don't leak memory in the odd event that there's lots of
-        --sysroot=... options.  */
-      if (simulator_sysroot[0] != '\0' && arg[0] != '\0')
+        --sysroot=... options.  We treat "" specially since this
+        is the statically initialized value and cannot free it.  */
+      if (simulator_sysroot[0] != '\0')
        free (simulator_sysroot);
-      simulator_sysroot = xstrdup (arg);
+      if (arg[0] != '\0')
+       simulator_sysroot = xstrdup (arg);
+      else
+       simulator_sysroot = "";
       break;
     }
 
@@ -515,7 +518,7 @@ dup_arg_p (const char *arg)
   arg_table[hash] = arg;
   return 0;
 }
-     
+
 /* Called by sim_open to parse the arguments.  */
 
 SIM_RC
@@ -621,7 +624,12 @@ sim_parse_args (SIM_DESC sd, char **argv)
                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++;
@@ -632,7 +640,7 @@ sim_parse_args (SIM_DESC sd, char **argv)
              }
          }
     }
-           
+
   /* Terminate the short and long option lists.  */
   *p = 0;
   lp->name = NULL;
@@ -664,11 +672,11 @@ sim_parse_args (SIM_DESC sd, char **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;
 }
 
@@ -729,7 +737,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);
          }
-       
+
        /* list any long options (aliases) for the current OPT */
        o = opt;
        do
@@ -910,13 +918,65 @@ find_match (SIM_DESC sd, sim_cpu *cpu, char *argv[], int *pargi)
   return matching_opt;
 }
 
+static char **
+complete_option_list (char **ret, size_t *cnt, const struct option_list *ol,
+                     char *text, 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, char *text, 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_args_command (SIM_DESC sd, char *cmd)
 {
   /* something to do? */
   if (cmd == NULL)
     return SIM_RC_OK; /* FIXME - perhaps help would be better */
-  
+
   if (cmd [0] == '-')
     {
       /* user specified -<opt> ... form? */
@@ -1007,7 +1067,7 @@ sim_args_command (SIM_DESC sd, char *cmd)
 
       freeargv (argv);
     }
-      
+
   /* didn't find anything that remotly matched */
   return SIM_RC_FAIL;
 }