1
|
1 // octave.cc -*- C++ -*- |
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 <csetjmp> |
|
32 #include <csignal> |
|
33 #include <cstdlib> |
|
34 #include <cstring> |
|
35 #include <ctime> |
|
36 |
|
37 #include <fstream.h> |
|
38 #include <iostream.h> |
|
39 #include <strstream.h> |
|
40 |
|
41 #ifdef HAVE_UNISTD_H |
1
|
42 #include <sys/types.h> |
139
|
43 #include <unistd.h> |
|
44 #endif |
1355
|
45 |
1
|
46 #include <pwd.h> |
636
|
47 |
139
|
48 #include "getopt.h" |
1
|
49 |
240
|
50 #include "lo-error.h" |
223
|
51 |
1355
|
52 #include "builtins.h" |
|
53 #include "defaults.h" |
|
54 #include "defun.h" |
703
|
55 #include "dynamic-ld.h" |
1355
|
56 #include "error.h" |
|
57 #include "file-io.h" |
|
58 #include "help.h" |
1
|
59 #include "input.h" |
|
60 #include "lex.h" |
1742
|
61 #include "oct-hist.h" |
1670
|
62 #include "toplev.h" |
1355
|
63 #include "pager.h" |
1
|
64 #include "parse.h" |
1355
|
65 #include "pathsearch.h" |
562
|
66 #include "procstream.h" |
1355
|
67 #include "sighandlers.h" |
|
68 #include "statdefs.h" |
|
69 #include "sysdep.h" |
1742
|
70 #include "pt-const.h" |
|
71 #include "pt-misc.h" |
|
72 #include "pt-plot.h" |
1
|
73 #include "unwind-prot.h" |
1355
|
74 #include "user-prefs.h" |
|
75 #include "utils.h" |
|
76 #include "variables.h" |
1
|
77 #include "version.h" |
|
78 |
1704
|
79 #if !defined (HAVE_ATEXIT) && defined (HAVE_ON_EXIT) |
|
80 extern "C" int on_exit (); |
|
81 #define atexit on_exit |
|
82 #endif |
|
83 |
1684
|
84 // This is from readline's paren.c: |
|
85 extern int rl_blink_matching_paren; |
1516
|
86 |
1
|
87 // Top level context (?) |
|
88 jmp_buf toplevel; |
|
89 |
|
90 // Nonzero means we read ~/.octaverc and ./.octaverc. |
616
|
91 // (--norc; --ignore-init-file; -f) |
1
|
92 static int read_init_files = 1; |
|
93 |
1358
|
94 // Nonzero means we don't print the usual startup message. |
616
|
95 // (--quiet; --silent; -q) |
1
|
96 static int inhibit_startup_message = 0; |
|
97 |
1410
|
98 // Nonzero means we turn on compatibility options. |
|
99 // (--traditional) |
|
100 static int traditional = 0; |
|
101 |
1
|
102 // Usage message |
139
|
103 static const char *usage_string = |
1613
|
104 "octave [-?Vdfhiqvx] [--debug] [--echo-commands] [--exec-path path]\n\ |
|
105 [--help] [--ignore-init-file] [--info-file file] [--info-program prog]\n\ |
|
106 [--interactive] [-p path] [--path path] [--silent] [--traditional]\n\ |
|
107 [--verbose] [--version] [file]"; |
1
|
108 |
1358
|
109 // This is here so that it's more likely that the usage message and |
1410
|
110 // the real set of options will agree. Note: the `+' must come first |
|
111 // to prevent getopt from permuting arguments! |
|
112 static const char *short_opts = "+?Vdfhip:qvx"; |
139
|
113 |
1103
|
114 // Long options. See the comments in getopt.h for the meanings of the |
|
115 // fields in this structure. |
1613
|
116 #define EXEC_PATH_OPTION 1 |
|
117 #define INFO_FILE_OPTION 2 |
|
118 #define INFO_PROG_OPTION 3 |
|
119 #define TRADITIONAL_OPTION 4 |
139
|
120 static struct option long_opts[] = |
|
121 { |
1103
|
122 { "debug", no_argument, 0, 'd' }, |
1613
|
123 { "echo-commands", no_argument, 0, 'x' }, |
|
124 { "exec-path", required_argument, 0, EXEC_PATH_OPTION }, |
1103
|
125 { "help", no_argument, 0, 'h' }, |
|
126 { "interactive", no_argument, 0, 'i' }, |
|
127 { "info-file", required_argument, 0, INFO_FILE_OPTION }, |
1613
|
128 { "info-program", required_argument, 0, INFO_PROG_OPTION }, |
|
129 { "ignore-init-file", no_argument, 0, 'f' }, |
1103
|
130 { "norc", no_argument, 0, 'f' }, |
|
131 { "path", required_argument, 0, 'p' }, |
|
132 { "quiet", no_argument, 0, 'q' }, |
|
133 { "silent", no_argument, 0, 'q' }, |
1410
|
134 { "traditional", no_argument, 0, TRADITIONAL_OPTION }, |
1103
|
135 { "verbose", no_argument, 0, 'V' }, |
|
136 { "version", no_argument, 0, 'v' }, |
|
137 { 0, 0, 0, 0 } |
139
|
138 }; |
1
|
139 |
1355
|
140 // Store the command-line options for later use. |
|
141 |
|
142 static void |
|
143 intern_argv (int argc, char **argv) |
|
144 { |
|
145 if (argc > 1) |
|
146 { |
1572
|
147 int max_len = 0; |
1355
|
148 for (int i = 1; i < argc; i++) |
1572
|
149 { |
|
150 int tmp_len = strlen (argv[i]); |
|
151 if (tmp_len > max_len) |
|
152 max_len = tmp_len; |
|
153 } |
|
154 |
|
155 octave_argv.resize (argc-1, max_len, 0); |
|
156 |
|
157 for (int i = 1; i < argc; i++) |
|
158 octave_argv.insert (argv[i], i-1, 0); |
1411
|
159 |
|
160 bind_builtin_variable ("argv", octave_argv, 1, 1, 0); |
1355
|
161 } |
|
162 } |
|
163 |
581
|
164 // Initialize some global variables for later use. |
|
165 |
1
|
166 static void |
|
167 initialize_globals (char *name) |
|
168 { |
1355
|
169 raw_prog_name = strsave (name); |
|
170 char *tmp = strrchr (raw_prog_name, '/'); |
|
171 prog_name = tmp ? strsave (tmp+1) : strsave (raw_prog_name); |
|
172 |
1
|
173 struct passwd *entry = getpwuid (getuid ()); |
|
174 if (entry) |
|
175 user_name = strsave (entry->pw_name); |
|
176 else |
|
177 user_name = strsave ("I have no name!"); |
|
178 endpwent (); |
|
179 |
|
180 char hostname[256]; |
|
181 if (gethostname (hostname, 255) < 0) |
|
182 host_name = strsave ("I have no host!"); |
|
183 else |
|
184 host_name = strsave (hostname); |
|
185 |
|
186 char *hd = getenv ("HOME"); |
529
|
187 if (hd) |
|
188 home_directory = strsave (hd); |
1
|
189 else |
529
|
190 home_directory = strsave ("I have no home!"); |
1
|
191 |
1613
|
192 exec_path = default_exec_path (); |
|
193 |
1
|
194 load_path = default_path (); |
186
|
195 |
|
196 info_file = default_info_file (); |
195
|
197 |
1613
|
198 info_prog = default_info_prog (); |
|
199 |
195
|
200 editor = default_editor (); |
1
|
201 } |
|
202 |
581
|
203 // Initialize by reading startup files. |
|
204 |
1
|
205 static void |
|
206 execute_startup_files (void) |
|
207 { |
315
|
208 begin_unwind_frame ("execute_startup_files"); |
|
209 |
1651
|
210 // XXX FIXME XXX -- need to make it possible to set this in startup |
|
211 // files. |
1588
|
212 |
1651
|
213 unwind_protect_int (input_from_startup_file); |
|
214 unwind_protect_int (user_pref.echo_executing_commands); |
|
215 |
|
216 input_from_startup_file = 1; |
1588
|
217 user_pref.echo_executing_commands = ECHO_OFF; |
315
|
218 |
793
|
219 int verbose = (verbose_flag && ! inhibit_startup_message); |
578
|
220 |
1477
|
221 // Execute commands from the site-wide configuration file. First |
|
222 // from the file $(prefix)/lib/octave/site/m/octaverc (if it exists), |
|
223 // then from the file $(prefix)/lib/octave/$(version)/m/octaverc (if |
|
224 // it exists). |
|
225 |
|
226 char *lsd = get_local_site_defaults (); |
|
227 parse_and_execute (lsd, 0, verbose); |
1
|
228 |
|
229 char *sd = get_site_defaults (); |
578
|
230 parse_and_execute (sd, 0, verbose); |
1
|
231 |
1358
|
232 // Try to execute commands from $HOME/.octaverc and ./.octaverc. |
1
|
233 |
529
|
234 char *home_rc = 0; |
|
235 if (home_directory) |
1
|
236 { |
85
|
237 home_rc = strconcat (home_directory, "/.octaverc"); |
578
|
238 parse_and_execute (home_rc, 0, verbose); |
1
|
239 } |
|
240 |
1358
|
241 // Names alone are not enough. |
85
|
242 |
|
243 struct stat home_rc_statbuf; |
|
244 stat (home_rc, &home_rc_statbuf); |
|
245 delete [] home_rc; |
|
246 |
|
247 struct stat dot_rc_statbuf; |
|
248 stat ("./.octaverc", &dot_rc_statbuf); |
|
249 |
|
250 if (home_rc_statbuf.st_ino != dot_rc_statbuf.st_ino) |
578
|
251 parse_and_execute ("./.octaverc", 0, verbose); |
315
|
252 |
|
253 run_unwind_frame ("execute_startup_files"); |
1
|
254 } |
|
255 |
581
|
256 // Usage message with extra help. |
|
257 |
1
|
258 static void |
|
259 verbose_usage (void) |
|
260 { |
1613
|
261 cout << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\ |
|
262 \n\ |
|
263 Usage: octave [options]\n\ |
|
264 \n\ |
|
265 Options:\n\ |
1119
|
266 \n\ |
1613
|
267 -d, --debug Enter parser debugging mode.\n\ |
|
268 -x, --echo-commands Echo commands as they are executed.\n\ |
|
269 --exec-path PATH Set path for executing subprograms.\n\ |
|
270 -h, -?, --help Print short help message and exit.\n\ |
|
271 -f, --ignore-init-file Don't read any initialization files.\n\ |
|
272 --info-file FILE Use top-level info file FILE.\n\ |
|
273 --info-program PROGRAM Use PROGRAM for reading info files.\n\ |
|
274 -i, --interactive Force interactive behavior.\n\ |
|
275 -p PATH, --path PATH Set initial LOADPATH to PATH.\n\ |
|
276 -q, --silent Don't print message at startup.\n\ |
|
277 --traditional Set compatibility variables.\n\ |
|
278 -V, --verbose Enable verbose output in some cases.\n\ |
|
279 -v, --version Print version number and exit.\n\ |
1119
|
280 \n\ |
1613
|
281 FILE Execute commands from FILE.\n\ |
1119
|
282 \n"; |
285
|
283 |
613
|
284 exit (0); |
1
|
285 } |
|
286 |
581
|
287 // Terse usage messsage. |
|
288 |
1
|
289 static void |
|
290 usage (void) |
|
291 { |
613
|
292 cerr << "usage: " << usage_string << "\n"; |
1
|
293 exit (1); |
|
294 } |
|
295 |
|
296 static void |
|
297 print_version_and_exit (void) |
|
298 { |
1107
|
299 cout << OCTAVE_NAME_AND_VERSION << "\n"; |
1
|
300 exit (0); |
|
301 } |
|
302 |
721
|
303 static void |
|
304 initialize_error_handlers () |
|
305 { |
|
306 set_liboctave_error_handler (error); |
|
307 } |
|
308 |
1410
|
309 // What happens on --traditional. |
|
310 |
|
311 static void |
|
312 maximum_braindamage (void) |
|
313 { |
|
314 bind_builtin_variable ("PS1", ">> "); |
|
315 bind_builtin_variable ("PS2", ""); |
1423
|
316 bind_builtin_variable ("beep_on_error", "true"); |
1410
|
317 bind_builtin_variable ("default_save_format", "mat-binary"); |
|
318 bind_builtin_variable ("define_all_return_values", "true"); |
|
319 bind_builtin_variable ("do_fortran_indexing", "true"); |
|
320 bind_builtin_variable ("empty_list_elements_ok", "true"); |
|
321 bind_builtin_variable ("implicit_str_to_num_ok", "true"); |
|
322 bind_builtin_variable ("ok_to_lose_imaginary_part", "true"); |
|
323 bind_builtin_variable ("page_screen_output", "false"); |
|
324 bind_builtin_variable ("prefer_column_vectors", "false"); |
|
325 bind_builtin_variable ("prefer_zero_one_indexing", "true"); |
|
326 bind_builtin_variable ("print_empty_dimensions", "false"); |
|
327 bind_builtin_variable ("treat_neg_dim_as_zero", "true"); |
|
328 bind_builtin_variable ("warn_function_name_clash", "false"); |
|
329 bind_builtin_variable ("whitespace_in_literal_matrix", "traditional"); |
|
330 } |
|
331 |
581
|
332 // You guessed it. |
|
333 |
1
|
334 int |
|
335 main (int argc, char **argv) |
|
336 { |
1588
|
337 int echo_commands = ECHO_OFF; |
|
338 |
1358
|
339 // The order of these calls is important, and initialize_globals |
|
340 // must come before the options are processed because some command |
|
341 // line options override defaults. |
721
|
342 |
|
343 init_user_prefs (); |
|
344 |
|
345 initialize_pager (); |
|
346 |
1
|
347 sysdep_init (); |
|
348 |
721
|
349 initialize_error_handlers (); |
223
|
350 |
1
|
351 initialize_globals (argv[0]); |
|
352 |
1744
|
353 initialize_pathsearch (argv[0]); |
|
354 |
139
|
355 int optc; |
|
356 while ((optc = getopt_long (argc, argv, short_opts, long_opts, 0)) != EOF) |
1
|
357 { |
139
|
358 switch (optc) |
1
|
359 { |
793
|
360 case 'V': |
|
361 verbose_flag++; |
|
362 break; |
|
363 |
1
|
364 case 'd': |
|
365 yydebug++; |
|
366 break; |
777
|
367 |
1
|
368 case 'f': |
|
369 read_init_files = 0; |
|
370 break; |
777
|
371 |
1
|
372 case 'h': |
|
373 case '?': |
|
374 verbose_usage (); |
|
375 break; |
777
|
376 |
1
|
377 case 'i': |
|
378 forced_interactive = 1; |
|
379 break; |
777
|
380 |
1
|
381 case 'p': |
529
|
382 if (optarg) |
139
|
383 load_path = strsave (optarg); |
1
|
384 break; |
777
|
385 |
1
|
386 case 'q': |
|
387 inhibit_startup_message = 1; |
|
388 break; |
777
|
389 |
1
|
390 case 'x': |
1588
|
391 echo_commands = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_CMD_LINE); |
1
|
392 break; |
777
|
393 |
1
|
394 case 'v': |
|
395 print_version_and_exit (); |
|
396 break; |
777
|
397 |
1613
|
398 case EXEC_PATH_OPTION: |
|
399 if (optarg) |
|
400 exec_path = strsave (optarg); |
|
401 break; |
|
402 |
186
|
403 case INFO_FILE_OPTION: |
529
|
404 if (optarg) |
186
|
405 info_file = strsave (optarg); |
|
406 break; |
777
|
407 |
1613
|
408 case INFO_PROG_OPTION: |
|
409 if (optarg) |
|
410 info_prog = strsave (optarg); |
|
411 break; |
|
412 |
1410
|
413 case TRADITIONAL_OPTION: |
|
414 traditional = 1; |
|
415 break; |
|
416 |
1
|
417 default: |
|
418 usage (); |
|
419 break; |
|
420 } |
|
421 } |
|
422 |
1651
|
423 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
1358
|
424 // Make sure we clean up when we exit. If we don't have atexit or |
|
425 // on_exit, we're going to leave some junk files around if we exit |
|
426 // abnormally. |
1
|
427 atexit (cleanup_tmp_files); |
318
|
428 #endif |
1
|
429 |
1358
|
430 // These can come after command line args since none of them set any |
|
431 // defaults that might be changed by command line options. |
581
|
432 |
636
|
433 install_signal_handlers (); |
|
434 |
1
|
435 initialize_file_io (); |
|
436 |
581
|
437 initialize_symbol_tables (); |
1
|
438 |
|
439 install_builtins (); |
|
440 |
315
|
441 initialize_readline (); |
|
442 |
721
|
443 init_dynamic_linker (); |
|
444 |
578
|
445 if (! inhibit_startup_message) |
1106
|
446 cout << OCTAVE_STARTUP_MESSAGE "\n" << endl; |
578
|
447 |
1410
|
448 if (traditional) |
|
449 maximum_braindamage (); |
|
450 |
1588
|
451 bind_builtin_variable ("echo_executing_commands", |
|
452 (double) echo_commands); |
|
453 |
1
|
454 if (read_init_files) |
1651
|
455 execute_startup_files (); |
|
456 |
|
457 initialize_history (); |
1
|
458 |
578
|
459 if (! inhibit_startup_message && reading_startup_message_printed) |
|
460 cout << endl; |
|
461 |
1358
|
462 // Avoid counting commands executed from startup files. |
|
463 |
1
|
464 current_command_number = 1; |
|
465 |
1358
|
466 // If there is an extra argument, see if it names a file to read. |
|
467 // Additional arguments are taken as command line options for the |
|
468 // script. |
1
|
469 |
149
|
470 int remaining_args = argc - optind; |
1355
|
471 if (remaining_args > 0) |
149
|
472 { |
984
|
473 reading_script_file = 1; |
|
474 curr_fcn_file_name = argv[optind]; |
1608
|
475 curr_fcn_file_full_name = curr_fcn_file_name; |
1411
|
476 |
984
|
477 FILE *infile = get_input_from_file (curr_fcn_file_name); |
1411
|
478 |
529
|
479 if (infile) |
287
|
480 { |
1516
|
481 input_from_command_line_file = 1; |
|
482 |
1411
|
483 bind_builtin_variable ("program_invocation_name", |
|
484 curr_fcn_file_name); |
|
485 |
1608
|
486 const char *tmp = strrchr (curr_fcn_file_name, '/'); |
1411
|
487 tmp = tmp ? tmp+1 : curr_fcn_file_name; |
|
488 |
|
489 bind_builtin_variable ("program_name", tmp); |
|
490 |
|
491 intern_argv (remaining_args, argv+optind); |
|
492 |
287
|
493 rl_blink_matching_paren = 0; |
|
494 switch_to_buffer (create_buffer (infile)); |
|
495 } |
529
|
496 else |
|
497 clean_up_and_exit (1); |
1
|
498 } |
|
499 else |
|
500 { |
1355
|
501 // Is input coming from a terminal? If so, we are probably |
|
502 // interactive. |
1
|
503 |
|
504 interactive = (isatty (fileno (stdin)) && isatty (fileno (stdout))); |
1355
|
505 |
|
506 intern_argv (argc, argv); |
|
507 |
|
508 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
1
|
509 } |
|
510 |
1358
|
511 // Force input to be echoed if not really interactive, but the user |
|
512 // has forced interactive behavior. |
1
|
513 |
|
514 if (!interactive && forced_interactive) |
287
|
515 { |
|
516 rl_blink_matching_paren = 0; |
1588
|
517 |
|
518 // XXX FIXME XXX -- is this the right thing to do? |
|
519 |
|
520 bind_builtin_variable ("echo_executing_commands", |
|
521 (double) ECHO_CMD_LINE); |
287
|
522 } |
1
|
523 |
1410
|
524 if (! interactive) |
1
|
525 using_readline = 0; |
|
526 |
1358
|
527 // Allow the user to interrupt us without exiting. |
1
|
528 |
|
529 if (setjmp (toplevel) != 0) |
|
530 { |
|
531 raw_mode (0); |
176
|
532 |
1
|
533 cout << "\n"; |
|
534 } |
|
535 |
|
536 can_interrupt = 1; |
|
537 |
1651
|
538 catch_interrupts (); |
1
|
539 |
1358
|
540 // The big loop. |
1
|
541 |
|
542 int retval; |
|
543 do |
|
544 { |
287
|
545 curr_sym_tab = top_level_sym_tab; |
|
546 |
1
|
547 reset_parser (); |
287
|
548 |
1
|
549 retval = yyparse (); |
287
|
550 |
529
|
551 if (retval == 0 && global_command) |
1
|
552 { |
|
553 global_command->eval (1); |
|
554 delete global_command; |
|
555 current_command_number++; |
|
556 } |
|
557 } |
|
558 while (retval == 0); |
|
559 |
1005
|
560 if (retval == 1 && ! error_state) |
|
561 retval = 0; |
|
562 |
1
|
563 clean_up_and_exit (retval); |
|
564 } |
|
565 |
560
|
566 /* |
1
|
567 ;;; Local Variables: *** |
|
568 ;;; mode: C++ *** |
|
569 ;;; page-delimiter: "^/\\*" *** |
|
570 ;;; End: *** |
|
571 */ |