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