* config/tc-arm.c (make_mapping_symbol): Handle the case
[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 934 if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
d350db38
PA
935 || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
936 || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
a7bdde9e
VP
937 {
938 /* Because validate_actionline and encode_action lookup
939 command's line as command, we need the line to
940 include 'while-stepping'.
941
942 For 'ws' alias, the command will have 'ws', not expanded
943 to 'while-stepping'. This is intentional -- we don't
944 really want frontend to send a command list with 'ws',
945 and next break-info returning command line with 'while-stepping'.
946 This should work, but might cause the breakpoint to be marked as
947 changed while it's actually not. */
948 *command = build_command_line (while_stepping_control, p);
949 }
950 else if (p_end - p > 5 && !strncmp (p, "while", 5))
311a4e6b
TJB
951 {
952 char *first_arg;
953 first_arg = p + 5;
50cb2941 954 while (first_arg < p_end && isspace (*first_arg))
311a4e6b
TJB
955 first_arg++;
956 *command = build_command_line (while_control, first_arg);
957 }
50cb2941 958 else if (p_end - p > 2 && !strncmp (p, "if", 2))
311a4e6b
TJB
959 {
960 char *first_arg;
961 first_arg = p + 2;
50cb2941 962 while (first_arg < p_end && isspace (*first_arg))
311a4e6b
TJB
963 first_arg++;
964 *command = build_command_line (if_control, first_arg);
965 }
50cb2941 966 else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
311a4e6b
TJB
967 {
968 char *first_arg;
969 first_arg = p + 8;
50cb2941 970 while (first_arg < p_end && isspace (*first_arg))
311a4e6b
TJB
971 first_arg++;
972 *command = build_command_line (commands_control, first_arg);
973 }
50cb2941 974 else if (p_end - p == 6 && !strncmp (p, "python", 6))
311a4e6b
TJB
975 {
976 /* Note that we ignore the inline "python command" form
977 here. */
978 *command = build_command_line (python_control, "");
979 }
50cb2941 980 else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
311a4e6b
TJB
981 {
982 *command = (struct command_line *)
983 xmalloc (sizeof (struct command_line));
984 (*command)->next = NULL;
985 (*command)->line = NULL;
986 (*command)->control_type = break_control;
987 (*command)->body_count = 0;
988 (*command)->body_list = NULL;
989 }
50cb2941 990 else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
311a4e6b
TJB
991 {
992 *command = (struct command_line *)
993 xmalloc (sizeof (struct command_line));
994 (*command)->next = NULL;
995 (*command)->line = NULL;
996 (*command)->control_type = continue_control;
997 (*command)->body_count = 0;
998 (*command)->body_list = NULL;
999 }
1000 else
1001 not_handled = 1;
d318976c 1002 }
311a4e6b
TJB
1003
1004 if (!parse_commands || not_handled)
d318976c
FN
1005 {
1006 /* A normal command. */
1007 *command = (struct command_line *)
1008 xmalloc (sizeof (struct command_line));
1009 (*command)->next = NULL;
50cb2941 1010 (*command)->line = savestring (p, p_end - p);
d318976c
FN
1011 (*command)->control_type = simple_control;
1012 (*command)->body_count = 0;
1013 (*command)->body_list = NULL;
1014 }
1015
a7bdde9e
VP
1016 if (validator)
1017 {
1018 volatile struct gdb_exception ex;
1019 TRY_CATCH (ex, RETURN_MASK_ALL)
1020 {
1021 validator ((*command)->line, closure);
1022 }
1023 if (ex.reason < 0)
1024 {
1025 xfree (*command);
1026 throw_exception (ex);
1027 }
1028 }
1029
d318976c
FN
1030 /* Nothing special. */
1031 return ok_command;
1032}
1033
1034/* Recursively read in the control structures and create a command_line
3c1179ff
VP
1035 structure from them. Use read_next_line_func to obtain lines of
1036 the command.
d318976c 1037
3c1179ff 1038*/
d318976c
FN
1039
1040static enum command_control_type
a58d7472 1041recurse_read_control_structure (char * (*read_next_line_func) (void),
a7bdde9e
VP
1042 struct command_line *current_cmd,
1043 void (*validator)(char *, void *),
1044 void *closure)
d318976c
FN
1045{
1046 int current_body, i;
1047 enum misc_command_type val;
1048 enum command_control_type ret;
1049 struct command_line **body_ptr, *child_tail, *next;
3c1179ff 1050 char *p;
d318976c
FN
1051
1052 child_tail = NULL;
1053 current_body = 1;
1054
1055 /* Sanity checks. */
1056 if (current_cmd->control_type == simple_control)
8a3fe4f8 1057 error (_("Recursed on a simple control type."));
d318976c
FN
1058
1059 if (current_body > current_cmd->body_count)
8a3fe4f8 1060 error (_("Allocated body is smaller than this command type needs."));
d318976c
FN
1061
1062 /* Read lines from the input stream and build control structures. */
1063 while (1)
1064 {
1065 dont_repeat ();
1066
1067 next = NULL;
3c1179ff 1068 val = process_next_line (read_next_line_func (), &next,
a7bdde9e
VP
1069 current_cmd->control_type != python_control,
1070 validator, closure);
d318976c
FN
1071
1072 /* Just skip blanks and comments. */
1073 if (val == nop_command)
1074 continue;
1075
1076 if (val == end_command)
1077 {
1078 if (current_cmd->control_type == while_control
a7bdde9e 1079 || current_cmd->control_type == while_stepping_control
40c03ae8 1080 || current_cmd->control_type == if_control
d57a3c85 1081 || current_cmd->control_type == python_control
40c03ae8 1082 || current_cmd->control_type == commands_control)
d318976c 1083 {
40c03ae8 1084 /* Success reading an entire canned sequence of commands. */
d318976c
FN
1085 ret = simple_control;
1086 break;
1087 }
1088 else
1089 {
1090 ret = invalid_control;
1091 break;
1092 }
1093 }
1094
1095 /* Not the end of a control structure. */
1096 if (val == else_command)
1097 {
1098 if (current_cmd->control_type == if_control
1099 && current_body == 1)
1100 {
1101 realloc_body_list (current_cmd, 2);
1102 current_body = 2;
1103 child_tail = NULL;
1104 continue;
1105 }
1106 else
1107 {
1108 ret = invalid_control;
1109 break;
1110 }
1111 }
1112
1113 if (child_tail)
1114 {
1115 child_tail->next = next;
1116 }
1117 else
1118 {
1119 body_ptr = current_cmd->body_list;
1120 for (i = 1; i < current_body; i++)
1121 body_ptr++;
1122
1123 *body_ptr = next;
1124
1125 }
1126
1127 child_tail = next;
1128
1129 /* If the latest line is another control structure, then recurse
1130 on it. */
1131 if (next->control_type == while_control
a7bdde9e 1132 || next->control_type == while_stepping_control
40c03ae8 1133 || next->control_type == if_control
d57a3c85 1134 || next->control_type == python_control
40c03ae8 1135 || next->control_type == commands_control)
d318976c
FN
1136 {
1137 control_level++;
a7bdde9e
VP
1138 ret = recurse_read_control_structure (read_next_line_func, next,
1139 validator, closure);
d318976c
FN
1140 control_level--;
1141
1142 if (ret != simple_control)
1143 break;
1144 }
1145 }
1146
1147 dont_repeat ();
1148
1149 return ret;
1150}
1151
1152/* Read lines from the input stream and accumulate them in a chain of
1153 struct command_line's, which is then returned. For input from a
1154 terminal, the special command "end" is used to mark the end of the
311a4e6b
TJB
1155 input, and is not included in the returned chain of commands.
1156
1157 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1158 is always stripped) in the line and attempt to recognize GDB control
1159 commands. Otherwise, only "end" is recognized. */
d318976c
FN
1160
1161#define END_MESSAGE "End with a line saying just \"end\"."
1162
1163struct command_line *
a7bdde9e
VP
1164read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1165 void (*validator)(char *, void *), void *closure)
d318976c 1166{
3c1179ff 1167 struct command_line *head;
698ba934
DJ
1168
1169 if (from_tty && input_from_terminal_p ())
d318976c 1170 {
698ba934
DJ
1171 if (deprecated_readline_begin_hook)
1172 {
1173 /* Note - intentional to merge messages with no newline */
1174 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
1175 }
1176 else
1177 {
1178 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1179 gdb_flush (gdb_stdout);
1180 }
d318976c
FN
1181 }
1182
a7bdde9e
VP
1183 head = read_command_lines_1 (read_next_line, parse_commands,
1184 validator, closure);
3c1179ff
VP
1185
1186 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1187 {
1188 (*deprecated_readline_end_hook) ();
1189 }
1190 return (head);
1191}
1192
1193/* Act the same way as read_command_lines, except that each new line is
1194 obtained using READ_NEXT_LINE_FUNC. */
1195
1196struct command_line *
a7bdde9e
VP
1197read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1198 void (*validator)(char *, void *), void *closure)
3c1179ff
VP
1199{
1200 struct command_line *head, *tail, *next;
1201 struct cleanup *old_chain;
1202 enum command_control_type ret;
1203 enum misc_command_type val;
1204
1205 control_level = 0;
d318976c
FN
1206 head = tail = NULL;
1207 old_chain = NULL;
1208
1209 while (1)
1210 {
f429d7d0 1211 dont_repeat ();
a7bdde9e
VP
1212 val = process_next_line (read_next_line_func (), &next, parse_commands,
1213 validator, closure);
d318976c
FN
1214
1215 /* Ignore blank lines or comments. */
1216 if (val == nop_command)
1217 continue;
1218
1219 if (val == end_command)
1220 {
1221 ret = simple_control;
1222 break;
1223 }
1224
1225 if (val != ok_command)
1226 {
1227 ret = invalid_control;
1228 break;
1229 }
1230
1231 if (next->control_type == while_control
40c03ae8 1232 || next->control_type == if_control
d57a3c85 1233 || next->control_type == python_control
a7bdde9e
VP
1234 || next->control_type == commands_control
1235 || next->control_type == while_stepping_control)
d318976c
FN
1236 {
1237 control_level++;
a7bdde9e
VP
1238 ret = recurse_read_control_structure (read_next_line_func, next,
1239 validator, closure);
d318976c
FN
1240 control_level--;
1241
1242 if (ret == invalid_control)
1243 break;
1244 }
1245
1246 if (tail)
1247 {
1248 tail->next = next;
1249 }
1250 else
1251 {
1252 head = next;
1253 old_chain = make_cleanup_free_command_lines (&head);
1254 }
1255 tail = next;
1256 }
1257
1258 dont_repeat ();
1259
1260 if (head)
1261 {
1262 if (ret != invalid_control)
1263 {
1264 discard_cleanups (old_chain);
1265 }
1266 else
1267 do_cleanups (old_chain);
1268 }
1269
3c1179ff 1270 return head;
d318976c
FN
1271}
1272
1273/* Free a chain of struct command_line's. */
1274
1275void
1276free_command_lines (struct command_line **lptr)
1277{
d5b5ac79
AC
1278 struct command_line *l = *lptr;
1279 struct command_line *next;
d318976c
FN
1280 struct command_line **blist;
1281 int i;
1282
1283 while (l)
1284 {
1285 if (l->body_count > 0)
1286 {
1287 blist = l->body_list;
1288 for (i = 0; i < l->body_count; i++, blist++)
1289 free_command_lines (blist);
1290 }
1291 next = l->next;
b8c9b27d
KB
1292 xfree (l->line);
1293 xfree (l);
d318976c
FN
1294 l = next;
1295 }
0d70f41b 1296 *lptr = NULL;
d318976c
FN
1297}
1298
1299static void
1300do_free_command_lines_cleanup (void *arg)
1301{
1302 free_command_lines (arg);
1303}
1304
6c50ab1c 1305struct cleanup *
d318976c
FN
1306make_cleanup_free_command_lines (struct command_line **arg)
1307{
1308 return make_cleanup (do_free_command_lines_cleanup, arg);
1309}
c2b8ed2c
MS
1310
1311struct command_line *
1312copy_command_lines (struct command_line *cmds)
1313{
1314 struct command_line *result = NULL;
1315
1316 if (cmds)
1317 {
1318 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1319
1320 result->next = copy_command_lines (cmds->next);
1321 result->line = xstrdup (cmds->line);
1322 result->control_type = cmds->control_type;
1323 result->body_count = cmds->body_count;
1324 if (cmds->body_count > 0)
1325 {
1326 int i;
1327
1328 result->body_list = (struct command_line **)
1329 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1330
1331 for (i = 0; i < cmds->body_count; i++)
1332 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1333 }
1334 else
1335 result->body_list = NULL;
1336 }
1337
1338 return result;
1339}
d318976c 1340\f
adb483fe
DJ
1341/* Validate that *COMNAME is a valid name for a command. Return the
1342 containing command list, in case it starts with a prefix command.
1343 The prefix must already exist. *COMNAME is advanced to point after
1344 any prefix, and a NUL character overwrites the space after the
1345 prefix. */
1346
1347static struct cmd_list_element **
1348validate_comname (char **comname)
d318976c 1349{
adb483fe
DJ
1350 struct cmd_list_element **list = &cmdlist;
1351 char *p, *last_word;
d318976c 1352
adb483fe 1353 if (*comname == 0)
e2e0b3e5 1354 error_no_arg (_("name of command to define"));
d318976c 1355
adb483fe
DJ
1356 /* Find the last word of the argument. */
1357 p = *comname + strlen (*comname);
1358 while (p > *comname && isspace (p[-1]))
1359 p--;
1360 while (p > *comname && !isspace (p[-1]))
1361 p--;
1362 last_word = p;
1363
1364 /* Find the corresponding command list. */
1365 if (last_word != *comname)
1366 {
1367 struct cmd_list_element *c;
1368 char saved_char, *tem = *comname;
1369
1370 /* Separate the prefix and the command. */
1371 saved_char = last_word[-1];
1372 last_word[-1] = '\0';
1373
1374 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1375 if (c->prefixlist == NULL)
1376 error (_("\"%s\" is not a prefix command."), *comname);
1377
1378 list = c->prefixlist;
1379 last_word[-1] = saved_char;
1380 *comname = last_word;
1381 }
1382
1383 p = *comname;
d318976c
FN
1384 while (*p)
1385 {
1386 if (!isalnum (*p) && *p != '-' && *p != '_')
8a3fe4f8 1387 error (_("Junk in argument list: \"%s\""), p);
d318976c
FN
1388 p++;
1389 }
adb483fe
DJ
1390
1391 return list;
d318976c
FN
1392}
1393
1394/* This is just a placeholder in the command data structures. */
1395static void
1396user_defined_command (char *ignore, int from_tty)
1397{
1398}
1399
1400void
1401define_command (char *comname, int from_tty)
1402{
1403#define MAX_TMPBUF 128
1404 enum cmd_hook_type
1405 {
1406 CMD_NO_HOOK = 0,
1407 CMD_PRE_HOOK,
1408 CMD_POST_HOOK
1409 };
d5b5ac79 1410 struct command_line *cmds;
adb483fe
DJ
1411 struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
1412 char *tem, *tem2, *comfull;
d318976c
FN
1413 char tmpbuf[MAX_TMPBUF];
1414 int hook_type = CMD_NO_HOOK;
1415 int hook_name_size = 0;
1416
1417#define HOOK_STRING "hook-"
1418#define HOOK_LEN 5
1419#define HOOK_POST_STRING "hookpost-"
1420#define HOOK_POST_LEN 9
1421
adb483fe
DJ
1422 comfull = comname;
1423 list = validate_comname (&comname);
d318976c
FN
1424
1425 /* Look it up, and verify that we got an exact match. */
adb483fe
DJ
1426 tem = comname;
1427 c = lookup_cmd (&tem, *list, "", -1, 1);
5cb316ef 1428 if (c && strcmp (comname, c->name) != 0)
d318976c
FN
1429 c = 0;
1430
1431 if (c)
1432 {
ab4e3d93 1433 int q;
d318976c 1434 if (c->class == class_user || c->class == class_alias)
e2e0b3e5 1435 q = query (_("Redefine command \"%s\"? "), c->name);
d318976c 1436 else
e2e0b3e5 1437 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
ab4e3d93 1438 if (!q)
8a3fe4f8 1439 error (_("Command \"%s\" not redefined."), c->name);
d318976c
FN
1440 }
1441
1442 /* If this new command is a hook, then mark the command which it
1443 is hooking. Note that we allow hooking `help' commands, so that
1444 we can hook the `stop' pseudo-command. */
1445
1446 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1447 {
1448 hook_type = CMD_PRE_HOOK;
1449 hook_name_size = HOOK_LEN;
1450 }
1451 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1452 {
1453 hook_type = CMD_POST_HOOK;
1454 hook_name_size = HOOK_POST_LEN;
1455 }
1456
1457 if (hook_type != CMD_NO_HOOK)
1458 {
1459 /* Look up cmd it hooks, and verify that we got an exact match. */
1460 tem = comname + hook_name_size;
adb483fe 1461 hookc = lookup_cmd (&tem, *list, "", -1, 0);
5cb316ef 1462 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
d318976c
FN
1463 hookc = 0;
1464 if (!hookc)
1465 {
8a3fe4f8 1466 warning (_("Your new `%s' command does not hook any existing command."),
adb483fe 1467 comfull);
9e2f0ad4 1468 if (!query (_("Proceed? ")))
8a3fe4f8 1469 error (_("Not confirmed."));
d318976c
FN
1470 }
1471 }
1472
1b36a34b 1473 comname = xstrdup (comname);
d318976c
FN
1474
1475 /* If the rest of the commands will be case insensitive, this one
1476 should behave in the same manner. */
1477 for (tem = comname; *tem; tem++)
1478 if (isupper (*tem))
1479 *tem = tolower (*tem);
1480
adb483fe 1481 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
a7bdde9e 1482 cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
d318976c
FN
1483
1484 if (c && c->class == class_user)
1485 free_command_lines (&c->user_commands);
1486
1487 newc = add_cmd (comname, class_user, user_defined_command,
1488 (c && c->class == class_user)
1b36a34b 1489 ? c->doc : xstrdup ("User-defined."), list);
d318976c
FN
1490 newc->user_commands = cmds;
1491
1492 /* If this new command is a hook, then mark both commands as being
1493 tied. */
1494 if (hookc)
1495 {
1496 switch (hook_type)
1497 {
1498 case CMD_PRE_HOOK:
1499 hookc->hook_pre = newc; /* Target gets hooked. */
1500 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1501 break;
1502 case CMD_POST_HOOK:
f48ff60a
FN
1503 hookc->hook_post = newc; /* Target gets hooked. */
1504 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
d318976c
FN
1505 break;
1506 default:
1507 /* Should never come here as hookc would be 0. */
e2e0b3e5 1508 internal_error (__FILE__, __LINE__, _("bad switch"));
d318976c
FN
1509 }
1510 }
1511}
1512
1513void
1514document_command (char *comname, int from_tty)
1515{
1516 struct command_line *doclines;
adb483fe
DJ
1517 struct cmd_list_element *c, **list;
1518 char *tem, *comfull;
d318976c
FN
1519 char tmpbuf[128];
1520
adb483fe
DJ
1521 comfull = comname;
1522 list = validate_comname (&comname);
d318976c 1523
adb483fe
DJ
1524 tem = comname;
1525 c = lookup_cmd (&tem, *list, "", 0, 1);
d318976c
FN
1526
1527 if (c->class != class_user)
adb483fe 1528 error (_("Command \"%s\" is built-in."), comfull);
d318976c 1529
adb483fe 1530 sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
a7bdde9e 1531 doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
d318976c
FN
1532
1533 if (c->doc)
b8c9b27d 1534 xfree (c->doc);
d318976c
FN
1535
1536 {
d5b5ac79
AC
1537 struct command_line *cl1;
1538 int len = 0;
d318976c
FN
1539
1540 for (cl1 = doclines; cl1; cl1 = cl1->next)
1541 len += strlen (cl1->line) + 1;
1542
1543 c->doc = (char *) xmalloc (len + 1);
1544 *c->doc = 0;
1545
1546 for (cl1 = doclines; cl1; cl1 = cl1->next)
1547 {
1548 strcat (c->doc, cl1->line);
1549 if (cl1->next)
1550 strcat (c->doc, "\n");
1551 }
1552 }
1553
1554 free_command_lines (&doclines);
1555}
1556\f
1557struct source_cleanup_lines_args
1558{
1559 int old_line;
1560 char *old_file;
d318976c
FN
1561};
1562
1563static void
4efb68b1 1564source_cleanup_lines (void *args)
d318976c
FN
1565{
1566 struct source_cleanup_lines_args *p =
1567 (struct source_cleanup_lines_args *) args;
1568 source_line_number = p->old_line;
1569 source_file_name = p->old_file;
d318976c
FN
1570}
1571
17d92a02
AC
1572struct wrapped_read_command_file_args
1573{
1574 FILE *stream;
1575};
1576
1577static void
1578wrapped_read_command_file (struct ui_out *uiout, void *data)
1579{
1580 struct wrapped_read_command_file_args *args = data;
1581 read_command_file (args->stream);
1582}
1583
d318976c
FN
1584/* Used to implement source_command */
1585
1586void
1587script_from_file (FILE *stream, char *file)
1588{
1589 struct cleanup *old_cleanups;
1590 struct source_cleanup_lines_args old_lines;
1591 int needed_length;
1592
1593 if (stream == NULL)
e2e0b3e5 1594 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
d318976c 1595
7c8a8b04 1596 old_cleanups = make_cleanup_fclose (stream);
d318976c
FN
1597
1598 old_lines.old_line = source_line_number;
1599 old_lines.old_file = source_file_name;
d318976c
FN
1600 make_cleanup (source_cleanup_lines, &old_lines);
1601 source_line_number = 0;
1602 source_file_name = file;
d318976c
FN
1603 /* This will get set every time we read a line. So it won't stay "" for
1604 long. */
1605 error_pre_print = "";
1606
17d92a02 1607 {
71fff37b 1608 struct gdb_exception e;
17d92a02
AC
1609 struct wrapped_read_command_file_args args;
1610 args.stream = stream;
1611 e = catch_exception (uiout, wrapped_read_command_file, &args,
1612 RETURN_MASK_ERROR);
1613 switch (e.reason)
1614 {
1615 case 0:
1616 break;
1617 case RETURN_ERROR:
1618 /* Re-throw the error, but with the file name information
1619 prepended. */
109c3e39
AC
1620 throw_error (e.error,
1621 _("%s:%d: Error in sourced command file:\n%s"),
637537d0 1622 source_file_name, source_line_number, e.message);
17d92a02 1623 default:
e2e0b3e5 1624 internal_error (__FILE__, __LINE__, _("bad reason"));
17d92a02
AC
1625 }
1626 }
d318976c
FN
1627
1628 do_cleanups (old_cleanups);
1629}
1630
adb483fe
DJ
1631/* Print the definition of user command C to STREAM. Or, if C is a
1632 prefix command, show the definitions of all user commands under C
1633 (recursively). PREFIX and NAME combined are the name of the
1634 current command. */
d318976c 1635void
adb483fe
DJ
1636show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
1637 struct ui_file *stream)
d318976c 1638{
d5b5ac79 1639 struct command_line *cmdlines;
d318976c 1640
adb483fe
DJ
1641 if (c->prefixlist != NULL)
1642 {
1643 char *prefixname = c->prefixname;
1644 for (c = *c->prefixlist; c != NULL; c = c->next)
1645 if (c->class == class_user || c->prefixlist != NULL)
1646 show_user_1 (c, prefixname, c->name, gdb_stdout);
1647 return;
1648 }
1649
d318976c
FN
1650 cmdlines = c->user_commands;
1651 if (!cmdlines)
1652 return;
adb483fe 1653 fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
d318976c 1654
d318976c
FN
1655 print_command_lines (uiout, cmdlines, 1);
1656 fputs_filtered ("\n", stream);
d318976c
FN
1657}
1658
This page took 0.646707 seconds and 4 git commands to generate.