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