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 |
|
318 print_usage ("casesen"); |
|
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 { |
4086
|
545 #ifdef HAVE_FORK |
2083
|
546 pid_t pid = fork (); |
|
547 |
|
548 if (pid < 0) |
|
549 error ("system: fork failed -- can't create child process"); |
|
550 else if (pid == 0) |
|
551 { |
5775
|
552 // FIXME -- should probably replace this |
3273
|
553 // call with something portable. |
|
554 |
5510
|
555 execl ("/bin/sh", "sh", "-c", cmd_str.c_str (), |
|
556 static_cast<void *> (0)); |
3273
|
557 |
|
558 panic_impossible (); |
2083
|
559 } |
|
560 else |
4254
|
561 retval(0) = pid; |
4086
|
562 #else |
|
563 error ("asynchronous system calls are not supported"); |
|
564 #endif |
2083
|
565 } |
2321
|
566 else if (return_output) |
|
567 retval = run_command_and_return_output (cmd_str); |
2083
|
568 else |
2321
|
569 { |
|
570 int status = system (cmd_str.c_str ()); |
|
571 |
|
572 // The value in status is as returned by waitpid. If |
|
573 // the process exited normally, extract the actual exit |
|
574 // status of the command. Otherwise, return 127 as a |
|
575 // failure code. |
|
576 |
2693
|
577 if (WIFEXITED (status)) |
|
578 status = WEXITSTATUS (status); |
2321
|
579 |
4233
|
580 retval(0) = status; |
2321
|
581 } |
2083
|
582 } |
1683
|
583 } |
|
584 else |
2083
|
585 print_usage ("system"); |
1683
|
586 |
3834
|
587 unwind_protect::run_frame ("Fsystem"); |
|
588 |
1683
|
589 return retval; |
|
590 } |
|
591 |
|
592 DEFALIAS (shell_cmd, system); |
|
593 |
5775
|
594 // FIXME -- this should really be static, but that causes |
2614
|
595 // problems on some systems. |
4214
|
596 std::stack<std::string> octave_atexit_functions; |
2077
|
597 |
|
598 void |
|
599 do_octave_atexit (void) |
|
600 { |
3216
|
601 static bool deja_vu = false; |
|
602 |
2077
|
603 while (! octave_atexit_functions.empty ()) |
|
604 { |
4233
|
605 std::string fcn = octave_atexit_functions.top (); |
4214
|
606 |
|
607 octave_atexit_functions.pop (); |
2077
|
608 |
5237
|
609 reset_error_handler (); |
|
610 |
4233
|
611 feval (fcn, octave_value_list (), 0); |
3215
|
612 |
|
613 flush_octave_stdout (); |
2077
|
614 } |
3216
|
615 |
|
616 if (! deja_vu) |
|
617 { |
|
618 deja_vu = true; |
|
619 |
|
620 command_editor::restore_terminal_state (); |
|
621 |
5775
|
622 // FIXME -- is this needed? Can it cause any trouble? |
3216
|
623 raw_mode (0); |
|
624 |
5305
|
625 octave_history_write_timestamp (); |
|
626 |
3216
|
627 command_history::clean_up_and_save (); |
|
628 |
|
629 close_files (); |
|
630 |
|
631 cleanup_tmp_files (); |
|
632 |
|
633 flush_octave_stdout (); |
|
634 |
5305
|
635 if (! quitting_gracefully && (interactive || forced_interactive)) |
5629
|
636 { |
|
637 octave_stdout << "\n"; |
|
638 |
|
639 // Yes, we want this to be separate from the call to |
|
640 // flush_octave_stdout above. |
|
641 |
|
642 flush_octave_stdout (); |
|
643 } |
3216
|
644 } |
2077
|
645 } |
|
646 |
2162
|
647 DEFUN (atexit, args, , |
3332
|
648 "-*- texinfo -*-\n\ |
|
649 @deftypefn {Built-in Function} {} atexit (@var{fcn})\n\ |
|
650 Register a function to be called when Octave exits. For example,\n\ |
2077
|
651 \n\ |
3332
|
652 @example\n\ |
|
653 @group\n\ |
3686
|
654 function print_fortune ()\n\ |
3332
|
655 printf (\"\\n%s\\n\", system (\"fortune\"));\n\ |
|
656 fflush (stdout);\n\ |
|
657 endfunction\n\ |
3686
|
658 atexit (\"print_fortune\");\n\ |
3332
|
659 @end group\n\ |
|
660 @end example\n\ |
|
661 \n\ |
|
662 @noindent\n\ |
|
663 will print a message when Octave exits.\n\ |
3333
|
664 @end deftypefn") |
2077
|
665 { |
2086
|
666 octave_value_list retval; |
2077
|
667 |
|
668 int nargin = args.length (); |
|
669 |
|
670 if (nargin == 1) |
|
671 { |
2475
|
672 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
3523
|
673 std::string arg = args(0).string_value (); |
2077
|
674 |
|
675 if (! error_state) |
|
676 octave_atexit_functions.push (arg); |
|
677 else |
|
678 error ("atexit: argument must be a string"); |
2475
|
679 #else |
|
680 gripe_not_supported ("atexit"); |
|
681 #endif |
2077
|
682 } |
|
683 else |
|
684 print_usage ("atexit"); |
|
685 |
|
686 return retval; |
|
687 } |
|
688 |
2689
|
689 DEFUN (octave_config_info, args, , |
3301
|
690 "-*- texinfo -*-\n\ |
|
691 @deftypefn {Built-in Function} {} octave_config_info (@var{option})\n\ |
|
692 Return a structure containing configuration and installation\n\ |
|
693 information for Octave.\n\ |
2689
|
694 \n\ |
3301
|
695 if @var{option} is a string, return the configuration information for the\n\ |
2689
|
696 specified option.\n\ |
|
697 \n\ |
3301
|
698 @end deftypefn") |
2162
|
699 { |
2689
|
700 octave_value retval; |
|
701 |
4128
|
702 #if defined (ENABLE_DYNAMIC_LINKING) |
2689
|
703 bool octave_supports_dynamic_linking = true; |
|
704 #else |
|
705 bool octave_supports_dynamic_linking = false; |
|
706 #endif |
|
707 |
4357
|
708 static bool initialized = false; |
|
709 static Octave_map m; |
2162
|
710 |
4357
|
711 static const char * const conf_info[] = |
|
712 { |
|
713 "ALL_CFLAGS", OCTAVE_CONF_ALL_CFLAGS, |
|
714 "ALL_CXXFLAGS", OCTAVE_CONF_ALL_CXXFLAGS, |
|
715 "ALL_FFLAGS", OCTAVE_CONF_ALL_FFLAGS, |
|
716 "ALL_LDFLAGS", OCTAVE_CONF_ALL_LDFLAGS, |
|
717 "AR", OCTAVE_CONF_AR, |
|
718 "ARFLAGS", OCTAVE_CONF_ARFLAGS, |
|
719 "BLAS_LIBS", OCTAVE_CONF_BLAS_LIBS, |
|
720 "CC", OCTAVE_CONF_CC, |
|
721 "CC_VERSION", OCTAVE_CONF_CC_VERSION, |
|
722 "CFLAGS", OCTAVE_CONF_CFLAGS, |
|
723 "CPICFLAG", OCTAVE_CONF_CPICFLAG, |
|
724 "CPPFLAGS", OCTAVE_CONF_CPPFLAGS, |
|
725 "CXX", OCTAVE_CONF_CXX, |
|
726 "CXXCPP", OCTAVE_CONF_CXXCPP, |
|
727 "CXXFLAGS", OCTAVE_CONF_CXXFLAGS, |
|
728 "CXXPICFLAG", OCTAVE_CONF_CXXPICFLAG, |
|
729 "CXX_VERSION", OCTAVE_CONF_CXX_VERSION, |
|
730 "DEFAULT_PAGER", OCTAVE_DEFAULT_PAGER, |
5398
|
731 "DEFS", OCTAVE_CONF_DEFS, |
|
732 "DLFCN_INCFLAGS", OCTAVE_CONF_DLFCN_INCFLAGS, |
4759
|
733 "DL_LD", OCTAVE_CONF_DL_LD, |
|
734 "DL_LDFLAGS", OCTAVE_CONF_DL_LDFLAGS, |
5398
|
735 "ENABLE_DYNAMIC_LINKING", OCTAVE_CONF_ENABLE_DYNAMIC_LINKING, |
4357
|
736 "EXEEXT", OCTAVE_CONF_EXEEXT, |
|
737 "F2C", OCTAVE_CONF_F2C, |
|
738 "F2CFLAGS", OCTAVE_CONF_F2CFLAGS, |
|
739 "F77", OCTAVE_CONF_F77, |
5498
|
740 "F77_FLOAT_STORE_FLAG", OCTAVE_CONF_F77_FLOAT_STORE_FLAG, |
4357
|
741 "FC", OCTAVE_CONF_FC, |
|
742 "FFLAGS", OCTAVE_CONF_FFLAGS, |
|
743 "FFTW_LIBS", OCTAVE_CONF_FFTW_LIBS, |
|
744 "FLIBS", OCTAVE_CONF_FLIBS, |
|
745 "FPICFLAG", OCTAVE_CONF_FPICFLAG, |
5235
|
746 "GLPK_LIBS", OCTAVE_CONF_GLPK_LIBS, |
4357
|
747 "INCFLAGS", OCTAVE_CONF_INCFLAGS, |
|
748 "LDFLAGS", OCTAVE_CONF_LDFLAGS, |
|
749 "LD_CXX", OCTAVE_CONF_LD_CXX, |
|
750 "LD_STATIC_FLAG", OCTAVE_CONF_LD_STATIC_FLAG, |
|
751 "LEX", OCTAVE_CONF_LEX, |
|
752 "LEXLIB", OCTAVE_CONF_LEXLIB, |
|
753 "LFLAGS", OCTAVE_CONF_LFLAGS, |
|
754 "LIBCRUFT", OCTAVE_CONF_LIBCRUFT, |
|
755 "LIBDLFCN", OCTAVE_CONF_LIBDLFCN, |
|
756 "LIBEXT", OCTAVE_CONF_LIBEXT, |
|
757 "LIBFLAGS", OCTAVE_CONF_LIBFLAGS, |
|
758 "LIBOCTAVE", OCTAVE_CONF_LIBOCTAVE, |
|
759 "LIBOCTINTERP", OCTAVE_CONF_LIBOCTINTERP, |
|
760 "LIBPLPLOT", OCTAVE_CONF_LIBPLPLOT, |
|
761 "LIBREADLINE", OCTAVE_CONF_LIBREADLINE, |
|
762 "LIBS", OCTAVE_CONF_LIBS, |
|
763 "LN_S", OCTAVE_CONF_LN_S, |
5398
|
764 "MKOCTFILE_DL_LDFLAGS", OCTAVE_CONF_MKOCTFILE_DL_LDFLAGS, |
4357
|
765 "MKOCTFILE_INCFLAGS", OCTAVE_CONF_MKOCTFILE_INCFLAGS, |
|
766 "MKOCTFILE_LFLAGS", OCTAVE_CONF_MKOCTFILE_LFLAGS, |
|
767 "RANLIB", OCTAVE_CONF_RANLIB, |
|
768 "RDYNAMIC_FLAG", OCTAVE_CONF_RDYNAMIC_FLAG, |
|
769 "RLD_FLAG", OCTAVE_CONF_RLD_FLAG, |
|
770 "RUNTEST", OCTAVE_CONF_RUNTEST, |
|
771 "SED", OCTAVE_CONF_SED, |
|
772 "SHARED_LIBS", OCTAVE_CONF_SHARED_LIBS, |
|
773 "SHLEXT", OCTAVE_CONF_SHLEXT, |
|
774 "SHLEXT_VER", OCTAVE_CONF_SHLEXT_VER, |
|
775 "SH_LD", OCTAVE_CONF_SH_LD, |
|
776 "SH_LDFLAGS", OCTAVE_CONF_SH_LDFLAGS, |
|
777 "SONAME_FLAGS", OCTAVE_CONF_SONAME_FLAGS, |
|
778 "STATIC_LIBS", OCTAVE_CONF_STATIC_LIBS, |
|
779 "UGLY_DEFS", OCTAVE_CONF_UGLY_DEFS, |
5275
|
780 "USE_64_BIT_IDX_T", OCTAVE_CONF_USE_64_BIT_IDX_T, |
4357
|
781 "XTRA_CFLAGS", OCTAVE_CONF_XTRA_CFLAGS, |
|
782 "XTRA_CXXFLAGS", OCTAVE_CONF_XTRA_CXXFLAGS, |
|
783 "YACC", OCTAVE_CONF_YACC, |
|
784 "YFLAGS", OCTAVE_CONF_YFLAGS, |
5655
|
785 "api_version", OCTAVE_API_VERSION, |
4357
|
786 "archlibdir", OCTAVE_ARCHLIBDIR, |
|
787 "bindir", OCTAVE_BINDIR, |
|
788 "canonical_host_type", OCTAVE_CANONICAL_HOST_TYPE, |
|
789 "config_opts", OCTAVE_CONF_config_opts, |
|
790 "datadir", OCTAVE_DATADIR, |
|
791 "exec_prefix", OCTAVE_EXEC_PREFIX, |
|
792 "fcnfiledir", OCTAVE_FCNFILEDIR, |
|
793 "imagedir", OCTAVE_IMAGEDIR, |
|
794 "includedir", OCTAVE_INCLUDEDIR, |
|
795 "infodir", OCTAVE_INFODIR, |
|
796 "infofile", OCTAVE_INFOFILE, |
|
797 "libdir", OCTAVE_LIBDIR, |
|
798 "libexecdir", OCTAVE_LIBEXECDIR, |
5398
|
799 "localapifcnfiledir", OCTAVE_LOCALAPIFCNFILEDIR, |
|
800 "localapioctfiledir", OCTAVE_LOCALAPIOCTFILEDIR, |
4357
|
801 "localarchlibdir", OCTAVE_LOCALARCHLIBDIR, |
|
802 "localfcnfiledir", OCTAVE_LOCALFCNFILEDIR, |
|
803 "localoctfiledir", OCTAVE_LOCALOCTFILEDIR, |
|
804 "localstartupfiledir", OCTAVE_LOCALSTARTUPFILEDIR, |
|
805 "localverarchlibdir", OCTAVE_LOCALVERARCHLIBDIR, |
|
806 "localverfcnfiledir", OCTAVE_LOCALVERFCNFILEDIR, |
|
807 "localveroctfiledir", OCTAVE_LOCALVEROCTFILEDIR, |
|
808 "man1dir", OCTAVE_MAN1DIR, |
|
809 "man1ext", OCTAVE_MAN1EXT, |
|
810 "mandir", OCTAVE_MANDIR, |
|
811 "octfiledir", OCTAVE_OCTFILEDIR, |
|
812 "octincludedir", OCTAVE_OCTINCLUDEDIR, |
|
813 "octlibdir", OCTAVE_OCTLIBDIR, |
|
814 "prefix", OCTAVE_PREFIX, |
|
815 "startupfiledir", OCTAVE_STARTUPFILEDIR, |
|
816 "version", OCTAVE_VERSION, |
|
817 0, 0 |
|
818 }; |
|
819 |
|
820 if (! initialized) |
|
821 { |
4675
|
822 m.assign ("dld", octave_value (octave_supports_dynamic_linking)); |
4357
|
823 |
4697
|
824 oct_mach_info::float_format ff = oct_mach_info::native_float_format (); |
|
825 m.assign ("float_format", |
|
826 octave_value (oct_mach_info::float_format_as_string (ff))); |
|
827 |
|
828 m.assign ("words_big_endian", |
|
829 octave_value (oct_mach_info::words_big_endian ())); |
|
830 |
|
831 m.assign ("words_little_endian", |
|
832 octave_value (oct_mach_info::words_little_endian ())); |
|
833 |
4357
|
834 int i = 0; |
4440
|
835 |
|
836 while (true) |
4357
|
837 { |
|
838 const char *key = conf_info[i++]; |
|
839 |
|
840 if (key) |
4675
|
841 m.assign (key, octave_value (conf_info[i++])); |
4357
|
842 else |
|
843 break; |
|
844 } |
|
845 |
4691
|
846 bool unix_system = true; |
|
847 bool windows_system = false; |
|
848 |
|
849 #if defined (WIN32) |
|
850 windows_system = true; |
|
851 #if !defined (__CYGWIN__) |
|
852 unix_system = false; |
|
853 #endif |
|
854 #endif |
|
855 |
|
856 m.assign ("unix", octave_value (unix_system)); |
|
857 m.assign ("windows", octave_value (windows_system)); |
|
858 |
4357
|
859 initialized = true; |
|
860 } |
2162
|
861 |
2689
|
862 int nargin = args.length (); |
|
863 |
|
864 if (nargin == 1) |
|
865 { |
3523
|
866 std::string arg = args(0).string_value (); |
2689
|
867 |
|
868 if (! error_state) |
4675
|
869 { |
|
870 Cell c = m.contents (arg.c_str ()); |
5199
|
871 |
|
872 if (c.is_empty ()) |
|
873 error ("octave_config_info: no info for `%s'", arg.c_str ()); |
|
874 else |
|
875 retval = c(0); |
4675
|
876 } |
2689
|
877 } |
|
878 else if (nargin == 0) |
4233
|
879 retval = m; |
2689
|
880 else |
|
881 print_usage ("octave_config_info"); |
|
882 |
|
883 return retval; |
2162
|
884 } |
|
885 |
1683
|
886 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
2806
|
887 |
1683
|
888 int debug_new_delete = 0; |
|
889 |
|
890 typedef void (*vfp)(void); |
|
891 extern vfp __new_handler; |
|
892 |
|
893 void * |
|
894 __builtin_new (size_t sz) |
|
895 { |
|
896 void *p; |
|
897 |
|
898 /* malloc (0) is unpredictable; avoid it. */ |
|
899 if (sz == 0) |
|
900 sz = 1; |
2800
|
901 p = malloc (sz); |
1683
|
902 while (p == 0) |
|
903 { |
|
904 (*__new_handler) (); |
2800
|
905 p = malloc (sz); |
1683
|
906 } |
|
907 |
|
908 if (debug_new_delete) |
5629
|
909 std::cerr << "__builtin_new: " << p << std::endl; |
1683
|
910 |
|
911 return p; |
|
912 } |
|
913 |
|
914 void |
|
915 __builtin_delete (void *ptr) |
|
916 { |
|
917 if (debug_new_delete) |
5629
|
918 std::cerr << "__builtin_delete: " << ptr << std::endl; |
1683
|
919 |
|
920 if (ptr) |
|
921 free (ptr); |
|
922 } |
2806
|
923 |
1683
|
924 #endif |
|
925 |
|
926 /* |
|
927 ;;; Local Variables: *** |
|
928 ;;; mode: C++ *** |
|
929 ;;; End: *** |
|
930 */ |