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