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