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