1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * read_line.c -- Input line reading routines for mgquery
|
---|
4 | * Copyright (C) 1994 Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: read_line.cpp 711 1999-10-17 23:43:31Z cs025 $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include <stdio.h>
|
---|
25 | #include "non_ansi.h"
|
---|
26 |
|
---|
27 | #include "sysfuncs.h"
|
---|
28 | #include "memlib.h"
|
---|
29 |
|
---|
30 | #ifdef WITH_GNU_READLINE
|
---|
31 | #include "readline/readline.h"
|
---|
32 | #include "readline/chardefs.h"
|
---|
33 | #if 0
|
---|
34 | #include "readline/history.h" /* dropped from recent versions of GNU readline */
|
---|
35 | #endif
|
---|
36 | #endif
|
---|
37 |
|
---|
38 |
|
---|
39 | #include "globals.h"
|
---|
40 | #include "environment.h"
|
---|
41 | #include "read_line.h"
|
---|
42 |
|
---|
43 |
|
---|
44 |
|
---|
45 | #ifdef WITH_GNU_READLINE
|
---|
46 | int rl_bind_key (int, int (*)());
|
---|
47 |
|
---|
48 |
|
---|
49 | void
|
---|
50 | Init_ReadLine (void)
|
---|
51 | {
|
---|
52 | /* Make TAB just insert itself rather than do a file name completion */
|
---|
53 | rl_bind_key (TAB, rl_insert);
|
---|
54 | }
|
---|
55 | #else
|
---|
56 | static FILE *rl_instream;
|
---|
57 | static FILE *rl_outstream;
|
---|
58 |
|
---|
59 | void
|
---|
60 | Init_ReadLine (void)
|
---|
61 | {
|
---|
62 | rl_instream = stdin;
|
---|
63 | rl_outstream = stdout;
|
---|
64 | }
|
---|
65 | #endif
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 |
|
---|
70 | /* WritePrompt() */
|
---|
71 | /* Write out a prompt if user is a TTY */
|
---|
72 | void
|
---|
73 | WritePrompt (void)
|
---|
74 | {
|
---|
75 | if (isatty (fileno (InFile)))
|
---|
76 | {
|
---|
77 | if (!BooleanEnv (GetEnv ("expert"), 0))
|
---|
78 | fprintf (stderr, "Enter a command or query (.quit to terminate, .help for assistance).\n");
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | #ifdef WITH_GNU_READLINE
|
---|
83 | static void memory_error_and_abort ();
|
---|
84 |
|
---|
85 | void *
|
---|
86 | xmalloc (size_t bytes)
|
---|
87 | {
|
---|
88 | void *temp = Xmalloc (bytes);
|
---|
89 |
|
---|
90 | if (!temp)
|
---|
91 | memory_error_and_abort ();
|
---|
92 | return (temp);
|
---|
93 | }
|
---|
94 |
|
---|
95 | void *
|
---|
96 | xrealloc (void *pointer, size_t bytes)
|
---|
97 | {
|
---|
98 | void *temp;
|
---|
99 |
|
---|
100 | if (!pointer)
|
---|
101 | temp = xmalloc (bytes);
|
---|
102 | else
|
---|
103 | temp = Xrealloc (pointer, bytes);
|
---|
104 |
|
---|
105 | if (!temp)
|
---|
106 | memory_error_and_abort ();
|
---|
107 |
|
---|
108 | return (temp);
|
---|
109 | }
|
---|
110 |
|
---|
111 | static void
|
---|
112 | memory_error_and_abort ()
|
---|
113 | {
|
---|
114 | fprintf (stderr, "history: Out of virtual memory!\n");
|
---|
115 | abort ();
|
---|
116 | }
|
---|
117 | #endif
|
---|
118 |
|
---|
119 |
|
---|
120 | #ifndef WITH_GNU_READLINE
|
---|
121 |
|
---|
122 | static char *
|
---|
123 | readline (char *pmt)
|
---|
124 | {
|
---|
125 | static char buf[MAXLINEBUFFERLEN + 1];
|
---|
126 | char *s;
|
---|
127 |
|
---|
128 | fprintf (rl_outstream, "%s", pmt);
|
---|
129 | s = fgets (buf, sizeof (buf), rl_instream);
|
---|
130 | if (s)
|
---|
131 | {
|
---|
132 | char *s1 = strrchr (s, '\n');
|
---|
133 | if (s1 && *(s1 + 1) == '\0')
|
---|
134 | *s1 = '\0';
|
---|
135 | }
|
---|
136 | return s ? (char *) Xstrdup (s) : (char *) NULL;
|
---|
137 | }
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | /*
|
---|
141 | * This routine returns a pointer to the users entered line
|
---|
142 | *
|
---|
143 | */
|
---|
144 | static char *
|
---|
145 | GetLine (char *pmt)
|
---|
146 | {
|
---|
147 | static char *the_line = NULL;
|
---|
148 | if (the_line)
|
---|
149 | delete the_line;
|
---|
150 | the_line = NULL;
|
---|
151 | rl_instream = InFile;
|
---|
152 | if (isatty (fileno (InFile)))
|
---|
153 | {
|
---|
154 | fputc ('\r', stderr);
|
---|
155 | if (!isatty (fileno (OutFile)))
|
---|
156 | rl_outstream = stderr;
|
---|
157 | else
|
---|
158 | rl_outstream = OutFile;
|
---|
159 | the_line = readline (pmt);
|
---|
160 | }
|
---|
161 | else
|
---|
162 | {
|
---|
163 | if (isatty (fileno (OutFile)))
|
---|
164 | {
|
---|
165 | the_line = readline (pmt);
|
---|
166 | fprintf (stderr, "%s\n", the_line ? the_line : "");
|
---|
167 | }
|
---|
168 | else
|
---|
169 | {
|
---|
170 | the_line = readline ("");
|
---|
171 | if (the_line)
|
---|
172 | fprintf (stderr, "%s%s\n", pmt, the_line);
|
---|
173 | }
|
---|
174 | }
|
---|
175 | #ifdef WITH_GNU_READLINE
|
---|
176 | if (the_line && *the_line)
|
---|
177 | add_history (the_line);
|
---|
178 | #endif
|
---|
179 | return (the_line);
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 |
|
---|
184 |
|
---|
185 |
|
---|
186 |
|
---|
187 | char *
|
---|
188 | GetMultiLine (void)
|
---|
189 | {
|
---|
190 | static char *line = NULL;
|
---|
191 | char *s;
|
---|
192 | if (line)
|
---|
193 | delete line;
|
---|
194 | line = NULL;
|
---|
195 |
|
---|
196 | if (!(s = GetLine ("> ")))
|
---|
197 | return (NULL);
|
---|
198 | if (!(line = Xstrdup (s)))
|
---|
199 | {
|
---|
200 | fprintf (stderr, "Unable to allocate memory for the line\n");
|
---|
201 | abort ();
|
---|
202 | }
|
---|
203 |
|
---|
204 | while ((s = strrchr (line, '\\')) && *(s + 1) == '\0')
|
---|
205 | {
|
---|
206 | char *newline;
|
---|
207 | *strrchr (line, '\\') = '\0';
|
---|
208 | if (!(s = GetLine ("? ")))
|
---|
209 | return (NULL);
|
---|
210 | if (!(newline = new char[strlen (line) + strlen (s) + 2]))
|
---|
211 | {
|
---|
212 | fprintf (stderr, "Unable to allocate memory for the line\n");
|
---|
213 | abort ();
|
---|
214 | }
|
---|
215 | strcpy (newline, line);
|
---|
216 | strcat (newline, "\n");
|
---|
217 | strcat (newline, s);
|
---|
218 | delete line;
|
---|
219 | line = newline;
|
---|
220 | }
|
---|
221 | return (line);
|
---|
222 | }
|
---|