Import of readline 4.3.
[deliverable/binutils-gdb.git] / readline / examples / rl.c
CommitLineData
d60d9f65
SS
1/*
2 * rl - command-line interface to read a line from the standard input
3 * (or another fd) using readline.
4 *
1b17e766 5 * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
d60d9f65
SS
6 */
7
9255ee31
EZ
8/* Copyright (C) 1987-2002 Free Software Foundation, Inc.
9
10 This file is part of the GNU Readline Library, a library for
11 reading lines of text with interactive input and history editing.
12
13 The GNU Readline Library is free software; you can redistribute it
14 and/or modify it under the terms of the GNU General Public License
15 as published by the Free Software Foundation; either version 2, or
16 (at your option) any later version.
17
18 The GNU Readline Library is distributed in the hope that it will be
19 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 The GNU General Public License is often shipped with GNU software, and
24 is generally kept in a file called COPYING or LICENSE. If you do not
25 have a copy of the license, write to the Free Software Foundation,
26 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
27
d60d9f65
SS
28#if defined (HAVE_CONFIG_H)
29# include <config.h>
30#endif
31
32#include <stdio.h>
33#include <sys/types.h>
34#include "posixstat.h"
1b17e766
EZ
35
36#if defined (READLINE_LIBRARY)
37# include "readline.h"
38# include "history.h"
39#else
40# include <readline/readline.h>
41# include <readline/history.h>
42#endif
d60d9f65
SS
43
44extern int optind;
45extern char *optarg;
46
47#if !defined (strchr) && !defined (__STDC__)
48extern char *strrchr();
49#endif
50
51static char *progname;
52static char *deftext;
53
54static int
55set_deftext ()
56{
57 if (deftext)
58 {
59 rl_insert_text (deftext);
60 deftext = (char *)NULL;
9255ee31 61 rl_startup_hook = (rl_hook_func_t *)NULL;
d60d9f65 62 }
1b17e766 63 return 0;
d60d9f65
SS
64}
65
c862e87b 66static void
d60d9f65
SS
67usage()
68{
1b17e766 69 fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
d60d9f65
SS
70 progname, progname);
71}
72
1b17e766 73int
d60d9f65
SS
74main (argc, argv)
75 int argc;
76 char **argv;
77{
78 char *temp, *prompt;
79 struct stat sb;
1b17e766 80 int opt, fd, nch;
d60d9f65
SS
81 FILE *ifp;
82
83 progname = strrchr(argv[0], '/');
84 if (progname == 0)
85 progname = argv[0];
86 else
87 progname++;
88
89 /* defaults */
90 prompt = "readline$ ";
1b17e766 91 fd = nch = 0;
d60d9f65
SS
92 deftext = (char *)0;
93
1b17e766 94 while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
d60d9f65
SS
95 {
96 switch (opt)
97 {
98 case 'p':
99 prompt = optarg;
100 break;
101 case 'u':
102 fd = atoi(optarg);
103 if (fd < 0)
104 {
105 fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
106 exit (2);
107 }
108 break;
109 case 'd':
110 deftext = optarg;
111 break;
1b17e766
EZ
112 case 'n':
113 nch = atoi(optarg);
114 if (nch < 0)
115 {
116 fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
117 exit (2);
118 }
119 break;
d60d9f65
SS
120 default:
121 usage ();
122 exit (2);
123 }
124 }
125
126 if (fd != 0)
127 {
128 if (fstat (fd, &sb) < 0)
129 {
130 fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
131 exit (1);
132 }
133 ifp = fdopen (fd, "r");
134 rl_instream = ifp;
135 }
136
137 if (deftext && *deftext)
138 rl_startup_hook = set_deftext;
139
1b17e766
EZ
140 if (nch > 0)
141 rl_num_chars_to_read = nch;
142
d60d9f65
SS
143 temp = readline (prompt);
144
145 /* Test for EOF. */
146 if (temp == 0)
147 exit (1);
148
9255ee31 149 printf ("%s\n", temp);
d60d9f65
SS
150 exit (0);
151}
This page took 0.20061 seconds and 4 git commands to generate.