summaryrefslogtreecommitdiff
path: root/examples/rl-callbacktest.c
diff options
context:
space:
mode:
authorChet Ramey <chet.ramey@case.edu>2023-04-27 15:33:37 -0400
committerChet Ramey <chet.ramey@case.edu>2023-04-27 15:33:37 -0400
commitf156385efb545a5ab96a2461555955d7f280aac4 (patch)
tree4c4ede0b0755f46629e1076b884ccf03654f5d47 /examples/rl-callbacktest.c
parent14144013617be12f94b99b05bf0928a8d1eba743 (diff)
downloadreadline-devel.tar.gz
asan updates to non-incremental search, redisplay; rework non-incremental search to avoid pointer aliasing issuesdevel
Diffstat (limited to 'examples/rl-callbacktest.c')
-rw-r--r--examples/rl-callbacktest.c103
1 files changed, 69 insertions, 34 deletions
diff --git a/examples/rl-callbacktest.c b/examples/rl-callbacktest.c
index 7febacd..269ed2e 100644
--- a/examples/rl-callbacktest.c
+++ b/examples/rl-callbacktest.c
@@ -1,7 +1,9 @@
/* Standard include files. stdio.h is required. */
+#include <errno.h>
#include <stdlib.h>
-#include <unistd.h>
#include <string.h>
+#include <unistd.h>
+#include <locale.h>
/* Used for select(2) */
#include <sys/types.h>
@@ -9,36 +11,37 @@
#include <signal.h>
-#include <errno.h>
#include <stdio.h>
-#include <locale.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;
+#include "readline.h"
+#include "history.h"
static void cb_linehandler (char *);
-static void signandler (int);
+static void sigwinch_handler (int);
+static void sigint_handler (int);
-int running, sigwinch_received;
+int running;
+int sigwinch_received;
+int sigint_received;
const char *prompt = "rltest$ ";
/* Handle SIGWINCH and window size changes when readline is not active and
reading a character. */
static void
-sighandler (int sig)
+sigwinch_handler (int sig)
{
sigwinch_received = 1;
}
+/* Handle SIGWINCH and window size changes when readline is not active and
+ reading a character. */
+static void
+sigint_handler (int sig)
+{
+ sigint_received = 1;
+}
+
/* 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). */
@@ -52,8 +55,8 @@ cb_linehandler (char *line)
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. */
+ and calling it from the line handler keeps one extra prompt from
+ being displayed. */
rl_callback_handler_remove ();
running = 0;
@@ -61,23 +64,45 @@ cb_linehandler (char *line)
else
{
if (*line)
- add_history (line);
+ add_history (line);
printf ("input line: %s\n", line);
free (line);
}
}
+int count = 2;
+
+static int
+my_getc (FILE *stream)
+{
+ if (--count == 0)
+ {
+ kill (getpid (), SIGINT);
+ }
+
+ int ch = rl_getc (stream);
+
+ return ch;
+}
+
+
int
main (int c, char **v)
{
fd_set fds;
int r;
+ /* Set the default locale values according to environment variables. */
setlocale (LC_ALL, "");
- /* Handle SIGWINCH */
- signal (SIGWINCH, sighandler);
-
+ /* Handle window size changes when readline is not active and reading
+ characters. */
+ signal (SIGWINCH, sigwinch_handler);
+
+ signal (SIGINT, sigint_handler);
+
+ rl_getc_function = my_getc;
+
/* Install the line handler. */
rl_callback_handler_install (prompt, cb_linehandler);
@@ -89,25 +114,35 @@ main (int c, char **v)
while (running)
{
FD_ZERO (&fds);
- FD_SET (fileno (rl_instream), &fds);
+ FD_SET (fileno (rl_instream), &fds);
r = select (FD_SETSIZE, &fds, NULL, NULL, NULL);
if (r < 0 && errno != EINTR)
- {
- perror ("rltest: select");
- rl_callback_handler_remove ();
- break;
- }
+ {
+ perror ("rltest: select");
+ rl_callback_handler_remove ();
+ break;
+ }
if (sigwinch_received)
- {
- rl_resize_terminal ();
- sigwinch_received = 0;
- }
+ {
+ rl_resize_terminal ();
+ sigwinch_received = 0;
+ }
+ if (sigint_received)
+ {
+ printf ("Quit\n");
+
+ rl_callback_handler_remove ();
+ rl_callback_handler_install (prompt, cb_linehandler);
+
+ sigint_received = 0;
+ continue;
+ }
if (r < 0)
- continue;
+ continue;
if (FD_ISSET (fileno (rl_instream), &fds))
- rl_callback_read_char ();
+ rl_callback_read_char ();
}
printf ("rltest: Event loop has exited\n");