Introduce command_line_up
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
index 33b657d64f1ac26219bc9a8eed2c9db3001a3096..e0e27efbb55815426e85bda63e60a14c89d842bb 100644 (file)
@@ -165,27 +165,20 @@ build_command_line (enum command_control_type type, const char *args)
 /* Build and return a new command structure for the control commands
    such as "if" and "while".  */
 
-struct command_line *
+command_line_up
 get_command_line (enum command_control_type type, const char *arg)
 {
-  struct command_line *cmd;
-  struct cleanup *old_chain = NULL;
-
   /* Allocate and build a new command line structure.  */
-  cmd = build_command_line (type, arg);
-
-  old_chain = make_cleanup_free_command_lines (&cmd);
+  command_line_up cmd (build_command_line (type, arg));
 
   /* Read in the body of this command.  */
-  if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
+  if (recurse_read_control_structure (read_next_line, cmd.get (), 0, 0)
       == invalid_control)
     {
       warning (_("Error reading in canned sequence of commands."));
-      do_cleanups (old_chain);
       return NULL;
     }
 
-  discard_cleanups (old_chain);
   return cmd;
 }
 
@@ -677,18 +670,15 @@ execute_control_command_untraced (struct command_line *cmd)
 static void
 while_command (char *arg, int from_tty)
 {
-  struct command_line *command = NULL;
-
   control_level = 1;
-  command = get_command_line (while_control, arg);
+  command_line_up command = get_command_line (while_control, arg);
 
   if (command == NULL)
     return;
 
   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
 
-  execute_control_command_untraced (command);
-  free_command_lines (&command);
+  execute_control_command_untraced (command.get ());
 }
 
 /* "if" command support.  Execute either the true or false arm depending
@@ -697,19 +687,15 @@ while_command (char *arg, int from_tty)
 static void
 if_command (char *arg, int from_tty)
 {
-  struct command_line *command = NULL;
-  struct cleanup *old_chain;
-
   control_level = 1;
-  command = get_command_line (if_control, arg);
+  command_line_up command = get_command_line (if_control, arg);
 
   if (command == NULL)
     return;
 
   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
 
-  execute_control_command_untraced (command);
-  free_command_lines (&command);
+  execute_control_command_untraced (command.get ());
 }
 
 /* Bind the incoming arguments for a user defined command to $arg0,
@@ -1208,12 +1194,10 @@ restore_interp (void *arg)
 
 #define END_MESSAGE "End with a line saying just \"end\"."
 
-struct command_line *
+command_line_up
 read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
                    void (*validator)(char *, void *), void *closure)
 {
-  struct command_line *head;
-
   if (from_tty && input_interactive_p (current_ui))
     {
       if (deprecated_readline_begin_hook)
@@ -1232,6 +1216,7 @@ read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
 
   /* Reading commands assumes the CLI behavior, so temporarily
      override the current interpreter with CLI.  */
+  command_line_up head;
   if (current_interp_named_p (INTERP_CONSOLE))
     head = read_command_lines_1 (read_next_line, parse_commands,
                                 validator, closure);
@@ -1256,17 +1241,17 @@ read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
 /* Act the same way as read_command_lines, except that each new line is
    obtained using READ_NEXT_LINE_FUNC.  */
 
-struct command_line *
+command_line_up
 read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
                      void (*validator)(char *, void *), void *closure)
 {
-  struct command_line *head, *tail, *next;
-  struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
+  struct command_line *tail, *next;
+  command_line_up head;
   enum command_control_type ret;
   enum misc_command_type val;
 
   control_level = 0;
-  head = tail = NULL;
+  tail = NULL;
 
   while (1)
     {
@@ -1307,18 +1292,15 @@ read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
        }
       else
        {
-         head = next;
-         make_cleanup_free_command_lines (&head);
+         head.reset (next);
        }
       tail = next;
     }
 
   dont_repeat ();
 
-  if (ret != invalid_control)
-    discard_cleanups (old_chain);
-  else
-    do_cleanups (old_chain);
+  if (ret == invalid_control)
+    return NULL;
 
   return head;
 }
