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