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.c 3745 2003-02-20 21:20:24Z mdewsnip $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "sysfuncs.h"
|
---|
25 | #include "memlib.h"
|
---|
26 |
|
---|
27 | #ifdef GNU_READLINE
|
---|
28 | #include "readline.h"
|
---|
29 | #include "chardefs.h"
|
---|
30 | #include "history.h"
|
---|
31 | #endif
|
---|
32 |
|
---|
33 |
|
---|
34 | #include "globals.h"
|
---|
35 | #include "environment.h"
|
---|
36 | #include "read_line.h"
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 | #ifdef GNU_READLINE
|
---|
41 | int rl_bind_key (int, int (*)());
|
---|
42 |
|
---|
43 |
|
---|
44 | void
|
---|
45 | Init_ReadLine (void)
|
---|
46 | {
|
---|
47 | /* Make TAB just insert itself rather than do a file name completion */
|
---|
48 | rl_bind_key (TAB, rl_insert);
|
---|
49 | }
|
---|
50 | #else
|
---|
51 |
|
---|
52 | static FILE *rl_instream = NULL;
|
---|
53 | static FILE *rl_outstream = NULL;
|
---|
54 |
|
---|
55 | void
|
---|
56 | Init_ReadLine (void)
|
---|
57 | {
|
---|
58 | rl_instream = stdin;
|
---|
59 | rl_outstream = stdout;
|
---|
60 | }
|
---|
61 |
|
---|
62 | static char *
|
---|
63 | readline (char *pmt)
|
---|
64 | {
|
---|
65 | static char buf[MAXLINEBUFFERLEN + 1];
|
---|
66 | char *s;
|
---|
67 |
|
---|
68 | fprintf (rl_outstream, "%s", pmt);
|
---|
69 | s = fgets (buf, sizeof (buf), rl_instream);
|
---|
70 | if (s)
|
---|
71 | {
|
---|
72 | char *s1 = strrchr (s, '\n');
|
---|
73 | if (s1 && *(s1 + 1) == '\0')
|
---|
74 | *s1 = '\0';
|
---|
75 | }
|
---|
76 | return s ? Xstrdup (s) : NULL;
|
---|
77 | }
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | /* WritePrompt() */
|
---|
81 | /* Write out a prompt if user is a TTY */
|
---|
82 | void
|
---|
83 | WritePrompt (void)
|
---|
84 | {
|
---|
85 | if (isatty (fileno (InFile)))
|
---|
86 | {
|
---|
87 | if (!BooleanEnv (GetEnv ("expert"), 0))
|
---|
88 | fprintf (stderr, "Enter a command or query (.quit to terminate, .help for assistance).\n");
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | #ifdef GNU_READLINE
|
---|
93 | static void memory_error_and_abort ();
|
---|
94 |
|
---|
95 | char *
|
---|
96 | xmalloc (bytes)
|
---|
97 | int bytes;
|
---|
98 | {
|
---|
99 | char *temp = (char *) Xmalloc (bytes);
|
---|
100 |
|
---|
101 | if (!temp)
|
---|
102 | memory_error_and_abort ();
|
---|
103 | return (temp);
|
---|
104 | }
|
---|
105 |
|
---|
106 | char *
|
---|
107 | xrealloc (pointer, bytes)
|
---|
108 | char *pointer;
|
---|
109 | int bytes;
|
---|
110 | {
|
---|
111 | char *temp;
|
---|
112 |
|
---|
113 | if (!pointer)
|
---|
114 | temp = (char *) xmalloc (bytes);
|
---|
115 | else
|
---|
116 | temp = (char *) Xrealloc (pointer, bytes);
|
---|
117 |
|
---|
118 | if (!temp)
|
---|
119 | memory_error_and_abort ();
|
---|
120 |
|
---|
121 | return (temp);
|
---|
122 | }
|
---|
123 |
|
---|
124 | static void
|
---|
125 | memory_error_and_abort ()
|
---|
126 | {
|
---|
127 | fprintf (stderr, "history: Out of virtual memory!\n");
|
---|
128 | abort ();
|
---|
129 | }
|
---|
130 | #endif
|
---|
131 |
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * This routine returns a pointer to the users entered line
|
---|
135 | *
|
---|
136 | */
|
---|
137 | static char *
|
---|
138 | GetLine (char *pmt)
|
---|
139 | {
|
---|
140 | static char *the_line = NULL;
|
---|
141 | if (the_line)
|
---|
142 | Xfree (the_line);
|
---|
143 | the_line = NULL;
|
---|
144 | rl_instream = InFile;
|
---|
145 | if (isatty (fileno (InFile)))
|
---|
146 | {
|
---|
147 | fputc ('\r', stderr);
|
---|
148 | if (!isatty (fileno (OutFile)))
|
---|
149 | rl_outstream = stderr;
|
---|
150 | else
|
---|
151 | rl_outstream = OutFile;
|
---|
152 | the_line = readline (pmt);
|
---|
153 | }
|
---|
154 | else
|
---|
155 | {
|
---|
156 | if (isatty (fileno (OutFile)))
|
---|
157 | {
|
---|
158 | the_line = readline (pmt);
|
---|
159 | fprintf (stderr, "%s\n", the_line ? the_line : "");
|
---|
160 | }
|
---|
161 | else
|
---|
162 | {
|
---|
163 | the_line = readline ("");
|
---|
164 | if (the_line)
|
---|
165 | fprintf (stderr, "%s%s\n", pmt, the_line);
|
---|
166 | }
|
---|
167 | }
|
---|
168 | #ifdef GNU_READLINE
|
---|
169 | if (the_line && *the_line)
|
---|
170 | add_history (the_line);
|
---|
171 | #endif
|
---|
172 | return (the_line);
|
---|
173 | }
|
---|
174 |
|
---|
175 |
|
---|
176 |
|
---|
177 |
|
---|
178 |
|
---|
179 |
|
---|
180 | char *
|
---|
181 | GetMultiLine (void)
|
---|
182 | {
|
---|
183 | static char *line = NULL;
|
---|
184 | char *s;
|
---|
185 | if (line)
|
---|
186 | Xfree (line);
|
---|
187 | line = NULL;
|
---|
188 | if (!(s = GetLine ("> ")))
|
---|
189 | return (NULL);
|
---|
190 | if (!(line = Xstrdup (s)))
|
---|
191 | {
|
---|
192 | fprintf (stderr, "Unable to allocate memory for the line\n");
|
---|
193 | abort ();
|
---|
194 | }
|
---|
195 |
|
---|
196 | while ((s = strrchr (line, '\\')) && *(s + 1) == '\0')
|
---|
197 | {
|
---|
198 | char *new;
|
---|
199 | *strrchr (line, '\\') = '\0';
|
---|
200 | if (!(s = GetLine ("? ")))
|
---|
201 | return (NULL);
|
---|
202 | if (!(new = Xmalloc (strlen (line) + strlen (s) + 2)))
|
---|
203 | {
|
---|
204 | fprintf (stderr, "Unable to allocate memory for the line\n");
|
---|
205 | abort ();
|
---|
206 | }
|
---|
207 | strcpy (new, line);
|
---|
208 | strcat (new, "\n");
|
---|
209 | strcat (new, s);
|
---|
210 | Xfree (line);
|
---|
211 | line = new;
|
---|
212 | }
|
---|
213 | return (line);
|
---|
214 | }
|
---|