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