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