メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

cmd-edit.cc

解説を見る。
00001 /*
00002 
00003 Copyright (C) 1996, 1997 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 2, or (at your option) any
00010 later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, write to the Free
00019 Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include <cstdlib>
00028 #include <cstring>
00029 
00030 #include <string>
00031 
00032 #ifdef HAVE_UNISTD_H
00033 #ifdef HAVE_SYS_TYPES_H
00034 #include <sys/types.h>
00035 #endif
00036 #include <unistd.h>
00037 #endif
00038 
00039 #include "cmd-edit.h"
00040 #include "cmd-hist.h"
00041 #include "lo-error.h"
00042 #include "lo-utils.h"
00043 #include "oct-env.h"
00044 #include "oct-time.h"
00045 
00046 command_editor *command_editor::instance = 0;
00047 
00048 #if defined (USE_READLINE)
00049 
00050 #include <cstdio>
00051 #include <cstdlib>
00052 
00053 #include "oct-rl-edit.h"
00054 
00055 class
00056 gnu_readline : public command_editor
00057 {
00058 public:
00059 
00060   typedef command_editor::startup_hook_fcn startup_hook_fcn;
00061 
00062   typedef command_editor::event_hook_fcn event_hook_hook_fcn;
00063 
00064   typedef command_editor::completion_fcn completion_fcn;
00065 
00066   gnu_readline (void);
00067 
00068   ~gnu_readline (void) { }
00069 
00070   void do_set_name (const std::string& n);
00071 
00072   std::string do_readline (const std::string& prompt, bool& eof);
00073 
00074   void do_set_input_stream (FILE *f);
00075 
00076   FILE *do_get_input_stream (void);
00077 
00078   void do_set_output_stream (FILE *f);
00079 
00080   FILE *do_get_output_stream (void);
00081 
00082   int do_terminal_rows (void);
00083 
00084   int do_terminal_cols (void);
00085 
00086   void do_clear_screen (void);
00087 
00088   void do_resize_terminal (void);
00089 
00090   std::string newline_chars (void);
00091 
00092   void do_restore_terminal_state (void);
00093 
00094   void do_blink_matching_paren (bool flag);
00095 
00096   void do_set_basic_word_break_characters (const std::string& s);
00097 
00098   void do_set_completer_word_break_characters (const std::string& s);
00099 
00100   void do_set_basic_quote_characters (const std::string& s);
00101 
00102   void do_set_completion_append_character (char c);
00103 
00104   void do_set_completion_function (completion_fcn f);
00105 
00106   completion_fcn do_get_completion_function (void) const;
00107 
00108   string_vector
00109   do_generate_filename_completions (const std::string& text);
00110 
00111   void do_insert_text (const std::string& text);
00112 
00113   void do_newline (void);
00114 
00115   void do_clear_undo_list (void);
00116 
00117   void do_set_startup_hook (startup_hook_fcn f);
00118 
00119   void do_restore_startup_hook (void);
00120 
00121   void do_set_event_hook (event_hook_fcn f);
00122 
00123   void do_restore_event_hook (void);
00124 
00125   void do_read_init_file (const std::string& file);
00126 
00127   bool do_filename_completion_desired (bool);
00128 
00129   static int operate_and_get_next (int, int);
00130 
00131   static int history_search_backward (int, int);
00132 
00133   static int history_search_forward (int, int);
00134 
00135 private:
00136 
00137   startup_hook_fcn previous_startup_hook;
00138 
00139   event_hook_fcn previous_event_hook;
00140 
00141   completion_fcn completion_function;
00142 
00143   static char *command_generator (const char *text, int state);
00144 
00145   static char **command_completer (const char *text, int start, int end);
00146 };
00147 
00148 gnu_readline::gnu_readline ()
00149   : command_editor (), previous_startup_hook (0),
00150     previous_event_hook (0), completion_function (0)
00151 {
00152   // XXX FIXME XXX -- need interface to rl_add_defun, rl_initialize, and
00153   // a function to set rl_terminal_name
00154 
00155   std::string term = octave_env::getenv ("TERM");
00156 
00157   octave_rl_set_terminal_name (term.c_str ());
00158 
00159   octave_rl_initialize ();
00160 
00161   do_blink_matching_paren (true);
00162 
00163   /* Bind operate-and-get-next. */
00164 
00165   octave_rl_add_defun ("operate-and-get-next",
00166                        gnu_readline::operate_and_get_next,
00167                        octave_rl_ctrl ('O'));
00168 
00169   /* And the history search functions. */
00170 
00171   octave_rl_add_defun ("history-search-backward",
00172                        gnu_readline::history_search_backward,
00173                        octave_rl_meta ('P'));
00174 
00175   octave_rl_add_defun ("history-search-forward",
00176                        gnu_readline::history_search_forward,
00177                        octave_rl_meta ('N'));
00178 }
00179 
00180 
00181 
00182 void
00183 gnu_readline::do_set_name (const std::string& nm)
00184 {
00185   ::octave_rl_set_name (nm.c_str ());
00186 }
00187 
00188 std::string
00189 gnu_readline::do_readline (const std::string& prompt, bool& eof)
00190 {
00191   std::string retval;
00192 
00193   eof = false;
00194 
00195   char *line = ::octave_rl_readline (prompt.c_str ());
00196 
00197   if (line)
00198     {
00199       retval = line;
00200 
00201       free (line);
00202     }
00203   else
00204     eof = true;
00205 
00206   return retval;
00207 }
00208 
00209 void
00210 gnu_readline::do_set_input_stream (FILE *f)
00211 {
00212   ::octave_rl_set_input_stream (f);
00213 }
00214 
00215 FILE *
00216 gnu_readline::do_get_input_stream (void)
00217 {
00218   return ::octave_rl_get_input_stream ();
00219 }
00220 
00221 void
00222 gnu_readline::do_set_output_stream (FILE *f)
00223 {
00224   ::octave_rl_set_output_stream (f);
00225 }
00226 
00227 FILE *
00228 gnu_readline::do_get_output_stream (void)
00229 {
00230   return ::octave_rl_get_output_stream ();
00231 }
00232 
00233 // GNU readline handles SIGWINCH, so these values have a good chance
00234 // of being correct even if the window changes size (they may be
00235 // wrong if, for example, the luser changes the window size while the
00236 // pager is running, and the signal is handled by the pager instead of
00237 // us.
00238 
00239 int
00240 gnu_readline::do_terminal_rows (void)
00241 {
00242   int sh = ::octave_rl_screen_height ();
00243 
00244   return sh > 0 ? sh : 24;
00245 }
00246 
00247 int
00248 gnu_readline::do_terminal_cols (void)
00249 {
00250   int sw = ::octave_rl_screen_width ();
00251 
00252   return sw > 0 ? sw : 80;
00253 }
00254 
00255 void
00256 gnu_readline::do_clear_screen (void)
00257 {
00258   ::octave_rl_clear_screen ();
00259 }
00260 
00261 void
00262 gnu_readline::do_resize_terminal (void)
00263 {
00264   ::octave_rl_resize_terminal ();
00265 }
00266 
00267 std::string
00268 gnu_readline::newline_chars (void)
00269 {
00270   return "\r\n";
00271 }
00272 
00273 void
00274 gnu_readline::do_restore_terminal_state (void)
00275 {
00276   ::octave_rl_restore_terminal_state ();
00277 }
00278 
00279 void
00280 gnu_readline::do_blink_matching_paren (bool flag)
00281 {
00282   ::octave_rl_enable_paren_matching (flag ? 1 : 0);
00283 }
00284 
00285 void
00286 gnu_readline::do_set_basic_word_break_characters (const std::string& s)
00287 {
00288   ::octave_rl_set_basic_word_break_characters (s.c_str ());
00289 }
00290 
00291 void
00292 gnu_readline::do_set_completer_word_break_characters (const std::string& s)
00293 {
00294   ::octave_rl_set_completer_word_break_characters (s.c_str ());
00295 }
00296 
00297 void
00298 gnu_readline::do_set_basic_quote_characters (const std::string& s)
00299 {
00300   ::octave_rl_set_basic_quote_characters (s.c_str ());
00301 }
00302 
00303 void
00304 gnu_readline::do_set_completion_append_character (char c)
00305 {
00306   ::octave_rl_set_completion_append_character (c);
00307 }
00308 
00309 void
00310 gnu_readline::do_set_completion_function (completion_fcn f)
00311 {
00312   completion_function = f;
00313 
00314   rl_attempted_completion_fcn_ptr fp
00315     = f ? gnu_readline::command_completer : 0;
00316 
00317   ::octave_rl_set_completion_function (fp);
00318 }
00319 
00320 gnu_readline::completion_fcn
00321 gnu_readline::do_get_completion_function (void) const
00322 {
00323   return completion_function;
00324 }
00325 
00326 string_vector
00327 gnu_readline::do_generate_filename_completions (const std::string& text)
00328 {
00329   string_vector retval;
00330 
00331   int n = 0;
00332   int count = 0;
00333 
00334   char *fn = 0;
00335 
00336   while (1)
00337     {
00338       fn = ::octave_rl_filename_completion_function (text.c_str (), count);
00339 
00340       if (fn)
00341         {
00342           if (count == n)
00343             {
00344               // Famous last words:  Most large directories will not
00345               // have more than a few hundred files, so we should not
00346               // resize too many times even if the growth is linear...
00347 
00348               n += 100;
00349               retval.resize (n);
00350             }
00351 
00352           retval[count++] = fn;
00353 
00354           free (fn);
00355         }
00356       else
00357         break;
00358     }
00359 
00360   retval.resize (count);
00361 
00362   return retval;
00363 }
00364 
00365 void
00366 gnu_readline::do_insert_text (const std::string& text)
00367 {
00368   ::octave_rl_insert_text (text.c_str ());
00369 }
00370 
00371 void
00372 gnu_readline::do_newline (void)
00373 {
00374   ::octave_rl_newline ();
00375 }
00376 
00377 void
00378 gnu_readline::do_clear_undo_list ()
00379 {
00380   ::octave_rl_clear_undo_list ();
00381 }
00382 
00383 void
00384 gnu_readline::do_set_startup_hook (startup_hook_fcn f)
00385 {
00386   previous_startup_hook = ::octave_rl_get_startup_hook ();
00387 
00388   ::octave_rl_set_startup_hook (f);
00389 }
00390 
00391 void
00392 gnu_readline::do_restore_startup_hook (void)
00393 {
00394   ::octave_rl_set_startup_hook (previous_startup_hook);
00395 }
00396 
00397 void
00398 gnu_readline::do_set_event_hook (event_hook_fcn f)
00399 {
00400   previous_event_hook = octave_rl_get_event_hook ();
00401 
00402   ::octave_rl_set_event_hook (f);
00403 }
00404 
00405 void
00406 gnu_readline::do_restore_event_hook (void)
00407 {
00408   ::octave_rl_set_event_hook (previous_event_hook);
00409 }
00410 
00411 void
00412 gnu_readline::do_read_init_file (const std::string& file)
00413 {
00414   ::octave_rl_read_init_file (file.c_str ());
00415 }
00416 
00417 bool
00418 gnu_readline::do_filename_completion_desired (bool arg)
00419 {
00420   return ::octave_rl_filename_completion_desired (arg);
00421 }
00422 
00423 int
00424 gnu_readline::operate_and_get_next (int /* count */, int /* c */)
00425 {
00426   // Accept the current line.
00427 
00428   command_editor::newline ();
00429 
00430   // Find the current line, and find the next line to use.
00431 
00432   int x_where = command_history::where ();
00433 
00434   int x_length = command_history::length ();
00435 
00436   if ((command_history::is_stifled ()
00437        && (x_length >= command_history::max_input_history ()))
00438       || (x_where >= x_length - 1))
00439     command_history::set_mark (x_where);
00440   else
00441     command_history::set_mark (x_where + 1);
00442 
00443   command_editor::set_startup_hook (command_history::goto_mark);
00444 
00445   return 0;
00446 }
00447 
00448 int
00449 gnu_readline::history_search_backward (int count, int c)
00450 {
00451   return octave_rl_history_search_backward (count, c);
00452 }
00453 
00454 int
00455 gnu_readline::history_search_forward (int count, int c)
00456 {
00457   return octave_rl_history_search_forward (count, c);
00458 }
00459 
00460 char *
00461 gnu_readline::command_generator (const char *text, int state)
00462 {
00463   char *retval = 0;
00464 
00465   completion_fcn f = command_editor::get_completion_function ();
00466 
00467   std::string tmp = f (text, state);
00468 
00469   size_t len = tmp.length ();
00470 
00471   if (len > 0)
00472     {
00473       retval = static_cast<char *> (malloc (len+1));
00474 
00475       strcpy (retval, tmp.c_str ());
00476     }
00477 
00478   return retval;
00479 }
00480 
00481 char **
00482 gnu_readline::command_completer (const char *text, int, int)
00483 {
00484   char **matches = 0;
00485   matches
00486     = ::octave_rl_completion_matches (text, gnu_readline::command_generator);
00487   return matches;
00488 }
00489 
00490 #endif
00491 
00492 class
00493 default_command_editor : public command_editor
00494 {
00495 public:
00496 
00497   default_command_editor (void)
00498     : command_editor (), input_stream (stdin), output_stream (stdout) { }
00499 
00500   ~default_command_editor (void) { }
00501 
00502   std::string do_readline (const std::string& prompt, bool& eof);
00503 
00504   void do_set_input_stream (FILE *f);
00505 
00506   FILE *do_get_input_stream (void);
00507 
00508   void do_set_output_stream (FILE *f);
00509 
00510   FILE *do_get_output_stream (void);
00511 
00512   string_vector do_generate_filename_completions (const std::string& text);
00513 
00514   void do_insert_text (const std::string&);
00515 
00516   void do_newline (void);
00517 
00518 private:
00519 
00520   FILE *input_stream;
00521 
00522   FILE *output_stream;
00523 };
00524 
00525 std::string
00526 default_command_editor::do_readline (const std::string& prompt, bool& eof)
00527 {
00528   fprintf (output_stream, prompt.c_str ());
00529   fflush (output_stream);
00530 
00531   return octave_fgetl (input_stream, eof);
00532 }
00533 
00534 void
00535 default_command_editor::do_set_input_stream (FILE *f)
00536 {
00537   input_stream = f;
00538 }
00539 
00540 FILE *
00541 default_command_editor::do_get_input_stream (void)
00542 {
00543   return input_stream;
00544 }
00545 
00546 void
00547 default_command_editor::do_set_output_stream (FILE *f)
00548 {
00549   output_stream = f;
00550 }
00551 
00552 FILE *
00553 default_command_editor::do_get_output_stream (void)
00554 {
00555   return output_stream;
00556 }
00557 
00558 string_vector
00559 default_command_editor::do_generate_filename_completions (const std::string&)
00560 {
00561   // XXX FIXME XXX
00562   return string_vector ();
00563 }
00564 
00565 void
00566 default_command_editor::do_insert_text (const std::string&)
00567 {
00568   // XXX FIXME XXX
00569 }
00570 
00571 void
00572 default_command_editor::do_newline (void)
00573 {
00574   // XXX FIXME XXX
00575 }
00576 
00577 bool
00578 command_editor::instance_ok (void)
00579 {
00580   bool retval = true;
00581 
00582   if (! instance)
00583     make_command_editor ();
00584 
00585   if (! instance)
00586     {
00587       current_liboctave_error_handler
00588         ("unable to create command history object!");
00589 
00590       retval = false;
00591     }
00592 
00593   return retval;
00594 }
00595 
00596 void
00597 command_editor::make_command_editor (void)
00598 {
00599 #if defined (USE_READLINE)
00600   instance = new gnu_readline ();
00601 #else
00602   instance = new default_command_editor ();
00603 #endif
00604 }
00605 
00606 void
00607 command_editor::set_name (const std::string& n)
00608 {
00609   if (instance_ok ())
00610     instance->do_set_name (n);
00611 }
00612 
00613 std::string
00614 command_editor::readline (const std::string& prompt)
00615 {
00616   bool eof;
00617 
00618   return readline (prompt, eof);
00619 }
00620 
00621 std::string
00622 command_editor::readline (const std::string& prompt, bool& eof)
00623 {
00624   return (instance_ok ())
00625     ? instance->do_readline (prompt, eof) : std::string ();
00626 }
00627 
00628 void
00629 command_editor::set_input_stream (FILE *f)
00630 {
00631   if (instance_ok ())
00632     instance->do_set_input_stream (f);
00633 }
00634 
00635 FILE *
00636 command_editor::get_input_stream (void)
00637 {
00638   return (instance_ok ())
00639     ? instance->do_get_input_stream () : 0;
00640 }
00641 
00642 void
00643 command_editor::set_output_stream (FILE *f)
00644 {
00645   if (instance_ok ())
00646     instance->do_set_output_stream (f);
00647 }
00648 
00649 FILE *
00650 command_editor::get_output_stream (void)
00651 {
00652   return (instance_ok ())
00653     ? instance->do_get_output_stream () : 0;
00654 }
00655 
00656 int
00657 command_editor::terminal_rows (void)
00658 {
00659   return (instance_ok ())
00660     ? instance->do_terminal_rows () : -1;
00661 }
00662 
00663 int
00664 command_editor::terminal_cols (void)
00665 {
00666   return (instance_ok ())
00667     ? instance->do_terminal_cols () : -1;
00668 }
00669 
00670 void
00671 command_editor::clear_screen (void)
00672 {
00673   if (instance_ok ())
00674     instance->do_clear_screen ();
00675 }
00676 
00677 void
00678 command_editor::resize_terminal (void)
00679 {
00680   if (instance_ok ())
00681     instance->do_resize_terminal ();
00682 }
00683 
00684 std::string
00685 command_editor::decode_prompt_string (const std::string& s)
00686 {
00687   return (instance_ok ())
00688     ? instance->do_decode_prompt_string (s) : std::string ();
00689 }
00690 
00691 int
00692 command_editor::current_command_number (void)
00693 {
00694   return (instance_ok ())
00695     ? instance->command_number : 0;
00696 }
00697 
00698 void
00699 command_editor::reset_current_command_number (int n)
00700 {
00701   if (instance_ok ())
00702     instance->command_number = n;
00703 }
00704 
00705 void
00706 command_editor::increment_current_command_number (void)
00707 {
00708   if (instance_ok ())
00709     instance->command_number++;
00710 }
00711 
00712 void
00713 command_editor::restore_terminal_state (void)
00714 {
00715   if (instance_ok ())
00716     instance->do_restore_terminal_state ();
00717 }
00718 
00719 void
00720 command_editor::blink_matching_paren (bool flag)
00721 {
00722   if (instance_ok ())
00723     instance->do_blink_matching_paren (flag);
00724 }
00725 
00726 void
00727 command_editor::set_basic_word_break_characters (const std::string& s)
00728 {
00729   if (instance_ok ())
00730     instance->do_set_basic_word_break_characters (s);
00731 }
00732 
00733 void
00734 command_editor::set_completer_word_break_characters (const std::string& s)
00735 {
00736   if (instance_ok ())
00737     instance->do_set_completer_word_break_characters (s);
00738 }
00739 
00740 void
00741 command_editor::set_basic_quote_characters (const std::string& s)
00742 {
00743   if (instance_ok ())
00744     instance->do_set_basic_quote_characters (s);
00745 }
00746 
00747 void
00748 command_editor::set_completion_append_character (char c)
00749 {
00750   if (instance_ok ())
00751     instance->do_set_completion_append_character (c);
00752 }
00753 
00754 void
00755 command_editor::set_completion_function (completion_fcn f)
00756 {
00757   if (instance_ok ())
00758     instance->do_set_completion_function (f);
00759 }
00760 
00761 command_editor::completion_fcn
00762 command_editor::get_completion_function (void)
00763 {
00764   return (instance_ok ())
00765     ? instance->do_get_completion_function () : 0;
00766 }
00767 
00768 string_vector
00769 command_editor::generate_filename_completions (const std::string& text)
00770 {
00771   return (instance_ok ())
00772     ? instance->do_generate_filename_completions (text) : string_vector ();
00773 }
00774 
00775 void
00776 command_editor::insert_text (const std::string& text)
00777 {
00778   if (instance_ok ())
00779     instance->do_insert_text (text);
00780 }
00781 
00782 void
00783 command_editor::newline (void)
00784 {
00785   if (instance_ok ())
00786     instance->do_newline ();
00787 }
00788 
00789 void
00790 command_editor::clear_undo_list (void)
00791 {
00792   if (instance_ok ())
00793     instance->do_clear_undo_list ();
00794 }
00795 
00796 void
00797 command_editor::set_startup_hook (startup_hook_fcn f)
00798 {
00799   if (instance_ok ())
00800     instance->do_set_startup_hook (f);
00801 }
00802 
00803 void
00804 command_editor::restore_startup_hook (void)
00805 {
00806   if (instance_ok ())
00807     instance->do_restore_startup_hook ();
00808 }
00809 
00810 void
00811 command_editor::set_event_hook (event_hook_fcn f)
00812 {
00813   if (instance_ok ())
00814     instance->do_set_event_hook (f);
00815 }
00816 
00817 void
00818 command_editor::restore_event_hook (void)
00819 {
00820   if (instance_ok ())
00821     instance->do_restore_event_hook ();
00822 }
00823 
00824 void
00825 command_editor::read_init_file (const std::string& file)
00826 {
00827   if (instance_ok ())
00828     instance->do_read_init_file (file);
00829 }
00830 
00831 bool
00832 command_editor::filename_completion_desired (bool arg)
00833 {
00834   return (instance_ok ())
00835     ? instance->do_filename_completion_desired (arg) : false;
00836 }
00837 
00838 // Return a string which will be printed as a prompt.  The string may
00839 // contain special characters which are decoded as follows: 
00840 //   
00841 //      \t      the time
00842 //      \d      the date
00843 //      \n      CRLF
00844 //      \s      the name of the shell (program)
00845 //      \w      the current working directory
00846 //      \W      the last element of PWD
00847 //      \u      your username
00848 //      \h      the hostname
00849 //      \#      the command number of this command
00850 //      \!      the history number of this command
00851 //      \$      a $ or a # if you are root
00852 //      <octal> character code in octal
00853 //      \\      a backslash
00854 
00855 std::string
00856 command_editor::do_decode_prompt_string (const std::string& s)
00857 {
00858   std::string result;
00859   std::string temp;
00860   size_t i = 0;
00861   size_t slen = s.length ();
00862   int c;
00863 
00864   while (i < slen)
00865     {
00866       c = s[i];
00867 
00868       i++;
00869 
00870       if (c == '\\')
00871         {
00872           c = s[i];
00873 
00874           switch (c)
00875             {
00876             case '0':
00877             case '1':
00878             case '2':
00879             case '3':
00880             case '4':
00881             case '5':
00882             case '6':
00883             case '7':
00884               // Maybe convert an octal number.
00885               {
00886                 int n = read_octal (s.substr (i, 3));
00887 
00888                 temp = "\\";
00889 
00890                 if (n != -1)
00891                   {
00892                     i += 3;
00893                     temp[0] = n;
00894                   }
00895 
00896                 c = 0;
00897                 goto add_string;
00898               }
00899           
00900             case 't':
00901             case 'd':
00902               // Make the current time/date into a string.
00903               {
00904                 octave_time now;
00905 
00906                 temp = now.ctime ();
00907 
00908                 if (c == 't')
00909                   {
00910                     temp = temp.substr (11);
00911                     temp.resize (8);
00912                   }
00913                 else
00914                   temp.resize (10);
00915 
00916                 goto add_string;
00917               }
00918 
00919             case 'n':
00920               {
00921                 temp = newline_chars ();
00922 
00923                 goto add_string;
00924               }
00925 
00926             case 's':
00927               {
00928                 temp = octave_env::get_program_name ();
00929                 temp = octave_env::base_pathname (temp);
00930 
00931                 goto add_string;
00932               }
00933         
00934             case 'w':
00935             case 'W':
00936               {
00937                 temp = octave_env::getcwd ();
00938 
00939                 if (c == 'W')
00940                   {
00941                     size_t pos = temp.rfind ('/');
00942 
00943                     if (pos != NPOS && pos != 0)
00944                       temp = temp.substr (pos + 1);
00945                   }
00946                 else
00947                   temp = octave_env::polite_directory_format (temp);
00948 
00949                 goto add_string;
00950               }
00951       
00952             case 'u':
00953               {
00954                 temp = octave_env::get_user_name ();
00955 
00956                 goto add_string;
00957               }
00958 
00959             case 'H':
00960               {
00961                 temp = octave_env::get_host_name ();
00962 
00963                 goto add_string;
00964               }
00965 
00966             case 'h':
00967               {
00968                 temp = octave_env::get_host_name ();
00969 
00970                 size_t pos = temp.find ('.');
00971 
00972                 if (pos != NPOS)
00973                   temp.resize (pos);
00974                 
00975                 goto add_string;
00976               }
00977 
00978             case '#':
00979               {
00980                 char number_buffer[128];
00981                 sprintf (number_buffer, "%d", command_number);
00982                 temp = number_buffer;
00983 
00984                 goto add_string;
00985               }
00986 
00987             case '!':
00988               {
00989                 char number_buffer[128];
00990                 int num = command_history::current_number ();
00991                 if (num > 0)
00992                   sprintf (number_buffer, "%d", num);
00993                 else
00994                   strcpy (number_buffer, "!");
00995                 temp = number_buffer;
00996 
00997                 goto add_string;
00998               }
00999 
01000             case '$':
01001               {
01002 #if defined (HAVE_GETEUID)
01003                 temp = (::geteuid () == 0 ? "#" : "$");
01004 #else
01005                 temp = "$";
01006 #endif
01007 
01008                 goto add_string;
01009               }
01010 
01011 #if defined (USE_READLINE)
01012             case '[':
01013             case ']':
01014               {
01015                 temp.resize (2);
01016 
01017                 temp[0] = '\001';
01018                 temp[1] = ((c == '[')
01019                            ? ::octave_rl_prompt_start_ignore ()
01020                            : ::octave_rl_prompt_end_ignore ());
01021 
01022                 goto add_string;
01023               }
01024 #endif
01025 
01026             case '\\':
01027               {
01028                 temp = "\\";
01029 
01030                 goto add_string;
01031               }
01032 
01033             default:
01034               {
01035                 temp = "\\ ";
01036                 temp[1] = c;
01037 
01038                 goto add_string;
01039               }
01040 
01041             add_string:
01042               {
01043                 if (c)
01044                   i++;
01045 
01046                 result.append (temp);
01047 
01048                 break;
01049               }
01050             }
01051         }
01052       else
01053         result += c;
01054     }
01055 
01056   return result;
01057 }
01058 
01059 // Return the octal number parsed from STRING, or -1 to indicate that
01060 // the string contained a bad number.
01061 
01062 int
01063 command_editor::read_octal (const std::string& s)
01064 {
01065   int result = 0;
01066   int digits = 0;
01067 
01068   size_t i = 0;
01069   size_t slen = s.length ();
01070 
01071   while (i < slen && s[i] >= '0' && s[i] < '8')
01072     {
01073       digits++;
01074       result = (result * 8) + s[i] - '0';
01075       i++;
01076     }
01077 
01078   if (! digits || result > 0777 || i < slen)
01079     result = -1;
01080 
01081   return result;
01082 }
01083 
01084 void
01085 command_editor::error (int err_num)
01086 {
01087   current_liboctave_error_handler ("%s", strerror (err_num));
01088 }
01089 
01090 void
01091 command_editor::error (const std::string& s)
01092 {
01093   current_liboctave_error_handler ("%s", s.c_str ());
01094 }
01095 
01096 /*
01097 ;;; Local Variables: ***
01098 ;;; mode: C++ ***
01099 ;;; End: ***
01100 */

Wed Dec 29 11:51:01 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送