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" |
1755
|
47 #include "str-vec.h" |
1683
|
48 |
|
49 #include "builtins.h" |
|
50 #include "defaults.h" |
|
51 #include "defun.h" |
|
52 #include "dynamic-ld.h" |
|
53 #include "error.h" |
|
54 #include "file-io.h" |
|
55 #include "help.h" |
|
56 #include "input.h" |
|
57 #include "lex.h" |
1742
|
58 #include "oct-hist.h" |
1683
|
59 #include "pager.h" |
|
60 #include "parse.h" |
|
61 #include "pathsearch.h" |
|
62 #include "procstream.h" |
1750
|
63 #include "pt-const.h" |
|
64 #include "pt-misc.h" |
|
65 #include "pt-plot.h" |
1683
|
66 #include "sighandlers.h" |
|
67 #include "sysdep.h" |
1750
|
68 #include "toplev.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. |
1755
|
76 string raw_prog_name; |
1683
|
77 |
|
78 // Cleaned-up name of this program, not including path information. |
1755
|
79 string prog_name; |
1683
|
80 |
|
81 // Login name for user running this program. |
1755
|
82 string user_name; |
1683
|
83 |
|
84 // Name of the host we are running on. |
1755
|
85 string host_name; |
1683
|
86 |
|
87 // User's home directory. |
1755
|
88 string home_directory; |
1683
|
89 |
|
90 // Guess what? |
1755
|
91 string the_current_working_directory; |
1683
|
92 |
|
93 // The path that will be searched for programs that we execute. |
|
94 // (--exec-path path) |
1755
|
95 string exec_path; |
1683
|
96 |
|
97 // Load path specified on command line. |
|
98 // (--path path; -p path) |
1755
|
99 string load_path; |
1683
|
100 |
|
101 // Name of the info file specified on command line. |
|
102 // (--info-file file) |
1755
|
103 string info_file; |
1683
|
104 |
|
105 // Name of the info reader we'd like to use. |
|
106 // (--info-program program) |
1755
|
107 string info_prog; |
1683
|
108 |
|
109 // Name of the editor to be invoked by the edit_history command. |
1755
|
110 string editor; |
1683
|
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 |
1750
|
184 parse_and_execute (const string& s, int print, int verbose, |
1683
|
185 const char *warn_for) |
|
186 { |
|
187 begin_unwind_frame ("parse_and_execute_2"); |
|
188 |
|
189 unwind_protect_int (reading_script_file); |
1755
|
190 unwind_protect_str (curr_fcn_file_full_name); |
1683
|
191 |
|
192 reading_script_file = 1; |
1755
|
193 curr_fcn_file_full_name = s; |
1683
|
194 |
|
195 FILE *f = get_input_from_file (s, 0); |
1750
|
196 |
1683
|
197 if (f) |
|
198 { |
|
199 unwind_protect_int (input_line_number); |
|
200 unwind_protect_int (current_input_column); |
|
201 |
|
202 input_line_number = 0; |
|
203 current_input_column = 1; |
|
204 |
|
205 if (verbose) |
|
206 { |
|
207 cout << "reading commands from " << s << " ... "; |
|
208 reading_startup_message_printed = 1; |
|
209 cout.flush (); |
|
210 } |
|
211 |
|
212 parse_and_execute (f, print); |
|
213 |
|
214 fclose (f); |
|
215 |
|
216 if (verbose) |
|
217 cout << "done." << endl; |
|
218 } |
|
219 else if (warn_for) |
1750
|
220 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
1683
|
221 |
|
222 run_unwind_frame ("parse_and_execute_2"); |
|
223 } |
|
224 |
|
225 DEFUN ("source", Fsource, Ssource, 10, |
|
226 "source (FILE)\n\ |
|
227 \n\ |
|
228 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
229 script file but without requiring the file to be named `FILE.m'.") |
|
230 { |
|
231 Octave_object retval; |
|
232 |
|
233 int nargin = args.length (); |
|
234 |
|
235 if (nargin == 1) |
|
236 { |
1750
|
237 string file = args(0).string_value (); |
1683
|
238 |
|
239 if (! error_state) |
|
240 { |
1750
|
241 file = oct_tilde_expand (file); |
1683
|
242 |
|
243 parse_and_execute (file, 1, 0, "source"); |
|
244 |
|
245 if (error_state) |
1751
|
246 error ("source: error sourcing file `%s'", file.c_str ()); |
1683
|
247 } |
|
248 else |
|
249 error ("source: expecting file name as argument"); |
|
250 } |
|
251 else |
|
252 print_usage ("source"); |
|
253 |
|
254 return retval; |
|
255 } |
|
256 |
|
257 // Fix up things before exiting. |
|
258 |
|
259 void |
|
260 clean_up_and_exit (int retval) |
|
261 { |
|
262 raw_mode (0); |
|
263 |
1799
|
264 octave_command_history.clean_up_and_save (); |
1683
|
265 |
|
266 close_plot_stream (); |
|
267 |
|
268 close_diary_file (); |
|
269 |
|
270 close_files (); |
|
271 |
|
272 cleanup_tmp_files (); |
|
273 |
|
274 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
275 cout << "\n"; |
|
276 |
|
277 if (retval == EOF) |
|
278 retval = 0; |
|
279 |
|
280 exit (retval); |
|
281 |
|
282 // This is bogus but should prevent g++ from giving a warning saying |
|
283 // that this volatile function does return. |
|
284 |
|
285 panic_impossible (); |
|
286 } |
|
287 |
|
288 DEFUN_TEXT ("casesen", Fcasesen, Scasesen, 10, |
|
289 "casesen [on|off]") |
|
290 { |
|
291 Octave_object retval; |
|
292 |
1755
|
293 int argc = args.length () + 1; |
|
294 |
|
295 string_vector argv = make_argv (args, "casesen"); |
1683
|
296 |
1755
|
297 if (error_state) |
|
298 return retval; |
|
299 |
|
300 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
301 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
302 else if (argc > 1 && argv[1] == "on") |
1683
|
303 ; // ok. |
|
304 else |
|
305 print_usage ("casesen"); |
|
306 |
|
307 return retval; |
|
308 } |
|
309 |
|
310 DEFUN ("computer", Fcomputer, Scomputer, 11, |
|
311 "computer ():\n\ |
|
312 \n\ |
|
313 Have Octave ask the system, \"What kind of computer are you?\"") |
|
314 { |
|
315 Octave_object retval; |
|
316 |
|
317 int nargin = args.length (); |
|
318 |
|
319 if (nargin != 0) |
|
320 warning ("computer: ignoring extra arguments"); |
|
321 |
|
322 ostrstream output_buf; |
|
323 |
|
324 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
325 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
326 else |
|
327 output_buf << TARGET_HOST_TYPE; |
|
328 |
|
329 if (nargout == 0) |
|
330 { |
|
331 output_buf << "\n" << ends; |
|
332 maybe_page_output (output_buf); |
|
333 } |
|
334 else |
|
335 { |
|
336 output_buf << ends; |
|
337 char *msg = output_buf.str (); |
|
338 retval = msg; |
|
339 delete [] msg; |
|
340 } |
|
341 |
|
342 return retval; |
|
343 } |
|
344 |
|
345 DEFUN ("flops", Fflops, Sflops, 10, |
|
346 "flops (): count floating point operations") |
|
347 { |
|
348 int nargin = args.length (); |
|
349 |
|
350 if (nargin > 0) |
|
351 print_usage ("flops"); |
|
352 |
|
353 warning ("flops is a flop, always returning zero"); |
|
354 |
|
355 return 0.0; |
|
356 } |
|
357 |
|
358 DEFUN ("quit", Fquit, Squit, 00, |
|
359 "quit (): exit Octave gracefully") |
|
360 { |
|
361 Octave_object retval; |
|
362 quitting_gracefully = 1; |
|
363 clean_up_and_exit (0); |
|
364 return retval; |
|
365 } |
|
366 |
|
367 DEFALIAS (exit, quit); |
|
368 |
|
369 DEFUN ("warranty", Fwarranty, Swarranty, 00, |
|
370 "warranty (): describe copying conditions") |
|
371 { |
|
372 Octave_object retval; |
|
373 |
|
374 ostrstream output_buf; |
|
375 output_buf << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
376 This program is free software; you can redistribute it and/or modify\n\ |
|
377 it under the terms of the GNU General Public License as published by\n\ |
|
378 the Free Software Foundation; either version 2 of the License, or\n\ |
|
379 (at your option) any later version.\n\ |
|
380 \n\ |
|
381 This program is distributed in the hope that it will be useful,\n\ |
|
382 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
383 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
384 GNU General Public License for more details.\n\ |
|
385 \n\ |
|
386 You should have received a copy of the GNU General Public License\n\ |
|
387 along with this program. If not, write to the Free Software\n\ |
|
388 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
389 \n"; |
|
390 |
|
391 output_buf << ends; |
|
392 maybe_page_output (output_buf); |
|
393 |
|
394 return retval; |
|
395 } |
|
396 |
|
397 // XXX FIXME XXX -- this may not be the best place for these... |
|
398 |
|
399 Octave_object |
|
400 feval (const Octave_object& args, int nargout) |
|
401 { |
|
402 Octave_object retval; |
|
403 |
|
404 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
|
405 if (fcn) |
|
406 { |
|
407 int tmp_nargin = args.length () - 1; |
|
408 Octave_object tmp_args; |
|
409 tmp_args.resize (tmp_nargin); |
|
410 for (int i = 0; i < tmp_nargin; i++) |
|
411 tmp_args(i) = args(i+1); |
|
412 retval = fcn->eval (0, nargout, tmp_args); |
|
413 } |
|
414 |
|
415 return retval; |
|
416 } |
|
417 |
|
418 DEFUN ("feval", Ffeval, Sfeval, 11, |
|
419 "feval (NAME, ARGS, ...)\n\ |
|
420 \n\ |
|
421 evaluate NAME as a function, passing ARGS as its arguments") |
|
422 { |
|
423 Octave_object retval; |
|
424 |
|
425 int nargin = args.length (); |
|
426 |
|
427 if (nargin > 0) |
|
428 retval = feval (args, nargout); |
|
429 else |
|
430 print_usage ("feval"); |
|
431 |
|
432 return retval; |
|
433 } |
|
434 |
|
435 static Octave_object |
1755
|
436 eval_string (const string& s, int print, int& parse_status, |
1683
|
437 int nargout) |
|
438 { |
|
439 begin_unwind_frame ("eval_string"); |
|
440 |
|
441 unwind_protect_int (get_input_from_eval_string); |
|
442 unwind_protect_int (input_from_command_line_file); |
|
443 unwind_protect_ptr (global_command); |
1755
|
444 unwind_protect_str (current_eval_string); |
1683
|
445 |
|
446 get_input_from_eval_string = 1; |
|
447 input_from_command_line_file = 0; |
1755
|
448 current_eval_string = s; |
1683
|
449 |
|
450 YY_BUFFER_STATE old_buf = current_buffer (); |
|
451 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
452 |
|
453 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
454 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
455 |
|
456 switch_to_buffer (new_buf); |
|
457 |
|
458 unwind_protect_ptr (curr_sym_tab); |
|
459 |
|
460 reset_parser (); |
|
461 |
|
462 parse_status = yyparse (); |
|
463 |
|
464 // Important to reset the idea of where input is coming from before |
|
465 // trying to eval the command we just parsed -- it might contain the |
|
466 // name of an function file that still needs to be parsed! |
|
467 |
|
468 tree_statement_list *command = global_command; |
|
469 |
|
470 run_unwind_frame ("eval_string"); |
|
471 |
|
472 Octave_object retval; |
|
473 |
|
474 if (parse_status == 0 && command) |
|
475 { |
|
476 retval = command->eval (print, nargout); |
|
477 delete command; |
|
478 } |
|
479 |
|
480 return retval; |
|
481 } |
|
482 |
|
483 tree_constant |
1755
|
484 eval_string (const string& s, int print, int& parse_status) |
1683
|
485 { |
|
486 tree_constant retval; |
|
487 |
1755
|
488 Octave_object tmp = eval_string (s, print, parse_status, 1); |
1683
|
489 |
|
490 retval = tmp(0); |
|
491 |
|
492 return retval; |
|
493 } |
|
494 |
|
495 static Octave_object |
|
496 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
|
497 { |
1755
|
498 string s = arg.string_value (); |
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 |
1755
|
508 return eval_string (s, 1, parse_status, nargout); |
1683
|
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 |
1755
|
576 string tmp = tc_command.string_value (); |
1683
|
577 |
|
578 if (error_state) |
|
579 { |
|
580 error ("system: expecting string as first argument"); |
|
581 } |
|
582 else |
|
583 { |
1755
|
584 iprocstream *cmd = new iprocstream (tmp.c_str ()); |
1683
|
585 |
|
586 add_unwind_protect (cleanup_iprocstream, cmd); |
|
587 |
|
588 int status = 127; |
|
589 |
|
590 if (cmd && *cmd) |
|
591 { |
|
592 ostrstream output_buf; |
|
593 |
|
594 char ch; |
|
595 while (cmd->get (ch)) |
|
596 output_buf.put (ch); |
|
597 |
|
598 output_buf << ends; |
|
599 |
|
600 status = cmd->close (); |
|
601 |
|
602 // The value in status is as returned by waitpid. If the |
|
603 // process exited normally, extract the actual exit status of |
|
604 // the command. Otherwise, return 127 as a failure code. |
|
605 |
|
606 if ((status & 0xff) == 0) |
|
607 status = (status & 0xff00) >> 8; |
|
608 |
|
609 if (nargout > 0 || nargin > 1) |
|
610 { |
|
611 char *msg = output_buf.str (); |
|
612 |
|
613 retval(1) = (double) status; |
|
614 retval(0) = msg; |
|
615 |
|
616 delete [] msg; |
|
617 } |
|
618 else |
|
619 maybe_page_output (output_buf); |
|
620 } |
|
621 else |
1755
|
622 error ("unable to start subprocess for `%s'", tmp.c_str ()); |
1683
|
623 |
|
624 run_unwind_protect (); |
|
625 } |
|
626 |
|
627 return retval; |
|
628 } |
|
629 |
|
630 DEFALIAS (shell_cmd, system); |
|
631 |
|
632 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
633 int debug_new_delete = 0; |
|
634 |
|
635 typedef void (*vfp)(void); |
|
636 extern vfp __new_handler; |
|
637 |
|
638 void * |
|
639 __builtin_new (size_t sz) |
|
640 { |
|
641 void *p; |
|
642 |
|
643 /* malloc (0) is unpredictable; avoid it. */ |
|
644 if (sz == 0) |
|
645 sz = 1; |
|
646 p = (void *) malloc (sz); |
|
647 while (p == 0) |
|
648 { |
|
649 (*__new_handler) (); |
|
650 p = (void *) malloc (sz); |
|
651 } |
|
652 |
|
653 if (debug_new_delete) |
|
654 cout << "__builtin_new: " << p << endl; |
|
655 |
|
656 return p; |
|
657 } |
|
658 |
|
659 void |
|
660 __builtin_delete (void *ptr) |
|
661 { |
|
662 if (debug_new_delete) |
|
663 cout << "__builtin_delete: " << ptr << endl; |
|
664 |
|
665 if (ptr) |
|
666 free (ptr); |
|
667 } |
|
668 #endif |
|
669 |
|
670 /* |
|
671 ;;; Local Variables: *** |
|
672 ;;; mode: C++ *** |
|
673 ;;; page-delimiter: "^/\\*" *** |
|
674 ;;; End: *** |
|
675 */ |