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