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