]> git.ipfire.org Git - thirdparty/bash.git/blob - lib/readline/examples/rl.c
Imported from ../bash-2.01.tar.gz.
[thirdparty/bash.git] / lib / readline / examples / rl.c
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]
6 */
7
8 /*
9 * Remove the next line if you're compiling this against an installed
10 * libreadline.a
11 */
12 #define READLINE_LIBRARY
13
14 #if defined (HAVE_CONFIG_H)
15 #include <config.h>
16 #endif
17
18 #include <stdio.h>
19 #include <sys/types.h>
20 #include "posixstat.h"
21 #include "readline.h"
22 #include "history.h"
23
24 extern int optind;
25 extern char *optarg;
26
27 extern char *strrchr();
28
29 static char *progname;
30 static char *deftext;
31
32 static int
33 set_deftext ()
34 {
35 if (deftext)
36 {
37 rl_insert_text (deftext);
38 deftext = (char *)NULL;
39 rl_startup_hook = (Function *)NULL;
40 }
41 }
42
43 usage()
44 {
45 fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default]\n",
46 progname, progname);
47 }
48
49 main (argc, argv)
50 int argc;
51 char **argv;
52 {
53 char *temp, *prompt;
54 struct stat sb;
55 int done, opt, fd;
56 FILE *ifp;
57
58 progname = strrchr(argv[0], '/');
59 if (progname == 0)
60 progname = argv[0];
61 else
62 progname++;
63
64 /* defaults */
65 prompt = "readline$ ";
66 fd = 0;
67 deftext = (char *)0;
68
69 while ((opt = getopt(argc, argv, "p:u:d:")) != EOF)
70 {
71 switch (opt)
72 {
73 case 'p':
74 prompt = optarg;
75 break;
76 case 'u':
77 fd = atoi(optarg);
78 if (fd < 0)
79 {
80 fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
81 exit (2);
82 }
83 break;
84 case 'd':
85 deftext = optarg;
86 break;
87 default:
88 usage ();
89 exit (2);
90 }
91 }
92
93 if (fd != 0)
94 {
95 if (fstat (fd, &sb) < 0)
96 {
97 fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
98 exit (1);
99 }
100 ifp = fdopen (fd, "r");
101 rl_instream = ifp;
102 }
103
104 if (deftext && *deftext)
105 rl_startup_hook = set_deftext;
106
107 temp = readline (prompt);
108
109 /* Test for EOF. */
110 if (temp == 0)
111 exit (1);
112
113 puts (temp);
114 exit (0);
115 }