commit 298ecf630e4ad585975102e47c11a8b8a1faba51
Author: Hiroo HAYASHI <24754036+hirooih@users.noreply.github.com>
Date:   Sun Jun 16 11:27:04 2024 +0900

    examples fixes
    
    Signed-off-by: Hiroo HAYASHI <24754036+hirooih@users.noreply.github.com>

diff --git a/.gitignore b/.gitignore
index c6bc41e..8e95983 100644
--- a/.gitignore
+++ b/.gitignore
@@ -15,15 +15,22 @@ doc/Makefile
 examples/Makefile
 shlib/Makefile
 
+examples/excallback
 examples/fileman
 examples/hist_erasedups
 examples/hist_purgecmd
 examples/histexamp
+examples/manexamp
 examples/rl
 examples/rl-callbacktest
+examples/rl-callbacktest2
+examples/rl-callbacktest3
+examples/rl-timeout
 examples/rlbasic
 examples/rlcat
 examples/rlevent
+examples/rlkeymaps
+examples/rlptytest
 examples/rltest
 examples/rlversion
 
@@ -33,6 +40,7 @@ libreadline.so.*
 *.dylib
 
 readline.pc
+history.pc
 
 stamp-h
 
diff --git a/config.h.in b/config.h.in
index dd0a956..1040ffa 100644
--- a/config.h.in
+++ b/config.h.in
@@ -215,6 +215,9 @@
 /* Define if you have the <ncurses/termcap.h> header file.  */
 #undef HAVE_NCURSES_TERMCAP_H
 
+/* Define if you have the <pty.h> header file.  */
+#undef HAVE_PTY_H
+
 /* Define if you have the <pwd.h> header file.  */
 #undef HAVE_PWD_H
 
diff --git a/configure.ac b/configure.ac
index 4604750..f536f6b 100644
--- a/configure.ac
+++ b/configure.ac
@@ -160,7 +160,8 @@ AC_FUNC_STRCOLL
 
 AC_CHECK_HEADERS(fcntl.h unistd.h stdlib.h varargs.h stdarg.h stdbool.h \
 		string.h strings.h \
-		limits.h locale.h pwd.h memory.h termcap.h termios.h termio.h)
+		limits.h locale.h pwd.h memory.h termcap.h termios.h termio.h \
+		pty.h)
 AC_CHECK_HEADERS(sys/ioctl.h sys/pte.h sys/stream.h sys/select.h \
 		sys/time.h sys/file.h)
 
diff --git a/doc/rltech.texi b/doc/rltech.texi
index 497cd4d..bb34e0c 100644
--- a/doc/rltech.texi
+++ b/doc/rltech.texi
@@ -1580,6 +1580,7 @@ It understands the EOF character or "exit" to exit the program.
 
 @example
 /* Standard include files. stdio.h is required. */
+#include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
diff --git a/examples/Makefile.in b/examples/Makefile.in
index 20a9896..34c9b80 100644
--- a/examples/Makefile.in
+++ b/examples/Makefile.in
@@ -75,21 +75,21 @@ SOURCES = excallback.c fileman.c histexamp.c manexamp.c rl-fgets.c rl.c \
 		hist_erasedups.c hist_purgecmd.c \
 		rlkeymaps.c rl-timeout.c
 
-EXECUTABLES = fileman$(EXEEXT) rltest$(EXEEXT) rl$(EXEEXT) rlcat$(EXEEXT) \
+EXECUTABLES =  excallback$(EXEEXT) fileman$(EXEEXT) rltest$(EXEEXT) rl$(EXEEXT) rlcat$(EXEEXT) \
 		rlevent$(EXEEXT) rlversion$(EXEEXT) histexamp$(EXEEXT) \
-		rl-callbacktest$(EXEEXT) rlbasic$(EXEEXT) \
+		rl-callbacktest$(EXEEXT) rl-callbacktest2$(EXEEXT) rl-callbacktest3$(EXEEXT) rlbasic$(EXEEXT) \
 		hist_erasedups$(EXEEXT) hist_purgecmd$(EXEEXT) \
 		rlkeymaps$(EXEEXT) rl-timeout$(EXEEXT)
 
