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