1683
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 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 |
2442
|
41 #ifdef HAVE_SYS_TYPES_H |
1683
|
42 #include <sys/types.h> |
2442
|
43 #endif |
1683
|
44 #include <unistd.h> |
|
45 #endif |
|
46 |
2926
|
47 #include "cmd-edit.h" |
|
48 #include "file-ops.h" |
1683
|
49 #include "lo-error.h" |
2370
|
50 #include "lo-mappers.h" |
1755
|
51 #include "str-vec.h" |
1683
|
52 |
2492
|
53 #include <defaults.h> |
1683
|
54 #include "defun.h" |
|
55 #include "dynamic-ld.h" |
|
56 #include "error.h" |
|
57 #include "file-io.h" |
|
58 #include "input.h" |
|
59 #include "lex.h" |
2492
|
60 #include <oct-conf.h> |
1742
|
61 #include "oct-hist.h" |
2162
|
62 #include "oct-map.h" |
2862
|
63 #include "oct-obj.h" |
1683
|
64 #include "pager.h" |
|
65 #include "parse.h" |
|
66 #include "pathsearch.h" |
|
67 #include "procstream.h" |
2370
|
68 #include "ov.h" |
2985
|
69 #include "pt-jump.h" |
1750
|
70 #include "pt-plot.h" |
2982
|
71 #include "pt-stmt.h" |
1683
|
72 #include "sighandlers.h" |
|
73 #include "sysdep.h" |
2693
|
74 #include "syswait.h" |
1750
|
75 #include "toplev.h" |
1683
|
76 #include "unwind-prot.h" |
|
77 #include "utils.h" |
|
78 #include "variables.h" |
2492
|
79 #include <version.h> |
1683
|
80 |
2806
|
81 // Nonzero means we print |
|
82 static bool Vdefault_eval_print_flag = true; |
|
83 |
1820
|
84 // Nonzero means we are using readline. |
|
85 // (--no-line-editing) |
2926
|
86 int line_editing = 1; |
2552
|
87 |
1683
|
88 // Nonzero means we printed messages about reading startup files. |
|
89 int reading_startup_message_printed = 0; |
|
90 |
|
91 // Nonzero means we are exiting via the builtin exit or quit functions. |
|
92 int quitting_gracefully = 0; |
|
93 |
|
94 // Current command to execute. |
|
95 tree_statement_list *global_command = 0; |
|
96 |
|
97 // Pointer to function that is currently being evaluated. |
2891
|
98 octave_user_function *curr_function = 0; |
1683
|
99 |
|
100 // Nonzero means input is coming from startup file. |
|
101 int input_from_startup_file = 0; |
|
102 |
|
103 // Nonzero means that input is coming from a file that was named on |
|
104 // the command line. |
|
105 int input_from_command_line_file = 1; |
|
106 |
1907
|
107 // Top level context (?) |
|
108 jmp_buf toplevel; |
|
109 |
1683
|
110 void |
2898
|
111 parse_and_execute (FILE *f) |
1683
|
112 { |
2985
|
113 unwind_protect::begin_frame ("parse_and_execute"); |
1683
|
114 |
|
115 YY_BUFFER_STATE old_buf = current_buffer (); |
|
116 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
117 |
2985
|
118 unwind_protect::add (restore_input_buffer, old_buf); |
|
119 unwind_protect::add (delete_input_buffer, new_buf); |
1683
|
120 |
|
121 switch_to_buffer (new_buf); |
|
122 |
2926
|
123 unwind_protect_int (line_editing); |
1683
|
124 unwind_protect_int (input_from_command_line_file); |
|
125 |
2926
|
126 line_editing = 0; |
1683
|
127 input_from_command_line_file = 0; |
|
128 |
|
129 unwind_protect_ptr (curr_sym_tab); |
|
130 |
|
131 int retval; |
|
132 do |
|
133 { |
|
134 reset_parser (); |
|
135 |
|
136 retval = yyparse (); |
|
137 |
|
138 if (retval == 0 && global_command) |
|
139 { |
2898
|
140 global_command->eval (); |
2620
|
141 |
1683
|
142 delete global_command; |
2620
|
143 |
|
144 global_command = 0; |
|
145 |
2985
|
146 bool quit = (tree_return_command::returning |
|
147 || tree_break_command::breaking); |
2620
|
148 |
2985
|
149 if (tree_return_command::returning) |
|
150 tree_return_command::returning = 0; |
2620
|
151 |
2985
|
152 if (tree_break_command::breaking) |
|
153 tree_break_command::breaking--; |
2620
|
154 |
|
155 if (error_state) |
|
156 { |
|
157 error ("near line %d of file `%s'", input_line_number, |
|
158 curr_fcn_file_full_name.c_str ()); |
|
159 |
|
160 break; |
|
161 } |
|
162 |
|
163 if (quit) |
|
164 break; |
1683
|
165 } |
|
166 } |
|
167 while (retval == 0); |
|
168 |
2985
|
169 unwind_protect::run_frame ("parse_and_execute"); |
1683
|
170 } |
|
171 |
2517
|
172 static void |
|
173 safe_fclose (void *f) |
|
174 { |
|
175 if (f) |
2800
|
176 fclose (static_cast<FILE *> (f)); |
2517
|
177 } |
|
178 |
1683
|
179 void |
2898
|
180 parse_and_execute (const string& s, bool verbose, const char *warn_for) |
1683
|
181 { |
2985
|
182 unwind_protect::begin_frame ("parse_and_execute_2"); |
1683
|
183 |
|
184 unwind_protect_int (reading_script_file); |
1755
|
185 unwind_protect_str (curr_fcn_file_full_name); |
1683
|
186 |
|
187 reading_script_file = 1; |
1755
|
188 curr_fcn_file_full_name = s; |
1683
|
189 |
|
190 FILE *f = get_input_from_file (s, 0); |
1750
|
191 |
1683
|
192 if (f) |
|
193 { |
2985
|
194 unwind_protect::add (safe_fclose, f); |
2517
|
195 |
1683
|
196 unwind_protect_int (input_line_number); |
|
197 unwind_protect_int (current_input_column); |
|
198 |
|
199 input_line_number = 0; |
|
200 current_input_column = 1; |
|
201 |
|
202 if (verbose) |
|
203 { |
|
204 cout << "reading commands from " << s << " ... "; |
|
205 reading_startup_message_printed = 1; |
|
206 cout.flush (); |
|
207 } |
|
208 |
2898
|
209 parse_and_execute (f); |
1683
|
210 |
|
211 if (verbose) |
|
212 cout << "done." << endl; |
|
213 } |
|
214 else if (warn_for) |
1750
|
215 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
1683
|
216 |
2985
|
217 unwind_protect::run_frame ("parse_and_execute_2"); |
1683
|
218 } |
|
219 |
1907
|
220 int |
|
221 main_loop (void) |
|
222 { |
|
223 // Allow the user to interrupt us without exiting. |
|
224 |
2016
|
225 octave_save_signal_mask (); |
|
226 |
1907
|
227 if (setjmp (toplevel) != 0) |
|
228 { |
|
229 raw_mode (0); |
|
230 |
|
231 cout << "\n"; |
2016
|
232 |
|
233 octave_restore_signal_mask (); |
1907
|
234 } |
|
235 |
|
236 can_interrupt = 1; |
|
237 |
2554
|
238 octave_catch_interrupts (); |
1907
|
239 |
|
240 // The big loop. |
|
241 |
|
242 int retval; |
|
243 do |
|
244 { |
|
245 curr_sym_tab = top_level_sym_tab; |
|
246 |
|
247 reset_parser (); |
|
248 |
|
249 retval = yyparse (); |
|
250 |
|
251 if (retval == 0 && global_command) |
|
252 { |
2898
|
253 global_command->eval (); |
2299
|
254 |
1907
|
255 delete global_command; |
2299
|
256 |
2620
|
257 global_command = 0; |
|
258 |
|
259 if (! (interactive || forced_interactive)) |
|
260 { |
2985
|
261 bool quit = (tree_return_command::returning |
|
262 || tree_break_command::breaking); |
2620
|
263 |
2985
|
264 if (tree_return_command::returning) |
|
265 tree_return_command::returning = 0; |
2620
|
266 |
2985
|
267 if (tree_break_command::breaking) |
|
268 tree_break_command::breaking--; |
2620
|
269 |
|
270 if (quit) |
|
271 break; |
|
272 } |
|
273 |
|
274 if (error_state) |
|
275 { |
|
276 if (! (interactive || forced_interactive)) |
|
277 break; |
|
278 } |
2299
|
279 else |
2620
|
280 { |
|
281 if (octave_completion_matches_called) |
|
282 octave_completion_matches_called = false; |
|
283 else |
2973
|
284 command_editor::increment_current_command_number (); |
2620
|
285 } |
1907
|
286 } |
|
287 } |
|
288 while (retval == 0); |
|
289 |
|
290 return retval; |
|
291 } |
|
292 |
1957
|
293 DEFUN (source, args, , |
1683
|
294 "source (FILE)\n\ |
|
295 \n\ |
|
296 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
297 script file but without requiring the file to be named `FILE.m'.") |
|
298 { |
2086
|
299 octave_value_list retval; |
1683
|
300 |
|
301 int nargin = args.length (); |
|
302 |
|
303 if (nargin == 1) |
|
304 { |
1750
|
305 string file = args(0).string_value (); |
1683
|
306 |
|
307 if (! error_state) |
|
308 { |
2926
|
309 file = file_ops::tilde_expand (file); |
1683
|
310 |
2898
|
311 parse_and_execute (file, false, "source"); |
1683
|
312 |
|
313 if (error_state) |
1751
|
314 error ("source: error sourcing file `%s'", file.c_str ()); |
1683
|
315 } |
|
316 else |
|
317 error ("source: expecting file name as argument"); |
|
318 } |
|
319 else |
|
320 print_usage ("source"); |
|
321 |
|
322 return retval; |
|
323 } |
|
324 |
|
325 // Fix up things before exiting. |
|
326 |
|
327 void |
|
328 clean_up_and_exit (int retval) |
|
329 { |
2926
|
330 command_editor::restore_terminal_state (); |
|
331 |
|
332 // XXX FIXME XXX -- is this needed? Can it cause any trouble? |
1683
|
333 raw_mode (0); |
|
334 |
2926
|
335 command_history::clean_up_and_save (); |
1683
|
336 |
|
337 close_plot_stream (); |
|
338 |
|
339 close_files (); |
|
340 |
|
341 cleanup_tmp_files (); |
|
342 |
|
343 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
344 cout << "\n"; |
|
345 |
|
346 if (retval == EOF) |
|
347 retval = 0; |
|
348 |
|
349 exit (retval); |
|
350 |
|
351 // This is bogus but should prevent g++ from giving a warning saying |
|
352 // that this volatile function does return. |
|
353 |
|
354 panic_impossible (); |
|
355 } |
|
356 |
1957
|
357 DEFUN_TEXT (casesen, args, , |
1683
|
358 "casesen [on|off]") |
|
359 { |
2086
|
360 octave_value_list retval; |
1683
|
361 |
1755
|
362 int argc = args.length () + 1; |
|
363 |
1965
|
364 string_vector argv = args.make_argv ("casesen"); |
1683
|
365 |
1755
|
366 if (error_state) |
|
367 return retval; |
|
368 |
|
369 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
370 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
371 else if (argc > 1 && argv[1] == "on") |
1683
|
372 ; // ok. |
|
373 else |
|
374 print_usage ("casesen"); |
|
375 |
|
376 return retval; |
|
377 } |
|
378 |
1957
|
379 DEFUN (computer, args, nargout, |
1683
|
380 "computer ():\n\ |
|
381 \n\ |
|
382 Have Octave ask the system, \"What kind of computer are you?\"") |
|
383 { |
2086
|
384 octave_value_list retval; |
1683
|
385 |
|
386 int nargin = args.length (); |
|
387 |
|
388 if (nargin != 0) |
|
389 warning ("computer: ignoring extra arguments"); |
|
390 |
2095
|
391 string msg; |
1683
|
392 |
|
393 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
2095
|
394 msg = "Hi Dave, I'm a HAL-9000"; |
1683
|
395 else |
2095
|
396 msg = TARGET_HOST_TYPE; |
1683
|
397 |
|
398 if (nargout == 0) |
2095
|
399 octave_stdout << msg << "\n"; |
1683
|
400 else |
2095
|
401 retval = msg; |
1683
|
402 |
|
403 return retval; |
|
404 } |
|
405 |
2068
|
406 DEFUN (quit, args, , |
|
407 "quit (STATUS): exit Octave gracefully, returning STATUS to the system.\n\ |
|
408 \n\ |
|
409 STATUS should be an integer value. If STATUS is missing, 0 is assumed.") |
1683
|
410 { |
2086
|
411 octave_value_list retval; |
2068
|
412 |
|
413 int exit_status = 0; |
|
414 |
|
415 quitting_gracefully = 1; |
|
416 |
1683
|
417 int nargin = args.length (); |
|
418 |
|
419 if (nargin > 0) |
2068
|
420 { |
|
421 // XXX FIXME XXX -- need a safe uniform way to do this. |
1683
|
422 |
2068
|
423 double tmp = args(0).double_value (); |
1683
|
424 |
2068
|
425 if (! error_state && ! xisnan (tmp)) |
|
426 exit_status = NINT (tmp); |
|
427 } |
|
428 |
|
429 clean_up_and_exit (exit_status); |
|
430 |
1683
|
431 return retval; |
|
432 } |
|
433 |
|
434 DEFALIAS (exit, quit); |
|
435 |
1957
|
436 DEFUN (warranty, , , |
1683
|
437 "warranty (): describe copying conditions") |
|
438 { |
2086
|
439 octave_value_list retval; |
1683
|
440 |
2095
|
441 octave_stdout << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
1683
|
442 This program is free software; you can redistribute it and/or modify\n\ |
|
443 it under the terms of the GNU General Public License as published by\n\ |
|
444 the Free Software Foundation; either version 2 of the License, or\n\ |
|
445 (at your option) any later version.\n\ |
|
446 \n\ |
|
447 This program is distributed in the hope that it will be useful,\n\ |
|
448 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
449 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
450 GNU General Public License for more details.\n\ |
|
451 \n\ |
|
452 You should have received a copy of the GNU General Public License\n\ |
|
453 along with this program. If not, write to the Free Software\n\ |
|
454 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
455 \n"; |
|
456 |
|
457 return retval; |
|
458 } |
|
459 |
|
460 // XXX FIXME XXX -- this may not be the best place for these... |
|
461 |
2942
|
462 static octave_value_list |
2086
|
463 feval (const octave_value_list& args, int nargout) |
1683
|
464 { |
2086
|
465 octave_value_list retval; |
1683
|
466 |
2973
|
467 octave_function *fcn = is_valid_function (args(0), "feval", 1); |
2891
|
468 |
1683
|
469 if (fcn) |
|
470 { |
2942
|
471 string_vector arg_names = args.name_tags (); |
|
472 |
1683
|
473 int tmp_nargin = args.length () - 1; |
2942
|
474 |
|
475 octave_value_list tmp_args (tmp_nargin, octave_value ()); |
|
476 |
|
477 string_vector tmp_arg_names (tmp_nargin); |
|
478 |
1683
|
479 for (int i = 0; i < tmp_nargin; i++) |
2942
|
480 { |
|
481 tmp_args(i) = args(i+1); |
|
482 tmp_arg_names(i) = arg_names(i+1); |
|
483 } |
|
484 |
|
485 tmp_args.stash_name_tags (tmp_arg_names); |
|
486 |
2973
|
487 retval = fcn->do_index_op (nargout, tmp_args); |
1683
|
488 } |
|
489 |
|
490 return retval; |
|
491 } |
|
492 |
1957
|
493 DEFUN (feval, args, nargout, |
1683
|
494 "feval (NAME, ARGS, ...)\n\ |
|
495 \n\ |
|
496 evaluate NAME as a function, passing ARGS as its arguments") |
|
497 { |
2086
|
498 octave_value_list retval; |
1683
|
499 |
|
500 int nargin = args.length (); |
|
501 |
|
502 if (nargin > 0) |
|
503 retval = feval (args, nargout); |
|
504 else |
|
505 print_usage ("feval"); |
|
506 |
|
507 return retval; |
|
508 } |
|
509 |
2086
|
510 static octave_value_list |
2898
|
511 eval_string (const string& s, bool silent, int& parse_status, int nargout) |
1683
|
512 { |
2985
|
513 unwind_protect::begin_frame ("eval_string"); |
1683
|
514 |
|
515 unwind_protect_int (get_input_from_eval_string); |
|
516 unwind_protect_int (input_from_command_line_file); |
|
517 unwind_protect_ptr (global_command); |
1755
|
518 unwind_protect_str (current_eval_string); |
1683
|
519 |
|
520 get_input_from_eval_string = 1; |
|
521 input_from_command_line_file = 0; |
1755
|
522 current_eval_string = s; |
1683
|
523 |
|
524 YY_BUFFER_STATE old_buf = current_buffer (); |
|
525 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
526 |
2985
|
527 unwind_protect::add (restore_input_buffer, old_buf); |
|
528 unwind_protect::add (delete_input_buffer, new_buf); |
1683
|
529 |
|
530 switch_to_buffer (new_buf); |
|
531 |
|
532 unwind_protect_ptr (curr_sym_tab); |
|
533 |
|
534 reset_parser (); |
|
535 |
|
536 parse_status = yyparse (); |
|
537 |
|
538 // Important to reset the idea of where input is coming from before |
|
539 // trying to eval the command we just parsed -- it might contain the |
|
540 // name of an function file that still needs to be parsed! |
|
541 |
|
542 tree_statement_list *command = global_command; |
|
543 |
2985
|
544 unwind_protect::run_frame ("eval_string"); |
1683
|
545 |
2086
|
546 octave_value_list retval; |
1683
|
547 |
|
548 if (parse_status == 0 && command) |
|
549 { |
2898
|
550 retval = command->eval (silent, nargout); |
1683
|
551 delete command; |
|
552 } |
|
553 |
|
554 return retval; |
|
555 } |
|
556 |
2086
|
557 octave_value |
2898
|
558 eval_string (const string& s, bool silent, int& parse_status) |
1683
|
559 { |
2086
|
560 octave_value retval; |
1683
|
561 |
2898
|
562 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
1683
|
563 |
3006
|
564 if (! tmp.empty ()) |
|
565 retval = tmp(0); |
1683
|
566 |
|
567 return retval; |
|
568 } |
|
569 |
2086
|
570 static octave_value_list |
2898
|
571 eval_string (const octave_value& arg, bool silent, int& parse_status, |
2806
|
572 int nargout) |
1683
|
573 { |
1755
|
574 string s = arg.string_value (); |
1683
|
575 |
|
576 if (error_state) |
|
577 { |
|
578 error ("eval: expecting string argument"); |
|
579 return -1.0; |
|
580 } |
|
581 |
2898
|
582 return eval_string (s, silent, parse_status, nargout); |
1683
|
583 } |
|
584 |
1957
|
585 DEFUN (eval, args, nargout, |
1683
|
586 "eval (TRY, CATCH)\n\ |
|
587 \n\ |
|
588 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
589 string CATCH.") |
|
590 { |
2086
|
591 octave_value_list retval; |
1683
|
592 |
|
593 int nargin = args.length (); |
|
594 |
|
595 if (nargin > 0) |
|
596 { |
2985
|
597 unwind_protect::begin_frame ("Feval"); |
1683
|
598 |
|
599 if (nargin > 1) |
|
600 { |
|
601 unwind_protect_int (buffer_error_messages); |
|
602 buffer_error_messages = 1; |
|
603 } |
|
604 |
|
605 int parse_status = 0; |
|
606 |
2898
|
607 retval = eval_string (args(0), ! Vdefault_eval_print_flag, |
2808
|
608 parse_status, nargout); |
1683
|
609 |
|
610 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
611 { |
|
612 error_state = 0; |
|
613 |
|
614 // Set up for letting the user print any messages from |
|
615 // errors that occurred in the first part of this eval(). |
|
616 |
|
617 buffer_error_messages = 0; |
|
618 bind_global_error_variable (); |
2985
|
619 unwind_protect::add (clear_global_error_variable, 0); |
1683
|
620 |
2808
|
621 eval_string (args(1), 0, parse_status, nargout); |
1683
|
622 |
2086
|
623 retval = octave_value_list (); |
1683
|
624 } |
|
625 |
2985
|
626 unwind_protect::run_frame ("Feval"); |
1683
|
627 } |
|
628 else |
|
629 print_usage ("eval"); |
|
630 |
|
631 return retval; |
|
632 } |
|
633 |
|
634 // Execute a shell command. |
|
635 |
1965
|
636 static void |
|
637 cleanup_iprocstream (void *p) |
|
638 { |
2800
|
639 delete static_cast<iprocstream *> (p); |
1965
|
640 } |
|
641 |
2086
|
642 static octave_value_list |
2321
|
643 run_command_and_return_output (const string& cmd_str) |
2083
|
644 { |
2086
|
645 octave_value_list retval; |
2083
|
646 |
|
647 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); |
|
648 |
2985
|
649 unwind_protect::add (cleanup_iprocstream, cmd); |
2083
|
650 |
|
651 int status = 127; |
|
652 |
|
653 if (cmd && *cmd) |
|
654 { |
|
655 ostrstream output_buf; |
2095
|
656 |
2083
|
657 char ch; |
|
658 while (cmd->get (ch)) |
2321
|
659 output_buf.put (ch); |
2083
|
660 |
|
661 status = cmd->close (); |
|
662 |
|
663 // The value in status is as returned by waitpid. If the |
|
664 // process exited normally, extract the actual exit status of |
|
665 // the command. Otherwise, return 127 as a failure code. |
|
666 |
2693
|
667 if (WIFEXITED (status)) |
|
668 status = WEXITSTATUS (status); |
2083
|
669 |
2321
|
670 output_buf << ends; |
2145
|
671 |
2321
|
672 char *msg = output_buf.str (); |
2083
|
673 |
2800
|
674 retval(1) = static_cast<double> (status); |
2321
|
675 retval(0) = msg; |
2083
|
676 |
2321
|
677 delete [] msg; |
2083
|
678 } |
|
679 else |
|
680 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); |
|
681 |
2985
|
682 unwind_protect::run (); |
2083
|
683 |
|
684 return retval; |
|
685 } |
|
686 |
1957
|
687 DEFUN (system, args, nargout, |
2083
|
688 "system (STRING [, RETURN_OUTPUT] [, TYPE])\n\ |
|
689 \n\ |
|
690 Execute the shell command specified by STRING.\n\ |
|
691 \n\ |
|
692 If TYPE is \"async\", the process is started in the background and the\n\ |
|
693 pid of the child proces is returned immediately. Otherwise, the\n\ |
|
694 process is started, and Octave waits until it exits. If TYPE argument\n\ |
|
695 is omitted, a value of \"sync\" is assumed.\n\ |
|
696 \n\ |
|
697 If NARGIN == 2 (the actual value of RETURN_OUTPUT is irrelevant) and\n\ |
|
698 the subprocess is started synchronously, or if system() is called with\n\ |
|
699 NARGIN == 1 and NARGOUT > 0, the output from the command is returned.\n\ |
|
700 Otherwise, if the subprocess is executed synchronously, it's output is\n\ |
2321
|
701 sent to the standard output. To send the output of a command executed\n\ |
|
702 with system() through the pager, use a command like\n\ |
|
703 \n\ |
|
704 disp (system (CMD, 1));\n\ |
|
705 \n\ |
|
706 or\n\ |
|
707 \n\ |
2322
|
708 printf (\"%s\\n\", system (CMD, 1));") |
1683
|
709 { |
2086
|
710 octave_value_list retval; |
1683
|
711 |
|
712 int nargin = args.length (); |
|
713 |
2083
|
714 if (nargin > 0 && nargin < 4) |
1683
|
715 { |
2083
|
716 bool return_output = (nargout > 0 || nargin > 1); |
|
717 |
|
718 string cmd_str = args(0).string_value (); |
|
719 |
|
720 enum exec_type { sync, async }; |
|
721 |
|
722 exec_type type = sync; |
|
723 |
|
724 if (! error_state) |
|
725 { |
|
726 if (nargin > 2) |
|
727 { |
|
728 string type_str = args(2).string_value (); |
1683
|
729 |
2083
|
730 if (! error_state) |
|
731 { |
|
732 if (type_str == "sync") |
|
733 type = sync; |
|
734 else if (type_str == "async") |
|
735 type = async; |
|
736 else |
|
737 error ("system: third arg must be \"sync\" or \"async\""); |
|
738 } |
|
739 else |
|
740 error ("system: third argument must be a string"); |
|
741 } |
|
742 } |
|
743 else |
|
744 error ("system: expecting string as first argument"); |
1683
|
745 |
2083
|
746 if (! error_state) |
|
747 { |
|
748 if (type == async) |
|
749 { |
|
750 pid_t pid = fork (); |
|
751 |
|
752 if (pid < 0) |
|
753 error ("system: fork failed -- can't create child process"); |
|
754 else if (pid == 0) |
|
755 { |
|
756 system (cmd_str.c_str ()); |
|
757 exit (0); |
|
758 retval(0) = 0.0; |
|
759 } |
|
760 else |
2800
|
761 retval(0) = static_cast<double> (pid); |
2083
|
762 } |
2321
|
763 else if (return_output) |
|
764 retval = run_command_and_return_output (cmd_str); |
2083
|
765 else |
2321
|
766 { |
|
767 int status = system (cmd_str.c_str ()); |
|
768 |
|
769 // The value in status is as returned by waitpid. If |
|
770 // the process exited normally, extract the actual exit |
|
771 // status of the command. Otherwise, return 127 as a |
|
772 // failure code. |
|
773 |
2693
|
774 if (WIFEXITED (status)) |
|
775 status = WEXITSTATUS (status); |
2321
|
776 |
2800
|
777 retval = static_cast<double> (status); |
2321
|
778 } |
2083
|
779 } |
1683
|
780 } |
|
781 else |
2083
|
782 print_usage ("system"); |
1683
|
783 |
|
784 return retval; |
|
785 } |
|
786 |
|
787 DEFALIAS (shell_cmd, system); |
|
788 |
2614
|
789 // XXX FIXME XXX -- this should really be static, but that causes |
|
790 // problems on some systems. |
|
791 SLStack<string> octave_atexit_functions; |
2077
|
792 |
|
793 void |
|
794 do_octave_atexit (void) |
|
795 { |
|
796 while (! octave_atexit_functions.empty ()) |
|
797 { |
2086
|
798 octave_value_list fcn = octave_atexit_functions.pop (); |
2077
|
799 |
2942
|
800 feval (fcn, 0); |
2077
|
801 } |
|
802 } |
|
803 |
2162
|
804 DEFUN (atexit, args, , |
2077
|
805 "atexit (NAME): register NAME as a function to call when Octave exits\n\ |
|
806 \n\ |
2802
|
807 Functions are called with no arguments in the reverse of the order in\n\ |
2077
|
808 which they were registered with atexit()") |
|
809 { |
2086
|
810 octave_value_list retval; |
2077
|
811 |
|
812 int nargin = args.length (); |
|
813 |
|
814 if (nargin == 1) |
|
815 { |
2475
|
816 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
2077
|
817 string arg = args(0).string_value (); |
|
818 |
|
819 if (! error_state) |
|
820 octave_atexit_functions.push (arg); |
|
821 else |
|
822 error ("atexit: argument must be a string"); |
2475
|
823 #else |
|
824 gripe_not_supported ("atexit"); |
|
825 #endif |
2077
|
826 } |
|
827 else |
|
828 print_usage ("atexit"); |
|
829 |
|
830 return retval; |
|
831 } |
|
832 |
2689
|
833 DEFUN (octave_config_info, args, , |
|
834 "octave_config_info (OPTION)\n\ |
|
835 \n\ |
|
836 If OPTION is a string, return the configuration information for the\n\ |
|
837 specified option.\n\ |
|
838 \n\ |
|
839 With no arguments, return a structure containing configuration\n\ |
|
840 information.") |
2162
|
841 { |
2689
|
842 octave_value retval; |
|
843 |
2696
|
844 #if defined (WITH_DYNAMIC_LINKING) && (defined (WITH_DL) || defined (WITH_SHL)) |
2689
|
845 bool octave_supports_dynamic_linking = true; |
|
846 #else |
|
847 bool octave_supports_dynamic_linking = false; |
|
848 #endif |
|
849 |
2162
|
850 Octave_map m; |
|
851 |
|
852 m ["default_pager"] = DEFAULT_PAGER; |
|
853 m ["prefix"] = OCTAVE_PREFIX; |
|
854 m ["exec_prefix"] = OCTAVE_EXEC_PREFIX; |
|
855 m ["datadir"] = OCTAVE_DATADIR; |
2800
|
856 m ["dld"] = static_cast<double> (octave_supports_dynamic_linking); |
2162
|
857 m ["libdir"] = OCTAVE_LIBDIR; |
|
858 m ["bindir"] = OCTAVE_BINDIR; |
|
859 m ["infodir"] = OCTAVE_INFODIR; |
|
860 m ["fcnfiledir"] = OCTAVE_FCNFILEDIR; |
|
861 m ["localfcnfiledir"] = OCTAVE_LOCALFCNFILEDIR; |
|
862 m ["localstartupfiledir"] = OCTAVE_LOCALSTARTUPFILEDIR; |
|
863 m ["startupfiledir"] = OCTAVE_STARTUPFILEDIR; |
|
864 m ["localfcnfilepath"] = OCTAVE_LOCALFCNFILEPATH; |
|
865 m ["archlibdir"] = OCTAVE_ARCHLIBDIR; |
2439
|
866 m ["localarchlibdir"] = OCTAVE_LOCALARCHLIBDIR; |
2162
|
867 m ["octfiledir"] = OCTAVE_OCTFILEDIR; |
|
868 m ["localoctfilepath"] = OCTAVE_LOCALOCTFILEPATH; |
|
869 m ["fcnfilepath"] = OCTAVE_FCNFILEPATH; |
|
870 m ["imagepath"] = OCTAVE_IMAGEPATH; |
|
871 m ["target_host_type"] = TARGET_HOST_TYPE; |
|
872 m ["configure_options"] = config_opts; |
|
873 m ["F77"] = F77; |
|
874 m ["FFLAGS"] = FFLAGS; |
|
875 m ["FPICFLAG"] = FPICFLAG; |
|
876 m ["F2C"] = F2C; |
|
877 m ["F2CFLAGS"] = F2CFLAGS; |
|
878 m ["FLIBS"] = FLIBS; |
|
879 m ["CPPFLAGS"] = CPPFLAGS; |
|
880 m ["INCFLAGS"] = INCFLAGS; |
|
881 m ["CC"] = CC " " CC_VERSION; |
|
882 m ["CFLAGS"] = CFLAGS; |
|
883 m ["CPICFLAG"] = CPICFLAG; |
|
884 m ["CXX"] = CXX " " CXX_VERSION; |
|
885 m ["CXXFLAGS"] = CXXFLAGS; |
|
886 m ["CXXPICFLAG"] = CXXPICFLAG; |
|
887 m ["LDFLAGS"] = LDFLAGS; |
|
888 m ["LIBFLAGS"] = LIBFLAGS; |
2675
|
889 m ["RLD_FLAG"] = RLD_FLAG; |
2162
|
890 m ["TERMLIBS"] = TERMLIBS; |
|
891 m ["LIBS"] = LIBS; |
|
892 m ["LEXLIB"] = LEXLIB; |
|
893 m ["LIBPLPLOT"] = LIBPLPLOT; |
|
894 m ["LIBDLFCN"] = LIBDLFCN; |
|
895 m ["DEFS"] = DEFS; |
|
896 |
2689
|
897 int nargin = args.length (); |
|
898 |
|
899 if (nargin == 1) |
|
900 { |
|
901 string arg = args(0).string_value (); |
|
902 |
|
903 if (! error_state) |
|
904 retval = octave_value (m [arg.c_str ()]); |
|
905 } |
|
906 else if (nargin == 0) |
|
907 retval = octave_value (m); |
|
908 else |
|
909 print_usage ("octave_config_info"); |
|
910 |
|
911 return retval; |
2162
|
912 } |
|
913 |
1683
|
914 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
2806
|
915 |
1683
|
916 int debug_new_delete = 0; |
|
917 |
|
918 typedef void (*vfp)(void); |
|
919 extern vfp __new_handler; |
|
920 |
|
921 void * |
|
922 __builtin_new (size_t sz) |
|
923 { |
|
924 void *p; |
|
925 |
|
926 /* malloc (0) is unpredictable; avoid it. */ |
|
927 if (sz == 0) |
|
928 sz = 1; |
2800
|
929 p = malloc (sz); |
1683
|
930 while (p == 0) |
|
931 { |
|
932 (*__new_handler) (); |
2800
|
933 p = malloc (sz); |
1683
|
934 } |
|
935 |
|
936 if (debug_new_delete) |
|
937 cout << "__builtin_new: " << p << endl; |
|
938 |
|
939 return p; |
|
940 } |
|
941 |
|
942 void |
|
943 __builtin_delete (void *ptr) |
|
944 { |
|
945 if (debug_new_delete) |
|
946 cout << "__builtin_delete: " << ptr << endl; |
|
947 |
|
948 if (ptr) |
|
949 free (ptr); |
|
950 } |
2806
|
951 |
1683
|
952 #endif |
|
953 |
2806
|
954 static int |
|
955 default_eval_print_flag (void) |
|
956 { |
|
957 Vdefault_eval_print_flag = check_preference ("default_eval_print_flag"); |
|
958 |
|
959 return 0; |
|
960 } |
|
961 |
|
962 void |
|
963 symbols_of_toplev (void) |
|
964 { |
|
965 DEFVAR (default_eval_print_flag, 1.0, 0, default_eval_print_flag, |
|
966 "If the value of this variable is nonzero, Octave will print the\n\ |
|
967 results of commands executed by eval() that do not end with semicolons."); |
|
968 |
|
969 } |
|
970 |
1683
|
971 /* |
|
972 ;;; Local Variables: *** |
|
973 ;;; mode: C++ *** |
|
974 ;;; End: *** |
|
975 */ |