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