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