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