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