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_diary_file (); |
|
312 |
|
313 close_files (); |
|
314 |
|
315 cleanup_tmp_files (); |
|
316 |
|
317 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
318 cout << "\n"; |
|
319 |
|
320 if (retval == EOF) |
|
321 retval = 0; |
|
322 |
|
323 exit (retval); |
|
324 |
|
325 // This is bogus but should prevent g++ from giving a warning saying |
|
326 // that this volatile function does return. |
|
327 |
|
328 panic_impossible (); |
|
329 } |
|
330 |
1957
|
331 DEFUN_TEXT (casesen, args, , |
1683
|
332 "casesen [on|off]") |
|
333 { |
2086
|
334 octave_value_list retval; |
1683
|
335 |
1755
|
336 int argc = args.length () + 1; |
|
337 |
1965
|
338 string_vector argv = args.make_argv ("casesen"); |
1683
|
339 |
1755
|
340 if (error_state) |
|
341 return retval; |
|
342 |
|
343 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
344 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
345 else if (argc > 1 && argv[1] == "on") |
1683
|
346 ; // ok. |
|
347 else |
|
348 print_usage ("casesen"); |
|
349 |
|
350 return retval; |
|
351 } |
|
352 |
1957
|
353 DEFUN (computer, args, nargout, |
1683
|
354 "computer ():\n\ |
|
355 \n\ |
|
356 Have Octave ask the system, \"What kind of computer are you?\"") |
|
357 { |
2086
|
358 octave_value_list retval; |
1683
|
359 |
|
360 int nargin = args.length (); |
|
361 |
|
362 if (nargin != 0) |
|
363 warning ("computer: ignoring extra arguments"); |
|
364 |
|
365 ostrstream output_buf; |
|
366 |
|
367 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
368 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
369 else |
|
370 output_buf << TARGET_HOST_TYPE; |
|
371 |
|
372 if (nargout == 0) |
|
373 { |
|
374 output_buf << "\n" << ends; |
|
375 maybe_page_output (output_buf); |
|
376 } |
|
377 else |
|
378 { |
|
379 output_buf << ends; |
|
380 char *msg = output_buf.str (); |
|
381 retval = msg; |
|
382 delete [] msg; |
|
383 } |
|
384 |
|
385 return retval; |
|
386 } |
|
387 |
2068
|
388 DEFUN (quit, args, , |
|
389 "quit (STATUS): exit Octave gracefully, returning STATUS to the system.\n\ |
|
390 \n\ |
|
391 STATUS should be an integer value. If STATUS is missing, 0 is assumed.") |
1683
|
392 { |
2086
|
393 octave_value_list retval; |
2068
|
394 |
|
395 int exit_status = 0; |
|
396 |
|
397 quitting_gracefully = 1; |
|
398 |
1683
|
399 int nargin = args.length (); |
|
400 |
|
401 if (nargin > 0) |
2068
|
402 { |
|
403 // XXX FIXME XXX -- need a safe uniform way to do this. |
1683
|
404 |
2068
|
405 double tmp = args(0).double_value (); |
1683
|
406 |
2068
|
407 if (! error_state && ! xisnan (tmp)) |
|
408 exit_status = NINT (tmp); |
|
409 } |
|
410 |
|
411 clean_up_and_exit (exit_status); |
|
412 |
1683
|
413 return retval; |
|
414 } |
|
415 |
|
416 DEFALIAS (exit, quit); |
|
417 |
1957
|
418 DEFUN (warranty, , , |
1683
|
419 "warranty (): describe copying conditions") |
|
420 { |
2086
|
421 octave_value_list retval; |
1683
|
422 |
|
423 ostrstream output_buf; |
|
424 output_buf << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
425 This program is free software; you can redistribute it and/or modify\n\ |
|
426 it under the terms of the GNU General Public License as published by\n\ |
|
427 the Free Software Foundation; either version 2 of the License, or\n\ |
|
428 (at your option) any later version.\n\ |
|
429 \n\ |
|
430 This program is distributed in the hope that it will be useful,\n\ |
|
431 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
432 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
433 GNU General Public License for more details.\n\ |
|
434 \n\ |
|
435 You should have received a copy of the GNU General Public License\n\ |
|
436 along with this program. If not, write to the Free Software\n\ |
|
437 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
438 \n"; |
|
439 |
|
440 output_buf << ends; |
|
441 maybe_page_output (output_buf); |
|
442 |
|
443 return retval; |
|
444 } |
|
445 |
|
446 // XXX FIXME XXX -- this may not be the best place for these... |
|
447 |
2086
|
448 octave_value_list |
|
449 feval (const octave_value_list& args, int nargout) |
1683
|
450 { |
2086
|
451 octave_value_list retval; |
1683
|
452 |
|
453 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
|
454 if (fcn) |
|
455 { |
|
456 int tmp_nargin = args.length () - 1; |
2086
|
457 octave_value_list tmp_args; |
1683
|
458 tmp_args.resize (tmp_nargin); |
|
459 for (int i = 0; i < tmp_nargin; i++) |
|
460 tmp_args(i) = args(i+1); |
|
461 retval = fcn->eval (0, nargout, tmp_args); |
|
462 } |
|
463 |
|
464 return retval; |
|
465 } |
|
466 |
1957
|
467 DEFUN (feval, args, nargout, |
1683
|
468 "feval (NAME, ARGS, ...)\n\ |
|
469 \n\ |
|
470 evaluate NAME as a function, passing ARGS as its arguments") |
|
471 { |
2086
|
472 octave_value_list retval; |
1683
|
473 |
|
474 int nargin = args.length (); |
|
475 |
|
476 if (nargin > 0) |
|
477 retval = feval (args, nargout); |
|
478 else |
|
479 print_usage ("feval"); |
|
480 |
|
481 return retval; |
|
482 } |
|
483 |
2086
|
484 static octave_value_list |
1755
|
485 eval_string (const string& s, int print, int& parse_status, |
1683
|
486 int nargout) |
|
487 { |
|
488 begin_unwind_frame ("eval_string"); |
|
489 |
|
490 unwind_protect_int (get_input_from_eval_string); |
|
491 unwind_protect_int (input_from_command_line_file); |
|
492 unwind_protect_ptr (global_command); |
1755
|
493 unwind_protect_str (current_eval_string); |
1683
|
494 |
|
495 get_input_from_eval_string = 1; |
|
496 input_from_command_line_file = 0; |
1755
|
497 current_eval_string = s; |
1683
|
498 |
|
499 YY_BUFFER_STATE old_buf = current_buffer (); |
|
500 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
501 |
|
502 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
503 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
504 |
|
505 switch_to_buffer (new_buf); |
|
506 |
|
507 unwind_protect_ptr (curr_sym_tab); |
|
508 |
|
509 reset_parser (); |
|
510 |
|
511 parse_status = yyparse (); |
|
512 |
|
513 // Important to reset the idea of where input is coming from before |
|
514 // trying to eval the command we just parsed -- it might contain the |
|
515 // name of an function file that still needs to be parsed! |
|
516 |
|
517 tree_statement_list *command = global_command; |
|
518 |
|
519 run_unwind_frame ("eval_string"); |
|
520 |
2086
|
521 octave_value_list retval; |
1683
|
522 |
|
523 if (parse_status == 0 && command) |
|
524 { |
|
525 retval = command->eval (print, nargout); |
|
526 delete command; |
|
527 } |
|
528 |
|
529 return retval; |
|
530 } |
|
531 |
2086
|
532 octave_value |
1755
|
533 eval_string (const string& s, int print, int& parse_status) |
1683
|
534 { |
2086
|
535 octave_value retval; |
1683
|
536 |
2086
|
537 octave_value_list tmp = eval_string (s, print, parse_status, 1); |
1683
|
538 |
|
539 retval = tmp(0); |
|
540 |
|
541 return retval; |
|
542 } |
|
543 |
2086
|
544 static octave_value_list |
|
545 eval_string (const octave_value& arg, int& parse_status, int nargout) |
1683
|
546 { |
1755
|
547 string s = arg.string_value (); |
1683
|
548 |
|
549 if (error_state) |
|
550 { |
|
551 error ("eval: expecting string argument"); |
|
552 return -1.0; |
|
553 } |
|
554 |
|
555 // Yes Virginia, we always print here... |
|
556 |
1755
|
557 return eval_string (s, 1, parse_status, nargout); |
1683
|
558 } |
|
559 |
1957
|
560 DEFUN (eval, args, nargout, |
1683
|
561 "eval (TRY, CATCH)\n\ |
|
562 \n\ |
|
563 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
564 string CATCH.") |
|
565 { |
2086
|
566 octave_value_list retval; |
1683
|
567 |
|
568 int nargin = args.length (); |
|
569 |
|
570 if (nargin > 0) |
|
571 { |
|
572 begin_unwind_frame ("Feval"); |
|
573 |
|
574 if (nargin > 1) |
|
575 { |
|
576 unwind_protect_int (buffer_error_messages); |
|
577 buffer_error_messages = 1; |
|
578 } |
|
579 |
|
580 int parse_status = 0; |
|
581 |
|
582 retval = eval_string (args(0), parse_status, nargout); |
|
583 |
|
584 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
585 { |
|
586 error_state = 0; |
|
587 |
|
588 // Set up for letting the user print any messages from |
|
589 // errors that occurred in the first part of this eval(). |
|
590 |
|
591 buffer_error_messages = 0; |
|
592 bind_global_error_variable (); |
|
593 add_unwind_protect (clear_global_error_variable, 0); |
|
594 |
|
595 eval_string (args(1), parse_status, nargout); |
|
596 |
2086
|
597 retval = octave_value_list (); |
1683
|
598 } |
|
599 |
|
600 run_unwind_frame ("Feval"); |
|
601 } |
|
602 else |
|
603 print_usage ("eval"); |
|
604 |
|
605 return retval; |
|
606 } |
|
607 |
|
608 // Execute a shell command. |
|
609 |
1965
|
610 static void |
|
611 cleanup_iprocstream (void *p) |
|
612 { |
|
613 delete (iprocstream *) p; |
|
614 } |
|
615 |
2086
|
616 static octave_value_list |
2083
|
617 do_system (const string& cmd_str, bool return_output) |
|
618 { |
2086
|
619 octave_value_list retval; |
2083
|
620 |
|
621 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); |
|
622 |
|
623 add_unwind_protect (cleanup_iprocstream, cmd); |
|
624 |
|
625 int status = 127; |
|
626 |
|
627 if (cmd && *cmd) |
|
628 { |
|
629 ostrstream output_buf; |
|
630 |
|
631 char ch; |
|
632 while (cmd->get (ch)) |
2084
|
633 { |
|
634 if (return_output || user_pref.page_screen_output) |
|
635 output_buf.put (ch); |
|
636 else |
|
637 cout.put (ch); |
|
638 } |
2083
|
639 |
|
640 output_buf << ends; |
|
641 |
|
642 status = cmd->close (); |
|
643 |
|
644 // The value in status is as returned by waitpid. If the |
|
645 // process exited normally, extract the actual exit status of |
|
646 // the command. Otherwise, return 127 as a failure code. |
|
647 |
|
648 if ((status & 0xff) == 0) |
|
649 status = (status & 0xff00) >> 8; |
|
650 |
|
651 if (return_output) |
|
652 { |
|
653 char *msg = output_buf.str (); |
|
654 |
|
655 retval(1) = (double) status; |
|
656 retval(0) = msg; |
|
657 |
|
658 delete [] msg; |
|
659 } |
|
660 else |
|
661 maybe_page_output (output_buf); |
|
662 } |
|
663 else |
|
664 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); |
|
665 |
|
666 run_unwind_protect (); |
|
667 |
|
668 return retval; |
|
669 } |
|
670 |
1957
|
671 DEFUN (system, args, nargout, |
2083
|
672 "system (STRING [, RETURN_OUTPUT] [, TYPE])\n\ |
|
673 \n\ |
|
674 Execute the shell command specified by STRING.\n\ |
|
675 \n\ |
|
676 If TYPE is \"async\", the process is started in the background and the\n\ |
|
677 pid of the child proces is returned immediately. Otherwise, the\n\ |
|
678 process is started, and Octave waits until it exits. If TYPE argument\n\ |
|
679 is omitted, a value of \"sync\" is assumed.\n\ |
|
680 \n\ |
|
681 If NARGIN == 2 (the actual value of RETURN_OUTPUT is irrelevant) and\n\ |
|
682 the subprocess is started synchronously, or if system() is called with\n\ |
|
683 NARGIN == 1 and NARGOUT > 0, the output from the command is returned.\n\ |
|
684 Otherwise, if the subprocess is executed synchronously, it's output is\n\ |
|
685 sent to Octave's standard output (possibly being passed through the\n\ |
|
686 pager).") |
1683
|
687 { |
2086
|
688 octave_value_list retval; |
1683
|
689 |
|
690 int nargin = args.length (); |
|
691 |
2083
|
692 if (nargin > 0 && nargin < 4) |
1683
|
693 { |
2083
|
694 bool return_output = (nargout > 0 || nargin > 1); |
|
695 |
|
696 string cmd_str = args(0).string_value (); |
|
697 |
|
698 enum exec_type { sync, async }; |
|
699 |
|
700 exec_type type = sync; |
|
701 |
|
702 if (! error_state) |
|
703 { |
|
704 if (nargin > 2) |
|
705 { |
|
706 string type_str = args(2).string_value (); |
1683
|
707 |
2083
|
708 if (! error_state) |
|
709 { |
|
710 if (type_str == "sync") |
|
711 type = sync; |
|
712 else if (type_str == "async") |
|
713 type = async; |
|
714 else |
|
715 error ("system: third arg must be \"sync\" or \"async\""); |
|
716 } |
|
717 else |
|
718 error ("system: third argument must be a string"); |
|
719 } |
|
720 } |
|
721 else |
|
722 error ("system: expecting string as first argument"); |
1683
|
723 |
2083
|
724 if (! error_state) |
|
725 { |
|
726 if (type == async) |
|
727 { |
|
728 pid_t pid = fork (); |
|
729 |
|
730 if (pid < 0) |
|
731 error ("system: fork failed -- can't create child process"); |
|
732 else if (pid == 0) |
|
733 { |
|
734 system (cmd_str.c_str ()); |
|
735 exit (0); |
|
736 retval(0) = 0.0; |
|
737 } |
|
738 else |
|
739 retval(0) = (double) pid; |
|
740 } |
|
741 else |
|
742 retval = do_system (cmd_str, return_output); |
|
743 } |
1683
|
744 } |
|
745 else |
2083
|
746 print_usage ("system"); |
1683
|
747 |
|
748 return retval; |
|
749 } |
|
750 |
|
751 DEFALIAS (shell_cmd, system); |
|
752 |
2077
|
753 static SLStack<string> octave_atexit_functions; |
|
754 |
|
755 void |
|
756 do_octave_atexit (void) |
|
757 { |
|
758 while (! octave_atexit_functions.empty ()) |
|
759 { |
2086
|
760 octave_value_list fcn = octave_atexit_functions.pop (); |
2077
|
761 |
|
762 feval (fcn, 0); |
|
763 } |
|
764 } |
|
765 |
|
766 DEFUN(atexit, args, , |
|
767 "atexit (NAME): register NAME as a function to call when Octave exits\n\ |
|
768 \n\ |
|
769 Functions are called with no arguments in the reverse of the order in |
|
770 which they were registered with atexit()") |
|
771 { |
2086
|
772 octave_value_list retval; |
2077
|
773 |
|
774 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
|
775 int nargin = args.length (); |
|
776 |
|
777 if (nargin == 1) |
|
778 { |
|
779 string arg = args(0).string_value (); |
|
780 |
|
781 if (! error_state) |
|
782 octave_atexit_functions.push (arg); |
|
783 else |
|
784 error ("atexit: argument must be a string"); |
|
785 } |
|
786 else |
|
787 print_usage ("atexit"); |
|
788 #else |
2078
|
789 gripe_not_supported ("atexit"); |
2077
|
790 #endif |
|
791 |
|
792 return retval; |
|
793 } |
|
794 |
1683
|
795 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
796 int debug_new_delete = 0; |
|
797 |
|
798 typedef void (*vfp)(void); |
|
799 extern vfp __new_handler; |
|
800 |
|
801 void * |
|
802 __builtin_new (size_t sz) |
|
803 { |
|
804 void *p; |
|
805 |
|
806 /* malloc (0) is unpredictable; avoid it. */ |
|
807 if (sz == 0) |
|
808 sz = 1; |
|
809 p = (void *) malloc (sz); |
|
810 while (p == 0) |
|
811 { |
|
812 (*__new_handler) (); |
|
813 p = (void *) malloc (sz); |
|
814 } |
|
815 |
|
816 if (debug_new_delete) |
|
817 cout << "__builtin_new: " << p << endl; |
|
818 |
|
819 return p; |
|
820 } |
|
821 |
|
822 void |
|
823 __builtin_delete (void *ptr) |
|
824 { |
|
825 if (debug_new_delete) |
|
826 cout << "__builtin_delete: " << ptr << endl; |
|
827 |
|
828 if (ptr) |
|
829 free (ptr); |
|
830 } |
|
831 #endif |
|
832 |
|
833 /* |
|
834 ;;; Local Variables: *** |
|
835 ;;; mode: C++ *** |
|
836 ;;; End: *** |
|
837 */ |