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 |
|
41 #include <sys/types.h> |
|
42 #include <unistd.h> |
|
43 #endif |
|
44 |
|
45 #include "lo-error.h" |
1755
|
46 #include "str-vec.h" |
1683
|
47 |
|
48 #include "builtins.h" |
|
49 #include "defaults.h" |
|
50 #include "defun.h" |
|
51 #include "dynamic-ld.h" |
|
52 #include "error.h" |
|
53 #include "file-io.h" |
|
54 #include "help.h" |
|
55 #include "input.h" |
|
56 #include "lex.h" |
1742
|
57 #include "oct-hist.h" |
1683
|
58 #include "pager.h" |
|
59 #include "parse.h" |
|
60 #include "pathsearch.h" |
|
61 #include "procstream.h" |
1750
|
62 #include "pt-const.h" |
|
63 #include "pt-misc.h" |
|
64 #include "pt-plot.h" |
1683
|
65 #include "sighandlers.h" |
|
66 #include "sysdep.h" |
1750
|
67 #include "toplev.h" |
1683
|
68 #include "unwind-prot.h" |
|
69 #include "user-prefs.h" |
|
70 #include "utils.h" |
|
71 #include "variables.h" |
|
72 #include "version.h" |
|
73 |
|
74 // argv[0] for this program. |
1755
|
75 string raw_prog_name; |
1683
|
76 |
|
77 // Cleaned-up name of this program, not including path information. |
1755
|
78 string prog_name; |
1683
|
79 |
|
80 // Login name for user running this program. |
1755
|
81 string user_name; |
1683
|
82 |
|
83 // Name of the host we are running on. |
1755
|
84 string host_name; |
1683
|
85 |
|
86 // User's home directory. |
1755
|
87 string home_directory; |
1683
|
88 |
|
89 // Guess what? |
1755
|
90 string the_current_working_directory; |
1683
|
91 |
|
92 // The path that will be searched for programs that we execute. |
|
93 // (--exec-path path) |
1755
|
94 string exec_path; |
1683
|
95 |
|
96 // Load path specified on command line. |
|
97 // (--path path; -p path) |
1755
|
98 string load_path; |
1683
|
99 |
|
100 // Name of the info file specified on command line. |
|
101 // (--info-file file) |
1755
|
102 string info_file; |
1683
|
103 |
|
104 // Name of the info reader we'd like to use. |
|
105 // (--info-program program) |
1755
|
106 string info_prog; |
1683
|
107 |
|
108 // Name of the editor to be invoked by the edit_history command. |
1755
|
109 string editor; |
1683
|
110 |
1820
|
111 // Nonzero means we are using readline. |
|
112 // (--no-line-editing) |
|
113 #if defined (USE_READLINE) |
|
114 int using_readline = 1; |
|
115 #else |
|
116 int using_readline = 0; |
|
117 #endif |
1683
|
118 |
|
119 // Nonzero means we printed messages about reading startup files. |
|
120 int reading_startup_message_printed = 0; |
|
121 |
|
122 // Command number, counting from the beginning of this session. |
|
123 int current_command_number = 1; |
|
124 |
|
125 // Nonzero means we are exiting via the builtin exit or quit functions. |
|
126 int quitting_gracefully = 0; |
|
127 |
|
128 // Current command to execute. |
|
129 tree_statement_list *global_command = 0; |
|
130 |
|
131 // Pointer to function that is currently being evaluated. |
|
132 tree_function *curr_function = 0; |
|
133 |
|
134 // Nonzero means input is coming from startup file. |
|
135 int input_from_startup_file = 0; |
|
136 |
|
137 // Nonzero means that input is coming from a file that was named on |
|
138 // the command line. |
|
139 int input_from_command_line_file = 1; |
|
140 |
1907
|
141 // Top level context (?) |
|
142 jmp_buf toplevel; |
|
143 |
1683
|
144 void |
|
145 parse_and_execute (FILE *f, int print) |
|
146 { |
|
147 begin_unwind_frame ("parse_and_execute"); |
|
148 |
|
149 YY_BUFFER_STATE old_buf = current_buffer (); |
|
150 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
151 |
|
152 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
153 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
154 |
|
155 switch_to_buffer (new_buf); |
|
156 |
|
157 unwind_protect_int (using_readline); |
|
158 unwind_protect_int (input_from_command_line_file); |
|
159 |
|
160 using_readline = 0; |
|
161 input_from_command_line_file = 0; |
|
162 |
|
163 unwind_protect_ptr (curr_sym_tab); |
|
164 |
|
165 int retval; |
|
166 do |
|
167 { |
|
168 reset_parser (); |
|
169 |
|
170 retval = yyparse (); |
|
171 |
|
172 if (retval == 0 && global_command) |
|
173 { |
|
174 global_command->eval (print); |
|
175 delete global_command; |
|
176 } |
|
177 } |
|
178 while (retval == 0); |
|
179 |
|
180 run_unwind_frame ("parse_and_execute"); |
|
181 } |
|
182 |
|
183 void |
1750
|
184 parse_and_execute (const string& s, int print, int verbose, |
1683
|
185 const char *warn_for) |
|
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 { |
|
199 unwind_protect_int (input_line_number); |
|
200 unwind_protect_int (current_input_column); |
|
201 |
|
202 input_line_number = 0; |
|
203 current_input_column = 1; |
|
204 |
|
205 if (verbose) |
|
206 { |
|
207 cout << "reading commands from " << s << " ... "; |
|
208 reading_startup_message_printed = 1; |
|
209 cout.flush (); |
|
210 } |
|
211 |
|
212 parse_and_execute (f, print); |
|
213 |
|
214 fclose (f); |
|
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 |
|
243 catch_interrupts (); |
|
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 { |
|
258 global_command->eval (1); |
|
259 delete global_command; |
|
260 current_command_number++; |
|
261 } |
|
262 } |
|
263 while (retval == 0); |
|
264 |
|
265 return retval; |
|
266 } |
|
267 |
1957
|
268 DEFUN (source, args, , |
1683
|
269 "source (FILE)\n\ |
|
270 \n\ |
|
271 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
272 script file but without requiring the file to be named `FILE.m'.") |
|
273 { |
2086
|
274 octave_value_list retval; |
1683
|
275 |
|
276 int nargin = args.length (); |
|
277 |
|
278 if (nargin == 1) |
|
279 { |
1750
|
280 string file = args(0).string_value (); |
1683
|
281 |
|
282 if (! error_state) |
|
283 { |
1750
|
284 file = oct_tilde_expand (file); |
1683
|
285 |
|
286 parse_and_execute (file, 1, 0, "source"); |
|
287 |
|
288 if (error_state) |
1751
|
289 error ("source: error sourcing file `%s'", file.c_str ()); |
1683
|
290 } |
|
291 else |
|
292 error ("source: expecting file name as argument"); |
|
293 } |
|
294 else |
|
295 print_usage ("source"); |
|
296 |
|
297 return retval; |
|
298 } |
|
299 |
|
300 // Fix up things before exiting. |
|
301 |
|
302 void |
|
303 clean_up_and_exit (int retval) |
|
304 { |
|
305 raw_mode (0); |
|
306 |
1799
|
307 octave_command_history.clean_up_and_save (); |
1683
|
308 |
|
309 close_plot_stream (); |
|
310 |
|
311 close_files (); |
|
312 |
|
313 cleanup_tmp_files (); |
|
314 |
|
315 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
316 cout << "\n"; |
|
317 |
|
318 if (retval == EOF) |
|
319 retval = 0; |
|
320 |
|
321 exit (retval); |
|
322 |
|
323 // This is bogus but should prevent g++ from giving a warning saying |
|
324 // that this volatile function does return. |
|
325 |
|
326 panic_impossible (); |
|
327 } |
|
328 |
1957
|
329 DEFUN_TEXT (casesen, args, , |
1683
|
330 "casesen [on|off]") |
|
331 { |
2086
|
332 octave_value_list retval; |
1683
|
333 |
1755
|
334 int argc = args.length () + 1; |
|
335 |
1965
|
336 string_vector argv = args.make_argv ("casesen"); |
1683
|
337 |
1755
|
338 if (error_state) |
|
339 return retval; |
|
340 |
|
341 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
342 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
343 else if (argc > 1 && argv[1] == "on") |
1683
|
344 ; // ok. |
|
345 else |
|
346 print_usage ("casesen"); |
|
347 |
|
348 return retval; |
|
349 } |
|
350 |
1957
|
351 DEFUN (computer, args, nargout, |
1683
|
352 "computer ():\n\ |
|
353 \n\ |
|
354 Have Octave ask the system, \"What kind of computer are you?\"") |
|
355 { |
2086
|
356 octave_value_list retval; |
1683
|
357 |
|
358 int nargin = args.length (); |
|
359 |
|
360 if (nargin != 0) |
|
361 warning ("computer: ignoring extra arguments"); |
|
362 |
2095
|
363 string msg; |
1683
|
364 |
|
365 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
2095
|
366 msg = "Hi Dave, I'm a HAL-9000"; |
1683
|
367 else |
2095
|
368 msg = TARGET_HOST_TYPE; |
1683
|
369 |
|
370 if (nargout == 0) |
2095
|
371 octave_stdout << msg << "\n"; |
1683
|
372 else |
2095
|
373 retval = msg; |
1683
|
374 |
|
375 return retval; |
|
376 } |
|
377 |
2068
|
378 DEFUN (quit, args, , |
|
379 "quit (STATUS): exit Octave gracefully, returning STATUS to the system.\n\ |
|
380 \n\ |
|
381 STATUS should be an integer value. If STATUS is missing, 0 is assumed.") |
1683
|
382 { |
2086
|
383 octave_value_list retval; |
2068
|
384 |
|
385 int exit_status = 0; |
|
386 |
|
387 quitting_gracefully = 1; |
|
388 |
1683
|
389 int nargin = args.length (); |
|
390 |
|
391 if (nargin > 0) |
2068
|
392 { |
|
393 // XXX FIXME XXX -- need a safe uniform way to do this. |
1683
|
394 |
2068
|
395 double tmp = args(0).double_value (); |
1683
|
396 |
2068
|
397 if (! error_state && ! xisnan (tmp)) |
|
398 exit_status = NINT (tmp); |
|
399 } |
|
400 |
|
401 clean_up_and_exit (exit_status); |
|
402 |
1683
|
403 return retval; |
|
404 } |
|
405 |
|
406 DEFALIAS (exit, quit); |
|
407 |
1957
|
408 DEFUN (warranty, , , |
1683
|
409 "warranty (): describe copying conditions") |
|
410 { |
2086
|
411 octave_value_list retval; |
1683
|
412 |
2095
|
413 octave_stdout << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
1683
|
414 This program is free software; you can redistribute it and/or modify\n\ |
|
415 it under the terms of the GNU General Public License as published by\n\ |
|
416 the Free Software Foundation; either version 2 of the License, or\n\ |
|
417 (at your option) any later version.\n\ |
|
418 \n\ |
|
419 This program is distributed in the hope that it will be useful,\n\ |
|
420 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
421 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
422 GNU General Public License for more details.\n\ |
|
423 \n\ |
|
424 You should have received a copy of the GNU General Public License\n\ |
|
425 along with this program. If not, write to the Free Software\n\ |
|
426 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
427 \n"; |
|
428 |
|
429 return retval; |
|
430 } |
|
431 |
|
432 // XXX FIXME XXX -- this may not be the best place for these... |
|
433 |
2086
|
434 octave_value_list |
|
435 feval (const octave_value_list& args, int nargout) |
1683
|
436 { |
2086
|
437 octave_value_list retval; |
1683
|
438 |
|
439 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
|
440 if (fcn) |
|
441 { |
|
442 int tmp_nargin = args.length () - 1; |
2086
|
443 octave_value_list tmp_args; |
1683
|
444 tmp_args.resize (tmp_nargin); |
|
445 for (int i = 0; i < tmp_nargin; i++) |
|
446 tmp_args(i) = args(i+1); |
|
447 retval = fcn->eval (0, nargout, tmp_args); |
|
448 } |
|
449 |
|
450 return retval; |
|
451 } |
|
452 |
1957
|
453 DEFUN (feval, args, nargout, |
1683
|
454 "feval (NAME, ARGS, ...)\n\ |
|
455 \n\ |
|
456 evaluate NAME as a function, passing ARGS as its arguments") |
|
457 { |
2086
|
458 octave_value_list retval; |
1683
|
459 |
|
460 int nargin = args.length (); |
|
461 |
|
462 if (nargin > 0) |
|
463 retval = feval (args, nargout); |
|
464 else |
|
465 print_usage ("feval"); |
|
466 |
|
467 return retval; |
|
468 } |
|
469 |
2086
|
470 static octave_value_list |
1755
|
471 eval_string (const string& s, int print, int& parse_status, |
1683
|
472 int nargout) |
|
473 { |
|
474 begin_unwind_frame ("eval_string"); |
|
475 |
|
476 unwind_protect_int (get_input_from_eval_string); |
|
477 unwind_protect_int (input_from_command_line_file); |
|
478 unwind_protect_ptr (global_command); |
1755
|
479 unwind_protect_str (current_eval_string); |
1683
|
480 |
|
481 get_input_from_eval_string = 1; |
|
482 input_from_command_line_file = 0; |
1755
|
483 current_eval_string = s; |
1683
|
484 |
|
485 YY_BUFFER_STATE old_buf = current_buffer (); |
|
486 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
487 |
|
488 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
489 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
490 |
|
491 switch_to_buffer (new_buf); |
|
492 |
|
493 unwind_protect_ptr (curr_sym_tab); |
|
494 |
|
495 reset_parser (); |
|
496 |
|
497 parse_status = yyparse (); |
|
498 |
|
499 // Important to reset the idea of where input is coming from before |
|
500 // trying to eval the command we just parsed -- it might contain the |
|
501 // name of an function file that still needs to be parsed! |
|
502 |
|
503 tree_statement_list *command = global_command; |
|
504 |
|
505 run_unwind_frame ("eval_string"); |
|
506 |
2086
|
507 octave_value_list retval; |
1683
|
508 |
|
509 if (parse_status == 0 && command) |
|
510 { |
|
511 retval = command->eval (print, nargout); |
|
512 delete command; |
|
513 } |
|
514 |
|
515 return retval; |
|
516 } |
|
517 |
2086
|
518 octave_value |
1755
|
519 eval_string (const string& s, int print, int& parse_status) |
1683
|
520 { |
2086
|
521 octave_value retval; |
1683
|
522 |
2086
|
523 octave_value_list tmp = eval_string (s, print, parse_status, 1); |
1683
|
524 |
|
525 retval = tmp(0); |
|
526 |
|
527 return retval; |
|
528 } |
|
529 |
2086
|
530 static octave_value_list |
|
531 eval_string (const octave_value& arg, int& parse_status, int nargout) |
1683
|
532 { |
1755
|
533 string s = arg.string_value (); |
1683
|
534 |
|
535 if (error_state) |
|
536 { |
|
537 error ("eval: expecting string argument"); |
|
538 return -1.0; |
|
539 } |
|
540 |
|
541 // Yes Virginia, we always print here... |
|
542 |
1755
|
543 return eval_string (s, 1, parse_status, nargout); |
1683
|
544 } |
|
545 |
1957
|
546 DEFUN (eval, args, nargout, |
1683
|
547 "eval (TRY, CATCH)\n\ |
|
548 \n\ |
|
549 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
550 string CATCH.") |
|
551 { |
2086
|
552 octave_value_list retval; |
1683
|
553 |
|
554 int nargin = args.length (); |
|
555 |
|
556 if (nargin > 0) |
|
557 { |
|
558 begin_unwind_frame ("Feval"); |
|
559 |
|
560 if (nargin > 1) |
|
561 { |
|
562 unwind_protect_int (buffer_error_messages); |
|
563 buffer_error_messages = 1; |
|
564 } |
|
565 |
|
566 int parse_status = 0; |
|
567 |
|
568 retval = eval_string (args(0), parse_status, nargout); |
|
569 |
|
570 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
571 { |
|
572 error_state = 0; |
|
573 |
|
574 // Set up for letting the user print any messages from |
|
575 // errors that occurred in the first part of this eval(). |
|
576 |
|
577 buffer_error_messages = 0; |
|
578 bind_global_error_variable (); |
|
579 add_unwind_protect (clear_global_error_variable, 0); |
|
580 |
|
581 eval_string (args(1), parse_status, nargout); |
|
582 |
2086
|
583 retval = octave_value_list (); |
1683
|
584 } |
|
585 |
|
586 run_unwind_frame ("Feval"); |
|
587 } |
|
588 else |
|
589 print_usage ("eval"); |
|
590 |
|
591 return retval; |
|
592 } |
|
593 |
|
594 // Execute a shell command. |
|
595 |
1965
|
596 static void |
|
597 cleanup_iprocstream (void *p) |
|
598 { |
|
599 delete (iprocstream *) p; |
|
600 } |
|
601 |
2086
|
602 static octave_value_list |
2083
|
603 do_system (const string& cmd_str, bool return_output) |
|
604 { |
2086
|
605 octave_value_list retval; |
2083
|
606 |
|
607 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); |
|
608 |
|
609 add_unwind_protect (cleanup_iprocstream, cmd); |
|
610 |
|
611 int status = 127; |
|
612 |
|
613 if (cmd && *cmd) |
|
614 { |
|
615 ostrstream output_buf; |
2095
|
616 |
2083
|
617 char ch; |
|
618 while (cmd->get (ch)) |
2084
|
619 { |
2095
|
620 if (return_output) |
2084
|
621 output_buf.put (ch); |
2145
|
622 else |
2095
|
623 octave_stdout.put (ch); |
2084
|
624 } |
2083
|
625 |
|
626 status = cmd->close (); |
|
627 |
|
628 // The value in status is as returned by waitpid. If the |
|
629 // process exited normally, extract the actual exit status of |
|
630 // the command. Otherwise, return 127 as a failure code. |
|
631 |
|
632 if ((status & 0xff) == 0) |
|
633 status = (status & 0xff00) >> 8; |
|
634 |
|
635 if (return_output) |
|
636 { |
2145
|
637 output_buf << ends; |
|
638 |
2083
|
639 char *msg = output_buf.str (); |
|
640 |
|
641 retval(1) = (double) status; |
|
642 retval(0) = msg; |
|
643 |
|
644 delete [] msg; |
|
645 } |
|
646 } |
|
647 else |
|
648 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); |
|
649 |
|
650 run_unwind_protect (); |
|
651 |
|
652 return retval; |
|
653 } |
|
654 |
1957
|
655 DEFUN (system, args, nargout, |
2083
|
656 "system (STRING [, RETURN_OUTPUT] [, TYPE])\n\ |
|
657 \n\ |
|
658 Execute the shell command specified by STRING.\n\ |
|
659 \n\ |
|
660 If TYPE is \"async\", the process is started in the background and the\n\ |
|
661 pid of the child proces is returned immediately. Otherwise, the\n\ |
|
662 process is started, and Octave waits until it exits. If TYPE argument\n\ |
|
663 is omitted, a value of \"sync\" is assumed.\n\ |
|
664 \n\ |
|
665 If NARGIN == 2 (the actual value of RETURN_OUTPUT is irrelevant) and\n\ |
|
666 the subprocess is started synchronously, or if system() is called with\n\ |
|
667 NARGIN == 1 and NARGOUT > 0, the output from the command is returned.\n\ |
|
668 Otherwise, if the subprocess is executed synchronously, it's output is\n\ |
|
669 sent to Octave's standard output (possibly being passed through the\n\ |
|
670 pager).") |
1683
|
671 { |
2086
|
672 octave_value_list retval; |
1683
|
673 |
|
674 int nargin = args.length (); |
|
675 |
2083
|
676 if (nargin > 0 && nargin < 4) |
1683
|
677 { |
2083
|
678 bool return_output = (nargout > 0 || nargin > 1); |
|
679 |
|
680 string cmd_str = args(0).string_value (); |
|
681 |
|
682 enum exec_type { sync, async }; |
|
683 |
|
684 exec_type type = sync; |
|
685 |
|
686 if (! error_state) |
|
687 { |
|
688 if (nargin > 2) |
|
689 { |
|
690 string type_str = args(2).string_value (); |
1683
|
691 |
2083
|
692 if (! error_state) |
|
693 { |
|
694 if (type_str == "sync") |
|
695 type = sync; |
|
696 else if (type_str == "async") |
|
697 type = async; |
|
698 else |
|
699 error ("system: third arg must be \"sync\" or \"async\""); |
|
700 } |
|
701 else |
|
702 error ("system: third argument must be a string"); |
|
703 } |
|
704 } |
|
705 else |
|
706 error ("system: expecting string as first argument"); |
1683
|
707 |
2083
|
708 if (! error_state) |
|
709 { |
|
710 if (type == async) |
|
711 { |
|
712 pid_t pid = fork (); |
|
713 |
|
714 if (pid < 0) |
|
715 error ("system: fork failed -- can't create child process"); |
|
716 else if (pid == 0) |
|
717 { |
|
718 system (cmd_str.c_str ()); |
|
719 exit (0); |
|
720 retval(0) = 0.0; |
|
721 } |
|
722 else |
|
723 retval(0) = (double) pid; |
|
724 } |
|
725 else |
|
726 retval = do_system (cmd_str, return_output); |
|
727 } |
1683
|
728 } |
|
729 else |
2083
|
730 print_usage ("system"); |
1683
|
731 |
|
732 return retval; |
|
733 } |
|
734 |
|
735 DEFALIAS (shell_cmd, system); |
|
736 |
2077
|
737 static SLStack<string> octave_atexit_functions; |
|
738 |
|
739 void |
|
740 do_octave_atexit (void) |
|
741 { |
|
742 while (! octave_atexit_functions.empty ()) |
|
743 { |
2086
|
744 octave_value_list fcn = octave_atexit_functions.pop (); |
2077
|
745 |
|
746 feval (fcn, 0); |
|
747 } |
|
748 } |
|
749 |
|
750 DEFUN(atexit, args, , |
|
751 "atexit (NAME): register NAME as a function to call when Octave exits\n\ |
|
752 \n\ |
|
753 Functions are called with no arguments in the reverse of the order in |
|
754 which they were registered with atexit()") |
|
755 { |
2086
|
756 octave_value_list retval; |
2077
|
757 |
|
758 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
|
759 int nargin = args.length (); |
|
760 |
|
761 if (nargin == 1) |
|
762 { |
|
763 string arg = args(0).string_value (); |
|
764 |
|
765 if (! error_state) |
|
766 octave_atexit_functions.push (arg); |
|
767 else |
|
768 error ("atexit: argument must be a string"); |
|
769 } |
|
770 else |
|
771 print_usage ("atexit"); |
|
772 #else |
2078
|
773 gripe_not_supported ("atexit"); |
2077
|
774 #endif |
|
775 |
|
776 return retval; |
|
777 } |
|
778 |
1683
|
779 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
780 int debug_new_delete = 0; |
|
781 |
|
782 typedef void (*vfp)(void); |
|
783 extern vfp __new_handler; |
|
784 |
|
785 void * |
|
786 __builtin_new (size_t sz) |
|
787 { |
|
788 void *p; |
|
789 |
|
790 /* malloc (0) is unpredictable; avoid it. */ |
|
791 if (sz == 0) |
|
792 sz = 1; |
|
793 p = (void *) malloc (sz); |
|
794 while (p == 0) |
|
795 { |
|
796 (*__new_handler) (); |
|
797 p = (void *) malloc (sz); |
|
798 } |
|
799 |
|
800 if (debug_new_delete) |
|
801 cout << "__builtin_new: " << p << endl; |
|
802 |
|
803 return p; |
|
804 } |
|
805 |
|
806 void |
|
807 __builtin_delete (void *ptr) |
|
808 { |
|
809 if (debug_new_delete) |
|
810 cout << "__builtin_delete: " << ptr << endl; |
|
811 |
|
812 if (ptr) |
|
813 free (ptr); |
|
814 } |
|
815 #endif |
|
816 |
|
817 /* |
|
818 ;;; Local Variables: *** |
|
819 ;;; mode: C++ *** |
|
820 ;;; End: *** |
|
821 */ |