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