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