-OBJECTS = fileman.o rltest.o rl.o rlevent.o rlcat.o rlversion.o histexamp.o \
-	  rl-callbacktest.o rlbasic.o hist_erasedups.o hist_purgecmd.o \
+OBJECTS = excallback.o fileman.o rltest.o rl.o rlevent.o rlcat.o rlversion.o histexamp.o \
+	  rl-callbacktest.o rl-callbacktest2.o rl-callbacktest3.o rlbasic.o hist_erasedups.o hist_purgecmd.o \
 	  rlkeymaps.o rl-timeout.o
 
 OTHEREXE = rlptytest$(EXEEXT)
 OTHEROBJ = rlptytest.o
 
 all: $(EXECUTABLES)
-everything: all
+everything: all $(OTHEREXE)
 
 asan:
 	${MAKE} ${MFLAGS} ASAN_CFLAGS='${ASAN_XCFLAGS}' ASAN_LDFLAGS='${ASAN_XLDFLAGS}' all
@@ -154,6 +154,9 @@ rl-timeout$(EXEEXT): rl-timeout.o $(READLINE_LIB)
 rlversion$(EXEEXT): rlversion.o $(READLINE_LIB)
 	$(CC) $(LDFLAGS) -o $@ rlversion.o $(READLINE_LIB) $(TERMCAP_LIB)
 
+excallback$(EXEEXT): excallback.o $(READLINE_LIB)
+	$(CC) $(LDFLAGS) -o $@ excallback.o $(READLINE_LIB) $(TERMCAP_LIB)
+
 histexamp$(EXEEXT): histexamp.o $(HISTORY_LIB)
 	$(CC) $(LDFLAGS) -o $@ histexamp.o -lhistory $(TERMCAP_LIB)
 
diff --git a/examples/excallback.c b/examples/excallback.c
index 04ecb14..dfa4057 100644
--- a/examples/excallback.c
+++ b/examples/excallback.c
@@ -19,7 +19,7 @@ My example shows how, using the alternate interface, you can
 interactively change the prompt (which is very nice imo). Also, I
 point out that you must roll your own terminal setting when using the
 alternate interface because readline depreps (using your parlance) the
-terminal while in the user callback. I try to demostrate what I mean
+terminal while in the user callback. I try to demonstrate what I mean
 with an example. I've included the program below.
 
 To compile, I just put the program in the examples directory and made
@@ -72,7 +72,7 @@ Copyright (C) 1999 Jeff Solomon
  * alternate interface. The first is the ability to interactively change the
  * prompt, which can't be done using the regular interface since rl_prompt is
  * read-only.
- * 
+ *
  * The second feature really highlights a subtle point when using the alternate
  * interface. That is, readline will not alter the terminal when inside your
  * callback handler. So let's so, your callback executes a user command that
@@ -92,22 +92,27 @@ Copyright (C) 1999 Jeff Solomon
  */
 
 void process_line(char *line);
-int  change_prompt(void);
+int  change_prompt(int, int);
 char *get_prompt(void);
 
 int prompt = 1;
 char prompt_buf[40], line_buf[256];
+
+#define USE_RL_CALLBACK_HANDLER_REMOVE 1
+#if ! USE_RL_CALLBACK_HANDLER_REMOVE
 tcflag_t old_lflag;
 cc_t     old_vtime;
 struct termios term;
+#endif
 
