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