150983Swpaul/*
250983Swpaul * rl - command-line interface to read a line from the standard input
350983Swpaul *      (or another fd) using readline.
450983Swpaul *
550983Swpaul * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
650983Swpaul */
750983Swpaul
850983Swpaul/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
950983Swpaul
1050983Swpaul   This file is part of the GNU Readline Library, a library for
1150983Swpaul   reading lines of text with interactive input and history editing.
1250983Swpaul
1350983Swpaul   The GNU Readline Library is free software; you can redistribute it
1450983Swpaul   and/or modify it under the terms of the GNU General Public License
1550983Swpaul   as published by the Free Software Foundation; either version 2, or
1650983Swpaul   (at your option) any later version.
1750983Swpaul
1850983Swpaul   The GNU Readline Library is distributed in the hope that it will be
1950983Swpaul   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
2050983Swpaul   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2150983Swpaul   GNU General Public License for more details.
2250983Swpaul
2350983Swpaul   The GNU General Public License is often shipped with GNU software, and
2450983Swpaul   is generally kept in a file called COPYING or LICENSE.  If you do not
2550983Swpaul   have a copy of the license, write to the Free Software Foundation,
2650983Swpaul   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
2750983Swpaul
2850983Swpaul#if defined (HAVE_CONFIG_H)
2950983Swpaul#  include <config.h>
3050983Swpaul#endif
3150983Swpaul
3250983Swpaul#include <stdio.h>
3350983Swpaul#include <sys/types.h>
3450983Swpaul#include "posixstat.h"
3550983Swpaul
3650983Swpaul#if defined (READLINE_LIBRARY)
3751428Swpaul#  include "readline.h"
3851428Swpaul#  include "history.h"
3950983Swpaul#else
4050983Swpaul#  include <readline/readline.h>
4150983Swpaul#  include <readline/history.h>
4250983Swpaul#endif
4350983Swpaul
4450983Swpaulextern int optind;
4550983Swpaulextern char *optarg;
4650983Swpaul
4750983Swpaul#if !defined (strchr) && !defined (__STDC__)
4850983Swpaulextern char *strrchr();
4950983Swpaul#endif
5050983Swpaul
5150983Swpaulstatic char *progname;
5250983Swpaulstatic char *deftext;
5350983Swpaul
5450983Swpaulstatic int
5550983Swpaulset_deftext ()
5650983Swpaul{
5750983Swpaul  if (deftext)
5850983Swpaul    {
5950983Swpaul      rl_insert_text (deftext);
6050983Swpaul      deftext = (char *)NULL;
6150983Swpaul      rl_startup_hook = (rl_hook_func_t *)NULL;
6250983Swpaul    }
63105135Salfred  return 0;
64105135Salfred}
6550983Swpaul
6650983Swpaulstatic void
6750983Swpaulusage()
6850983Swpaul{
6950983Swpaul  fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
7095722Sphk		progname, progname);
7150983Swpaul}
7250983Swpaul
7350983Swpaulint
7450983Swpaulmain (argc, argv)
7550983Swpaul     int argc;
7650983Swpaul     char **argv;
7750983Swpaul{
7850983Swpaul  char *temp, *prompt;
7950983Swpaul  struct stat sb;
8050983Swpaul  int opt, fd, nch;
8150983Swpaul  FILE *ifp;
8250983Swpaul
8350983Swpaul  progname = strrchr(argv[0], '/');
8450983Swpaul  if (progname == 0)
8592739Salfred    progname = argv[0];
8692739Salfred  else
8750983Swpaul    progname++;
88105135Salfred
89105135Salfred  /* defaults */
9050983Swpaul  prompt = "readline$ ";
9150983Swpaul  fd = nch = 0;
9250983Swpaul  deftext = (char *)0;
9350983Swpaul
9450983Swpaul  while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
9550983Swpaul    {
9651428Swpaul      switch (opt)
9751428Swpaul	{
9851428Swpaul	case 'p':
9951428Swpaul	  prompt = optarg;
10050983Swpaul	  break;
10150983Swpaul	case 'u':
10251428Swpaul	  fd = atoi(optarg);
10351428Swpaul	  if (fd < 0)
10451428Swpaul	    {
10551428Swpaul	      fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
10650983Swpaul	      exit (2);
10750983Swpaul	    }
10850983Swpaul	  break;
10950983Swpaul	case 'd':
110105135Salfred	  deftext = optarg;
111105135Salfred	  break;
11250983Swpaul	case 'n':
11350983Swpaul	  nch = atoi(optarg);
11450983Swpaul	  if (nch < 0)
11550983Swpaul	    {
11650983Swpaul	      fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
11750983Swpaul	      exit (2);
11850983Swpaul	    }
11950983Swpaul	  break;
12050983Swpaul	default:
12150983Swpaul	  usage ();
12250983Swpaul	  exit (2);
12350983Swpaul	}
12450983Swpaul    }
12550983Swpaul
12650983Swpaul  if (fd != 0)
12750983Swpaul    {
12850983Swpaul      if (fstat (fd, &sb) < 0)
12950983Swpaul	{
13050983Swpaul	  fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
13150983Swpaul	  exit (1);
13250983Swpaul	}
13350983Swpaul      ifp = fdopen (fd, "r");
13450983Swpaul      rl_instream = ifp;
13550983Swpaul    }
13650983Swpaul
13750983Swpaul  if (deftext && *deftext)
13850983Swpaul    rl_startup_hook = set_deftext;
13950983Swpaul
14050983Swpaul  if (nch > 0)
14150983Swpaul    rl_num_chars_to_read = nch;
14250983Swpaul
14350983Swpaul  temp = readline (prompt);
14450983Swpaul
14550983Swpaul  /* Test for EOF. */
14695667Sphk  if (temp == 0)
14750983Swpaul    exit (1);
14850983Swpaul
14950983Swpaul  printf ("%s\n", temp);
15050983Swpaul  exit (0);
15150983Swpaul}
15250983Swpaul