-int 
+int
 main(int c, char **v)
 {
     fd_set fds;
 
     setlocale (LC_ALL, "");
 
+#if ! USE_RL_CALLBACK_HANDLER_REMOVE
     /* Adjust the terminal slightly before the handler is installed. Disable
      * canonical mode processing and set the input character time flag to be
      * non-blocking.
@@ -125,6 +130,7 @@ main(int c, char **v)
         perror("tcsetattr");
         exit(1);
     }
+#endif
 
     rl_add_defun("change-prompt", change_prompt, CTRL('t'));
     rl_callback_handler_install(get_prompt(), process_line);
@@ -148,15 +154,19 @@ void
 process_line(char *line)
 {
   if( line == NULL ) {
-    fprintf(stderr, "\n", line);
+    fprintf(stderr, "\n");
 
     /* reset the old terminal setting before exiting */
+#if USE_RL_CALLBACK_HANDLER_REMOVE
+    rl_callback_handler_remove();
+#else
     term.c_lflag     = old_lflag;
     term.c_cc[VTIME] = old_vtime;
     if( tcsetattr(STDIN_FILENO, TCSANOW, &term) < 0 ) {
         perror("tcsetattr");
         exit(1);
     }
+#endif
     exit(0);
   }
 
@@ -170,11 +180,14 @@ process_line(char *line)
 }
 
 int
