[testsuite] Unbuffer the output in gdb.base/multi-forks.c
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
1 /* GDB CLI command scripting.
2
3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "value.h"
22 #include "language.h" /* For value_true */
23 #include <ctype.h>
24
25 #include "ui-out.h"
26 #include "top.h"
27 #include "breakpoint.h"
28 #include "cli/cli-cmds.h"
29 #include "cli/cli-decode.h"
30 #include "cli/cli-script.h"
31
32 #include "extension.h"
33 #include "interps.h"
34 #include "compile/compile.h"
35
36 /* Prototypes for local functions. */
37
38 static enum command_control_type
39 recurse_read_control_structure (char * (*read_next_line_func) (void),
40 struct command_line *current_cmd,
41 void (*validator)(char *, void *),
42 void *closure);
43
44 static char *insert_args (char *line);
45
46 static struct cleanup * setup_user_args (char *p);
47
48 static char *read_next_line (void);
49
50 /* Level of control structure when reading. */
51 static int control_level;
52
53 /* Level of control structure when executing. */
54 static int command_nest_depth = 1;
55
56 /* This is to prevent certain commands being printed twice. */
57 static int suppress_next_print_command_trace = 0;
58
59 /* Structure for arguments to user defined functions. */
60 #define MAXUSERARGS 10
61 struct user_args
62 {
63 struct user_args *next;
64 /* It is necessary to store a malloced copy of the command line to
65 ensure that the arguments are not overwritten before they are
66 used. */
67 char *command;
68 struct
69 {
70 char *arg;
71 int len;
72 }
73 a[MAXUSERARGS];
74 int count;
75 }
76 *user_args;
77
78 \f
79 /* Return non-zero if TYPE is a multi-line command (i.e., is terminated
80 by "end"). */
81
82 static int
83 multi_line_command_p (enum command_control_type type)
84 {
85 switch (type)
86 {
87 case if_control:
88 case while_control:
89 case while_stepping_control:
90 case commands_control:
91 case compile_control:
92 case python_control:
93 case guile_control:
94 return 1;
95 default:
96 return 0;
97 }
98 }
99
100 /* Allocate, initialize a new command line structure for one of the
101 control commands (if/while). */
102
103 static struct command_line *
104 build_command_line (enum command_control_type type, char *args)
105 {
106 struct command_line *cmd;
107
108 if (args == NULL && (type == if_control || type == while_control))
109 error (_("if/while commands require arguments."));
110 gdb_assert (args != NULL);
111
112 cmd = XNEW (struct command_line);
113 cmd->next = NULL;
114 cmd->control_type = type;
115
116 cmd->body_count = 1;
117 cmd->body_list = XCNEWVEC (struct command_line *, cmd->body_count);
118 cmd->line = xstrdup (args);
119
120 return cmd;
121 }
122
123 /* Build and return a new command structure for the control commands
124 such as "if" and "while". */
125
126 struct command_line *
127 get_command_line (enum command_control_type type, char *arg)
128 {
129 struct command_line *cmd;
130 struct cleanup *old_chain = NULL;
131
132 /* Allocate and build a new command line structure. */
133 cmd = build_command_line (type, arg);
134
135 old_chain = make_cleanup_free_command_lines (&cmd);
136
137 /* Read in the body of this command. */
138 if (recurse_read_control_structure (read_next_line, cmd, 0, 0)
139 == invalid_control)
140 {
141 warning (_("Error reading in canned sequence of commands."));
142 do_cleanups (old_chain);
143 return NULL;
144 }
145
146 discard_cleanups (old_chain);
147 return cmd;
148 }
149
150 /* Recursively print a command (including full control structures). */
151
152 void
153 print_command_lines (struct ui_out *uiout, struct command_line *cmd,
154 unsigned int depth)
155 {
156 struct command_line *list;
157
158 list = cmd;
159 while (list)
160 {
161 if (depth)
162 ui_out_spaces (uiout, 2 * depth);
163
164 /* A simple command, print it and continue. */
165 if (list->control_type == simple_control)
166 {
167 ui_out_field_string (uiout, NULL, list->line);
168 ui_out_text (uiout, "\n");
169 list = list->next;
170 continue;
171 }
172
173 /* loop_continue to jump to the start of a while loop, print it
174 and continue. */
175 if (list->control_type == continue_control)
176 {
177 ui_out_field_string (uiout, NULL, "loop_continue");
178 ui_out_text (uiout, "\n");
179 list = list->next;
180 continue;
181 }
182
183 /* loop_break to break out of a while loop, print it and
184 continue. */
185 if (list->control_type == break_control)
186 {
187 ui_out_field_string (uiout, NULL, "loop_break");
188 ui_out_text (uiout, "\n");
189 list = list->next;
190 continue;
191 }
192
193 /* A while command. Recursively print its subcommands and
194 continue. */
195 if (list->control_type == while_control
196 || list->control_type == while_stepping_control)
197 {
198 /* For while-stepping, the line includes the 'while-stepping'
199 token. See comment in process_next_line for explanation.
200 Here, take care not print 'while-stepping' twice. */
201 if (list->control_type == while_control)
202 ui_out_field_fmt (uiout, NULL, "while %s", list->line);
203 else
204 ui_out_field_string (uiout, NULL, list->line);
205 ui_out_text (uiout, "\n");
206 print_command_lines (uiout, *list->body_list, depth + 1);
207 if (depth)
208 ui_out_spaces (uiout, 2 * depth);
209 ui_out_field_string (uiout, NULL, "end");
210 ui_out_text (uiout, "\n");
211 list = list->next;
212 continue;
213 }
214
215 /* An if command. Recursively print both arms before
216 continueing. */
217 if (list->control_type == if_control)
218 {
219 ui_out_field_fmt (uiout, NULL, "if %s", list->line);
220 ui_out_text (uiout, "\n");
221 /* The true arm. */
222 print_command_lines (uiout, list->body_list[0], depth + 1);
223
224 /* Show the false arm if it exists. */
225 if (list->body_count == 2)
226 {
227 if (depth)
228 ui_out_spaces (uiout, 2 * depth);
229 ui_out_field_string (uiout, NULL, "else");
230 ui_out_text (uiout, "\n");
231 print_command_lines (uiout, list->body_list[1], depth + 1);
232 }
233
234 if (depth)
235 ui_out_spaces (uiout, 2 * depth);
236 ui_out_field_string (uiout, NULL, "end");
237 ui_out_text (uiout, "\n");
238 list = list->next;
239 continue;
240 }
241
242 /* A commands command. Print the breakpoint commands and
243 continue. */
244 if (list->control_type == commands_control)
245 {
246 if (*(list->line))
247 ui_out_field_fmt (uiout, NULL, "commands %s", list->line);
248 else
249 ui_out_field_string (uiout, NULL, "commands");
250 ui_out_text (uiout, "\n");
251 print_command_lines (uiout, *list->body_list, depth + 1);
252 if (depth)
253 ui_out_spaces (uiout, 2 * depth);
254 ui_out_field_string (uiout, NULL, "end");
255 ui_out_text (uiout, "\n");
256 list = list->next;
257 continue;
258 }
259
260 if (list->control_type == python_control)
261 {
262 ui_out_field_string (uiout, NULL, "python");
263 ui_out_text (uiout, "\n");
264 /* Don't indent python code at all. */
265 print_command_lines (uiout, *list->body_list, 0);
266 if (depth)
267 ui_out_spaces (uiout, 2 * depth);
268 ui_out_field_string (uiout, NULL, "end");
269 ui_out_text (uiout, "\n");
270 list = list->next;
271 continue;
272 }
273
274 if (list->control_type == compile_control)
275 {
276 ui_out_field_string (uiout, NULL, "compile expression");
277 ui_out_text (uiout, "\n");
278 print_command_lines (uiout, *list->body_list, 0);
279 if (depth)
280 ui_out_spaces (uiout, 2 * depth);
281 ui_out_field_string (uiout, NULL, "end");
282 ui_out_text (uiout, "\n");
283 list = list->next;
284 continue;
285 }
286
287 if (list->control_type == guile_control)
288 {
289 ui_out_field_string (uiout, NULL, "guile");
290 ui_out_text (uiout, "\n");
291 print_command_lines (uiout, *list->body_list, depth + 1);
292 if (depth)
293 ui_out_spaces (uiout, 2 * depth);
294 ui_out_field_string (uiout, NULL, "end");
295 ui_out_text (uiout, "\n");
296 list = list->next;
297 continue;
298 }
299
300 /* Ignore illegal command type and try next. */
301 list = list->next;
302 } /* while (list) */
303 }
304
305 /* Handle pre-post hooks. */
306
307 static void
308 clear_hook_in_cleanup (void *data)
309 {
310 struct cmd_list_element *c = (struct cmd_list_element *) data;
311
312 c->hook_in = 0; /* Allow hook to work again once it is complete. */
313 }
314
315 void
316 execute_cmd_pre_hook (struct cmd_list_element *c)
317 {
318 if ((c->hook_pre) && (!c->hook_in))
319 {
320 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
321 c->hook_in = 1; /* Prevent recursive hooking. */
322 execute_user_command (c->hook_pre, (char *) 0);
323 do_cleanups (cleanups);
324 }
325 }
326
327 void
328 execute_cmd_post_hook (struct cmd_list_element *c)
329 {
330 if ((c->hook_post) && (!c->hook_in))
331 {
332 struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c);
333
334 c->hook_in = 1; /* Prevent recursive hooking. */
335 execute_user_command (c->hook_post, (char *) 0);
336 do_cleanups (cleanups);
337 }
338 }
339
340 /* Execute the command in CMD. */
341 static void
342 do_restore_user_call_depth (void * call_depth)
343 {
344 int *depth = (int *) call_depth;
345
346 (*depth)--;
347 if ((*depth) == 0)
348 in_user_command = 0;
349 }
350
351
352 void
353 execute_user_command (struct cmd_list_element *c, char *args)
354 {
355 struct command_line *cmdlines;
356 struct cleanup *old_chain;
357 enum command_control_type ret;
358 static int user_call_depth = 0;
359 extern unsigned int max_user_call_depth;
360
361 cmdlines = c->user_commands;
362 if (cmdlines == 0)
363 /* Null command */
364 return;
365
366 old_chain = setup_user_args (args);
367
368 if (++user_call_depth > max_user_call_depth)
369 error (_("Max user call depth exceeded -- command aborted."));
370
371 make_cleanup (do_restore_user_call_depth, &user_call_depth);
372
373 /* Set the instream to 0, indicating execution of a
374 user-defined function. */
375 make_cleanup (do_restore_instream_cleanup, instream);
376 instream = (FILE *) 0;
377
378 /* Also set the global in_user_command, so that NULL instream is
379 not confused with Insight. */
380 in_user_command = 1;
381
382 make_cleanup_restore_integer (&interpreter_async);
383 interpreter_async = 0;
384
385 command_nest_depth++;
386 while (cmdlines)
387 {
388 ret = execute_control_command (cmdlines);
389 if (ret != simple_control && ret != break_control)
390 {
391 warning (_("Error executing canned sequence of commands."));
392 break;
393 }
394 cmdlines = cmdlines->next;
395 }
396 command_nest_depth--;
397 do_cleanups (old_chain);
398 }
399
400 /* This function is called every time GDB prints a prompt. It ensures
401 that errors and the like do not confuse the command tracing. */
402
403 void
404 reset_command_nest_depth (void)
405 {
406 command_nest_depth = 1;
407
408 /* Just in case. */
409 suppress_next_print_command_trace = 0;
410 }
411
412 /* Print the command, prefixed with '+' to represent the call depth.
413 This is slightly complicated because this function may be called
414 from execute_command and execute_control_command. Unfortunately
415 execute_command also prints the top level control commands.
416 In these cases execute_command will call execute_control_command
417 via while_command or if_command. Inner levels of 'if' and 'while'
418 are dealt with directly. Therefore we can use these functions
419 to determine whether the command has been printed already or not. */
420 void
421 print_command_trace (const char *cmd)
422 {
423 int i;
424
425 if (suppress_next_print_command_trace)
426 {
427 suppress_next_print_command_trace = 0;
428 return;
429 }
430
431 if (!source_verbose && !trace_commands)
432 return;
433
434 for (i=0; i < command_nest_depth; i++)
435 printf_filtered ("+");
436
437 printf_filtered ("%s\n", cmd);
438 }
439
440 enum command_control_type
441 execute_control_command (struct command_line *cmd)
442 {
443 struct expression *expr;
444 struct command_line *current;
445 struct cleanup *old_chain = make_cleanup (null_cleanup, 0);
446 struct value *val;
447 struct value *val_mark;
448 int loop;
449 enum command_control_type ret;
450 char *new_line;
451
452 /* Start by assuming failure, if a problem is detected, the code
453 below will simply "break" out of the switch. */
454 ret = invalid_control;
455
456 switch (cmd->control_type)
457 {
458 case simple_control:
459 /* A simple command, execute it and return. */
460 new_line = insert_args (cmd->line);
461 if (!new_line)
462 break;
463 make_cleanup (free_current_contents, &new_line);
464 execute_command (new_line, 0);
465 ret = cmd->control_type;
466 break;
467
468 case continue_control:
469 print_command_trace ("loop_continue");
470
471 /* Return for "continue", and "break" so we can either
472 continue the loop at the top, or break out. */
473 ret = cmd->control_type;
474 break;
475
476 case break_control:
477 print_command_trace ("loop_break");
478
479 /* Return for "continue", and "break" so we can either
480 continue the loop at the top, or break out. */
481 ret = cmd->control_type;
482 break;
483
484 case while_control:
485 {
486 int len = strlen (cmd->line) + 7;
487 char *buffer = (char *) alloca (len);
488
489 xsnprintf (buffer, len, "while %s", cmd->line);
490 print_command_trace (buffer);
491
492 /* Parse the loop control expression for the while statement. */
493 new_line = insert_args (cmd->line);
494 if (!new_line)
495 break;
496 make_cleanup (free_current_contents, &new_line);
497 expr = parse_expression (new_line);
498 make_cleanup (free_current_contents, &expr);
499
500 ret = simple_control;
501 loop = 1;
502
503 /* Keep iterating so long as the expression is true. */
504 while (loop == 1)
505 {
506 int cond_result;
507
508 QUIT;
509
510 /* Evaluate the expression. */
511 val_mark = value_mark ();
512 val = evaluate_expression (expr);
513 cond_result = value_true (val);
514 value_free_to_mark (val_mark);
515
516 /* If the value is false, then break out of the loop. */
517 if (!cond_result)
518 break;
519
520 /* Execute the body of the while statement. */
521 current = *cmd->body_list;
522 while (current)
523 {
524 command_nest_depth++;
525 ret = execute_control_command (current);
526 command_nest_depth--;
527
528 /* If we got an error, or a "break" command, then stop
529 looping. */
530 if (ret == invalid_control || ret == break_control)
531 {
532 loop = 0;
533 break;
534 }
535
536 /* If we got a "continue" command, then restart the loop
537 at this point. */
538 if (ret == continue_control)
539 break;
540
541 /* Get the next statement. */
542 current = current->next;
543 }
544 }
545
546 /* Reset RET so that we don't recurse the break all the way down. */
547 if (ret == break_control)
548 ret = simple_control;
549
550 break;
551 }
552
553 case if_control:
554 {
555 int len = strlen (cmd->line) + 4;
556 char *buffer = (char *) alloca (len);
557
558 xsnprintf (buffer, len, "if %s", cmd->line);
559 print_command_trace (buffer);
560
561 new_line = insert_args (cmd->line);
562 if (!new_line)
563 break;
564 make_cleanup (free_current_contents, &new_line);
565 /* Parse the conditional for the if statement. */
566 expr = parse_expression (new_line);
567 make_cleanup (free_current_contents, &expr);
568
569 current = NULL;
570 ret = simple_control;
571
572 /* Evaluate the conditional. */
573 val_mark = value_mark ();
574 val = evaluate_expression (expr);
575
576 /* Choose which arm to take commands from based on the value
577 of the conditional expression. */
578 if (value_true (val))
579 current = *cmd->body_list;
580 else if (cmd->body_count == 2)
581 current = *(cmd->body_list + 1);
582 value_free_to_mark (val_mark);
583
584 /* Execute commands in the given arm. */
585 while (current)
586 {
587 command_nest_depth++;
588 ret = execute_control_command (current);
589 command_nest_depth--;
590
591 /* If we got an error, get out. */
592 if (ret != simple_control)
593 break;
594
595 /* Get the next statement in the body. */
596 current = current->next;
597 }
598
599 break;
600 }
601
602 case commands_control:
603 {
604 /* Breakpoint commands list, record the commands in the
605 breakpoint's command list and return. */
606 new_line = insert_args (cmd->line);
607 if (!new_line)
608 break;
609 make_cleanup (free_current_contents, &new_line);
610 ret = commands_from_control_command (new_line, cmd);
611 break;
612 }
613
614 case compile_control:
615 eval_compile_command (cmd, NULL, cmd->control_u.compile.scope,
616 cmd->control_u.compile.scope_data);
617 ret = simple_control;
618 break;
619
620 case python_control:
621 case guile_control:
622 {
623 eval_ext_lang_from_control_command (cmd);
624 ret = simple_control;
625 break;
626 }
627
628 default:
629 warning (_("Invalid control type in canned commands structure."));
630 break;
631 }
632
633 do_cleanups (old_chain);
634
635 return ret;
636 }
637
638 /* Like execute_control_command, but first set
639 suppress_next_print_command_trace. */
640
641 enum command_control_type
642 execute_control_command_untraced (struct command_line *cmd)
643 {
644 suppress_next_print_command_trace = 1;
645 return execute_control_command (cmd);
646 }
647
648
649 /* "while" command support. Executes a body of statements while the
650 loop condition is nonzero. */
651
652 static void
653 while_command (char *arg, int from_tty)
654 {
655 struct command_line *command = NULL;
656 struct cleanup *old_chain;
657
658 control_level = 1;
659 command = get_command_line (while_control, arg);
660
661 if (command == NULL)
662 return;
663
664 old_chain = make_cleanup_restore_integer (&interpreter_async);
665 interpreter_async = 0;
666
667 execute_control_command_untraced (command);
668 free_command_lines (&command);
669
670 do_cleanups (old_chain);
671 }
672
673 /* "if" command support. Execute either the true or false arm depending
674 on the value of the if conditional. */
675
676 static void
677 if_command (char *arg, int from_tty)
678 {
679 struct command_line *command = NULL;
680 struct cleanup *old_chain;
681
682 control_level = 1;
683 command = get_command_line (if_control, arg);
684
685 if (command == NULL)
686 return;
687
688 old_chain = make_cleanup_restore_integer (&interpreter_async);
689 interpreter_async = 0;
690
691 execute_control_command_untraced (command);
692 free_command_lines (&command);
693
694 do_cleanups (old_chain);
695 }
696
697 /* Cleanup */
698 static void
699 arg_cleanup (void *ignore)
700 {
701 struct user_args *oargs = user_args;
702
703 if (!user_args)
704 internal_error (__FILE__, __LINE__,
705 _("arg_cleanup called with no user args.\n"));
706
707 user_args = user_args->next;
708 xfree (oargs->command);
709 xfree (oargs);
710 }
711
712 /* Bind the incomming arguments for a user defined command to
713 $arg0, $arg1 ... $argMAXUSERARGS. */
714
715 static struct cleanup *
716 setup_user_args (char *p)
717 {
718 struct user_args *args;
719 struct cleanup *old_chain;
720 unsigned int arg_count = 0;
721
722 args = XNEW (struct user_args);
723 memset (args, 0, sizeof (struct user_args));
724
725 args->next = user_args;
726 user_args = args;
727
728 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
729
730 if (p == NULL)
731 return old_chain;
732
733 user_args->command = p = xstrdup (p);
734
735 while (*p)
736 {
737 char *start_arg;
738 int squote = 0;
739 int dquote = 0;
740 int bsquote = 0;
741
742 if (arg_count >= MAXUSERARGS)
743 error (_("user defined function may only have %d arguments."),
744 MAXUSERARGS);
745
746 /* Strip whitespace. */
747 while (*p == ' ' || *p == '\t')
748 p++;
749
750 /* P now points to an argument. */
751 start_arg = p;
752 user_args->a[arg_count].arg = p;
753
754 /* Get to the end of this argument. */
755 while (*p)
756 {
757 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
758 break;
759 else
760 {
761 if (bsquote)
762 bsquote = 0;
763 else if (*p == '\\')
764 bsquote = 1;
765 else if (squote)
766 {
767 if (*p == '\'')
768 squote = 0;
769 }
770 else if (dquote)
771 {
772 if (*p == '"')
773 dquote = 0;
774 }
775 else
776 {
777 if (*p == '\'')
778 squote = 1;
779 else if (*p == '"')
780 dquote = 1;
781 }
782 p++;
783 }
784 }
785
786 user_args->a[arg_count].len = p - start_arg;
787 arg_count++;
788 user_args->count++;
789 }
790 return old_chain;
791 }
792
793 /* Given character string P, return a point to the first argument
794 ($arg), or NULL if P contains no arguments. */
795
796 static char *
797 locate_arg (char *p)
798 {
799 while ((p = strchr (p, '$')))
800 {
801 if (startswith (p, "$arg")
802 && (isdigit (p[4]) || p[4] == 'c'))
803 return p;
804 p++;
805 }
806 return NULL;
807 }
808
809 /* Insert the user defined arguments stored in user_arg into the $arg
810 arguments found in line, with the updated copy being placed into
811 nline. */
812
813 static char *
814 insert_args (char *line)
815 {
816 char *p, *save_line, *new_line;
817 unsigned len, i;
818
819 /* If we are not in a user-defined function, treat $argc, $arg0, et
820 cetera as normal convenience variables. */
821 if (user_args == NULL)
822 return xstrdup (line);
823
824 /* First we need to know how much memory to allocate for the new
825 line. */
826 save_line = line;
827 len = 0;
828 while ((p = locate_arg (line)))
829 {
830 len += p - line;
831 i = p[4] - '0';
832
833 if (p[4] == 'c')
834 {
835 /* $argc. Number will be <=10. */
836 len += user_args->count == 10 ? 2 : 1;
837 }
838 else if (i >= user_args->count)
839 {
840 error (_("Missing argument %d in user function."), i);
841 return NULL;
842 }
843 else
844 {
845 len += user_args->a[i].len;
846 }
847 line = p + 5;
848 }
849
850 /* Don't forget the tail. */
851 len += strlen (line);
852
853 /* Allocate space for the new line and fill it in. */
854 new_line = (char *) xmalloc (len + 1);
855 if (new_line == NULL)
856 return NULL;
857
858 /* Restore pointer to beginning of old line. */
859 line = save_line;
860
861 /* Save pointer to beginning of new line. */
862 save_line = new_line;
863
864 while ((p = locate_arg (line)))
865 {
866 int i, len;
867
868 memcpy (new_line, line, p - line);
869 new_line += p - line;
870
871 if (p[4] == 'c')
872 {
873 gdb_assert (user_args->count >= 0 && user_args->count <= 10);
874 if (user_args->count == 10)
875 {
876 *(new_line++) = '1';
877 *(new_line++) = '0';
878 }
879 else
880 *(new_line++) = user_args->count + '0';
881 }
882 else
883 {
884 i = p[4] - '0';
885 len = user_args->a[i].len;
886 if (len)
887 {
888 memcpy (new_line, user_args->a[i].arg, len);
889 new_line += len;
890 }
891 }
892 line = p + 5;
893 }
894 /* Don't forget the tail. */
895 strcpy (new_line, line);
896
897 /* Return a pointer to the beginning of the new line. */
898 return save_line;
899 }
900
901 \f
902 /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
903 code bodies. This is typically used when we encounter an "else"
904 clause for an "if" command. */
905
906 static void
907 realloc_body_list (struct command_line *command, int new_length)
908 {
909 int n;
910 struct command_line **body_list;
911
912 n = command->body_count;
913
914 /* Nothing to do? */
915 if (new_length <= n)
916 return;
917
918 body_list = XCNEWVEC (struct command_line *, new_length);
919
920 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
921
922 xfree (command->body_list);
923 command->body_list = body_list;
924 command->body_count = new_length;
925 }
926
927 /* Read next line from stdin. Passed to read_command_line_1 and
928 recurse_read_control_structure whenever we need to read commands
929 from stdin. */
930
931 static char *
932 read_next_line (void)
933 {
934 char *prompt_ptr, control_prompt[256];
935 int i = 0;
936
937 if (control_level >= 254)
938 error (_("Control nesting too deep!"));
939
940 /* Set a prompt based on the nesting of the control commands. */
941 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
942 {
943 for (i = 0; i < control_level; i++)
944 control_prompt[i] = ' ';
945 control_prompt[i] = '>';
946 control_prompt[i + 1] = '\0';
947 prompt_ptr = (char *) &control_prompt[0];
948 }
949 else
950 prompt_ptr = NULL;
951
952 return command_line_input (prompt_ptr, instream == stdin, "commands");
953 }
954
955 /* Process one input line. If the command is an "end", return such an
956 indication to the caller. If PARSE_COMMANDS is true, strip leading
957 whitespace (trailing whitespace is always stripped) in the line,
958 attempt to recognize GDB control commands, and also return an
959 indication if the command is an "else" or a nop.
960
961 Otherwise, only "end" is recognized. */
962
963 static enum misc_command_type
964 process_next_line (char *p, struct command_line **command, int parse_commands,
965 void (*validator)(char *, void *), void *closure)
966 {
967 char *p_end;
968 char *p_start;
969 int not_handled = 0;
970
971 /* Not sure what to do here. */
972 if (p == NULL)
973 return end_command;
974
975 /* Strip trailing whitespace. */
976 p_end = p + strlen (p);
977 while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
978 p_end--;
979
980 p_start = p;
981 /* Strip leading whitespace. */
982 while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
983 p_start++;
984
985 /* 'end' is always recognized, regardless of parse_commands value.
986 We also permit whitespace before end and after. */
987 if (p_end - p_start == 3 && startswith (p_start, "end"))
988 return end_command;
989
990 if (parse_commands)
991 {
992 /* If commands are parsed, we skip initial spaces. Otherwise,
993 which is the case for Python commands and documentation
994 (see the 'document' command), spaces are preserved. */
995 p = p_start;
996
997 /* Blanks and comments don't really do anything, but we need to
998 distinguish them from else, end and other commands which can
999 be executed. */
1000 if (p_end == p || p[0] == '#')
1001 return nop_command;
1002
1003 /* Is the else clause of an if control structure? */
1004 if (p_end - p == 4 && startswith (p, "else"))
1005 return else_command;
1006
1007 /* Check for while, if, break, continue, etc and build a new
1008 command line structure for them. */
1009 if ((p_end - p >= 14 && startswith (p, "while-stepping"))
1010 || (p_end - p >= 8 && startswith (p, "stepping"))
1011 || (p_end - p >= 2 && startswith (p, "ws")))
1012 {
1013 /* Because validate_actionline and encode_action lookup
1014 command's line as command, we need the line to
1015 include 'while-stepping'.
1016
1017 For 'ws' alias, the command will have 'ws', not expanded
1018 to 'while-stepping'. This is intentional -- we don't
1019 really want frontend to send a command list with 'ws',
1020 and next break-info returning command line with
1021 'while-stepping'. This should work, but might cause the
1022 breakpoint to be marked as changed while it's actually
1023 not. */
1024 *command = build_command_line (while_stepping_control, p);
1025 }
1026 else if (p_end - p > 5 && startswith (p, "while"))
1027 {
1028 char *first_arg;
1029
1030 first_arg = p + 5;
1031 while (first_arg < p_end && isspace (*first_arg))
1032 first_arg++;
1033 *command = build_command_line (while_control, first_arg);
1034 }
1035 else if (p_end - p > 2 && startswith (p, "if"))
1036 {
1037 char *first_arg;
1038
1039 first_arg = p + 2;
1040 while (first_arg < p_end && isspace (*first_arg))
1041 first_arg++;
1042 *command = build_command_line (if_control, first_arg);
1043 }
1044 else if (p_end - p >= 8 && startswith (p, "commands"))
1045 {
1046 char *first_arg;
1047
1048 first_arg = p + 8;
1049 while (first_arg < p_end && isspace (*first_arg))
1050 first_arg++;
1051 *command = build_command_line (commands_control, first_arg);
1052 }
1053 else if (p_end - p == 6 && startswith (p, "python"))
1054 {
1055 /* Note that we ignore the inline "python command" form
1056 here. */
1057 *command = build_command_line (python_control, "");
1058 }
1059 else if (p_end - p == 6 && startswith (p, "compile"))
1060 {
1061 /* Note that we ignore the inline "compile command" form
1062 here. */
1063 *command = build_command_line (compile_control, "");
1064 (*command)->control_u.compile.scope = COMPILE_I_INVALID_SCOPE;
1065 }
1066
1067 else if (p_end - p == 5 && startswith (p, "guile"))
1068 {
1069 /* Note that we ignore the inline "guile command" form here. */
1070 *command = build_command_line (guile_control, "");
1071 }
1072 else if (p_end - p == 10 && startswith (p, "loop_break"))
1073 {
1074 *command = XNEW (struct command_line);
1075 (*command)->next = NULL;
1076 (*command)->line = NULL;
1077 (*command)->control_type = break_control;
1078 (*command)->body_count = 0;
1079 (*command)->body_list = NULL;
1080 }
1081 else if (p_end - p == 13 && startswith (p, "loop_continue"))
1082 {
1083 *command = XNEW (struct command_line);
1084 (*command)->next = NULL;
1085 (*command)->line = NULL;
1086 (*command)->control_type = continue_control;
1087 (*command)->body_count = 0;
1088 (*command)->body_list = NULL;
1089 }
1090 else
1091 not_handled = 1;
1092 }
1093
1094 if (!parse_commands || not_handled)
1095 {
1096 /* A normal command. */
1097 *command = XNEW (struct command_line);
1098 (*command)->next = NULL;
1099 (*command)->line = savestring (p, p_end - p);
1100 (*command)->control_type = simple_control;
1101 (*command)->body_count = 0;
1102 (*command)->body_list = NULL;
1103 }
1104
1105 if (validator)
1106 {
1107
1108 TRY
1109 {
1110 validator ((*command)->line, closure);
1111 }
1112 CATCH (ex, RETURN_MASK_ALL)
1113 {
1114 xfree (*command);
1115 throw_exception (ex);
1116 }
1117 END_CATCH
1118 }
1119
1120 /* Nothing special. */
1121 return ok_command;
1122 }
1123
1124 /* Recursively read in the control structures and create a
1125 command_line structure from them. Use read_next_line_func to
1126 obtain lines of the command. */
1127
1128 static enum command_control_type
1129 recurse_read_control_structure (char * (*read_next_line_func) (void),
1130 struct command_line *current_cmd,
1131 void (*validator)(char *, void *),
1132 void *closure)
1133 {
1134 int current_body, i;
1135 enum misc_command_type val;
1136 enum command_control_type ret;
1137 struct command_line **body_ptr, *child_tail, *next;
1138
1139 child_tail = NULL;
1140 current_body = 1;
1141
1142 /* Sanity checks. */
1143 if (current_cmd->control_type == simple_control)
1144 error (_("Recursed on a simple control type."));
1145
1146 if (current_body > current_cmd->body_count)
1147 error (_("Allocated body is smaller than this command type needs."));
1148
1149 /* Read lines from the input stream and build control structures. */
1150 while (1)
1151 {
1152 dont_repeat ();
1153
1154 next = NULL;
1155 val = process_next_line (read_next_line_func (), &next,
1156 current_cmd->control_type != python_control
1157 && current_cmd->control_type != guile_control
1158 && current_cmd->control_type != compile_control,
1159 validator, closure);
1160
1161 /* Just skip blanks and comments. */
1162 if (val == nop_command)
1163 continue;
1164
1165 if (val == end_command)
1166 {
1167 if (multi_line_command_p (current_cmd->control_type))
1168 {
1169 /* Success reading an entire canned sequence of commands. */
1170 ret = simple_control;
1171 break;
1172 }
1173 else
1174 {
1175 ret = invalid_control;
1176 break;
1177 }
1178 }
1179
1180 /* Not the end of a control structure. */
1181 if (val == else_command)
1182 {
1183 if (current_cmd->control_type == if_control
1184 && current_body == 1)
1185 {
1186 realloc_body_list (current_cmd, 2);
1187 current_body = 2;
1188 child_tail = NULL;
1189 continue;
1190 }
1191 else
1192 {
1193 ret = invalid_control;
1194 break;
1195 }
1196 }
1197
1198 if (child_tail)
1199 {
1200 child_tail->next = next;
1201 }
1202 else
1203 {
1204 body_ptr = current_cmd->body_list;
1205 for (i = 1; i < current_body; i++)
1206 body_ptr++;
1207
1208 *body_ptr = next;
1209
1210 }
1211
1212 child_tail = next;
1213
1214 /* If the latest line is another control structure, then recurse
1215 on it. */
1216 if (multi_line_command_p (next->control_type))
1217 {
1218 control_level++;
1219 ret = recurse_read_control_structure (read_next_line_func, next,
1220 validator, closure);
1221 control_level--;
1222
1223 if (ret != simple_control)
1224 break;
1225 }
1226 }
1227
1228 dont_repeat ();
1229
1230 return ret;
1231 }
1232
1233 static void
1234 restore_interp (void *arg)
1235 {
1236 interp_set_temp (interp_name ((struct interp *)arg));
1237 }
1238
1239 /* Read lines from the input stream and accumulate them in a chain of
1240 struct command_line's, which is then returned. For input from a
1241 terminal, the special command "end" is used to mark the end of the
1242 input, and is not included in the returned chain of commands.
1243
1244 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1245 is always stripped) in the line and attempt to recognize GDB control
1246 commands. Otherwise, only "end" is recognized. */
1247
1248 #define END_MESSAGE "End with a line saying just \"end\"."
1249
1250 struct command_line *
1251 read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1252 void (*validator)(char *, void *), void *closure)
1253 {
1254 struct command_line *head;
1255
1256 if (from_tty && input_from_terminal_p ())
1257 {
1258 if (deprecated_readline_begin_hook)
1259 {
1260 /* Note - intentional to merge messages with no newline. */
1261 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg,
1262 END_MESSAGE);
1263 }
1264 else
1265 {
1266 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1267 gdb_flush (gdb_stdout);
1268 }
1269 }
1270
1271
1272 /* Reading commands assumes the CLI behavior, so temporarily
1273 override the current interpreter with CLI. */
1274 if (current_interp_named_p (INTERP_CONSOLE))
1275 head = read_command_lines_1 (read_next_line, parse_commands,
1276 validator, closure);
1277 else
1278 {
1279 struct interp *old_interp = interp_set_temp (INTERP_CONSOLE);
1280 struct cleanup *old_chain = make_cleanup (restore_interp, old_interp);
1281
1282 head = read_command_lines_1 (read_next_line, parse_commands,
1283 validator, closure);
1284 do_cleanups (old_chain);
1285 }
1286
1287 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1288 {
1289 (*deprecated_readline_end_hook) ();
1290 }
1291 return (head);
1292 }
1293
1294 /* Act the same way as read_command_lines, except that each new line is
1295 obtained using READ_NEXT_LINE_FUNC. */
1296
1297 struct command_line *
1298 read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1299 void (*validator)(char *, void *), void *closure)
1300 {
1301 struct command_line *head, *tail, *next;
1302 struct cleanup *old_chain = make_cleanup (null_cleanup, NULL);
1303 enum command_control_type ret;
1304 enum misc_command_type val;
1305
1306 control_level = 0;
1307 head = tail = NULL;
1308
1309 while (1)
1310 {
1311 dont_repeat ();
1312 val = process_next_line (read_next_line_func (), &next, parse_commands,
1313 validator, closure);
1314
1315 /* Ignore blank lines or comments. */
1316 if (val == nop_command)
1317 continue;
1318
1319 if (val == end_command)
1320 {
1321 ret = simple_control;
1322 break;
1323 }
1324
1325 if (val != ok_command)
1326 {
1327 ret = invalid_control;
1328 break;
1329 }
1330
1331 if (multi_line_command_p (next->control_type))
1332 {
1333 control_level++;
1334 ret = recurse_read_control_structure (read_next_line_func, next,
1335 validator, closure);
1336 control_level--;
1337
1338 if (ret == invalid_control)
1339 break;
1340 }
1341
1342 if (tail)
1343 {
1344 tail->next = next;
1345 }
1346 else
1347 {
1348 head = next;
1349 make_cleanup_free_command_lines (&head);
1350 }
1351 tail = next;
1352 }
1353
1354 dont_repeat ();
1355
1356 if (ret != invalid_control)
1357 discard_cleanups (old_chain);
1358 else
1359 do_cleanups (old_chain);
1360
1361 return head;
1362 }
1363
1364 /* Free a chain of struct command_line's. */
1365
1366 void
1367 free_command_lines (struct command_line **lptr)
1368 {
1369 struct command_line *l = *lptr;
1370 struct command_line *next;
1371 struct command_line **blist;
1372 int i;
1373
1374 while (l)
1375 {
1376 if (l->body_count > 0)
1377 {
1378 blist = l->body_list;
1379 for (i = 0; i < l->body_count; i++, blist++)
1380 free_command_lines (blist);
1381 }
1382 next = l->next;
1383 xfree (l->line);
1384 xfree (l);
1385 l = next;
1386 }
1387 *lptr = NULL;
1388 }
1389
1390 static void
1391 do_free_command_lines_cleanup (void *arg)
1392 {
1393 free_command_lines ((struct command_line **) arg);
1394 }
1395
1396 struct cleanup *
1397 make_cleanup_free_command_lines (struct command_line **arg)
1398 {
1399 return make_cleanup (do_free_command_lines_cleanup, arg);
1400 }
1401
1402 struct command_line *
1403 copy_command_lines (struct command_line *cmds)
1404 {
1405 struct command_line *result = NULL;
1406
1407 if (cmds)
1408 {
1409 result = XNEW (struct command_line);
1410
1411 result->next = copy_command_lines (cmds->next);
1412 result->line = xstrdup (cmds->line);
1413 result->control_type = cmds->control_type;
1414 result->body_count = cmds->body_count;
1415 if (cmds->body_count > 0)
1416 {
1417 int i;
1418
1419 result->body_list = XNEWVEC (struct command_line *, cmds->body_count);
1420
1421 for (i = 0; i < cmds->body_count; i++)
1422 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1423 }
1424 else
1425 result->body_list = NULL;
1426 }
1427
1428 return result;
1429 }
1430 \f
1431 /* Validate that *COMNAME is a valid name for a command. Return the
1432 containing command list, in case it starts with a prefix command.
1433 The prefix must already exist. *COMNAME is advanced to point after
1434 any prefix, and a NUL character overwrites the space after the
1435 prefix. */
1436
1437 static struct cmd_list_element **
1438 validate_comname (char **comname)
1439 {
1440 struct cmd_list_element **list = &cmdlist;
1441 char *p, *last_word;
1442
1443 if (*comname == 0)
1444 error_no_arg (_("name of command to define"));
1445
1446 /* Find the last word of the argument. */
1447 p = *comname + strlen (*comname);
1448 while (p > *comname && isspace (p[-1]))
1449 p--;
1450 while (p > *comname && !isspace (p[-1]))
1451 p--;
1452 last_word = p;
1453
1454 /* Find the corresponding command list. */
1455 if (last_word != *comname)
1456 {
1457 struct cmd_list_element *c;
1458 char saved_char;
1459 const char *tem = *comname;
1460
1461 /* Separate the prefix and the command. */
1462 saved_char = last_word[-1];
1463 last_word[-1] = '\0';
1464
1465 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1466 if (c->prefixlist == NULL)
1467 error (_("\"%s\" is not a prefix command."), *comname);
1468
1469 list = c->prefixlist;
1470 last_word[-1] = saved_char;
1471 *comname = last_word;
1472 }
1473
1474 p = *comname;
1475 while (*p)
1476 {
1477 if (!isalnum (*p) && *p != '-' && *p != '_')
1478 error (_("Junk in argument list: \"%s\""), p);
1479 p++;
1480 }
1481
1482 return list;
1483 }
1484
1485 /* This is just a placeholder in the command data structures. */
1486 static void
1487 user_defined_command (char *ignore, int from_tty)
1488 {
1489 }
1490
1491 static void
1492 define_command (char *comname, int from_tty)
1493 {
1494 #define MAX_TMPBUF 128
1495 enum cmd_hook_type
1496 {
1497 CMD_NO_HOOK = 0,
1498 CMD_PRE_HOOK,
1499 CMD_POST_HOOK
1500 };
1501 struct command_line *cmds;
1502 struct cmd_list_element *c, *newc, *hookc = 0, **list;
1503 char *tem, *comfull;
1504 const char *tem_c;
1505 char tmpbuf[MAX_TMPBUF];
1506 int hook_type = CMD_NO_HOOK;
1507 int hook_name_size = 0;
1508
1509 #define HOOK_STRING "hook-"
1510 #define HOOK_LEN 5
1511 #define HOOK_POST_STRING "hookpost-"
1512 #define HOOK_POST_LEN 9
1513
1514 comfull = comname;
1515 list = validate_comname (&comname);
1516
1517 /* Look it up, and verify that we got an exact match. */
1518 tem_c = comname;
1519 c = lookup_cmd (&tem_c, *list, "", -1, 1);
1520 if (c && strcmp (comname, c->name) != 0)
1521 c = 0;
1522
1523 if (c)
1524 {
1525 int q;
1526
1527 if (c->theclass == class_user || c->theclass == class_alias)
1528 q = query (_("Redefine command \"%s\"? "), c->name);
1529 else
1530 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
1531 if (!q)
1532 error (_("Command \"%s\" not redefined."), c->name);
1533 }
1534
1535 /* If this new command is a hook, then mark the command which it
1536 is hooking. Note that we allow hooking `help' commands, so that
1537 we can hook the `stop' pseudo-command. */
1538
1539 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1540 {
1541 hook_type = CMD_PRE_HOOK;
1542 hook_name_size = HOOK_LEN;
1543 }
1544 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1545 {
1546 hook_type = CMD_POST_HOOK;
1547 hook_name_size = HOOK_POST_LEN;
1548 }
1549
1550 if (hook_type != CMD_NO_HOOK)
1551 {
1552 /* Look up cmd it hooks, and verify that we got an exact match. */
1553 tem_c = comname + hook_name_size;
1554 hookc = lookup_cmd (&tem_c, *list, "", -1, 0);
1555 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
1556 hookc = 0;
1557 if (!hookc)
1558 {
1559 warning (_("Your new `%s' command does not "
1560 "hook any existing command."),
1561 comfull);
1562 if (!query (_("Proceed? ")))
1563 error (_("Not confirmed."));
1564 }
1565 }
1566
1567 comname = xstrdup (comname);
1568
1569 /* If the rest of the commands will be case insensitive, this one
1570 should behave in the same manner. */
1571 for (tem = comname; *tem; tem++)
1572 if (isupper (*tem))
1573 *tem = tolower (*tem);
1574
1575 xsnprintf (tmpbuf, sizeof (tmpbuf),
1576 "Type commands for definition of \"%s\".", comfull);
1577 cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
1578
1579 if (c && c->theclass == class_user)
1580 free_command_lines (&c->user_commands);
1581
1582 newc = add_cmd (comname, class_user, user_defined_command,
1583 (c && c->theclass == class_user)
1584 ? c->doc : xstrdup ("User-defined."), list);
1585 newc->user_commands = cmds;
1586
1587 /* If this new command is a hook, then mark both commands as being
1588 tied. */
1589 if (hookc)
1590 {
1591 switch (hook_type)
1592 {
1593 case CMD_PRE_HOOK:
1594 hookc->hook_pre = newc; /* Target gets hooked. */
1595 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
1596 break;
1597 case CMD_POST_HOOK:
1598 hookc->hook_post = newc; /* Target gets hooked. */
1599 newc->hookee_post = hookc; /* We are marked as hooking
1600 target cmd. */
1601 break;
1602 default:
1603 /* Should never come here as hookc would be 0. */
1604 internal_error (__FILE__, __LINE__, _("bad switch"));
1605 }
1606 }
1607 }
1608
1609 static void
1610 document_command (char *comname, int from_tty)
1611 {
1612 struct command_line *doclines;
1613 struct cmd_list_element *c, **list;
1614 const char *tem;
1615 char *comfull;
1616 char tmpbuf[128];
1617
1618 comfull = comname;
1619 list = validate_comname (&comname);
1620
1621 tem = comname;
1622 c = lookup_cmd (&tem, *list, "", 0, 1);
1623
1624 if (c->theclass != class_user)
1625 error (_("Command \"%s\" is built-in."), comfull);
1626
1627 xsnprintf (tmpbuf, sizeof (tmpbuf), "Type documentation for \"%s\".",
1628 comfull);
1629 doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
1630
1631 if (c->doc)
1632 xfree ((char *) c->doc);
1633
1634 {
1635 struct command_line *cl1;
1636 int len = 0;
1637 char *doc;
1638
1639 for (cl1 = doclines; cl1; cl1 = cl1->next)
1640 len += strlen (cl1->line) + 1;
1641
1642 doc = (char *) xmalloc (len + 1);
1643 *doc = 0;
1644
1645 for (cl1 = doclines; cl1; cl1 = cl1->next)
1646 {
1647 strcat (doc, cl1->line);
1648 if (cl1->next)
1649 strcat (doc, "\n");
1650 }
1651
1652 c->doc = doc;
1653 }
1654
1655 free_command_lines (&doclines);
1656 }
1657 \f
1658 struct source_cleanup_lines_args
1659 {
1660 int old_line;
1661 const char *old_file;
1662 };
1663
1664 static void
1665 source_cleanup_lines (void *args)
1666 {
1667 struct source_cleanup_lines_args *p =
1668 (struct source_cleanup_lines_args *) args;
1669
1670 source_line_number = p->old_line;
1671 source_file_name = p->old_file;
1672 }
1673
1674 /* Used to implement source_command. */
1675
1676 void
1677 script_from_file (FILE *stream, const char *file)
1678 {
1679 struct cleanup *old_cleanups;
1680 struct source_cleanup_lines_args old_lines;
1681
1682 if (stream == NULL)
1683 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
1684
1685 old_lines.old_line = source_line_number;
1686 old_lines.old_file = source_file_name;
1687 old_cleanups = make_cleanup (source_cleanup_lines, &old_lines);
1688 source_line_number = 0;
1689 source_file_name = file;
1690
1691 make_cleanup_restore_integer (&interpreter_async);
1692 interpreter_async = 0;
1693
1694 {
1695
1696 TRY
1697 {
1698 read_command_file (stream);
1699 }
1700 CATCH (e, RETURN_MASK_ERROR)
1701 {
1702 /* Re-throw the error, but with the file name information
1703 prepended. */
1704 throw_error (e.error,
1705 _("%s:%d: Error in sourced command file:\n%s"),
1706 source_file_name, source_line_number, e.message);
1707 }
1708 END_CATCH
1709 }
1710
1711 do_cleanups (old_cleanups);
1712 }
1713
1714 /* Print the definition of user command C to STREAM. Or, if C is a
1715 prefix command, show the definitions of all user commands under C
1716 (recursively). PREFIX and NAME combined are the name of the
1717 current command. */
1718 void
1719 show_user_1 (struct cmd_list_element *c, const char *prefix, const char *name,
1720 struct ui_file *stream)
1721 {
1722 struct command_line *cmdlines;
1723
1724 if (c->prefixlist != NULL)
1725 {
1726 const char *prefixname = c->prefixname;
1727
1728 for (c = *c->prefixlist; c != NULL; c = c->next)
1729 if (c->theclass == class_user || c->prefixlist != NULL)
1730 show_user_1 (c, prefixname, c->name, gdb_stdout);
1731 return;
1732 }
1733
1734 cmdlines = c->user_commands;
1735 fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
1736
1737 if (!cmdlines)
1738 return;
1739 print_command_lines (current_uiout, cmdlines, 1);
1740 fputs_filtered ("\n", stream);
1741 }
1742
1743 \f
1744
1745 initialize_file_ftype _initialize_cli_script;
1746
1747 void
1748 _initialize_cli_script (void)
1749 {
1750 add_com ("document", class_support, document_command, _("\
1751 Document a user-defined command.\n\
1752 Give command name as argument. Give documentation on following lines.\n\
1753 End with a line of just \"end\"."));
1754 add_com ("define", class_support, define_command, _("\
1755 Define a new command name. Command name is argument.\n\
1756 Definition appears on following lines, one command per line.\n\
1757 End with a line of just \"end\".\n\
1758 Use the \"document\" command to give documentation for the new command.\n\
1759 Commands defined in this way may have up to ten arguments."));
1760
1761 add_com ("while", class_support, while_command, _("\
1762 Execute nested commands WHILE the conditional expression is non zero.\n\
1763 The conditional expression must follow the word `while' and must in turn be\n\
1764 followed by a new line. The nested commands must be entered one per line,\n\
1765 and should be terminated by the word `end'."));
1766
1767 add_com ("if", class_support, if_command, _("\
1768 Execute nested commands once IF the conditional expression is non zero.\n\
1769 The conditional expression must follow the word `if' and must in turn be\n\
1770 followed by a new line. The nested commands must be entered one per line,\n\
1771 and should be terminated by the word 'else' or `end'. If an else clause\n\
1772 is used, the same rules apply to its nested commands as to the first ones."));
1773 }
This page took 0.066772 seconds and 4 git commands to generate.