1683
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
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 |
4180
|
178 static void |
|
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\ |
|
347 the Free Software Foundation; either version 2 of the License, or\n\ |
|
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\ |
|
356 along with this program. If not, write to the Free Software\n\ |
5307
|
357 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA\n\ |
|
358 02110-1301, USA.\n\ |
1683
|
359 \n"; |
|
360 |
|
361 return retval; |
|
362 } |
|
363 |
|
364 // Execute a shell command. |
|
365 |
1965
|
366 static void |
|
367 cleanup_iprocstream (void *p) |
|
368 { |
3060
|
369 iprocstream *cmd = static_cast<iprocstream *> (p); |
|
370 |
|
371 octave_child_list::remove (cmd->pid ()); |
|
372 |
|
373 delete cmd; |
1965
|
374 } |
|
375 |
6316
|
376 static int |
|
377 wait_for_input (int fid) |
|
378 { |
|
379 int retval = -1; |
|
380 |
|
381 #if defined (HAVE_SELECT) |
|
382 if (fid >= 0) |
|
383 { |
|
384 fd_set set; |
|
385 |
|
386 FD_ZERO (&set); |
|
387 FD_SET (fid, &set); |
|
388 |
|
389 retval = select (FD_SETSIZE, &set, 0, 0, 0); |
|
390 } |
|
391 #else |
|
392 retval = 1; |
|
393 #endif |
|
394 |
|
395 return retval; |
|
396 } |
|
397 |
2086
|
398 static octave_value_list |
3523
|
399 run_command_and_return_output (const std::string& cmd_str) |
2083
|
400 { |
2086
|
401 octave_value_list retval; |
2083
|
402 |
|
403 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); |
|
404 |
3060
|
405 if (cmd) |
2083
|
406 { |
3060
|
407 unwind_protect::add (cleanup_iprocstream, cmd); |
|
408 |
|
409 if (*cmd) |
|
410 { |
6316
|
411 int fid = cmd->file_number (); |
2095
|
412 |
6316
|
413 std::ostringstream output_buf; |
4494
|
414 |
3060
|
415 char ch; |
3147
|
416 |
4489
|
417 for (;;) |
3147
|
418 { |
4489
|
419 if (cmd->get (ch)) |
|
420 output_buf.put (ch); |
|
421 else |
|
422 { |
|
423 if (! cmd->eof () && errno == EAGAIN) |
|
424 { |
|
425 cmd->clear (); |
3147
|
426 |
6316
|
427 if (wait_for_input (fid) != 1) |
|
428 break; |
4489
|
429 } |
|
430 else |
|
431 break; |
|
432 } |
3147
|
433 } |
|
434 |
3252
|
435 int cmd_status = cmd->close (); |
2083
|
436 |
3060
|
437 if (WIFEXITED (cmd_status)) |
|
438 cmd_status = WEXITSTATUS (cmd_status); |
|
439 else |
|
440 cmd_status = 127; |
2083
|
441 |
5659
|
442 retval(0) = (double) cmd_status; |
5765
|
443 retval(1) = output_buf.str (); |
3060
|
444 } |
|
445 |
|
446 unwind_protect::run (); |
2083
|
447 } |
|
448 else |
|
449 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); |
|
450 |
|
451 return retval; |
|
452 } |
|
453 |
5285
|
454 enum system_exec_type { et_sync, et_async }; |
|
455 |
1957
|
456 DEFUN (system, args, nargout, |
3301
|
457 "-*- texinfo -*-\n\ |
|
458 @deftypefn {Built-in Function} {} system (@var{string}, @var{return_output}, @var{type})\n\ |
|
459 Execute a shell command specified by @var{string}. The second\n\ |
|
460 argument is optional. If @var{type} is @code{\"async\"}, the process\n\ |
|
461 is started in the background and the process id of the child process\n\ |
|
462 is returned immediately. Otherwise, the process is started, and\n\ |
|
463 Octave waits until it exits. If @var{type} argument is omitted, a\n\ |
|
464 value of @code{\"sync\"} is assumed.\n\ |
2083
|
465 \n\ |
3301
|
466 If two input arguments are given (the actual value of\n\ |
|
467 @var{return_output} is irrelevant) and the subprocess is started\n\ |
|
468 synchronously, or if @var{system} is called with one input argument and\n\ |
|
469 one or more output arguments, the output from the command is returned.\n\ |
5097
|
470 Otherwise, if the subprocess is executed synchronously, its output is\n\ |
2321
|
471 sent to the standard output. To send the output of a command executed\n\ |
3301
|
472 with @var{system} through the pager, use a command like\n\ |
2321
|
473 \n\ |
3301
|
474 @example\n\ |
|
475 disp (system (cmd, 1));\n\ |
|
476 @end example\n\ |
2321
|
477 \n\ |
3301
|
478 @noindent\n\ |
2321
|
479 or\n\ |
|
480 \n\ |
3301
|
481 @example\n\ |
|
482 printf (\"%s\n\", system (cmd, 1));\n\ |
|
483 @end example\n\ |
|
484 \n\ |
5659
|
485 The @code{system} function can return two values. The first is the\n\ |
|
486 exit status of the command and the second is any output from the\n\ |
|
487 command that was written to the standard output stream. For example,\n\ |
3301
|
488 \n\ |
|
489 @example\n\ |
5717
|
490 [status, output] = system (\"echo foo; exit 2\");\n\ |
3301
|
491 @end example\n\ |
|
492 \n\ |
|
493 @noindent\n\ |
|
494 will set the variable @code{output} to the string @samp{foo}, and the\n\ |
|
495 variable @code{status} to the integer @samp{2}.\n\ |
|
496 @end deftypefn") |
1683
|
497 { |
2086
|
498 octave_value_list retval; |
1683
|
499 |
3834
|
500 unwind_protect::begin_frame ("Fsystem"); |
|
501 |
1683
|
502 int nargin = args.length (); |
|
503 |
2083
|
504 if (nargin > 0 && nargin < 4) |
1683
|
505 { |
5672
|
506 bool return_output = (nargout > 1 || nargin > 1); |
2083
|
507 |
3523
|
508 std::string cmd_str = args(0).string_value (); |
2083
|
509 |
5285
|
510 system_exec_type type = et_sync; |
2083
|
511 |
|
512 if (! error_state) |
|
513 { |
|
514 if (nargin > 2) |
|
515 { |
3523
|
516 std::string type_str = args(2).string_value (); |
1683
|
517 |
2083
|
518 if (! error_state) |
|
519 { |
|
520 if (type_str == "sync") |
5285
|
521 type = et_sync; |
2083
|
522 else if (type_str == "async") |
5285
|
523 type = et_async; |
2083
|
524 else |
|
525 error ("system: third arg must be \"sync\" or \"async\""); |
|
526 } |
|
527 else |
|
528 error ("system: third argument must be a string"); |
|
529 } |
|
530 } |
|
531 else |
3523
|
532 error ("system: expecting std::string as first argument"); |
1683
|
533 |
2083
|
534 if (! error_state) |
|
535 { |
5285
|
536 if (type == et_async) |
2083
|
537 { |
6222
|
538 // FIXME -- maybe this should go in sysdep.cc? |
4086
|
539 #ifdef HAVE_FORK |
2083
|
540 pid_t pid = fork (); |
|
541 |
|
542 if (pid < 0) |
|
543 error ("system: fork failed -- can't create child process"); |
|
544 else if (pid == 0) |
|
545 { |
5775
|
546 // FIXME -- should probably replace this |
3273
|
547 // call with something portable. |
|
548 |
5510
|
549 execl ("/bin/sh", "sh", "-c", cmd_str.c_str (), |
|
550 static_cast<void *> (0)); |
3273
|
551 |
|
552 panic_impossible (); |
2083
|
553 } |
|
554 else |
4254
|
555 retval(0) = pid; |
6222
|
556 #elif defined (__WIN32__) |
|
557 STARTUPINFO si; |
|
558 PROCESS_INFORMATION pi; |
|
559 ZeroMemory (&si, sizeof (si)); |
|
560 ZeroMemory (&pi, sizeof (pi)); |
|
561 OCTAVE_LOCAL_BUFFER (char, xcmd_str, cmd_str.length()+1); |
6676
|
562 strcpy (xcmd_str, cmd_str.c_str ()); |
6222
|
563 |
|
564 if (! CreateProcess (0, xcmd_str, 0, 0, FALSE, 0, 0, 0, &si, &pi)) |
|
565 error ("system: CreateProcess failed -- can't create child process"); |
|
566 else |
|
567 { |
|
568 retval(0) = pi.dwProcessId; |
|
569 CloseHandle (pi.hProcess); |
|
570 CloseHandle (pi.hThread); |
|
571 } |
4086
|
572 #else |
|
573 error ("asynchronous system calls are not supported"); |
|
574 #endif |
2083
|
575 } |
2321
|
576 else if (return_output) |
|
577 retval = run_command_and_return_output (cmd_str); |
2083
|
578 else |
2321
|
579 { |
|
580 int status = system (cmd_str.c_str ()); |
|
581 |
|
582 // The value in status is as returned by waitpid. If |
|
583 // the process exited normally, extract the actual exit |
|
584 // status of the command. Otherwise, return 127 as a |
|
585 // failure code. |
|
586 |
2693
|
587 if (WIFEXITED (status)) |
|
588 status = WEXITSTATUS (status); |
2321
|
589 |
4233
|
590 retval(0) = status; |
2321
|
591 } |
2083
|
592 } |
1683
|
593 } |
|
594 else |
5823
|
595 print_usage (); |
1683
|
596 |
3834
|
597 unwind_protect::run_frame ("Fsystem"); |
|
598 |
1683
|
599 return retval; |
|
600 } |
|
601 |
|
602 DEFALIAS (shell_cmd, system); |
|
603 |
5775
|
604 // FIXME -- this should really be static, but that causes |
2614
|
605 // problems on some systems. |
6680
|
606 std::list<std::string> octave_atexit_functions; |
2077
|
607 |
|
608 void |
|
609 do_octave_atexit (void) |
|
610 { |
3216
|
611 static bool deja_vu = false; |
|
612 |
2077
|
613 while (! octave_atexit_functions.empty ()) |
|
614 { |
6680
|
615 std::string fcn = octave_atexit_functions.front (); |
4214
|
616 |
6680
|
617 octave_atexit_functions.pop_front (); |
2077
|
618 |
5237
|
619 reset_error_handler (); |
|
620 |
4233
|
621 feval (fcn, octave_value_list (), 0); |
3215
|
622 |
|
623 flush_octave_stdout (); |
2077
|
624 } |
3216
|
625 |
|
626 if (! deja_vu) |
|
627 { |
|
628 deja_vu = true; |
|
629 |
6068
|
630 // Do this explicitly so that destructors for mex file objects |
|
631 // are called, so that functions registered with mexAtExit are |
|
632 // called. |
6072
|
633 clear_mex_functions (); |
6068
|
634 |
3216
|
635 command_editor::restore_terminal_state (); |
|
636 |
5775
|
637 // FIXME -- is this needed? Can it cause any trouble? |
3216
|
638 raw_mode (0); |
|
639 |
5305
|
640 octave_history_write_timestamp (); |
|
641 |
3216
|
642 command_history::clean_up_and_save (); |
|
643 |
|
644 close_files (); |
|
645 |
|
646 cleanup_tmp_files (); |
|
647 |
|
648 flush_octave_stdout (); |
|
649 |
5305
|
650 if (! quitting_gracefully && (interactive || forced_interactive)) |
5629
|
651 { |
|
652 octave_stdout << "\n"; |
|
653 |
|
654 // Yes, we want this to be separate from the call to |
|
655 // flush_octave_stdout above. |
|
656 |
|
657 flush_octave_stdout (); |
|
658 } |
3216
|
659 } |
2077
|
660 } |
|
661 |
6680
|
662 DEFUN (atexit, args, nargout, |
3332
|
663 "-*- texinfo -*-\n\ |
|
664 @deftypefn {Built-in Function} {} atexit (@var{fcn})\n\ |
|
665 Register a function to be called when Octave exits. For example,\n\ |
2077
|
666 \n\ |
3332
|
667 @example\n\ |
|
668 @group\n\ |
6620
|
669 function bye_bye ()\n\ |
|
670 disp (\"Bye bye\");\n\ |
3332
|
671 endfunction\n\ |
6620
|
672 atexit (\"bye_bye\");\n\ |
3332
|
673 @end group\n\ |
|
674 @end example\n\ |
|
675 \n\ |
|
676 @noindent\n\ |
6620
|
677 will print the message \"Bye bye\" when Octave exits.\n\ |
6680
|
678 \n\ |
|
679 @deftypefnx {Built-in Function} {} atexit (@var{fcn}, @var{flag})\n\ |
|
680 Register or unregister a function to be called when Octave exits,\n\ |
|
681 depending on @var{flag}. If @var{flag} is true, the function is\n\ |
|
682 registered, if @var{flag} is false, it is unregistered. For example,\n\ |
|
683 after registering the function @code{bye_bye} as above,\n\ |
|
684 \n\ |
|
685 @example\n\ |
|
686 atexit (\"bye_bye\", false);\n\ |
|
687 @end example\n\ |
|
688 \n\ |
|
689 @noindent\n\ |
|
690 will remove the function from the list and Octave will not call\n\ |
|
691 the function @code{bye_by} when it exits.\n\ |
|
692 \n\ |
7001
|
693 Note that @code{atexit} only removes the first occurrence of a function\n\ |
6680
|
694 from the list, so if a function was placed in the list multiple\n\ |
|
695 times with @code{atexit}, it must also be removed from the list\n\ |
|
696 multiple times.\n\ |
3333
|
697 @end deftypefn") |
2077
|
698 { |
2086
|
699 octave_value_list retval; |
2077
|
700 |
|
701 int nargin = args.length (); |
|
702 |
6680
|
703 if (nargin == 1 || nargin == 2) |
2077
|
704 { |
3523
|
705 std::string arg = args(0).string_value (); |
2077
|
706 |
|
707 if (! error_state) |
6680
|
708 { |
|
709 bool add_mode = true; |
|
710 |
|
711 if (nargin == 2) |
|
712 { |
|
713 add_mode = args(1).bool_value (); |
|
714 |
|
715 if (error_state) |
|
716 error ("atexit: second argument must be a logical value"); |
|
717 } |
|
718 |
|
719 if (! error_state) |
|
720 { |
|
721 if (add_mode) |
|
722 octave_atexit_functions.push_front (arg); |
|
723 else |
|
724 { |
|
725 bool found = false; |
|
726 std::list<std::string>::iterator it; |
|
727 |
|
728 for (std::list<std::string>::iterator p = octave_atexit_functions.begin (); |
|
729 p != octave_atexit_functions.end (); p++) |
|
730 { |
|
731 if (*p == arg) |
|
732 { |
|
733 octave_atexit_functions.erase (p); |
|
734 found = true; |
|
735 break; |
|
736 } |
|
737 } |
|
738 |
|
739 if (nargout > 0) |
|
740 retval(0) = found; |
|
741 } |
|
742 } |
|
743 } |
2077
|
744 else |
6680
|
745 error ("atexit: argument must be a string"); |
2077
|
746 } |
|
747 else |
5823
|
748 print_usage (); |
2077
|
749 |
|
750 return retval; |
|
751 } |
|
752 |
2689
|
753 DEFUN (octave_config_info, args, , |
3301
|
754 "-*- texinfo -*-\n\ |
|
755 @deftypefn {Built-in Function} {} octave_config_info (@var{option})\n\ |
|
756 Return a structure containing configuration and installation\n\ |
|
757 information for Octave.\n\ |
2689
|
758 \n\ |
3301
|
759 if @var{option} is a string, return the configuration information for the\n\ |
2689
|
760 specified option.\n\ |
|
761 \n\ |
3301
|
762 @end deftypefn") |
2162
|
763 { |
2689
|
764 octave_value retval; |
|
765 |
4128
|
766 #if defined (ENABLE_DYNAMIC_LINKING) |
2689
|
767 bool octave_supports_dynamic_linking = true; |
|
768 #else |
|
769 bool octave_supports_dynamic_linking = false; |
|
770 #endif |
|
771 |
4357
|
772 static bool initialized = false; |
|
773 static Octave_map m; |
2162
|
774 |
6274
|
775 struct conf_info_struct |
|
776 { |
|
777 bool subst_home; |
|
778 const char *key; |
|
779 const char *val; |
|
780 }; |
|
781 |
|
782 static const conf_info_struct conf_info[] = |
4357
|
783 { |
6274
|
784 { false, "ALL_CFLAGS", OCTAVE_CONF_ALL_CFLAGS }, |
|
785 { false, "ALL_CXXFLAGS", OCTAVE_CONF_ALL_CXXFLAGS }, |
|
786 { false, "ALL_FFLAGS", OCTAVE_CONF_ALL_FFLAGS }, |
|
787 { false, "ALL_LDFLAGS", OCTAVE_CONF_ALL_LDFLAGS }, |
|
788 { false, "AR", OCTAVE_CONF_AR }, |
|
789 { false, "ARFLAGS", OCTAVE_CONF_ARFLAGS }, |
|
790 { false, "BLAS_LIBS", OCTAVE_CONF_BLAS_LIBS }, |
|
791 { false, "CC", OCTAVE_CONF_CC }, |
|
792 { false, "CC_VERSION", OCTAVE_CONF_CC_VERSION }, |
|
793 { false, "CFLAGS", OCTAVE_CONF_CFLAGS }, |
|
794 { false, "CPICFLAG", OCTAVE_CONF_CPICFLAG }, |
|
795 { false, "CPPFLAGS", OCTAVE_CONF_CPPFLAGS }, |
|
796 { false, "CURL_LIBS", OCTAVE_CONF_CURL_LIBS }, |
|
797 { false, "CXX", OCTAVE_CONF_CXX }, |
|
798 { false, "CXXCPP", OCTAVE_CONF_CXXCPP }, |
|
799 { false, "CXXFLAGS", OCTAVE_CONF_CXXFLAGS }, |
|
800 { false, "CXXPICFLAG", OCTAVE_CONF_CXXPICFLAG }, |
|
801 { false, "CXX_VERSION", OCTAVE_CONF_CXX_VERSION }, |
|
802 { false, "DEFAULT_PAGER", OCTAVE_DEFAULT_PAGER }, |
|
803 { false, "DEFS", OCTAVE_CONF_DEFS }, |
|
804 { false, "DLFCN_INCFLAGS", OCTAVE_CONF_DLFCN_INCFLAGS }, |
|
805 { false, "DL_LD", OCTAVE_CONF_DL_LD }, |
|
806 { false, "DL_LDFLAGS", OCTAVE_CONF_DL_LDFLAGS }, |
|
807 { false, "ENABLE_DYNAMIC_LINKING", OCTAVE_CONF_ENABLE_DYNAMIC_LINKING }, |
|
808 { false, "EXEEXT", OCTAVE_CONF_EXEEXT }, |
|
809 { false, "F2C", OCTAVE_CONF_F2C }, |
|
810 { false, "F2CFLAGS", OCTAVE_CONF_F2CFLAGS }, |
|
811 { false, "F77", OCTAVE_CONF_F77 }, |
|
812 { false, "F77_FLOAT_STORE_FLAG", OCTAVE_CONF_F77_FLOAT_STORE_FLAG }, |
|
813 { false, "FC", OCTAVE_CONF_FC }, |
|
814 { false, "FFLAGS", OCTAVE_CONF_FFLAGS }, |
|
815 { false, "FFTW_LIBS", OCTAVE_CONF_FFTW_LIBS }, |
|
816 { false, "FLIBS", OCTAVE_CONF_FLIBS }, |
|
817 { false, "FPICFLAG", OCTAVE_CONF_FPICFLAG }, |
|
818 { false, "GLPK_LIBS", OCTAVE_CONF_GLPK_LIBS }, |
|
819 { false, "INCFLAGS", OCTAVE_CONF_INCFLAGS }, |
|
820 { false, "LDFLAGS", OCTAVE_CONF_LDFLAGS }, |
|
821 { false, "LD_CXX", OCTAVE_CONF_LD_CXX }, |
|
822 { false, "LD_STATIC_FLAG", OCTAVE_CONF_LD_STATIC_FLAG }, |
|
823 { false, "LEX", OCTAVE_CONF_LEX }, |
|
824 { false, "LEXLIB", OCTAVE_CONF_LEXLIB }, |
|
825 { false, "LFLAGS", OCTAVE_CONF_LFLAGS }, |
|
826 { false, "LIBCRUFT", OCTAVE_CONF_LIBCRUFT }, |
|
827 { false, "LIBDLFCN", OCTAVE_CONF_LIBDLFCN }, |
|
828 { false, "LIBEXT", OCTAVE_CONF_LIBEXT }, |
|
829 { false, "LIBFLAGS", OCTAVE_CONF_LIBFLAGS }, |
|
830 { false, "LIBOCTAVE", OCTAVE_CONF_LIBOCTAVE }, |
|
831 { false, "LIBOCTINTERP", OCTAVE_CONF_LIBOCTINTERP }, |
|
832 { false, "LIBREADLINE", OCTAVE_CONF_LIBREADLINE }, |
|
833 { false, "LIBS", OCTAVE_CONF_LIBS }, |
|
834 { false, "LN_S", OCTAVE_CONF_LN_S }, |
|
835 { false, "MKOCTFILE_DL_LDFLAGS", OCTAVE_CONF_MKOCTFILE_DL_LDFLAGS }, |
|
836 { false, "RANLIB", OCTAVE_CONF_RANLIB }, |
|
837 { false, "RDYNAMIC_FLAG", OCTAVE_CONF_RDYNAMIC_FLAG }, |
|
838 { false, "RLD_FLAG", OCTAVE_CONF_RLD_FLAG }, |
|
839 { false, "SED", OCTAVE_CONF_SED }, |
|
840 { false, "SHARED_LIBS", OCTAVE_CONF_SHARED_LIBS }, |
|
841 { false, "SHLEXT", OCTAVE_CONF_SHLEXT }, |
|
842 { false, "SHLEXT_VER", OCTAVE_CONF_SHLEXT_VER }, |
|
843 { false, "SH_LD", OCTAVE_CONF_SH_LD }, |
|
844 { false, "SH_LDFLAGS", OCTAVE_CONF_SH_LDFLAGS }, |
|
845 { false, "SONAME_FLAGS", OCTAVE_CONF_SONAME_FLAGS }, |
|
846 { false, "STATIC_LIBS", OCTAVE_CONF_STATIC_LIBS }, |
|
847 { false, "UGLY_DEFS", OCTAVE_CONF_UGLY_DEFS }, |
|
848 { false, "USE_64_BIT_IDX_T", OCTAVE_CONF_USE_64_BIT_IDX_T }, |
|
849 { false, "XTRA_CFLAGS", OCTAVE_CONF_XTRA_CFLAGS }, |
|
850 { false, "XTRA_CXXFLAGS", OCTAVE_CONF_XTRA_CXXFLAGS }, |
|
851 { false, "YACC", OCTAVE_CONF_YACC }, |
|
852 { false, "YFLAGS", OCTAVE_CONF_YFLAGS }, |
|
853 { false, "api_version", OCTAVE_API_VERSION }, |
|
854 { true, "archlibdir", OCTAVE_ARCHLIBDIR }, |
|
855 { true, "bindir", OCTAVE_BINDIR }, |
|
856 { false, "canonical_host_type", OCTAVE_CANONICAL_HOST_TYPE }, |
|
857 { false, "config_opts", OCTAVE_CONF_config_opts }, |
|
858 { true, "datadir", OCTAVE_DATADIR }, |
|
859 { true, "datarootdir", OCTAVE_DATAROOTDIR }, |
6276
|
860 { true, "exec_prefix", OCTAVE_EXEC_PREFIX }, |
6274
|
861 { true, "fcnfiledir", OCTAVE_FCNFILEDIR }, |
|
862 { true, "imagedir", OCTAVE_IMAGEDIR }, |
|
863 { true, "includedir", OCTAVE_INCLUDEDIR }, |
|
864 { true, "infodir", OCTAVE_INFODIR }, |
6276
|
865 { true, "infofile", OCTAVE_INFOFILE }, |
6274
|
866 { true, "libdir", OCTAVE_LIBDIR }, |
|
867 { true, "libexecdir", OCTAVE_LIBEXECDIR }, |
6311
|
868 { true, "localapiarchlibdir", OCTAVE_LOCALAPIARCHLIBDIR }, |
6274
|
869 { true, "localapifcnfiledir", OCTAVE_LOCALAPIFCNFILEDIR }, |
|
870 { true, "localapioctfiledir", OCTAVE_LOCALAPIOCTFILEDIR }, |
|
871 { true, "localarchlibdir", OCTAVE_LOCALARCHLIBDIR }, |
|
872 { true, "localfcnfiledir", OCTAVE_LOCALFCNFILEDIR }, |
|
873 { true, "localoctfiledir", OCTAVE_LOCALOCTFILEDIR }, |
|
874 { true, "localstartupfiledir", OCTAVE_LOCALSTARTUPFILEDIR }, |
|
875 { true, "localverarchlibdir", OCTAVE_LOCALVERARCHLIBDIR }, |
|
876 { true, "localverfcnfiledir", OCTAVE_LOCALVERFCNFILEDIR }, |
|
877 { true, "localveroctfiledir", OCTAVE_LOCALVEROCTFILEDIR }, |
|
878 { true, "man1dir", OCTAVE_MAN1DIR }, |
|
879 { false, "man1ext", OCTAVE_MAN1EXT }, |
|
880 { true, "mandir", OCTAVE_MANDIR }, |
|
881 { true, "octfiledir", OCTAVE_OCTFILEDIR }, |
|
882 { true, "octincludedir", OCTAVE_OCTINCLUDEDIR }, |
|
883 { true, "octlibdir", OCTAVE_OCTLIBDIR }, |
6276
|
884 { true, "prefix", OCTAVE_PREFIX }, |
6274
|
885 { true, "startupfiledir", OCTAVE_STARTUPFILEDIR }, |
|
886 { false, "version", OCTAVE_VERSION }, |
|
887 { false, 0, 0 } |
4357
|
888 }; |
|
889 |
|
890 if (! initialized) |
|
891 { |
4675
|
892 m.assign ("dld", octave_value (octave_supports_dynamic_linking)); |
4357
|
893 |
4697
|
894 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
|
895 m.assign ("float_format", |
|
896 octave_value (oct_mach_info::float_format_as_string (ff))); |
|
897 |
|
898 m.assign ("words_big_endian", |
|
899 octave_value (oct_mach_info::words_big_endian ())); |
|
900 |
|
901 m.assign ("words_little_endian", |
|
902 octave_value (oct_mach_info::words_little_endian ())); |
|
903 |
4357
|
904 int i = 0; |
4440
|
905 |
|
906 while (true) |
4357
|
907 { |
6274
|
908 const conf_info_struct& elt = conf_info[i++]; |
|
909 |
|
910 const char *key = elt.key; |
4357
|
911 |
|
912 if (key) |
6274
|
913 { |
|
914 if (elt.subst_home) |
|
915 m.assign (key, octave_value (subst_octave_home (elt.val))); |
|
916 else |
|
917 m.assign (key, octave_value (elt.val)); |
|
918 } |
4357
|
919 else |
|
920 break; |
|
921 } |
|
922 |
4691
|
923 bool unix_system = true; |
7013
|
924 bool mac_system = false; |
4691
|
925 bool windows_system = false; |
|
926 |
|
927 #if defined (WIN32) |
|
928 windows_system = true; |
|
929 #if !defined (__CYGWIN__) |
|
930 unix_system = false; |
|
931 #endif |
|
932 #endif |
|
933 |
7010
|
934 #if defined (__APPLE__) && defined (__MACH__) |
|
935 mac_system = true; |
|
936 #endif |
|
937 |
4691
|
938 m.assign ("unix", octave_value (unix_system)); |
7010
|
939 m.assign ("mac", octave_value (mac_system)); |
4691
|
940 m.assign ("windows", octave_value (windows_system)); |
|
941 |
4357
|
942 initialized = true; |
|
943 } |
2162
|
944 |
2689
|
945 int nargin = args.length (); |
|
946 |
|
947 if (nargin == 1) |
|
948 { |
3523
|
949 std::string arg = args(0).string_value (); |
2689
|
950 |
|
951 if (! error_state) |
4675
|
952 { |
|
953 Cell c = m.contents (arg.c_str ()); |
5199
|
954 |
|
955 if (c.is_empty ()) |
|
956 error ("octave_config_info: no info for `%s'", arg.c_str ()); |
|
957 else |
|
958 retval = c(0); |
4675
|
959 } |
2689
|
960 } |
|
961 else if (nargin == 0) |
4233
|
962 retval = m; |
2689
|
963 else |
5823
|
964 print_usage (); |
2689
|
965 |
|
966 return retval; |
2162
|
967 } |
|
968 |
1683
|
969 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
2806
|
970 |
1683
|
971 int debug_new_delete = 0; |
|
972 |
|
973 typedef void (*vfp)(void); |
|
974 extern vfp __new_handler; |
|
975 |
|
976 void * |
|
977 __builtin_new (size_t sz) |
|
978 { |
|
979 void *p; |
|
980 |
|
981 /* malloc (0) is unpredictable; avoid it. */ |
|
982 if (sz == 0) |
|
983 sz = 1; |
2800
|
984 p = malloc (sz); |
1683
|
985 while (p == 0) |
|
986 { |
|
987 (*__new_handler) (); |
2800
|
988 p = malloc (sz); |
1683
|
989 } |
|
990 |
|
991 if (debug_new_delete) |
5629
|
992 std::cerr << "__builtin_new: " << p << std::endl; |
1683
|
993 |
|
994 return p; |
|
995 } |
|
996 |
|
997 void |
|
998 __builtin_delete (void *ptr) |
|
999 { |
|
1000 if (debug_new_delete) |
5629
|
1001 std::cerr << "__builtin_delete: " << ptr << std::endl; |
1683
|
1002 |
|
1003 if (ptr) |
|
1004 free (ptr); |
|
1005 } |
2806
|
1006 |
1683
|
1007 #endif |
|
1008 |
|
1009 /* |
|
1010 ;;; Local Variables: *** |
|
1011 ;;; mode: C++ *** |
|
1012 ;;; End: *** |
|
1013 */ |