1683
|
1 // toplev.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994, 1995 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, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
|
28 #include <cassert> |
|
29 #include <csetjmp> |
|
30 #include <csignal> |
|
31 #include <cstdlib> |
|
32 #include <cstring> |
|
33 #include <ctime> |
|
34 |
|
35 #include <fstream.h> |
|
36 #include <iostream.h> |
|
37 #include <strstream.h> |
|
38 |
|
39 #ifdef HAVE_UNISTD_H |
|
40 #include <sys/types.h> |
|
41 #include <unistd.h> |
|
42 #endif |
|
43 |
|
44 #include <pwd.h> |
|
45 |
|
46 #include <readline/tilde.h> |
|
47 |
|
48 #include "lo-error.h" |
|
49 |
|
50 #include "builtins.h" |
|
51 #include "defaults.h" |
|
52 #include "defun.h" |
|
53 #include "dynamic-ld.h" |
|
54 #include "error.h" |
|
55 #include "file-io.h" |
|
56 #include "help.h" |
|
57 #include "input.h" |
|
58 #include "lex.h" |
|
59 #include "octave-hist.h" |
|
60 #include "toplev.h" |
|
61 #include "pager.h" |
|
62 #include "parse.h" |
|
63 #include "pathsearch.h" |
|
64 #include "procstream.h" |
|
65 #include "sighandlers.h" |
|
66 #include "statdefs.h" |
|
67 #include "sysdep.h" |
|
68 #include "tree-const.h" |
|
69 #include "tree-misc.h" |
|
70 #include "tree-plot.h" |
|
71 #include "unwind-prot.h" |
|
72 #include "user-prefs.h" |
|
73 #include "utils.h" |
|
74 #include "variables.h" |
|
75 #include "version.h" |
|
76 |
|
77 #if !defined (HAVE_ATEXIT) && defined (HAVE_ON_EXIT) |
|
78 extern "C" int on_exit (); |
|
79 #define atexit on_exit |
|
80 #endif |
|
81 |
|
82 // argv[0] for this program. |
|
83 char *raw_prog_name = 0; |
|
84 |
|
85 // Cleaned-up name of this program, not including path information. |
|
86 char *prog_name = 0; |
|
87 |
|
88 // Login name for user running this program. |
|
89 char *user_name = 0; |
|
90 |
|
91 // Name of the host we are running on. |
|
92 char *host_name = 0; |
|
93 |
|
94 // User's home directory. |
|
95 char *home_directory = 0; |
|
96 |
|
97 // Guess what? |
|
98 char *the_current_working_directory = 0; |
|
99 |
|
100 // The path that will be searched for programs that we execute. |
|
101 // (--exec-path path) |
|
102 char *exec_path = 0; |
|
103 |
|
104 // Load path specified on command line. |
|
105 // (--path path; -p path) |
|
106 char *load_path = 0; |
|
107 |
|
108 // Name of the info file specified on command line. |
|
109 // (--info-file file) |
|
110 char *info_file = 0; |
|
111 |
|
112 // Name of the info reader we'd like to use. |
|
113 // (--info-program program) |
|
114 char *info_prog = 0; |
|
115 |
|
116 // Name of the editor to be invoked by the edit_history command. |
|
117 char *editor = 0; |
|
118 |
|
119 // If nonzero, don't do fancy line editing. |
|
120 int no_line_editing = 0; |
|
121 |
|
122 // If nonzero, print verbose info in some cases. |
|
123 // (--verbose; -V) |
|
124 int verbose_flag = 0; |
|
125 |
|
126 // Nonzero means we printed messages about reading startup files. |
|
127 int reading_startup_message_printed = 0; |
|
128 |
|
129 // Command number, counting from the beginning of this session. |
|
130 int current_command_number = 1; |
|
131 |
|
132 // Nonzero means we are exiting via the builtin exit or quit functions. |
|
133 int quitting_gracefully = 0; |
|
134 |
|
135 // Current command to execute. |
|
136 tree_statement_list *global_command = 0; |
|
137 |
|
138 // Pointer to function that is currently being evaluated. |
|
139 tree_function *curr_function = 0; |
|
140 |
|
141 // Nonzero means input is coming from startup file. |
|
142 int input_from_startup_file = 0; |
|
143 |
|
144 // The command-line options. |
|
145 charMatrix octave_argv; |
|
146 |
|
147 // Nonzero means that input is coming from a file that was named on |
|
148 // the command line. |
|
149 int input_from_command_line_file = 1; |
|
150 |
|
151 void |
|
152 parse_and_execute (FILE *f, int print) |
|
153 { |
|
154 begin_unwind_frame ("parse_and_execute"); |
|
155 |
|
156 YY_BUFFER_STATE old_buf = current_buffer (); |
|
157 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
158 |
|
159 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
160 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
161 |
|
162 switch_to_buffer (new_buf); |
|
163 |
|
164 unwind_protect_int (using_readline); |
|
165 unwind_protect_int (input_from_command_line_file); |
|
166 |
|
167 using_readline = 0; |
|
168 input_from_command_line_file = 0; |
|
169 |
|
170 unwind_protect_ptr (curr_sym_tab); |
|
171 |
|
172 int retval; |
|
173 do |
|
174 { |
|
175 reset_parser (); |
|
176 |
|
177 retval = yyparse (); |
|
178 |
|
179 if (retval == 0 && global_command) |
|
180 { |
|
181 global_command->eval (print); |
|
182 delete global_command; |
|
183 } |
|
184 } |
|
185 while (retval == 0); |
|
186 |
|
187 run_unwind_frame ("parse_and_execute"); |
|
188 } |
|
189 |
|
190 void |
|
191 parse_and_execute (const char *s, int print, int verbose, |
|
192 const char *warn_for) |
|
193 { |
|
194 begin_unwind_frame ("parse_and_execute_2"); |
|
195 |
|
196 unwind_protect_int (reading_script_file); |
|
197 unwind_protect_ptr (curr_fcn_file_full_name); |
|
198 |
|
199 reading_script_file = 1; |
|
200 curr_fcn_file_full_name = s; |
|
201 |
|
202 FILE *f = get_input_from_file (s, 0); |
|
203 if (f) |
|
204 { |
|
205 unwind_protect_int (input_line_number); |
|
206 unwind_protect_int (current_input_column); |
|
207 |
|
208 input_line_number = 0; |
|
209 current_input_column = 1; |
|
210 |
|
211 if (verbose) |
|
212 { |
|
213 cout << "reading commands from " << s << " ... "; |
|
214 reading_startup_message_printed = 1; |
|
215 cout.flush (); |
|
216 } |
|
217 |
|
218 parse_and_execute (f, print); |
|
219 |
|
220 fclose (f); |
|
221 |
|
222 if (verbose) |
|
223 cout << "done." << endl; |
|
224 } |
|
225 else if (warn_for) |
|
226 error ("%s: unable to open file `%s'", warn_for, s); |
|
227 |
|
228 run_unwind_frame ("parse_and_execute_2"); |
|
229 } |
|
230 |
|
231 DEFUN ("source", Fsource, Ssource, 10, |
|
232 "source (FILE)\n\ |
|
233 \n\ |
|
234 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
235 script file but without requiring the file to be named `FILE.m'.") |
|
236 { |
|
237 Octave_object retval; |
|
238 |
|
239 int nargin = args.length (); |
|
240 |
|
241 if (nargin == 1) |
|
242 { |
|
243 const char *file = args(0).string_value (); |
|
244 |
|
245 if (! error_state) |
|
246 { |
|
247 file = tilde_expand (file); |
|
248 |
|
249 parse_and_execute (file, 1, 0, "source"); |
|
250 |
|
251 if (error_state) |
|
252 error ("source: error sourcing file `%s'", file); |
|
253 |
|
254 delete [] file; |
|
255 } |
|
256 else |
|
257 error ("source: expecting file name as argument"); |
|
258 } |
|
259 else |
|
260 print_usage ("source"); |
|
261 |
|
262 return retval; |
|
263 } |
|
264 |
|
265 // Fix up things before exiting. |
|
266 |
|
267 void |
|
268 clean_up_and_exit (int retval) |
|
269 { |
|
270 raw_mode (0); |
|
271 |
|
272 clean_up_history (); |
|
273 |
|
274 close_plot_stream (); |
|
275 |
|
276 close_diary_file (); |
|
277 |
|
278 close_files (); |
|
279 |
|
280 cleanup_tmp_files (); |
|
281 |
|
282 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
283 cout << "\n"; |
|
284 |
|
285 if (retval == EOF) |
|
286 retval = 0; |
|
287 |
|
288 exit (retval); |
|
289 |
|
290 // This is bogus but should prevent g++ from giving a warning saying |
|
291 // that this volatile function does return. |
|
292 |
|
293 panic_impossible (); |
|
294 } |
|
295 |
|
296 DEFUN_TEXT ("casesen", Fcasesen, Scasesen, 10, |
|
297 "casesen [on|off]") |
|
298 { |
|
299 Octave_object retval; |
|
300 |
|
301 DEFINE_ARGV("casesen"); |
|
302 |
|
303 if (argc == 1 || (argc > 1 && strcmp (argv[1], "off") == 0)) |
|
304 warning ("casesen: sorry, Octave is always case sensitive"); |
|
305 else if (argc > 1 && strcmp (argv[1], "on") == 0) |
|
306 ; // ok. |
|
307 else |
|
308 print_usage ("casesen"); |
|
309 |
|
310 DELETE_ARGV; |
|
311 |
|
312 return retval; |
|
313 } |
|
314 |
|
315 DEFUN ("computer", Fcomputer, Scomputer, 11, |
|
316 "computer ():\n\ |
|
317 \n\ |
|
318 Have Octave ask the system, \"What kind of computer are you?\"") |
|
319 { |
|
320 Octave_object retval; |
|
321 |
|
322 int nargin = args.length (); |
|
323 |
|
324 if (nargin != 0) |
|
325 warning ("computer: ignoring extra arguments"); |
|
326 |
|
327 ostrstream output_buf; |
|
328 |
|
329 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
330 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
331 else |
|
332 output_buf << TARGET_HOST_TYPE; |
|
333 |
|
334 if (nargout == 0) |
|
335 { |
|
336 output_buf << "\n" << ends; |
|
337 maybe_page_output (output_buf); |
|
338 } |
|
339 else |
|
340 { |
|
341 output_buf << ends; |
|
342 char *msg = output_buf.str (); |
|
343 retval = msg; |
|
344 delete [] msg; |
|
345 } |
|
346 |
|
347 return retval; |
|
348 } |
|
349 |
|
350 DEFUN ("flops", Fflops, Sflops, 10, |
|
351 "flops (): count floating point operations") |
|
352 { |
|
353 int nargin = args.length (); |
|
354 |
|
355 if (nargin > 0) |
|
356 print_usage ("flops"); |
|
357 |
|
358 warning ("flops is a flop, always returning zero"); |
|
359 |
|
360 return 0.0; |
|
361 } |
|
362 |
|
363 DEFUN ("quit", Fquit, Squit, 00, |
|
364 "quit (): exit Octave gracefully") |
|
365 { |
|
366 Octave_object retval; |
|
367 quitting_gracefully = 1; |
|
368 clean_up_and_exit (0); |
|
369 return retval; |
|
370 } |
|
371 |
|
372 DEFALIAS (exit, quit); |
|
373 |
|
374 DEFUN ("warranty", Fwarranty, Swarranty, 00, |
|
375 "warranty (): describe copying conditions") |
|
376 { |
|
377 Octave_object retval; |
|
378 |
|
379 ostrstream output_buf; |
|
380 output_buf << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
381 This program is free software; you can redistribute it and/or modify\n\ |
|
382 it under the terms of the GNU General Public License as published by\n\ |
|
383 the Free Software Foundation; either version 2 of the License, or\n\ |
|
384 (at your option) any later version.\n\ |
|
385 \n\ |
|
386 This program is distributed in the hope that it will be useful,\n\ |
|
387 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
388 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
389 GNU General Public License for more details.\n\ |
|
390 \n\ |
|
391 You should have received a copy of the GNU General Public License\n\ |
|
392 along with this program. If not, write to the Free Software\n\ |
|
393 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
394 \n"; |
|
395 |
|
396 output_buf << ends; |
|
397 maybe_page_output (output_buf); |
|
398 |
|
399 return retval; |
|
400 } |
|
401 |
|
402 // XXX FIXME XXX -- this may not be the best place for these... |
|
403 |
|
404 Octave_object |
|
405 feval (const Octave_object& args, int nargout) |
|
406 { |
|
407 Octave_object retval; |
|
408 |
|
409 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
|
410 if (fcn) |
|
411 { |
|
412 int tmp_nargin = args.length () - 1; |
|
413 Octave_object tmp_args; |
|
414 tmp_args.resize (tmp_nargin); |
|
415 for (int i = 0; i < tmp_nargin; i++) |
|
416 tmp_args(i) = args(i+1); |
|
417 retval = fcn->eval (0, nargout, tmp_args); |
|
418 } |
|
419 |
|
420 return retval; |
|
421 } |
|
422 |
|
423 DEFUN ("feval", Ffeval, Sfeval, 11, |
|
424 "feval (NAME, ARGS, ...)\n\ |
|
425 \n\ |
|
426 evaluate NAME as a function, passing ARGS as its arguments") |
|
427 { |
|
428 Octave_object retval; |
|
429 |
|
430 int nargin = args.length (); |
|
431 |
|
432 if (nargin > 0) |
|
433 retval = feval (args, nargout); |
|
434 else |
|
435 print_usage ("feval"); |
|
436 |
|
437 return retval; |
|
438 } |
|
439 |
|
440 static Octave_object |
|
441 eval_string (const char *string, int print, int& parse_status, |
|
442 int nargout) |
|
443 { |
|
444 begin_unwind_frame ("eval_string"); |
|
445 |
|
446 unwind_protect_int (get_input_from_eval_string); |
|
447 unwind_protect_int (input_from_command_line_file); |
|
448 unwind_protect_ptr (global_command); |
|
449 unwind_protect_ptr (current_eval_string); |
|
450 |
|
451 get_input_from_eval_string = 1; |
|
452 input_from_command_line_file = 0; |
|
453 current_eval_string = string; |
|
454 |
|
455 YY_BUFFER_STATE old_buf = current_buffer (); |
|
456 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
457 |
|
458 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
459 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
460 |
|
461 switch_to_buffer (new_buf); |
|
462 |
|
463 unwind_protect_ptr (curr_sym_tab); |
|
464 |
|
465 reset_parser (); |
|
466 |
|
467 parse_status = yyparse (); |
|
468 |
|
469 // Important to reset the idea of where input is coming from before |
|
470 // trying to eval the command we just parsed -- it might contain the |
|
471 // name of an function file that still needs to be parsed! |
|
472 |
|
473 tree_statement_list *command = global_command; |
|
474 |
|
475 run_unwind_frame ("eval_string"); |
|
476 |
|
477 Octave_object retval; |
|
478 |
|
479 if (parse_status == 0 && command) |
|
480 { |
|
481 retval = command->eval (print, nargout); |
|
482 delete command; |
|
483 } |
|
484 |
|
485 return retval; |
|
486 } |
|
487 |
|
488 tree_constant |
|
489 eval_string (const char *string, int print, int& parse_status) |
|
490 { |
|
491 tree_constant retval; |
|
492 |
|
493 Octave_object tmp = eval_string (string, print, parse_status, 1); |
|
494 |
|
495 retval = tmp(0); |
|
496 |
|
497 return retval; |
|
498 } |
|
499 |
|
500 static Octave_object |
|
501 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
|
502 { |
|
503 const char *string = arg.string_value (); |
|
504 |
|
505 if (error_state) |
|
506 { |
|
507 error ("eval: expecting string argument"); |
|
508 return -1.0; |
|
509 } |
|
510 |
|
511 // Yes Virginia, we always print here... |
|
512 |
|
513 return eval_string (string, 1, parse_status, nargout); |
|
514 } |
|
515 |
|
516 DEFUN ("eval", Feval, Seval, 11, |
|
517 "eval (TRY, CATCH)\n\ |
|
518 \n\ |
|
519 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
520 string CATCH.") |
|
521 { |
|
522 Octave_object retval; |
|
523 |
|
524 int nargin = args.length (); |
|
525 |
|
526 if (nargin > 0) |
|
527 { |
|
528 begin_unwind_frame ("Feval"); |
|
529 |
|
530 if (nargin > 1) |
|
531 { |
|
532 unwind_protect_int (buffer_error_messages); |
|
533 buffer_error_messages = 1; |
|
534 } |
|
535 |
|
536 int parse_status = 0; |
|
537 |
|
538 retval = eval_string (args(0), parse_status, nargout); |
|
539 |
|
540 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
541 { |
|
542 error_state = 0; |
|
543 |
|
544 // Set up for letting the user print any messages from |
|
545 // errors that occurred in the first part of this eval(). |
|
546 |
|
547 buffer_error_messages = 0; |
|
548 bind_global_error_variable (); |
|
549 add_unwind_protect (clear_global_error_variable, 0); |
|
550 |
|
551 eval_string (args(1), parse_status, nargout); |
|
552 |
|
553 retval = Octave_object (); |
|
554 } |
|
555 |
|
556 run_unwind_frame ("Feval"); |
|
557 } |
|
558 else |
|
559 print_usage ("eval"); |
|
560 |
|
561 return retval; |
|
562 } |
|
563 |
|
564 // Execute a shell command. |
|
565 |
|
566 DEFUN ("system", Fsystem, Ssystem, 11, |
|
567 "system (string [, return_output]): execute shell commands") |
|
568 { |
|
569 Octave_object retval; |
|
570 |
|
571 int nargin = args.length (); |
|
572 |
|
573 if (nargin < 1 || nargin > 2) |
|
574 { |
|
575 print_usage ("system"); |
|
576 return retval; |
|
577 } |
|
578 |
|
579 tree_constant tc_command = args(0); |
|
580 |
|
581 const char *tmp_str = tc_command.string_value (); |
|
582 |
|
583 if (error_state) |
|
584 { |
|
585 error ("system: expecting string as first argument"); |
|
586 } |
|
587 else |
|
588 { |
|
589 iprocstream *cmd = new iprocstream (tmp_str); |
|
590 |
|
591 add_unwind_protect (cleanup_iprocstream, cmd); |
|
592 |
|
593 int status = 127; |
|
594 |
|
595 if (cmd && *cmd) |
|
596 { |
|
597 ostrstream output_buf; |
|
598 |
|
599 char ch; |
|
600 while (cmd->get (ch)) |
|
601 output_buf.put (ch); |
|
602 |
|
603 output_buf << ends; |
|
604 |
|
605 status = cmd->close (); |
|
606 |
|
607 // The value in status is as returned by waitpid. If the |
|
608 // process exited normally, extract the actual exit status of |
|
609 // the command. Otherwise, return 127 as a failure code. |
|
610 |
|
611 if ((status & 0xff) == 0) |
|
612 status = (status & 0xff00) >> 8; |
|
613 |
|
614 if (nargout > 0 || nargin > 1) |
|
615 { |
|
616 char *msg = output_buf.str (); |
|
617 |
|
618 retval(1) = (double) status; |
|
619 retval(0) = msg; |
|
620 |
|
621 delete [] msg; |
|
622 } |
|
623 else |
|
624 maybe_page_output (output_buf); |
|
625 } |
|
626 else |
|
627 error ("unable to start subprocess for `%s'", tmp_str); |
|
628 |
|
629 run_unwind_protect (); |
|
630 } |
|
631 |
|
632 return retval; |
|
633 } |
|
634 |
|
635 DEFALIAS (shell_cmd, system); |
|
636 |
|
637 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
638 int debug_new_delete = 0; |
|
639 |
|
640 typedef void (*vfp)(void); |
|
641 extern vfp __new_handler; |
|
642 |
|
643 void * |
|
644 __builtin_new (size_t sz) |
|
645 { |
|
646 void *p; |
|
647 |
|
648 /* malloc (0) is unpredictable; avoid it. */ |
|
649 if (sz == 0) |
|
650 sz = 1; |
|
651 p = (void *) malloc (sz); |
|
652 while (p == 0) |
|
653 { |
|
654 (*__new_handler) (); |
|
655 p = (void *) malloc (sz); |
|
656 } |
|
657 |
|
658 if (debug_new_delete) |
|
659 cout << "__builtin_new: " << p << endl; |
|
660 |
|
661 return p; |
|
662 } |
|
663 |
|
664 void |
|
665 __builtin_delete (void *ptr) |
|
666 { |
|
667 if (debug_new_delete) |
|
668 cout << "__builtin_delete: " << ptr << endl; |
|
669 |
|
670 if (ptr) |
|
671 free (ptr); |
|
672 } |
|
673 #endif |
|
674 |
|
675 /* |
|
676 ;;; Local Variables: *** |
|
677 ;;; mode: C++ *** |
|
678 ;;; page-delimiter: "^/\\*" *** |
|
679 ;;; End: *** |
|
680 */ |