-change_prompt(void)
+change_prompt(int count, int key)
 {
   /* toggle the prompt variable */
   prompt = !prompt;
 
+#if RL_READLINE_VERSION >= 0x0402
+  rl_set_prompt(get_prompt());
+#else
   /* save away the current contents of the line */
   strcpy(line_buf, rl_line_buffer);
 
@@ -188,13 +201,16 @@ change_prompt(void)
    * redraw-current-line command.
    */
   rl_refresh_line(0, 0);
+#endif
+
+  return 0;
 }
 
 char *
 get_prompt(void)
 {
   /* The prompts can even be different lengths! */
-  sprintf(prompt_buf, "%s", 
+  sprintf(prompt_buf, "%s",
     prompt ? "Hit ctrl-t to toggle prompt> " : "Pretty cool huh?> ");
   return prompt_buf;
 }
diff --git a/examples/fileman.c b/examples/fileman.c
index c26bccf..ccba324 100644
--- a/examples/fileman.c
+++ b/examples/fileman.c
@@ -333,9 +333,9 @@ com_stat (char *arg)
 
   printf ("Statistics for `%s':\n", arg);
 
-  printf ("%s has %d link%s, and is %lu byte%s in length.\n",
+  printf ("%s has %lu link%s, and is %lu byte%s in length.\n",
 	  arg,
-          finfo.st_nlink,
+          (unsigned long)finfo.st_nlink,
           (finfo.st_nlink == 1) ? "" : "s",
           (unsigned long)finfo.st_size,
           (finfo.st_size == 1) ? "" : "s");
diff --git a/examples/histexamp.c b/examples/histexamp.c
index 2e946ac..d592b7e 100644
--- a/examples/histexamp.c
+++ b/examples/histexamp.c
@@ -110,7 +110,7 @@ main (int argc, char **argv)
 	  int which;
 	  if ((sscanf (line + 6, "%d", &which)) == 1)
 	    {
-	      HIST_ENTRY *entry = remove_history (which);
+	      HIST_ENTRY *entry = remove_history (which - history_base);
 	      if (!entry)
 		fprintf (stderr, "No such entry %d\n", which);
 	      else
diff --git a/examples/rl-callbacktest.c b/examples/rl-callbacktest.c
index 7febacd..34cac40 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,11 +11,8 @@
 
 #include <signal.h>
 
-#include <errno.h>
 #include <stdio.h>
 
-#include <locale.h>
-
 /* Standard readline include files. */
 #if defined (READLINE_LIBRARY)
 #  include "readline.h"
@@ -23,12 +22,11 @@
 #  include <readline/history.h>
 #endif
 
-extern int errno;
-
 static void cb_linehandler (char *);
 static void signandler (int);
 
-int running, sigwinch_received;
+int running;
+int sigwinch_received;
 const char *prompt = "rltest$ ";
 
 /* Handle SIGWINCH and window size changes when readline is not active and
@@ -73,11 +71,13 @@ main (int c, char **v)
   fd_set fds;
   int r;
 
+  /* Set the default locale values according to environment variables. */
   setlocale (LC_ALL, "");
 
-  /* Handle SIGWINCH */
+  /* Handle window size changes when readline is not active and reading
+     characters. */
   signal (SIGWINCH, sighandler);
-  
+
   /* Install the line handler. */
   rl_callback_handler_install (prompt, cb_linehandler);
 
@@ -89,7 +89,7 @@ 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)
diff --git a/examples/rl-callbacktest2.c b/examples/rl-callbacktest2.c
index 0bf0641..7bb76ca 100644
--- a/examples/rl-callbacktest2.c
+++ b/examples/rl-callbacktest2.c
@@ -51,18 +51,18 @@ cb_readline (void)
   fd_set fds;
   int r, err;
   char *not_done = "";
-  
+
   /* Install the line handler. */
   rl_callback_handler_install (prompt, cb_linehandler);
 
-if (RL_ISSTATE (RL_STATE_ISEARCH))
-  fprintf(stderr, "cb_readline: after handler install, state (ISEARCH) = %d", rl_readline_state);
-else if (RL_ISSTATE (RL_STATE_NSEARCH))
-  fprintf(stderr, "cb_readline: after handler install, state (NSEARCH) = %d", rl_readline_state);
-/* MULTIKEY VIMOTION NUMERICARG _rl_callback_func */
+  if (RL_ISSTATE (RL_STATE_ISEARCH))
+    fprintf(stderr, "cb_readline: after handler install, state (ISEARCH) = %lu", rl_readline_state);
+  else if (RL_ISSTATE (RL_STATE_NSEARCH))
+    fprintf(stderr, "cb_readline: after handler install, state (NSEARCH) = %lu", rl_readline_state);
+  /* MULTIKEY VIMOTION NUMERICARG _rl_callback_func */
 
   FD_ZERO (&fds);
-  FD_SET (fileno (rl_instream), &fds);    
+  FD_SET (fileno (rl_instream), &fds);
 
   input_string = not_done;
 
@@ -76,10 +76,10 @@ else if (RL_ISSTATE (RL_STATE_NSEARCH))
       while (r == 0)
 	{
 	  struct timeval timeout = {0, 100000};
-	  struct timeval *timeoutp = NULL;      
+	  struct timeval *timeoutp = NULL;
 
 	  timeoutp = &timeout;
-	  FD_SET (fileno (rl_instream), &fds);    
+	  FD_SET (fileno (rl_instream), &fds);
 	  r = select (FD_SETSIZE, &fds, NULL, NULL, timeoutp);
 	  err = errno;
 	}
@@ -115,8 +115,8 @@ sigint_handler (int s)
   rl_cleanup_after_signal ();
   rl_callback_handler_remove ();
   saw_signal = 0;
-fprintf(stderr, "sigint_handler: readline state = %d\r\n", rl_readline_state);
-  return s;  
+  fprintf(stderr, "sigint_handler: readline state = %lu\r\n", rl_readline_state);
+  return s;
 }
 
 int
diff --git a/examples/rlptytest.c b/examples/rlptytest.c
index 021a868..dda0133 100644
--- a/examples/rlptytest.c
+++ b/examples/rlptytest.c
@@ -19,10 +19,11 @@
 
 #include <signal.h>
 
-#if 1	/* LINUX */
+#ifdef HAVE_PTY_H
 #include <pty.h>
 #else
 #include <util.h>
+#include <sys/ioctl.h>
 #endif
 
 #ifdef HAVE_LOCALE_H
@@ -31,10 +32,14 @@
 
 #ifdef READLINE_LIBRARY
 #  include "readline.h"
+#  include "history.h"
 #else
 #  include <readline/readline.h>
+#  include "history.h"
 #endif
 
+int tty_reset(int);
+
 /**
  * Master/Slave PTY used to keep readline off of stdin/stdout.
  */
