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