Home | History | Annotate | Line # | Download | only in cli
      1   1.1  christos /* GDB CLI command scripting.
      2   1.1  christos 
      3  1.11  christos    Copyright (C) 1986-2024 Free Software Foundation, Inc.
      4   1.1  christos 
      5   1.1  christos    This file is part of GDB.
      6   1.1  christos 
      7   1.1  christos    This program is free software; you can redistribute it and/or modify
      8   1.1  christos    it under the terms of the GNU General Public License as published by
      9   1.1  christos    the Free Software Foundation; either version 3 of the License, or
     10   1.1  christos    (at your option) any later version.
     11   1.1  christos 
     12   1.1  christos    This program is distributed in the hope that it will be useful,
     13   1.1  christos    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14   1.1  christos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15   1.1  christos    GNU General Public License for more details.
     16   1.1  christos 
     17   1.1  christos    You should have received a copy of the GNU General Public License
     18   1.1  christos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
     19   1.1  christos 
     20  1.11  christos #include "event-top.h"
     21   1.1  christos #include "value.h"
     22   1.1  christos #include <ctype.h>
     23   1.1  christos 
     24   1.1  christos #include "ui-out.h"
     25   1.1  christos #include "top.h"
     26  1.11  christos #include "ui.h"
     27   1.1  christos #include "breakpoint.h"
     28   1.8  christos #include "tracepoint.h"
     29   1.1  christos #include "cli/cli-cmds.h"
     30   1.1  christos #include "cli/cli-decode.h"
     31   1.1  christos #include "cli/cli-script.h"
     32   1.9  christos #include "cli/cli-style.h"
     33   1.1  christos 
     34   1.3  christos #include "extension.h"
     35   1.1  christos #include "interps.h"
     36   1.3  christos #include "compile/compile.h"
     37  1.11  christos #include <string_view>
     38   1.8  christos #include "python/python.h"
     39   1.8  christos #include "guile/guile.h"
     40   1.1  christos 
     41   1.7  christos #include <vector>
     42   1.7  christos 
     43   1.1  christos /* Prototypes for local functions.  */
     44   1.1  christos 
     45   1.1  christos static enum command_control_type
     46   1.8  christos recurse_read_control_structure
     47  1.10  christos     (read_next_line_ftype read_next_line_func,
     48   1.8  christos      struct command_line *current_cmd,
     49   1.8  christos      gdb::function_view<void (const char *)> validator);
     50   1.8  christos 
     51   1.8  christos static void do_define_command (const char *comname, int from_tty,
     52   1.8  christos 			       const counted_command_line *commands);
     53   1.1  christos 
     54  1.10  christos static void do_document_command (const char *comname, int from_tty,
     55  1.11  christos 				 const counted_command_line *commands);
     56  1.10  christos 
     57  1.10  christos static const char *read_next_line (std::string &buffer);
     58   1.1  christos 
     59   1.1  christos /* Level of control structure when reading.  */
     60   1.1  christos static int control_level;
     61   1.1  christos 
     62   1.1  christos /* Level of control structure when executing.  */
     63   1.1  christos static int command_nest_depth = 1;
     64   1.1  christos 
     65   1.1  christos /* This is to prevent certain commands being printed twice.  */
     66   1.1  christos static int suppress_next_print_command_trace = 0;
     67   1.1  christos 
     68   1.8  christos /* Command element for the 'while' command.  */
     69   1.8  christos static cmd_list_element *while_cmd_element = nullptr;
     70   1.7  christos 
     71   1.8  christos /* Command element for the 'if' command.  */
     72   1.8  christos static cmd_list_element *if_cmd_element = nullptr;
     73   1.7  christos 
     74   1.8  christos /* Command element for the 'define' command.  */
     75   1.8  christos static cmd_list_element *define_cmd_element = nullptr;
     76   1.7  christos 
     77  1.10  christos /* Command element for the 'document' command.  */
     78  1.10  christos static cmd_list_element *document_cmd_element = nullptr;
     79  1.10  christos 
     80   1.1  christos /* Structure for arguments to user defined functions.  */
     81   1.7  christos 
     82   1.7  christos class user_args
     83   1.7  christos {
     84   1.7  christos public:
     85   1.7  christos   /* Save the command line and store the locations of arguments passed
     86   1.7  christos      to the user defined function.  */
     87   1.7  christos   explicit user_args (const char *line);
     88   1.7  christos 
     89   1.7  christos   /* Insert the stored user defined arguments into the $arg arguments
     90   1.7  christos      found in LINE.  */
     91   1.7  christos   std::string insert_args (const char *line) const;
     92   1.7  christos 
     93   1.7  christos private:
     94   1.7  christos   /* Disable copy/assignment.  (Since the elements of A point inside
     95   1.7  christos      COMMAND, copying would need to reconstruct the A vector in the
     96   1.7  christos      new copy.)  */
     97   1.7  christos   user_args (const user_args &) =delete;
     98   1.7  christos   user_args &operator= (const user_args &) =delete;
     99   1.7  christos 
    100   1.7  christos   /* It is necessary to store a copy of the command line to ensure
    101   1.7  christos      that the arguments are not overwritten before they are used.  */
    102   1.7  christos   std::string m_command_line;
    103   1.7  christos 
    104   1.7  christos   /* The arguments.  Each element points inside M_COMMAND_LINE.  */
    105  1.11  christos   std::vector<std::string_view> m_args;
    106   1.7  christos };
    107   1.7  christos 
    108   1.7  christos /* The stack of arguments passed to user defined functions.  We need a
    109   1.7  christos    stack because user-defined functions can call other user-defined
    110   1.7  christos    functions.  */
    111   1.7  christos static std::vector<std::unique_ptr<user_args>> user_args_stack;
    112   1.7  christos 
    113   1.7  christos /* An RAII-base class used to push/pop args on the user args
    114   1.7  christos    stack.  */
    115   1.7  christos struct scoped_user_args_level
    116   1.7  christos {
    117   1.7  christos   /* Parse the command line and push the arguments in the user args
    118   1.7  christos      stack.  */
    119   1.7  christos   explicit scoped_user_args_level (const char *line)
    120   1.1  christos   {
    121   1.7  christos     user_args_stack.emplace_back (new user_args (line));
    122   1.7  christos   }
    123   1.7  christos 
    124   1.7  christos   /* Pop the current user arguments from the stack.  */
    125   1.7  christos   ~scoped_user_args_level ()
    126   1.7  christos   {
    127   1.7  christos     user_args_stack.pop_back ();
    128   1.1  christos   }
    129   1.7  christos };
    130   1.1  christos 
    131   1.1  christos 
    132   1.1  christos /* Return non-zero if TYPE is a multi-line command (i.e., is terminated
    134   1.1  christos    by "end").  */
    135   1.1  christos 
    136   1.1  christos static int
    137   1.1  christos multi_line_command_p (enum command_control_type type)
    138   1.1  christos {
    139   1.1  christos   switch (type)
    140   1.1  christos     {
    141   1.1  christos     case if_control:
    142   1.1  christos     case while_control:
    143   1.1  christos     case while_stepping_control:
    144   1.3  christos     case commands_control:
    145   1.1  christos     case compile_control:
    146   1.3  christos     case python_control:
    147   1.8  christos     case guile_control:
    148  1.10  christos     case define_control:
    149   1.1  christos     case document_control:
    150   1.1  christos       return 1;
    151   1.1  christos     default:
    152   1.1  christos       return 0;
    153   1.1  christos     }
    154   1.1  christos }
    155   1.1  christos 
    156   1.1  christos /* Allocate, initialize a new command line structure for one of the
    157   1.1  christos    control commands (if/while).  */
    158  1.10  christos 
    159   1.7  christos static command_line_up
    160   1.1  christos build_command_line (enum command_control_type type, const char *args)
    161   1.8  christos {
    162   1.8  christos   if (args == NULL || *args == '\0')
    163   1.8  christos     {
    164   1.8  christos       if (type == if_control)
    165   1.8  christos 	error (_("if command requires an argument."));
    166   1.8  christos       else if (type == while_control)
    167   1.8  christos 	error (_("while command requires an argument."));
    168   1.8  christos       else if (type == define_control)
    169  1.10  christos 	error (_("define command requires an argument."));
    170  1.10  christos       else if (type == document_control)
    171   1.8  christos 	error (_("document command requires an argument."));
    172   1.1  christos     }
    173   1.1  christos   gdb_assert (args != NULL);
    174  1.10  christos 
    175   1.1  christos   return command_line_up (new command_line (type, xstrdup (args)));
    176   1.1  christos }
    177   1.1  christos 
    178   1.1  christos /* Build and return a new command structure for the control commands
    179   1.1  christos    such as "if" and "while".  */
    180   1.8  christos 
    181   1.7  christos counted_command_line
    182   1.1  christos get_command_line (enum command_control_type type, const char *arg)
    183   1.1  christos {
    184  1.10  christos   /* Allocate and build a new command line structure.  */
    185   1.8  christos   counted_command_line cmd (build_command_line (type, arg).release (),
    186   1.1  christos 			    command_lines_deleter ());
    187   1.1  christos 
    188   1.8  christos   /* Read in the body of this command.  */
    189   1.1  christos   if (recurse_read_control_structure (read_next_line, cmd.get (), 0)
    190   1.1  christos       == invalid_control)
    191   1.1  christos     {
    192   1.1  christos       warning (_("Error reading in canned sequence of commands."));
    193   1.1  christos       return NULL;
    194   1.1  christos     }
    195   1.1  christos 
    196   1.1  christos   return cmd;
    197   1.1  christos }
    198   1.1  christos 
    199   1.1  christos /* Recursively print a command (including full control structures).  */
    200   1.1  christos 
    201   1.1  christos void
    202   1.1  christos print_command_lines (struct ui_out *uiout, struct command_line *cmd,
    203   1.1  christos 		     unsigned int depth)
    204   1.1  christos {
    205   1.1  christos   struct command_line *list;
    206   1.1  christos 
    207   1.1  christos   list = cmd;
    208   1.1  christos   while (list)
    209   1.1  christos     {
    210   1.7  christos       if (depth)
    211   1.1  christos 	uiout->spaces (2 * depth);
    212   1.1  christos 
    213   1.1  christos       /* A simple command, print it and continue.  */
    214   1.1  christos       if (list->control_type == simple_control)
    215   1.7  christos 	{
    216   1.7  christos 	  uiout->field_string (NULL, list->line);
    217   1.1  christos 	  uiout->text ("\n");
    218   1.1  christos 	  list = list->next;
    219   1.1  christos 	  continue;
    220   1.1  christos 	}
    221   1.1  christos 
    222  1.10  christos       /* loop_continue to jump to the start of a while loop, print it
    223   1.1  christos 	 and continue. */
    224   1.1  christos       if (list->control_type == continue_control)
    225   1.7  christos 	{
    226   1.7  christos 	  uiout->field_string (NULL, "loop_continue");
    227   1.1  christos 	  uiout->text ("\n");
    228   1.1  christos 	  list = list->next;
    229   1.1  christos 	  continue;
    230   1.1  christos 	}
    231   1.1  christos 
    232   1.1  christos       /* loop_break to break out of a while loop, print it and
    233   1.1  christos 	 continue.  */
    234   1.1  christos       if (list->control_type == break_control)
    235   1.7  christos 	{
    236   1.7  christos 	  uiout->field_string (NULL, "loop_break");
    237   1.1  christos 	  uiout->text ("\n");
    238   1.1  christos 	  list = list->next;
    239   1.1  christos 	  continue;
    240   1.1  christos 	}
    241   1.1  christos 
    242   1.1  christos       /* A while command.  Recursively print its subcommands and
    243   1.1  christos 	 continue.  */
    244   1.1  christos       if (list->control_type == while_control
    245   1.1  christos 	  || list->control_type == while_stepping_control)
    246   1.1  christos 	{
    247   1.1  christos 	  /* For while-stepping, the line includes the 'while-stepping'
    248   1.1  christos 	     token.  See comment in process_next_line for explanation.
    249   1.1  christos 	     Here, take care not print 'while-stepping' twice.  */
    250   1.7  christos 	  if (list->control_type == while_control)
    251   1.1  christos 	    uiout->field_fmt (NULL, "while %s", list->line);
    252   1.7  christos 	  else
    253   1.7  christos 	    uiout->field_string (NULL, list->line);
    254   1.8  christos 	  uiout->text ("\n");
    255   1.1  christos 	  print_command_lines (uiout, list->body_list_0.get (), depth + 1);
    256   1.7  christos 	  if (depth)
    257   1.7  christos 	    uiout->spaces (2 * depth);
    258   1.7  christos 	  uiout->field_string (NULL, "end");
    259   1.1  christos 	  uiout->text ("\n");
    260   1.1  christos 	  list = list->next;
    261   1.1  christos 	  continue;
    262   1.1  christos 	}
    263   1.1  christos 
    264   1.9  christos       /* An if command.  Recursively print both arms before
    265   1.1  christos 	 continuing.  */
    266   1.1  christos       if (list->control_type == if_control)
    267   1.7  christos 	{
    268   1.7  christos 	  uiout->field_fmt (NULL, "if %s", list->line);
    269   1.1  christos 	  uiout->text ("\n");
    270   1.8  christos 	  /* The true arm.  */
    271   1.1  christos 	  print_command_lines (uiout, list->body_list_0.get (), depth + 1);
    272   1.1  christos 
    273   1.8  christos 	  /* Show the false arm if it exists.  */
    274   1.1  christos 	  if (list->body_list_1 != nullptr)
    275   1.1  christos 	    {
    276   1.7  christos 	      if (depth)
    277   1.7  christos 		uiout->spaces (2 * depth);
    278   1.7  christos 	      uiout->field_string (NULL, "else");
    279   1.8  christos 	      uiout->text ("\n");
    280   1.1  christos 	      print_command_lines (uiout, list->body_list_1.get (), depth + 1);
    281   1.1  christos 	    }
    282   1.1  christos 
    283   1.7  christos 	  if (depth)
    284   1.7  christos 	    uiout->spaces (2 * depth);
    285   1.7  christos 	  uiout->field_string (NULL, "end");
    286   1.1  christos 	  uiout->text ("\n");
    287   1.1  christos 	  list = list->next;
    288   1.1  christos 	  continue;
    289   1.1  christos 	}
    290   1.1  christos 
    291   1.1  christos       /* A commands command.  Print the breakpoint commands and
    292   1.1  christos 	 continue.  */
    293   1.1  christos       if (list->control_type == commands_control)
    294   1.1  christos 	{
    295   1.7  christos 	  if (*(list->line))
    296   1.1  christos 	    uiout->field_fmt (NULL, "commands %s", list->line);
    297   1.7  christos 	  else
    298   1.7  christos 	    uiout->field_string (NULL, "commands");
    299   1.8  christos 	  uiout->text ("\n");
    300   1.1  christos 	  print_command_lines (uiout, list->body_list_0.get (), depth + 1);
    301   1.7  christos 	  if (depth)
    302   1.7  christos 	    uiout->spaces (2 * depth);
    303   1.7  christos 	  uiout->field_string (NULL, "end");
    304   1.1  christos 	  uiout->text ("\n");
    305   1.1  christos 	  list = list->next;
    306   1.1  christos 	  continue;
    307   1.1  christos 	}
    308   1.1  christos 
    309   1.1  christos       if (list->control_type == python_control)
    310   1.7  christos 	{
    311   1.7  christos 	  uiout->field_string (NULL, "python");
    312   1.1  christos 	  uiout->text ("\n");
    313   1.8  christos 	  /* Don't indent python code at all.  */
    314   1.1  christos 	  print_command_lines (uiout, list->body_list_0.get (), 0);
    315   1.7  christos 	  if (depth)
    316   1.7  christos 	    uiout->spaces (2 * depth);
    317   1.7  christos 	  uiout->field_string (NULL, "end");
    318   1.1  christos 	  uiout->text ("\n");
    319   1.1  christos 	  list = list->next;
    320   1.1  christos 	  continue;
    321   1.1  christos 	}
    322   1.3  christos 
    323   1.3  christos       if (list->control_type == compile_control)
    324   1.7  christos 	{
    325   1.7  christos 	  uiout->field_string (NULL, "compile expression");
    326   1.8  christos 	  uiout->text ("\n");
    327   1.3  christos 	  print_command_lines (uiout, list->body_list_0.get (), 0);
    328   1.7  christos 	  if (depth)
    329   1.7  christos 	    uiout->spaces (2 * depth);
    330   1.7  christos 	  uiout->field_string (NULL, "end");
    331   1.3  christos 	  uiout->text ("\n");
    332   1.3  christos 	  list = list->next;
    333   1.3  christos 	  continue;
    334   1.3  christos 	}
    335   1.3  christos 
    336   1.3  christos       if (list->control_type == guile_control)
    337   1.7  christos 	{
    338   1.7  christos 	  uiout->field_string (NULL, "guile");
    339   1.8  christos 	  uiout->text ("\n");
    340   1.3  christos 	  print_command_lines (uiout, list->body_list_0.get (), depth + 1);
    341   1.7  christos 	  if (depth)
    342   1.7  christos 	    uiout->spaces (2 * depth);
    343   1.7  christos 	  uiout->field_string (NULL, "end");
    344   1.3  christos 	  uiout->text ("\n");
    345   1.3  christos 	  list = list->next;
    346   1.3  christos 	  continue;
    347   1.3  christos 	}
    348   1.1  christos 
    349   1.1  christos       /* Ignore illegal command type and try next.  */
    350   1.1  christos       list = list->next;
    351   1.1  christos     }				/* while (list) */
    352   1.1  christos }
    353   1.1  christos 
    354   1.1  christos /* Handle pre-post hooks.  */
    355   1.8  christos 
    356   1.1  christos class scoped_restore_hook_in
    357   1.8  christos {
    358   1.1  christos public:
    359   1.8  christos 
    360   1.8  christos   scoped_restore_hook_in (struct cmd_list_element *c)
    361   1.8  christos     : m_cmd (c)
    362   1.8  christos   {
    363   1.8  christos   }
    364   1.8  christos 
    365   1.8  christos   ~scoped_restore_hook_in ()
    366   1.8  christos   {
    367   1.8  christos     m_cmd->hook_in = 0;
    368   1.8  christos   }
    369   1.8  christos 
    370   1.8  christos   scoped_restore_hook_in (const scoped_restore_hook_in &) = delete;
    371   1.8  christos   scoped_restore_hook_in &operator= (const scoped_restore_hook_in &) = delete;
    372   1.8  christos 
    373   1.8  christos private:
    374   1.8  christos 
    375   1.8  christos   struct cmd_list_element *m_cmd;
    376   1.1  christos };
    377   1.1  christos 
    378   1.1  christos void
    379   1.1  christos execute_cmd_pre_hook (struct cmd_list_element *c)
    380   1.1  christos {
    381   1.1  christos   if ((c->hook_pre) && (!c->hook_in))
    382   1.8  christos     {
    383   1.1  christos       scoped_restore_hook_in restore_hook (c);
    384   1.8  christos       c->hook_in = 1; /* Prevent recursive hooking.  */
    385   1.1  christos       execute_user_command (c->hook_pre, nullptr);
    386   1.1  christos     }
    387   1.1  christos }
    388   1.1  christos 
    389   1.1  christos void
    390   1.1  christos execute_cmd_post_hook (struct cmd_list_element *c)
    391   1.1  christos {
    392   1.1  christos   if ((c->hook_post) && (!c->hook_in))
    393   1.8  christos     {
    394   1.1  christos       scoped_restore_hook_in restore_hook (c);
    395   1.8  christos       c->hook_in = 1; /* Prevent recursive hooking.  */
    396   1.1  christos       execute_user_command (c->hook_post, nullptr);
    397   1.1  christos     }
    398   1.1  christos }
    399   1.8  christos 
    400   1.1  christos /* See cli-script.h.  */
    401   1.1  christos 
    402   1.8  christos void
    403   1.1  christos execute_control_commands (struct command_line *cmdlines, int from_tty)
    404   1.7  christos {
    405   1.8  christos   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
    406   1.8  christos   scoped_restore save_nesting
    407   1.1  christos     = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
    408   1.1  christos 
    409   1.1  christos   while (cmdlines)
    410   1.8  christos     {
    411   1.8  christos       enum command_control_type ret = execute_control_command (cmdlines,
    412   1.1  christos 							       from_tty);
    413   1.1  christos       if (ret != simple_control && ret != break_control)
    414   1.1  christos 	{
    415   1.1  christos 	  warning (_("Error executing canned sequence of commands."));
    416   1.1  christos 	  break;
    417   1.1  christos 	}
    418   1.1  christos       cmdlines = cmdlines->next;
    419   1.8  christos     }
    420   1.8  christos }
    421   1.8  christos 
    422   1.8  christos /* See cli-script.h.  */
    423   1.8  christos 
    424   1.8  christos std::string
    425   1.8  christos execute_control_commands_to_string (struct command_line *commands,
    426   1.8  christos 				    int from_tty)
    427  1.10  christos {
    428   1.8  christos   std::string result;
    429  1.10  christos 
    430  1.10  christos   execute_fn_to_string (result, [&] ()
    431  1.10  christos     {
    432  1.10  christos       execute_control_commands (commands, from_tty);
    433   1.8  christos     }, false);
    434  1.10  christos 
    435   1.8  christos   return result;
    436   1.8  christos }
    437   1.8  christos 
    438   1.8  christos void
    439   1.8  christos execute_user_command (struct cmd_list_element *c, const char *args)
    440   1.8  christos {
    441   1.8  christos   counted_command_line cmdlines_copy;
    442   1.8  christos 
    443   1.8  christos   /* Ensure that the user commands can't be deleted while they are
    444   1.8  christos      executing.  */
    445   1.8  christos   cmdlines_copy = c->user_commands;
    446   1.8  christos   if (cmdlines_copy == 0)
    447   1.8  christos     /* Null command */
    448   1.8  christos     return;
    449   1.8  christos   struct command_line *cmdlines = cmdlines_copy.get ();
    450   1.8  christos 
    451   1.8  christos   scoped_user_args_level push_user_args (args);
    452   1.8  christos 
    453   1.8  christos   if (user_args_stack.size () > max_user_call_depth)
    454   1.8  christos     error (_("Max user call depth exceeded -- command aborted."));
    455  1.10  christos 
    456  1.10  christos   /* Set the instream to nullptr, indicating execution of a
    457  1.10  christos      user-defined function.  */
    458  1.10  christos   scoped_restore restore_instream
    459  1.10  christos     = make_scoped_restore (&current_ui->instream, nullptr);
    460   1.8  christos 
    461   1.1  christos   execute_control_commands (cmdlines, 0);
    462   1.1  christos }
    463   1.1  christos 
    464   1.1  christos /* This function is called every time GDB prints a prompt.  It ensures
    465   1.1  christos    that errors and the like do not confuse the command tracing.  */
    466   1.1  christos 
    467   1.1  christos void
    468   1.1  christos reset_command_nest_depth (void)
    469   1.1  christos {
    470   1.1  christos   command_nest_depth = 1;
    471   1.1  christos 
    472   1.1  christos   /* Just in case.  */
    473   1.1  christos   suppress_next_print_command_trace = 0;
    474   1.1  christos }
    475   1.1  christos 
    476   1.1  christos /* Print the command, prefixed with '+' to represent the call depth.
    477   1.1  christos    This is slightly complicated because this function may be called
    478   1.1  christos    from execute_command and execute_control_command.  Unfortunately
    479   1.1  christos    execute_command also prints the top level control commands.
    480   1.1  christos    In these cases execute_command will call execute_control_command
    481   1.1  christos    via while_command or if_command.  Inner levels of 'if' and 'while'
    482   1.1  christos    are dealt with directly.  Therefore we can use these functions
    483   1.8  christos    to determine whether the command has been printed already or not.  */
    484   1.1  christos ATTRIBUTE_PRINTF (1, 2)
    485   1.8  christos void
    486   1.1  christos print_command_trace (const char *fmt, ...)
    487   1.1  christos {
    488   1.1  christos   int i;
    489   1.1  christos 
    490   1.1  christos   if (suppress_next_print_command_trace)
    491   1.1  christos     {
    492   1.1  christos       suppress_next_print_command_trace = 0;
    493   1.1  christos       return;
    494   1.1  christos     }
    495   1.1  christos 
    496   1.1  christos   if (!source_verbose && !trace_commands)
    497   1.1  christos     return;
    498   1.1  christos 
    499  1.10  christos   for (i=0; i < command_nest_depth; i++)
    500   1.1  christos     gdb_printf ("+");
    501   1.8  christos 
    502   1.8  christos   va_list args;
    503   1.8  christos 
    504  1.10  christos   va_start (args, fmt);
    505   1.8  christos   gdb_vprintf (fmt, args);
    506  1.10  christos   va_end (args);
    507   1.1  christos   gdb_puts ("\n");
    508   1.1  christos }
    509   1.8  christos 
    510   1.8  christos /* Helper for execute_control_command.  */
    511   1.8  christos 
    512   1.8  christos static enum command_control_type
    513   1.1  christos execute_control_command_1 (struct command_line *cmd, int from_tty)
    514   1.1  christos {
    515   1.1  christos   struct command_line *current;
    516   1.1  christos   int loop;
    517   1.1  christos   enum command_control_type ret;
    518   1.1  christos 
    519   1.1  christos   /* Start by assuming failure, if a problem is detected, the code
    520   1.1  christos      below will simply "break" out of the switch.  */
    521   1.1  christos   ret = invalid_control;
    522   1.1  christos 
    523   1.1  christos   switch (cmd->control_type)
    524   1.1  christos     {
    525   1.7  christos     case simple_control:
    526   1.7  christos       {
    527   1.7  christos 	/* A simple command, execute it and return.  */
    528   1.8  christos 	std::string new_line = insert_user_defined_cmd_args (cmd->line);
    529   1.7  christos 	execute_command (new_line.c_str (), from_tty);
    530   1.1  christos 	ret = cmd->control_type;
    531   1.7  christos 	break;
    532   1.1  christos       }
    533   1.1  christos 
    534   1.1  christos     case continue_control:
    535   1.1  christos       print_command_trace ("loop_continue");
    536   1.1  christos 
    537  1.10  christos       /* Return for "continue", and "break" so we can either
    538   1.1  christos 	 continue the loop at the top, or break out.  */
    539   1.1  christos       ret = cmd->control_type;
    540   1.1  christos       break;
    541   1.1  christos 
    542   1.1  christos     case break_control:
    543   1.1  christos       print_command_trace ("loop_break");
    544   1.1  christos 
    545  1.10  christos       /* Return for "continue", and "break" so we can either
    546   1.1  christos 	 continue the loop at the top, or break out.  */
    547   1.1  christos       ret = cmd->control_type;
    548   1.1  christos       break;
    549   1.1  christos 
    550   1.1  christos     case while_control:
    551   1.8  christos       {
    552   1.1  christos 	print_command_trace ("while %s", cmd->line);
    553   1.1  christos 
    554   1.7  christos 	/* Parse the loop control expression for the while statement.  */
    555   1.7  christos 	std::string new_line = insert_user_defined_cmd_args (cmd->line);
    556   1.1  christos 	expression_up expr = parse_expression (new_line.c_str ());
    557   1.1  christos 
    558   1.1  christos 	ret = simple_control;
    559   1.1  christos 	loop = 1;
    560   1.1  christos 
    561   1.1  christos 	/* Keep iterating so long as the expression is true.  */
    562   1.1  christos 	while (loop == 1)
    563  1.10  christos 	  {
    564   1.1  christos 	    bool cond_result;
    565   1.1  christos 
    566   1.1  christos 	    QUIT;
    567   1.1  christos 
    568  1.10  christos 	    /* Evaluate the expression.  */
    569  1.10  christos 	    {
    570  1.11  christos 	      scoped_value_mark mark;
    571  1.10  christos 	      value *val = expr->evaluate ();
    572  1.10  christos 	      cond_result = value_true (val);
    573   1.1  christos 	    }
    574   1.1  christos 
    575   1.1  christos 	    /* If the value is false, then break out of the loop.  */
    576   1.1  christos 	    if (!cond_result)
    577   1.1  christos 	      break;
    578   1.1  christos 
    579   1.8  christos 	    /* Execute the body of the while statement.  */
    580   1.1  christos 	    current = cmd->body_list_0.get ();
    581   1.1  christos 	    while (current)
    582   1.8  christos 	      {
    583   1.8  christos 		scoped_restore save_nesting
    584   1.8  christos 		  = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
    585   1.1  christos 		ret = execute_control_command_1 (current, from_tty);
    586   1.1  christos 
    587   1.1  christos 		/* If we got an error, or a "break" command, then stop
    588   1.1  christos 		   looping.  */
    589   1.1  christos 		if (ret == invalid_control || ret == break_control)
    590   1.1  christos 		  {
    591   1.1  christos 		    loop = 0;
    592   1.1  christos 		    break;
    593   1.1  christos 		  }
    594   1.1  christos 
    595   1.1  christos 		/* If we got a "continue" command, then restart the loop
    596   1.1  christos 		   at this point.  */
    597   1.1  christos 		if (ret == continue_control)
    598   1.1  christos 		  break;
    599   1.1  christos 
    600   1.1  christos 		/* Get the next statement.  */
    601   1.1  christos 		current = current->next;
    602   1.1  christos 	      }
    603   1.1  christos 	  }
    604   1.1  christos 
    605   1.1  christos 	/* Reset RET so that we don't recurse the break all the way down.  */
    606   1.1  christos 	if (ret == break_control)
    607   1.1  christos 	  ret = simple_control;
    608   1.1  christos 
    609   1.1  christos 	break;
    610   1.1  christos       }
    611   1.1  christos 
    612   1.1  christos     case if_control:
    613   1.8  christos       {
    614   1.1  christos 	print_command_trace ("if %s", cmd->line);
    615   1.1  christos 
    616   1.7  christos 	/* Parse the conditional for the if statement.  */
    617   1.7  christos 	std::string new_line = insert_user_defined_cmd_args (cmd->line);
    618   1.1  christos 	expression_up expr = parse_expression (new_line.c_str ());
    619   1.1  christos 
    620   1.1  christos 	current = NULL;
    621   1.1  christos 	ret = simple_control;
    622   1.1  christos 
    623  1.10  christos 	/* Evaluate the conditional.  */
    624  1.10  christos 	{
    625  1.11  christos 	  scoped_value_mark mark;
    626   1.1  christos 	  value *val = expr->evaluate ();
    627  1.10  christos 
    628  1.10  christos 	  /* Choose which arm to take commands from based on the value
    629  1.10  christos 	     of the conditional expression.  */
    630  1.10  christos 	  if (value_true (val))
    631  1.10  christos 	    current = cmd->body_list_0.get ();
    632  1.10  christos 	  else if (cmd->body_list_1 != nullptr)
    633  1.10  christos 	    current = cmd->body_list_1.get ();
    634   1.1  christos 	}
    635   1.1  christos 
    636   1.1  christos 	/* Execute commands in the given arm.  */
    637   1.1  christos 	while (current)
    638   1.8  christos 	  {
    639   1.8  christos 	    scoped_restore save_nesting
    640   1.8  christos 	      = make_scoped_restore (&command_nest_depth, command_nest_depth + 1);
    641   1.1  christos 	    ret = execute_control_command_1 (current, from_tty);
    642   1.1  christos 
    643   1.1  christos 	    /* If we got an error, get out.  */
    644   1.1  christos 	    if (ret != simple_control)
    645   1.1  christos 	      break;
    646   1.1  christos 
    647   1.1  christos 	    /* Get the next statement in the body.  */
    648   1.1  christos 	    current = current->next;
    649   1.1  christos 	  }
    650   1.1  christos 
    651   1.1  christos 	break;
    652   1.1  christos       }
    653   1.1  christos 
    654   1.1  christos     case commands_control:
    655   1.1  christos       {
    656   1.1  christos 	/* Breakpoint commands list, record the commands in the
    657   1.7  christos 	   breakpoint's command list and return.  */
    658   1.7  christos 	std::string new_line = insert_user_defined_cmd_args (cmd->line);
    659   1.1  christos 	ret = commands_from_control_command (new_line.c_str (), cmd);
    660   1.1  christos 	break;
    661   1.1  christos       }
    662   1.3  christos 
    663   1.5  christos     case compile_control:
    664   1.5  christos       eval_compile_command (cmd, NULL, cmd->control_u.compile.scope,
    665   1.3  christos 			    cmd->control_u.compile.scope_data);
    666   1.3  christos       ret = simple_control;
    667   1.3  christos       break;
    668   1.8  christos 
    669   1.8  christos     case define_control:
    670   1.8  christos       print_command_trace ("define %s", cmd->line);
    671   1.8  christos       do_define_command (cmd->line, 0, &cmd->body_list_0);
    672   1.8  christos       ret = simple_control;
    673   1.8  christos       break;
    674  1.10  christos 
    675  1.10  christos     case document_control:
    676  1.10  christos       print_command_trace ("document %s", cmd->line);
    677  1.10  christos       do_document_command (cmd->line, 0, &cmd->body_list_0);
    678  1.10  christos       ret = simple_control;
    679  1.10  christos       break;
    680   1.1  christos 
    681   1.3  christos     case python_control:
    682   1.1  christos     case guile_control:
    683   1.3  christos       {
    684   1.1  christos 	eval_ext_lang_from_control_command (cmd);
    685   1.1  christos 	ret = simple_control;
    686   1.1  christos 	break;
    687   1.1  christos       }
    688   1.1  christos 
    689   1.1  christos     default:
    690   1.1  christos       warning (_("Invalid control type in canned commands structure."));
    691   1.1  christos       break;
    692   1.1  christos     }
    693   1.1  christos 
    694   1.1  christos   return ret;
    695   1.1  christos }
    696   1.8  christos 
    697   1.8  christos enum command_control_type
    698   1.8  christos execute_control_command (struct command_line *cmd, int from_tty)
    699   1.9  christos {
    700   1.9  christos   if (!current_uiout->is_mi_like_p ())
    701   1.9  christos     return execute_control_command_1 (cmd, from_tty);
    702   1.8  christos 
    703   1.8  christos   /* Make sure we use the console uiout.  It's possible that we are executing
    704   1.8  christos      breakpoint commands while running the MI interpreter.  */
    705   1.8  christos   interp *console = interp_lookup (current_ui, INTERP_CONSOLE);
    706   1.8  christos   scoped_restore save_uiout
    707   1.8  christos     = make_scoped_restore (&current_uiout, console->interp_ui_out ());
    708   1.8  christos 
    709   1.8  christos   return execute_control_command_1 (cmd, from_tty);
    710   1.8  christos }
    711   1.1  christos 
    712   1.1  christos /* Like execute_control_command, but first set
    713   1.1  christos    suppress_next_print_command_trace.  */
    714   1.1  christos 
    715   1.1  christos enum command_control_type
    716   1.1  christos execute_control_command_untraced (struct command_line *cmd)
    717   1.1  christos {
    718   1.1  christos   suppress_next_print_command_trace = 1;
    719   1.1  christos   return execute_control_command (cmd);
    720   1.1  christos }
    721   1.1  christos 
    722   1.1  christos 
    723   1.1  christos /* "while" command support.  Executes a body of statements while the
    724   1.1  christos    loop condition is nonzero.  */
    725   1.1  christos 
    726   1.8  christos static void
    727   1.1  christos while_command (const char *arg, int from_tty)
    728   1.1  christos {
    729   1.8  christos   control_level = 1;
    730   1.1  christos   counted_command_line command = get_command_line (while_control, arg);
    731   1.1  christos 
    732   1.1  christos   if (command == NULL)
    733   1.1  christos     return;
    734   1.7  christos 
    735   1.1  christos   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
    736   1.7  christos 
    737   1.1  christos   execute_control_command_untraced (command.get ());
    738   1.1  christos }
    739   1.1  christos 
    740   1.1  christos /* "if" command support.  Execute either the true or false arm depending
    741   1.1  christos    on the value of the if conditional.  */
    742   1.1  christos 
    743   1.8  christos static void
    744   1.1  christos if_command (const char *arg, int from_tty)
    745   1.1  christos {
    746   1.8  christos   control_level = 1;
    747   1.1  christos   counted_command_line command = get_command_line (if_control, arg);
    748   1.1  christos 
    749   1.1  christos   if (command == NULL)
    750   1.1  christos     return;
    751   1.7  christos 
    752   1.1  christos   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
    753   1.7  christos 
    754   1.1  christos   execute_control_command_untraced (command.get ());
    755   1.1  christos }
    756   1.7  christos 
    757   1.7  christos /* Bind the incoming arguments for a user defined command to $arg0,
    758   1.1  christos    $arg1 ... $argN.  */
    759   1.7  christos 
    760   1.1  christos user_args::user_args (const char *command_line)
    761   1.7  christos {
    762   1.1  christos   const char *p;
    763   1.7  christos 
    764   1.7  christos   if (command_line == NULL)
    765   1.1  christos     return;
    766   1.7  christos 
    767   1.7  christos   m_command_line = command_line;
    768   1.1  christos   p = m_command_line.c_str ();
    769   1.1  christos 
    770   1.1  christos   while (*p)
    771   1.7  christos     {
    772   1.1  christos       const char *start_arg;
    773   1.1  christos       int squote = 0;
    774   1.1  christos       int dquote = 0;
    775   1.1  christos       int bsquote = 0;
    776   1.1  christos 
    777   1.1  christos       /* Strip whitespace.  */
    778   1.1  christos       while (*p == ' ' || *p == '\t')
    779   1.1  christos 	p++;
    780   1.1  christos 
    781   1.1  christos       /* P now points to an argument.  */
    782   1.1  christos       start_arg = p;
    783   1.1  christos 
    784   1.1  christos       /* Get to the end of this argument.  */
    785   1.1  christos       while (*p)
    786   1.1  christos 	{
    787   1.1  christos 	  if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
    788   1.1  christos 	    break;
    789   1.1  christos 	  else
    790   1.1  christos 	    {
    791   1.1  christos 	      if (bsquote)
    792   1.1  christos 		bsquote = 0;
    793   1.1  christos 	      else if (*p == '\\')
    794   1.1  christos 		bsquote = 1;
    795   1.1  christos 	      else if (squote)
    796   1.1  christos 		{
    797   1.1  christos 		  if (*p == '\'')
    798   1.1  christos 		    squote = 0;
    799   1.1  christos 		}
    800   1.1  christos 	      else if (dquote)
    801   1.1  christos 		{
    802   1.1  christos 		  if (*p == '"')
    803   1.1  christos 		    dquote = 0;
    804   1.1  christos 		}
    805   1.1  christos 	      else
    806   1.1  christos 		{
    807   1.1  christos 		  if (*p == '\'')
    808   1.1  christos 		    squote = 1;
    809   1.1  christos 		  else if (*p == '"')
    810   1.1  christos 		    dquote = 1;
    811   1.1  christos 		}
    812   1.1  christos 	      p++;
    813   1.1  christos 	    }
    814   1.1  christos 	}
    815   1.7  christos 
    816   1.1  christos       m_args.emplace_back (start_arg, p - start_arg);
    817   1.1  christos     }
    818   1.1  christos }
    819   1.1  christos 
    820   1.1  christos /* Given character string P, return a point to the first argument
    821   1.1  christos    ($arg), or NULL if P contains no arguments.  */
    822   1.7  christos 
    823   1.7  christos static const char *
    824   1.1  christos locate_arg (const char *p)
    825   1.1  christos {
    826   1.1  christos   while ((p = strchr (p, '$')))
    827   1.5  christos     {
    828   1.1  christos       if (startswith (p, "$arg")
    829   1.1  christos 	  && (isdigit (p[4]) || p[4] == 'c'))
    830   1.1  christos 	return p;
    831   1.1  christos       p++;
    832   1.1  christos     }
    833   1.1  christos   return NULL;
    834   1.1  christos }
    835   1.7  christos 
    836   1.1  christos /* See cli-script.h.  */
    837   1.7  christos 
    838   1.7  christos std::string
    839   1.1  christos insert_user_defined_cmd_args (const char *line)
    840   1.7  christos {
    841   1.1  christos   /* If we are not in a user-defined command, treat $argc, $arg0, et
    842   1.7  christos      cetera as normal convenience variables.  */
    843   1.7  christos   if (user_args_stack.empty ())
    844   1.1  christos     return line;
    845   1.7  christos 
    846   1.7  christos   const std::unique_ptr<user_args> &args = user_args_stack.back ();
    847   1.7  christos   return args->insert_args (line);
    848   1.1  christos }
    849   1.7  christos 
    850   1.7  christos /* Insert the user defined arguments stored in user_args into the $arg
    851   1.1  christos    arguments found in line.  */
    852   1.7  christos 
    853   1.7  christos std::string
    854   1.7  christos user_args::insert_args (const char *line) const
    855   1.7  christos {
    856   1.7  christos   std::string new_line;
    857   1.1  christos   const char *p;
    858   1.1  christos 
    859   1.1  christos   while ((p = locate_arg (line)))
    860   1.7  christos     {
    861   1.1  christos       new_line.append (line, p - line);
    862   1.1  christos 
    863   1.1  christos       if (p[4] == 'c')
    864   1.8  christos 	{
    865   1.7  christos 	  new_line += std::to_string (m_args.size ());
    866   1.1  christos 	  line = p + 5;
    867   1.1  christos 	}
    868   1.1  christos       else
    869   1.7  christos 	{
    870   1.7  christos 	  char *tmp;
    871   1.7  christos 	  unsigned long i;
    872   1.7  christos 
    873   1.7  christos 	  errno = 0;
    874   1.7  christos 	  i = strtoul (p + 4, &tmp, 10);
    875   1.7  christos 	  if ((i == 0 && tmp == p + 4) || errno != 0)
    876   1.7  christos 	    line = p + 4;
    877   1.7  christos 	  else if (i >= m_args.size ())
    878   1.7  christos 	    error (_("Missing argument %ld in user function."), i);
    879   1.1  christos 	  else
    880   1.8  christos 	    {
    881   1.7  christos 	      new_line.append (m_args[i].data (), m_args[i].length ());
    882   1.1  christos 	      line = tmp;
    883   1.1  christos 	    }
    884   1.1  christos 	}
    885   1.1  christos     }
    886   1.7  christos   /* Don't forget the tail.  */
    887   1.1  christos   new_line.append (line);
    888   1.7  christos 
    889   1.1  christos   return new_line;
    890   1.1  christos }
    891   1.1  christos 
    892   1.6  christos 
    893   1.1  christos /* Read next line from stdin.  Passed to read_command_line_1 and
    895   1.1  christos    recurse_read_control_structure whenever we need to read commands
    896   1.9  christos    from stdin.  */
    897  1.10  christos 
    898   1.1  christos static const char *
    899   1.6  christos read_next_line (std::string &buffer)
    900   1.1  christos {
    901   1.1  christos   struct ui *ui = current_ui;
    902   1.6  christos   char *prompt_ptr, control_prompt[256];
    903   1.1  christos   int i = 0;
    904   1.1  christos   int from_tty = ui->instream == ui->stdin_stream;
    905   1.1  christos 
    906   1.1  christos   if (control_level >= 254)
    907   1.1  christos     error (_("Control nesting too deep!"));
    908   1.6  christos 
    909   1.6  christos   /* Set a prompt based on the nesting of the control commands.  */
    910   1.1  christos   if (from_tty
    911   1.1  christos       || (ui->instream == 0 && deprecated_readline_hook != NULL))
    912   1.1  christos     {
    913   1.1  christos       for (i = 0; i < control_level; i++)
    914   1.1  christos 	control_prompt[i] = ' ';
    915   1.1  christos       control_prompt[i] = '>';
    916   1.1  christos       control_prompt[i + 1] = '\0';
    917   1.1  christos       prompt_ptr = (char *) &control_prompt[0];
    918   1.1  christos     }
    919   1.1  christos   else
    920  1.10  christos     prompt_ptr = NULL;
    921   1.7  christos 
    922   1.7  christos   return command_line_input (buffer, prompt_ptr, "commands");
    923   1.7  christos }
    924   1.7  christos 
    925   1.7  christos /* Given an input line P, skip the command and return a pointer to the
    926   1.7  christos    first argument.  */
    927   1.7  christos 
    928   1.7  christos static const char *
    929   1.7  christos line_first_arg (const char *p)
    930   1.7  christos {
    931   1.8  christos   const char *first_arg = p + find_command_name_length (p);
    932   1.7  christos 
    933   1.7  christos   return skip_spaces (first_arg);
    934   1.1  christos }
    935   1.1  christos 
    936   1.1  christos /* Process one input line.  If the command is an "end", return such an
    937   1.1  christos    indication to the caller.  If PARSE_COMMANDS is true, strip leading
    938   1.1  christos    whitespace (trailing whitespace is always stripped) in the line,
    939   1.1  christos    attempt to recognize GDB control commands, and also return an
    940   1.1  christos    indication if the command is an "else" or a nop.
    941   1.1  christos 
    942   1.1  christos    Otherwise, only "end" is recognized.  */
    943  1.10  christos 
    944   1.8  christos static enum misc_command_type
    945   1.8  christos process_next_line (const char *p, command_line_up *command,
    946   1.8  christos 		   int parse_commands,
    947   1.1  christos 		   gdb::function_view<void (const char *)> validator)
    948   1.8  christos 
    949   1.8  christos {
    950   1.1  christos   const char *p_end;
    951   1.1  christos   const char *p_start;
    952   1.1  christos   int not_handled = 0;
    953   1.1  christos 
    954   1.1  christos   /* Not sure what to do here.  */
    955   1.1  christos   if (p == NULL)
    956   1.1  christos     return end_command;
    957   1.1  christos 
    958   1.1  christos   /* Strip trailing whitespace.  */
    959   1.1  christos   p_end = p + strlen (p);
    960   1.1  christos   while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
    961   1.1  christos     p_end--;
    962   1.1  christos 
    963   1.1  christos   p_start = p;
    964   1.1  christos   /* Strip leading whitespace.  */
    965   1.1  christos   while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
    966   1.1  christos     p_start++;
    967   1.1  christos 
    968   1.5  christos   /* 'end' is always recognized, regardless of parse_commands value.
    969   1.1  christos      We also permit whitespace before end and after.  */
    970   1.7  christos   if (p_end - p_start == 3 && startswith (p_start, "end"))
    971   1.1  christos     return end_command;
    972   1.1  christos 
    973   1.7  christos   if (parse_commands)
    974   1.7  christos     {
    975   1.7  christos       /* Resolve command abbreviations (e.g. 'ws' for 'while-stepping').  */
    976   1.9  christos       const char *cmd_name = p;
    977   1.8  christos       struct cmd_list_element *cmd
    978   1.8  christos 	= lookup_cmd_1 (&cmd_name, cmdlist, NULL, NULL, 1);
    979   1.7  christos       cmd_name = skip_spaces (cmd_name);
    980   1.1  christos       bool inline_cmd = *cmd_name != '\0';
    981   1.1  christos 
    982   1.1  christos       /* If commands are parsed, we skip initial spaces.  Otherwise,
    983   1.1  christos 	 which is the case for Python commands and documentation
    984   1.1  christos 	 (see the 'document' command), spaces are preserved.  */
    985   1.1  christos       p = p_start;
    986   1.1  christos 
    987   1.1  christos       /* Blanks and comments don't really do anything, but we need to
    988   1.1  christos 	 distinguish them from else, end and other commands which can
    989   1.1  christos 	 be executed.  */
    990   1.1  christos       if (p_end == p || p[0] == '#')
    991   1.1  christos 	return nop_command;
    992   1.5  christos 
    993   1.1  christos       /* Is the else clause of an if control structure?  */
    994   1.1  christos       if (p_end - p == 4 && startswith (p, "else"))
    995   1.1  christos 	return else_command;
    996   1.1  christos 
    997   1.8  christos       /* Check for while, if, break, continue, etc and build a new
    998   1.1  christos 	 command line structure for them.  */
    999   1.1  christos       if (cmd == while_stepping_cmd_element)
   1000   1.1  christos 	{
   1001   1.1  christos 	  /* Because validate_actionline and encode_action lookup
   1002   1.1  christos 	     command's line as command, we need the line to
   1003   1.1  christos 	     include 'while-stepping'.
   1004   1.1  christos 
   1005   1.1  christos 	     For 'ws' alias, the command will have 'ws', not expanded
   1006   1.1  christos 	     to 'while-stepping'.  This is intentional -- we don't
   1007   1.1  christos 	     really want frontend to send a command list with 'ws',
   1008   1.1  christos 	     and next break-info returning command line with
   1009   1.1  christos 	     'while-stepping'.  This should work, but might cause the
   1010   1.1  christos 	     breakpoint to be marked as changed while it's actually
   1011   1.1  christos 	     not.  */
   1012   1.8  christos 	  *command = build_command_line (while_stepping_control, p);
   1013   1.8  christos 	}
   1014   1.8  christos       else if (cmd == while_cmd_element)
   1015   1.8  christos 	*command = build_command_line (while_control, line_first_arg (p));
   1016   1.8  christos       else if (cmd == if_cmd_element)
   1017   1.8  christos 	*command = build_command_line (if_control, line_first_arg (p));
   1018   1.8  christos       else if (cmd == commands_cmd_element)
   1019   1.8  christos 	*command = build_command_line (commands_control, line_first_arg (p));
   1020  1.10  christos       else if (cmd == define_cmd_element)
   1021  1.10  christos 	*command = build_command_line (define_control, line_first_arg (p));
   1022   1.8  christos       else if (cmd == document_cmd_element)
   1023   1.1  christos 	*command = build_command_line (document_control, line_first_arg (p));
   1024   1.1  christos       else if (cmd == python_cmd_element && !inline_cmd)
   1025   1.1  christos 	{
   1026   1.1  christos 	  /* Note that we ignore the inline "python command" form
   1027   1.1  christos 	     here.  */
   1028   1.8  christos 	  *command = build_command_line (python_control, "");
   1029   1.3  christos 	}
   1030   1.3  christos       else if (cmd == compile_cmd_element && !inline_cmd)
   1031   1.3  christos 	{
   1032   1.3  christos 	  /* Note that we ignore the inline "compile command" form
   1033   1.3  christos 	     here.  */
   1034   1.3  christos 	  *command = build_command_line (compile_control, "");
   1035   1.8  christos 	  (*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
   1036   1.3  christos 	}
   1037   1.3  christos       else if (cmd == guile_cmd_element && !inline_cmd)
   1038   1.3  christos 	{
   1039   1.3  christos 	  /* Note that we ignore the inline "guile command" form here.  */
   1040   1.5  christos 	  *command = build_command_line (guile_control, "");
   1041  1.10  christos 	}
   1042   1.5  christos       else if (p_end - p == 10 && startswith (p, "loop_break"))
   1043  1.10  christos 	*command = command_line_up (new command_line (break_control));
   1044   1.1  christos       else if (p_end - p == 13 && startswith (p, "loop_continue"))
   1045   1.1  christos 	*command = command_line_up (new command_line (continue_control));
   1046   1.1  christos       else
   1047   1.1  christos 	not_handled = 1;
   1048   1.1  christos     }
   1049   1.1  christos 
   1050   1.1  christos   if (!parse_commands || not_handled)
   1051  1.10  christos     {
   1052  1.10  christos       /* A normal command.  */
   1053   1.1  christos       *command = command_line_up (new command_line (simple_control,
   1054   1.1  christos 						    savestring (p, p_end - p)));
   1055   1.1  christos     }
   1056  1.10  christos 
   1057   1.1  christos   if (validator)
   1058   1.1  christos     validator ((*command)->line);
   1059   1.1  christos 
   1060   1.1  christos   /* Nothing special.  */
   1061   1.1  christos   return ok_command;
   1062   1.1  christos }
   1063   1.1  christos 
   1064   1.1  christos /* Recursively read in the control structures and create a
   1065   1.1  christos    command_line structure from them.  Use read_next_line_func to
   1066   1.1  christos    obtain lines of the command.  */
   1067  1.10  christos 
   1068   1.1  christos static enum command_control_type
   1069   1.8  christos recurse_read_control_structure (read_next_line_ftype read_next_line_func,
   1070   1.1  christos 				struct command_line *current_cmd,
   1071   1.1  christos 				gdb::function_view<void (const char *)> validator)
   1072   1.1  christos {
   1073  1.10  christos   enum misc_command_type val;
   1074   1.8  christos   enum command_control_type ret;
   1075  1.10  christos   struct command_line *child_tail;
   1076   1.1  christos   counted_command_line *current_body = &current_cmd->body_list_0;
   1077  1.10  christos   command_line_up next;
   1078   1.1  christos 
   1079   1.1  christos   child_tail = nullptr;
   1080   1.1  christos 
   1081   1.1  christos   /* Sanity checks.  */
   1082   1.1  christos   if (current_cmd->control_type == simple_control)
   1083   1.1  christos     error (_("Recursed on a simple control type."));
   1084   1.1  christos 
   1085   1.1  christos   /* Read lines from the input stream and build control structures.  */
   1086   1.1  christos   while (1)
   1087   1.1  christos     {
   1088  1.10  christos       dont_repeat ();
   1089  1.10  christos 
   1090  1.10  christos       std::string buffer;
   1091   1.3  christos       next = nullptr;
   1092   1.3  christos       val = process_next_line (read_next_line_func (buffer), &next,
   1093   1.3  christos 			       current_cmd->control_type != python_control
   1094   1.8  christos 			       && current_cmd->control_type != guile_control
   1095   1.1  christos 			       && current_cmd->control_type != compile_control,
   1096   1.1  christos 			       validator);
   1097   1.1  christos 
   1098   1.1  christos       /* Just skip blanks and comments.  */
   1099   1.1  christos       if (val == nop_command)
   1100   1.1  christos 	continue;
   1101   1.1  christos 
   1102   1.1  christos       if (val == end_command)
   1103   1.1  christos 	{
   1104   1.1  christos 	  if (multi_line_command_p (current_cmd->control_type))
   1105   1.1  christos 	    {
   1106   1.1  christos 	      /* Success reading an entire canned sequence of commands.  */
   1107   1.1  christos 	      ret = simple_control;
   1108   1.1  christos 	      break;
   1109   1.1  christos 	    }
   1110   1.1  christos 	  else
   1111   1.1  christos 	    {
   1112   1.1  christos 	      ret = invalid_control;
   1113   1.1  christos 	      break;
   1114   1.1  christos 	    }
   1115   1.1  christos 	}
   1116   1.1  christos 
   1117   1.1  christos       /* Not the end of a control structure.  */
   1118   1.1  christos       if (val == else_command)
   1119   1.8  christos 	{
   1120   1.1  christos 	  if (current_cmd->control_type == if_control
   1121   1.8  christos 	      && current_body == &current_cmd->body_list_0)
   1122  1.10  christos 	    {
   1123   1.1  christos 	      current_body = &current_cmd->body_list_1;
   1124   1.1  christos 	      child_tail = nullptr;
   1125   1.1  christos 	      continue;
   1126   1.1  christos 	    }
   1127   1.1  christos 	  else
   1128   1.1  christos 	    {
   1129   1.1  christos 	      ret = invalid_control;
   1130   1.1  christos 	      break;
   1131   1.1  christos 	    }
   1132  1.10  christos 	}
   1133  1.10  christos 
   1134   1.1  christos       /* Transfer ownership of NEXT to the command's body list.  */
   1135  1.10  christos       if (child_tail != nullptr)
   1136  1.10  christos 	{
   1137   1.1  christos 	  child_tail->next = next.release ();
   1138   1.1  christos 	  child_tail = child_tail->next;
   1139  1.10  christos 	}
   1140  1.10  christos       else
   1141  1.10  christos 	{
   1142  1.10  christos 	  child_tail = next.get ();
   1143  1.10  christos 	  *current_body = counted_command_line (next.release (),
   1144   1.1  christos 						command_lines_deleter ());
   1145   1.1  christos 	}
   1146  1.10  christos 
   1147  1.10  christos       /* If the latest line is another control structure, then recurse
   1148   1.1  christos 	 on it.  */
   1149   1.1  christos       if (multi_line_command_p (child_tail->control_type))
   1150  1.10  christos 	{
   1151  1.10  christos 	  control_level++;
   1152   1.8  christos 	  ret = recurse_read_control_structure (read_next_line_func,
   1153   1.1  christos 						child_tail,
   1154   1.1  christos 						validator);
   1155   1.1  christos 	  control_level--;
   1156   1.1  christos 
   1157   1.1  christos 	  if (ret != simple_control)
   1158   1.1  christos 	    break;
   1159   1.1  christos 	}
   1160   1.1  christos     }
   1161   1.1  christos 
   1162   1.1  christos   dont_repeat ();
   1163   1.1  christos 
   1164   1.1  christos   return ret;
   1165   1.1  christos }
   1166   1.1  christos 
   1167   1.1  christos /* Read lines from the input stream and accumulate them in a chain of
   1168   1.1  christos    struct command_line's, which is then returned.  For input from a
   1169   1.1  christos    terminal, the special command "end" is used to mark the end of the
   1170   1.1  christos    input, and is not included in the returned chain of commands.
   1171   1.1  christos 
   1172   1.1  christos    If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
   1173   1.1  christos    is always stripped) in the line and attempt to recognize GDB control
   1174   1.1  christos    commands.  Otherwise, only "end" is recognized.  */
   1175   1.1  christos 
   1176   1.8  christos #define END_MESSAGE "End with a line saying just \"end\"."
   1177   1.8  christos 
   1178   1.8  christos counted_command_line
   1179   1.1  christos read_command_lines (const char *prompt_arg, int from_tty, int parse_commands,
   1180  1.10  christos 		    gdb::function_view<void (const char *)> validator)
   1181   1.1  christos {
   1182   1.1  christos   if (from_tty && current_ui->input_interactive_p ())
   1183   1.1  christos     {
   1184   1.1  christos       if (deprecated_readline_begin_hook)
   1185   1.1  christos 	{
   1186   1.1  christos 	  /* Note - intentional to merge messages with no newline.  */
   1187   1.1  christos 	  (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg,
   1188   1.1  christos 					     END_MESSAGE);
   1189   1.9  christos 	}
   1190   1.1  christos       else
   1191   1.1  christos 	printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
   1192   1.1  christos     }
   1193   1.1  christos 
   1194   1.1  christos 
   1195   1.8  christos   /* Reading commands assumes the CLI behavior, so temporarily
   1196   1.1  christos      override the current interpreter with CLI.  */
   1197   1.1  christos   counted_command_line head (nullptr, command_lines_deleter ());
   1198   1.8  christos   if (current_interp_named_p (INTERP_CONSOLE))
   1199   1.1  christos     head = read_command_lines_1 (read_next_line, parse_commands,
   1200   1.1  christos 				 validator);
   1201   1.8  christos   else
   1202   1.1  christos     {
   1203   1.1  christos       scoped_restore_interp interp_restorer (INTERP_CONSOLE);
   1204   1.8  christos 
   1205   1.1  christos       head = read_command_lines_1 (read_next_line, parse_commands,
   1206   1.1  christos 				   validator);
   1207  1.10  christos     }
   1208   1.6  christos 
   1209   1.1  christos   if (from_tty && current_ui->input_interactive_p ()
   1210   1.1  christos       && deprecated_readline_end_hook)
   1211   1.1  christos     {
   1212   1.1  christos       (*deprecated_readline_end_hook) ();
   1213   1.1  christos     }
   1214   1.1  christos   return (head);
   1215   1.1  christos }
   1216   1.1  christos 
   1217   1.1  christos /* Act the same way as read_command_lines, except that each new line is
   1218   1.8  christos    obtained using READ_NEXT_LINE_FUNC.  */
   1219  1.10  christos 
   1220   1.8  christos counted_command_line
   1221   1.8  christos read_command_lines_1 (read_next_line_ftype read_next_line_func,
   1222   1.1  christos 		      int parse_commands,
   1223  1.10  christos 		      gdb::function_view<void (const char *)> validator)
   1224   1.8  christos {
   1225   1.1  christos   struct command_line *tail;
   1226   1.1  christos   counted_command_line head (nullptr, command_lines_deleter ());
   1227  1.10  christos   enum command_control_type ret;
   1228   1.1  christos   enum misc_command_type val;
   1229   1.1  christos   command_line_up next;
   1230   1.7  christos 
   1231   1.1  christos   control_level = 0;
   1232   1.1  christos   tail = NULL;
   1233   1.1  christos 
   1234   1.1  christos   while (1)
   1235  1.10  christos     {
   1236  1.10  christos       dont_repeat ();
   1237  1.10  christos 
   1238   1.8  christos       std::string buffer;
   1239   1.1  christos       val = process_next_line (read_next_line_func (buffer), &next, parse_commands,
   1240   1.1  christos 			       validator);
   1241   1.1  christos 
   1242   1.1  christos       /* Ignore blank lines or comments.  */
   1243   1.1  christos       if (val == nop_command)
   1244   1.1  christos 	continue;
   1245   1.1  christos 
   1246   1.1  christos       if (val == end_command)
   1247   1.1  christos 	{
   1248   1.1  christos 	  ret = simple_control;
   1249   1.1  christos 	  break;
   1250   1.1  christos 	}
   1251   1.1  christos 
   1252   1.1  christos       if (val != ok_command)
   1253   1.1  christos 	{
   1254   1.1  christos 	  ret = invalid_control;
   1255   1.1  christos 	  break;
   1256   1.1  christos 	}
   1257   1.1  christos 
   1258   1.1  christos       if (multi_line_command_p (next->control_type))
   1259  1.10  christos 	{
   1260   1.8  christos 	  control_level++;
   1261   1.1  christos 	  ret = recurse_read_control_structure (read_next_line_func, next.get (),
   1262   1.1  christos 						validator);
   1263   1.1  christos 	  control_level--;
   1264   1.1  christos 
   1265   1.1  christos 	  if (ret == invalid_control)
   1266   1.1  christos 	    break;
   1267  1.10  christos 	}
   1268   1.1  christos 
   1269   1.1  christos       /* Transfer ownership of NEXT to the HEAD list.  */
   1270  1.10  christos       if (tail)
   1271  1.10  christos 	{
   1272   1.1  christos 	  tail->next = next.release ();
   1273   1.1  christos 	  tail = tail->next;
   1274   1.1  christos 	}
   1275  1.10  christos       else
   1276  1.10  christos 	{
   1277  1.10  christos 	  tail = next.get ();
   1278   1.1  christos 	  head = counted_command_line (next.release (),
   1279   1.1  christos 				       command_lines_deleter ());
   1280   1.1  christos 	}
   1281   1.1  christos     }
   1282   1.1  christos 
   1283   1.7  christos   dont_repeat ();
   1284   1.7  christos 
   1285   1.1  christos   if (ret == invalid_control)
   1286   1.1  christos     return NULL;
   1287   1.1  christos 
   1288   1.1  christos   return head;
   1289   1.1  christos }
   1290   1.1  christos 
   1291   1.1  christos /* Free a chain of struct command_line's.  */
   1292   1.1  christos 
   1293   1.1  christos void
   1294   1.1  christos free_command_lines (struct command_line **lptr)
   1295   1.1  christos {
   1296   1.1  christos   struct command_line *l = *lptr;
   1297   1.1  christos   struct command_line *next;
   1298   1.1  christos 
   1299   1.1  christos   while (l)
   1300   1.8  christos     {
   1301   1.1  christos       next = l->next;
   1302   1.1  christos       delete l;
   1303   1.1  christos       l = next;
   1304   1.1  christos     }
   1305   1.1  christos   *lptr = NULL;
   1306   1.1  christos }
   1307   1.1  christos 
   1308   1.1  christos /* Validate that *COMNAME is a valid name for a command.  Return the
   1310   1.1  christos    containing command list, in case it starts with a prefix command.
   1311   1.1  christos    The prefix must already exist.  *COMNAME is advanced to point after
   1312   1.1  christos    any prefix, and a NUL character overwrites the space after the
   1313   1.8  christos    prefix.  */
   1314   1.1  christos 
   1315   1.1  christos static struct cmd_list_element **
   1316   1.8  christos validate_comname (const char **comname)
   1317   1.1  christos {
   1318   1.1  christos   struct cmd_list_element **list = &cmdlist;
   1319   1.1  christos   const char *p, *last_word;
   1320   1.1  christos 
   1321   1.1  christos   if (*comname == 0)
   1322   1.1  christos     error_no_arg (_("name of command to define"));
   1323   1.1  christos 
   1324   1.1  christos   /* Find the last word of the argument.  */
   1325   1.1  christos   p = *comname + strlen (*comname);
   1326   1.1  christos   while (p > *comname && isspace (p[-1]))
   1327   1.1  christos     p--;
   1328   1.1  christos   while (p > *comname && !isspace (p[-1]))
   1329   1.1  christos     p--;
   1330   1.1  christos   last_word = p;
   1331   1.1  christos 
   1332   1.1  christos   /* Find the corresponding command list.  */
   1333   1.1  christos   if (last_word != *comname)
   1334   1.1  christos     {
   1335   1.8  christos       struct cmd_list_element *c;
   1336   1.8  christos 
   1337   1.1  christos       /* Separate the prefix and the command.  */
   1338   1.9  christos       std::string prefix (*comname, last_word - 1);
   1339  1.10  christos       const char *tem = prefix.c_str ();
   1340   1.8  christos 
   1341   1.1  christos       c = lookup_cmd (&tem, cmdlist, "", NULL, 0, 1);
   1342  1.10  christos       if (!c->is_prefix ())
   1343   1.1  christos 	error (_("\"%s\" is not a prefix command."), prefix.c_str ());
   1344   1.1  christos 
   1345   1.1  christos       list = c->subcommands;
   1346   1.1  christos       *comname = last_word;
   1347   1.1  christos     }
   1348   1.1  christos 
   1349   1.9  christos   p = *comname;
   1350   1.1  christos   while (*p)
   1351   1.1  christos     {
   1352   1.1  christos       if (!valid_cmd_char_p (*p))
   1353   1.1  christos 	error (_("Junk in argument list: \"%s\""), p);
   1354   1.1  christos       p++;
   1355   1.1  christos     }
   1356   1.1  christos 
   1357   1.1  christos   return list;
   1358   1.1  christos }
   1359   1.8  christos 
   1360   1.1  christos /* This is just a placeholder in the command data structures.  */
   1361   1.1  christos static void
   1362   1.1  christos user_defined_command (const char *ignore, int from_tty)
   1363   1.8  christos {
   1364   1.8  christos }
   1365   1.8  christos 
   1366   1.8  christos /* Define a user-defined command.  If COMMANDS is NULL, then this is a
   1367   1.8  christos    top-level call and the commands will be read using
   1368   1.8  christos    read_command_lines.  Otherwise, it is a "define" command in an
   1369   1.1  christos    existing command and the commands are provided.  In the
   1370   1.8  christos    non-top-level case, various prompts and warnings are disabled.  */
   1371   1.8  christos 
   1372   1.1  christos static void
   1373   1.1  christos do_define_command (const char *comname, int from_tty,
   1374   1.1  christos 		   const counted_command_line *commands)
   1375   1.1  christos {
   1376   1.1  christos   enum cmd_hook_type
   1377   1.1  christos     {
   1378   1.1  christos       CMD_NO_HOOK = 0,
   1379   1.1  christos       CMD_PRE_HOOK,
   1380  1.10  christos       CMD_POST_HOOK
   1381   1.1  christos     };
   1382   1.1  christos   struct cmd_list_element *c, *newc, *hookc = 0, **list;
   1383   1.1  christos   const char *comfull;
   1384   1.1  christos   int  hook_type      = CMD_NO_HOOK;
   1385   1.1  christos   int  hook_name_size = 0;
   1386   1.1  christos 
   1387   1.1  christos #define	HOOK_STRING	"hook-"
   1388   1.1  christos #define	HOOK_LEN 5
   1389   1.1  christos #define HOOK_POST_STRING "hookpost-"
   1390   1.1  christos #define HOOK_POST_LEN    9
   1391   1.1  christos 
   1392  1.10  christos   comfull = comname;
   1393   1.1  christos   list = validate_comname (&comname);
   1394   1.8  christos 
   1395   1.1  christos   c = lookup_cmd_exact (comname, *list);
   1396   1.1  christos 
   1397   1.1  christos   if (c && commands == nullptr)
   1398   1.5  christos     {
   1399   1.9  christos       int q;
   1400   1.9  christos 
   1401   1.9  christos       if (c->theclass == class_user || c->theclass == class_alias)
   1402   1.9  christos 	{
   1403  1.10  christos 	  /* if C is a prefix command that was previously defined,
   1404   1.9  christos 	     tell the user its subcommands will be kept, and ask
   1405   1.9  christos 	     if ok to redefine the command.  */
   1406   1.9  christos 	  if (c->is_prefix ())
   1407   1.9  christos 	    q = (c->user_commands.get () == nullptr
   1408   1.9  christos 		 || query (_("Keeping subcommands of prefix command \"%s\".\n"
   1409   1.9  christos 			     "Redefine command \"%s\"? "), c->name, c->name));
   1410   1.1  christos 	  else
   1411   1.1  christos 	    q = query (_("Redefine command \"%s\"? "), c->name);
   1412   1.1  christos 	}
   1413   1.1  christos       else
   1414   1.1  christos 	q = query (_("Really redefine built-in command \"%s\"? "), c->name);
   1415   1.1  christos       if (!q)
   1416   1.1  christos 	error (_("Command \"%s\" not redefined."), c->name);
   1417   1.1  christos     }
   1418   1.1  christos 
   1419   1.1  christos   /* If this new command is a hook, then mark the command which it
   1420   1.1  christos      is hooking.  Note that we allow hooking `help' commands, so that
   1421   1.1  christos      we can hook the `stop' pseudo-command.  */
   1422   1.1  christos 
   1423   1.1  christos   if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
   1424   1.1  christos     {
   1425   1.1  christos        hook_type      = CMD_PRE_HOOK;
   1426   1.1  christos        hook_name_size = HOOK_LEN;
   1427   1.1  christos     }
   1428   1.1  christos   else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
   1429   1.1  christos     {
   1430   1.9  christos       hook_type      = CMD_POST_HOOK;
   1431   1.1  christos       hook_name_size = HOOK_POST_LEN;
   1432   1.1  christos     }
   1433  1.10  christos 
   1434  1.10  christos   if (hook_type != CMD_NO_HOOK)
   1435  1.10  christos     {
   1436   1.8  christos       /* Look up cmd it hooks.  */
   1437   1.1  christos       hookc = lookup_cmd_exact (comname + hook_name_size, *list,
   1438   1.1  christos 				/* ignore_help_classes = */ false);
   1439   1.1  christos       if (!hookc && commands == nullptr)
   1440   1.1  christos 	{
   1441   1.1  christos 	  warning (_("Your new `%s' command does not "
   1442   1.1  christos 		     "hook any existing command."),
   1443   1.1  christos 		   comfull);
   1444   1.1  christos 	  if (!query (_("Proceed? ")))
   1445   1.1  christos 	    error (_("Not confirmed."));
   1446   1.1  christos 	}
   1447   1.1  christos     }
   1448   1.8  christos 
   1449   1.8  christos   comname = xstrdup (comname);
   1450   1.8  christos 
   1451   1.8  christos   counted_command_line cmds;
   1452   1.8  christos   if (commands == nullptr)
   1453   1.8  christos     {
   1454   1.8  christos       std::string prompt
   1455   1.8  christos 	= string_printf ("Type commands for definition of \"%s\".", comfull);
   1456   1.8  christos       cmds = read_command_lines (prompt.c_str (), from_tty, 1, 0);
   1457   1.1  christos     }
   1458   1.9  christos   else
   1459  1.10  christos     cmds = *commands;
   1460  1.10  christos 
   1461   1.9  christos   {
   1462   1.9  christos     struct cmd_list_element **c_subcommands
   1463   1.9  christos       = c == nullptr ? nullptr : c->subcommands;
   1464   1.9  christos 
   1465   1.9  christos     newc = add_cmd (comname, class_user, user_defined_command,
   1466   1.9  christos 		    (c != nullptr && c->theclass == class_user)
   1467   1.9  christos 		    ? c->doc : xstrdup ("User-defined."), list);
   1468   1.9  christos     newc->user_commands = std::move (cmds);
   1469  1.10  christos 
   1470   1.9  christos     /* If we define or re-define a command that was previously defined
   1471  1.10  christos        as a prefix, keep the prefix information.  */
   1472   1.9  christos     if (c_subcommands != nullptr)
   1473   1.9  christos       {
   1474   1.9  christos 	newc->subcommands = c_subcommands;
   1475   1.9  christos 	/* allow_unknown: see explanation in equivalent logic in
   1476   1.9  christos 	   define_prefix_command ().  */
   1477   1.1  christos 	newc->allow_unknown = newc->user_commands.get () != nullptr;
   1478   1.1  christos     }
   1479   1.1  christos   }
   1480   1.1  christos 
   1481   1.1  christos   /* If this new command is a hook, then mark both commands as being
   1482   1.1  christos      tied.  */
   1483  1.10  christos   if (hookc)
   1484  1.10  christos     {
   1485  1.10  christos       switch (hook_type)
   1486  1.10  christos 	{
   1487  1.10  christos 	case CMD_PRE_HOOK:
   1488  1.10  christos 	  hookc->hook_pre  = newc;  /* Target gets hooked.  */
   1489  1.10  christos 	  newc->hookee_pre = hookc; /* We are marked as hooking target cmd.  */
   1490  1.10  christos 	  break;
   1491   1.1  christos 	case CMD_POST_HOOK:
   1492  1.10  christos 	  hookc->hook_post  = newc;  /* Target gets hooked.  */
   1493  1.10  christos 	  newc->hookee_post = hookc; /* We are marked as hooking
   1494  1.10  christos 					target cmd.  */
   1495  1.10  christos 	  break;
   1496  1.10  christos 	default:
   1497   1.1  christos 	  /* Should never come here as hookc would be 0.  */
   1498   1.1  christos 	  internal_error (_("bad switch"));
   1499   1.1  christos 	}
   1500   1.1  christos     }
   1501   1.8  christos }
   1502   1.8  christos 
   1503   1.8  christos static void
   1504   1.8  christos define_command (const char *comname, int from_tty)
   1505   1.8  christos {
   1506  1.10  christos   do_define_command (comname, from_tty, nullptr);
   1507  1.10  christos }
   1508  1.10  christos 
   1509  1.10  christos /* Document a user-defined command or user defined alias.  If COMMANDS is NULL,
   1510   1.8  christos    then this is a top-level call and the document will be read using
   1511  1.10  christos    read_command_lines.  Otherwise, it is a "document" command in an existing
   1512  1.11  christos    command and the commands are provided.  */
   1513   1.1  christos static void
   1514  1.10  christos do_document_command (const char *comname, int from_tty,
   1515   1.8  christos 		     const counted_command_line *commands)
   1516   1.1  christos {
   1517   1.1  christos   struct cmd_list_element *alias, *prefix_cmd, *c;
   1518  1.10  christos   const char *comfull;
   1519  1.10  christos 
   1520  1.10  christos   comfull = comname;
   1521  1.10  christos   validate_comname (&comname);
   1522  1.10  christos 
   1523  1.11  christos   lookup_cmd_composition (comfull, &alias, &prefix_cmd, &c);
   1524  1.11  christos   if (c == nullptr)
   1525  1.10  christos     error (_("Undefined command: \"%s\"."), comfull);
   1526  1.10  christos   else if (c == CMD_LIST_AMBIGUOUS)
   1527  1.10  christos     error (_("Ambiguous command: \"%s\"."), comfull);
   1528  1.10  christos 
   1529  1.10  christos   if (c->theclass != class_user
   1530  1.10  christos       && (alias == nullptr || alias->theclass != class_alias))
   1531  1.10  christos     {
   1532  1.10  christos       if (alias == nullptr)
   1533  1.10  christos 	error (_("Command \"%s\" is built-in."), comfull);
   1534   1.1  christos       else
   1535  1.10  christos 	error (_("Alias \"%s\" is built-in."), comfull);
   1536  1.10  christos     }
   1537  1.10  christos 
   1538  1.10  christos   /* If we found an alias of class_alias, the user is documenting this
   1539   1.1  christos      user-defined alias.  */
   1540  1.10  christos   if (alias != nullptr)
   1541   1.1  christos     c = alias;
   1542  1.10  christos 
   1543  1.10  christos   counted_command_line doclines;
   1544  1.10  christos 
   1545  1.11  christos   if (commands == nullptr)
   1546  1.10  christos     {
   1547  1.10  christos       std::string prompt
   1548  1.10  christos 	= string_printf ("Type documentation for \"%s\".", comfull);
   1549  1.10  christos       doclines = read_command_lines (prompt.c_str (), from_tty, 0, 0);
   1550   1.1  christos     }
   1551  1.10  christos   else
   1552  1.10  christos     doclines = *commands;
   1553   1.1  christos 
   1554   1.1  christos   if (c->doc_allocated)
   1555   1.1  christos     xfree ((char *) c->doc);
   1556   1.1  christos 
   1557   1.3  christos   {
   1558   1.1  christos     struct command_line *cl1;
   1559   1.7  christos     int len = 0;
   1560   1.1  christos     char *doc;
   1561   1.1  christos 
   1562   1.3  christos     for (cl1 = doclines.get (); cl1; cl1 = cl1->next)
   1563   1.3  christos       len += strlen (cl1->line) + 1;
   1564   1.1  christos 
   1565   1.7  christos     doc = (char *) xmalloc (len + 1);
   1566   1.1  christos     *doc = 0;
   1567   1.3  christos 
   1568   1.1  christos     for (cl1 = doclines.get (); cl1; cl1 = cl1->next)
   1569   1.3  christos       {
   1570   1.1  christos 	strcat (doc, cl1->line);
   1571   1.3  christos 	if (cl1->next)
   1572   1.3  christos 	  strcat (doc, "\n");
   1573  1.10  christos       }
   1574   1.1  christos 
   1575   1.1  christos     c->doc = doc;
   1576   1.9  christos     c->doc_allocated = 1;
   1577  1.10  christos   }
   1578  1.10  christos }
   1579  1.10  christos 
   1580  1.10  christos static void
   1581  1.10  christos document_command (const char *comname, int from_tty)
   1582  1.10  christos {
   1583   1.9  christos   do_document_command (comname, from_tty, nullptr);
   1584   1.9  christos }
   1585   1.9  christos 
   1586   1.9  christos /* Implementation of the "define-prefix" command.  */
   1587   1.9  christos 
   1588   1.9  christos static void
   1589   1.9  christos define_prefix_command (const char *comname, int from_tty)
   1590   1.9  christos {
   1591   1.9  christos   struct cmd_list_element *c, **list;
   1592   1.9  christos   const char *comfull;
   1593   1.9  christos 
   1594  1.10  christos   comfull = comname;
   1595   1.9  christos   list = validate_comname (&comname);
   1596   1.9  christos 
   1597   1.9  christos   c = lookup_cmd_exact (comname, *list);
   1598   1.9  christos 
   1599  1.10  christos   if (c != nullptr && c->theclass != class_user)
   1600   1.9  christos     error (_("Command \"%s\" is built-in."), comfull);
   1601   1.9  christos 
   1602   1.9  christos   if (c != nullptr && c->is_prefix ())
   1603   1.9  christos     {
   1604   1.9  christos       /* c is already a user defined prefix command.  */
   1605   1.9  christos       return;
   1606   1.9  christos     }
   1607   1.9  christos 
   1608   1.9  christos   /* If the command does not exist at all, create it.  */
   1609   1.9  christos   if (c == nullptr)
   1610   1.9  christos     {
   1611   1.9  christos       comname = xstrdup (comname);
   1612   1.9  christos       c = add_cmd (comname, class_user, user_defined_command,
   1613  1.10  christos 		   xstrdup ("User-defined."), list);
   1614   1.9  christos     }
   1615  1.10  christos 
   1616  1.10  christos   /* Allocate the c->subcommands, which marks the command as a prefix
   1617   1.9  christos      command.  */
   1618   1.9  christos   c->subcommands = new struct cmd_list_element*;
   1619   1.9  christos   *(c->subcommands) = nullptr;
   1620   1.9  christos   /* If the prefix command C is not a command, then it must be followed
   1621   1.9  christos      by known subcommands.  Otherwise, if C is also a normal command,
   1622   1.9  christos      it can be followed by C args that must not cause a 'subcommand'
   1623   1.9  christos      not recognised error, and thus we must allow unknown.  */
   1624   1.1  christos   c->allow_unknown = c->user_commands.get () != nullptr;
   1625   1.1  christos }
   1626   1.1  christos 
   1627   1.1  christos 
   1628   1.1  christos /* Used to implement source_command.  */
   1630   1.1  christos 
   1631  1.10  christos void
   1632   1.1  christos script_from_file (FILE *stream, const char *file)
   1633   1.8  christos {
   1634   1.8  christos   if (stream == NULL)
   1635   1.8  christos     internal_error (_("called with NULL file pointer!"));
   1636   1.8  christos 
   1637   1.8  christos   scoped_restore restore_line_number
   1638   1.1  christos     = make_scoped_restore (&source_line_number, 0);
   1639   1.8  christos   scoped_restore restore_file
   1640   1.1  christos     = make_scoped_restore<std::string, const std::string &> (&source_file_name,
   1641   1.9  christos 							     file);
   1642   1.8  christos 
   1643   1.8  christos   scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
   1644   1.8  christos 
   1645   1.9  christos   try
   1646   1.8  christos     {
   1647   1.8  christos       read_command_file (stream);
   1648   1.8  christos     }
   1649   1.8  christos   catch (const gdb_exception_error &e)
   1650   1.8  christos     {
   1651   1.9  christos       /* Re-throw the error, but with the file name information
   1652   1.9  christos 	 prepended.  */
   1653   1.8  christos       throw_error (e.error,
   1654   1.1  christos 		   _("%s:%d: Error in sourced command file:\n%s"),
   1655   1.1  christos 		   source_file_name.c_str (), source_line_number,
   1656   1.1  christos 		   e.what ());
   1657   1.1  christos     }
   1658   1.1  christos }
   1659   1.1  christos 
   1660   1.1  christos /* Print the definition of user command C to STREAM.  Or, if C is a
   1661   1.1  christos    prefix command, show the definitions of all user commands under C
   1662   1.1  christos    (recursively).  PREFIX and NAME combined are the name of the
   1663   1.1  christos    current command.  */
   1664   1.9  christos void
   1665   1.9  christos show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
   1666   1.9  christos 	     struct ui_file *stream)
   1667   1.9  christos {
   1668  1.10  christos   if (cli_user_command_p (c))
   1669  1.10  christos     {
   1670   1.9  christos       struct command_line *cmdlines = c->user_commands.get ();
   1671   1.9  christos 
   1672  1.10  christos       gdb_printf (stream, "User %scommand \"",
   1673   1.9  christos 		  c->is_prefix () ? "prefix" : "");
   1674   1.9  christos       fprintf_styled (stream, title_style.style (), "%s%s",
   1675   1.9  christos 		      prefix, name);
   1676  1.10  christos       gdb_printf (stream, "\":\n");
   1677   1.9  christos       if (cmdlines)
   1678   1.9  christos 	{
   1679   1.1  christos 	  print_command_lines (current_uiout, cmdlines, 1);
   1680  1.10  christos 	  gdb_puts ("\n", stream);
   1681   1.1  christos 	}
   1682  1.10  christos     }
   1683   1.1  christos 
   1684  1.10  christos   if (c->is_prefix ())
   1685  1.10  christos     {
   1686  1.10  christos       const std::string prefixname = c->prefixname ();
   1687   1.1  christos 
   1688   1.1  christos       for (c = *c->subcommands; c != NULL; c = c->next)
   1689   1.1  christos 	if (c->theclass == class_user || c->is_prefix ())
   1690   1.1  christos 	  show_user_1 (c, prefixname.c_str (), c->name, gdb_stdout);
   1691   1.9  christos     }
   1692   1.1  christos 
   1693   1.9  christos }
   1694   1.1  christos 
   1695   1.9  christos void _initialize_cli_script ();
   1696   1.9  christos void
   1697   1.9  christos _initialize_cli_script ()
   1698   1.9  christos {
   1699   1.9  christos   struct cmd_list_element *c;
   1700  1.10  christos 
   1701  1.11  christos   /* "document", "define" and "define-prefix" use command_completer,
   1702  1.10  christos      as this helps the user to either type the command name and/or
   1703  1.10  christos      its prefixes.  */
   1704   1.1  christos   document_cmd_element = add_com ("document", class_support, document_command,
   1705  1.10  christos 				  _("\
   1706   1.8  christos Document a user-defined command or user-defined alias.\n\
   1707   1.1  christos Give command or alias name as argument.  Give documentation on following lines.\n\
   1708   1.1  christos End with a line of just \"end\"."));
   1709   1.1  christos   set_cmd_completer (document_cmd_element, command_completer);
   1710   1.1  christos   define_cmd_element = add_com ("define", class_support, define_command, _("\
   1711   1.8  christos Define a new command name.  Command name is argument.\n\
   1712   1.8  christos Definition appears on following lines, one command per line.\n\
   1713   1.8  christos End with a line of just \"end\".\n\
   1714   1.9  christos Use the \"document\" command to give documentation for the new command.\n\
   1715   1.9  christos Commands defined in this way may accept an unlimited number of arguments\n\
   1716   1.9  christos accessed via $arg0 .. $argN.  $argc tells how many arguments have\n\
   1717   1.9  christos been passed."));
   1718   1.9  christos   set_cmd_completer (define_cmd_element, command_completer);
   1719   1.9  christos   c = add_com ("define-prefix", class_support, define_prefix_command,
   1720   1.9  christos 	   _("\
   1721   1.9  christos Define or mark a command as a user-defined prefix command.\n\
   1722   1.1  christos User defined prefix commands can be used as prefix commands for\n\
   1723   1.8  christos other user defined commands.\n\
   1724   1.1  christos If the command already exists, it is changed to a prefix command."));
   1725   1.1  christos   set_cmd_completer (c, command_completer);
   1726   1.1  christos 
   1727   1.1  christos   while_cmd_element = add_com ("while", class_support, while_command, _("\
   1728   1.1  christos Execute nested commands WHILE the conditional expression is non zero.\n\
   1729   1.8  christos The conditional expression must follow the word `while' and must in turn be\n\
   1730   1.1  christos followed by a new line.  The nested commands must be entered one per line,\n\
   1731   1.1  christos and should be terminated by the word `end'."));
   1732   1.1  christos 
   1733   1.1  christos   if_cmd_element = add_com ("if", class_support, if_command, _("\
   1734   1.1  christos Execute nested commands once IF the conditional expression is non zero.\n\
   1735   1.1  christos The conditional expression must follow the word `if' and must in turn be\n\
   1736                 followed by a new line.  The nested commands must be entered one per line,\n\
   1737                 and should be terminated by the word 'else' or `end'.  If an else clause\n\
   1738                 is used, the same rules apply to its nested commands as to the first ones."));
   1739                 }
   1740