Mercurial > hg > octave-nkf
annotate liboctave/cmd-edit.cc @ 7758:8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Mon, 05 May 2008 03:21:52 -0400 |
parents | a1dbe9d80eee |
children | 78400fde223e |
rev | line source |
---|---|
2926 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, |
4 2006, 2007 John W. Eaton | |
2926 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
2926 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
2926 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
3225 | 28 #include <cstdlib> |
2926 | 29 #include <cstring> |
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 | |
5247 | 40 #include "quit.h" |
41 | |
2926 | 42 #include "cmd-edit.h" |
43 #include "cmd-hist.h" | |
5872 | 44 #include "file-ops.h" |
2926 | 45 #include "lo-error.h" |
46 #include "lo-utils.h" | |
47 #include "oct-env.h" | |
3260 | 48 #include "oct-time.h" |
2926 | 49 |
50 command_editor *command_editor::instance = 0; | |
51 | |
6913 | 52 std::set<command_editor::startup_hook_fcn> command_editor::startup_hook_set; |
53 | |
54 std::set<command_editor::event_hook_fcn> command_editor::event_hook_set; | |
55 | |
2926 | 56 #if defined (USE_READLINE) |
57 | |
58 #include <cstdio> | |
59 #include <cstdlib> | |
60 | |
3519 | 61 #include "oct-rl-edit.h" |
2926 | 62 |
63 class | |
64 gnu_readline : public command_editor | |
65 { | |
66 public: | |
67 | |
3519 | 68 typedef command_editor::startup_hook_fcn startup_hook_fcn; |
69 | |
6913 | 70 typedef command_editor::event_hook_fcn event_hook_fcn; |
2926 | 71 |
2941 | 72 typedef command_editor::completion_fcn completion_fcn; |
73 | |
2926 | 74 gnu_readline (void); |
75 | |
76 ~gnu_readline (void) { } | |
77 | |
3504 | 78 void do_set_name (const std::string& n); |
2926 | 79 |
3504 | 80 std::string do_readline (const std::string& prompt, bool& eof); |
2926 | 81 |
82 void do_set_input_stream (FILE *f); | |
83 | |
84 FILE *do_get_input_stream (void); | |
85 | |
86 void do_set_output_stream (FILE *f); | |
87 | |
88 FILE *do_get_output_stream (void); | |
89 | |
90 int do_terminal_rows (void); | |
91 | |
92 int do_terminal_cols (void); | |
93 | |
94 void do_clear_screen (void); | |
95 | |
3281 | 96 void do_resize_terminal (void); |
97 | |
3504 | 98 std::string newline_chars (void); |
2926 | 99 |
100 void do_restore_terminal_state (void); | |
101 | |
102 void do_blink_matching_paren (bool flag); | |
103 | |
3933 | 104 void do_set_basic_word_break_characters (const std::string& s); |
105 | |
106 void do_set_completer_word_break_characters (const std::string& s); | |
107 | |
3504 | 108 void do_set_basic_quote_characters (const std::string& s); |
2926 | 109 |
6979 | 110 void do_set_filename_quote_characters (const std::string& s); |
111 | |
112 void do_set_completer_quote_characters (const std::string& s); | |
113 | |
2926 | 114 void do_set_completion_append_character (char c); |
115 | |
2941 | 116 void do_set_completion_function (completion_fcn f); |
117 | |
6979 | 118 void do_set_quoting_function (quoting_fcn f); |
119 | |
120 void do_set_dequoting_function (dequoting_fcn f); | |
121 | |
122 void do_set_char_is_quoted_function (char_is_quoted_fcn f); | |
123 | |
124 void do_set_user_accept_line_function (user_accept_line_fcn f); | |
125 | |
2941 | 126 completion_fcn do_get_completion_function (void) const; |
2926 | 127 |
6979 | 128 quoting_fcn do_get_quoting_function (void) const; |
129 | |
130 dequoting_fcn do_get_dequoting_function (void) const; | |
131 | |
132 char_is_quoted_fcn do_get_char_is_quoted_function (void) const; | |
133 | |
134 user_accept_line_fcn do_get_user_accept_line_function (void) const; | |
135 | |
4604 | 136 string_vector |
137 do_generate_filename_completions (const std::string& text); | |
138 | |
3504 | 139 void do_insert_text (const std::string& text); |
2926 | 140 |
141 void do_newline (void); | |
142 | |
6979 | 143 void do_accept_line (void); |
144 | |
2926 | 145 void do_clear_undo_list (void); |
146 | |
6913 | 147 void set_startup_hook (startup_hook_fcn f); |
148 | |
149 void restore_startup_hook (void); | |
2926 | 150 |
6913 | 151 void set_event_hook (event_hook_fcn f); |
2926 | 152 |
6913 | 153 void restore_event_hook (void); |
3215 | 154 |
155 void do_restore_event_hook (void); | |
156 | |
3504 | 157 void do_read_init_file (const std::string& file); |
3189 | 158 |
7758
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
159 void do_re_read_init_file (void); |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
160 |
4143 | 161 bool do_filename_completion_desired (bool); |
162 | |
6979 | 163 bool do_filename_quoting_desired (bool); |
164 | |
3519 | 165 static int operate_and_get_next (int, int); |
2926 | 166 |
3951 | 167 static int history_search_backward (int, int); |
168 | |
169 static int history_search_forward (int, int); | |
170 | |
2926 | 171 private: |
172 | |
3519 | 173 startup_hook_fcn previous_startup_hook; |
2926 | 174 |
3519 | 175 event_hook_fcn previous_event_hook; |
3215 | 176 |
2941 | 177 completion_fcn completion_function; |
178 | |
6979 | 179 quoting_fcn quoting_function; |
180 | |
181 dequoting_fcn dequoting_function; | |
182 | |
183 char_is_quoted_fcn char_is_quoted_function; | |
184 | |
185 user_accept_line_fcn user_accept_line_function; | |
186 | |
2941 | 187 static char *command_generator (const char *text, int state); |
188 | |
6979 | 189 static char *command_quoter (char *text, int match_type, char *quote_pointer); |
190 static char *command_dequoter (char *text, int match_type); | |
191 | |
192 static int command_char_is_quoted (char *text, int index); | |
193 | |
194 static int command_accept_line (int count, int key); | |
195 | |
3519 | 196 static char **command_completer (const char *text, int start, int end); |
2926 | 197 }; |
198 | |
199 gnu_readline::gnu_readline () | |
3215 | 200 : command_editor (), previous_startup_hook (0), |
6979 | 201 previous_event_hook (0), completion_function (0), |
202 quoting_function (0), dequoting_function (0), | |
203 char_is_quoted_function (0), user_accept_line_function (0) | |
2926 | 204 { |
5775 | 205 // FIXME -- need interface to rl_add_defun, rl_initialize, and |
3519 | 206 // a function to set rl_terminal_name |
3225 | 207 |
3520 | 208 std::string term = octave_env::getenv ("TERM"); |
3519 | 209 |
210 octave_rl_set_terminal_name (term.c_str ()); | |
211 | |
212 octave_rl_initialize (); | |
2926 | 213 |
214 do_blink_matching_paren (true); | |
215 | |
5452 | 216 // Bind operate-and-get-next. |
2926 | 217 |
3519 | 218 octave_rl_add_defun ("operate-and-get-next", |
219 gnu_readline::operate_and_get_next, | |
220 octave_rl_ctrl ('O')); | |
2926 | 221 |
5452 | 222 // And the history search functions. |
2926 | 223 |
3519 | 224 octave_rl_add_defun ("history-search-backward", |
3951 | 225 gnu_readline::history_search_backward, |
3519 | 226 octave_rl_meta ('P')); |
2926 | 227 |
3519 | 228 octave_rl_add_defun ("history-search-forward", |
3951 | 229 gnu_readline::history_search_forward, |
3519 | 230 octave_rl_meta ('N')); |
2926 | 231 } |
232 | |
233 void | |
3519 | 234 gnu_readline::do_set_name (const std::string& nm) |
2926 | 235 { |
3519 | 236 ::octave_rl_set_name (nm.c_str ()); |
2926 | 237 } |
238 | |
3504 | 239 std::string |
240 gnu_readline::do_readline (const std::string& prompt, bool& eof) | |
2926 | 241 { |
3504 | 242 std::string retval; |
2926 | 243 |
3219 | 244 eof = false; |
245 | |
5247 | 246 char *line = 0; |
247 | |
5336 | 248 const char *p = prompt.c_str (); |
249 | |
5452 | 250 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
5247 | 251 |
5336 | 252 line = ::octave_rl_readline (p); |
5247 | 253 |
5452 | 254 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
2926 | 255 |
256 if (line) | |
257 { | |
258 retval = line; | |
259 | |
260 free (line); | |
261 } | |
3219 | 262 else |
263 eof = true; | |
2926 | 264 |
265 return retval; | |
266 } | |
267 | |
268 void | |
269 gnu_readline::do_set_input_stream (FILE *f) | |
270 { | |
3519 | 271 ::octave_rl_set_input_stream (f); |
2926 | 272 } |
273 | |
274 FILE * | |
275 gnu_readline::do_get_input_stream (void) | |
276 { | |
3519 | 277 return ::octave_rl_get_input_stream (); |
2926 | 278 } |
279 | |
280 void | |
281 gnu_readline::do_set_output_stream (FILE *f) | |
282 { | |
3519 | 283 ::octave_rl_set_output_stream (f); |
2926 | 284 } |
285 | |
286 FILE * | |
287 gnu_readline::do_get_output_stream (void) | |
288 { | |
3519 | 289 return ::octave_rl_get_output_stream (); |
2926 | 290 } |
291 | |
292 // GNU readline handles SIGWINCH, so these values have a good chance | |
293 // of being correct even if the window changes size (they may be | |
294 // wrong if, for example, the luser changes the window size while the | |
295 // pager is running, and the signal is handled by the pager instead of | |
296 // us. | |
297 | |
298 int | |
299 gnu_readline::do_terminal_rows (void) | |
300 { | |
3519 | 301 int sh = ::octave_rl_screen_height (); |
302 | |
303 return sh > 0 ? sh : 24; | |
2926 | 304 } |
305 | |
306 int | |
307 gnu_readline::do_terminal_cols (void) | |
308 { | |
3519 | 309 int sw = ::octave_rl_screen_width (); |
310 | |
311 return sw > 0 ? sw : 80; | |
2926 | 312 } |
313 | |
314 void | |
315 gnu_readline::do_clear_screen (void) | |
316 { | |
3519 | 317 ::octave_rl_clear_screen (); |
2926 | 318 } |
319 | |
3281 | 320 void |
321 gnu_readline::do_resize_terminal (void) | |
322 { | |
3519 | 323 ::octave_rl_resize_terminal (); |
3281 | 324 } |
325 | |
3504 | 326 std::string |
2926 | 327 gnu_readline::newline_chars (void) |
328 { | |
329 return "\r\n"; | |
330 } | |
331 | |
332 void | |
333 gnu_readline::do_restore_terminal_state (void) | |
334 { | |
3519 | 335 ::octave_rl_restore_terminal_state (); |
2926 | 336 } |
337 | |
338 void | |
339 gnu_readline::do_blink_matching_paren (bool flag) | |
340 { | |
3779 | 341 ::octave_rl_enable_paren_matching (flag ? 1 : 0); |
2926 | 342 } |
343 | |
344 void | |
3933 | 345 gnu_readline::do_set_basic_word_break_characters (const std::string& s) |
346 { | |
347 ::octave_rl_set_basic_word_break_characters (s.c_str ()); | |
348 } | |
349 | |
350 void | |
351 gnu_readline::do_set_completer_word_break_characters (const std::string& s) | |
352 { | |
353 ::octave_rl_set_completer_word_break_characters (s.c_str ()); | |
354 } | |
355 | |
356 void | |
3504 | 357 gnu_readline::do_set_basic_quote_characters (const std::string& s) |
2926 | 358 { |
3519 | 359 ::octave_rl_set_basic_quote_characters (s.c_str ()); |
2926 | 360 } |
361 | |
362 void | |
6979 | 363 gnu_readline::do_set_filename_quote_characters (const std::string& s) |
364 { | |
365 ::octave_rl_set_filename_quote_characters (s.c_str ()); | |
366 } | |
367 | |
368 void | |
369 gnu_readline::do_set_completer_quote_characters (const std::string& s) | |
370 { | |
371 ::octave_rl_set_completer_quote_characters (s.c_str ()); | |
372 } | |
373 | |
374 void | |
2926 | 375 gnu_readline::do_set_completion_append_character (char c) |
376 { | |
3519 | 377 ::octave_rl_set_completion_append_character (c); |
2926 | 378 } |
379 | |
380 void | |
2941 | 381 gnu_readline::do_set_completion_function (completion_fcn f) |
2926 | 382 { |
2941 | 383 completion_function = f; |
384 | |
3519 | 385 rl_attempted_completion_fcn_ptr fp |
386 = f ? gnu_readline::command_completer : 0; | |
2941 | 387 |
3519 | 388 ::octave_rl_set_completion_function (fp); |
2941 | 389 } |
390 | |
6979 | 391 void |
392 gnu_readline::do_set_quoting_function (quoting_fcn f) | |
393 { | |
394 quoting_function = f; | |
395 | |
396 rl_quoting_fcn_ptr fp | |
397 = f ? gnu_readline::command_quoter : 0; | |
398 | |
399 ::octave_rl_set_quoting_function (fp); | |
400 } | |
401 | |
402 void | |
403 gnu_readline::do_set_dequoting_function (dequoting_fcn f) | |
404 { | |
405 dequoting_function = f; | |
406 | |
407 rl_dequoting_fcn_ptr fp | |
408 = f ? gnu_readline::command_dequoter : 0; | |
409 | |
410 ::octave_rl_set_dequoting_function (fp); | |
411 } | |
412 | |
413 void | |
414 gnu_readline::do_set_char_is_quoted_function (char_is_quoted_fcn f) | |
415 { | |
416 char_is_quoted_function = f; | |
417 | |
418 rl_char_is_quoted_fcn_ptr fp | |
419 = f ? gnu_readline::command_char_is_quoted : 0; | |
420 | |
421 ::octave_rl_set_char_is_quoted_function (fp); | |
422 } | |
423 | |
424 void | |
425 gnu_readline::do_set_user_accept_line_function (user_accept_line_fcn f) | |
426 { | |
427 user_accept_line_function = f; | |
428 | |
429 if (f) | |
430 octave_rl_add_defun ("accept-line", gnu_readline::command_accept_line, | |
431 ::octave_rl_ctrl ('M')); | |
432 else | |
433 octave_rl_add_defun ("accept-line", ::octave_rl_newline, | |
434 ::octave_rl_ctrl ('M')); | |
435 } | |
436 | |
2941 | 437 gnu_readline::completion_fcn |
438 gnu_readline::do_get_completion_function (void) const | |
439 { | |
440 return completion_function; | |
2926 | 441 } |
442 | |
6979 | 443 gnu_readline::quoting_fcn |
444 gnu_readline::do_get_quoting_function (void) const | |
445 { | |
446 return quoting_function; | |
447 } | |
448 | |
449 gnu_readline::dequoting_fcn | |
450 gnu_readline::do_get_dequoting_function (void) const | |
451 { | |
452 return dequoting_function; | |
453 } | |
454 | |
455 gnu_readline::char_is_quoted_fcn | |
456 gnu_readline::do_get_char_is_quoted_function (void) const | |
457 { | |
458 return char_is_quoted_function; | |
459 } | |
460 | |
461 gnu_readline::user_accept_line_fcn | |
462 gnu_readline::do_get_user_accept_line_function (void) const | |
463 { | |
464 return user_accept_line_function; | |
465 } | |
466 | |
4604 | 467 string_vector |
468 gnu_readline::do_generate_filename_completions (const std::string& text) | |
469 { | |
470 string_vector retval; | |
471 | |
472 int n = 0; | |
473 int count = 0; | |
474 | |
475 char *fn = 0; | |
476 | |
477 while (1) | |
478 { | |
479 fn = ::octave_rl_filename_completion_function (text.c_str (), count); | |
480 | |
481 if (fn) | |
482 { | |
483 if (count == n) | |
484 { | |
485 // Famous last words: Most large directories will not | |
486 // have more than a few hundred files, so we should not | |
487 // resize too many times even if the growth is linear... | |
488 | |
489 n += 100; | |
490 retval.resize (n); | |
491 } | |
492 | |
493 retval[count++] = fn; | |
494 | |
495 free (fn); | |
496 } | |
497 else | |
498 break; | |
499 } | |
500 | |
501 retval.resize (count); | |
502 | |
503 return retval; | |
504 } | |
505 | |
2926 | 506 void |
3504 | 507 gnu_readline::do_insert_text (const std::string& text) |
2926 | 508 { |
3519 | 509 ::octave_rl_insert_text (text.c_str ()); |
2926 | 510 } |
511 | |
512 void | |
513 gnu_readline::do_newline (void) | |
514 { | |
6979 | 515 ::octave_rl_newline (1, '\n'); |
516 } | |
517 | |
518 void | |
519 gnu_readline::do_accept_line (void) | |
520 { | |
521 command_accept_line (1, '\n'); | |
2926 | 522 } |
523 | |
524 void | |
525 gnu_readline::do_clear_undo_list () | |
526 { | |
3519 | 527 ::octave_rl_clear_undo_list (); |
2926 | 528 } |
529 | |
530 void | |
6913 | 531 gnu_readline::set_startup_hook (startup_hook_fcn f) |
2926 | 532 { |
3519 | 533 previous_startup_hook = ::octave_rl_get_startup_hook (); |
2926 | 534 |
6917 | 535 if (f != previous_startup_hook) |
536 ::octave_rl_set_startup_hook (f); | |
2926 | 537 } |
538 | |
539 void | |
6913 | 540 gnu_readline::restore_startup_hook (void) |
2926 | 541 { |
3519 | 542 ::octave_rl_set_startup_hook (previous_startup_hook); |
2926 | 543 } |
544 | |
545 void | |
6913 | 546 gnu_readline::set_event_hook (event_hook_fcn f) |
3215 | 547 { |
3519 | 548 previous_event_hook = octave_rl_get_event_hook (); |
3215 | 549 |
3519 | 550 ::octave_rl_set_event_hook (f); |
3215 | 551 } |
552 | |
553 void | |
6913 | 554 gnu_readline::restore_event_hook (void) |
3215 | 555 { |
3519 | 556 ::octave_rl_set_event_hook (previous_event_hook); |
3215 | 557 } |
558 | |
559 void | |
3504 | 560 gnu_readline::do_read_init_file (const std::string& file) |
3189 | 561 { |
3519 | 562 ::octave_rl_read_init_file (file.c_str ()); |
3189 | 563 } |
564 | |
7758
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
565 void |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
566 gnu_readline::do_re_read_init_file (void) |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
567 { |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
568 ::octave_rl_re_read_init_file (); |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
569 } |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
570 |
4143 | 571 bool |
572 gnu_readline::do_filename_completion_desired (bool arg) | |
573 { | |
574 return ::octave_rl_filename_completion_desired (arg); | |
575 } | |
576 | |
6979 | 577 bool |
578 gnu_readline::do_filename_quoting_desired (bool arg) | |
579 { | |
580 return ::octave_rl_filename_quoting_desired (arg); | |
581 } | |
582 | |
3519 | 583 int |
2926 | 584 gnu_readline::operate_and_get_next (int /* count */, int /* c */) |
585 { | |
586 // Accept the current line. | |
587 | |
6979 | 588 command_editor::accept_line (); |
2926 | 589 |
590 // Find the current line, and find the next line to use. | |
591 | |
592 int x_where = command_history::where (); | |
593 | |
594 int x_length = command_history::length (); | |
595 | |
596 if ((command_history::is_stifled () | |
597 && (x_length >= command_history::max_input_history ())) | |
598 || (x_where >= x_length - 1)) | |
599 command_history::set_mark (x_where); | |
600 else | |
601 command_history::set_mark (x_where + 1); | |
602 | |
6913 | 603 command_editor::add_startup_hook (command_history::goto_mark); |
3519 | 604 |
605 return 0; | |
2926 | 606 } |
607 | |
3951 | 608 int |
609 gnu_readline::history_search_backward (int count, int c) | |
610 { | |
611 return octave_rl_history_search_backward (count, c); | |
612 } | |
613 | |
614 int | |
615 gnu_readline::history_search_forward (int count, int c) | |
616 { | |
617 return octave_rl_history_search_forward (count, c); | |
618 } | |
619 | |
2941 | 620 char * |
621 gnu_readline::command_generator (const char *text, int state) | |
622 { | |
623 char *retval = 0; | |
624 | |
625 completion_fcn f = command_editor::get_completion_function (); | |
626 | |
3504 | 627 std::string tmp = f (text, state); |
2941 | 628 |
629 size_t len = tmp.length (); | |
630 | |
631 if (len > 0) | |
632 { | |
633 retval = static_cast<char *> (malloc (len+1)); | |
634 | |
635 strcpy (retval, tmp.c_str ()); | |
636 } | |
637 | |
638 return retval; | |
639 } | |
640 | |
6979 | 641 char * |
642 gnu_readline::command_quoter (char *text, int matches, char *qcp) | |
643 { | |
644 char *retval = 0; | |
645 | |
646 quoting_fcn f = command_editor::get_quoting_function (); | |
647 | |
648 std::string tmp = f (text, matches, *qcp); | |
649 | |
650 size_t len = tmp.length (); | |
651 | |
652 if (len > 0) | |
653 { | |
654 retval = static_cast<char *> (malloc (len+1)); | |
655 | |
656 strcpy (retval, tmp.c_str ()); | |
657 } | |
658 | |
659 return retval; | |
660 } | |
661 | |
662 char * | |
663 gnu_readline::command_dequoter (char *text, int quote) | |
664 { | |
665 char *retval = 0; | |
666 | |
667 dequoting_fcn f = command_editor::get_dequoting_function (); | |
668 | |
669 std::string tmp = f (text, quote); | |
670 | |
671 size_t len = tmp.length (); | |
672 | |
673 if (len > 0) | |
674 { | |
675 retval = static_cast<char *> (malloc (len+1)); | |
676 | |
677 strcpy (retval, tmp.c_str ()); | |
678 } | |
679 | |
680 return retval; | |
681 } | |
682 | |
683 int | |
684 gnu_readline::command_char_is_quoted (char *text, int quote) | |
685 { | |
686 char_is_quoted_fcn f = command_editor::get_char_is_quoted_function (); | |
687 | |
688 return f (text, quote); | |
689 } | |
690 | |
691 int | |
692 gnu_readline::command_accept_line (int count, int key) | |
693 { | |
694 user_accept_line_fcn f = command_editor::get_user_accept_line_function (); | |
695 | |
696 if (f) | |
697 f (::octave_rl_line_buffer ()); | |
698 | |
699 ::octave_rl_redisplay (); | |
700 | |
701 return ::octave_rl_newline (count, key); | |
702 } | |
703 | |
2941 | 704 char ** |
3519 | 705 gnu_readline::command_completer (const char *text, int, int) |
2941 | 706 { |
707 char **matches = 0; | |
3519 | 708 matches |
709 = ::octave_rl_completion_matches (text, gnu_readline::command_generator); | |
2941 | 710 return matches; |
711 } | |
712 | |
2926 | 713 #endif |
714 | |
715 class | |
716 default_command_editor : public command_editor | |
717 { | |
718 public: | |
719 | |
720 default_command_editor (void) | |
721 : command_editor (), input_stream (stdin), output_stream (stdout) { } | |
722 | |
723 ~default_command_editor (void) { } | |
724 | |
3504 | 725 std::string do_readline (const std::string& prompt, bool& eof); |
2926 | 726 |
727 void do_set_input_stream (FILE *f); | |
728 | |
729 FILE *do_get_input_stream (void); | |
730 | |
731 void do_set_output_stream (FILE *f); | |
732 | |
733 FILE *do_get_output_stream (void); | |
734 | |
4604 | 735 string_vector do_generate_filename_completions (const std::string& text); |
736 | |
3504 | 737 void do_insert_text (const std::string&); |
2926 | 738 |
739 void do_newline (void); | |
740 | |
6979 | 741 void do_accept_line (void); |
742 | |
2926 | 743 private: |
744 | |
745 FILE *input_stream; | |
746 | |
747 FILE *output_stream; | |
748 }; | |
749 | |
3504 | 750 std::string |
751 default_command_editor::do_readline (const std::string& prompt, bool& eof) | |
2926 | 752 { |
753 fprintf (output_stream, prompt.c_str ()); | |
754 fflush (output_stream); | |
755 | |
4527 | 756 return octave_fgetl (input_stream, eof); |
2926 | 757 } |
758 | |
759 void | |
760 default_command_editor::do_set_input_stream (FILE *f) | |
761 { | |
762 input_stream = f; | |
763 } | |
764 | |
765 FILE * | |
766 default_command_editor::do_get_input_stream (void) | |
767 { | |
768 return input_stream; | |
769 } | |
770 | |
771 void | |
772 default_command_editor::do_set_output_stream (FILE *f) | |
773 { | |
774 output_stream = f; | |
775 } | |
776 | |
777 FILE * | |
778 default_command_editor::do_get_output_stream (void) | |
779 { | |
780 return output_stream; | |
781 } | |
782 | |
4604 | 783 string_vector |
4663 | 784 default_command_editor::do_generate_filename_completions (const std::string&) |
4604 | 785 { |
5775 | 786 // FIXME |
4604 | 787 return string_vector (); |
788 } | |
789 | |
2926 | 790 void |
3504 | 791 default_command_editor::do_insert_text (const std::string&) |
2926 | 792 { |
5775 | 793 // FIXME |
2926 | 794 } |
795 | |
796 void | |
797 default_command_editor::do_newline (void) | |
798 { | |
5775 | 799 // FIXME |
2926 | 800 } |
801 | |
6979 | 802 void |
803 default_command_editor::do_accept_line (void) | |
804 { | |
805 // FIXME | |
806 } | |
807 | |
2926 | 808 bool |
809 command_editor::instance_ok (void) | |
810 { | |
811 bool retval = true; | |
812 | |
813 if (! instance) | |
814 make_command_editor (); | |
815 | |
816 if (! instance) | |
817 { | |
2941 | 818 current_liboctave_error_handler |
2926 | 819 ("unable to create command history object!"); |
820 | |
821 retval = false; | |
822 } | |
823 | |
824 return retval; | |
825 } | |
826 | |
827 void | |
828 command_editor::make_command_editor (void) | |
829 { | |
830 #if defined (USE_READLINE) | |
831 instance = new gnu_readline (); | |
832 #else | |
833 instance = new default_command_editor (); | |
834 #endif | |
835 } | |
836 | |
6913 | 837 int |
838 command_editor::startup_handler (void) | |
839 { | |
840 for (startup_hook_set_iterator p = startup_hook_set.begin (); | |
841 p != startup_hook_set.end (); p++) | |
842 { | |
843 startup_hook_fcn f = *p; | |
844 | |
845 if (f) | |
846 f (); | |
847 } | |
848 | |
849 return 0; | |
850 } | |
851 | |
852 int | |
853 command_editor::event_handler (void) | |
854 { | |
855 for (event_hook_set_iterator p = event_hook_set.begin (); | |
856 p != event_hook_set.end (); p++) | |
857 { | |
858 event_hook_fcn f = *p; | |
859 | |
860 if (f) | |
861 f (); | |
862 } | |
863 | |
864 return 0; | |
865 } | |
866 | |
2926 | 867 void |
3504 | 868 command_editor::set_name (const std::string& n) |
2926 | 869 { |
870 if (instance_ok ()) | |
871 instance->do_set_name (n); | |
872 } | |
873 | |
3504 | 874 std::string |
875 command_editor::readline (const std::string& prompt) | |
2926 | 876 { |
3219 | 877 bool eof; |
878 | |
879 return readline (prompt, eof); | |
880 } | |
881 | |
3504 | 882 std::string |
883 command_editor::readline (const std::string& prompt, bool& eof) | |
3219 | 884 { |
2926 | 885 return (instance_ok ()) |
3504 | 886 ? instance->do_readline (prompt, eof) : std::string (); |
2926 | 887 } |
888 | |
889 void | |
890 command_editor::set_input_stream (FILE *f) | |
891 { | |
892 if (instance_ok ()) | |
893 instance->do_set_input_stream (f); | |
894 } | |
895 | |
896 FILE * | |
897 command_editor::get_input_stream (void) | |
898 { | |
899 return (instance_ok ()) | |
900 ? instance->do_get_input_stream () : 0; | |
901 } | |
902 | |
903 void | |
904 command_editor::set_output_stream (FILE *f) | |
905 { | |
906 if (instance_ok ()) | |
907 instance->do_set_output_stream (f); | |
908 } | |
909 | |
910 FILE * | |
911 command_editor::get_output_stream (void) | |
912 { | |
913 return (instance_ok ()) | |
914 ? instance->do_get_output_stream () : 0; | |
915 } | |
916 | |
917 int | |
918 command_editor::terminal_rows (void) | |
919 { | |
920 return (instance_ok ()) | |
921 ? instance->do_terminal_rows () : -1; | |
922 } | |
923 | |
924 int | |
925 command_editor::terminal_cols (void) | |
926 { | |
927 return (instance_ok ()) | |
928 ? instance->do_terminal_cols () : -1; | |
929 } | |
930 | |
931 void | |
932 command_editor::clear_screen (void) | |
933 { | |
934 if (instance_ok ()) | |
935 instance->do_clear_screen (); | |
936 } | |
937 | |
3281 | 938 void |
939 command_editor::resize_terminal (void) | |
940 { | |
941 if (instance_ok ()) | |
942 instance->do_resize_terminal (); | |
943 } | |
944 | |
3504 | 945 std::string |
946 command_editor::decode_prompt_string (const std::string& s) | |
2926 | 947 { |
948 return (instance_ok ()) | |
3504 | 949 ? instance->do_decode_prompt_string (s) : std::string (); |
2926 | 950 } |
951 | |
952 int | |
953 command_editor::current_command_number (void) | |
954 { | |
955 return (instance_ok ()) | |
956 ? instance->command_number : 0; | |
957 } | |
958 | |
959 void | |
960 command_editor::reset_current_command_number (int n) | |
961 { | |
962 if (instance_ok ()) | |
963 instance->command_number = n; | |
964 } | |
965 | |
966 void | |
2967 | 967 command_editor::increment_current_command_number (void) |
968 { | |
969 if (instance_ok ()) | |
970 instance->command_number++; | |
971 } | |
972 | |
973 void | |
2926 | 974 command_editor::restore_terminal_state (void) |
975 { | |
976 if (instance_ok ()) | |
977 instance->do_restore_terminal_state (); | |
978 } | |
979 | |
980 void | |
981 command_editor::blink_matching_paren (bool flag) | |
982 { | |
983 if (instance_ok ()) | |
984 instance->do_blink_matching_paren (flag); | |
985 } | |
986 | |
987 void | |
3933 | 988 command_editor::set_basic_word_break_characters (const std::string& s) |
989 { | |
990 if (instance_ok ()) | |
991 instance->do_set_basic_word_break_characters (s); | |
992 } | |
993 | |
994 void | |
995 command_editor::set_completer_word_break_characters (const std::string& s) | |
996 { | |
997 if (instance_ok ()) | |
998 instance->do_set_completer_word_break_characters (s); | |
999 } | |
1000 | |
1001 void | |
3504 | 1002 command_editor::set_basic_quote_characters (const std::string& s) |
2926 | 1003 { |
1004 if (instance_ok ()) | |
3004 | 1005 instance->do_set_basic_quote_characters (s); |
2926 | 1006 } |
1007 | |
1008 void | |
6979 | 1009 command_editor::set_filename_quote_characters (const std::string& s) |
1010 { | |
1011 if (instance_ok ()) | |
1012 instance->do_set_filename_quote_characters (s); | |
1013 } | |
1014 | |
1015 void | |
1016 command_editor::set_completer_quote_characters (const std::string& s) | |
1017 { | |
1018 if (instance_ok ()) | |
1019 instance->do_set_completer_quote_characters (s); | |
1020 } | |
1021 | |
1022 void | |
2926 | 1023 command_editor::set_completion_append_character (char c) |
1024 { | |
1025 if (instance_ok ()) | |
1026 instance->do_set_completion_append_character (c); | |
1027 } | |
1028 | |
1029 void | |
2941 | 1030 command_editor::set_completion_function (completion_fcn f) |
2926 | 1031 { |
1032 if (instance_ok ()) | |
2941 | 1033 instance->do_set_completion_function (f); |
1034 } | |
1035 | |
6979 | 1036 void |
1037 command_editor::set_quoting_function (quoting_fcn f) | |
1038 { | |
1039 if (instance_ok ()) | |
1040 instance->do_set_quoting_function (f); | |
1041 } | |
1042 | |
1043 void | |
1044 command_editor::set_dequoting_function (dequoting_fcn f) | |
1045 { | |
1046 if (instance_ok ()) | |
1047 instance->do_set_dequoting_function (f); | |
1048 } | |
1049 | |
1050 void | |
1051 command_editor::set_char_is_quoted_function (char_is_quoted_fcn f) | |
1052 { | |
1053 if (instance_ok ()) | |
1054 instance->do_set_char_is_quoted_function (f); | |
1055 } | |
1056 | |
1057 void | |
1058 command_editor::set_user_accept_line_function (user_accept_line_fcn f) | |
1059 { | |
1060 if (instance_ok ()) | |
1061 instance->do_set_user_accept_line_function (f); | |
1062 } | |
1063 | |
2941 | 1064 command_editor::completion_fcn |
1065 command_editor::get_completion_function (void) | |
1066 { | |
1067 return (instance_ok ()) | |
1068 ? instance->do_get_completion_function () : 0; | |
2926 | 1069 } |
1070 | |
6979 | 1071 command_editor::quoting_fcn |
1072 command_editor::get_quoting_function (void) | |
1073 { | |
1074 return (instance_ok ()) | |
1075 ? instance->do_get_quoting_function () : 0; | |
1076 } | |
1077 | |
1078 command_editor::dequoting_fcn | |
1079 command_editor::get_dequoting_function (void) | |
1080 { | |
1081 return (instance_ok ()) | |
1082 ? instance->do_get_dequoting_function () : 0; | |
1083 } | |
1084 | |
1085 command_editor::char_is_quoted_fcn | |
1086 command_editor::get_char_is_quoted_function (void) | |
1087 { | |
1088 return (instance_ok ()) | |
1089 ? instance->do_get_char_is_quoted_function () : 0; | |
1090 } | |
1091 | |
1092 command_editor::user_accept_line_fcn | |
1093 command_editor::get_user_accept_line_function (void) | |
1094 { | |
1095 return (instance_ok ()) | |
1096 ? instance->do_get_user_accept_line_function () : 0; | |
1097 } | |
1098 | |
4604 | 1099 string_vector |
1100 command_editor::generate_filename_completions (const std::string& text) | |
1101 { | |
1102 return (instance_ok ()) | |
1103 ? instance->do_generate_filename_completions (text) : string_vector (); | |
1104 } | |
1105 | |
2926 | 1106 void |
3504 | 1107 command_editor::insert_text (const std::string& text) |
2926 | 1108 { |
1109 if (instance_ok ()) | |
1110 instance->do_insert_text (text); | |
1111 } | |
1112 | |
1113 void | |
1114 command_editor::newline (void) | |
1115 { | |
1116 if (instance_ok ()) | |
1117 instance->do_newline (); | |
1118 } | |
1119 | |
1120 void | |
6979 | 1121 command_editor::accept_line (void) |
1122 { | |
1123 if (instance_ok ()) | |
1124 instance->do_accept_line (); | |
1125 } | |
1126 | |
1127 void | |
2926 | 1128 command_editor::clear_undo_list (void) |
1129 { | |
1130 if (instance_ok ()) | |
1131 instance->do_clear_undo_list (); | |
1132 } | |
1133 | |
1134 void | |
6913 | 1135 command_editor::add_startup_hook (startup_hook_fcn f) |
2926 | 1136 { |
1137 if (instance_ok ()) | |
6913 | 1138 { |
1139 startup_hook_set.insert (f); | |
1140 | |
1141 instance->set_startup_hook (startup_handler); | |
1142 } | |
2926 | 1143 } |
1144 | |
1145 void | |
6913 | 1146 command_editor::remove_startup_hook (startup_hook_fcn f) |
2926 | 1147 { |
1148 if (instance_ok ()) | |
6913 | 1149 { |
1150 startup_hook_set_iterator p = startup_hook_set.find (f); | |
1151 | |
1152 if (p != startup_hook_set.end ()) | |
6916 | 1153 startup_hook_set.erase (p); |
6913 | 1154 |
1155 if (startup_hook_set.empty ()) | |
1156 instance->restore_startup_hook (); | |
1157 } | |
2926 | 1158 } |
1159 | |
3189 | 1160 void |
6913 | 1161 command_editor::add_event_hook (event_hook_fcn f) |
3215 | 1162 { |
1163 if (instance_ok ()) | |
6913 | 1164 { |
1165 event_hook_set.insert (f); | |
1166 | |
1167 instance->set_event_hook (event_handler); | |
1168 } | |
3215 | 1169 } |
1170 | |
1171 void | |
6913 | 1172 command_editor::remove_event_hook (event_hook_fcn f) |
3215 | 1173 { |
1174 if (instance_ok ()) | |
6913 | 1175 { |
1176 event_hook_set_iterator p = event_hook_set.find (f); | |
1177 | |
1178 if (p != event_hook_set.end ()) | |
1179 event_hook_set.erase (p); | |
1180 | |
1181 if (event_hook_set.empty ()) | |
1182 instance->restore_event_hook (); | |
1183 } | |
3215 | 1184 } |
1185 | |
1186 void | |
5872 | 1187 command_editor::read_init_file (const std::string& file_arg) |
3189 | 1188 { |
1189 if (instance_ok ()) | |
5872 | 1190 { |
1191 std::string file = file_ops::tilde_expand (file_arg); | |
1192 | |
1193 instance->do_read_init_file (file); | |
1194 } | |
3189 | 1195 } |
1196 | |
7758
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
1197 void |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
1198 command_editor::re_read_init_file (void) |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
1199 { |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
1200 if (instance_ok ()) |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
1201 instance->do_re_read_init_file (); |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
1202 } |
8e14a01ffe9f
input.cc (Fre_read_readline_init_file): new function
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
1203 |
4143 | 1204 bool |
1205 command_editor::filename_completion_desired (bool arg) | |
1206 { | |
1207 return (instance_ok ()) | |
1208 ? instance->do_filename_completion_desired (arg) : false; | |
1209 } | |
1210 | |
6979 | 1211 bool |
1212 command_editor::filename_quoting_desired (bool arg) | |
1213 { | |
1214 return (instance_ok ()) | |
1215 ? instance->do_filename_quoting_desired (arg) : false; | |
1216 } | |
1217 | |
2926 | 1218 // Return a string which will be printed as a prompt. The string may |
1219 // contain special characters which are decoded as follows: | |
1220 // | |
5442 | 1221 // \a bell (ascii 07) |
2926 | 1222 // \d the date |
5442 | 1223 // \e escape (ascii 033) |
1224 // \h the hostname up to the first `.' | |
1225 // \H the hostname | |
2926 | 1226 // \n CRLF |
5442 | 1227 // \r CR |
2926 | 1228 // \s the name of the shell (program) |
5442 | 1229 // \t the time |
1230 // \T the time in 12-hour hh:mm:ss format | |
1231 // \@ the time in 12-hour hh:mm am/pm format | |
1232 // \A the time in 24-hour hh:mm format | |
1233 // \u your username | |
2926 | 1234 // \w the current working directory |
1235 // \W the last element of PWD | |
5442 | 1236 // \! the history number of this command |
2926 | 1237 // \# the command number of this command |
1238 // \$ a $ or a # if you are root | |
5442 | 1239 // \nnn character code nnn in octal |
2926 | 1240 // \\ a backslash |
5442 | 1241 // \[ begin a sequence of non-printing chars |
1242 // \] end a sequence of non-printing chars | |
2926 | 1243 |
3504 | 1244 std::string |
1245 command_editor::do_decode_prompt_string (const std::string& s) | |
2926 | 1246 { |
3504 | 1247 std::string result; |
1248 std::string temp; | |
2926 | 1249 size_t i = 0; |
1250 size_t slen = s.length (); | |
1251 int c; | |
1252 | |
1253 while (i < slen) | |
1254 { | |
1255 c = s[i]; | |
1256 | |
1257 i++; | |
1258 | |
1259 if (c == '\\') | |
1260 { | |
1261 c = s[i]; | |
1262 | |
1263 switch (c) | |
1264 { | |
1265 case '0': | |
1266 case '1': | |
1267 case '2': | |
1268 case '3': | |
1269 case '4': | |
1270 case '5': | |
1271 case '6': | |
1272 case '7': | |
1273 // Maybe convert an octal number. | |
1274 { | |
1275 int n = read_octal (s.substr (i, 3)); | |
1276 | |
1277 temp = "\\"; | |
1278 | |
1279 if (n != -1) | |
1280 { | |
1281 i += 3; | |
1282 temp[0] = n; | |
1283 } | |
1284 | |
1285 c = 0; | |
1286 goto add_string; | |
1287 } | |
5442 | 1288 |
1289 case 'a': | |
1290 { | |
1291 temp = '\a'; | |
1292 | |
1293 goto add_string; | |
1294 } | |
1295 | |
1296 case 'e': | |
1297 { | |
1298 temp = '\033'; | |
1299 | |
1300 goto add_string; | |
1301 } | |
1302 | |
1303 case 'r': | |
1304 { | |
1305 temp = '\r'; | |
1306 | |
1307 goto add_string; | |
1308 } | |
1309 | |
1310 case 'd': | |
2926 | 1311 case 't': |
5442 | 1312 case 'T': |
1313 case '@': | |
1314 case 'A': | |
2926 | 1315 // Make the current time/date into a string. |
1316 { | |
5442 | 1317 octave_localtime now; |
2926 | 1318 |
5442 | 1319 if (c == 'd') |
1320 temp = now.strftime ("%a %b %d"); | |
1321 else if (c == 't') | |
1322 temp = now.strftime ("%H:%M:%S"); | |
1323 else if (c == 'T') | |
1324 temp = now.strftime ("%I:%M:%S"); | |
1325 else if (c == '@') | |
1326 temp = now.strftime ("%I:%M %p"); | |
1327 else if (c == 'A') | |
1328 temp = now.strftime ("%H:%M"); | |
2926 | 1329 |
1330 goto add_string; | |
1331 } | |
1332 | |
1333 case 'n': | |
1334 { | |
1335 temp = newline_chars (); | |
1336 | |
1337 goto add_string; | |
1338 } | |
1339 | |
1340 case 's': | |
1341 { | |
1342 temp = octave_env::get_program_name (); | |
1343 temp = octave_env::base_pathname (temp); | |
1344 | |
1345 goto add_string; | |
1346 } | |
5442 | 1347 |
2926 | 1348 case 'w': |
1349 case 'W': | |
1350 { | |
1351 temp = octave_env::getcwd (); | |
1352 | |
5442 | 1353 std::string home_dir = octave_env::get_home_directory (); |
1354 | |
1355 if (c == 'W' && (home_dir.empty () || temp != home_dir)) | |
2926 | 1356 { |
5442 | 1357 if (temp != "/" && temp != "//") |
1358 { | |
1359 size_t pos = temp.rfind ('/'); | |
2926 | 1360 |
5442 | 1361 if (pos != NPOS && pos != 0) |
1362 temp = temp.substr (pos + 1); | |
1363 } | |
2926 | 1364 } |
1365 else | |
1366 temp = octave_env::polite_directory_format (temp); | |
1367 | |
1368 goto add_string; | |
1369 } | |
5442 | 1370 |
2926 | 1371 case 'u': |
1372 { | |
1373 temp = octave_env::get_user_name (); | |
1374 | |
1375 goto add_string; | |
1376 } | |
1377 | |
1378 case 'H': | |
1379 { | |
1380 temp = octave_env::get_host_name (); | |
1381 | |
1382 goto add_string; | |
1383 } | |
1384 | |
1385 case 'h': | |
1386 { | |
1387 temp = octave_env::get_host_name (); | |
1388 | |
1389 size_t pos = temp.find ('.'); | |
1390 | |
1391 if (pos != NPOS) | |
1392 temp.resize (pos); | |
1393 | |
1394 goto add_string; | |
1395 } | |
1396 | |
1397 case '#': | |
1398 { | |
1399 char number_buffer[128]; | |
1400 sprintf (number_buffer, "%d", command_number); | |
1401 temp = number_buffer; | |
1402 | |
1403 goto add_string; | |
1404 } | |
1405 | |
1406 case '!': | |
1407 { | |
1408 char number_buffer[128]; | |
1409 int num = command_history::current_number (); | |
1410 if (num > 0) | |
1411 sprintf (number_buffer, "%d", num); | |
1412 else | |
1413 strcpy (number_buffer, "!"); | |
1414 temp = number_buffer; | |
1415 | |
1416 goto add_string; | |
1417 } | |
1418 | |
1419 case '$': | |
1420 { | |
4062 | 1421 #if defined (HAVE_GETEUID) |
2926 | 1422 temp = (::geteuid () == 0 ? "#" : "$"); |
4062 | 1423 #else |
1424 temp = "$"; | |
1425 #endif | |
2926 | 1426 |
1427 goto add_string; | |
1428 } | |
1429 | |
1430 #if defined (USE_READLINE) | |
1431 case '[': | |
1432 case ']': | |
1433 { | |
6940 | 1434 temp.resize (1); |
2926 | 1435 |
6940 | 1436 temp[0] = ((c == '[') |
3519 | 1437 ? ::octave_rl_prompt_start_ignore () |
1438 : ::octave_rl_prompt_end_ignore ()); | |
2926 | 1439 |
1440 goto add_string; | |
1441 } | |
1442 #endif | |
1443 | |
1444 case '\\': | |
1445 { | |
1446 temp = "\\"; | |
1447 | |
1448 goto add_string; | |
1449 } | |
1450 | |
1451 default: | |
1452 { | |
1453 temp = "\\ "; | |
1454 temp[1] = c; | |
1455 | |
1456 goto add_string; | |
1457 } | |
1458 | |
1459 add_string: | |
1460 { | |
1461 if (c) | |
1462 i++; | |
1463 | |
1464 result.append (temp); | |
1465 | |
1466 break; | |
1467 } | |
1468 } | |
1469 } | |
1470 else | |
1471 result += c; | |
1472 } | |
1473 | |
1474 return result; | |
1475 } | |
1476 | |
1477 // Return the octal number parsed from STRING, or -1 to indicate that | |
1478 // the string contained a bad number. | |
1479 | |
1480 int | |
3504 | 1481 command_editor::read_octal (const std::string& s) |
2926 | 1482 { |
1483 int result = 0; | |
1484 int digits = 0; | |
1485 | |
1486 size_t i = 0; | |
1487 size_t slen = s.length (); | |
1488 | |
1489 while (i < slen && s[i] >= '0' && s[i] < '8') | |
1490 { | |
1491 digits++; | |
1492 result = (result * 8) + s[i] - '0'; | |
1493 i++; | |
1494 } | |
1495 | |
1496 if (! digits || result > 0777 || i < slen) | |
1497 result = -1; | |
1498 | |
1499 return result; | |
1500 } | |
1501 | |
1502 void | |
1503 command_editor::error (int err_num) | |
1504 { | |
2941 | 1505 current_liboctave_error_handler ("%s", strerror (err_num)); |
2926 | 1506 } |
1507 | |
1508 void | |
3504 | 1509 command_editor::error (const std::string& s) |
2926 | 1510 { |
2941 | 1511 current_liboctave_error_handler ("%s", s.c_str ()); |
2926 | 1512 } |
1513 | |
1514 /* | |
1515 ;;; Local Variables: *** | |
1516 ;;; mode: C++ *** | |
1517 ;;; End: *** | |
1518 */ |