Initial creation of sourceware repository
[deliverable/binutils-gdb.git] / 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 #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 }
43 }
44
45 usage()
46 {
47 fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default]\n",
48 progname, progname);
49 }
50
51 main (argc, argv)
52 int argc;
53 char **argv;
54 {
55 char *temp, *prompt;
56 struct stat sb;
57 int done, opt, fd;
58 FILE *ifp;
59
60 progname = strrchr(argv[0], '/');
61 if (progname == 0)
62 progname = argv[0];
63 else
64 progname++;
65
66 /* defaults */
67 prompt = "readline$ ";
68 fd = 0;
69 deftext = (char *)0;
70
71 while ((opt = getopt(argc, argv, "p:u:d:")) != EOF)
72 {
73 switch (opt)
74 {
75 case 'p':
76 prompt = optarg;
77 break;
78 case 'u':
79 fd = atoi(optarg);
80 if (fd < 0)
81 {
82 fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
83 exit (2);
84 }
85 break;
86 case 'd':
87 deftext = optarg;
88 break;
89 default:
90 usage ();
91 exit (2);
92 }
93 }
94
95 if (fd != 0)
96 {
97 if (fstat (fd, &sb) < 0)
98 {
99 fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
100 exit (1);
101 }
102 ifp = fdopen (fd, "r");
103 rl_instream = ifp;
104 }
105
106 if (deftext && *deftext)
107 rl_startup_hook = set_deftext;
108
109 temp = readline (prompt);
110
111 /* Test for EOF. */
112 if (temp == 0)
113 exit (1);
114
115 puts (temp);
116 exit (0);
117 }
This page took 0.031464 seconds and 4 git commands to generate.