summaryrefslogtreecommitdiffstats
path: root/examples/rl-timeout.c
blob: b8a24bafb94fcd8670a73ba977f8ca6eaf6e0907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* rl-timeout: test various readline builtin timeouts. */

/* Copyright (C) 2021 Free Software Foundation, Inc.

   This file is part of the GNU Readline Library (Readline), a library for
   reading lines of text with interactive input and history editing.

   Readline is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Readline is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with readline.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Standard include files. stdio.h is required. */
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>

/* Used for select(2) */
#include <sys/types.h>
#include <sys/select.h>

#include <errno.h>
#include <stdio.h>

/* Standard readline include files. */
#if defined (READLINE_LIBRARY)
#  include "readline.h"
#  include "history.h"
#else
#  include <readline/readline.h>
#  include <readline/history.h>
#endif

extern int errno;

static void cb_linehandler (char *);

int timeout_secs = 1, timeout_usecs = 0;
int running;
const char *prompt = "rl-timeout$ ";

/* **************************************************************** */
/*								    */
/* Example 1: readline () with rl_readline_state		    */
/*								    */
/* **************************************************************** */

void
rltest_timeout_readline1 ()
{
  const char *temp;

  rl_set_timeout (timeout_secs, timeout_usecs);
  temp = readline (prompt);
  if (RL_ISSTATE (RL_STATE_TIMEOUT))
    printf ("timeout\n");
  else if (temp == NULL)
    printf ("no input line\n");
  else
    printf ("input line: %s\n", temp);
  free ((void *) temp);
}

/* **************************************************************** */
/*								    */
/* Example 2: readline () with rl_timeout_event_hook		    */
/*								    */
/* **************************************************************** */

static int
timeout_handler ()
{
  printf ("timeout\n");
  return READERR;
}

void
rltest_timeout_readline2 ()
{
  const char *temp;

  rl_set_timeout (timeout_secs, timeout_usecs);
  rl_timeout_event_hook = timeout_handler;
  temp = readline (prompt);
  if (temp == NULL)
    printf ("no input line\n");
  else
    printf ("input line: %s\n", temp);
  free ((void *)temp);
}

/* **************************************************************** */
/*								    */
/* Example 3: rl_callback_* () with rl_timeout_remaining	    */
/*								    */
/* **************************************************************** */

/* Callback function called for each line when accept-line executed, EOF
   seen, or EOF character read.  This sets a flag and returns; it could
   also call exit(3). */
static void
cb_linehandler (char *line)
{
  /* Can use ^D (stty eof) or `exit' to exit. */
  if (line == NULL || strcmp (line, "exit") == 0)
    {
      if (line == 0)
	printf ("\n");
      printf ("exit\n");
      /* This function needs to be called to reset the terminal settings,
	 and calling it from the line handler keeps one extra prompt from
	 being displayed. */
      rl_callback_handler_remove ();

      running = 0;
    }
  else
    {
      if (*line)
	add_history (line);
      printf ("input line: %s\n", line);
      free (line);
    }
}

void
rltest_timeout_callback1 ()
{
  fd_set fds;
  int r;
  unsigned sec, usec;

  rl_set_timeout (timeout_secs, timeout_usecs);
  rl_callback_handler_install (prompt, cb_linehandler);
  running = 1;
  while (running)
    {
      FD_ZERO (&fds);
      FD_SET (fileno (rl_instream), &fds);
      r = rl_timeout_remaining (&sec, &usec);
      if (r == 1)
	{
	  struct timeval timeout = {sec, usec};
	  r = select (FD_SETSIZE, &fds, NULL, NULL, &timeout);
	}
      if (r < 0 && errno != EINTR)
	{
	  perror ("rl-timeout: select");
	  rl_callback_handler_remove ();
	  break;
	}
      else if (r == 0)
	{
	  printf ("rl-timeout: timeout\n");
	  rl_callback_handler_remove ();
	  break;
	}

      if (FD_ISSET (fileno (rl_instream), &fds))
	rl_callback_read_char ();
    }

  printf ("rl-timeout: Event loop has exited\n");
}

/* **************************************************************** */
/*								    */
/* Example 4: rl_callback_* () with rl_timeout_event_hook	    */
/*								    */
/* **************************************************************** */

static int
cb_timeouthandler ()
{
  printf ("timeout\n");
  rl_callback_handler_remove ();
  running = 0;
  return READERR;
}

void
rltest_timeout_callback2 ()
{
  int r;

  rl_set_timeout (timeout_secs, timeout_usecs);
  rl_timeout_event_hook = cb_timeouthandler;
  rl_callback_handler_install (prompt, cb_linehandler);
  running = 1;
  while (running)
    rl_callback_read_char ();

  printf ("rl-timeout: Event loop has exited\n");
}

int
main (int argc, char **argv)
{
  if (argc >= 2)
    {
      if (argc >= 3)
	{
	  double timeout = atof (argv[2]);
	  if (timeout <= 0.0)
	    {
	      fprintf (stderr, "rl-timeout: specify a positive number for timeout.\n");
	      return 2;
	    }
	  else if (timeout > UINT_MAX)
	    {
	      fprintf (stderr, "rl-timeout: timeout too large.\n");
	      return 2;
	    }
	  timeout_secs = (unsigned) timeout;
	  timeout_usecs = (unsigned) ((timeout - timeout_secs) * 1000000 + 0.5);
	}

      if (strcmp (argv[1], "readline1") == 0)
	rltest_timeout_readline1 ();
      else if (strcmp (argv[1], "readline2") == 0)
	rltest_timeout_readline2 ();
      else if (strcmp (argv[1], "callback1") == 0)
	rltest_timeout_callback1 ();
      else if (strcmp (argv[1], "callback2") == 0)
	rltest_timeout_callback2 ();
      else
	return 2;
    }
  else
    {
      fprintf (stderr, "usage: rl-timeout [readline1 | readline2 | callback1 | callback2] [timeout]\n");
      return 2;
    }
  return 0;
}