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