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

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