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 | ||
d60d9f65 SS |
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" | |
1b17e766 EZ |
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 | |
d60d9f65 SS |
23 | |
24 | extern int optind; | |
25 | extern char *optarg; | |
26 | ||
27 | #if !defined (strchr) && !defined (__STDC__) | |
28 | extern char *strrchr(); | |
29 | #endif | |
30 | ||
31 | static char *progname; | |
32 | static char *deftext; | |
33 | ||
34 | static int | |
35 | set_deftext () | |
36 | { | |
37 | if (deftext) | |
38 | { | |
39 | rl_insert_text (deftext); | |
40 | deftext = (char *)NULL; | |
41 | rl_startup_hook = (Function *)NULL; | |
42 | } | |
1b17e766 | 43 | return 0; |
d60d9f65 SS |
44 | } |
45 | ||
c862e87b | 46 | static void |
d60d9f65 SS |
47 | usage() |
48 | { | |
1b17e766 | 49 | fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n", |
d60d9f65 SS |
50 | progname, progname); |
51 | } | |
52 | ||
1b17e766 | 53 | int |
d60d9f65 SS |
54 | main (argc, argv) |
55 | int argc; | |
56 | char **argv; | |
57 | { | |
58 | char *temp, *prompt; | |
59 | struct stat sb; | |
1b17e766 | 60 | int opt, fd, nch; |
d60d9f65 SS |
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$ "; | |
1b17e766 | 71 | fd = nch = 0; |
d60d9f65 SS |
72 | deftext = (char *)0; |
73 | ||
1b17e766 | 74 | while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF) |
d60d9f65 SS |
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; | |
1b17e766 EZ |
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; | |
d60d9f65 SS |
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 | ||
1b17e766 EZ |
120 | if (nch > 0) |
121 | rl_num_chars_to_read = nch; | |
122 | ||
d60d9f65 SS |
123 | temp = readline (prompt); |
124 | ||
125 | /* Test for EOF. */ | |
126 | if (temp == 0) | |
127 | exit (1); | |
128 | ||
129 | puts (temp); | |
130 | exit (0); | |
131 | } |