1
|
1 // octave.cc -*- C++ -*- |
|
2 /* |
|
3 |
268
|
4 Copyright (C) 1992, 1993, 1994 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 |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Born February 20, 1992. |
|
25 |
240
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include "config.h" |
1
|
28 #endif |
|
29 |
|
30 #include <sys/types.h> |
139
|
31 #ifdef HAVE_UNISTD_H |
|
32 #include <unistd.h> |
|
33 #endif |
85
|
34 #include <sys/stat.h> |
1
|
35 #include <time.h> |
|
36 #include <pwd.h> |
|
37 #include <setjmp.h> |
|
38 #include <stdlib.h> |
|
39 #include <string.h> |
|
40 #include <signal.h> |
|
41 #include <assert.h> |
|
42 #include <iostream.h> |
|
43 #include <fstream.h> |
139
|
44 |
|
45 #include "getopt.h" |
1
|
46 |
240
|
47 #include "lo-error.h" |
223
|
48 |
1
|
49 #include "sighandlers.h" |
|
50 #include "variables.h" |
|
51 #include "error.h" |
|
52 #include "tree-const.h" |
|
53 #include "utils.h" |
|
54 #include "builtins.h" |
|
55 #include "input.h" |
|
56 #include "pager.h" |
|
57 #include "lex.h" |
|
58 #include "octave.h" |
|
59 #include "parse.h" |
|
60 #include "unwind-prot.h" |
|
61 #include "octave-hist.h" |
|
62 #include "version.h" |
|
63 #include "file-io.h" |
|
64 #include "sysdep.h" |
|
65 |
|
66 #ifdef sun |
|
67 extern "C" { int on_exit (); } |
|
68 #define atexit on_exit |
|
69 #endif |
|
70 |
|
71 // argv[0] for this program. |
|
72 char *raw_prog_name = (char *) NULL; |
|
73 |
|
74 // Cleaned-up name of this program, not including path information. |
|
75 char *prog_name = (char *) NULL; |
|
76 |
|
77 // Login name for user running this program. |
|
78 char *user_name = (char *) NULL; |
|
79 |
|
80 // Name of the host we are running on. |
|
81 char *host_name = (char *) NULL; |
|
82 |
|
83 // User's home directory. |
|
84 char *home_directory = (char *) NULL; |
|
85 |
|
86 // Guess what? |
|
87 char *the_current_working_directory = (char *) NULL; |
|
88 |
|
89 // Load path specified on command line. |
|
90 char *load_path = (char *) NULL; |
|
91 |
186
|
92 // Name of the info file specified on command line. |
|
93 char *info_file = (char *) NULL; |
|
94 |
195
|
95 // Name of the editor to be invoked by the edit_history command. |
|
96 char *editor = (char *) NULL; |
|
97 |
1
|
98 // If nonzero, don't do fancy line editing. |
|
99 int no_line_editing = 0; |
|
100 |
|
101 // Command number, counting from the beginning of this session. |
|
102 int current_command_number = 1; |
|
103 |
|
104 // Nonzero means we are exiting via the builtin exit or quit functions. |
|
105 int quitting_gracefully = 0; |
|
106 |
|
107 // Current command to execute. |
|
108 tree *global_command = (tree *) NULL; |
|
109 |
206
|
110 // Pointer to function that is currently being evaluated. |
|
111 tree_function *curr_function = (tree_function *) NULL; |
|
112 |
315
|
113 // Nonzero means input is coming from startup file. |
|
114 int input_from_startup_file = 0; |
|
115 |
1
|
116 // Top level context (?) |
|
117 jmp_buf toplevel; |
|
118 |
195
|
119 // This is not really the right place to do this... |
|
120 typedef void (*one_arg_error_handler_t) (const char*); |
|
121 extern one_arg_error_handler_t set_Complex_error_handler |
|
122 (one_arg_error_handler_t f); |
|
123 |
287
|
124 // This is from readline's paren.c: |
|
125 extern int rl_blink_matching_paren; |
|
126 |
195
|
127 static void |
|
128 octave_Complex_error_handler (const char* msg) |
|
129 { |
|
130 warning (msg); |
|
131 } |
|
132 |
1
|
133 // Nonzero means we read ~/.octaverc and ./.octaverc. |
|
134 static int read_init_files = 1; |
|
135 |
|
136 // Nonzero means we don\'t print the usual startup message. |
|
137 static int inhibit_startup_message = 0; |
|
138 |
|
139 // Usage message |
139
|
140 static const char *usage_string = |
|
141 "octave [-?dfhiqvx] [-p path] [--debug] [--help] [--interactive]\n\ |
186
|
142 [--info-file file] [--norc] [--path path] [--quiet] [--version]\n\ |
|
143 [--echo-commands] [file]"; |
1
|
144 |
|
145 // This is here so that it\'s more likely that the usage message and |
|
146 // the real set of options will agree. |
139
|
147 static const char *short_opts = "?dfhip:qvx"; |
|
148 |
|
149 // Long options. |
186
|
150 #define INFO_FILE_OPTION 1 |
139
|
151 static struct option long_opts[] = |
|
152 { |
|
153 { "debug", 0, 0, 'd' }, |
|
154 { "help", 0, 0, 'h' }, |
|
155 { "interactive", 0, 0, 'i' }, |
186
|
156 { "info-file", 1, 0, INFO_FILE_OPTION }, |
139
|
157 { "norc", 0, 0, 'f' }, |
|
158 { "path", 1, 0, 'p' }, |
|
159 { "quiet", 0, 0, 'q' }, |
|
160 { "version", 0, 0, 'v' }, |
|
161 { "echo-commands", 0, 0, 'x' }, |
|
162 { 0, 0, 0, 0 } |
|
163 }; |
1
|
164 |
|
165 /* |
|
166 * Initialize some global variables for later use. |
|
167 */ |
|
168 static void |
|
169 initialize_globals (char *name) |
|
170 { |
|
171 struct passwd *entry = getpwuid (getuid ()); |
|
172 if (entry) |
|
173 user_name = strsave (entry->pw_name); |
|
174 else |
|
175 user_name = strsave ("I have no name!"); |
|
176 endpwent (); |
|
177 |
|
178 char hostname[256]; |
|
179 if (gethostname (hostname, 255) < 0) |
|
180 host_name = strsave ("I have no host!"); |
|
181 else |
|
182 host_name = strsave (hostname); |
|
183 |
|
184 char *hd = getenv ("HOME"); |
|
185 if (hd == (char *) NULL) |
|
186 home_directory = strsave ("I have no home~!"); |
|
187 else |
|
188 home_directory = strsave (hd); |
|
189 |
|
190 raw_prog_name = strsave (name); |
|
191 prog_name = strsave ("octave"); |
|
192 |
|
193 load_path = default_path (); |
186
|
194 |
|
195 info_file = default_info_file (); |
195
|
196 |
|
197 editor = default_editor (); |
1
|
198 } |
|
199 |
|
200 void |
|
201 parse_and_execute (FILE *f, int print) |
|
202 { |
|
203 begin_unwind_frame ("parse_and_execute"); |
|
204 |
|
205 YY_BUFFER_STATE old_buf = current_buffer (); |
|
206 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
207 |
|
208 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
209 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
210 |
|
211 switch_to_buffer (new_buf); |
|
212 |
|
213 unwind_protect_int (echo_input); |
|
214 unwind_protect_int (using_readline); |
|
215 unwind_protect_int (saving_history); |
|
216 |
|
217 echo_input = 0; |
|
218 using_readline = 0; |
|
219 saving_history = 0; |
|
220 |
|
221 unwind_protect_ptr (curr_sym_tab); |
|
222 |
|
223 int retval; |
|
224 do |
|
225 { |
191
|
226 reset_parser (); |
1
|
227 retval = yyparse (); |
|
228 if (retval == 0 && global_command != NULL_TREE) |
|
229 { |
|
230 global_command->eval (print); |
|
231 delete global_command; |
|
232 } |
|
233 } |
|
234 while (retval == 0); |
|
235 |
|
236 run_unwind_frame ("parse_and_execute"); |
|
237 } |
|
238 |
|
239 void |
|
240 parse_and_execute (char *s, int print) |
|
241 { |
|
242 begin_unwind_frame ("parse_and_execute_2"); |
|
243 |
|
244 unwind_protect_int (reading_script_file); |
|
245 |
|
246 reading_script_file = 1; |
|
247 |
|
248 FILE *f = get_input_from_file (s, 0); |
|
249 if (f != (FILE *) NULL) |
|
250 { |
|
251 unwind_protect_int (input_line_number); |
|
252 unwind_protect_int (current_input_column); |
|
253 unwind_protect_int (echo_input); |
|
254 |
|
255 input_line_number = 0; |
143
|
256 current_input_column = 1; |
1
|
257 echo_input = 0; |
|
258 |
|
259 parse_and_execute (f, print); |
|
260 } |
|
261 |
|
262 run_unwind_frame ("parse_and_execute_2"); |
|
263 } |
|
264 |
|
265 /* |
|
266 * Initialize by reading startup files. |
|
267 */ |
|
268 static void |
|
269 execute_startup_files (void) |
|
270 { |
315
|
271 begin_unwind_frame ("execute_startup_files"); |
|
272 |
|
273 unwind_protect_int (input_from_startup_file); |
|
274 input_from_startup_file = 1; |
|
275 |
1
|
276 // Execute commands from the site-wide configuration file. |
|
277 |
|
278 char *sd = get_site_defaults (); |
|
279 |
|
280 parse_and_execute (sd, 0); |
|
281 |
|
282 // Try to execute commands from $HOME/.octaverc and ./.octaverc. |
|
283 |
85
|
284 char *home_rc = (char *) NULL; |
1
|
285 if (home_directory != NULL) |
|
286 { |
85
|
287 home_rc = strconcat (home_directory, "/.octaverc"); |
|
288 parse_and_execute (home_rc, 0); |
1
|
289 } |
|
290 |
85
|
291 // Names alone are not enough. |
|
292 |
|
293 struct stat home_rc_statbuf; |
|
294 stat (home_rc, &home_rc_statbuf); |
|
295 delete [] home_rc; |
|
296 |
|
297 struct stat dot_rc_statbuf; |
|
298 stat ("./.octaverc", &dot_rc_statbuf); |
|
299 |
|
300 if (home_rc_statbuf.st_ino != dot_rc_statbuf.st_ino) |
82
|
301 parse_and_execute ("./.octaverc", 0); |
315
|
302 |
|
303 run_unwind_frame ("execute_startup_files"); |
1
|
304 } |
|
305 |
|
306 /* |
|
307 * Usage message with extra help. |
|
308 */ |
|
309 static void |
|
310 verbose_usage (void) |
|
311 { |
|
312 cout << "\n" |
|
313 << " Octave, version " << version_string |
268
|
314 << ". Copyright (C) 1992, 1993, 1994 John W. Eaton.\n" |
1
|
315 << " This is free software with ABSOLUTELY NO WARRANTY.\n" |
|
316 << "\n" |
139
|
317 << " usage: " << usage_string |
|
318 << "\n\n" |
1
|
319 << " d : enter parser debugging mode\n" |
|
320 << " f : don't read ~/.octaverc or .octaverc at startup\n" |
|
321 << " h|? : print short help message and exit\n" |
|
322 << " i : force interactive behavior\n" |
|
323 << " q : don't print message at startup\n" |
139
|
324 << " v : print version number and exit\n" |
1
|
325 << " x : echo commands as they are executed\n" |
|
326 << "\n" |
|
327 << " file : execute commands from named file\n" |
285
|
328 << "\n"; |
|
329 |
1
|
330 exit (1); |
|
331 } |
|
332 |
|
333 /* |
|
334 * Terse usage messsage. |
|
335 */ |
|
336 static void |
|
337 usage (void) |
|
338 { |
139
|
339 cerr << "usage: " << usage_string << "\n"; |
1
|
340 exit (1); |
|
341 } |
|
342 |
|
343 /* |
|
344 * Fix up things before exiting. |
|
345 */ |
195
|
346 void |
1
|
347 clean_up_and_exit (int retval) |
|
348 { |
|
349 raw_mode (0); |
176
|
350 |
1
|
351 clean_up_history (); |
176
|
352 |
1
|
353 close_plot_stream (); |
176
|
354 |
1
|
355 close_files (); |
|
356 |
|
357 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
358 cout << "\n"; |
|
359 |
|
360 if (retval == EOF) |
|
361 retval = 0; |
|
362 |
|
363 exit (retval); |
195
|
364 |
|
365 // This is bogus but should prevent g++ from giving a warning saying |
|
366 // that this volatile function does return. |
|
367 |
|
368 panic_impossible (); |
1
|
369 } |
|
370 |
|
371 static void |
|
372 print_version_and_exit (void) |
|
373 { |
|
374 cout << "octave, version " << version_string << "\n"; |
|
375 exit (0); |
|
376 } |
|
377 |
|
378 /* |
|
379 * You guessed it. |
|
380 */ |
|
381 int |
|
382 main (int argc, char **argv) |
|
383 { |
|
384 // Allow for system dependent initialization. See sysdep.cc for more |
|
385 // details. |
|
386 sysdep_init (); |
|
387 |
195
|
388 // This is not really the right place to do this... |
|
389 set_Complex_error_handler (octave_Complex_error_handler); |
|
390 |
223
|
391 // Or this, probably... |
|
392 set_liboctave_error_handler (error); |
|
393 |
1
|
394 // Do this first, since some command line arguments may override the |
|
395 // defaults. |
|
396 initialize_globals (argv[0]); |
|
397 |
139
|
398 int optc; |
|
399 while ((optc = getopt_long (argc, argv, short_opts, long_opts, 0)) != EOF) |
1
|
400 { |
139
|
401 switch (optc) |
1
|
402 { |
|
403 case 'd': |
|
404 yydebug++; |
|
405 break; |
|
406 case 'f': |
|
407 read_init_files = 0; |
|
408 break; |
|
409 case 'h': |
|
410 case '?': |
|
411 verbose_usage (); |
|
412 break; |
|
413 case 'i': |
|
414 forced_interactive = 1; |
|
415 break; |
|
416 case 'p': |
139
|
417 if (optarg != (char *) NULL) |
|
418 load_path = strsave (optarg); |
1
|
419 break; |
|
420 case 'q': |
|
421 inhibit_startup_message = 1; |
|
422 break; |
|
423 case 'x': |
|
424 echo_input = 1; |
|
425 break; |
|
426 case 'v': |
|
427 print_version_and_exit (); |
|
428 break; |
186
|
429 case INFO_FILE_OPTION: |
|
430 if (optarg != (char *) NULL) |
|
431 info_file = strsave (optarg); |
|
432 break; |
1
|
433 default: |
|
434 usage (); |
|
435 break; |
|
436 } |
|
437 } |
|
438 |
|
439 // Make sure we clean up when we exit. |
|
440 atexit (cleanup_tmp_files); |
|
441 |
|
442 initialize_history (); |
|
443 |
|
444 initialize_file_io (); |
|
445 |
195
|
446 initialize_symbol_tables (); |
1
|
447 |
|
448 install_builtins (); |
|
449 |
315
|
450 initialize_readline (); |
|
451 |
|
452 initialize_pager (); |
|
453 |
|
454 install_signal_handlers (); |
|
455 |
1
|
456 if (read_init_files) |
|
457 { |
|
458 saving_history = 0; |
|
459 execute_startup_files (); |
|
460 saving_history = 1; |
|
461 } |
|
462 |
|
463 // Avoid counting commands executed from startup files. |
|
464 current_command_number = 1; |
|
465 |
|
466 // If there is an extra argument, see if it names a file to read. |
|
467 |
149
|
468 int remaining_args = argc - optind; |
|
469 if (remaining_args > 1) |
|
470 { |
|
471 usage (); |
|
472 } |
|
473 else if (remaining_args == 1) |
1
|
474 { |
139
|
475 FILE *infile = get_input_from_file (argv[optind]); |
1
|
476 if (infile == (FILE *) NULL) |
|
477 clean_up_and_exit (1); |
|
478 else |
287
|
479 { |
|
480 rl_blink_matching_paren = 0; |
|
481 switch_to_buffer (create_buffer (infile)); |
|
482 } |
1
|
483 } |
|
484 else |
|
485 { |
149
|
486 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
|
487 |
1
|
488 // Is input coming from a terminal? If so, we are probably |
|
489 // interactive. |
|
490 |
|
491 interactive = (isatty (fileno (stdin)) && isatty (fileno (stdout))); |
|
492 } |
|
493 |
|
494 // Force input to be echoed if not really interactive, but the user |
|
495 // has forced interactive behavior. |
|
496 |
|
497 if (!interactive && forced_interactive) |
287
|
498 { |
|
499 rl_blink_matching_paren = 0; |
|
500 echo_input = 1; |
|
501 } |
1
|
502 |
|
503 if (! (interactive || forced_interactive)) |
|
504 using_readline = 0; |
|
505 |
149
|
506 if (! inhibit_startup_message) |
|
507 { |
|
508 cout << "Octave, version " << version_string |
268
|
509 << ". Copyright (C) 1992, 1993, 1994 John W. Eaton.\n" |
149
|
510 << "This is free software with ABSOLUTELY NO WARRANTY.\n" |
|
511 << "For details, type `warranty'.\n" |
287
|
512 << endl; |
149
|
513 } |
|
514 |
1
|
515 // Allow the user to interrupt us without exiting. |
|
516 |
|
517 volatile sig_handler *saved_sigint_handler = signal (SIGINT, SIG_IGN); |
|
518 |
|
519 if (setjmp (toplevel) != 0) |
|
520 { |
|
521 raw_mode (0); |
176
|
522 |
1
|
523 cout << "\n"; |
|
524 } |
|
525 |
|
526 can_interrupt = 1; |
|
527 |
|
528 signal (SIGINT, saved_sigint_handler); |
|
529 |
|
530 // The big loop. |
|
531 |
|
532 int retval; |
|
533 do |
|
534 { |
287
|
535 curr_sym_tab = top_level_sym_tab; |
|
536 |
1
|
537 reset_parser (); |
287
|
538 |
1
|
539 retval = yyparse (); |
287
|
540 |
1
|
541 if (retval == 0 && global_command != NULL_TREE) |
|
542 { |
|
543 global_command->eval (1); |
|
544 delete global_command; |
|
545 current_command_number++; |
|
546 } |
|
547 } |
|
548 while (retval == 0); |
|
549 |
|
550 clean_up_and_exit (retval); |
|
551 } |
|
552 |
|
553 /* |
|
554 ;;; Local Variables: *** |
|
555 ;;; mode: C++ *** |
|
556 ;;; page-delimiter: "^/\\*" *** |
|
557 ;;; End: *** |
|
558 */ |