Commit | Line | Data |
---|---|---|
d318976c | 1 | /* GDB CLI command scripting. |
8926118c AC |
2 | |
3 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, | |
4 | 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002 Free Software | |
5 | Foundation, Inc. | |
d318976c FN |
6 | |
7 | This file is part of GDB. | |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
11 | the Free Software Foundation; either version 2 of the License, or | |
12 | (at your option) any later version. | |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
20 | along with this program; if not, write to the Free Software | |
21 | Foundation, Inc., 59 Temple Place - Suite 330, | |
22 | Boston, MA 02111-1307, USA. */ | |
23 | ||
24 | #include "defs.h" | |
25 | #include "value.h" | |
26 | #include "language.h" /* For value_true */ | |
27 | #include <ctype.h> | |
28 | ||
d318976c | 29 | #include "ui-out.h" |
5f8a3188 | 30 | #include "gdb_string.h" |
d318976c FN |
31 | |
32 | #include "top.h" | |
33 | #include "cli/cli-cmds.h" | |
34 | #include "cli/cli-decode.h" | |
35 | #include "cli/cli-script.h" | |
36 | ||
d318976c FN |
37 | /* Prototypes for local functions */ |
38 | ||
39 | static struct cleanup * | |
40 | make_cleanup_free_command_lines (struct command_line **arg); | |
41 | ||
42 | static enum command_control_type | |
43 | recurse_read_control_structure (struct command_line *current_cmd); | |
44 | ||
45 | static char *insert_args (char *line); | |
46 | ||
47 | static struct cleanup * setup_user_args (char *p); | |
48 | ||
49 | static void validate_comname (char *); | |
50 | ||
51 | /* Level of control structure. */ | |
52 | static int control_level; | |
53 | ||
54 | /* Source command state variable. */ | |
55 | static int source_error_allocated; | |
56 | ||
57 | /* Structure for arguments to user defined functions. */ | |
58 | #define MAXUSERARGS 10 | |
59 | struct user_args | |
60 | { | |
61 | struct user_args *next; | |
62 | struct | |
63 | { | |
64 | char *arg; | |
65 | int len; | |
66 | } | |
67 | a[MAXUSERARGS]; | |
68 | int count; | |
69 | } | |
70 | *user_args; | |
71 | ||
72 | \f | |
73 | /* Allocate, initialize a new command line structure for one of the | |
74 | control commands (if/while). */ | |
75 | ||
76 | static struct command_line * | |
77 | build_command_line (enum command_control_type type, char *args) | |
78 | { | |
79 | struct command_line *cmd; | |
80 | ||
81 | if (args == NULL) | |
82 | error ("if/while commands require arguments.\n"); | |
83 | ||
84 | cmd = (struct command_line *) xmalloc (sizeof (struct command_line)); | |
85 | cmd->next = NULL; | |
86 | cmd->control_type = type; | |
87 | ||
88 | cmd->body_count = 1; | |
89 | cmd->body_list | |
90 | = (struct command_line **) xmalloc (sizeof (struct command_line *) | |
91 | * cmd->body_count); | |
92 | memset (cmd->body_list, 0, sizeof (struct command_line *) * cmd->body_count); | |
93 | cmd->line = savestring (args, strlen (args)); | |
94 | return cmd; | |
95 | } | |
96 | ||
97 | /* Build and return a new command structure for the control commands | |
98 | such as "if" and "while". */ | |
99 | ||
100 | static struct command_line * | |
101 | get_command_line (enum command_control_type type, char *arg) | |
102 | { | |
103 | struct command_line *cmd; | |
104 | struct cleanup *old_chain = NULL; | |
105 | ||
106 | /* Allocate and build a new command line structure. */ | |
107 | cmd = build_command_line (type, arg); | |
108 | ||
109 | old_chain = make_cleanup_free_command_lines (&cmd); | |
110 | ||
111 | /* Read in the body of this command. */ | |
112 | if (recurse_read_control_structure (cmd) == invalid_control) | |
113 | { | |
114 | warning ("error reading in control structure\n"); | |
115 | do_cleanups (old_chain); | |
116 | return NULL; | |
117 | } | |
118 | ||
119 | discard_cleanups (old_chain); | |
120 | return cmd; | |
121 | } | |
122 | ||
123 | /* Recursively print a command (including full control structures). */ | |
8926118c | 124 | |
d318976c FN |
125 | void |
126 | print_command_lines (struct ui_out *uiout, struct command_line *cmd, | |
127 | unsigned int depth) | |
128 | { | |
129 | struct command_line *list; | |
130 | ||
131 | list = cmd; | |
132 | while (list) | |
133 | { | |
134 | ||
135 | if (depth) | |
136 | ui_out_spaces (uiout, 2 * depth); | |
137 | ||
138 | /* A simple command, print it and continue. */ | |
139 | if (list->control_type == simple_control) | |
140 | { | |
141 | ui_out_field_string (uiout, NULL, list->line); | |
142 | ui_out_text (uiout, "\n"); | |
143 | list = list->next; | |
144 | continue; | |
145 | } | |
146 | ||
147 | /* loop_continue to jump to the start of a while loop, print it | |
148 | and continue. */ | |
149 | if (list->control_type == continue_control) | |
150 | { | |
151 | ui_out_field_string (uiout, NULL, "loop_continue"); | |
152 | ui_out_text (uiout, "\n"); | |
153 | list = list->next; | |
154 | continue; | |
155 | } | |
156 | ||
157 | /* loop_break to break out of a while loop, print it and continue. */ | |
158 | if (list->control_type == break_control) | |
159 | { | |
160 | ui_out_field_string (uiout, NULL, "loop_break"); | |
161 | ui_out_text (uiout, "\n"); | |
162 | list = list->next; | |
163 | continue; | |
164 | } | |
165 | ||
166 | /* A while command. Recursively print its subcommands and continue. */ | |
167 | if (list->control_type == while_control) | |
168 | { | |
d318976c FN |
169 | ui_out_field_fmt (uiout, NULL, "while %s", list->line); |
170 | ui_out_text (uiout, "\n"); | |
171 | print_command_lines (uiout, *list->body_list, depth + 1); | |
d318976c FN |
172 | if (depth) |
173 | ui_out_spaces (uiout, 2 * depth); | |
e6ccd35f JSC |
174 | ui_out_field_string (uiout, NULL, "end"); |
175 | ui_out_text (uiout, "\n"); | |
d318976c FN |
176 | list = list->next; |
177 | continue; | |
178 | } | |
179 | ||
180 | /* An if command. Recursively print both arms before continueing. */ | |
181 | if (list->control_type == if_control) | |
182 | { | |
d318976c FN |
183 | ui_out_field_fmt (uiout, NULL, "if %s", list->line); |
184 | ui_out_text (uiout, "\n"); | |
185 | /* The true arm. */ | |
186 | print_command_lines (uiout, list->body_list[0], depth + 1); | |
187 | ||
188 | /* Show the false arm if it exists. */ | |
189 | if (list->body_count == 2) | |
190 | { | |
191 | if (depth) | |
192 | ui_out_spaces (uiout, 2 * depth); | |
193 | ui_out_field_string (uiout, NULL, "else"); | |
e6ccd35f | 194 | ui_out_text (uiout, "\n"); |
d318976c FN |
195 | print_command_lines (uiout, list->body_list[1], depth + 1); |
196 | } | |
197 | ||
d318976c FN |
198 | if (depth) |
199 | ui_out_spaces (uiout, 2 * depth); | |
e6ccd35f JSC |
200 | ui_out_field_string (uiout, NULL, "end"); |
201 | ui_out_text (uiout, "\n"); | |
d318976c FN |
202 | list = list->next; |
203 | continue; | |
204 | } | |
205 | ||
206 | /* ignore illegal command type and try next */ | |
207 | list = list->next; | |
208 | } /* while (list) */ | |
209 | } | |
d318976c | 210 | |
5913bcb0 AC |
211 | /* Handle pre-post hooks. */ |
212 | ||
b9362cc7 | 213 | static void |
5913bcb0 AC |
214 | clear_hook_in_cleanup (void *data) |
215 | { | |
216 | struct cmd_list_element *c = data; | |
217 | c->hook_in = 0; /* Allow hook to work again once it is complete */ | |
218 | } | |
219 | ||
220 | void | |
221 | execute_cmd_pre_hook (struct cmd_list_element *c) | |
222 | { | |
223 | if ((c->hook_pre) && (!c->hook_in)) | |
224 | { | |
225 | struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c); | |
226 | c->hook_in = 1; /* Prevent recursive hooking */ | |
227 | execute_user_command (c->hook_pre, (char *) 0); | |
228 | do_cleanups (cleanups); | |
229 | } | |
230 | } | |
231 | ||
232 | void | |
233 | execute_cmd_post_hook (struct cmd_list_element *c) | |
234 | { | |
235 | if ((c->hook_post) && (!c->hook_in)) | |
236 | { | |
237 | struct cleanup *cleanups = make_cleanup (clear_hook_in_cleanup, c); | |
238 | c->hook_in = 1; /* Prevent recursive hooking */ | |
239 | execute_user_command (c->hook_post, (char *) 0); | |
240 | do_cleanups (cleanups); | |
241 | } | |
242 | } | |
243 | ||
d318976c | 244 | /* Execute the command in CMD. */ |
b9362cc7 | 245 | static void |
20f01a46 DH |
246 | do_restore_user_call_depth (void * call_depth) |
247 | { | |
248 | int * depth = call_depth; | |
249 | /* We will be returning_to_top_level() at this point, so we want to | |
250 | reset our depth. */ | |
251 | (*depth) = 0; | |
252 | } | |
253 | ||
d318976c FN |
254 | |
255 | void | |
256 | execute_user_command (struct cmd_list_element *c, char *args) | |
257 | { | |
d5b5ac79 | 258 | struct command_line *cmdlines; |
d318976c FN |
259 | struct cleanup *old_chain; |
260 | enum command_control_type ret; | |
20f01a46 DH |
261 | static int user_call_depth = 0; |
262 | extern int max_user_call_depth; | |
d318976c FN |
263 | |
264 | old_chain = setup_user_args (args); | |
265 | ||
266 | cmdlines = c->user_commands; | |
267 | if (cmdlines == 0) | |
268 | /* Null command */ | |
269 | return; | |
270 | ||
20f01a46 DH |
271 | if (++user_call_depth > max_user_call_depth) |
272 | error ("Max user call depth exceeded -- command aborted\n"); | |
273 | ||
274 | old_chain = make_cleanup (do_restore_user_call_depth, &user_call_depth); | |
275 | ||
d318976c FN |
276 | /* Set the instream to 0, indicating execution of a |
277 | user-defined function. */ | |
278 | old_chain = make_cleanup (do_restore_instream_cleanup, instream); | |
279 | instream = (FILE *) 0; | |
280 | while (cmdlines) | |
281 | { | |
282 | ret = execute_control_command (cmdlines); | |
283 | if (ret != simple_control && ret != break_control) | |
284 | { | |
285 | warning ("Error in control structure.\n"); | |
286 | break; | |
287 | } | |
288 | cmdlines = cmdlines->next; | |
289 | } | |
290 | do_cleanups (old_chain); | |
20f01a46 DH |
291 | |
292 | user_call_depth--; | |
d318976c FN |
293 | } |
294 | ||
295 | enum command_control_type | |
296 | execute_control_command (struct command_line *cmd) | |
297 | { | |
298 | struct expression *expr; | |
299 | struct command_line *current; | |
300 | struct cleanup *old_chain = 0; | |
f976f6d4 AC |
301 | struct value *val; |
302 | struct value *val_mark; | |
d318976c FN |
303 | int loop; |
304 | enum command_control_type ret; | |
305 | char *new_line; | |
306 | ||
307 | switch (cmd->control_type) | |
308 | { | |
309 | case simple_control: | |
310 | /* A simple command, execute it and return. */ | |
311 | new_line = insert_args (cmd->line); | |
312 | if (!new_line) | |
313 | return invalid_control; | |
314 | old_chain = make_cleanup (free_current_contents, &new_line); | |
315 | execute_command (new_line, 0); | |
316 | ret = cmd->control_type; | |
317 | break; | |
318 | ||
319 | case continue_control: | |
320 | case break_control: | |
321 | /* Return for "continue", and "break" so we can either | |
322 | continue the loop at the top, or break out. */ | |
323 | ret = cmd->control_type; | |
324 | break; | |
325 | ||
326 | case while_control: | |
327 | { | |
328 | /* Parse the loop control expression for the while statement. */ | |
329 | new_line = insert_args (cmd->line); | |
330 | if (!new_line) | |
331 | return invalid_control; | |
332 | old_chain = make_cleanup (free_current_contents, &new_line); | |
333 | expr = parse_expression (new_line); | |
334 | make_cleanup (free_current_contents, &expr); | |
335 | ||
336 | ret = simple_control; | |
337 | loop = 1; | |
338 | ||
339 | /* Keep iterating so long as the expression is true. */ | |
340 | while (loop == 1) | |
341 | { | |
342 | int cond_result; | |
343 | ||
344 | QUIT; | |
345 | ||
346 | /* Evaluate the expression. */ | |
347 | val_mark = value_mark (); | |
348 | val = evaluate_expression (expr); | |
349 | cond_result = value_true (val); | |
350 | value_free_to_mark (val_mark); | |
351 | ||
352 | /* If the value is false, then break out of the loop. */ | |
353 | if (!cond_result) | |
354 | break; | |
355 | ||
356 | /* Execute the body of the while statement. */ | |
357 | current = *cmd->body_list; | |
358 | while (current) | |
359 | { | |
360 | ret = execute_control_command (current); | |
361 | ||
362 | /* If we got an error, or a "break" command, then stop | |
363 | looping. */ | |
364 | if (ret == invalid_control || ret == break_control) | |
365 | { | |
366 | loop = 0; | |
367 | break; | |
368 | } | |
369 | ||
370 | /* If we got a "continue" command, then restart the loop | |
371 | at this point. */ | |
372 | if (ret == continue_control) | |
373 | break; | |
374 | ||
375 | /* Get the next statement. */ | |
376 | current = current->next; | |
377 | } | |
378 | } | |
379 | ||
380 | /* Reset RET so that we don't recurse the break all the way down. */ | |
381 | if (ret == break_control) | |
382 | ret = simple_control; | |
383 | ||
384 | break; | |
385 | } | |
386 | ||
387 | case if_control: | |
388 | { | |
389 | new_line = insert_args (cmd->line); | |
390 | if (!new_line) | |
391 | return invalid_control; | |
392 | old_chain = make_cleanup (free_current_contents, &new_line); | |
393 | /* Parse the conditional for the if statement. */ | |
394 | expr = parse_expression (new_line); | |
395 | make_cleanup (free_current_contents, &expr); | |
396 | ||
397 | current = NULL; | |
398 | ret = simple_control; | |
399 | ||
400 | /* Evaluate the conditional. */ | |
401 | val_mark = value_mark (); | |
402 | val = evaluate_expression (expr); | |
403 | ||
404 | /* Choose which arm to take commands from based on the value of the | |
405 | conditional expression. */ | |
406 | if (value_true (val)) | |
407 | current = *cmd->body_list; | |
408 | else if (cmd->body_count == 2) | |
409 | current = *(cmd->body_list + 1); | |
410 | value_free_to_mark (val_mark); | |
411 | ||
412 | /* Execute commands in the given arm. */ | |
413 | while (current) | |
414 | { | |
415 | ret = execute_control_command (current); | |
416 | ||
417 | /* If we got an error, get out. */ | |
418 | if (ret != simple_control) | |
419 | break; | |
420 | ||
421 | /* Get the next statement in the body. */ | |
422 | current = current->next; | |
423 | } | |
424 | ||
425 | break; | |
426 | } | |
427 | ||
428 | default: | |
429 | warning ("Invalid control type in command structure."); | |
430 | return invalid_control; | |
431 | } | |
432 | ||
433 | if (old_chain) | |
434 | do_cleanups (old_chain); | |
435 | ||
436 | return ret; | |
437 | } | |
438 | ||
439 | /* "while" command support. Executes a body of statements while the | |
440 | loop condition is nonzero. */ | |
441 | ||
442 | void | |
443 | while_command (char *arg, int from_tty) | |
444 | { | |
445 | struct command_line *command = NULL; | |
446 | ||
447 | control_level = 1; | |
448 | command = get_command_line (while_control, arg); | |
449 | ||
450 | if (command == NULL) | |
451 | return; | |
452 | ||
453 | execute_control_command (command); | |
454 | free_command_lines (&command); | |
455 | } | |
456 | ||
457 | /* "if" command support. Execute either the true or false arm depending | |
458 | on the value of the if conditional. */ | |
459 | ||
460 | void | |
461 | if_command (char *arg, int from_tty) | |
462 | { | |
463 | struct command_line *command = NULL; | |
464 | ||
465 | control_level = 1; | |
466 | command = get_command_line (if_control, arg); | |
467 | ||
468 | if (command == NULL) | |
469 | return; | |
470 | ||
471 | execute_control_command (command); | |
472 | free_command_lines (&command); | |
473 | } | |
474 | ||
475 | /* Cleanup */ | |
476 | static void | |
477 | arg_cleanup (void *ignore) | |
478 | { | |
479 | struct user_args *oargs = user_args; | |
480 | if (!user_args) | |
8e65ff28 AC |
481 | internal_error (__FILE__, __LINE__, |
482 | "arg_cleanup called with no user args.\n"); | |
d318976c FN |
483 | |
484 | user_args = user_args->next; | |
b8c9b27d | 485 | xfree (oargs); |
d318976c FN |
486 | } |
487 | ||
488 | /* Bind the incomming arguments for a user defined command to | |
489 | $arg0, $arg1 ... $argMAXUSERARGS. */ | |
490 | ||
491 | static struct cleanup * | |
492 | setup_user_args (char *p) | |
493 | { | |
494 | struct user_args *args; | |
495 | struct cleanup *old_chain; | |
496 | unsigned int arg_count = 0; | |
497 | ||
498 | args = (struct user_args *) xmalloc (sizeof (struct user_args)); | |
499 | memset (args, 0, sizeof (struct user_args)); | |
500 | ||
501 | args->next = user_args; | |
502 | user_args = args; | |
503 | ||
504 | old_chain = make_cleanup (arg_cleanup, 0/*ignored*/); | |
505 | ||
506 | if (p == NULL) | |
507 | return old_chain; | |
508 | ||
509 | while (*p) | |
510 | { | |
511 | char *start_arg; | |
512 | int squote = 0; | |
513 | int dquote = 0; | |
514 | int bsquote = 0; | |
515 | ||
516 | if (arg_count >= MAXUSERARGS) | |
517 | { | |
518 | error ("user defined function may only have %d arguments.\n", | |
519 | MAXUSERARGS); | |
520 | return old_chain; | |
521 | } | |
522 | ||
523 | /* Strip whitespace. */ | |
524 | while (*p == ' ' || *p == '\t') | |
525 | p++; | |
526 | ||
527 | /* P now points to an argument. */ | |
528 | start_arg = p; | |
529 | user_args->a[arg_count].arg = p; | |
530 | ||
531 | /* Get to the end of this argument. */ | |
532 | while (*p) | |
533 | { | |
534 | if (((*p == ' ' || *p == '\t')) && !squote && !dquote && !bsquote) | |
535 | break; | |
536 | else | |
537 | { | |
538 | if (bsquote) | |
539 | bsquote = 0; | |
540 | else if (*p == '\\') | |
541 | bsquote = 1; | |
542 | else if (squote) | |
543 | { | |
544 | if (*p == '\'') | |
545 | squote = 0; | |
546 | } | |
547 | else if (dquote) | |
548 | { | |
549 | if (*p == '"') | |
550 | dquote = 0; | |
551 | } | |
552 | else | |
553 | { | |
554 | if (*p == '\'') | |
555 | squote = 1; | |
556 | else if (*p == '"') | |
557 | dquote = 1; | |
558 | } | |
559 | p++; | |
560 | } | |
561 | } | |
562 | ||
563 | user_args->a[arg_count].len = p - start_arg; | |
564 | arg_count++; | |
565 | user_args->count++; | |
566 | } | |
567 | return old_chain; | |
568 | } | |
569 | ||
570 | /* Given character string P, return a point to the first argument ($arg), | |
571 | or NULL if P contains no arguments. */ | |
572 | ||
573 | static char * | |
574 | locate_arg (char *p) | |
575 | { | |
576 | while ((p = strchr (p, '$'))) | |
577 | { | |
578 | if (strncmp (p, "$arg", 4) == 0 && isdigit (p[4])) | |
579 | return p; | |
580 | p++; | |
581 | } | |
582 | return NULL; | |
583 | } | |
584 | ||
585 | /* Insert the user defined arguments stored in user_arg into the $arg | |
586 | arguments found in line, with the updated copy being placed into nline. */ | |
587 | ||
588 | static char * | |
589 | insert_args (char *line) | |
590 | { | |
591 | char *p, *save_line, *new_line; | |
592 | unsigned len, i; | |
593 | ||
594 | /* First we need to know how much memory to allocate for the new line. */ | |
595 | save_line = line; | |
596 | len = 0; | |
597 | while ((p = locate_arg (line))) | |
598 | { | |
599 | len += p - line; | |
600 | i = p[4] - '0'; | |
601 | ||
602 | if (i >= user_args->count) | |
603 | { | |
604 | error ("Missing argument %d in user function.\n", i); | |
605 | return NULL; | |
606 | } | |
607 | len += user_args->a[i].len; | |
608 | line = p + 5; | |
609 | } | |
610 | ||
611 | /* Don't forget the tail. */ | |
612 | len += strlen (line); | |
613 | ||
614 | /* Allocate space for the new line and fill it in. */ | |
615 | new_line = (char *) xmalloc (len + 1); | |
616 | if (new_line == NULL) | |
617 | return NULL; | |
618 | ||
619 | /* Restore pointer to beginning of old line. */ | |
620 | line = save_line; | |
621 | ||
622 | /* Save pointer to beginning of new line. */ | |
623 | save_line = new_line; | |
624 | ||
625 | while ((p = locate_arg (line))) | |
626 | { | |
627 | int i, len; | |
628 | ||
629 | memcpy (new_line, line, p - line); | |
630 | new_line += p - line; | |
631 | i = p[4] - '0'; | |
632 | ||
633 | len = user_args->a[i].len; | |
634 | if (len) | |
635 | { | |
636 | memcpy (new_line, user_args->a[i].arg, len); | |
637 | new_line += len; | |
638 | } | |
639 | line = p + 5; | |
640 | } | |
641 | /* Don't forget the tail. */ | |
642 | strcpy (new_line, line); | |
643 | ||
644 | /* Return a pointer to the beginning of the new line. */ | |
645 | return save_line; | |
646 | } | |
647 | ||
648 | \f | |
649 | /* Expand the body_list of COMMAND so that it can hold NEW_LENGTH | |
650 | code bodies. This is typically used when we encounter an "else" | |
651 | clause for an "if" command. */ | |
652 | ||
653 | static void | |
654 | realloc_body_list (struct command_line *command, int new_length) | |
655 | { | |
656 | int n; | |
657 | struct command_line **body_list; | |
658 | ||
659 | n = command->body_count; | |
660 | ||
661 | /* Nothing to do? */ | |
662 | if (new_length <= n) | |
663 | return; | |
664 | ||
665 | body_list = (struct command_line **) | |
666 | xmalloc (sizeof (struct command_line *) * new_length); | |
667 | ||
668 | memcpy (body_list, command->body_list, sizeof (struct command_line *) * n); | |
669 | ||
b8c9b27d | 670 | xfree (command->body_list); |
d318976c FN |
671 | command->body_list = body_list; |
672 | command->body_count = new_length; | |
673 | } | |
674 | ||
675 | /* Read one line from the input stream. If the command is an "else" or | |
676 | "end", return such an indication to the caller. */ | |
677 | ||
678 | static enum misc_command_type | |
679 | read_next_line (struct command_line **command) | |
680 | { | |
681 | char *p, *p1, *prompt_ptr, control_prompt[256]; | |
682 | int i = 0; | |
683 | ||
684 | if (control_level >= 254) | |
685 | error ("Control nesting too deep!\n"); | |
686 | ||
687 | /* Set a prompt based on the nesting of the control commands. */ | |
688 | if (instream == stdin || (instream == 0 && readline_hook != NULL)) | |
689 | { | |
690 | for (i = 0; i < control_level; i++) | |
691 | control_prompt[i] = ' '; | |
692 | control_prompt[i] = '>'; | |
693 | control_prompt[i + 1] = '\0'; | |
694 | prompt_ptr = (char *) &control_prompt[0]; | |
695 | } | |
696 | else | |
697 | prompt_ptr = NULL; | |
698 | ||
699 | p = command_line_input (prompt_ptr, instream == stdin, "commands"); | |
700 | ||
701 | /* Not sure what to do here. */ | |
702 | if (p == NULL) | |
703 | return end_command; | |
704 | ||
705 | /* Strip leading and trailing whitespace. */ | |
706 | while (*p == ' ' || *p == '\t') | |
707 | p++; | |
708 | ||
709 | p1 = p + strlen (p); | |
710 | while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) | |
711 | p1--; | |
712 | ||
713 | /* Blanks and comments don't really do anything, but we need to | |
714 | distinguish them from else, end and other commands which can be | |
715 | executed. */ | |
716 | if (p1 == p || p[0] == '#') | |
717 | return nop_command; | |
718 | ||
719 | /* Is this the end of a simple, while, or if control structure? */ | |
720 | if (p1 - p == 3 && !strncmp (p, "end", 3)) | |
721 | return end_command; | |
722 | ||
723 | /* Is the else clause of an if control structure? */ | |
724 | if (p1 - p == 4 && !strncmp (p, "else", 4)) | |
725 | return else_command; | |
726 | ||
727 | /* Check for while, if, break, continue, etc and build a new command | |
728 | line structure for them. */ | |
729 | if (p1 - p > 5 && !strncmp (p, "while", 5)) | |
730 | *command = build_command_line (while_control, p + 6); | |
731 | else if (p1 - p > 2 && !strncmp (p, "if", 2)) | |
732 | *command = build_command_line (if_control, p + 3); | |
733 | else if (p1 - p == 10 && !strncmp (p, "loop_break", 10)) | |
734 | { | |
735 | *command = (struct command_line *) | |
736 | xmalloc (sizeof (struct command_line)); | |
737 | (*command)->next = NULL; | |
738 | (*command)->line = NULL; | |
739 | (*command)->control_type = break_control; | |
740 | (*command)->body_count = 0; | |
741 | (*command)->body_list = NULL; | |
742 | } | |
743 | else if (p1 - p == 13 && !strncmp (p, "loop_continue", 13)) | |
744 | { | |
745 | *command = (struct command_line *) | |
746 | xmalloc (sizeof (struct command_line)); | |
747 | (*command)->next = NULL; | |
748 | (*command)->line = NULL; | |
749 | (*command)->control_type = continue_control; | |
750 | (*command)->body_count = 0; | |
751 | (*command)->body_list = NULL; | |
752 | } | |
753 | else | |
754 | { | |
755 | /* A normal command. */ | |
756 | *command = (struct command_line *) | |
757 | xmalloc (sizeof (struct command_line)); | |
758 | (*command)->next = NULL; | |
759 | (*command)->line = savestring (p, p1 - p); | |
760 | (*command)->control_type = simple_control; | |
761 | (*command)->body_count = 0; | |
762 | (*command)->body_list = NULL; | |
763 | } | |
764 | ||
765 | /* Nothing special. */ | |
766 | return ok_command; | |
767 | } | |
768 | ||
769 | /* Recursively read in the control structures and create a command_line | |
770 | structure from them. | |
771 | ||
772 | The parent_control parameter is the control structure in which the | |
773 | following commands are nested. */ | |
774 | ||
775 | static enum command_control_type | |
776 | recurse_read_control_structure (struct command_line *current_cmd) | |
777 | { | |
778 | int current_body, i; | |
779 | enum misc_command_type val; | |
780 | enum command_control_type ret; | |
781 | struct command_line **body_ptr, *child_tail, *next; | |
782 | ||
783 | child_tail = NULL; | |
784 | current_body = 1; | |
785 | ||
786 | /* Sanity checks. */ | |
787 | if (current_cmd->control_type == simple_control) | |
788 | { | |
789 | error ("Recursed on a simple control type\n"); | |
790 | return invalid_control; | |
791 | } | |
792 | ||
793 | if (current_body > current_cmd->body_count) | |
794 | { | |
795 | error ("Allocated body is smaller than this command type needs\n"); | |
796 | return invalid_control; | |
797 | } | |
798 | ||
799 | /* Read lines from the input stream and build control structures. */ | |
800 | while (1) | |
801 | { | |
802 | dont_repeat (); | |
803 | ||
804 | next = NULL; | |
805 | val = read_next_line (&next); | |
806 | ||
807 | /* Just skip blanks and comments. */ | |
808 | if (val == nop_command) | |
809 | continue; | |
810 | ||
811 | if (val == end_command) | |
812 | { | |
813 | if (current_cmd->control_type == while_control | |
814 | || current_cmd->control_type == if_control) | |
815 | { | |
816 | /* Success reading an entire control structure. */ | |
817 | ret = simple_control; | |
818 | break; | |
819 | } | |
820 | else | |
821 | { | |
822 | ret = invalid_control; | |
823 | break; | |
824 | } | |
825 | } | |
826 | ||
827 | /* Not the end of a control structure. */ | |
828 | if (val == else_command) | |
829 | { | |
830 | if (current_cmd->control_type == if_control | |
831 | && current_body == 1) | |
832 | { | |
833 | realloc_body_list (current_cmd, 2); | |
834 | current_body = 2; | |
835 | child_tail = NULL; | |
836 | continue; | |
837 | } | |
838 | else | |
839 | { | |
840 | ret = invalid_control; | |
841 | break; | |
842 | } | |
843 | } | |
844 | ||
845 | if (child_tail) | |
846 | { | |
847 | child_tail->next = next; | |
848 | } | |
849 | else | |
850 | { | |
851 | body_ptr = current_cmd->body_list; | |
852 | for (i = 1; i < current_body; i++) | |
853 | body_ptr++; | |
854 | ||
855 | *body_ptr = next; | |
856 | ||
857 | } | |
858 | ||
859 | child_tail = next; | |
860 | ||
861 | /* If the latest line is another control structure, then recurse | |
862 | on it. */ | |
863 | if (next->control_type == while_control | |
864 | || next->control_type == if_control) | |
865 | { | |
866 | control_level++; | |
867 | ret = recurse_read_control_structure (next); | |
868 | control_level--; | |
869 | ||
870 | if (ret != simple_control) | |
871 | break; | |
872 | } | |
873 | } | |
874 | ||
875 | dont_repeat (); | |
876 | ||
877 | return ret; | |
878 | } | |
879 | ||
880 | /* Read lines from the input stream and accumulate them in a chain of | |
881 | struct command_line's, which is then returned. For input from a | |
882 | terminal, the special command "end" is used to mark the end of the | |
883 | input, and is not included in the returned chain of commands. */ | |
884 | ||
885 | #define END_MESSAGE "End with a line saying just \"end\"." | |
886 | ||
887 | struct command_line * | |
888 | read_command_lines (char *prompt_arg, int from_tty) | |
889 | { | |
890 | struct command_line *head, *tail, *next; | |
891 | struct cleanup *old_chain; | |
892 | enum command_control_type ret; | |
893 | enum misc_command_type val; | |
894 | ||
895 | control_level = 0; | |
896 | if (readline_begin_hook) | |
897 | { | |
898 | /* Note - intentional to merge messages with no newline */ | |
899 | (*readline_begin_hook) ("%s %s\n", prompt_arg, END_MESSAGE); | |
900 | } | |
901 | else if (from_tty && input_from_terminal_p ()) | |
902 | { | |
903 | printf_unfiltered ("%s\n%s\n", prompt_arg, END_MESSAGE); | |
904 | gdb_flush (gdb_stdout); | |
905 | } | |
906 | ||
907 | head = tail = NULL; | |
908 | old_chain = NULL; | |
909 | ||
910 | while (1) | |
911 | { | |
912 | val = read_next_line (&next); | |
913 | ||
914 | /* Ignore blank lines or comments. */ | |
915 | if (val == nop_command) | |
916 | continue; | |
917 | ||
918 | if (val == end_command) | |
919 | { | |
920 | ret = simple_control; | |
921 | break; | |
922 | } | |
923 | ||
924 | if (val != ok_command) | |
925 | { | |
926 | ret = invalid_control; | |
927 | break; | |
928 | } | |
929 | ||
930 | if (next->control_type == while_control | |
931 | || next->control_type == if_control) | |
932 | { | |
933 | control_level++; | |
934 | ret = recurse_read_control_structure (next); | |
935 | control_level--; | |
936 | ||
937 | if (ret == invalid_control) | |
938 | break; | |
939 | } | |
940 | ||
941 | if (tail) | |
942 | { | |
943 | tail->next = next; | |
944 | } | |
945 | else | |
946 | { | |
947 | head = next; | |
948 | old_chain = make_cleanup_free_command_lines (&head); | |
949 | } | |
950 | tail = next; | |
951 | } | |
952 | ||
953 | dont_repeat (); | |
954 | ||
955 | if (head) | |
956 | { | |
957 | if (ret != invalid_control) | |
958 | { | |
959 | discard_cleanups (old_chain); | |
960 | } | |
961 | else | |
962 | do_cleanups (old_chain); | |
963 | } | |
964 | ||
965 | if (readline_end_hook) | |
966 | { | |
967 | (*readline_end_hook) (); | |
968 | } | |
969 | return (head); | |
970 | } | |
971 | ||
972 | /* Free a chain of struct command_line's. */ | |
973 | ||
974 | void | |
975 | free_command_lines (struct command_line **lptr) | |
976 | { | |
d5b5ac79 AC |
977 | struct command_line *l = *lptr; |
978 | struct command_line *next; | |
d318976c FN |
979 | struct command_line **blist; |
980 | int i; | |
981 | ||
982 | while (l) | |
983 | { | |
984 | if (l->body_count > 0) | |
985 | { | |
986 | blist = l->body_list; | |
987 | for (i = 0; i < l->body_count; i++, blist++) | |
988 | free_command_lines (blist); | |
989 | } | |
990 | next = l->next; | |
b8c9b27d KB |
991 | xfree (l->line); |
992 | xfree (l); | |
d318976c FN |
993 | l = next; |
994 | } | |
0d70f41b | 995 | *lptr = NULL; |
d318976c FN |
996 | } |
997 | ||
998 | static void | |
999 | do_free_command_lines_cleanup (void *arg) | |
1000 | { | |
1001 | free_command_lines (arg); | |
1002 | } | |
1003 | ||
1004 | static struct cleanup * | |
1005 | make_cleanup_free_command_lines (struct command_line **arg) | |
1006 | { | |
1007 | return make_cleanup (do_free_command_lines_cleanup, arg); | |
1008 | } | |
c2b8ed2c MS |
1009 | |
1010 | struct command_line * | |
1011 | copy_command_lines (struct command_line *cmds) | |
1012 | { | |
1013 | struct command_line *result = NULL; | |
1014 | ||
1015 | if (cmds) | |
1016 | { | |
1017 | result = (struct command_line *) xmalloc (sizeof (struct command_line)); | |
1018 | ||
1019 | result->next = copy_command_lines (cmds->next); | |
1020 | result->line = xstrdup (cmds->line); | |
1021 | result->control_type = cmds->control_type; | |
1022 | result->body_count = cmds->body_count; | |
1023 | if (cmds->body_count > 0) | |
1024 | { | |
1025 | int i; | |
1026 | ||
1027 | result->body_list = (struct command_line **) | |
1028 | xmalloc (sizeof (struct command_line *) * cmds->body_count); | |
1029 | ||
1030 | for (i = 0; i < cmds->body_count; i++) | |
1031 | result->body_list[i] = copy_command_lines (cmds->body_list[i]); | |
1032 | } | |
1033 | else | |
1034 | result->body_list = NULL; | |
1035 | } | |
1036 | ||
1037 | return result; | |
1038 | } | |
d318976c FN |
1039 | \f |
1040 | static void | |
1041 | validate_comname (char *comname) | |
1042 | { | |
d5b5ac79 | 1043 | char *p; |
d318976c FN |
1044 | |
1045 | if (comname == 0) | |
1046 | error_no_arg ("name of command to define"); | |
1047 | ||
1048 | p = comname; | |
1049 | while (*p) | |
1050 | { | |
1051 | if (!isalnum (*p) && *p != '-' && *p != '_') | |
1052 | error ("Junk in argument list: \"%s\"", p); | |
1053 | p++; | |
1054 | } | |
1055 | } | |
1056 | ||
1057 | /* This is just a placeholder in the command data structures. */ | |
1058 | static void | |
1059 | user_defined_command (char *ignore, int from_tty) | |
1060 | { | |
1061 | } | |
1062 | ||
1063 | void | |
1064 | define_command (char *comname, int from_tty) | |
1065 | { | |
1066 | #define MAX_TMPBUF 128 | |
1067 | enum cmd_hook_type | |
1068 | { | |
1069 | CMD_NO_HOOK = 0, | |
1070 | CMD_PRE_HOOK, | |
1071 | CMD_POST_HOOK | |
1072 | }; | |
d5b5ac79 AC |
1073 | struct command_line *cmds; |
1074 | struct cmd_list_element *c, *newc, *oldc, *hookc = 0; | |
d318976c FN |
1075 | char *tem = comname; |
1076 | char *tem2; | |
1077 | char tmpbuf[MAX_TMPBUF]; | |
1078 | int hook_type = CMD_NO_HOOK; | |
1079 | int hook_name_size = 0; | |
1080 | ||
1081 | #define HOOK_STRING "hook-" | |
1082 | #define HOOK_LEN 5 | |
1083 | #define HOOK_POST_STRING "hookpost-" | |
1084 | #define HOOK_POST_LEN 9 | |
1085 | ||
1086 | validate_comname (comname); | |
1087 | ||
1088 | /* Look it up, and verify that we got an exact match. */ | |
1089 | c = lookup_cmd (&tem, cmdlist, "", -1, 1); | |
5cb316ef | 1090 | if (c && strcmp (comname, c->name) != 0) |
d318976c FN |
1091 | c = 0; |
1092 | ||
1093 | if (c) | |
1094 | { | |
ab4e3d93 | 1095 | int q; |
d318976c | 1096 | if (c->class == class_user || c->class == class_alias) |
ab4e3d93 | 1097 | q = query ("Redefine command \"%s\"? ", c->name); |
d318976c | 1098 | else |
ab4e3d93 AC |
1099 | q = query ("Really redefine built-in command \"%s\"? ", c->name); |
1100 | if (!q) | |
d318976c FN |
1101 | error ("Command \"%s\" not redefined.", c->name); |
1102 | } | |
1103 | ||
1104 | /* If this new command is a hook, then mark the command which it | |
1105 | is hooking. Note that we allow hooking `help' commands, so that | |
1106 | we can hook the `stop' pseudo-command. */ | |
1107 | ||
1108 | if (!strncmp (comname, HOOK_STRING, HOOK_LEN)) | |
1109 | { | |
1110 | hook_type = CMD_PRE_HOOK; | |
1111 | hook_name_size = HOOK_LEN; | |
1112 | } | |
1113 | else if (!strncmp (comname, HOOK_POST_STRING, HOOK_POST_LEN)) | |
1114 | { | |
1115 | hook_type = CMD_POST_HOOK; | |
1116 | hook_name_size = HOOK_POST_LEN; | |
1117 | } | |
1118 | ||
1119 | if (hook_type != CMD_NO_HOOK) | |
1120 | { | |
1121 | /* Look up cmd it hooks, and verify that we got an exact match. */ | |
1122 | tem = comname + hook_name_size; | |
1123 | hookc = lookup_cmd (&tem, cmdlist, "", -1, 0); | |
5cb316ef | 1124 | if (hookc && strcmp (comname + hook_name_size, hookc->name) != 0) |
d318976c FN |
1125 | hookc = 0; |
1126 | if (!hookc) | |
1127 | { | |
1128 | warning ("Your new `%s' command does not hook any existing command.", | |
1129 | comname); | |
1130 | if (!query ("Proceed? ")) | |
1131 | error ("Not confirmed."); | |
1132 | } | |
1133 | } | |
1134 | ||
1135 | comname = savestring (comname, strlen (comname)); | |
1136 | ||
1137 | /* If the rest of the commands will be case insensitive, this one | |
1138 | should behave in the same manner. */ | |
1139 | for (tem = comname; *tem; tem++) | |
1140 | if (isupper (*tem)) | |
1141 | *tem = tolower (*tem); | |
1142 | ||
1143 | sprintf (tmpbuf, "Type commands for definition of \"%s\".", comname); | |
1144 | cmds = read_command_lines (tmpbuf, from_tty); | |
1145 | ||
1146 | if (c && c->class == class_user) | |
1147 | free_command_lines (&c->user_commands); | |
1148 | ||
1149 | newc = add_cmd (comname, class_user, user_defined_command, | |
1150 | (c && c->class == class_user) | |
1151 | ? c->doc : savestring ("User-defined.", 13), &cmdlist); | |
1152 | newc->user_commands = cmds; | |
1153 | ||
1154 | /* If this new command is a hook, then mark both commands as being | |
1155 | tied. */ | |
1156 | if (hookc) | |
1157 | { | |
1158 | switch (hook_type) | |
1159 | { | |
1160 | case CMD_PRE_HOOK: | |
1161 | hookc->hook_pre = newc; /* Target gets hooked. */ | |
1162 | newc->hookee_pre = hookc; /* We are marked as hooking target cmd. */ | |
1163 | break; | |
1164 | case CMD_POST_HOOK: | |
f48ff60a FN |
1165 | hookc->hook_post = newc; /* Target gets hooked. */ |
1166 | newc->hookee_post = hookc; /* We are marked as hooking target cmd. */ | |
d318976c FN |
1167 | break; |
1168 | default: | |
1169 | /* Should never come here as hookc would be 0. */ | |
8e65ff28 | 1170 | internal_error (__FILE__, __LINE__, "bad switch"); |
d318976c FN |
1171 | } |
1172 | } | |
1173 | } | |
1174 | ||
1175 | void | |
1176 | document_command (char *comname, int from_tty) | |
1177 | { | |
1178 | struct command_line *doclines; | |
d5b5ac79 | 1179 | struct cmd_list_element *c; |
d318976c FN |
1180 | char *tem = comname; |
1181 | char tmpbuf[128]; | |
1182 | ||
1183 | validate_comname (comname); | |
1184 | ||
1185 | c = lookup_cmd (&tem, cmdlist, "", 0, 1); | |
1186 | ||
1187 | if (c->class != class_user) | |
1188 | error ("Command \"%s\" is built-in.", comname); | |
1189 | ||
1190 | sprintf (tmpbuf, "Type documentation for \"%s\".", comname); | |
1191 | doclines = read_command_lines (tmpbuf, from_tty); | |
1192 | ||
1193 | if (c->doc) | |
b8c9b27d | 1194 | xfree (c->doc); |
d318976c FN |
1195 | |
1196 | { | |
d5b5ac79 AC |
1197 | struct command_line *cl1; |
1198 | int len = 0; | |
d318976c FN |
1199 | |
1200 | for (cl1 = doclines; cl1; cl1 = cl1->next) | |
1201 | len += strlen (cl1->line) + 1; | |
1202 | ||
1203 | c->doc = (char *) xmalloc (len + 1); | |
1204 | *c->doc = 0; | |
1205 | ||
1206 | for (cl1 = doclines; cl1; cl1 = cl1->next) | |
1207 | { | |
1208 | strcat (c->doc, cl1->line); | |
1209 | if (cl1->next) | |
1210 | strcat (c->doc, "\n"); | |
1211 | } | |
1212 | } | |
1213 | ||
1214 | free_command_lines (&doclines); | |
1215 | } | |
1216 | \f | |
1217 | struct source_cleanup_lines_args | |
1218 | { | |
1219 | int old_line; | |
1220 | char *old_file; | |
1221 | char *old_pre_error; | |
1222 | char *old_error_pre_print; | |
1223 | }; | |
1224 | ||
1225 | static void | |
4efb68b1 | 1226 | source_cleanup_lines (void *args) |
d318976c FN |
1227 | { |
1228 | struct source_cleanup_lines_args *p = | |
1229 | (struct source_cleanup_lines_args *) args; | |
1230 | source_line_number = p->old_line; | |
1231 | source_file_name = p->old_file; | |
1232 | source_pre_error = p->old_pre_error; | |
1233 | error_pre_print = p->old_error_pre_print; | |
1234 | } | |
1235 | ||
d318976c FN |
1236 | static void |
1237 | do_fclose_cleanup (void *stream) | |
1238 | { | |
1239 | fclose (stream); | |
1240 | } | |
1241 | ||
1242 | /* Used to implement source_command */ | |
1243 | ||
1244 | void | |
1245 | script_from_file (FILE *stream, char *file) | |
1246 | { | |
1247 | struct cleanup *old_cleanups; | |
1248 | struct source_cleanup_lines_args old_lines; | |
1249 | int needed_length; | |
1250 | ||
1251 | if (stream == NULL) | |
1252 | { | |
8e65ff28 | 1253 | internal_error (__FILE__, __LINE__, "called with NULL file pointer!"); |
d318976c FN |
1254 | } |
1255 | ||
1256 | old_cleanups = make_cleanup (do_fclose_cleanup, stream); | |
1257 | ||
1258 | old_lines.old_line = source_line_number; | |
1259 | old_lines.old_file = source_file_name; | |
1260 | old_lines.old_pre_error = source_pre_error; | |
1261 | old_lines.old_error_pre_print = error_pre_print; | |
1262 | make_cleanup (source_cleanup_lines, &old_lines); | |
1263 | source_line_number = 0; | |
1264 | source_file_name = file; | |
1265 | source_pre_error = error_pre_print == NULL ? "" : error_pre_print; | |
1266 | source_pre_error = savestring (source_pre_error, strlen (source_pre_error)); | |
b8c9b27d | 1267 | make_cleanup (xfree, source_pre_error); |
d318976c FN |
1268 | /* This will get set every time we read a line. So it won't stay "" for |
1269 | long. */ | |
1270 | error_pre_print = ""; | |
1271 | ||
1272 | needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80; | |
1273 | if (source_error_allocated < needed_length) | |
1274 | { | |
1275 | source_error_allocated *= 2; | |
1276 | if (source_error_allocated < needed_length) | |
1277 | source_error_allocated = needed_length; | |
1278 | if (source_error == NULL) | |
1279 | source_error = xmalloc (source_error_allocated); | |
1280 | else | |
1281 | source_error = xrealloc (source_error, source_error_allocated); | |
1282 | } | |
1283 | ||
1284 | read_command_file (stream); | |
1285 | ||
1286 | do_cleanups (old_cleanups); | |
1287 | } | |
1288 | ||
1289 | void | |
1290 | show_user_1 (struct cmd_list_element *c, struct ui_file *stream) | |
1291 | { | |
d5b5ac79 | 1292 | struct command_line *cmdlines; |
d318976c FN |
1293 | |
1294 | cmdlines = c->user_commands; | |
1295 | if (!cmdlines) | |
1296 | return; | |
1297 | fputs_filtered ("User command ", stream); | |
1298 | fputs_filtered (c->name, stream); | |
1299 | fputs_filtered (":\n", stream); | |
1300 | ||
d318976c FN |
1301 | print_command_lines (uiout, cmdlines, 1); |
1302 | fputs_filtered ("\n", stream); | |
d318976c FN |
1303 | } |
1304 |