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