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