24729b1b1e2fd8e6f420b19ed3ddcf197b50e436
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
1 /* GDB CLI command scripting.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22 #include "defs.h"
23 #include "value.h"
24 #include "language.h" /* For value_true */
25 #include <ctype.h>
26
27 #ifdef UI_OUT
28 #include "ui-out.h"
29 #endif
30
31 #include "top.h"
32 #include "cli/cli-cmds.h"
33 #include "cli/cli-decode.h"
34 #include "cli/cli-script.h"
35
36 /* From gdb/top.c */
37
38 extern void dont_repeat (void);
39
40 extern void do_restore_instream_cleanup (void *stream);
41
42 /* Prototypes for local functions */
43
44 static struct cleanup *
45 make_cleanup_free_command_lines (struct command_line **arg);
46
47 static enum command_control_type
48 recurse_read_control_structure (struct command_line *current_cmd);
49
50 static char *insert_args (char *line);
51
52 static struct cleanup * setup_user_args (char *p);
53
54 static void validate_comname (char *);
55
56 /* Level of control structure. */
57 static int control_level;
58
59 /* Source command state variable. */
60 static int source_error_allocated;
61
62 /* Structure for arguments to user defined functions. */
63 #define MAXUSERARGS 10
64 struct user_args
65 {
66 struct user_args *next;
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)
87 error ("if/while commands require arguments.\n");
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 control structure\n");
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 #ifdef UI_OUT
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_text (uiout, "while ");
175 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
176 ui_out_text (uiout, "\n");
177 print_command_lines (uiout, *list->body_list, depth + 1);
178 ui_out_field_string (uiout, NULL, "end");
179 if (depth)
180 ui_out_spaces (uiout, 2 * depth);
181 ui_out_text (uiout, "end\n");
182 list = list->next;
183 continue;
184 }
185
186 /* An if command. Recursively print both arms before continueing. */
187 if (list->control_type == if_control)
188 {
189 ui_out_text (uiout, "if ");
190 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
191 ui_out_text (uiout, "\n");
192 /* The true arm. */
193 print_command_lines (uiout, list->body_list[0], depth + 1);
194
195 /* Show the false arm if it exists. */
196 if (list->body_count == 2)
197 {
198 if (depth)
199 ui_out_spaces (uiout, 2 * depth);
200 ui_out_field_string (uiout, NULL, "else");
201 ui_out_text (uiout, "else\n");
202 print_command_lines (uiout, list->body_list[1], depth + 1);
203 }
204
205 ui_out_field_string (uiout, NULL, "end");
206 if (depth)
207 ui_out_spaces (uiout, 2 * depth);
208 ui_out_text (uiout, "end\n");
209 list = list->next;
210 continue;
211 }
212
213 /* ignore illegal command type and try next */
214 list = list->next;
215 } /* while (list) */
216 }
217 #else
218 void
219 print_command_line (struct command_line *cmd, unsigned int depth,
220 struct ui_file *stream)
221 {
222 unsigned int i;
223
224 if (depth)
225 {
226 for (i = 0; i < depth; i++)
227 fputs_filtered (" ", stream);
228 }
229
230 /* A simple command, print it and return. */
231 if (cmd->control_type == simple_control)
232 {
233 fputs_filtered (cmd->line, stream);
234 fputs_filtered ("\n", stream);
235 return;
236 }
237
238 /* loop_continue to jump to the start of a while loop, print it
239 and return. */
240 if (cmd->control_type == continue_control)
241 {
242 fputs_filtered ("loop_continue\n", stream);
243 return;
244 }
245
246 /* loop_break to break out of a while loop, print it and return. */
247 if (cmd->control_type == break_control)
248 {
249 fputs_filtered ("loop_break\n", stream);
250 return;
251 }
252
253 /* A while command. Recursively print its subcommands before returning. */
254 if (cmd->control_type == while_control)
255 {
256 struct command_line *list;
257 fputs_filtered ("while ", stream);
258 fputs_filtered (cmd->line, stream);
259 fputs_filtered ("\n", stream);
260 list = *cmd->body_list;
261 while (list)
262 {
263 print_command_line (list, depth + 1, stream);
264 list = list->next;
265 }
266 }
267
268 /* An if command. Recursively print both arms before returning. */
269 if (cmd->control_type == if_control)
270 {
271 fputs_filtered ("if ", stream);
272 fputs_filtered (cmd->line, stream);
273 fputs_filtered ("\n", stream);
274 /* The true arm. */
275 print_command_line (cmd->body_list[0], depth + 1, stream);
276
277 /* Show the false arm if it exists. */
278 if (cmd->body_count == 2)
279 {
280 if (depth)
281 {
282 for (i = 0; i < depth; i++)
283 fputs_filtered (" ", stream);
284 }
285 fputs_filtered ("else\n", stream);
286 print_command_line (cmd->body_list[1], depth + 1, stream);
287 }
288 if (depth)
289 {
290 for (i = 0; i < depth; i++)
291 fputs_filtered (" ", stream);
292 }
293 fputs_filtered ("end\n", stream);
294 }
295 }
296 #endif
297
298 /* Execute the command in CMD. */
299
300 void
301 execute_user_command (struct cmd_list_element *c, char *args)
302 {
303 register struct command_line *cmdlines;
304 struct cleanup *old_chain;
305 enum command_control_type ret;
306
307 old_chain = setup_user_args (args);
308
309 cmdlines = c->user_commands;
310 if (cmdlines == 0)
311 /* Null command */
312 return;
313
314 /* Set the instream to 0, indicating execution of a
315 user-defined function. */
316 old_chain = make_cleanup (do_restore_instream_cleanup, instream);
317 instream = (FILE *) 0;
318 while (cmdlines)
319 {
320 ret = execute_control_command (cmdlines);
321 if (ret != simple_control && ret != break_control)
322 {
323 warning ("Error in control structure.\n");
324 break;
325 }
326 cmdlines = cmdlines->next;
327 }
328 do_cleanups (old_chain);
329 }
330
331 enum command_control_type
332 execute_control_command (struct command_line *cmd)
333 {
334 struct expression *expr;
335 struct command_line *current;
336 struct cleanup *old_chain = 0;
337 value_ptr val;
338 value_ptr val_mark;
339 int loop;
340 enum command_control_type ret;
341 char *new_line;
342
343 switch (cmd->control_type)
344 {
345 case simple_control:
346 /* A simple command, execute it and return. */
347 new_line = insert_args (cmd->line);
348 if (!new_line)
349 return invalid_control;
350 old_chain = make_cleanup (free_current_contents, &new_line);
351 execute_command (new_line, 0);
352 ret = cmd->control_type;
353 break;
354
355 case continue_control:
356 case break_control:
357 /* Return for "continue", and "break" so we can either
358 continue the loop at the top, or break out. */
359 ret = cmd->control_type;
360 break;
361
362 case while_control:
363 {
364 /* Parse the loop control expression for the while statement. */
365 new_line = insert_args (cmd->line);
366 if (!new_line)
367 return invalid_control;
368 old_chain = make_cleanup (free_current_contents, &new_line);
369 expr = parse_expression (new_line);
370 make_cleanup (free_current_contents, &expr);
371
372 ret = simple_control;
373 loop = 1;
374
375 /* Keep iterating so long as the expression is true. */
376 while (loop == 1)
377 {
378 int cond_result;
379
380 QUIT;
381
382 /* Evaluate the expression. */
383 val_mark = value_mark ();
384 val = evaluate_expression (expr);
385 cond_result = value_true (val);
386 value_free_to_mark (val_mark);
387
388 /* If the value is false, then break out of the loop. */
389 if (!cond_result)
390 break;
391
392 /* Execute the body of the while statement. */
393 current = *cmd->body_list;
394 while (current)
395 {
396 ret = execute_control_command (current);
397
398 /* If we got an error, or a "break" command, then stop
399 looping. */
400 if (ret == invalid_control || ret == break_control)
401 {
402 loop = 0;
403 break;
404 }
405
406 /* If we got a "continue" command, then restart the loop
407 at this point. */
408 if (ret == continue_control)
409 break;
410
411 /* Get the next statement. */
412 current = current->next;
413 }
414 }
415
416 /* Reset RET so that we don't recurse the break all the way down. */
417 if (ret == break_control)
418 ret = simple_control;
419
420 break;
421 }
422
423 case if_control:
424 {
425 new_line = insert_args (cmd->line);
426 if (!new_line)
427 return invalid_control;
428 old_chain = make_cleanup (free_current_contents, &new_line);
429 /* Parse the conditional for the if statement. */
430 expr = parse_expression (new_line);
431 make_cleanup (free_current_contents, &expr);
432
433 current = NULL;
434 ret = simple_control;
435
436 /* Evaluate the conditional. */
437 val_mark = value_mark ();
438 val = evaluate_expression (expr);
439
440 /* Choose which arm to take commands from based on the value of the
441 conditional expression. */
442 if (value_true (val))
443 current = *cmd->body_list;
444 else if (cmd->body_count == 2)
445 current = *(cmd->body_list + 1);
446 value_free_to_mark (val_mark);
447
448 /* Execute commands in the given arm. */
449 while (current)
450 {
451 ret = execute_control_command (current);
452
453 /* If we got an error, get out. */
454 if (ret != simple_control)
455 break;
456
457 /* Get the next statement in the body. */
458 current = current->next;
459 }
460
461 break;
462 }
463
464 default:
465 warning ("Invalid control type in command structure.");
466 return invalid_control;
467 }
468
469 if (old_chain)
470 do_cleanups (old_chain);
471
472 return ret;
473 }
474
475 /* "while" command support. Executes a body of statements while the
476 loop condition is nonzero. */
477
478 void
479 while_command (char *arg, int from_tty)
480 {
481 struct command_line *command = NULL;
482
483 control_level = 1;
484 command = get_command_line (while_control, arg);
485
486 if (command == NULL)
487 return;
488
489 execute_control_command (command);
490 free_command_lines (&command);
491 }
492
493 /* "if" command support. Execute either the true or false arm depending
494 on the value of the if conditional. */
495
496 void
497 if_command (char *arg, int from_tty)
498 {
499 struct command_line *command = NULL;
500
501 control_level = 1;
502 command = get_command_line (if_control, arg);
503
504 if (command == NULL)
505 return;
506
507 execute_control_command (command);
508 free_command_lines (&command);
509 }
510
511 /* Cleanup */
512 static void
513 arg_cleanup (void *ignore)
514 {
515 struct user_args *oargs = user_args;
516 if (!user_args)
517 internal_error (__FILE__, __LINE__,
518 "arg_cleanup called with no user args.\n");
519
520 user_args = user_args->next;
521 xfree (oargs);
522 }
523
524 /* Bind the incomming arguments for a user defined command to
525 $arg0, $arg1 ... $argMAXUSERARGS. */
526
527 static struct cleanup *
528 setup_user_args (char *p)
529 {
530 struct user_args *args;
531 struct cleanup *old_chain;
532 unsigned int arg_count = 0;
533
534 args = (struct user_args *) xmalloc (sizeof (struct user_args));
535 memset (args, 0, sizeof (struct user_args));
536
537 args->next = user_args;
538 user_args = args;
539
540 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
541
542 if (p == NULL)
543 return old_chain;
544
545 while (*p)
546 {
547 char *start_arg;
548 int squote = 0;
549 int dquote = 0;
550 int bsquote = 0;
551
552 if (arg_count >= MAXUSERARGS)
553 {
554 error ("user defined function may only have %d arguments.\n",
555 MAXUSERARGS);
556 return old_chain;
557 }
558
559 /* Strip whitespace. */
560 while (*p == ' ' || *p == '\t')
561 p++;
562
563 /* P now points to an argument. */
564 start_arg = p;
565 user_args->a[arg_count].arg = p;
566
567 /* Get to the end of this argument. */
568 while (*p)
569 {
570 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
571 break;
572 else
573 {
574 if (bsquote)
575 bsquote = 0;
576 else if (*p == '\\')
577 bsquote = 1;
578 else if (squote)
579 {
580 if (*p == '\'')
581 squote = 0;
582 }
583 else if (dquote)
584 {
585 if (*p == '"')
586 dquote = 0;
587 }
588 else
589 {
590 if (*p == '\'')
591 squote = 1;
592 else if (*p == '"')
593 dquote = 1;
594 }
595 p++;
596 }
597 }
598
599 user_args->a[arg_count].len = p - start_arg;
600 arg_count++;
601 user_args->count++;
602 }
603 return old_chain;
604 }
605
606 /* Given character string P, return a point to the first argument ($arg),
607 or NULL if P contains no arguments. */
608
609 static char *
610 locate_arg (char *p)
611 {
612 while ((p = strchr (p, '$')))
613 {
614 if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4]))
615 return p;
616 p++;
617 }
618 return NULL;
619 }
620
621 /* Insert the user defined arguments stored in user_arg into the $arg
622 arguments found in line, with the updated copy being placed into nline. */
623
624 static char *
625 insert_args (char *line)
626 {
627 char *p, *save_line, *new_line;
628 unsigned len, i;
629
630 /* First we need to know how much memory to allocate for the new line. */
631 save_line = line;
632 len = 0;
633 while ((p = locate_arg (line)))
634 {
635 len += p - line;
636 i = p[4] - '0';
637
638 if (i >= user_args->count)
639 {
640 error ("Missing argument %d in user function.\n", i);
641 return NULL;
642 }
643 len += user_args->a[i].len;
644 line = p + 5;
645 }
646
647 /* Don't forget the tail. */
648 len += strlen (line);
649
650 /* Allocate space for the new line and fill it in. */
651 new_line = (char *) xmalloc (len + 1);
652 if (new_line == NULL)
653 return NULL;
654
655 /* Restore pointer to beginning of old line. */
656 line = save_line;
657
658 /* Save pointer to beginning of new line. */
659 save_line = new_line;
660
661 while ((p = locate_arg (line)))
662 {
663 int i, len;
664
665 memcpy (new_line, line, p - line);
666 new_line += p - line;
667 i = p[4] - '0';
668
669 len = user_args->a[i].len;
670 if (len)
671 {
672 memcpy (new_line, user_args->a[i].arg, len);
673 new_line += len;
674 }
675 line = p + 5;
676 }
677 /* Don't forget the tail. */
678 strcpy (new_line, line);
679
680 /* Return a pointer to the beginning of the new line. */
681 return save_line;
682 }
683
684 \f
685 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
686 code bodies. This is typically used when we encounter an "else"
687 clause for an "if" command. */
688
689 static void
690 realloc_body_list (struct command_line *command, int new_length)
691 {
692 int n;
693 struct command_line **body_list;
694
695 n = command->body_count;
696
697 /* Nothing to do? */
698 if (new_length <= n)
699 return;
700
701 body_list = (struct command_line **)
702 xmalloc (sizeof (struct command_line *) * new_length);
703
704 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
705
706 xfree (command->body_list);
707 command->body_list = body_list;
708 command->body_count = new_length;
709 }
710
711 /* Read one line from the input stream. If the command is an "else" or
712 "end", return such an indication to the caller. */
713
714 static enum misc_command_type
715 read_next_line (struct command_line **command)
716 {
717 char *p, *p1, *prompt_ptr, control_prompt[256];
718 int i = 0;
719
720 if (control_level >= 254)
721 error ("Control nesting too deep!\n");
722
723 /* Set a prompt based on the nesting of the control commands. */
724 if (instream == stdin || (instream == 0 && readline_hook != NULL))
725 {
726 for (i = 0; i < control_level; i++)
727 control_prompt[i] = ' ';
728 control_prompt[i] = '>';
729 control_prompt[i + 1] = '\0';
730 prompt_ptr = (char *) &control_prompt[0];
731 }
732 else
733 prompt_ptr = NULL;
734
735 p = command_line_input (prompt_ptr, instream == stdin, "commands");
736
737 /* Not sure what to do here. */
738 if (p == NULL)
739 return end_command;
740
741 /* Strip leading and trailing whitespace. */
742 while (*p == ' ' || *p == '\t')
743 p++;
744
745 p1 = p + strlen (p);
746 while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t'))
747 p1--;
748
749 /* Blanks and comments don't really do anything, but we need to
750 distinguish them from else, end and other commands which can be
751 executed. */
752 if (p1 == p || p[0] == '#')
753 return nop_command;
754
755 /* Is this the end of a simple, while, or if control structure? */
756 if (p1 - p == 3 && !strncmp (p, "end", 3))
757 return end_command;
758
759 /* Is the else clause of an if control structure? */
760 if (p1 - p == 4 && !strncmp (p, "else", 4))
761 return else_command;
762
763 /* Check for while, if, break, continue, etc and build a new command
764 line structure for them. */
765 if (p1 - p > 5 && !strncmp (p, "while", 5))
766 *command = build_command_line (while_control, p + 6);
767 else if (p1 - p > 2 && !strncmp (p, "if", 2))
768 *command = build_command_line (if_control, p + 3);
769 else if (p1 - p == 10 && !strncmp (p, "loop_break", 10))
770 {
771 *command = (struct command_line *)
772 xmalloc (sizeof (struct command_line));
773 (*command)->next = NULL;
774 (*command)->line = NULL;
775 (*command)->control_type = break_control;
776 (*command)->body_count = 0;
777 (*command)->body_list = NULL;
778 }
779 else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13))
780 {
781 *command = (struct command_line *)
782 xmalloc (sizeof (struct command_line));
783 (*command)->next = NULL;
784 (*command)->line = NULL;
785 (*command)->control_type = continue_control;
786 (*command)->body_count = 0;
787 (*command)->body_list = NULL;
788 }
789 else
790 {
791 /* A normal command. */
792 *command = (struct command_line *)
793 xmalloc (sizeof (struct command_line));
794 (*command)->next = NULL;
795 (*command)->line = savestring (p, p1 - p);
796 (*command)->control_type = simple_control;
797 (*command)->body_count = 0;
798 (*command)->body_list = NULL;
799 }
800
801 /* Nothing special. */
802 return ok_command;
803 }
804
805 /* Recursively read in the control structures and create a command_line
806 structure from them.
807
808 The parent_control parameter is the control structure in which the
809 following commands are nested. */
810
811 static enum command_control_type
812 recurse_read_control_structure (struct command_line *current_cmd)
813 {
814 int current_body, i;
815 enum misc_command_type val;
816 enum command_control_type ret;
817 struct command_line **body_ptr, *child_tail, *next;
818
819 child_tail = NULL;
820 current_body = 1;
821
822 /* Sanity checks. */
823 if (current_cmd->control_type == simple_control)
824 {
825 error ("Recursed on a simple control type\n");
826 return invalid_control;
827 }
828
829 if (current_body > current_cmd->body_count)
830 {
831 error ("Allocated body is smaller than this command type needs\n");
832 return invalid_control;
833 }
834
835 /* Read lines from the input stream and build control structures. */
836 while (1)
837 {
838 dont_repeat ();
839
840 next = NULL;
841 val = read_next_line (&next);
842
843 /* Just skip blanks and comments. */
844 if (val == nop_command)
845 continue;
846
847 if (val == end_command)
848 {
849 if (current_cmd->control_type == while_control
850 || current_cmd->control_type == if_control)
851 {
852 /* Success reading an entire control structure. */
853 ret = simple_control;
854 break;
855 }
856 else
857 {
858 ret = invalid_control;
859 break;
860 }
861 }
862
863 /* Not the end of a control structure. */
864 if (val == else_command)
865 {
866 if (current_cmd->control_type == if_control
867 && current_body == 1)
868 {
869 realloc_body_list (current_cmd, 2);
870 current_body = 2;
871 child_tail = NULL;
872 continue;
873 }
874 else
875 {
876 ret = invalid_control;
877 break;
878 }
879 }
880
881 if (child_tail)
882 {
883 child_tail->next = next;
884 }
885 else
886 {
887 body_ptr = current_cmd->body_list;
888 for (i = 1; i < current_body; i++)
889 body_ptr++;
890
891 *body_ptr = next;
892
893 }
894
895 child_tail = next;
896
897 /* If the latest line is another control structure, then recurse
898 on it. */
899 if (next->control_type == while_control
900 || next->control_type == if_control)
901 {
902 control_level++;
903 ret = recurse_read_control_structure (next);
904 control_level--;
905
906 if (ret != simple_control)
907 break;
908 }
909 }
910
911 dont_repeat ();
912
913 return ret;
914 }
915
916 /* Read lines from the input stream and accumulate them in a chain of
917 struct command_line's, which is then returned. For input from a
918 terminal, the special command "end" is used to mark the end of the
919 input, and is not included in the returned chain of commands. */
920
921 #define END_MESSAGE "End with a line saying just \"end\"."
922
923 struct command_line *
924 read_command_lines (char *prompt_arg, int from_tty)
925 {
926 struct command_line *head, *tail, *next;
927 struct cleanup *old_chain;
928 enum command_control_type ret;
929 enum misc_command_type val;
930
931 control_level = 0;
932 if (readline_begin_hook)
933 {
934 /* Note - intentional to merge messages with no newline */
935 (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE);
936 }
937 else if (from_tty && input_from_terminal_p ())
938 {
939 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
940 gdb_flush (gdb_stdout);
941 }
942
943 head = tail = NULL;
944 old_chain = NULL;
945
946 while (1)
947 {
948 val = read_next_line (&next);
949
950 /* Ignore blank lines or comments. */
951 if (val == nop_command)
952 continue;
953
954 if (val == end_command)
955 {
956 ret = simple_control;
957 break;
958 }
959
960 if (val != ok_command)
961 {
962 ret = invalid_control;
963 break;
964 }
965
966 if (next->control_type == while_control
967 || next->control_type == if_control)
968 {
969 control_level++;
970 ret = recurse_read_control_structure (next);
971 control_level--;
972
973 if (ret == invalid_control)
974 break;
975 }
976
977 if (tail)
978 {
979 tail->next = next;
980 }
981 else
982 {
983 head = next;
984 old_chain = make_cleanup_free_command_lines (&head);
985 }
986 tail = next;
987 }
988
989 dont_repeat ();
990
991 if (head)
992 {
993 if (ret != invalid_control)
994 {
995 discard_cleanups (old_chain);
996 }
997 else
998 do_cleanups (old_chain);
999 }
1000
1001 if (readline_end_hook)
1002 {
1003 (*readline_end_hook) ();
1004 }
1005 return (head);
1006 }
1007
1008 /* Free a chain of struct command_line's. */
1009
1010 void
1011 free_command_lines (struct command_line **lptr)
1012 {
1013 register struct command_line *l = *lptr;
1014 register struct command_line *next;
1015 struct command_line **blist;
1016 int i;
1017
1018 while (l)
1019 {
1020 if (l->body_count > 0)
1021 {
1022 blist = l->body_list;
1023 for (i = 0; i < l->body_count; i++, blist++)
1024 free_command_lines (blist);
1025 }
1026 next = l->next;
1027 xfree (l->line);
1028 xfree (l);
1029 l = next;
1030 }
1031 }
1032
1033 static void
1034 do_free_command_lines_cleanup (void *arg)
1035 {
1036 free_command_lines (arg);
1037 }
1038
1039 static struct cleanup *
1040 make_cleanup_free_command_lines (struct command_line **arg)
1041 {
1042 return make_cleanup (do_free_command_lines_cleanup, arg);
1043 }
1044 \f
1045 static void
1046 validate_comname (char *comname)
1047 {
1048 register char *p;
1049
1050 if (comname == 0)
1051 error_no_arg ("name of command to define");
1052
1053 p = comname;
1054 while (*p)
1055 {
1056 if (!isalnum (*p) && *p != '-' && *p != '_')
1057 error ("Junk in argument list: \"%s\"", p);
1058 p++;
1059 }
1060 }
1061
1062 /* This is just a placeholder in the command data structures. */
1063 static void
1064 user_defined_command (char *ignore, int from_tty)
1065 {
1066 }
1067
1068 void
1069 define_command (char *comname, int from_tty)
1070 {
1071 #define MAX_TMPBUF 128
1072 enum cmd_hook_type
1073 {
1074 CMD_NO_HOOK = 0,
1075 CMD_PRE_HOOK,
1076 CMD_POST_HOOK
1077 };
1078 register struct command_line *cmds;
1079 register struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1080 char *tem = comname;
1081 char *tem2;
1082 char tmpbuf[MAX_TMPBUF];
1083 int hook_type = CMD_NO_HOOK;
1084 int hook_name_size = 0;
1085
1086 #define HOOK_STRING "hook-"
1087 #define HOOK_LEN 5
1088 #define HOOK_POST_STRING "hookpost-"
1089 #define HOOK_POST_LEN 9
1090
1091 validate_comname (comname);
1092
1093 /* Look it up, and verify that we got an exact match. */
1094 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1095 if (c && !STREQ (comname, c->name))
1096 c = 0;
1097
1098 if (c)
1099 {
1100 if (c->class == class_user || c->class == class_alias)
1101 tem = "Redefine command \"%s\"? ";
1102 else
1103 tem = "Really redefine built-in command \"%s\"? ";
1104 if (!query (tem, c->name))
1105 error ("Command \"%s\" not redefined.", c->name);
1106 }
1107
1108 /* If this new command is a hook, then mark the command which it
1109 is hooking. Note that we allow hooking `help' commands, so that
1110 we can hook the `stop' pseudo-command. */
1111
1112 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1113 {
1114 hook_type = CMD_PRE_HOOK;
1115 hook_name_size = HOOK_LEN;
1116 }
1117 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1118 {
1119 hook_type = CMD_POST_HOOK;
1120 hook_name_size = HOOK_POST_LEN;
1121 }
1122
1123 if (hook_type != CMD_NO_HOOK)
1124 {
1125 /* Look up cmd it hooks, and verify that we got an exact match. */
1126 tem = comname + hook_name_size;
1127 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1128 if (hookc && !STREQ (comname + hook_name_size, hookc->name))
1129 hookc = 0;
1130 if (!hookc)
1131 {
1132 warning ("Your new `%s' command does not hook any existing command.",
1133 comname);
1134 if (!query ("Proceed? "))
1135 error ("Not confirmed.");
1136 }
1137 }
1138
1139 comname = savestring (comname, strlen (comname));
1140
1141 /* If the rest of the commands will be case insensitive, this one
1142 should behave in the same manner. */
1143 for (tem = comname; *tem; tem++)
1144 if (isupper (*tem))
1145 *tem = tolower (*tem);
1146
1147 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1148 cmds = read_command_lines (tmpbuf, from_tty);
1149
1150 if (c && c->class == class_user)
1151 free_command_lines (&c->user_commands);
1152
1153 newc = add_cmd (comname, class_user, user_defined_command,
1154 (c && c->class == class_user)
1155 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1156 newc->user_commands = cmds;
1157
1158 /* If this new command is a hook, then mark both commands as being
1159 tied. */
1160 if (hookc)
1161 {
1162 switch (hook_type)
1163 {
1164 case CMD_PRE_HOOK:
1165 hookc->hook_pre = newc; /* Target gets hooked. */
1166 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1167 break;
1168 case CMD_POST_HOOK:
1169 hookc->hook_pre = newc; /* Target gets hooked. */
1170 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1171 break;
1172 default:
1173 /* Should never come here as hookc would be 0. */
1174 internal_error (__FILE__, __LINE__, "bad switch");
1175 }
1176 }
1177 }
1178
1179 void
1180 document_command (char *comname, int from_tty)
1181 {
1182 struct command_line *doclines;
1183 register struct cmd_list_element *c;
1184 char *tem = comname;
1185 char tmpbuf[128];
1186
1187 validate_comname (comname);
1188
1189 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1190
1191 if (c->class != class_user)
1192 error ("Command \"%s\" is built-in.", comname);
1193
1194 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1195 doclines = read_command_lines (tmpbuf, from_tty);
1196
1197 if (c->doc)
1198 xfree (c->doc);
1199
1200 {
1201 register struct command_line *cl1;
1202 register int len = 0;
1203
1204 for (cl1 = doclines; cl1; cl1 = cl1->next)
1205 len += strlen (cl1->line) + 1;
1206
1207 c->doc = (char *) xmalloc (len + 1);
1208 *c->doc = 0;
1209
1210 for (cl1 = doclines; cl1; cl1 = cl1->next)
1211 {
1212 strcat (c->doc, cl1->line);
1213 if (cl1->next)
1214 strcat (c->doc, "\n");
1215 }
1216 }
1217
1218 free_command_lines (&doclines);
1219 }
1220 \f
1221 struct source_cleanup_lines_args
1222 {
1223 int old_line;
1224 char *old_file;
1225 char *old_pre_error;
1226 char *old_error_pre_print;
1227 };
1228
1229 static void
1230 source_cleanup_lines (PTR args)
1231 {
1232 struct source_cleanup_lines_args *p =
1233 (struct source_cleanup_lines_args *) args;
1234 source_line_number = p->old_line;
1235 source_file_name = p->old_file;
1236 source_pre_error = p->old_pre_error;
1237 error_pre_print = p->old_error_pre_print;
1238 }
1239
1240 /* ARGSUSED */
1241 static void
1242 do_fclose_cleanup (void *stream)
1243 {
1244 fclose (stream);
1245 }
1246
1247 /* Used to implement source_command */
1248
1249 void
1250 script_from_file (FILE *stream, char *file)
1251 {
1252 struct cleanup *old_cleanups;
1253 struct source_cleanup_lines_args old_lines;
1254 int needed_length;
1255
1256 if (stream == NULL)
1257 {
1258 internal_error (__FILE__, __LINE__, "called with NULL file pointer!");
1259 }
1260
1261 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1262
1263 old_lines.old_line = source_line_number;
1264 old_lines.old_file = source_file_name;
1265 old_lines.old_pre_error = source_pre_error;
1266 old_lines.old_error_pre_print = error_pre_print;
1267 make_cleanup (source_cleanup_lines, &old_lines);
1268 source_line_number = 0;
1269 source_file_name = file;
1270 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
1271 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
1272 make_cleanup (xfree, source_pre_error);
1273 /* This will get set every time we read a line. So it won't stay "" for
1274 long. */
1275 error_pre_print = "";
1276
1277 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
1278 if (source_error_allocated < needed_length)
1279 {
1280 source_error_allocated *= 2;
1281 if (source_error_allocated < needed_length)
1282 source_error_allocated = needed_length;
1283 if (source_error == NULL)
1284 source_error = xmalloc (source_error_allocated);
1285 else
1286 source_error = xrealloc (source_error, source_error_allocated);
1287 }
1288
1289 read_command_file (stream);
1290
1291 do_cleanups (old_cleanups);
1292 }
1293
1294 void
1295 show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1296 {
1297 register struct command_line *cmdlines;
1298
1299 cmdlines = c->user_commands;
1300 if (!cmdlines)
1301 return;
1302 fputs_filtered ("User command ", stream);
1303 fputs_filtered (c->name, stream);
1304 fputs_filtered (":\n", stream);
1305
1306 #ifdef UI_OUT
1307 print_command_lines (uiout, cmdlines, 1);
1308 fputs_filtered ("\n", stream);
1309 #else
1310 while (cmdlines)
1311 {
1312 print_command_line (cmdlines, 4, stream);
1313 cmdlines = cmdlines->next;
1314 }
1315 fputs_filtered ("\n", stream);
1316 #endif
1317 }
1318
This page took 0.057851 seconds and 4 git commands to generate.