1683
|
1 // toplev.cc -*- C++ -*- |
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 John W. Eaton |
1683
|
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 |
1820
|
112 // Nonzero means we are using readline. |
|
113 // (--no-line-editing) |
|
114 #if defined (USE_READLINE) |
|
115 int using_readline = 1; |
|
116 #else |
|
117 int using_readline = 0; |
|
118 #endif |
1683
|
119 |
|
120 // Nonzero means we printed messages about reading startup files. |
|
121 int reading_startup_message_printed = 0; |
|
122 |
|
123 // Command number, counting from the beginning of this session. |
|
124 int current_command_number = 1; |
|
125 |
|
126 // Nonzero means we are exiting via the builtin exit or quit functions. |
|
127 int quitting_gracefully = 0; |
|
128 |
|
129 // Current command to execute. |
|
130 tree_statement_list *global_command = 0; |
|
131 |
|
132 // Pointer to function that is currently being evaluated. |
|
133 tree_function *curr_function = 0; |
|
134 |
|
135 // Nonzero means input is coming from startup file. |
|
136 int input_from_startup_file = 0; |
|
137 |
|
138 // The command-line options. |
|
139 charMatrix octave_argv; |
|
140 |
|
141 // Nonzero means that input is coming from a file that was named on |
|
142 // the command line. |
|
143 int input_from_command_line_file = 1; |
|
144 |
|
145 void |
|
146 parse_and_execute (FILE *f, int print) |
|
147 { |
|
148 begin_unwind_frame ("parse_and_execute"); |
|
149 |
|
150 YY_BUFFER_STATE old_buf = current_buffer (); |
|
151 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
152 |
|
153 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
154 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
155 |
|
156 switch_to_buffer (new_buf); |
|
157 |
|
158 unwind_protect_int (using_readline); |
|
159 unwind_protect_int (input_from_command_line_file); |
|
160 |
|
161 using_readline = 0; |
|
162 input_from_command_line_file = 0; |
|
163 |
|
164 unwind_protect_ptr (curr_sym_tab); |
|
165 |
|
166 int retval; |
|
167 do |
|
168 { |
|
169 reset_parser (); |
|
170 |
|
171 retval = yyparse (); |
|
172 |
|
173 if (retval == 0 && global_command) |
|
174 { |
|
175 global_command->eval (print); |
|
176 delete global_command; |
|
177 } |
|
178 } |
|
179 while (retval == 0); |
|
180 |
|
181 run_unwind_frame ("parse_and_execute"); |
|
182 } |
|
183 |
|
184 void |
1750
|
185 parse_and_execute (const string& s, int print, int verbose, |
1683
|
186 const char *warn_for) |
|
187 { |
|
188 begin_unwind_frame ("parse_and_execute_2"); |
|
189 |
|
190 unwind_protect_int (reading_script_file); |
1755
|
191 unwind_protect_str (curr_fcn_file_full_name); |
1683
|
192 |
|
193 reading_script_file = 1; |
1755
|
194 curr_fcn_file_full_name = s; |
1683
|
195 |
|
196 FILE *f = get_input_from_file (s, 0); |
1750
|
197 |
1683
|
198 if (f) |
|
199 { |
|
200 unwind_protect_int (input_line_number); |
|
201 unwind_protect_int (current_input_column); |
|
202 |
|
203 input_line_number = 0; |
|
204 current_input_column = 1; |
|
205 |
|
206 if (verbose) |
|
207 { |
|
208 cout << "reading commands from " << s << " ... "; |
|
209 reading_startup_message_printed = 1; |
|
210 cout.flush (); |
|
211 } |
|
212 |
|
213 parse_and_execute (f, print); |
|
214 |
|
215 fclose (f); |
|
216 |
|
217 if (verbose) |
|
218 cout << "done." << endl; |
|
219 } |
|
220 else if (warn_for) |
1750
|
221 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
1683
|
222 |
|
223 run_unwind_frame ("parse_and_execute_2"); |
|
224 } |
|
225 |
|
226 DEFUN ("source", Fsource, Ssource, 10, |
|
227 "source (FILE)\n\ |
|
228 \n\ |
|
229 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
230 script file but without requiring the file to be named `FILE.m'.") |
|
231 { |
|
232 Octave_object retval; |
|
233 |
|
234 int nargin = args.length (); |
|
235 |
|
236 if (nargin == 1) |
|
237 { |
1750
|
238 string file = args(0).string_value (); |
1683
|
239 |
|
240 if (! error_state) |
|
241 { |
1750
|
242 file = oct_tilde_expand (file); |
1683
|
243 |
|
244 parse_and_execute (file, 1, 0, "source"); |
|
245 |
|
246 if (error_state) |
1751
|
247 error ("source: error sourcing file `%s'", file.c_str ()); |
1683
|
248 } |
|
249 else |
|
250 error ("source: expecting file name as argument"); |
|
251 } |
|
252 else |
|
253 print_usage ("source"); |
|
254 |
|
255 return retval; |
|
256 } |
|
257 |
|
258 // Fix up things before exiting. |
|
259 |
|
260 void |
|
261 clean_up_and_exit (int retval) |
|
262 { |
|
263 raw_mode (0); |
|
264 |
1799
|
265 octave_command_history.clean_up_and_save (); |
1683
|
266 |
|
267 close_plot_stream (); |
|
268 |
|
269 close_diary_file (); |
|
270 |
|
271 close_files (); |
|
272 |
|
273 cleanup_tmp_files (); |
|
274 |
|
275 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
276 cout << "\n"; |
|
277 |
|
278 if (retval == EOF) |
|
279 retval = 0; |
|
280 |
|
281 exit (retval); |
|
282 |
|
283 // This is bogus but should prevent g++ from giving a warning saying |
|
284 // that this volatile function does return. |
|
285 |
|
286 panic_impossible (); |
|
287 } |
|
288 |
|
289 DEFUN_TEXT ("casesen", Fcasesen, Scasesen, 10, |
|
290 "casesen [on|off]") |
|
291 { |
|
292 Octave_object retval; |
|
293 |
1755
|
294 int argc = args.length () + 1; |
|
295 |
|
296 string_vector argv = make_argv (args, "casesen"); |
1683
|
297 |
1755
|
298 if (error_state) |
|
299 return retval; |
|
300 |
|
301 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
302 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
303 else if (argc > 1 && argv[1] == "on") |
1683
|
304 ; // ok. |
|
305 else |
|
306 print_usage ("casesen"); |
|
307 |
|
308 return retval; |
|
309 } |
|
310 |
|
311 DEFUN ("computer", Fcomputer, Scomputer, 11, |
|
312 "computer ():\n\ |
|
313 \n\ |
|
314 Have Octave ask the system, \"What kind of computer are you?\"") |
|
315 { |
|
316 Octave_object retval; |
|
317 |
|
318 int nargin = args.length (); |
|
319 |
|
320 if (nargin != 0) |
|
321 warning ("computer: ignoring extra arguments"); |
|
322 |
|
323 ostrstream output_buf; |
|
324 |
|
325 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
326 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
327 else |
|
328 output_buf << TARGET_HOST_TYPE; |
|
329 |
|
330 if (nargout == 0) |
|
331 { |
|
332 output_buf << "\n" << ends; |
|
333 maybe_page_output (output_buf); |
|
334 } |
|
335 else |
|
336 { |
|
337 output_buf << ends; |
|
338 char *msg = output_buf.str (); |
|
339 retval = msg; |
|
340 delete [] msg; |
|
341 } |
|
342 |
|
343 return retval; |
|
344 } |
|
345 |
|
346 DEFUN ("flops", Fflops, Sflops, 10, |
|
347 "flops (): count floating point operations") |
|
348 { |
|
349 int nargin = args.length (); |
|
350 |
|
351 if (nargin > 0) |
|
352 print_usage ("flops"); |
|
353 |
|
354 warning ("flops is a flop, always returning zero"); |
|
355 |
|
356 return 0.0; |
|
357 } |
|
358 |
|
359 DEFUN ("quit", Fquit, Squit, 00, |
|
360 "quit (): exit Octave gracefully") |
|
361 { |
|
362 Octave_object retval; |
|
363 quitting_gracefully = 1; |
|
364 clean_up_and_exit (0); |
|
365 return retval; |
|
366 } |
|
367 |
|
368 DEFALIAS (exit, quit); |
|
369 |
|
370 DEFUN ("warranty", Fwarranty, Swarranty, 00, |
|
371 "warranty (): describe copying conditions") |
|
372 { |
|
373 Octave_object retval; |
|
374 |
|
375 ostrstream output_buf; |
|
376 output_buf << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
377 This program is free software; you can redistribute it and/or modify\n\ |
|
378 it under the terms of the GNU General Public License as published by\n\ |
|
379 the Free Software Foundation; either version 2 of the License, or\n\ |
|
380 (at your option) any later version.\n\ |
|
381 \n\ |
|
382 This program is distributed in the hope that it will be useful,\n\ |
|
383 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
384 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
385 GNU General Public License for more details.\n\ |
|
386 \n\ |
|
387 You should have received a copy of the GNU General Public License\n\ |
|
388 along with this program. If not, write to the Free Software\n\ |
|
389 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
390 \n"; |
|
391 |
|
392 output_buf << ends; |
|
393 maybe_page_output (output_buf); |
|
394 |
|
395 return retval; |
|
396 } |
|
397 |
|
398 // XXX FIXME XXX -- this may not be the best place for these... |
|
399 |
|
400 Octave_object |
|
401 feval (const Octave_object& args, int nargout) |
|
402 { |
|
403 Octave_object retval; |
|
404 |
|
405 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
|
406 if (fcn) |
|
407 { |
|
408 int tmp_nargin = args.length () - 1; |
|
409 Octave_object tmp_args; |
|
410 tmp_args.resize (tmp_nargin); |
|
411 for (int i = 0; i < tmp_nargin; i++) |
|
412 tmp_args(i) = args(i+1); |
|
413 retval = fcn->eval (0, nargout, tmp_args); |
|
414 } |
|
415 |
|
416 return retval; |
|
417 } |
|
418 |
|
419 DEFUN ("feval", Ffeval, Sfeval, 11, |
|
420 "feval (NAME, ARGS, ...)\n\ |
|
421 \n\ |
|
422 evaluate NAME as a function, passing ARGS as its arguments") |
|
423 { |
|
424 Octave_object retval; |
|
425 |
|
426 int nargin = args.length (); |
|
427 |
|
428 if (nargin > 0) |
|
429 retval = feval (args, nargout); |
|
430 else |
|
431 print_usage ("feval"); |
|
432 |
|
433 return retval; |
|
434 } |
|
435 |
|
436 static Octave_object |
1755
|
437 eval_string (const string& s, int print, int& parse_status, |
1683
|
438 int nargout) |
|
439 { |
|
440 begin_unwind_frame ("eval_string"); |
|
441 |
|
442 unwind_protect_int (get_input_from_eval_string); |
|
443 unwind_protect_int (input_from_command_line_file); |
|
444 unwind_protect_ptr (global_command); |
1755
|
445 unwind_protect_str (current_eval_string); |
1683
|
446 |
|
447 get_input_from_eval_string = 1; |
|
448 input_from_command_line_file = 0; |
1755
|
449 current_eval_string = s; |
1683
|
450 |
|
451 YY_BUFFER_STATE old_buf = current_buffer (); |
|
452 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
453 |
|
454 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
455 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
456 |
|
457 switch_to_buffer (new_buf); |
|
458 |
|
459 unwind_protect_ptr (curr_sym_tab); |
|
460 |
|
461 reset_parser (); |
|
462 |
|
463 parse_status = yyparse (); |
|
464 |
|
465 // Important to reset the idea of where input is coming from before |
|
466 // trying to eval the command we just parsed -- it might contain the |
|
467 // name of an function file that still needs to be parsed! |
|
468 |
|
469 tree_statement_list *command = global_command; |
|
470 |
|
471 run_unwind_frame ("eval_string"); |
|
472 |
|
473 Octave_object retval; |
|
474 |
|
475 if (parse_status == 0 && command) |
|
476 { |
|
477 retval = command->eval (print, nargout); |
|
478 delete command; |
|
479 } |
|
480 |
|
481 return retval; |
|
482 } |
|
483 |
|
484 tree_constant |
1755
|
485 eval_string (const string& s, int print, int& parse_status) |
1683
|
486 { |
|
487 tree_constant retval; |
|
488 |
1755
|
489 Octave_object tmp = eval_string (s, print, parse_status, 1); |
1683
|
490 |
|
491 retval = tmp(0); |
|
492 |
|
493 return retval; |
|
494 } |
|
495 |
|
496 static Octave_object |
|
497 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
|
498 { |
1755
|
499 string s = arg.string_value (); |
1683
|
500 |
|
501 if (error_state) |
|
502 { |
|
503 error ("eval: expecting string argument"); |
|
504 return -1.0; |
|
505 } |
|
506 |
|
507 // Yes Virginia, we always print here... |
|
508 |
1755
|
509 return eval_string (s, 1, parse_status, nargout); |
1683
|
510 } |
|
511 |
|
512 DEFUN ("eval", Feval, Seval, 11, |
|
513 "eval (TRY, CATCH)\n\ |
|
514 \n\ |
|
515 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
516 string CATCH.") |
|
517 { |
|
518 Octave_object retval; |
|
519 |
|
520 int nargin = args.length (); |
|
521 |
|
522 if (nargin > 0) |
|
523 { |
|
524 begin_unwind_frame ("Feval"); |
|
525 |
|
526 if (nargin > 1) |
|
527 { |
|
528 unwind_protect_int (buffer_error_messages); |
|
529 buffer_error_messages = 1; |
|
530 } |
|
531 |
|
532 int parse_status = 0; |
|
533 |
|
534 retval = eval_string (args(0), parse_status, nargout); |
|
535 |
|
536 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
537 { |
|
538 error_state = 0; |
|
539 |
|
540 // Set up for letting the user print any messages from |
|
541 // errors that occurred in the first part of this eval(). |
|
542 |
|
543 buffer_error_messages = 0; |
|
544 bind_global_error_variable (); |
|
545 add_unwind_protect (clear_global_error_variable, 0); |
|
546 |
|
547 eval_string (args(1), parse_status, nargout); |
|
548 |
|
549 retval = Octave_object (); |
|
550 } |
|
551 |
|
552 run_unwind_frame ("Feval"); |
|
553 } |
|
554 else |
|
555 print_usage ("eval"); |
|
556 |
|
557 return retval; |
|
558 } |
|
559 |
|
560 // Execute a shell command. |
|
561 |
|
562 DEFUN ("system", Fsystem, Ssystem, 11, |
|
563 "system (string [, return_output]): execute shell commands") |
|
564 { |
|
565 Octave_object retval; |
|
566 |
|
567 int nargin = args.length (); |
|
568 |
|
569 if (nargin < 1 || nargin > 2) |
|
570 { |
|
571 print_usage ("system"); |
|
572 return retval; |
|
573 } |
|
574 |
|
575 tree_constant tc_command = args(0); |
|
576 |
1755
|
577 string tmp = tc_command.string_value (); |
1683
|
578 |
|
579 if (error_state) |
|
580 { |
|
581 error ("system: expecting string as first argument"); |
|
582 } |
|
583 else |
|
584 { |
1755
|
585 iprocstream *cmd = new iprocstream (tmp.c_str ()); |
1683
|
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 |
1755
|
623 error ("unable to start subprocess for `%s'", tmp.c_str ()); |
1683
|
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 */ |