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