source: trunk/third/readline/examples/rl.c @ 17010

Revision 17010, 2.2 KB checked in by ghudson, 23 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r17009, which included commits to RCS files with non-trunk default branches.
Line 
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#if defined (HAVE_CONFIG_H)
9#  include <config.h>
10#endif
11
12#include <stdio.h>
13#include <sys/types.h>
14#include "posixstat.h"
15
16#if defined (READLINE_LIBRARY)
17#  include "readline.h"
18#  include "history.h"
19#else
20#  include <readline/readline.h>
21#  include <readline/history.h>
22#endif
23
24extern int optind;
25extern char *optarg;
26
27#if !defined (strchr) && !defined (__STDC__)
28extern char *strrchr();
29#endif
30
31static char *progname;
32static char *deftext;
33
34static int
35set_deftext ()
36{
37  if (deftext)
38    {
39      rl_insert_text (deftext);
40      deftext = (char *)NULL;
41      rl_startup_hook = (rl_hook_func_t *)NULL;
42    }
43  return 0;
44}
45
46static void
47usage()
48{
49  fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
50                progname, progname);
51}
52
53int
54main (argc, argv)
55     int argc;
56     char **argv;
57{
58  char *temp, *prompt;
59  struct stat sb;
60  int opt, fd, nch;
61  FILE *ifp;
62
63  progname = strrchr(argv[0], '/');
64  if (progname == 0)
65    progname = argv[0];
66  else
67    progname++;
68
69  /* defaults */
70  prompt = "readline$ ";
71  fd = nch = 0;
72  deftext = (char *)0;
73
74  while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
75    {
76      switch (opt)
77        {
78        case 'p':
79          prompt = optarg;
80          break;
81        case 'u':
82          fd = atoi(optarg);
83          if (fd < 0)
84            {
85              fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
86              exit (2);
87            }
88          break;
89        case 'd':
90          deftext = optarg;
91          break;
92        case 'n':
93          nch = atoi(optarg);
94          if (nch < 0)
95            {
96              fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
97              exit (2);
98            }
99          break;
100        default:
101          usage ();
102          exit (2);
103        }
104    }
105
106  if (fd != 0)
107    {
108      if (fstat (fd, &sb) < 0)
109        {
110          fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
111          exit (1);
112        }
113      ifp = fdopen (fd, "r");
114      rl_instream = ifp;
115    }
116
117  if (deftext && *deftext)
118    rl_startup_hook = set_deftext;
119
120  if (nch > 0)
121    rl_num_chars_to_read = nch;
122
123  temp = readline (prompt);
124
125  /* Test for EOF. */
126  if (temp == 0)
127    exit (1);
128
129  printf ("%s\n", temp);
130  exit (0);
131}
Note: See TracBrowser for help on using the repository browser.