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