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