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