198944Sobrien/* Top level stuff for GDB, the GNU debugger.
2130803Smarcel   Copyright 1999, 2000, 2001, 2002, 2004 Free Software Foundation, Inc.
398944Sobrien   Written by Elena Zannoni <ezannoni@cygnus.com> of Cygnus Solutions.
498944Sobrien
598944Sobrien   This file is part of GDB.
698944Sobrien
798944Sobrien   This program is free software; you can redistribute it and/or modify
898944Sobrien   it under the terms of the GNU General Public License as published by
998944Sobrien   the Free Software Foundation; either version 2 of the License, or
1098944Sobrien   (at your option) any later version.
1198944Sobrien
1298944Sobrien   This program is distributed in the hope that it will be useful,
1398944Sobrien   but WITHOUT ANY WARRANTY; without even the implied warranty of
1498944Sobrien   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1598944Sobrien   GNU General Public License for more details.
1698944Sobrien
1798944Sobrien   You should have received a copy of the GNU General Public License
1898944Sobrien   along with this program; if not, write to the Free Software
1998944Sobrien   Foundation, Inc., 59 Temple Place - Suite 330,
2098944Sobrien   Boston, MA 02111-1307, USA. */
2198944Sobrien
2298944Sobrien#include "defs.h"
2398944Sobrien#include "top.h"
2498944Sobrien#include "inferior.h"
2598944Sobrien#include "target.h"
2698944Sobrien#include "terminal.h"		/* for job_control */
2798944Sobrien#include "event-loop.h"
2898944Sobrien#include "event-top.h"
29130803Smarcel#include "interps.h"
3098944Sobrien#include <signal.h>
3198944Sobrien
3298944Sobrien/* For dont_repeat() */
3398944Sobrien#include "gdbcmd.h"
3498944Sobrien
3598944Sobrien/* readline include files */
36130803Smarcel#include "readline/readline.h"
37130803Smarcel#include "readline/history.h"
3898944Sobrien
3998944Sobrien/* readline defines this.  */
4098944Sobrien#undef savestring
4198944Sobrien
4298944Sobrienstatic void rl_callback_read_char_wrapper (gdb_client_data client_data);
4398944Sobrienstatic void command_line_handler (char *rl);
4498944Sobrienstatic void command_line_handler_continuation (struct continuation_arg *arg);
4598944Sobrienstatic void change_line_handler (void);
4698944Sobrienstatic void change_annotation_level (void);
4798944Sobrienstatic void command_handler (char *command);
4898944Sobrienstatic void async_do_nothing (gdb_client_data arg);
4998944Sobrienstatic void async_disconnect (gdb_client_data arg);
5098944Sobrienstatic void async_stop_sig (gdb_client_data arg);
5198944Sobrienstatic void async_float_handler (gdb_client_data arg);
5298944Sobrien
5398944Sobrien/* Signal handlers. */
5498944Sobrienstatic void handle_sigquit (int sig);
5598944Sobrienstatic void handle_sighup (int sig);
5698944Sobrienstatic void handle_sigfpe (int sig);
5798944Sobrien#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
5898944Sobrienstatic void handle_sigwinch (int sig);
5998944Sobrien#endif
6098944Sobrien
6198944Sobrien/* Functions to be invoked by the event loop in response to
6298944Sobrien   signals. */
6398944Sobrienstatic void async_do_nothing (gdb_client_data);
6498944Sobrienstatic void async_disconnect (gdb_client_data);
6598944Sobrienstatic void async_float_handler (gdb_client_data);
6698944Sobrienstatic void async_stop_sig (gdb_client_data);
6798944Sobrien
6898944Sobrien/* Readline offers an alternate interface, via callback
6998944Sobrien   functions. These are all included in the file callback.c in the
7098944Sobrien   readline distribution.  This file provides (mainly) a function, which
7198944Sobrien   the event loop uses as callback (i.e. event handler) whenever an event
7298944Sobrien   is detected on the standard input file descriptor.
7398944Sobrien   readline_callback_read_char is called (by the GDB event loop) whenever
7498944Sobrien   there is a new character ready on the input stream. This function
7598944Sobrien   incrementally builds a buffer internal to readline where it
7698944Sobrien   accumulates the line read up to the point of invocation.  In the
7798944Sobrien   special case in which the character read is newline, the function
7898944Sobrien   invokes a GDB supplied callback routine, which does the processing of
7998944Sobrien   a full command line.  This latter routine is the asynchronous analog
8098944Sobrien   of the old command_line_input in gdb. Instead of invoking (and waiting
8198944Sobrien   for) readline to read the command line and pass it back to
8298944Sobrien   command_loop for processing, the new command_line_handler function has
8398944Sobrien   the command line already available as its parameter.  INPUT_HANDLER is
8498944Sobrien   to be set to the function that readline will invoke when a complete
8598944Sobrien   line of input is ready.  CALL_READLINE is to be set to the function
8698944Sobrien   that readline offers as callback to the event_loop. */
8798944Sobrien
8898944Sobrienvoid (*input_handler) (char *);
8998944Sobrienvoid (*call_readline) (gdb_client_data);
9098944Sobrien
9198944Sobrien/* Important variables for the event loop. */
9298944Sobrien
9398944Sobrien/* This is used to determine if GDB is using the readline library or
9498944Sobrien   its own simplified form of readline. It is used by the asynchronous
9598944Sobrien   form of the set editing command.
9698944Sobrien   ezannoni: as of 1999-04-29 I expect that this
9798944Sobrien   variable will not be used after gdb is changed to use the event
9898944Sobrien   loop as default engine, and event-top.c is merged into top.c. */
9998944Sobrienint async_command_editing_p;
10098944Sobrien
10198944Sobrien/* This variable contains the new prompt that the user sets with the
10298944Sobrien   set prompt command. */
10398944Sobrienchar *new_async_prompt;
10498944Sobrien
10598944Sobrien/* This is the annotation suffix that will be used when the
10698944Sobrien   annotation_level is 2. */
10798944Sobrienchar *async_annotation_suffix;
10898944Sobrien
10998944Sobrien/* This is used to display the notification of the completion of an
11098944Sobrien   asynchronous execution command. */
11198944Sobrienint exec_done_display_p = 0;
11298944Sobrien
11398944Sobrien/* This is the file descriptor for the input stream that GDB uses to
11498944Sobrien   read commands from. */
11598944Sobrienint input_fd;
11698944Sobrien
11798944Sobrien/* This is the prompt stack. Prompts will be pushed on the stack as
11898944Sobrien   needed by the different 'kinds' of user inputs GDB is asking
11998944Sobrien   for. See event-loop.h. */
12098944Sobrienstruct prompts the_prompts;
12198944Sobrien
12298944Sobrien/* signal handling variables */
12398944Sobrien/* Each of these is a pointer to a function that the event loop will
12498944Sobrien   invoke if the corresponding signal has received. The real signal
12598944Sobrien   handlers mark these functions as ready to be executed and the event
12698944Sobrien   loop, in a later iteration, calls them. See the function
12798944Sobrien   invoke_async_signal_handler. */
12898944Sobrienvoid *sigint_token;
12998944Sobrien#ifdef SIGHUP
13098944Sobrienvoid *sighup_token;
13198944Sobrien#endif
13298944Sobrienvoid *sigquit_token;
13398944Sobrienvoid *sigfpe_token;
13498944Sobrien#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
13598944Sobrienvoid *sigwinch_token;
13698944Sobrien#endif
13798944Sobrien#ifdef STOP_SIGNAL
13898944Sobrienvoid *sigtstp_token;
13998944Sobrien#endif
14098944Sobrien
14198944Sobrien/* Structure to save a partially entered command.  This is used when
14298944Sobrien   the user types '\' at the end of a command line. This is necessary
14398944Sobrien   because each line of input is handled by a different call to
14498944Sobrien   command_line_handler, and normally there is no state retained
14598944Sobrien   between different calls. */
14698944Sobrienint more_to_come = 0;
14798944Sobrien
14898944Sobrienstruct readline_input_state
14998944Sobrien  {
15098944Sobrien    char *linebuffer;
15198944Sobrien    char *linebuffer_ptr;
15298944Sobrien  }
15398944Sobrienreadline_input_state;
15498944Sobrien
15598944Sobrien/* This hook is called by rl_callback_read_char_wrapper after each
15698944Sobrien   character is processed.  */
15798944Sobrienvoid (*after_char_processing_hook) ();
15898944Sobrien
15998944Sobrien
16098944Sobrien/* Wrapper function for calling into the readline library. The event
16198944Sobrien   loop expects the callback function to have a paramter, while readline
16298944Sobrien   expects none. */
16398944Sobrienstatic void
16498944Sobrienrl_callback_read_char_wrapper (gdb_client_data client_data)
16598944Sobrien{
16698944Sobrien  rl_callback_read_char ();
16798944Sobrien  if (after_char_processing_hook)
16898944Sobrien    (*after_char_processing_hook) ();
16998944Sobrien}
17098944Sobrien
17198944Sobrien/* Initialize all the necessary variables, start the event loop,
17298944Sobrien   register readline, and stdin, start the loop. */
17398944Sobrienvoid
17498944Sobriencli_command_loop (void)
17598944Sobrien{
17698944Sobrien  int length;
17798944Sobrien  char *a_prompt;
17898944Sobrien  char *gdb_prompt = get_prompt ();
17998944Sobrien
18098944Sobrien  /* If we are using readline, set things up and display the first
18198944Sobrien     prompt, otherwise just print the prompt. */
18298944Sobrien  if (async_command_editing_p)
18398944Sobrien    {
18498944Sobrien      /* Tell readline what the prompt to display is and what function it
18598944Sobrien         will need to call after a whole line is read. This also displays
18698944Sobrien         the first prompt. */
18798944Sobrien      length = strlen (PREFIX (0)) + strlen (gdb_prompt) + strlen (SUFFIX (0)) + 1;
18898944Sobrien      a_prompt = (char *) xmalloc (length);
18998944Sobrien      strcpy (a_prompt, PREFIX (0));
19098944Sobrien      strcat (a_prompt, gdb_prompt);
19198944Sobrien      strcat (a_prompt, SUFFIX (0));
19298944Sobrien      rl_callback_handler_install (a_prompt, input_handler);
19398944Sobrien    }
19498944Sobrien  else
19598944Sobrien    display_gdb_prompt (0);
19698944Sobrien
19798944Sobrien  /* Now it's time to start the event loop. */
19898944Sobrien  start_event_loop ();
19998944Sobrien}
20098944Sobrien
20198944Sobrien/* Change the function to be invoked every time there is a character
20298944Sobrien   ready on stdin. This is used when the user sets the editing off,
20398944Sobrien   therefore bypassing readline, and letting gdb handle the input
20498944Sobrien   itself, via gdb_readline2. Also it is used in the opposite case in
20598944Sobrien   which the user sets editing on again, by restoring readline
20698944Sobrien   handling of the input. */
20798944Sobrienstatic void
20898944Sobrienchange_line_handler (void)
20998944Sobrien{
21098944Sobrien  /* NOTE: this operates on input_fd, not instream. If we are reading
21198944Sobrien     commands from a file, instream will point to the file. However in
21298944Sobrien     async mode, we always read commands from a file with editing
21398944Sobrien     off. This means that the 'set editing on/off' will have effect
21498944Sobrien     only on the interactive session. */
21598944Sobrien
21698944Sobrien  if (async_command_editing_p)
21798944Sobrien    {
21898944Sobrien      /* Turn on editing by using readline. */
21998944Sobrien      call_readline = rl_callback_read_char_wrapper;
22098944Sobrien      input_handler = command_line_handler;
22198944Sobrien    }
22298944Sobrien  else
22398944Sobrien    {
22498944Sobrien      /* Turn off editing by using gdb_readline2. */
22598944Sobrien      rl_callback_handler_remove ();
22698944Sobrien      call_readline = gdb_readline2;
22798944Sobrien
22898944Sobrien      /* Set up the command handler as well, in case we are called as
22998944Sobrien         first thing from .gdbinit. */
23098944Sobrien      input_handler = command_line_handler;
23198944Sobrien    }
23298944Sobrien}
23398944Sobrien
23498944Sobrien/* Displays the prompt. The prompt that is displayed is the current
23598944Sobrien   top of the prompt stack, if the argument NEW_PROMPT is
23698944Sobrien   0. Otherwise, it displays whatever NEW_PROMPT is. This is used
23798944Sobrien   after each gdb command has completed, and in the following cases:
23898944Sobrien   1. when the user enters a command line which is ended by '\'
23998944Sobrien   indicating that the command will continue on the next line.
24098944Sobrien   In that case the prompt that is displayed is the empty string.
24198944Sobrien   2. When the user is entering 'commands' for a breakpoint, or
24298944Sobrien   actions for a tracepoint. In this case the prompt will be '>'
24398944Sobrien   3. Other????
24498944Sobrien   FIXME: 2. & 3. not implemented yet for async. */
24598944Sobrienvoid
24698944Sobriendisplay_gdb_prompt (char *new_prompt)
24798944Sobrien{
24898944Sobrien  int prompt_length = 0;
24998944Sobrien  char *gdb_prompt = get_prompt ();
25098944Sobrien
251130803Smarcel  /* Each interpreter has its own rules on displaying the command
252130803Smarcel     prompt.  */
253130803Smarcel  if (!current_interp_display_prompt_p ())
25498944Sobrien    return;
25598944Sobrien
25698944Sobrien  if (target_executing && sync_execution)
25798944Sobrien    {
25898944Sobrien      /* This is to trick readline into not trying to display the
25998944Sobrien         prompt.  Even though we display the prompt using this
26098944Sobrien         function, readline still tries to do its own display if we
26198944Sobrien         don't call rl_callback_handler_install and
26298944Sobrien         rl_callback_handler_remove (which readline detects because a
26398944Sobrien         global variable is not set). If readline did that, it could
26498944Sobrien         mess up gdb signal handlers for SIGINT.  Readline assumes
26598944Sobrien         that between calls to rl_set_signals and rl_clear_signals gdb
26698944Sobrien         doesn't do anything with the signal handlers. Well, that's
26798944Sobrien         not the case, because when the target executes we change the
26898944Sobrien         SIGINT signal handler. If we allowed readline to display the
26998944Sobrien         prompt, the signal handler change would happen exactly
27098944Sobrien         between the calls to the above two functions.
27198944Sobrien         Calling rl_callback_handler_remove(), does the job. */
27298944Sobrien
27398944Sobrien      rl_callback_handler_remove ();
27498944Sobrien      return;
27598944Sobrien    }
27698944Sobrien
27798944Sobrien  if (!new_prompt)
27898944Sobrien    {
27998944Sobrien      /* Just use the top of the prompt stack. */
28098944Sobrien      prompt_length = strlen (PREFIX (0)) +
28198944Sobrien	strlen (SUFFIX (0)) +
28298944Sobrien	strlen (gdb_prompt) + 1;
28398944Sobrien
28498944Sobrien      new_prompt = (char *) alloca (prompt_length);
28598944Sobrien
28698944Sobrien      /* Prefix needs to have new line at end. */
28798944Sobrien      strcpy (new_prompt, PREFIX (0));
28898944Sobrien      strcat (new_prompt, gdb_prompt);
28998944Sobrien      /* Suffix needs to have a new line at end and \032 \032 at
29098944Sobrien         beginning. */
29198944Sobrien      strcat (new_prompt, SUFFIX (0));
29298944Sobrien    }
29398944Sobrien
29498944Sobrien  if (async_command_editing_p)
29598944Sobrien    {
29698944Sobrien      rl_callback_handler_remove ();
29798944Sobrien      rl_callback_handler_install (new_prompt, input_handler);
29898944Sobrien    }
29998944Sobrien  /* new_prompt at this point can be the top of the stack or the one passed in */
30098944Sobrien  else if (new_prompt)
30198944Sobrien    {
30298944Sobrien      /* Don't use a _filtered function here.  It causes the assumed
30398944Sobrien         character position to be off, since the newline we read from
30498944Sobrien         the user is not accounted for.  */
30598944Sobrien      fputs_unfiltered (new_prompt, gdb_stdout);
30698944Sobrien      gdb_flush (gdb_stdout);
30798944Sobrien    }
30898944Sobrien}
30998944Sobrien
31098944Sobrien/* Used when the user requests a different annotation level, with
31198944Sobrien   'set annotate'. It pushes a new prompt (with prefix and suffix) on top
31298944Sobrien   of the prompt stack, if the annotation level desired is 2, otherwise
31398944Sobrien   it pops the top of the prompt stack when we want the annotation level
31498944Sobrien   to be the normal ones (1 or 0). */
31598944Sobrienstatic void
31698944Sobrienchange_annotation_level (void)
31798944Sobrien{
31898944Sobrien  char *prefix, *suffix;
31998944Sobrien
32098944Sobrien  if (!PREFIX (0) || !PROMPT (0) || !SUFFIX (0))
32198944Sobrien    {
32298944Sobrien      /* The prompt stack has not been initialized to "", we are
32398944Sobrien         using gdb w/o the --async switch */
32498944Sobrien      warning ("Command has same effect as set annotate");
32598944Sobrien      return;
32698944Sobrien    }
32798944Sobrien
32898944Sobrien  if (annotation_level > 1)
32998944Sobrien    {
33098944Sobrien      if (!strcmp (PREFIX (0), "") && !strcmp (SUFFIX (0), ""))
33198944Sobrien	{
33298944Sobrien	  /* Push a new prompt if the previous annotation_level was not >1. */
33398944Sobrien	  prefix = (char *) alloca (strlen (async_annotation_suffix) + 10);
33498944Sobrien	  strcpy (prefix, "\n\032\032pre-");
33598944Sobrien	  strcat (prefix, async_annotation_suffix);
33698944Sobrien	  strcat (prefix, "\n");
33798944Sobrien
33898944Sobrien	  suffix = (char *) alloca (strlen (async_annotation_suffix) + 6);
33998944Sobrien	  strcpy (suffix, "\n\032\032");
34098944Sobrien	  strcat (suffix, async_annotation_suffix);
34198944Sobrien	  strcat (suffix, "\n");
34298944Sobrien
34398944Sobrien	  push_prompt (prefix, (char *) 0, suffix);
34498944Sobrien	}
34598944Sobrien    }
34698944Sobrien  else
34798944Sobrien    {
34898944Sobrien      if (strcmp (PREFIX (0), "") && strcmp (SUFFIX (0), ""))
34998944Sobrien	{
35098944Sobrien	  /* Pop the top of the stack, we are going back to annotation < 1. */
35198944Sobrien	  pop_prompt ();
35298944Sobrien	}
35398944Sobrien    }
35498944Sobrien}
35598944Sobrien
35698944Sobrien/* Pushes a new prompt on the prompt stack. Each prompt has three
35798944Sobrien   parts: prefix, prompt, suffix. Usually prefix and suffix are empty
35898944Sobrien   strings, except when the annotation level is 2. Memory is allocated
35998944Sobrien   within savestring for the new prompt. */
36098944Sobrienvoid
36198944Sobrienpush_prompt (char *prefix, char *prompt, char *suffix)
36298944Sobrien{
36398944Sobrien  the_prompts.top++;
36498944Sobrien  PREFIX (0) = savestring (prefix, strlen (prefix));
36598944Sobrien
36698944Sobrien  /* Note that this function is used by the set annotate 2
36798944Sobrien     command. This is why we take care of saving the old prompt
36898944Sobrien     in case a new one is not specified. */
36998944Sobrien  if (prompt)
37098944Sobrien    PROMPT (0) = savestring (prompt, strlen (prompt));
37198944Sobrien  else
37298944Sobrien    PROMPT (0) = savestring (PROMPT (-1), strlen (PROMPT (-1)));
37398944Sobrien
37498944Sobrien  SUFFIX (0) = savestring (suffix, strlen (suffix));
37598944Sobrien}
37698944Sobrien
37798944Sobrien/* Pops the top of the prompt stack, and frees the memory allocated for it. */
37898944Sobrienvoid
37998944Sobrienpop_prompt (void)
38098944Sobrien{
38198944Sobrien  /* If we are not during a 'synchronous' execution command, in which
38298944Sobrien     case, the top prompt would be empty. */
38398944Sobrien  if (strcmp (PROMPT (0), ""))
38498944Sobrien    /* This is for the case in which the prompt is set while the
38598944Sobrien       annotation level is 2. The top prompt will be changed, but when
38698944Sobrien       we return to annotation level < 2, we want that new prompt to be
38798944Sobrien       in effect, until the user does another 'set prompt'. */
38898944Sobrien    if (strcmp (PROMPT (0), PROMPT (-1)))
38998944Sobrien      {
39098944Sobrien	xfree (PROMPT (-1));
39198944Sobrien	PROMPT (-1) = savestring (PROMPT (0), strlen (PROMPT (0)));
39298944Sobrien      }
39398944Sobrien
39498944Sobrien  xfree (PREFIX (0));
39598944Sobrien  xfree (PROMPT (0));
39698944Sobrien  xfree (SUFFIX (0));
39798944Sobrien  the_prompts.top--;
39898944Sobrien}
39998944Sobrien
40098944Sobrien/* When there is an event ready on the stdin file desriptor, instead
40198944Sobrien   of calling readline directly throught the callback function, or
40298944Sobrien   instead of calling gdb_readline2, give gdb a chance to detect
40398944Sobrien   errors and do something. */
40498944Sobrienvoid
40598944Sobrienstdin_event_handler (int error, gdb_client_data client_data)
40698944Sobrien{
40798944Sobrien  if (error)
40898944Sobrien    {
40998944Sobrien      printf_unfiltered ("error detected on stdin\n");
41098944Sobrien      delete_file_handler (input_fd);
41198944Sobrien      discard_all_continuations ();
41298944Sobrien      /* If stdin died, we may as well kill gdb. */
41398944Sobrien      quit_command ((char *) 0, stdin == instream);
41498944Sobrien    }
41598944Sobrien  else
41698944Sobrien    (*call_readline) (client_data);
41798944Sobrien}
41898944Sobrien
41998944Sobrien/* Re-enable stdin after the end of an execution command in
42098944Sobrien   synchronous mode, or after an error from the target, and we aborted
42198944Sobrien   the exec operation. */
42298944Sobrien
42398944Sobrienvoid
42498944Sobrienasync_enable_stdin (void *dummy)
42598944Sobrien{
42698944Sobrien  /* See NOTE in async_disable_stdin() */
42798944Sobrien  /* FIXME: cagney/1999-09-27: Call this before clearing
42898944Sobrien     sync_execution.  Current target_terminal_ours() implementations
42998944Sobrien     check for sync_execution before switching the terminal. */
43098944Sobrien  target_terminal_ours ();
43198944Sobrien  pop_prompt ();
43298944Sobrien  sync_execution = 0;
43398944Sobrien}
43498944Sobrien
43598944Sobrien/* Disable reads from stdin (the console) marking the command as
43698944Sobrien   synchronous. */
43798944Sobrien
43898944Sobrienvoid
43998944Sobrienasync_disable_stdin (void)
44098944Sobrien{
44198944Sobrien  sync_execution = 1;
44298944Sobrien  push_prompt ("", "", "");
44398944Sobrien  /* FIXME: cagney/1999-09-27: At present this call is technically
44498944Sobrien     redundant since infcmd.c and infrun.c both already call
44598944Sobrien     target_terminal_inferior().  As the terminal handling (in
44698944Sobrien     sync/async mode) is refined, the duplicate calls can be
44798944Sobrien     eliminated (Here or in infcmd.c/infrun.c). */
44898944Sobrien  target_terminal_inferior ();
44998944Sobrien  /* Add the reinstate of stdin to the list of cleanups to be done
45098944Sobrien     in case the target errors out and dies. These cleanups are also
45198944Sobrien     done in case of normal successful termination of the execution
45298944Sobrien     command, by complete_execution(). */
45398944Sobrien  make_exec_error_cleanup (async_enable_stdin, NULL);
45498944Sobrien}
45598944Sobrien
45698944Sobrien
45798944Sobrien/* Handles a gdb command. This function is called by
45898944Sobrien   command_line_handler, which has processed one or more input lines
45998944Sobrien   into COMMAND. */
46098944Sobrien/* NOTE: 1999-04-30 This is the asynchronous version of the command_loop
46198944Sobrien   function.  The command_loop function will be obsolete when we
46298944Sobrien   switch to use the event loop at every execution of gdb. */
46398944Sobrienstatic void
46498944Sobriencommand_handler (char *command)
46598944Sobrien{
46698944Sobrien  struct cleanup *old_chain;
46798944Sobrien  int stdin_is_tty = ISATTY (stdin);
46898944Sobrien  struct continuation_arg *arg1;
46998944Sobrien  struct continuation_arg *arg2;
47098944Sobrien  long time_at_cmd_start;
47198944Sobrien#ifdef HAVE_SBRK
47298944Sobrien  long space_at_cmd_start = 0;
47398944Sobrien#endif
47498944Sobrien  extern int display_time;
47598944Sobrien  extern int display_space;
47698944Sobrien
47798944Sobrien  quit_flag = 0;
47898944Sobrien  if (instream == stdin && stdin_is_tty)
47998944Sobrien    reinitialize_more_filter ();
48098944Sobrien  old_chain = make_cleanup (null_cleanup, 0);
48198944Sobrien
48298944Sobrien  /* If readline returned a NULL command, it means that the
48398944Sobrien     connection with the terminal is gone. This happens at the
48498944Sobrien     end of a testsuite run, after Expect has hung up
48598944Sobrien     but GDB is still alive. In such a case, we just quit gdb
48698944Sobrien     killing the inferior program too. */
48798944Sobrien  if (command == 0)
48898944Sobrien    quit_command ((char *) 0, stdin == instream);
48998944Sobrien
49098944Sobrien  time_at_cmd_start = get_run_time ();
49198944Sobrien
49298944Sobrien  if (display_space)
49398944Sobrien    {
49498944Sobrien#ifdef HAVE_SBRK
49598944Sobrien      char *lim = (char *) sbrk (0);
496130803Smarcel      space_at_cmd_start = lim - lim_at_start;
49798944Sobrien#endif
49898944Sobrien    }
49998944Sobrien
50098944Sobrien  execute_command (command, instream == stdin);
50198944Sobrien
50298944Sobrien  /* Set things up for this function to be compete later, once the
50398944Sobrien     execution has completed, if we are doing an execution command,
50498944Sobrien     otherwise, just go ahead and finish. */
50598944Sobrien  if (target_can_async_p () && target_executing)
50698944Sobrien    {
50798944Sobrien      arg1 =
50898944Sobrien	(struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
50998944Sobrien      arg2 =
51098944Sobrien	(struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
51198944Sobrien      arg1->next = arg2;
51298944Sobrien      arg2->next = NULL;
51398944Sobrien      arg1->data.longint = time_at_cmd_start;
51498944Sobrien#ifdef HAVE_SBRK
51598944Sobrien      arg2->data.longint = space_at_cmd_start;
51698944Sobrien#endif
51798944Sobrien      add_continuation (command_line_handler_continuation, arg1);
51898944Sobrien    }
51998944Sobrien
52098944Sobrien  /* Do any commands attached to breakpoint we stopped at. Only if we
52198944Sobrien     are always running synchronously. Or if we have just executed a
52298944Sobrien     command that doesn't start the target. */
52398944Sobrien  if (!target_can_async_p () || !target_executing)
52498944Sobrien    {
52598944Sobrien      bpstat_do_actions (&stop_bpstat);
52698944Sobrien      do_cleanups (old_chain);
52798944Sobrien
52898944Sobrien      if (display_time)
52998944Sobrien	{
53098944Sobrien	  long cmd_time = get_run_time () - time_at_cmd_start;
53198944Sobrien
53298944Sobrien	  printf_unfiltered ("Command execution time: %ld.%06ld\n",
53398944Sobrien			     cmd_time / 1000000, cmd_time % 1000000);
53498944Sobrien	}
53598944Sobrien
53698944Sobrien      if (display_space)
53798944Sobrien	{
53898944Sobrien#ifdef HAVE_SBRK
53998944Sobrien	  char *lim = (char *) sbrk (0);
540130803Smarcel	  long space_now = lim - lim_at_start;
54198944Sobrien	  long space_diff = space_now - space_at_cmd_start;
54298944Sobrien
54398944Sobrien	  printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
54498944Sobrien			     space_now,
54598944Sobrien			     (space_diff >= 0 ? '+' : '-'),
54698944Sobrien			     space_diff);
54798944Sobrien#endif
54898944Sobrien	}
54998944Sobrien    }
55098944Sobrien}
55198944Sobrien
55298944Sobrien/* Do any commands attached to breakpoint we stopped at. Only if we
55398944Sobrien   are always running synchronously. Or if we have just executed a
55498944Sobrien   command that doesn't start the target. */
55598944Sobrienvoid
55698944Sobriencommand_line_handler_continuation (struct continuation_arg *arg)
55798944Sobrien{
55898944Sobrien  extern int display_time;
55998944Sobrien  extern int display_space;
56098944Sobrien
56198944Sobrien  long time_at_cmd_start  = arg->data.longint;
56298944Sobrien  long space_at_cmd_start = arg->next->data.longint;
56398944Sobrien
56498944Sobrien  bpstat_do_actions (&stop_bpstat);
56598944Sobrien  /*do_cleanups (old_chain); *//*?????FIXME????? */
56698944Sobrien
56798944Sobrien  if (display_time)
56898944Sobrien    {
56998944Sobrien      long cmd_time = get_run_time () - time_at_cmd_start;
57098944Sobrien
57198944Sobrien      printf_unfiltered ("Command execution time: %ld.%06ld\n",
57298944Sobrien			 cmd_time / 1000000, cmd_time % 1000000);
57398944Sobrien    }
57498944Sobrien  if (display_space)
57598944Sobrien    {
57698944Sobrien#ifdef HAVE_SBRK
57798944Sobrien      char *lim = (char *) sbrk (0);
578130803Smarcel      long space_now = lim - lim_at_start;
57998944Sobrien      long space_diff = space_now - space_at_cmd_start;
58098944Sobrien
58198944Sobrien      printf_unfiltered ("Space used: %ld (%c%ld for this command)\n",
58298944Sobrien			 space_now,
58398944Sobrien			 (space_diff >= 0 ? '+' : '-'),
58498944Sobrien			 space_diff);
58598944Sobrien#endif
58698944Sobrien    }
58798944Sobrien}
58898944Sobrien
58998944Sobrien/* Handle a complete line of input. This is called by the callback
59098944Sobrien   mechanism within the readline library.  Deal with incomplete commands
59198944Sobrien   as well, by saving the partial input in a global buffer.  */
59298944Sobrien
59398944Sobrien/* NOTE: 1999-04-30 This is the asynchronous version of the
59498944Sobrien   command_line_input function. command_line_input will become
59598944Sobrien   obsolete once we use the event loop as the default mechanism in
59698944Sobrien   GDB. */
59798944Sobrienstatic void
59898944Sobriencommand_line_handler (char *rl)
59998944Sobrien{
60098944Sobrien  static char *linebuffer = 0;
60198944Sobrien  static unsigned linelength = 0;
602130803Smarcel  char *p;
60398944Sobrien  char *p1;
60498944Sobrien  extern char *line;
60598944Sobrien  extern int linesize;
60698944Sobrien  char *nline;
60798944Sobrien  char got_eof = 0;
60898944Sobrien
60998944Sobrien
61098944Sobrien  int repeat = (instream == stdin);
61198944Sobrien
61298944Sobrien  if (annotation_level > 1 && instream == stdin)
61398944Sobrien    {
61498944Sobrien      printf_unfiltered ("\n\032\032post-");
615130803Smarcel      puts_unfiltered (async_annotation_suffix);
61698944Sobrien      printf_unfiltered ("\n");
61798944Sobrien    }
61898944Sobrien
61998944Sobrien  if (linebuffer == 0)
62098944Sobrien    {
62198944Sobrien      linelength = 80;
62298944Sobrien      linebuffer = (char *) xmalloc (linelength);
62398944Sobrien    }
62498944Sobrien
62598944Sobrien  p = linebuffer;
62698944Sobrien
62798944Sobrien  if (more_to_come)
62898944Sobrien    {
62998944Sobrien      strcpy (linebuffer, readline_input_state.linebuffer);
63098944Sobrien      p = readline_input_state.linebuffer_ptr;
63198944Sobrien      xfree (readline_input_state.linebuffer);
63298944Sobrien      more_to_come = 0;
63398944Sobrien      pop_prompt ();
63498944Sobrien    }
63598944Sobrien
63698944Sobrien#ifdef STOP_SIGNAL
63798944Sobrien  if (job_control)
63898944Sobrien    signal (STOP_SIGNAL, handle_stop_sig);
63998944Sobrien#endif
64098944Sobrien
64198944Sobrien  /* Make sure that all output has been output.  Some machines may let
64298944Sobrien     you get away with leaving out some of the gdb_flush, but not all.  */
64398944Sobrien  wrap_here ("");
64498944Sobrien  gdb_flush (gdb_stdout);
64598944Sobrien  gdb_flush (gdb_stderr);
64698944Sobrien
64798944Sobrien  if (source_file_name != NULL)
64898944Sobrien    {
64998944Sobrien      ++source_line_number;
65098944Sobrien      sprintf (source_error,
65198944Sobrien	       "%s%s:%d: Error in sourced command file:\n",
65298944Sobrien	       source_pre_error,
65398944Sobrien	       source_file_name,
65498944Sobrien	       source_line_number);
65598944Sobrien      error_pre_print = source_error;
65698944Sobrien    }
65798944Sobrien
65898944Sobrien  /* If we are in this case, then command_handler will call quit
65998944Sobrien     and exit from gdb. */
66098944Sobrien  if (!rl || rl == (char *) EOF)
66198944Sobrien    {
66298944Sobrien      got_eof = 1;
66398944Sobrien      command_handler (0);
66498944Sobrien    }
66598944Sobrien  if (strlen (rl) + 1 + (p - linebuffer) > linelength)
66698944Sobrien    {
66798944Sobrien      linelength = strlen (rl) + 1 + (p - linebuffer);
66898944Sobrien      nline = (char *) xrealloc (linebuffer, linelength);
66998944Sobrien      p += nline - linebuffer;
67098944Sobrien      linebuffer = nline;
67198944Sobrien    }
67298944Sobrien  p1 = rl;
67398944Sobrien  /* Copy line.  Don't copy null at end.  (Leaves line alone
67498944Sobrien     if this was just a newline)  */
67598944Sobrien  while (*p1)
67698944Sobrien    *p++ = *p1++;
67798944Sobrien
67898944Sobrien  xfree (rl);			/* Allocated in readline.  */
67998944Sobrien
680130803Smarcel  if (p > linebuffer && *(p - 1) == '\\')
68198944Sobrien    {
68298944Sobrien      p--;			/* Put on top of '\'.  */
68398944Sobrien
684130803Smarcel      readline_input_state.linebuffer = savestring (linebuffer,
685130803Smarcel						    strlen (linebuffer));
686130803Smarcel      readline_input_state.linebuffer_ptr = p;
68798944Sobrien
688130803Smarcel      /* We will not invoke a execute_command if there is more
689130803Smarcel	 input expected to complete the command. So, we need to
690130803Smarcel	 print an empty prompt here. */
691130803Smarcel      more_to_come = 1;
692130803Smarcel      push_prompt ("", "", "");
693130803Smarcel      display_gdb_prompt (0);
694130803Smarcel      return;
69598944Sobrien    }
69698944Sobrien
69798944Sobrien#ifdef STOP_SIGNAL
69898944Sobrien  if (job_control)
69998944Sobrien    signal (STOP_SIGNAL, SIG_DFL);
70098944Sobrien#endif
70198944Sobrien
70298944Sobrien#define SERVER_COMMAND_LENGTH 7
70398944Sobrien  server_command =
70498944Sobrien    (p - linebuffer > SERVER_COMMAND_LENGTH)
705130803Smarcel    && strncmp (linebuffer, "server ", SERVER_COMMAND_LENGTH) == 0;
70698944Sobrien  if (server_command)
70798944Sobrien    {
70898944Sobrien      /* Note that we don't set `line'.  Between this and the check in
70998944Sobrien         dont_repeat, this insures that repeating will still do the
71098944Sobrien         right thing.  */
71198944Sobrien      *p = '\0';
71298944Sobrien      command_handler (linebuffer + SERVER_COMMAND_LENGTH);
71398944Sobrien      display_gdb_prompt (0);
71498944Sobrien      return;
71598944Sobrien    }
71698944Sobrien
71798944Sobrien  /* Do history expansion if that is wished.  */
71898944Sobrien  if (history_expansion_p && instream == stdin
71998944Sobrien      && ISATTY (instream))
72098944Sobrien    {
72198944Sobrien      char *history_value;
72298944Sobrien      int expanded;
72398944Sobrien
72498944Sobrien      *p = '\0';		/* Insert null now.  */
72598944Sobrien      expanded = history_expand (linebuffer, &history_value);
72698944Sobrien      if (expanded)
72798944Sobrien	{
72898944Sobrien	  /* Print the changes.  */
72998944Sobrien	  printf_unfiltered ("%s\n", history_value);
73098944Sobrien
73198944Sobrien	  /* If there was an error, call this function again.  */
73298944Sobrien	  if (expanded < 0)
73398944Sobrien	    {
73498944Sobrien	      xfree (history_value);
73598944Sobrien	      return;
73698944Sobrien	    }
73798944Sobrien	  if (strlen (history_value) > linelength)
73898944Sobrien	    {
73998944Sobrien	      linelength = strlen (history_value) + 1;
74098944Sobrien	      linebuffer = (char *) xrealloc (linebuffer, linelength);
74198944Sobrien	    }
74298944Sobrien	  strcpy (linebuffer, history_value);
74398944Sobrien	  p = linebuffer + strlen (linebuffer);
74498944Sobrien	  xfree (history_value);
74598944Sobrien	}
74698944Sobrien    }
74798944Sobrien
74898944Sobrien  /* If we just got an empty line, and that is supposed
74998944Sobrien     to repeat the previous command, return the value in the
75098944Sobrien     global buffer.  */
75198944Sobrien  if (repeat && p == linebuffer && *p != '\\')
75298944Sobrien    {
75398944Sobrien      command_handler (line);
75498944Sobrien      display_gdb_prompt (0);
75598944Sobrien      return;
75698944Sobrien    }
75798944Sobrien
75898944Sobrien  for (p1 = linebuffer; *p1 == ' ' || *p1 == '\t'; p1++);
75998944Sobrien  if (repeat && !*p1)
76098944Sobrien    {
76198944Sobrien      command_handler (line);
76298944Sobrien      display_gdb_prompt (0);
76398944Sobrien      return;
76498944Sobrien    }
76598944Sobrien
76698944Sobrien  *p = 0;
76798944Sobrien
76898944Sobrien  /* Add line to history if appropriate.  */
76998944Sobrien  if (instream == stdin
77098944Sobrien      && ISATTY (stdin) && *linebuffer)
77198944Sobrien    add_history (linebuffer);
77298944Sobrien
77398944Sobrien  /* Note: lines consisting solely of comments are added to the command
77498944Sobrien     history.  This is useful when you type a command, and then
77598944Sobrien     realize you don't want to execute it quite yet.  You can comment
77698944Sobrien     out the command and then later fetch it from the value history
77798944Sobrien     and remove the '#'.  The kill ring is probably better, but some
77898944Sobrien     people are in the habit of commenting things out.  */
77998944Sobrien  if (*p1 == '#')
78098944Sobrien    *p1 = '\0';			/* Found a comment. */
78198944Sobrien
78298944Sobrien  /* Save into global buffer if appropriate.  */
78398944Sobrien  if (repeat)
78498944Sobrien    {
78598944Sobrien      if (linelength > linesize)
78698944Sobrien	{
78798944Sobrien	  line = xrealloc (line, linelength);
78898944Sobrien	  linesize = linelength;
78998944Sobrien	}
79098944Sobrien      strcpy (line, linebuffer);
79198944Sobrien      if (!more_to_come)
79298944Sobrien	{
79398944Sobrien	  command_handler (line);
79498944Sobrien	  display_gdb_prompt (0);
79598944Sobrien	}
79698944Sobrien      return;
79798944Sobrien    }
79898944Sobrien
79998944Sobrien  command_handler (linebuffer);
80098944Sobrien  display_gdb_prompt (0);
80198944Sobrien  return;
80298944Sobrien}
80398944Sobrien
80498944Sobrien/* Does reading of input from terminal w/o the editing features
80598944Sobrien   provided by the readline library. */
80698944Sobrien
80798944Sobrien/* NOTE: 1999-04-30 Asynchronous version of gdb_readline. gdb_readline
80898944Sobrien   will become obsolete when the event loop is made the default
80998944Sobrien   execution for gdb. */
81098944Sobrienvoid
81198944Sobriengdb_readline2 (gdb_client_data client_data)
81298944Sobrien{
81398944Sobrien  int c;
81498944Sobrien  char *result;
81598944Sobrien  int input_index = 0;
81698944Sobrien  int result_size = 80;
81798944Sobrien  static int done_once = 0;
81898944Sobrien
81998944Sobrien  /* Unbuffer the input stream, so that, later on, the calls to fgetc
82098944Sobrien     fetch only one char at the time from the stream. The fgetc's will
82198944Sobrien     get up to the first newline, but there may be more chars in the
82298944Sobrien     stream after '\n'. If we buffer the input and fgetc drains the
82398944Sobrien     stream, getting stuff beyond the newline as well, a select, done
82498944Sobrien     afterwards will not trigger. */
82598944Sobrien  if (!done_once && !ISATTY (instream))
82698944Sobrien    {
82798944Sobrien      setbuf (instream, NULL);
82898944Sobrien      done_once = 1;
82998944Sobrien    }
83098944Sobrien
83198944Sobrien  result = (char *) xmalloc (result_size);
83298944Sobrien
83398944Sobrien  /* We still need the while loop here, even though it would seem
83498944Sobrien     obvious to invoke gdb_readline2 at every character entered.  If
83598944Sobrien     not using the readline library, the terminal is in cooked mode,
83698944Sobrien     which sends the characters all at once. Poll will notice that the
83798944Sobrien     input fd has changed state only after enter is pressed. At this
83898944Sobrien     point we still need to fetch all the chars entered. */
83998944Sobrien
84098944Sobrien  while (1)
84198944Sobrien    {
84298944Sobrien      /* Read from stdin if we are executing a user defined command.
84398944Sobrien         This is the right thing for prompt_for_continue, at least.  */
84498944Sobrien      c = fgetc (instream ? instream : stdin);
84598944Sobrien
84698944Sobrien      if (c == EOF)
84798944Sobrien	{
84898944Sobrien	  if (input_index > 0)
84998944Sobrien	    /* The last line does not end with a newline.  Return it, and
85098944Sobrien	       if we are called again fgetc will still return EOF and
85198944Sobrien	       we'll return NULL then.  */
85298944Sobrien	    break;
85398944Sobrien	  xfree (result);
85498944Sobrien	  (*input_handler) (0);
85598944Sobrien	}
85698944Sobrien
85798944Sobrien      if (c == '\n')
85898944Sobrien#ifndef CRLF_SOURCE_FILES
85998944Sobrien	break;
86098944Sobrien#else
86198944Sobrien	{
86298944Sobrien	  if (input_index > 0 && result[input_index - 1] == '\r')
86398944Sobrien	    input_index--;
86498944Sobrien	  break;
86598944Sobrien	}
86698944Sobrien#endif
86798944Sobrien
86898944Sobrien      result[input_index++] = c;
86998944Sobrien      while (input_index >= result_size)
87098944Sobrien	{
87198944Sobrien	  result_size *= 2;
87298944Sobrien	  result = (char *) xrealloc (result, result_size);
87398944Sobrien	}
87498944Sobrien    }
87598944Sobrien
87698944Sobrien  result[input_index++] = '\0';
87798944Sobrien  (*input_handler) (result);
87898944Sobrien}
87998944Sobrien
88098944Sobrien
88198944Sobrien/* Initialization of signal handlers and tokens.  There is a function
88298944Sobrien   handle_sig* for each of the signals GDB cares about. Specifically:
88398944Sobrien   SIGINT, SIGFPE, SIGQUIT, SIGTSTP, SIGHUP, SIGWINCH.  These
88498944Sobrien   functions are the actual signal handlers associated to the signals
88598944Sobrien   via calls to signal().  The only job for these functions is to
88698944Sobrien   enqueue the appropriate event/procedure with the event loop.  Such
88798944Sobrien   procedures are the old signal handlers. The event loop will take
88898944Sobrien   care of invoking the queued procedures to perform the usual tasks
88998944Sobrien   associated with the reception of the signal. */
89098944Sobrien/* NOTE: 1999-04-30 This is the asynchronous version of init_signals.
89198944Sobrien   init_signals will become obsolete as we move to have to event loop
89298944Sobrien   as the default for gdb. */
89398944Sobrienvoid
89498944Sobrienasync_init_signals (void)
89598944Sobrien{
89698944Sobrien  signal (SIGINT, handle_sigint);
89798944Sobrien  sigint_token =
89898944Sobrien    create_async_signal_handler (async_request_quit, NULL);
89998944Sobrien
90098944Sobrien  /* If SIGTRAP was set to SIG_IGN, then the SIG_IGN will get passed
90198944Sobrien     to the inferior and breakpoints will be ignored.  */
90298944Sobrien#ifdef SIGTRAP
90398944Sobrien  signal (SIGTRAP, SIG_DFL);
90498944Sobrien#endif
90598944Sobrien
90698944Sobrien  /* If we initialize SIGQUIT to SIG_IGN, then the SIG_IGN will get
90798944Sobrien     passed to the inferior, which we don't want.  It would be
90898944Sobrien     possible to do a "signal (SIGQUIT, SIG_DFL)" after we fork, but
90998944Sobrien     on BSD4.3 systems using vfork, that can affect the
91098944Sobrien     GDB process as well as the inferior (the signal handling tables
91198944Sobrien     might be in memory, shared between the two).  Since we establish
91298944Sobrien     a handler for SIGQUIT, when we call exec it will set the signal
91398944Sobrien     to SIG_DFL for us.  */
91498944Sobrien  signal (SIGQUIT, handle_sigquit);
91598944Sobrien  sigquit_token =
91698944Sobrien    create_async_signal_handler (async_do_nothing, NULL);
91798944Sobrien#ifdef SIGHUP
91898944Sobrien  if (signal (SIGHUP, handle_sighup) != SIG_IGN)
91998944Sobrien    sighup_token =
92098944Sobrien      create_async_signal_handler (async_disconnect, NULL);
92198944Sobrien  else
92298944Sobrien    sighup_token =
92398944Sobrien      create_async_signal_handler (async_do_nothing, NULL);
92498944Sobrien#endif
92598944Sobrien  signal (SIGFPE, handle_sigfpe);
92698944Sobrien  sigfpe_token =
92798944Sobrien    create_async_signal_handler (async_float_handler, NULL);
92898944Sobrien
92998944Sobrien#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
93098944Sobrien  signal (SIGWINCH, handle_sigwinch);
93198944Sobrien  sigwinch_token =
93298944Sobrien    create_async_signal_handler (SIGWINCH_HANDLER, NULL);
93398944Sobrien#endif
93498944Sobrien#ifdef STOP_SIGNAL
93598944Sobrien  sigtstp_token =
93698944Sobrien    create_async_signal_handler (async_stop_sig, NULL);
93798944Sobrien#endif
93898944Sobrien
93998944Sobrien}
94098944Sobrien
94198944Sobrienvoid
94298944Sobrienmark_async_signal_handler_wrapper (void *token)
94398944Sobrien{
94498944Sobrien  mark_async_signal_handler ((struct async_signal_handler *) token);
94598944Sobrien}
94698944Sobrien
94798944Sobrien/* Tell the event loop what to do if SIGINT is received.
94898944Sobrien   See event-signal.c. */
94998944Sobrienvoid
95098944Sobrienhandle_sigint (int sig)
95198944Sobrien{
95298944Sobrien  signal (sig, handle_sigint);
95398944Sobrien
95498944Sobrien  /* If immediate_quit is set, we go ahead and process the SIGINT right
95598944Sobrien     away, even if we usually would defer this to the event loop. The
95698944Sobrien     assumption here is that it is safe to process ^C immediately if
95798944Sobrien     immediate_quit is set. If we didn't, SIGINT would be really
95898944Sobrien     processed only the next time through the event loop.  To get to
95998944Sobrien     that point, though, the command that we want to interrupt needs to
96098944Sobrien     finish first, which is unacceptable. */
96198944Sobrien  if (immediate_quit)
96298944Sobrien    async_request_quit (0);
96398944Sobrien  else
96498944Sobrien    /* If immediate quit is not set, we process SIGINT the next time
96598944Sobrien       through the loop, which is fine. */
96698944Sobrien    mark_async_signal_handler_wrapper (sigint_token);
96798944Sobrien}
96898944Sobrien
96998944Sobrien/* Do the quit. All the checks have been done by the caller. */
97098944Sobrienvoid
97198944Sobrienasync_request_quit (gdb_client_data arg)
97298944Sobrien{
97398944Sobrien  quit_flag = 1;
97498944Sobrien  quit ();
97598944Sobrien}
97698944Sobrien
97798944Sobrien/* Tell the event loop what to do if SIGQUIT is received.
97898944Sobrien   See event-signal.c. */
97998944Sobrienstatic void
98098944Sobrienhandle_sigquit (int sig)
98198944Sobrien{
98298944Sobrien  mark_async_signal_handler_wrapper (sigquit_token);
98398944Sobrien  signal (sig, handle_sigquit);
98498944Sobrien}
98598944Sobrien
98698944Sobrien/* Called by the event loop in response to a SIGQUIT. */
98798944Sobrienstatic void
98898944Sobrienasync_do_nothing (gdb_client_data arg)
98998944Sobrien{
99098944Sobrien  /* Empty function body. */
99198944Sobrien}
99298944Sobrien
99398944Sobrien#ifdef SIGHUP
99498944Sobrien/* Tell the event loop what to do if SIGHUP is received.
99598944Sobrien   See event-signal.c. */
99698944Sobrienstatic void
99798944Sobrienhandle_sighup (int sig)
99898944Sobrien{
99998944Sobrien  mark_async_signal_handler_wrapper (sighup_token);
100098944Sobrien  signal (sig, handle_sighup);
100198944Sobrien}
100298944Sobrien
100398944Sobrien/* Called by the event loop to process a SIGHUP */
100498944Sobrienstatic void
100598944Sobrienasync_disconnect (gdb_client_data arg)
100698944Sobrien{
100798944Sobrien  catch_errors (quit_cover, NULL,
100898944Sobrien		"Could not kill the program being debugged",
100998944Sobrien		RETURN_MASK_ALL);
101098944Sobrien  signal (SIGHUP, SIG_DFL);	/*FIXME: ??????????? */
101198944Sobrien  kill (getpid (), SIGHUP);
101298944Sobrien}
101398944Sobrien#endif
101498944Sobrien
101598944Sobrien#ifdef STOP_SIGNAL
101698944Sobrienvoid
101798944Sobrienhandle_stop_sig (int sig)
101898944Sobrien{
101998944Sobrien  mark_async_signal_handler_wrapper (sigtstp_token);
102098944Sobrien  signal (sig, handle_stop_sig);
102198944Sobrien}
102298944Sobrien
102398944Sobrienstatic void
102498944Sobrienasync_stop_sig (gdb_client_data arg)
102598944Sobrien{
102698944Sobrien  char *prompt = get_prompt ();
102798944Sobrien#if STOP_SIGNAL == SIGTSTP
102898944Sobrien  signal (SIGTSTP, SIG_DFL);
102998944Sobrien#if HAVE_SIGPROCMASK
103098944Sobrien  {
103198944Sobrien    sigset_t zero;
103298944Sobrien
103398944Sobrien    sigemptyset (&zero);
103498944Sobrien    sigprocmask (SIG_SETMASK, &zero, 0);
103598944Sobrien  }
103698944Sobrien#elif HAVE_SIGSETMASK
103798944Sobrien  sigsetmask (0);
103898944Sobrien#endif
103998944Sobrien  kill (getpid (), SIGTSTP);
104098944Sobrien  signal (SIGTSTP, handle_stop_sig);
104198944Sobrien#else
104298944Sobrien  signal (STOP_SIGNAL, handle_stop_sig);
104398944Sobrien#endif
104498944Sobrien  printf_unfiltered ("%s", prompt);
104598944Sobrien  gdb_flush (gdb_stdout);
104698944Sobrien
104798944Sobrien  /* Forget about any previous command -- null line now will do nothing.  */
104898944Sobrien  dont_repeat ();
104998944Sobrien}
105098944Sobrien#endif /* STOP_SIGNAL */
105198944Sobrien
105298944Sobrien/* Tell the event loop what to do if SIGFPE is received.
105398944Sobrien   See event-signal.c. */
105498944Sobrienstatic void
105598944Sobrienhandle_sigfpe (int sig)
105698944Sobrien{
105798944Sobrien  mark_async_signal_handler_wrapper (sigfpe_token);
105898944Sobrien  signal (sig, handle_sigfpe);
105998944Sobrien}
106098944Sobrien
106198944Sobrien/* Event loop will call this functin to process a SIGFPE. */
106298944Sobrienstatic void
106398944Sobrienasync_float_handler (gdb_client_data arg)
106498944Sobrien{
106598944Sobrien  /* This message is based on ANSI C, section 4.7. Note that integer
106698944Sobrien     divide by zero causes this, so "float" is a misnomer. */
106798944Sobrien  error ("Erroneous arithmetic operation.");
106898944Sobrien}
106998944Sobrien
107098944Sobrien/* Tell the event loop what to do if SIGWINCH is received.
107198944Sobrien   See event-signal.c. */
107298944Sobrien#if defined(SIGWINCH) && defined(SIGWINCH_HANDLER)
107398944Sobrienstatic void
107498944Sobrienhandle_sigwinch (int sig)
107598944Sobrien{
107698944Sobrien  mark_async_signal_handler_wrapper (sigwinch_token);
107798944Sobrien  signal (sig, handle_sigwinch);
107898944Sobrien}
107998944Sobrien#endif
108098944Sobrien
108198944Sobrien
108298944Sobrien/* Called by do_setshow_command.  */
108398944Sobrienvoid
108498944Sobrienset_async_editing_command (char *args, int from_tty, struct cmd_list_element *c)
108598944Sobrien{
108698944Sobrien  change_line_handler ();
108798944Sobrien}
108898944Sobrien
108998944Sobrien/* Called by do_setshow_command.  */
109098944Sobrienvoid
109198944Sobrienset_async_annotation_level (char *args, int from_tty, struct cmd_list_element *c)
109298944Sobrien{
109398944Sobrien  change_annotation_level ();
109498944Sobrien}
109598944Sobrien
109698944Sobrien/* Called by do_setshow_command.  */
109798944Sobrienvoid
109898944Sobrienset_async_prompt (char *args, int from_tty, struct cmd_list_element *c)
109998944Sobrien{
110098944Sobrien  PROMPT (0) = savestring (new_async_prompt, strlen (new_async_prompt));
110198944Sobrien}
110298944Sobrien
110398944Sobrien/* Set things up for readline to be invoked via the alternate
110498944Sobrien   interface, i.e. via a callback function (rl_callback_read_char),
110598944Sobrien   and hook up instream to the event loop. */
110698944Sobrienvoid
1107130803Smarcelgdb_setup_readline (void)
110898944Sobrien{
1109130803Smarcel  /* This function is a noop for the sync case.  The assumption is that
1110130803Smarcel     the sync setup is ALL done in gdb_init, and we would only mess it up
1111130803Smarcel     here.  The sync stuff should really go away over time. */
1112130803Smarcel
111398944Sobrien  if (event_loop_p)
111498944Sobrien    {
1115130803Smarcel      gdb_stdout = stdio_fileopen (stdout);
1116130803Smarcel      gdb_stderr = stdio_fileopen (stderr);
1117130803Smarcel      gdb_stdlog = gdb_stderr;  /* for moment */
1118130803Smarcel      gdb_stdtarg = gdb_stderr; /* for moment */
1119130803Smarcel
112098944Sobrien      /* If the input stream is connected to a terminal, turn on
112198944Sobrien         editing.  */
112298944Sobrien      if (ISATTY (instream))
112398944Sobrien	{
112498944Sobrien	  /* Tell gdb that we will be using the readline library. This
112598944Sobrien	     could be overwritten by a command in .gdbinit like 'set
112698944Sobrien	     editing on' or 'off'. */
112798944Sobrien	  async_command_editing_p = 1;
112898944Sobrien
112998944Sobrien	  /* When a character is detected on instream by select or
113098944Sobrien	     poll, readline will be invoked via this callback
113198944Sobrien	     function. */
113298944Sobrien	  call_readline = rl_callback_read_char_wrapper;
113398944Sobrien	}
113498944Sobrien      else
113598944Sobrien	{
113698944Sobrien	  async_command_editing_p = 0;
113798944Sobrien	  call_readline = gdb_readline2;
113898944Sobrien	}
113998944Sobrien
114098944Sobrien      /* When readline has read an end-of-line character, it passes
114198944Sobrien         the complete line to gdb for processing. command_line_handler
114298944Sobrien         is the function that does this. */
114398944Sobrien      input_handler = command_line_handler;
114498944Sobrien
114598944Sobrien      /* Tell readline to use the same input stream that gdb uses. */
114698944Sobrien      rl_instream = instream;
114798944Sobrien
114898944Sobrien      /* Get a file descriptor for the input stream, so that we can
114998944Sobrien         register it with the event loop. */
115098944Sobrien      input_fd = fileno (instream);
115198944Sobrien
115298944Sobrien      /* Now we need to create the event sources for the input file
115398944Sobrien         descriptor. */
115498944Sobrien      /* At this point in time, this is the only event source that we
115598944Sobrien         register with the even loop. Another source is going to be
115698944Sobrien         the target program (inferior), but that must be registered
115798944Sobrien         only when it actually exists (I.e. after we say 'run' or
115898944Sobrien         after we connect to a remote target. */
115998944Sobrien      add_file_handler (input_fd, stdin_event_handler, 0);
116098944Sobrien    }
116198944Sobrien}
1162130803Smarcel
1163130803Smarcel/* Disable command input through the standard CLI channels.  Used in
1164130803Smarcel   the suspend proc for interpreters that use the standard gdb readline
1165130803Smarcel   interface, like the cli & the mi.  */
1166130803Smarcelvoid
1167130803Smarcelgdb_disable_readline (void)
1168130803Smarcel{
1169130803Smarcel  if (event_loop_p)
1170130803Smarcel    {
1171130803Smarcel      /* FIXME - It is too heavyweight to delete and remake these
1172130803Smarcel         every time you run an interpreter that needs readline.
1173130803Smarcel         It is probably better to have the interpreters cache these,
1174130803Smarcel         which in turn means that this needs to be moved into interpreter
1175130803Smarcel         specific code. */
1176130803Smarcel
1177130803Smarcel#if 0
1178130803Smarcel      ui_file_delete (gdb_stdout);
1179130803Smarcel      ui_file_delete (gdb_stderr);
1180130803Smarcel      gdb_stdlog = NULL;
1181130803Smarcel      gdb_stdtarg = NULL;
1182130803Smarcel#endif
1183130803Smarcel
1184130803Smarcel      rl_callback_handler_remove ();
1185130803Smarcel      delete_file_handler (input_fd);
1186130803Smarcel    }
1187130803Smarcel}
1188