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