]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/cli/cli-script.c
gdb/
[thirdparty/binutils-gdb.git] / gdb / cli / cli-script.c
1 /* GDB CLI command scripting.
2
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, 2008,
5 2009, 2010 Free Software Foundation, Inc.
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 3 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, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include "value.h"
24 #include "language.h" /* For value_true */
25 #include <ctype.h>
26
27 #include "ui-out.h"
28 #include "gdb_string.h"
29 #include "exceptions.h"
30 #include "top.h"
31 #include "breakpoint.h"
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-script.h"
35 #include "gdb_assert.h"
36
37 #include "python/python.h"
38
39 /* Prototypes for local functions */
40
41 static enum command_control_type
42 recurse_read_control_structure (char * (*read_next_line_func) (void),
43 struct command_line *current_cmd,
44 void (*validator)(char *, void *),
45 void *closure);
46
47 static char *insert_args (char *line);
48
49 static struct cleanup * setup_user_args (char *p);
50
51 static char *read_next_line (void);
52
53 /* Level of control structure when reading. */
54 static int control_level;
55
56 /* Level of control structure when executing. */
57 static int command_nest_depth = 1;
58
59 /* This is to prevent certain commands being printed twice. */
60 static int suppress_next_print_command_trace = 0;
61
62 /* Structure for arguments to user defined functions. */
63 #define MAXUSERARGS 10
64 struct user_args
65 {
66 struct user_args *next;
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;
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
84 static struct command_line *
85 build_command_line (enum command_control_type type, char *args)
86 {
87 struct command_line *cmd;
88
89 if (args == NULL && (type == if_control || type == while_control))
90 error (_("if/while commands require arguments."));
91 gdb_assert (args != NULL);
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);
102 cmd->line = xstrdup (args);
103
104 return cmd;
105 }
106
107 /* Build and return a new command structure for the control commands
108 such as "if" and "while". */
109
110 struct command_line *
111 get_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. */
122 if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
123 == invalid_control)
124 {
125 warning (_("Error reading in canned sequence of commands."));
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). */
135
136 void
137 print_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. */
178 if (list->control_type == while_control
179 || list->control_type == while_stepping_control)
180 {
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);
188 ui_out_text (uiout, "\n");
189 print_command_lines (uiout, *list->body_list, depth + 1);
190 if (depth)
191 ui_out_spaces (uiout, 2 * depth);
192 ui_out_field_string (uiout, NULL, "end");
193 ui_out_text (uiout, "\n");
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 {
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");
212 ui_out_text (uiout, "\n");
213 print_command_lines (uiout, list->body_list[1], depth + 1);
214 }
215
216 if (depth)
217 ui_out_spaces (uiout, 2 * depth);
218 ui_out_field_string (uiout, NULL, "end");
219 ui_out_text (uiout, "\n");
220 list = list->next;
221 continue;
222 }
223
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
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
255 /* ignore illegal command type and try next */
256 list = list->next;
257 } /* while (list) */
258 }
259
260 /* Handle pre-post hooks. */
261
262 static void
263 clear_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
269 void
270 execute_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
281 void
282 execute_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
293 /* Execute the command in CMD. */
294 static void
295 do_restore_user_call_depth (void * call_depth)
296 {
297 int * depth = call_depth;
298 (*depth)--;
299 if ((*depth) == 0)
300 in_user_command = 0;
301 }
302
303
304 void
305 execute_user_command (struct cmd_list_element *c, char *args)
306 {
307 struct command_line *cmdlines;
308 struct cleanup *old_chain;
309 enum command_control_type ret;
310 static int user_call_depth = 0;
311 extern int max_user_call_depth;
312
313 old_chain = setup_user_args (args);
314
315 cmdlines = c->user_commands;
316 if (cmdlines == 0)
317 /* Null command */
318 return;
319
320 if (++user_call_depth > max_user_call_depth)
321 error (_("Max user call depth exceeded -- command aborted."));
322
323 make_cleanup (do_restore_user_call_depth, &user_call_depth);
324
325 /* Set the instream to 0, indicating execution of a
326 user-defined function. */
327 make_cleanup (do_restore_instream_cleanup, instream);
328 instream = (FILE *) 0;
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
334 command_nest_depth++;
335 while (cmdlines)
336 {
337 ret = execute_control_command (cmdlines);
338 if (ret != simple_control && ret != break_control)
339 {
340 warning (_("Error executing canned sequence of commands."));
341 break;
342 }
343 cmdlines = cmdlines->next;
344 }
345 command_nest_depth--;
346 do_cleanups (old_chain);
347 }
348
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
352 void
353 reset_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. */
369 void
370 print_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
389 enum command_control_type
390 execute_control_command (struct command_line *cmd)
391 {
392 struct expression *expr;
393 struct command_line *current;
394 struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
395 struct value *val;
396 struct value *val_mark;
397 int loop;
398 enum command_control_type ret;
399 char *new_line;
400
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
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)
411 break;
412 make_cleanup (free_current_contents, &new_line);
413 execute_command (new_line, 0);
414 ret = cmd->control_type;
415 break;
416
417 case continue_control:
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
425 case break_control:
426 print_command_trace ("loop_break");
427
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 {
435 char *buffer = alloca (strlen (cmd->line) + 7);
436 sprintf (buffer, "while %s", cmd->line);
437 print_command_trace (buffer);
438
439 /* Parse the loop control expression for the while statement. */
440 new_line = insert_args (cmd->line);
441 if (!new_line)
442 break;
443 make_cleanup (free_current_contents, &new_line);
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 {
471 command_nest_depth++;
472 ret = execute_control_command (current);
473 command_nest_depth--;
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 {
502 char *buffer = alloca (strlen (cmd->line) + 4);
503 sprintf (buffer, "if %s", cmd->line);
504 print_command_trace (buffer);
505
506 new_line = insert_args (cmd->line);
507 if (!new_line)
508 break;
509 make_cleanup (free_current_contents, &new_line);
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 {
532 command_nest_depth++;
533 ret = execute_control_command (current);
534 command_nest_depth--;
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 }
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 }
557 case python_control:
558 {
559 eval_python_from_control_command (cmd);
560 ret = simple_control;
561 break;
562 }
563
564 default:
565 warning (_("Invalid control type in canned commands structure."));
566 break;
567 }
568
569 do_cleanups (old_chain);
570
571 return ret;
572 }
573
574 /* Like execute_control_command, but first set
575 suppress_next_print_command_trace. */
576
577 enum command_control_type
578 execute_control_command_untraced (struct command_line *cmd)
579 {
580 suppress_next_print_command_trace = 1;
581 return execute_control_command (cmd);
582 }
583
584
585 /* "while" command support. Executes a body of statements while the
586 loop condition is nonzero. */
587
588 void
589 while_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
599 execute_control_command_untraced (command);
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
606 void
607 if_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
617 execute_control_command_untraced (command);
618 free_command_lines (&command);
619 }
620
621 /* Cleanup */
622 static void
623 arg_cleanup (void *ignore)
624 {
625 struct user_args *oargs = user_args;
626 if (!user_args)
627 internal_error (__FILE__, __LINE__,
628 _("arg_cleanup called with no user args.\n"));
629
630 user_args = user_args->next;
631 xfree (oargs->command);
632 xfree (oargs);
633 }
634
635 /* Bind the incomming arguments for a user defined command to
636 $arg0, $arg1 ... $argMAXUSERARGS. */
637
638 static struct cleanup *
639 setup_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
656 user_args->command = p = xstrdup (p);
657
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 {
667 error (_("user defined function may only have %d arguments."),
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
722 static char *
723 locate_arg (char *p)
724 {
725 while ((p = strchr (p, '$')))
726 {
727 if (strncmp (p, "$arg", 4) == 0
728 && (isdigit (p[4]) || p[4] == 'c'))
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
738 static char *
739 insert_args (char *line)
740 {
741 char *p, *save_line, *new_line;
742 unsigned len, i;
743
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
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
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)
763 {
764 error (_("Missing argument %d in user function."), i);
765 return NULL;
766 }
767 else
768 {
769 len += user_args->a[i].len;
770 }
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;
794
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
807 {
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 }
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
830 static void
831 realloc_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);
846 memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
847
848 xfree (command->body_list);
849 command->body_list = body_list;
850 command->body_count = new_length;
851 }
852
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. */
856
857 static char *
858 read_next_line (void)
859 {
860 char *prompt_ptr, control_prompt[256];
861 int i = 0;
862
863 if (control_level >= 254)
864 error (_("Control nesting too deep!"));
865
866 /* Set a prompt based on the nesting of the control commands. */
867 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
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
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
888 static enum misc_command_type
889 process_next_line (char *p, struct command_line **command, int parse_commands,
890 void (*validator)(char *, void *), void *closure)
891 {
892 char *p_end;
893 char *p_start;
894 int not_handled = 0;
895
896 /* Not sure what to do here. */
897 if (p == NULL)
898 return end_command;
899
900 /* Strip trailing whitespace. */
901 p_end = p + strlen (p);
902 while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
903 p_end--;
904
905 p_start = p;
906 /* Strip leading whitespace. */
907 while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
908 p_start++;
909
910 /* 'end' is always recognized, regardless of parse_commands value.
911 We also permit whitespace before end and after. */
912 if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
913 return end_command;
914
915 if (parse_commands)
916 {
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. */
920 p = p_start;
921
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. */
925 if (p_end == p || p[0] == '#')
926 return nop_command;
927
928 /* Is the else clause of an if control structure? */
929 if (p_end - p == 4 && !strncmp (p, "else", 4))
930 return else_command;
931
932 /* Check for while, if, break, continue, etc and build a new command
933 line structure for them. */
934 if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
935 || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
936 || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
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))
951 {
952 char *first_arg;
953 first_arg = p + 5;
954 while (first_arg < p_end && isspace (*first_arg))
955 first_arg++;
956 *command = build_command_line (while_control, first_arg);
957 }
958 else if (p_end - p > 2 && !strncmp (p, "if", 2))
959 {
960 char *first_arg;
961 first_arg = p + 2;
962 while (first_arg < p_end && isspace (*first_arg))
963 first_arg++;
964 *command = build_command_line (if_control, first_arg);
965 }
966 else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
967 {
968 char *first_arg;
969 first_arg = p + 8;
970 while (first_arg < p_end && isspace (*first_arg))
971 first_arg++;
972 *command = build_command_line (commands_control, first_arg);
973 }
974 else if (p_end - p == 6 && !strncmp (p, "python", 6))
975 {
976 /* Note that we ignore the inline "python command" form
977 here. */
978 *command = build_command_line (python_control, "");
979 }
980 else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
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 }
990 else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
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;
1002 }
1003
1004 if (!parse_commands || not_handled)
1005 {
1006 /* A normal command. */
1007 *command = (struct command_line *)
1008 xmalloc (sizeof (struct command_line));
1009 (*command)->next = NULL;
1010 (*command)->line = savestring (p, p_end - p);
1011 (*command)->control_type = simple_control;
1012 (*command)->body_count = 0;
1013 (*command)->body_list = NULL;
1014 }
1015
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
1030 /* Nothing special. */
1031 return ok_command;
1032 }
1033
1034 /* Recursively read in the control structures and create a command_line
1035 structure from them. Use read_next_line_func to obtain lines of
1036 the command.
1037
1038 */
1039
1040 static enum command_control_type
1041 recurse_read_control_structure (char * (*read_next_line_func) (void),
1042 struct command_line *current_cmd,
1043 void (*validator)(char *, void *),
1044 void *closure)
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;
1050 char *p;
1051
1052 child_tail = NULL;
1053 current_body = 1;
1054
1055 /* Sanity checks. */
1056 if (current_cmd->control_type == simple_control)
1057 error (_("Recursed on a simple control type."));
1058
1059 if (current_body > current_cmd->body_count)
1060 error (_("Allocated body is smaller than this command type needs."));
1061
1062 /* Read lines from the input stream and build control structures. */
1063 while (1)
1064 {
1065 dont_repeat ();
1066
1067 next = NULL;
1068 val = process_next_line (read_next_line_func (), &next,
1069 current_cmd->control_type != python_control,
1070 validator, closure);
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
1079 || current_cmd->control_type == while_stepping_control
1080 || current_cmd->control_type == if_control
1081 || current_cmd->control_type == python_control
1082 || current_cmd->control_type == commands_control)
1083 {
1084 /* Success reading an entire canned sequence of commands. */
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
1132 || next->control_type == while_stepping_control
1133 || next->control_type == if_control
1134 || next->control_type == python_control
1135 || next->control_type == commands_control)
1136 {
1137 control_level++;
1138 ret = recurse_read_control_structure (read_next_line_func, next,
1139 validator, closure);
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
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. */
1160
1161 #define END_MESSAGE "End with a line saying just \"end\"."
1162
1163 struct command_line *
1164 read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1165 void (*validator)(char *, void *), void *closure)
1166 {
1167 struct command_line *head;
1168
1169 if (from_tty && input_from_terminal_p ())
1170 {
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 }
1181 }
1182
1183 head = read_command_lines_1 (read_next_line, parse_commands,
1184 validator, closure);
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
1196 struct command_line *
1197 read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1198 void (*validator)(char *, void *), void *closure)
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;
1206 head = tail = NULL;
1207 old_chain = NULL;
1208
1209 while (1)
1210 {
1211 dont_repeat ();
1212 val = process_next_line (read_next_line_func (), &next, parse_commands,
1213 validator, closure);
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
1232 || next->control_type == if_control
1233 || next->control_type == python_control
1234 || next->control_type == commands_control
1235 || next->control_type == while_stepping_control)
1236 {
1237 control_level++;
1238 ret = recurse_read_control_structure (read_next_line_func, next,
1239 validator, closure);
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
1270 return head;
1271 }
1272
1273 /* Free a chain of struct command_line's. */
1274
1275 void
1276 free_command_lines (struct command_line **lptr)
1277 {
1278 struct command_line *l = *lptr;
1279 struct command_line *next;
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;
1292 xfree (l->line);
1293 xfree (l);
1294 l = next;
1295 }
1296 *lptr = NULL;
1297 }
1298
1299 static void
1300 do_free_command_lines_cleanup (void *arg)
1301 {
1302 free_command_lines (arg);
1303 }
1304
1305 struct cleanup *
1306 make_cleanup_free_command_lines (struct command_line **arg)
1307 {
1308 return make_cleanup (do_free_command_lines_cleanup, arg);
1309 }
1310
1311 struct command_line *
1312 copy_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 }
1340 \f
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
1347 static struct cmd_list_element **
1348 validate_comname (char **comname)
1349 {
1350 struct cmd_list_element **list = &cmdlist;
1351 char *p, *last_word;
1352
1353 if (*comname == 0)
1354 error_no_arg (_("name of command to define"));
1355
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;
1384 while (*p)
1385 {
1386 if (!isalnum (*p) && *p != '-' && *p != '_')
1387 error (_("Junk in argument list: \"%s\""), p);
1388 p++;
1389 }
1390
1391 return list;
1392 }
1393
1394 /* This is just a placeholder in the command data structures. */
1395 static void
1396 user_defined_command (char *ignore, int from_tty)
1397 {
1398 }
1399
1400 void
1401 define_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 };
1410 struct command_line *cmds;
1411 struct cmd_list_element *c, *newc, *oldc, *hookc = 0, **list;
1412 char *tem, *tem2, *comfull;
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
1422 comfull = comname;
1423 list = validate_comname (&comname);
1424
1425 /* Look it up, and verify that we got an exact match. */
1426 tem = comname;
1427 c = lookup_cmd (&tem, *list, "", -1, 1);
1428 if (c && strcmp (comname, c->name) != 0)
1429 c = 0;
1430
1431 if (c)
1432 {
1433 int q;
1434 if (c->class == class_user || c->class == class_alias)
1435 q = query (_("Redefine command \"%s\"? "), c->name);
1436 else
1437 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1438 if (!q)
1439 error (_("Command \"%s\" not redefined."), c->name);
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;
1461 hookc = lookup_cmd (&tem, *list, "", -1, 0);
1462 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1463 hookc = 0;
1464 if (!hookc)
1465 {
1466 warning (_("Your new `%s' command does not hook any existing command."),
1467 comfull);
1468 if (!query (_("Proceed? ")))
1469 error (_("Not confirmed."));
1470 }
1471 }
1472
1473 comname = xstrdup (comname);
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
1481 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
1482 cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
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)
1489 ? c->doc : xstrdup ("User-defined."), list);
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:
1503 hookc->hook_post = newc; /* Target gets hooked. */
1504 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
1505 break;
1506 default:
1507 /* Should never come here as hookc would be 0. */
1508 internal_error (__FILE__, __LINE__, _("bad switch"));
1509 }
1510 }
1511 }
1512
1513 void
1514 document_command (char *comname, int from_tty)
1515 {
1516 struct command_line *doclines;
1517 struct cmd_list_element *c, **list;
1518 char *tem, *comfull;
1519 char tmpbuf[128];
1520
1521 comfull = comname;
1522 list = validate_comname (&comname);
1523
1524 tem = comname;
1525 c = lookup_cmd (&tem, *list, "", 0, 1);
1526
1527 if (c->class != class_user)
1528 error (_("Command \"%s\" is built-in."), comfull);
1529
1530 sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
1531 doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
1532
1533 if (c->doc)
1534 xfree (c->doc);
1535
1536 {
1537 struct command_line *cl1;
1538 int len = 0;
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
1557 struct source_cleanup_lines_args
1558 {
1559 int old_line;
1560 char *old_file;
1561 };
1562
1563 static void
1564 source_cleanup_lines (void *args)
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;
1570 }
1571
1572 struct wrapped_read_command_file_args
1573 {
1574 FILE *stream;
1575 };
1576
1577 static void
1578 wrapped_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
1584 /* Used to implement source_command */
1585
1586 void
1587 script_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)
1594 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1595
1596 old_cleanups = make_cleanup_fclose (stream);
1597
1598 old_lines.old_line = source_line_number;
1599 old_lines.old_file = source_file_name;
1600 make_cleanup (source_cleanup_lines, &old_lines);
1601 source_line_number = 0;
1602 source_file_name = file;
1603 /* This will get set every time we read a line. So it won't stay "" for
1604 long. */
1605 error_pre_print = "";
1606
1607 {
1608 struct gdb_exception e;
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. */
1620 throw_error (e.error,
1621 _("%s:%d: Error in sourced command file:\n%s"),
1622 source_file_name, source_line_number, e.message);
1623 default:
1624 internal_error (__FILE__, __LINE__, _("bad reason"));
1625 }
1626 }
1627
1628 do_cleanups (old_cleanups);
1629 }
1630
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. */
1635 void
1636 show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
1637 struct ui_file *stream)
1638 {
1639 struct command_line *cmdlines;
1640
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
1650 cmdlines = c->user_commands;
1651 if (!cmdlines)
1652 return;
1653 fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
1654
1655 print_command_lines (uiout, cmdlines, 1);
1656 fputs_filtered ("\n", stream);
1657 }
1658