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