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