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

1.1       misho       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: #include <stdio.h>
                     32: #include <sys/types.h>
                     33: 
                     34: #ifdef HAVE_STDLIB_H
                     35: #  include <stdlib.h>
                     36: #else 
                     37: extern void exit();
                     38: #endif
                     39: 
                     40: #if defined (READLINE_LIBRARY)
                     41: #  include "posixstat.h"
                     42: #  include "readline.h"
                     43: #  include "history.h"
                     44: #else
                     45: #  include <sys/stat.h>
                     46: #  include <readline/readline.h>
                     47: #  include <readline/history.h>
                     48: #endif
                     49: 
                     50: extern int optind;
                     51: extern char *optarg;
                     52: 
                     53: #if !defined (strchr) && !defined (__STDC__)
                     54: extern char *strrchr();
                     55: #endif
                     56: 
                     57: static char *progname;
                     58: static char *deftext;
                     59: 
                     60: static int
                     61: set_deftext ()
                     62: {
                     63:   if (deftext)
                     64:     {
                     65:       rl_insert_text (deftext);
                     66:       deftext = (char *)NULL;
                     67:       rl_startup_hook = (rl_hook_func_t *)NULL;
                     68:     }
                     69:   return 0;
                     70: }
                     71: 
                     72: static void
                     73: usage()
                     74: {
                     75:   fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
                     76:                progname, progname);
                     77: }
                     78: 
                     79: int
                     80: main (argc, argv)
                     81:      int argc;
                     82:      char **argv;
                     83: {
                     84:   char *temp, *prompt;
                     85:   struct stat sb;
                     86:   int opt, fd, nch;
                     87:   FILE *ifp;
                     88: 
                     89:   progname = strrchr(argv[0], '/');
                     90:   if (progname == 0)
                     91:     progname = argv[0];
                     92:   else
                     93:     progname++;
                     94: 
                     95:   /* defaults */
                     96:   prompt = "readline$ ";
                     97:   fd = nch = 0;
                     98:   deftext = (char *)0;
                     99: 
                    100:   while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
                    101:     {
                    102:       switch (opt)
                    103:        {
                    104:        case 'p':
                    105:          prompt = optarg;
                    106:          break;
                    107:        case 'u':
                    108:          fd = atoi(optarg);
                    109:          if (fd < 0)
                    110:            {
                    111:              fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
                    112:              exit (2);
                    113:            }
                    114:          break;
                    115:        case 'd':
                    116:          deftext = optarg;
                    117:          break;
                    118:        case 'n':
                    119:          nch = atoi(optarg);
                    120:          if (nch < 0)
                    121:            {
                    122:              fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
                    123:              exit (2);
                    124:            }
                    125:          break;
                    126:        default:
                    127:          usage ();
                    128:          exit (2);
                    129:        }
                    130:     }
                    131: 
                    132:   if (fd != 0)
                    133:     {
                    134:       if (fstat (fd, &sb) < 0)
                    135:        {
                    136:          fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
                    137:          exit (1);
                    138:        }
                    139:       ifp = fdopen (fd, "r");
                    140:       rl_instream = ifp;
                    141:     }
                    142: 
                    143:   if (deftext && *deftext)
                    144:     rl_startup_hook = set_deftext;
                    145: 
                    146:   if (nch > 0)
                    147:     rl_num_chars_to_read = nch;
                    148: 
                    149:   temp = readline (prompt);
                    150: 
                    151:   /* Test for EOF. */
                    152:   if (temp == 0)
                    153:     exit (1);
                    154: 
                    155:   printf ("%s\n", temp);
                    156:   exit (0);
                    157: }

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