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 // Nonzero means that input is coming from a file that was named on |
|
139 // the command line. |
|
140 int input_from_command_line_file = 1; |
|
141 |
1907
|
142 // Top level context (?) |
|
143 jmp_buf toplevel; |
|
144 |
1683
|
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 |
1907
|
226 int |
|
227 main_loop (void) |
|
228 { |
|
229 // Allow the user to interrupt us without exiting. |
|
230 |
|
231 if (setjmp (toplevel) != 0) |
|
232 { |
|
233 raw_mode (0); |
|
234 |
|
235 cout << "\n"; |
|
236 } |
|
237 |
|
238 can_interrupt = 1; |
|
239 |
|
240 catch_interrupts (); |
|
241 |
|
242 // The big loop. |
|
243 |
|
244 int retval; |
|
245 do |
|
246 { |
|
247 curr_sym_tab = top_level_sym_tab; |
|
248 |
|
249 reset_parser (); |
|
250 |
|
251 retval = yyparse (); |
|
252 |
|
253 if (retval == 0 && global_command) |
|
254 { |
|
255 global_command->eval (1); |
|
256 delete global_command; |
|
257 current_command_number++; |
|
258 } |
|
259 } |
|
260 while (retval == 0); |
|
261 |
|
262 return retval; |
|
263 } |
|
264 |
1957
|
265 DEFUN (source, args, , |
1683
|
266 "source (FILE)\n\ |
|
267 \n\ |
|
268 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
269 script file but without requiring the file to be named `FILE.m'.") |
|
270 { |
|
271 Octave_object retval; |
|
272 |
|
273 int nargin = args.length (); |
|
274 |
|
275 if (nargin == 1) |
|
276 { |
1750
|
277 string file = args(0).string_value (); |
1683
|
278 |
|
279 if (! error_state) |
|
280 { |
1750
|
281 file = oct_tilde_expand (file); |
1683
|
282 |
|
283 parse_and_execute (file, 1, 0, "source"); |
|
284 |
|
285 if (error_state) |
1751
|
286 error ("source: error sourcing file `%s'", file.c_str ()); |
1683
|
287 } |
|
288 else |
|
289 error ("source: expecting file name as argument"); |
|
290 } |
|
291 else |
|
292 print_usage ("source"); |
|
293 |
|
294 return retval; |
|
295 } |
|
296 |
|
297 // Fix up things before exiting. |
|
298 |
|
299 void |
|
300 clean_up_and_exit (int retval) |
|
301 { |
|
302 raw_mode (0); |
|
303 |
1799
|
304 octave_command_history.clean_up_and_save (); |
1683
|
305 |
|
306 close_plot_stream (); |
|
307 |
|
308 close_diary_file (); |
|
309 |
|
310 close_files (); |
|
311 |
|
312 cleanup_tmp_files (); |
|
313 |
|
314 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
315 cout << "\n"; |
|
316 |
|
317 if (retval == EOF) |
|
318 retval = 0; |
|
319 |
|
320 exit (retval); |
|
321 |
|
322 // This is bogus but should prevent g++ from giving a warning saying |
|
323 // that this volatile function does return. |
|
324 |
|
325 panic_impossible (); |
|
326 } |
|
327 |
1957
|
328 DEFUN_TEXT (casesen, args, , |
1683
|
329 "casesen [on|off]") |
|
330 { |
|
331 Octave_object retval; |
|
332 |
1755
|
333 int argc = args.length () + 1; |
|
334 |
|
335 string_vector argv = make_argv (args, "casesen"); |
1683
|
336 |
1755
|
337 if (error_state) |
|
338 return retval; |
|
339 |
|
340 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
341 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
342 else if (argc > 1 && argv[1] == "on") |
1683
|
343 ; // ok. |
|
344 else |
|
345 print_usage ("casesen"); |
|
346 |
|
347 return retval; |
|
348 } |
|
349 |
1957
|
350 DEFUN (computer, args, nargout, |
1683
|
351 "computer ():\n\ |
|
352 \n\ |
|
353 Have Octave ask the system, \"What kind of computer are you?\"") |
|
354 { |
|
355 Octave_object retval; |
|
356 |
|
357 int nargin = args.length (); |
|
358 |
|
359 if (nargin != 0) |
|
360 warning ("computer: ignoring extra arguments"); |
|
361 |
|
362 ostrstream output_buf; |
|
363 |
|
364 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
365 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
366 else |
|
367 output_buf << TARGET_HOST_TYPE; |
|
368 |
|
369 if (nargout == 0) |
|
370 { |
|
371 output_buf << "\n" << ends; |
|
372 maybe_page_output (output_buf); |
|
373 } |
|
374 else |
|
375 { |
|
376 output_buf << ends; |
|
377 char *msg = output_buf.str (); |
|
378 retval = msg; |
|
379 delete [] msg; |
|
380 } |
|
381 |
|
382 return retval; |
|
383 } |
|
384 |
1957
|
385 DEFUN (flops, args, , |
1683
|
386 "flops (): count floating point operations") |
|
387 { |
|
388 int nargin = args.length (); |
|
389 |
|
390 if (nargin > 0) |
|
391 print_usage ("flops"); |
|
392 |
|
393 warning ("flops is a flop, always returning zero"); |
|
394 |
|
395 return 0.0; |
|
396 } |
|
397 |
1957
|
398 DEFUN (quit, , , |
1683
|
399 "quit (): exit Octave gracefully") |
|
400 { |
|
401 Octave_object retval; |
|
402 quitting_gracefully = 1; |
|
403 clean_up_and_exit (0); |
|
404 return retval; |
|
405 } |
|
406 |
|
407 DEFALIAS (exit, quit); |
|
408 |
1957
|
409 DEFUN (warranty, , , |
1683
|
410 "warranty (): describe copying conditions") |
|
411 { |
|
412 Octave_object retval; |
|
413 |
|
414 ostrstream output_buf; |
|
415 output_buf << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
416 This program is free software; you can redistribute it and/or modify\n\ |
|
417 it under the terms of the GNU General Public License as published by\n\ |
|
418 the Free Software Foundation; either version 2 of the License, or\n\ |
|
419 (at your option) any later version.\n\ |
|
420 \n\ |
|
421 This program is distributed in the hope that it will be useful,\n\ |
|
422 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
423 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
424 GNU General Public License for more details.\n\ |
|
425 \n\ |
|
426 You should have received a copy of the GNU General Public License\n\ |
|
427 along with this program. If not, write to the Free Software\n\ |
|
428 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
429 \n"; |
|
430 |
|
431 output_buf << ends; |
|
432 maybe_page_output (output_buf); |
|
433 |
|
434 return retval; |
|
435 } |
|
436 |
|
437 // XXX FIXME XXX -- this may not be the best place for these... |
|
438 |
|
439 Octave_object |
|
440 feval (const Octave_object& args, int nargout) |
|
441 { |
|
442 Octave_object retval; |
|
443 |
|
444 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
|
445 if (fcn) |
|
446 { |
|
447 int tmp_nargin = args.length () - 1; |
|
448 Octave_object tmp_args; |
|
449 tmp_args.resize (tmp_nargin); |
|
450 for (int i = 0; i < tmp_nargin; i++) |
|
451 tmp_args(i) = args(i+1); |
|
452 retval = fcn->eval (0, nargout, tmp_args); |
|
453 } |
|
454 |
|
455 return retval; |
|
456 } |
|
457 |
1957
|
458 DEFUN (feval, args, nargout, |
1683
|
459 "feval (NAME, ARGS, ...)\n\ |
|
460 \n\ |
|
461 evaluate NAME as a function, passing ARGS as its arguments") |
|
462 { |
|
463 Octave_object retval; |
|
464 |
|
465 int nargin = args.length (); |
|
466 |
|
467 if (nargin > 0) |
|
468 retval = feval (args, nargout); |
|
469 else |
|
470 print_usage ("feval"); |
|
471 |
|
472 return retval; |
|
473 } |
|
474 |
|
475 static Octave_object |
1755
|
476 eval_string (const string& s, int print, int& parse_status, |
1683
|
477 int nargout) |
|
478 { |
|
479 begin_unwind_frame ("eval_string"); |
|
480 |
|
481 unwind_protect_int (get_input_from_eval_string); |
|
482 unwind_protect_int (input_from_command_line_file); |
|
483 unwind_protect_ptr (global_command); |
1755
|
484 unwind_protect_str (current_eval_string); |
1683
|
485 |
|
486 get_input_from_eval_string = 1; |
|
487 input_from_command_line_file = 0; |
1755
|
488 current_eval_string = s; |
1683
|
489 |
|
490 YY_BUFFER_STATE old_buf = current_buffer (); |
|
491 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
492 |
|
493 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
494 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
495 |
|
496 switch_to_buffer (new_buf); |
|
497 |
|
498 unwind_protect_ptr (curr_sym_tab); |
|
499 |
|
500 reset_parser (); |
|
501 |
|
502 parse_status = yyparse (); |
|
503 |
|
504 // Important to reset the idea of where input is coming from before |
|
505 // trying to eval the command we just parsed -- it might contain the |
|
506 // name of an function file that still needs to be parsed! |
|
507 |
|
508 tree_statement_list *command = global_command; |
|
509 |
|
510 run_unwind_frame ("eval_string"); |
|
511 |
|
512 Octave_object retval; |
|
513 |
|
514 if (parse_status == 0 && command) |
|
515 { |
|
516 retval = command->eval (print, nargout); |
|
517 delete command; |
|
518 } |
|
519 |
|
520 return retval; |
|
521 } |
|
522 |
|
523 tree_constant |
1755
|
524 eval_string (const string& s, int print, int& parse_status) |
1683
|
525 { |
|
526 tree_constant retval; |
|
527 |
1755
|
528 Octave_object tmp = eval_string (s, print, parse_status, 1); |
1683
|
529 |
|
530 retval = tmp(0); |
|
531 |
|
532 return retval; |
|
533 } |
|
534 |
|
535 static Octave_object |
|
536 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
|
537 { |
1755
|
538 string s = arg.string_value (); |
1683
|
539 |
|
540 if (error_state) |
|
541 { |
|
542 error ("eval: expecting string argument"); |
|
543 return -1.0; |
|
544 } |
|
545 |
|
546 // Yes Virginia, we always print here... |
|
547 |
1755
|
548 return eval_string (s, 1, parse_status, nargout); |
1683
|
549 } |
|
550 |
1957
|
551 DEFUN (eval, args, nargout, |
1683
|
552 "eval (TRY, CATCH)\n\ |
|
553 \n\ |
|
554 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
555 string CATCH.") |
|
556 { |
|
557 Octave_object retval; |
|
558 |
|
559 int nargin = args.length (); |
|
560 |
|
561 if (nargin > 0) |
|
562 { |
|
563 begin_unwind_frame ("Feval"); |
|
564 |
|
565 if (nargin > 1) |
|
566 { |
|
567 unwind_protect_int (buffer_error_messages); |
|
568 buffer_error_messages = 1; |
|
569 } |
|
570 |
|
571 int parse_status = 0; |
|
572 |
|
573 retval = eval_string (args(0), parse_status, nargout); |
|
574 |
|
575 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
576 { |
|
577 error_state = 0; |
|
578 |
|
579 // Set up for letting the user print any messages from |
|
580 // errors that occurred in the first part of this eval(). |
|
581 |
|
582 buffer_error_messages = 0; |
|
583 bind_global_error_variable (); |
|
584 add_unwind_protect (clear_global_error_variable, 0); |
|
585 |
|
586 eval_string (args(1), parse_status, nargout); |
|
587 |
|
588 retval = Octave_object (); |
|
589 } |
|
590 |
|
591 run_unwind_frame ("Feval"); |
|
592 } |
|
593 else |
|
594 print_usage ("eval"); |
|
595 |
|
596 return retval; |
|
597 } |
|
598 |
|
599 // Execute a shell command. |
|
600 |
1957
|
601 DEFUN (system, args, nargout, |
1683
|
602 "system (string [, return_output]): execute shell commands") |
|
603 { |
|
604 Octave_object retval; |
|
605 |
|
606 int nargin = args.length (); |
|
607 |
|
608 if (nargin < 1 || nargin > 2) |
|
609 { |
|
610 print_usage ("system"); |
|
611 return retval; |
|
612 } |
|
613 |
|
614 tree_constant tc_command = args(0); |
|
615 |
1755
|
616 string tmp = tc_command.string_value (); |
1683
|
617 |
|
618 if (error_state) |
|
619 { |
|
620 error ("system: expecting string as first argument"); |
|
621 } |
|
622 else |
|
623 { |
1755
|
624 iprocstream *cmd = new iprocstream (tmp.c_str ()); |
1683
|
625 |
|
626 add_unwind_protect (cleanup_iprocstream, cmd); |
|
627 |
|
628 int status = 127; |
|
629 |
|
630 if (cmd && *cmd) |
|
631 { |
|
632 ostrstream output_buf; |
|
633 |
|
634 char ch; |
|
635 while (cmd->get (ch)) |
|
636 output_buf.put (ch); |
|
637 |
|
638 output_buf << ends; |
|
639 |
|
640 status = cmd->close (); |
|
641 |
|
642 // The value in status is as returned by waitpid. If the |
|
643 // process exited normally, extract the actual exit status of |
|
644 // the command. Otherwise, return 127 as a failure code. |
|
645 |
|
646 if ((status & 0xff) == 0) |
|
647 status = (status & 0xff00) >> 8; |
|
648 |
|
649 if (nargout > 0 || nargin > 1) |
|
650 { |
|
651 char *msg = output_buf.str (); |
|
652 |
|
653 retval(1) = (double) status; |
|
654 retval(0) = msg; |
|
655 |
|
656 delete [] msg; |
|
657 } |
|
658 else |
|
659 maybe_page_output (output_buf); |
|
660 } |
|
661 else |
1755
|
662 error ("unable to start subprocess for `%s'", tmp.c_str ()); |
1683
|
663 |
|
664 run_unwind_protect (); |
|
665 } |
|
666 |
|
667 return retval; |
|
668 } |
|
669 |
|
670 DEFALIAS (shell_cmd, system); |
|
671 |
|
672 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
673 int debug_new_delete = 0; |
|
674 |
|
675 typedef void (*vfp)(void); |
|
676 extern vfp __new_handler; |
|
677 |
|
678 void * |
|
679 __builtin_new (size_t sz) |
|
680 { |
|
681 void *p; |
|
682 |
|
683 /* malloc (0) is unpredictable; avoid it. */ |
|
684 if (sz == 0) |
|
685 sz = 1; |
|
686 p = (void *) malloc (sz); |
|
687 while (p == 0) |
|
688 { |
|
689 (*__new_handler) (); |
|
690 p = (void *) malloc (sz); |
|
691 } |
|
692 |
|
693 if (debug_new_delete) |
|
694 cout << "__builtin_new: " << p << endl; |
|
695 |
|
696 return p; |
|
697 } |
|
698 |
|
699 void |
|
700 __builtin_delete (void *ptr) |
|
701 { |
|
702 if (debug_new_delete) |
|
703 cout << "__builtin_delete: " << ptr << endl; |
|
704 |
|
705 if (ptr) |
|
706 free (ptr); |
|
707 } |
|
708 #endif |
|
709 |
|
710 /* |
|
711 ;;; Local Variables: *** |
|
712 ;;; mode: C++ *** |
|
713 ;;; page-delimiter: "^/\\*" *** |
|
714 ;;; End: *** |
|
715 */ |