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