* gdbarch.sh: When an int variable, print value using %d instead
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
CommitLineData
d318976c 1/* GDB CLI command scripting.
b6ba6518
KB
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
d318976c
FN
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
38extern void dont_repeat (void);
39
40extern void do_restore_instream_cleanup (void *stream);
41
42/* Prototypes for local functions */
43
44static struct cleanup *
45 make_cleanup_free_command_lines (struct command_line **arg);
46
47static enum command_control_type
48 recurse_read_control_structure (struct command_line *current_cmd);
49
50static char *insert_args (char *line);
51
52static struct cleanup * setup_user_args (char *p);
53
54static void validate_comname (char *);
55
56/* Level of control structure. */
57static int control_level;
58
59/* Source command state variable. */
60static int source_error_allocated;
61
62/* Structure for arguments to user defined functions. */
63#define MAXUSERARGS 10
64struct 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
81static struct command_line *
82build_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
105static struct command_line *
106get_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
130void
131print_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 {
d318976c
FN
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);
d318976c
FN
177 if (depth)
178 ui_out_spaces (uiout, 2 * depth);
e6ccd35f
JSC
179 ui_out_field_string (uiout, NULL, "end");
180 ui_out_text (uiout, "\n");
d318976c
FN
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 {
d318976c
FN
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");
e6ccd35f 199 ui_out_text (uiout, "\n");
d318976c
FN
200 print_command_lines (uiout, list->body_list[1], depth + 1);
201 }
202
d318976c
FN
203 if (depth)
204 ui_out_spaces (uiout, 2 * depth);
e6ccd35f
JSC
205 ui_out_field_string (uiout, NULL, "end");
206 ui_out_text (uiout, "\n");
d318976c
FN
207 list = list->next;
208 continue;
209 }
210
211 /* ignore illegal command type and try next */
212 list = list->next;
213 } /* while (list) */
214}
215#else
216void
217print_command_line (struct command_line *cmd, unsigned int depth,
218 struct ui_file *stream)
219{
220 unsigned int i;
221
222 if (depth)
223 {
224 for (i = 0; i < depth; i++)
225 fputs_filtered (" ", stream);
226 }
227
228 /* A simple command, print it and return. */
229 if (cmd->control_type == simple_control)
230 {
231 fputs_filtered (cmd->line, stream);
232 fputs_filtered ("\n", stream);
233 return;
234 }
235
236 /* loop_continue to jump to the start of a while loop, print it
237 and return. */
238 if (cmd->control_type == continue_control)
239 {
240 fputs_filtered ("loop_continue\n", stream);
241 return;
242 }
243
244 /* loop_break to break out of a while loop, print it and return. */
245 if (cmd->control_type == break_control)
246 {
247 fputs_filtered ("loop_break\n", stream);
248 return;
249 }
250
251 /* A while command. Recursively print its subcommands before returning. */
252 if (cmd->control_type == while_control)
253 {
254 struct command_line *list;
255 fputs_filtered ("while ", stream);
256 fputs_filtered (cmd->line, stream);
257 fputs_filtered ("\n", stream);
258 list = *cmd->body_list;
259 while (list)
260 {
261 print_command_line (list, depth + 1, stream);
262 list = list->next;
263 }
264 }
265
266 /* An if command. Recursively print both arms before returning. */
267 if (cmd->control_type == if_control)
268 {
269 fputs_filtered ("if ", stream);
270 fputs_filtered (cmd->line, stream);
271 fputs_filtered ("\n", stream);
272 /* The true arm. */
273 print_command_line (cmd->body_list[0], depth + 1, stream);
274
275 /* Show the false arm if it exists. */
276 if (cmd->body_count == 2)
277 {
278 if (depth)
279 {
280 for (i = 0; i < depth; i++)
281 fputs_filtered (" ", stream);
282 }
283 fputs_filtered ("else\n", stream);
284 print_command_line (cmd->body_list[1], depth + 1, stream);
285 }
286 if (depth)
287 {
288 for (i = 0; i < depth; i++)
289 fputs_filtered (" ", stream);
290 }
291 fputs_filtered ("end\n", stream);
292 }
293}
294#endif
295
296/* Execute the command in CMD. */
297
298void
299execute_user_command (struct cmd_list_element *c, char *args)
300{
301 register struct command_line *cmdlines;
302 struct cleanup *old_chain;
303 enum command_control_type ret;
304
305 old_chain = setup_user_args (args);
306
307 cmdlines = c->user_commands;
308 if (cmdlines == 0)
309 /* Null command */
310 return;
311
312 /* Set the instream to 0, indicating execution of a
313 user-defined function. */
314 old_chain = make_cleanup (do_restore_instream_cleanup, instream);
315 instream = (FILE *) 0;
316 while (cmdlines)
317 {
318 ret = execute_control_command (cmdlines);
319 if (ret != simple_control && ret != break_control)
320 {
321 warning ("Error in control structure.\n");
322 break;
323 }
324 cmdlines = cmdlines->next;
325 }
326 do_cleanups (old_chain);
327}
328
329enum command_control_type
330execute_control_command (struct command_line *cmd)
331{
332 struct expression *expr;
333 struct command_line *current;
334 struct cleanup *old_chain = 0;
335 value_ptr val;
336 value_ptr val_mark;
337 int loop;
338 enum command_control_type ret;
339 char *new_line;
340
341 switch (cmd->control_type)
342 {
343 case simple_control:
344 /* A simple command, execute it and return. */
345 new_line = insert_args (cmd->line);
346 if (!new_line)
347 return invalid_control;
348 old_chain = make_cleanup (free_current_contents, &new_line);
349 execute_command (new_line, 0);
350 ret = cmd->control_type;
351 break;
352
353 case continue_control:
354 case break_control:
355 /* Return for "continue", and "break" so we can either
356 continue the loop at the top, or break out. */
357 ret = cmd->control_type;
358 break;
359
360 case while_control:
361 {
362 /* Parse the loop control expression for the while statement. */
363 new_line = insert_args (cmd->line);
364 if (!new_line)
365 return invalid_control;
366 old_chain = make_cleanup (free_current_contents, &new_line);
367 expr = parse_expression (new_line);
368 make_cleanup (free_current_contents, &expr);
369
370 ret = simple_control;
371 loop = 1;
372
373 /* Keep iterating so long as the expression is true. */
374 while (loop == 1)
375 {
376 int cond_result;
377
378 QUIT;
379
380 /* Evaluate the expression. */
381 val_mark = value_mark ();
382 val = evaluate_expression (expr);
383 cond_result = value_true (val);
384 value_free_to_mark (val_mark);
385
386 /* If the value is false, then break out of the loop. */
387 if (!cond_result)
388 break;
389
390 /* Execute the body of the while statement. */
391 current = *cmd->body_list;
392 while (current)
393 {
394 ret = execute_control_command (current);
395
396 /* If we got an error, or a "break" command, then stop
397 looping. */
398 if (ret == invalid_control || ret == break_control)
399 {
400 loop = 0;
401 break;
402 }
403
404 /* If we got a "continue" command, then restart the loop
405 at this point. */
406 if (ret == continue_control)
407 break;
408
409 /* Get the next statement. */
410 current = current->next;
411 }
412 }
413
414 /* Reset RET so that we don't recurse the break all the way down. */
415 if (ret == break_control)
416 ret = simple_control;
417
418 break;
419 }
420
421 case if_control:
422 {
423 new_line = insert_args (cmd->line);
424 if (!new_line)
425 return invalid_control;
426 old_chain = make_cleanup (free_current_contents, &new_line);
427 /* Parse the conditional for the if statement. */
428 expr = parse_expression (new_line);
429 make_cleanup (free_current_contents, &expr);
430
431 current = NULL;
432 ret = simple_control;
433
434 /* Evaluate the conditional. */
435 val_mark = value_mark ();
436 val = evaluate_expression (expr);
437
438 /* Choose which arm to take commands from based on the value of the
439 conditional expression. */
440 if (value_true (val))
441 current = *cmd->body_list;
442 else if (cmd->body_count == 2)
443 current = *(cmd->body_list + 1);
444 value_free_to_mark (val_mark);
445
446 /* Execute commands in the given arm. */
447 while (current)
448 {
449 ret = execute_control_command (current);
450
451 /* If we got an error, get out. */
452 if (ret != simple_control)
453 break;
454
455 /* Get the next statement in the body. */
456 current = current->next;
457 }
458
459 break;
460 }
461
462 default:
463 warning ("Invalid control type in command structure.");
464 return invalid_control;
465 }
466
467 if (old_chain)
468 do_cleanups (old_chain);
469
470 return ret;
471}
472
473/* "while" command support. Executes a body of statements while the
474 loop condition is nonzero. */
475
476void
477while_command (char *arg, int from_tty)
478{
479 struct command_line *command = NULL;
480
481 control_level = 1;
482 command = get_command_line (while_control, arg);
483
484 if (command == NULL)
485 return;
486
487 execute_control_command (command);
488 free_command_lines (&command);
489}
490
491/* "if" command support. Execute either the true or false arm depending
492 on the value of the if conditional. */
493
494void
495if_command (char *arg, int from_tty)
496{
497 struct command_line *command = NULL;
498
499 control_level = 1;
500 command = get_command_line (if_control, arg);
501
502 if (command == NULL)
503 return;
504
505 execute_control_command (command);
506 free_command_lines (&command);
507}
508
509/* Cleanup */
510static void
511arg_cleanup (void *ignore)
512{
513 struct user_args *oargs = user_args;
514 if (!user_args)
8e65ff28
AC
515 internal_error (__FILE__, __LINE__,
516 "arg_cleanup called with no user args.\n");
d318976c
FN
517
518 user_args = user_args->next;
b8c9b27d 519 xfree (oargs);
d318976c
FN
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
b8c9b27d 704 xfree (command->body_list);
d318976c
FN
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;
b8c9b27d
KB
1025 xfree (l->line);
1026 xfree (l);
d318976c
FN
1027 l = next;
1028 }
0d70f41b 1029 *lptr = NULL;
d318976c
FN
1030}
1031
1032static void
1033do_free_command_lines_cleanup (void *arg)
1034{
1035 free_command_lines (arg);
1036}
1037
1038static struct cleanup *
1039make_cleanup_free_command_lines (struct command_line **arg)
1040{
1041 return make_cleanup (do_free_command_lines_cleanup, arg);
1042}
1043\f
1044static void
1045validate_comname (char *comname)
1046{
1047 register char *p;
1048
1049 if (comname == 0)
1050 error_no_arg ("name of command to define");
1051
1052 p = comname;
1053 while (*p)
1054 {
1055 if (!isalnum (*p) && *p != '-' && *p != '_')
1056 error ("Junk in argument list: \"%s\"", p);
1057 p++;
1058 }
1059}
1060
1061/* This is just a placeholder in the command data structures. */
1062static void
1063user_defined_command (char *ignore, int from_tty)
1064{
1065}
1066
1067void
1068define_command (char *comname, int from_tty)
1069{
1070#define MAX_TMPBUF 128
1071 enum cmd_hook_type
1072 {
1073 CMD_NO_HOOK = 0,
1074 CMD_PRE_HOOK,
1075 CMD_POST_HOOK
1076 };
1077 register struct command_line *cmds;
1078 register struct cmd_list_element *c, *newc, *oldc, *hookc = 0;
1079 char *tem = comname;
1080 char *tem2;
1081 char tmpbuf[MAX_TMPBUF];
1082 int hook_type = CMD_NO_HOOK;
1083 int hook_name_size = 0;
1084
1085#define HOOK_STRING "hook-"
1086#define HOOK_LEN 5
1087#define HOOK_POST_STRING "hookpost-"
1088#define HOOK_POST_LEN 9
1089
1090 validate_comname (comname);
1091
1092 /* Look it up, and verify that we got an exact match. */
1093 c = lookup_cmd (&tem, cmdlist, "", -1, 1);
1094 if (c && !STREQ (comname, c->name))
1095 c = 0;
1096
1097 if (c)
1098 {
1099 if (c->class == class_user || c->class == class_alias)
1100 tem = "Redefine command \"%s\"? ";
1101 else
1102 tem = "Really redefine built-in command \"%s\"? ";
1103 if (!query (tem, c->name))
1104 error ("Command \"%s\" not redefined.", c->name);
1105 }
1106
1107 /* If this new command is a hook, then mark the command which it
1108 is hooking. Note that we allow hooking `help' commands, so that
1109 we can hook the `stop' pseudo-command. */
1110
1111 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1112 {
1113 hook_type = CMD_PRE_HOOK;
1114 hook_name_size = HOOK_LEN;
1115 }
1116 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1117 {
1118 hook_type = CMD_POST_HOOK;
1119 hook_name_size = HOOK_POST_LEN;
1120 }
1121
1122 if (hook_type != CMD_NO_HOOK)
1123 {
1124 /* Look up cmd it hooks, and verify that we got an exact match. */
1125 tem = comname + hook_name_size;
1126 hookc = lookup_cmd (&tem, cmdlist, "", -1, 0);
1127 if (hookc && !STREQ (comname + hook_name_size, hookc->name))
1128 hookc = 0;
1129 if (!hookc)
1130 {
1131 warning ("Your new `%s' command does not hook any existing command.",
1132 comname);
1133 if (!query ("Proceed? "))
1134 error ("Not confirmed.");
1135 }
1136 }
1137
1138 comname = savestring (comname, strlen (comname));
1139
1140 /* If the rest of the commands will be case insensitive, this one
1141 should behave in the same manner. */
1142 for (tem = comname; *tem; tem++)
1143 if (isupper (*tem))
1144 *tem = tolower (*tem);
1145
1146 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname);
1147 cmds = read_command_lines (tmpbuf, from_tty);
1148
1149 if (c && c->class == class_user)
1150 free_command_lines (&c->user_commands);
1151
1152 newc = add_cmd (comname, class_user, user_defined_command,
1153 (c && c->class == class_user)
1154 ? c->doc : savestring ("User-defined.", 13), &cmdlist);
1155 newc->user_commands = cmds;
1156
1157 /* If this new command is a hook, then mark both commands as being
1158 tied. */
1159 if (hookc)
1160 {
1161 switch (hook_type)
1162 {
1163 case CMD_PRE_HOOK:
1164 hookc->hook_pre = newc; /* Target gets hooked. */
1165 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1166 break;
1167 case CMD_POST_HOOK:
f48ff60a
FN
1168 hookc->hook_post = newc; /* Target gets hooked. */
1169 newc->hookee_post = hookc; /* We are marked as hooking target cmd. */
d318976c
FN
1170 break;
1171 default:
1172 /* Should never come here as hookc would be 0. */
8e65ff28 1173 internal_error (__FILE__, __LINE__, "bad switch");
d318976c
FN
1174 }
1175 }
1176}
1177
1178void
1179document_command (char *comname, int from_tty)
1180{
1181 struct command_line *doclines;
1182 register struct cmd_list_element *c;
1183 char *tem = comname;
1184 char tmpbuf[128];
1185
1186 validate_comname (comname);
1187
1188 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1189
1190 if (c->class != class_user)
1191 error ("Command \"%s\" is built-in.", comname);
1192
1193 sprintf (tmpbuf, "Type documentation for \"%s\".", comname);
1194 doclines = read_command_lines (tmpbuf, from_tty);
1195
1196 if (c->doc)
b8c9b27d 1197 xfree (c->doc);
d318976c
FN
1198
1199 {
1200 register struct command_line *cl1;
1201 register int len = 0;
1202
1203 for (cl1 = doclines; cl1; cl1 = cl1->next)
1204 len += strlen (cl1->line) + 1;
1205
1206 c->doc = (char *) xmalloc (len + 1);
1207 *c->doc = 0;
1208
1209 for (cl1 = doclines; cl1; cl1 = cl1->next)
1210 {
1211 strcat (c->doc, cl1->line);
1212 if (cl1->next)
1213 strcat (c->doc, "\n");
1214 }
1215 }
1216
1217 free_command_lines (&doclines);
1218}
1219\f
1220struct source_cleanup_lines_args
1221{
1222 int old_line;
1223 char *old_file;
1224 char *old_pre_error;
1225 char *old_error_pre_print;
1226};
1227
1228static void
1229source_cleanup_lines (PTR args)
1230{
1231 struct source_cleanup_lines_args *p =
1232 (struct source_cleanup_lines_args *) args;
1233 source_line_number = p->old_line;
1234 source_file_name = p->old_file;
1235 source_pre_error = p->old_pre_error;
1236 error_pre_print = p->old_error_pre_print;
1237}
1238
1239/* ARGSUSED */
1240static void
1241do_fclose_cleanup (void *stream)
1242{
1243 fclose (stream);
1244}
1245
1246/* Used to implement source_command */
1247
1248void
1249script_from_file (FILE *stream, char *file)
1250{
1251 struct cleanup *old_cleanups;
1252 struct source_cleanup_lines_args old_lines;
1253 int needed_length;
1254
1255 if (stream == NULL)
1256 {
8e65ff28 1257 internal_error (__FILE__, __LINE__, "called with NULL file pointer!");
d318976c
FN
1258 }
1259
1260 old_cleanups = make_cleanup (do_fclose_cleanup, stream);
1261
1262 old_lines.old_line = source_line_number;
1263 old_lines.old_file = source_file_name;
1264 old_lines.old_pre_error = source_pre_error;
1265 old_lines.old_error_pre_print = error_pre_print;
1266 make_cleanup (source_cleanup_lines, &old_lines);
1267 source_line_number = 0;
1268 source_file_name = file;
1269 source_pre_error = error_pre_print == NULL ? "" : error_pre_print;
1270 source_pre_error = savestring (source_pre_error, strlen (source_pre_error));
b8c9b27d 1271 make_cleanup (xfree, source_pre_error);
d318976c
FN
1272 /* This will get set every time we read a line. So it won't stay "" for
1273 long. */
1274 error_pre_print = "";
1275
1276 needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80;
1277 if (source_error_allocated < needed_length)
1278 {
1279 source_error_allocated *= 2;
1280 if (source_error_allocated < needed_length)
1281 source_error_allocated = needed_length;
1282 if (source_error == NULL)
1283 source_error = xmalloc (source_error_allocated);
1284 else
1285 source_error = xrealloc (source_error, source_error_allocated);
1286 }
1287
1288 read_command_file (stream);
1289
1290 do_cleanups (old_cleanups);
1291}
1292
1293void
1294show_user_1 (struct cmd_list_element *c, struct ui_file *stream)
1295{
1296 register struct command_line *cmdlines;
1297
1298 cmdlines = c->user_commands;
1299 if (!cmdlines)
1300 return;
1301 fputs_filtered ("User command ", stream);
1302 fputs_filtered (c->name, stream);
1303 fputs_filtered (":\n", stream);
1304
1305#ifdef UI_OUT
1306 print_command_lines (uiout, cmdlines, 1);
1307 fputs_filtered ("\n", stream);
1308#else
1309 while (cmdlines)
1310 {
1311 print_command_line (cmdlines, 4, stream);
1312 cmdlines = cmdlines->next;
1313 }
1314 fputs_filtered ("\n", stream);
1315#endif
1316}
1317
This page took 0.121008 seconds and 4 git commands to generate.