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