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