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