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