126497Sache/*
226497Sache * rl - command-line interface to read a line from the standard input
326497Sache *      (or another fd) using readline.
426497Sache *
558310Sache * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
626497Sache */
726497Sache
8119610Sache/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
9119610Sache
10119610Sache   This file is part of the GNU Readline Library, a library for
11119610Sache   reading lines of text with interactive input and history editing.
12119610Sache
13119610Sache   The GNU Readline Library is free software; you can redistribute it
14119610Sache   and/or modify it under the terms of the GNU General Public License
15119610Sache   as published by the Free Software Foundation; either version 2, or
16119610Sache   (at your option) any later version.
17119610Sache
18119610Sache   The GNU Readline Library is distributed in the hope that it will be
19119610Sache   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20119610Sache   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21119610Sache   GNU General Public License for more details.
22119610Sache
23119610Sache   The GNU General Public License is often shipped with GNU software, and
24119610Sache   is generally kept in a file called COPYING or LICENSE.  If you do not
25119610Sache   have a copy of the license, write to the Free Software Foundation,
26119610Sache   59 Temple Place, Suite 330, Boston, MA 02111 USA. */
27119610Sache
2826497Sache#if defined (HAVE_CONFIG_H)
2935486Sache#  include <config.h>
3026497Sache#endif
3126497Sache
3226497Sache#include <stdio.h>
3326497Sache#include <sys/types.h>
3426497Sache
35157184Sache#ifdef HAVE_STDLIB_H
36157184Sache#  include <stdlib.h>
37157184Sache#else
38157184Sacheextern void exit();
39157184Sache#endif
40157184Sache
4158310Sache#if defined (READLINE_LIBRARY)
42157184Sache#  include "posixstat.h"
4358310Sache#  include "readline.h"
4458310Sache#  include "history.h"
4558310Sache#else
46157184Sache#  include <sys/stat.h>
4758310Sache#  include <readline/readline.h>
4858310Sache#  include <readline/history.h>
4958310Sache#endif
5058310Sache
5126497Sacheextern int optind;
5226497Sacheextern char *optarg;
5326497Sache
5435486Sache#if !defined (strchr) && !defined (__STDC__)
5526497Sacheextern char *strrchr();
5635486Sache#endif
5726497Sache
5826497Sachestatic char *progname;
5926497Sachestatic char *deftext;
6026497Sache
6126497Sachestatic int
6226497Sacheset_deftext ()
6326497Sache{
6426497Sache  if (deftext)
6526497Sache    {
6626497Sache      rl_insert_text (deftext);
6726497Sache      deftext = (char *)NULL;
6875406Sache      rl_startup_hook = (rl_hook_func_t *)NULL;
6926497Sache    }
7058310Sache  return 0;
7126497Sache}
7226497Sache
7347558Sachestatic void
7426497Sacheusage()
7526497Sache{
7658310Sache  fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
7726497Sache		progname, progname);
7826497Sache}
7926497Sache
8058310Sacheint
8126497Sachemain (argc, argv)
8226497Sache     int argc;
8326497Sache     char **argv;
8426497Sache{
8526497Sache  char *temp, *prompt;
8626497Sache  struct stat sb;
8758310Sache  int opt, fd, nch;
8826497Sache  FILE *ifp;
8926497Sache
9026497Sache  progname = strrchr(argv[0], '/');
9126497Sache  if (progname == 0)
9226497Sache    progname = argv[0];
9326497Sache  else
9426497Sache    progname++;
9526497Sache
9626497Sache  /* defaults */
9726497Sache  prompt = "readline$ ";
9858310Sache  fd = nch = 0;
9926497Sache  deftext = (char *)0;
10026497Sache
10158310Sache  while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
10226497Sache    {
10326497Sache      switch (opt)
10426497Sache	{
10526497Sache	case 'p':
10626497Sache	  prompt = optarg;
10726497Sache	  break;
10826497Sache	case 'u':
10926497Sache	  fd = atoi(optarg);
11026497Sache	  if (fd < 0)
11126497Sache	    {
11226497Sache	      fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
11326497Sache	      exit (2);
11426497Sache	    }
11526497Sache	  break;
11626497Sache	case 'd':
11726497Sache	  deftext = optarg;
11826497Sache	  break;
11958310Sache	case 'n':
12058310Sache	  nch = atoi(optarg);
12158310Sache	  if (nch < 0)
12258310Sache	    {
12358310Sache	      fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
12458310Sache	      exit (2);
12558310Sache	    }
12658310Sache	  break;
12726497Sache	default:
12826497Sache	  usage ();
12926497Sache	  exit (2);
13026497Sache	}
13126497Sache    }
13226497Sache
13326497Sache  if (fd != 0)
13426497Sache    {
13526497Sache      if (fstat (fd, &sb) < 0)
13626497Sache	{
13726497Sache	  fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
13826497Sache	  exit (1);
13926497Sache	}
14026497Sache      ifp = fdopen (fd, "r");
14126497Sache      rl_instream = ifp;
14226497Sache    }
14326497Sache
14426497Sache  if (deftext && *deftext)
14526497Sache    rl_startup_hook = set_deftext;
14626497Sache
14758310Sache  if (nch > 0)
14858310Sache    rl_num_chars_to_read = nch;
14958310Sache
15026497Sache  temp = readline (prompt);
15126497Sache
15226497Sache  /* Test for EOF. */
15326497Sache  if (temp == 0)
15426497Sache    exit (1);
15526497Sache
15675406Sache  printf ("%s\n", temp);
15726497Sache  exit (0);
15826497Sache}
159