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 |
|
147 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
148 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
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) |
|
204 fclose ((FILE *) f); |
|
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 { |
2517
|
223 add_unwind_protect (safe_fclose, (void *) f); |
|
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 |
1755
|
528 eval_string (const string& s, int print, int& parse_status, |
1683
|
529 int nargout) |
|
530 { |
|
531 begin_unwind_frame ("eval_string"); |
|
532 |
|
533 unwind_protect_int (get_input_from_eval_string); |
|
534 unwind_protect_int (input_from_command_line_file); |
|
535 unwind_protect_ptr (global_command); |
1755
|
536 unwind_protect_str (current_eval_string); |
1683
|
537 |
|
538 get_input_from_eval_string = 1; |
|
539 input_from_command_line_file = 0; |
1755
|
540 current_eval_string = s; |
1683
|
541 |
|
542 YY_BUFFER_STATE old_buf = current_buffer (); |
|
543 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
544 |
|
545 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
546 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
547 |
|
548 switch_to_buffer (new_buf); |
|
549 |
|
550 unwind_protect_ptr (curr_sym_tab); |
|
551 |
|
552 reset_parser (); |
|
553 |
|
554 parse_status = yyparse (); |
|
555 |
|
556 // Important to reset the idea of where input is coming from before |
|
557 // trying to eval the command we just parsed -- it might contain the |
|
558 // name of an function file that still needs to be parsed! |
|
559 |
|
560 tree_statement_list *command = global_command; |
|
561 |
|
562 run_unwind_frame ("eval_string"); |
|
563 |
2086
|
564 octave_value_list retval; |
1683
|
565 |
|
566 if (parse_status == 0 && command) |
|
567 { |
|
568 retval = command->eval (print, nargout); |
|
569 delete command; |
|
570 } |
|
571 |
|
572 return retval; |
|
573 } |
|
574 |
2086
|
575 octave_value |
1755
|
576 eval_string (const string& s, int print, int& parse_status) |
1683
|
577 { |
2086
|
578 octave_value retval; |
1683
|
579 |
2086
|
580 octave_value_list tmp = eval_string (s, print, parse_status, 1); |
1683
|
581 |
|
582 retval = tmp(0); |
|
583 |
|
584 return retval; |
|
585 } |
|
586 |
2086
|
587 static octave_value_list |
|
588 eval_string (const octave_value& arg, int& parse_status, int nargout) |
1683
|
589 { |
1755
|
590 string s = arg.string_value (); |
1683
|
591 |
|
592 if (error_state) |
|
593 { |
|
594 error ("eval: expecting string argument"); |
|
595 return -1.0; |
|
596 } |
|
597 |
|
598 // Yes Virginia, we always print here... |
|
599 |
1755
|
600 return eval_string (s, 1, parse_status, nargout); |
1683
|
601 } |
|
602 |
1957
|
603 DEFUN (eval, args, nargout, |
1683
|
604 "eval (TRY, CATCH)\n\ |
|
605 \n\ |
|
606 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
607 string CATCH.") |
|
608 { |
2086
|
609 octave_value_list retval; |
1683
|
610 |
|
611 int nargin = args.length (); |
|
612 |
|
613 if (nargin > 0) |
|
614 { |
|
615 begin_unwind_frame ("Feval"); |
|
616 |
|
617 if (nargin > 1) |
|
618 { |
|
619 unwind_protect_int (buffer_error_messages); |
|
620 buffer_error_messages = 1; |
|
621 } |
|
622 |
|
623 int parse_status = 0; |
|
624 |
|
625 retval = eval_string (args(0), parse_status, nargout); |
|
626 |
|
627 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
628 { |
|
629 error_state = 0; |
|
630 |
|
631 // Set up for letting the user print any messages from |
|
632 // errors that occurred in the first part of this eval(). |
|
633 |
|
634 buffer_error_messages = 0; |
|
635 bind_global_error_variable (); |
|
636 add_unwind_protect (clear_global_error_variable, 0); |
|
637 |
|
638 eval_string (args(1), parse_status, nargout); |
|
639 |
2086
|
640 retval = octave_value_list (); |
1683
|
641 } |
|
642 |
|
643 run_unwind_frame ("Feval"); |
|
644 } |
|
645 else |
|
646 print_usage ("eval"); |
|
647 |
|
648 return retval; |
|
649 } |
|
650 |
|
651 // Execute a shell command. |
|
652 |
2693
|
653 static sigset_t signal_set, old_signal_set; |
|
654 |
1965
|
655 static void |
|
656 cleanup_iprocstream (void *p) |
|
657 { |
2693
|
658 UNBLOCK_CHILD (old_signal_set); |
|
659 |
1965
|
660 delete (iprocstream *) p; |
|
661 } |
|
662 |
2086
|
663 static octave_value_list |
2321
|
664 run_command_and_return_output (const string& cmd_str) |
2083
|
665 { |
2086
|
666 octave_value_list retval; |
2083
|
667 |
2693
|
668 BLOCK_CHILD (signal_set, old_signal_set); |
|
669 |
2083
|
670 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); |
|
671 |
|
672 add_unwind_protect (cleanup_iprocstream, cmd); |
|
673 |
|
674 int status = 127; |
|
675 |
|
676 if (cmd && *cmd) |
|
677 { |
|
678 ostrstream output_buf; |
2095
|
679 |
2083
|
680 char ch; |
|
681 while (cmd->get (ch)) |
2321
|
682 output_buf.put (ch); |
2083
|
683 |
|
684 status = cmd->close (); |
|
685 |
|
686 // The value in status is as returned by waitpid. If the |
|
687 // process exited normally, extract the actual exit status of |
|
688 // the command. Otherwise, return 127 as a failure code. |
|
689 |
2693
|
690 if (WIFEXITED (status)) |
|
691 status = WEXITSTATUS (status); |
2083
|
692 |
2321
|
693 output_buf << ends; |
2145
|
694 |
2321
|
695 char *msg = output_buf.str (); |
2083
|
696 |
2321
|
697 retval(1) = (double) status; |
|
698 retval(0) = msg; |
2083
|
699 |
2321
|
700 delete [] msg; |
2083
|
701 } |
|
702 else |
|
703 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); |
|
704 |
|
705 run_unwind_protect (); |
|
706 |
|
707 return retval; |
|
708 } |
|
709 |
1957
|
710 DEFUN (system, args, nargout, |
2083
|
711 "system (STRING [, RETURN_OUTPUT] [, TYPE])\n\ |
|
712 \n\ |
|
713 Execute the shell command specified by STRING.\n\ |
|
714 \n\ |
|
715 If TYPE is \"async\", the process is started in the background and the\n\ |
|
716 pid of the child proces is returned immediately. Otherwise, the\n\ |
|
717 process is started, and Octave waits until it exits. If TYPE argument\n\ |
|
718 is omitted, a value of \"sync\" is assumed.\n\ |
|
719 \n\ |
|
720 If NARGIN == 2 (the actual value of RETURN_OUTPUT is irrelevant) and\n\ |
|
721 the subprocess is started synchronously, or if system() is called with\n\ |
|
722 NARGIN == 1 and NARGOUT > 0, the output from the command is returned.\n\ |
|
723 Otherwise, if the subprocess is executed synchronously, it's output is\n\ |
2321
|
724 sent to the standard output. To send the output of a command executed\n\ |
|
725 with system() through the pager, use a command like\n\ |
|
726 \n\ |
|
727 disp (system (CMD, 1));\n\ |
|
728 \n\ |
|
729 or\n\ |
|
730 \n\ |
2322
|
731 printf (\"%s\\n\", system (CMD, 1));") |
1683
|
732 { |
2086
|
733 octave_value_list retval; |
1683
|
734 |
|
735 int nargin = args.length (); |
|
736 |
2083
|
737 if (nargin > 0 && nargin < 4) |
1683
|
738 { |
2083
|
739 bool return_output = (nargout > 0 || nargin > 1); |
|
740 |
|
741 string cmd_str = args(0).string_value (); |
|
742 |
|
743 enum exec_type { sync, async }; |
|
744 |
|
745 exec_type type = sync; |
|
746 |
|
747 if (! error_state) |
|
748 { |
|
749 if (nargin > 2) |
|
750 { |
|
751 string type_str = args(2).string_value (); |
1683
|
752 |
2083
|
753 if (! error_state) |
|
754 { |
|
755 if (type_str == "sync") |
|
756 type = sync; |
|
757 else if (type_str == "async") |
|
758 type = async; |
|
759 else |
|
760 error ("system: third arg must be \"sync\" or \"async\""); |
|
761 } |
|
762 else |
|
763 error ("system: third argument must be a string"); |
|
764 } |
|
765 } |
|
766 else |
|
767 error ("system: expecting string as first argument"); |
1683
|
768 |
2083
|
769 if (! error_state) |
|
770 { |
|
771 if (type == async) |
|
772 { |
|
773 pid_t pid = fork (); |
|
774 |
|
775 if (pid < 0) |
|
776 error ("system: fork failed -- can't create child process"); |
|
777 else if (pid == 0) |
|
778 { |
|
779 system (cmd_str.c_str ()); |
|
780 exit (0); |
|
781 retval(0) = 0.0; |
|
782 } |
|
783 else |
|
784 retval(0) = (double) pid; |
|
785 } |
2321
|
786 else if (return_output) |
|
787 retval = run_command_and_return_output (cmd_str); |
2083
|
788 else |
2321
|
789 { |
|
790 int status = system (cmd_str.c_str ()); |
|
791 |
|
792 // The value in status is as returned by waitpid. If |
|
793 // the process exited normally, extract the actual exit |
|
794 // status of the command. Otherwise, return 127 as a |
|
795 // failure code. |
|
796 |
2693
|
797 if (WIFEXITED (status)) |
|
798 status = WEXITSTATUS (status); |
2321
|
799 |
|
800 retval = (double) status; |
|
801 } |
2083
|
802 } |
1683
|
803 } |
|
804 else |
2083
|
805 print_usage ("system"); |
1683
|
806 |
|
807 return retval; |
|
808 } |
|
809 |
|
810 DEFALIAS (shell_cmd, system); |
|
811 |
2614
|
812 // XXX FIXME XXX -- this should really be static, but that causes |
|
813 // problems on some systems. |
|
814 SLStack<string> octave_atexit_functions; |
2077
|
815 |
|
816 void |
|
817 do_octave_atexit (void) |
|
818 { |
|
819 while (! octave_atexit_functions.empty ()) |
|
820 { |
2086
|
821 octave_value_list fcn = octave_atexit_functions.pop (); |
2077
|
822 |
|
823 feval (fcn, 0); |
|
824 } |
|
825 } |
|
826 |
2162
|
827 DEFUN (atexit, args, , |
2077
|
828 "atexit (NAME): register NAME as a function to call when Octave exits\n\ |
|
829 \n\ |
|
830 Functions are called with no arguments in the reverse of the order in |
|
831 which they were registered with atexit()") |
|
832 { |
2086
|
833 octave_value_list retval; |
2077
|
834 |
|
835 int nargin = args.length (); |
|
836 |
|
837 if (nargin == 1) |
|
838 { |
2475
|
839 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
2077
|
840 string arg = args(0).string_value (); |
|
841 |
|
842 if (! error_state) |
|
843 octave_atexit_functions.push (arg); |
|
844 else |
|
845 error ("atexit: argument must be a string"); |
2475
|
846 #else |
|
847 gripe_not_supported ("atexit"); |
|
848 #endif |
2077
|
849 } |
|
850 else |
|
851 print_usage ("atexit"); |
|
852 |
|
853 return retval; |
|
854 } |
|
855 |
2689
|
856 DEFUN (octave_config_info, args, , |
|
857 "octave_config_info (OPTION)\n\ |
|
858 \n\ |
|
859 If OPTION is a string, return the configuration information for the\n\ |
|
860 specified option.\n\ |
|
861 \n\ |
|
862 With no arguments, return a structure containing configuration\n\ |
|
863 information.") |
2162
|
864 { |
2689
|
865 octave_value retval; |
|
866 |
2696
|
867 #if defined (WITH_DYNAMIC_LINKING) && (defined (WITH_DL) || defined (WITH_SHL)) |
2689
|
868 bool octave_supports_dynamic_linking = true; |
|
869 #else |
|
870 bool octave_supports_dynamic_linking = false; |
|
871 #endif |
|
872 |
2162
|
873 Octave_map m; |
|
874 |
|
875 m ["default_pager"] = DEFAULT_PAGER; |
|
876 m ["prefix"] = OCTAVE_PREFIX; |
|
877 m ["exec_prefix"] = OCTAVE_EXEC_PREFIX; |
|
878 m ["datadir"] = OCTAVE_DATADIR; |
2689
|
879 m ["dld"] = (double) octave_supports_dynamic_linking; |
2162
|
880 m ["libdir"] = OCTAVE_LIBDIR; |
|
881 m ["bindir"] = OCTAVE_BINDIR; |
|
882 m ["infodir"] = OCTAVE_INFODIR; |
|
883 m ["fcnfiledir"] = OCTAVE_FCNFILEDIR; |
|
884 m ["localfcnfiledir"] = OCTAVE_LOCALFCNFILEDIR; |
|
885 m ["localstartupfiledir"] = OCTAVE_LOCALSTARTUPFILEDIR; |
|
886 m ["startupfiledir"] = OCTAVE_STARTUPFILEDIR; |
|
887 m ["localfcnfilepath"] = OCTAVE_LOCALFCNFILEPATH; |
|
888 m ["archlibdir"] = OCTAVE_ARCHLIBDIR; |
2439
|
889 m ["localarchlibdir"] = OCTAVE_LOCALARCHLIBDIR; |
2162
|
890 m ["octfiledir"] = OCTAVE_OCTFILEDIR; |
|
891 m ["localoctfilepath"] = OCTAVE_LOCALOCTFILEPATH; |
|
892 m ["fcnfilepath"] = OCTAVE_FCNFILEPATH; |
|
893 m ["imagepath"] = OCTAVE_IMAGEPATH; |
|
894 m ["target_host_type"] = TARGET_HOST_TYPE; |
|
895 m ["configure_options"] = config_opts; |
|
896 m ["F77"] = F77; |
|
897 m ["FFLAGS"] = FFLAGS; |
|
898 m ["FPICFLAG"] = FPICFLAG; |
|
899 m ["F2C"] = F2C; |
|
900 m ["F2CFLAGS"] = F2CFLAGS; |
|
901 m ["FLIBS"] = FLIBS; |
|
902 m ["CPPFLAGS"] = CPPFLAGS; |
|
903 m ["INCFLAGS"] = INCFLAGS; |
|
904 m ["CC"] = CC " " CC_VERSION; |
|
905 m ["CFLAGS"] = CFLAGS; |
|
906 m ["CPICFLAG"] = CPICFLAG; |
|
907 m ["CXX"] = CXX " " CXX_VERSION; |
|
908 m ["CXXFLAGS"] = CXXFLAGS; |
|
909 m ["CXXPICFLAG"] = CXXPICFLAG; |
|
910 m ["LDFLAGS"] = LDFLAGS; |
|
911 m ["LIBFLAGS"] = LIBFLAGS; |
2675
|
912 m ["RLD_FLAG"] = RLD_FLAG; |
2162
|
913 m ["CXXLIBS"] = CXXLIBS; |
|
914 m ["TERMLIBS"] = TERMLIBS; |
|
915 m ["LIBS"] = LIBS; |
|
916 m ["LEXLIB"] = LEXLIB; |
|
917 m ["LIBPLPLOT"] = LIBPLPLOT; |
|
918 m ["LIBDLFCN"] = LIBDLFCN; |
|
919 m ["DEFS"] = DEFS; |
|
920 |
2689
|
921 int nargin = args.length (); |
|
922 |
|
923 if (nargin == 1) |
|
924 { |
|
925 string arg = args(0).string_value (); |
|
926 |
|
927 if (! error_state) |
|
928 retval = octave_value (m [arg.c_str ()]); |
|
929 } |
|
930 else if (nargin == 0) |
|
931 retval = octave_value (m); |
|
932 else |
|
933 print_usage ("octave_config_info"); |
|
934 |
|
935 return retval; |
2162
|
936 } |
|
937 |
1683
|
938 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
939 int debug_new_delete = 0; |
|
940 |
|
941 typedef void (*vfp)(void); |
|
942 extern vfp __new_handler; |
|
943 |
|
944 void * |
|
945 __builtin_new (size_t sz) |
|
946 { |
|
947 void *p; |
|
948 |
|
949 /* malloc (0) is unpredictable; avoid it. */ |
|
950 if (sz == 0) |
|
951 sz = 1; |
|
952 p = (void *) malloc (sz); |
|
953 while (p == 0) |
|
954 { |
|
955 (*__new_handler) (); |
|
956 p = (void *) malloc (sz); |
|
957 } |
|
958 |
|
959 if (debug_new_delete) |
|
960 cout << "__builtin_new: " << p << endl; |
|
961 |
|
962 return p; |
|
963 } |
|
964 |
|
965 void |
|
966 __builtin_delete (void *ptr) |
|
967 { |
|
968 if (debug_new_delete) |
|
969 cout << "__builtin_delete: " << ptr << endl; |
|
970 |
|
971 if (ptr) |
|
972 free (ptr); |
|
973 } |
|
974 #endif |
|
975 |
|
976 /* |
|
977 ;;; Local Variables: *** |
|
978 ;;; mode: C++ *** |
|
979 ;;; End: *** |
|
980 */ |