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