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