]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - readline/examples/rl.c
This commit was manufactured by cvs2svn to create branch 'gdb_7_2-branch'.
[thirdparty/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] [-n nchars]
6 */
7
8 /* Copyright (C) 1987-2002 Free Software Foundation, Inc.
9
10 This file is part of the GNU Readline Library, a library for
11 reading lines of text with interactive input and history editing.
12
13 The GNU Readline Library is free software; you can redistribute it
14 and/or modify it under the terms of the GNU General Public License
15 as published by the Free Software Foundation; either version 2, or
16 (at your option) any later version.
17
18 The GNU Readline Library is distributed in the hope that it will be
19 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
20 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 The GNU General Public License is often shipped with GNU software, and
24 is generally kept in a file called COPYING or LICENSE. If you do not
25 have a copy of the license, write to the Free Software Foundation,
26 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
27
28 #if defined (HAVE_CONFIG_H)
29 # include <config.h>
30 #endif
31
32 #include <stdio.h>
33 #include <sys/types.h>
34
35 #ifdef HAVE_STDLIB_H
36 # include <stdlib.h>
37 #else
38 extern void exit();
39 #endif
40
41 #if defined (READLINE_LIBRARY)
42 # include "posixstat.h"
43 # include "readline.h"
44 # include "history.h"
45 #else
46 # include <sys/stat.h>
47 # include <readline/readline.h>
48 # include <readline/history.h>
49 #endif
50
51 extern int optind;
52 extern char *optarg;
53
54 #if !defined (strchr) && !defined (__STDC__)
55 extern char *strrchr();
56 #endif
57
58 static char *progname;
59 static char *deftext;
60
61 static int
62 set_deftext ()
63 {
64 if (deftext)
65 {
66 rl_insert_text (deftext);
67 deftext = (char *)NULL;
68 rl_startup_hook = (rl_hook_func_t *)NULL;
69 }
70 return 0;
71 }
72
73 static void
74 usage()
75 {
76 fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
77 progname, progname);
78 }
79
80 int
81 main (argc, argv)
82 int argc;
83 char **argv;
84 {
85 char *temp, *prompt;
86 struct stat sb;
87 int opt, fd, nch;
88 FILE *ifp;
89
90 progname = strrchr(argv[0], '/');
91 if (progname == 0)
92 progname = argv[0];
93 else
94 progname++;
95
96 /* defaults */
97 prompt = "readline$ ";
98 fd = nch = 0;
99 deftext = (char *)0;
100
101 while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
102 {
103 switch (opt)
104 {
105 case 'p':
106 prompt = optarg;
107 break;
108 case 'u':
109 fd = atoi(optarg);
110 if (fd < 0)
111 {
112 fprintf (stderr, "%s: bad file descriptor `%s'\n", progname, optarg);
113 exit (2);
114 }
115 break;
116 case 'd':
117 deftext = optarg;
118 break;
119 case 'n':
120 nch = atoi(optarg);
121 if (nch < 0)
122 {
123 fprintf (stderr, "%s: bad value for -n: `%s'\n", progname, optarg);
124 exit (2);
125 }
126 break;
127 default:
128 usage ();
129 exit (2);
130 }
131 }
132
133 if (fd != 0)
134 {
135 if (fstat (fd, &sb) < 0)
136 {
137 fprintf (stderr, "%s: %d: bad file descriptor\n", progname, fd);
138 exit (1);
139 }
140 ifp = fdopen (fd, "r");
141 rl_instream = ifp;
142 }
143
144 if (deftext && *deftext)
145 rl_startup_hook = set_deftext;
146
147 if (nch > 0)
148 rl_num_chars_to_read = nch;
149
150 temp = readline (prompt);
151
152 /* Test for EOF. */
153 if (temp == 0)
154 exit (1);
155
156 printf ("%s\n", temp);
157 exit (0);
158 }