2926
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
3225
|
27 #include <cstdlib> |
2926
|
28 #include <cstring> |
|
29 |
|
30 #include <string> |
|
31 |
|
32 #ifdef HAVE_UNISTD_H |
|
33 #ifdef HAVE_SYS_TYPES_H |
|
34 #include <sys/types.h> |
|
35 #endif |
|
36 #include <unistd.h> |
|
37 #endif |
|
38 |
|
39 #include "cmd-edit.h" |
|
40 #include "cmd-hist.h" |
|
41 #include "lo-error.h" |
|
42 #include "lo-utils.h" |
|
43 #include "oct-env.h" |
3260
|
44 #include "oct-time.h" |
2926
|
45 |
|
46 command_editor *command_editor::instance = 0; |
|
47 |
|
48 #if defined (USE_READLINE) |
|
49 |
|
50 #include <cstdio> |
|
51 #include <cstdlib> |
|
52 |
3519
|
53 #include "oct-rl-edit.h" |
2926
|
54 |
|
55 class |
|
56 gnu_readline : public command_editor |
|
57 { |
|
58 public: |
|
59 |
3519
|
60 typedef command_editor::startup_hook_fcn startup_hook_fcn; |
|
61 |
|
62 typedef command_editor::event_hook_fcn event_hook_hook_fcn; |
2926
|
63 |
2941
|
64 typedef command_editor::completion_fcn completion_fcn; |
|
65 |
2926
|
66 gnu_readline (void); |
|
67 |
|
68 ~gnu_readline (void) { } |
|
69 |
3504
|
70 void do_set_name (const std::string& n); |
2926
|
71 |
3504
|
72 std::string do_readline (const std::string& prompt, bool& eof); |
2926
|
73 |
|
74 void do_set_input_stream (FILE *f); |
|
75 |
|
76 FILE *do_get_input_stream (void); |
|
77 |
|
78 void do_set_output_stream (FILE *f); |
|
79 |
|
80 FILE *do_get_output_stream (void); |
|
81 |
|
82 int do_terminal_rows (void); |
|
83 |
|
84 int do_terminal_cols (void); |
|
85 |
|
86 void do_clear_screen (void); |
|
87 |
3281
|
88 void do_resize_terminal (void); |
|
89 |
3504
|
90 std::string newline_chars (void); |
2926
|
91 |
|
92 void do_restore_terminal_state (void); |
|
93 |
|
94 void do_blink_matching_paren (bool flag); |
|
95 |
3933
|
96 void do_set_basic_word_break_characters (const std::string& s); |
|
97 |
|
98 void do_set_completer_word_break_characters (const std::string& s); |
|
99 |
3504
|
100 void do_set_basic_quote_characters (const std::string& s); |
2926
|
101 |
|
102 void do_set_completion_append_character (char c); |
|
103 |
2941
|
104 void do_set_completion_function (completion_fcn f); |
|
105 |
|
106 completion_fcn do_get_completion_function (void) const; |
2926
|
107 |
3504
|
108 void do_insert_text (const std::string& text); |
2926
|
109 |
|
110 void do_newline (void); |
|
111 |
|
112 void do_clear_undo_list (void); |
|
113 |
3519
|
114 void do_set_startup_hook (startup_hook_fcn f); |
2926
|
115 |
|
116 void do_restore_startup_hook (void); |
|
117 |
3519
|
118 void do_set_event_hook (event_hook_fcn f); |
3215
|
119 |
|
120 void do_restore_event_hook (void); |
|
121 |
3504
|
122 void do_read_init_file (const std::string& file); |
3189
|
123 |
3519
|
124 static int operate_and_get_next (int, int); |
2926
|
125 |
3951
|
126 static int history_search_backward (int, int); |
|
127 |
|
128 static int history_search_forward (int, int); |
|
129 |
2926
|
130 private: |
|
131 |
3519
|
132 startup_hook_fcn previous_startup_hook; |
2926
|
133 |
3519
|
134 event_hook_fcn previous_event_hook; |
3215
|
135 |
2941
|
136 completion_fcn completion_function; |
|
137 |
|
138 static char *command_generator (const char *text, int state); |
|
139 |
3519
|
140 static char **command_completer (const char *text, int start, int end); |
2926
|
141 }; |
|
142 |
|
143 gnu_readline::gnu_readline () |
3215
|
144 : command_editor (), previous_startup_hook (0), |
|
145 previous_event_hook (0), completion_function (0) |
2926
|
146 { |
3519
|
147 // XXX FIXME XXX -- need interface to rl_add_defun, rl_initialize, and |
|
148 // a function to set rl_terminal_name |
3225
|
149 |
3520
|
150 std::string term = octave_env::getenv ("TERM"); |
3519
|
151 |
|
152 octave_rl_set_terminal_name (term.c_str ()); |
|
153 |
|
154 octave_rl_initialize (); |
2926
|
155 |
|
156 do_blink_matching_paren (true); |
|
157 |
3519
|
158 /* Bind operate-and-get-next. */ |
2926
|
159 |
3519
|
160 octave_rl_add_defun ("operate-and-get-next", |
|
161 gnu_readline::operate_and_get_next, |
|
162 octave_rl_ctrl ('O')); |
2926
|
163 |
3519
|
164 /* And the history search functions. */ |
2926
|
165 |
3519
|
166 octave_rl_add_defun ("history-search-backward", |
3951
|
167 gnu_readline::history_search_backward, |
3519
|
168 octave_rl_meta ('P')); |
2926
|
169 |
3519
|
170 octave_rl_add_defun ("history-search-forward", |
3951
|
171 gnu_readline::history_search_forward, |
3519
|
172 octave_rl_meta ('N')); |
2926
|
173 } |
|
174 |
3519
|
175 |
|
176 |
2926
|
177 void |
3519
|
178 gnu_readline::do_set_name (const std::string& nm) |
2926
|
179 { |
3519
|
180 ::octave_rl_set_name (nm.c_str ()); |
2926
|
181 } |
|
182 |
3504
|
183 std::string |
|
184 gnu_readline::do_readline (const std::string& prompt, bool& eof) |
2926
|
185 { |
3504
|
186 std::string retval; |
2926
|
187 |
3219
|
188 eof = false; |
|
189 |
3519
|
190 char *line = ::octave_rl_readline (prompt.c_str ()); |
2926
|
191 |
|
192 if (line) |
|
193 { |
|
194 retval = line; |
|
195 |
|
196 free (line); |
|
197 } |
3219
|
198 else |
|
199 eof = true; |
2926
|
200 |
|
201 return retval; |
|
202 } |
|
203 |
|
204 void |
|
205 gnu_readline::do_set_input_stream (FILE *f) |
|
206 { |
3519
|
207 ::octave_rl_set_input_stream (f); |
2926
|
208 } |
|
209 |
|
210 FILE * |
|
211 gnu_readline::do_get_input_stream (void) |
|
212 { |
3519
|
213 return ::octave_rl_get_input_stream (); |
2926
|
214 } |
|
215 |
|
216 void |
|
217 gnu_readline::do_set_output_stream (FILE *f) |
|
218 { |
3519
|
219 ::octave_rl_set_output_stream (f); |
2926
|
220 } |
|
221 |
|
222 FILE * |
|
223 gnu_readline::do_get_output_stream (void) |
|
224 { |
3519
|
225 return ::octave_rl_get_output_stream (); |
2926
|
226 } |
|
227 |
|
228 // GNU readline handles SIGWINCH, so these values have a good chance |
|
229 // of being correct even if the window changes size (they may be |
|
230 // wrong if, for example, the luser changes the window size while the |
|
231 // pager is running, and the signal is handled by the pager instead of |
|
232 // us. |
|
233 |
|
234 int |
|
235 gnu_readline::do_terminal_rows (void) |
|
236 { |
3519
|
237 int sh = ::octave_rl_screen_height (); |
|
238 |
|
239 return sh > 0 ? sh : 24; |
2926
|
240 } |
|
241 |
|
242 int |
|
243 gnu_readline::do_terminal_cols (void) |
|
244 { |
3519
|
245 int sw = ::octave_rl_screen_width (); |
|
246 |
|
247 return sw > 0 ? sw : 80; |
2926
|
248 } |
|
249 |
|
250 void |
|
251 gnu_readline::do_clear_screen (void) |
|
252 { |
3519
|
253 ::octave_rl_clear_screen (); |
2926
|
254 } |
|
255 |
3281
|
256 void |
|
257 gnu_readline::do_resize_terminal (void) |
|
258 { |
3519
|
259 ::octave_rl_resize_terminal (); |
3281
|
260 } |
|
261 |
3504
|
262 std::string |
2926
|
263 gnu_readline::newline_chars (void) |
|
264 { |
|
265 return "\r\n"; |
|
266 } |
|
267 |
|
268 void |
|
269 gnu_readline::do_restore_terminal_state (void) |
|
270 { |
3519
|
271 ::octave_rl_restore_terminal_state (); |
2926
|
272 } |
|
273 |
|
274 void |
|
275 gnu_readline::do_blink_matching_paren (bool flag) |
|
276 { |
3779
|
277 ::octave_rl_enable_paren_matching (flag ? 1 : 0); |
2926
|
278 } |
|
279 |
|
280 void |
3933
|
281 gnu_readline::do_set_basic_word_break_characters (const std::string& s) |
|
282 { |
|
283 ::octave_rl_set_basic_word_break_characters (s.c_str ()); |
|
284 } |
|
285 |
|
286 void |
|
287 gnu_readline::do_set_completer_word_break_characters (const std::string& s) |
|
288 { |
|
289 ::octave_rl_set_completer_word_break_characters (s.c_str ()); |
|
290 } |
|
291 |
|
292 void |
3504
|
293 gnu_readline::do_set_basic_quote_characters (const std::string& s) |
2926
|
294 { |
3519
|
295 ::octave_rl_set_basic_quote_characters (s.c_str ()); |
2926
|
296 } |
|
297 |
|
298 void |
|
299 gnu_readline::do_set_completion_append_character (char c) |
|
300 { |
3519
|
301 ::octave_rl_set_completion_append_character (c); |
2926
|
302 } |
|
303 |
|
304 void |
2941
|
305 gnu_readline::do_set_completion_function (completion_fcn f) |
2926
|
306 { |
2941
|
307 completion_function = f; |
|
308 |
3519
|
309 rl_attempted_completion_fcn_ptr fp |
|
310 = f ? gnu_readline::command_completer : 0; |
2941
|
311 |
3519
|
312 ::octave_rl_set_completion_function (fp); |
2941
|
313 } |
|
314 |
|
315 gnu_readline::completion_fcn |
|
316 gnu_readline::do_get_completion_function (void) const |
|
317 { |
|
318 return completion_function; |
2926
|
319 } |
|
320 |
|
321 void |
3504
|
322 gnu_readline::do_insert_text (const std::string& text) |
2926
|
323 { |
3519
|
324 ::octave_rl_insert_text (text.c_str ()); |
2926
|
325 } |
|
326 |
|
327 void |
|
328 gnu_readline::do_newline (void) |
|
329 { |
3519
|
330 ::octave_rl_newline (); |
2926
|
331 } |
|
332 |
|
333 void |
|
334 gnu_readline::do_clear_undo_list () |
|
335 { |
3519
|
336 ::octave_rl_clear_undo_list (); |
2926
|
337 } |
|
338 |
|
339 void |
3519
|
340 gnu_readline::do_set_startup_hook (startup_hook_fcn f) |
2926
|
341 { |
3519
|
342 previous_startup_hook = ::octave_rl_get_startup_hook (); |
2926
|
343 |
3519
|
344 ::octave_rl_set_startup_hook (f); |
2926
|
345 } |
|
346 |
|
347 void |
|
348 gnu_readline::do_restore_startup_hook (void) |
|
349 { |
3519
|
350 ::octave_rl_set_startup_hook (previous_startup_hook); |
2926
|
351 } |
|
352 |
|
353 void |
3519
|
354 gnu_readline::do_set_event_hook (event_hook_fcn f) |
3215
|
355 { |
3519
|
356 previous_event_hook = octave_rl_get_event_hook (); |
3215
|
357 |
3519
|
358 ::octave_rl_set_event_hook (f); |
3215
|
359 } |
|
360 |
|
361 void |
|
362 gnu_readline::do_restore_event_hook (void) |
|
363 { |
3519
|
364 ::octave_rl_set_event_hook (previous_event_hook); |
3215
|
365 } |
|
366 |
|
367 void |
3504
|
368 gnu_readline::do_read_init_file (const std::string& file) |
3189
|
369 { |
3519
|
370 ::octave_rl_read_init_file (file.c_str ()); |
3189
|
371 } |
|
372 |
3519
|
373 int |
2926
|
374 gnu_readline::operate_and_get_next (int /* count */, int /* c */) |
|
375 { |
|
376 // Accept the current line. |
|
377 |
|
378 command_editor::newline (); |
|
379 |
|
380 // Find the current line, and find the next line to use. |
|
381 |
|
382 int x_where = command_history::where (); |
|
383 |
|
384 int x_length = command_history::length (); |
|
385 |
|
386 if ((command_history::is_stifled () |
|
387 && (x_length >= command_history::max_input_history ())) |
|
388 || (x_where >= x_length - 1)) |
|
389 command_history::set_mark (x_where); |
|
390 else |
|
391 command_history::set_mark (x_where + 1); |
|
392 |
|
393 command_editor::set_startup_hook (command_history::goto_mark); |
3519
|
394 |
|
395 return 0; |
2926
|
396 } |
|
397 |
3951
|
398 int |
|
399 gnu_readline::history_search_backward (int count, int c) |
|
400 { |
|
401 return octave_rl_history_search_backward (count, c); |
|
402 } |
|
403 |
|
404 int |
|
405 gnu_readline::history_search_forward (int count, int c) |
|
406 { |
|
407 return octave_rl_history_search_forward (count, c); |
|
408 } |
|
409 |
2941
|
410 char * |
|
411 gnu_readline::command_generator (const char *text, int state) |
|
412 { |
|
413 char *retval = 0; |
|
414 |
|
415 completion_fcn f = command_editor::get_completion_function (); |
|
416 |
3504
|
417 std::string tmp = f (text, state); |
2941
|
418 |
|
419 size_t len = tmp.length (); |
|
420 |
|
421 if (len > 0) |
|
422 { |
|
423 retval = static_cast<char *> (malloc (len+1)); |
|
424 |
|
425 strcpy (retval, tmp.c_str ()); |
|
426 } |
|
427 |
|
428 return retval; |
|
429 } |
|
430 |
|
431 char ** |
3519
|
432 gnu_readline::command_completer (const char *text, int, int) |
2941
|
433 { |
|
434 char **matches = 0; |
3519
|
435 matches |
|
436 = ::octave_rl_completion_matches (text, gnu_readline::command_generator); |
2941
|
437 return matches; |
|
438 } |
|
439 |
2926
|
440 #endif |
|
441 |
|
442 class |
|
443 default_command_editor : public command_editor |
|
444 { |
|
445 public: |
|
446 |
|
447 default_command_editor (void) |
|
448 : command_editor (), input_stream (stdin), output_stream (stdout) { } |
|
449 |
|
450 ~default_command_editor (void) { } |
|
451 |
3504
|
452 std::string do_readline (const std::string& prompt, bool& eof); |
2926
|
453 |
|
454 void do_set_input_stream (FILE *f); |
|
455 |
|
456 FILE *do_get_input_stream (void); |
|
457 |
|
458 void do_set_output_stream (FILE *f); |
|
459 |
|
460 FILE *do_get_output_stream (void); |
|
461 |
3504
|
462 void do_insert_text (const std::string&); |
2926
|
463 |
|
464 void do_newline (void); |
|
465 |
|
466 private: |
|
467 |
|
468 FILE *input_stream; |
|
469 |
|
470 FILE *output_stream; |
|
471 }; |
|
472 |
3504
|
473 std::string |
|
474 default_command_editor::do_readline (const std::string& prompt, bool& eof) |
2926
|
475 { |
3219
|
476 eof = false; |
|
477 |
2926
|
478 fprintf (output_stream, prompt.c_str ()); |
|
479 fflush (output_stream); |
|
480 |
|
481 return octave_fgets (input_stream); |
|
482 } |
|
483 |
|
484 void |
|
485 default_command_editor::do_set_input_stream (FILE *f) |
|
486 { |
|
487 input_stream = f; |
|
488 } |
|
489 |
|
490 FILE * |
|
491 default_command_editor::do_get_input_stream (void) |
|
492 { |
|
493 return input_stream; |
|
494 } |
|
495 |
|
496 void |
|
497 default_command_editor::do_set_output_stream (FILE *f) |
|
498 { |
|
499 output_stream = f; |
|
500 } |
|
501 |
|
502 FILE * |
|
503 default_command_editor::do_get_output_stream (void) |
|
504 { |
|
505 return output_stream; |
|
506 } |
|
507 |
|
508 void |
3504
|
509 default_command_editor::do_insert_text (const std::string&) |
2926
|
510 { |
|
511 // XXX FIXME XXX |
|
512 } |
|
513 |
|
514 void |
|
515 default_command_editor::do_newline (void) |
|
516 { |
|
517 // XXX FIXME XXX |
|
518 } |
|
519 |
|
520 bool |
|
521 command_editor::instance_ok (void) |
|
522 { |
|
523 bool retval = true; |
|
524 |
|
525 if (! instance) |
|
526 make_command_editor (); |
|
527 |
|
528 if (! instance) |
|
529 { |
2941
|
530 current_liboctave_error_handler |
2926
|
531 ("unable to create command history object!"); |
|
532 |
|
533 retval = false; |
|
534 } |
|
535 |
|
536 return retval; |
|
537 } |
|
538 |
|
539 void |
|
540 command_editor::make_command_editor (void) |
|
541 { |
|
542 #if defined (USE_READLINE) |
|
543 instance = new gnu_readline (); |
|
544 #else |
|
545 instance = new default_command_editor (); |
|
546 #endif |
|
547 } |
|
548 |
|
549 void |
3504
|
550 command_editor::set_name (const std::string& n) |
2926
|
551 { |
|
552 if (instance_ok ()) |
|
553 instance->do_set_name (n); |
|
554 } |
|
555 |
3504
|
556 std::string |
|
557 command_editor::readline (const std::string& prompt) |
2926
|
558 { |
3219
|
559 bool eof; |
|
560 |
|
561 return readline (prompt, eof); |
|
562 } |
|
563 |
3504
|
564 std::string |
|
565 command_editor::readline (const std::string& prompt, bool& eof) |
3219
|
566 { |
2926
|
567 return (instance_ok ()) |
3504
|
568 ? instance->do_readline (prompt, eof) : std::string (); |
2926
|
569 } |
|
570 |
|
571 void |
|
572 command_editor::set_input_stream (FILE *f) |
|
573 { |
|
574 if (instance_ok ()) |
|
575 instance->do_set_input_stream (f); |
|
576 } |
|
577 |
|
578 FILE * |
|
579 command_editor::get_input_stream (void) |
|
580 { |
|
581 return (instance_ok ()) |
|
582 ? instance->do_get_input_stream () : 0; |
|
583 } |
|
584 |
|
585 void |
|
586 command_editor::set_output_stream (FILE *f) |
|
587 { |
|
588 if (instance_ok ()) |
|
589 instance->do_set_output_stream (f); |
|
590 } |
|
591 |
|
592 FILE * |
|
593 command_editor::get_output_stream (void) |
|
594 { |
|
595 return (instance_ok ()) |
|
596 ? instance->do_get_output_stream () : 0; |
|
597 } |
|
598 |
|
599 int |
|
600 command_editor::terminal_rows (void) |
|
601 { |
|
602 return (instance_ok ()) |
|
603 ? instance->do_terminal_rows () : -1; |
|
604 } |
|
605 |
|
606 int |
|
607 command_editor::terminal_cols (void) |
|
608 { |
|
609 return (instance_ok ()) |
|
610 ? instance->do_terminal_cols () : -1; |
|
611 } |
|
612 |
|
613 void |
|
614 command_editor::clear_screen (void) |
|
615 { |
|
616 if (instance_ok ()) |
|
617 instance->do_clear_screen (); |
|
618 } |
|
619 |
3281
|
620 void |
|
621 command_editor::resize_terminal (void) |
|
622 { |
|
623 if (instance_ok ()) |
|
624 instance->do_resize_terminal (); |
|
625 } |
|
626 |
3504
|
627 std::string |
|
628 command_editor::decode_prompt_string (const std::string& s) |
2926
|
629 { |
|
630 return (instance_ok ()) |
3504
|
631 ? instance->do_decode_prompt_string (s) : std::string (); |
2926
|
632 } |
|
633 |
|
634 int |
|
635 command_editor::current_command_number (void) |
|
636 { |
|
637 return (instance_ok ()) |
|
638 ? instance->command_number : 0; |
|
639 } |
|
640 |
|
641 void |
|
642 command_editor::reset_current_command_number (int n) |
|
643 { |
|
644 if (instance_ok ()) |
|
645 instance->command_number = n; |
|
646 } |
|
647 |
|
648 void |
2967
|
649 command_editor::increment_current_command_number (void) |
|
650 { |
|
651 if (instance_ok ()) |
|
652 instance->command_number++; |
|
653 } |
|
654 |
|
655 void |
2926
|
656 command_editor::restore_terminal_state (void) |
|
657 { |
|
658 if (instance_ok ()) |
|
659 instance->do_restore_terminal_state (); |
|
660 } |
|
661 |
|
662 void |
|
663 command_editor::blink_matching_paren (bool flag) |
|
664 { |
|
665 if (instance_ok ()) |
|
666 instance->do_blink_matching_paren (flag); |
|
667 } |
|
668 |
|
669 void |
3933
|
670 command_editor::set_basic_word_break_characters (const std::string& s) |
|
671 { |
|
672 if (instance_ok ()) |
|
673 instance->do_set_basic_word_break_characters (s); |
|
674 } |
|
675 |
|
676 void |
|
677 command_editor::set_completer_word_break_characters (const std::string& s) |
|
678 { |
|
679 if (instance_ok ()) |
|
680 instance->do_set_completer_word_break_characters (s); |
|
681 } |
|
682 |
|
683 void |
3504
|
684 command_editor::set_basic_quote_characters (const std::string& s) |
2926
|
685 { |
|
686 if (instance_ok ()) |
3004
|
687 instance->do_set_basic_quote_characters (s); |
2926
|
688 } |
|
689 |
|
690 void |
|
691 command_editor::set_completion_append_character (char c) |
|
692 { |
|
693 if (instance_ok ()) |
|
694 instance->do_set_completion_append_character (c); |
|
695 } |
|
696 |
|
697 void |
2941
|
698 command_editor::set_completion_function (completion_fcn f) |
2926
|
699 { |
|
700 if (instance_ok ()) |
2941
|
701 instance->do_set_completion_function (f); |
|
702 } |
|
703 |
|
704 command_editor::completion_fcn |
|
705 command_editor::get_completion_function (void) |
|
706 { |
|
707 return (instance_ok ()) |
|
708 ? instance->do_get_completion_function () : 0; |
2926
|
709 } |
|
710 |
|
711 void |
3504
|
712 command_editor::insert_text (const std::string& text) |
2926
|
713 { |
|
714 if (instance_ok ()) |
|
715 instance->do_insert_text (text); |
|
716 } |
|
717 |
|
718 void |
|
719 command_editor::newline (void) |
|
720 { |
|
721 if (instance_ok ()) |
|
722 instance->do_newline (); |
|
723 } |
|
724 |
|
725 void |
|
726 command_editor::clear_undo_list (void) |
|
727 { |
|
728 if (instance_ok ()) |
|
729 instance->do_clear_undo_list (); |
|
730 } |
|
731 |
|
732 void |
3519
|
733 command_editor::set_startup_hook (startup_hook_fcn f) |
2926
|
734 { |
|
735 if (instance_ok ()) |
|
736 instance->do_set_startup_hook (f); |
|
737 } |
|
738 |
|
739 void |
|
740 command_editor::restore_startup_hook (void) |
|
741 { |
|
742 if (instance_ok ()) |
|
743 instance->do_restore_startup_hook (); |
|
744 } |
|
745 |
3189
|
746 void |
3519
|
747 command_editor::set_event_hook (event_hook_fcn f) |
3215
|
748 { |
|
749 if (instance_ok ()) |
|
750 instance->do_set_event_hook (f); |
|
751 } |
|
752 |
|
753 void |
|
754 command_editor::restore_event_hook (void) |
|
755 { |
|
756 if (instance_ok ()) |
|
757 instance->do_restore_event_hook (); |
|
758 } |
|
759 |
|
760 void |
3504
|
761 command_editor::read_init_file (const std::string& file) |
3189
|
762 { |
|
763 if (instance_ok ()) |
|
764 instance->do_read_init_file (file); |
|
765 } |
|
766 |
2926
|
767 // Return a string which will be printed as a prompt. The string may |
|
768 // contain special characters which are decoded as follows: |
|
769 // |
|
770 // \t the time |
|
771 // \d the date |
|
772 // \n CRLF |
|
773 // \s the name of the shell (program) |
|
774 // \w the current working directory |
|
775 // \W the last element of PWD |
|
776 // \u your username |
|
777 // \h the hostname |
|
778 // \# the command number of this command |
|
779 // \! the history number of this command |
|
780 // \$ a $ or a # if you are root |
|
781 // \<octal> character code in octal |
|
782 // \\ a backslash |
|
783 |
3504
|
784 std::string |
|
785 command_editor::do_decode_prompt_string (const std::string& s) |
2926
|
786 { |
3504
|
787 std::string result; |
|
788 std::string temp; |
2926
|
789 size_t i = 0; |
|
790 size_t slen = s.length (); |
|
791 int c; |
|
792 |
|
793 while (i < slen) |
|
794 { |
|
795 c = s[i]; |
|
796 |
|
797 i++; |
|
798 |
|
799 if (c == '\\') |
|
800 { |
|
801 c = s[i]; |
|
802 |
|
803 switch (c) |
|
804 { |
|
805 case '0': |
|
806 case '1': |
|
807 case '2': |
|
808 case '3': |
|
809 case '4': |
|
810 case '5': |
|
811 case '6': |
|
812 case '7': |
|
813 // Maybe convert an octal number. |
|
814 { |
|
815 int n = read_octal (s.substr (i, 3)); |
|
816 |
|
817 temp = "\\"; |
|
818 |
|
819 if (n != -1) |
|
820 { |
|
821 i += 3; |
|
822 temp[0] = n; |
|
823 } |
|
824 |
|
825 c = 0; |
|
826 goto add_string; |
|
827 } |
|
828 |
|
829 case 't': |
|
830 case 'd': |
|
831 // Make the current time/date into a string. |
|
832 { |
3260
|
833 octave_time now; |
2926
|
834 |
3260
|
835 temp = now.ctime (); |
2926
|
836 |
|
837 if (c == 't') |
|
838 { |
|
839 temp = temp.substr (11); |
|
840 temp.resize (8); |
|
841 } |
|
842 else |
|
843 temp.resize (10); |
|
844 |
|
845 goto add_string; |
|
846 } |
|
847 |
|
848 case 'n': |
|
849 { |
|
850 temp = newline_chars (); |
|
851 |
|
852 goto add_string; |
|
853 } |
|
854 |
|
855 case 's': |
|
856 { |
|
857 temp = octave_env::get_program_name (); |
|
858 temp = octave_env::base_pathname (temp); |
|
859 |
|
860 goto add_string; |
|
861 } |
|
862 |
|
863 case 'w': |
|
864 case 'W': |
|
865 { |
|
866 temp = octave_env::getcwd (); |
|
867 |
|
868 if (c == 'W') |
|
869 { |
|
870 size_t pos = temp.rfind ('/'); |
|
871 |
|
872 if (pos != NPOS && pos != 0) |
|
873 temp = temp.substr (pos + 1); |
|
874 } |
|
875 else |
|
876 temp = octave_env::polite_directory_format (temp); |
|
877 |
|
878 goto add_string; |
|
879 } |
|
880 |
|
881 case 'u': |
|
882 { |
|
883 temp = octave_env::get_user_name (); |
|
884 |
|
885 goto add_string; |
|
886 } |
|
887 |
|
888 case 'H': |
|
889 { |
|
890 temp = octave_env::get_host_name (); |
|
891 |
|
892 goto add_string; |
|
893 } |
|
894 |
|
895 case 'h': |
|
896 { |
|
897 temp = octave_env::get_host_name (); |
|
898 |
|
899 size_t pos = temp.find ('.'); |
|
900 |
|
901 if (pos != NPOS) |
|
902 temp.resize (pos); |
|
903 |
|
904 goto add_string; |
|
905 } |
|
906 |
|
907 case '#': |
|
908 { |
|
909 char number_buffer[128]; |
|
910 sprintf (number_buffer, "%d", command_number); |
|
911 temp = number_buffer; |
|
912 |
|
913 goto add_string; |
|
914 } |
|
915 |
|
916 case '!': |
|
917 { |
|
918 char number_buffer[128]; |
|
919 int num = command_history::current_number (); |
|
920 if (num > 0) |
|
921 sprintf (number_buffer, "%d", num); |
|
922 else |
|
923 strcpy (number_buffer, "!"); |
|
924 temp = number_buffer; |
|
925 |
|
926 goto add_string; |
|
927 } |
|
928 |
|
929 case '$': |
|
930 { |
|
931 temp = (::geteuid () == 0 ? "#" : "$"); |
|
932 |
|
933 goto add_string; |
|
934 } |
|
935 |
|
936 #if defined (USE_READLINE) |
|
937 case '[': |
|
938 case ']': |
|
939 { |
|
940 temp.resize (2); |
|
941 |
|
942 temp[0] = '\001'; |
|
943 temp[1] = ((c == '[') |
3519
|
944 ? ::octave_rl_prompt_start_ignore () |
|
945 : ::octave_rl_prompt_end_ignore ()); |
2926
|
946 |
|
947 goto add_string; |
|
948 } |
|
949 #endif |
|
950 |
|
951 case '\\': |
|
952 { |
|
953 temp = "\\"; |
|
954 |
|
955 goto add_string; |
|
956 } |
|
957 |
|
958 default: |
|
959 { |
|
960 temp = "\\ "; |
|
961 temp[1] = c; |
|
962 |
|
963 goto add_string; |
|
964 } |
|
965 |
|
966 add_string: |
|
967 { |
|
968 if (c) |
|
969 i++; |
|
970 |
|
971 result.append (temp); |
|
972 |
|
973 break; |
|
974 } |
|
975 } |
|
976 } |
|
977 else |
|
978 result += c; |
|
979 } |
|
980 |
|
981 return result; |
|
982 } |
|
983 |
|
984 // Return the octal number parsed from STRING, or -1 to indicate that |
|
985 // the string contained a bad number. |
|
986 |
|
987 int |
3504
|
988 command_editor::read_octal (const std::string& s) |
2926
|
989 { |
|
990 int result = 0; |
|
991 int digits = 0; |
|
992 |
|
993 size_t i = 0; |
|
994 size_t slen = s.length (); |
|
995 |
|
996 while (i < slen && s[i] >= '0' && s[i] < '8') |
|
997 { |
|
998 digits++; |
|
999 result = (result * 8) + s[i] - '0'; |
|
1000 i++; |
|
1001 } |
|
1002 |
|
1003 if (! digits || result > 0777 || i < slen) |
|
1004 result = -1; |
|
1005 |
|
1006 return result; |
|
1007 } |
|
1008 |
|
1009 void |
|
1010 command_editor::error (int err_num) |
|
1011 { |
2941
|
1012 current_liboctave_error_handler ("%s", strerror (err_num)); |
2926
|
1013 } |
|
1014 |
|
1015 void |
3504
|
1016 command_editor::error (const std::string& s) |
2926
|
1017 { |
2941
|
1018 current_liboctave_error_handler ("%s", s.c_str ()); |
2926
|
1019 } |
|
1020 |
|
1021 /* |
|
1022 ;;; Local Variables: *** |
|
1023 ;;; mode: C++ *** |
|
1024 ;;; End: *** |
|
1025 */ |