octave.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1993-2012 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 3 of the License, or (at your
00010 option) any 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, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 // Born February 20, 1992.
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028 
00029 #include <cassert>
00030 #include <cstdlib>
00031 #include <cstring>
00032 #include <ctime>
00033 
00034 #include <iostream>
00035 
00036 #include <getopt.h>
00037 #include <sys/types.h>
00038 #include <unistd.h>
00039 
00040 #include "cmd-edit.h"
00041 #include "f77-fcn.h"
00042 #include "file-ops.h"
00043 #include "file-stat.h"
00044 #include "lo-error.h"
00045 #include "oct-env.h"
00046 #include "pathsearch.h"
00047 #include "str-vec.h"
00048 
00049 #include <defaults.h>
00050 #include "Cell.h"
00051 #include "defun.h"
00052 #include "display.h"
00053 #include "error.h"
00054 #include "file-io.h"
00055 #include "input.h"
00056 #include "lex.h"
00057 #include "load-path.h"
00058 #include "octave.h"
00059 #include "oct-hist.h"
00060 #include "oct-map.h"
00061 #include "oct-mutex.h"
00062 #include "oct-obj.h"
00063 #include "ops.h"
00064 #include "ov.h"
00065 #include "ov-range.h"
00066 #include "toplev.h"
00067 #include "parse.h"
00068 #include "procstream.h"
00069 #include "sighandlers.h"
00070 #include "sysdep.h"
00071 #include "unwind-prot.h"
00072 #include "utils.h"
00073 #include "variables.h"
00074 #include <version.h>
00075 
00076 // Kluge.
00077 extern "C" F77_RET_T
00078 F77_FUNC (xerbla, XERBLA) (F77_CONST_CHAR_ARG_DECL,
00079                            const octave_idx_type&
00080                            F77_CHAR_ARG_LEN_DECL);
00081 
00082 extern void install_builtins (void);
00083 
00084 // The command-line options.
00085 static string_vector octave_argv;
00086 
00087 // The name used to invoke Octave.
00088 static std::string
00089 octave_program_invocation_name;
00090 
00091 // The last component of octave_program_invocation_name.
00092 static std::string octave_program_name;
00093 
00094 // TRUE means we read ~/.octaverc and ./.octaverc.
00095 // (--norc; --no-init-file; -f)
00096 static bool read_init_files = true;
00097 
00098 // TRUE means we read the site-wide octaverc files.
00099 // (--norc; --no-site-file; -f)
00100 static bool read_site_files = true;
00101 
00102 // TRUE means we set the initial path to configured defaults.
00103 // (--no-init-path)
00104 static bool set_initial_path = true;
00105 
00106 // TRUE means we don't print the usual startup message.
00107 // (--quiet; --silent; -q)
00108 static bool inhibit_startup_message = false;
00109 
00110 // TRUE means we turn on compatibility options.
00111 // (--traditional)
00112 static bool traditional = false;
00113 
00114 // If TRUE, print verbose info in some cases.
00115 // (--verbose; -V)
00116 static bool verbose_flag = false;
00117 
00118 // Usage message
00119 static const char *usage_string =
00120   "octave [-HVdfhiqvx] [--debug] [--echo-commands] [--eval CODE]\n\
00121        [--exec-path path] [--help] [--image-path path] [--info-file file]\n\
00122        [--info-program prog] [--interactive] [--line-editing]\n\
00123        [--no-history] [--no-init-file] [--no-init-path] [--no-line-editing]\n\
00124        [--no-site-file] [--no-window-system] [-p path] [--path path]\n\
00125        [--silent] [--traditional] [--verbose] [--version] [file]";
00126 
00127 // This is here so that it's more likely that the usage message and
00128 // the real set of options will agree.  Note: the '+' must come first
00129 // to prevent getopt from permuting arguments!
00130 static const char *short_opts = "+HVdfhip:qvx";
00131 
00132 // The code to evaluate at startup (--eval CODE)
00133 static std::string code_to_eval;
00134 
00135 // If TRUE, don't exit after evaluating code given by --eval option.
00136 static bool persist = false;
00137 
00138 // Long options.  See the comments in getopt.h for the meanings of the
00139 // fields in this structure.
00140 #define DOC_CACHE_FILE_OPTION 1
00141 #define EVAL_OPTION 2
00142 #define EXEC_PATH_OPTION 3
00143 #define IMAGE_PATH_OPTION 4
00144 #define INFO_FILE_OPTION 5
00145 #define INFO_PROG_OPTION 6
00146 #define LINE_EDITING_OPTION 7
00147 #define NO_INIT_FILE_OPTION 8
00148 #define NO_INIT_PATH_OPTION 9
00149 #define NO_LINE_EDITING_OPTION 10
00150 #define NO_SITE_FILE_OPTION 11
00151 #define NO_WINDOW_SYSTEM_OPTION 12
00152 #define PERSIST_OPTION 13
00153 #define TRADITIONAL_OPTION 14
00154 struct option long_opts[] =
00155   {
00156     { "braindead",        no_argument,       0, TRADITIONAL_OPTION },
00157     { "debug",            no_argument,       0, 'd' },
00158     { "doc-cache-file",   required_argument, 0, DOC_CACHE_FILE_OPTION },
00159     { "echo-commands",    no_argument,       0, 'x' },
00160     { "eval",             required_argument, 0, EVAL_OPTION },
00161     { "exec-path",        required_argument, 0, EXEC_PATH_OPTION },
00162     { "help",             no_argument,       0, 'h' },
00163     { "image-path",       required_argument, 0, IMAGE_PATH_OPTION },
00164     { "info-file",        required_argument, 0, INFO_FILE_OPTION },
00165     { "info-program",     required_argument, 0, INFO_PROG_OPTION },
00166     { "interactive",      no_argument,       0, 'i' },
00167     { "line-editing",     no_argument,       0, LINE_EDITING_OPTION },
00168     { "no-history",       no_argument,       0, 'H' },
00169     { "no-init-file",     no_argument,       0, NO_INIT_FILE_OPTION },
00170     { "no-init-path",     no_argument,       0, NO_INIT_PATH_OPTION },
00171     { "no-line-editing",  no_argument,       0, NO_LINE_EDITING_OPTION },
00172     { "no-site-file",     no_argument,       0, NO_SITE_FILE_OPTION },
00173     { "no-window-system", no_argument,       0, NO_WINDOW_SYSTEM_OPTION },
00174     { "norc",             no_argument,       0, 'f' },
00175     { "path",             required_argument, 0, 'p' },
00176     { "persist",          no_argument,       0, PERSIST_OPTION },
00177     { "quiet",            no_argument,       0, 'q' },
00178     { "silent",           no_argument,       0, 'q' },
00179     { "traditional",      no_argument,       0, TRADITIONAL_OPTION },
00180     { "verbose",          no_argument,       0, 'V' },
00181     { "version",          no_argument,       0, 'v' },
00182     { 0,                  0,                 0, 0 }
00183   };
00184 
00185 // Store the command-line options for later use.
00186 
00187 static void
00188 intern_argv (int argc, char **argv)
00189 {
00190   assert (symbol_table::at_top_level ());
00191 
00192   symbol_table::varref (".nargin.") = argc - 1;
00193 
00194   symbol_table::mark_hidden (".nargin.");
00195 
00196   if (argc > 0)
00197     {
00198       octave_argv.resize (argc - 1);
00199 
00200       // Skip program name in argv.
00201       int i = argc;
00202       while (--i > 0)
00203         octave_argv[i-1] = *(argv+i);
00204     }
00205 }
00206 
00207 static void
00208 initialize_pathsearch (void)
00209 {
00210   // This may seem odd, but doing it this way means that we don't have
00211   // to modify the kpathsea library...
00212 
00213   std::string odb = octave_env::getenv ("OCTAVE_DB_PATH");
00214 
00215   // For backward compatibility.
00216 
00217   if (odb.empty ())
00218     odb = octave_env::getenv ("OCTAVE_DB_DIR");
00219 
00220   if (odb.empty ())
00221     odb = Vdata_dir + file_ops::dir_sep_str () + "octave:"
00222       + Vlibexec_dir + file_ops::dir_sep_str () + "octave";
00223 }
00224 
00225 DEFUN (__version_info__, args, ,
00226   "-*- texinfo -*-\n\
00227 @deftypefn {Built-in Function} {retval =} __version_info__ (@var{name}, @var{version}, @var{release}, @var{date})\n\
00228 Undocumented internal function.\n\
00229 @end deftypefn")
00230 {
00231   octave_value retval;
00232 
00233   static octave_map vinfo;
00234 
00235   int nargin = args.length ();
00236 
00237   if (nargin == 4)
00238     {
00239       if (vinfo.nfields () == 0)
00240         {
00241           vinfo.assign ("Name", args (0));
00242           vinfo.assign ("Version", args (1));
00243           vinfo.assign ("Release", args (2));
00244           vinfo.assign ("Date", args (3));
00245         }
00246       else
00247         {
00248           octave_idx_type n = vinfo.numel () + 1;
00249 
00250           vinfo.resize (dim_vector (n, 1));
00251 
00252           octave_value idx (n);
00253 
00254           vinfo.assign (idx, "Name", Cell (octave_value (args (0))));
00255           vinfo.assign (idx, "Version", Cell (octave_value (args (1))));
00256           vinfo.assign (idx, "Release", Cell (octave_value (args (2))));
00257           vinfo.assign (idx, "Date", Cell (octave_value (args (3))));
00258         }
00259     }
00260   else if (nargin == 0)
00261     retval = vinfo;
00262   else
00263     print_usage ();
00264 
00265   return retval;
00266 }
00267 
00268 static void
00269 initialize_version_info (void)
00270 {
00271   octave_value_list args;
00272 
00273   args(3) = OCTAVE_RELEASE_DATE;
00274   args(2) = OCTAVE_RELEASE;
00275   args(1) = OCTAVE_VERSION;
00276   args(0) = "GNU Octave";
00277 
00278   F__version_info__ (args, 0);
00279 }
00280 
00281 static void
00282 gripe_safe_source_exception (const std::string& file, const std::string& msg)
00283 {
00284   std::cerr << "error: " << msg << "\n"
00285             << "error: execution of " << file << " failed\n"
00286             << "error: trying to make my way to a command prompt"
00287             << std::endl;
00288 }
00289 
00290 // Execute commands from a file and catch potential exceptions in a
00291 // consistent way.  This function should be called anywhere we might
00292 // parse and execute commands from a file before before we have entered
00293 // the main loop in toplev.cc.
00294 
00295 static void
00296 safe_source_file (const std::string& file_name,
00297                   const std::string& context = std::string (),
00298                   bool verbose = false, bool require_file = true,
00299                   const std::string& warn_for = std::string ())
00300 {
00301   try
00302     {
00303       source_file (file_name, context, verbose, require_file, warn_for);
00304     }
00305   catch (octave_interrupt_exception)
00306     {
00307       recover_from_exception ();
00308       octave_stdout << "\n";
00309       if (quitting_gracefully)
00310         clean_up_and_exit (exit_status);
00311     }
00312   catch (octave_execution_exception)
00313     {
00314       recover_from_exception ();
00315       gripe_safe_source_exception (file_name, "unhandled execution exception");
00316     }
00317   catch (std::bad_alloc)
00318     {
00319       recover_from_exception ();
00320       gripe_safe_source_exception
00321         (file_name,
00322          "memory exhausted or requested size too large for range of Octave's index type");
00323     }
00324 }
00325 
00326 // Initialize by reading startup files.
00327 
00328 static void
00329 execute_startup_files (void)
00330 {
00331   unwind_protect frame;
00332 
00333   frame.protect_var (input_from_startup_file);
00334 
00335   input_from_startup_file = true;
00336 
00337   std::string context;
00338 
00339   bool verbose = (verbose_flag && ! inhibit_startup_message);
00340 
00341   bool require_file = false;
00342 
00343   if (read_site_files)
00344     {
00345       // Execute commands from the site-wide configuration file.
00346       // First from the file $(prefix)/lib/octave/site/m/octaverc
00347       // (if it exists), then from the file
00348       // $(prefix)/share/octave/$(version)/m/octaverc (if it exists).
00349 
00350       safe_source_file (Vlocal_site_defaults_file, context, verbose,
00351                         require_file);
00352 
00353       safe_source_file (Vsite_defaults_file, context, verbose, require_file);
00354     }
00355 
00356   if (read_init_files)
00357     {
00358       // Try to execute commands from $HOME/$OCTAVE_INITFILE and
00359       // $OCTAVE_INITFILE.  If $OCTAVE_INITFILE is not set, .octaverc
00360       // is assumed.
00361 
00362       bool home_rc_already_executed = false;
00363 
00364       std::string initfile = octave_env::getenv ("OCTAVE_INITFILE");
00365 
00366       if (initfile.empty ())
00367         initfile = ".octaverc";
00368 
00369       std::string home_dir = octave_env::get_home_directory ();
00370 
00371       std::string home_rc = octave_env::make_absolute (initfile, home_dir);
00372 
00373       std::string local_rc;
00374 
00375       if (! home_rc.empty ())
00376         {
00377           safe_source_file (home_rc, context, verbose, require_file);
00378 
00379           // Names alone are not enough.
00380 
00381           file_stat fs_home_rc (home_rc);
00382 
00383           if (fs_home_rc)
00384             {
00385               // We want to check for curr_dir after executing home_rc
00386               // because doing that may change the working directory.
00387 
00388               local_rc = octave_env::make_absolute (initfile);
00389 
00390               home_rc_already_executed = same_file (home_rc, local_rc);
00391             }
00392         }
00393 
00394       if (! home_rc_already_executed)
00395         {
00396           if (local_rc.empty ())
00397             local_rc = octave_env::make_absolute (initfile);
00398 
00399           safe_source_file (local_rc, context, verbose, require_file);
00400         }
00401     }
00402 }
00403 
00404 static int
00405 execute_eval_option_code (const std::string& code)
00406 {
00407   unwind_protect frame;
00408 
00409   octave_save_signal_mask ();
00410 
00411   can_interrupt = true;
00412 
00413   octave_signal_hook = octave_signal_handler;
00414   octave_interrupt_hook = 0;
00415   octave_bad_alloc_hook = 0;
00416 
00417   octave_catch_interrupts ();
00418 
00419   octave_initialized = true;
00420 
00421   frame.protect_var (interactive);
00422 
00423   interactive = false;
00424 
00425   int parse_status = 0;
00426 
00427   try
00428     {
00429       eval_string (code, false, parse_status, 0);
00430     }
00431   catch (octave_interrupt_exception)
00432     {
00433       recover_from_exception ();
00434       octave_stdout << "\n";
00435       if (quitting_gracefully)
00436         clean_up_and_exit (exit_status);
00437     }
00438   catch (octave_execution_exception)
00439     {
00440       recover_from_exception ();
00441       std::cerr << "error: unhandled execution exception -- eval failed"
00442                 << std::endl;
00443     }
00444   catch (std::bad_alloc)
00445     {
00446       std::cerr << "error: memory exhausted or requested size too large for range of Octave's index type -- eval failed"
00447                 << std::endl;
00448     }
00449 
00450   return parse_status;
00451 }
00452 
00453 static void
00454 execute_command_line_file (const std::string& fname)
00455 {
00456   unwind_protect frame;
00457 
00458   octave_save_signal_mask ();
00459 
00460   can_interrupt = true;
00461 
00462   octave_signal_hook = octave_signal_handler;
00463   octave_interrupt_hook = 0;
00464   octave_bad_alloc_hook = 0;
00465 
00466   octave_catch_interrupts ();
00467 
00468   octave_initialized = true;
00469 
00470   frame.protect_var (interactive);
00471   frame.protect_var (reading_script_file);
00472   frame.protect_var (input_from_command_line_file);
00473 
00474   frame.protect_var (curr_fcn_file_name);
00475   frame.protect_var (curr_fcn_file_full_name);
00476 
00477   frame.protect_var (octave_program_invocation_name);
00478   frame.protect_var (octave_program_name);
00479 
00480   interactive = false;
00481   reading_script_file = true;
00482   input_from_command_line_file = true;
00483 
00484   curr_fcn_file_name = fname;
00485   curr_fcn_file_full_name = curr_fcn_file_name;
00486 
00487   octave_program_invocation_name = curr_fcn_file_name;
00488 
00489   size_t pos = curr_fcn_file_name.find_last_of (file_ops::dir_sep_chars ());
00490 
00491   std::string tmp = (pos != std::string::npos)
00492     ? curr_fcn_file_name.substr (pos+1) : curr_fcn_file_name;
00493 
00494   octave_program_name = tmp;
00495 
00496   std::string context;
00497   bool verbose = false;
00498   bool require_file = true;
00499 
00500   safe_source_file (fname, context, verbose, require_file, "octave");
00501 }
00502 
00503 // Usage message with extra help.
00504 
00505 static void
00506 verbose_usage (void)
00507 {
00508   std::cout << OCTAVE_NAME_VERSION_COPYRIGHT_COPYING_AND_WARRANTY "\n\
00509 \n\
00510 Usage: octave [options] [FILE]\n\
00511 \n\
00512 Options:\n\
00513 \n\
00514   --debug, -d             Enter parser debugging mode.\n\
00515   --doc-cache-file FILE   Use doc cache file FILE.\n\
00516   --echo-commands, -x     Echo commands as they are executed.\n\
00517   --eval CODE             Evaluate CODE.  Exit when done unless --persist.\n\
00518   --exec-path PATH        Set path for executing subprograms.\n\
00519   --help, -h,             Print short help message and exit.\n\
00520   --image-path PATH       Add PATH to head of image search path.\n\
00521   --info-file FILE        Use top-level info file FILE.\n\
00522   --info-program PROGRAM  Use PROGRAM for reading info files.\n\
00523   --interactive, -i       Force interactive behavior.\n\
00524   --line-editing          Force readline use for command-line editing.\n\
00525   --no-history, -H        Don't save commands to the history list\n\
00526   --no-init-file          Don't read the ~/.octaverc or .octaverc files.\n\
00527   --no-init-path          Don't initialize function search path.\n\
00528   --no-line-editing       Don't use readline for command-line editing.\n\
00529   --no-site-file          Don't read the site-wide octaverc file.\n\
00530   --no-window-system      Disable window system, including graphics.\n\
00531   --norc, -f              Don't read any initialization files.\n\
00532   --path PATH, -p PATH    Add PATH to head of function search path.\n\
00533   --persist               Go interactive after --eval or reading from FILE.\n\
00534   --silent, -q            Don't print message at startup.\n\
00535   --traditional           Set variables for closer MATLAB compatibility.\n\
00536   --verbose, -V           Enable verbose output in some cases.\n\
00537   --version, -v           Print version number and exit.\n\
00538 \n\
00539   FILE                    Execute commands from FILE.  Exit when done\n\
00540                           unless --persist is also specified.\n\
00541 \n"
00542 OCTAVE_WWW_STATEMENT "\n\
00543 \n"
00544 OCTAVE_CONTRIB_STATEMENT "\n\
00545 \n"
00546 OCTAVE_BUGS_STATEMENT "\n";
00547 
00548   exit (0);
00549 }
00550 
00551 // Terse usage messsage.
00552 
00553 static void
00554 usage (void)
00555 {
00556   std::cerr << "\nusage: " << usage_string << "\n\n";
00557   exit (1);
00558 }
00559 
00560 static void
00561 print_version_and_exit (void)
00562 {
00563   std::cout << OCTAVE_NAME_VERSION_COPYRIGHT_COPYING_WARRANTY_AND_BUGS "\n";
00564   exit (0);
00565 }
00566 
00567 static void
00568 lo_error_handler (const char *fmt, ...)
00569 {
00570   va_list args;
00571   va_start (args, fmt);
00572   verror_with_cfn (fmt, args);
00573   va_end (args);
00574 
00575   octave_throw_execution_exception ();
00576 }
00577 
00578 static void
00579 lo_error_with_id_handler (const char *id, const char *fmt, ...)
00580 {
00581   va_list args;
00582   va_start (args, fmt);
00583   verror_with_id_cfn (id, fmt, args);
00584   va_end (args);
00585 
00586   octave_throw_execution_exception ();
00587 }
00588 
00589 static void
00590 initialize_error_handlers ()
00591 {
00592   set_liboctave_error_handler (lo_error_handler);
00593   set_liboctave_error_with_id_handler (lo_error_with_id_handler);
00594   set_liboctave_warning_handler (warning);
00595   set_liboctave_warning_with_id_handler (warning_with_id);
00596 }
00597 
00598 // What happens on --traditional.
00599 
00600 static void
00601 maximum_braindamage (void)
00602 {
00603   persist = true;
00604 
00605   bind_internal_variable ("PS1", ">> ");
00606   bind_internal_variable ("PS2", "");
00607   bind_internal_variable ("allow_noninteger_range_as_index", true);
00608   bind_internal_variable ("beep_on_error", true);
00609   bind_internal_variable ("confirm_recursive_rmdir", false);
00610   bind_internal_variable ("crash_dumps_octave_core", false);
00611   bind_internal_variable ("default_save_options", "-mat-binary");
00612   bind_internal_variable ("do_braindead_shortcircuit_evaluation", true);
00613   bind_internal_variable ("fixed_point_format", true);
00614   bind_internal_variable ("history_timestamp_format_string",
00615                          "%%-- %D %I:%M %p --%%");
00616   bind_internal_variable ("page_screen_output", false);
00617   bind_internal_variable ("print_empty_dimensions", false);
00618 
00619   disable_warning ("Octave:abbreviated-property-match");
00620   disable_warning ("Octave:fopen-file-in-path");
00621   disable_warning ("Octave:function-name-clash");
00622   disable_warning ("Octave:load-file-in-path");
00623   disable_warning ("Octave:possible-matlab-short-circuit-operator");
00624 }
00625 
00626 // You guessed it.
00627 
00628 int
00629 octave_main (int argc, char **argv, int embedded)
00630 {
00631   octave_env::set_program_name (argv[0]);
00632 
00633   octave_program_invocation_name = octave_env::get_program_invocation_name ();
00634   octave_program_name = octave_env::get_program_name ();
00635 
00636   octave_thread::init ();
00637 
00638   // The order of these calls is important.  The call to
00639   // install_defaults must come before install_builtins because
00640   // default variable values must be available for the variables to be
00641   // installed, and the call to install_builtins must come before the
00642   // options are processed because some command line options override
00643   // defaults by calling bind_internal_variable.
00644 
00645   init_signals ();
00646 
00647   sysdep_init ();
00648 
00649   octave_ieee_init ();
00650 
00651   // The idea here is to force xerbla to be referenced so that we will
00652   // link to our own version instead of the one provided by the BLAS
00653   // library.  But octave_NaN should never be -1, so we should never
00654   // actually call xerbla.
00655 
00656   if (octave_NaN == -1)
00657     F77_FUNC (xerbla, XERBLA) ("octave", 13 F77_CHAR_ARG_LEN (6));
00658 
00659   initialize_error_handlers ();
00660 
00661   initialize_default_warning_state ();
00662 
00663   install_defaults ();
00664 
00665   initialize_pathsearch ();
00666 
00667   if (! embedded)
00668     install_signal_handlers ();
00669   else
00670     quit_allowed = false;
00671 
00672   initialize_file_io ();
00673 
00674   install_types ();
00675 
00676   install_ops ();
00677 
00678   install_builtins ();
00679 
00680   bool forced_line_editing = false;
00681 
00682   bool read_history_file = true;
00683 
00684   while (true)
00685     {
00686       int long_idx;
00687 
00688       int optc = getopt_long (argc, argv, short_opts, long_opts, &long_idx);
00689 
00690       if (optc < 0)
00691         break;
00692 
00693       switch (optc)
00694         {
00695         case '?':
00696           // Unrecognized option.  getopt_long already printed a
00697           // message about that, so we will just print the usage string
00698           // and exit.
00699           usage ();
00700           break;
00701 
00702         case 'H':
00703           read_history_file = false;
00704           bind_internal_variable ("saving_history", false);
00705           break;
00706 
00707         case 'V':
00708           verbose_flag = true;
00709           break;
00710 
00711         case 'd':
00712           // This is the same as yydebug in parse.y.
00713           octave_debug++;
00714           break;
00715 
00716         case 'f':
00717           read_init_files = false;
00718           read_site_files = false;
00719           break;
00720 
00721         case 'h':
00722           verbose_usage ();
00723           break;
00724 
00725         case 'i':
00726           forced_interactive = true;
00727           break;
00728 
00729         case 'p':
00730           if (optarg)
00731             load_path::set_command_line_path (optarg);
00732           break;
00733 
00734         case 'q':
00735           inhibit_startup_message = true;
00736           break;
00737 
00738         case 'x':
00739           {
00740             double tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_CMD_LINE);
00741             bind_internal_variable ("echo_executing_commands", tmp);
00742           }
00743           break;
00744 
00745         case 'v':
00746           print_version_and_exit ();
00747           break;
00748 
00749         case DOC_CACHE_FILE_OPTION:
00750           if (optarg)
00751             bind_internal_variable ("doc_cache_file", optarg);
00752           break;
00753 
00754         case EVAL_OPTION:
00755           if (optarg)
00756             {
00757               if (code_to_eval.empty ())
00758                 code_to_eval = optarg;
00759               else
00760                 code_to_eval += std::string (" ") + optarg;
00761             }
00762           break;
00763 
00764         case EXEC_PATH_OPTION:
00765           if (optarg)
00766             set_exec_path (optarg);
00767           break;
00768 
00769         case IMAGE_PATH_OPTION:
00770           if (optarg)
00771             set_image_path (optarg);
00772           break;
00773 
00774         case INFO_FILE_OPTION:
00775           if (optarg)
00776             bind_internal_variable ("info_file", optarg);
00777           break;
00778 
00779         case INFO_PROG_OPTION:
00780           if (optarg)
00781             bind_internal_variable ("info_program", optarg);
00782           break;
00783 
00784         case LINE_EDITING_OPTION:
00785           forced_line_editing = true;
00786           break;
00787 
00788         case NO_INIT_FILE_OPTION:
00789           read_init_files = false;
00790           break;
00791 
00792         case NO_INIT_PATH_OPTION:
00793           set_initial_path = false;
00794           break;
00795 
00796         case NO_LINE_EDITING_OPTION:
00797           line_editing = false;
00798           break;
00799 
00800         case NO_SITE_FILE_OPTION:
00801           read_site_files = 0;
00802           break;
00803 
00804         case NO_WINDOW_SYSTEM_OPTION:
00805           display_info::no_window_system ();
00806           break;
00807 
00808         case PERSIST_OPTION:
00809           persist = true;
00810           break;
00811 
00812         case TRADITIONAL_OPTION:
00813           traditional = true;
00814           break;
00815 
00816         default:
00817           // getopt_long should print a message about unrecognized
00818           // options and return '?', which is handled above.  So if we
00819           // end up here, it is because there was an option but we
00820           // forgot to handle it.  That should be fatal.
00821           panic_impossible ();
00822           break;
00823         }
00824     }
00825 
00826   // Make sure we clean up when we exit.  Also allow users to register
00827   // functions.  If we don't have atexit or on_exit, we're going to
00828   // leave some junk files around if we exit abnormally.
00829 
00830   atexit (do_octave_atexit);
00831 
00832   // Is input coming from a terminal?  If so, we are probably
00833   // interactive.
00834 
00835   // If stdin is not a tty, then we are reading commands from a pipe or
00836   // a redirected file.
00837   stdin_is_tty = gnulib::isatty (fileno (stdin));
00838 
00839   interactive = (! embedded && stdin_is_tty
00840                  && gnulib::isatty (fileno (stdout)));
00841 
00842   if (! interactive && ! forced_line_editing)
00843     line_editing = false;
00844 
00845   // Force default line editor if we don't want readline editing.
00846   if (! line_editing)
00847     command_editor::force_default_editor ();
00848 
00849   // These can come after command line args since none of them set any
00850   // defaults that might be changed by command line options.
00851 
00852   if (line_editing)
00853     initialize_command_input ();
00854 
00855   if (! inhibit_startup_message)
00856     std::cout << OCTAVE_STARTUP_MESSAGE "\n" << std::endl;
00857 
00858   if (traditional)
00859     maximum_braindamage ();
00860 
00861   octave_interpreter_ready = true;
00862 
00863   initialize_version_info ();
00864 
00865   // Make all command-line arguments available to startup files,
00866   // including PKG_ADD files.
00867 
00868   intern_argv (argc, argv);
00869 
00870   load_path::initialize (set_initial_path);
00871 
00872   initialize_history (read_history_file);
00873 
00874   execute_startup_files ();
00875 
00876   if (! inhibit_startup_message && reading_startup_message_printed)
00877     std::cout << std::endl;
00878 
00879   // If there is an extra argument, see if it names a file to read.
00880   // Additional arguments are taken as command line options for the
00881   // script.
00882 
00883   int last_arg_idx = optind;
00884 
00885   int remaining_args = argc - last_arg_idx;
00886 
00887   if (! code_to_eval.empty ())
00888     {
00889       int parse_status = execute_eval_option_code (code_to_eval);
00890 
00891       if (! (persist || remaining_args > 0))
00892         clean_up_and_exit (parse_status || error_state ? 1 : 0);
00893     }
00894 
00895   if (remaining_args > 0)
00896     {
00897       // If we are running an executable script (#! /bin/octave) then
00898       // we should only see the args passed to the script.
00899 
00900       intern_argv (remaining_args, argv+last_arg_idx);
00901 
00902       execute_command_line_file (argv[last_arg_idx]);
00903 
00904       if (! persist)
00905         {
00906           quitting_gracefully = true;
00907 
00908           clean_up_and_exit (error_state ? 1 : 0);
00909         }
00910     }
00911 
00912   // Avoid counting commands executed from startup files.
00913 
00914   command_editor::reset_current_command_number (1);
00915 
00916   // Now argv should have the full set of args.
00917   intern_argv (argc, argv);
00918 
00919   if (! embedded)
00920     switch_to_buffer (create_buffer (get_input_from_stdin ()));
00921 
00922   // Force input to be echoed if not really interactive, but the user
00923   // has forced interactive behavior.
00924 
00925   if (! interactive && forced_interactive)
00926     {
00927       command_editor::blink_matching_paren (false);
00928 
00929       // FIXME -- is this the right thing to do?
00930 
00931       bind_internal_variable ("echo_executing_commands", ECHO_CMD_LINE);
00932     }
00933 
00934   if (embedded)
00935     {
00936       // FIXME -- do we need to do any cleanup here before
00937       // returning?  If we don't, what will happen to Octave functions
00938       // that have been registered to execute with atexit, for example?
00939 
00940       return 1;
00941     }
00942 
00943   int retval = main_loop ();
00944 
00945   if (retval == 1 && ! error_state)
00946     retval = 0;
00947 
00948   quitting_gracefully = true;
00949 
00950   clean_up_and_exit (retval);
00951 
00952   return 0;
00953 }
00954 
00955 DEFUN (argv, args, ,
00956   "-*- texinfo -*-\n\
00957 @deftypefn {Built-in Function} {} argv ()\n\
00958 Return the command line arguments passed to Octave.  For example,\n\
00959 if you invoked Octave using the command\n\
00960 \n\
00961 @example\n\
00962 octave --no-line-editing --silent\n\
00963 @end example\n\
00964 \n\
00965 @noindent\n\
00966 @code{argv} would return a cell array of strings with the elements\n\
00967 @option{--no-line-editing} and @option{--silent}.\n\
00968 \n\
00969 If you write an executable Octave script, @code{argv} will return the\n\
00970 list of arguments passed to the script.  @xref{Executable Octave Programs},\n\
00971 for an example of how to create an executable Octave script.\n\
00972 @end deftypefn")
00973 {
00974   octave_value retval;
00975 
00976   if (args.length () == 0)
00977     retval = Cell (octave_argv);
00978   else
00979     print_usage ();
00980 
00981   return retval;
00982 }
00983 
00984 /*
00985 %!error argv (1);
00986 %!assert (iscellstr (argv ()));
00987 */
00988 
00989 DEFUN (program_invocation_name, args, ,
00990   "-*- texinfo -*-\n\
00991 @deftypefn {Built-in Function} {} program_invocation_name ()\n\
00992 Return the name that was typed at the shell prompt to run Octave.\n\
00993 \n\
00994 If executing a script from the command line (e.g., @code{octave foo.m})\n\
00995 or using an executable Octave script, the program name is set to the\n\
00996 name of the script.  @xref{Executable Octave Programs}, for an example of\n\
00997 how to create an executable Octave script.\n\
00998 @seealso{program_name}\n\
00999 @end deftypefn")
01000 {
01001   octave_value retval;
01002 
01003   if (args.length () == 0)
01004     retval = octave_program_invocation_name;
01005   else
01006     print_usage ();
01007 
01008   return retval;
01009 }
01010 
01011 /*
01012 %!error program_invocation_name (1);
01013 %!assert (ischar (program_invocation_name ()));
01014 */
01015 
01016 DEFUN (program_name, args, ,
01017   "-*- texinfo -*-\n\
01018 @deftypefn {Built-in Function} {} program_name ()\n\
01019 Return the last component of the value returned by\n\
01020 @code{program_invocation_name}.\n\
01021 @seealso{program_invocation_name}\n\
01022 @end deftypefn")
01023 {
01024   octave_value retval;
01025 
01026   if (args.length () == 0)
01027     retval = octave_program_name;
01028   else
01029     print_usage ();
01030 
01031   return retval;
01032 }
01033 
01034 /*
01035 %!error program_name (1);
01036 %!assert (ischar (program_name ()));
01037 */
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines