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