GDB (API)
/home/stan/gdb/src/gdb/cli/cli-script.c
Go to the documentation of this file.
00001 /* GDB CLI command scripting.
00002 
00003    Copyright (C) 1986-2013 Free Software Foundation, Inc.
00004 
00005    This file is part of GDB.
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 3 of the License, or
00010    (at your option) any later version.
00011 
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016 
00017    You should have received a copy of the GNU General Public License
00018    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00019 
00020 #include "defs.h"
00021 #include "value.h"
00022 #include "language.h"           /* For value_true */
00023 #include <ctype.h>
00024 
00025 #include "ui-out.h"
00026 #include "gdb_string.h"
00027 #include "exceptions.h"
00028 #include "top.h"
00029 #include "breakpoint.h"
00030 #include "cli/cli-cmds.h"
00031 #include "cli/cli-decode.h"
00032 #include "cli/cli-script.h"
00033 #include "gdb_assert.h"
00034 
00035 #include "python/python.h"
00036 #include "interps.h"
00037 
00038 /* Prototypes for local functions.  */
00039 
00040 static enum command_control_type
00041 recurse_read_control_structure (char * (*read_next_line_func) (void),
00042                                 struct command_line *current_cmd,
00043                                 void (*validator)(char *, void *),
00044                                 void *closure);
00045 
00046 static char *insert_args (char *line);
00047 
00048 static struct cleanup * setup_user_args (char *p);
00049 
00050 static char *read_next_line (void);
00051 
00052 /* Level of control structure when reading.  */
00053 static int control_level;
00054 
00055 /* Level of control structure when executing.  */
00056 static int command_nest_depth = 1;
00057 
00058 /* This is to prevent certain commands being printed twice.  */
00059 static int suppress_next_print_command_trace = 0;
00060 
00061 /* Structure for arguments to user defined functions.  */
00062 #define MAXUSERARGS 10
00063 struct user_args
00064   {
00065     struct user_args *next;
00066     /* It is necessary to store a malloced copy of the command line to
00067        ensure that the arguments are not overwritten before they are
00068        used.  */
00069     char *command;
00070     struct
00071       {
00072         char *arg;
00073         int len;
00074       }
00075     a[MAXUSERARGS];
00076     int count;
00077   }
00078  *user_args;
00079 
00080 
00081 /* Allocate, initialize a new command line structure for one of the
00082    control commands (if/while).  */
00083 
00084 static struct command_line *
00085 build_command_line (enum command_control_type type, char *args)
00086 {
00087   struct command_line *cmd;
00088 
00089   if (args == NULL && (type == if_control || type == while_control))
00090     error (_("if/while commands require arguments."));
00091   gdb_assert (args != NULL);
00092 
00093   cmd = (struct command_line *) xmalloc (sizeof (struct command_line));
00094   cmd->next = NULL;
00095   cmd->control_type = type;
00096 
00097   cmd->body_count = 1;
00098   cmd->body_list
00099     = (struct command_line **) xmalloc (sizeof (struct command_line *)
00100                                         * cmd->body_count);
00101   memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count);
00102   cmd->line = xstrdup (args);
00103 
00104   return cmd;
00105 }
00106 
00107 /* Build and return a new command structure for the control commands
00108    such as "if" and "while".  */
00109 
00110 struct command_line *
00111 get_command_line (enum command_control_type type, char *arg)
00112 {
00113   struct command_line *cmd;
00114   struct cleanup *old_chain = NULL;
00115 
00116   /* Allocate and build a new command line structure.  */
00117   cmd = build_command_line (type, arg);
00118 
00119   old_chain = make_cleanup_free_command_lines (&cmd);
00120 
00121   /* Read in the body of this command.  */
00122   if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
00123       == invalid_control)
00124     {
00125       warning (_("Error reading in canned sequence of commands."));
00126       do_cleanups (old_chain);
00127       return NULL;
00128     }
00129 
00130   discard_cleanups (old_chain);
00131   return cmd;
00132 }
00133 
00134 /* Recursively print a command (including full control structures).  */
00135 
00136 void
00137 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
00138                      unsigned int depth)
00139 {
00140   struct command_line *list;
00141 
00142   list = cmd;
00143   while (list)
00144     {
00145       if (depth)
00146         ui_out_spaces (uiout, 2 * depth);
00147 
00148       /* A simple command, print it and continue.  */
00149       if (list->control_type == simple_control)
00150         {
00151           ui_out_field_string (uiout, NULL, list->line);
00152           ui_out_text (uiout, "\n");
00153           list = list->next;
00154           continue;
00155         }
00156 
00157       /* loop_continue to jump to the start of a while loop, print it
00158          and continue. */
00159       if (list->control_type == continue_control)
00160         {
00161           ui_out_field_string (uiout, NULL, "loop_continue");
00162           ui_out_text (uiout, "\n");
00163           list = list->next;
00164           continue;
00165         }
00166 
00167       /* loop_break to break out of a while loop, print it and
00168          continue.  */
00169       if (list->control_type == break_control)
00170         {
00171           ui_out_field_string (uiout, NULL, "loop_break");
00172           ui_out_text (uiout, "\n");
00173           list = list->next;
00174           continue;
00175         }
00176 
00177       /* A while command.  Recursively print its subcommands and
00178          continue.  */
00179       if (list->control_type == while_control
00180           || list->control_type == while_stepping_control)
00181         {
00182           /* For while-stepping, the line includes the 'while-stepping'
00183              token.  See comment in process_next_line for explanation.
00184              Here, take care not print 'while-stepping' twice.  */
00185           if (list->control_type == while_control)
00186             ui_out_field_fmt (uiout, NULL, "while %s", list->line);
00187           else
00188             ui_out_field_string (uiout, NULL, list->line);
00189           ui_out_text (uiout, "\n");
00190           print_command_lines (uiout, *list->body_list, depth + 1);
00191           if (depth)
00192             ui_out_spaces (uiout, 2 * depth);
00193           ui_out_field_string (uiout, NULL, "end");
00194           ui_out_text (uiout, "\n");
00195           list = list->next;
00196           continue;
00197         }
00198 
00199       /* An if command.  Recursively print both arms before
00200          continueing.  */
00201       if (list->control_type == if_control)
00202         {
00203           ui_out_field_fmt (uiout, NULL, "if %s", list->line);
00204           ui_out_text (uiout, "\n");
00205           /* The true arm.  */
00206           print_command_lines (uiout, list->body_list[0], depth + 1);
00207 
00208           /* Show the false arm if it exists.  */
00209           if (list->body_count == 2)
00210             {
00211               if (depth)
00212                 ui_out_spaces (uiout, 2 * depth);
00213               ui_out_field_string (uiout, NULL, "else");
00214               ui_out_text (uiout, "\n");
00215               print_command_lines (uiout, list->body_list[1], depth + 1);
00216             }
00217 
00218           if (depth)
00219             ui_out_spaces (uiout, 2 * depth);
00220           ui_out_field_string (uiout, NULL, "end");
00221           ui_out_text (uiout, "\n");
00222           list = list->next;
00223           continue;
00224         }
00225 
00226       /* A commands command.  Print the breakpoint commands and
00227          continue.  */
00228       if (list->control_type == commands_control)
00229         {
00230           if (*(list->line))
00231             ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
00232           else
00233             ui_out_field_string (uiout, NULL, "commands");
00234           ui_out_text (uiout, "\n");
00235           print_command_lines (uiout, *list->body_list, depth + 1);
00236           if (depth)
00237             ui_out_spaces (uiout, 2 * depth);
00238           ui_out_field_string (uiout, NULL, "end");
00239           ui_out_text (uiout, "\n");
00240           list = list->next;
00241           continue;
00242         }
00243 
00244       if (list->control_type == python_control)
00245         {
00246           ui_out_field_string (uiout, NULL, "python");
00247           ui_out_text (uiout, "\n");
00248           /* Don't indent python code at all.  */
00249           print_command_lines (uiout, *list->body_list, 0);
00250           if (depth)
00251             ui_out_spaces (uiout, 2 * depth);
00252           ui_out_field_string (uiout, NULL, "end");
00253           ui_out_text (uiout, "\n");
00254           list = list->next;
00255           continue;
00256         }
00257 
00258       /* Ignore illegal command type and try next.  */
00259       list = list->next;
00260     }                           /* while (list) */
00261 }
00262 
00263 /* Handle pre-post hooks.  */
00264 
00265 static void
00266 clear_hook_in_cleanup (void *data)
00267 {
00268   struct cmd_list_element *c = data;
00269 
00270   c->hook_in = 0; /* Allow hook to work again once it is complete.  */
00271 }
00272 
00273 void
00274 execute_cmd_pre_hook (struct cmd_list_element *c)
00275 {
00276   if ((c->hook_pre) && (!c->hook_in))
00277     {
00278       struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
00279       c->hook_in = 1; /* Prevent recursive hooking.  */
00280       execute_user_command (c->hook_pre, (char *) 0);
00281       do_cleanups (cleanups);
00282     }
00283 }
00284 
00285 void
00286 execute_cmd_post_hook (struct cmd_list_element *c)
00287 {
00288   if ((c->hook_post) && (!c->hook_in))
00289     {
00290       struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
00291 
00292       c->hook_in = 1; /* Prevent recursive hooking.  */
00293       execute_user_command (c->hook_post, (char *) 0);
00294       do_cleanups (cleanups);
00295     }
00296 }
00297 
00298 /* Execute the command in CMD.  */
00299 static void
00300 do_restore_user_call_depth (void * call_depth)
00301 {       
00302   int *depth = call_depth;
00303 
00304   (*depth)--;
00305   if ((*depth) == 0)
00306     in_user_command = 0;
00307 }
00308 
00309 
00310 void
00311 execute_user_command (struct cmd_list_element *c, char *args)
00312 {
00313   struct command_line *cmdlines;
00314   struct cleanup *old_chain;
00315   enum command_control_type ret;
00316   static int user_call_depth = 0;
00317   extern unsigned int max_user_call_depth;
00318 
00319   cmdlines = c->user_commands;
00320   if (cmdlines == 0)
00321     /* Null command */
00322     return;
00323 
00324   old_chain = setup_user_args (args);
00325 
00326   if (++user_call_depth > max_user_call_depth)
00327     error (_("Max user call depth exceeded -- command aborted."));
00328 
00329   make_cleanup (do_restore_user_call_depth, &user_call_depth);
00330 
00331   /* Set the instream to 0, indicating execution of a
00332      user-defined function.  */
00333   make_cleanup (do_restore_instream_cleanup, instream);
00334   instream = (FILE *) 0;
00335 
00336   /* Also set the global in_user_command, so that NULL instream is
00337      not confused with Insight.  */
00338   in_user_command = 1;
00339 
00340   make_cleanup_restore_integer (&interpreter_async);
00341   interpreter_async = 0;
00342 
00343   command_nest_depth++;
00344   while (cmdlines)
00345     {
00346       ret = execute_control_command (cmdlines);
00347       if (ret != simple_control && ret != break_control)
00348         {
00349           warning (_("Error executing canned sequence of commands."));
00350           break;
00351         }
00352       cmdlines = cmdlines->next;
00353     }
00354   command_nest_depth--;
00355   do_cleanups (old_chain);
00356 }
00357 
00358 /* This function is called every time GDB prints a prompt.  It ensures
00359    that errors and the like do not confuse the command tracing.  */
00360 
00361 void
00362 reset_command_nest_depth (void)
00363 {
00364   command_nest_depth = 1;
00365 
00366   /* Just in case.  */
00367   suppress_next_print_command_trace = 0;
00368 }
00369 
00370 /* Print the command, prefixed with '+' to represent the call depth.
00371    This is slightly complicated because this function may be called
00372    from execute_command and execute_control_command.  Unfortunately
00373    execute_command also prints the top level control commands.
00374    In these cases execute_command will call execute_control_command
00375    via while_command or if_command.  Inner levels of 'if' and 'while'
00376    are dealt with directly.  Therefore we can use these functions
00377    to determine whether the command has been printed already or not.  */
00378 void
00379 print_command_trace (const char *cmd)
00380 {
00381   int i;
00382 
00383   if (suppress_next_print_command_trace)
00384     {
00385       suppress_next_print_command_trace = 0;
00386       return;
00387     }
00388 
00389   if (!source_verbose && !trace_commands)
00390     return;
00391 
00392   for (i=0; i < command_nest_depth; i++)
00393     printf_filtered ("+");
00394 
00395   printf_filtered ("%s\n", cmd);
00396 }
00397 
00398 enum command_control_type
00399 execute_control_command (struct command_line *cmd)
00400 {
00401   struct expression *expr;
00402   struct command_line *current;
00403   struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
00404   struct value *val;
00405   struct value *val_mark;
00406   int loop;
00407   enum command_control_type ret;
00408   char *new_line;
00409 
00410   /* Start by assuming failure, if a problem is detected, the code
00411      below will simply "break" out of the switch.  */
00412   ret = invalid_control;
00413 
00414   switch (cmd->control_type)
00415     {
00416     case simple_control:
00417       /* A simple command, execute it and return.  */
00418       new_line = insert_args (cmd->line);
00419       if (!new_line)
00420         break;
00421       make_cleanup (free_current_contents, &new_line);
00422       execute_command (new_line, 0);
00423       ret = cmd->control_type;
00424       break;
00425 
00426     case continue_control:
00427       print_command_trace ("loop_continue");
00428 
00429       /* Return for "continue", and "break" so we can either
00430          continue the loop at the top, or break out.  */
00431       ret = cmd->control_type;
00432       break;
00433 
00434     case break_control:
00435       print_command_trace ("loop_break");
00436 
00437       /* Return for "continue", and "break" so we can either
00438          continue the loop at the top, or break out.  */
00439       ret = cmd->control_type;
00440       break;
00441 
00442     case while_control:
00443       {
00444         int len = strlen (cmd->line) + 7;
00445         char *buffer = alloca (len);
00446 
00447         xsnprintf (buffer, len, "while %s", cmd->line);
00448         print_command_trace (buffer);
00449 
00450         /* Parse the loop control expression for the while statement.  */
00451         new_line = insert_args (cmd->line);
00452         if (!new_line)
00453           break;
00454         make_cleanup (free_current_contents, &new_line);
00455         expr = parse_expression (new_line);
00456         make_cleanup (free_current_contents, &expr);
00457 
00458         ret = simple_control;
00459         loop = 1;
00460 
00461         /* Keep iterating so long as the expression is true.  */
00462         while (loop == 1)
00463           {
00464             int cond_result;
00465 
00466             QUIT;
00467 
00468             /* Evaluate the expression.  */
00469             val_mark = value_mark ();
00470             val = evaluate_expression (expr);
00471             cond_result = value_true (val);
00472             value_free_to_mark (val_mark);
00473 
00474             /* If the value is false, then break out of the loop.  */
00475             if (!cond_result)
00476               break;
00477 
00478             /* Execute the body of the while statement.  */
00479             current = *cmd->body_list;
00480             while (current)
00481               {
00482                 command_nest_depth++;
00483                 ret = execute_control_command (current);
00484                 command_nest_depth--;
00485 
00486                 /* If we got an error, or a "break" command, then stop
00487                    looping.  */
00488                 if (ret == invalid_control || ret == break_control)
00489                   {
00490                     loop = 0;
00491                     break;
00492                   }
00493 
00494                 /* If we got a "continue" command, then restart the loop
00495                    at this point.  */
00496                 if (ret == continue_control)
00497                   break;
00498 
00499                 /* Get the next statement.  */
00500                 current = current->next;
00501               }
00502           }
00503 
00504         /* Reset RET so that we don't recurse the break all the way down.  */
00505         if (ret == break_control)
00506           ret = simple_control;
00507 
00508         break;
00509       }
00510 
00511     case if_control:
00512       {
00513         int len = strlen (cmd->line) + 4;
00514         char *buffer = alloca (len);
00515 
00516         xsnprintf (buffer, len, "if %s", cmd->line);
00517         print_command_trace (buffer);
00518 
00519         new_line = insert_args (cmd->line);
00520         if (!new_line)
00521           break;
00522         make_cleanup (free_current_contents, &new_line);
00523         /* Parse the conditional for the if statement.  */
00524         expr = parse_expression (new_line);
00525         make_cleanup (free_current_contents, &expr);
00526 
00527         current = NULL;
00528         ret = simple_control;
00529 
00530         /* Evaluate the conditional.  */
00531         val_mark = value_mark ();
00532         val = evaluate_expression (expr);
00533 
00534         /* Choose which arm to take commands from based on the value
00535            of the conditional expression.  */
00536         if (value_true (val))
00537           current = *cmd->body_list;
00538         else if (cmd->body_count == 2)
00539           current = *(cmd->body_list + 1);
00540         value_free_to_mark (val_mark);
00541 
00542         /* Execute commands in the given arm.  */
00543         while (current)
00544           {
00545             command_nest_depth++;
00546             ret = execute_control_command (current);
00547             command_nest_depth--;
00548 
00549             /* If we got an error, get out.  */
00550             if (ret != simple_control)
00551               break;
00552 
00553             /* Get the next statement in the body.  */
00554             current = current->next;
00555           }
00556 
00557         break;
00558       }
00559     case commands_control:
00560       {
00561         /* Breakpoint commands list, record the commands in the
00562            breakpoint's command list and return.  */
00563         new_line = insert_args (cmd->line);
00564         if (!new_line)
00565           break;
00566         make_cleanup (free_current_contents, &new_line);
00567         ret = commands_from_control_command (new_line, cmd);
00568         break;
00569       }
00570     case python_control:
00571       {
00572         eval_python_from_control_command (cmd);
00573         ret = simple_control;
00574         break;
00575       }
00576 
00577     default:
00578       warning (_("Invalid control type in canned commands structure."));
00579       break;
00580     }
00581 
00582   do_cleanups (old_chain);
00583 
00584   return ret;
00585 }
00586 
00587 /* Like execute_control_command, but first set
00588    suppress_next_print_command_trace.  */
00589 
00590 enum command_control_type
00591 execute_control_command_untraced (struct command_line *cmd)
00592 {
00593   suppress_next_print_command_trace = 1;
00594   return execute_control_command (cmd);
00595 }
00596 
00597 
00598 /* "while" command support.  Executes a body of statements while the
00599    loop condition is nonzero.  */
00600 
00601 static void
00602 while_command (char *arg, int from_tty)
00603 {
00604   struct command_line *command = NULL;
00605   struct cleanup *old_chain;
00606 
00607   control_level = 1;
00608   command = get_command_line (while_control, arg);
00609 
00610   if (command == NULL)
00611     return;
00612 
00613   old_chain = make_cleanup_restore_integer (&interpreter_async);
00614   interpreter_async = 0;
00615 
00616   execute_control_command_untraced (command);
00617   free_command_lines (&command);
00618 
00619   do_cleanups (old_chain);
00620 }
00621 
00622 /* "if" command support.  Execute either the true or false arm depending
00623    on the value of the if conditional.  */
00624 
00625 static void
00626 if_command (char *arg, int from_tty)
00627 {
00628   struct command_line *command = NULL;
00629   struct cleanup *old_chain;
00630 
00631   control_level = 1;
00632   command = get_command_line (if_control, arg);
00633 
00634   if (command == NULL)
00635     return;
00636 
00637   old_chain = make_cleanup_restore_integer (&interpreter_async);
00638   interpreter_async = 0;
00639 
00640   execute_control_command_untraced (command);
00641   free_command_lines (&command);
00642 
00643   do_cleanups (old_chain);
00644 }
00645 
00646 /* Cleanup */
00647 static void
00648 arg_cleanup (void *ignore)
00649 {
00650   struct user_args *oargs = user_args;
00651 
00652   if (!user_args)
00653     internal_error (__FILE__, __LINE__,
00654                     _("arg_cleanup called with no user args.\n"));
00655 
00656   user_args = user_args->next;
00657   xfree (oargs->command);
00658   xfree (oargs);
00659 }
00660 
00661 /* Bind the incomming arguments for a user defined command to
00662    $arg0, $arg1 ... $argMAXUSERARGS.  */
00663 
00664 static struct cleanup *
00665 setup_user_args (char *p)
00666 {
00667   struct user_args *args;
00668   struct cleanup *old_chain;
00669   unsigned int arg_count = 0;
00670 
00671   args = (struct user_args *) xmalloc (sizeof (struct user_args));
00672   memset (args, 0, sizeof (struct user_args));
00673 
00674   args->next = user_args;
00675   user_args = args;
00676 
00677   old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
00678 
00679   if (p == NULL)
00680     return old_chain;
00681 
00682   user_args->command = p = xstrdup (p);
00683 
00684   while (*p)
00685     {
00686       char *start_arg;
00687       int squote = 0;
00688       int dquote = 0;
00689       int bsquote = 0;
00690 
00691       if (arg_count >= MAXUSERARGS)
00692         error (_("user defined function may only have %d arguments."),
00693                MAXUSERARGS);
00694 
00695       /* Strip whitespace.  */
00696       while (*p == ' ' || *p == '\t')
00697         p++;
00698 
00699       /* P now points to an argument.  */
00700       start_arg = p;
00701       user_args->a[arg_count].arg = p;
00702 
00703       /* Get to the end of this argument.  */
00704       while (*p)
00705         {
00706           if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
00707             break;
00708           else
00709             {
00710               if (bsquote)
00711                 bsquote = 0;
00712               else if (*p == '\\')
00713                 bsquote = 1;
00714               else if (squote)
00715                 {
00716                   if (*p == '\'')
00717                     squote = 0;
00718                 }
00719               else if (dquote)
00720                 {
00721                   if (*p == '"')
00722                     dquote = 0;
00723                 }
00724               else
00725                 {
00726                   if (*p == '\'')
00727                     squote = 1;
00728                   else if (*p == '"')
00729                     dquote = 1;
00730                 }
00731               p++;
00732             }
00733         }
00734 
00735       user_args->a[arg_count].len = p - start_arg;
00736       arg_count++;
00737       user_args->count++;
00738     }
00739   return old_chain;
00740 }
00741 
00742 /* Given character string P, return a point to the first argument
00743    ($arg), or NULL if P contains no arguments.  */
00744 
00745 static char *
00746 locate_arg (char *p)
00747 {
00748   while ((p = strchr (p, '$')))
00749     {
00750       if (strncmp (p, "$arg", 4) == 0
00751           && (isdigit (p[4]) || p[4] == 'c'))
00752         return p;
00753       p++;
00754     }
00755   return NULL;
00756 }
00757 
00758 /* Insert the user defined arguments stored in user_arg into the $arg
00759    arguments found in line, with the updated copy being placed into
00760    nline.  */
00761 
00762 static char *
00763 insert_args (char *line)
00764 {
00765   char *p, *save_line, *new_line;
00766   unsigned len, i;
00767 
00768   /* If we are not in a user-defined function, treat $argc, $arg0, et
00769      cetera as normal convenience variables.  */
00770   if (user_args == NULL)
00771     return xstrdup (line);
00772 
00773   /* First we need to know how much memory to allocate for the new
00774      line.  */
00775   save_line = line;
00776   len = 0;
00777   while ((p = locate_arg (line)))
00778     {
00779       len += p - line;
00780       i = p[4] - '0';
00781 
00782       if (p[4] == 'c')
00783         {
00784           /* $argc.  Number will be <=10.  */
00785           len += user_args->count == 10 ? 2 : 1;
00786         }
00787       else if (i >= user_args->count)
00788         {
00789           error (_("Missing argument %d in user function."), i);
00790           return NULL;
00791         }
00792       else
00793         {
00794           len += user_args->a[i].len;
00795         }
00796       line = p + 5;
00797     }
00798 
00799   /* Don't forget the tail.  */
00800   len += strlen (line);
00801 
00802   /* Allocate space for the new line and fill it in.  */
00803   new_line = (char *) xmalloc (len + 1);
00804   if (new_line == NULL)
00805     return NULL;
00806 
00807   /* Restore pointer to beginning of old line.  */
00808   line = save_line;
00809 
00810   /* Save pointer to beginning of new line.  */
00811   save_line = new_line;
00812 
00813   while ((p = locate_arg (line)))
00814     {
00815       int i, len;
00816 
00817       memcpy (new_line, line, p - line);
00818       new_line += p - line;
00819 
00820       if (p[4] == 'c')
00821         {
00822           gdb_assert (user_args->count >= 0 && user_args->count <= 10);
00823           if (user_args->count == 10)
00824             {
00825               *(new_line++) = '1';
00826               *(new_line++) = '0';
00827             }
00828           else
00829             *(new_line++) = user_args->count + '0';
00830         }
00831       else
00832         {
00833           i = p[4] - '0';
00834           len = user_args->a[i].len;
00835           if (len)
00836             {
00837               memcpy (new_line, user_args->a[i].arg, len);
00838               new_line += len;
00839             }
00840         }
00841       line = p + 5;
00842     }
00843   /* Don't forget the tail.  */
00844   strcpy (new_line, line);
00845 
00846   /* Return a pointer to the beginning of the new line.  */
00847   return save_line;
00848 }
00849 
00850 
00851 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
00852    code bodies.  This is typically used when we encounter an "else"
00853    clause for an "if" command.  */
00854 
00855 static void
00856 realloc_body_list (struct command_line *command, int new_length)
00857 {
00858   int n;
00859   struct command_line **body_list;
00860 
00861   n = command->body_count;
00862 
00863   /* Nothing to do?  */
00864   if (new_length <= n)
00865     return;
00866 
00867   body_list = (struct command_line **)
00868     xmalloc (sizeof (struct command_line *) * new_length);
00869 
00870   memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
00871   memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
00872 
00873   xfree (command->body_list);
00874   command->body_list = body_list;
00875   command->body_count = new_length;
00876 }
00877 
00878 /* Read next line from stdout.  Passed to read_command_line_1 and
00879    recurse_read_control_structure whenever we need to read commands
00880    from stdout.  */
00881 
00882 static char *
00883 read_next_line (void)
00884 {
00885   char *prompt_ptr, control_prompt[256];
00886   int i = 0;
00887 
00888   if (control_level >= 254)
00889     error (_("Control nesting too deep!"));
00890 
00891   /* Set a prompt based on the nesting of the control commands.  */
00892   if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
00893     {
00894       for (i = 0; i < control_level; i++)
00895         control_prompt[i] = ' ';
00896       control_prompt[i] = '>';
00897       control_prompt[i + 1] = '\0';
00898       prompt_ptr = (char *) &control_prompt[0];
00899     }
00900   else
00901     prompt_ptr = NULL;
00902 
00903   return command_line_input (prompt_ptr, instream == stdin, "commands");
00904 }
00905 
00906 /* Process one input line.  If the command is an "end", return such an
00907    indication to the caller.  If PARSE_COMMANDS is true, strip leading
00908    whitespace (trailing whitespace is always stripped) in the line,
00909    attempt to recognize GDB control commands, and also return an
00910    indication if the command is an "else" or a nop.
00911 
00912    Otherwise, only "end" is recognized.  */
00913 
00914 static enum misc_command_type
00915 process_next_line (char *p, struct command_line **command, int parse_commands,
00916                    void (*validator)(char *, void *), void *closure)
00917 {
00918   char *p_end;
00919   char *p_start;
00920   int not_handled = 0;
00921 
00922   /* Not sure what to do here.  */
00923   if (p == NULL)
00924     return end_command;
00925 
00926   /* Strip trailing whitespace.  */
00927   p_end = p + strlen (p);
00928   while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
00929     p_end--;
00930 
00931   p_start = p;
00932   /* Strip leading whitespace.  */
00933   while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
00934     p_start++;
00935 
00936   /* 'end' is always recognized, regardless of parse_commands value.
00937      We also permit whitespace before end and after.  */
00938   if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
00939     return end_command;
00940   
00941   if (parse_commands)
00942     {
00943       /* If commands are parsed, we skip initial spaces.  Otherwise,
00944          which is the case for Python commands and documentation
00945          (see the 'document' command), spaces are preserved.  */
00946       p = p_start;
00947 
00948       /* Blanks and comments don't really do anything, but we need to
00949          distinguish them from else, end and other commands which can
00950          be executed.  */
00951       if (p_end == p || p[0] == '#')
00952         return nop_command;
00953 
00954       /* Is the else clause of an if control structure?  */
00955       if (p_end - p == 4 && !strncmp (p, "else", 4))
00956         return else_command;
00957 
00958       /* Check for while, if, break, continue, etc and build a new
00959          command line structure for them.  */
00960       if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
00961           || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
00962           || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
00963         {
00964           /* Because validate_actionline and encode_action lookup
00965              command's line as command, we need the line to
00966              include 'while-stepping'.
00967 
00968              For 'ws' alias, the command will have 'ws', not expanded
00969              to 'while-stepping'.  This is intentional -- we don't
00970              really want frontend to send a command list with 'ws',
00971              and next break-info returning command line with
00972              'while-stepping'.  This should work, but might cause the
00973              breakpoint to be marked as changed while it's actually
00974              not.  */
00975           *command = build_command_line (while_stepping_control, p);
00976         }
00977       else if (p_end - p > 5 && !strncmp (p, "while", 5))
00978         {
00979           char *first_arg;
00980 
00981           first_arg = p + 5;
00982           while (first_arg < p_end && isspace (*first_arg))
00983             first_arg++;
00984           *command = build_command_line (while_control, first_arg);
00985         }
00986       else if (p_end - p > 2 && !strncmp (p, "if", 2))
00987         {
00988           char *first_arg;
00989 
00990           first_arg = p + 2;
00991           while (first_arg < p_end && isspace (*first_arg))
00992             first_arg++;
00993           *command = build_command_line (if_control, first_arg);
00994         }
00995       else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
00996         {
00997           char *first_arg;
00998 
00999           first_arg = p + 8;
01000           while (first_arg < p_end && isspace (*first_arg))
01001             first_arg++;
01002           *command = build_command_line (commands_control, first_arg);
01003         }
01004       else if (p_end - p == 6 && !strncmp (p, "python", 6))
01005         {
01006           /* Note that we ignore the inline "python command" form
01007              here.  */
01008           *command = build_command_line (python_control, "");
01009         }
01010       else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
01011         {
01012           *command = (struct command_line *)
01013             xmalloc (sizeof (struct command_line));
01014           (*command)->next = NULL;
01015           (*command)->line = NULL;
01016           (*command)->control_type = break_control;
01017           (*command)->body_count = 0;
01018           (*command)->body_list = NULL;
01019         }
01020       else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
01021         {
01022           *command = (struct command_line *)
01023             xmalloc (sizeof (struct command_line));
01024           (*command)->next = NULL;
01025           (*command)->line = NULL;
01026           (*command)->control_type = continue_control;
01027           (*command)->body_count = 0;
01028           (*command)->body_list = NULL;
01029         }
01030       else
01031         not_handled = 1;
01032     }
01033 
01034   if (!parse_commands || not_handled)
01035     {
01036       /* A normal command.  */
01037       *command = (struct command_line *)
01038         xmalloc (sizeof (struct command_line));
01039       (*command)->next = NULL;
01040       (*command)->line = savestring (p, p_end - p);
01041       (*command)->control_type = simple_control;
01042       (*command)->body_count = 0;
01043       (*command)->body_list = NULL;
01044     }
01045 
01046   if (validator)
01047     {
01048       volatile struct gdb_exception ex;
01049 
01050       TRY_CATCH (ex, RETURN_MASK_ALL)
01051         {
01052           validator ((*command)->line, closure);
01053         }
01054       if (ex.reason < 0)
01055         {
01056           xfree (*command);
01057           throw_exception (ex);
01058         }
01059     }
01060 
01061   /* Nothing special.  */
01062   return ok_command;
01063 }
01064 
01065 /* Recursively read in the control structures and create a
01066    command_line structure from them.  Use read_next_line_func to
01067    obtain lines of the command.  */
01068 
01069 static enum command_control_type
01070 recurse_read_control_structure (char * (*read_next_line_func) (void),
01071                                 struct command_line *current_cmd,
01072                                 void (*validator)(char *, void *),
01073                                 void *closure)
01074 {
01075   int current_body, i;
01076   enum misc_command_type val;
01077   enum command_control_type ret;
01078   struct command_line **body_ptr, *child_tail, *next;
01079 
01080   child_tail = NULL;
01081   current_body = 1;
01082 
01083   /* Sanity checks.  */
01084   if (current_cmd->control_type == simple_control)
01085     error (_("Recursed on a simple control type."));
01086 
01087   if (current_body > current_cmd->body_count)
01088     error (_("Allocated body is smaller than this command type needs."));
01089 
01090   /* Read lines from the input stream and build control structures.  */
01091   while (1)
01092     {
01093       dont_repeat ();
01094 
01095       next = NULL;
01096       val = process_next_line (read_next_line_func (), &next, 
01097                                current_cmd->control_type != python_control,
01098                                validator, closure);
01099 
01100       /* Just skip blanks and comments.  */
01101       if (val == nop_command)
01102         continue;
01103 
01104       if (val == end_command)
01105         {
01106           if (current_cmd->control_type == while_control
01107               || current_cmd->control_type == while_stepping_control
01108               || current_cmd->control_type == if_control
01109               || current_cmd->control_type == python_control
01110               || current_cmd->control_type == commands_control)
01111             {
01112               /* Success reading an entire canned sequence of commands.  */
01113               ret = simple_control;
01114               break;
01115             }
01116           else
01117             {
01118               ret = invalid_control;
01119               break;
01120             }
01121         }
01122 
01123       /* Not the end of a control structure.  */
01124       if (val == else_command)
01125         {
01126           if (current_cmd->control_type == if_control
01127               && current_body == 1)
01128             {
01129               realloc_body_list (current_cmd, 2);
01130               current_body = 2;
01131               child_tail = NULL;
01132               continue;
01133             }
01134           else
01135             {
01136               ret = invalid_control;
01137               break;
01138             }
01139         }
01140 
01141       if (child_tail)
01142         {
01143           child_tail->next = next;
01144         }
01145       else
01146         {
01147           body_ptr = current_cmd->body_list;
01148           for (i = 1; i < current_body; i++)
01149             body_ptr++;
01150 
01151           *body_ptr = next;
01152 
01153         }
01154 
01155       child_tail = next;
01156 
01157       /* If the latest line is another control structure, then recurse
01158          on it.  */
01159       if (next->control_type == while_control
01160           || next->control_type == while_stepping_control
01161           || next->control_type == if_control
01162           || next->control_type == python_control
01163           || next->control_type == commands_control)
01164         {
01165           control_level++;
01166           ret = recurse_read_control_structure (read_next_line_func, next,
01167                                                 validator, closure);
01168           control_level--;
01169 
01170           if (ret != simple_control)
01171             break;
01172         }
01173     }
01174 
01175   dont_repeat ();
01176 
01177   return ret;
01178 }
01179 
01180 static void
01181 restore_interp (void *arg)
01182 {
01183   interp_set_temp (interp_name ((struct interp *)arg));
01184 }
01185 
01186 /* Read lines from the input stream and accumulate them in a chain of
01187    struct command_line's, which is then returned.  For input from a
01188    terminal, the special command "end" is used to mark the end of the
01189    input, and is not included in the returned chain of commands.
01190 
01191    If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
01192    is always stripped) in the line and attempt to recognize GDB control
01193    commands.  Otherwise, only "end" is recognized.  */
01194 
01195 #define END_MESSAGE "End with a line saying just \"end\"."
01196 
01197 struct command_line *
01198 read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
01199                     void (*validator)(char *, void *), void *closure)
01200 {
01201   struct command_line *head;
01202 
01203   if (from_tty && input_from_terminal_p ())
01204     {
01205       if (deprecated_readline_begin_hook)
01206         {
01207           /* Note - intentional to merge messages with no newline.  */
01208           (*deprecated_readline_begin_hook) ("%s  %s\n", prompt_arg,
01209                                              END_MESSAGE);
01210         }
01211       else
01212         {
01213           printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
01214           gdb_flush (gdb_stdout);
01215         }
01216     }
01217 
01218 
01219   /* Reading commands assumes the CLI behavior, so temporarily
01220      override the current interpreter with CLI.  */
01221   if (current_interp_named_p (INTERP_CONSOLE))
01222     head = read_command_lines_1 (read_next_line, parse_commands,
01223                                  validator, closure);
01224   else
01225     {
01226       struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
01227       struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
01228 
01229       head = read_command_lines_1 (read_next_line, parse_commands,
01230                                    validator, closure);
01231       do_cleanups (old_chain);
01232     }
01233 
01234   if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
01235     {
01236       (*deprecated_readline_end_hook) ();
01237     }
01238   return (head);
01239 }
01240 
01241 /* Act the same way as read_command_lines, except that each new line is
01242    obtained using READ_NEXT_LINE_FUNC.  */
01243 
01244 struct command_line *
01245 read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
01246                       void (*validator)(char *, void *), void *closure)
01247 {
01248   struct command_line *head, *tail, *next;
01249   struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
01250   enum command_control_type ret;
01251   enum misc_command_type val;
01252 
01253   control_level = 0;
01254   head = tail = NULL;
01255 
01256   while (1)
01257     {
01258       dont_repeat ();
01259       val = process_next_line (read_next_line_func (), &next, parse_commands,
01260                                validator, closure);
01261 
01262       /* Ignore blank lines or comments.  */
01263       if (val == nop_command)
01264         continue;
01265 
01266       if (val == end_command)
01267         {
01268           ret = simple_control;
01269           break;
01270         }
01271 
01272       if (val != ok_command)
01273         {
01274           ret = invalid_control;
01275           break;
01276         }
01277 
01278       if (next->control_type == while_control
01279           || next->control_type == if_control
01280           || next->control_type == python_control
01281           || next->control_type == commands_control
01282           || next->control_type == while_stepping_control)
01283         {
01284           control_level++;
01285           ret = recurse_read_control_structure (read_next_line_func, next,
01286                                                 validator, closure);
01287           control_level--;
01288 
01289           if (ret == invalid_control)
01290             break;
01291         }
01292 
01293       if (tail)
01294         {
01295           tail->next = next;
01296         }
01297       else
01298         {
01299           head = next;
01300           make_cleanup_free_command_lines (&head);
01301         }
01302       tail = next;
01303     }
01304 
01305   dont_repeat ();
01306 
01307   if (ret != invalid_control)
01308     discard_cleanups (old_chain);
01309   else
01310     do_cleanups (old_chain);
01311 
01312   return head;
01313 }
01314 
01315 /* Free a chain of struct command_line's.  */
01316 
01317 void
01318 free_command_lines (struct command_line **lptr)
01319 {
01320   struct command_line *l = *lptr;
01321   struct command_line *next;
01322   struct command_line **blist;
01323   int i;
01324 
01325   while (l)
01326     {
01327       if (l->body_count > 0)
01328         {
01329           blist = l->body_list;
01330           for (i = 0; i < l->body_count; i++, blist++)
01331             free_command_lines (blist);
01332         }
01333       next = l->next;
01334       xfree (l->line);
01335       xfree (l);
01336       l = next;
01337     }
01338   *lptr = NULL;
01339 }
01340 
01341 static void
01342 do_free_command_lines_cleanup (void *arg)
01343 {
01344   free_command_lines (arg);
01345 }
01346 
01347 struct cleanup *
01348 make_cleanup_free_command_lines (struct command_line **arg)
01349 {
01350   return make_cleanup (do_free_command_lines_cleanup, arg);
01351 }
01352 
01353 struct command_line *
01354 copy_command_lines (struct command_line *cmds)
01355 {
01356   struct command_line *result = NULL;
01357 
01358   if (cmds)
01359     {
01360       result = (struct command_line *) xmalloc (sizeof (struct command_line));
01361 
01362       result->next = copy_command_lines (cmds->next);
01363       result->line = xstrdup (cmds->line);
01364       result->control_type = cmds->control_type;
01365       result->body_count = cmds->body_count;
01366       if (cmds->body_count > 0)
01367         {
01368           int i;
01369 
01370           result->body_list = (struct command_line **)
01371             xmalloc (sizeof (struct command_line *) * cmds->body_count);
01372 
01373           for (i = 0; i < cmds->body_count; i++)
01374             result->body_list[i] = copy_command_lines (cmds->body_list[i]);
01375         }
01376       else
01377         result->body_list = NULL;
01378     }
01379 
01380   return result;
01381 }
01382 
01383 /* Validate that *COMNAME is a valid name for a command.  Return the
01384    containing command list, in case it starts with a prefix command.
01385    The prefix must already exist.  *COMNAME is advanced to point after
01386    any prefix, and a NUL character overwrites the space after the
01387    prefix.  */
01388 
01389 static struct cmd_list_element **
01390 validate_comname (char **comname)
01391 {
01392   struct cmd_list_element **list = &cmdlist;
01393   char *p, *last_word;
01394 
01395   if (*comname == 0)
01396     error_no_arg (_("name of command to define"));
01397 
01398   /* Find the last word of the argument.  */
01399   p = *comname + strlen (*comname);
01400   while (p > *comname && isspace (p[-1]))
01401     p--;
01402   while (p > *comname && !isspace (p[-1]))
01403     p--;
01404   last_word = p;
01405 
01406   /* Find the corresponding command list.  */
01407   if (last_word != *comname)
01408     {
01409       struct cmd_list_element *c;
01410       char saved_char;
01411       const char *tem = *comname;
01412 
01413       /* Separate the prefix and the command.  */
01414       saved_char = last_word[-1];
01415       last_word[-1] = '\0';
01416 
01417       c = lookup_cmd (&tem, cmdlist, "", 0, 1);
01418       if (c->prefixlist == NULL)
01419         error (_("\"%s\" is not a prefix command."), *comname);
01420 
01421       list = c->prefixlist;
01422       last_word[-1] = saved_char;
01423       *comname = last_word;
01424     }
01425 
01426   p = *comname;
01427   while (*p)
01428     {
01429       if (!isalnum (*p) && *p != '-' && *p != '_')
01430         error (_("Junk in argument list: \"%s\""), p);
01431       p++;
01432     }
01433 
01434   return list;
01435 }
01436 
01437 /* This is just a placeholder in the command data structures.  */
01438 static void
01439 user_defined_command (char *ignore, int from_tty)
01440 {
01441 }
01442 
01443 static void
01444 define_command (char *comname, int from_tty)
01445 {
01446 #define MAX_TMPBUF 128   
01447   enum cmd_hook_type
01448     {
01449       CMD_NO_HOOK = 0,
01450       CMD_PRE_HOOK,
01451       CMD_POST_HOOK
01452     };
01453   struct command_line *cmds;
01454   struct cmd_list_element *c, *newc, *hookc = 0, **list;
01455   char *tem, *comfull;
01456   const char *tem_c;
01457   char tmpbuf[MAX_TMPBUF];
01458   int  hook_type      = CMD_NO_HOOK;
01459   int  hook_name_size = 0;
01460    
01461 #define HOOK_STRING     "hook-"
01462 #define HOOK_LEN 5
01463 #define HOOK_POST_STRING "hookpost-"
01464 #define HOOK_POST_LEN    9
01465 
01466   comfull = comname;
01467   list = validate_comname (&comname);
01468 
01469   /* Look it up, and verify that we got an exact match.  */
01470   tem_c = comname;
01471   c = lookup_cmd (&tem_c, *list, "", -1, 1);
01472   if (c && strcmp (comname, c->name) != 0)
01473     c = 0;
01474 
01475   if (c)
01476     {
01477       int q;
01478 
01479       if (c->class == class_user || c->class == class_alias)
01480         q = query (_("Redefine command \"%s\"? "), c->name);
01481       else
01482         q = query (_("Really redefine built-in command \"%s\"? "), c->name);
01483       if (!q)
01484         error (_("Command \"%s\" not redefined."), c->name);
01485     }
01486 
01487   /* If this new command is a hook, then mark the command which it
01488      is hooking.  Note that we allow hooking `help' commands, so that
01489      we can hook the `stop' pseudo-command.  */
01490 
01491   if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
01492     {
01493        hook_type      = CMD_PRE_HOOK;
01494        hook_name_size = HOOK_LEN;
01495     }
01496   else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
01497     {
01498       hook_type      = CMD_POST_HOOK;
01499       hook_name_size = HOOK_POST_LEN;
01500     }
01501    
01502   if (hook_type != CMD_NO_HOOK)
01503     {
01504       /* Look up cmd it hooks, and verify that we got an exact match.  */
01505       tem_c = comname + hook_name_size;
01506       hookc = lookup_cmd (&tem_c, *list, "", -1, 0);
01507       if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
01508         hookc = 0;
01509       if (!hookc)
01510         {
01511           warning (_("Your new `%s' command does not "
01512                      "hook any existing command."),
01513                    comfull);
01514           if (!query (_("Proceed? ")))
01515             error (_("Not confirmed."));
01516         }
01517     }
01518 
01519   comname = xstrdup (comname);
01520 
01521   /* If the rest of the commands will be case insensitive, this one
01522      should behave in the same manner.  */
01523   for (tem = comname; *tem; tem++)
01524     if (isupper (*tem))
01525       *tem = tolower (*tem);
01526 
01527   xsnprintf (tmpbuf, sizeof (tmpbuf),
01528              "Type commands for definition of \"%s\".", comfull);
01529   cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
01530 
01531   if (c && c->class == class_user)
01532     free_command_lines (&c->user_commands);
01533 
01534   newc = add_cmd (comname, class_user, user_defined_command,
01535                   (c && c->class == class_user)
01536                   ? c->doc : xstrdup ("User-defined."), list);
01537   newc->user_commands = cmds;
01538 
01539   /* If this new command is a hook, then mark both commands as being
01540      tied.  */
01541   if (hookc)
01542     {
01543       switch (hook_type)
01544         {
01545         case CMD_PRE_HOOK:
01546           hookc->hook_pre  = newc;  /* Target gets hooked.  */
01547           newc->hookee_pre = hookc; /* We are marked as hooking target cmd.  */
01548           break;
01549         case CMD_POST_HOOK:
01550           hookc->hook_post  = newc;  /* Target gets hooked.  */
01551           newc->hookee_post = hookc; /* We are marked as hooking
01552                                         target cmd.  */
01553           break;
01554         default:
01555           /* Should never come here as hookc would be 0.  */
01556           internal_error (__FILE__, __LINE__, _("bad switch"));
01557         }
01558     }
01559 }
01560 
01561 static void
01562 document_command (char *comname, int from_tty)
01563 {
01564   struct command_line *doclines;
01565   struct cmd_list_element *c, **list;
01566   const char *tem;
01567   char *comfull;
01568   char tmpbuf[128];
01569 
01570   comfull = comname;
01571   list = validate_comname (&comname);
01572 
01573   tem = comname;
01574   c = lookup_cmd (&tem, *list, "", 0, 1);
01575 
01576   if (c->class != class_user)
01577     error (_("Command \"%s\" is built-in."), comfull);
01578 
01579   xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
01580              comfull);
01581   doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
01582 
01583   if (c->doc)
01584     xfree (c->doc);
01585 
01586   {
01587     struct command_line *cl1;
01588     int len = 0;
01589 
01590     for (cl1 = doclines; cl1; cl1 = cl1->next)
01591       len += strlen (cl1->line) + 1;
01592 
01593     c->doc = (char *) xmalloc (len + 1);
01594     *c->doc = 0;
01595 
01596     for (cl1 = doclines; cl1; cl1 = cl1->next)
01597       {
01598         strcat (c->doc, cl1->line);
01599         if (cl1->next)
01600           strcat (c->doc, "\n");
01601       }
01602   }
01603 
01604   free_command_lines (&doclines);
01605 }
01606 
01607 struct source_cleanup_lines_args
01608 {
01609   int old_line;
01610   const char *old_file;
01611 };
01612 
01613 static void
01614 source_cleanup_lines (void *args)
01615 {
01616   struct source_cleanup_lines_args *p =
01617     (struct source_cleanup_lines_args *) args;
01618 
01619   source_line_number = p->old_line;
01620   source_file_name = p->old_file;
01621 }
01622 
01623 /* Used to implement source_command.  */
01624 
01625 void
01626 script_from_file (FILE *stream, const char *file)
01627 {
01628   struct cleanup *old_cleanups;
01629   struct source_cleanup_lines_args old_lines;
01630 
01631   if (stream == NULL)
01632     internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
01633 
01634   old_lines.old_line = source_line_number;
01635   old_lines.old_file = source_file_name;
01636   old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
01637   source_line_number = 0;
01638   source_file_name = file;
01639 
01640   {
01641     volatile struct gdb_exception e;
01642 
01643     TRY_CATCH (e, RETURN_MASK_ERROR)
01644       {
01645         read_command_file (stream);
01646       }
01647     switch (e.reason)
01648       {
01649       case 0:
01650         break;
01651       case RETURN_ERROR:
01652         /* Re-throw the error, but with the file name information
01653            prepended.  */
01654         throw_error (e.error,
01655                      _("%s:%d: Error in sourced command file:\n%s"),
01656                      source_file_name, source_line_number, e.message);
01657       default:
01658         internal_error (__FILE__, __LINE__, _("bad reason"));
01659       }
01660   }
01661 
01662   do_cleanups (old_cleanups);
01663 }
01664 
01665 /* Print the definition of user command C to STREAM.  Or, if C is a
01666    prefix command, show the definitions of all user commands under C
01667    (recursively).  PREFIX and NAME combined are the name of the
01668    current command.  */
01669 void
01670 show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
01671              struct ui_file *stream)
01672 {
01673   struct command_line *cmdlines;
01674 
01675   if (c->prefixlist != NULL)
01676     {
01677       char *prefixname = c->prefixname;
01678 
01679       for (c = *c->prefixlist; c != NULL; c = c->next)
01680         if (c->class == class_user || c->prefixlist != NULL)
01681           show_user_1 (c, prefixname, c->name, gdb_stdout);
01682       return;
01683     }
01684 
01685   cmdlines = c->user_commands;
01686   if (!cmdlines)
01687     return;
01688   fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
01689 
01690   print_command_lines (current_uiout, cmdlines, 1);
01691   fputs_filtered ("\n", stream);
01692 }
01693 
01694 
01695 
01696 initialize_file_ftype _initialize_cli_script;
01697 
01698 void
01699 _initialize_cli_script (void)
01700 {
01701   add_com ("document", class_support, document_command, _("\
01702 Document a user-defined command.\n\
01703 Give command name as argument.  Give documentation on following lines.\n\
01704 End with a line of just \"end\"."));
01705   add_com ("define", class_support, define_command, _("\
01706 Define a new command name.  Command name is argument.\n\
01707 Definition appears on following lines, one command per line.\n\
01708 End with a line of just \"end\".\n\
01709 Use the \"document\" command to give documentation for the new command.\n\
01710 Commands defined in this way may have up to ten arguments."));
01711 
01712   add_com ("while", class_support, while_command, _("\
01713 Execute nested commands WHILE the conditional expression is non zero.\n\
01714 The conditional expression must follow the word `while' and must in turn be\n\
01715 followed by a new line.  The nested commands must be entered one per line,\n\
01716 and should be terminated by the word `end'."));
01717 
01718   add_com ("if", class_support, if_command, _("\
01719 Execute nested commands once IF the conditional expression is non zero.\n\
01720 The conditional expression must follow the word `if' and must in turn be\n\
01721 followed by a new line.  The nested commands must be entered one per line,\n\
01722 and should be terminated by the word 'else' or `end'.  If an else clause\n\
01723 is used, the same rules apply to its nested commands as to the first ones."));
01724 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines