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