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