]>
Commit | Line | Data |
---|---|---|
b5a0ac70 SS |
1 | /* Top level stuff for GDB, the GNU debugger. |
2 | Copyright 1999 Free Software Foundation, Inc. | |
3 | Written by Elena Zannoni <[email protected]> of Cygnus Solutions. | |
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "defs.h" | |
22 | #include "event-loop.h" | |
23 | #ifdef HAVE_POLL | |
24 | #include <sys/poll.h> | |
25 | #endif | |
26 | #include "inferior.h" | |
27 | ||
28 | /* readline include files */ | |
29 | #include <readline/readline.h> | |
30 | #include <readline/history.h> | |
31 | ||
32 | /* readline defines this. */ | |
33 | #undef savestring | |
34 | ||
35 | extern FILE *instream; | |
36 | ||
37 | static void command_line_handler PARAMS ((char *)); | |
38 | static void gdb_readline2 PARAMS ((void)); | |
39 | static void pop_prompt PARAMS ((void)); | |
40 | static void push_prompt PARAMS ((char *, char *, char *)); | |
392a587b JM |
41 | static void change_line_handler PARAMS ((void)); |
42 | static void change_annotation_level PARAMS ((void)); | |
43 | static void command_handler PARAMS ((char *)); | |
b5a0ac70 SS |
44 | |
45 | /* Signal handlers. */ | |
46 | void handle_sigint PARAMS ((int)); | |
47 | void handle_sigquit PARAMS ((int)); | |
48 | void handle_sighup PARAMS ((int)); | |
49 | void handle_sigfpe PARAMS ((int)); | |
50 | void handle_sigwinch PARAMS ((int)); | |
51 | ||
52 | /* Functions to be invoked by the event loop in response to | |
53 | signals. */ | |
54 | void async_request_quit PARAMS ((void)); | |
55 | void async_do_nothing PARAMS ((void)); | |
56 | void async_disconnect PARAMS ((void)); | |
57 | void async_float_handler PARAMS ((void)); | |
58 | ||
59 | /* Functions from top.c. */ | |
60 | extern void command_loop_marker PARAMS ((int)); | |
61 | extern int quit_cover PARAMS ((PTR)); | |
62 | extern void quit_command PARAMS ((char *, int)); | |
63 | extern void execute_command PARAMS ((char *, int)); | |
64 | ||
65 | /* Variables from top.c. */ | |
66 | extern int source_line_number; | |
67 | extern char *source_file_name; | |
68 | extern char *source_error; | |
69 | extern char *source_pre_error; | |
70 | extern int history_expansion_p; | |
71 | extern int server_command; | |
72 | ||
73 | /* If this definition isn't overridden by the header files, assume | |
74 | that isatty and fileno exist on this system. */ | |
75 | #ifndef ISATTY | |
76 | #define ISATTY(FP) (isatty (fileno (FP))) | |
77 | #endif | |
78 | ||
79 | /* Hook for alternate command interface. */ | |
80 | void (*async_hook) PARAMS ((void)); | |
81 | ||
82 | /* Readline offers an alternate interface, via callback | |
83 | functions. These are all included in the file callback.c in the | |
84 | readline distribution. This file provides (mainly) a function, which | |
85 | the event loop uses as callback (i.e. event handler) whenever an event | |
86 | is detected on the standard input file descriptor. | |
87 | readline_callback_read_char is called (by the GDB event loop) whenever | |
88 | there is a new character ready on the input stream. This function | |
89 | incrementally builds a buffer internal to readline where it | |
90 | accumulates the line read up to the point of invocation. In the | |
91 | special case in which the character read is newline, the function | |
92 | invokes a GDB supplied callback routine, which does the processing of | |
93 | a full command line. This latter routine is the asynchronous analog | |
94 | of the old command_line_input in gdb. Instead of invoking (and waiting | |
95 | for) readline to read the command line and pass it back to | |
96 | command_loop for processing, the new command_line_handler function has | |
97 | the command line already available as its parameter. INPUT_HANDLER is | |
98 | to be set to the function that readline will invoke when a complete | |
99 | line of input is ready. CALL_READLINE is to be set to the function | |
100 | that readline offers as callback to the event_loop. */ | |
101 | ||
102 | void (*input_handler) PARAMS ((char *)); | |
103 | void (*call_readline) PARAMS ((void)); | |
104 | ||
105 | /* Important variables for the event loop. */ | |
106 | ||
107 | /* This is used to determine if GDB is using the readline library or | |
108 | its own simplified form of readline. It is used by the asynchronous | |
109 | form of the set editing command. | |
392a587b | 110 | ezannoni: as of 1999-04-29 I expect that this |
b5a0ac70 SS |
111 | variable will not be used after gdb is changed to use the event |
112 | loop as default engine, and event-top.c is merged into top.c. */ | |
113 | int async_command_editing_p; | |
114 | ||
115 | /* This variable contains the new prompt that the user sets with the | |
116 | set prompt command. */ | |
117 | char *new_async_prompt; | |
118 | ||
119 | /* This is the annotation suffix that will be used when the | |
120 | annotation_level is 2. */ | |
121 | char *async_annotation_suffix; | |
122 | ||
123 | /* This is the file descriptor for the input stream that GDB uses to | |
124 | read commands from. */ | |
125 | int input_fd; | |
126 | ||
127 | /* This is the prompt stack. Prompts will be pushed on the stack as | |
128 | needed by the different 'kinds' of user inputs GDB is asking | |
129 | for. See event-loop.h. */ | |
130 | struct prompts the_prompts; | |
131 | ||
132 | /* signal handling variables */ | |
133 | /* Each of these is a pointer to a function that the event loop will | |
134 | invoke if the corresponding signal has received. The real signal | |
135 | handlers mark these functions as ready to be executed and the event | |
136 | loop, in a later iteration, calls them. See the function | |
137 | invoke_async_signal_handler. */ | |
138 | async_signal_handler *sigint_token; | |
139 | #ifdef SIGHUP | |
140 | async_signal_handler *sighup_token; | |
141 | #endif | |
142 | async_signal_handler *sigquit_token; | |
143 | async_signal_handler *sigfpe_token; | |
144 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) | |
145 | async_signal_handler *sigwinch_token; | |
146 | #endif | |
147 | ||
148 | /* Structure to save a partially entered command. This is used when | |
149 | the user types '\' at the end of a command line. This is necessary | |
150 | because each line of input is handled by a different call to | |
151 | command_line_handler, and normally there is no state retained | |
152 | between different calls. */ | |
153 | int more_to_come = 0; | |
154 | ||
155 | struct readline_input_state | |
156 | { | |
157 | char *linebuffer; | |
158 | char *linebuffer_ptr; | |
159 | } | |
160 | readline_input_state; | |
161 | \f | |
162 | ||
163 | /* Initialize all the necessary variables, start the event loop, | |
164 | register readline, and stdin. */ | |
165 | void | |
166 | setup_event_loop () | |
167 | { | |
168 | int length = strlen (PREFIX (0)) + strlen (PROMPT (0)) + strlen (SUFFIX (0)) + 1; | |
169 | char *a_prompt = (char *) xmalloc (length); | |
170 | ||
171 | /* Set things up for readline to be invoked via the alternate | |
172 | interface, i.e. via a callback function (rl_callback_read_char). */ | |
173 | call_readline = rl_callback_read_char; | |
174 | ||
175 | /* When readline has read an end-of-line character, it passes the | |
176 | complete line to gdb for processing. command_line_handler is the | |
177 | function that does this. */ | |
178 | input_handler = command_line_handler; | |
179 | ||
180 | /* Tell readline what the prompt to display is and what function it | |
181 | will need to call after a whole line is read. */ | |
182 | strcpy (a_prompt, PREFIX (0)); | |
183 | strcat (a_prompt, PROMPT (0)); | |
184 | strcat (a_prompt, SUFFIX (0)); | |
185 | rl_callback_handler_install (a_prompt, input_handler); | |
186 | ||
187 | /* Tell readline to use the same input stream that gdb uses. */ | |
188 | rl_instream = instream; | |
189 | /* Get a file descriptor for the input stream, so that we can | |
190 | register it with the event loop. */ | |
191 | input_fd = fileno (instream); | |
192 | ||
193 | /* Now we need to create the event sources for the input file descriptor. */ | |
194 | /* At this point in time, this is the only event source that we | |
195 | register with the even loop. Another source is going to be the | |
196 | target program (inferior), but that must be registered only when | |
197 | it actually exists (I.e. after we say 'run' or after we connect | |
198 | to a remote target. */ | |
199 | #ifdef HAVE_POLL | |
200 | create_file_handler (input_fd, POLLIN, | |
201 | (file_handler_func *) call_readline, 0); | |
202 | #else | |
203 | create_file_handler (input_fd, GDB_READABLE, | |
204 | (file_handler_func *) call_readline, 0); | |
205 | #endif | |
206 | ||
207 | /* Loop until there is something to do. This is the entry point to | |
208 | the event loop engine. gdb_do_one_event will process one event | |
209 | for each invocation. It always returns 1, unless there are no | |
210 | more event sources registered. In this case it returns 0. */ | |
211 | while (gdb_do_one_event () != 0) | |
212 | ; | |
213 | ||
214 | /* We are done with the event loop. There are no more event sources | |
215 | to listen to. So we exit GDB. */ | |
216 | return; | |
217 | } | |
218 | ||
219 | /* Change the function to be invoked every time there is a character | |
220 | ready on stdin. This is used when the user sets the editing off, | |
221 | therefore bypassing readline, and letting gdb handle the input | |
222 | itself, via gdb_readline2. Also it is used in the opposite case in | |
223 | which the user sets editing on again, by restoring readline | |
224 | handling of the input. */ | |
392a587b | 225 | static void |
b5a0ac70 SS |
226 | change_line_handler () |
227 | { | |
228 | if (async_command_editing_p) | |
229 | { | |
230 | /* Turn on editing by using readline. */ | |
231 | call_readline = rl_callback_read_char; | |
232 | } | |
233 | else | |
234 | { | |
235 | /* Turn off editing by using gdb_readline2. */ | |
236 | rl_callback_handler_remove (); | |
237 | call_readline = gdb_readline2; | |
238 | } | |
239 | ||
240 | /* To tell the event loop to change the handler associated with the | |
241 | input file descriptor, we need to create a new event source, | |
242 | corresponding to the same fd, but with a new event handler | |
243 | function. */ | |
244 | delete_file_handler (input_fd); | |
245 | #ifdef HAVE_POLL | |
246 | create_file_handler (input_fd, POLLIN, | |
247 | (file_handler_func *) call_readline, 0); | |
248 | #else | |
249 | create_file_handler (input_fd, GDB_READABLE, | |
250 | (file_handler_func *) call_readline, 0); | |
251 | #endif | |
252 | } | |
253 | ||
254 | /* Displays the prompt. The prompt that is displayed is the current | |
255 | top of the prompt stack, if the argument NEW_PROMPT is | |
256 | 0. Otherwise, it displays whatever NEW_PROMPT is. This is used | |
257 | after each gdb command has completed, and in the following cases: | |
258 | 1. when the user enters a command line which is ended by '\' | |
259 | indicating that the command will continue on the next line. | |
260 | In that case the prompt that is displayed is the empty string. | |
261 | 2. When the user is entering 'commands' for a breakpoint, or | |
262 | actions for a tracepoint. In this case the prompt will be '>' | |
263 | 3. Other???? | |
264 | FIXME: 2. & 3. not implemented yet for async. */ | |
265 | void | |
266 | display_gdb_prompt (new_prompt) | |
267 | char *new_prompt; | |
268 | { | |
269 | int prompt_length = 0; | |
270 | ||
271 | if (!new_prompt) | |
272 | { | |
273 | /* Just use the top of the prompt stack. */ | |
274 | prompt_length = strlen (PREFIX (0)) + | |
275 | strlen (SUFFIX (0)) + | |
276 | strlen (PROMPT (0)) + 1; | |
277 | ||
278 | new_prompt = (char *) alloca (prompt_length); | |
279 | ||
280 | /* Prefix needs to have new line at end. */ | |
281 | strcpy (new_prompt, PREFIX (0)); | |
282 | strcat (new_prompt, PROMPT (0)); | |
283 | /* Suffix needs to have a new line at end and \032 \032 at | |
284 | beginning. */ | |
285 | strcat (new_prompt, SUFFIX (0)); | |
286 | } | |
287 | ||
288 | if (async_command_editing_p) | |
289 | { | |
290 | rl_callback_handler_remove (); | |
291 | rl_callback_handler_install (new_prompt, input_handler); | |
292 | } | |
293 | else if (new_prompt) | |
294 | { | |
295 | /* Don't use a _filtered function here. It causes the assumed | |
296 | character position to be off, since the newline we read from | |
297 | the user is not accounted for. */ | |
298 | fputs_unfiltered (new_prompt, gdb_stdout); | |
299 | ||
300 | #ifdef MPW | |
301 | /* Move to a new line so the entered line doesn't have a prompt | |
302 | on the front of it. */ | |
303 | fputs_unfiltered ("\n", gdb_stdout); | |
304 | #endif /* MPW */ | |
305 | gdb_flush (gdb_stdout); | |
306 | } | |
307 | } | |
308 | ||
309 | /* Used when the user requests a different annotation level, with | |
310 | 'set annotate'. It pushes a new prompt (with prefix and suffix) on top | |
311 | of the prompt stack, if the annotation level desired is 2, otherwise | |
312 | it pops the top of the prompt stack when we want the annotation level | |
313 | to be the normal ones (1 or 2). */ | |
392a587b | 314 | static void |
b5a0ac70 SS |
315 | change_annotation_level () |
316 | { | |
317 | char *prefix, *suffix; | |
318 | ||
319 | if (!PREFIX (0) || !PROMPT (0) || !SUFFIX (0)) | |
320 | { | |
321 | /* The prompt stack has not been initialized to "", we are | |
322 | using gdb w/o the --async switch */ | |
323 | warning ("Command has same effect as set annotate"); | |
324 | return; | |
325 | } | |
326 | ||
327 | if (annotation_level > 1) | |
328 | { | |
329 | if (!strcmp (PREFIX (0), "") && !strcmp (SUFFIX (0), "")) | |
330 | { | |
331 | /* Push a new prompt if the previous annotation_level was not >1. */ | |
332 | prefix = (char *) alloca (strlen (async_annotation_suffix) + 10); | |
333 | strcpy (prefix, "\n\032\032pre-"); | |
334 | strcat (prefix, async_annotation_suffix); | |
335 | strcat (prefix, "\n"); | |
336 | ||
337 | suffix = (char *) alloca (strlen (async_annotation_suffix) + 6); | |
338 | strcpy (suffix, "\n\032\032"); | |
339 | strcat (suffix, async_annotation_suffix); | |
340 | strcat (suffix, "\n"); | |
341 | ||
342 | push_prompt (prefix, (char *) 0, suffix); | |
343 | } | |
344 | } | |
345 | else | |
346 | { | |
347 | if (strcmp (PREFIX (0), "") && strcmp (SUFFIX (0), "")) | |
348 | { | |
349 | /* Pop the top of the stack, we are going back to annotation < 1. */ | |
350 | pop_prompt (); | |
351 | } | |
352 | } | |
353 | } | |
354 | ||
355 | /* Pushes a new prompt on the prompt stack. Each prompt has three | |
356 | parts: prefix, prompt, suffix. Usually prefix and suffix are empty | |
357 | strings, except when the annotation level is 2. Memory is allocated | |
358 | within savestring for the new prompt. */ | |
359 | static void | |
360 | push_prompt (prefix, prompt, suffix) | |
361 | char *prefix; | |
362 | char *prompt; | |
363 | char *suffix; | |
364 | { | |
365 | the_prompts.top++; | |
366 | PREFIX (0) = savestring (prefix, strlen (prefix)); | |
367 | ||
368 | if (prompt) | |
369 | PROMPT (0) = savestring (prompt, strlen (prompt)); | |
370 | else | |
371 | PROMPT (0) = savestring (PROMPT (-1), strlen (PROMPT (-1))); | |
372 | ||
373 | SUFFIX (0) = savestring (suffix, strlen (suffix)); | |
374 | } | |
375 | ||
376 | /* Pops the top of the prompt stack, and frees the memory allocated for it. */ | |
377 | static void | |
378 | pop_prompt () | |
379 | { | |
380 | if (strcmp (PROMPT (0), PROMPT (-1))) | |
381 | { | |
382 | free (PROMPT (-1)); | |
383 | PROMPT (-1) = savestring (PROMPT (0), strlen (PROMPT (0))); | |
384 | } | |
385 | ||
386 | free (PREFIX (0)); | |
387 | free (PROMPT (0)); | |
388 | free (SUFFIX (0)); | |
389 | the_prompts.top--; | |
390 | } | |
391 | \f | |
392 | /* Handles a gdb command. This function is called by | |
393 | command_line_handler, which has processed one or more input lines | |
394 | into COMMAND. */ | |
392a587b | 395 | /* NOTE: 1999-04-30 This is the asynchronous version of the command_loop |
b5a0ac70 SS |
396 | function. The command_loop function will be obsolete when we |
397 | switch to use the event loop at every execution of gdb. */ | |
392a587b | 398 | static void |
b5a0ac70 SS |
399 | command_handler (command) |
400 | char *command; | |
401 | { | |
402 | struct cleanup *old_chain; | |
403 | int stdin_is_tty = ISATTY (stdin); | |
404 | long time_at_cmd_start; | |
405 | #ifdef HAVE_SBRK | |
406 | long space_at_cmd_start = 0; | |
407 | #endif | |
408 | extern int display_time; | |
409 | extern int display_space; | |
410 | ||
411 | #if defined(TUI) | |
412 | extern int insert_mode; | |
413 | #endif | |
414 | ||
415 | quit_flag = 0; | |
416 | if (instream == stdin && stdin_is_tty) | |
417 | reinitialize_more_filter (); | |
418 | old_chain = make_cleanup ((make_cleanup_func) command_loop_marker, 0); | |
419 | ||
420 | #if defined(TUI) | |
421 | insert_mode = 0; | |
422 | #endif | |
423 | /* If readline returned a NULL command, it means that the | |
424 | connection with the terminal is gone. This happens at the | |
425 | end of a testsuite run, after Expect has hung up | |
426 | but GDB is still alive. In such a case, we just quit gdb | |
427 | killing the inferior program too. */ | |
428 | if (command == 0) | |
429 | quit_command ((char *) 0, stdin == instream); | |
430 | ||
431 | time_at_cmd_start = get_run_time (); | |
432 | ||
433 | if (display_space) | |
434 | { | |
435 | #ifdef HAVE_SBRK | |
436 | extern char **environ; | |
437 | char *lim = (char *) sbrk (0); | |
438 | ||
439 | space_at_cmd_start = (long) (lim - (char *) &environ); | |
440 | #endif | |
441 | } | |
442 | ||
443 | execute_command (command, instream == stdin); | |
444 | ||
445 | /* Do any commands attached to breakpoint we stopped at. */ | |
446 | bpstat_do_actions (&stop_bpstat); | |
447 | do_cleanups (old_chain); | |
448 | ||
449 | if (display_time) | |
450 | { | |
451 | long cmd_time = get_run_time () - time_at_cmd_start; | |
452 | ||
453 | printf_unfiltered ("Command execution time: %ld.%06ld\n", | |
454 | cmd_time / 1000000, cmd_time % 1000000); | |
455 | } | |
456 | ||
457 | if (display_space) | |
458 | { | |
459 | #ifdef HAVE_SBRK | |
460 | extern char **environ; | |
461 | char *lim = (char *) sbrk (0); | |
462 | long space_now = lim - (char *) &environ; | |
463 | long space_diff = space_now - space_at_cmd_start; | |
464 | ||
465 | printf_unfiltered ("Space used: %ld (%c%ld for this command)\n", | |
466 | space_now, | |
467 | (space_diff >= 0 ? '+' : '-'), | |
468 | space_diff); | |
469 | #endif | |
470 | } | |
471 | } | |
472 | ||
473 | /* Handle a complete line of input. This is called by the callback | |
474 | mechanism within the readline library. Deal with incomplete commands | |
475 | as well, by saving the partial input in a global buffer. */ | |
476 | ||
392a587b | 477 | /* NOTE: 1999-04-30 This is the asynchronous version of the |
b5a0ac70 SS |
478 | command_line_input function. command_line_input will become |
479 | obsolete once we use the event loop as the default mechanism in | |
480 | GDB. */ | |
481 | static void | |
482 | command_line_handler (rl) | |
483 | char *rl; | |
484 | { | |
485 | static char *linebuffer = 0; | |
486 | static unsigned linelength = 0; | |
487 | register char *p; | |
488 | char *p1; | |
489 | int change_prompt = 0; | |
490 | extern char *line; | |
491 | extern int linesize; | |
492 | char *nline; | |
493 | char got_eof = 0; | |
494 | ||
495 | ||
496 | int repeat = (instream == stdin); | |
497 | ||
498 | if (annotation_level > 1 && instream == stdin) | |
499 | { | |
500 | printf_unfiltered ("\n\032\032post-"); | |
501 | printf_unfiltered (async_annotation_suffix); | |
502 | printf_unfiltered ("\n"); | |
503 | } | |
504 | ||
505 | if (linebuffer == 0) | |
506 | { | |
507 | linelength = 80; | |
508 | linebuffer = (char *) xmalloc (linelength); | |
509 | } | |
510 | ||
511 | p = linebuffer; | |
512 | ||
513 | if (more_to_come) | |
514 | { | |
515 | strcpy (linebuffer, readline_input_state.linebuffer); | |
516 | p = readline_input_state.linebuffer_ptr; | |
517 | free (readline_input_state.linebuffer); | |
518 | more_to_come = 0; | |
519 | change_prompt = 1; | |
520 | } | |
521 | ||
522 | #ifdef STOP_SIGNAL | |
523 | if (job_control) | |
524 | signal (STOP_SIGNAL, stop_sig); | |
525 | #endif | |
526 | ||
527 | /* Make sure that all output has been output. Some machines may let | |
528 | you get away with leaving out some of the gdb_flush, but not all. */ | |
529 | wrap_here (""); | |
530 | gdb_flush (gdb_stdout); | |
531 | gdb_flush (gdb_stderr); | |
532 | ||
533 | if (source_file_name != NULL) | |
534 | { | |
535 | ++source_line_number; | |
536 | sprintf (source_error, | |
537 | "%s%s:%d: Error in sourced command file:\n", | |
538 | source_pre_error, | |
539 | source_file_name, | |
540 | source_line_number); | |
541 | error_pre_print = source_error; | |
542 | } | |
543 | ||
544 | /* If we are in this case, then command_handler will call quit | |
545 | and exit from gdb. */ | |
546 | if (!rl || rl == (char *) EOF) | |
547 | { | |
548 | got_eof = 1; | |
549 | command_handler (0); | |
550 | } | |
551 | if (strlen (rl) + 1 + (p - linebuffer) > linelength) | |
552 | { | |
553 | linelength = strlen (rl) + 1 + (p - linebuffer); | |
554 | nline = (char *) xrealloc (linebuffer, linelength); | |
555 | p += nline - linebuffer; | |
556 | linebuffer = nline; | |
557 | } | |
558 | p1 = rl; | |
559 | /* Copy line. Don't copy null at end. (Leaves line alone | |
560 | if this was just a newline) */ | |
561 | while (*p1) | |
562 | *p++ = *p1++; | |
563 | ||
564 | free (rl); /* Allocated in readline. */ | |
565 | ||
566 | if (p == linebuffer || *(p - 1) == '\\') | |
567 | { | |
568 | /* We come here also if the line entered is empty (just a 'return') */ | |
569 | p--; /* Put on top of '\'. */ | |
570 | ||
571 | if (*p == '\\') | |
572 | { | |
573 | readline_input_state.linebuffer = savestring (linebuffer, | |
574 | strlen (linebuffer)); | |
575 | readline_input_state.linebuffer_ptr = p; | |
576 | ||
577 | /* We will not invoke a execute_command if there is more | |
578 | input expected to complete the command. So, we need to | |
579 | print an empty prompt here. */ | |
580 | display_gdb_prompt (""); | |
581 | more_to_come = 1; | |
582 | } | |
583 | } | |
584 | ||
585 | #ifdef STOP_SIGNAL | |
586 | if (job_control) | |
587 | signal (STOP_SIGNAL, SIG_DFL); | |
588 | #endif | |
589 | ||
590 | #define SERVER_COMMAND_LENGTH 7 | |
591 | server_command = | |
592 | (p - linebuffer > SERVER_COMMAND_LENGTH) | |
593 | && STREQN (linebuffer, "server ", SERVER_COMMAND_LENGTH); | |
594 | if (server_command) | |
595 | { | |
596 | /* Note that we don't set `line'. Between this and the check in | |
597 | dont_repeat, this insures that repeating will still do the | |
598 | right thing. */ | |
599 | *p = '\0'; | |
600 | command_handler (linebuffer + SERVER_COMMAND_LENGTH); | |
601 | display_gdb_prompt (0); | |
602 | return; | |
603 | } | |
604 | ||
605 | /* Do history expansion if that is wished. */ | |
606 | if (history_expansion_p && instream == stdin | |
607 | && ISATTY (instream)) | |
608 | { | |
609 | char *history_value; | |
610 | int expanded; | |
611 | ||
612 | *p = '\0'; /* Insert null now. */ | |
613 | expanded = history_expand (linebuffer, &history_value); | |
614 | if (expanded) | |
615 | { | |
616 | /* Print the changes. */ | |
617 | printf_unfiltered ("%s\n", history_value); | |
618 | ||
619 | /* If there was an error, call this function again. */ | |
620 | if (expanded < 0) | |
621 | { | |
622 | free (history_value); | |
623 | return; | |
624 | } | |
625 | if (strlen (history_value) > linelength) | |
626 | { | |
627 | linelength = strlen (history_value) + 1; | |
628 | linebuffer = (char *) xrealloc (linebuffer, linelength); | |
629 | } | |
630 | strcpy (linebuffer, history_value); | |
631 | p = linebuffer + strlen (linebuffer); | |
632 | free (history_value); | |
633 | } | |
634 | } | |
635 | ||
636 | /* If we just got an empty line, and that is supposed | |
637 | to repeat the previous command, return the value in the | |
638 | global buffer. */ | |
639 | if (repeat && p == linebuffer && *p != '\\') | |
640 | { | |
641 | command_handler (line); | |
642 | display_gdb_prompt (0); | |
643 | return; | |
644 | } | |
645 | ||
646 | for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++); | |
647 | if (repeat && !*p1) | |
648 | { | |
649 | command_handler (line); | |
650 | display_gdb_prompt (0); | |
651 | return; | |
652 | } | |
653 | ||
654 | *p = 0; | |
655 | ||
656 | /* Add line to history if appropriate. */ | |
657 | if (instream == stdin | |
658 | && ISATTY (stdin) && *linebuffer) | |
659 | add_history (linebuffer); | |
660 | ||
661 | /* Note: lines consisting solely of comments are added to the command | |
662 | history. This is useful when you type a command, and then | |
663 | realize you don't want to execute it quite yet. You can comment | |
664 | out the command and then later fetch it from the value history | |
665 | and remove the '#'. The kill ring is probably better, but some | |
666 | people are in the habit of commenting things out. */ | |
667 | if (*p1 == '#') | |
668 | *p1 = '\0'; /* Found a comment. */ | |
669 | ||
670 | /* Save into global buffer if appropriate. */ | |
671 | if (repeat) | |
672 | { | |
673 | if (linelength > linesize) | |
674 | { | |
675 | line = xrealloc (line, linelength); | |
676 | linesize = linelength; | |
677 | } | |
678 | strcpy (line, linebuffer); | |
679 | if (!more_to_come) | |
680 | { | |
681 | command_handler (line); | |
682 | display_gdb_prompt (0); | |
683 | } | |
684 | return; | |
685 | } | |
686 | ||
687 | command_handler (linebuffer); | |
688 | display_gdb_prompt (0); | |
689 | return; | |
690 | } | |
691 | ||
692 | /* Does reading of input from terminal w/o the editing features | |
693 | provided by the readline library. */ | |
694 | ||
392a587b | 695 | /* NOTE: 1999-04-30 Asynchronous version of gdb_readline. gdb_readline |
b5a0ac70 SS |
696 | will become obsolete when the event loop is made the default |
697 | execution for gdb. */ | |
698 | static void | |
699 | gdb_readline2 () | |
700 | { | |
701 | int c; | |
702 | char *result; | |
703 | int input_index = 0; | |
704 | int result_size = 80; | |
705 | ||
706 | result = (char *) xmalloc (result_size); | |
707 | ||
708 | /* We still need the while loop here, even though it would seem | |
709 | obvious to invoke gdb_readline2 at every character entered. If | |
710 | not using the readline library, the terminal is in cooked mode, | |
711 | which sends the characters all at once. Poll will notice that the | |
712 | input fd has changed state only after enter is pressed. At this | |
713 | point we still need to fetch all the chars entered. */ | |
714 | ||
715 | while (1) | |
716 | { | |
717 | /* Read from stdin if we are executing a user defined command. | |
718 | This is the right thing for prompt_for_continue, at least. */ | |
719 | c = fgetc (instream ? instream : stdin); | |
720 | ||
721 | if (c == EOF) | |
722 | { | |
723 | if (input_index > 0) | |
724 | /* The last line does not end with a newline. Return it, and | |
725 | if we are called again fgetc will still return EOF and | |
726 | we'll return NULL then. */ | |
727 | break; | |
728 | free (result); | |
729 | command_line_handler (0); | |
730 | } | |
731 | ||
732 | if (c == '\n') | |
733 | #ifndef CRLF_SOURCE_FILES | |
734 | break; | |
735 | #else | |
736 | { | |
737 | if (input_index > 0 && result[input_index - 1] == '\r') | |
738 | input_index--; | |
739 | break; | |
740 | } | |
741 | #endif | |
742 | ||
743 | result[input_index++] = c; | |
744 | while (input_index >= result_size) | |
745 | { | |
746 | result_size *= 2; | |
747 | result = (char *) xrealloc (result, result_size); | |
748 | } | |
749 | } | |
750 | ||
751 | result[input_index++] = '\0'; | |
752 | command_line_handler (result); | |
753 | } | |
754 | \f | |
755 | ||
756 | /* Initialization of signal handlers and tokens. There is a function | |
757 | handle_sig* for each of the signals GDB cares about. Specifically: | |
758 | SIGINT, SIGFPE, SIGQUIT, SIGTSTP, SIGHUP, SIGWINCH. These | |
759 | functions are the actual signal handlers associated to the signals | |
760 | via calls to signal(). The only job for these functions is to | |
761 | enqueue the appropriate event/procedure with the event loop. Such | |
762 | procedures are the old signal handlers. The event loop will take | |
763 | care of invoking the queued procedures to perform the usual tasks | |
764 | associated with the reception of the signal. */ | |
392a587b | 765 | /* NOTE: 1999-04-30 This is the asynchronous version of init_signals. |
b5a0ac70 SS |
766 | init_signals will become obsolete as we move to have to event loop |
767 | as the default for gdb. */ | |
768 | void | |
769 | async_init_signals () | |
770 | { | |
771 | signal (SIGINT, handle_sigint); | |
772 | sigint_token = | |
773 | create_async_signal_handler ((async_handler_func *) async_request_quit, NULL); | |
774 | ||
775 | /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed | |
776 | to the inferior and breakpoints will be ignored. */ | |
777 | #ifdef SIGTRAP | |
778 | signal (SIGTRAP, SIG_DFL); | |
779 | #endif | |
780 | ||
781 | /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get | |
782 | passed to the inferior, which we don't want. It would be | |
783 | possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but | |
784 | on BSD4.3 systems using vfork, that can affect the | |
785 | GDB process as well as the inferior (the signal handling tables | |
786 | might be in memory, shared between the two). Since we establish | |
787 | a handler for SIGQUIT, when we call exec it will set the signal | |
788 | to SIG_DFL for us. */ | |
789 | signal (SIGQUIT, handle_sigquit); | |
790 | sigquit_token = | |
791 | create_async_signal_handler ((async_handler_func *) async_do_nothing, NULL); | |
792 | #ifdef SIGHUP | |
793 | if (signal (SIGHUP, handle_sighup) != SIG_IGN) | |
794 | sighup_token = | |
795 | create_async_signal_handler ((async_handler_func *) async_disconnect, NULL); | |
796 | else | |
797 | sighup_token = | |
798 | create_async_signal_handler ((async_handler_func *) async_do_nothing, NULL); | |
799 | #endif | |
800 | signal (SIGFPE, handle_sigfpe); | |
801 | sigfpe_token = | |
802 | create_async_signal_handler ((async_handler_func *) async_float_handler, NULL); | |
803 | ||
804 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) | |
805 | signal (SIGWINCH, handle_sigwinch); | |
806 | sigwinch_token = | |
807 | create_async_signal_handler ((async_handler_func *) SIGWINCH_HANDLER, NULL); | |
808 | #endif | |
809 | } | |
810 | ||
811 | /* Tell the event loop what to do if SIGINT is received. | |
812 | See event-signal.c. */ | |
813 | void | |
814 | handle_sigint (sig) | |
815 | int sig; | |
816 | { | |
817 | signal (sig, handle_sigint); | |
818 | ||
819 | /* If immediate_quit is set, we go ahead and process the SIGINT right | |
820 | away, even if we usually would defer this to the event loop. The | |
821 | assumption here is that it is safe to process ^C immediately if | |
822 | immediate_quit is set. If we didn't, SIGINT would be really | |
823 | processed only the next time through the event loop. To get to | |
824 | that point, though, the command that we want to interrupt needs to | |
825 | finish first, which is unacceptable. */ | |
826 | if (immediate_quit) | |
827 | async_request_quit (); | |
828 | else | |
829 | /* If immediate quit is not set, we process SIGINT the next time | |
830 | through the loop, which is fine. */ | |
831 | mark_async_signal_handler (sigint_token); | |
832 | } | |
833 | ||
834 | /* Do the quit. All the checks have been done by the caller. */ | |
835 | void | |
836 | async_request_quit () | |
837 | { | |
838 | quit_flag = 1; | |
839 | #ifdef REQUEST_QUIT | |
840 | REQUEST_QUIT; | |
841 | #else | |
842 | quit (); | |
843 | #endif | |
844 | } | |
845 | ||
846 | /* Tell the event loop what to do if SIGQUIT is received. | |
847 | See event-signal.c. */ | |
848 | void | |
849 | handle_sigquit (sig) | |
850 | int sig; | |
851 | { | |
852 | mark_async_signal_handler (sigquit_token); | |
853 | signal (sig, handle_sigquit); | |
854 | } | |
855 | ||
856 | /* Called by the event loop in response to a SIGQUIT. */ | |
857 | void | |
858 | async_do_nothing () | |
859 | { | |
860 | /* Empty function body. */ | |
861 | } | |
862 | ||
863 | #ifdef SIGHUP | |
864 | /* Tell the event loop what to do if SIGHUP is received. | |
865 | See event-signal.c. */ | |
866 | void | |
867 | handle_sighup (sig) | |
868 | int sig; | |
869 | { | |
870 | mark_async_signal_handler (sighup_token); | |
871 | signal (sig, handle_sighup); | |
872 | } | |
873 | ||
874 | /* Called by the event loop to process a SIGHUP. */ | |
875 | void | |
876 | async_disconnect () | |
877 | { | |
878 | catch_errors (quit_cover, NULL, | |
879 | "Could not kill the program being debugged", | |
880 | RETURN_MASK_ALL); | |
881 | signal (SIGHUP, SIG_DFL); /*FIXME: ??????????? */ | |
882 | kill (getpid (), SIGHUP); | |
883 | } | |
884 | #endif | |
885 | ||
886 | /* Tell the event loop what to do if SIGFPE is received. | |
887 | See event-signal.c. */ | |
888 | void | |
889 | handle_sigfpe (sig) | |
890 | int sig; | |
891 | { | |
892 | mark_async_signal_handler (sigfpe_token); | |
893 | signal (sig, handle_sigfpe); | |
894 | } | |
895 | ||
896 | /* Event loop will call this functin to process a SIGFPE. */ | |
897 | void | |
898 | async_float_handler () | |
899 | { | |
900 | /* This message is based on ANSI C, section 4.7. Note that integer | |
901 | divide by zero causes this, so "float" is a misnomer. */ | |
902 | error ("Erroneous arithmetic operation."); | |
903 | } | |
904 | ||
905 | /* Tell the event loop what to do if SIGWINCH is received. | |
906 | See event-signal.c. */ | |
907 | #if defined(SIGWINCH) && defined(SIGWINCH_HANDLER) | |
908 | void | |
909 | handle_sigwinch (sig) | |
910 | int sig; | |
911 | { | |
912 | mark_async_signal_handler (sigwinch_token); | |
913 | signal (sig, handle_sigwinch); | |
914 | } | |
915 | #endif | |
916 | \f | |
917 | ||
918 | /* Called by do_setshow_command. */ | |
919 | /* ARGSUSED */ | |
920 | void | |
921 | set_async_editing_command (args, from_tty, c) | |
922 | char *args; | |
923 | int from_tty; | |
924 | struct cmd_list_element *c; | |
925 | { | |
926 | change_line_handler (); | |
927 | } | |
928 | ||
929 | /* Called by do_setshow_command. */ | |
930 | /* ARGSUSED */ | |
931 | void | |
932 | set_async_annotation_level (args, from_tty, c) | |
933 | char *args; | |
934 | int from_tty; | |
935 | struct cmd_list_element *c; | |
936 | { | |
937 | change_annotation_level (); | |
938 | } | |
939 | ||
940 | /* Called by do_setshow_command. */ | |
941 | /* ARGSUSED */ | |
942 | void | |
943 | set_async_prompt (args, from_tty, c) | |
944 | char *args; | |
945 | int from_tty; | |
946 | struct cmd_list_element *c; | |
947 | { | |
948 | PROMPT (0) = savestring (new_async_prompt, strlen (new_async_prompt)); | |
949 | } | |
950 | ||
951 | ||
952 | ||
953 |