Automatic date update in version.in
[binutils-gdb.git] / readline / examples / rl.c
blob845a4b176d74ae7539c4b89347266ae2db5bc7b2
1 /*
2 * rl - command-line interface to read a line from the standard input
3 * (or another fd) using readline.
5 * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
6 */
8 /* Copyright (C) 1987-2009 Free Software Foundation, Inc.
10 This file is part of the GNU Readline Library (Readline), a library for
11 reading lines of text with interactive input and history editing.
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
16 (at your option) any later version.
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
21 GNU General Public License for more details.
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/>.
27 #if defined (HAVE_CONFIG_H)
28 # include <config.h>
29 #endif
31 #include <stdio.h>
32 #include <sys/types.h>
34 #ifdef HAVE_STDLIB_H
35 # include <stdlib.h>
36 #else
37 extern void exit();
38 #endif
40 #if defined (READLINE_LIBRARY)
41 # include "posixstat.h"
42 # include "readline.h"
43 # include "history.h"
44 #else
45 # include <sys/stat.h>
46 # include <readline/readline.h>
47 # include <readline/history.h>
48 #endif
50 extern int optind;
51 extern char *optarg;
53 #if !defined (strchr) && !defined (__STDC__)
54 extern char *strrchr();
55 #endif
57 static char *progname;
58 static char *deftext;
60 static int
61 set_deftext ()
63 if (deftext)
65 rl_insert_text (deftext);
66 deftext = (char *)NULL;
67 rl_startup_hook = (rl_hook_func_t *)NULL;
69 return 0;
72 static void
73 usage()
75 fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
76 progname, progname);
79 int
80 main (argc, argv)
81 int argc;
82 char **argv;
84 char *temp, *prompt;
85 struct stat sb;
86 int opt, fd, nch;
87 FILE *ifp;
89 progname = strrchr(argv[0], '/');
90 if (progname == 0)
91 progname = argv[0];
92 else
93 progname++;
95 /* defaults */
96 prompt = "readline$ ";
97 fd = nch = 0;
98 deftext = (char *)0;
100 while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
102 switch (opt)
104 case 'p':
105 prompt = optarg;
106 break;
107 case 'u':
108 fd = atoi(optarg);
109 if (fd < 0)
111 fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
112 exit (2);
114 break;
115 case 'd':
116 deftext = optarg;
117 break;
118 case 'n':
119 nch = atoi(optarg);
120 if (nch < 0)
122 fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
123 exit (2);
125 break;
126 default:
127 usage ();
128 exit (2);
132 if (fd != 0)
134 if (fstat (fd, &sb) < 0)
136 fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
137 exit (1);
139 ifp = fdopen (fd, "r");
140 rl_instream = ifp;
143 if (deftext && *deftext)
144 rl_startup_hook = set_deftext;
146 if (nch > 0)
147 rl_num_chars_to_read = nch;
149 temp = readline (prompt);
151 /* Test for EOF. */
152 if (temp == 0)
153 exit (1);
155 printf ("%s\n", temp);
156 exit (0);