"Fossies" - the Fresh Open Source Software Archive

Member "readline-8.0/examples/rlevent.c" (21 May 2018, 3393 Bytes) of package /linux/misc/readline-8.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "rlevent.c": 7.0_vs_8.0.

    1 /*
    2  * rl - command-line interface to read a line from the standard input
    3  *      (or another fd) using readline.
    4  *
    5  * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
    6  */
    7 
    8 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
    9 
   10    This file is part of the GNU Readline Library (Readline), a library for
   11    reading lines of text with interactive input and history editing.
   12 
   13    Readline is free software: you can redistribute it and/or modify
   14    it under the terms of the GNU General Public License as published by
   15    the Free Software Foundation, either version 3 of the License, or
   16    (at your option) any later version.
   17 
   18    Readline is distributed in the hope that it will be useful,
   19    but WITHOUT ANY WARRANTY; without even the implied warranty of
   20    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   21    GNU General Public License for more details.
   22 
   23    You should have received a copy of the GNU General Public License
   24    along with Readline.  If not, see <http://www.gnu.org/licenses/>.
   25 */
   26 
   27 #if defined (HAVE_CONFIG_H)
   28 #  include <config.h>
   29 #endif
   30 
   31 #ifdef HAVE_UNISTD_H
   32 #  include <unistd.h>
   33 #else
   34 extern int getopt();
   35 extern int sleep();
   36 #endif
   37 
   38 #include <stdio.h>
   39 #include <sys/types.h>
   40 
   41 #ifdef HAVE_STDLIB_H
   42 #  include <stdlib.h>
   43 #else 
   44 extern void exit();
   45 #endif
   46 
   47 #if defined (READLINE_LIBRARY)
   48 #  include "posixstat.h"
   49 #  include "readline.h"
   50 #  include "history.h"
   51 #else
   52 #  include <sys/stat.h>
   53 #  include <readline/readline.h>
   54 #  include <readline/history.h>
   55 #endif
   56 
   57 extern int optind;
   58 extern char *optarg;
   59 
   60 #if !defined (strchr) && !defined (__STDC__)
   61 extern char *strrchr();
   62 #endif
   63 
   64 static char *progname;
   65 static char *deftext;
   66 
   67 static int
   68 event_hook ()
   69 {
   70   fprintf (stderr, "ding!\n");
   71   sleep (1);
   72   return 0;
   73 }
   74 
   75 static int
   76 set_deftext ()
   77 {
   78   if (deftext)
   79     {
   80       rl_insert_text (deftext);
   81       deftext = (char *)NULL;
   82       rl_startup_hook = (rl_hook_func_t *)NULL;
   83     }
   84   return 0;
   85 }
   86 
   87 static void
   88 usage()
   89 {
   90   fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
   91         progname, progname);
   92 }
   93 
   94 int
   95 main (argc, argv)
   96      int argc;
   97      char **argv;
   98 {
   99   char *temp, *prompt;
  100   struct stat sb;
  101   int opt, fd, nch;
  102   FILE *ifp;
  103 
  104   progname = strrchr(argv[0], '/');
  105   if (progname == 0)
  106     progname = argv[0];
  107   else
  108     progname++;
  109 
  110   /* defaults */
  111   prompt = "readline$ ";
  112   fd = nch = 0;
  113   deftext = (char *)0;
  114 
  115   while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
  116     {
  117       switch (opt)
  118     {
  119     case 'p':
  120       prompt = optarg;
  121       break;
  122     case 'u':
  123       fd = atoi(optarg);
  124       if (fd < 0)
  125         {
  126           fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
  127           exit (2);
  128         }
  129       break;
  130     case 'd':
  131       deftext = optarg;
  132       break;
  133     case 'n':
  134       nch = atoi(optarg);
  135       if (nch < 0)
  136         {
  137           fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
  138           exit (2);
  139         }
  140       break;
  141     default:
  142       usage ();
  143       exit (2);
  144     }
  145     }
  146 
  147   if (fd != 0)
  148     {
  149       if (fstat (fd, &sb) < 0)
  150     {
  151       fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
  152       exit (1);
  153     }
  154       ifp = fdopen (fd, "r");
  155       rl_instream = ifp;
  156     }
  157 
  158   if (deftext && *deftext)
  159     rl_startup_hook = set_deftext;
  160 
  161   if (nch > 0)
  162     rl_num_chars_to_read = nch;
  163 
  164   rl_event_hook = event_hook;
  165   temp = readline (prompt);
  166 
  167   /* Test for EOF. */
  168   if (temp == 0)
  169     exit (1);
  170 
  171   printf ("%s\n", temp);
  172   exit (0);
  173 }