1167465Smp/* callback.c -- functions to use readline as an X `callback' mechanism. */
259243Sobrien
359243Sobrien/* Copyright (C) 1987, 1989, 1992 Free Software Foundation, Inc.
459243Sobrien
559243Sobrien   This file is part of the GNU Readline Library, a library for
659243Sobrien   reading lines of text with interactive input and history editing.
759243Sobrien
859243Sobrien   The GNU Readline Library is free software; you can redistribute it
959243Sobrien   and/or modify it under the terms of the GNU General Public License
1059243Sobrien   as published by the Free Software Foundation; either version 2, or
1159243Sobrien   (at your option) any later version.
1259243Sobrien
1359243Sobrien   The GNU Readline Library is distributed in the hope that it will be
1459243Sobrien   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
1559243Sobrien   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1659243Sobrien   GNU General Public License for more details.
17100616Smp
1859243Sobrien   The GNU General Public License is often shipped with GNU software, and
1959243Sobrien   is generally kept in a file called COPYING or LICENSE.  If you do not
2059243Sobrien   have a copy of the license, write to the Free Software Foundation,
2159243Sobrien   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
2259243Sobrien#define READLINE_LIBRARY
2359243Sobrien
2459243Sobrien#if defined (HAVE_CONFIG_H)
2559243Sobrien#  include <config.h>
2659243Sobrien#endif
2759243Sobrien
2859243Sobrien#include "rlconf.h"
2959243Sobrien
3059243Sobrien#if defined (READLINE_CALLBACKS)
3159243Sobrien
3259243Sobrien#include <sys/types.h>
3359243Sobrien
3459243Sobrien#ifdef HAVE_STDLIB_H
35167465Smp#  include <stdlib.h>
3659243Sobrien#else
3759243Sobrien#  include "ansi_stdlib.h"
3859243Sobrien#endif
3959243Sobrien
4059243Sobrien#include <stdio.h>
41167465Smp
4259243Sobrien/* System-specific feature definitions and include files. */
43167465Smp#include "rldefs.h"
44167465Smp#include "readline.h"
45167465Smp#include "rlprivate.h"
46145479Smp
4759243Sobrien/* **************************************************************** */
4859243Sobrien/*								    */
49167465Smp/*			Callback Readline Functions                 */
50167465Smp/*								    */
5159243Sobrien/* **************************************************************** */
5259243Sobrien
53167465Smp/* Allow using readline in situations where a program may have multiple
5459243Sobrien   things to handle at once, and dispatches them via select().  Call
5559243Sobrien   rl_callback_handler_install() with the prompt and a function to call
5659243Sobrien   whenever a complete line of input is ready.  The user must then
5759243Sobrien   call rl_callback_read_char() every time some input is available, and
58167465Smp   rl_callback_read_char() will call the user's function with the complete
59167465Smp   text read in at each end of line.  The terminal is kept prepped and
6059243Sobrien   signals handled all the time, except during calls to the user's function. */
61167465Smp
62167465Smprl_vcpfunc_t *rl_linefunc;		/* user callback function */
63167465Smpstatic int in_handler;		/* terminal_prepped and signals set? */
64167465Smp
65167465Smp/* Make sure the terminal is set up, initialize readline, and prompt. */
6659243Sobrienstatic void
6759243Sobrien_rl_callback_newline ()
68167465Smp{
69167465Smp  rl_initialize ();
70167465Smp
7159243Sobrien  if (in_handler == 0)
72167465Smp    {
73167465Smp      in_handler = 1;
7459243Sobrien
7559243Sobrien      (*rl_prep_term_function) (_rl_meta_flag);
7659243Sobrien
7759243Sobrien#if defined (HANDLE_SIGNALS)
7859243Sobrien      rl_set_signals ();
7959243Sobrien#endif
80167465Smp    }
8159243Sobrien
8259243Sobrien  readline_internal_setup ();
83167465Smp}
84167465Smp
8559243Sobrien/* Install a readline handler, set up the terminal, and issue the prompt. */
86167465Smpvoid
87167465Smprl_callback_handler_install (prompt, linefunc)
8859243Sobrien     const char *prompt;
8959243Sobrien     rl_vcpfunc_t *linefunc;
9059243Sobrien{
9159243Sobrien  rl_set_prompt (prompt);
9259243Sobrien  rl_linefunc = linefunc;
9359243Sobrien  _rl_callback_newline ();
9459243Sobrien}
9559243Sobrien
9659243Sobrien/* Read one character, and dispatch to the handler if it ends the line. */
9759243Sobrienvoid
9859243Sobrienrl_callback_read_char ()
9959243Sobrien{
10059243Sobrien  char *line;
10159243Sobrien  int eof;
10259243Sobrien
10359243Sobrien  if (rl_linefunc == NULL)
10459243Sobrien    {
10559243Sobrien      fprintf (stderr, "readline: readline_callback_read_char() called with no handler!\r\n");
10659243Sobrien      abort ();
10759243Sobrien    }
10859243Sobrien
10959243Sobrien  eof = readline_internal_char ();
110167465Smp
11159243Sobrien  /* We loop in case some function has pushed input back with rl_execute_next. */
11259243Sobrien  for (;;)
11359243Sobrien    {
11459243Sobrien      if (rl_done)
11559243Sobrien	{
11659243Sobrien	  line = readline_internal_teardown (eof);
11759243Sobrien
11859243Sobrien	  (*rl_deprep_term_function) ();
11959243Sobrien#if defined (HANDLE_SIGNALS)
12059243Sobrien	  rl_clear_signals ();
12159243Sobrien#endif
12259243Sobrien	  in_handler = 0;
12359243Sobrien	  (*rl_linefunc) (line);
12459243Sobrien
12559243Sobrien	  /* If the user did not clear out the line, do it for him. */
12659243Sobrien	  if (rl_line_buffer[0])
12759243Sobrien	    _rl_init_line_state ();
12859243Sobrien
12959243Sobrien	  /* Redisplay the prompt if readline_handler_{install,remove}
13059243Sobrien	     not called. */
13159243Sobrien	  if (in_handler == 0 && rl_linefunc)
132167465Smp	    _rl_callback_newline ();
133167465Smp	}
13459243Sobrien      if (rl_pending_input)
135167465Smp	eof = readline_internal_char ();
136167465Smp      else
13759243Sobrien        break;
13859243Sobrien    }
13959243Sobrien}
14059243Sobrien
14159243Sobrien/* Remove the handler, and make sure the terminal is in its normal state. */
14259243Sobrienvoid
14359243Sobrienrl_callback_handler_remove ()
14459243Sobrien{
14559243Sobrien  rl_linefunc = NULL;
146167465Smp  if (in_handler)
14759243Sobrien    {
14859243Sobrien      in_handler = 0;
14959243Sobrien      (*rl_deprep_term_function) ();
150167465Smp#if defined (HANDLE_SIGNALS)
151167465Smp      rl_clear_signals ();
152167465Smp#endif
153167465Smp    }
154167465Smp}
155167465Smp
156167465Smp#endif
157167465Smp