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 |
|
53 #include <readline/readline.h> |
|
54 |
|
55 // It would be nice if readline.h declared these, I think. |
|
56 |
|
57 extern int rl_blink_matching_paren; |
|
58 |
|
59 extern int screenheight; |
|
60 |
|
61 extern int screenwidth; |
|
62 |
|
63 class |
|
64 gnu_readline : public command_editor |
|
65 { |
|
66 public: |
|
67 |
|
68 typedef command_editor::fcn fcn; |
|
69 |
2941
|
70 typedef command_editor::completion_fcn completion_fcn; |
|
71 |
2926
|
72 gnu_readline (void); |
|
73 |
|
74 ~gnu_readline (void) { } |
|
75 |
3504
|
76 void do_set_name (const std::string& n); |
2926
|
77 |
3504
|
78 std::string do_readline (const std::string& prompt, bool& eof); |
2926
|
79 |
|
80 void do_set_input_stream (FILE *f); |
|
81 |
|
82 FILE *do_get_input_stream (void); |
|
83 |
|
84 void do_set_output_stream (FILE *f); |
|
85 |
|
86 FILE *do_get_output_stream (void); |
|
87 |
|
88 int do_terminal_rows (void); |
|
89 |
|
90 int do_terminal_cols (void); |
|
91 |
|
92 void do_clear_screen (void); |
|
93 |
3281
|
94 void do_resize_terminal (void); |
|
95 |
3504
|
96 std::string newline_chars (void); |
2926
|
97 |
|
98 void do_restore_terminal_state (void); |
|
99 |
|
100 void do_blink_matching_paren (bool flag); |
|
101 |
3504
|
102 void do_set_basic_quote_characters (const std::string& s); |
2926
|
103 |
|
104 void do_set_completion_append_character (char c); |
|
105 |
2941
|
106 void do_set_completion_function (completion_fcn f); |
|
107 |
|
108 completion_fcn do_get_completion_function (void) const; |
2926
|
109 |
3504
|
110 void do_insert_text (const std::string& text); |
2926
|
111 |
|
112 void do_newline (void); |
|
113 |
|
114 void do_clear_undo_list (void); |
|
115 |
|
116 void do_set_startup_hook (fcn f); |
|
117 |
|
118 void do_restore_startup_hook (void); |
|
119 |
3215
|
120 void do_set_event_hook (fcn f); |
|
121 |
|
122 void do_restore_event_hook (void); |
|
123 |
3504
|
124 void do_read_init_file (const std::string& file); |
3189
|
125 |
2926
|
126 static void operate_and_get_next (int, int); |
|
127 |
|
128 private: |
|
129 |
|
130 fcn previous_startup_hook; |
|
131 |
3215
|
132 fcn previous_event_hook; |
|
133 |
2941
|
134 completion_fcn completion_function; |
|
135 |
|
136 static char *command_generator (const char *text, int state); |
|
137 |
|
138 static char **command_completer (char *text, int start, int end); |
2926
|
139 }; |
|
140 |
|
141 gnu_readline::gnu_readline () |
3215
|
142 : command_editor (), previous_startup_hook (0), |
|
143 previous_event_hook (0), completion_function (0) |
2926
|
144 { |
3225
|
145 rl_terminal_name = ::getenv ("TERM"); |
|
146 |
2926
|
147 rl_initialize (); |
|
148 |
|
149 do_blink_matching_paren (true); |
|
150 |
|
151 // Bind operate-and-get-next. |
|
152 |
|
153 rl_add_defun ("operate-and-get-next", |
|
154 gnu_readline::operate_and_get_next, CTRL ('O')); |
|
155 |
|
156 // And the history search functions. |
|
157 |
|
158 rl_add_defun ("history-search-backward", |
|
159 rl_history_search_backward, META ('p')); |
|
160 |
|
161 rl_add_defun ("history-search-forward", |
|
162 rl_history_search_forward, META ('n')); |
|
163 } |
|
164 |
|
165 void |
3504
|
166 gnu_readline::do_set_name (const std::string& n) |
2926
|
167 { |
|
168 static char *nm = 0; |
|
169 |
|
170 delete [] nm; |
|
171 |
|
172 nm = strsave (n.c_str ()); |
|
173 |
|
174 rl_readline_name = nm; |
|
175 |
|
176 // Since we've already called rl_initialize, we need to re-read the |
|
177 // init file to take advantage of the conditional parsing feature |
|
178 // based on rl_readline_name; |
|
179 |
|
180 rl_re_read_init_file (); |
|
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 |
2926
|
190 char *line = ::readline (prompt.c_str ()); |
|
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 { |
|
207 rl_instream = f; |
|
208 } |
|
209 |
|
210 FILE * |
|
211 gnu_readline::do_get_input_stream (void) |
|
212 { |
|
213 return rl_instream; |
|
214 } |
|
215 |
|
216 void |
|
217 gnu_readline::do_set_output_stream (FILE *f) |
|
218 { |
|
219 rl_outstream = f; |
|
220 } |
|
221 |
|
222 FILE * |
|
223 gnu_readline::do_get_output_stream (void) |
|
224 { |
|
225 return rl_outstream; |
|
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 { |
|
237 return screenheight > 0 ? screenheight : 24; |
|
238 } |
|
239 |
|
240 int |
|
241 gnu_readline::do_terminal_cols (void) |
|
242 { |
|
243 return screenwidth > 0 ? screenwidth : 80; |
|
244 } |
|
245 |
|
246 void |
|
247 gnu_readline::do_clear_screen (void) |
|
248 { |
|
249 rl_clear_screen (); |
|
250 } |
|
251 |
3281
|
252 void |
|
253 gnu_readline::do_resize_terminal (void) |
|
254 { |
|
255 rl_resize_terminal (); |
|
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 { |
3004
|
267 if (rl_deprep_term_function) |
|
268 rl_deprep_term_function (); |
2926
|
269 } |
|
270 |
|
271 void |
|
272 gnu_readline::do_blink_matching_paren (bool flag) |
|
273 { |
|
274 rl_blink_matching_paren = flag ? 1 : 0; |
|
275 } |
|
276 |
|
277 void |
3504
|
278 gnu_readline::do_set_basic_quote_characters (const std::string& s) |
2926
|
279 { |
|
280 static char *ss = 0; |
|
281 |
|
282 delete [] ss; |
|
283 |
|
284 ss = strsave (s.c_str ()); |
|
285 |
3004
|
286 rl_basic_quote_characters = ss; |
2926
|
287 } |
|
288 |
|
289 void |
|
290 gnu_readline::do_set_completion_append_character (char c) |
|
291 { |
|
292 rl_completion_append_character = c; |
|
293 } |
|
294 |
|
295 void |
2941
|
296 gnu_readline::do_set_completion_function (completion_fcn f) |
2926
|
297 { |
2941
|
298 completion_function = f; |
|
299 |
|
300 typedef char** (*foo) (...); |
|
301 |
|
302 rl_attempted_completion_function |
|
303 = completion_function |
3145
|
304 ? reinterpret_cast<foo> (gnu_readline::command_completer) : 0; |
2941
|
305 } |
|
306 |
|
307 gnu_readline::completion_fcn |
|
308 gnu_readline::do_get_completion_function (void) const |
|
309 { |
|
310 return completion_function; |
2926
|
311 } |
|
312 |
|
313 void |
3504
|
314 gnu_readline::do_insert_text (const std::string& text) |
2926
|
315 { |
|
316 rl_insert_text (text.c_str ()); |
|
317 } |
|
318 |
|
319 void |
|
320 gnu_readline::do_newline (void) |
|
321 { |
|
322 rl_newline (); |
|
323 } |
|
324 |
|
325 void |
|
326 gnu_readline::do_clear_undo_list () |
|
327 { |
|
328 if (rl_undo_list) |
|
329 { |
|
330 free_undo_list (); |
|
331 |
|
332 rl_undo_list = 0; |
|
333 } |
|
334 } |
|
335 |
|
336 void |
|
337 gnu_readline::do_set_startup_hook (fcn f) |
|
338 { |
|
339 previous_startup_hook = rl_startup_hook; |
|
340 |
|
341 rl_startup_hook = f; |
|
342 } |
|
343 |
|
344 void |
|
345 gnu_readline::do_restore_startup_hook (void) |
|
346 { |
|
347 rl_startup_hook = previous_startup_hook; |
|
348 } |
|
349 |
|
350 void |
3215
|
351 gnu_readline::do_set_event_hook (fcn f) |
|
352 { |
|
353 previous_event_hook = rl_event_hook; |
|
354 |
|
355 rl_event_hook = f; |
|
356 } |
|
357 |
|
358 void |
|
359 gnu_readline::do_restore_event_hook (void) |
|
360 { |
|
361 rl_event_hook = previous_event_hook; |
|
362 } |
|
363 |
|
364 void |
3504
|
365 gnu_readline::do_read_init_file (const std::string& file) |
3189
|
366 { |
|
367 if (file.empty ()) |
|
368 rl_re_read_init_file (); |
|
369 else |
|
370 rl_read_init_file (file.c_str ()); |
|
371 } |
|
372 |
|
373 void |
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); |
|
394 } |
|
395 |
2941
|
396 char * |
|
397 gnu_readline::command_generator (const char *text, int state) |
|
398 { |
|
399 char *retval = 0; |
|
400 |
|
401 completion_fcn f = command_editor::get_completion_function (); |
|
402 |
3504
|
403 std::string tmp = f (text, state); |
2941
|
404 |
|
405 size_t len = tmp.length (); |
|
406 |
|
407 if (len > 0) |
|
408 { |
|
409 retval = static_cast<char *> (malloc (len+1)); |
|
410 |
|
411 strcpy (retval, tmp.c_str ()); |
|
412 } |
|
413 |
|
414 return retval; |
|
415 } |
|
416 |
|
417 char ** |
|
418 gnu_readline::command_completer (char *text, int /* start */, int /* end */) |
|
419 { |
|
420 char **matches = 0; |
|
421 matches = completion_matches (text, gnu_readline::command_generator); |
|
422 return matches; |
|
423 } |
|
424 |
2926
|
425 #endif |
|
426 |
|
427 class |
|
428 default_command_editor : public command_editor |
|
429 { |
|
430 public: |
|
431 |
|
432 default_command_editor (void) |
|
433 : command_editor (), input_stream (stdin), output_stream (stdout) { } |
|
434 |
|
435 ~default_command_editor (void) { } |
|
436 |
3504
|
437 std::string do_readline (const std::string& prompt, bool& eof); |
2926
|
438 |
|
439 void do_set_input_stream (FILE *f); |
|
440 |
|
441 FILE *do_get_input_stream (void); |
|
442 |
|
443 void do_set_output_stream (FILE *f); |
|
444 |
|
445 FILE *do_get_output_stream (void); |
|
446 |
3504
|
447 void do_insert_text (const std::string&); |
2926
|
448 |
|
449 void do_newline (void); |
|
450 |
|
451 private: |
|
452 |
|
453 FILE *input_stream; |
|
454 |
|
455 FILE *output_stream; |
|
456 }; |
|
457 |
3504
|
458 std::string |
|
459 default_command_editor::do_readline (const std::string& prompt, bool& eof) |
2926
|
460 { |
3219
|
461 eof = false; |
|
462 |
2926
|
463 fprintf (output_stream, prompt.c_str ()); |
|
464 fflush (output_stream); |
|
465 |
|
466 return octave_fgets (input_stream); |
|
467 } |
|
468 |
|
469 void |
|
470 default_command_editor::do_set_input_stream (FILE *f) |
|
471 { |
|
472 input_stream = f; |
|
473 } |
|
474 |
|
475 FILE * |
|
476 default_command_editor::do_get_input_stream (void) |
|
477 { |
|
478 return input_stream; |
|
479 } |
|
480 |
|
481 void |
|
482 default_command_editor::do_set_output_stream (FILE *f) |
|
483 { |
|
484 output_stream = f; |
|
485 } |
|
486 |
|
487 FILE * |
|
488 default_command_editor::do_get_output_stream (void) |
|
489 { |
|
490 return output_stream; |
|
491 } |
|
492 |
|
493 void |
3504
|
494 default_command_editor::do_insert_text (const std::string&) |
2926
|
495 { |
|
496 // XXX FIXME XXX |
|
497 } |
|
498 |
|
499 void |
|
500 default_command_editor::do_newline (void) |
|
501 { |
|
502 // XXX FIXME XXX |
|
503 } |
|
504 |
|
505 bool |
|
506 command_editor::instance_ok (void) |
|
507 { |
|
508 bool retval = true; |
|
509 |
|
510 if (! instance) |
|
511 make_command_editor (); |
|
512 |
|
513 if (! instance) |
|
514 { |
2941
|
515 current_liboctave_error_handler |
2926
|
516 ("unable to create command history object!"); |
|
517 |
|
518 retval = false; |
|
519 } |
|
520 |
|
521 return retval; |
|
522 } |
|
523 |
|
524 void |
|
525 command_editor::make_command_editor (void) |
|
526 { |
|
527 #if defined (USE_READLINE) |
|
528 instance = new gnu_readline (); |
|
529 #else |
|
530 instance = new default_command_editor (); |
|
531 #endif |
|
532 } |
|
533 |
|
534 void |
3504
|
535 command_editor::set_name (const std::string& n) |
2926
|
536 { |
|
537 if (instance_ok ()) |
|
538 instance->do_set_name (n); |
|
539 } |
|
540 |
3504
|
541 std::string |
|
542 command_editor::readline (const std::string& prompt) |
2926
|
543 { |
3219
|
544 bool eof; |
|
545 |
|
546 return readline (prompt, eof); |
|
547 } |
|
548 |
3504
|
549 std::string |
|
550 command_editor::readline (const std::string& prompt, bool& eof) |
3219
|
551 { |
2926
|
552 return (instance_ok ()) |
3504
|
553 ? instance->do_readline (prompt, eof) : std::string (); |
2926
|
554 } |
|
555 |
|
556 void |
|
557 command_editor::set_input_stream (FILE *f) |
|
558 { |
|
559 if (instance_ok ()) |
|
560 instance->do_set_input_stream (f); |
|
561 } |
|
562 |
|
563 FILE * |
|
564 command_editor::get_input_stream (void) |
|
565 { |
|
566 return (instance_ok ()) |
|
567 ? instance->do_get_input_stream () : 0; |
|
568 } |
|
569 |
|
570 void |
|
571 command_editor::set_output_stream (FILE *f) |
|
572 { |
|
573 if (instance_ok ()) |
|
574 instance->do_set_output_stream (f); |
|
575 } |
|
576 |
|
577 FILE * |
|
578 command_editor::get_output_stream (void) |
|
579 { |
|
580 return (instance_ok ()) |
|
581 ? instance->do_get_output_stream () : 0; |
|
582 } |
|
583 |
|
584 int |
|
585 command_editor::terminal_rows (void) |
|
586 { |
|
587 return (instance_ok ()) |
|
588 ? instance->do_terminal_rows () : -1; |
|
589 } |
|
590 |
|
591 int |
|
592 command_editor::terminal_cols (void) |
|
593 { |
|
594 return (instance_ok ()) |
|
595 ? instance->do_terminal_cols () : -1; |
|
596 } |
|
597 |
|
598 void |
|
599 command_editor::clear_screen (void) |
|
600 { |
|
601 if (instance_ok ()) |
|
602 instance->do_clear_screen (); |
|
603 } |
|
604 |
3281
|
605 void |
|
606 command_editor::resize_terminal (void) |
|
607 { |
|
608 if (instance_ok ()) |
|
609 instance->do_resize_terminal (); |
|
610 } |
|
611 |
3504
|
612 std::string |
|
613 command_editor::decode_prompt_string (const std::string& s) |
2926
|
614 { |
|
615 return (instance_ok ()) |
3504
|
616 ? instance->do_decode_prompt_string (s) : std::string (); |
2926
|
617 } |
|
618 |
|
619 int |
|
620 command_editor::current_command_number (void) |
|
621 { |
|
622 return (instance_ok ()) |
|
623 ? instance->command_number : 0; |
|
624 } |
|
625 |
|
626 void |
|
627 command_editor::reset_current_command_number (int n) |
|
628 { |
|
629 if (instance_ok ()) |
|
630 instance->command_number = n; |
|
631 } |
|
632 |
|
633 void |
2967
|
634 command_editor::increment_current_command_number (void) |
|
635 { |
|
636 if (instance_ok ()) |
|
637 instance->command_number++; |
|
638 } |
|
639 |
|
640 void |
2926
|
641 command_editor::restore_terminal_state (void) |
|
642 { |
|
643 if (instance_ok ()) |
|
644 instance->do_restore_terminal_state (); |
|
645 } |
|
646 |
|
647 void |
|
648 command_editor::blink_matching_paren (bool flag) |
|
649 { |
|
650 if (instance_ok ()) |
|
651 instance->do_blink_matching_paren (flag); |
|
652 } |
|
653 |
|
654 void |
3504
|
655 command_editor::set_basic_quote_characters (const std::string& s) |
2926
|
656 { |
|
657 if (instance_ok ()) |
3004
|
658 instance->do_set_basic_quote_characters (s); |
2926
|
659 } |
|
660 |
|
661 void |
|
662 command_editor::set_completion_append_character (char c) |
|
663 { |
|
664 if (instance_ok ()) |
|
665 instance->do_set_completion_append_character (c); |
|
666 } |
|
667 |
|
668 void |
2941
|
669 command_editor::set_completion_function (completion_fcn f) |
2926
|
670 { |
|
671 if (instance_ok ()) |
2941
|
672 instance->do_set_completion_function (f); |
|
673 } |
|
674 |
|
675 command_editor::completion_fcn |
|
676 command_editor::get_completion_function (void) |
|
677 { |
|
678 return (instance_ok ()) |
|
679 ? instance->do_get_completion_function () : 0; |
2926
|
680 } |
|
681 |
|
682 void |
3504
|
683 command_editor::insert_text (const std::string& text) |
2926
|
684 { |
|
685 if (instance_ok ()) |
|
686 instance->do_insert_text (text); |
|
687 } |
|
688 |
|
689 void |
|
690 command_editor::newline (void) |
|
691 { |
|
692 if (instance_ok ()) |
|
693 instance->do_newline (); |
|
694 } |
|
695 |
|
696 void |
|
697 command_editor::clear_undo_list (void) |
|
698 { |
|
699 if (instance_ok ()) |
|
700 instance->do_clear_undo_list (); |
|
701 } |
|
702 |
|
703 void |
|
704 command_editor::set_startup_hook (fcn f) |
|
705 { |
|
706 if (instance_ok ()) |
|
707 instance->do_set_startup_hook (f); |
|
708 } |
|
709 |
|
710 void |
|
711 command_editor::restore_startup_hook (void) |
|
712 { |
|
713 if (instance_ok ()) |
|
714 instance->do_restore_startup_hook (); |
|
715 } |
|
716 |
3189
|
717 void |
3215
|
718 command_editor::set_event_hook (fcn f) |
|
719 { |
|
720 if (instance_ok ()) |
|
721 instance->do_set_event_hook (f); |
|
722 } |
|
723 |
|
724 void |
|
725 command_editor::restore_event_hook (void) |
|
726 { |
|
727 if (instance_ok ()) |
|
728 instance->do_restore_event_hook (); |
|
729 } |
|
730 |
|
731 void |
3504
|
732 command_editor::read_init_file (const std::string& file) |
3189
|
733 { |
|
734 if (instance_ok ()) |
|
735 instance->do_read_init_file (file); |
|
736 } |
|
737 |
2926
|
738 // Return a string which will be printed as a prompt. The string may |
|
739 // contain special characters which are decoded as follows: |
|
740 // |
|
741 // \t the time |
|
742 // \d the date |
|
743 // \n CRLF |
|
744 // \s the name of the shell (program) |
|
745 // \w the current working directory |
|
746 // \W the last element of PWD |
|
747 // \u your username |
|
748 // \h the hostname |
|
749 // \# the command number of this command |
|
750 // \! the history number of this command |
|
751 // \$ a $ or a # if you are root |
|
752 // \<octal> character code in octal |
|
753 // \\ a backslash |
|
754 |
3504
|
755 std::string |
|
756 command_editor::do_decode_prompt_string (const std::string& s) |
2926
|
757 { |
3504
|
758 std::string result; |
|
759 std::string temp; |
2926
|
760 size_t i = 0; |
|
761 size_t slen = s.length (); |
|
762 int c; |
|
763 |
|
764 while (i < slen) |
|
765 { |
|
766 c = s[i]; |
|
767 |
|
768 i++; |
|
769 |
|
770 if (c == '\\') |
|
771 { |
|
772 c = s[i]; |
|
773 |
|
774 switch (c) |
|
775 { |
|
776 case '0': |
|
777 case '1': |
|
778 case '2': |
|
779 case '3': |
|
780 case '4': |
|
781 case '5': |
|
782 case '6': |
|
783 case '7': |
|
784 // Maybe convert an octal number. |
|
785 { |
|
786 int n = read_octal (s.substr (i, 3)); |
|
787 |
|
788 temp = "\\"; |
|
789 |
|
790 if (n != -1) |
|
791 { |
|
792 i += 3; |
|
793 temp[0] = n; |
|
794 } |
|
795 |
|
796 c = 0; |
|
797 goto add_string; |
|
798 } |
|
799 |
|
800 case 't': |
|
801 case 'd': |
|
802 // Make the current time/date into a string. |
|
803 { |
3260
|
804 octave_time now; |
2926
|
805 |
3260
|
806 temp = now.ctime (); |
2926
|
807 |
|
808 if (c == 't') |
|
809 { |
|
810 temp = temp.substr (11); |
|
811 temp.resize (8); |
|
812 } |
|
813 else |
|
814 temp.resize (10); |
|
815 |
|
816 goto add_string; |
|
817 } |
|
818 |
|
819 case 'n': |
|
820 { |
|
821 temp = newline_chars (); |
|
822 |
|
823 goto add_string; |
|
824 } |
|
825 |
|
826 case 's': |
|
827 { |
|
828 temp = octave_env::get_program_name (); |
|
829 temp = octave_env::base_pathname (temp); |
|
830 |
|
831 goto add_string; |
|
832 } |
|
833 |
|
834 case 'w': |
|
835 case 'W': |
|
836 { |
|
837 temp = octave_env::getcwd (); |
|
838 |
|
839 if (c == 'W') |
|
840 { |
|
841 size_t pos = temp.rfind ('/'); |
|
842 |
|
843 if (pos != NPOS && pos != 0) |
|
844 temp = temp.substr (pos + 1); |
|
845 } |
|
846 else |
|
847 temp = octave_env::polite_directory_format (temp); |
|
848 |
|
849 goto add_string; |
|
850 } |
|
851 |
|
852 case 'u': |
|
853 { |
|
854 temp = octave_env::get_user_name (); |
|
855 |
|
856 goto add_string; |
|
857 } |
|
858 |
|
859 case 'H': |
|
860 { |
|
861 temp = octave_env::get_host_name (); |
|
862 |
|
863 goto add_string; |
|
864 } |
|
865 |
|
866 case 'h': |
|
867 { |
|
868 temp = octave_env::get_host_name (); |
|
869 |
|
870 size_t pos = temp.find ('.'); |
|
871 |
|
872 if (pos != NPOS) |
|
873 temp.resize (pos); |
|
874 |
|
875 goto add_string; |
|
876 } |
|
877 |
|
878 case '#': |
|
879 { |
|
880 char number_buffer[128]; |
|
881 sprintf (number_buffer, "%d", command_number); |
|
882 temp = number_buffer; |
|
883 |
|
884 goto add_string; |
|
885 } |
|
886 |
|
887 case '!': |
|
888 { |
|
889 char number_buffer[128]; |
|
890 int num = command_history::current_number (); |
|
891 if (num > 0) |
|
892 sprintf (number_buffer, "%d", num); |
|
893 else |
|
894 strcpy (number_buffer, "!"); |
|
895 temp = number_buffer; |
|
896 |
|
897 goto add_string; |
|
898 } |
|
899 |
|
900 case '$': |
|
901 { |
|
902 temp = (::geteuid () == 0 ? "#" : "$"); |
|
903 |
|
904 goto add_string; |
|
905 } |
|
906 |
|
907 #if defined (USE_READLINE) |
|
908 case '[': |
|
909 case ']': |
|
910 { |
|
911 temp.resize (2); |
|
912 |
|
913 temp[0] = '\001'; |
|
914 temp[1] = ((c == '[') |
|
915 ? RL_PROMPT_START_IGNORE |
|
916 : RL_PROMPT_END_IGNORE); |
|
917 |
|
918 goto add_string; |
|
919 } |
|
920 #endif |
|
921 |
|
922 case '\\': |
|
923 { |
|
924 temp = "\\"; |
|
925 |
|
926 goto add_string; |
|
927 } |
|
928 |
|
929 default: |
|
930 { |
|
931 temp = "\\ "; |
|
932 temp[1] = c; |
|
933 |
|
934 goto add_string; |
|
935 } |
|
936 |
|
937 add_string: |
|
938 { |
|
939 if (c) |
|
940 i++; |
|
941 |
|
942 result.append (temp); |
|
943 |
|
944 break; |
|
945 } |
|
946 } |
|
947 } |
|
948 else |
|
949 result += c; |
|
950 } |
|
951 |
|
952 return result; |
|
953 } |
|
954 |
|
955 // Return the octal number parsed from STRING, or -1 to indicate that |
|
956 // the string contained a bad number. |
|
957 |
|
958 int |
3504
|
959 command_editor::read_octal (const std::string& s) |
2926
|
960 { |
|
961 int result = 0; |
|
962 int digits = 0; |
|
963 |
|
964 size_t i = 0; |
|
965 size_t slen = s.length (); |
|
966 |
|
967 while (i < slen && s[i] >= '0' && s[i] < '8') |
|
968 { |
|
969 digits++; |
|
970 result = (result * 8) + s[i] - '0'; |
|
971 i++; |
|
972 } |
|
973 |
|
974 if (! digits || result > 0777 || i < slen) |
|
975 result = -1; |
|
976 |
|
977 return result; |
|
978 } |
|
979 |
|
980 void |
|
981 command_editor::error (int err_num) |
|
982 { |
2941
|
983 current_liboctave_error_handler ("%s", strerror (err_num)); |
2926
|
984 } |
|
985 |
|
986 void |
3504
|
987 command_editor::error (const std::string& s) |
2926
|
988 { |
2941
|
989 current_liboctave_error_handler ("%s", s.c_str ()); |
2926
|
990 } |
|
991 |
|
992 /* |
|
993 ;;; Local Variables: *** |
|
994 ;;; mode: C++ *** |
|
995 ;;; End: *** |
|
996 */ |