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