1
|
1 // input.cc -*- C++ -*- |
|
2 /* |
|
3 |
269
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
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" |
269
|
40 #include "readline/history.h" |
1
|
41 |
252
|
42 extern char *xmalloc (); |
|
43 |
1
|
44 /* |
|
45 * Yes, this sucks, but it avoids a conflict with another readline |
|
46 * function declared in iostream.h. |
|
47 */ |
|
48 #if 0 |
|
49 #define LINE_SIZE 8192 |
|
50 static int no_line_editing = 1; |
|
51 #endif |
|
52 |
|
53 char * |
|
54 gnu_readline (char *s) |
|
55 { |
|
56 #if 0 |
|
57 static int state = 0; |
|
58 static char *line_from_stdin = (char *) NULL; |
|
59 if (no_line_editing) |
|
60 { |
|
61 if (! state) |
|
62 { |
|
63 line_from_stdin = (char *) malloc (LINE_SIZE); |
|
64 state = 1; |
|
65 } |
|
66 fputs ("octave> ", stdout); |
|
67 fgets (line_from_stdin, LINE_SIZE, stdin); |
|
68 return line_from_stdin; |
|
69 } |
|
70 else |
|
71 #endif |
|
72 return readline (s); |
|
73 } |
|
74 } |
|
75 |
|
76 #include "variables.h" |
|
77 #include "error.h" |
|
78 #include "utils.h" |
|
79 #include "input.h" |
|
80 #include "pager.h" |
|
81 #include "help.h" |
|
82 #include "octave-hist.h" |
|
83 #include "sighandlers.h" |
|
84 #include "parse.h" |
|
85 #include "user-prefs.h" |
|
86 #include "builtins.h" |
|
87 |
|
88 // Global pointer for eval(). |
164
|
89 const char *current_eval_string = (char *) NULL; |
1
|
90 |
|
91 // Nonzero means get input from current_eval_string. |
|
92 int get_input_from_eval_string = 0; |
|
93 |
|
94 // Nonzero means we're parsing an M-file. |
|
95 int reading_m_file = 0; |
|
96 |
|
97 // Simple name of M-file we are reading. |
|
98 char *curr_m_file_name = (char *) NULL; |
|
99 |
|
100 // Nonzero means we're parsing a script file. |
|
101 int reading_script_file = 0; |
|
102 |
|
103 // If we are reading from an M-file, this is it. |
|
104 FILE *mf_instream = (FILE *) NULL; |
|
105 |
|
106 // Nonzero means we are using readline. |
|
107 int using_readline = 1; |
|
108 |
|
109 // Nonzero means commands are echoed as they are executed (-x). |
|
110 int echo_input = 0; |
|
111 |
|
112 // Nonzero means this is an interactive shell. |
|
113 int interactive = 0; |
|
114 |
|
115 // Nonzero means the user forced this shell to be interactive (-i). |
|
116 int forced_interactive = 0; |
|
117 |
|
118 // Should we issue a prompt? |
|
119 int promptflag = 1; |
|
120 |
|
121 // The current line of input, from wherever. |
|
122 char *current_input_line = (char *) NULL; |
|
123 |
|
124 // A line of input from readline. |
|
125 static char *octave_gets_line = (char *) NULL; |
|
126 |
|
127 /* |
|
128 * Use GNU readline to get an input line and store it in the history |
|
129 * list. |
|
130 */ |
|
131 char * |
|
132 octave_gets (void) |
|
133 { |
|
134 if (octave_gets_line != NULL) |
126
|
135 { |
|
136 free (octave_gets_line); |
|
137 octave_gets_line = (char *) NULL; |
|
138 } |
1
|
139 |
|
140 if (interactive || forced_interactive) |
|
141 { |
|
142 char *ps = (promptflag > 0) ? user_pref.ps1 : user_pref.ps2; |
|
143 char *prompt = decode_prompt_string (ps); |
|
144 |
|
145 if (interactive) |
|
146 { |
|
147 pipe_handler_error_count = 0; |
|
148 flush_output_to_pager (); |
|
149 } |
|
150 |
|
151 octave_gets_line = gnu_readline (prompt); |
|
152 delete [] prompt; |
|
153 } |
|
154 else |
|
155 octave_gets_line = gnu_readline (""); |
|
156 |
|
157 current_input_line = octave_gets_line; |
|
158 |
|
159 if (octave_gets_line && *octave_gets_line) |
|
160 { |
|
161 maybe_save_history (octave_gets_line); |
|
162 |
|
163 if (echo_input) |
|
164 { |
|
165 if (!forced_interactive) |
|
166 cout << "+ "; |
|
167 if (octave_gets_line != (char *) NULL) |
|
168 cout << octave_gets_line << "\n"; |
|
169 } |
|
170 } |
|
171 return octave_gets_line; |
|
172 } |
|
173 |
|
174 /* |
|
175 * Read a line from the input stream. |
|
176 */ |
269
|
177 int |
1
|
178 octave_read (char *buf, int max_size) |
|
179 { |
|
180 int status = 0; |
|
181 |
|
182 static char *stashed_line = (char *) NULL; |
|
183 |
|
184 if (get_input_from_eval_string) |
|
185 { |
|
186 int len = strlen (current_eval_string); |
|
187 if (len < max_size - 1) |
|
188 { |
|
189 strcpy (buf, current_eval_string); |
|
190 buf[len++] = '\n'; |
|
191 buf[len] = '\0'; // Paranoia. |
|
192 status = len; |
|
193 } |
|
194 else |
|
195 status = -1; |
|
196 |
|
197 if (stashed_line) |
|
198 delete [] stashed_line; |
|
199 |
|
200 stashed_line = strsave (buf); |
|
201 current_input_line = stashed_line; |
|
202 } |
|
203 else if (using_readline) |
|
204 { |
|
205 char *cp = octave_gets (); |
|
206 if (cp != (char *) NULL) |
|
207 { |
|
208 int len = strlen (cp); |
|
209 if (len >= max_size) |
|
210 status = -1; |
|
211 else |
|
212 { |
|
213 strcpy (buf, cp); |
|
214 buf[len++] = '\n'; |
|
215 buf[len] = '\0'; // Paranoia. |
|
216 status = len; |
|
217 } |
|
218 } |
|
219 current_input_line = cp; |
|
220 } |
|
221 else |
|
222 { |
|
223 FILE *curr_stream = rl_instream; |
|
224 if (reading_m_file || reading_script_file) |
|
225 curr_stream = mf_instream; |
|
226 |
|
227 assert (curr_stream != (FILE *) NULL); |
|
228 |
|
229 // Why is this required? |
|
230 buf[0] = '\0'; |
|
231 |
|
232 if (fgets (buf, max_size, curr_stream) != (char *) NULL) |
|
233 { |
|
234 int len = strlen (buf); |
|
235 if (len > max_size - 2) |
|
236 status = -1; |
|
237 else |
|
238 { |
|
239 if (buf[len-1] != '\n') |
|
240 { |
|
241 buf[len++] = '\n'; |
|
242 buf[len] = '\0'; |
|
243 } |
|
244 status = len; |
|
245 } |
|
246 } |
|
247 else |
|
248 status = 0; // Tell yylex that we found EOF. |
|
249 |
|
250 if (stashed_line) |
|
251 delete [] stashed_line; |
|
252 |
|
253 stashed_line = strsave (buf); |
|
254 current_input_line = stashed_line; |
|
255 } |
|
256 input_line_number++; |
|
257 return status; |
|
258 } |
|
259 |
|
260 /* |
|
261 * Fix things up so that input can come from file `name', printing a |
|
262 * warning if the file doesn't exist. |
|
263 */ |
|
264 FILE * |
|
265 get_input_from_file (char *name, int warn = 1) |
|
266 { |
|
267 FILE *instream = (FILE *) NULL; |
|
268 |
|
269 if (name && *name) |
|
270 instream = fopen (name, "r"); |
|
271 |
|
272 if (instream == (FILE *) NULL && warn) |
217
|
273 warning ("%s: no such file or directory", name); |
1
|
274 |
|
275 if (reading_m_file || reading_script_file) |
|
276 mf_instream = instream; |
|
277 else |
|
278 rl_instream = instream; |
|
279 |
|
280 return instream; |
|
281 } |
|
282 |
|
283 /* |
|
284 * Fix things up so that input can come from the standard input. This |
|
285 * may need to become much more complicated, which is why it's in a |
|
286 * separate function. |
|
287 */ |
|
288 FILE * |
|
289 get_input_from_stdin (void) |
|
290 { |
|
291 rl_instream = stdin; |
|
292 return rl_instream; |
|
293 } |
|
294 |
269
|
295 static char * |
1
|
296 command_generator (char *text, int state) |
|
297 { |
|
298 static int len = 0; |
|
299 static int list_index = 0; |
|
300 |
|
301 static char **name_list = (char **) NULL; |
|
302 |
|
303 if (state == 0) |
|
304 { |
|
305 list_index = 0; |
|
306 len = strlen (text); |
|
307 |
|
308 if (name_list != (char **) NULL) |
244
|
309 { |
|
310 char **ptr = name_list; |
|
311 while (ptr && *ptr) |
|
312 delete [] *ptr++; |
|
313 delete [] name_list; |
|
314 } |
1
|
315 |
|
316 name_list = make_name_list (); |
|
317 } |
|
318 |
|
319 char *name; |
|
320 while ((name = name_list[list_index]) != (char *) NULL) |
|
321 { |
|
322 list_index++; |
|
323 if (strncmp (name, text, len) == 0) |
244
|
324 { |
|
325 char *buf = xmalloc (1 + strlen (name)); |
|
326 strcpy (buf, name); |
|
327 return buf; |
|
328 } |
1
|
329 } |
|
330 |
|
331 return (char *) NULL; |
|
332 } |
|
333 |
269
|
334 static char ** |
1
|
335 command_completer (char *text, int start, int end) |
|
336 { |
|
337 char **matches = (char **) NULL; |
|
338 matches = completion_matches (text, command_generator); |
|
339 return matches; |
|
340 } |
|
341 |
269
|
342 /* |
|
343 * The next two functions implement the equivalent of the K*rn shell |
|
344 * C-o operate-and-get-next-history-line editing command. Stolen from |
|
345 * the GNU Bourne Again SHell. |
|
346 */ |
|
347 |
|
348 // ?? |
|
349 static int saved_history_line_to_use = 0; |
|
350 |
|
351 // ?? |
|
352 static Function *old_rl_startup_hook = (Function *) NULL; |
|
353 |
|
354 static void |
|
355 set_saved_history (void) |
|
356 { |
|
357 HIST_ENTRY *h; |
|
358 |
|
359 if (saved_history_line_to_use) |
|
360 { |
|
361 if (history_set_pos (saved_history_line_to_use)) |
|
362 { |
|
363 h = current_history (); |
|
364 if (h) |
|
365 { |
|
366 rl_insert_text (h->line); |
|
367 |
|
368 // Get rid of any undo list created by the previous insert, so the |
|
369 // line won't totally be erased when the edits are undone (they will |
|
370 // be normally, because this is a history line -- cf. readline.c: |
|
371 // line 380 or so). |
|
372 if (rl_undo_list) |
|
373 { |
|
374 free_undo_list (); |
|
375 rl_undo_list = (UNDO_LIST *) NULL; |
|
376 } |
|
377 } |
|
378 } |
|
379 } |
|
380 saved_history_line_to_use = 0; |
|
381 rl_startup_hook = old_rl_startup_hook; |
|
382 } |
|
383 |
|
384 static void |
|
385 operate_and_get_next (int count, int c) |
|
386 { |
|
387 int where; |
|
388 extern int history_stifled, history_length, max_input_history; |
|
389 |
|
390 /* Accept the current line. */ |
|
391 rl_newline (); |
|
392 |
|
393 /* Find the current line, and find the next line to use. */ |
|
394 where = where_history (); |
|
395 |
|
396 if (history_stifled && (history_length >= max_input_history)) |
|
397 saved_history_line_to_use = where; |
|
398 else |
|
399 saved_history_line_to_use = where + 1; |
|
400 |
|
401 old_rl_startup_hook = rl_startup_hook; |
|
402 rl_startup_hook = (Function *) set_saved_history; |
|
403 } |
|
404 |
1
|
405 void |
|
406 initialize_readline (void) |
|
407 { |
|
408 // Allow conditional parsing of the ~/.inputrc file |
|
409 rl_readline_name = "Octave"; |
|
410 |
|
411 // Tell the completer that we want to try first. |
|
412 rl_attempted_completion_function = (Function *) command_completer; |
269
|
413 |
|
414 // Bind operate-and-get-next. |
|
415 rl_add_defun ("operate-and-get-next", |
|
416 (Function *) operate_and_get_next, CTRL ('O')); |
1
|
417 } |
|
418 |
|
419 /* |
|
420 ;;; Local Variables: *** |
|
421 ;;; mode: C++ *** |
|
422 ;;; page-delimiter: "^/\\*" *** |
|
423 ;;; End: *** |
|
424 */ |