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