2926
|
1 /* |
|
2 |
|
3 Copyright (C) 1996, 1997 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cstring> |
|
28 #include <ctime> |
|
29 |
|
30 #include <string> |
|
31 |
|
32 #ifdef HAVE_UNISTD_H |
|
33 #ifdef HAVE_SYS_TYPES_H |
|
34 #include <sys/types.h> |
|
35 #endif |
|
36 #include <unistd.h> |
|
37 #endif |
|
38 |
|
39 #include "cmd-edit.h" |
|
40 #include "cmd-hist.h" |
|
41 #include "lo-error.h" |
|
42 #include "lo-utils.h" |
|
43 #include "oct-env.h" |
|
44 |
|
45 command_editor *command_editor::instance = 0; |
|
46 |
|
47 #if defined (USE_READLINE) |
|
48 |
|
49 #include <cstdio> |
|
50 #include <cstdlib> |
|
51 |
|
52 #include <readline/readline.h> |
|
53 |
|
54 // It would be nice if readline.h declared these, I think. |
|
55 |
|
56 extern int rl_blink_matching_paren; |
|
57 |
|
58 extern int screenheight; |
|
59 |
|
60 extern int screenwidth; |
|
61 |
|
62 class |
|
63 gnu_readline : public command_editor |
|
64 { |
|
65 public: |
|
66 |
|
67 typedef command_editor::fcn fcn; |
|
68 |
2941
|
69 typedef command_editor::completion_fcn completion_fcn; |
|
70 |
2926
|
71 gnu_readline (void); |
|
72 |
|
73 ~gnu_readline (void) { } |
|
74 |
|
75 void do_set_name (const string& n); |
|
76 |
|
77 string do_readline (const string& prompt); |
|
78 |
|
79 void do_set_input_stream (FILE *f); |
|
80 |
|
81 FILE *do_get_input_stream (void); |
|
82 |
|
83 void do_set_output_stream (FILE *f); |
|
84 |
|
85 FILE *do_get_output_stream (void); |
|
86 |
|
87 int do_terminal_rows (void); |
|
88 |
|
89 int do_terminal_cols (void); |
|
90 |
|
91 void do_clear_screen (void); |
|
92 |
|
93 string newline_chars (void); |
|
94 |
|
95 void do_restore_terminal_state (void); |
|
96 |
|
97 void do_blink_matching_paren (bool flag); |
|
98 |
3004
|
99 void do_set_basic_quote_characters (const string& s); |
2926
|
100 |
|
101 void do_set_completion_append_character (char c); |
|
102 |
2941
|
103 void do_set_completion_function (completion_fcn f); |
|
104 |
|
105 completion_fcn do_get_completion_function (void) const; |
2926
|
106 |
|
107 void do_insert_text (const string& text); |
|
108 |
|
109 void do_newline (void); |
|
110 |
|
111 void do_clear_undo_list (void); |
|
112 |
|
113 void do_set_startup_hook (fcn f); |
|
114 |
|
115 void do_restore_startup_hook (void); |
|
116 |
3215
|
117 void do_set_event_hook (fcn f); |
|
118 |
|
119 void do_restore_event_hook (void); |
|
120 |
3189
|
121 void do_read_init_file (const string& file); |
|
122 |
2926
|
123 static void operate_and_get_next (int, int); |
|
124 |
|
125 private: |
|
126 |
|
127 fcn previous_startup_hook; |
|
128 |
3215
|
129 fcn previous_event_hook; |
|
130 |
2941
|
131 completion_fcn completion_function; |
|
132 |
|
133 static char *command_generator (const char *text, int state); |
|
134 |
|
135 static char **command_completer (char *text, int start, int end); |
2926
|
136 }; |
|
137 |
|
138 gnu_readline::gnu_readline () |
3215
|
139 : command_editor (), previous_startup_hook (0), |
|
140 previous_event_hook (0), completion_function (0) |
2926
|
141 { |
|
142 rl_initialize (); |
|
143 |
|
144 do_blink_matching_paren (true); |
|
145 |
|
146 // Bind operate-and-get-next. |
|
147 |
|
148 rl_add_defun ("operate-and-get-next", |
|
149 gnu_readline::operate_and_get_next, CTRL ('O')); |
|
150 |
|
151 // And the history search functions. |
|
152 |
|
153 rl_add_defun ("history-search-backward", |
|
154 rl_history_search_backward, META ('p')); |
|
155 |
|
156 rl_add_defun ("history-search-forward", |
|
157 rl_history_search_forward, META ('n')); |
|
158 } |
|
159 |
|
160 void |
|
161 gnu_readline::do_set_name (const string& n) |
|
162 { |
|
163 static char *nm = 0; |
|
164 |
|
165 delete [] nm; |
|
166 |
|
167 nm = strsave (n.c_str ()); |
|
168 |
|
169 rl_readline_name = nm; |
|
170 |
|
171 // Since we've already called rl_initialize, we need to re-read the |
|
172 // init file to take advantage of the conditional parsing feature |
|
173 // based on rl_readline_name; |
|
174 |
|
175 rl_re_read_init_file (); |
|
176 } |
|
177 |
|
178 string |
|
179 gnu_readline::do_readline (const string& prompt) |
|
180 { |
|
181 string retval; |
|
182 |
|
183 char *line = ::readline (prompt.c_str ()); |
|
184 |
|
185 if (line) |
|
186 { |
|
187 retval = line; |
|
188 |
|
189 free (line); |
|
190 } |
|
191 |
|
192 return retval; |
|
193 } |
|
194 |
|
195 void |
|
196 gnu_readline::do_set_input_stream (FILE *f) |
|
197 { |
|
198 rl_instream = f; |
|
199 } |
|
200 |
|
201 FILE * |
|
202 gnu_readline::do_get_input_stream (void) |
|
203 { |
|
204 return rl_instream; |
|
205 } |
|
206 |
|
207 void |
|
208 gnu_readline::do_set_output_stream (FILE *f) |
|
209 { |
|
210 rl_outstream = f; |
|
211 } |
|
212 |
|
213 FILE * |
|
214 gnu_readline::do_get_output_stream (void) |
|
215 { |
|
216 return rl_outstream; |
|
217 } |
|
218 |
|
219 // GNU readline handles SIGWINCH, so these values have a good chance |
|
220 // of being correct even if the window changes size (they may be |
|
221 // wrong if, for example, the luser changes the window size while the |
|
222 // pager is running, and the signal is handled by the pager instead of |
|
223 // us. |
|
224 |
|
225 int |
|
226 gnu_readline::do_terminal_rows (void) |
|
227 { |
|
228 return screenheight > 0 ? screenheight : 24; |
|
229 } |
|
230 |
|
231 int |
|
232 gnu_readline::do_terminal_cols (void) |
|
233 { |
|
234 return screenwidth > 0 ? screenwidth : 80; |
|
235 } |
|
236 |
|
237 void |
|
238 gnu_readline::do_clear_screen (void) |
|
239 { |
|
240 rl_clear_screen (); |
|
241 } |
|
242 |
|
243 string |
|
244 gnu_readline::newline_chars (void) |
|
245 { |
|
246 return "\r\n"; |
|
247 } |
|
248 |
|
249 void |
|
250 gnu_readline::do_restore_terminal_state (void) |
|
251 { |
3004
|
252 if (rl_deprep_term_function) |
|
253 rl_deprep_term_function (); |
2926
|
254 } |
|
255 |
|
256 void |
|
257 gnu_readline::do_blink_matching_paren (bool flag) |
|
258 { |
|
259 rl_blink_matching_paren = flag ? 1 : 0; |
|
260 } |
|
261 |
|
262 void |
3004
|
263 gnu_readline::do_set_basic_quote_characters (const string& s) |
2926
|
264 { |
|
265 static char *ss = 0; |
|
266 |
|
267 delete [] ss; |
|
268 |
|
269 ss = strsave (s.c_str ()); |
|
270 |
3004
|
271 rl_basic_quote_characters = ss; |
2926
|
272 } |
|
273 |
|
274 void |
|
275 gnu_readline::do_set_completion_append_character (char c) |
|
276 { |
|
277 rl_completion_append_character = c; |
|
278 } |
|
279 |
|
280 void |
2941
|
281 gnu_readline::do_set_completion_function (completion_fcn f) |
2926
|
282 { |
2941
|
283 completion_function = f; |
|
284 |
|
285 typedef char** (*foo) (...); |
|
286 |
|
287 rl_attempted_completion_function |
|
288 = completion_function |
3145
|
289 ? reinterpret_cast<foo> (gnu_readline::command_completer) : 0; |
2941
|
290 } |
|
291 |
|
292 gnu_readline::completion_fcn |
|
293 gnu_readline::do_get_completion_function (void) const |
|
294 { |
|
295 return completion_function; |
2926
|
296 } |
|
297 |
|
298 void |
|
299 gnu_readline::do_insert_text (const string& text) |
|
300 { |
|
301 rl_insert_text (text.c_str ()); |
|
302 } |
|
303 |
|
304 void |
|
305 gnu_readline::do_newline (void) |
|
306 { |
|
307 rl_newline (); |
|
308 } |
|
309 |
|
310 void |
|
311 gnu_readline::do_clear_undo_list () |
|
312 { |
|
313 if (rl_undo_list) |
|
314 { |
|
315 free_undo_list (); |
|
316 |
|
317 rl_undo_list = 0; |
|
318 } |
|
319 } |
|
320 |
|
321 void |
|
322 gnu_readline::do_set_startup_hook (fcn f) |
|
323 { |
|
324 previous_startup_hook = rl_startup_hook; |
|
325 |
|
326 rl_startup_hook = f; |
|
327 } |
|
328 |
|
329 void |
|
330 gnu_readline::do_restore_startup_hook (void) |
|
331 { |
|
332 rl_startup_hook = previous_startup_hook; |
|
333 } |
|
334 |
|
335 void |
3215
|
336 gnu_readline::do_set_event_hook (fcn f) |
|
337 { |
|
338 previous_event_hook = rl_event_hook; |
|
339 |
|
340 rl_event_hook = f; |
|
341 } |
|
342 |
|
343 void |
|
344 gnu_readline::do_restore_event_hook (void) |
|
345 { |
|
346 rl_event_hook = previous_event_hook; |
|
347 } |
|
348 |
|
349 void |
3189
|
350 gnu_readline::do_read_init_file (const string& file) |
|
351 { |
|
352 if (file.empty ()) |
|
353 rl_re_read_init_file (); |
|
354 else |
|
355 rl_read_init_file (file.c_str ()); |
|
356 } |
|
357 |
|
358 void |
2926
|
359 gnu_readline::operate_and_get_next (int /* count */, int /* c */) |
|
360 { |
|
361 // Accept the current line. |
|
362 |
|
363 command_editor::newline (); |
|
364 |
|
365 // Find the current line, and find the next line to use. |
|
366 |
|
367 int x_where = command_history::where (); |
|
368 |
|
369 int x_length = command_history::length (); |
|
370 |
|
371 if ((command_history::is_stifled () |
|
372 && (x_length >= command_history::max_input_history ())) |
|
373 || (x_where >= x_length - 1)) |
|
374 command_history::set_mark (x_where); |
|
375 else |
|
376 command_history::set_mark (x_where + 1); |
|
377 |
|
378 command_editor::set_startup_hook (command_history::goto_mark); |
|
379 } |
|
380 |
2941
|
381 char * |
|
382 gnu_readline::command_generator (const char *text, int state) |
|
383 { |
|
384 char *retval = 0; |
|
385 |
|
386 completion_fcn f = command_editor::get_completion_function (); |
|
387 |
|
388 string tmp = f (text, state); |
|
389 |
|
390 size_t len = tmp.length (); |
|
391 |
|
392 if (len > 0) |
|
393 { |
|
394 retval = static_cast<char *> (malloc (len+1)); |
|
395 |
|
396 strcpy (retval, tmp.c_str ()); |
|
397 } |
|
398 |
|
399 return retval; |
|
400 } |
|
401 |
|
402 char ** |
|
403 gnu_readline::command_completer (char *text, int /* start */, int /* end */) |
|
404 { |
|
405 char **matches = 0; |
|
406 matches = completion_matches (text, gnu_readline::command_generator); |
|
407 return matches; |
|
408 } |
|
409 |
2926
|
410 #endif |
|
411 |
|
412 class |
|
413 default_command_editor : public command_editor |
|
414 { |
|
415 public: |
|
416 |
|
417 default_command_editor (void) |
|
418 : command_editor (), input_stream (stdin), output_stream (stdout) { } |
|
419 |
|
420 ~default_command_editor (void) { } |
|
421 |
|
422 string do_readline (const string& prompt); |
|
423 |
|
424 void do_set_input_stream (FILE *f); |
|
425 |
|
426 FILE *do_get_input_stream (void); |
|
427 |
|
428 void do_set_output_stream (FILE *f); |
|
429 |
|
430 FILE *do_get_output_stream (void); |
|
431 |
|
432 void do_insert_text (const string&); |
|
433 |
|
434 void do_newline (void); |
|
435 |
|
436 private: |
|
437 |
|
438 FILE *input_stream; |
|
439 |
|
440 FILE *output_stream; |
|
441 }; |
|
442 |
|
443 string |
|
444 default_command_editor::do_readline (const string& prompt) |
|
445 { |
|
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 |
|
477 default_command_editor::do_insert_text (const string&) |
|
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 |
|
518 command_editor::set_name (const string& n) |
|
519 { |
|
520 if (instance_ok ()) |
|
521 instance->do_set_name (n); |
|
522 } |
|
523 |
|
524 string |
|
525 command_editor::readline (const string& prompt) |
|
526 { |
|
527 return (instance_ok ()) |
|
528 ? instance->do_readline (prompt) : string (); |
|
529 } |
|
530 |
|
531 void |
|
532 command_editor::set_input_stream (FILE *f) |
|
533 { |
|
534 if (instance_ok ()) |
|
535 instance->do_set_input_stream (f); |
|
536 } |
|
537 |
|
538 FILE * |
|
539 command_editor::get_input_stream (void) |
|
540 { |
|
541 return (instance_ok ()) |
|
542 ? instance->do_get_input_stream () : 0; |
|
543 } |
|
544 |
|
545 void |
|
546 command_editor::set_output_stream (FILE *f) |
|
547 { |
|
548 if (instance_ok ()) |
|
549 instance->do_set_output_stream (f); |
|
550 } |
|
551 |
|
552 FILE * |
|
553 command_editor::get_output_stream (void) |
|
554 { |
|
555 return (instance_ok ()) |
|
556 ? instance->do_get_output_stream () : 0; |
|
557 } |
|
558 |
|
559 int |
|
560 command_editor::terminal_rows (void) |
|
561 { |
|
562 return (instance_ok ()) |
|
563 ? instance->do_terminal_rows () : -1; |
|
564 } |
|
565 |
|
566 int |
|
567 command_editor::terminal_cols (void) |
|
568 { |
|
569 return (instance_ok ()) |
|
570 ? instance->do_terminal_cols () : -1; |
|
571 } |
|
572 |
|
573 void |
|
574 command_editor::clear_screen (void) |
|
575 { |
|
576 if (instance_ok ()) |
|
577 instance->do_clear_screen (); |
|
578 } |
|
579 |
|
580 string |
|
581 command_editor::decode_prompt_string (const string& s) |
|
582 { |
|
583 return (instance_ok ()) |
|
584 ? instance->do_decode_prompt_string (s) : string (); |
|
585 } |
|
586 |
|
587 int |
|
588 command_editor::current_command_number (void) |
|
589 { |
|
590 return (instance_ok ()) |
|
591 ? instance->command_number : 0; |
|
592 } |
|
593 |
|
594 void |
|
595 command_editor::reset_current_command_number (int n) |
|
596 { |
|
597 if (instance_ok ()) |
|
598 instance->command_number = n; |
|
599 } |
|
600 |
|
601 void |
2967
|
602 command_editor::increment_current_command_number (void) |
|
603 { |
|
604 if (instance_ok ()) |
|
605 instance->command_number++; |
|
606 } |
|
607 |
|
608 void |
2926
|
609 command_editor::restore_terminal_state (void) |
|
610 { |
|
611 if (instance_ok ()) |
|
612 instance->do_restore_terminal_state (); |
|
613 } |
|
614 |
|
615 void |
|
616 command_editor::blink_matching_paren (bool flag) |
|
617 { |
|
618 if (instance_ok ()) |
|
619 instance->do_blink_matching_paren (flag); |
|
620 } |
|
621 |
|
622 void |
3004
|
623 command_editor::set_basic_quote_characters (const string& s) |
2926
|
624 { |
|
625 if (instance_ok ()) |
3004
|
626 instance->do_set_basic_quote_characters (s); |
2926
|
627 } |
|
628 |
|
629 void |
|
630 command_editor::set_completion_append_character (char c) |
|
631 { |
|
632 if (instance_ok ()) |
|
633 instance->do_set_completion_append_character (c); |
|
634 } |
|
635 |
|
636 void |
2941
|
637 command_editor::set_completion_function (completion_fcn f) |
2926
|
638 { |
|
639 if (instance_ok ()) |
2941
|
640 instance->do_set_completion_function (f); |
|
641 } |
|
642 |
|
643 command_editor::completion_fcn |
|
644 command_editor::get_completion_function (void) |
|
645 { |
|
646 return (instance_ok ()) |
|
647 ? instance->do_get_completion_function () : 0; |
2926
|
648 } |
|
649 |
|
650 void |
|
651 command_editor::insert_text (const string& text) |
|
652 { |
|
653 if (instance_ok ()) |
|
654 instance->do_insert_text (text); |
|
655 } |
|
656 |
|
657 void |
|
658 command_editor::newline (void) |
|
659 { |
|
660 if (instance_ok ()) |
|
661 instance->do_newline (); |
|
662 } |
|
663 |
|
664 void |
|
665 command_editor::clear_undo_list (void) |
|
666 { |
|
667 if (instance_ok ()) |
|
668 instance->do_clear_undo_list (); |
|
669 } |
|
670 |
|
671 void |
|
672 command_editor::set_startup_hook (fcn f) |
|
673 { |
|
674 if (instance_ok ()) |
|
675 instance->do_set_startup_hook (f); |
|
676 } |
|
677 |
|
678 void |
|
679 command_editor::restore_startup_hook (void) |
|
680 { |
|
681 if (instance_ok ()) |
|
682 instance->do_restore_startup_hook (); |
|
683 } |
|
684 |
3189
|
685 void |
3215
|
686 command_editor::set_event_hook (fcn f) |
|
687 { |
|
688 if (instance_ok ()) |
|
689 instance->do_set_event_hook (f); |
|
690 } |
|
691 |
|
692 void |
|
693 command_editor::restore_event_hook (void) |
|
694 { |
|
695 if (instance_ok ()) |
|
696 instance->do_restore_event_hook (); |
|
697 } |
|
698 |
|
699 void |
3189
|
700 command_editor::read_init_file (const string& file) |
|
701 { |
|
702 if (instance_ok ()) |
|
703 instance->do_read_init_file (file); |
|
704 } |
|
705 |
2926
|
706 // Return a string which will be printed as a prompt. The string may |
|
707 // contain special characters which are decoded as follows: |
|
708 // |
|
709 // \t the time |
|
710 // \d the date |
|
711 // \n CRLF |
|
712 // \s the name of the shell (program) |
|
713 // \w the current working directory |
|
714 // \W the last element of PWD |
|
715 // \u your username |
|
716 // \h the hostname |
|
717 // \# the command number of this command |
|
718 // \! the history number of this command |
|
719 // \$ a $ or a # if you are root |
|
720 // \<octal> character code in octal |
|
721 // \\ a backslash |
|
722 |
|
723 string |
|
724 command_editor::do_decode_prompt_string (const string& s) |
|
725 { |
|
726 string result; |
|
727 string temp; |
|
728 size_t i = 0; |
|
729 size_t slen = s.length (); |
|
730 int c; |
|
731 |
|
732 while (i < slen) |
|
733 { |
|
734 c = s[i]; |
|
735 |
|
736 i++; |
|
737 |
|
738 if (c == '\\') |
|
739 { |
|
740 c = s[i]; |
|
741 |
|
742 switch (c) |
|
743 { |
|
744 case '0': |
|
745 case '1': |
|
746 case '2': |
|
747 case '3': |
|
748 case '4': |
|
749 case '5': |
|
750 case '6': |
|
751 case '7': |
|
752 // Maybe convert an octal number. |
|
753 { |
|
754 int n = read_octal (s.substr (i, 3)); |
|
755 |
|
756 temp = "\\"; |
|
757 |
|
758 if (n != -1) |
|
759 { |
|
760 i += 3; |
|
761 temp[0] = n; |
|
762 } |
|
763 |
|
764 c = 0; |
|
765 goto add_string; |
|
766 } |
|
767 |
|
768 case 't': |
|
769 case 'd': |
|
770 // Make the current time/date into a string. |
|
771 { |
|
772 time_t now = time (0); |
|
773 |
|
774 temp = ctime (&now); |
|
775 |
|
776 if (c == 't') |
|
777 { |
|
778 temp = temp.substr (11); |
|
779 temp.resize (8); |
|
780 } |
|
781 else |
|
782 temp.resize (10); |
|
783 |
|
784 goto add_string; |
|
785 } |
|
786 |
|
787 case 'n': |
|
788 { |
|
789 temp = newline_chars (); |
|
790 |
|
791 goto add_string; |
|
792 } |
|
793 |
|
794 case 's': |
|
795 { |
|
796 temp = octave_env::get_program_name (); |
|
797 temp = octave_env::base_pathname (temp); |
|
798 |
|
799 goto add_string; |
|
800 } |
|
801 |
|
802 case 'w': |
|
803 case 'W': |
|
804 { |
|
805 temp = octave_env::getcwd (); |
|
806 |
|
807 if (c == 'W') |
|
808 { |
|
809 size_t pos = temp.rfind ('/'); |
|
810 |
|
811 if (pos != NPOS && pos != 0) |
|
812 temp = temp.substr (pos + 1); |
|
813 } |
|
814 else |
|
815 temp = octave_env::polite_directory_format (temp); |
|
816 |
|
817 goto add_string; |
|
818 } |
|
819 |
|
820 case 'u': |
|
821 { |
|
822 temp = octave_env::get_user_name (); |
|
823 |
|
824 goto add_string; |
|
825 } |
|
826 |
|
827 case 'H': |
|
828 { |
|
829 temp = octave_env::get_host_name (); |
|
830 |
|
831 goto add_string; |
|
832 } |
|
833 |
|
834 case 'h': |
|
835 { |
|
836 temp = octave_env::get_host_name (); |
|
837 |
|
838 size_t pos = temp.find ('.'); |
|
839 |
|
840 if (pos != NPOS) |
|
841 temp.resize (pos); |
|
842 |
|
843 goto add_string; |
|
844 } |
|
845 |
|
846 case '#': |
|
847 { |
|
848 char number_buffer[128]; |
|
849 sprintf (number_buffer, "%d", command_number); |
|
850 temp = number_buffer; |
|
851 |
|
852 goto add_string; |
|
853 } |
|
854 |
|
855 case '!': |
|
856 { |
|
857 char number_buffer[128]; |
|
858 int num = command_history::current_number (); |
|
859 if (num > 0) |
|
860 sprintf (number_buffer, "%d", num); |
|
861 else |
|
862 strcpy (number_buffer, "!"); |
|
863 temp = number_buffer; |
|
864 |
|
865 goto add_string; |
|
866 } |
|
867 |
|
868 case '$': |
|
869 { |
|
870 temp = (::geteuid () == 0 ? "#" : "$"); |
|
871 |
|
872 goto add_string; |
|
873 } |
|
874 |
|
875 #if defined (USE_READLINE) |
|
876 case '[': |
|
877 case ']': |
|
878 { |
|
879 temp.resize (2); |
|
880 |
|
881 temp[0] = '\001'; |
|
882 temp[1] = ((c == '[') |
|
883 ? RL_PROMPT_START_IGNORE |
|
884 : RL_PROMPT_END_IGNORE); |
|
885 |
|
886 goto add_string; |
|
887 } |
|
888 #endif |
|
889 |
|
890 case '\\': |
|
891 { |
|
892 temp = "\\"; |
|
893 |
|
894 goto add_string; |
|
895 } |
|
896 |
|
897 default: |
|
898 { |
|
899 temp = "\\ "; |
|
900 temp[1] = c; |
|
901 |
|
902 goto add_string; |
|
903 } |
|
904 |
|
905 add_string: |
|
906 { |
|
907 if (c) |
|
908 i++; |
|
909 |
|
910 result.append (temp); |
|
911 |
|
912 break; |
|
913 } |
|
914 } |
|
915 } |
|
916 else |
|
917 result += c; |
|
918 } |
|
919 |
|
920 return result; |
|
921 } |
|
922 |
|
923 // Return the octal number parsed from STRING, or -1 to indicate that |
|
924 // the string contained a bad number. |
|
925 |
|
926 int |
|
927 command_editor::read_octal (const string& s) |
|
928 { |
|
929 int result = 0; |
|
930 int digits = 0; |
|
931 |
|
932 size_t i = 0; |
|
933 size_t slen = s.length (); |
|
934 |
|
935 while (i < slen && s[i] >= '0' && s[i] < '8') |
|
936 { |
|
937 digits++; |
|
938 result = (result * 8) + s[i] - '0'; |
|
939 i++; |
|
940 } |
|
941 |
|
942 if (! digits || result > 0777 || i < slen) |
|
943 result = -1; |
|
944 |
|
945 return result; |
|
946 } |
|
947 |
|
948 void |
|
949 command_editor::error (int err_num) |
|
950 { |
2941
|
951 current_liboctave_error_handler ("%s", strerror (err_num)); |
2926
|
952 } |
|
953 |
|
954 void |
|
955 command_editor::error (const string& s) |
|
956 { |
2941
|
957 current_liboctave_error_handler ("%s", s.c_str ()); |
2926
|
958 } |
|
959 |
|
960 /* |
|
961 ;;; Local Variables: *** |
|
962 ;;; mode: C++ *** |
|
963 ;;; End: *** |
|
964 */ |