source: trunk/gsdl/packages/mg/src/text/read_line.c@ 1014

Last change on this file since 1014 was 1014, checked in by sjboddie, 24 years ago

newer compilers complain about using stdin and stdout to do global
initialization as they're not constant

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 KB
Line 
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 1014 2000-03-05 22:45:58Z sjboddie $
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
41int rl_bind_key (int, int (*)());
42
43
44void
45Init_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
51void
52Init_ReadLine (void)
53{
54 rl_instream = stdin;
55 rl_outstream = stdout;
56}
57#endif
58
59
60
61
62/* WritePrompt() */
63/* Write out a prompt if user is a TTY */
64void
65WritePrompt (void)
66{
67 if (isatty (fileno (InFile)))
68 {
69 if (!BooleanEnv (GetEnv ("expert"), 0))
70 fprintf (stderr, "Enter a command or query (.quit to terminate, .help for assistance).\n");
71 }
72}
73
74#ifdef GNU_READLINE
75static void memory_error_and_abort ();
76
77char *
78xmalloc (bytes)
79 int bytes;
80{
81 char *temp = (char *) Xmalloc (bytes);
82
83 if (!temp)
84 memory_error_and_abort ();
85 return (temp);
86}
87
88char *
89xrealloc (pointer, bytes)
90 char *pointer;
91 int bytes;
92{
93 char *temp;
94
95 if (!pointer)
96 temp = (char *) xmalloc (bytes);
97 else
98 temp = (char *) Xrealloc (pointer, bytes);
99
100 if (!temp)
101 memory_error_and_abort ();
102
103 return (temp);
104}
105
106static void
107memory_error_and_abort ()
108{
109 fprintf (stderr, "history: Out of virtual memory!\n");
110 abort ();
111}
112#endif
113
114
115#ifndef GNU_READLINE
116
117static FILE *rl_instream = NULL;
118static FILE *rl_outstream = NULL;
119
120static char *
121readline (char *pmt)
122{
123 static char buf[MAXLINEBUFFERLEN + 1];
124 char *s;
125
126 fprintf (rl_outstream, "%s", pmt);
127 s = fgets (buf, sizeof (buf), rl_instream);
128 if (s)
129 {
130 char *s1 = strrchr (s, '\n');
131 if (s1 && *(s1 + 1) == '\0')
132 *s1 = '\0';
133 }
134 return s ? Xstrdup (s) : NULL;
135}
136#endif
137
138/*
139 * This routine returns a pointer to the users entered line
140 *
141 */
142static char *
143GetLine (char *pmt)
144{
145 static char *the_line = NULL;
146 if (the_line)
147 Xfree (the_line);
148 the_line = NULL;
149 rl_instream = InFile;
150 if (isatty (fileno (InFile)))
151 {
152 fputc ('\r', stderr);
153 if (!isatty (fileno (OutFile)))
154 rl_outstream = stderr;
155 else
156 rl_outstream = OutFile;
157 the_line = readline (pmt);
158 }
159 else
160 {
161 if (isatty (fileno (OutFile)))
162 {
163 the_line = readline (pmt);
164 fprintf (stderr, "%s\n", the_line ? the_line : "");
165 }
166 else
167 {
168 the_line = readline ("");
169 if (the_line)
170 fprintf (stderr, "%s%s\n", pmt, the_line);
171 }
172 }
173#ifdef GNU_READLINE
174 if (the_line && *the_line)
175 add_history (the_line);
176#endif
177 return (the_line);
178}
179
180
181
182
183
184
185char *
186GetMultiLine (void)
187{
188 static char *line = NULL;
189 char *s;
190 if (line)
191 Xfree (line);
192 line = NULL;
193 if (!(s = GetLine ("> ")))
194 return (NULL);
195 if (!(line = Xstrdup (s)))
196 {
197 fprintf (stderr, "Unable to allocate memory for the line\n");
198 abort ();
199 }
200
201 while ((s = strrchr (line, '\\')) && *(s + 1) == '\0')
202 {
203 char *new;
204 *strrchr (line, '\\') = '\0';
205 if (!(s = GetLine ("? ")))
206 return (NULL);
207 if (!(new = Xmalloc (strlen (line) + strlen (s) + 2)))
208 {
209 fprintf (stderr, "Unable to allocate memory for the line\n");
210 abort ();
211 }
212 strcpy (new, line);
213 strcat (new, "\n");
214 strcat (new, s);
215 Xfree (line);
216 line = new;
217 }
218 return (line);
219}
Note: See TracBrowser for help on using the repository browser.