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