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