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 |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
|
23 // Born February 20, 1992. |
|
24 |
240
|
25 #ifdef HAVE_CONFIG_H |
1192
|
26 #include <config.h> |
1
|
27 #endif |
|
28 |
1355
|
29 #include <cassert> |
|
30 #include <csignal> |
|
31 #include <cstdlib> |
|
32 #include <cstring> |
|
33 #include <ctime> |
|
34 |
|
35 #include <fstream.h> |
|
36 #include <iostream.h> |
|
37 #include <strstream.h> |
|
38 |
|
39 #ifdef HAVE_UNISTD_H |
2442
|
40 #ifdef HAVE_SYS_TYPES_H |
1
|
41 #include <sys/types.h> |
2442
|
42 #endif |
139
|
43 #include <unistd.h> |
|
44 #endif |
1355
|
45 |
2926
|
46 #include "cmd-edit.h" |
|
47 #include "file-stat.h" |
240
|
48 #include "lo-error.h" |
2926
|
49 #include "oct-env.h" |
3019
|
50 #include "pathsearch.h" |
1907
|
51 #include "str-vec.h" |
223
|
52 |
2492
|
53 #include <defaults.h> |
1355
|
54 #include "defun.h" |
703
|
55 #include "dynamic-ld.h" |
1355
|
56 #include "error.h" |
|
57 #include "file-io.h" |
1
|
58 #include "input.h" |
|
59 #include "lex.h" |
1742
|
60 #include "oct-hist.h" |
2375
|
61 #include "ops.h" |
1670
|
62 #include "toplev.h" |
1
|
63 #include "parse.h" |
2982
|
64 #include "pt-plot.h" |
562
|
65 #include "procstream.h" |
1817
|
66 #include "prog-args.h" |
1355
|
67 #include "sighandlers.h" |
|
68 #include "sysdep.h" |
2375
|
69 #include "ov.h" |
1
|
70 #include "unwind-prot.h" |
1355
|
71 #include "utils.h" |
|
72 #include "variables.h" |
2492
|
73 #include <version.h> |
1
|
74 |
2910
|
75 extern void install_builtins (void); |
|
76 |
1704
|
77 #if !defined (HAVE_ATEXIT) && defined (HAVE_ON_EXIT) |
|
78 extern "C" int on_exit (); |
|
79 #define atexit on_exit |
|
80 #endif |
|
81 |
1907
|
82 // The command-line options. |
|
83 static string_vector octave_argv; |
1
|
84 |
2239
|
85 // TRUE means we read ~/.octaverc and ./.octaverc. |
|
86 // (--norc; --no-init-file; -f) |
|
87 static bool read_init_files = true; |
|
88 |
|
89 // TRUE means we read the site-wide octaverc files. |
|
90 // (--norc; --no-site-file; -f) |
2240
|
91 static bool read_site_files = true; |
1
|
92 |
3019
|
93 // TRUE means we don't print the usual startup message. |
616
|
94 // (--quiet; --silent; -q) |
2239
|
95 static bool inhibit_startup_message = false; |
1
|
96 |
3019
|
97 // TRUE means we turn on compatibility options. |
1410
|
98 // (--traditional) |
2239
|
99 static bool traditional = false; |
1410
|
100 |
3019
|
101 // If TRUE, print verbose info in some cases. |
1825
|
102 // (--verbose; -V) |
2239
|
103 static bool verbose_flag = false; |
1825
|
104 |
1
|
105 // Usage message |
139
|
106 static const char *usage_string = |
1613
|
107 "octave [-?Vdfhiqvx] [--debug] [--echo-commands] [--exec-path path]\n\ |
2239
|
108 [--help] [--info-file file] [--info-program prog] [--interactive]\n\ |
|
109 [--no-init-file] [--no-line-editing] [--no-site-file] [-p path]\n\ |
|
110 [--path path] [--silent] [--traditional] [--verbose] [--version] [file]"; |
1
|
111 |
1358
|
112 // This is here so that it's more likely that the usage message and |
1410
|
113 // the real set of options will agree. Note: the `+' must come first |
|
114 // to prevent getopt from permuting arguments! |
|
115 static const char *short_opts = "+?Vdfhip:qvx"; |
139
|
116 |
1103
|
117 // Long options. See the comments in getopt.h for the meanings of the |
|
118 // fields in this structure. |
1613
|
119 #define EXEC_PATH_OPTION 1 |
|
120 #define INFO_FILE_OPTION 2 |
|
121 #define INFO_PROG_OPTION 3 |
2240
|
122 #define NO_INIT_FILE_OPTION 4 |
|
123 #define NO_LINE_EDITING_OPTION 5 |
|
124 #define NO_SITE_FILE_OPTION 6 |
|
125 #define TRADITIONAL_OPTION 7 |
1817
|
126 long_options long_opts[] = |
139
|
127 { |
1855
|
128 { "debug", prog_args::no_arg, 0, 'd' }, |
2277
|
129 { "braindead", prog_args::no_arg, 0, TRADITIONAL_OPTION }, |
1855
|
130 { "echo-commands", prog_args::no_arg, 0, 'x' }, |
|
131 { "exec-path", prog_args::required_arg, 0, EXEC_PATH_OPTION }, |
|
132 { "help", prog_args::no_arg, 0, 'h' }, |
|
133 { "info-file", prog_args::required_arg, 0, INFO_FILE_OPTION }, |
|
134 { "info-program", prog_args::required_arg, 0, INFO_PROG_OPTION }, |
|
135 { "interactive", prog_args::no_arg, 0, 'i' }, |
2239
|
136 { "no-init-file", prog_args::no_arg, 0, NO_INIT_FILE_OPTION }, |
1855
|
137 { "no-line-editing", prog_args::no_arg, 0, NO_LINE_EDITING_OPTION }, |
2239
|
138 { "no-site-file", prog_args::no_arg, 0, NO_SITE_FILE_OPTION }, |
1855
|
139 { "norc", prog_args::no_arg, 0, 'f' }, |
|
140 { "path", prog_args::required_arg, 0, 'p' }, |
|
141 { "quiet", prog_args::no_arg, 0, 'q' }, |
|
142 { "silent", prog_args::no_arg, 0, 'q' }, |
|
143 { "traditional", prog_args::no_arg, 0, TRADITIONAL_OPTION }, |
|
144 { "verbose", prog_args::no_arg, 0, 'V' }, |
|
145 { "version", prog_args::no_arg, 0, 'v' }, |
3019
|
146 { 0, 0, 0, 0 } |
139
|
147 }; |
1
|
148 |
1355
|
149 // Store the command-line options for later use. |
|
150 |
|
151 static void |
|
152 intern_argv (int argc, char **argv) |
|
153 { |
|
154 if (argc > 1) |
|
155 { |
2495
|
156 // Skip program name in argv. |
1572
|
157 |
2495
|
158 octave_argv = string_vector (argv+1, argc-1); |
1411
|
159 |
|
160 bind_builtin_variable ("argv", octave_argv, 1, 1, 0); |
2404
|
161 bind_builtin_variable ("__argv__", octave_argv, 1, 1, 0); |
1355
|
162 } |
1907
|
163 |
2800
|
164 bind_builtin_variable ("nargin", static_cast<double> (argc-1), 1, 1, 0); |
1355
|
165 } |
|
166 |
1792
|
167 static void |
|
168 initialize_pathsearch (void) |
|
169 { |
|
170 // This may seem odd, but doing it this way means that we don't have |
|
171 // to modify the kpathsea library... |
|
172 |
2926
|
173 string odb = octave_env::getenv ("OCTAVE_DB_DIR"); |
1792
|
174 |
2926
|
175 if (odb.empty ()) |
1792
|
176 { |
2926
|
177 string oh = octave_env::getenv ("OCTAVE_HOME"); |
1792
|
178 |
2926
|
179 if (oh.empty ()) |
|
180 octave_env::putenv ("TEXMF", OCTAVE_DATADIR "/octave"); |
|
181 else |
1792
|
182 { |
2926
|
183 oh.append ("/lib/octave"); |
|
184 octave_env::putenv ("TEXMF", oh); |
1792
|
185 } |
|
186 } |
2926
|
187 else |
|
188 octave_env::putenv ("TEXMF", odb); |
1792
|
189 } |
|
190 |
581
|
191 // Initialize by reading startup files. |
|
192 |
1
|
193 static void |
|
194 execute_startup_files (void) |
|
195 { |
2985
|
196 unwind_protect::begin_frame ("execute_startup_files"); |
315
|
197 |
1651
|
198 // XXX FIXME XXX -- need to make it possible to set this in startup |
|
199 // files. |
1588
|
200 |
3019
|
201 unwind_protect_bool (input_from_startup_file); |
1651
|
202 |
3019
|
203 input_from_startup_file = true; |
315
|
204 |
793
|
205 int verbose = (verbose_flag && ! inhibit_startup_message); |
578
|
206 |
2239
|
207 if (read_site_files) |
1
|
208 { |
2239
|
209 // Execute commands from the site-wide configuration file. |
|
210 // First from the file $(prefix)/lib/octave/site/m/octaverc |
|
211 // (if it exists), then from the file |
|
212 // $(prefix)/lib/octave/$(version)/m/octaverc (if it exists). |
1755
|
213 |
2897
|
214 parse_and_execute (Vlocal_site_defaults_file, verbose); |
1755
|
215 |
2897
|
216 parse_and_execute (Vsite_defaults_file, verbose); |
1
|
217 } |
|
218 |
2239
|
219 if (read_init_files) |
|
220 { |
2512
|
221 // Try to execute commands from $HOME/$OCTAVE_INITFILE and |
|
222 // $OCTAVE_INITFILE. If $OCTAVE_INITFILE is not set, .octaverc |
|
223 // is assumed. |
2239
|
224 |
|
225 int home_rc_already_executed = 0; |
2512
|
226 |
2926
|
227 string initfile = octave_env::getenv ("OCTAVE_INITFILE"); |
2512
|
228 |
2926
|
229 if (initfile.empty ()) |
2512
|
230 initfile = ".octaverc"; |
|
231 |
2926
|
232 string home_dir = octave_env::get_home_directory (); |
|
233 |
|
234 string home_rc = home_dir + "/" + initfile; |
2512
|
235 string local_rc = string ("./") + initfile; |
2239
|
236 |
2926
|
237 if (! home_dir.empty ()) |
2239
|
238 { |
2897
|
239 parse_and_execute (home_rc, verbose); |
2239
|
240 |
|
241 // Names alone are not enough. |
|
242 |
|
243 file_stat fs_home_rc (home_rc); |
|
244 |
|
245 if (fs_home_rc) |
|
246 { |
2512
|
247 file_stat fs_dot_rc (local_rc); |
2239
|
248 |
|
249 if (fs_dot_rc && fs_home_rc.ino () == fs_dot_rc.ino ()) |
|
250 home_rc_already_executed = 1; |
|
251 } |
|
252 } |
|
253 |
|
254 if (! home_rc_already_executed) |
2897
|
255 parse_and_execute (local_rc, verbose); |
2239
|
256 } |
315
|
257 |
2985
|
258 unwind_protect::run_frame ("execute_startup_files"); |
1
|
259 } |
|
260 |
581
|
261 // Usage message with extra help. |
|
262 |
1
|
263 static void |
|
264 verbose_usage (void) |
|
265 { |
2375
|
266 cout << OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\ |
1613
|
267 \n\ |
|
268 Usage: octave [options]\n\ |
|
269 \n\ |
|
270 Options:\n\ |
1119
|
271 \n\ |
1613
|
272 -d, --debug Enter parser debugging mode.\n\ |
|
273 -x, --echo-commands Echo commands as they are executed.\n\ |
|
274 --exec-path PATH Set path for executing subprograms.\n\ |
|
275 -h, -?, --help Print short help message and exit.\n\ |
2239
|
276 -f, --norc Don't read any initialization files.\n\ |
1613
|
277 --info-file FILE Use top-level info file FILE.\n\ |
|
278 --info-program PROGRAM Use PROGRAM for reading info files.\n\ |
|
279 -i, --interactive Force interactive behavior.\n\ |
2470
|
280 --no-init-file Don't read the ~/.octaverc or .octaverc files.\n\ |
2212
|
281 --no-line-editing Don't use readline for command-line editing.\n\ |
2470
|
282 --no-site-file Don't read the site-wide octaverc file.\n\ |
1613
|
283 -p PATH, --path PATH Set initial LOADPATH to PATH.\n\ |
|
284 -q, --silent Don't print message at startup.\n\ |
|
285 --traditional Set compatibility variables.\n\ |
|
286 -V, --verbose Enable verbose output in some cases.\n\ |
|
287 -v, --version Print version number and exit.\n\ |
1119
|
288 \n\ |
1613
|
289 FILE Execute commands from FILE.\n\ |
2375
|
290 \n\ |
|
291 Additional information about Octave is available via the WWW at\n\ |
|
292 http://www.che.wisc.edu/octave.\n\ |
|
293 \n\ |
|
294 Please report bugs to the mailing list `bug-octave@bevo.che.wisc.edu'.\n"; |
285
|
295 |
613
|
296 exit (0); |
1
|
297 } |
|
298 |
581
|
299 // Terse usage messsage. |
|
300 |
1
|
301 static void |
|
302 usage (void) |
|
303 { |
613
|
304 cerr << "usage: " << usage_string << "\n"; |
1
|
305 exit (1); |
|
306 } |
|
307 |
|
308 static void |
|
309 print_version_and_exit (void) |
|
310 { |
1107
|
311 cout << OCTAVE_NAME_AND_VERSION << "\n"; |
1
|
312 exit (0); |
|
313 } |
|
314 |
721
|
315 static void |
|
316 initialize_error_handlers () |
|
317 { |
|
318 set_liboctave_error_handler (error); |
|
319 } |
|
320 |
1410
|
321 // What happens on --traditional. |
|
322 |
|
323 static void |
|
324 maximum_braindamage (void) |
|
325 { |
|
326 bind_builtin_variable ("PS1", ">> "); |
|
327 bind_builtin_variable ("PS2", ""); |
2402
|
328 bind_builtin_variable ("beep_on_error", 1.0); |
2993
|
329 bind_builtin_variable ("default_eval_print_flag", 0.0); |
1410
|
330 bind_builtin_variable ("default_save_format", "mat-binary"); |
2402
|
331 bind_builtin_variable ("define_all_return_values", 1.0); |
|
332 bind_builtin_variable ("do_fortran_indexing", 1.0); |
|
333 bind_builtin_variable ("empty_list_elements_ok", 1.0); |
|
334 bind_builtin_variable ("implicit_str_to_num_ok", 1.0); |
|
335 bind_builtin_variable ("ok_to_lose_imaginary_part", 1.0); |
|
336 bind_builtin_variable ("page_screen_output", 0.0); |
|
337 bind_builtin_variable ("prefer_column_vectors", 0.0); |
|
338 bind_builtin_variable ("print_empty_dimensions", 0.0); |
|
339 bind_builtin_variable ("treat_neg_dim_as_zero", 1.0); |
|
340 bind_builtin_variable ("warn_function_name_clash", 0.0); |
1410
|
341 bind_builtin_variable ("whitespace_in_literal_matrix", "traditional"); |
|
342 } |
|
343 |
581
|
344 // You guessed it. |
|
345 |
1
|
346 int |
|
347 main (int argc, char **argv) |
|
348 { |
3019
|
349 octave_env::set_program_name (argv[0]); |
|
350 |
2205
|
351 // The order of these calls is important. The call to |
2926
|
352 // install_defaults must come before install_builtins because |
3019
|
353 // default variable values must be available for the variables to be |
2205
|
354 // installed, and the call to install_builtins must come before the |
|
355 // options are processed because some command line options override |
|
356 // defaults by calling bind_builtin_variable. |
721
|
357 |
1
|
358 sysdep_init (); |
|
359 |
721
|
360 initialize_error_handlers (); |
223
|
361 |
2926
|
362 install_defaults (); |
1
|
363 |
1792
|
364 initialize_pathsearch (); |
1744
|
365 |
2205
|
366 install_signal_handlers (); |
|
367 |
|
368 initialize_file_io (); |
|
369 |
2375
|
370 initialize_symbol_tables (); |
|
371 |
|
372 install_types (); |
|
373 |
|
374 install_ops (); |
2205
|
375 |
|
376 install_builtins (); |
|
377 |
1817
|
378 prog_args args (argc, argv, short_opts, long_opts); |
|
379 |
139
|
380 int optc; |
1817
|
381 while ((optc = args.getopt ()) != EOF) |
1
|
382 { |
139
|
383 switch (optc) |
1
|
384 { |
793
|
385 case 'V': |
2239
|
386 verbose_flag = true; |
793
|
387 break; |
|
388 |
1
|
389 case 'd': |
|
390 yydebug++; |
|
391 break; |
777
|
392 |
1
|
393 case 'f': |
2239
|
394 read_init_files = false; |
|
395 read_site_files = false; |
1
|
396 break; |
777
|
397 |
1
|
398 case 'h': |
|
399 case '?': |
|
400 verbose_usage (); |
|
401 break; |
777
|
402 |
1
|
403 case 'i': |
3019
|
404 forced_interactive = true; |
1
|
405 break; |
777
|
406 |
1
|
407 case 'p': |
1817
|
408 if (args.optarg ()) |
2205
|
409 bind_builtin_variable ("LOADPATH", args.optarg ()); |
1
|
410 break; |
777
|
411 |
1
|
412 case 'q': |
2239
|
413 inhibit_startup_message = true; |
1
|
414 break; |
777
|
415 |
1
|
416 case 'x': |
2205
|
417 { |
|
418 double tmp = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_CMD_LINE); |
|
419 bind_builtin_variable ("echo_executing_commands", tmp); |
|
420 } |
1
|
421 break; |
777
|
422 |
1
|
423 case 'v': |
|
424 print_version_and_exit (); |
|
425 break; |
777
|
426 |
1613
|
427 case EXEC_PATH_OPTION: |
1817
|
428 if (args.optarg ()) |
2205
|
429 bind_builtin_variable ("EXEC_PATH", args.optarg ()); |
1613
|
430 break; |
|
431 |
186
|
432 case INFO_FILE_OPTION: |
1817
|
433 if (args.optarg ()) |
2205
|
434 bind_builtin_variable ("INFO_FILE", args.optarg ()); |
186
|
435 break; |
777
|
436 |
1613
|
437 case INFO_PROG_OPTION: |
1817
|
438 if (args.optarg ()) |
2205
|
439 bind_builtin_variable ("INFO_PROGRAM", args.optarg ()); |
1613
|
440 break; |
|
441 |
2239
|
442 case NO_INIT_FILE_OPTION: |
|
443 read_init_files = false; |
|
444 break; |
|
445 |
1821
|
446 case NO_LINE_EDITING_OPTION: |
3019
|
447 line_editing = false; |
2239
|
448 break; |
|
449 |
|
450 case NO_SITE_FILE_OPTION: |
|
451 read_site_files = 0; |
1821
|
452 break; |
|
453 |
1410
|
454 case TRADITIONAL_OPTION: |
2239
|
455 traditional = true; |
1410
|
456 break; |
|
457 |
1
|
458 default: |
|
459 usage (); |
|
460 break; |
|
461 } |
|
462 } |
|
463 |
1651
|
464 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
2077
|
465 // Make sure we clean up when we exit. Also allow users to register |
|
466 // functions. If we don't have atexit or on_exit, we're going to |
|
467 // leave some junk files around if we exit abnormally. |
|
468 |
|
469 atexit (do_octave_atexit); |
|
470 |
1
|
471 atexit (cleanup_tmp_files); |
318
|
472 #endif |
1
|
473 |
1358
|
474 // These can come after command line args since none of them set any |
|
475 // defaults that might be changed by command line options. |
581
|
476 |
2926
|
477 initialize_command_input (); |
315
|
478 |
578
|
479 if (! inhibit_startup_message) |
1106
|
480 cout << OCTAVE_STARTUP_MESSAGE "\n" << endl; |
578
|
481 |
1410
|
482 if (traditional) |
|
483 maximum_braindamage (); |
|
484 |
2239
|
485 execute_startup_files (); |
1651
|
486 |
2926
|
487 command_history::read (false); |
1
|
488 |
578
|
489 if (! inhibit_startup_message && reading_startup_message_printed) |
|
490 cout << endl; |
|
491 |
1358
|
492 // Avoid counting commands executed from startup files. |
|
493 |
2926
|
494 command_editor::reset_current_command_number (1); |
1
|
495 |
1358
|
496 // If there is an extra argument, see if it names a file to read. |
|
497 // Additional arguments are taken as command line options for the |
|
498 // script. |
1
|
499 |
1817
|
500 int last_arg_idx = args.optind (); |
3019
|
501 |
1817
|
502 int remaining_args = argc - last_arg_idx; |
3019
|
503 |
1355
|
504 if (remaining_args > 0) |
149
|
505 { |
3019
|
506 reading_script_file = true; |
|
507 |
1817
|
508 curr_fcn_file_name = argv[last_arg_idx]; |
1608
|
509 curr_fcn_file_full_name = curr_fcn_file_name; |
1411
|
510 |
984
|
511 FILE *infile = get_input_from_file (curr_fcn_file_name); |
1411
|
512 |
529
|
513 if (infile) |
287
|
514 { |
3019
|
515 input_from_command_line_file = true; |
1516
|
516 |
1411
|
517 bind_builtin_variable ("program_invocation_name", |
|
518 curr_fcn_file_name); |
|
519 |
1755
|
520 size_t pos = curr_fcn_file_name.rfind ('/'); |
|
521 |
|
522 string tmp = (pos != NPOS) |
|
523 ? curr_fcn_file_name.substr (pos+1) : curr_fcn_file_name; |
1411
|
524 |
|
525 bind_builtin_variable ("program_name", tmp); |
|
526 |
1817
|
527 intern_argv (remaining_args, argv+last_arg_idx); |
1411
|
528 |
2926
|
529 command_editor::blink_matching_paren (false); |
|
530 |
287
|
531 switch_to_buffer (create_buffer (infile)); |
|
532 } |
529
|
533 else |
|
534 clean_up_and_exit (1); |
1
|
535 } |
|
536 else |
|
537 { |
1355
|
538 // Is input coming from a terminal? If so, we are probably |
|
539 // interactive. |
1
|
540 |
|
541 interactive = (isatty (fileno (stdin)) && isatty (fileno (stdout))); |
1355
|
542 |
|
543 intern_argv (argc, argv); |
|
544 |
|
545 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
1
|
546 } |
|
547 |
1358
|
548 // Force input to be echoed if not really interactive, but the user |
|
549 // has forced interactive behavior. |
1
|
550 |
1907
|
551 if (! interactive && forced_interactive) |
287
|
552 { |
2926
|
553 command_editor::blink_matching_paren (false); |
1588
|
554 |
|
555 // XXX FIXME XXX -- is this the right thing to do? |
|
556 |
|
557 bind_builtin_variable ("echo_executing_commands", |
2800
|
558 static_cast<double> (ECHO_CMD_LINE)); |
287
|
559 } |
1
|
560 |
1410
|
561 if (! interactive) |
3019
|
562 line_editing = false; |
1
|
563 |
1907
|
564 int retval = main_loop (); |
1
|
565 |
1005
|
566 if (retval == 1 && ! error_state) |
|
567 retval = 0; |
|
568 |
1
|
569 clean_up_and_exit (retval); |
|
570 } |
|
571 |
560
|
572 /* |
1
|
573 ;;; Local Variables: *** |
|
574 ;;; mode: C++ *** |
|
575 ;;; End: *** |
|
576 */ |