gdb/testsuite/
[deliverable/binutils-gdb.git] / gdb / cli / cli-script.c
CommitLineData
d318976c 1/* GDB CLI command scripting.
8926118c 2
0b302171 3 Copyright (c) 1986-2002, 2004-2012 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
DH
316 static int user_call_depth = 0;
317 extern 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 {
16026cd7 444 char *buffer = alloca (strlen (cmd->line) + 7);
cdb27c12 445
16026cd7
AS
446 sprintf (buffer, "while %s", cmd->line);
447 print_command_trace (buffer);
448
d318976c
FN
449 /* Parse the loop control expression for the while statement. */
450 new_line = insert_args (cmd->line);
451 if (!new_line)
4d2acc65
AC
452 break;
453 make_cleanup (free_current_contents, &new_line);
d318976c
FN
454 expr = parse_expression (new_line);
455 make_cleanup (free_current_contents, &expr);
456
457 ret = simple_control;
458 loop = 1;
459
460 /* Keep iterating so long as the expression is true. */
461 while (loop == 1)
462 {
463 int cond_result;
464
465 QUIT;
466
467 /* Evaluate the expression. */
468 val_mark = value_mark ();
469 val = evaluate_expression (expr);
470 cond_result = value_true (val);
471 value_free_to_mark (val_mark);
472
473 /* If the value is false, then break out of the loop. */
474 if (!cond_result)
475 break;
476
477 /* Execute the body of the while statement. */
478 current = *cmd->body_list;
479 while (current)
480 {
16026cd7 481 command_nest_depth++;
d318976c 482 ret = execute_control_command (current);
16026cd7 483 command_nest_depth--;
d318976c
FN
484
485 /* If we got an error, or a "break" command, then stop
486 looping. */
487 if (ret == invalid_control || ret == break_control)
488 {
489 loop = 0;
490 break;
491 }
492
493 /* If we got a "continue" command, then restart the loop
494 at this point. */
495 if (ret == continue_control)
496 break;
497
498 /* Get the next statement. */
499 current = current->next;
500 }
501 }
502
503 /* Reset RET so that we don't recurse the break all the way down. */
504 if (ret == break_control)
505 ret = simple_control;
506
507 break;
508 }
509
510 case if_control:
511 {
16026cd7 512 char *buffer = alloca (strlen (cmd->line) + 4);
cdb27c12 513
16026cd7
AS
514 sprintf (buffer, "if %s", cmd->line);
515 print_command_trace (buffer);
516
d318976c
FN
517 new_line = insert_args (cmd->line);
518 if (!new_line)
4d2acc65
AC
519 break;
520 make_cleanup (free_current_contents, &new_line);
d318976c
FN
521 /* Parse the conditional for the if statement. */
522 expr = parse_expression (new_line);
523 make_cleanup (free_current_contents, &expr);
524
525 current = NULL;
526 ret = simple_control;
527
528 /* Evaluate the conditional. */
529 val_mark = value_mark ();
530 val = evaluate_expression (expr);
531
ebcd3b23
MS
532 /* Choose which arm to take commands from based on the value
533 of the conditional expression. */
d318976c
FN
534 if (value_true (val))
535 current = *cmd->body_list;
536 else if (cmd->body_count == 2)
537 current = *(cmd->body_list + 1);
538 value_free_to_mark (val_mark);
539
540 /* Execute commands in the given arm. */
541 while (current)
542 {
16026cd7 543 command_nest_depth++;
d318976c 544 ret = execute_control_command (current);
16026cd7 545 command_nest_depth--;
d318976c
FN
546
547 /* If we got an error, get out. */
548 if (ret != simple_control)
549 break;
550
551 /* Get the next statement in the body. */
552 current = current->next;
553 }
554
555 break;
556 }
40c03ae8
EZ
557 case commands_control:
558 {
ebcd3b23
MS
559 /* Breakpoint commands list, record the commands in the
560 breakpoint's command list and return. */
40c03ae8
EZ
561 new_line = insert_args (cmd->line);
562 if (!new_line)
563 break;
564 make_cleanup (free_current_contents, &new_line);
565 ret = commands_from_control_command (new_line, cmd);
566 break;
567 }
d57a3c85
TJB
568 case python_control:
569 {
570 eval_python_from_control_command (cmd);
571 ret = simple_control;
572 break;
573 }
d318976c
FN
574
575 default:
40c03ae8 576 warning (_("Invalid control type in canned commands structure."));
4d2acc65 577 break;
d318976c
FN
578 }
579
4d2acc65 580 do_cleanups (old_chain);
d318976c
FN
581
582 return ret;
583}
584
d57a3c85 585/* Like execute_control_command, but first set
ebcd3b23 586 suppress_next_print_command_trace. */
d57a3c85
TJB
587
588enum command_control_type
589execute_control_command_untraced (struct command_line *cmd)
590{
591 suppress_next_print_command_trace = 1;
592 return execute_control_command (cmd);
593}
594
595
d318976c
FN
596/* "while" command support. Executes a body of statements while the
597 loop condition is nonzero. */
598
599void
600while_command (char *arg, int from_tty)
601{
602 struct command_line *command = NULL;
b4a14fd0 603 struct cleanup *old_chain;
d318976c
FN
604
605 control_level = 1;
606 command = get_command_line (while_control, arg);
607
608 if (command == NULL)
609 return;
610
b4a14fd0
PA
611 old_chain = make_cleanup_restore_integer (&interpreter_async);
612 interpreter_async = 0;
613
d57a3c85 614 execute_control_command_untraced (command);
d318976c 615 free_command_lines (&command);
b4a14fd0
PA
616
617 do_cleanups (old_chain);
d318976c
FN
618}
619
620/* "if" command support. Execute either the true or false arm depending
621 on the value of the if conditional. */
622
623void
624if_command (char *arg, int from_tty)
625{
626 struct command_line *command = NULL;
b4a14fd0 627 struct cleanup *old_chain;
d318976c
FN
628
629 control_level = 1;
630 command = get_command_line (if_control, arg);
631
632 if (command == NULL)
633 return;
634
b4a14fd0
PA
635 old_chain = make_cleanup_restore_integer (&interpreter_async);
636 interpreter_async = 0;
637
d57a3c85 638 execute_control_command_untraced (command);
d318976c 639 free_command_lines (&command);
b4a14fd0
PA
640
641 do_cleanups (old_chain);
d318976c
FN
642}
643
644/* Cleanup */
645static void
646arg_cleanup (void *ignore)
647{
648 struct user_args *oargs = user_args;
cdb27c12 649
d318976c 650 if (!user_args)
8e65ff28 651 internal_error (__FILE__, __LINE__,
e2e0b3e5 652 _("arg_cleanup called with no user args.\n"));
d318976c
FN
653
654 user_args = user_args->next;
e28493f2 655 xfree (oargs->command);
b8c9b27d 656 xfree (oargs);
d318976c
FN
657}
658
659/* Bind the incomming arguments for a user defined command to
660 $arg0, $arg1 ... $argMAXUSERARGS. */
661
662static struct cleanup *
663setup_user_args (char *p)
664{
665 struct user_args *args;
666 struct cleanup *old_chain;
667 unsigned int arg_count = 0;
668
669 args = (struct user_args *) xmalloc (sizeof (struct user_args));
670 memset (args, 0, sizeof (struct user_args));
671
672 args->next = user_args;
673 user_args = args;
674
675 old_chain = make_cleanup (arg_cleanup, 0/*ignored*/);
676
677 if (p == NULL)
678 return old_chain;
679
e28493f2
AS
680 user_args->command = p = xstrdup (p);
681
d318976c
FN
682 while (*p)
683 {
684 char *start_arg;
685 int squote = 0;
686 int dquote = 0;
687 int bsquote = 0;
688
689 if (arg_count >= MAXUSERARGS)
690 {
8a3fe4f8 691 error (_("user defined function may only have %d arguments."),
d318976c
FN
692 MAXUSERARGS);
693 return old_chain;
694 }
695
696 /* Strip whitespace. */
697 while (*p == ' ' || *p == '\t')
698 p++;
699
700 /* P now points to an argument. */
701 start_arg = p;
702 user_args->a[arg_count].arg = p;
703
704 /* Get to the end of this argument. */
705 while (*p)
706 {
707 if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote)
708 break;
709 else
710 {
711 if (bsquote)
712 bsquote = 0;
713 else if (*p == '\\')
714 bsquote = 1;
715 else if (squote)
716 {
717 if (*p == '\'')
718 squote = 0;
719 }
720 else if (dquote)
721 {
722 if (*p == '"')
723 dquote = 0;
724 }
725 else
726 {
727 if (*p == '\'')
728 squote = 1;
729 else if (*p == '"')
730 dquote = 1;
731 }
732 p++;
733 }
734 }
735
736 user_args->a[arg_count].len = p - start_arg;
737 arg_count++;
738 user_args->count++;
739 }
740 return old_chain;
741}
742
ebcd3b23
MS
743/* Given character string P, return a point to the first argument
744 ($arg), or NULL if P contains no arguments. */
d318976c
FN
745
746static char *
747locate_arg (char *p)
748{
749 while ((p = strchr (p, '$')))
750 {
c03c782f
AS
751 if (strncmp (p, "$arg", 4) == 0
752 && (isdigit (p[4]) || p[4] == 'c'))
d318976c
FN
753 return p;
754 p++;
755 }
756 return NULL;
757}
758
759/* Insert the user defined arguments stored in user_arg into the $arg
ebcd3b23
MS
760 arguments found in line, with the updated copy being placed into
761 nline. */
d318976c
FN
762
763static char *
764insert_args (char *line)
765{
766 char *p, *save_line, *new_line;
767 unsigned len, i;
768
61d9b92f
DJ
769 /* If we are not in a user-defined function, treat $argc, $arg0, et
770 cetera as normal convenience variables. */
771 if (user_args == NULL)
772 return xstrdup (line);
773
ebcd3b23
MS
774 /* First we need to know how much memory to allocate for the new
775 line. */
d318976c
FN
776 save_line = line;
777 len = 0;
778 while ((p = locate_arg (line)))
779 {
780 len += p - line;
781 i = p[4] - '0';
782
c03c782f
AS
783 if (p[4] == 'c')
784 {
785 /* $argc. Number will be <=10. */
786 len += user_args->count == 10 ? 2 : 1;
787 }
788 else if (i >= user_args->count)
d318976c 789 {
8a3fe4f8 790 error (_("Missing argument %d in user function."), i);
d318976c
FN
791 return NULL;
792 }
c03c782f
AS
793 else
794 {
795 len += user_args->a[i].len;
796 }
d318976c
FN
797 line = p + 5;
798 }
799
800 /* Don't forget the tail. */
801 len += strlen (line);
802
803 /* Allocate space for the new line and fill it in. */
804 new_line = (char *) xmalloc (len + 1);
805 if (new_line == NULL)
806 return NULL;
807
808 /* Restore pointer to beginning of old line. */
809 line = save_line;
810
811 /* Save pointer to beginning of new line. */
812 save_line = new_line;
813
814 while ((p = locate_arg (line)))
815 {
816 int i, len;
817
818 memcpy (new_line, line, p - line);
819 new_line += p - line;
d318976c 820
c03c782f
AS
821 if (p[4] == 'c')
822 {
823 gdb_assert (user_args->count >= 0 && user_args->count <= 10);
824 if (user_args->count == 10)
825 {
826 *(new_line++) = '1';
827 *(new_line++) = '0';
828 }
829 else
830 *(new_line++) = user_args->count + '0';
831 }
832 else
d318976c 833 {
c03c782f
AS
834 i = p[4] - '0';
835 len = user_args->a[i].len;
836 if (len)
cdb27c12
MS
837 {
838 memcpy (new_line, user_args->a[i].arg, len);
839 new_line += len;
840 }
d318976c
FN
841 }
842 line = p + 5;
843 }
844 /* Don't forget the tail. */
845 strcpy (new_line, line);
846
847 /* Return a pointer to the beginning of the new line. */
848 return save_line;
849}
850
851\f
852/* Expand the body_list of COMMAND so that it can hold NEW_LENGTH
853 code bodies. This is typically used when we encounter an "else"
854 clause for an "if" command. */
855
856static void
857realloc_body_list (struct command_line *command, int new_length)
858{
859 int n;
860 struct command_line **body_list;
861
862 n = command->body_count;
863
864 /* Nothing to do? */
865 if (new_length <= n)
866 return;
867
868 body_list = (struct command_line **)
869 xmalloc (sizeof (struct command_line *) * new_length);
870
871 memcpy (body_list, command->body_list, sizeof (struct command_line *) * n);
42b575e5 872 memset (body_list + n, 0, sizeof (struct command_line *) * (new_length - n));
d318976c 873
b8c9b27d 874 xfree (command->body_list);
d318976c
FN
875 command->body_list = body_list;
876 command->body_count = new_length;
877}
878
3c1179ff
VP
879/* Read next line from stdout. Passed to read_command_line_1 and
880 recurse_read_control_structure whenever we need to read commands
881 from stdout. */
d318976c 882
3c1179ff 883static char *
a58d7472 884read_next_line (void)
d318976c 885{
3c1179ff 886 char *prompt_ptr, control_prompt[256];
d318976c
FN
887 int i = 0;
888
889 if (control_level >= 254)
8a3fe4f8 890 error (_("Control nesting too deep!"));
d318976c
FN
891
892 /* Set a prompt based on the nesting of the control commands. */
9a4105ab 893 if (instream == stdin || (instream == 0 && deprecated_readline_hook != NULL))
d318976c
FN
894 {
895 for (i = 0; i < control_level; i++)
896 control_prompt[i] = ' ';
897 control_prompt[i] = '>';
898 control_prompt[i + 1] = '\0';
899 prompt_ptr = (char *) &control_prompt[0];
900 }
901 else
902 prompt_ptr = NULL;
903
3c1179ff
VP
904 return command_line_input (prompt_ptr, instream == stdin, "commands");
905}
906
ebcd3b23
MS
907/* Process one input line. If the command is an "end", return such an
908 indication to the caller. If PARSE_COMMANDS is true, strip leading
909 whitespace (trailing whitespace is always stripped) in the line,
910 attempt to recognize GDB control commands, and also return an
911 indication if the command is an "else" or a nop.
912
3c1179ff
VP
913 Otherwise, only "end" is recognized. */
914
915static enum misc_command_type
a7bdde9e
VP
916process_next_line (char *p, struct command_line **command, int parse_commands,
917 void (*validator)(char *, void *), void *closure)
3c1179ff 918{
50cb2941
JK
919 char *p_end;
920 char *p_start;
3c1179ff 921 int not_handled = 0;
d318976c
FN
922
923 /* Not sure what to do here. */
924 if (p == NULL)
925 return end_command;
926
311a4e6b 927 /* Strip trailing whitespace. */
50cb2941
JK
928 p_end = p + strlen (p);
929 while (p_end > p && (p_end[-1] == ' ' || p_end[-1] == '\t'))
930 p_end--;
d318976c 931
50cb2941 932 p_start = p;
3630a92d 933 /* Strip leading whitespace. */
50cb2941
JK
934 while (p_start < p_end && (*p_start == ' ' || *p_start == '\t'))
935 p_start++;
d318976c 936
ebcd3b23 937 /* 'end' is always recognized, regardless of parse_commands value.
3630a92d 938 We also permit whitespace before end and after. */
50cb2941 939 if (p_end - p_start == 3 && !strncmp (p_start, "end", 3))
3630a92d
VP
940 return end_command;
941
311a4e6b 942 if (parse_commands)
d318976c 943 {
ebcd3b23 944 /* If commands are parsed, we skip initial spaces. Otherwise,
3630a92d
VP
945 which is the case for Python commands and documentation
946 (see the 'document' command), spaces are preserved. */
50cb2941 947 p = p_start;
3630a92d 948
311a4e6b 949 /* Blanks and comments don't really do anything, but we need to
ebcd3b23
MS
950 distinguish them from else, end and other commands which can
951 be executed. */
50cb2941 952 if (p_end == p || p[0] == '#')
311a4e6b
TJB
953 return nop_command;
954
955 /* Is the else clause of an if control structure? */
50cb2941 956 if (p_end - p == 4 && !strncmp (p, "else", 4))
311a4e6b
TJB
957 return else_command;
958
ebcd3b23
MS
959 /* Check for while, if, break, continue, etc and build a new
960 command line structure for them. */
a7bdde9e 961 if ((p_end - p >= 14 && !strncmp (p, "while-stepping", 14))
d350db38
PA
962 || (p_end - p >= 8 && !strncmp (p, "stepping", 8))
963 || (p_end - p >= 2 && !strncmp (p, "ws", 2)))
a7bdde9e
VP
964 {
965 /* Because validate_actionline and encode_action lookup
966 command's line as command, we need the line to
967 include 'while-stepping'.
968
969 For 'ws' alias, the command will have 'ws', not expanded
ebcd3b23 970 to 'while-stepping'. This is intentional -- we don't
a7bdde9e 971 really want frontend to send a command list with 'ws',
ebcd3b23
MS
972 and next break-info returning command line with
973 'while-stepping'. This should work, but might cause the
974 breakpoint to be marked as changed while it's actually
975 not. */
a7bdde9e
VP
976 *command = build_command_line (while_stepping_control, p);
977 }
978 else if (p_end - p > 5 && !strncmp (p, "while", 5))
311a4e6b
TJB
979 {
980 char *first_arg;
cdb27c12 981
311a4e6b 982 first_arg = p + 5;
50cb2941 983 while (first_arg < p_end && isspace (*first_arg))
311a4e6b
TJB
984 first_arg++;
985 *command = build_command_line (while_control, first_arg);
986 }
50cb2941 987 else if (p_end - p > 2 && !strncmp (p, "if", 2))
311a4e6b
TJB
988 {
989 char *first_arg;
cdb27c12 990
311a4e6b 991 first_arg = p + 2;
50cb2941 992 while (first_arg < p_end && isspace (*first_arg))
311a4e6b
TJB
993 first_arg++;
994 *command = build_command_line (if_control, first_arg);
995 }
50cb2941 996 else if (p_end - p >= 8 && !strncmp (p, "commands", 8))
311a4e6b
TJB
997 {
998 char *first_arg;
cdb27c12 999
311a4e6b 1000 first_arg = p + 8;
50cb2941 1001 while (first_arg < p_end && isspace (*first_arg))
311a4e6b
TJB
1002 first_arg++;
1003 *command = build_command_line (commands_control, first_arg);
1004 }
50cb2941 1005 else if (p_end - p == 6 && !strncmp (p, "python", 6))
311a4e6b
TJB
1006 {
1007 /* Note that we ignore the inline "python command" form
1008 here. */
1009 *command = build_command_line (python_control, "");
1010 }
50cb2941 1011 else if (p_end - p == 10 && !strncmp (p, "loop_break", 10))
311a4e6b
TJB
1012 {
1013 *command = (struct command_line *)
1014 xmalloc (sizeof (struct command_line));
1015 (*command)->next = NULL;
1016 (*command)->line = NULL;
1017 (*command)->control_type = break_control;
1018 (*command)->body_count = 0;
1019 (*command)->body_list = NULL;
1020 }
50cb2941 1021 else if (p_end - p == 13 && !strncmp (p, "loop_continue", 13))
311a4e6b
TJB
1022 {
1023 *command = (struct command_line *)
1024 xmalloc (sizeof (struct command_line));
1025 (*command)->next = NULL;
1026 (*command)->line = NULL;
1027 (*command)->control_type = continue_control;
1028 (*command)->body_count = 0;
1029 (*command)->body_list = NULL;
1030 }
1031 else
1032 not_handled = 1;
d318976c 1033 }
311a4e6b
TJB
1034
1035 if (!parse_commands || not_handled)
d318976c
FN
1036 {
1037 /* A normal command. */
1038 *command = (struct command_line *)
1039 xmalloc (sizeof (struct command_line));
1040 (*command)->next = NULL;
50cb2941 1041 (*command)->line = savestring (p, p_end - p);
d318976c
FN
1042 (*command)->control_type = simple_control;
1043 (*command)->body_count = 0;
1044 (*command)->body_list = NULL;
1045 }
1046
a7bdde9e
VP
1047 if (validator)
1048 {
1049 volatile struct gdb_exception ex;
cdb27c12 1050
a7bdde9e
VP
1051 TRY_CATCH (ex, RETURN_MASK_ALL)
1052 {
1053 validator ((*command)->line, closure);
1054 }
1055 if (ex.reason < 0)
1056 {
1057 xfree (*command);
1058 throw_exception (ex);
1059 }
1060 }
1061
d318976c
FN
1062 /* Nothing special. */
1063 return ok_command;
1064}
1065
ebcd3b23
MS
1066/* Recursively read in the control structures and create a
1067 command_line structure from them. Use read_next_line_func to
1068 obtain lines of the command. */
d318976c
FN
1069
1070static enum command_control_type
a58d7472 1071recurse_read_control_structure (char * (*read_next_line_func) (void),
a7bdde9e
VP
1072 struct command_line *current_cmd,
1073 void (*validator)(char *, void *),
1074 void *closure)
d318976c
FN
1075{
1076 int current_body, i;
1077 enum misc_command_type val;
1078 enum command_control_type ret;
1079 struct command_line **body_ptr, *child_tail, *next;
1080
1081 child_tail = NULL;
1082 current_body = 1;
1083
1084 /* Sanity checks. */
1085 if (current_cmd->control_type == simple_control)
8a3fe4f8 1086 error (_("Recursed on a simple control type."));
d318976c
FN
1087
1088 if (current_body > current_cmd->body_count)
8a3fe4f8 1089 error (_("Allocated body is smaller than this command type needs."));
d318976c
FN
1090
1091 /* Read lines from the input stream and build control structures. */
1092 while (1)
1093 {
1094 dont_repeat ();
1095
1096 next = NULL;
3c1179ff 1097 val = process_next_line (read_next_line_func (), &next,
a7bdde9e
VP
1098 current_cmd->control_type != python_control,
1099 validator, closure);
d318976c
FN
1100
1101 /* Just skip blanks and comments. */
1102 if (val == nop_command)
1103 continue;
1104
1105 if (val == end_command)
1106 {
1107 if (current_cmd->control_type == while_control
a7bdde9e 1108 || current_cmd->control_type == while_stepping_control
40c03ae8 1109 || current_cmd->control_type == if_control
d57a3c85 1110 || current_cmd->control_type == python_control
40c03ae8 1111 || current_cmd->control_type == commands_control)
d318976c 1112 {
40c03ae8 1113 /* Success reading an entire canned sequence of commands. */
d318976c
FN
1114 ret = simple_control;
1115 break;
1116 }
1117 else
1118 {
1119 ret = invalid_control;
1120 break;
1121 }
1122 }
1123
1124 /* Not the end of a control structure. */
1125 if (val == else_command)
1126 {
1127 if (current_cmd->control_type == if_control
1128 && current_body == 1)
1129 {
1130 realloc_body_list (current_cmd, 2);
1131 current_body = 2;
1132 child_tail = NULL;
1133 continue;
1134 }
1135 else
1136 {
1137 ret = invalid_control;
1138 break;
1139 }
1140 }
1141
1142 if (child_tail)
1143 {
1144 child_tail->next = next;
1145 }
1146 else
1147 {
1148 body_ptr = current_cmd->body_list;
1149 for (i = 1; i < current_body; i++)
1150 body_ptr++;
1151
1152 *body_ptr = next;
1153
1154 }
1155
1156 child_tail = next;
1157
1158 /* If the latest line is another control structure, then recurse
1159 on it. */
1160 if (next->control_type == while_control
a7bdde9e 1161 || next->control_type == while_stepping_control
40c03ae8 1162 || next->control_type == if_control
d57a3c85 1163 || next->control_type == python_control
40c03ae8 1164 || next->control_type == commands_control)
d318976c
FN
1165 {
1166 control_level++;
a7bdde9e
VP
1167 ret = recurse_read_control_structure (read_next_line_func, next,
1168 validator, closure);
d318976c
FN
1169 control_level--;
1170
1171 if (ret != simple_control)
1172 break;
1173 }
1174 }
1175
1176 dont_repeat ();
1177
1178 return ret;
1179}
1180
1181/* Read lines from the input stream and accumulate them in a chain of
1182 struct command_line's, which is then returned. For input from a
1183 terminal, the special command "end" is used to mark the end of the
311a4e6b
TJB
1184 input, and is not included in the returned chain of commands.
1185
1186 If PARSE_COMMANDS is true, strip leading whitespace (trailing whitespace
1187 is always stripped) in the line and attempt to recognize GDB control
1188 commands. Otherwise, only "end" is recognized. */
d318976c
FN
1189
1190#define END_MESSAGE "End with a line saying just \"end\"."
1191
1192struct command_line *
a7bdde9e
VP
1193read_command_lines (char *prompt_arg, int from_tty, int parse_commands,
1194 void (*validator)(char *, void *), void *closure)
d318976c 1195{
3c1179ff 1196 struct command_line *head;
698ba934
DJ
1197
1198 if (from_tty && input_from_terminal_p ())
d318976c 1199 {
698ba934
DJ
1200 if (deprecated_readline_begin_hook)
1201 {
ebcd3b23 1202 /* Note - intentional to merge messages with no newline. */
9a2b4c1b
MS
1203 (*deprecated_readline_begin_hook) ("%s %s\n", prompt_arg,
1204 END_MESSAGE);
698ba934
DJ
1205 }
1206 else
1207 {
1208 printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE);
1209 gdb_flush (gdb_stdout);
1210 }
d318976c
FN
1211 }
1212
a7bdde9e
VP
1213 head = read_command_lines_1 (read_next_line, parse_commands,
1214 validator, closure);
3c1179ff
VP
1215
1216 if (deprecated_readline_end_hook && from_tty && input_from_terminal_p ())
1217 {
1218 (*deprecated_readline_end_hook) ();
1219 }
1220 return (head);
1221}
1222
1223/* Act the same way as read_command_lines, except that each new line is
1224 obtained using READ_NEXT_LINE_FUNC. */
1225
1226struct command_line *
a7bdde9e
VP
1227read_command_lines_1 (char * (*read_next_line_func) (void), int parse_commands,
1228 void (*validator)(char *, void *), void *closure)
3c1179ff
VP
1229{
1230 struct command_line *head, *tail, *next;
1231 struct cleanup *old_chain;
1232 enum command_control_type ret;
1233 enum misc_command_type val;
1234
1235 control_level = 0;
d318976c
FN
1236 head = tail = NULL;
1237 old_chain = NULL;
1238
1239 while (1)
1240 {
f429d7d0 1241 dont_repeat ();
a7bdde9e
VP
1242 val = process_next_line (read_next_line_func (), &next, parse_commands,
1243 validator, closure);
d318976c
FN
1244
1245 /* Ignore blank lines or comments. */
1246 if (val == nop_command)
1247 continue;
1248
1249 if (val == end_command)
1250 {
1251 ret = simple_control;
1252 break;
1253 }
1254
1255 if (val != ok_command)
1256 {
1257 ret = invalid_control;
1258 break;
1259 }
1260
1261 if (next->control_type == while_control
40c03ae8 1262 || next->control_type == if_control
d57a3c85 1263 || next->control_type == python_control
a7bdde9e
VP
1264 || next->control_type == commands_control
1265 || next->control_type == while_stepping_control)
d318976c
FN
1266 {
1267 control_level++;
a7bdde9e
VP
1268 ret = recurse_read_control_structure (read_next_line_func, next,
1269 validator, closure);
d318976c
FN
1270 control_level--;
1271
1272 if (ret == invalid_control)
1273 break;
1274 }
1275
1276 if (tail)
1277 {
1278 tail->next = next;
1279 }
1280 else
1281 {
1282 head = next;
1283 old_chain = make_cleanup_free_command_lines (&head);
1284 }
1285 tail = next;
1286 }
1287
1288 dont_repeat ();
1289
1290 if (head)
1291 {
1292 if (ret != invalid_control)
1293 {
1294 discard_cleanups (old_chain);
1295 }
1296 else
1297 do_cleanups (old_chain);
1298 }
1299
3c1179ff 1300 return head;
d318976c
FN
1301}
1302
1303/* Free a chain of struct command_line's. */
1304
1305void
1306free_command_lines (struct command_line **lptr)
1307{
d5b5ac79
AC
1308 struct command_line *l = *lptr;
1309 struct command_line *next;
d318976c
FN
1310 struct command_line **blist;
1311 int i;
1312
1313 while (l)
1314 {
1315 if (l->body_count > 0)
1316 {
1317 blist = l->body_list;
1318 for (i = 0; i < l->body_count; i++, blist++)
1319 free_command_lines (blist);
1320 }
1321 next = l->next;
b8c9b27d
KB
1322 xfree (l->line);
1323 xfree (l);
d318976c
FN
1324 l = next;
1325 }
0d70f41b 1326 *lptr = NULL;
d318976c
FN
1327}
1328
1329static void
1330do_free_command_lines_cleanup (void *arg)
1331{
1332 free_command_lines (arg);
1333}
1334
6c50ab1c 1335struct cleanup *
d318976c
FN
1336make_cleanup_free_command_lines (struct command_line **arg)
1337{
1338 return make_cleanup (do_free_command_lines_cleanup, arg);
1339}
c2b8ed2c
MS
1340
1341struct command_line *
1342copy_command_lines (struct command_line *cmds)
1343{
1344 struct command_line *result = NULL;
1345
1346 if (cmds)
1347 {
1348 result = (struct command_line *) xmalloc (sizeof (struct command_line));
1349
1350 result->next = copy_command_lines (cmds->next);
1351 result->line = xstrdup (cmds->line);
1352 result->control_type = cmds->control_type;
1353 result->body_count = cmds->body_count;
1354 if (cmds->body_count > 0)
1355 {
1356 int i;
1357
1358 result->body_list = (struct command_line **)
1359 xmalloc (sizeof (struct command_line *) * cmds->body_count);
1360
1361 for (i = 0; i < cmds->body_count; i++)
1362 result->body_list[i] = copy_command_lines (cmds->body_list[i]);
1363 }
1364 else
1365 result->body_list = NULL;
1366 }
1367
1368 return result;
1369}
d318976c 1370\f
adb483fe
DJ
1371/* Validate that *COMNAME is a valid name for a command. Return the
1372 containing command list, in case it starts with a prefix command.
1373 The prefix must already exist. *COMNAME is advanced to point after
1374 any prefix, and a NUL character overwrites the space after the
1375 prefix. */
1376
1377static struct cmd_list_element **
1378validate_comname (char **comname)
d318976c 1379{
adb483fe
DJ
1380 struct cmd_list_element **list = &cmdlist;
1381 char *p, *last_word;
d318976c 1382
adb483fe 1383 if (*comname == 0)
e2e0b3e5 1384 error_no_arg (_("name of command to define"));
d318976c 1385
adb483fe
DJ
1386 /* Find the last word of the argument. */
1387 p = *comname + strlen (*comname);
1388 while (p > *comname && isspace (p[-1]))
1389 p--;
1390 while (p > *comname && !isspace (p[-1]))
1391 p--;
1392 last_word = p;
1393
1394 /* Find the corresponding command list. */
1395 if (last_word != *comname)
1396 {
1397 struct cmd_list_element *c;
1398 char saved_char, *tem = *comname;
1399
1400 /* Separate the prefix and the command. */
1401 saved_char = last_word[-1];
1402 last_word[-1] = '\0';
1403
1404 c = lookup_cmd (&tem, cmdlist, "", 0, 1);
1405 if (c->prefixlist == NULL)
1406 error (_("\"%s\" is not a prefix command."), *comname);
1407
1408 list = c->prefixlist;
1409 last_word[-1] = saved_char;
1410 *comname = last_word;
1411 }
1412
1413 p = *comname;
d318976c
FN
1414 while (*p)
1415 {
1416 if (!isalnum (*p) && *p != '-' && *p != '_')
8a3fe4f8 1417 error (_("Junk in argument list: \"%s\""), p);
d318976c
FN
1418 p++;
1419 }
adb483fe
DJ
1420
1421 return list;
d318976c
FN
1422}
1423
1424/* This is just a placeholder in the command data structures. */
1425static void
1426user_defined_command (char *ignore, int from_tty)
1427{
1428}
1429
1430void
1431define_command (char *comname, int from_tty)
1432{
1433#define MAX_TMPBUF 128
1434 enum cmd_hook_type
1435 {
1436 CMD_NO_HOOK = 0,
1437 CMD_PRE_HOOK,
1438 CMD_POST_HOOK
1439 };
d5b5ac79 1440 struct command_line *cmds;
d36fc00b
MS
1441 struct cmd_list_element *c, *newc, *hookc = 0, **list;
1442 char *tem, *comfull;
d318976c
FN
1443 char tmpbuf[MAX_TMPBUF];
1444 int hook_type = CMD_NO_HOOK;
1445 int hook_name_size = 0;
1446
1447#define HOOK_STRING "hook-"
1448#define HOOK_LEN 5
1449#define HOOK_POST_STRING "hookpost-"
1450#define HOOK_POST_LEN 9
1451
adb483fe
DJ
1452 comfull = comname;
1453 list = validate_comname (&comname);
d318976c
FN
1454
1455 /* Look it up, and verify that we got an exact match. */
adb483fe
DJ
1456 tem = comname;
1457 c = lookup_cmd (&tem, *list, "", -1, 1);
5cb316ef 1458 if (c && strcmp (comname, c->name) != 0)
d318976c
FN
1459 c = 0;
1460
1461 if (c)
1462 {
ab4e3d93 1463 int q;
cdb27c12 1464
d318976c 1465 if (c->class == class_user || c->class == class_alias)
e2e0b3e5 1466 q = query (_("Redefine command \"%s\"? "), c->name);
d318976c 1467 else
e2e0b3e5 1468 q = query (_("Really redefine built-in command \"%s\"? "), c->name);
ab4e3d93 1469 if (!q)
8a3fe4f8 1470 error (_("Command \"%s\" not redefined."), c->name);
d318976c
FN
1471 }
1472
1473 /* If this new command is a hook, then mark the command which it
1474 is hooking. Note that we allow hooking `help' commands, so that
1475 we can hook the `stop' pseudo-command. */
1476
1477 if (!strncmp (comname, HOOK_STRING, HOOK_LEN))
1478 {
1479 hook_type = CMD_PRE_HOOK;
1480 hook_name_size = HOOK_LEN;
1481 }
1482 else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN))
1483 {
1484 hook_type = CMD_POST_HOOK;
1485 hook_name_size = HOOK_POST_LEN;
1486 }
1487
1488 if (hook_type != CMD_NO_HOOK)
1489 {
1490 /* Look up cmd it hooks, and verify that we got an exact match. */
1491 tem = comname + hook_name_size;
adb483fe 1492 hookc = lookup_cmd (&tem, *list, "", -1, 0);
5cb316ef 1493 if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0)
d318976c
FN
1494 hookc = 0;
1495 if (!hookc)
1496 {
9a2b4c1b
MS
1497 warning (_("Your new `%s' command does not "
1498 "hook any existing command."),
adb483fe 1499 comfull);
9e2f0ad4 1500 if (!query (_("Proceed? ")))
8a3fe4f8 1501 error (_("Not confirmed."));
d318976c
FN
1502 }
1503 }
1504
1b36a34b 1505 comname = xstrdup (comname);
d318976c
FN
1506
1507 /* If the rest of the commands will be case insensitive, this one
ebcd3b23 1508 should behave in the same manner. */
d318976c
FN
1509 for (tem = comname; *tem; tem++)
1510 if (isupper (*tem))
1511 *tem = tolower (*tem);
1512
adb483fe 1513 sprintf (tmpbuf, "Type commands for definition of \"%s\".", comfull);
a7bdde9e 1514 cmds = read_command_lines (tmpbuf, from_tty, 1, 0, 0);
d318976c
FN
1515
1516 if (c && c->class == class_user)
1517 free_command_lines (&c->user_commands);
1518
1519 newc = add_cmd (comname, class_user, user_defined_command,
1520 (c && c->class == class_user)
1b36a34b 1521 ? c->doc : xstrdup ("User-defined."), list);
d318976c
FN
1522 newc->user_commands = cmds;
1523
1524 /* If this new command is a hook, then mark both commands as being
1525 tied. */
1526 if (hookc)
1527 {
1528 switch (hook_type)
1529 {
1530 case CMD_PRE_HOOK:
1531 hookc->hook_pre = newc; /* Target gets hooked. */
ebcd3b23 1532 newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */
d318976c
FN
1533 break;
1534 case CMD_POST_HOOK:
f48ff60a 1535 hookc->hook_post = newc; /* Target gets hooked. */
9a2b4c1b
MS
1536 newc->hookee_post = hookc; /* We are marked as hooking
1537 target cmd. */
d318976c
FN
1538 break;
1539 default:
ebcd3b23 1540 /* Should never come here as hookc would be 0. */
e2e0b3e5 1541 internal_error (__FILE__, __LINE__, _("bad switch"));
d318976c
FN
1542 }
1543 }
1544}
1545
1546void
1547document_command (char *comname, int from_tty)
1548{
1549 struct command_line *doclines;
adb483fe
DJ
1550 struct cmd_list_element *c, **list;
1551 char *tem, *comfull;
d318976c
FN
1552 char tmpbuf[128];
1553
adb483fe
DJ
1554 comfull = comname;
1555 list = validate_comname (&comname);
d318976c 1556
adb483fe
DJ
1557 tem = comname;
1558 c = lookup_cmd (&tem, *list, "", 0, 1);
d318976c
FN
1559
1560 if (c->class != class_user)
adb483fe 1561 error (_("Command \"%s\" is built-in."), comfull);
d318976c 1562
adb483fe 1563 sprintf (tmpbuf, "Type documentation for \"%s\".", comfull);
a7bdde9e 1564 doclines = read_command_lines (tmpbuf, from_tty, 0, 0, 0);
d318976c
FN
1565
1566 if (c->doc)
b8c9b27d 1567 xfree (c->doc);
d318976c
FN
1568
1569 {
d5b5ac79
AC
1570 struct command_line *cl1;
1571 int len = 0;
d318976c
FN
1572
1573 for (cl1 = doclines; cl1; cl1 = cl1->next)
1574 len += strlen (cl1->line) + 1;
1575
1576 c->doc = (char *) xmalloc (len + 1);
1577 *c->doc = 0;
1578
1579 for (cl1 = doclines; cl1; cl1 = cl1->next)
1580 {
1581 strcat (c->doc, cl1->line);
1582 if (cl1->next)
1583 strcat (c->doc, "\n");
1584 }
1585 }
1586
1587 free_command_lines (&doclines);
1588}
1589\f
1590struct source_cleanup_lines_args
1591{
1592 int old_line;
05159abe 1593 const char *old_file;
d318976c
FN
1594};
1595
1596static void
4efb68b1 1597source_cleanup_lines (void *args)
d318976c
FN
1598{
1599 struct source_cleanup_lines_args *p =
cdb27c12
MS
1600 (struct source_cleanup_lines_args *) args;
1601
d318976c
FN
1602 source_line_number = p->old_line;
1603 source_file_name = p->old_file;
d318976c
FN
1604}
1605
ebcd3b23 1606/* Used to implement source_command. */
d318976c
FN
1607
1608void
05159abe 1609script_from_file (FILE *stream, const char *file)
d318976c
FN
1610{
1611 struct cleanup *old_cleanups;
1612 struct source_cleanup_lines_args old_lines;
d318976c
FN
1613
1614 if (stream == NULL)
e2e0b3e5 1615 internal_error (__FILE__, __LINE__, _("called with NULL file pointer!"));
d318976c 1616
7c8a8b04 1617 old_cleanups = make_cleanup_fclose (stream);
d318976c
FN
1618
1619 old_lines.old_line = source_line_number;
1620 old_lines.old_file = source_file_name;
d318976c
FN
1621 make_cleanup (source_cleanup_lines, &old_lines);
1622 source_line_number = 0;
1623 source_file_name = file;
ebcd3b23
MS
1624 /* This will get set every time we read a line. So it won't stay ""
1625 for long. */
d318976c
FN
1626 error_pre_print = "";
1627
17d92a02 1628 {
04bd08de 1629 volatile struct gdb_exception e;
cdb27c12 1630
04bd08de
TT
1631 TRY_CATCH (e, RETURN_MASK_ERROR)
1632 {
1633 read_command_file (stream);
1634 }
17d92a02
AC
1635 switch (e.reason)
1636 {
1637 case 0:
1638 break;
1639 case RETURN_ERROR:
1640 /* Re-throw the error, but with the file name information
1641 prepended. */
109c3e39
AC
1642 throw_error (e.error,
1643 _("%s:%d: Error in sourced command file:\n%s"),
637537d0 1644 source_file_name, source_line_number, e.message);
17d92a02 1645 default:
e2e0b3e5 1646 internal_error (__FILE__, __LINE__, _("bad reason"));
17d92a02
AC
1647 }
1648 }
d318976c
FN
1649
1650 do_cleanups (old_cleanups);
1651}
1652
adb483fe
DJ
1653/* Print the definition of user command C to STREAM. Or, if C is a
1654 prefix command, show the definitions of all user commands under C
1655 (recursively). PREFIX and NAME combined are the name of the
1656 current command. */
d318976c 1657void
adb483fe
DJ
1658show_user_1 (struct cmd_list_element *c, char *prefix, char *name,
1659 struct ui_file *stream)
d318976c 1660{
d5b5ac79 1661 struct command_line *cmdlines;
d318976c 1662
adb483fe
DJ
1663 if (c->prefixlist != NULL)
1664 {
1665 char *prefixname = c->prefixname;
cdb27c12 1666
adb483fe
DJ
1667 for (c = *c->prefixlist; c != NULL; c = c->next)
1668 if (c->class == class_user || c->prefixlist != NULL)
1669 show_user_1 (c, prefixname, c->name, gdb_stdout);
1670 return;
1671 }
1672
d318976c
FN
1673 cmdlines = c->user_commands;
1674 if (!cmdlines)
1675 return;
adb483fe 1676 fprintf_filtered (stream, "User command \"%s%s\":\n", prefix, name);
d318976c 1677
79a45e25 1678 print_command_lines (current_uiout, cmdlines, 1);
d318976c 1679 fputs_filtered ("\n", stream);
d318976c
FN
1680}
1681
This page took 0.759482 seconds and 4 git commands to generate.