1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
|
23 // Born February 20, 1992. |
|
24 |
240
|
25 #ifdef HAVE_CONFIG_H |
1192
|
26 #include <config.h> |
1
|
27 #endif |
|
28 |
1355
|
29 #include <cassert> |
|
30 #include <cstdlib> |
|
31 #include <cstring> |
|
32 #include <ctime> |
|
33 |
3503
|
34 #include <fstream> |
|
35 #include <iostream> |
1355
|
36 |
|
37 #ifdef HAVE_UNISTD_H |
2442
|
38 #ifdef HAVE_SYS_TYPES_H |
1
|
39 #include <sys/types.h> |
2442
|
40 #endif |
139
|
41 #include <unistd.h> |
|
42 #endif |
1355
|
43 |
2926
|
44 #include "cmd-edit.h" |
3690
|
45 #include "f77-fcn.h" |
4097
|
46 #include "file-ops.h" |
2926
|
47 #include "file-stat.h" |
240
|
48 #include "lo-error.h" |
2926
|
49 #include "oct-env.h" |
3019
|
50 #include "pathsearch.h" |
1907
|
51 #include "str-vec.h" |
223
|
52 |
2492
|
53 #include <defaults.h> |
3933
|
54 #include "Cell.h" |
1355
|
55 #include "defun.h" |
|
56 #include "error.h" |
|
57 #include "file-io.h" |
1
|
58 #include "input.h" |
|
59 #include "lex.h" |
4113
|
60 #include "octave.h" |
1742
|
61 #include "oct-hist.h" |
3195
|
62 #include "oct-obj.h" |
2375
|
63 #include "ops.h" |
1670
|
64 #include "toplev.h" |
1
|
65 #include "parse.h" |
562
|
66 #include "procstream.h" |
1817
|
67 #include "prog-args.h" |
1355
|
68 #include "sighandlers.h" |
|
69 #include "sysdep.h" |
2375
|
70 #include "ov.h" |
1
|
71 #include "unwind-prot.h" |
1355
|
72 #include "utils.h" |
|
73 #include "variables.h" |
2492
|
74 #include <version.h> |
1
|
75 |
4159
|
76 // Kluge. |
|
77 extern "C" void F77_FUNC (xerbla, XERBLA) (const char *, int, long); |
|
78 |
2910
|
79 extern void install_builtins (void); |
|
80 |
1704
|
81 #if !defined (HAVE_ATEXIT) && defined (HAVE_ON_EXIT) |
|
82 extern "C" int on_exit (); |
|
83 #define atexit on_exit |
|
84 #endif |
|
85 |
1907
|
86 // The command-line options. |
|
87 static string_vector octave_argv; |
1
|
88 |
2239
|
89 // TRUE means we read ~/.octaverc and ./.octaverc. |
|
90 // (--norc; --no-init-file; -f) |
|
91 static bool read_init_files = true; |
|
92 |
|
93 // TRUE means we read the site-wide octaverc files. |
|
94 // (--norc; --no-site-file; -f) |
2240
|
95 static bool read_site_files = true; |
1
|
96 |
3019
|
97 // TRUE means we don't print the usual startup message. |
616
|
98 // (--quiet; --silent; -q) |
2239
|
99 static bool inhibit_startup_message = false; |
1
|
100 |
3019
|
101 // TRUE means we turn on compatibility options. |
1410
|
102 // (--traditional) |
2239
|
103 static bool traditional = false; |
1410
|
104 |
3019
|
105 // If TRUE, print verbose info in some cases. |
1825
|
106 // (--verbose; -V) |
2239
|
107 static bool verbose_flag = false; |
1825
|
108 |
1
|
109 // Usage message |
139
|
110 static const char *usage_string = |
5189
|
111 "octave [-?HVdfhiqvx] [--debug] [--echo-commands] [--eval CODE]\n\ |
|
112 [--exec-path path] [--help] [--info-file file] [--info-program prog]\n\ |
4356
|
113 [--interactive] [--no-history] [--no-init-file] [--no-line-editing]\n\ |
|
114 [--no-site-file] [-p path] [--path path] [--silent] [--traditional]\n\ |
|
115 [--verbose] [--version] [file]"; |
1
|
116 |
1358
|
117 // This is here so that it's more likely that the usage message and |
1410
|
118 // the real set of options will agree. Note: the `+' must come first |
|
119 // to prevent getopt from permuting arguments! |
3180
|
120 static const char *short_opts = "+?HVdfhip:qvx"; |
139
|
121 |
5189
|
122 // The code to evaluate at startup (--eval CODE) |
|
123 static std::string code_to_eval; |
|
124 |
|
125 // If TRUE, don't exit after evaluating code given by --eval option. |
|
126 static bool persist = false; |
4356
|
127 |
1103
|
128 // Long options. See the comments in getopt.h for the meanings of the |
|
129 // fields in this structure. |
5189
|
130 #define EVAL_OPTION 1 |
|
131 #define EXEC_PATH_OPTION 2 |
4356
|
132 #define INFO_FILE_OPTION 3 |
|
133 #define INFO_PROG_OPTION 4 |
|
134 #define NO_INIT_FILE_OPTION 5 |
|
135 #define NO_LINE_EDITING_OPTION 6 |
|
136 #define NO_SITE_FILE_OPTION 7 |
5189
|
137 #define PERSIST_OPTION 8 |
|
138 #define TRADITIONAL_OPTION 9 |
1817
|
139 long_options long_opts[] = |
139
|
140 { |
1855
|
141 { "debug", prog_args::no_arg, 0, 'd' }, |
2277
|
142 { "braindead", prog_args::no_arg, 0, TRADITIONAL_OPTION }, |
1855
|
143 { "echo-commands", prog_args::no_arg, 0, 'x' }, |
5189
|
144 { "eval", prog_args::required_arg, 0, EVAL_OPTION }, |
1855
|
145 { "exec-path", prog_args::required_arg, 0, EXEC_PATH_OPTION }, |
|
146 { "help", prog_args::no_arg, 0, 'h' }, |
|
147 { "info-file", prog_args::required_arg, 0, INFO_FILE_OPTION }, |
|
148 { "info-program", prog_args::required_arg, 0, INFO_PROG_OPTION }, |
|
149 { "interactive", prog_args::no_arg, 0, 'i' }, |
3180
|
150 { "no-history", prog_args::no_arg, 0, 'H' }, |
2239
|
151 { "no-init-file", prog_args::no_arg, 0, NO_INIT_FILE_OPTION }, |
1855
|
152 { "no-line-editing", prog_args::no_arg, 0, NO_LINE_EDITING_OPTION }, |
2239
|
153 { "no-site-file", prog_args::no_arg, 0, NO_SITE_FILE_OPTION }, |
1855
|
154 { "norc", prog_args::no_arg, 0, 'f' }, |
|
155 { "path", prog_args::required_arg, 0, 'p' }, |
5189
|
156 { "persist", prog_args::no_arg, 0, PERSIST_OPTION }, |
1855
|
157 { "quiet", prog_args::no_arg, 0, 'q' }, |
|
158 { "silent", prog_args::no_arg, 0, 'q' }, |
|
159 { "traditional", prog_args::no_arg, 0, TRADITIONAL_OPTION }, |
|
160 { "verbose", prog_args::no_arg, 0, 'V' }, |
|
161 { "version", prog_args::no_arg, 0, 'v' }, |
3019
|
162 { 0, 0, 0, 0 } |
139
|
163 }; |
1
|
164 |
1355
|
165 // Store the command-line options for later use. |
|
166 |
|
167 static void |
|
168 intern_argv (int argc, char **argv) |
|
169 { |
4892
|
170 symbol_record *nargin_sr = top_level_sym_tab->lookup ("__nargin__", true); |
|
171 |
|
172 nargin_sr->mark_as_static (); |
|
173 |
|
174 nargin_sr->define (argc-1); |
3195
|
175 |
4587
|
176 Cell args; |
3195
|
177 |
1355
|
178 if (argc > 1) |
|
179 { |
3933
|
180 Array<octave_value> tmp (argc-1); |
|
181 |
2495
|
182 // Skip program name in argv. |
3933
|
183 int i = argc; |
|
184 while (--i > 0) |
|
185 tmp(i-1) = octave_value (*(argv+i)); |
1411
|
186 |
4587
|
187 args = Cell (tmp, argc-1, 1); |
1355
|
188 } |
1907
|
189 |
4587
|
190 bind_builtin_constant ("argv", args, true, true); |
1355
|
191 } |
|
192 |
1792
|
193 static void |
|
194 initialize_pathsearch (void) |
|
195 { |
|
196 // This may seem odd, but doing it this way means that we don't have |
|
197 // to modify the kpathsea library... |
|
198 |
3523
|
199 std::string odb = octave_env::getenv ("OCTAVE_DB_PATH"); |
3141
|
200 |
|
201 // For backward compatibility. |
1792
|
202 |
2926
|
203 if (odb.empty ()) |
3141
|
204 odb = octave_env::getenv ("OCTAVE_DB_DIR"); |
1792
|
205 |
3141
|
206 if (odb.empty ()) |
4101
|
207 odb = Vdata_dir + file_ops::dir_sep_str + "octave:" |
|
208 + Vlibexec_dir + file_ops::dir_sep_str + "octave"; |
1792
|
209 } |
|
210 |
581
|
211 // Initialize by reading startup files. |
|
212 |
1
|
213 static void |
|
214 execute_startup_files (void) |
|
215 { |
2985
|
216 unwind_protect::begin_frame ("execute_startup_files"); |
315
|
217 |
3019
|
218 unwind_protect_bool (input_from_startup_file); |
1651
|
219 |
3019
|
220 input_from_startup_file = true; |
315
|
221 |
793
|
222 int verbose = (verbose_flag && ! inhibit_startup_message); |
578
|
223 |
2239
|
224 if (read_site_files) |
1
|
225 { |
2239
|
226 // Execute commands from the site-wide configuration file. |
|
227 // First from the file $(prefix)/lib/octave/site/m/octaverc |
|
228 // (if it exists), then from the file |
3597
|
229 // $(prefix)/share/octave/$(version)/m/octaverc (if it exists). |
1755
|
230 |
2897
|
231 parse_and_execute (Vlocal_site_defaults_file, verbose); |
1755
|
232 |
2897
|
233 parse_and_execute (Vsite_defaults_file, verbose); |
1
|
234 } |
|
235 |
2239
|
236 if (read_init_files) |
|
237 { |
2512
|
238 // Try to execute commands from $HOME/$OCTAVE_INITFILE and |
|
239 // $OCTAVE_INITFILE. If $OCTAVE_INITFILE is not set, .octaverc |
|
240 // is assumed. |
2239
|
241 |
4804
|
242 bool home_rc_already_executed = false; |
2512
|
243 |
3523
|
244 std::string initfile = octave_env::getenv ("OCTAVE_INITFILE"); |
2512
|
245 |
2926
|
246 if (initfile.empty ()) |
2512
|
247 initfile = ".octaverc"; |
|
248 |
3523
|
249 std::string home_dir = octave_env::get_home_directory (); |
2926
|
250 |
4804
|
251 std::string home_rc = octave_env::make_absolute (initfile, home_dir); |
|
252 |
|
253 std::string local_rc; |
2239
|
254 |
2926
|
255 if (! home_dir.empty ()) |
2239
|
256 { |
2897
|
257 parse_and_execute (home_rc, verbose); |
2239
|
258 |
|
259 // Names alone are not enough. |
|
260 |
|
261 file_stat fs_home_rc (home_rc); |
|
262 |
|
263 if (fs_home_rc) |
|
264 { |
4804
|
265 // We want to check for curr_dir after executing home_rc |
|
266 // because doing that may change the working directory. |
|
267 |
|
268 std::string curr_dir = octave_env::getcwd (); |
|
269 |
|
270 local_rc = octave_env::make_absolute (initfile, curr_dir); |
|
271 |
2512
|
272 file_stat fs_dot_rc (local_rc); |
2239
|
273 |
|
274 if (fs_dot_rc && fs_home_rc.ino () == fs_dot_rc.ino ()) |
4804
|
275 home_rc_already_executed = true; |
2239
|
276 } |
|
277 } |
|
278 |
|
279 if (! home_rc_already_executed) |
4804
|
280 { |
|
281 if (local_rc.empty ()) |
|
282 { |
|
283 std::string curr_dir = octave_env::getcwd (); |
|
284 |
|
285 local_rc = octave_env::make_absolute (initfile, curr_dir); |
|
286 } |
|
287 |
|
288 parse_and_execute (local_rc, verbose); |
|
289 } |
2239
|
290 } |
315
|
291 |
2985
|
292 unwind_protect::run_frame ("execute_startup_files"); |
1
|
293 } |
|
294 |
5189
|
295 static int |
|
296 execute_eval_option_code (const std::string& code) |
|
297 { |
|
298 unwind_protect::begin_frame ("execute_eval_option_code"); |
|
299 |
|
300 unwind_protect_bool (interactive); |
|
301 |
|
302 interactive = false; |
|
303 |
|
304 int parse_status = 0; |
|
305 |
|
306 eval_string (code, false, parse_status, 0); |
|
307 |
|
308 unwind_protect::run_frame ("execute_eval_option_code"); |
|
309 |
|
310 return parse_status; |
|
311 } |
|
312 |
|
313 static void |
|
314 restore_program_name (void *) |
|
315 { |
|
316 bind_builtin_variable ("program_invocation_name", |
|
317 octave_env::get_program_invocation_name ()); |
|
318 |
|
319 bind_builtin_variable ("program_name", octave_env::get_program_name ()); |
|
320 } |
|
321 |
|
322 static void |
|
323 execute_command_line_file (const std::string& fname) |
|
324 { |
|
325 unwind_protect::begin_frame ("execute_command_line_file"); |
|
326 |
|
327 unwind_protect_bool (interactive); |
|
328 unwind_protect_bool (reading_script_file); |
|
329 unwind_protect_bool (input_from_command_line_file); |
|
330 |
|
331 unwind_protect_str (curr_fcn_file_name); |
|
332 unwind_protect_str (curr_fcn_file_full_name); |
|
333 |
|
334 unwind_protect::add (restore_program_name, 0); |
|
335 |
|
336 interactive = false; |
|
337 reading_script_file = true; |
|
338 input_from_command_line_file = true; |
|
339 |
|
340 curr_fcn_file_name = fname; |
|
341 curr_fcn_file_full_name = curr_fcn_file_name; |
|
342 |
|
343 bind_builtin_variable ("program_invocation_name", curr_fcn_file_name); |
|
344 |
|
345 size_t pos = curr_fcn_file_name.find_last_of (file_ops::dir_sep_chars); |
|
346 |
|
347 std::string tmp = (pos != NPOS) |
|
348 ? curr_fcn_file_name.substr (pos+1) : curr_fcn_file_name; |
|
349 |
|
350 bind_builtin_variable ("program_name", tmp); |
|
351 |
|
352 parse_and_execute (fname, false, "octave"); |
|
353 |
|
354 unwind_protect::run_frame ("execute_command_line_file"); |
|
355 } |
|
356 |
581
|
357 // Usage message with extra help. |
|
358 |
1
|
359 static void |
|
360 verbose_usage (void) |
|
361 { |
3922
|
362 std::cout << OCTAVE_NAME_VERSION_COPYRIGHT_COPYING_AND_WARRANTY "\n\ |
1613
|
363 \n\ |
|
364 Usage: octave [options]\n\ |
|
365 \n\ |
|
366 Options:\n\ |
1119
|
367 \n\ |
3180
|
368 --debug, -d Enter parser debugging mode.\n\ |
|
369 --echo-commands, -x Echo commands as they are executed.\n\ |
1613
|
370 --exec-path PATH Set path for executing subprograms.\n\ |
5189
|
371 --eval CODE Evaluate CODE and exit when done unless --persist.\n\ |
3180
|
372 --help, -h, -? Print short help message and exit.\n\ |
1613
|
373 --info-file FILE Use top-level info file FILE.\n\ |
|
374 --info-program PROGRAM Use PROGRAM for reading info files.\n\ |
3180
|
375 --interactive, -i Force interactive behavior.\n\ |
|
376 --no-history, -H Don't save commands to the history list\n\ |
2470
|
377 --no-init-file Don't read the ~/.octaverc or .octaverc files.\n\ |
2212
|
378 --no-line-editing Don't use readline for command-line editing.\n\ |
2470
|
379 --no-site-file Don't read the site-wide octaverc file.\n\ |
3238
|
380 --norc, -f Don't read any initialization files.\n\ |
3180
|
381 --path PATH, -p PATH Set initial LOADPATH to PATH.\n\ |
5189
|
382 --persist Go interactive after --eval or reading from FILE.\n\ |
3180
|
383 --silent, -q Don't print message at startup.\n\ |
1613
|
384 --traditional Set compatibility variables.\n\ |
3180
|
385 --verbose, -V Enable verbose output in some cases.\n\ |
|
386 --version, -v Print version number and exit.\n\ |
1119
|
387 \n\ |
1613
|
388 FILE Execute commands from FILE.\n\ |
4829
|
389 \n" |
|
390 OCTAVE_WWW_STATEMENT "\n\ |
4356
|
391 \n" |
|
392 OCTAVE_CONTRIB_STATEMENT "\n\ |
|
393 \n" |
|
394 OCTAVE_BUGS_STATEMENT "\n"; |
285
|
395 |
613
|
396 exit (0); |
1
|
397 } |
|
398 |
581
|
399 // Terse usage messsage. |
|
400 |
1
|
401 static void |
|
402 usage (void) |
|
403 { |
3531
|
404 std::cerr << "usage: " << usage_string << "\n"; |
1
|
405 exit (1); |
|
406 } |
|
407 |
|
408 static void |
|
409 print_version_and_exit (void) |
|
410 { |
3922
|
411 std::cout << OCTAVE_NAME_VERSION_COPYRIGHT_COPYING_WARRANTY_AND_BUGS "\n"; |
1
|
412 exit (0); |
|
413 } |
|
414 |
721
|
415 static void |
|
416 initialize_error_handlers () |
|
417 { |
|
418 set_liboctave_error_handler (error); |
3325
|
419 set_liboctave_warning_handler (warning); |
721
|
420 } |
|
421 |
1410
|
422 // What happens on --traditional. |
|
423 |
|
424 static void |
|
425 maximum_braindamage (void) |
|
426 { |
5189
|
427 persist = true; |
|
428 |
1410
|
429 bind_builtin_variable ("PS1", ">> "); |
|
430 bind_builtin_variable ("PS2", ""); |
4324
|
431 bind_builtin_variable ("beep_on_error", true); |
|
432 bind_builtin_variable ("crash_dumps_octave_core", false); |
1410
|
433 bind_builtin_variable ("default_save_format", "mat-binary"); |
4324
|
434 bind_builtin_variable ("fixed_point_format", true); |
|
435 bind_builtin_variable ("page_screen_output", false); |
|
436 bind_builtin_variable ("print_empty_dimensions", false); |
|
437 bind_builtin_variable ("warn_function_name_clash", false); |
4476
|
438 bind_builtin_variable ("warn_matlab_incompatible", true); |
1410
|
439 } |
|
440 |
581
|
441 // You guessed it. |
|
442 |
1
|
443 int |
4368
|
444 octave_main (int argc, char **argv, int embedded) |
1
|
445 { |
3019
|
446 octave_env::set_program_name (argv[0]); |
|
447 |
2205
|
448 // The order of these calls is important. The call to |
2926
|
449 // install_defaults must come before install_builtins because |
3019
|
450 // default variable values must be available for the variables to be |
2205
|
451 // installed, and the call to install_builtins must come before the |
|
452 // options are processed because some command line options override |
|
453 // defaults by calling bind_builtin_variable. |
721
|
454 |
1
|
455 sysdep_init (); |
|
456 |
4159
|
457 // The idea here is to force xerbla to be referenced so that we will |
|
458 // link to our own version instead of the one provided by the BLAS |
|
459 // library. But octave_NaN should never be -1, so we should never |
|
460 // actually call xerbla. |
|
461 |
|
462 if (octave_NaN == -1) |
|
463 F77_FUNC (xerbla, XERBLA) ("octave", 13, 6L); |
|
464 |
721
|
465 initialize_error_handlers (); |
223
|
466 |
2926
|
467 install_defaults (); |
1
|
468 |
1792
|
469 initialize_pathsearch (); |
1744
|
470 |
4368
|
471 if (! embedded) |
|
472 install_signal_handlers (); |
2205
|
473 |
|
474 initialize_file_io (); |
|
475 |
2375
|
476 initialize_symbol_tables (); |
|
477 |
|
478 install_types (); |
|
479 |
|
480 install_ops (); |
2205
|
481 |
|
482 install_builtins (); |
|
483 |
1817
|
484 prog_args args (argc, argv, short_opts, long_opts); |
|
485 |
139
|
486 int optc; |
1817
|
487 while ((optc = args.getopt ()) != EOF) |
1
|
488 { |
139
|
489 switch (optc) |
1
|
490 { |
3180
|
491 case 'H': |
4324
|
492 bind_builtin_variable ("saving_history", false); |
3180
|
493 break; |
|
494 |
793
|
495 case 'V': |
2239
|
496 verbose_flag = true; |
793
|
497 break; |
|
498 |
1
|
499 case 'd': |
4753
|
500 // This is the same as yydebug in parse.y. |
|
501 octave_debug++; |
1
|
502 break; |
777
|
503 |
1
|
504 case 'f': |
2239
|
505 read_init_files = false; |
|
506 read_site_files = false; |
1
|
507 break; |
777
|
508 |
1
|
509 case 'h': |
|
510 case '?': |
|
511 verbose_usage (); |
|
512 break; |
777
|
513 |
1
|
514 case 'i': |
3019
|
515 forced_interactive = true; |
1
|
516 break; |
777
|
517 |
1
|
518 case 'p': |
1817
|
519 if (args.optarg ()) |
2205
|
520 bind_builtin_variable ("LOADPATH", args.optarg ()); |
1
|
521 break; |
777
|
522 |
1
|
523 case 'q': |
2239
|
524 inhibit_startup_message = true; |
1
|
525 break; |
777
|
526 |
1
|
527 case 'x': |
2205
|
528 { |
|
529 double tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_CMD_LINE); |
|
530 bind_builtin_variable ("echo_executing_commands", tmp); |
|
531 } |
1
|
532 break; |
777
|
533 |
1
|
534 case 'v': |
|
535 print_version_and_exit (); |
|
536 break; |
777
|
537 |
5189
|
538 case EVAL_OPTION: |
|
539 if (args.optarg ()) |
|
540 code_to_eval = args.optarg (); |
|
541 break; |
|
542 |
1613
|
543 case EXEC_PATH_OPTION: |
1817
|
544 if (args.optarg ()) |
2205
|
545 bind_builtin_variable ("EXEC_PATH", args.optarg ()); |
1613
|
546 break; |
|
547 |
186
|
548 case INFO_FILE_OPTION: |
1817
|
549 if (args.optarg ()) |
2205
|
550 bind_builtin_variable ("INFO_FILE", args.optarg ()); |
186
|
551 break; |
777
|
552 |
1613
|
553 case INFO_PROG_OPTION: |
1817
|
554 if (args.optarg ()) |
2205
|
555 bind_builtin_variable ("INFO_PROGRAM", args.optarg ()); |
1613
|
556 break; |
|
557 |
2239
|
558 case NO_INIT_FILE_OPTION: |
|
559 read_init_files = false; |
|
560 break; |
|
561 |
1821
|
562 case NO_LINE_EDITING_OPTION: |
3019
|
563 line_editing = false; |
2239
|
564 break; |
|
565 |
|
566 case NO_SITE_FILE_OPTION: |
|
567 read_site_files = 0; |
1821
|
568 break; |
|
569 |
1410
|
570 case TRADITIONAL_OPTION: |
2239
|
571 traditional = true; |
1410
|
572 break; |
|
573 |
5189
|
574 case PERSIST_OPTION: |
|
575 persist = true; |
|
576 break; |
|
577 |
1
|
578 default: |
|
579 usage (); |
|
580 break; |
|
581 } |
|
582 } |
|
583 |
1651
|
584 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
2077
|
585 // Make sure we clean up when we exit. Also allow users to register |
|
586 // functions. If we don't have atexit or on_exit, we're going to |
|
587 // leave some junk files around if we exit abnormally. |
|
588 |
|
589 atexit (do_octave_atexit); |
318
|
590 #endif |
1
|
591 |
1358
|
592 // These can come after command line args since none of them set any |
|
593 // defaults that might be changed by command line options. |
581
|
594 |
2926
|
595 initialize_command_input (); |
315
|
596 |
578
|
597 if (! inhibit_startup_message) |
3538
|
598 std::cout << OCTAVE_STARTUP_MESSAGE "\n" << std::endl; |
578
|
599 |
1410
|
600 if (traditional) |
|
601 maximum_braindamage (); |
|
602 |
4217
|
603 octave_interpreter_ready = true; |
|
604 |
|
605 execute_default_pkg_add_files (); |
|
606 |
2239
|
607 execute_startup_files (); |
1651
|
608 |
2926
|
609 command_history::read (false); |
1
|
610 |
578
|
611 if (! inhibit_startup_message && reading_startup_message_printed) |
3538
|
612 std::cout << std::endl; |
578
|
613 |
5189
|
614 // Is input coming from a terminal? If so, we are probably |
|
615 // interactive. |
1358
|
616 |
5189
|
617 interactive = (! embedded |
|
618 && isatty (fileno (stdin)) && isatty (fileno (stdout))); |
|
619 |
|
620 if (! interactive) |
|
621 line_editing = false; |
1
|
622 |
1358
|
623 // If there is an extra argument, see if it names a file to read. |
|
624 // Additional arguments are taken as command line options for the |
|
625 // script. |
1
|
626 |
1817
|
627 int last_arg_idx = args.optind (); |
3019
|
628 |
1817
|
629 int remaining_args = argc - last_arg_idx; |
3019
|
630 |
5189
|
631 if (! code_to_eval.empty ()) |
149
|
632 { |
5189
|
633 // We probably want all the args for an --eval option. |
1355
|
634 |
|
635 intern_argv (argc, argv); |
|
636 |
5189
|
637 int parse_status = execute_eval_option_code (code_to_eval); |
|
638 |
|
639 if (! (persist && remaining_args > 0)) |
|
640 return (parse_status || error_state ? 1 : 0); |
1
|
641 } |
|
642 |
5189
|
643 if (remaining_args > 0) |
|
644 { |
|
645 // If we are running an executable script (#! /bin/octave) then |
|
646 // we should only see the args passed to the script. |
|
647 |
|
648 intern_argv (remaining_args, argv+last_arg_idx); |
|
649 |
|
650 execute_command_line_file (argv[last_arg_idx]); |
|
651 |
|
652 if (! persist) |
|
653 return (error_state ? 1 : 0); |
|
654 } |
|
655 |
|
656 // Avoid counting commands executed from startup files. |
|
657 |
|
658 command_editor::reset_current_command_number (1); |
|
659 |
|
660 // Now argv should have the full set of args. |
|
661 intern_argv (argc, argv); |
|
662 |
|
663 if (! embedded) |
|
664 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
|
665 |
1358
|
666 // Force input to be echoed if not really interactive, but the user |
|
667 // has forced interactive behavior. |
1
|
668 |
1907
|
669 if (! interactive && forced_interactive) |
287
|
670 { |
2926
|
671 command_editor::blink_matching_paren (false); |
1588
|
672 |
|
673 // XXX FIXME XXX -- is this the right thing to do? |
|
674 |
4233
|
675 bind_builtin_variable ("echo_executing_commands", ECHO_CMD_LINE); |
287
|
676 } |
1
|
677 |
4368
|
678 if (embedded) |
|
679 return 1; |
|
680 |
5189
|
681 int retval = main_loop (); |
1
|
682 |
1005
|
683 if (retval == 1 && ! error_state) |
|
684 retval = 0; |
|
685 |
1
|
686 clean_up_and_exit (retval); |
4247
|
687 |
|
688 return 0; |
1
|
689 } |
|
690 |
560
|
691 /* |
1
|
692 ;;; Local Variables: *** |
|
693 ;;; mode: C++ *** |
|
694 ;;; End: *** |
|
695 */ |