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