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