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