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 { |
4892
|
192 symbol_record *nargin_sr = top_level_sym_tab->lookup ("__nargin__", true); |
|
193 |
|
194 nargin_sr->mark_as_static (); |
|
195 |
|
196 nargin_sr->define (argc-1); |
3195
|
197 |
1355
|
198 if (argc > 1) |
|
199 { |
5780
|
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 ()) |
4101
|
223 odb = Vdata_dir + file_ops::dir_sep_str + "octave:" |
|
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 |
793
|
294 int verbose = (verbose_flag && ! inhibit_startup_message); |
578
|
295 |
2239
|
296 if (read_site_files) |
1
|
297 { |
2239
|
298 // Execute commands from the site-wide configuration file. |
|
299 // First from the file $(prefix)/lib/octave/site/m/octaverc |
|
300 // (if it exists), then from the file |
3597
|
301 // $(prefix)/share/octave/$(version)/m/octaverc (if it exists). |
1755
|
302 |
2897
|
303 parse_and_execute (Vlocal_site_defaults_file, verbose); |
1755
|
304 |
2897
|
305 parse_and_execute (Vsite_defaults_file, verbose); |
1
|
306 } |
|
307 |
2239
|
308 if (read_init_files) |
|
309 { |
2512
|
310 // Try to execute commands from $HOME/$OCTAVE_INITFILE and |
|
311 // $OCTAVE_INITFILE. If $OCTAVE_INITFILE is not set, .octaverc |
|
312 // is assumed. |
2239
|
313 |
4804
|
314 bool home_rc_already_executed = false; |
2512
|
315 |
3523
|
316 std::string initfile = octave_env::getenv ("OCTAVE_INITFILE"); |
2512
|
317 |
2926
|
318 if (initfile.empty ()) |
2512
|
319 initfile = ".octaverc"; |
|
320 |
3523
|
321 std::string home_dir = octave_env::get_home_directory (); |
2926
|
322 |
4804
|
323 std::string home_rc = octave_env::make_absolute (initfile, home_dir); |
|
324 |
|
325 std::string local_rc; |
2239
|
326 |
5781
|
327 if (! home_rc.empty ()) |
2239
|
328 { |
2897
|
329 parse_and_execute (home_rc, verbose); |
2239
|
330 |
|
331 // Names alone are not enough. |
|
332 |
|
333 file_stat fs_home_rc (home_rc); |
|
334 |
|
335 if (fs_home_rc) |
|
336 { |
4804
|
337 // We want to check for curr_dir after executing home_rc |
|
338 // because doing that may change the working directory. |
|
339 |
|
340 std::string curr_dir = octave_env::getcwd (); |
|
341 |
|
342 local_rc = octave_env::make_absolute (initfile, curr_dir); |
|
343 |
6598
|
344 home_rc_already_executed = same_file (home_rc, local_rc); |
2239
|
345 } |
|
346 } |
|
347 |
|
348 if (! home_rc_already_executed) |
4804
|
349 { |
|
350 if (local_rc.empty ()) |
|
351 { |
|
352 std::string curr_dir = octave_env::getcwd (); |
|
353 |
|
354 local_rc = octave_env::make_absolute (initfile, curr_dir); |
|
355 } |
|
356 |
|
357 parse_and_execute (local_rc, verbose); |
|
358 } |
2239
|
359 } |
315
|
360 |
2985
|
361 unwind_protect::run_frame ("execute_startup_files"); |
1
|
362 } |
|
363 |
5189
|
364 static int |
|
365 execute_eval_option_code (const std::string& code) |
|
366 { |
|
367 unwind_protect::begin_frame ("execute_eval_option_code"); |
|
368 |
|
369 unwind_protect_bool (interactive); |
|
370 |
|
371 interactive = false; |
|
372 |
|
373 int parse_status = 0; |
|
374 |
6487
|
375 try |
|
376 { |
|
377 eval_string (code, false, parse_status, 0); |
|
378 } |
|
379 catch (std::bad_alloc) |
|
380 { |
6680
|
381 std::cerr << "error: memory exhausted or requested size too large for range of Octave's index type -- eval failed" |
|
382 << std::endl; |
6487
|
383 } |
5189
|
384 |
|
385 unwind_protect::run_frame ("execute_eval_option_code"); |
|
386 |
|
387 return parse_status; |
|
388 } |
|
389 |
|
390 static void |
|
391 execute_command_line_file (const std::string& fname) |
|
392 { |
|
393 unwind_protect::begin_frame ("execute_command_line_file"); |
|
394 |
|
395 unwind_protect_bool (interactive); |
|
396 unwind_protect_bool (reading_script_file); |
|
397 unwind_protect_bool (input_from_command_line_file); |
|
398 |
|
399 unwind_protect_str (curr_fcn_file_name); |
|
400 unwind_protect_str (curr_fcn_file_full_name); |
|
401 |
5780
|
402 unwind_protect_str (octave_program_invocation_name); |
|
403 unwind_protect_str (octave_program_name); |
5189
|
404 |
|
405 interactive = false; |
|
406 reading_script_file = true; |
|
407 input_from_command_line_file = true; |
|
408 |
|
409 curr_fcn_file_name = fname; |
|
410 curr_fcn_file_full_name = curr_fcn_file_name; |
|
411 |
5780
|
412 octave_program_invocation_name = curr_fcn_file_name; |
5189
|
413 |
|
414 size_t pos = curr_fcn_file_name.find_last_of (file_ops::dir_sep_chars); |
|
415 |
|
416 std::string tmp = (pos != NPOS) |
|
417 ? curr_fcn_file_name.substr (pos+1) : curr_fcn_file_name; |
|
418 |
5780
|
419 octave_program_name = tmp; |
5189
|
420 |
6504
|
421 try |
|
422 { |
|
423 parse_and_execute (fname, false, "octave"); |
|
424 } |
|
425 catch (std::bad_alloc) |
|
426 { |
6680
|
427 std::cerr << "error: memory exhausted or requested size too large for range of Octave's index type -- execution of " |
|
428 << fname << " failed" << std::endl; |
6504
|
429 } |
5189
|
430 |
|
431 unwind_protect::run_frame ("execute_command_line_file"); |
|
432 } |
|
433 |
581
|
434 // Usage message with extra help. |
|
435 |
1
|
436 static void |
|
437 verbose_usage (void) |
|
438 { |
3922
|
439 std::cout << OCTAVE_NAME_VERSION_COPYRIGHT_COPYING_AND_WARRANTY "\n\ |
1613
|
440 \n\ |
|
441 Usage: octave [options]\n\ |
|
442 \n\ |
|
443 Options:\n\ |
1119
|
444 \n\ |
3180
|
445 --debug, -d Enter parser debugging mode.\n\ |
|
446 --echo-commands, -x Echo commands as they are executed.\n\ |
5191
|
447 --eval CODE Evaluate CODE. Exit when done unless --persist.\n\ |
1613
|
448 --exec-path PATH Set path for executing subprograms.\n\ |
3180
|
449 --help, -h, -? Print short help message and exit.\n\ |
6615
|
450 --image-path PATH Set initial IMAGE_PATH to PATH.\n\ |
1613
|
451 --info-file FILE Use top-level info file FILE.\n\ |
|
452 --info-program PROGRAM Use PROGRAM for reading info files.\n\ |
3180
|
453 --interactive, -i Force interactive behavior.\n\ |
6938
|
454 --line-editing Force readline use for command-line editing.\n\ |
3180
|
455 --no-history, -H Don't save commands to the history list\n\ |
2470
|
456 --no-init-file Don't read the ~/.octaverc or .octaverc files.\n\ |
2212
|
457 --no-line-editing Don't use readline for command-line editing.\n\ |
2470
|
458 --no-site-file Don't read the site-wide octaverc file.\n\ |
3238
|
459 --norc, -f Don't read any initialization files.\n\ |
5814
|
460 --path PATH, -p PATH Set initial function search path to PATH.\n\ |
5189
|
461 --persist Go interactive after --eval or reading from FILE.\n\ |
3180
|
462 --silent, -q Don't print message at startup.\n\ |
1613
|
463 --traditional Set compatibility variables.\n\ |
3180
|
464 --verbose, -V Enable verbose output in some cases.\n\ |
|
465 --version, -v Print version number and exit.\n\ |
1119
|
466 \n\ |
5190
|
467 FILE Execute commands from FILE. Exit when done\n\ |
5191
|
468 unless --persist is also specified.\n\ |
4829
|
469 \n" |
|
470 OCTAVE_WWW_STATEMENT "\n\ |
4356
|
471 \n" |
|
472 OCTAVE_CONTRIB_STATEMENT "\n\ |
|
473 \n" |
|
474 OCTAVE_BUGS_STATEMENT "\n"; |
285
|
475 |
613
|
476 exit (0); |
1
|
477 } |
|
478 |
581
|
479 // Terse usage messsage. |
|
480 |
1
|
481 static void |
|
482 usage (void) |
|
483 { |
3531
|
484 std::cerr << "usage: " << usage_string << "\n"; |
1
|
485 exit (1); |
|
486 } |
|
487 |
|
488 static void |
|
489 print_version_and_exit (void) |
|
490 { |
3922
|
491 std::cout << OCTAVE_NAME_VERSION_COPYRIGHT_COPYING_WARRANTY_AND_BUGS "\n"; |
1
|
492 exit (0); |
|
493 } |
|
494 |
721
|
495 static void |
|
496 initialize_error_handlers () |
|
497 { |
|
498 set_liboctave_error_handler (error); |
3325
|
499 set_liboctave_warning_handler (warning); |
5781
|
500 set_liboctave_warning_with_id_handler (warning_with_id); |
721
|
501 } |
|
502 |
1410
|
503 // What happens on --traditional. |
|
504 |
|
505 static void |
|
506 maximum_braindamage (void) |
|
507 { |
5189
|
508 persist = true; |
|
509 |
5794
|
510 bind_internal_variable ("PS1", ">> "); |
|
511 bind_internal_variable ("PS2", ""); |
|
512 bind_internal_variable ("beep_on_error", true); |
|
513 bind_internal_variable ("crash_dumps_octave_core", false); |
|
514 bind_internal_variable ("default_save_options", "-mat-binary"); |
|
515 bind_internal_variable ("fixed_point_format", true); |
|
516 bind_internal_variable ("history_timestamp_format_string", |
5305
|
517 "%%-- %D %I:%M %p --%%"); |
5794
|
518 bind_internal_variable ("page_screen_output", false); |
|
519 bind_internal_variable ("print_empty_dimensions", false); |
5904
|
520 |
6159
|
521 disable_warning ("Octave:fopen-file-in-path"); |
5904
|
522 disable_warning ("Octave:function-name-clash"); |
6159
|
523 disable_warning ("Octave:load-file-in-path"); |
1410
|
524 } |
|
525 |
581
|
526 // You guessed it. |
|
527 |
1
|
528 int |
4368
|
529 octave_main (int argc, char **argv, int embedded) |
1
|
530 { |
3019
|
531 octave_env::set_program_name (argv[0]); |
|
532 |
5780
|
533 octave_program_invocation_name = octave_env::get_program_invocation_name (); |
|
534 octave_program_name = octave_env::get_program_name (); |
|
535 |
2205
|
536 // The order of these calls is important. The call to |
2926
|
537 // install_defaults must come before install_builtins because |
3019
|
538 // default variable values must be available for the variables to be |
2205
|
539 // installed, and the call to install_builtins must come before the |
|
540 // options are processed because some command line options override |
5794
|
541 // defaults by calling bind_internal_variable. |
721
|
542 |
5844
|
543 init_signals (); |
|
544 |
1
|
545 sysdep_init (); |
|
546 |
4159
|
547 // The idea here is to force xerbla to be referenced so that we will |
|
548 // link to our own version instead of the one provided by the BLAS |
|
549 // library. But octave_NaN should never be -1, so we should never |
|
550 // actually call xerbla. |
|
551 |
|
552 if (octave_NaN == -1) |
|
553 F77_FUNC (xerbla, XERBLA) ("octave", 13, 6L); |
|
554 |
721
|
555 initialize_error_handlers (); |
223
|
556 |
5794
|
557 initialize_default_warning_state (); |
|
558 |
2926
|
559 install_defaults (); |
1
|
560 |
1792
|
561 initialize_pathsearch (); |
1744
|
562 |
4368
|
563 if (! embedded) |
|
564 install_signal_handlers (); |
2205
|
565 |
|
566 initialize_file_io (); |
|
567 |
2375
|
568 initialize_symbol_tables (); |
|
569 |
|
570 install_types (); |
|
571 |
|
572 install_ops (); |
2205
|
573 |
|
574 install_builtins (); |
|
575 |
1817
|
576 prog_args args (argc, argv, short_opts, long_opts); |
|
577 |
6938
|
578 bool forced_line_editing = false; |
|
579 |
139
|
580 int optc; |
1817
|
581 while ((optc = args.getopt ()) != EOF) |
1
|
582 { |
139
|
583 switch (optc) |
1
|
584 { |
3180
|
585 case 'H': |
5794
|
586 bind_internal_variable ("saving_history", false); |
3180
|
587 break; |
|
588 |
793
|
589 case 'V': |
2239
|
590 verbose_flag = true; |
793
|
591 break; |
|
592 |
1
|
593 case 'd': |
4753
|
594 // This is the same as yydebug in parse.y. |
|
595 octave_debug++; |
1
|
596 break; |
777
|
597 |
1
|
598 case 'f': |
2239
|
599 read_init_files = false; |
|
600 read_site_files = false; |
1
|
601 break; |
777
|
602 |
1
|
603 case 'h': |
|
604 case '?': |
|
605 verbose_usage (); |
|
606 break; |
777
|
607 |
1
|
608 case 'i': |
3019
|
609 forced_interactive = true; |
1
|
610 break; |
777
|
611 |
1
|
612 case 'p': |
1817
|
613 if (args.optarg ()) |
5832
|
614 load_path::set_command_line_path (args.optarg ()); |
1
|
615 break; |
777
|
616 |
1
|
617 case 'q': |
2239
|
618 inhibit_startup_message = true; |
1
|
619 break; |
777
|
620 |
1
|
621 case 'x': |
2205
|
622 { |
|
623 double tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_CMD_LINE); |
5794
|
624 bind_internal_variable ("echo_executing_commands", tmp); |
2205
|
625 } |
1
|
626 break; |
777
|
627 |
1
|
628 case 'v': |
|
629 print_version_and_exit (); |
|
630 break; |
777
|
631 |
5189
|
632 case EVAL_OPTION: |
|
633 if (args.optarg ()) |
5193
|
634 { |
|
635 if (code_to_eval.empty ()) |
|
636 code_to_eval = args.optarg (); |
|
637 else |
|
638 code_to_eval += std::string (" ") + args.optarg (); |
|
639 } |
5189
|
640 break; |
|
641 |
1613
|
642 case EXEC_PATH_OPTION: |
1817
|
643 if (args.optarg ()) |
5814
|
644 set_exec_path (args.optarg ()); |
|
645 break; |
|
646 |
|
647 case IMAGE_PATH_OPTION: |
|
648 if (args.optarg ()) |
|
649 set_image_path (args.optarg ()); |
1613
|
650 break; |
|
651 |
186
|
652 case INFO_FILE_OPTION: |
1817
|
653 if (args.optarg ()) |
5794
|
654 bind_internal_variable ("info_file", args.optarg ()); |
186
|
655 break; |
777
|
656 |
1613
|
657 case INFO_PROG_OPTION: |
1817
|
658 if (args.optarg ()) |
5794
|
659 bind_internal_variable ("info_program", args.optarg ()); |
1613
|
660 break; |
|
661 |
6938
|
662 case LINE_EDITING_OPTION: |
|
663 forced_line_editing = true; |
|
664 break; |
|
665 |
2239
|
666 case NO_INIT_FILE_OPTION: |
|
667 read_init_files = false; |
|
668 break; |
|
669 |
1821
|
670 case NO_LINE_EDITING_OPTION: |
3019
|
671 line_editing = false; |
2239
|
672 break; |
|
673 |
|
674 case NO_SITE_FILE_OPTION: |
|
675 read_site_files = 0; |
1821
|
676 break; |
|
677 |
6365
|
678 case NO_INITIAL_PATH_OPTION: |
|
679 set_initial_path = false; |
|
680 break; |
|
681 |
1410
|
682 case TRADITIONAL_OPTION: |
2239
|
683 traditional = true; |
1410
|
684 break; |
|
685 |
5189
|
686 case PERSIST_OPTION: |
|
687 persist = true; |
|
688 break; |
|
689 |
1
|
690 default: |
|
691 usage (); |
|
692 break; |
|
693 } |
|
694 } |
|
695 |
1651
|
696 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
2077
|
697 // Make sure we clean up when we exit. Also allow users to register |
|
698 // functions. If we don't have atexit or on_exit, we're going to |
|
699 // leave some junk files around if we exit abnormally. |
|
700 |
|
701 atexit (do_octave_atexit); |
318
|
702 #endif |
1
|
703 |
1358
|
704 // These can come after command line args since none of them set any |
|
705 // defaults that might be changed by command line options. |
581
|
706 |
2926
|
707 initialize_command_input (); |
315
|
708 |
578
|
709 if (! inhibit_startup_message) |
3538
|
710 std::cout << OCTAVE_STARTUP_MESSAGE "\n" << std::endl; |
578
|
711 |
1410
|
712 if (traditional) |
|
713 maximum_braindamage (); |
|
714 |
4217
|
715 octave_interpreter_ready = true; |
|
716 |
5654
|
717 initialize_version_info (); |
|
718 |
6365
|
719 load_path::initialize (set_initial_path); |
4217
|
720 |
2239
|
721 execute_startup_files (); |
1651
|
722 |
5794
|
723 initialize_history (); |
1
|
724 |
578
|
725 if (! inhibit_startup_message && reading_startup_message_printed) |
3538
|
726 std::cout << std::endl; |
578
|
727 |
5189
|
728 // Is input coming from a terminal? If so, we are probably |
|
729 // interactive. |
1358
|
730 |
5189
|
731 interactive = (! embedded |
|
732 && isatty (fileno (stdin)) && isatty (fileno (stdout))); |
|
733 |
6938
|
734 if (! interactive && ! forced_line_editing) |
5189
|
735 line_editing = false; |
1
|
736 |
1358
|
737 // If there is an extra argument, see if it names a file to read. |
|
738 // Additional arguments are taken as command line options for the |
|
739 // script. |
1
|
740 |
1817
|
741 int last_arg_idx = args.optind (); |
3019
|
742 |
1817
|
743 int remaining_args = argc - last_arg_idx; |
3019
|
744 |
5189
|
745 if (! code_to_eval.empty ()) |
149
|
746 { |
5189
|
747 // We probably want all the args for an --eval option. |
1355
|
748 |
|
749 intern_argv (argc, argv); |
|
750 |
5189
|
751 int parse_status = execute_eval_option_code (code_to_eval); |
|
752 |
5242
|
753 if (! (persist || remaining_args > 0)) |
5502
|
754 clean_up_and_exit (parse_status || error_state ? 1 : 0); |
1
|
755 } |
|
756 |
5189
|
757 if (remaining_args > 0) |
|
758 { |
|
759 // If we are running an executable script (#! /bin/octave) then |
|
760 // we should only see the args passed to the script. |
|
761 |
|
762 intern_argv (remaining_args, argv+last_arg_idx); |
|
763 |
|
764 execute_command_line_file (argv[last_arg_idx]); |
|
765 |
|
766 if (! persist) |
5502
|
767 clean_up_and_exit (error_state ? 1 : 0); |
5189
|
768 } |
|
769 |
|
770 // Avoid counting commands executed from startup files. |
|
771 |
|
772 command_editor::reset_current_command_number (1); |
|
773 |
|
774 // Now argv should have the full set of args. |
|
775 intern_argv (argc, argv); |
|
776 |
|
777 if (! embedded) |
|
778 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
|
779 |
1358
|
780 // Force input to be echoed if not really interactive, but the user |
|
781 // has forced interactive behavior. |
1
|
782 |
1907
|
783 if (! interactive && forced_interactive) |
287
|
784 { |
2926
|
785 command_editor::blink_matching_paren (false); |
1588
|
786 |
5775
|
787 // FIXME -- is this the right thing to do? |
1588
|
788 |
5794
|
789 bind_internal_variable ("echo_executing_commands", ECHO_CMD_LINE); |
287
|
790 } |
1
|
791 |
4368
|
792 if (embedded) |
5502
|
793 { |
5775
|
794 // FIXME -- do we need to do any cleanup here before |
5502
|
795 // returning? If we don't, what will happen to Octave functions |
|
796 // that have been registered to execute with atexit, for example? |
|
797 |
|
798 return 1; |
|
799 } |
4368
|
800 |
5189
|
801 int retval = main_loop (); |
1
|
802 |
1005
|
803 if (retval == 1 && ! error_state) |
|
804 retval = 0; |
|
805 |
1
|
806 clean_up_and_exit (retval); |
4247
|
807 |
|
808 return 0; |
1
|
809 } |
|
810 |
5780
|
811 DEFUN (argv, args, , |
|
812 "-*- texinfo -*-\n\ |
|
813 @deftypefn {Built-in Function} {} argv ()\n\ |
|
814 Return the command line arguments passed to Octave. For example,\n\ |
|
815 if you invoked Octave using the command\n\ |
|
816 \n\ |
|
817 @example\n\ |
|
818 octave --no-line-editing --silent\n\ |
|
819 @end example\n\ |
|
820 \n\ |
|
821 @noindent\n\ |
|
822 @code{argv} would return a cell array of strings with the elements\n\ |
|
823 @code{--no-line-editing} and @code{--silent}.\n\ |
|
824 \n\ |
|
825 If you write an executable Octave script, @code{argv} will return the\n\ |
|
826 list of arguments passed to the script. @xref{Executable Octave Programs},\n\ |
|
827 for an example of how to create an executable Octave script.\n\ |
|
828 @end deftypefn") |
|
829 { |
|
830 octave_value retval; |
|
831 |
|
832 if (args.length () == 0) |
|
833 retval = Cell (octave_argv); |
|
834 else |
5823
|
835 print_usage (); |
5780
|
836 |
|
837 return retval; |
|
838 } |
|
839 |
|
840 DEFUN (program_invocation_name, args, , |
|
841 "-*- texinfo -*-\n\ |
|
842 @deftypefn {Built-in Function} program_invocation_name ()\n\ |
|
843 Return the name that was typed at the shell prompt to run Octave.\n\ |
|
844 \n\ |
|
845 If executing a script from the command line (e.g., @code{octave foo.m})\n\ |
|
846 or using an executable Octave script, the program name is set to the\n\ |
|
847 name of the script. @xref{Executable Octave Programs}, for an example of\n\ |
|
848 how to create an executable Octave script.\n\ |
|
849 @seealso{program_name}\n\ |
|
850 @end deftypefn") |
|
851 { |
|
852 octave_value retval; |
|
853 |
|
854 if (args.length () == 0) |
|
855 retval = octave_program_invocation_name; |
|
856 else |
5823
|
857 print_usage (); |
5780
|
858 |
|
859 return retval; |
|
860 } |
|
861 |
|
862 DEFUN (program_name, args, , |
|
863 "-*- texinfo -*-\n\ |
|
864 @deftypefn {Built-in Function} {} program_name ()\n\ |
|
865 Return the last component of of the value returned by\n\ |
|
866 @code{program_invocation_name}.\n\ |
|
867 @seealso{program_invocation_name}\n\ |
|
868 @end deftypefn") |
|
869 { |
|
870 octave_value retval; |
|
871 |
|
872 if (args.length () == 0) |
|
873 retval = octave_program_name; |
|
874 else |
5823
|
875 print_usage (); |
5780
|
876 |
|
877 return retval; |
|
878 } |
|
879 |
560
|
880 /* |
1
|
881 ;;; Local Variables: *** |
|
882 ;;; mode: C++ *** |
|
883 ;;; End: *** |
|
884 */ |