@@ -1349,19 +1331,7 @@ free_command_lines (struct command_line **lptr)
   *lptr = NULL;
 }
 
-static void
-do_free_command_lines_cleanup (void *arg)
-{
-  free_command_lines ((struct command_line **) arg);
-}
-
-struct cleanup *
-make_cleanup_free_command_lines (struct command_line **arg)
-{
-  return make_cleanup (do_free_command_lines_cleanup, arg);
-}
-
-struct command_line *
+command_line_up
 copy_command_lines (struct command_line *cmds)
 {
   struct command_line *result = NULL;
@@ -1370,7 +1340,7 @@ copy_command_lines (struct command_line *cmds)
     {
       result = XNEW (struct command_line);
 
-      result->next = copy_command_lines (cmds->next);
+      result->next = copy_command_lines (cmds->next).release ();
       result->line = xstrdup (cmds->line);
       result->control_type = cmds->control_type;
       result->body_count = cmds->body_count;
@@ -1381,13 +1351,14 @@ copy_command_lines (struct command_line *cmds)
           result->body_list = XNEWVEC (struct command_line *, cmds->body_count);
 
           for (i = 0; i < cmds->body_count; i++)
-            result->body_list[i] = copy_command_lines (cmds->body_list[i]);
+            result->body_list[i]
+             = copy_command_lines (cmds->body_list[i]).release ();
         }
       else
         result->body_list = NULL;
     }
 
-  return result;
+  return command_line_up (result);
 }
 \f
 /* Validate that *COMNAME is a valid name for a command.  Return the
@@ -1460,7 +1431,6 @@ define_command (char *comname, int from_tty)
       CMD_PRE_HOOK,
       CMD_POST_HOOK
     };
-  struct command_line *cmds;
   struct cmd_list_element *c, *newc, *hookc = 0, **list;
   char *tem, *comfull;
   const char *tem_c;
@@ -1536,7 +1506,7 @@ define_command (char *comname, int from_tty)
 
   xsnprintf (tmpbuf, sizeof (tmpbuf),
             "Type commands for definition of \"%s\".", comfull);
-  cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
+  command_line_up cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
 
   if (c && c->theclass == class_user)
     free_command_lines (&c->user_commands);
@@ -1544,7 +1514,7 @@ define_command (char *comname, int from_tty)
   newc = add_cmd (comname, class_user, user_defined_command,
                  (c && c->theclass == class_user)
                  ? c->doc : xstrdup ("User-defined."), list);
-  newc->user_commands = cmds;
+  newc->user_commands = cmds.release ();
 
   /* If this new command is a hook, then mark both commands as being
      tied.  */
@@ -1571,7 +1541,6 @@ define_command (char *comname, int from_tty)
 static void
 document_command (char *comname, int from_tty)
 {
-  struct command_line *doclines;
   struct cmd_list_element *c, **list;
   const char *tem;
   char *comfull;
@@ -1588,7 +1557,7 @@ document_command (char *comname, int from_tty)
 
   xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
             comfull);
-  doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
+  command_line_up doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
 
   if (c->doc)
     xfree ((char *) c->doc);
@@ -1598,13 +1567,13 @@ document_command (char *comname, int from_tty)
     int len = 0;
     char *doc;
 
-    for (cl1 = doclines; cl1; cl1 = cl1->next)
+    for (cl1 = doclines.get (); cl1; cl1 = cl1->next)
       len += strlen (cl1->line) + 1;
 
     doc = (char *) xmalloc (len + 1);
     *doc = 0;
 
-    for (cl1 = doclines; cl1; cl1 = cl1->next)
+    for (cl1 = doclines.get (); cl1; cl1 = cl1->next)
       {
        strcat (doc, cl1->line);
        if (cl1->next)
@@ -1613,8 +1582,6 @@ document_command (char *comname, int from_tty)
 
     c->doc = doc;
   }
-
-  free_command_lines (&doclines);
 }
 \f
 struct source_cleanup_lines_args
This page took 0.031007 seconds and 4 git commands to generate.