1
|
1 // input.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Use the GNU readline library for command line editing and hisory. |
|
25 |
240
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include "config.h" |
1
|
28 #endif |
|
29 |
|
30 #include <stdio.h> |
|
31 #include <stdlib.h> |
195
|
32 #include <iostream.h> |
188
|
33 #include <string.h> |
1
|
34 #include <assert.h> |
|
35 |
|
36 // This must come before anything that includes iostream.h... |
|
37 extern "C" |
|
38 { |
|
39 #include "readline/readline.h" |
|
40 |
|
41 /* |
|
42 * Yes, this sucks, but it avoids a conflict with another readline |
|
43 * function declared in iostream.h. |
|
44 */ |
|
45 #if 0 |
|
46 #define LINE_SIZE 8192 |
|
47 static int no_line_editing = 1; |
|
48 #endif |
|
49 |
|
50 char * |
|
51 gnu_readline (char *s) |
|
52 { |
|
53 #if 0 |
|
54 static int state = 0; |
|
55 static char *line_from_stdin = (char *) NULL; |
|
56 if (no_line_editing) |
|
57 { |
|
58 if (! state) |
|
59 { |
|
60 line_from_stdin = (char *) malloc (LINE_SIZE); |
|
61 state = 1; |
|
62 } |
|
63 fputs ("octave> ", stdout); |
|
64 fgets (line_from_stdin, LINE_SIZE, stdin); |
|
65 return line_from_stdin; |
|
66 } |
|
67 else |
|
68 #endif |
|
69 return readline (s); |
|
70 } |
|
71 } |
|
72 |
|
73 #include "variables.h" |
|
74 #include "error.h" |
|
75 #include "utils.h" |
|
76 #include "input.h" |
|
77 #include "pager.h" |
|
78 #include "help.h" |
|
79 #include "octave-hist.h" |
|
80 #include "sighandlers.h" |
|
81 #include "parse.h" |
|
82 #include "user-prefs.h" |
|
83 #include "builtins.h" |
|
84 |
|
85 // Global pointer for eval(). |
164
|
86 const char *current_eval_string = (char *) NULL; |
1
|
87 |
|
88 // Nonzero means get input from current_eval_string. |
|
89 int get_input_from_eval_string = 0; |
|
90 |
|
91 // Nonzero means we're parsing an M-file. |
|
92 int reading_m_file = 0; |
|
93 |
|
94 // Simple name of M-file we are reading. |
|
95 char *curr_m_file_name = (char *) NULL; |
|
96 |
|
97 // Nonzero means we're parsing a script file. |
|
98 int reading_script_file = 0; |
|
99 |
|
100 // If we are reading from an M-file, this is it. |
|
101 FILE *mf_instream = (FILE *) NULL; |
|
102 |
|
103 // Nonzero means we are using readline. |
|
104 int using_readline = 1; |
|
105 |
|
106 // Nonzero means commands are echoed as they are executed (-x). |
|
107 int echo_input = 0; |
|
108 |
|
109 // Nonzero means this is an interactive shell. |
|
110 int interactive = 0; |
|
111 |
|
112 // Nonzero means the user forced this shell to be interactive (-i). |
|
113 int forced_interactive = 0; |
|
114 |
|
115 // Should we issue a prompt? |
|
116 int promptflag = 1; |
|
117 |
|
118 // The current line of input, from wherever. |
|
119 char *current_input_line = (char *) NULL; |
|
120 |
|
121 // A line of input from readline. |
|
122 static char *octave_gets_line = (char *) NULL; |
|
123 |
|
124 /* |
|
125 * Use GNU readline to get an input line and store it in the history |
|
126 * list. |
|
127 */ |
|
128 char * |
|
129 octave_gets (void) |
|
130 { |
|
131 if (octave_gets_line != NULL) |
126
|
132 { |
|
133 free (octave_gets_line); |
|
134 octave_gets_line = (char *) NULL; |
|
135 } |
1
|
136 |
|
137 if (interactive || forced_interactive) |
|
138 { |
|
139 char *ps = (promptflag > 0) ? user_pref.ps1 : user_pref.ps2; |
|
140 char *prompt = decode_prompt_string (ps); |
|
141 |
|
142 if (interactive) |
|
143 { |
|
144 pipe_handler_error_count = 0; |
|
145 flush_output_to_pager (); |
|
146 } |
|
147 |
|
148 octave_gets_line = gnu_readline (prompt); |
|
149 delete [] prompt; |
|
150 } |
|
151 else |
|
152 octave_gets_line = gnu_readline (""); |
|
153 |
|
154 current_input_line = octave_gets_line; |
|
155 |
|
156 if (octave_gets_line && *octave_gets_line) |
|
157 { |
|
158 maybe_save_history (octave_gets_line); |
|
159 |
|
160 if (echo_input) |
|
161 { |
|
162 if (!forced_interactive) |
|
163 cout << "+ "; |
|
164 if (octave_gets_line != (char *) NULL) |
|
165 cout << octave_gets_line << "\n"; |
|
166 } |
|
167 } |
|
168 return octave_gets_line; |
|
169 } |
|
170 |
|
171 /* |
|
172 * Read a line from the input stream. |
|
173 */ |
|
174 int |
|
175 octave_read (char *buf, int max_size) |
|
176 { |
|
177 int status = 0; |
|
178 |
|
179 static char *stashed_line = (char *) NULL; |
|
180 |
|
181 if (get_input_from_eval_string) |
|
182 { |
|
183 int len = strlen (current_eval_string); |
|
184 if (len < max_size - 1) |
|
185 { |
|
186 strcpy (buf, current_eval_string); |
|
187 buf[len++] = '\n'; |
|
188 buf[len] = '\0'; // Paranoia. |
|
189 status = len; |
|
190 } |
|
191 else |
|
192 status = -1; |
|
193 |
|
194 if (stashed_line) |
|
195 delete [] stashed_line; |
|
196 |
|
197 stashed_line = strsave (buf); |
|
198 current_input_line = stashed_line; |
|
199 } |
|
200 else if (using_readline) |
|
201 { |
|
202 char *cp = octave_gets (); |
|
203 if (cp != (char *) NULL) |
|
204 { |
|
205 int len = strlen (cp); |
|
206 if (len >= max_size) |
|
207 status = -1; |
|
208 else |
|
209 { |
|
210 strcpy (buf, cp); |
|
211 buf[len++] = '\n'; |
|
212 buf[len] = '\0'; // Paranoia. |
|
213 status = len; |
|
214 } |
|
215 } |
|
216 current_input_line = cp; |
|
217 } |
|
218 else |
|
219 { |
|
220 FILE *curr_stream = rl_instream; |
|
221 if (reading_m_file || reading_script_file) |
|
222 curr_stream = mf_instream; |
|
223 |
|
224 assert (curr_stream != (FILE *) NULL); |
|
225 |
|
226 // Why is this required? |
|
227 buf[0] = '\0'; |
|
228 |
|
229 if (fgets (buf, max_size, curr_stream) != (char *) NULL) |
|
230 { |
|
231 int len = strlen (buf); |
|
232 if (len > max_size - 2) |
|
233 status = -1; |
|
234 else |
|
235 { |
|
236 if (buf[len-1] != '\n') |
|
237 { |
|
238 buf[len++] = '\n'; |
|
239 buf[len] = '\0'; |
|
240 } |
|
241 status = len; |
|
242 } |
|
243 } |
|
244 else |
|
245 status = 0; // Tell yylex that we found EOF. |
|
246 |
|
247 if (stashed_line) |
|
248 delete [] stashed_line; |
|
249 |
|
250 stashed_line = strsave (buf); |
|
251 current_input_line = stashed_line; |
|
252 } |
|
253 input_line_number++; |
|
254 return status; |
|
255 } |
|
256 |
|
257 /* |
|
258 * Fix things up so that input can come from file `name', printing a |
|
259 * warning if the file doesn't exist. |
|
260 */ |
|
261 FILE * |
|
262 get_input_from_file (char *name, int warn = 1) |
|
263 { |
|
264 FILE *instream = (FILE *) NULL; |
|
265 |
|
266 if (name && *name) |
|
267 instream = fopen (name, "r"); |
|
268 |
|
269 if (instream == (FILE *) NULL && warn) |
217
|
270 warning ("%s: no such file or directory", name); |
1
|
271 |
|
272 if (reading_m_file || reading_script_file) |
|
273 mf_instream = instream; |
|
274 else |
|
275 rl_instream = instream; |
|
276 |
|
277 return instream; |
|
278 } |
|
279 |
|
280 /* |
|
281 * Fix things up so that input can come from the standard input. This |
|
282 * may need to become much more complicated, which is why it's in a |
|
283 * separate function. |
|
284 */ |
|
285 FILE * |
|
286 get_input_from_stdin (void) |
|
287 { |
|
288 rl_instream = stdin; |
|
289 return rl_instream; |
|
290 } |
|
291 |
|
292 char * |
|
293 command_generator (char *text, int state) |
|
294 { |
|
295 static int len = 0; |
|
296 static int list_index = 0; |
|
297 |
|
298 static char **name_list = (char **) NULL; |
|
299 |
|
300 if (state == 0) |
|
301 { |
|
302 list_index = 0; |
|
303 len = strlen (text); |
|
304 |
|
305 if (name_list != (char **) NULL) |
244
|
306 { |
|
307 char **ptr = name_list; |
|
308 while (ptr && *ptr) |
|
309 delete [] *ptr++; |
|
310 delete [] name_list; |
|
311 } |
1
|
312 |
|
313 name_list = make_name_list (); |
|
314 } |
|
315 |
|
316 char *name; |
|
317 while ((name = name_list[list_index]) != (char *) NULL) |
|
318 { |
|
319 list_index++; |
|
320 if (strncmp (name, text, len) == 0) |
244
|
321 { |
|
322 char *buf = xmalloc (1 + strlen (name)); |
|
323 strcpy (buf, name); |
|
324 return buf; |
|
325 } |
1
|
326 } |
|
327 |
|
328 return (char *) NULL; |
|
329 } |
|
330 |
|
331 char ** |
|
332 command_completer (char *text, int start, int end) |
|
333 { |
|
334 char **matches = (char **) NULL; |
|
335 matches = completion_matches (text, command_generator); |
|
336 return matches; |
|
337 } |
|
338 |
|
339 void |
|
340 initialize_readline (void) |
|
341 { |
|
342 // Allow conditional parsing of the ~/.inputrc file |
|
343 rl_readline_name = "Octave"; |
|
344 |
|
345 // Tell the completer that we want to try first. |
|
346 rl_attempted_completion_function = (Function *) command_completer; |
|
347 } |
|
348 |
|
349 /* |
|
350 ;;; Local Variables: *** |
|
351 ;;; mode: C++ *** |
|
352 ;;; page-delimiter: "^/\\*" *** |
|
353 ;;; End: *** |
|
354 */ |