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