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