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