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