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