#include "interps.h"
#include "compile/compile.h"
+#include <vector>
+
/* Prototypes for local functions. */
static enum command_control_type
void (*validator)(char *, void *),
void *closure);
-static struct cleanup * setup_user_args (char *p);
-
static char *read_next_line (void);
/* Level of control structure when reading. */
/* This is to prevent certain commands being printed twice. */
static int suppress_next_print_command_trace = 0;
+/* A non-owning slice of a string. */
+
+struct string_view
+{
+ string_view (const char *str_, size_t len_)
+ : str (str_), len (len_)
+ {}
+
+ const char *str;
+ size_t len;
+};
+
/* Structure for arguments to user defined functions. */
-#define MAXUSERARGS 10
-struct user_args
+
+class user_args
+{
+public:
+ /* Save the command line and store the locations of arguments passed
+ to the user defined function. */
+ explicit user_args (const char *line);
+
+ /* Insert the stored user defined arguments into the $arg arguments
+ found in LINE. */
+ std::string insert_args (const char *line) const;
+
+private:
+ /* Disable copy/assignment. (Since the elements of A point inside
+ COMMAND, copying would need to reconstruct the A vector in the
+ new copy.) */
+ user_args (const user_args &) =delete;
+ user_args &operator= (const user_args &) =delete;
+
+ /* It is necessary to store a copy of the command line to ensure
+ that the arguments are not overwritten before they are used. */
+ std::string m_command_line;
+
+ /* The arguments. Each element points inside M_COMMAND_LINE. */
+ std::vector<string_view> m_args;
+};
+
+/* The stack of arguments passed to user defined functions. We need a
+ stack because user-defined functions can call other user-defined
+ functions. */
+static std::vector<std::unique_ptr<user_args>> user_args_stack;
+
+/* An RAII-base class used to push/pop args on the user args
+ stack. */
+struct scoped_user_args_level
+{
+ /* Parse the command line and push the arguments in the user args
+ stack. */
+ explicit scoped_user_args_level (const char *line)
{
- struct user_args *next;
- /* It is necessary to store a malloced copy of the command line to
- ensure that the arguments are not overwritten before they are
- used. */
- char *command;
- struct
- {
- char *arg;
- int len;
- }
- a[MAXUSERARGS];
- int count;
+ user_args_stack.emplace_back (new user_args (line));
}
- *user_args;
+
+ /* Pop the current user arguments from the stack. */
+ ~scoped_user_args_level ()
+ {
+ user_args_stack.pop_back ();
+ }
+};
\f
/* Return non-zero if TYPE is a multi-line command (i.e., is terminated
/* Null command */
return;
- old_chain = setup_user_args (args);
+ scoped_user_args_level push_user_args (args);
if (++user_call_depth > max_user_call_depth)
error (_("Max user call depth exceeded -- command aborted."));
- make_cleanup (do_restore_user_call_depth, &user_call_depth);
+ old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth);
/* Set the instream to 0, indicating execution of a
user-defined function. */
free_command_lines (&command);
}
-/* Cleanup */
-static void
-arg_cleanup (void *ignore)
-{
- struct user_args *oargs = user_args;
-
- if (!user_args)
- internal_error (__FILE__, __LINE__,
- _("arg_cleanup called with no user args.\n"));
-
- user_args = user_args->next;
- xfree (oargs->command);
- xfree (oargs);
-}
-
-/* Bind the incomming arguments for a user defined command to
- $arg0, $arg1 ... $argMAXUSERARGS. */
+/* Bind the incoming arguments for a user defined command to $arg0,
+ $arg1 ... $argN. */
-static struct cleanup *
-setup_user_args (char *p)
+user_args::user_args (const char *command_line)
{
- struct user_args *args;
- struct cleanup *old_chain;
- unsigned int arg_count = 0;
-
- args = XNEW (struct user_args);
- memset (args, 0, sizeof (struct user_args));
-
- args->next = user_args;
- user_args = args;
-
- old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
+ const char *p;
- if (p == NULL)
- return old_chain;
+ if (command_line == NULL)
+ return;
- user_args->command = p = xstrdup (p);
+ m_command_line = command_line;
+ p = m_command_line.c_str ();
while (*p)
{
- char *start_arg;
+ const char *start_arg;
int squote = 0;
int dquote = 0;
int bsquote = 0;
- if (arg_count >= MAXUSERARGS)
- error (_("user defined function may only have %d arguments."),
- MAXUSERARGS);
-
/* Strip whitespace. */
while (*p == ' ' || *p == '\t')
p++;
/* P now points to an argument. */
start_arg = p;
- user_args->a[arg_count].arg = p;
/* Get to the end of this argument. */
while (*p)
}
}
- user_args->a[arg_count].len = p - start_arg;
- arg_count++;
- user_args->count++;
+ m_args.emplace_back (start_arg, p - start_arg);
}
- return old_chain;
}
/* Given character string P, return a point to the first argument
{
/* If we are not in a user-defined command, treat $argc, $arg0, et
cetera as normal convenience variables. */
- if (user_args == NULL)
+ if (user_args_stack.empty ())
return line;
+ const std::unique_ptr<user_args> &args = user_args_stack.back ();
+ return args->insert_args (line);
+}
+
+/* Insert the user defined arguments stored in user_args into the $arg
+ arguments found in line. */
+
+std::string
+user_args::insert_args (const char *line) const
+{
std::string new_line;
const char *p;
while ((p = locate_arg (line)))
{
- int i, len;
-
new_line.append (line, p - line);
if (p[4] == 'c')
{
- gdb_assert (user_args->count >= 0 && user_args->count <= 10);
- if (user_args->count == 10)
- {
- new_line += '1';
- new_line += '0';
- }
- else
- new_line += user_args->count + '0';
+ new_line += std::to_string (m_args.size ());
+ line = p + 5;
}
else
{
- i = p[4] - '0';
- if (i >= user_args->count)
- error (_("Missing argument %d in user function."), i);
-
- len = user_args->a[i].len;
- if (len > 0)
- new_line.append (user_args->a[i].arg, len);
+ char *tmp;
+ unsigned long i;
+
+ errno = 0;
+ i = strtoul (p + 4, &tmp, 10);
+ if ((i == 0 && tmp == p + 4) || errno != 0)
+ line = p + 4;
+ else if (i >= m_args.size ())
+ error (_("Missing argument %ld in user function."), i);
+ else
+ {
+ new_line.append (m_args[i].str, m_args[i].len);
+ line = tmp;
+ }
}
- line = p + 5;
}
/* Don't forget the tail. */
new_line.append (line);