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