Mercurial > hg > octave-lyh
annotate src/octave.cc @ 8861:31f864877246
doc and lookfor fixes
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 24 Feb 2009 15:08:39 -0500 |
parents | 4dd0213bb883 |
children | eb63fbe60fab |
rev | line source |
---|---|
1 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, |
4 2002, 2003, 2004, 2005, 2006, 2007 John W. Eaton | |
1 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1 | 21 |
22 */ | |
23 | |
24 // Born February 20, 1992. | |
25 | |
240 | 26 #ifdef HAVE_CONFIG_H |
1192 | 27 #include <config.h> |
1 | 28 #endif |
29 | |
1355 | 30 #include <cassert> |
31 #include <cstdlib> | |
32 #include <cstring> | |
33 #include <ctime> | |
34 | |
3503 | 35 #include <fstream> |
36 #include <iostream> | |
1355 | 37 |
38 #ifdef HAVE_UNISTD_H | |
2442 | 39 #ifdef HAVE_SYS_TYPES_H |
1 | 40 #include <sys/types.h> |
2442 | 41 #endif |
139 | 42 #include <unistd.h> |
43 #endif | |
1355 | 44 |
2926 | 45 #include "cmd-edit.h" |
3690 | 46 #include "f77-fcn.h" |
4097 | 47 #include "file-ops.h" |
2926 | 48 #include "file-stat.h" |
240 | 49 #include "lo-error.h" |
2926 | 50 #include "oct-env.h" |
3019 | 51 #include "pathsearch.h" |
1907 | 52 #include "str-vec.h" |
223 | 53 |
2492 | 54 #include <defaults.h> |
3933 | 55 #include "Cell.h" |
1355 | 56 #include "defun.h" |
57 #include "error.h" | |
58 #include "file-io.h" | |
1 | 59 #include "input.h" |
60 #include "lex.h" | |
5832 | 61 #include "load-path.h" |
4113 | 62 #include "octave.h" |
1742 | 63 #include "oct-hist.h" |
5654 | 64 #include "oct-map.h" |
3195 | 65 #include "oct-obj.h" |
2375 | 66 #include "ops.h" |
1670 | 67 #include "toplev.h" |
1 | 68 #include "parse.h" |
562 | 69 #include "procstream.h" |
1817 | 70 #include "prog-args.h" |
1355 | 71 #include "sighandlers.h" |
72 #include "sysdep.h" | |
2375 | 73 #include "ov.h" |
1 | 74 #include "unwind-prot.h" |
1355 | 75 #include "utils.h" |
76 #include "variables.h" | |
2492 | 77 #include <version.h> |
1 | 78 |
4159 | 79 // Kluge. |
5990 | 80 extern "C" F77_RET_T |
81 F77_FUNC (xerbla, XERBLA) (F77_CONST_CHAR_ARG_DECL, const octave_idx_type& | |
82 F77_CHAR_ARG_LEN_DECL); | |
4159 | 83 |
2910 | 84 extern void install_builtins (void); |
85 | |
1704 | 86 #if !defined (HAVE_ATEXIT) && defined (HAVE_ON_EXIT) |
87 extern "C" int on_exit (); | |
88 #define atexit on_exit | |
89 #endif | |
90 | |
1907 | 91 // The command-line options. |
92 static string_vector octave_argv; | |
1 | 93 |
5780 | 94 // The name used to invoke Octave. |
95 static std::string | |
96 octave_program_invocation_name; | |
97 | |
98 // The last component of octave_program_invocation_name. | |
99 static std::string octave_program_name; | |
100 | |
2239 | 101 // TRUE means we read ~/.octaverc and ./.octaverc. |
102 // (--norc; --no-init-file; -f) | |
103 static bool read_init_files = true; | |
104 | |
105 // TRUE means we read the site-wide octaverc files. | |
106 // (--norc; --no-site-file; -f) | |
2240 | 107 static bool read_site_files = true; |
1 | 108 |
6365 | 109 // TRUE means we set the initial path to configured defaults. |
110 // (--no-initial-path) | |
111 static bool set_initial_path = true; | |
112 | |
3019 | 113 // TRUE means we don't print the usual startup message. |
616 | 114 // (--quiet; --silent; -q) |
2239 | 115 static bool inhibit_startup_message = false; |
1 | 116 |
3019 | 117 // TRUE means we turn on compatibility options. |
1410 | 118 // (--traditional) |
2239 | 119 static bool traditional = false; |
1410 | 120 |
3019 | 121 // If TRUE, print verbose info in some cases. |
1825 | 122 // (--verbose; -V) |
2239 | 123 static bool verbose_flag = false; |
1825 | 124 |
1 | 125 // Usage message |
139 | 126 static const char *usage_string = |
5189 | 127 "octave [-?HVdfhiqvx] [--debug] [--echo-commands] [--eval CODE]\n\ |
5814 | 128 [--exec-path path] [--help] [--image-path path] [--info-file file]\n\ |
6938 | 129 [--info-program prog] [--interactive] [--line-editing] [--no-history] [--no-init-file]\n\ |
6365 | 130 [--no-line-editing] [--no-site-file] [--no-init-path] [-p path]\n\ |
131 [--path path] [--silent] [--traditional] [--verbose] [--version] [file]"; | |
1 | 132 |
1358 | 133 // This is here so that it's more likely that the usage message and |
1410 | 134 // the real set of options will agree. Note: the `+' must come first |
135 // to prevent getopt from permuting arguments! | |
3180 | 136 static const char *short_opts = "+?HVdfhip:qvx"; |
139 | 137 |
5189 | 138 // The code to evaluate at startup (--eval CODE) |
139 static std::string code_to_eval; | |
140 | |
141 // If TRUE, don't exit after evaluating code given by --eval option. | |
142 static bool persist = false; | |
4356 | 143 |
1103 | 144 // Long options. See the comments in getopt.h for the meanings of the |
145 // fields in this structure. | |
8861 | 146 #define DOC_CACHE_FILE_OPTION 1 |
147 #define EVAL_OPTION 2 | |
148 #define EXEC_PATH_OPTION 3 | |
149 #define IMAGE_PATH_OPTION 4 | |
150 #define INFO_FILE_OPTION 5 | |
151 #define INFO_PROG_OPTION 6 | |
152 #define NO_INIT_FILE_OPTION 7 | |
153 #define NO_LINE_EDITING_OPTION 8 | |
154 #define NO_SITE_FILE_OPTION 9 | |
155 #define NO_INITIAL_PATH_OPTION 10 | |
156 #define PERSIST_OPTION 11 | |
157 #define TRADITIONAL_OPTION 12 | |
158 #define LINE_EDITING_OPTION 13 | |
1817 | 159 long_options long_opts[] = |
139 | 160 { |
1855 | 161 { "debug", prog_args::no_arg, 0, 'd' }, |
2277 | 162 { "braindead", prog_args::no_arg, 0, TRADITIONAL_OPTION }, |
8861 | 163 { "doc-cache-file", prog_args::required_arg, 0, DOC_CACHE_FILE_OPTION }, |
1855 | 164 { "echo-commands", prog_args::no_arg, 0, 'x' }, |
5189 | 165 { "eval", prog_args::required_arg, 0, EVAL_OPTION }, |
1855 | 166 { "exec-path", prog_args::required_arg, 0, EXEC_PATH_OPTION }, |
167 { "help", prog_args::no_arg, 0, 'h' }, | |
5814 | 168 { "image-path", prog_args::required_arg, 0, IMAGE_PATH_OPTION }, |
1855 | 169 { "info-file", prog_args::required_arg, 0, INFO_FILE_OPTION }, |
170 { "info-program", prog_args::required_arg, 0, INFO_PROG_OPTION }, | |
171 { "interactive", prog_args::no_arg, 0, 'i' }, | |
6938 | 172 { "line-editing", prog_args::no_arg, 0, LINE_EDITING_OPTION }, |
3180 | 173 { "no-history", prog_args::no_arg, 0, 'H' }, |
2239 | 174 { "no-init-file", prog_args::no_arg, 0, NO_INIT_FILE_OPTION }, |
1855 | 175 { "no-line-editing", prog_args::no_arg, 0, NO_LINE_EDITING_OPTION }, |
2239 | 176 { "no-site-file", prog_args::no_arg, 0, NO_SITE_FILE_OPTION }, |
6365 | 177 { "no-initial-path", prog_args::no_arg, 0, NO_INITIAL_PATH_OPTION }, |
1855 | 178 { "norc", prog_args::no_arg, 0, 'f' }, |
179 { "path", prog_args::required_arg, 0, 'p' }, | |
5189 | 180 { "persist", prog_args::no_arg, 0, PERSIST_OPTION }, |
1855 | 181 { "quiet", prog_args::no_arg, 0, 'q' }, |
182 { "silent", prog_args::no_arg, 0, 'q' }, | |
183 { "traditional", prog_args::no_arg, 0, TRADITIONAL_OPTION }, | |
184 { "verbose", prog_args::no_arg, 0, 'V' }, | |
185 { "version", prog_args::no_arg, 0, 'v' }, | |
3019 | 186 { 0, 0, 0, 0 } |
139 | 187 }; |
1 | 188 |
1355 | 189 // Store the command-line options for later use. |
190 | |
191 static void | |
192 intern_argv (int argc, char **argv) | |
193 { | |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
194 assert (symbol_table::at_top_level ()); |
4892 | 195 |
7752
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
196 symbol_table::varref (".nargin.") = argc - 1; |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
197 |
40c428ea3408
initial implementation of dbup and dbdown
John W. Eaton <jwe@octave.org>
parents:
7715
diff
changeset
|
198 symbol_table::mark_hidden (".nargin."); |
3195 | 199 |
1355 | 200 if (argc > 1) |
201 { | |
7336 | 202 octave_argv.resize (argc - 1); |
3933 | 203 |
2495 | 204 // Skip program name in argv. |
3933 | 205 int i = argc; |
206 while (--i > 0) | |
5780 | 207 octave_argv[i-1] = *(argv+i); |
1355 | 208 } |
209 } | |
210 | |
1792 | 211 static void |
212 initialize_pathsearch (void) | |
213 { | |
214 // This may seem odd, but doing it this way means that we don't have | |
215 // to modify the kpathsea library... | |
216 | |
3523 | 217 std::string odb = octave_env::getenv ("OCTAVE_DB_PATH"); |
3141 | 218 |
219 // For backward compatibility. | |
1792 | 220 |
2926 | 221 if (odb.empty ()) |
3141 | 222 odb = octave_env::getenv ("OCTAVE_DB_DIR"); |
1792 | 223 |
3141 | 224 if (odb.empty ()) |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
225 odb = Vdata_dir + file_ops::dir_sep_str () + "octave:" |
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
226 + Vlibexec_dir + file_ops::dir_sep_str () + "octave"; |
1792 | 227 } |
228 | |
5654 | 229 DEFUN (__version_info__, args, , |
230 "-*- texinfo -*-\n\ | |
6678 | 231 @deftypefn {Built-in Function} {retval =} __version_info__ (@var{name}, @var{version}, @var{release}, @var{date})\n\ |
6945 | 232 Undocumented internal function.\n\ |
5654 | 233 @end deftypefn") |
234 { | |
235 octave_value retval; | |
236 | |
237 static Octave_map vinfo; | |
238 | |
239 int nargin = args.length (); | |
240 | |
5658 | 241 if (nargin == 4) |
5654 | 242 { |
6639 | 243 if (vinfo.nfields () == 0) |
5781 | 244 { |
245 vinfo.assign ("Name", args (0)); | |
246 vinfo.assign ("Version", args (1)); | |
247 vinfo.assign ("Release", args (2)); | |
248 vinfo.assign ("Date", args (3)); | |
249 } | |
250 else | |
251 { | |
252 octave_idx_type n = vinfo.numel () + 1; | |
5654 | 253 |
5781 | 254 vinfo.resize (dim_vector (n, 1)); |
255 | |
256 octave_value idx (n); | |
257 | |
258 vinfo.assign (idx, "Name", Cell (octave_value (args (0)))); | |
259 vinfo.assign (idx, "Version", Cell (octave_value (args (1)))); | |
260 vinfo.assign (idx, "Release", Cell (octave_value (args (2)))); | |
261 vinfo.assign (idx, "Date", Cell (octave_value (args (3)))); | |
262 } | |
5654 | 263 } |
264 else if (nargin == 0) | |
265 retval = vinfo; | |
266 else | |
5823 | 267 print_usage (); |
5654 | 268 |
269 return retval; | |
270 } | |
271 | |
272 static void | |
273 initialize_version_info (void) | |
274 { | |
275 octave_value_list args; | |
276 | |
5658 | 277 args(3) = OCTAVE_RELEASE_DATE; |
278 args(2) = OCTAVE_RELEASE; | |
5654 | 279 args(1) = OCTAVE_VERSION; |
280 args(0) = "GNU Octave"; | |
281 | |
282 F__version_info__ (args, 0); | |
283 } | |
284 | |
581 | 285 // Initialize by reading startup files. |
286 | |
1 | 287 static void |
288 execute_startup_files (void) | |
289 { | |
2985 | 290 unwind_protect::begin_frame ("execute_startup_files"); |
315 | 291 |
3019 | 292 unwind_protect_bool (input_from_startup_file); |
1651 | 293 |
3019 | 294 input_from_startup_file = true; |
315 | 295 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
296 std::string context; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
297 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
298 bool verbose = (verbose_flag && ! inhibit_startup_message); |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
299 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
300 bool require_file = false; |
578 | 301 |
2239 | 302 if (read_site_files) |
1 | 303 { |
2239 | 304 // Execute commands from the site-wide configuration file. |
305 // First from the file $(prefix)/lib/octave/site/m/octaverc | |
306 // (if it exists), then from the file | |
3597 | 307 // $(prefix)/share/octave/$(version)/m/octaverc (if it exists). |
1755 | 308 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
309 source_file (Vlocal_site_defaults_file, context, verbose, require_file); |
1755 | 310 |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
311 source_file (Vsite_defaults_file, context, verbose, require_file); |
1 | 312 } |
313 | |
2239 | 314 if (read_init_files) |
315 { | |
2512 | 316 // Try to execute commands from $HOME/$OCTAVE_INITFILE and |
317 // $OCTAVE_INITFILE. If $OCTAVE_INITFILE is not set, .octaverc | |
318 // is assumed. | |
2239 | 319 |
4804 | 320 bool home_rc_already_executed = false; |
2512 | 321 |
3523 | 322 std::string initfile = octave_env::getenv ("OCTAVE_INITFILE"); |
2512 | 323 |
2926 | 324 if (initfile.empty ()) |
2512 | 325 initfile = ".octaverc"; |
326 | |
3523 | 327 std::string home_dir = octave_env::get_home_directory (); |
2926 | 328 |
4804 | 329 std::string home_rc = octave_env::make_absolute (initfile, home_dir); |
330 | |
331 std::string local_rc; | |
2239 | 332 |
5781 | 333 if (! home_rc.empty ()) |
2239 | 334 { |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
335 source_file (home_rc, context, verbose, require_file); |
2239 | 336 |
337 // Names alone are not enough. | |
338 | |
339 file_stat fs_home_rc (home_rc); | |
340 | |
341 if (fs_home_rc) | |
342 { | |
4804 | 343 // We want to check for curr_dir after executing home_rc |
344 // because doing that may change the working directory. | |
345 | |
346 std::string curr_dir = octave_env::getcwd (); | |
347 | |
348 local_rc = octave_env::make_absolute (initfile, curr_dir); | |
349 | |
6598 | 350 home_rc_already_executed = same_file (home_rc, local_rc); |
2239 | 351 } |
352 } | |
353 | |
354 if (! home_rc_already_executed) | |
4804 | 355 { |
356 if (local_rc.empty ()) | |
357 { | |
358 std::string curr_dir = octave_env::getcwd (); | |
359 | |
360 local_rc = octave_env::make_absolute (initfile, curr_dir); | |
361 } | |
362 | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
363 source_file (local_rc, context, verbose, require_file); |
4804 | 364 } |
2239 | 365 } |
315 | 366 |
2985 | 367 unwind_protect::run_frame ("execute_startup_files"); |
1 | 368 } |
369 | |
5189 | 370 static int |
371 execute_eval_option_code (const std::string& code) | |
372 { | |
373 unwind_protect::begin_frame ("execute_eval_option_code"); | |
374 | |
7202 | 375 octave_save_signal_mask (); |
376 | |
377 can_interrupt = true; | |
378 | |
379 octave_signal_hook = octave_signal_handler; | |
380 octave_interrupt_hook = unwind_protect::run_all; | |
381 octave_bad_alloc_hook = unwind_protect::run_all; | |
382 | |
383 octave_catch_interrupts (); | |
384 | |
385 octave_initialized = true; | |
386 | |
5189 | 387 unwind_protect_bool (interactive); |
388 | |
389 interactive = false; | |
390 | |
391 int parse_status = 0; | |
392 | |
6487 | 393 try |
394 { | |
395 eval_string (code, false, parse_status, 0); | |
396 } | |
7202 | 397 catch (octave_interrupt_exception) |
398 { | |
399 recover_from_exception (); | |
400 octave_stdout << "\n"; | |
401 } | |
6487 | 402 catch (std::bad_alloc) |
403 { | |
6680 | 404 std::cerr << "error: memory exhausted or requested size too large for range of Octave's index type -- eval failed" |
405 << std::endl; | |
6487 | 406 } |
5189 | 407 |
408 unwind_protect::run_frame ("execute_eval_option_code"); | |
409 | |
410 return parse_status; | |
411 } | |
412 | |
413 static void | |
414 execute_command_line_file (const std::string& fname) | |
415 { | |
416 unwind_protect::begin_frame ("execute_command_line_file"); | |
417 | |
7202 | 418 octave_save_signal_mask (); |
419 | |
420 can_interrupt = true; | |
421 | |
422 octave_signal_hook = octave_signal_handler; | |
423 octave_interrupt_hook = unwind_protect::run_all; | |
424 octave_bad_alloc_hook = unwind_protect::run_all; | |
425 | |
426 octave_catch_interrupts (); | |
427 | |
428 octave_initialized = true; | |
429 | |
5189 | 430 unwind_protect_bool (interactive); |
431 unwind_protect_bool (reading_script_file); | |
432 unwind_protect_bool (input_from_command_line_file); | |
433 | |
434 unwind_protect_str (curr_fcn_file_name); | |
435 unwind_protect_str (curr_fcn_file_full_name); | |
436 | |
5780 | 437 unwind_protect_str (octave_program_invocation_name); |
438 unwind_protect_str (octave_program_name); | |
5189 | 439 |
440 interactive = false; | |
441 reading_script_file = true; | |
442 input_from_command_line_file = true; | |
443 | |
444 curr_fcn_file_name = fname; | |
445 curr_fcn_file_full_name = curr_fcn_file_name; | |
446 | |
5780 | 447 octave_program_invocation_name = curr_fcn_file_name; |
5189 | 448 |
8007
a2ab20ba78f7
make file_ops a proper singleton class
John W. Eaton <jwe@octave.org>
parents:
7752
diff
changeset
|
449 size_t pos = curr_fcn_file_name.find_last_of (file_ops::dir_sep_chars ()); |
5189 | 450 |
8021 | 451 std::string tmp = (pos != std::string::npos) |
5189 | 452 ? curr_fcn_file_name.substr (pos+1) : curr_fcn_file_name; |
453 | |
5780 | 454 octave_program_name = tmp; |
5189 | 455 |
6504 | 456 try |
457 { | |
7715
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
458 std::string context; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
459 bool verbose = false; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
460 bool require_file = true; |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
461 |
5b4d278ec828
parse scripts completely before executing
John W. Eaton <jwe@octave.org>
parents:
7481
diff
changeset
|
462 source_file (fname, context, verbose, require_file, "octave"); |
6504 | 463 } |
7202 | 464 catch (octave_interrupt_exception) |
465 { | |
466 recover_from_exception (); | |
467 octave_stdout << "\n"; | |
468 } | |
6504 | 469 catch (std::bad_alloc) |
470 { | |
6680 | 471 std::cerr << "error: memory exhausted or requested size too large for range of Octave's index type -- execution of " |
472 << fname << " failed" << std::endl; | |
6504 | 473 } |
5189 | 474 |
475 unwind_protect::run_frame ("execute_command_line_file"); | |
476 } | |
477 | |
581 | 478 // Usage message with extra help. |
479 | |
1 | 480 static void |
481 verbose_usage (void) | |
482 { | |
3922 | 483 std::cout << OCTAVE_NAME_VERSION_COPYRIGHT_COPYING_AND_WARRANTY "\n\ |
1613 | 484 \n\ |
485 Usage: octave [options]\n\ | |
486 \n\ | |
487 Options:\n\ | |
1119 | 488 \n\ |
3180 | 489 --debug, -d Enter parser debugging mode.\n\ |
490 --echo-commands, -x Echo commands as they are executed.\n\ | |
5191 | 491 --eval CODE Evaluate CODE. Exit when done unless --persist.\n\ |
1613 | 492 --exec-path PATH Set path for executing subprograms.\n\ |
3180 | 493 --help, -h, -? Print short help message and exit.\n\ |
6615 | 494 --image-path PATH Set initial IMAGE_PATH to PATH.\n\ |
1613 | 495 --info-file FILE Use top-level info file FILE.\n\ |
496 --info-program PROGRAM Use PROGRAM for reading info files.\n\ | |
3180 | 497 --interactive, -i Force interactive behavior.\n\ |
6938 | 498 --line-editing Force readline use for command-line editing.\n\ |
3180 | 499 --no-history, -H Don't save commands to the history list\n\ |
2470 | 500 --no-init-file Don't read the ~/.octaverc or .octaverc files.\n\ |
2212 | 501 --no-line-editing Don't use readline for command-line editing.\n\ |
2470 | 502 --no-site-file Don't read the site-wide octaverc file.\n\ |
3238 | 503 --norc, -f Don't read any initialization files.\n\ |
5814 | 504 --path PATH, -p PATH Set initial function search path to PATH.\n\ |
5189 | 505 --persist Go interactive after --eval or reading from FILE.\n\ |
3180 | 506 --silent, -q Don't print message at startup.\n\ |
1613 | 507 --traditional Set compatibility variables.\n\ |
3180 | 508 --verbose, -V Enable verbose output in some cases.\n\ |
509 --version, -v Print version number and exit.\n\ | |
1119 | 510 \n\ |
5190 | 511 FILE Execute commands from FILE. Exit when done\n\ |
5191 | 512 unless --persist is also specified.\n\ |
4829 | 513 \n" |
514 OCTAVE_WWW_STATEMENT "\n\ | |
4356 | 515 \n" |
516 OCTAVE_CONTRIB_STATEMENT "\n\ | |
517 \n" | |
518 OCTAVE_BUGS_STATEMENT "\n"; | |
285 | 519 |
613 | 520 exit (0); |
1 | 521 } |
522 | |
581 | 523 // Terse usage messsage. |
524 | |
1 | 525 static void |
526 usage (void) | |
527 { | |
3531 | 528 std::cerr << "usage: " << usage_string << "\n"; |
1 | 529 exit (1); |
530 } | |
531 | |
532 static void | |
533 print_version_and_exit (void) | |
534 { | |
3922 | 535 std::cout << OCTAVE_NAME_VERSION_COPYRIGHT_COPYING_WARRANTY_AND_BUGS "\n"; |
1 | 536 exit (0); |
537 } | |
538 | |
721 | 539 static void |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
540 lo_error_handler (const char *fmt, ...) |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
541 { |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
542 va_list args; |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
543 va_start (args, fmt); |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
544 verror (fmt, args); |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
545 va_end (args); |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
546 |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
547 octave_throw_execution_exception (); |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
548 } |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
549 |
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
550 static void |
721 | 551 initialize_error_handlers () |
552 { | |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7336
diff
changeset
|
553 set_liboctave_error_handler (lo_error_handler); |
3325 | 554 set_liboctave_warning_handler (warning); |
5781 | 555 set_liboctave_warning_with_id_handler (warning_with_id); |
721 | 556 } |
557 | |
1410 | 558 // What happens on --traditional. |
559 | |
560 static void | |
561 maximum_braindamage (void) | |
562 { | |
5189 | 563 persist = true; |
564 | |
5794 | 565 bind_internal_variable ("PS1", ">> "); |
566 bind_internal_variable ("PS2", ""); | |
567 bind_internal_variable ("beep_on_error", true); | |
8473
4dd0213bb883
octave.cc (maximum_braindamage): set confirm_recursive_rmdir to false
John W. Eaton <jwe@octave.org>
parents:
8347
diff
changeset
|
568 bind_internal_variable ("confirm_recursive_rmdir", false); |
5794 | 569 bind_internal_variable ("crash_dumps_octave_core", false); |
570 bind_internal_variable ("default_save_options", "-mat-binary"); | |
571 bind_internal_variable ("fixed_point_format", true); | |
572 bind_internal_variable ("history_timestamp_format_string", | |
5305 | 573 "%%-- %D %I:%M %p --%%"); |
5794 | 574 bind_internal_variable ("page_screen_output", false); |
575 bind_internal_variable ("print_empty_dimensions", false); | |
5904 | 576 |
6159 | 577 disable_warning ("Octave:fopen-file-in-path"); |
5904 | 578 disable_warning ("Octave:function-name-clash"); |
6159 | 579 disable_warning ("Octave:load-file-in-path"); |
1410 | 580 } |
581 | |
581 | 582 // You guessed it. |
583 | |
1 | 584 int |
4368 | 585 octave_main (int argc, char **argv, int embedded) |
1 | 586 { |
3019 | 587 octave_env::set_program_name (argv[0]); |
588 | |
5780 | 589 octave_program_invocation_name = octave_env::get_program_invocation_name (); |
590 octave_program_name = octave_env::get_program_name (); | |
591 | |
2205 | 592 // The order of these calls is important. The call to |
2926 | 593 // install_defaults must come before install_builtins because |
3019 | 594 // default variable values must be available for the variables to be |
2205 | 595 // installed, and the call to install_builtins must come before the |
596 // options are processed because some command line options override | |
5794 | 597 // defaults by calling bind_internal_variable. |
721 | 598 |
5844 | 599 init_signals (); |
600 | |
1 | 601 sysdep_init (); |
602 | |
4159 | 603 // The idea here is to force xerbla to be referenced so that we will |
604 // link to our own version instead of the one provided by the BLAS | |
605 // library. But octave_NaN should never be -1, so we should never | |
606 // actually call xerbla. | |
607 | |
608 if (octave_NaN == -1) | |
609 F77_FUNC (xerbla, XERBLA) ("octave", 13, 6L); | |
610 | |
721 | 611 initialize_error_handlers (); |
223 | 612 |
5794 | 613 initialize_default_warning_state (); |
614 | |
2926 | 615 install_defaults (); |
1 | 616 |
1792 | 617 initialize_pathsearch (); |
1744 | 618 |
4368 | 619 if (! embedded) |
620 install_signal_handlers (); | |
2205 | 621 |
622 initialize_file_io (); | |
623 | |
2375 | 624 install_types (); |
625 | |
626 install_ops (); | |
2205 | 627 |
628 install_builtins (); | |
629 | |
1817 | 630 prog_args args (argc, argv, short_opts, long_opts); |
631 | |
6938 | 632 bool forced_line_editing = false; |
633 | |
8273
2c1ba965b486
skip reading history file with --no-history option
John W. Eaton <jwe@octave.org>
parents:
8099
diff
changeset
|
634 bool read_history_file = true; |
2c1ba965b486
skip reading history file with --no-history option
John W. Eaton <jwe@octave.org>
parents:
8099
diff
changeset
|
635 |
139 | 636 int optc; |
1817 | 637 while ((optc = args.getopt ()) != EOF) |
1 | 638 { |
139 | 639 switch (optc) |
1 | 640 { |
3180 | 641 case 'H': |
8273
2c1ba965b486
skip reading history file with --no-history option
John W. Eaton <jwe@octave.org>
parents:
8099
diff
changeset
|
642 read_history_file = false; |
5794 | 643 bind_internal_variable ("saving_history", false); |
3180 | 644 break; |
645 | |
793 | 646 case 'V': |
2239 | 647 verbose_flag = true; |
793 | 648 break; |
649 | |
1 | 650 case 'd': |
4753 | 651 // This is the same as yydebug in parse.y. |
652 octave_debug++; | |
1 | 653 break; |
777 | 654 |
1 | 655 case 'f': |
2239 | 656 read_init_files = false; |
657 read_site_files = false; | |
1 | 658 break; |
777 | 659 |
1 | 660 case 'h': |
661 case '?': | |
662 verbose_usage (); | |
663 break; | |
777 | 664 |
1 | 665 case 'i': |
3019 | 666 forced_interactive = true; |
1 | 667 break; |
777 | 668 |
1 | 669 case 'p': |
1817 | 670 if (args.optarg ()) |
5832 | 671 load_path::set_command_line_path (args.optarg ()); |
1 | 672 break; |
777 | 673 |
1 | 674 case 'q': |
2239 | 675 inhibit_startup_message = true; |
1 | 676 break; |
777 | 677 |
1 | 678 case 'x': |
2205 | 679 { |
680 double tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_CMD_LINE); | |
5794 | 681 bind_internal_variable ("echo_executing_commands", tmp); |
2205 | 682 } |
1 | 683 break; |
777 | 684 |
1 | 685 case 'v': |
686 print_version_and_exit (); | |
687 break; | |
777 | 688 |
8861 | 689 case DOC_CACHE_FILE_OPTION: |
690 if (args.optarg ()) | |
691 bind_internal_variable ("doc_cache_file", args.optarg ()); | |
692 break; | |
693 | |
5189 | 694 case EVAL_OPTION: |
695 if (args.optarg ()) | |
5193 | 696 { |
697 if (code_to_eval.empty ()) | |
698 code_to_eval = args.optarg (); | |
699 else | |
700 code_to_eval += std::string (" ") + args.optarg (); | |
701 } | |
5189 | 702 break; |
703 | |
1613 | 704 case EXEC_PATH_OPTION: |
1817 | 705 if (args.optarg ()) |
5814 | 706 set_exec_path (args.optarg ()); |
707 break; | |
708 | |
709 case IMAGE_PATH_OPTION: | |
710 if (args.optarg ()) | |
711 set_image_path (args.optarg ()); | |
1613 | 712 break; |
713 | |
186 | 714 case INFO_FILE_OPTION: |
1817 | 715 if (args.optarg ()) |
5794 | 716 bind_internal_variable ("info_file", args.optarg ()); |
186 | 717 break; |
777 | 718 |
1613 | 719 case INFO_PROG_OPTION: |
1817 | 720 if (args.optarg ()) |
5794 | 721 bind_internal_variable ("info_program", args.optarg ()); |
1613 | 722 break; |
723 | |
6938 | 724 case LINE_EDITING_OPTION: |
725 forced_line_editing = true; | |
726 break; | |
727 | |
2239 | 728 case NO_INIT_FILE_OPTION: |
729 read_init_files = false; | |
730 break; | |
731 | |
1821 | 732 case NO_LINE_EDITING_OPTION: |
3019 | 733 line_editing = false; |
2239 | 734 break; |
735 | |
736 case NO_SITE_FILE_OPTION: | |
737 read_site_files = 0; | |
1821 | 738 break; |
739 | |
6365 | 740 case NO_INITIAL_PATH_OPTION: |
741 set_initial_path = false; | |
742 break; | |
743 | |
1410 | 744 case TRADITIONAL_OPTION: |
2239 | 745 traditional = true; |
1410 | 746 break; |
747 | |
5189 | 748 case PERSIST_OPTION: |
749 persist = true; | |
750 break; | |
751 | |
1 | 752 default: |
753 usage (); | |
754 break; | |
755 } | |
756 } | |
757 | |
1651 | 758 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
2077 | 759 // Make sure we clean up when we exit. Also allow users to register |
760 // functions. If we don't have atexit or on_exit, we're going to | |
761 // leave some junk files around if we exit abnormally. | |
762 | |
763 atexit (do_octave_atexit); | |
318 | 764 #endif |
1 | 765 |
1358 | 766 // These can come after command line args since none of them set any |
767 // defaults that might be changed by command line options. | |
581 | 768 |
2926 | 769 initialize_command_input (); |
315 | 770 |
578 | 771 if (! inhibit_startup_message) |
3538 | 772 std::cout << OCTAVE_STARTUP_MESSAGE "\n" << std::endl; |
578 | 773 |
1410 | 774 if (traditional) |
775 maximum_braindamage (); | |
776 | |
4217 | 777 octave_interpreter_ready = true; |
778 | |
5654 | 779 initialize_version_info (); |
780 | |
8099
e0a6e54f5a19
octave.cc (octave_main): make all command-line arguments available to startup scripts
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
781 // Make all command-line arguments available to startup files, |
e0a6e54f5a19
octave.cc (octave_main): make all command-line arguments available to startup scripts
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
782 // including PKG_ADD files. |
e0a6e54f5a19
octave.cc (octave_main): make all command-line arguments available to startup scripts
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
783 |
e0a6e54f5a19
octave.cc (octave_main): make all command-line arguments available to startup scripts
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
784 intern_argv (argc, argv); |
e0a6e54f5a19
octave.cc (octave_main): make all command-line arguments available to startup scripts
John W. Eaton <jwe@octave.org>
parents:
8021
diff
changeset
|
785 |
6365 | 786 load_path::initialize (set_initial_path); |
4217 | 787 |
2239 | 788 execute_startup_files (); |
1651 | 789 |
8273
2c1ba965b486
skip reading history file with --no-history option
John W. Eaton <jwe@octave.org>
parents:
8099
diff
changeset
|
790 initialize_history (read_history_file); |
1 | 791 |
578 | 792 if (! inhibit_startup_message && reading_startup_message_printed) |
3538 | 793 std::cout << std::endl; |
578 | 794 |
5189 | 795 // Is input coming from a terminal? If so, we are probably |
796 // interactive. | |
1358 | 797 |
5189 | 798 interactive = (! embedded |
799 && isatty (fileno (stdin)) && isatty (fileno (stdout))); | |
800 | |
6938 | 801 if (! interactive && ! forced_line_editing) |
5189 | 802 line_editing = false; |
1 | 803 |
1358 | 804 // If there is an extra argument, see if it names a file to read. |
805 // Additional arguments are taken as command line options for the | |
806 // script. | |
1 | 807 |
1817 | 808 int last_arg_idx = args.optind (); |
3019 | 809 |
1817 | 810 int remaining_args = argc - last_arg_idx; |
3019 | 811 |
5189 | 812 if (! code_to_eval.empty ()) |
149 | 813 { |
5189 | 814 int parse_status = execute_eval_option_code (code_to_eval); |
815 | |
5242 | 816 if (! (persist || remaining_args > 0)) |
5502 | 817 clean_up_and_exit (parse_status || error_state ? 1 : 0); |
1 | 818 } |
819 | |
5189 | 820 if (remaining_args > 0) |
821 { | |
822 // If we are running an executable script (#! /bin/octave) then | |
823 // we should only see the args passed to the script. | |
824 | |
825 intern_argv (remaining_args, argv+last_arg_idx); | |
826 | |
827 execute_command_line_file (argv[last_arg_idx]); | |
828 | |
829 if (! persist) | |
5502 | 830 clean_up_and_exit (error_state ? 1 : 0); |
5189 | 831 } |
832 | |
833 // Avoid counting commands executed from startup files. | |
834 | |
835 command_editor::reset_current_command_number (1); | |
836 | |
837 // Now argv should have the full set of args. | |
838 intern_argv (argc, argv); | |
839 | |
840 if (! embedded) | |
841 switch_to_buffer (create_buffer (get_input_from_stdin ())); | |
842 | |
1358 | 843 // Force input to be echoed if not really interactive, but the user |
844 // has forced interactive behavior. | |
1 | 845 |
1907 | 846 if (! interactive && forced_interactive) |
287 | 847 { |
2926 | 848 command_editor::blink_matching_paren (false); |
1588 | 849 |
5775 | 850 // FIXME -- is this the right thing to do? |
1588 | 851 |
5794 | 852 bind_internal_variable ("echo_executing_commands", ECHO_CMD_LINE); |
287 | 853 } |
1 | 854 |
4368 | 855 if (embedded) |
5502 | 856 { |
5775 | 857 // FIXME -- do we need to do any cleanup here before |
5502 | 858 // returning? If we don't, what will happen to Octave functions |
859 // that have been registered to execute with atexit, for example? | |
860 | |
861 return 1; | |
862 } | |
4368 | 863 |
5189 | 864 int retval = main_loop (); |
1 | 865 |
1005 | 866 if (retval == 1 && ! error_state) |
867 retval = 0; | |
868 | |
1 | 869 clean_up_and_exit (retval); |
4247 | 870 |
871 return 0; | |
1 | 872 } |
873 | |
5780 | 874 DEFUN (argv, args, , |
875 "-*- texinfo -*-\n\ | |
876 @deftypefn {Built-in Function} {} argv ()\n\ | |
877 Return the command line arguments passed to Octave. For example,\n\ | |
878 if you invoked Octave using the command\n\ | |
879 \n\ | |
880 @example\n\ | |
881 octave --no-line-editing --silent\n\ | |
882 @end example\n\ | |
883 \n\ | |
884 @noindent\n\ | |
885 @code{argv} would return a cell array of strings with the elements\n\ | |
886 @code{--no-line-editing} and @code{--silent}.\n\ | |
887 \n\ | |
888 If you write an executable Octave script, @code{argv} will return the\n\ | |
889 list of arguments passed to the script. @xref{Executable Octave Programs},\n\ | |
890 for an example of how to create an executable Octave script.\n\ | |
891 @end deftypefn") | |
892 { | |
893 octave_value retval; | |
894 | |
895 if (args.length () == 0) | |
896 retval = Cell (octave_argv); | |
897 else | |
5823 | 898 print_usage (); |
5780 | 899 |
900 return retval; | |
901 } | |
902 | |
903 DEFUN (program_invocation_name, args, , | |
904 "-*- texinfo -*-\n\ | |
905 @deftypefn {Built-in Function} program_invocation_name ()\n\ | |
906 Return the name that was typed at the shell prompt to run Octave.\n\ | |
907 \n\ | |
908 If executing a script from the command line (e.g., @code{octave foo.m})\n\ | |
909 or using an executable Octave script, the program name is set to the\n\ | |
910 name of the script. @xref{Executable Octave Programs}, for an example of\n\ | |
911 how to create an executable Octave script.\n\ | |
912 @seealso{program_name}\n\ | |
913 @end deftypefn") | |
914 { | |
915 octave_value retval; | |
916 | |
917 if (args.length () == 0) | |
918 retval = octave_program_invocation_name; | |
919 else | |
5823 | 920 print_usage (); |
5780 | 921 |
922 return retval; | |
923 } | |
924 | |
925 DEFUN (program_name, args, , | |
926 "-*- texinfo -*-\n\ | |
927 @deftypefn {Built-in Function} {} program_name ()\n\ | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
8273
diff
changeset
|
928 Return the last component of the value returned by\n\ |
5780 | 929 @code{program_invocation_name}.\n\ |
930 @seealso{program_invocation_name}\n\ | |
931 @end deftypefn") | |
932 { | |
933 octave_value retval; | |
934 | |
935 if (args.length () == 0) | |
936 retval = octave_program_name; | |
937 else | |
5823 | 938 print_usage (); |
5780 | 939 |
940 return retval; | |
941 } | |
942 | |
560 | 943 /* |
1 | 944 ;;; Local Variables: *** |
945 ;;; mode: C++ *** | |
946 ;;; End: *** | |
947 */ |