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