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 "C" void rl_deprep_terminal (void); |
|
57 |
|
58 extern int rl_blink_matching_paren; |
|
59 |
|
60 extern int screenheight; |
|
61 |
|
62 extern int screenwidth; |
|
63 |
|
64 class |
|
65 gnu_readline : public command_editor |
|
66 { |
|
67 public: |
|
68 |
|
69 typedef command_editor::fcn fcn; |
|
70 |
2941
|
71 typedef command_editor::completion_fcn completion_fcn; |
|
72 |
2926
|
73 gnu_readline (void); |
|
74 |
|
75 ~gnu_readline (void) { } |
|
76 |
|
77 void do_set_name (const string& n); |
|
78 |
|
79 string do_readline (const string& prompt); |
|
80 |
|
81 void do_set_input_stream (FILE *f); |
|
82 |
|
83 FILE *do_get_input_stream (void); |
|
84 |
|
85 void do_set_output_stream (FILE *f); |
|
86 |
|
87 FILE *do_get_output_stream (void); |
|
88 |
|
89 int do_terminal_rows (void); |
|
90 |
|
91 int do_terminal_cols (void); |
|
92 |
|
93 void do_clear_screen (void); |
|
94 |
|
95 string newline_chars (void); |
|
96 |
|
97 void do_restore_terminal_state (void); |
|
98 |
|
99 void do_blink_matching_paren (bool flag); |
|
100 |
|
101 void do_set_paren_string_delimiters (const string& s); |
|
102 |
|
103 void do_set_completion_append_character (char c); |
|
104 |
2941
|
105 void do_set_completion_function (completion_fcn f); |
|
106 |
|
107 completion_fcn do_get_completion_function (void) const; |
2926
|
108 |
|
109 void do_insert_text (const string& text); |
|
110 |
|
111 void do_newline (void); |
|
112 |
|
113 void do_clear_undo_list (void); |
|
114 |
|
115 void do_set_startup_hook (fcn f); |
|
116 |
|
117 void do_restore_startup_hook (void); |
|
118 |
|
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 { |
|
245 rl_deprep_terminal (); |
|
246 } |
|
247 |
|
248 void |
|
249 gnu_readline::do_blink_matching_paren (bool flag) |
|
250 { |
|
251 rl_blink_matching_paren = flag ? 1 : 0; |
|
252 } |
|
253 |
|
254 void |
|
255 gnu_readline::do_set_paren_string_delimiters (const string& s) |
|
256 { |
|
257 static char *ss = 0; |
|
258 |
|
259 delete [] ss; |
|
260 |
|
261 ss = strsave (s.c_str ()); |
|
262 |
|
263 rl_paren_string_delimiters = ss; |
|
264 } |
|
265 |
|
266 void |
|
267 gnu_readline::do_set_completion_append_character (char c) |
|
268 { |
|
269 rl_completion_append_character = c; |
|
270 } |
|
271 |
|
272 void |
2941
|
273 gnu_readline::do_set_completion_function (completion_fcn f) |
2926
|
274 { |
2941
|
275 completion_function = f; |
|
276 |
|
277 typedef char** (*foo) (...); |
|
278 |
|
279 rl_attempted_completion_function |
|
280 = completion_function |
|
281 ? static_cast<foo> (gnu_readline::command_completer) : 0; |
|
282 } |
|
283 |
|
284 gnu_readline::completion_fcn |
|
285 gnu_readline::do_get_completion_function (void) const |
|
286 { |
|
287 return completion_function; |
2926
|
288 } |
|
289 |
|
290 void |
|
291 gnu_readline::do_insert_text (const string& text) |
|
292 { |
|
293 rl_insert_text (text.c_str ()); |
|
294 } |
|
295 |
|
296 void |
|
297 gnu_readline::do_newline (void) |
|
298 { |
|
299 rl_newline (); |
|
300 } |
|
301 |
|
302 void |
|
303 gnu_readline::do_clear_undo_list () |
|
304 { |
|
305 if (rl_undo_list) |
|
306 { |
|
307 free_undo_list (); |
|
308 |
|
309 rl_undo_list = 0; |
|
310 } |
|
311 } |
|
312 |
|
313 void |
|
314 gnu_readline::do_set_startup_hook (fcn f) |
|
315 { |
|
316 previous_startup_hook = rl_startup_hook; |
|
317 |
|
318 rl_startup_hook = f; |
|
319 } |
|
320 |
|
321 void |
|
322 gnu_readline::do_restore_startup_hook (void) |
|
323 { |
|
324 rl_startup_hook = previous_startup_hook; |
|
325 } |
|
326 |
|
327 void |
|
328 gnu_readline::operate_and_get_next (int /* count */, int /* c */) |
|
329 { |
|
330 // Accept the current line. |
|
331 |
|
332 command_editor::newline (); |
|
333 |
|
334 // Find the current line, and find the next line to use. |
|
335 |
|
336 int x_where = command_history::where (); |
|
337 |
|
338 int x_length = command_history::length (); |
|
339 |
|
340 if ((command_history::is_stifled () |
|
341 && (x_length >= command_history::max_input_history ())) |
|
342 || (x_where >= x_length - 1)) |
|
343 command_history::set_mark (x_where); |
|
344 else |
|
345 command_history::set_mark (x_where + 1); |
|
346 |
|
347 command_editor::set_startup_hook (command_history::goto_mark); |
|
348 } |
|
349 |
2941
|
350 char * |
|
351 gnu_readline::command_generator (const char *text, int state) |
|
352 { |
|
353 char *retval = 0; |
|
354 |
|
355 completion_fcn f = command_editor::get_completion_function (); |
|
356 |
|
357 string tmp = f (text, state); |
|
358 |
|
359 size_t len = tmp.length (); |
|
360 |
|
361 if (len > 0) |
|
362 { |
|
363 retval = static_cast<char *> (malloc (len+1)); |
|
364 |
|
365 strcpy (retval, tmp.c_str ()); |
|
366 } |
|
367 |
|
368 return retval; |
|
369 } |
|
370 |
|
371 char ** |
|
372 gnu_readline::command_completer (char *text, int /* start */, int /* end */) |
|
373 { |
|
374 char **matches = 0; |
|
375 matches = completion_matches (text, gnu_readline::command_generator); |
|
376 return matches; |
|
377 } |
|
378 |
2926
|
379 #endif |
|
380 |
|
381 class |
|
382 default_command_editor : public command_editor |
|
383 { |
|
384 public: |
|
385 |
|
386 default_command_editor (void) |
|
387 : command_editor (), input_stream (stdin), output_stream (stdout) { } |
|
388 |
|
389 ~default_command_editor (void) { } |
|
390 |
|
391 string do_readline (const string& prompt); |
|
392 |
|
393 void do_set_input_stream (FILE *f); |
|
394 |
|
395 FILE *do_get_input_stream (void); |
|
396 |
|
397 void do_set_output_stream (FILE *f); |
|
398 |
|
399 FILE *do_get_output_stream (void); |
|
400 |
|
401 void do_insert_text (const string&); |
|
402 |
|
403 void do_newline (void); |
|
404 |
|
405 private: |
|
406 |
|
407 FILE *input_stream; |
|
408 |
|
409 FILE *output_stream; |
|
410 }; |
|
411 |
|
412 string |
|
413 default_command_editor::do_readline (const string& prompt) |
|
414 { |
|
415 fprintf (output_stream, prompt.c_str ()); |
|
416 fflush (output_stream); |
|
417 |
|
418 return octave_fgets (input_stream); |
|
419 } |
|
420 |
|
421 void |
|
422 default_command_editor::do_set_input_stream (FILE *f) |
|
423 { |
|
424 input_stream = f; |
|
425 } |
|
426 |
|
427 FILE * |
|
428 default_command_editor::do_get_input_stream (void) |
|
429 { |
|
430 return input_stream; |
|
431 } |
|
432 |
|
433 void |
|
434 default_command_editor::do_set_output_stream (FILE *f) |
|
435 { |
|
436 output_stream = f; |
|
437 } |
|
438 |
|
439 FILE * |
|
440 default_command_editor::do_get_output_stream (void) |
|
441 { |
|
442 return output_stream; |
|
443 } |
|
444 |
|
445 void |
|
446 default_command_editor::do_insert_text (const string&) |
|
447 { |
|
448 // XXX FIXME XXX |
|
449 } |
|
450 |
|
451 void |
|
452 default_command_editor::do_newline (void) |
|
453 { |
|
454 // XXX FIXME XXX |
|
455 } |
|
456 |
|
457 bool |
|
458 command_editor::instance_ok (void) |
|
459 { |
|
460 bool retval = true; |
|
461 |
|
462 if (! instance) |
|
463 make_command_editor (); |
|
464 |
|
465 if (! instance) |
|
466 { |
2941
|
467 current_liboctave_error_handler |
2926
|
468 ("unable to create command history object!"); |
|
469 |
|
470 retval = false; |
|
471 } |
|
472 |
|
473 return retval; |
|
474 } |
|
475 |
|
476 void |
|
477 command_editor::make_command_editor (void) |
|
478 { |
|
479 #if defined (USE_READLINE) |
|
480 instance = new gnu_readline (); |
|
481 #else |
|
482 instance = new default_command_editor (); |
|
483 #endif |
|
484 } |
|
485 |
|
486 void |
|
487 command_editor::set_name (const string& n) |
|
488 { |
|
489 if (instance_ok ()) |
|
490 instance->do_set_name (n); |
|
491 } |
|
492 |
|
493 string |
|
494 command_editor::readline (const string& prompt) |
|
495 { |
|
496 return (instance_ok ()) |
|
497 ? instance->do_readline (prompt) : string (); |
|
498 } |
|
499 |
|
500 void |
|
501 command_editor::set_input_stream (FILE *f) |
|
502 { |
|
503 if (instance_ok ()) |
|
504 instance->do_set_input_stream (f); |
|
505 } |
|
506 |
|
507 FILE * |
|
508 command_editor::get_input_stream (void) |
|
509 { |
|
510 return (instance_ok ()) |
|
511 ? instance->do_get_input_stream () : 0; |
|
512 } |
|
513 |
|
514 void |
|
515 command_editor::set_output_stream (FILE *f) |
|
516 { |
|
517 if (instance_ok ()) |
|
518 instance->do_set_output_stream (f); |
|
519 } |
|
520 |
|
521 FILE * |
|
522 command_editor::get_output_stream (void) |
|
523 { |
|
524 return (instance_ok ()) |
|
525 ? instance->do_get_output_stream () : 0; |
|
526 } |
|
527 |
|
528 int |
|
529 command_editor::terminal_rows (void) |
|
530 { |
|
531 return (instance_ok ()) |
|
532 ? instance->do_terminal_rows () : -1; |
|
533 } |
|
534 |
|
535 int |
|
536 command_editor::terminal_cols (void) |
|
537 { |
|
538 return (instance_ok ()) |
|
539 ? instance->do_terminal_cols () : -1; |
|
540 } |
|
541 |
|
542 void |
|
543 command_editor::clear_screen (void) |
|
544 { |
|
545 if (instance_ok ()) |
|
546 instance->do_clear_screen (); |
|
547 } |
|
548 |
|
549 string |
|
550 command_editor::decode_prompt_string (const string& s) |
|
551 { |
|
552 return (instance_ok ()) |
|
553 ? instance->do_decode_prompt_string (s) : string (); |
|
554 } |
|
555 |
|
556 int |
|
557 command_editor::current_command_number (void) |
|
558 { |
|
559 return (instance_ok ()) |
|
560 ? instance->command_number : 0; |
|
561 } |
|
562 |
|
563 void |
|
564 command_editor::reset_current_command_number (int n) |
|
565 { |
|
566 if (instance_ok ()) |
|
567 instance->command_number = n; |
|
568 } |
|
569 |
|
570 void |
|
571 command_editor::restore_terminal_state (void) |
|
572 { |
|
573 if (instance_ok ()) |
|
574 instance->do_restore_terminal_state (); |
|
575 } |
|
576 |
|
577 void |
|
578 command_editor::blink_matching_paren (bool flag) |
|
579 { |
|
580 if (instance_ok ()) |
|
581 instance->do_blink_matching_paren (flag); |
|
582 } |
|
583 |
|
584 void |
|
585 command_editor::set_paren_string_delimiters (const string& s) |
|
586 { |
|
587 if (instance_ok ()) |
|
588 instance->do_set_paren_string_delimiters (s); |
|
589 } |
|
590 |
|
591 void |
|
592 command_editor::set_completion_append_character (char c) |
|
593 { |
|
594 if (instance_ok ()) |
|
595 instance->do_set_completion_append_character (c); |
|
596 } |
|
597 |
|
598 void |
2941
|
599 command_editor::set_completion_function (completion_fcn f) |
2926
|
600 { |
|
601 if (instance_ok ()) |
2941
|
602 instance->do_set_completion_function (f); |
|
603 } |
|
604 |
|
605 command_editor::completion_fcn |
|
606 command_editor::get_completion_function (void) |
|
607 { |
|
608 return (instance_ok ()) |
|
609 ? instance->do_get_completion_function () : 0; |
2926
|
610 } |
|
611 |
|
612 void |
|
613 command_editor::insert_text (const string& text) |
|
614 { |
|
615 if (instance_ok ()) |
|
616 instance->do_insert_text (text); |
|
617 } |
|
618 |
|
619 void |
|
620 command_editor::newline (void) |
|
621 { |
|
622 if (instance_ok ()) |
|
623 instance->do_newline (); |
|
624 } |
|
625 |
|
626 void |
|
627 command_editor::clear_undo_list (void) |
|
628 { |
|
629 if (instance_ok ()) |
|
630 instance->do_clear_undo_list (); |
|
631 } |
|
632 |
|
633 void |
|
634 command_editor::set_startup_hook (fcn f) |
|
635 { |
|
636 if (instance_ok ()) |
|
637 instance->do_set_startup_hook (f); |
|
638 } |
|
639 |
|
640 void |
|
641 command_editor::restore_startup_hook (void) |
|
642 { |
|
643 if (instance_ok ()) |
|
644 instance->do_restore_startup_hook (); |
|
645 } |
|
646 |
|
647 // Return a string which will be printed as a prompt. The string may |
|
648 // contain special characters which are decoded as follows: |
|
649 // |
|
650 // \t the time |
|
651 // \d the date |
|
652 // \n CRLF |
|
653 // \s the name of the shell (program) |
|
654 // \w the current working directory |
|
655 // \W the last element of PWD |
|
656 // \u your username |
|
657 // \h the hostname |
|
658 // \# the command number of this command |
|
659 // \! the history number of this command |
|
660 // \$ a $ or a # if you are root |
|
661 // \<octal> character code in octal |
|
662 // \\ a backslash |
|
663 |
|
664 string |
|
665 command_editor::do_decode_prompt_string (const string& s) |
|
666 { |
|
667 string result; |
|
668 string temp; |
|
669 size_t i = 0; |
|
670 size_t slen = s.length (); |
|
671 int c; |
|
672 |
|
673 while (i < slen) |
|
674 { |
|
675 c = s[i]; |
|
676 |
|
677 i++; |
|
678 |
|
679 if (c == '\\') |
|
680 { |
|
681 c = s[i]; |
|
682 |
|
683 switch (c) |
|
684 { |
|
685 case '0': |
|
686 case '1': |
|
687 case '2': |
|
688 case '3': |
|
689 case '4': |
|
690 case '5': |
|
691 case '6': |
|
692 case '7': |
|
693 // Maybe convert an octal number. |
|
694 { |
|
695 int n = read_octal (s.substr (i, 3)); |
|
696 |
|
697 temp = "\\"; |
|
698 |
|
699 if (n != -1) |
|
700 { |
|
701 i += 3; |
|
702 temp[0] = n; |
|
703 } |
|
704 |
|
705 c = 0; |
|
706 goto add_string; |
|
707 } |
|
708 |
|
709 case 't': |
|
710 case 'd': |
|
711 // Make the current time/date into a string. |
|
712 { |
|
713 time_t now = time (0); |
|
714 |
|
715 temp = ctime (&now); |
|
716 |
|
717 if (c == 't') |
|
718 { |
|
719 temp = temp.substr (11); |
|
720 temp.resize (8); |
|
721 } |
|
722 else |
|
723 temp.resize (10); |
|
724 |
|
725 goto add_string; |
|
726 } |
|
727 |
|
728 case 'n': |
|
729 { |
|
730 temp = newline_chars (); |
|
731 |
|
732 goto add_string; |
|
733 } |
|
734 |
|
735 case 's': |
|
736 { |
|
737 temp = octave_env::get_program_name (); |
|
738 temp = octave_env::base_pathname (temp); |
|
739 |
|
740 goto add_string; |
|
741 } |
|
742 |
|
743 case 'w': |
|
744 case 'W': |
|
745 { |
|
746 temp = octave_env::getcwd (); |
|
747 |
|
748 if (c == 'W') |
|
749 { |
|
750 size_t pos = temp.rfind ('/'); |
|
751 |
|
752 if (pos != NPOS && pos != 0) |
|
753 temp = temp.substr (pos + 1); |
|
754 } |
|
755 else |
|
756 temp = octave_env::polite_directory_format (temp); |
|
757 |
|
758 goto add_string; |
|
759 } |
|
760 |
|
761 case 'u': |
|
762 { |
|
763 temp = octave_env::get_user_name (); |
|
764 |
|
765 goto add_string; |
|
766 } |
|
767 |
|
768 case 'H': |
|
769 { |
|
770 temp = octave_env::get_host_name (); |
|
771 |
|
772 goto add_string; |
|
773 } |
|
774 |
|
775 case 'h': |
|
776 { |
|
777 temp = octave_env::get_host_name (); |
|
778 |
|
779 size_t pos = temp.find ('.'); |
|
780 |
|
781 if (pos != NPOS) |
|
782 temp.resize (pos); |
|
783 |
|
784 goto add_string; |
|
785 } |
|
786 |
|
787 case '#': |
|
788 { |
|
789 char number_buffer[128]; |
|
790 sprintf (number_buffer, "%d", command_number); |
|
791 temp = number_buffer; |
|
792 |
|
793 goto add_string; |
|
794 } |
|
795 |
|
796 case '!': |
|
797 { |
|
798 char number_buffer[128]; |
|
799 int num = command_history::current_number (); |
|
800 if (num > 0) |
|
801 sprintf (number_buffer, "%d", num); |
|
802 else |
|
803 strcpy (number_buffer, "!"); |
|
804 temp = number_buffer; |
|
805 |
|
806 goto add_string; |
|
807 } |
|
808 |
|
809 case '$': |
|
810 { |
|
811 temp = (::geteuid () == 0 ? "#" : "$"); |
|
812 |
|
813 goto add_string; |
|
814 } |
|
815 |
|
816 #if defined (USE_READLINE) |
|
817 case '[': |
|
818 case ']': |
|
819 { |
|
820 temp.resize (2); |
|
821 |
|
822 temp[0] = '\001'; |
|
823 temp[1] = ((c == '[') |
|
824 ? RL_PROMPT_START_IGNORE |
|
825 : RL_PROMPT_END_IGNORE); |
|
826 |
|
827 goto add_string; |
|
828 } |
|
829 #endif |
|
830 |
|
831 case '\\': |
|
832 { |
|
833 temp = "\\"; |
|
834 |
|
835 goto add_string; |
|
836 } |
|
837 |
|
838 default: |
|
839 { |
|
840 temp = "\\ "; |
|
841 temp[1] = c; |
|
842 |
|
843 goto add_string; |
|
844 } |
|
845 |
|
846 add_string: |
|
847 { |
|
848 if (c) |
|
849 i++; |
|
850 |
|
851 result.append (temp); |
|
852 |
|
853 break; |
|
854 } |
|
855 } |
|
856 } |
|
857 else |
|
858 result += c; |
|
859 } |
|
860 |
|
861 return result; |
|
862 } |
|
863 |
|
864 // Return the octal number parsed from STRING, or -1 to indicate that |
|
865 // the string contained a bad number. |
|
866 |
|
867 int |
|
868 command_editor::read_octal (const string& s) |
|
869 { |
|
870 int result = 0; |
|
871 int digits = 0; |
|
872 |
|
873 size_t i = 0; |
|
874 size_t slen = s.length (); |
|
875 |
|
876 while (i < slen && s[i] >= '0' && s[i] < '8') |
|
877 { |
|
878 digits++; |
|
879 result = (result * 8) + s[i] - '0'; |
|
880 i++; |
|
881 } |
|
882 |
|
883 if (! digits || result > 0777 || i < slen) |
|
884 result = -1; |
|
885 |
|
886 return result; |
|
887 } |
|
888 |
|
889 void |
|
890 command_editor::error (int err_num) |
|
891 { |
2941
|
892 current_liboctave_error_handler ("%s", strerror (err_num)); |
2926
|
893 } |
|
894 |
|
895 void |
|
896 command_editor::error (const string& s) |
|
897 { |
2941
|
898 current_liboctave_error_handler ("%s", s.c_str ()); |
2926
|
899 } |
|
900 |
|
901 /* |
|
902 ;;; Local Variables: *** |
|
903 ;;; mode: C++ *** |
|
904 ;;; End: *** |
|
905 */ |