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