File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / readline / examples / rl-callbacktest.c
Revision 1.1.1.2 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Wed Mar 17 01:01:01 2021 UTC (3 years, 3 months ago) by misho
Branches: readline, MAIN
CVS tags: v8_2p0, v8_1p0, HEAD
readline 8.1

    1: /* Standard include files. stdio.h is required. */
    2: #include <stdlib.h>
    3: #include <unistd.h>
    4: #include <string.h>
    5: #include <locale.h>
    6: 
    7: /* Used for select(2) */
    8: #include <sys/types.h>
    9: #include <sys/select.h>
   10: 
   11: #include <signal.h>
   12: 
   13: #include <errno.h>
   14: #include <stdio.h>
   15: 
   16: /* Standard readline include files. */
   17: #if defined (READLINE_LIBRARY)
   18: #  include "readline.h"
   19: #  include "history.h"
   20: #else
   21: #  include <readline/readline.h>
   22: #  include <readline/history.h>
   23: #endif
   24: 
   25: extern int errno;
   26: 
   27: static void cb_linehandler (char *);
   28: static void signandler (int);
   29: 
   30: int running, sigwinch_received;
   31: const char *prompt = "rltest$ ";
   32: 
   33: /* Handle SIGWINCH and window size changes when readline is not active and
   34:    reading a character. */
   35: static void
   36: sighandler (int sig)
   37: {
   38:   sigwinch_received = 1;
   39: }
   40: 
   41: /* Callback function called for each line when accept-line executed, EOF
   42:    seen, or EOF character read.  This sets a flag and returns; it could
   43:    also call exit(3). */
   44: static void
   45: cb_linehandler (char *line)
   46: {
   47:   /* Can use ^D (stty eof) or `exit' to exit. */
   48:   if (line == NULL || strcmp (line, "exit") == 0)
   49:     {
   50:       if (line == 0)
   51:         printf ("\n");
   52:       printf ("exit\n");
   53:       /* This function needs to be called to reset the terminal settings,
   54: 	 and calling it from the line handler keeps one extra prompt from
   55: 	 being displayed. */
   56:       rl_callback_handler_remove ();
   57: 
   58:       running = 0;
   59:     }
   60:   else
   61:     {
   62:       if (*line)
   63: 	add_history (line);
   64:       printf ("input line: %s\n", line);
   65:       free (line);
   66:     }
   67: }
   68: 
   69: int
   70: main (int c, char **v)
   71: {
   72:   fd_set fds;
   73:   int r;
   74: 
   75: 
   76:   setlocale (LC_ALL, "");
   77: 
   78:   /* Handle SIGWINCH */
   79:   signal (SIGWINCH, sighandler);
   80:   
   81:   /* Install the line handler. */
   82:   rl_callback_handler_install (prompt, cb_linehandler);
   83: 
   84:   /* Enter a simple event loop.  This waits until something is available
   85:      to read on readline's input stream (defaults to standard input) and
   86:      calls the builtin character read callback to read it.  It does not
   87:      have to modify the user's terminal settings. */
   88:   running = 1;
   89:   while (running)
   90:     {
   91:       FD_ZERO (&fds);
   92:       FD_SET (fileno (rl_instream), &fds);    
   93: 
   94:       r = select (FD_SETSIZE, &fds, NULL, NULL, NULL);
   95:       if (r < 0 && errno != EINTR)
   96: 	{
   97: 	  perror ("rltest: select");
   98: 	  rl_callback_handler_remove ();
   99: 	  break;
  100: 	}
  101:       if (sigwinch_received)
  102: 	{
  103: 	  rl_resize_terminal ();
  104: 	  sigwinch_received = 0;
  105: 	}
  106:       if (r < 0)
  107: 	continue;
  108: 
  109:       if (FD_ISSET (fileno (rl_instream), &fds))
  110: 	rl_callback_read_char ();
  111:     }
  112: 
  113:   printf ("rltest: Event loop has exited\n");
  114:   return 0;
  115: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>