xref: /openbsd-src/gnu/lib/libreadline/examples/rl.c (revision 15b117eae8ea0caffec2fbd5cbd366a5ccee20ab)
11acd27e7Smillert /*
21acd27e7Smillert  * rl - command-line interface to read a line from the standard input
31acd27e7Smillert  *      (or another fd) using readline.
41acd27e7Smillert  *
51acd27e7Smillert  * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
61acd27e7Smillert  */
71acd27e7Smillert 
8*15b117eaSkettenis /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
9*15b117eaSkettenis 
10*15b117eaSkettenis    This file is part of the GNU Readline Library, a library for
11*15b117eaSkettenis    reading lines of text with interactive input and history editing.
12*15b117eaSkettenis 
13*15b117eaSkettenis    The GNU Readline Library is free software; you can redistribute it
14*15b117eaSkettenis    and/or modify it under the terms of the GNU General Public License
15*15b117eaSkettenis    as published by the Free Software Foundation; either version 2, or
16*15b117eaSkettenis    (at your option) any later version.
17*15b117eaSkettenis 
18*15b117eaSkettenis    The GNU Readline Library is distributed in the hope that it will be
19*15b117eaSkettenis    useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20*15b117eaSkettenis    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*15b117eaSkettenis    GNU General Public License for more details.
22*15b117eaSkettenis 
23*15b117eaSkettenis    The GNU General Public License is often shipped with GNU software, and
24*15b117eaSkettenis    is generally kept in a file called COPYING or LICENSE.  If you do not
25*15b117eaSkettenis    have a copy of the license, write to the Free Software Foundation,
26*15b117eaSkettenis    59 Temple Place, Suite 330, Boston, MA 02111 USA. */
27*15b117eaSkettenis 
281acd27e7Smillert #if defined (HAVE_CONFIG_H)
291acd27e7Smillert #  include <config.h>
301acd27e7Smillert #endif
311acd27e7Smillert 
321acd27e7Smillert #include <stdio.h>
331acd27e7Smillert #include <sys/types.h>
341acd27e7Smillert #include "posixstat.h"
351acd27e7Smillert 
361acd27e7Smillert #if defined (READLINE_LIBRARY)
371acd27e7Smillert #  include "readline.h"
381acd27e7Smillert #  include "history.h"
391acd27e7Smillert #else
401acd27e7Smillert #  include <readline/readline.h>
411acd27e7Smillert #  include <readline/history.h>
421acd27e7Smillert #endif
431acd27e7Smillert 
441acd27e7Smillert extern int optind;
451acd27e7Smillert extern char *optarg;
461acd27e7Smillert 
471acd27e7Smillert #if !defined (strchr) && !defined (__STDC__)
481acd27e7Smillert extern char *strrchr();
491acd27e7Smillert #endif
501acd27e7Smillert 
511acd27e7Smillert static char *progname;
521acd27e7Smillert static char *deftext;
531acd27e7Smillert 
541acd27e7Smillert static int
set_deftext()551acd27e7Smillert set_deftext ()
561acd27e7Smillert {
571acd27e7Smillert   if (deftext)
581acd27e7Smillert     {
591acd27e7Smillert       rl_insert_text (deftext);
601acd27e7Smillert       deftext = (char *)NULL;
61*15b117eaSkettenis       rl_startup_hook = (rl_hook_func_t *)NULL;
621acd27e7Smillert     }
631acd27e7Smillert   return 0;
641acd27e7Smillert }
651acd27e7Smillert 
661acd27e7Smillert static void
usage()671acd27e7Smillert usage()
681acd27e7Smillert {
691acd27e7Smillert   fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
701acd27e7Smillert 		progname, progname);
711acd27e7Smillert }
721acd27e7Smillert 
731acd27e7Smillert int
main(argc,argv)741acd27e7Smillert main (argc, argv)
751acd27e7Smillert      int argc;
761acd27e7Smillert      char **argv;
771acd27e7Smillert {
781acd27e7Smillert   char *temp, *prompt;
791acd27e7Smillert   struct stat sb;
801acd27e7Smillert   int opt, fd, nch;
811acd27e7Smillert   FILE *ifp;
821acd27e7Smillert 
831acd27e7Smillert   progname = strrchr(argv[0], '/');
841acd27e7Smillert   if (progname == 0)
851acd27e7Smillert     progname = argv[0];
861acd27e7Smillert   else
871acd27e7Smillert     progname++;
881acd27e7Smillert 
891acd27e7Smillert   /* defaults */
901acd27e7Smillert   prompt = "readline$ ";
911acd27e7Smillert   fd = nch = 0;
921acd27e7Smillert   deftext = (char *)0;
931acd27e7Smillert 
941acd27e7Smillert   while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
951acd27e7Smillert     {
961acd27e7Smillert       switch (opt)
971acd27e7Smillert 	{
981acd27e7Smillert 	case 'p':
991acd27e7Smillert 	  prompt = optarg;
1001acd27e7Smillert 	  break;
1011acd27e7Smillert 	case 'u':
1021acd27e7Smillert 	  fd = atoi(optarg);
1031acd27e7Smillert 	  if (fd < 0)
1041acd27e7Smillert 	    {
1051acd27e7Smillert 	      fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
1061acd27e7Smillert 	      exit (2);
1071acd27e7Smillert 	    }
1081acd27e7Smillert 	  break;
1091acd27e7Smillert 	case 'd':
1101acd27e7Smillert 	  deftext = optarg;
1111acd27e7Smillert 	  break;
1121acd27e7Smillert 	case 'n':
1131acd27e7Smillert 	  nch = atoi(optarg);
1141acd27e7Smillert 	  if (nch < 0)
1151acd27e7Smillert 	    {
1161acd27e7Smillert 	      fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
1171acd27e7Smillert 	      exit (2);
1181acd27e7Smillert 	    }
1191acd27e7Smillert 	  break;
1201acd27e7Smillert 	default:
1211acd27e7Smillert 	  usage ();
1221acd27e7Smillert 	  exit (2);
1231acd27e7Smillert 	}
1241acd27e7Smillert     }
1251acd27e7Smillert 
1261acd27e7Smillert   if (fd != 0)
1271acd27e7Smillert     {
1281acd27e7Smillert       if (fstat (fd, &sb) < 0)
1291acd27e7Smillert 	{
1301acd27e7Smillert 	  fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
1311acd27e7Smillert 	  exit (1);
1321acd27e7Smillert 	}
1331acd27e7Smillert       ifp = fdopen (fd, "r");
1341acd27e7Smillert       rl_instream = ifp;
1351acd27e7Smillert     }
1361acd27e7Smillert 
1371acd27e7Smillert   if (deftext && *deftext)
1381acd27e7Smillert     rl_startup_hook = set_deftext;
1391acd27e7Smillert 
1401acd27e7Smillert   if (nch > 0)
1411acd27e7Smillert     rl_num_chars_to_read = nch;
1421acd27e7Smillert 
1431acd27e7Smillert   temp = readline (prompt);
1441acd27e7Smillert 
1451acd27e7Smillert   /* Test for EOF. */
1461acd27e7Smillert   if (temp == 0)
1471acd27e7Smillert     exit (1);
1481acd27e7Smillert 
149*15b117eaSkettenis   printf ("%s\n", temp);
1501acd27e7Smillert   exit (0);
1511acd27e7Smillert }
152