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