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 "help.h" |
|
59 #include "input.h" |
|
60 #include "lex.h" |
2492
|
61 #include <oct-conf.h> |
1742
|
62 #include "oct-hist.h" |
2162
|
63 #include "oct-map.h" |
2862
|
64 #include "oct-obj.h" |
1683
|
65 #include "pager.h" |
|
66 #include "parse.h" |
|
67 #include "pathsearch.h" |
|
68 #include "procstream.h" |
2370
|
69 #include "ov.h" |
2985
|
70 #include "pt-jump.h" |
1750
|
71 #include "pt-plot.h" |
2982
|
72 #include "pt-stmt.h" |
1683
|
73 #include "sighandlers.h" |
|
74 #include "sysdep.h" |
2693
|
75 #include "syswait.h" |
1750
|
76 #include "toplev.h" |
1683
|
77 #include "unwind-prot.h" |
|
78 #include "utils.h" |
|
79 #include "variables.h" |
2492
|
80 #include <version.h> |
1683
|
81 |
2806
|
82 // Nonzero means we print |
|
83 static bool Vdefault_eval_print_flag = true; |
|
84 |
1820
|
85 // Nonzero means we are using readline. |
|
86 // (--no-line-editing) |
2926
|
87 int line_editing = 1; |
2552
|
88 |
1683
|
89 // Nonzero means we printed messages about reading startup files. |
|
90 int reading_startup_message_printed = 0; |
|
91 |
|
92 // Nonzero means we are exiting via the builtin exit or quit functions. |
|
93 int quitting_gracefully = 0; |
|
94 |
|
95 // Current command to execute. |
|
96 tree_statement_list *global_command = 0; |
|
97 |
|
98 // Pointer to function that is currently being evaluated. |
2891
|
99 octave_user_function *curr_function = 0; |
1683
|
100 |
|
101 // Nonzero means input is coming from startup file. |
|
102 int input_from_startup_file = 0; |
|
103 |
|
104 // Nonzero means that input is coming from a file that was named on |
|
105 // the command line. |
|
106 int input_from_command_line_file = 1; |
|
107 |
1907
|
108 // Top level context (?) |
|
109 jmp_buf toplevel; |
|
110 |
1683
|
111 void |
2898
|
112 parse_and_execute (FILE *f) |
1683
|
113 { |
2985
|
114 unwind_protect::begin_frame ("parse_and_execute"); |
1683
|
115 |
|
116 YY_BUFFER_STATE old_buf = current_buffer (); |
|
117 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
118 |
2985
|
119 unwind_protect::add (restore_input_buffer, old_buf); |
|
120 unwind_protect::add (delete_input_buffer, new_buf); |
1683
|
121 |
|
122 switch_to_buffer (new_buf); |
|
123 |
2926
|
124 unwind_protect_int (line_editing); |
1683
|
125 unwind_protect_int (input_from_command_line_file); |
|
126 |
2926
|
127 line_editing = 0; |
1683
|
128 input_from_command_line_file = 0; |
|
129 |
|
130 unwind_protect_ptr (curr_sym_tab); |
|
131 |
|
132 int retval; |
|
133 do |
|
134 { |
|
135 reset_parser (); |
|
136 |
|
137 retval = yyparse (); |
|
138 |
|
139 if (retval == 0 && global_command) |
|
140 { |
2898
|
141 global_command->eval (); |
2620
|
142 |
1683
|
143 delete global_command; |
2620
|
144 |
|
145 global_command = 0; |
|
146 |
2985
|
147 bool quit = (tree_return_command::returning |
|
148 || tree_break_command::breaking); |
2620
|
149 |
2985
|
150 if (tree_return_command::returning) |
|
151 tree_return_command::returning = 0; |
2620
|
152 |
2985
|
153 if (tree_break_command::breaking) |
|
154 tree_break_command::breaking--; |
2620
|
155 |
|
156 if (error_state) |
|
157 { |
|
158 error ("near line %d of file `%s'", input_line_number, |
|
159 curr_fcn_file_full_name.c_str ()); |
|
160 |
|
161 break; |
|
162 } |
|
163 |
|
164 if (quit) |
|
165 break; |
1683
|
166 } |
|
167 } |
|
168 while (retval == 0); |
|
169 |
2985
|
170 unwind_protect::run_frame ("parse_and_execute"); |
1683
|
171 } |
|
172 |
2517
|
173 static void |
|
174 safe_fclose (void *f) |
|
175 { |
|
176 if (f) |
2800
|
177 fclose (static_cast<FILE *> (f)); |
2517
|
178 } |
|
179 |
1683
|
180 void |
2898
|
181 parse_and_execute (const string& s, bool verbose, const char *warn_for) |
1683
|
182 { |
2985
|
183 unwind_protect::begin_frame ("parse_and_execute_2"); |
1683
|
184 |
|
185 unwind_protect_int (reading_script_file); |
1755
|
186 unwind_protect_str (curr_fcn_file_full_name); |
1683
|
187 |
|
188 reading_script_file = 1; |
1755
|
189 curr_fcn_file_full_name = s; |
1683
|
190 |
|
191 FILE *f = get_input_from_file (s, 0); |
1750
|
192 |
1683
|
193 if (f) |
|
194 { |
2985
|
195 unwind_protect::add (safe_fclose, f); |
2517
|
196 |
1683
|
197 unwind_protect_int (input_line_number); |
|
198 unwind_protect_int (current_input_column); |
|
199 |
|
200 input_line_number = 0; |
|
201 current_input_column = 1; |
|
202 |
|
203 if (verbose) |
|
204 { |
|
205 cout << "reading commands from " << s << " ... "; |
|
206 reading_startup_message_printed = 1; |
|
207 cout.flush (); |
|
208 } |
|
209 |
2898
|
210 parse_and_execute (f); |
1683
|
211 |
|
212 if (verbose) |
|
213 cout << "done." << endl; |
|
214 } |
|
215 else if (warn_for) |
1750
|
216 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
1683
|
217 |
2985
|
218 unwind_protect::run_frame ("parse_and_execute_2"); |
1683
|
219 } |
|
220 |
1907
|
221 int |
|
222 main_loop (void) |
|
223 { |
|
224 // Allow the user to interrupt us without exiting. |
|
225 |
2016
|
226 octave_save_signal_mask (); |
|
227 |
1907
|
228 if (setjmp (toplevel) != 0) |
|
229 { |
|
230 raw_mode (0); |
|
231 |
|
232 cout << "\n"; |
2016
|
233 |
|
234 octave_restore_signal_mask (); |
1907
|
235 } |
|
236 |
|
237 can_interrupt = 1; |
|
238 |
2554
|
239 octave_catch_interrupts (); |
1907
|
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 { |
2898
|
254 global_command->eval (); |
2299
|
255 |
1907
|
256 delete global_command; |
2299
|
257 |
2620
|
258 global_command = 0; |
|
259 |
|
260 if (! (interactive || forced_interactive)) |
|
261 { |
2985
|
262 bool quit = (tree_return_command::returning |
|
263 || tree_break_command::breaking); |
2620
|
264 |
2985
|
265 if (tree_return_command::returning) |
|
266 tree_return_command::returning = 0; |
2620
|
267 |
2985
|
268 if (tree_break_command::breaking) |
|
269 tree_break_command::breaking--; |
2620
|
270 |
|
271 if (quit) |
|
272 break; |
|
273 } |
|
274 |
|
275 if (error_state) |
|
276 { |
|
277 if (! (interactive || forced_interactive)) |
|
278 break; |
|
279 } |
2299
|
280 else |
2620
|
281 { |
|
282 if (octave_completion_matches_called) |
|
283 octave_completion_matches_called = false; |
|
284 else |
2973
|
285 command_editor::increment_current_command_number (); |
2620
|
286 } |
1907
|
287 } |
|
288 } |
|
289 while (retval == 0); |
|
290 |
|
291 return retval; |
|
292 } |
|
293 |
1957
|
294 DEFUN (source, args, , |
1683
|
295 "source (FILE)\n\ |
|
296 \n\ |
|
297 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
298 script file but without requiring the file to be named `FILE.m'.") |
|
299 { |
2086
|
300 octave_value_list retval; |
1683
|
301 |
|
302 int nargin = args.length (); |
|
303 |
|
304 if (nargin == 1) |
|
305 { |
1750
|
306 string file = args(0).string_value (); |
1683
|
307 |
|
308 if (! error_state) |
|
309 { |
2926
|
310 file = file_ops::tilde_expand (file); |
1683
|
311 |
2898
|
312 parse_and_execute (file, false, "source"); |
1683
|
313 |
|
314 if (error_state) |
1751
|
315 error ("source: error sourcing file `%s'", file.c_str ()); |
1683
|
316 } |
|
317 else |
|
318 error ("source: expecting file name as argument"); |
|
319 } |
|
320 else |
|
321 print_usage ("source"); |
|
322 |
|
323 return retval; |
|
324 } |
|
325 |
|
326 // Fix up things before exiting. |
|
327 |
|
328 void |
|
329 clean_up_and_exit (int retval) |
|
330 { |
2926
|
331 command_editor::restore_terminal_state (); |
|
332 |
|
333 // XXX FIXME XXX -- is this needed? Can it cause any trouble? |
1683
|
334 raw_mode (0); |
|
335 |
2926
|
336 command_history::clean_up_and_save (); |
1683
|
337 |
|
338 close_plot_stream (); |
|
339 |
|
340 close_files (); |
|
341 |
|
342 cleanup_tmp_files (); |
|
343 |
|
344 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
345 cout << "\n"; |
|
346 |
|
347 if (retval == EOF) |
|
348 retval = 0; |
|
349 |
|
350 exit (retval); |
|
351 |
|
352 // This is bogus but should prevent g++ from giving a warning saying |
|
353 // that this volatile function does return. |
|
354 |
|
355 panic_impossible (); |
|
356 } |
|
357 |
1957
|
358 DEFUN_TEXT (casesen, args, , |
1683
|
359 "casesen [on|off]") |
|
360 { |
2086
|
361 octave_value_list retval; |
1683
|
362 |
1755
|
363 int argc = args.length () + 1; |
|
364 |
1965
|
365 string_vector argv = args.make_argv ("casesen"); |
1683
|
366 |
1755
|
367 if (error_state) |
|
368 return retval; |
|
369 |
|
370 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
371 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
372 else if (argc > 1 && argv[1] == "on") |
1683
|
373 ; // ok. |
|
374 else |
|
375 print_usage ("casesen"); |
|
376 |
|
377 return retval; |
|
378 } |
|
379 |
1957
|
380 DEFUN (computer, args, nargout, |
1683
|
381 "computer ():\n\ |
|
382 \n\ |
|
383 Have Octave ask the system, \"What kind of computer are you?\"") |
|
384 { |
2086
|
385 octave_value_list retval; |
1683
|
386 |
|
387 int nargin = args.length (); |
|
388 |
|
389 if (nargin != 0) |
|
390 warning ("computer: ignoring extra arguments"); |
|
391 |
2095
|
392 string msg; |
1683
|
393 |
|
394 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
2095
|
395 msg = "Hi Dave, I'm a HAL-9000"; |
1683
|
396 else |
2095
|
397 msg = TARGET_HOST_TYPE; |
1683
|
398 |
|
399 if (nargout == 0) |
2095
|
400 octave_stdout << msg << "\n"; |
1683
|
401 else |
2095
|
402 retval = msg; |
1683
|
403 |
|
404 return retval; |
|
405 } |
|
406 |
2068
|
407 DEFUN (quit, args, , |
|
408 "quit (STATUS): exit Octave gracefully, returning STATUS to the system.\n\ |
|
409 \n\ |
|
410 STATUS should be an integer value. If STATUS is missing, 0 is assumed.") |
1683
|
411 { |
2086
|
412 octave_value_list retval; |
2068
|
413 |
|
414 int exit_status = 0; |
|
415 |
|
416 quitting_gracefully = 1; |
|
417 |
1683
|
418 int nargin = args.length (); |
|
419 |
|
420 if (nargin > 0) |
2068
|
421 { |
|
422 // XXX FIXME XXX -- need a safe uniform way to do this. |
1683
|
423 |
2068
|
424 double tmp = args(0).double_value (); |
1683
|
425 |
2068
|
426 if (! error_state && ! xisnan (tmp)) |
|
427 exit_status = NINT (tmp); |
|
428 } |
|
429 |
|
430 clean_up_and_exit (exit_status); |
|
431 |
1683
|
432 return retval; |
|
433 } |
|
434 |
|
435 DEFALIAS (exit, quit); |
|
436 |
1957
|
437 DEFUN (warranty, , , |
1683
|
438 "warranty (): describe copying conditions") |
|
439 { |
2086
|
440 octave_value_list retval; |
1683
|
441 |
2095
|
442 octave_stdout << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
1683
|
443 This program is free software; you can redistribute it and/or modify\n\ |
|
444 it under the terms of the GNU General Public License as published by\n\ |
|
445 the Free Software Foundation; either version 2 of the License, or\n\ |
|
446 (at your option) any later version.\n\ |
|
447 \n\ |
|
448 This program is distributed in the hope that it will be useful,\n\ |
|
449 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
450 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
451 GNU General Public License for more details.\n\ |
|
452 \n\ |
|
453 You should have received a copy of the GNU General Public License\n\ |
|
454 along with this program. If not, write to the Free Software\n\ |
|
455 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
456 \n"; |
|
457 |
|
458 return retval; |
|
459 } |
|
460 |
|
461 // XXX FIXME XXX -- this may not be the best place for these... |
|
462 |
2942
|
463 static octave_value_list |
2086
|
464 feval (const octave_value_list& args, int nargout) |
1683
|
465 { |
2086
|
466 octave_value_list retval; |
1683
|
467 |
2973
|
468 octave_function *fcn = is_valid_function (args(0), "feval", 1); |
2891
|
469 |
1683
|
470 if (fcn) |
|
471 { |
2942
|
472 string_vector arg_names = args.name_tags (); |
|
473 |
1683
|
474 int tmp_nargin = args.length () - 1; |
2942
|
475 |
|
476 octave_value_list tmp_args (tmp_nargin, octave_value ()); |
|
477 |
|
478 string_vector tmp_arg_names (tmp_nargin); |
|
479 |
1683
|
480 for (int i = 0; i < tmp_nargin; i++) |
2942
|
481 { |
|
482 tmp_args(i) = args(i+1); |
|
483 tmp_arg_names(i) = arg_names(i+1); |
|
484 } |
|
485 |
|
486 tmp_args.stash_name_tags (tmp_arg_names); |
|
487 |
2973
|
488 retval = fcn->do_index_op (nargout, tmp_args); |
1683
|
489 } |
|
490 |
|
491 return retval; |
|
492 } |
|
493 |
1957
|
494 DEFUN (feval, args, nargout, |
1683
|
495 "feval (NAME, ARGS, ...)\n\ |
|
496 \n\ |
|
497 evaluate NAME as a function, passing ARGS as its arguments") |
|
498 { |
2086
|
499 octave_value_list retval; |
1683
|
500 |
|
501 int nargin = args.length (); |
|
502 |
|
503 if (nargin > 0) |
|
504 retval = feval (args, nargout); |
|
505 else |
|
506 print_usage ("feval"); |
|
507 |
|
508 return retval; |
|
509 } |
|
510 |
2086
|
511 static octave_value_list |
2898
|
512 eval_string (const string& s, bool silent, int& parse_status, int nargout) |
1683
|
513 { |
2985
|
514 unwind_protect::begin_frame ("eval_string"); |
1683
|
515 |
|
516 unwind_protect_int (get_input_from_eval_string); |
|
517 unwind_protect_int (input_from_command_line_file); |
|
518 unwind_protect_ptr (global_command); |
1755
|
519 unwind_protect_str (current_eval_string); |
1683
|
520 |
|
521 get_input_from_eval_string = 1; |
|
522 input_from_command_line_file = 0; |
1755
|
523 current_eval_string = s; |
1683
|
524 |
|
525 YY_BUFFER_STATE old_buf = current_buffer (); |
|
526 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
527 |
2985
|
528 unwind_protect::add (restore_input_buffer, old_buf); |
|
529 unwind_protect::add (delete_input_buffer, new_buf); |
1683
|
530 |
|
531 switch_to_buffer (new_buf); |
|
532 |
|
533 unwind_protect_ptr (curr_sym_tab); |
|
534 |
|
535 reset_parser (); |
|
536 |
|
537 parse_status = yyparse (); |
|
538 |
|
539 // Important to reset the idea of where input is coming from before |
|
540 // trying to eval the command we just parsed -- it might contain the |
|
541 // name of an function file that still needs to be parsed! |
|
542 |
|
543 tree_statement_list *command = global_command; |
|
544 |
2985
|
545 unwind_protect::run_frame ("eval_string"); |
1683
|
546 |
2086
|
547 octave_value_list retval; |
1683
|
548 |
|
549 if (parse_status == 0 && command) |
|
550 { |
2898
|
551 retval = command->eval (silent, nargout); |
1683
|
552 delete command; |
|
553 } |
|
554 |
|
555 return retval; |
|
556 } |
|
557 |
2086
|
558 octave_value |
2898
|
559 eval_string (const string& s, bool silent, int& parse_status) |
1683
|
560 { |
2086
|
561 octave_value retval; |
1683
|
562 |
2898
|
563 octave_value_list tmp = eval_string (s, silent, parse_status, 1); |
1683
|
564 |
|
565 retval = tmp(0); |
|
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 */ |