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