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