Annotation of embedaddon/readline/examples/rl-callbacktest.c, revision 1.1.1.1

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

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