Mercurial > octave
annotate src/toplev.cc @ 7719:87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Wed, 16 Apr 2008 22:08:15 -0400 |
parents | 6070c3bd69c4 |
children | bb614b3883a9 |
rev | line source |
---|---|
1683 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, |
4 2004, 2005, 2006, 2007 John W. Eaton | |
1683 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
1683 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
1683 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
28 #include <cassert> | |
4489 | 29 #include <cerrno> |
1683 | 30 #include <cstdlib> |
31 #include <cstring> | |
4221 | 32 #include <new> |
1683 | 33 |
3503 | 34 #include <fstream> |
35 #include <iostream> | |
5765 | 36 #include <sstream> |
1728 | 37 #include <string> |
38 | |
1683 | 39 #ifdef HAVE_UNISTD_H |
2442 | 40 #ifdef HAVE_SYS_TYPES_H |
1683 | 41 #include <sys/types.h> |
2442 | 42 #endif |
1683 | 43 #include <unistd.h> |
44 #endif | |
45 | |
2926 | 46 #include "cmd-edit.h" |
47 #include "file-ops.h" | |
1683 | 48 #include "lo-error.h" |
2370 | 49 #include "lo-mappers.h" |
3020 | 50 #include "oct-env.h" |
4153 | 51 #include "quit.h" |
1755 | 52 #include "str-vec.h" |
1683 | 53 |
2492 | 54 #include <defaults.h> |
1683 | 55 #include "defun.h" |
56 #include "error.h" | |
57 #include "file-io.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" |
2862 | 63 #include "oct-obj.h" |
1683 | 64 #include "pager.h" |
65 #include "parse.h" | |
66 #include "pathsearch.h" | |
67 #include "procstream.h" | |
2370 | 68 #include "ov.h" |
2985 | 69 #include "pt-jump.h" |
2982 | 70 #include "pt-stmt.h" |
1683 | 71 #include "sighandlers.h" |
72 #include "sysdep.h" | |
2693 | 73 #include "syswait.h" |
1750 | 74 #include "toplev.h" |
1683 | 75 #include "unwind-prot.h" |
76 #include "utils.h" | |
77 #include "variables.h" | |
2492 | 78 #include <version.h> |
1683 | 79 |
3020 | 80 // TRUE means we are exiting via the builtin exit or quit functions. |
81 static bool quitting_gracefully = false; | |
1683 | 82 |
4217 | 83 // TRUE means we are ready to interpret commands, but not everything |
84 // is ready for interactive use. | |
85 bool octave_interpreter_ready = false; | |
86 | |
4172 | 87 // TRUE means we've processed all the init code and we are good to go. |
88 bool octave_initialized = false; | |
89 | |
1683 | 90 // Current command to execute. |
91 tree_statement_list *global_command = 0; | |
92 | |
4238 | 93 // Pointer to parent function that is currently being evaluated. |
4748 | 94 octave_function *curr_parent_function = 0; |
4238 | 95 |
5743 | 96 octave_call_stack *octave_call_stack::instance = 0; |
97 | |
5744 | 98 octave_user_script * |
99 octave_call_stack::do_caller_user_script (void) | |
100 { | |
101 octave_user_script *retval = 0; | |
102 | |
103 for (iterator p = cs.begin (); p != cs.end (); p++) | |
104 { | |
105 octave_function *f = *p; | |
106 | |
107 if (f && f->is_user_script ()) | |
108 { | |
109 retval = dynamic_cast<octave_user_script *> (f); | |
110 break; | |
111 } | |
112 } | |
113 | |
114 return retval; | |
115 } | |
116 | |
5743 | 117 octave_user_function * |
5744 | 118 octave_call_stack::do_caller_user_function (void) |
5743 | 119 { |
120 octave_user_function *retval = 0; | |
121 | |
122 for (iterator p = cs.begin (); p != cs.end (); p++) | |
123 { | |
124 octave_function *f = *p; | |
125 | |
126 if (f && f->is_user_function ()) | |
127 { | |
128 retval = dynamic_cast<octave_user_function *> (f); | |
129 break; | |
130 } | |
131 } | |
132 | |
133 return retval; | |
134 } | |
135 | |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
136 octave_user_code * |
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
137 octave_call_stack::do_caller_user_code (void) |
5744 | 138 { |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
139 octave_user_code *retval = 0; |
5744 | 140 |
141 for (iterator p = cs.begin (); p != cs.end (); p++) | |
142 { | |
143 octave_function *f = *p; | |
144 | |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
145 if (f && f->is_user_code ()) |
5744 | 146 { |
7719
87eda1f8faaa
octave_user_code: new base class for octave_user_script and octave_user_function
John W. Eaton <jwe@octave.org>
parents:
7552
diff
changeset
|
147 retval = dynamic_cast<octave_user_code *> (f); |
5744 | 148 break; |
149 } | |
150 } | |
151 | |
152 return retval; | |
153 } | |
154 | |
155 void | |
156 octave_call_stack::unwind_pop_script (void *) | |
157 { | |
158 octave_function *f = top (); | |
159 pop (); | |
160 assert (f && f->is_user_script ()); | |
161 delete f; | |
162 } | |
163 | |
7202 | 164 void |
4180 | 165 recover_from_exception (void) |
166 { | |
167 can_interrupt = true; | |
4182 | 168 octave_interrupt_immediately = 0; |
4180 | 169 octave_interrupt_state = 0; |
5142 | 170 octave_signal_caught = 0; |
7481
78f3811155f7
use exceptions in liboctave error handler
John W. Eaton <jwe@octave.org>
parents:
7409
diff
changeset
|
171 octave_exception_state = octave_no_exception; |
4180 | 172 octave_restore_signal_mask (); |
173 octave_catch_interrupts (); | |
174 } | |
175 | |
1907 | 176 int |
5189 | 177 main_loop (void) |
1907 | 178 { |
2016 | 179 octave_save_signal_mask (); |
180 | |
3020 | 181 can_interrupt = true; |
1907 | 182 |
5142 | 183 octave_signal_hook = octave_signal_handler; |
4429 | 184 octave_interrupt_hook = unwind_protect::run_all; |
185 octave_bad_alloc_hook = unwind_protect::run_all; | |
186 | |
2554 | 187 octave_catch_interrupts (); |
1907 | 188 |
4172 | 189 octave_initialized = true; |
190 | |
1907 | 191 // The big loop. |
192 | |
4153 | 193 int retval = 0; |
1907 | 194 do |
195 { | |
4180 | 196 try |
1907 | 197 { |
7336 | 198 symbol_table::reset_scope (); |
2620 | 199 |
4318 | 200 reset_error_handler (); |
201 | |
4153 | 202 reset_parser (); |
2620 | 203 |
4753 | 204 // This is the same as yyparse in parse.y. |
205 retval = octave_parse (); | |
2620 | 206 |
4153 | 207 if (retval == 0) |
208 { | |
209 if (global_command) | |
3804 | 210 { |
4153 | 211 global_command->eval (); |
3883 | 212 |
4153 | 213 delete global_command; |
3883 | 214 |
4153 | 215 global_command = 0; |
3883 | 216 |
4171 | 217 OCTAVE_QUIT; |
218 | |
3883 | 219 if (! (interactive || forced_interactive)) |
220 { | |
4207 | 221 bool quit = (tree_return_command::returning |
222 || tree_break_command::breaking); | |
4153 | 223 |
4207 | 224 if (tree_return_command::returning) |
225 tree_return_command::returning = 0; | |
4153 | 226 |
4207 | 227 if (tree_break_command::breaking) |
228 tree_break_command::breaking--; | |
4153 | 229 |
230 if (quit) | |
231 break; | |
232 } | |
233 | |
234 if (error_state) | |
235 { | |
236 if (! (interactive || forced_interactive)) | |
237 { | |
238 // We should exit with a non-zero status. | |
239 retval = 1; | |
240 break; | |
241 } | |
242 } | |
243 else | |
244 { | |
245 if (octave_completion_matches_called) | |
246 octave_completion_matches_called = false; | |
247 else | |
248 command_editor::increment_current_command_number (); | |
3883 | 249 } |
250 } | |
4153 | 251 else if (parser_end_of_input) |
252 break; | |
2620 | 253 } |
4153 | 254 } |
4182 | 255 catch (octave_interrupt_exception) |
4153 | 256 { |
4180 | 257 recover_from_exception (); |
5629 | 258 octave_stdout << "\n"; |
4180 | 259 } |
4181 | 260 catch (std::bad_alloc) |
4180 | 261 { |
262 recover_from_exception (); | |
263 std::cerr | |
6680 | 264 << "error: memory exhausted or requested size too large for range of Octave's index type -- trying to return to prompt" |
265 << std::endl; | |
1907 | 266 } |
267 } | |
268 while (retval == 0); | |
269 | |
270 return retval; | |
271 } | |
272 | |
1683 | 273 // Fix up things before exiting. |
274 | |
275 void | |
3162 | 276 clean_up_and_exit (int retval) |
277 { | |
3216 | 278 do_octave_atexit (); |
1683 | 279 |
5451 | 280 sysdep_cleanup (); |
281 | |
3162 | 282 exit (retval == EOF ? 0 : retval); |
1683 | 283 } |
284 | |
3180 | 285 DEFUN (quit, args, nargout, |
3332 | 286 "-*- texinfo -*-\n\ |
287 @deftypefn {Built-in Function} {} exit (@var{status})\n\ | |
288 @deftypefnx {Built-in Function} {} quit (@var{status})\n\ | |
289 Exit the current Octave session. If the optional integer value\n\ | |
290 @var{status} is supplied, pass that value to the operating system as the\n\ | |
6615 | 291 Octave's exit status. The default value is zero.\n\ |
3333 | 292 @end deftypefn") |
1683 | 293 { |
2086 | 294 octave_value_list retval; |
2068 | 295 |
3180 | 296 if (nargout == 0) |
2068 | 297 { |
3180 | 298 int exit_status = 0; |
299 | |
300 quitting_gracefully = true; | |
1683 | 301 |
3180 | 302 if (args.length () > 0) |
303 { | |
3202 | 304 int tmp = args(0).nint_value (); |
1683 | 305 |
3202 | 306 if (! error_state) |
307 exit_status = tmp; | |
3180 | 308 } |
309 | |
310 clean_up_and_exit (exit_status); | |
2068 | 311 } |
3180 | 312 else |
313 error ("quit: invalid number of output arguments"); | |
2068 | 314 |
1683 | 315 return retval; |
316 } | |
317 | |
318 DEFALIAS (exit, quit); | |
319 | |
1957 | 320 DEFUN (warranty, , , |
3446 | 321 "-*- texinfo -*-\n\ |
322 @deftypefn {Built-in Function} {} warranty ()\n\ | |
323 Describe the conditions for copying and distributing Octave.\n\ | |
324 @end deftypefn") | |
1683 | 325 { |
2086 | 326 octave_value_list retval; |
1683 | 327 |
3922 | 328 octave_stdout << "\n" \ |
329 OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\ | |
330 \n\ | |
1683 | 331 This program is free software; you can redistribute it and/or modify\n\ |
332 it under the terms of the GNU General Public License as published by\n\ | |
7016 | 333 the Free Software Foundation; either version 3 of the License, or\n\ |
1683 | 334 (at your option) any later version.\n\ |
335 \n\ | |
336 This program is distributed in the hope that it will be useful,\n\ | |
337 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ | |
338 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ | |
339 GNU General Public License for more details.\n\ | |
340 \n\ | |
341 You should have received a copy of the GNU General Public License\n\ | |
7016 | 342 along with this program. If not, see <http://www.gnu.org/licenses/>.\n\ |
1683 | 343 \n"; |
344 | |
345 return retval; | |
346 } | |
347 | |
348 // Execute a shell command. | |
349 | |
1965 | 350 static void |
351 cleanup_iprocstream (void *p) | |
352 { | |
3060 | 353 iprocstream *cmd = static_cast<iprocstream *> (p); |
354 | |
355 octave_child_list::remove (cmd->pid ()); | |
356 | |
357 delete cmd; | |
1965 | 358 } |
359 | |
6316 | 360 static int |
361 wait_for_input (int fid) | |
362 { | |
363 int retval = -1; | |
364 | |
365 #if defined (HAVE_SELECT) | |
366 if (fid >= 0) | |
367 { | |
368 fd_set set; | |
369 | |
370 FD_ZERO (&set); | |
371 FD_SET (fid, &set); | |
372 | |
373 retval = select (FD_SETSIZE, &set, 0, 0, 0); | |
374 } | |
375 #else | |
376 retval = 1; | |
377 #endif | |
378 | |
379 return retval; | |
380 } | |
381 | |
2086 | 382 static octave_value_list |
3523 | 383 run_command_and_return_output (const std::string& cmd_str) |
2083 | 384 { |
2086 | 385 octave_value_list retval; |
2083 | 386 |
387 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); | |
388 | |
3060 | 389 if (cmd) |
2083 | 390 { |
3060 | 391 unwind_protect::add (cleanup_iprocstream, cmd); |
392 | |
393 if (*cmd) | |
394 { | |
6316 | 395 int fid = cmd->file_number (); |
2095 | 396 |
6316 | 397 std::ostringstream output_buf; |
4494 | 398 |
3060 | 399 char ch; |
3147 | 400 |
4489 | 401 for (;;) |
3147 | 402 { |
4489 | 403 if (cmd->get (ch)) |
404 output_buf.put (ch); | |
405 else | |
406 { | |
407 if (! cmd->eof () && errno == EAGAIN) | |
408 { | |
409 cmd->clear (); | |
3147 | 410 |
6316 | 411 if (wait_for_input (fid) != 1) |
412 break; | |
4489 | 413 } |
414 else | |
415 break; | |
416 } | |
3147 | 417 } |
418 | |
3252 | 419 int cmd_status = cmd->close (); |
2083 | 420 |
3060 | 421 if (WIFEXITED (cmd_status)) |
422 cmd_status = WEXITSTATUS (cmd_status); | |
423 else | |
424 cmd_status = 127; | |
2083 | 425 |
5659 | 426 retval(0) = (double) cmd_status; |
5765 | 427 retval(1) = output_buf.str (); |
3060 | 428 } |
429 | |
430 unwind_protect::run (); | |
2083 | 431 } |
432 else | |
433 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); | |
434 | |
435 return retval; | |
436 } | |
437 | |
5285 | 438 enum system_exec_type { et_sync, et_async }; |
439 | |
1957 | 440 DEFUN (system, args, nargout, |
3301 | 441 "-*- texinfo -*-\n\ |
442 @deftypefn {Built-in Function} {} system (@var{string}, @var{return_output}, @var{type})\n\ | |
443 Execute a shell command specified by @var{string}. The second\n\ | |
444 argument is optional. If @var{type} is @code{\"async\"}, the process\n\ | |
445 is started in the background and the process id of the child process\n\ | |
446 is returned immediately. Otherwise, the process is started, and\n\ | |
447 Octave waits until it exits. If @var{type} argument is omitted, a\n\ | |
448 value of @code{\"sync\"} is assumed.\n\ | |
2083 | 449 \n\ |
3301 | 450 If two input arguments are given (the actual value of\n\ |
451 @var{return_output} is irrelevant) and the subprocess is started\n\ | |
452 synchronously, or if @var{system} is called with one input argument and\n\ | |
453 one or more output arguments, the output from the command is returned.\n\ | |
5097 | 454 Otherwise, if the subprocess is executed synchronously, its output is\n\ |
2321 | 455 sent to the standard output. To send the output of a command executed\n\ |
3301 | 456 with @var{system} through the pager, use a command like\n\ |
2321 | 457 \n\ |
3301 | 458 @example\n\ |
459 disp (system (cmd, 1));\n\ | |
460 @end example\n\ | |
2321 | 461 \n\ |
3301 | 462 @noindent\n\ |
2321 | 463 or\n\ |
464 \n\ | |
3301 | 465 @example\n\ |
466 printf (\"%s\n\", system (cmd, 1));\n\ | |
467 @end example\n\ | |
468 \n\ | |
5659 | 469 The @code{system} function can return two values. The first is the\n\ |
470 exit status of the command and the second is any output from the\n\ | |
471 command that was written to the standard output stream. For example,\n\ | |
3301 | 472 \n\ |
473 @example\n\ | |
5717 | 474 [status, output] = system (\"echo foo; exit 2\");\n\ |
3301 | 475 @end example\n\ |
476 \n\ | |
477 @noindent\n\ | |
478 will set the variable @code{output} to the string @samp{foo}, and the\n\ | |
479 variable @code{status} to the integer @samp{2}.\n\ | |
480 @end deftypefn") | |
1683 | 481 { |
2086 | 482 octave_value_list retval; |
1683 | 483 |
3834 | 484 unwind_protect::begin_frame ("Fsystem"); |
485 | |
1683 | 486 int nargin = args.length (); |
487 | |
2083 | 488 if (nargin > 0 && nargin < 4) |
1683 | 489 { |
5672 | 490 bool return_output = (nargout > 1 || nargin > 1); |
2083 | 491 |
3523 | 492 std::string cmd_str = args(0).string_value (); |
2083 | 493 |
5285 | 494 system_exec_type type = et_sync; |
2083 | 495 |
496 if (! error_state) | |
497 { | |
498 if (nargin > 2) | |
499 { | |
3523 | 500 std::string type_str = args(2).string_value (); |
1683 | 501 |
2083 | 502 if (! error_state) |
503 { | |
504 if (type_str == "sync") | |
5285 | 505 type = et_sync; |
2083 | 506 else if (type_str == "async") |
5285 | 507 type = et_async; |
2083 | 508 else |
509 error ("system: third arg must be \"sync\" or \"async\""); | |
510 } | |
511 else | |
512 error ("system: third argument must be a string"); | |
513 } | |
514 } | |
515 else | |
3523 | 516 error ("system: expecting std::string as first argument"); |
1683 | 517 |
2083 | 518 if (! error_state) |
519 { | |
7104 | 520 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
521 // Work around weird double-quote handling on Windows systems. | |
522 if (type == et_sync) | |
523 cmd_str = "\"" + cmd_str + "\""; | |
524 #endif | |
525 | |
5285 | 526 if (type == et_async) |
2083 | 527 { |
6222 | 528 // FIXME -- maybe this should go in sysdep.cc? |
4086 | 529 #ifdef HAVE_FORK |
2083 | 530 pid_t pid = fork (); |
531 | |
532 if (pid < 0) | |
533 error ("system: fork failed -- can't create child process"); | |
534 else if (pid == 0) | |
535 { | |
5775 | 536 // FIXME -- should probably replace this |
3273 | 537 // call with something portable. |
538 | |
5510 | 539 execl ("/bin/sh", "sh", "-c", cmd_str.c_str (), |
540 static_cast<void *> (0)); | |
3273 | 541 |
542 panic_impossible (); | |
2083 | 543 } |
544 else | |
4254 | 545 retval(0) = pid; |
6222 | 546 #elif defined (__WIN32__) |
547 STARTUPINFO si; | |
548 PROCESS_INFORMATION pi; | |
549 ZeroMemory (&si, sizeof (si)); | |
550 ZeroMemory (&pi, sizeof (pi)); | |
551 OCTAVE_LOCAL_BUFFER (char, xcmd_str, cmd_str.length()+1); | |
6676 | 552 strcpy (xcmd_str, cmd_str.c_str ()); |
6222 | 553 |
554 if (! CreateProcess (0, xcmd_str, 0, 0, FALSE, 0, 0, 0, &si, &pi)) | |
555 error ("system: CreateProcess failed -- can't create child process"); | |
556 else | |
557 { | |
558 retval(0) = pi.dwProcessId; | |
559 CloseHandle (pi.hProcess); | |
560 CloseHandle (pi.hThread); | |
561 } | |
4086 | 562 #else |
563 error ("asynchronous system calls are not supported"); | |
564 #endif | |
2083 | 565 } |
2321 | 566 else if (return_output) |
567 retval = run_command_and_return_output (cmd_str); | |
2083 | 568 else |
2321 | 569 { |
570 int status = system (cmd_str.c_str ()); | |
571 | |
572 // The value in status is as returned by waitpid. If | |
573 // the process exited normally, extract the actual exit | |
574 // status of the command. Otherwise, return 127 as a | |
575 // failure code. | |
576 | |
2693 | 577 if (WIFEXITED (status)) |
578 status = WEXITSTATUS (status); | |
2321 | 579 |
4233 | 580 retval(0) = status; |
2321 | 581 } |
2083 | 582 } |
1683 | 583 } |
584 else | |
5823 | 585 print_usage (); |
1683 | 586 |
3834 | 587 unwind_protect::run_frame ("Fsystem"); |
588 | |
1683 | 589 return retval; |
590 } | |
591 | |
592 DEFALIAS (shell_cmd, system); | |
593 | |
5775 | 594 // FIXME -- this should really be static, but that causes |
2614 | 595 // problems on some systems. |
6680 | 596 std::list<std::string> octave_atexit_functions; |
2077 | 597 |
598 void | |
599 do_octave_atexit (void) | |
600 { | |
3216 | 601 static bool deja_vu = false; |
602 | |
2077 | 603 while (! octave_atexit_functions.empty ()) |
604 { | |
6680 | 605 std::string fcn = octave_atexit_functions.front (); |
4214 | 606 |
6680 | 607 octave_atexit_functions.pop_front (); |
2077 | 608 |
5237 | 609 reset_error_handler (); |
610 | |
4233 | 611 feval (fcn, octave_value_list (), 0); |
3215 | 612 |
613 flush_octave_stdout (); | |
2077 | 614 } |
3216 | 615 |
616 if (! deja_vu) | |
617 { | |
618 deja_vu = true; | |
619 | |
6068 | 620 // Do this explicitly so that destructors for mex file objects |
621 // are called, so that functions registered with mexAtExit are | |
622 // called. | |
6072 | 623 clear_mex_functions (); |
6068 | 624 |
3216 | 625 command_editor::restore_terminal_state (); |
626 | |
5775 | 627 // FIXME -- is this needed? Can it cause any trouble? |
3216 | 628 raw_mode (0); |
629 | |
5305 | 630 octave_history_write_timestamp (); |
631 | |
3216 | 632 command_history::clean_up_and_save (); |
633 | |
634 close_files (); | |
635 | |
636 cleanup_tmp_files (); | |
637 | |
638 flush_octave_stdout (); | |
639 | |
5305 | 640 if (! quitting_gracefully && (interactive || forced_interactive)) |
5629 | 641 { |
642 octave_stdout << "\n"; | |
643 | |
644 // Yes, we want this to be separate from the call to | |
645 // flush_octave_stdout above. | |
646 | |
647 flush_octave_stdout (); | |
648 } | |
3216 | 649 } |
2077 | 650 } |
651 | |
7409 | 652 void |
653 octave_add_atexit_function (const std::string& fname) | |
654 { | |
655 octave_atexit_functions.push_front (fname); | |
656 } | |
657 | |
658 bool | |
659 octave_remove_atexit_function (const std::string& fname) | |
660 { | |
661 bool found = false; | |
662 | |
663 for (std::list<std::string>::iterator p = octave_atexit_functions.begin (); | |
664 p != octave_atexit_functions.end (); p++) | |
665 { | |
666 if (*p == fname) | |
667 { | |
668 octave_atexit_functions.erase (p); | |
669 found = true; | |
670 break; | |
671 } | |
672 } | |
673 | |
674 return found; | |
675 } | |
676 | |
677 | |
6680 | 678 DEFUN (atexit, args, nargout, |
3332 | 679 "-*- texinfo -*-\n\ |
680 @deftypefn {Built-in Function} {} atexit (@var{fcn})\n\ | |
681 Register a function to be called when Octave exits. For example,\n\ | |
2077 | 682 \n\ |
3332 | 683 @example\n\ |
684 @group\n\ | |
6620 | 685 function bye_bye ()\n\ |
686 disp (\"Bye bye\");\n\ | |
3332 | 687 endfunction\n\ |
6620 | 688 atexit (\"bye_bye\");\n\ |
3332 | 689 @end group\n\ |
690 @end example\n\ | |
691 \n\ | |
692 @noindent\n\ | |
6620 | 693 will print the message \"Bye bye\" when Octave exits.\n\ |
6680 | 694 \n\ |
695 @deftypefnx {Built-in Function} {} atexit (@var{fcn}, @var{flag})\n\ | |
696 Register or unregister a function to be called when Octave exits,\n\ | |
697 depending on @var{flag}. If @var{flag} is true, the function is\n\ | |
698 registered, if @var{flag} is false, it is unregistered. For example,\n\ | |
699 after registering the function @code{bye_bye} as above,\n\ | |
700 \n\ | |
701 @example\n\ | |
702 atexit (\"bye_bye\", false);\n\ | |
703 @end example\n\ | |
704 \n\ | |
705 @noindent\n\ | |
706 will remove the function from the list and Octave will not call\n\ | |
707 the function @code{bye_by} when it exits.\n\ | |
708 \n\ | |
7001 | 709 Note that @code{atexit} only removes the first occurrence of a function\n\ |
6680 | 710 from the list, so if a function was placed in the list multiple\n\ |
711 times with @code{atexit}, it must also be removed from the list\n\ | |
712 multiple times.\n\ | |
3333 | 713 @end deftypefn") |
2077 | 714 { |
2086 | 715 octave_value_list retval; |
2077 | 716 |
717 int nargin = args.length (); | |
718 | |
6680 | 719 if (nargin == 1 || nargin == 2) |
2077 | 720 { |
3523 | 721 std::string arg = args(0).string_value (); |
2077 | 722 |
723 if (! error_state) | |
6680 | 724 { |
725 bool add_mode = true; | |
726 | |
727 if (nargin == 2) | |
728 { | |
729 add_mode = args(1).bool_value (); | |
730 | |
731 if (error_state) | |
732 error ("atexit: second argument must be a logical value"); | |
733 } | |
734 | |
735 if (! error_state) | |
736 { | |
737 if (add_mode) | |
7409 | 738 octave_add_atexit_function (arg); |
6680 | 739 else |
740 { | |
7409 | 741 bool found = octave_remove_atexit_function (arg); |
6680 | 742 |
743 if (nargout > 0) | |
744 retval(0) = found; | |
745 } | |
746 } | |
747 } | |
2077 | 748 else |
6680 | 749 error ("atexit: argument must be a string"); |
2077 | 750 } |
751 else | |
5823 | 752 print_usage (); |
2077 | 753 |
754 return retval; | |
755 } | |
756 | |
2689 | 757 DEFUN (octave_config_info, args, , |
3301 | 758 "-*- texinfo -*-\n\ |
759 @deftypefn {Built-in Function} {} octave_config_info (@var{option})\n\ | |
760 Return a structure containing configuration and installation\n\ | |
761 information for Octave.\n\ | |
2689 | 762 \n\ |
3301 | 763 if @var{option} is a string, return the configuration information for the\n\ |
2689 | 764 specified option.\n\ |
765 \n\ | |
3301 | 766 @end deftypefn") |
2162 | 767 { |
2689 | 768 octave_value retval; |
769 | |
4128 | 770 #if defined (ENABLE_DYNAMIC_LINKING) |
2689 | 771 bool octave_supports_dynamic_linking = true; |
772 #else | |
773 bool octave_supports_dynamic_linking = false; | |
774 #endif | |
775 | |
4357 | 776 static bool initialized = false; |
777 static Octave_map m; | |
2162 | 778 |
6274 | 779 struct conf_info_struct |
780 { | |
781 bool subst_home; | |
782 const char *key; | |
783 const char *val; | |
784 }; | |
785 | |
786 static const conf_info_struct conf_info[] = | |
4357 | 787 { |
6274 | 788 { false, "ALL_CFLAGS", OCTAVE_CONF_ALL_CFLAGS }, |
789 { false, "ALL_CXXFLAGS", OCTAVE_CONF_ALL_CXXFLAGS }, | |
790 { false, "ALL_FFLAGS", OCTAVE_CONF_ALL_FFLAGS }, | |
791 { false, "ALL_LDFLAGS", OCTAVE_CONF_ALL_LDFLAGS }, | |
792 { false, "AR", OCTAVE_CONF_AR }, | |
793 { false, "ARFLAGS", OCTAVE_CONF_ARFLAGS }, | |
794 { false, "BLAS_LIBS", OCTAVE_CONF_BLAS_LIBS }, | |
795 { false, "CC", OCTAVE_CONF_CC }, | |
796 { false, "CC_VERSION", OCTAVE_CONF_CC_VERSION }, | |
797 { false, "CFLAGS", OCTAVE_CONF_CFLAGS }, | |
798 { false, "CPICFLAG", OCTAVE_CONF_CPICFLAG }, | |
799 { false, "CPPFLAGS", OCTAVE_CONF_CPPFLAGS }, | |
800 { false, "CURL_LIBS", OCTAVE_CONF_CURL_LIBS }, | |
801 { false, "CXX", OCTAVE_CONF_CXX }, | |
802 { false, "CXXCPP", OCTAVE_CONF_CXXCPP }, | |
803 { false, "CXXFLAGS", OCTAVE_CONF_CXXFLAGS }, | |
804 { false, "CXXPICFLAG", OCTAVE_CONF_CXXPICFLAG }, | |
805 { false, "CXX_VERSION", OCTAVE_CONF_CXX_VERSION }, | |
806 { false, "DEFAULT_PAGER", OCTAVE_DEFAULT_PAGER }, | |
807 { false, "DEFS", OCTAVE_CONF_DEFS }, | |
808 { false, "DL_LD", OCTAVE_CONF_DL_LD }, | |
809 { false, "DL_LDFLAGS", OCTAVE_CONF_DL_LDFLAGS }, | |
810 { false, "ENABLE_DYNAMIC_LINKING", OCTAVE_CONF_ENABLE_DYNAMIC_LINKING }, | |
811 { false, "EXEEXT", OCTAVE_CONF_EXEEXT }, | |
812 { false, "F2C", OCTAVE_CONF_F2C }, | |
813 { false, "F2CFLAGS", OCTAVE_CONF_F2CFLAGS }, | |
814 { false, "F77", OCTAVE_CONF_F77 }, | |
815 { false, "F77_FLOAT_STORE_FLAG", OCTAVE_CONF_F77_FLOAT_STORE_FLAG }, | |
816 { false, "FC", OCTAVE_CONF_FC }, | |
817 { false, "FFLAGS", OCTAVE_CONF_FFLAGS }, | |
818 { false, "FFTW_LIBS", OCTAVE_CONF_FFTW_LIBS }, | |
819 { false, "FLIBS", OCTAVE_CONF_FLIBS }, | |
820 { false, "FPICFLAG", OCTAVE_CONF_FPICFLAG }, | |
821 { false, "GLPK_LIBS", OCTAVE_CONF_GLPK_LIBS }, | |
7361 | 822 { false, "GNUPLOT", OCTAVE_CONF_GNUPLOT }, |
6274 | 823 { false, "INCFLAGS", OCTAVE_CONF_INCFLAGS }, |
824 { false, "LDFLAGS", OCTAVE_CONF_LDFLAGS }, | |
825 { false, "LD_CXX", OCTAVE_CONF_LD_CXX }, | |
826 { false, "LD_STATIC_FLAG", OCTAVE_CONF_LD_STATIC_FLAG }, | |
827 { false, "LEX", OCTAVE_CONF_LEX }, | |
828 { false, "LEXLIB", OCTAVE_CONF_LEXLIB }, | |
829 { false, "LFLAGS", OCTAVE_CONF_LFLAGS }, | |
830 { false, "LIBCRUFT", OCTAVE_CONF_LIBCRUFT }, | |
831 { false, "LIBEXT", OCTAVE_CONF_LIBEXT }, | |
832 { false, "LIBFLAGS", OCTAVE_CONF_LIBFLAGS }, | |
833 { false, "LIBOCTAVE", OCTAVE_CONF_LIBOCTAVE }, | |
834 { false, "LIBOCTINTERP", OCTAVE_CONF_LIBOCTINTERP }, | |
835 { false, "LIBREADLINE", OCTAVE_CONF_LIBREADLINE }, | |
836 { false, "LIBS", OCTAVE_CONF_LIBS }, | |
837 { false, "LN_S", OCTAVE_CONF_LN_S }, | |
838 { false, "MKOCTFILE_DL_LDFLAGS", OCTAVE_CONF_MKOCTFILE_DL_LDFLAGS }, | |
839 { false, "RANLIB", OCTAVE_CONF_RANLIB }, | |
840 { false, "RDYNAMIC_FLAG", OCTAVE_CONF_RDYNAMIC_FLAG }, | |
841 { false, "RLD_FLAG", OCTAVE_CONF_RLD_FLAG }, | |
842 { false, "SED", OCTAVE_CONF_SED }, | |
843 { false, "SHARED_LIBS", OCTAVE_CONF_SHARED_LIBS }, | |
844 { false, "SHLEXT", OCTAVE_CONF_SHLEXT }, | |
845 { false, "SHLEXT_VER", OCTAVE_CONF_SHLEXT_VER }, | |
846 { false, "SH_LD", OCTAVE_CONF_SH_LD }, | |
847 { false, "SH_LDFLAGS", OCTAVE_CONF_SH_LDFLAGS }, | |
848 { false, "SONAME_FLAGS", OCTAVE_CONF_SONAME_FLAGS }, | |
849 { false, "STATIC_LIBS", OCTAVE_CONF_STATIC_LIBS }, | |
850 { false, "UGLY_DEFS", OCTAVE_CONF_UGLY_DEFS }, | |
851 { false, "USE_64_BIT_IDX_T", OCTAVE_CONF_USE_64_BIT_IDX_T }, | |
852 { false, "XTRA_CFLAGS", OCTAVE_CONF_XTRA_CFLAGS }, | |
853 { false, "XTRA_CXXFLAGS", OCTAVE_CONF_XTRA_CXXFLAGS }, | |
854 { false, "YACC", OCTAVE_CONF_YACC }, | |
855 { false, "YFLAGS", OCTAVE_CONF_YFLAGS }, | |
856 { false, "api_version", OCTAVE_API_VERSION }, | |
857 { true, "archlibdir", OCTAVE_ARCHLIBDIR }, | |
858 { true, "bindir", OCTAVE_BINDIR }, | |
859 { false, "canonical_host_type", OCTAVE_CANONICAL_HOST_TYPE }, | |
860 { false, "config_opts", OCTAVE_CONF_config_opts }, | |
861 { true, "datadir", OCTAVE_DATADIR }, | |
862 { true, "datarootdir", OCTAVE_DATAROOTDIR }, | |
6276 | 863 { true, "exec_prefix", OCTAVE_EXEC_PREFIX }, |
6274 | 864 { true, "fcnfiledir", OCTAVE_FCNFILEDIR }, |
865 { true, "imagedir", OCTAVE_IMAGEDIR }, | |
866 { true, "includedir", OCTAVE_INCLUDEDIR }, | |
867 { true, "infodir", OCTAVE_INFODIR }, | |
6276 | 868 { true, "infofile", OCTAVE_INFOFILE }, |
6274 | 869 { true, "libdir", OCTAVE_LIBDIR }, |
870 { true, "libexecdir", OCTAVE_LIBEXECDIR }, | |
6311 | 871 { true, "localapiarchlibdir", OCTAVE_LOCALAPIARCHLIBDIR }, |
6274 | 872 { true, "localapifcnfiledir", OCTAVE_LOCALAPIFCNFILEDIR }, |
873 { true, "localapioctfiledir", OCTAVE_LOCALAPIOCTFILEDIR }, | |
874 { true, "localarchlibdir", OCTAVE_LOCALARCHLIBDIR }, | |
875 { true, "localfcnfiledir", OCTAVE_LOCALFCNFILEDIR }, | |
876 { true, "localoctfiledir", OCTAVE_LOCALOCTFILEDIR }, | |
877 { true, "localstartupfiledir", OCTAVE_LOCALSTARTUPFILEDIR }, | |
878 { true, "localverarchlibdir", OCTAVE_LOCALVERARCHLIBDIR }, | |
879 { true, "localverfcnfiledir", OCTAVE_LOCALVERFCNFILEDIR }, | |
880 { true, "localveroctfiledir", OCTAVE_LOCALVEROCTFILEDIR }, | |
881 { true, "man1dir", OCTAVE_MAN1DIR }, | |
882 { false, "man1ext", OCTAVE_MAN1EXT }, | |
883 { true, "mandir", OCTAVE_MANDIR }, | |
884 { true, "octfiledir", OCTAVE_OCTFILEDIR }, | |
885 { true, "octincludedir", OCTAVE_OCTINCLUDEDIR }, | |
886 { true, "octlibdir", OCTAVE_OCTLIBDIR }, | |
6276 | 887 { true, "prefix", OCTAVE_PREFIX }, |
6274 | 888 { true, "startupfiledir", OCTAVE_STARTUPFILEDIR }, |
889 { false, "version", OCTAVE_VERSION }, | |
890 { false, 0, 0 } | |
4357 | 891 }; |
892 | |
893 if (! initialized) | |
894 { | |
4675 | 895 m.assign ("dld", octave_value (octave_supports_dynamic_linking)); |
4357 | 896 |
4697 | 897 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
898 m.assign ("float_format", | |
899 octave_value (oct_mach_info::float_format_as_string (ff))); | |
900 | |
901 m.assign ("words_big_endian", | |
902 octave_value (oct_mach_info::words_big_endian ())); | |
903 | |
904 m.assign ("words_little_endian", | |
905 octave_value (oct_mach_info::words_little_endian ())); | |
906 | |
4357 | 907 int i = 0; |
4440 | 908 |
909 while (true) | |
4357 | 910 { |
6274 | 911 const conf_info_struct& elt = conf_info[i++]; |
912 | |
913 const char *key = elt.key; | |
4357 | 914 |
915 if (key) | |
6274 | 916 { |
917 if (elt.subst_home) | |
918 m.assign (key, octave_value (subst_octave_home (elt.val))); | |
919 else | |
920 m.assign (key, octave_value (elt.val)); | |
921 } | |
4357 | 922 else |
923 break; | |
924 } | |
925 | |
4691 | 926 bool unix_system = true; |
7013 | 927 bool mac_system = false; |
4691 | 928 bool windows_system = false; |
929 | |
930 #if defined (WIN32) | |
931 windows_system = true; | |
932 #if !defined (__CYGWIN__) | |
933 unix_system = false; | |
934 #endif | |
935 #endif | |
936 | |
7010 | 937 #if defined (__APPLE__) && defined (__MACH__) |
938 mac_system = true; | |
939 #endif | |
940 | |
4691 | 941 m.assign ("unix", octave_value (unix_system)); |
7010 | 942 m.assign ("mac", octave_value (mac_system)); |
4691 | 943 m.assign ("windows", octave_value (windows_system)); |
944 | |
4357 | 945 initialized = true; |
946 } | |
2162 | 947 |
2689 | 948 int nargin = args.length (); |
949 | |
950 if (nargin == 1) | |
951 { | |
3523 | 952 std::string arg = args(0).string_value (); |
2689 | 953 |
954 if (! error_state) | |
4675 | 955 { |
956 Cell c = m.contents (arg.c_str ()); | |
5199 | 957 |
958 if (c.is_empty ()) | |
959 error ("octave_config_info: no info for `%s'", arg.c_str ()); | |
960 else | |
961 retval = c(0); | |
4675 | 962 } |
2689 | 963 } |
964 else if (nargin == 0) | |
4233 | 965 retval = m; |
2689 | 966 else |
5823 | 967 print_usage (); |
2689 | 968 |
969 return retval; | |
2162 | 970 } |
971 | |
1683 | 972 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
2806 | 973 |
1683 | 974 int debug_new_delete = 0; |
975 | |
976 typedef void (*vfp)(void); | |
977 extern vfp __new_handler; | |
978 | |
979 void * | |
980 __builtin_new (size_t sz) | |
981 { | |
982 void *p; | |
983 | |
984 /* malloc (0) is unpredictable; avoid it. */ | |
985 if (sz == 0) | |
986 sz = 1; | |
2800 | 987 p = malloc (sz); |
1683 | 988 while (p == 0) |
989 { | |
990 (*__new_handler) (); | |
2800 | 991 p = malloc (sz); |
1683 | 992 } |
993 | |
994 if (debug_new_delete) | |
5629 | 995 std::cerr << "__builtin_new: " << p << std::endl; |
1683 | 996 |
997 return p; | |
998 } | |
999 | |
1000 void | |
1001 __builtin_delete (void *ptr) | |
1002 { | |
1003 if (debug_new_delete) | |
5629 | 1004 std::cerr << "__builtin_delete: " << ptr << std::endl; |
1683 | 1005 |
1006 if (ptr) | |
1007 free (ptr); | |
1008 } | |
2806 | 1009 |
1683 | 1010 #endif |
1011 | |
1012 /* | |
1013 ;;; Local Variables: *** | |
1014 ;;; mode: C++ *** | |
1015 ;;; End: *** | |
1016 */ |