Readline 5.1 import for HEAD.
[deliverable/binutils-gdb.git] / readline / examples / rl.c
CommitLineData
d60d9f65
SS
1/*
2 * rl - command-line interface to read a line from the standard input
3 * (or another fd) using readline.
4 *
1b17e766 5 * usage: rl [-p prompt] [-u unit] [-d default] [-n nchars]
d60d9f65
SS
6 */
7
9255ee31
EZ
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
d60d9f65
SS
28#if defined (HAVE_CONFIG_H)
29# include <config.h>
30#endif
31
32#include <stdio.h>
33#include <sys/types.h>
5bdf8622
DJ
34
35#ifdef HAVE_STDLIB_H
36# include <stdlib.h>
37#else
38extern void exit();
39#endif
1b17e766
EZ
40
41#if defined (READLINE_LIBRARY)
5bdf8622 42# include "posixstat.h"
1b17e766
EZ
43# include "readline.h"
44# include "history.h"
45#else
5bdf8622 46# include <sys/stat.h>
1b17e766
EZ
47# include <readline/readline.h>
48# include <readline/history.h>
49#endif
d60d9f65
SS
50
51extern int optind;
52extern char *optarg;
53
54#if !defined (strchr) && !defined (__STDC__)
55extern char *strrchr();
56#endif
57
58static char *progname;
59static char *deftext;
60
61static int
62set_deftext ()
63{
64 if (deftext)
65 {
66 rl_insert_text (deftext);
67 deftext = (char *)NULL;
9255ee31 68 rl_startup_hook = (rl_hook_func_t *)NULL;
d60d9f65 69 }
1b17e766 70 return 0;
d60d9f65
SS
71}
72
c862e87b 73static void
d60d9f65
SS
74usage()
75{
1b17e766 76 fprintf (stderr, "%s: usage: %s [-p prompt] [-u unit] [-d default] [-n nchars]\n",
d60d9f65
SS
77 progname, progname);
78}
79
1b17e766 80int
d60d9f65
SS
81main (argc, argv)
82 int argc;
83 char **argv;
84{
85 char *temp, *prompt;
86 struct stat sb;
1b17e766 87 int opt, fd, nch;
d60d9f65
SS
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$ ";
1b17e766 98 fd = nch = 0;
d60d9f65
SS
99 deftext = (char *)0;
100
1b17e766 101 while ((opt = getopt(argc, argv, "p:u:d:n:")) != EOF)
d60d9f65
SS
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;
1b17e766
EZ
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;
d60d9f65
SS
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
1b17e766
EZ
147 if (nch > 0)
148 rl_num_chars_to_read = nch;
149
d60d9f65
SS
150 temp = readline (prompt);
151
152 /* Test for EOF. */
153 if (temp == 0)
154 exit (1);
155
9255ee31 156 printf ("%s\n", temp);
d60d9f65
SS
157 exit (0);
158}
This page took 0.306526 seconds and 4 git commands to generate.