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