]>
Commit | Line | Data |
---|---|---|
172559ec JK |
1 | /* Top level stuff for GDB, the GNU debugger. |
2 | Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program; if not, write to the Free Software | |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
20 | ||
21 | #include "defs.h" | |
22 | #include "gdbcmd.h" | |
23 | #include "call-cmds.h" | |
24 | #include "symtab.h" | |
25 | #include "inferior.h" | |
26 | #include "signals.h" | |
27 | #include "target.h" | |
28 | #include "breakpoint.h" | |
29 | #include "gdbtypes.h" | |
30 | #include "expression.h" | |
31 | #include "language.h" | |
32 | #include "terminal.h" /* For job_control. */ | |
33 | #include "annotate.h" | |
34 | #include <setjmp.h> | |
35 | #include "top.h" | |
36 | ||
37 | /* readline include files */ | |
38 | #include "readline.h" | |
39 | #include "history.h" | |
40 | ||
41 | /* readline defines this. */ | |
42 | #undef savestring | |
43 | ||
44 | #include <sys/types.h> | |
45 | #ifdef USG | |
46 | /* What is this for? X_OK? */ | |
47 | #include <unistd.h> | |
48 | #endif | |
49 | ||
50 | #include <string.h> | |
51 | #ifndef NO_SYS_FILE | |
52 | #include <sys/file.h> | |
53 | #endif | |
54 | #include <sys/param.h> | |
55 | #include <sys/stat.h> | |
56 | #include <ctype.h> | |
57 | ||
58 | /* Prototypes for local functions */ | |
59 | ||
60 | static char * | |
61 | symbol_completion_function PARAMS ((char *, int)); | |
62 | ||
63 | static void | |
64 | command_loop_marker PARAMS ((int)); | |
65 | ||
66 | static void | |
67 | init_main PARAMS ((void)); | |
68 | ||
69 | static void | |
70 | init_cmd_lists PARAMS ((void)); | |
71 | ||
72 | static void | |
73 | float_handler PARAMS ((int)); | |
74 | ||
75 | static void | |
76 | init_signals PARAMS ((void)); | |
77 | ||
78 | static void | |
79 | set_verbose PARAMS ((char *, int, struct cmd_list_element *)); | |
80 | ||
81 | static void | |
82 | show_history PARAMS ((char *, int)); | |
83 | ||
84 | static void | |
85 | set_history PARAMS ((char *, int)); | |
86 | ||
87 | static void | |
88 | set_history_size_command PARAMS ((char *, int, struct cmd_list_element *)); | |
89 | ||
90 | static void | |
91 | show_commands PARAMS ((char *, int)); | |
92 | ||
93 | static void | |
94 | echo_command PARAMS ((char *, int)); | |
95 | ||
96 | static void | |
97 | pwd_command PARAMS ((char *, int)); | |
98 | ||
99 | static void | |
100 | show_version PARAMS ((char *, int)); | |
101 | ||
102 | static void | |
103 | document_command PARAMS ((char *, int)); | |
104 | ||
105 | static void | |
106 | define_command PARAMS ((char *, int)); | |
107 | ||
108 | static void | |
109 | validate_comname PARAMS ((char *)); | |
110 | ||
111 | static void | |
112 | help_command PARAMS ((char *, int)); | |
113 | ||
114 | static void | |
115 | show_command PARAMS ((char *, int)); | |
116 | ||
117 | static void | |
118 | info_command PARAMS ((char *, int)); | |
119 | ||
120 | static void | |
121 | complete_command PARAMS ((char *, int)); | |
122 | ||
123 | static void | |
124 | do_nothing PARAMS ((int)); | |
125 | ||
126 | static int | |
127 | quit_cover PARAMS ((char *)); | |
128 | ||
129 | static void | |
130 | disconnect PARAMS ((int)); | |
131 | ||
132 | static void | |
133 | source_cleanup PARAMS ((FILE *)); | |
134 | ||
135 | /* If this definition isn't overridden by the header files, assume | |
136 | that isatty and fileno exist on this system. */ | |
137 | #ifndef ISATTY | |
138 | #define ISATTY(FP) (isatty (fileno (FP))) | |
139 | #endif | |
140 | ||
141 | /* Initialization file name for gdb. This is overridden in some configs. */ | |
142 | ||
143 | #ifndef GDBINIT_FILENAME | |
144 | #define GDBINIT_FILENAME ".gdbinit" | |
145 | #endif | |
146 | char gdbinit[] = GDBINIT_FILENAME; | |
147 | int inhibit_gdbinit = 0; | |
148 | ||
149 | /* Version number of GDB, as a string. */ | |
150 | ||
151 | extern char *version; | |
152 | ||
153 | /* Canonical host name as a string. */ | |
154 | ||
155 | extern char *host_name; | |
156 | ||
157 | /* Canonical target name as a string. */ | |
158 | ||
159 | extern char *target_name; | |
160 | ||
161 | extern char lang_frame_mismatch_warn[]; /* language.c */ | |
162 | ||
163 | /* Flag for whether we want all the "from_tty" gubbish printed. */ | |
164 | ||
165 | int caution = 1; /* Default is yes, sigh. */ | |
166 | ||
167 | /* | |
168 | * Define all cmd_list_element's | |
169 | */ | |
170 | ||
171 | /* Chain containing all defined commands. */ | |
172 | ||
173 | struct cmd_list_element *cmdlist; | |
174 | ||
175 | /* Chain containing all defined info subcommands. */ | |
176 | ||
177 | struct cmd_list_element *infolist; | |
178 | ||
179 | /* Chain containing all defined enable subcommands. */ | |
180 | ||
181 | struct cmd_list_element *enablelist; | |
182 | ||
183 | /* Chain containing all defined disable subcommands. */ | |
184 | ||
185 | struct cmd_list_element *disablelist; | |
186 | ||
187 | /* Chain containing all defined delete subcommands. */ | |
188 | ||
189 | struct cmd_list_element *deletelist; | |
190 | ||
191 | /* Chain containing all defined "enable breakpoint" subcommands. */ | |
192 | ||
193 | struct cmd_list_element *enablebreaklist; | |
194 | ||
195 | /* Chain containing all defined set subcommands */ | |
196 | ||
197 | struct cmd_list_element *setlist; | |
198 | ||
199 | /* Chain containing all defined unset subcommands */ | |
200 | ||
201 | struct cmd_list_element *unsetlist; | |
202 | ||
203 | /* Chain containing all defined show subcommands. */ | |
204 | ||
205 | struct cmd_list_element *showlist; | |
206 | ||
207 | /* Chain containing all defined \"set history\". */ | |
208 | ||
209 | struct cmd_list_element *sethistlist; | |
210 | ||
211 | /* Chain containing all defined \"show history\". */ | |
212 | ||
213 | struct cmd_list_element *showhistlist; | |
214 | ||
215 | /* Chain containing all defined \"unset history\". */ | |
216 | ||
217 | struct cmd_list_element *unsethistlist; | |
218 | ||
219 | /* Chain containing all defined maintenance subcommands. */ | |
220 | ||
221 | #if MAINTENANCE_CMDS | |
222 | struct cmd_list_element *maintenancelist; | |
223 | #endif | |
224 | ||
225 | /* Chain containing all defined "maintenance info" subcommands. */ | |
226 | ||
227 | #if MAINTENANCE_CMDS | |
228 | struct cmd_list_element *maintenanceinfolist; | |
229 | #endif | |
230 | ||
231 | /* Chain containing all defined "maintenance print" subcommands. */ | |
232 | ||
233 | #if MAINTENANCE_CMDS | |
234 | struct cmd_list_element *maintenanceprintlist; | |
235 | #endif | |
236 | ||
237 | struct cmd_list_element *setprintlist; | |
238 | ||
239 | struct cmd_list_element *showprintlist; | |
240 | ||
241 | struct cmd_list_element *setchecklist; | |
242 | ||
243 | struct cmd_list_element *showchecklist; | |
244 | ||
245 | /* stdio stream that command input is being read from. Set to stdin normally. | |
246 | Set by source_command to the file we are sourcing. Set to NULL if we are | |
247 | executing a user-defined command. */ | |
248 | ||
249 | FILE *instream; | |
250 | ||
251 | /* Current working directory. */ | |
252 | ||
253 | char *current_directory; | |
254 | ||
255 | /* The directory name is actually stored here (usually). */ | |
256 | char dirbuf[1024]; | |
257 | ||
258 | /* Function to call before reading a command, if nonzero. | |
259 | The function receives two args: an input stream, | |
260 | and a prompt string. */ | |
261 | ||
262 | void (*window_hook) PARAMS ((FILE *, char *)); | |
263 | ||
264 | int epoch_interface; | |
265 | int xgdb_verbose; | |
266 | ||
267 | /* gdb prints this when reading a command interactively */ | |
268 | static char *prompt; | |
269 | ||
270 | /* Buffer used for reading command lines, and the size | |
271 | allocated for it so far. */ | |
272 | ||
273 | char *line; | |
274 | int linesize = 100; | |
275 | ||
276 | /* Nonzero if the current command is modified by "server ". This | |
277 | affects things like recording into the command history, comamnds | |
278 | repeating on RETURN, etc. This is so a user interface (emacs, GUI, | |
279 | whatever) can issue its own commands and also send along commands | |
280 | from the user, and have the user not notice that the user interface | |
281 | is issuing commands too. */ | |
282 | int server_command; | |
283 | ||
284 | /* Baud rate specified for talking to serial target systems. Default | |
285 | is left as -1, so targets can choose their own defaults. */ | |
286 | /* FIXME: This means that "show remotebaud" and gr_files_info can print -1 | |
287 | or (unsigned int)-1. This is a Bad User Interface. */ | |
288 | ||
289 | int baud_rate = -1; | |
290 | ||
291 | /* Non-zero tells remote* modules to output debugging info. */ | |
292 | ||
293 | int remote_debug = 0; | |
294 | ||
295 | /* Signal to catch ^Z typed while reading a command: SIGTSTP or SIGCONT. */ | |
296 | ||
297 | #ifndef STOP_SIGNAL | |
298 | #ifdef SIGTSTP | |
299 | #define STOP_SIGNAL SIGTSTP | |
300 | static void stop_sig PARAMS ((int)); | |
301 | #endif | |
302 | #endif | |
303 | ||
304 | /* Some System V have job control but not sigsetmask(). */ | |
305 | #if !defined (HAVE_SIGSETMASK) | |
306 | #if !defined (USG) | |
307 | #define HAVE_SIGSETMASK 1 | |
308 | #else | |
309 | #define HAVE_SIGSETMASK 0 | |
310 | #endif | |
311 | #endif | |
312 | ||
313 | #if 0 == (HAVE_SIGSETMASK) | |
314 | #define sigsetmask(n) | |
315 | #endif | |
316 | \f | |
317 | /* Where to go for return_to_top_level (RETURN_ERROR). */ | |
318 | jmp_buf error_return; | |
319 | /* Where to go for return_to_top_level (RETURN_QUIT). */ | |
320 | jmp_buf quit_return; | |
321 | ||
322 | /* Return for reason REASON. This generally gets back to the command | |
323 | loop, but can be caught via catch_errors. */ | |
324 | ||
325 | NORETURN void | |
326 | return_to_top_level (reason) | |
327 | enum return_reason reason; | |
328 | { | |
329 | quit_flag = 0; | |
330 | immediate_quit = 0; | |
331 | ||
332 | /* Perhaps it would be cleaner to do this via the cleanup chain (not sure | |
333 | I can think of a reason why that is vital, though). */ | |
334 | bpstat_clear_actions(stop_bpstat); /* Clear queued breakpoint commands */ | |
335 | ||
336 | disable_current_display (); | |
337 | do_cleanups (ALL_CLEANUPS); | |
338 | ||
339 | if (annotation_level > 1) | |
340 | switch (reason) | |
341 | { | |
342 | case RETURN_QUIT: | |
343 | annotate_quit (); | |
344 | break; | |
345 | case RETURN_ERROR: | |
346 | annotate_error (); | |
347 | break; | |
348 | } | |
349 | ||
350 | (NORETURN void) longjmp | |
351 | (reason == RETURN_ERROR ? error_return : quit_return, 1); | |
352 | } | |
353 | ||
354 | /* Call FUNC with arg ARGS, catching any errors. If there is no | |
355 | error, return the value returned by FUNC. If there is an error, | |
356 | print ERRSTRING, print the specific error message, then return | |
357 | zero. | |
358 | ||
359 | Must not be called with immediate_quit in effect (bad things might | |
360 | happen, say we got a signal in the middle of a memcpy to quit_return). | |
361 | This is an OK restriction; with very few exceptions immediate_quit can | |
362 | be replaced by judicious use of QUIT. | |
363 | ||
364 | MASK specifies what to catch; it is normally set to | |
365 | RETURN_MASK_ALL, if for no other reason than that the code which | |
366 | calls catch_errors might not be set up to deal with a quit which | |
367 | isn't caught. But if the code can deal with it, it generally | |
368 | should be RETURN_MASK_ERROR, unless for some reason it is more | |
369 | useful to abort only the portion of the operation inside the | |
370 | catch_errors. Note that quit should return to the command line | |
371 | fairly quickly, even if some further processing is being done. */ | |
372 | ||
373 | int | |
374 | catch_errors (func, args, errstring, mask) | |
375 | int (*func) PARAMS ((char *)); | |
376 | PTR args; | |
377 | char *errstring; | |
378 | return_mask mask; | |
379 | { | |
380 | jmp_buf saved_error; | |
381 | jmp_buf saved_quit; | |
382 | jmp_buf tmp_jmp; | |
383 | int val; | |
384 | struct cleanup *saved_cleanup_chain; | |
385 | char *saved_error_pre_print; | |
386 | ||
387 | saved_cleanup_chain = save_cleanups (); | |
388 | saved_error_pre_print = error_pre_print; | |
389 | ||
390 | if (mask & RETURN_MASK_ERROR) | |
391 | memcpy ((char *)saved_error, (char *)error_return, sizeof (jmp_buf)); | |
392 | if (mask & RETURN_MASK_QUIT) | |
393 | memcpy (saved_quit, quit_return, sizeof (jmp_buf)); | |
394 | error_pre_print = errstring; | |
395 | ||
396 | if (setjmp (tmp_jmp) == 0) | |
397 | { | |
398 | if (mask & RETURN_MASK_ERROR) | |
399 | memcpy (error_return, tmp_jmp, sizeof (jmp_buf)); | |
400 | if (mask & RETURN_MASK_QUIT) | |
401 | memcpy (quit_return, tmp_jmp, sizeof (jmp_buf)); | |
402 | val = (*func) (args); | |
403 | } | |
404 | else | |
405 | val = 0; | |
406 | ||
407 | restore_cleanups (saved_cleanup_chain); | |
408 | ||
409 | error_pre_print = saved_error_pre_print; | |
410 | if (mask & RETURN_MASK_ERROR) | |
411 | memcpy (error_return, saved_error, sizeof (jmp_buf)); | |
412 | if (mask & RETURN_MASK_QUIT) | |
413 | memcpy (quit_return, saved_quit, sizeof (jmp_buf)); | |
414 | return val; | |
415 | } | |
416 | ||
417 | /* Handler for SIGHUP. */ | |
418 | ||
419 | static void | |
420 | disconnect (signo) | |
421 | int signo; | |
422 | { | |
423 | catch_errors (quit_cover, NULL, | |
424 | "Could not kill the program being debugged", RETURN_MASK_ALL); | |
425 | signal (SIGHUP, SIG_DFL); | |
426 | kill (getpid (), SIGHUP); | |
427 | } | |
428 | ||
429 | /* Just a little helper function for disconnect(). */ | |
430 | ||
431 | static int | |
432 | quit_cover (s) | |
433 | char *s; | |
434 | { | |
435 | caution = 0; /* Throw caution to the wind -- we're exiting. | |
436 | This prevents asking the user dumb questions. */ | |
437 | quit_command((char *)0, 0); | |
438 | return 0; | |
439 | } | |
440 | \f | |
441 | /* Line number we are currently in in a file which is being sourced. */ | |
442 | static int source_line_number; | |
443 | ||
444 | /* Name of the file we are sourcing. */ | |
445 | static char *source_file_name; | |
446 | ||
447 | /* Buffer containing the error_pre_print used by the source stuff. | |
448 | Malloc'd. */ | |
449 | static char *source_error; | |
450 | static int source_error_allocated; | |
451 | ||
452 | /* Something to glom on to the start of error_pre_print if source_file_name | |
453 | is set. */ | |
454 | static char *source_pre_error; | |
455 | ||
456 | /* Clean up on error during a "source" command (or execution of a | |
457 | user-defined command). */ | |
458 | ||
459 | static void | |
460 | source_cleanup (stream) | |
461 | FILE *stream; | |
462 | { | |
463 | /* Restore the previous input stream. */ | |
464 | instream = stream; | |
465 | } | |
466 | ||
467 | /* Read commands from STREAM. */ | |
468 | void | |
469 | read_command_file (stream) | |
470 | FILE *stream; | |
471 | { | |
472 | struct cleanup *cleanups; | |
473 | ||
474 | cleanups = make_cleanup (source_cleanup, instream); | |
475 | instream = stream; | |
476 | command_loop (); | |
477 | do_cleanups (cleanups); | |
478 | } | |
479 | \f | |
480 | extern void init_proc (); | |
481 | ||
482 | void | |
483 | gdb_init () | |
484 | { | |
485 | /* Run the init function of each source file */ | |
486 | ||
f36b58b1 TL |
487 | getcwd (dirbuf, sizeof (dirbuf)); |
488 | current_directory = dirbuf; | |
489 | ||
172559ec JK |
490 | init_cmd_lists (); /* This needs to be done first */ |
491 | initialize_all_files (); | |
492 | init_main (); /* But that omits this file! Do it now */ | |
493 | init_signals (); | |
494 | ||
495 | init_proc (); | |
496 | ||
497 | /* We need a default language for parsing expressions, so simple things like | |
498 | "set width 0" won't fail if no language is explicitly set in a config file | |
499 | or implicitly set by reading an executable during startup. */ | |
500 | set_language (language_c); | |
501 | expected_language = current_language; /* don't warn about the change. */ | |
502 | } | |
503 | ||
504 | void | |
505 | execute_user_command (c, args) | |
506 | struct cmd_list_element *c; | |
507 | char *args; | |
508 | { | |
509 | register struct command_line *cmdlines; | |
510 | struct cleanup *old_chain; | |
511 | ||
512 | if (args) | |
513 | error ("User-defined commands cannot take arguments."); | |
514 | ||
515 | cmdlines = c->user_commands; | |
516 | if (cmdlines == 0) | |
517 | /* Null command */ | |
518 | return; | |
519 | ||
520 | /* Set the instream to 0, indicating execution of a | |
521 | user-defined function. */ | |
522 | old_chain = make_cleanup (source_cleanup, instream); | |
523 | instream = (FILE *) 0; | |
524 | while (cmdlines) | |
525 | { | |
526 | execute_command (cmdlines->line, 0); | |
527 | cmdlines = cmdlines->next; | |
528 | } | |
529 | do_cleanups (old_chain); | |
530 | } | |
531 | ||
532 | /* Execute the line P as a command. | |
533 | Pass FROM_TTY as second argument to the defining function. */ | |
534 | ||
535 | void | |
536 | execute_command (p, from_tty) | |
537 | char *p; | |
538 | int from_tty; | |
539 | { | |
540 | register struct cmd_list_element *c; | |
541 | register enum language flang; | |
542 | static int warned = 0; | |
543 | ||
544 | free_all_values (); | |
545 | ||
546 | /* This can happen when command_line_input hits end of file. */ | |
547 | if (p == NULL) | |
548 | return; | |
549 | ||
550 | while (*p == ' ' || *p == '\t') p++; | |
551 | if (*p) | |
552 | { | |
553 | char *arg; | |
554 | ||
555 | c = lookup_cmd (&p, cmdlist, "", 0, 1); | |
556 | /* Pass null arg rather than an empty one. */ | |
557 | arg = *p ? p : 0; | |
558 | ||
559 | /* If this command has been hooked, run the hook first. */ | |
560 | if (c->hook) | |
561 | execute_user_command (c->hook, (char *)0); | |
562 | ||
563 | if (c->class == class_user) | |
564 | execute_user_command (c, arg); | |
565 | else if (c->type == set_cmd || c->type == show_cmd) | |
566 | do_setshow_command (arg, from_tty & caution, c); | |
567 | else if (c->function.cfunc == NO_FUNCTION) | |
568 | error ("That is not a command, just a help topic."); | |
569 | else | |
570 | (*c->function.cfunc) (arg, from_tty & caution); | |
571 | } | |
572 | ||
573 | /* Tell the user if the language has changed (except first time). */ | |
574 | if (current_language != expected_language) | |
575 | { | |
576 | if (language_mode == language_mode_auto) { | |
577 | language_info (1); /* Print what changed. */ | |
578 | } | |
579 | warned = 0; | |
580 | } | |
581 | ||
582 | /* Warn the user if the working language does not match the | |
583 | language of the current frame. Only warn the user if we are | |
584 | actually running the program, i.e. there is a stack. */ | |
585 | /* FIXME: This should be cacheing the frame and only running when | |
586 | the frame changes. */ | |
587 | if (target_has_stack) | |
588 | { | |
589 | flang = get_frame_language (); | |
590 | if (!warned | |
591 | && flang != language_unknown | |
592 | && flang != current_language->la_language) | |
593 | { | |
594 | printf_filtered ("%s\n", lang_frame_mismatch_warn); | |
595 | warned = 1; | |
596 | } | |
597 | } | |
598 | } | |
599 | ||
600 | /* ARGSUSED */ | |
601 | static void | |
602 | command_loop_marker (foo) | |
603 | int foo; | |
604 | { | |
605 | } | |
606 | ||
607 | /* Read commands from `instream' and execute them | |
608 | until end of file or error reading instream. */ | |
609 | void | |
610 | command_loop () | |
611 | { | |
612 | struct cleanup *old_chain; | |
613 | char *command; | |
614 | int stdin_is_tty = ISATTY (stdin); | |
615 | ||
616 | while (!feof (instream)) | |
617 | { | |
618 | if (window_hook && instream == stdin) | |
619 | (*window_hook) (instream, prompt); | |
620 | ||
621 | quit_flag = 0; | |
622 | if (instream == stdin && stdin_is_tty) | |
623 | reinitialize_more_filter (); | |
624 | old_chain = make_cleanup (command_loop_marker, 0); | |
625 | command = command_line_input (instream == stdin ? prompt : (char *) NULL, | |
626 | instream == stdin, "prompt"); | |
627 | if (command == 0) | |
628 | return; | |
629 | execute_command (command, instream == stdin); | |
630 | /* Do any commands attached to breakpoint we stopped at. */ | |
631 | bpstat_do_actions (&stop_bpstat); | |
632 | do_cleanups (old_chain); | |
633 | } | |
634 | } | |
635 | \f | |
636 | /* Commands call this if they do not want to be repeated by null lines. */ | |
637 | ||
638 | void | |
639 | dont_repeat () | |
640 | { | |
641 | if (server_command) | |
642 | return; | |
643 | ||
644 | /* If we aren't reading from standard input, we are saving the last | |
645 | thing read from stdin in line and don't want to delete it. Null lines | |
646 | won't repeat here in any case. */ | |
647 | if (instream == stdin) | |
648 | *line = 0; | |
649 | } | |
650 | \f | |
651 | /* Read a line from the stream "instream" without command line editing. | |
652 | ||
653 | It prints PRROMPT once at the start. | |
654 | Action is compatible with "readline", e.g. space for the result is | |
655 | malloc'd and should be freed by the caller. | |
656 | ||
657 | A NULL return means end of file. */ | |
658 | char * | |
659 | gdb_readline (prrompt) | |
660 | char *prrompt; | |
661 | { | |
662 | int c; | |
663 | char *result; | |
664 | int input_index = 0; | |
665 | int result_size = 80; | |
666 | ||
667 | if (prrompt) | |
668 | { | |
669 | /* Don't use a _filtered function here. It causes the assumed | |
670 | character position to be off, since the newline we read from | |
671 | the user is not accounted for. */ | |
672 | fputs_unfiltered (prrompt, gdb_stdout); | |
673 | /* start-sanitize-mpw */ | |
674 | #ifdef MPW | |
675 | /* Move to a new line so the entered line doesn't have a prompt | |
676 | on the front of it. */ | |
677 | fputs_unfiltered ("\n", gdb_stdout); | |
678 | #endif /* MPW */ | |
679 | /* end-sanitize-mpw */ | |
680 | gdb_flush (gdb_stdout); | |
681 | } | |
682 | ||
683 | result = (char *) xmalloc (result_size); | |
684 | ||
685 | while (1) | |
686 | { | |
687 | /* Read from stdin if we are executing a user defined command. | |
688 | This is the right thing for prompt_for_continue, at least. */ | |
689 | c = fgetc (instream ? instream : stdin); | |
690 | ||
691 | if (c == EOF) | |
692 | { | |
693 | if (input_index > 0) | |
694 | /* The last line does not end with a newline. Return it, and | |
695 | if we are called again fgetc will still return EOF and | |
696 | we'll return NULL then. */ | |
697 | break; | |
698 | free (result); | |
699 | return NULL; | |
700 | } | |
701 | ||
702 | if (c == '\n') | |
703 | break; | |
704 | ||
705 | result[input_index++] = c; | |
706 | while (input_index >= result_size) | |
707 | { | |
708 | result_size *= 2; | |
709 | result = (char *) xrealloc (result, result_size); | |
710 | } | |
711 | } | |
712 | ||
713 | result[input_index++] = '\0'; | |
714 | return result; | |
715 | } | |
716 | ||
717 | /* Variables which control command line editing and history | |
718 | substitution. These variables are given default values at the end | |
719 | of this file. */ | |
720 | static int command_editing_p; | |
721 | static int history_expansion_p; | |
722 | static int write_history_p; | |
723 | static int history_size; | |
724 | static char *history_filename; | |
725 | ||
726 | /* readline uses the word breaks for two things: | |
727 | (1) In figuring out where to point the TEXT parameter to the | |
728 | rl_completion_entry_function. Since we don't use TEXT for much, | |
729 | it doesn't matter a lot what the word breaks are for this purpose, but | |
730 | it does affect how much stuff M-? lists. | |
731 | (2) If one of the matches contains a word break character, readline | |
732 | will quote it. That's why we switch between | |
733 | gdb_completer_word_break_characters and | |
734 | gdb_completer_command_word_break_characters. I'm not sure when | |
735 | we need this behavior (perhaps for funky characters in C++ symbols?). */ | |
736 | ||
737 | /* Variables which are necessary for fancy command line editing. */ | |
738 | char *gdb_completer_word_break_characters = | |
739 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,-"; | |
740 | ||
741 | /* When completing on command names, we remove '-' from the list of | |
742 | word break characters, since we use it in command names. If the | |
743 | readline library sees one in any of the current completion strings, | |
744 | it thinks that the string needs to be quoted and automatically supplies | |
745 | a leading quote. */ | |
746 | char *gdb_completer_command_word_break_characters = | |
747 | " \t\n!@#$%^&*()+=|~`}{[]\"';:?/>.<,"; | |
748 | ||
749 | /* Characters that can be used to quote completion strings. Note that we | |
750 | can't include '"' because the gdb C parser treats such quoted sequences | |
751 | as strings. */ | |
752 | char *gdb_completer_quote_characters = | |
753 | "'"; | |
754 | ||
755 | /* Functions that are used as part of the fancy command line editing. */ | |
756 | ||
757 | /* This can be used for functions which don't want to complete on symbols | |
758 | but don't want to complete on anything else either. */ | |
759 | /* ARGSUSED */ | |
760 | char ** | |
761 | noop_completer (text, prefix) | |
762 | char *text; | |
763 | char *prefix; | |
764 | { | |
765 | return NULL; | |
766 | } | |
767 | ||
768 | /* Complete on filenames. */ | |
769 | char ** | |
770 | filename_completer (text, word) | |
771 | char *text; | |
772 | char *word; | |
773 | { | |
774 | /* From readline. */ | |
775 | extern char *filename_completion_function (); | |
776 | int subsequent_name; | |
777 | char **return_val; | |
778 | int return_val_used; | |
779 | int return_val_alloced; | |
780 | ||
781 | return_val_used = 0; | |
782 | /* Small for testing. */ | |
783 | return_val_alloced = 1; | |
784 | return_val = (char **) xmalloc (return_val_alloced * sizeof (char *)); | |
785 | ||
786 | subsequent_name = 0; | |
787 | while (1) | |
788 | { | |
789 | char *p; | |
790 | p = filename_completion_function (text, subsequent_name); | |
791 | if (return_val_used >= return_val_alloced) | |
792 | { | |
793 | return_val_alloced *= 2; | |
794 | return_val = | |
795 | (char **) xrealloc (return_val, | |
796 | return_val_alloced * sizeof (char *)); | |
797 | } | |
798 | if (p == NULL) | |
799 | { | |
800 | return_val[return_val_used++] = p; | |
801 | break; | |
802 | } | |
803 | /* Like emacs, don't complete on old versions. Especially useful | |
804 | in the "source" command. */ | |
805 | if (p[strlen (p) - 1] == '~') | |
806 | continue; | |
807 | ||
808 | { | |
809 | char *q; | |
810 | if (word == text) | |
811 | /* Return exactly p. */ | |
812 | return_val[return_val_used++] = p; | |
813 | else if (word > text) | |
814 | { | |
815 | /* Return some portion of p. */ | |
816 | q = xmalloc (strlen (p) + 5); | |
817 | strcpy (q, p + (word - text)); | |
818 | return_val[return_val_used++] = q; | |
819 | free (p); | |
820 | } | |
821 | else | |
822 | { | |
823 | /* Return some of TEXT plus p. */ | |
824 | q = xmalloc (strlen (p) + (text - word) + 5); | |
825 | strncpy (q, word, text - word); | |
826 | q[text - word] = '\0'; | |
827 | strcat (q, p); | |
828 | return_val[return_val_used++] = q; | |
829 | free (p); | |
830 | } | |
831 | } | |
832 | subsequent_name = 1; | |
833 | } | |
834 | #if 0 | |
835 | /* There is no way to do this just long enough to affect quote inserting | |
836 | without also affecting the next completion. This should be fixed in | |
837 | readline. FIXME. */ | |
838 | /* Insure that readline does the right thing | |
839 | with respect to inserting quotes. */ | |
840 | rl_completer_word_break_characters = ""; | |
841 | #endif | |
842 | return return_val; | |
843 | } | |
844 | ||
845 | /* Here are some useful test cases for completion. FIXME: These should | |
846 | be put in the test suite. They should be tested with both M-? and TAB. | |
847 | ||
848 | "show output-" "radix" | |
849 | "show output" "-radix" | |
850 | "p" ambiguous (commands starting with p--path, print, printf, etc.) | |
851 | "p " ambiguous (all symbols) | |
852 | "info t foo" no completions | |
853 | "info t " no completions | |
854 | "info t" ambiguous ("info target", "info terminal", etc.) | |
855 | "info ajksdlfk" no completions | |
856 | "info ajksdlfk " no completions | |
857 | "info" " " | |
858 | "info " ambiguous (all info commands) | |
859 | "p \"a" no completions (string constant) | |
860 | "p 'a" ambiguous (all symbols starting with a) | |
861 | "p b-a" ambiguous (all symbols starting with a) | |
862 | "p b-" ambiguous (all symbols) | |
863 | "file Make" "file" (word break hard to screw up here) | |
864 | "file ../gdb.stabs/we" "ird" (needs to not break word at slash) | |
865 | */ | |
866 | ||
867 | /* Generate completions one by one for the completer. Each time we are | |
868 | called return another potential completion to the caller. The function | |
869 | is misnamed; it just completes on commands or passes the buck to the | |
870 | command's completer function; the stuff specific to symbol completion | |
871 | is in make_symbol_completion_list. | |
872 | ||
873 | TEXT is readline's idea of the "word" we are looking at; we don't really | |
874 | like readline's ideas about word breaking so we ignore it. | |
875 | ||
876 | MATCHES is the number of matches that have currently been collected from | |
877 | calling this completion function. When zero, then we need to initialize, | |
878 | otherwise the initialization has already taken place and we can just | |
879 | return the next potential completion string. | |
880 | ||
881 | Returns NULL if there are no more completions, else a pointer to a string | |
882 | which is a possible completion. | |
883 | ||
884 | RL_LINE_BUFFER is available to be looked at; it contains the entire text | |
885 | of the line. RL_POINT is the offset in that line of the cursor. You | |
886 | should pretend that the line ends at RL_POINT. */ | |
887 | ||
888 | static char * | |
889 | symbol_completion_function (text, matches) | |
890 | char *text; | |
891 | int matches; | |
892 | { | |
893 | static char **list = (char **)NULL; /* Cache of completions */ | |
894 | static int index; /* Next cached completion */ | |
895 | char *output = NULL; | |
896 | char *tmp_command, *p; | |
897 | /* Pointer within tmp_command which corresponds to text. */ | |
898 | char *word; | |
899 | struct cmd_list_element *c, *result_list; | |
900 | ||
901 | if (matches == 0) | |
902 | { | |
903 | /* The caller is beginning to accumulate a new set of completions, so | |
904 | we need to find all of them now, and cache them for returning one at | |
905 | a time on future calls. */ | |
906 | ||
907 | if (list) | |
908 | { | |
909 | /* Free the storage used by LIST, but not by the strings inside. | |
910 | This is because rl_complete_internal () frees the strings. */ | |
911 | free ((PTR)list); | |
912 | } | |
913 | list = 0; | |
914 | index = 0; | |
915 | ||
916 | /* Choose the default set of word break characters to break completions. | |
917 | If we later find out that we are doing completions on command strings | |
918 | (as opposed to strings supplied by the individual command completer | |
919 | functions, which can be any string) then we will switch to the | |
920 | special word break set for command strings, which leaves out the | |
921 | '-' character used in some commands. */ | |
922 | ||
923 | rl_completer_word_break_characters = | |
924 | gdb_completer_word_break_characters; | |
925 | ||
926 | /* Decide whether to complete on a list of gdb commands or on symbols. */ | |
927 | tmp_command = (char *) alloca (rl_point + 1); | |
928 | p = tmp_command; | |
929 | ||
930 | strncpy (tmp_command, rl_line_buffer, rl_point); | |
931 | tmp_command[rl_point] = '\0'; | |
932 | /* Since text always contains some number of characters leading up | |
933 | to rl_point, we can find the equivalent position in tmp_command | |
934 | by subtracting that many characters from the end of tmp_command. */ | |
935 | word = tmp_command + rl_point - strlen (text); | |
936 | ||
937 | if (rl_point == 0) | |
938 | { | |
939 | /* An empty line we want to consider ambiguous; that is, it | |
940 | could be any command. */ | |
941 | c = (struct cmd_list_element *) -1; | |
942 | result_list = 0; | |
943 | } | |
944 | else | |
945 | { | |
946 | c = lookup_cmd_1 (&p, cmdlist, &result_list, 1); | |
947 | } | |
948 | ||
949 | /* Move p up to the next interesting thing. */ | |
950 | while (*p == ' ' || *p == '\t') | |
951 | { | |
952 | p++; | |
953 | } | |
954 | ||
955 | if (!c) | |
956 | { | |
957 | /* It is an unrecognized command. So there are no | |
958 | possible completions. */ | |
959 | list = NULL; | |
960 | } | |
961 | else if (c == (struct cmd_list_element *) -1) | |
962 | { | |
963 | char *q; | |
964 | ||
965 | /* lookup_cmd_1 advances p up to the first ambiguous thing, but | |
966 | doesn't advance over that thing itself. Do so now. */ | |
967 | q = p; | |
968 | while (*q && (isalnum (*q) || *q == '-' || *q == '_')) | |
969 | ++q; | |
970 | if (q != tmp_command + rl_point) | |
971 | { | |
972 | /* There is something beyond the ambiguous | |
973 | command, so there are no possible completions. For | |
974 | example, "info t " or "info t foo" does not complete | |
975 | to anything, because "info t" can be "info target" or | |
976 | "info terminal". */ | |
977 | list = NULL; | |
978 | } | |
979 | else | |
980 | { | |
981 | /* We're trying to complete on the command which was ambiguous. | |
982 | This we can deal with. */ | |
983 | if (result_list) | |
984 | { | |
985 | list = complete_on_cmdlist (*result_list->prefixlist, p, | |
986 | word); | |
987 | } | |
988 | else | |
989 | { | |
990 | list = complete_on_cmdlist (cmdlist, p, word); | |
991 | } | |
992 | /* Insure that readline does the right thing with respect to | |
993 | inserting quotes. */ | |
994 | rl_completer_word_break_characters = | |
995 | gdb_completer_command_word_break_characters; | |
996 | } | |
997 | } | |
998 | else | |
999 | { | |
1000 | /* We've recognized a full command. */ | |
1001 | ||
1002 | if (p == tmp_command + rl_point) | |
1003 | { | |
1004 | /* There is no non-whitespace in the line beyond the command. */ | |
1005 | ||
1006 | if (p[-1] == ' ' || p[-1] == '\t') | |
1007 | { | |
1008 | /* The command is followed by whitespace; we need to complete | |
1009 | on whatever comes after command. */ | |
1010 | if (c->prefixlist) | |
1011 | { | |
1012 | /* It is a prefix command; what comes after it is | |
1013 | a subcommand (e.g. "info "). */ | |
1014 | list = complete_on_cmdlist (*c->prefixlist, p, word); | |
1015 | ||
1016 | /* Insure that readline does the right thing | |
1017 | with respect to inserting quotes. */ | |
1018 | rl_completer_word_break_characters = | |
1019 | gdb_completer_command_word_break_characters; | |
1020 | } | |
1021 | else | |
1022 | { | |
1023 | /* It is a normal command; what comes after it is | |
1024 | completed by the command's completer function. */ | |
1025 | list = (*c->completer) (p, word); | |
1026 | } | |
1027 | } | |
1028 | else | |
1029 | { | |
1030 | /* The command is not followed by whitespace; we need to | |
1031 | complete on the command itself. e.g. "p" which is a | |
1032 | command itself but also can complete to "print", "ptype" | |
1033 | etc. */ | |
1034 | char *q; | |
1035 | ||
1036 | /* Find the command we are completing on. */ | |
1037 | q = p; | |
1038 | while (q > tmp_command) | |
1039 | { | |
1040 | if (isalnum (q[-1]) || q[-1] == '-' || q[-1] == '_') | |
1041 | --q; | |
1042 | else | |
1043 | break; | |
1044 | } | |
1045 | ||
1046 | list = complete_on_cmdlist (result_list, q, word); | |
1047 | ||
1048 | /* Insure that readline does the right thing | |
1049 | with respect to inserting quotes. */ | |
1050 | rl_completer_word_break_characters = | |
1051 | gdb_completer_command_word_break_characters; | |
1052 | } | |
1053 | } | |
1054 | else | |
1055 | { | |
1056 | /* There is non-whitespace beyond the command. */ | |
1057 | ||
1058 | if (c->prefixlist && !c->allow_unknown) | |
1059 | { | |
1060 | /* It is an unrecognized subcommand of a prefix command, | |
1061 | e.g. "info adsfkdj". */ | |
1062 | list = NULL; | |
1063 | } | |
1064 | else | |
1065 | { | |
1066 | /* It is a normal command. */ | |
1067 | list = (*c->completer) (p, word); | |
1068 | } | |
1069 | } | |
1070 | } | |
1071 | } | |
1072 | ||
1073 | /* If we found a list of potential completions during initialization then | |
1074 | dole them out one at a time. The vector of completions is NULL | |
1075 | terminated, so after returning the last one, return NULL (and continue | |
1076 | to do so) each time we are called after that, until a new list is | |
1077 | available. */ | |
1078 | ||
1079 | if (list) | |
1080 | { | |
1081 | output = list[index]; | |
1082 | if (output) | |
1083 | { | |
1084 | index++; | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | #if 0 | |
1089 | /* Can't do this because readline hasn't yet checked the word breaks | |
1090 | for figuring out whether to insert a quote. */ | |
1091 | if (output == NULL) | |
1092 | /* Make sure the word break characters are set back to normal for the | |
1093 | next time that readline tries to complete something. */ | |
1094 | rl_completer_word_break_characters = | |
1095 | gdb_completer_word_break_characters; | |
1096 | #endif | |
1097 | ||
1098 | return (output); | |
1099 | } | |
1100 | ||
1101 | /* Skip over a possibly quoted word (as defined by the quote characters | |
1102 | and word break characters the completer uses). Returns pointer to the | |
1103 | location after the "word". */ | |
1104 | ||
1105 | char * | |
1106 | skip_quoted (str) | |
1107 | char *str; | |
1108 | { | |
1109 | char quote_char = '\0'; | |
1110 | char *scan; | |
1111 | ||
1112 | for (scan = str; *scan != '\0'; scan++) | |
1113 | { | |
1114 | if (quote_char != '\0') | |
1115 | { | |
1116 | /* Ignore everything until the matching close quote char */ | |
1117 | if (*scan == quote_char) | |
1118 | { | |
1119 | /* Found matching close quote. */ | |
1120 | scan++; | |
1121 | break; | |
1122 | } | |
1123 | } | |
1124 | else if (strchr (gdb_completer_quote_characters, *scan)) | |
1125 | { | |
1126 | /* Found start of a quoted string. */ | |
1127 | quote_char = *scan; | |
1128 | } | |
1129 | else if (strchr (gdb_completer_word_break_characters, *scan)) | |
1130 | { | |
1131 | break; | |
1132 | } | |
1133 | } | |
1134 | return (scan); | |
1135 | } | |
1136 | ||
1137 | \f | |
1138 | #ifdef STOP_SIGNAL | |
1139 | static void | |
1140 | stop_sig (signo) | |
1141 | int signo; | |
1142 | { | |
1143 | #if STOP_SIGNAL == SIGTSTP | |
1144 | signal (SIGTSTP, SIG_DFL); | |
1145 | sigsetmask (0); | |
1146 | kill (getpid (), SIGTSTP); | |
1147 | signal (SIGTSTP, stop_sig); | |
1148 | #else | |
1149 | signal (STOP_SIGNAL, stop_sig); | |
1150 | #endif | |
1151 | printf_unfiltered ("%s", prompt); | |
1152 | gdb_flush (gdb_stdout); | |
1153 | ||
1154 | /* Forget about any previous command -- null line now will do nothing. */ | |
1155 | dont_repeat (); | |
1156 | } | |
1157 | #endif /* STOP_SIGNAL */ | |
1158 | ||
1159 | /* Initialize signal handlers. */ | |
1160 | static void | |
1161 | do_nothing (signo) | |
1162 | int signo; | |
1163 | { | |
1164 | } | |
1165 | ||
1166 | static void | |
1167 | init_signals () | |
1168 | { | |
1169 | signal (SIGINT, request_quit); | |
1170 | ||
1171 | /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get | |
1172 | passed to the inferior, which we don't want. It would be | |
1173 | possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but | |
1174 | on BSD4.3 systems using vfork, that can affect the | |
1175 | GDB process as well as the inferior (the signal handling tables | |
1176 | might be in memory, shared between the two). Since we establish | |
1177 | a handler for SIGQUIT, when we call exec it will set the signal | |
1178 | to SIG_DFL for us. */ | |
1179 | signal (SIGQUIT, do_nothing); | |
1180 | if (signal (SIGHUP, do_nothing) != SIG_IGN) | |
1181 | signal (SIGHUP, disconnect); | |
1182 | signal (SIGFPE, float_handler); | |
1183 | ||
1184 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) | |
1185 | signal (SIGWINCH, SIGWINCH_HANDLER); | |
1186 | #endif | |
1187 | } | |
1188 | \f | |
1189 | /* Read one line from the command input stream `instream' | |
1190 | into the local static buffer `linebuffer' (whose current length | |
1191 | is `linelength'). | |
1192 | The buffer is made bigger as necessary. | |
1193 | Returns the address of the start of the line. | |
1194 | ||
1195 | NULL is returned for end of file. | |
1196 | ||
1197 | *If* the instream == stdin & stdin is a terminal, the line read | |
1198 | is copied into the file line saver (global var char *line, | |
1199 | length linesize) so that it can be duplicated. | |
1200 | ||
1201 | This routine either uses fancy command line editing or | |
1202 | simple input as the user has requested. */ | |
1203 | ||
1204 | char * | |
1205 | command_line_input (prrompt, repeat, annotation_suffix) | |
1206 | char *prrompt; | |
1207 | int repeat; | |
1208 | char *annotation_suffix; | |
1209 | { | |
1210 | static char *linebuffer = 0; | |
1211 | static unsigned linelength = 0; | |
1212 | register char *p; | |
1213 | char *p1; | |
1214 | char *rl; | |
1215 | char *local_prompt = prrompt; | |
1216 | register int c; | |
1217 | char *nline; | |
1218 | char got_eof = 0; | |
1219 | ||
1220 | if (annotation_level > 1 && instream == stdin) | |
1221 | { | |
1222 | local_prompt = alloca ((prrompt == NULL ? 0 : strlen (prrompt)) | |
1223 | + strlen (annotation_suffix) + 40); | |
1224 | if (prrompt == NULL) | |
1225 | local_prompt[0] = '\0'; | |
1226 | else | |
1227 | strcpy (local_prompt, prrompt); | |
1228 | strcat (local_prompt, "\n\032\032"); | |
1229 | strcat (local_prompt, annotation_suffix); | |
1230 | strcat (local_prompt, "\n"); | |
1231 | } | |
1232 | ||
1233 | if (linebuffer == 0) | |
1234 | { | |
1235 | linelength = 80; | |
1236 | linebuffer = (char *) xmalloc (linelength); | |
1237 | } | |
1238 | ||
1239 | p = linebuffer; | |
1240 | ||
1241 | /* Control-C quits instantly if typed while in this loop | |
1242 | since it should not wait until the user types a newline. */ | |
1243 | immediate_quit++; | |
1244 | #ifdef STOP_SIGNAL | |
1245 | if (job_control) | |
1246 | signal (STOP_SIGNAL, stop_sig); | |
1247 | #endif | |
1248 | ||
1249 | while (1) | |
1250 | { | |
1251 | /* Make sure that all output has been output. Some machines may let | |
1252 | you get away with leaving out some of the gdb_flush, but not all. */ | |
1253 | wrap_here (""); | |
1254 | gdb_flush (gdb_stdout); | |
1255 | gdb_flush (gdb_stderr); | |
1256 | ||
1257 | if (source_file_name != NULL) | |
1258 | { | |
1259 | ++source_line_number; | |
1260 | sprintf (source_error, | |
1261 | "%s%s:%d: Error in sourced command file:\n", | |
1262 | source_pre_error, | |
1263 | source_file_name, | |
1264 | source_line_number); | |
1265 | error_pre_print = source_error; | |
1266 | } | |
1267 | ||
1268 | if (annotation_level > 1 && instream == stdin) | |
1269 | { | |
1270 | printf_unfiltered ("\n\032\032pre-"); | |
1271 | printf_unfiltered (annotation_suffix); | |
1272 | printf_unfiltered ("\n"); | |
1273 | } | |
1274 | ||
1275 | /* Don't use fancy stuff if not talking to stdin. */ | |
1276 | if (command_editing_p && instream == stdin | |
1277 | && ISATTY (instream)) | |
1278 | rl = readline (local_prompt); | |
1279 | else | |
1280 | rl = gdb_readline (local_prompt); | |
1281 | ||
1282 | if (annotation_level > 1 && instream == stdin) | |
1283 | { | |
1284 | printf_unfiltered ("\n\032\032post-"); | |
1285 | printf_unfiltered (annotation_suffix); | |
1286 | printf_unfiltered ("\n"); | |
1287 | } | |
1288 | ||
1289 | if (!rl || rl == (char *) EOF) | |
1290 | { | |
1291 | got_eof = 1; | |
1292 | break; | |
1293 | } | |
1294 | if (strlen(rl) + 1 + (p - linebuffer) > linelength) | |
1295 | { | |
1296 | linelength = strlen(rl) + 1 + (p - linebuffer); | |
1297 | nline = (char *) xrealloc (linebuffer, linelength); | |
1298 | p += nline - linebuffer; | |
1299 | linebuffer = nline; | |
1300 | } | |
1301 | p1 = rl; | |
1302 | /* Copy line. Don't copy null at end. (Leaves line alone | |
1303 | if this was just a newline) */ | |
1304 | while (*p1) | |
1305 | *p++ = *p1++; | |
1306 | ||
1307 | free (rl); /* Allocated in readline. */ | |
1308 | ||
1309 | if (p == linebuffer || *(p - 1) != '\\') | |
1310 | break; | |
1311 | ||
1312 | p--; /* Put on top of '\'. */ | |
1313 | local_prompt = (char *) 0; | |
1314 | } | |
1315 | ||
1316 | #ifdef STOP_SIGNAL | |
1317 | if (job_control) | |
1318 | signal (STOP_SIGNAL, SIG_DFL); | |
1319 | #endif | |
1320 | immediate_quit--; | |
1321 | ||
1322 | if (got_eof) | |
1323 | return NULL; | |
1324 | ||
1325 | #define SERVER_COMMAND_LENGTH 7 | |
1326 | server_command = | |
1327 | (p - linebuffer > SERVER_COMMAND_LENGTH) | |
1328 | && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH); | |
1329 | if (server_command) | |
1330 | { | |
1331 | /* Note that we don't set `line'. Between this and the check in | |
1332 | dont_repeat, this insures that repeating will still do the | |
1333 | right thing. */ | |
1334 | *p = '\0'; | |
1335 | return linebuffer + SERVER_COMMAND_LENGTH; | |
1336 | } | |
1337 | ||
1338 | /* Do history expansion if that is wished. */ | |
1339 | if (history_expansion_p && instream == stdin | |
1340 | && ISATTY (instream)) | |
1341 | { | |
1342 | char *history_value; | |
1343 | int expanded; | |
1344 | ||
1345 | *p = '\0'; /* Insert null now. */ | |
1346 | expanded = history_expand (linebuffer, &history_value); | |
1347 | if (expanded) | |
1348 | { | |
1349 | /* Print the changes. */ | |
1350 | printf_unfiltered ("%s\n", history_value); | |
1351 | ||
1352 | /* If there was an error, call this function again. */ | |
1353 | if (expanded < 0) | |
1354 | { | |
1355 | free (history_value); | |
1356 | return command_line_input (prrompt, repeat, annotation_suffix); | |
1357 | } | |
1358 | if (strlen (history_value) > linelength) | |
1359 | { | |
1360 | linelength = strlen (history_value) + 1; | |
1361 | linebuffer = (char *) xrealloc (linebuffer, linelength); | |
1362 | } | |
1363 | strcpy (linebuffer, history_value); | |
1364 | p = linebuffer + strlen(linebuffer); | |
1365 | free (history_value); | |
1366 | } | |
1367 | } | |
1368 | ||
1369 | /* If we just got an empty line, and that is supposed | |
1370 | to repeat the previous command, return the value in the | |
1371 | global buffer. */ | |
1372 | if (repeat) | |
1373 | { | |
1374 | if (p == linebuffer) | |
1375 | return line; | |
1376 | p1 = linebuffer; | |
1377 | while (*p1 == ' ' || *p1 == '\t') | |
1378 | p1++; | |
1379 | if (!*p1) | |
1380 | return line; | |
1381 | } | |
1382 | ||
1383 | *p = 0; | |
1384 | ||
1385 | /* Add line to history if appropriate. */ | |
1386 | if (instream == stdin | |
1387 | && ISATTY (stdin) && *linebuffer) | |
1388 | add_history (linebuffer); | |
1389 | ||
1390 | /* Note: lines consisting solely of comments are added to the command | |
1391 | history. This is useful when you type a command, and then | |
1392 | realize you don't want to execute it quite yet. You can comment | |
1393 | out the command and then later fetch it from the value history | |
1394 | and remove the '#'. The kill ring is probably better, but some | |
1395 | people are in the habit of commenting things out. */ | |
1396 | p1 = linebuffer; | |
1397 | while ((c = *p1++) != '\0') | |
1398 | { | |
1399 | if (c == '"') | |
1400 | while ((c = *p1++) != '"') | |
1401 | { | |
1402 | /* Make sure an escaped '"' doesn't make us think the string | |
1403 | is ended. */ | |
1404 | if (c == '\\') | |
1405 | parse_escape (&p1); | |
1406 | if (c == '\0') | |
1407 | break; | |
1408 | } | |
1409 | else if (c == '\'') | |
1410 | while ((c = *p1++) != '\'') | |
1411 | { | |
1412 | /* Make sure an escaped '\'' doesn't make us think the string | |
1413 | is ended. */ | |
1414 | if (c == '\\') | |
1415 | parse_escape (&p1); | |
1416 | if (c == '\0') | |
1417 | break; | |
1418 | } | |
1419 | else if (c == '#') | |
1420 | { | |
1421 | /* Found a comment. */ | |
1422 | p1[-1] = '\0'; | |
1423 | break; | |
1424 | } | |
1425 | } | |
1426 | ||
1427 | /* Save into global buffer if appropriate. */ | |
1428 | if (repeat) | |
1429 | { | |
1430 | if (linelength > linesize) | |
1431 | { | |
1432 | line = xrealloc (line, linelength); | |
1433 | linesize = linelength; | |
1434 | } | |
1435 | strcpy (line, linebuffer); | |
1436 | return line; | |
1437 | } | |
1438 | ||
1439 | return linebuffer; | |
1440 | } | |
1441 | \f | |
1442 | /* Read lines from the input stream | |
1443 | and accumulate them in a chain of struct command_line's | |
1444 | which is then returned. */ | |
1445 | ||
1446 | struct command_line * | |
1447 | read_command_lines () | |
1448 | { | |
1449 | struct command_line *first = 0; | |
1450 | register struct command_line *next, *tail = 0; | |
1451 | register char *p, *p1; | |
1452 | struct cleanup *old_chain = 0; | |
1453 | ||
1454 | while (1) | |
1455 | { | |
1456 | dont_repeat (); | |
1457 | p = command_line_input ((char *) NULL, instream == stdin, "commands"); | |
1458 | if (p == NULL) | |
1459 | /* Treat end of file like "end". */ | |
1460 | break; | |
1461 | ||
1462 | /* Remove leading and trailing blanks. */ | |
1463 | while (*p == ' ' || *p == '\t') p++; | |
1464 | p1 = p + strlen (p); | |
1465 | while (p1 != p && (p1[-1] == ' ' || p1[-1] == '\t')) p1--; | |
1466 | ||
1467 | /* Is this "end"? */ | |
1468 | if (p1 - p == 3 && !strncmp (p, "end", 3)) | |
1469 | break; | |
1470 | ||
1471 | /* No => add this line to the chain of command lines. */ | |
1472 | next = (struct command_line *) xmalloc (sizeof (struct command_line)); | |
1473 | next->line = savestring (p, p1 - p); | |
1474 | next->next = 0; | |
1475 | if (tail) | |
1476 | { | |
1477 | tail->next = next; | |
1478 | } | |
1479 | else | |
1480 | { | |
1481 | /* We just read the first line. | |
1482 | From now on, arrange to throw away the lines we have | |
1483 | if we quit or get an error while inside this function. */ | |
1484 | first = next; | |
1485 | old_chain = make_cleanup (free_command_lines, &first); | |
1486 | } | |
1487 | tail = next; | |
1488 | } | |
1489 | ||
1490 | dont_repeat (); | |
1491 | ||
1492 | /* Now we are about to return the chain to our caller, | |
1493 | so freeing it becomes his responsibility. */ | |
1494 | if (first) | |
1495 | discard_cleanups (old_chain); | |
1496 | return first; | |
1497 | } | |
1498 | ||
1499 | /* Free a chain of struct command_line's. */ | |
1500 | ||
1501 | void | |
1502 | free_command_lines (lptr) | |
1503 | struct command_line **lptr; | |
1504 | { | |
1505 | register struct command_line *l = *lptr; | |
1506 | register struct command_line *next; | |
1507 | ||
1508 | while (l) | |
1509 | { | |
1510 | next = l->next; | |
1511 | free (l->line); | |
1512 | free ((PTR)l); | |
1513 | l = next; | |
1514 | } | |
1515 | } | |
1516 | \f | |
1517 | /* Add an element to the list of info subcommands. */ | |
1518 | ||
1519 | void | |
1520 | add_info (name, fun, doc) | |
1521 | char *name; | |
1522 | void (*fun) PARAMS ((char *, int)); | |
1523 | char *doc; | |
1524 | { | |
1525 | add_cmd (name, no_class, fun, doc, &infolist); | |
1526 | } | |
1527 | ||
1528 | /* Add an alias to the list of info subcommands. */ | |
1529 | ||
1530 | void | |
1531 | add_info_alias (name, oldname, abbrev_flag) | |
1532 | char *name; | |
1533 | char *oldname; | |
1534 | int abbrev_flag; | |
1535 | { | |
1536 | add_alias_cmd (name, oldname, 0, abbrev_flag, &infolist); | |
1537 | } | |
1538 | ||
1539 | /* The "info" command is defined as a prefix, with allow_unknown = 0. | |
1540 | Therefore, its own definition is called only for "info" with no args. */ | |
1541 | ||
1542 | /* ARGSUSED */ | |
1543 | static void | |
1544 | info_command (arg, from_tty) | |
1545 | char *arg; | |
1546 | int from_tty; | |
1547 | { | |
1548 | printf_unfiltered ("\"info\" must be followed by the name of an info command.\n"); | |
1549 | help_list (infolist, "info ", -1, gdb_stdout); | |
1550 | } | |
1551 | ||
1552 | /* The "complete" command is used by Emacs to implement completion. */ | |
1553 | ||
1554 | /* ARGSUSED */ | |
1555 | static void | |
1556 | complete_command (arg, from_tty) | |
1557 | char *arg; | |
1558 | int from_tty; | |
1559 | { | |
1560 | int i; | |
1561 | char *completion; | |
1562 | ||
1563 | dont_repeat (); | |
1564 | ||
1565 | if (arg == NULL) | |
1566 | { | |
1567 | rl_line_buffer[0] = '\0'; | |
1568 | rl_point = 0; | |
1569 | } | |
1570 | else | |
1571 | { | |
1572 | strcpy (rl_line_buffer, arg); | |
1573 | rl_point = strlen (arg); | |
1574 | } | |
1575 | ||
1576 | for (completion = symbol_completion_function (rl_line_buffer, i = 0); | |
1577 | completion; | |
1578 | completion = symbol_completion_function (rl_line_buffer, ++i)) | |
1579 | printf_unfiltered ("%s\n", completion); | |
1580 | } | |
1581 | ||
1582 | /* The "show" command with no arguments shows all the settings. */ | |
1583 | ||
1584 | /* ARGSUSED */ | |
1585 | static void | |
1586 | show_command (arg, from_tty) | |
1587 | char *arg; | |
1588 | int from_tty; | |
1589 | { | |
1590 | cmd_show_list (showlist, from_tty, ""); | |
1591 | } | |
1592 | \f | |
1593 | /* Add an element to the list of commands. */ | |
1594 | ||
1595 | void | |
1596 | add_com (name, class, fun, doc) | |
1597 | char *name; | |
1598 | enum command_class class; | |
1599 | void (*fun) PARAMS ((char *, int)); | |
1600 | char *doc; | |
1601 | { | |
1602 | add_cmd (name, class, fun, doc, &cmdlist); | |
1603 | } | |
1604 | ||
1605 | /* Add an alias or abbreviation command to the list of commands. */ | |
1606 | ||
1607 | void | |
1608 | add_com_alias (name, oldname, class, abbrev_flag) | |
1609 | char *name; | |
1610 | char *oldname; | |
1611 | enum command_class class; | |
1612 | int abbrev_flag; | |
1613 | { | |
1614 | add_alias_cmd (name, oldname, class, abbrev_flag, &cmdlist); | |
1615 | } | |
1616 | ||
1617 | void | |
1618 | error_no_arg (why) | |
1619 | char *why; | |
1620 | { | |
1621 | error ("Argument required (%s).", why); | |
1622 | } | |
1623 | ||
1624 | /* ARGSUSED */ | |
1625 | static void | |
1626 | help_command (command, from_tty) | |
1627 | char *command; | |
1628 | int from_tty; /* Ignored */ | |
1629 | { | |
1630 | help_cmd (command, gdb_stdout); | |
1631 | } | |
1632 | \f | |
1633 | static void | |
1634 | validate_comname (comname) | |
1635 | char *comname; | |
1636 | { | |
1637 | register char *p; | |
1638 | ||
1639 | if (comname == 0) | |
1640 | error_no_arg ("name of command to define"); | |
1641 | ||
1642 | p = comname; | |
1643 | while (*p) | |
1644 | { | |
1645 | if (!isalnum(*p) && *p != '-') | |
1646 | error ("Junk in argument list: \"%s\"", p); | |
1647 | p++; | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | /* This is just a placeholder in the command data structures. */ | |
1652 | static void | |
1653 | user_defined_command (ignore, from_tty) | |
1654 | char *ignore; | |
1655 | int from_tty; | |
1656 | { | |
1657 | } | |
1658 | ||
1659 | static void | |
1660 | define_command (comname, from_tty) | |
1661 | char *comname; | |
1662 | int from_tty; | |
1663 | { | |
1664 | register struct command_line *cmds; | |
1665 | register struct cmd_list_element *c, *newc, *hookc = 0; | |
1666 | char *tem = comname; | |
1667 | #define HOOK_STRING "hook-" | |
1668 | #define HOOK_LEN 5 | |
1669 | ||
1670 | validate_comname (comname); | |
1671 | ||
1672 | /* Look it up, and verify that we got an exact match. */ | |
1673 | c = lookup_cmd (&tem, cmdlist, "", -1, 1); | |
1674 | if (c && !STREQ (comname, c->name)) | |
1675 | c = 0; | |
1676 | ||
1677 | if (c) | |
1678 | { | |
1679 | if (c->class == class_user || c->class == class_alias) | |
1680 | tem = "Redefine command \"%s\"? "; | |
1681 | else | |
1682 | tem = "Really redefine built-in command \"%s\"? "; | |
1683 | if (!query (tem, c->name)) | |
1684 | error ("Command \"%s\" not redefined.", c->name); | |
1685 | } | |
1686 | ||
1687 | /* If this new command is a hook, then mark the command which it | |
1688 | is hooking. Note that we allow hooking `help' commands, so that | |
1689 | we can hook the `stop' pseudo-command. */ | |
1690 | ||
1691 | if (!strncmp (comname, HOOK_STRING, HOOK_LEN)) | |
1692 | { | |
1693 | /* Look up cmd it hooks, and verify that we got an exact match. */ | |
1694 | tem = comname+HOOK_LEN; | |
1695 | hookc = lookup_cmd (&tem, cmdlist, "", -1, 0); | |
1696 | if (hookc && !STREQ (comname+HOOK_LEN, hookc->name)) | |
1697 | hookc = 0; | |
1698 | if (!hookc) | |
1699 | { | |
1700 | warning ("Your new `%s' command does not hook any existing command.", | |
1701 | comname); | |
1702 | if (!query ("Proceed? ", (char *)0)) | |
1703 | error ("Not confirmed."); | |
1704 | } | |
1705 | } | |
1706 | ||
1707 | comname = savestring (comname, strlen (comname)); | |
1708 | ||
1709 | /* If the rest of the commands will be case insensitive, this one | |
1710 | should behave in the same manner. */ | |
1711 | for (tem = comname; *tem; tem++) | |
1712 | if (isupper(*tem)) *tem = tolower(*tem); | |
1713 | ||
1714 | if (from_tty) | |
1715 | { | |
1716 | printf_unfiltered ("Type commands for definition of \"%s\".\n\ | |
1717 | End with a line saying just \"end\".\n", comname); | |
1718 | gdb_flush (gdb_stdout); | |
1719 | } | |
1720 | ||
1721 | cmds = read_command_lines (); | |
1722 | ||
1723 | if (c && c->class == class_user) | |
1724 | free_command_lines (&c->user_commands); | |
1725 | ||
1726 | newc = add_cmd (comname, class_user, user_defined_command, | |
1727 | (c && c->class == class_user) | |
1728 | ? c->doc : savestring ("User-defined.", 13), &cmdlist); | |
1729 | newc->user_commands = cmds; | |
1730 | ||
1731 | /* If this new command is a hook, then mark both commands as being | |
1732 | tied. */ | |
1733 | if (hookc) | |
1734 | { | |
1735 | hookc->hook = newc; /* Target gets hooked. */ | |
1736 | newc->hookee = hookc; /* We are marked as hooking target cmd. */ | |
1737 | } | |
1738 | } | |
1739 | ||
1740 | static void | |
1741 | document_command (comname, from_tty) | |
1742 | char *comname; | |
1743 | int from_tty; | |
1744 | { | |
1745 | struct command_line *doclines; | |
1746 | register struct cmd_list_element *c; | |
1747 | char *tem = comname; | |
1748 | ||
1749 | validate_comname (comname); | |
1750 | ||
1751 | c = lookup_cmd (&tem, cmdlist, "", 0, 1); | |
1752 | ||
1753 | if (c->class != class_user) | |
1754 | error ("Command \"%s\" is built-in.", comname); | |
1755 | ||
1756 | if (from_tty) | |
1757 | printf_unfiltered ("Type documentation for \"%s\".\n\ | |
1758 | End with a line saying just \"end\".\n", comname); | |
1759 | ||
1760 | doclines = read_command_lines (); | |
1761 | ||
1762 | if (c->doc) free (c->doc); | |
1763 | ||
1764 | { | |
1765 | register struct command_line *cl1; | |
1766 | register int len = 0; | |
1767 | ||
1768 | for (cl1 = doclines; cl1; cl1 = cl1->next) | |
1769 | len += strlen (cl1->line) + 1; | |
1770 | ||
1771 | c->doc = (char *) xmalloc (len + 1); | |
1772 | *c->doc = 0; | |
1773 | ||
1774 | for (cl1 = doclines; cl1; cl1 = cl1->next) | |
1775 | { | |
1776 | strcat (c->doc, cl1->line); | |
1777 | if (cl1->next) | |
1778 | strcat (c->doc, "\n"); | |
1779 | } | |
1780 | } | |
1781 | ||
1782 | free_command_lines (&doclines); | |
1783 | } | |
1784 | \f | |
1785 | void | |
1786 | print_gnu_advertisement () | |
1787 | { | |
1788 | printf_unfiltered ("\ | |
1789 | GDB is free software and you are welcome to distribute copies of it\n\ | |
1790 | under certain conditions; type \"show copying\" to see the conditions.\n\ | |
1791 | There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\ | |
1792 | "); | |
1793 | } | |
1794 | ||
1795 | void | |
1796 | print_gdb_version (stream) | |
1797 | GDB_FILE *stream; | |
1798 | { | |
1799 | fprintf_filtered (stream, "\ | |
1800 | GDB %s (%s", version, host_name); | |
1801 | ||
1802 | if (!STREQ (host_name, target_name)) | |
1803 | fprintf_filtered (stream, " --target %s", target_name); | |
1804 | ||
1805 | fprintf_filtered (stream, "), "); | |
1806 | wrap_here(""); | |
1807 | fprintf_filtered (stream, "Copyright 1994 Free Software Foundation, Inc."); | |
1808 | } | |
1809 | ||
1810 | /* ARGSUSED */ | |
1811 | static void | |
1812 | show_version (args, from_tty) | |
1813 | char *args; | |
1814 | int from_tty; | |
1815 | { | |
1816 | immediate_quit++; | |
1817 | print_gnu_advertisement (); | |
1818 | print_gdb_version (gdb_stdout); | |
1819 | printf_filtered ("\n"); | |
1820 | immediate_quit--; | |
1821 | } | |
1822 | \f | |
1823 | /* xgdb calls this to reprint the usual GDB prompt. Obsolete now that xgdb | |
1824 | is obsolete. */ | |
1825 | ||
1826 | void | |
1827 | print_prompt () | |
1828 | { | |
1829 | printf_unfiltered ("%s", prompt); | |
1830 | gdb_flush (gdb_stdout); | |
1831 | } | |
1832 | \f | |
1833 | void | |
1834 | quit_command (args, from_tty) | |
1835 | char *args; | |
1836 | int from_tty; | |
1837 | { | |
1838 | if (inferior_pid != 0 && target_has_execution) | |
1839 | { | |
1840 | if (attach_flag) | |
1841 | { | |
1842 | if (query ("The program is running. Quit anyway (and detach it)? ")) | |
1843 | target_detach (args, from_tty); | |
1844 | else | |
1845 | error ("Not confirmed."); | |
1846 | } | |
1847 | else | |
1848 | { | |
1849 | if (query ("The program is running. Quit anyway (and kill it)? ")) | |
1850 | target_kill (); | |
1851 | else | |
1852 | error ("Not confirmed."); | |
1853 | } | |
1854 | } | |
1855 | /* UDI wants this, to kill the TIP. */ | |
1856 | target_close (1); | |
1857 | ||
1858 | /* Save the history information if it is appropriate to do so. */ | |
1859 | if (write_history_p && history_filename) | |
1860 | write_history (history_filename); | |
1861 | ||
1862 | exit (0); | |
1863 | } | |
1864 | ||
1865 | /* Returns whether GDB is running on a terminal and whether the user | |
1866 | desires that questions be asked of them on that terminal. */ | |
1867 | ||
1868 | int | |
1869 | input_from_terminal_p () | |
1870 | { | |
1871 | return gdb_has_a_terminal () && (instream == stdin) & caution; | |
1872 | } | |
1873 | \f | |
1874 | /* ARGSUSED */ | |
1875 | static void | |
1876 | pwd_command (args, from_tty) | |
1877 | char *args; | |
1878 | int from_tty; | |
1879 | { | |
1880 | if (args) error ("The \"pwd\" command does not take an argument: %s", args); | |
1881 | getcwd (dirbuf, sizeof (dirbuf)); | |
1882 | ||
1883 | if (!STREQ (dirbuf, current_directory)) | |
1884 | printf_unfiltered ("Working directory %s\n (canonically %s).\n", | |
1885 | current_directory, dirbuf); | |
1886 | else | |
1887 | printf_unfiltered ("Working directory %s.\n", current_directory); | |
1888 | } | |
1889 | ||
1890 | void | |
1891 | cd_command (dir, from_tty) | |
1892 | char *dir; | |
1893 | int from_tty; | |
1894 | { | |
1895 | int len; | |
1896 | /* Found something other than leading repetitions of "/..". */ | |
1897 | int found_real_path; | |
1898 | char *p; | |
1899 | ||
1900 | /* If the new directory is absolute, repeat is a no-op; if relative, | |
1901 | repeat might be useful but is more likely to be a mistake. */ | |
1902 | dont_repeat (); | |
1903 | ||
1904 | if (dir == 0) | |
1905 | error_no_arg ("new working directory"); | |
1906 | ||
1907 | dir = tilde_expand (dir); | |
1908 | make_cleanup (free, dir); | |
1909 | ||
1910 | if (chdir (dir) < 0) | |
1911 | perror_with_name (dir); | |
1912 | ||
1913 | len = strlen (dir); | |
1914 | dir = savestring (dir, len - (len > 1 && dir[len-1] == '/')); | |
1915 | if (dir[0] == '/') | |
1916 | current_directory = dir; | |
1917 | else | |
1918 | { | |
1919 | if (current_directory[0] == '/' && current_directory[1] == '\0') | |
1920 | current_directory = concat (current_directory, dir, NULL); | |
1921 | else | |
1922 | current_directory = concat (current_directory, "/", dir, NULL); | |
1923 | free (dir); | |
1924 | } | |
1925 | ||
1926 | /* Now simplify any occurrences of `.' and `..' in the pathname. */ | |
1927 | ||
1928 | found_real_path = 0; | |
1929 | for (p = current_directory; *p;) | |
1930 | { | |
1931 | if (p[0] == '/' && p[1] == '.' && (p[2] == 0 || p[2] == '/')) | |
1932 | strcpy (p, p + 2); | |
1933 | else if (p[0] == '/' && p[1] == '.' && p[2] == '.' | |
1934 | && (p[3] == 0 || p[3] == '/')) | |
1935 | { | |
1936 | if (found_real_path) | |
1937 | { | |
1938 | /* Search backwards for the directory just before the "/.." | |
1939 | and obliterate it and the "/..". */ | |
1940 | char *q = p; | |
1941 | while (q != current_directory && q[-1] != '/') | |
1942 | --q; | |
1943 | ||
1944 | if (q == current_directory) | |
1945 | /* current_directory is | |
1946 | a relative pathname ("can't happen"--leave it alone). */ | |
1947 | ++p; | |
1948 | else | |
1949 | { | |
1950 | strcpy (q - 1, p + 3); | |
1951 | p = q - 1; | |
1952 | } | |
1953 | } | |
1954 | else | |
1955 | /* We are dealing with leading repetitions of "/..", for example | |
1956 | "/../..", which is the Mach super-root. */ | |
1957 | p += 3; | |
1958 | } | |
1959 | else | |
1960 | { | |
1961 | found_real_path = 1; | |
1962 | ++p; | |
1963 | } | |
1964 | } | |
1965 | ||
1966 | forget_cached_source_info (); | |
1967 | ||
1968 | if (from_tty) | |
1969 | pwd_command ((char *) 0, 1); | |
1970 | } | |
1971 | \f | |
1972 | struct source_cleanup_lines_args { | |
1973 | int old_line; | |
1974 | char *old_file; | |
1975 | char *old_pre_error; | |
1976 | char *old_error_pre_print; | |
1977 | }; | |
1978 | ||
1979 | static void | |
1980 | source_cleanup_lines (args) | |
1981 | PTR args; | |
1982 | { | |
1983 | struct source_cleanup_lines_args *p = | |
1984 | (struct source_cleanup_lines_args *)args; | |
1985 | source_line_number = p->old_line; | |
1986 | source_file_name = p->old_file; | |
1987 | source_pre_error = p->old_pre_error; | |
1988 | error_pre_print = p->old_error_pre_print; | |
1989 | } | |
1990 | ||
1991 | /* ARGSUSED */ | |
1992 | void | |
1993 | source_command (args, from_tty) | |
1994 | char *args; | |
1995 | int from_tty; | |
1996 | { | |
1997 | FILE *stream; | |
1998 | struct cleanup *old_cleanups; | |
1999 | char *file = args; | |
2000 | struct source_cleanup_lines_args old_lines; | |
2001 | int needed_length; | |
2002 | ||
2003 | if (file == NULL) | |
2004 | { | |
2005 | error ("source command requires pathname of file to source."); | |
2006 | } | |
2007 | ||
2008 | file = tilde_expand (file); | |
2009 | old_cleanups = make_cleanup (free, file); | |
2010 | ||
2011 | stream = fopen (file, FOPEN_RT); | |
2012 | if (stream == 0) | |
2013 | perror_with_name (file); | |
2014 | ||
2015 | make_cleanup (fclose, stream); | |
2016 | ||
2017 | old_lines.old_line = source_line_number; | |
2018 | old_lines.old_file = source_file_name; | |
2019 | old_lines.old_pre_error = source_pre_error; | |
2020 | old_lines.old_error_pre_print = error_pre_print; | |
2021 | make_cleanup (source_cleanup_lines, &old_lines); | |
2022 | source_line_number = 0; | |
2023 | source_file_name = file; | |
2024 | source_pre_error = error_pre_print == NULL ? "" : error_pre_print; | |
2025 | source_pre_error = savestring (source_pre_error, strlen (source_pre_error)); | |
2026 | make_cleanup (free, source_pre_error); | |
2027 | /* This will get set every time we read a line. So it won't stay "" for | |
2028 | long. */ | |
2029 | error_pre_print = ""; | |
2030 | ||
2031 | needed_length = strlen (source_file_name) + strlen (source_pre_error) + 80; | |
2032 | if (source_error_allocated < needed_length) | |
2033 | { | |
2034 | source_error_allocated *= 2; | |
2035 | if (source_error_allocated < needed_length) | |
2036 | source_error_allocated = needed_length; | |
2037 | if (source_error == NULL) | |
2038 | source_error = xmalloc (source_error_allocated); | |
2039 | else | |
2040 | source_error = xrealloc (source_error, source_error_allocated); | |
2041 | } | |
2042 | ||
2043 | read_command_file (stream); | |
2044 | ||
2045 | do_cleanups (old_cleanups); | |
2046 | } | |
2047 | ||
2048 | /* ARGSUSED */ | |
2049 | static void | |
2050 | echo_command (text, from_tty) | |
2051 | char *text; | |
2052 | int from_tty; | |
2053 | { | |
2054 | char *p = text; | |
2055 | register int c; | |
2056 | ||
2057 | if (text) | |
2058 | while ((c = *p++) != '\0') | |
2059 | { | |
2060 | if (c == '\\') | |
2061 | { | |
2062 | /* \ at end of argument is used after spaces | |
2063 | so they won't be lost. */ | |
2064 | if (*p == 0) | |
2065 | return; | |
2066 | ||
2067 | c = parse_escape (&p); | |
2068 | if (c >= 0) | |
2069 | printf_filtered ("%c", c); | |
2070 | } | |
2071 | else | |
2072 | printf_filtered ("%c", c); | |
2073 | } | |
2074 | ||
2075 | /* Force this output to appear now. */ | |
2076 | wrap_here (""); | |
2077 | gdb_flush (gdb_stdout); | |
2078 | } | |
2079 | ||
2080 | \f | |
2081 | /* Functions to manipulate command line editing control variables. */ | |
2082 | ||
2083 | /* Number of commands to print in each call to show_commands. */ | |
2084 | #define Hist_print 10 | |
2085 | static void | |
2086 | show_commands (args, from_tty) | |
2087 | char *args; | |
2088 | int from_tty; | |
2089 | { | |
2090 | /* Index for history commands. Relative to history_base. */ | |
2091 | int offset; | |
2092 | ||
2093 | /* Number of the history entry which we are planning to display next. | |
2094 | Relative to history_base. */ | |
2095 | static int num = 0; | |
2096 | ||
2097 | /* The first command in the history which doesn't exist (i.e. one more | |
2098 | than the number of the last command). Relative to history_base. */ | |
2099 | int hist_len; | |
2100 | ||
2101 | extern HIST_ENTRY *history_get PARAMS ((int)); | |
2102 | ||
2103 | /* Print out some of the commands from the command history. */ | |
2104 | /* First determine the length of the history list. */ | |
2105 | hist_len = history_size; | |
2106 | for (offset = 0; offset < history_size; offset++) | |
2107 | { | |
2108 | if (!history_get (history_base + offset)) | |
2109 | { | |
2110 | hist_len = offset; | |
2111 | break; | |
2112 | } | |
2113 | } | |
2114 | ||
2115 | if (args) | |
2116 | { | |
2117 | if (args[0] == '+' && args[1] == '\0') | |
2118 | /* "info editing +" should print from the stored position. */ | |
2119 | ; | |
2120 | else | |
2121 | /* "info editing <exp>" should print around command number <exp>. */ | |
2122 | num = (parse_and_eval_address (args) - history_base) - Hist_print / 2; | |
2123 | } | |
2124 | /* "show commands" means print the last Hist_print commands. */ | |
2125 | else | |
2126 | { | |
2127 | num = hist_len - Hist_print; | |
2128 | } | |
2129 | ||
2130 | if (num < 0) | |
2131 | num = 0; | |
2132 | ||
2133 | /* If there are at least Hist_print commands, we want to display the last | |
2134 | Hist_print rather than, say, the last 6. */ | |
2135 | if (hist_len - num < Hist_print) | |
2136 | { | |
2137 | num = hist_len - Hist_print; | |
2138 | if (num < 0) | |
2139 | num = 0; | |
2140 | } | |
2141 | ||
2142 | for (offset = num; offset < num + Hist_print && offset < hist_len; offset++) | |
2143 | { | |
2144 | printf_filtered ("%5d %s\n", history_base + offset, | |
2145 | (history_get (history_base + offset))->line); | |
2146 | } | |
2147 | ||
2148 | /* The next command we want to display is the next one that we haven't | |
2149 | displayed yet. */ | |
2150 | num += Hist_print; | |
2151 | ||
2152 | /* If the user repeats this command with return, it should do what | |
2153 | "show commands +" does. This is unnecessary if arg is null, | |
2154 | because "show commands +" is not useful after "show commands". */ | |
2155 | if (from_tty && args) | |
2156 | { | |
2157 | args[0] = '+'; | |
2158 | args[1] = '\0'; | |
2159 | } | |
2160 | } | |
2161 | ||
2162 | /* Called by do_setshow_command. */ | |
2163 | /* ARGSUSED */ | |
2164 | static void | |
2165 | set_history_size_command (args, from_tty, c) | |
2166 | char *args; | |
2167 | int from_tty; | |
2168 | struct cmd_list_element *c; | |
2169 | { | |
2170 | if (history_size == INT_MAX) | |
2171 | unstifle_history (); | |
2172 | else if (history_size >= 0) | |
2173 | stifle_history (history_size); | |
2174 | else | |
2175 | { | |
2176 | history_size = INT_MAX; | |
2177 | error ("History size must be non-negative"); | |
2178 | } | |
2179 | } | |
2180 | ||
2181 | /* ARGSUSED */ | |
2182 | static void | |
2183 | set_history (args, from_tty) | |
2184 | char *args; | |
2185 | int from_tty; | |
2186 | { | |
2187 | printf_unfiltered ("\"set history\" must be followed by the name of a history subcommand.\n"); | |
2188 | help_list (sethistlist, "set history ", -1, gdb_stdout); | |
2189 | } | |
2190 | ||
2191 | /* ARGSUSED */ | |
2192 | static void | |
2193 | show_history (args, from_tty) | |
2194 | char *args; | |
2195 | int from_tty; | |
2196 | { | |
2197 | cmd_show_list (showhistlist, from_tty, ""); | |
2198 | } | |
2199 | ||
2200 | int info_verbose = 0; /* Default verbose msgs off */ | |
2201 | ||
2202 | /* Called by do_setshow_command. An elaborate joke. */ | |
2203 | /* ARGSUSED */ | |
2204 | static void | |
2205 | set_verbose (args, from_tty, c) | |
2206 | char *args; | |
2207 | int from_tty; | |
2208 | struct cmd_list_element *c; | |
2209 | { | |
2210 | char *cmdname = "verbose"; | |
2211 | struct cmd_list_element *showcmd; | |
2212 | ||
2213 | showcmd = lookup_cmd_1 (&cmdname, showlist, NULL, 1); | |
2214 | ||
2215 | if (info_verbose) | |
2216 | { | |
2217 | c->doc = "Set verbose printing of informational messages."; | |
2218 | showcmd->doc = "Show verbose printing of informational messages."; | |
2219 | } | |
2220 | else | |
2221 | { | |
2222 | c->doc = "Set verbosity."; | |
2223 | showcmd->doc = "Show verbosity."; | |
2224 | } | |
2225 | } | |
2226 | ||
2227 | static void | |
2228 | float_handler (signo) | |
2229 | int signo; | |
2230 | { | |
2231 | /* This message is based on ANSI C, section 4.7. Note that integer | |
2232 | divide by zero causes this, so "float" is a misnomer. */ | |
2233 | signal (SIGFPE, float_handler); | |
2234 | error ("Erroneous arithmetic operation."); | |
2235 | } | |
2236 | ||
2237 | /* Return whether we are running a batch file or from terminal. */ | |
2238 | int | |
2239 | batch_mode () | |
2240 | { | |
2241 | return !(instream == stdin && ISATTY (stdin)); | |
2242 | } | |
2243 | ||
2244 | \f | |
2245 | static void | |
2246 | init_cmd_lists () | |
2247 | { | |
2248 | cmdlist = NULL; | |
2249 | infolist = NULL; | |
2250 | enablelist = NULL; | |
2251 | disablelist = NULL; | |
2252 | deletelist = NULL; | |
2253 | enablebreaklist = NULL; | |
2254 | setlist = NULL; | |
2255 | unsetlist = NULL; | |
2256 | showlist = NULL; | |
2257 | sethistlist = NULL; | |
2258 | showhistlist = NULL; | |
2259 | unsethistlist = NULL; | |
2260 | #if MAINTENANCE_CMDS | |
2261 | maintenancelist = NULL; | |
2262 | maintenanceinfolist = NULL; | |
2263 | maintenanceprintlist = NULL; | |
2264 | #endif | |
2265 | setprintlist = NULL; | |
2266 | showprintlist = NULL; | |
2267 | setchecklist = NULL; | |
2268 | showchecklist = NULL; | |
2269 | } | |
2270 | ||
2271 | /* Init the history buffer. Note that we are called after the init file(s) | |
2272 | * have been read so that the user can change the history file via his | |
2273 | * .gdbinit file (for instance). The GDBHISTFILE environment variable | |
2274 | * overrides all of this. | |
2275 | */ | |
2276 | ||
2277 | void | |
2278 | init_history() | |
2279 | { | |
2280 | char *tmpenv; | |
2281 | ||
2282 | tmpenv = getenv ("HISTSIZE"); | |
2283 | if (tmpenv) | |
2284 | history_size = atoi (tmpenv); | |
2285 | else if (!history_size) | |
2286 | history_size = 256; | |
2287 | ||
2288 | stifle_history (history_size); | |
2289 | ||
2290 | tmpenv = getenv ("GDBHISTFILE"); | |
2291 | if (tmpenv) | |
2292 | history_filename = savestring (tmpenv, strlen(tmpenv)); | |
2293 | else if (!history_filename) { | |
2294 | /* We include the current directory so that if the user changes | |
2295 | directories the file written will be the same as the one | |
2296 | that was read. */ | |
2297 | history_filename = concat (current_directory, "/.gdb_history", NULL); | |
2298 | } | |
2299 | read_history (history_filename); | |
2300 | } | |
2301 | ||
2302 | static void | |
2303 | init_main () | |
2304 | { | |
2305 | struct cmd_list_element *c; | |
2306 | ||
2307 | #ifdef DEFAULT_PROMPT | |
2308 | prompt = savestring (DEFAULT_PROMPT, strlen(DEFAULT_PROMPT)); | |
2309 | #else | |
2310 | prompt = savestring ("(gdb) ", 6); | |
2311 | #endif | |
2312 | ||
2313 | /* Set the important stuff up for command editing. */ | |
2314 | command_editing_p = 1; | |
2315 | history_expansion_p = 0; | |
2316 | write_history_p = 0; | |
2317 | ||
2318 | /* Setup important stuff for command line editing. */ | |
2319 | rl_completion_entry_function = (int (*)()) symbol_completion_function; | |
2320 | rl_completer_word_break_characters = gdb_completer_word_break_characters; | |
2321 | rl_completer_quote_characters = gdb_completer_quote_characters; | |
2322 | rl_readline_name = "gdb"; | |
2323 | ||
2324 | /* Define the classes of commands. | |
2325 | They will appear in the help list in the reverse of this order. */ | |
2326 | ||
2327 | add_cmd ("internals", class_maintenance, NO_FUNCTION, | |
2328 | "Maintenance commands.\n\ | |
2329 | Some gdb commands are provided just for use by gdb maintainers.\n\ | |
2330 | These commands are subject to frequent change, and may not be as\n\ | |
2331 | well documented as user commands.", | |
2332 | &cmdlist); | |
2333 | add_cmd ("obscure", class_obscure, NO_FUNCTION, "Obscure features.", &cmdlist); | |
2334 | add_cmd ("aliases", class_alias, NO_FUNCTION, "Aliases of other commands.", &cmdlist); | |
2335 | add_cmd ("user-defined", class_user, NO_FUNCTION, "User-defined commands.\n\ | |
2336 | The commands in this class are those defined by the user.\n\ | |
2337 | Use the \"define\" command to define a command.", &cmdlist); | |
2338 | add_cmd ("support", class_support, NO_FUNCTION, "Support facilities.", &cmdlist); | |
2339 | add_cmd ("status", class_info, NO_FUNCTION, "Status inquiries.", &cmdlist); | |
2340 | add_cmd ("files", class_files, NO_FUNCTION, "Specifying and examining files.", &cmdlist); | |
2341 | add_cmd ("breakpoints", class_breakpoint, NO_FUNCTION, "Making program stop at certain points.", &cmdlist); | |
2342 | add_cmd ("data", class_vars, NO_FUNCTION, "Examining data.", &cmdlist); | |
2343 | add_cmd ("stack", class_stack, NO_FUNCTION, "Examining the stack.\n\ | |
2344 | The stack is made up of stack frames. Gdb assigns numbers to stack frames\n\ | |
2345 | counting from zero for the innermost (currently executing) frame.\n\n\ | |
2346 | At any time gdb identifies one frame as the \"selected\" frame.\n\ | |
2347 | Variable lookups are done with respect to the selected frame.\n\ | |
2348 | When the program being debugged stops, gdb selects the innermost frame.\n\ | |
2349 | The commands below can be used to select other frames by number or address.", | |
2350 | &cmdlist); | |
2351 | add_cmd ("running", class_run, NO_FUNCTION, "Running the program.", &cmdlist); | |
2352 | ||
2353 | add_com ("pwd", class_files, pwd_command, | |
2354 | "Print working directory. This is used for your program as well."); | |
2355 | c = add_cmd ("cd", class_files, cd_command, | |
2356 | "Set working directory to DIR for debugger and program being debugged.\n\ | |
2357 | The change does not take effect for the program being debugged\n\ | |
2358 | until the next time it is started.", &cmdlist); | |
2359 | c->completer = filename_completer; | |
2360 | ||
2361 | add_show_from_set | |
2362 | (add_set_cmd ("prompt", class_support, var_string, (char *)&prompt, | |
2363 | "Set gdb's prompt", | |
2364 | &setlist), | |
2365 | &showlist); | |
2366 | ||
2367 | add_com ("echo", class_support, echo_command, | |
2368 | "Print a constant string. Give string as argument.\n\ | |
2369 | C escape sequences may be used in the argument.\n\ | |
2370 | No newline is added at the end of the argument;\n\ | |
2371 | use \"\\n\" if you want a newline to be printed.\n\ | |
2372 | Since leading and trailing whitespace are ignored in command arguments,\n\ | |
2373 | if you want to print some you must use \"\\\" before leading whitespace\n\ | |
2374 | to be printed or after trailing whitespace."); | |
2375 | add_com ("document", class_support, document_command, | |
2376 | "Document a user-defined command.\n\ | |
2377 | Give command name as argument. Give documentation on following lines.\n\ | |
2378 | End with a line of just \"end\"."); | |
2379 | add_com ("define", class_support, define_command, | |
2380 | "Define a new command name. Command name is argument.\n\ | |
2381 | Definition appears on following lines, one command per line.\n\ | |
2382 | End with a line of just \"end\".\n\ | |
2383 | Use the \"document\" command to give documentation for the new command.\n\ | |
2384 | Commands defined in this way do not take arguments."); | |
2385 | ||
2386 | #ifdef __STDC__ | |
2387 | c = add_cmd ("source", class_support, source_command, | |
2388 | "Read commands from a file named FILE.\n\ | |
2389 | Note that the file \"" GDBINIT_FILENAME "\" is read automatically in this way\n\ | |
2390 | when gdb is started.", &cmdlist); | |
2391 | #else | |
2392 | /* Punt file name, we can't help it easily. */ | |
2393 | c = add_cmd ("source", class_support, source_command, | |
2394 | "Read commands from a file named FILE.\n\ | |
2395 | Note that the file \".gdbinit\" is read automatically in this way\n\ | |
2396 | when gdb is started.", &cmdlist); | |
2397 | #endif | |
2398 | c->completer = filename_completer; | |
2399 | ||
2400 | add_com ("quit", class_support, quit_command, "Exit gdb."); | |
2401 | add_com ("help", class_support, help_command, "Print list of commands."); | |
2402 | add_com_alias ("q", "quit", class_support, 1); | |
2403 | add_com_alias ("h", "help", class_support, 1); | |
2404 | ||
2405 | ||
2406 | c = add_set_cmd ("verbose", class_support, var_boolean, (char *)&info_verbose, | |
2407 | "Set ", | |
2408 | &setlist), | |
2409 | add_show_from_set (c, &showlist); | |
2410 | c->function.sfunc = set_verbose; | |
2411 | set_verbose (NULL, 0, c); | |
2412 | ||
2413 | add_show_from_set | |
2414 | (add_set_cmd ("editing", class_support, var_boolean, (char *)&command_editing_p, | |
2415 | "Set editing of command lines as they are typed.\n\ | |
2416 | Use \"on\" to enable to enable the editing, and \"off\" to disable it.\n\ | |
2417 | Without an argument, command line editing is enabled. To edit, use\n\ | |
2418 | EMACS-like or VI-like commands like control-P or ESC.", &setlist), | |
2419 | &showlist); | |
2420 | ||
2421 | add_prefix_cmd ("history", class_support, set_history, | |
2422 | "Generic command for setting command history parameters.", | |
2423 | &sethistlist, "set history ", 0, &setlist); | |
2424 | add_prefix_cmd ("history", class_support, show_history, | |
2425 | "Generic command for showing command history parameters.", | |
2426 | &showhistlist, "show history ", 0, &showlist); | |
2427 | ||
2428 | add_show_from_set | |
2429 | (add_set_cmd ("expansion", no_class, var_boolean, (char *)&history_expansion_p, | |
2430 | "Set history expansion on command input.\n\ | |
2431 | Without an argument, history expansion is enabled.", &sethistlist), | |
2432 | &showhistlist); | |
2433 | ||
2434 | add_show_from_set | |
2435 | (add_set_cmd ("save", no_class, var_boolean, (char *)&write_history_p, | |
2436 | "Set saving of the history record on exit.\n\ | |
2437 | Use \"on\" to enable to enable the saving, and \"off\" to disable it.\n\ | |
2438 | Without an argument, saving is enabled.", &sethistlist), | |
2439 | &showhistlist); | |
2440 | ||
2441 | c = add_set_cmd ("size", no_class, var_integer, (char *)&history_size, | |
2442 | "Set the size of the command history, \n\ | |
2443 | ie. the number of previous commands to keep a record of.", &sethistlist); | |
2444 | add_show_from_set (c, &showhistlist); | |
2445 | c->function.sfunc = set_history_size_command; | |
2446 | ||
2447 | add_show_from_set | |
2448 | (add_set_cmd ("filename", no_class, var_filename, (char *)&history_filename, | |
2449 | "Set the filename in which to record the command history\n\ | |
2450 | (the list of previous commands of which a record is kept).", &sethistlist), | |
2451 | &showhistlist); | |
2452 | ||
2453 | add_show_from_set | |
2454 | (add_set_cmd ("confirm", class_support, var_boolean, | |
2455 | (char *)&caution, | |
2456 | "Set whether to confirm potentially dangerous operations.", | |
2457 | &setlist), | |
2458 | &showlist); | |
2459 | ||
2460 | add_prefix_cmd ("info", class_info, info_command, | |
2461 | "Generic command for showing things about the program being debugged.", | |
2462 | &infolist, "info ", 0, &cmdlist); | |
2463 | add_com_alias ("i", "info", class_info, 1); | |
2464 | ||
2465 | add_com ("complete", class_obscure, complete_command, | |
2466 | "List the completions for the rest of the line as a command."); | |
2467 | ||
2468 | add_prefix_cmd ("show", class_info, show_command, | |
2469 | "Generic command for showing things about the debugger.", | |
2470 | &showlist, "show ", 0, &cmdlist); | |
2471 | /* Another way to get at the same thing. */ | |
2472 | add_info ("set", show_command, "Show all GDB settings."); | |
2473 | ||
2474 | add_cmd ("commands", no_class, show_commands, | |
2475 | "Show the the history of commands you typed.\n\ | |
2476 | You can supply a command number to start with, or a `+' to start after\n\ | |
2477 | the previous command number shown.", | |
2478 | &showlist); | |
2479 | ||
2480 | add_cmd ("version", no_class, show_version, | |
2481 | "Show what version of GDB this is.", &showlist); | |
2482 | ||
2483 | /* If target is open when baud changes, it doesn't take effect until the | |
2484 | next open (I think, not sure). */ | |
2485 | add_show_from_set (add_set_cmd ("remotebaud", no_class, | |
2486 | var_zinteger, (char *)&baud_rate, | |
2487 | "Set baud rate for remote serial I/O.\n\ | |
2488 | This value is used to set the speed of the serial port when debugging\n\ | |
2489 | using remote targets.", &setlist), | |
2490 | &showlist); | |
2491 | ||
2492 | add_show_from_set ( | |
2493 | add_set_cmd ("remotedebug", no_class, var_zinteger, (char *)&remote_debug, | |
2494 | "Set debugging of remote protocol.\n\ | |
2495 | When enabled, each packet sent or received with the remote target\n\ | |
2496 | is displayed.", &setlist), | |
2497 | &showlist); | |
2498 | } |