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