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