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