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