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