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