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