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