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 |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cassert> |
|
28 #include <cstdlib> |
|
29 #include <cstring> |
4221
|
30 #include <new> |
1683
|
31 |
3503
|
32 #include <fstream> |
|
33 #include <iostream> |
1728
|
34 #include <string> |
|
35 |
1683
|
36 #ifdef HAVE_UNISTD_H |
2442
|
37 #ifdef HAVE_SYS_TYPES_H |
1683
|
38 #include <sys/types.h> |
2442
|
39 #endif |
1683
|
40 #include <unistd.h> |
|
41 #endif |
|
42 |
2926
|
43 #include "cmd-edit.h" |
|
44 #include "file-ops.h" |
1683
|
45 #include "lo-error.h" |
2370
|
46 #include "lo-mappers.h" |
4051
|
47 #include "lo-sstream.h" |
3020
|
48 #include "oct-env.h" |
4153
|
49 #include "quit.h" |
1755
|
50 #include "str-vec.h" |
1683
|
51 |
2492
|
52 #include <defaults.h> |
1683
|
53 #include "defun.h" |
|
54 #include "error.h" |
|
55 #include "file-io.h" |
|
56 #include "input.h" |
|
57 #include "lex.h" |
2492
|
58 #include <oct-conf.h> |
1742
|
59 #include "oct-hist.h" |
2162
|
60 #include "oct-map.h" |
2862
|
61 #include "oct-obj.h" |
1683
|
62 #include "pager.h" |
|
63 #include "parse.h" |
|
64 #include "pathsearch.h" |
|
65 #include "procstream.h" |
2370
|
66 #include "ov.h" |
2985
|
67 #include "pt-jump.h" |
1750
|
68 #include "pt-plot.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 |
|
92 // Pointer to function that is currently being evaluated. |
2891
|
93 octave_user_function *curr_function = 0; |
1683
|
94 |
4238
|
95 // Pointer to parent function that is currently being evaluated. |
|
96 octave_user_function *curr_parent_function = 0; |
|
97 |
3834
|
98 // Original value of TEXMFDBS environment variable. |
|
99 std::string octave_original_texmfdbs; |
|
100 |
4180
|
101 static void |
|
102 recover_from_exception (void) |
|
103 { |
|
104 unwind_protect::run_all (); |
|
105 can_interrupt = true; |
4182
|
106 octave_interrupt_immediately = 0; |
4180
|
107 octave_interrupt_state = 0; |
|
108 octave_allocation_error = 0; |
|
109 octave_restore_signal_mask (); |
|
110 octave_catch_interrupts (); |
|
111 } |
|
112 |
1907
|
113 int |
|
114 main_loop (void) |
|
115 { |
2016
|
116 octave_save_signal_mask (); |
|
117 |
4153
|
118 if (octave_set_current_context) |
1907
|
119 { |
4153
|
120 #if defined (USE_EXCEPTIONS_FOR_INTERRUPTS) |
|
121 panic_impossible (); |
|
122 #else |
|
123 unwind_protect::run_all (); |
1907
|
124 raw_mode (0); |
3531
|
125 std::cout << "\n"; |
2016
|
126 octave_restore_signal_mask (); |
4153
|
127 #endif |
1907
|
128 } |
|
129 |
3020
|
130 can_interrupt = true; |
1907
|
131 |
2554
|
132 octave_catch_interrupts (); |
1907
|
133 |
4172
|
134 octave_initialized = true; |
|
135 |
1907
|
136 // The big loop. |
|
137 |
4153
|
138 int retval = 0; |
1907
|
139 do |
|
140 { |
4180
|
141 try |
1907
|
142 { |
4153
|
143 curr_sym_tab = top_level_sym_tab; |
2620
|
144 |
4318
|
145 reset_error_handler (); |
|
146 |
4153
|
147 reset_parser (); |
2620
|
148 |
4153
|
149 retval = yyparse (); |
2620
|
150 |
4153
|
151 if (retval == 0) |
|
152 { |
|
153 if (global_command) |
3804
|
154 { |
4153
|
155 global_command->eval (); |
3883
|
156 |
4153
|
157 delete global_command; |
3883
|
158 |
4153
|
159 global_command = 0; |
3883
|
160 |
4171
|
161 OCTAVE_QUIT; |
|
162 |
3883
|
163 if (! (interactive || forced_interactive)) |
|
164 { |
4207
|
165 bool quit = (tree_return_command::returning |
|
166 || tree_break_command::breaking); |
4153
|
167 |
4207
|
168 if (tree_return_command::returning) |
|
169 tree_return_command::returning = 0; |
4153
|
170 |
4207
|
171 if (tree_break_command::breaking) |
|
172 tree_break_command::breaking--; |
4153
|
173 |
|
174 if (quit) |
|
175 break; |
|
176 } |
|
177 |
|
178 if (error_state) |
|
179 { |
|
180 if (! (interactive || forced_interactive)) |
|
181 { |
|
182 // We should exit with a non-zero status. |
|
183 retval = 1; |
|
184 break; |
|
185 } |
|
186 } |
|
187 else |
|
188 { |
|
189 if (octave_completion_matches_called) |
|
190 octave_completion_matches_called = false; |
|
191 else |
|
192 command_editor::increment_current_command_number (); |
3883
|
193 } |
|
194 } |
4153
|
195 else if (parser_end_of_input) |
|
196 break; |
2620
|
197 } |
4153
|
198 } |
4182
|
199 catch (octave_interrupt_exception) |
4153
|
200 { |
4180
|
201 recover_from_exception (); |
4155
|
202 std::cout << "\n"; |
4180
|
203 } |
4181
|
204 catch (std::bad_alloc) |
4180
|
205 { |
|
206 recover_from_exception (); |
|
207 std::cerr |
|
208 << "error: memory exhausted -- trying to return to prompt\n"; |
1907
|
209 } |
|
210 } |
|
211 while (retval == 0); |
|
212 |
|
213 return retval; |
|
214 } |
|
215 |
1683
|
216 // Fix up things before exiting. |
|
217 |
|
218 void |
3162
|
219 clean_up_and_exit (int retval) |
|
220 { |
3216
|
221 do_octave_atexit (); |
1683
|
222 |
3162
|
223 exit (retval == EOF ? 0 : retval); |
1683
|
224 } |
|
225 |
4208
|
226 DEFCMD (casesen, args, , |
3446
|
227 "-*- texinfo -*-\n\ |
|
228 @deffn {Command} casesen arg\n\ |
|
229 Provided for compatibility with Matlab, but does nothing.\n\ |
|
230 @end deffn") |
1683
|
231 { |
2086
|
232 octave_value_list retval; |
1683
|
233 |
1755
|
234 int argc = args.length () + 1; |
|
235 |
1965
|
236 string_vector argv = args.make_argv ("casesen"); |
1683
|
237 |
1755
|
238 if (error_state) |
|
239 return retval; |
|
240 |
|
241 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
242 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
243 else if (argc > 1 && argv[1] == "on") |
1683
|
244 ; // ok. |
|
245 else |
|
246 print_usage ("casesen"); |
|
247 |
|
248 return retval; |
|
249 } |
|
250 |
1957
|
251 DEFUN (computer, args, nargout, |
3301
|
252 "-*- texinfo -*-\n\ |
|
253 @deftypefn {Built-in Function} {} computer ()\n\ |
|
254 Print or return a string of the form @var{cpu}-@var{vendor}-@var{os}\n\ |
|
255 that identifies the kind of computer Octave is running on. If invoked\n\ |
|
256 with an output argument, the value is returned instead of printed. For\n\ |
|
257 example,\n\ |
1683
|
258 \n\ |
3301
|
259 @example\n\ |
|
260 @group\n\ |
|
261 computer ()\n\ |
|
262 @print{} i586-pc-linux-gnu\n\ |
|
263 \n\ |
|
264 x = computer ()\n\ |
|
265 @result{} x = \"i586-pc-linux-gnu\"\n\ |
|
266 @end group\n\ |
|
267 @end example\n\ |
|
268 @end deftypefn") |
1683
|
269 { |
4233
|
270 octave_value retval; |
1683
|
271 |
|
272 int nargin = args.length (); |
|
273 |
|
274 if (nargin != 0) |
|
275 warning ("computer: ignoring extra arguments"); |
|
276 |
3523
|
277 std::string msg; |
1683
|
278 |
3584
|
279 if (strcmp (OCTAVE_CANONICAL_HOST_TYPE, "unknown") == 0) |
2095
|
280 msg = "Hi Dave, I'm a HAL-9000"; |
1683
|
281 else |
3584
|
282 msg = OCTAVE_CANONICAL_HOST_TYPE; |
1683
|
283 |
|
284 if (nargout == 0) |
2095
|
285 octave_stdout << msg << "\n"; |
1683
|
286 else |
2095
|
287 retval = msg; |
1683
|
288 |
|
289 return retval; |
|
290 } |
|
291 |
3180
|
292 DEFUN (quit, args, nargout, |
3332
|
293 "-*- texinfo -*-\n\ |
|
294 @deftypefn {Built-in Function} {} exit (@var{status})\n\ |
|
295 @deftypefnx {Built-in Function} {} quit (@var{status})\n\ |
|
296 Exit the current Octave session. If the optional integer value\n\ |
|
297 @var{status} is supplied, pass that value to the operating system as the\n\ |
|
298 Octave's exit status.\n\ |
3333
|
299 @end deftypefn") |
1683
|
300 { |
2086
|
301 octave_value_list retval; |
2068
|
302 |
3180
|
303 if (nargout == 0) |
2068
|
304 { |
3180
|
305 int exit_status = 0; |
|
306 |
|
307 quitting_gracefully = true; |
1683
|
308 |
3180
|
309 if (args.length () > 0) |
|
310 { |
3202
|
311 int tmp = args(0).nint_value (); |
1683
|
312 |
3202
|
313 if (! error_state) |
|
314 exit_status = tmp; |
3180
|
315 } |
|
316 |
|
317 clean_up_and_exit (exit_status); |
2068
|
318 } |
3180
|
319 else |
|
320 error ("quit: invalid number of output arguments"); |
2068
|
321 |
1683
|
322 return retval; |
|
323 } |
|
324 |
|
325 DEFALIAS (exit, quit); |
|
326 |
1957
|
327 DEFUN (warranty, , , |
3446
|
328 "-*- texinfo -*-\n\ |
|
329 @deftypefn {Built-in Function} {} warranty ()\n\ |
|
330 Describe the conditions for copying and distributing Octave.\n\ |
|
331 @end deftypefn") |
1683
|
332 { |
2086
|
333 octave_value_list retval; |
1683
|
334 |
3922
|
335 octave_stdout << "\n" \ |
|
336 OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\ |
|
337 \n\ |
1683
|
338 This program is free software; you can redistribute it and/or modify\n\ |
|
339 it under the terms of the GNU General Public License as published by\n\ |
|
340 the Free Software Foundation; either version 2 of the License, or\n\ |
|
341 (at your option) any later version.\n\ |
|
342 \n\ |
|
343 This program is distributed in the hope that it will be useful,\n\ |
|
344 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
345 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
346 GNU General Public License for more details.\n\ |
|
347 \n\ |
|
348 You should have received a copy of the GNU General Public License\n\ |
|
349 along with this program. If not, write to the Free Software\n\ |
|
350 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
351 \n"; |
|
352 |
|
353 return retval; |
|
354 } |
|
355 |
|
356 // Execute a shell command. |
|
357 |
1965
|
358 static void |
|
359 cleanup_iprocstream (void *p) |
|
360 { |
3060
|
361 iprocstream *cmd = static_cast<iprocstream *> (p); |
|
362 |
|
363 octave_child_list::remove (cmd->pid ()); |
|
364 |
|
365 delete cmd; |
1965
|
366 } |
|
367 |
2086
|
368 static octave_value_list |
3523
|
369 run_command_and_return_output (const std::string& cmd_str) |
2083
|
370 { |
2086
|
371 octave_value_list retval; |
2083
|
372 |
|
373 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); |
|
374 |
3060
|
375 if (cmd) |
2083
|
376 { |
3060
|
377 unwind_protect::add (cleanup_iprocstream, cmd); |
|
378 |
|
379 if (*cmd) |
|
380 { |
4051
|
381 OSSTREAM output_buf; |
2095
|
382 |
3147
|
383 // XXX FIXME XXX -- sometimes, the subprocess hasn't written |
|
384 // anything before we try to read from the procstream. The |
|
385 // kluge below (simply waiting and trying again) is ugly, |
|
386 // but it seems to work, at least most of the time. It |
|
387 // could probably still fail if the subprocess hasn't |
|
388 // started writing after the snooze. Isn't there a better |
|
389 // way? If there is, you should also fix the code for the |
|
390 // ls function in dirfns.cc. |
|
391 |
3060
|
392 char ch; |
3147
|
393 |
|
394 if (cmd->get (ch)) |
|
395 output_buf.put (ch); |
|
396 else |
|
397 { |
|
398 cmd->clear (); |
|
399 |
3308
|
400 octave_usleep (100); |
3147
|
401 } |
|
402 |
3060
|
403 while (cmd->get (ch)) |
|
404 output_buf.put (ch); |
2083
|
405 |
3252
|
406 int cmd_status = cmd->close (); |
2083
|
407 |
3060
|
408 if (WIFEXITED (cmd_status)) |
|
409 cmd_status = WEXITSTATUS (cmd_status); |
|
410 else |
|
411 cmd_status = 127; |
2083
|
412 |
4051
|
413 output_buf << OSSTREAM_ENDS; |
2083
|
414 |
3060
|
415 retval(1) = (double) cmd_status; |
4051
|
416 retval(0) = OSSTREAM_STR (output_buf); |
2083
|
417 |
4051
|
418 OSSTREAM_FREEZE (output_buf); |
3060
|
419 } |
|
420 |
|
421 unwind_protect::run (); |
2083
|
422 } |
|
423 else |
|
424 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); |
|
425 |
|
426 return retval; |
|
427 } |
|
428 |
3834
|
429 static void |
|
430 restore_texmfdbs_envvar (void *ptr) |
|
431 { |
|
432 std::string *s = static_cast<std::string *> (ptr); |
|
433 |
|
434 octave_env::putenv ("TEXMFDBS", *s); |
|
435 } |
|
436 |
1957
|
437 DEFUN (system, args, nargout, |
3301
|
438 "-*- texinfo -*-\n\ |
|
439 @deftypefn {Built-in Function} {} system (@var{string}, @var{return_output}, @var{type})\n\ |
|
440 Execute a shell command specified by @var{string}. The second\n\ |
|
441 argument is optional. If @var{type} is @code{\"async\"}, the process\n\ |
|
442 is started in the background and the process id of the child process\n\ |
|
443 is returned immediately. Otherwise, the process is started, and\n\ |
|
444 Octave waits until it exits. If @var{type} argument is omitted, a\n\ |
|
445 value of @code{\"sync\"} is assumed.\n\ |
2083
|
446 \n\ |
3301
|
447 If two input arguments are given (the actual value of\n\ |
|
448 @var{return_output} is irrelevant) and the subprocess is started\n\ |
|
449 synchronously, or if @var{system} is called with one input argument and\n\ |
|
450 one or more output arguments, the output from the command is returned.\n\ |
2083
|
451 Otherwise, if the subprocess is executed synchronously, it's output is\n\ |
2321
|
452 sent to the standard output. To send the output of a command executed\n\ |
3301
|
453 with @var{system} through the pager, use a command like\n\ |
2321
|
454 \n\ |
3301
|
455 @example\n\ |
|
456 disp (system (cmd, 1));\n\ |
|
457 @end example\n\ |
2321
|
458 \n\ |
3301
|
459 @noindent\n\ |
2321
|
460 or\n\ |
|
461 \n\ |
3301
|
462 @example\n\ |
|
463 printf (\"%s\n\", system (cmd, 1));\n\ |
|
464 @end example\n\ |
|
465 \n\ |
|
466 The @code{system} function can return two values. The first is any\n\ |
|
467 output from the command that was written to the standard output stream,\n\ |
|
468 and the second is the output status of the command. For example,\n\ |
|
469 \n\ |
|
470 @example\n\ |
|
471 [output, status] = system (\"echo foo; exit 2\");\n\ |
|
472 @end example\n\ |
|
473 \n\ |
|
474 @noindent\n\ |
|
475 will set the variable @code{output} to the string @samp{foo}, and the\n\ |
|
476 variable @code{status} to the integer @samp{2}.\n\ |
|
477 @end deftypefn") |
1683
|
478 { |
2086
|
479 octave_value_list retval; |
1683
|
480 |
3834
|
481 unwind_protect::begin_frame ("Fsystem"); |
|
482 |
1683
|
483 int nargin = args.length (); |
|
484 |
2083
|
485 if (nargin > 0 && nargin < 4) |
1683
|
486 { |
2083
|
487 bool return_output = (nargout > 0 || nargin > 1); |
|
488 |
3523
|
489 std::string cmd_str = args(0).string_value (); |
2083
|
490 |
|
491 enum exec_type { sync, async }; |
|
492 |
|
493 exec_type type = sync; |
|
494 |
|
495 if (! error_state) |
|
496 { |
|
497 if (nargin > 2) |
|
498 { |
3523
|
499 std::string type_str = args(2).string_value (); |
1683
|
500 |
2083
|
501 if (! error_state) |
|
502 { |
|
503 if (type_str == "sync") |
|
504 type = sync; |
|
505 else if (type_str == "async") |
|
506 type = async; |
|
507 else |
|
508 error ("system: third arg must be \"sync\" or \"async\""); |
|
509 } |
|
510 else |
|
511 error ("system: third argument must be a string"); |
|
512 } |
|
513 } |
|
514 else |
3523
|
515 error ("system: expecting std::string as first argument"); |
1683
|
516 |
2083
|
517 if (! error_state) |
|
518 { |
3834
|
519 // The value of TEXMFDBS that Octave puts in the environment |
|
520 // will cause trouble if we are asked to run TeX, so we |
|
521 // should reset it to whatever it was before Octave started. |
|
522 // |
|
523 // XXX FIXME XXX -- it would be better to fix the |
|
524 // kpathsearch library to not always do TeX-specific |
|
525 // things... |
|
526 |
3841
|
527 static std::string odb; |
3834
|
528 |
|
529 odb = octave_env::getenv ("TEXMFDBS"); |
|
530 |
|
531 unwind_protect::add (restore_texmfdbs_envvar, &odb); |
|
532 |
|
533 octave_env::putenv ("TEXMFDBS", octave_original_texmfdbs); |
|
534 |
2083
|
535 if (type == async) |
|
536 { |
4086
|
537 #ifdef HAVE_FORK |
2083
|
538 pid_t pid = fork (); |
|
539 |
|
540 if (pid < 0) |
|
541 error ("system: fork failed -- can't create child process"); |
|
542 else if (pid == 0) |
|
543 { |
3273
|
544 // XXX FIXME XXX -- should probably replace this |
|
545 // call with something portable. |
|
546 |
|
547 execl ("/bin/sh", "sh", "-c", cmd_str.c_str (), 0); |
|
548 |
|
549 panic_impossible (); |
2083
|
550 } |
|
551 else |
4254
|
552 retval(0) = pid; |
4086
|
553 #else |
|
554 error ("asynchronous system calls are not supported"); |
|
555 #endif |
2083
|
556 } |
2321
|
557 else if (return_output) |
|
558 retval = run_command_and_return_output (cmd_str); |
2083
|
559 else |
2321
|
560 { |
|
561 int status = system (cmd_str.c_str ()); |
|
562 |
|
563 // The value in status is as returned by waitpid. If |
|
564 // the process exited normally, extract the actual exit |
|
565 // status of the command. Otherwise, return 127 as a |
|
566 // failure code. |
|
567 |
2693
|
568 if (WIFEXITED (status)) |
|
569 status = WEXITSTATUS (status); |
2321
|
570 |
4233
|
571 retval(0) = status; |
2321
|
572 } |
2083
|
573 } |
1683
|
574 } |
|
575 else |
2083
|
576 print_usage ("system"); |
1683
|
577 |
3834
|
578 unwind_protect::run_frame ("Fsystem"); |
|
579 |
1683
|
580 return retval; |
|
581 } |
|
582 |
|
583 DEFALIAS (shell_cmd, system); |
|
584 |
2614
|
585 // XXX FIXME XXX -- this should really be static, but that causes |
|
586 // problems on some systems. |
4214
|
587 std::stack<std::string> octave_atexit_functions; |
2077
|
588 |
|
589 void |
|
590 do_octave_atexit (void) |
|
591 { |
3216
|
592 static bool deja_vu = false; |
|
593 |
2077
|
594 while (! octave_atexit_functions.empty ()) |
|
595 { |
4233
|
596 std::string fcn = octave_atexit_functions.top (); |
4214
|
597 |
|
598 octave_atexit_functions.pop (); |
2077
|
599 |
4233
|
600 feval (fcn, octave_value_list (), 0); |
3215
|
601 |
|
602 flush_octave_stdout (); |
2077
|
603 } |
3216
|
604 |
|
605 if (! deja_vu) |
|
606 { |
|
607 deja_vu = true; |
|
608 |
|
609 command_editor::restore_terminal_state (); |
|
610 |
|
611 // XXX FIXME XXX -- is this needed? Can it cause any trouble? |
|
612 raw_mode (0); |
|
613 |
|
614 command_history::clean_up_and_save (); |
|
615 |
|
616 close_plot_stream (); |
|
617 |
|
618 close_files (); |
|
619 |
|
620 cleanup_tmp_files (); |
|
621 |
|
622 flush_octave_stdout (); |
|
623 |
|
624 if (!quitting_gracefully && (interactive || forced_interactive)) |
3531
|
625 std::cout << "\n"; |
3216
|
626 } |
2077
|
627 } |
|
628 |
2162
|
629 DEFUN (atexit, args, , |
3332
|
630 "-*- texinfo -*-\n\ |
|
631 @deftypefn {Built-in Function} {} atexit (@var{fcn})\n\ |
|
632 Register a function to be called when Octave exits. For example,\n\ |
2077
|
633 \n\ |
3332
|
634 @example\n\ |
|
635 @group\n\ |
3686
|
636 function print_fortune ()\n\ |
3332
|
637 printf (\"\\n%s\\n\", system (\"fortune\"));\n\ |
|
638 fflush (stdout);\n\ |
|
639 endfunction\n\ |
3686
|
640 atexit (\"print_fortune\");\n\ |
3332
|
641 @end group\n\ |
|
642 @end example\n\ |
|
643 \n\ |
|
644 @noindent\n\ |
|
645 will print a message when Octave exits.\n\ |
3333
|
646 @end deftypefn") |
2077
|
647 { |
2086
|
648 octave_value_list retval; |
2077
|
649 |
|
650 int nargin = args.length (); |
|
651 |
|
652 if (nargin == 1) |
|
653 { |
2475
|
654 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
3523
|
655 std::string arg = args(0).string_value (); |
2077
|
656 |
|
657 if (! error_state) |
|
658 octave_atexit_functions.push (arg); |
|
659 else |
|
660 error ("atexit: argument must be a string"); |
2475
|
661 #else |
|
662 gripe_not_supported ("atexit"); |
|
663 #endif |
2077
|
664 } |
|
665 else |
|
666 print_usage ("atexit"); |
|
667 |
|
668 return retval; |
|
669 } |
|
670 |
2689
|
671 DEFUN (octave_config_info, args, , |
3301
|
672 "-*- texinfo -*-\n\ |
|
673 @deftypefn {Built-in Function} {} octave_config_info (@var{option})\n\ |
|
674 Return a structure containing configuration and installation\n\ |
|
675 information for Octave.\n\ |
2689
|
676 \n\ |
3301
|
677 if @var{option} is a string, return the configuration information for the\n\ |
2689
|
678 specified option.\n\ |
|
679 \n\ |
3301
|
680 @end deftypefn") |
2162
|
681 { |
2689
|
682 octave_value retval; |
|
683 |
4128
|
684 #if defined (ENABLE_DYNAMIC_LINKING) |
2689
|
685 bool octave_supports_dynamic_linking = true; |
|
686 #else |
|
687 bool octave_supports_dynamic_linking = false; |
|
688 #endif |
|
689 |
2162
|
690 Octave_map m; |
|
691 |
4233
|
692 m ["ALL_CFLAGS"](0) = OCTAVE_CONF_ALL_CFLAGS; |
|
693 m ["ALL_CXXFLAGS"](0) = OCTAVE_CONF_ALL_CXXFLAGS; |
|
694 m ["ALL_FFLAGS"](0) = OCTAVE_CONF_ALL_FFLAGS; |
|
695 m ["ALL_LDFLAGS"](0) = OCTAVE_CONF_ALL_LDFLAGS; |
|
696 m ["AR"](0) = OCTAVE_CONF_AR; |
|
697 m ["ARFLAGS"](0) = OCTAVE_CONF_ARFLAGS; |
|
698 m ["BLAS_LIBS"](0) = OCTAVE_CONF_BLAS_LIBS; |
|
699 m ["CC"](0) = OCTAVE_CONF_CC; |
|
700 m ["CC_VERSION"](0) = OCTAVE_CONF_CC_VERSION; |
|
701 m ["CFLAGS"](0) = OCTAVE_CONF_CFLAGS; |
|
702 m ["CPICFLAG"](0) = OCTAVE_CONF_CPICFLAG; |
|
703 m ["CPPFLAGS"](0) = OCTAVE_CONF_CPPFLAGS; |
|
704 m ["CXX"](0) = OCTAVE_CONF_CXX; |
|
705 m ["CXXCPP"](0) = OCTAVE_CONF_CXXCPP; |
|
706 m ["CXXFLAGS"](0) = OCTAVE_CONF_CXXFLAGS; |
|
707 m ["CXXPICFLAG"](0) = OCTAVE_CONF_CXXPICFLAG; |
|
708 m ["CXX_VERSION"](0) = OCTAVE_CONF_CXX_VERSION; |
|
709 m ["DEFAULT_PAGER"](0) = OCTAVE_DEFAULT_PAGER; |
|
710 m ["DLFCN_INCFLAGS"](0) = OCTAVE_CONF_DLFCN_INCFLAGS; |
|
711 m ["EXEEXT"](0) = OCTAVE_CONF_EXEEXT; |
|
712 m ["F2C"](0) = OCTAVE_CONF_F2C; |
|
713 m ["F2CFLAGS"](0) = OCTAVE_CONF_F2CFLAGS; |
|
714 m ["F77"](0) = OCTAVE_CONF_F77; |
|
715 m ["FC"](0) = OCTAVE_CONF_FC; |
|
716 m ["FFLAGS"](0) = OCTAVE_CONF_FFLAGS; |
|
717 m ["FFTW_LIBS"](0) = OCTAVE_CONF_FFTW_LIBS; |
|
718 m ["FLIBS"](0) = OCTAVE_CONF_FLIBS; |
|
719 m ["FPICFLAG"](0) = OCTAVE_CONF_FPICFLAG; |
|
720 m ["GLOB_INCFLAGS"](0) = OCTAVE_CONF_GLOB_INCFLAGS; |
|
721 m ["INCFLAGS"](0) = OCTAVE_CONF_INCFLAGS; |
|
722 m ["LDFLAGS"](0) = OCTAVE_CONF_LDFLAGS; |
|
723 m ["LD_CXX"](0) = OCTAVE_CONF_LD_CXX; |
|
724 m ["LD_STATIC_FLAG"](0) = OCTAVE_CONF_LD_STATIC_FLAG; |
|
725 m ["LEX"](0) = OCTAVE_CONF_LEX; |
|
726 m ["LEXLIB"](0) = OCTAVE_CONF_LEXLIB; |
|
727 m ["LFLAGS"](0) = OCTAVE_CONF_LFLAGS; |
|
728 m ["LIBCRUFT"](0) = OCTAVE_CONF_LIBCRUFT; |
|
729 m ["LIBDLFCN"](0) = OCTAVE_CONF_LIBDLFCN; |
|
730 m ["LIBEXT"](0) = OCTAVE_CONF_LIBEXT; |
|
731 m ["LIBFLAGS"](0) = OCTAVE_CONF_LIBFLAGS; |
|
732 m ["LIBGLOB"](0) = OCTAVE_CONF_LIBGLOB; |
|
733 m ["LIBKPATHSEA"](0) = OCTAVE_CONF_LIBKPATHSEA; |
|
734 m ["LIBOCTAVE"](0) = OCTAVE_CONF_LIBOCTAVE; |
|
735 m ["LIBOCTINTERP"](0) = OCTAVE_CONF_LIBOCTINTERP; |
|
736 m ["LIBPLPLOT"](0) = OCTAVE_CONF_LIBPLPLOT; |
|
737 m ["LIBREADLINE"](0) = OCTAVE_CONF_LIBREADLINE; |
|
738 m ["LIBS"](0) = OCTAVE_CONF_LIBS; |
|
739 m ["LN_S"](0) = OCTAVE_CONF_LN_S; |
|
740 m ["MKOCTFILE_INCFLAGS"](0) = OCTAVE_CONF_MKOCTFILE_INCFLAGS; |
|
741 m ["MKOCTFILE_LFLAGS"](0) = OCTAVE_CONF_MKOCTFILE_LFLAGS; |
|
742 m ["MKOCTFILE_SH_LDFLAGS"](0) = OCTAVE_CONF_MKOCTFILE_SH_LDFLAGS; |
|
743 m ["RANLIB"](0) = OCTAVE_CONF_RANLIB; |
|
744 m ["RDYNAMIC_FLAG"](0) = OCTAVE_CONF_RDYNAMIC_FLAG; |
|
745 m ["RLD_FLAG"](0) = OCTAVE_CONF_RLD_FLAG; |
|
746 m ["RUNTEST"](0) = OCTAVE_CONF_RUNTEST; |
|
747 m ["SED"](0) = OCTAVE_CONF_SED; |
|
748 m ["SHARED_LIBS"](0) = OCTAVE_CONF_SHARED_LIBS; |
|
749 m ["SHLEXT"](0) = OCTAVE_CONF_SHLEXT; |
|
750 m ["SHLEXT_VER"](0) = OCTAVE_CONF_SHLEXT_VER; |
|
751 m ["SH_LD"](0) = OCTAVE_CONF_SH_LD; |
|
752 m ["SH_LDFLAGS"](0) = OCTAVE_CONF_SH_LDFLAGS; |
|
753 m ["SONAME_FLAGS"](0) = OCTAVE_CONF_SONAME_FLAGS; |
|
754 m ["STATIC_LIBS"](0) = OCTAVE_CONF_STATIC_LIBS; |
|
755 m ["UGLY_DEFS"](0) = OCTAVE_CONF_DEFS; |
|
756 m ["UGLY_DEFS"](0) = OCTAVE_CONF_UGLY_DEFS; |
|
757 m ["ENABLE_DYNAMIC_LINKING"](0) = OCTAVE_CONF_ENABLE_DYNAMIC_LINKING; |
|
758 m ["XTRA_CFLAGS"](0) = OCTAVE_CONF_XTRA_CFLAGS; |
|
759 m ["XTRA_CXXFLAGS"](0) = OCTAVE_CONF_XTRA_CXXFLAGS; |
|
760 m ["YACC"](0) = OCTAVE_CONF_YACC; |
|
761 m ["YFLAGS"](0) = OCTAVE_CONF_YFLAGS; |
|
762 m ["archlibdir"](0) = OCTAVE_ARCHLIBDIR; |
|
763 m ["bindir"](0) = OCTAVE_BINDIR; |
|
764 m ["canonical_host_type"](0) = OCTAVE_CANONICAL_HOST_TYPE; |
|
765 m ["config_opts"](0) = OCTAVE_CONF_config_opts; |
|
766 m ["datadir"](0) = OCTAVE_DATADIR; |
|
767 m ["dld"](0) = octave_supports_dynamic_linking; |
|
768 m ["exec_prefix"](0) = OCTAVE_EXEC_PREFIX; |
|
769 m ["fcnfiledir"](0) = OCTAVE_FCNFILEDIR; |
|
770 m ["fcnfilepath"](0) = OCTAVE_FCNFILEPATH; |
|
771 m ["imagedir"](0) = OCTAVE_IMAGEDIR; |
|
772 m ["imagepath"](0) = OCTAVE_IMAGEPATH; |
|
773 m ["includedir"](0) = OCTAVE_INCLUDEDIR; |
|
774 m ["infodir"](0) = OCTAVE_INFODIR; |
|
775 m ["infofile"](0) = OCTAVE_INFOFILE; |
|
776 m ["libdir"](0) = OCTAVE_LIBDIR; |
|
777 m ["libexecdir"](0) = OCTAVE_LIBEXECDIR; |
|
778 m ["localarchlibdir"](0) = OCTAVE_LOCALARCHLIBDIR; |
|
779 m ["localfcnfiledir"](0) = OCTAVE_LOCALFCNFILEDIR; |
|
780 m ["localfcnfilepath"](0) = OCTAVE_LOCALFCNFILEPATH; |
|
781 m ["localoctfiledir"](0) = OCTAVE_LOCALOCTFILEDIR; |
|
782 m ["localoctfilepath"](0) = OCTAVE_LOCALOCTFILEPATH; |
|
783 m ["localstartupfiledir"](0) = OCTAVE_LOCALSTARTUPFILEDIR; |
|
784 m ["localverarchlibdir"](0) = OCTAVE_LOCALVERARCHLIBDIR; |
|
785 m ["localverfcnfiledir"](0) = OCTAVE_LOCALVERFCNFILEDIR; |
|
786 m ["localveroctfiledir"](0) = OCTAVE_LOCALVEROCTFILEDIR; |
|
787 m ["man1dir"](0) = OCTAVE_MAN1DIR; |
|
788 m ["man1ext"](0) = OCTAVE_MAN1EXT; |
|
789 m ["mandir"](0) = OCTAVE_MANDIR; |
|
790 m ["octfiledir"](0) = OCTAVE_OCTFILEDIR; |
|
791 m ["octincludedir"](0) = OCTAVE_OCTINCLUDEDIR; |
|
792 m ["octlibdir"](0) = OCTAVE_OCTLIBDIR; |
|
793 m ["prefix"](0) = OCTAVE_PREFIX; |
|
794 m ["startupfiledir"](0) = OCTAVE_STARTUPFILEDIR; |
|
795 m ["version"](0) = OCTAVE_VERSION; |
2162
|
796 |
2689
|
797 int nargin = args.length (); |
|
798 |
|
799 if (nargin == 1) |
|
800 { |
3523
|
801 std::string arg = args(0).string_value (); |
2689
|
802 |
|
803 if (! error_state) |
4233
|
804 retval = m [arg.c_str ()](0); |
2689
|
805 } |
|
806 else if (nargin == 0) |
4233
|
807 retval = m; |
2689
|
808 else |
|
809 print_usage ("octave_config_info"); |
|
810 |
|
811 return retval; |
2162
|
812 } |
|
813 |
1683
|
814 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
2806
|
815 |
1683
|
816 int debug_new_delete = 0; |
|
817 |
|
818 typedef void (*vfp)(void); |
|
819 extern vfp __new_handler; |
|
820 |
|
821 void * |
|
822 __builtin_new (size_t sz) |
|
823 { |
|
824 void *p; |
|
825 |
|
826 /* malloc (0) is unpredictable; avoid it. */ |
|
827 if (sz == 0) |
|
828 sz = 1; |
2800
|
829 p = malloc (sz); |
1683
|
830 while (p == 0) |
|
831 { |
|
832 (*__new_handler) (); |
2800
|
833 p = malloc (sz); |
1683
|
834 } |
|
835 |
|
836 if (debug_new_delete) |
3538
|
837 std::cout << "__builtin_new: " << p << std::endl; |
1683
|
838 |
|
839 return p; |
|
840 } |
|
841 |
|
842 void |
|
843 __builtin_delete (void *ptr) |
|
844 { |
|
845 if (debug_new_delete) |
3538
|
846 std::cout << "__builtin_delete: " << ptr << std::endl; |
1683
|
847 |
|
848 if (ptr) |
|
849 free (ptr); |
|
850 } |
2806
|
851 |
1683
|
852 #endif |
|
853 |
2806
|
854 void |
|
855 symbols_of_toplev (void) |
|
856 { |
3141
|
857 DEFCONST (argv, , |
3332
|
858 "-*- texinfo -*-\n\ |
|
859 @defvr {Built-in Variable} argv\n\ |
|
860 The command line arguments passed to Octave are available in this\n\ |
|
861 variable. For example, if you invoked Octave using the command\n\ |
|
862 \n\ |
|
863 @example\n\ |
|
864 octave --no-line-editing --silent\n\ |
|
865 @end example\n\ |
|
866 \n\ |
|
867 @noindent\n\ |
3971
|
868 @code{argv} would be a cell array of strings with the elements\n\ |
3332
|
869 @code{--no-line-editing} and @code{--silent}.\n\ |
|
870 \n\ |
|
871 If you write an executable Octave script, @code{argv} will contain the\n\ |
3402
|
872 list of arguments passed to the script. @xref{Executable Octave Programs},\n\ |
|
873 for an example of how to create an executable Octave script.\n\ |
3333
|
874 @end defvr"); |
2806
|
875 |
3020
|
876 DEFCONST (program_invocation_name, |
3141
|
877 octave_env::get_program_invocation_name (), |
3332
|
878 "-*- texinfo -*-\n\ |
|
879 @defvr {Built-in Variable} program_invocation_name\n\ |
|
880 @defvrx {Built-in Variable} program_name\n\ |
|
881 When Octave starts, the value of the built-in variable\n\ |
|
882 @code{program_invocation_name} is automatically set to the name that was\n\ |
|
883 typed at the shell prompt to run Octave, and the value of\n\ |
|
884 @code{program_name} is automatically set to the final component of\n\ |
|
885 @code{program_invocation_name}. For example, if you typed\n\ |
|
886 @samp{@value{OCTAVEHOME}/bin/octave} to start Octave,\n\ |
|
887 @code{program_invocation_name} would have the value\n\ |
|
888 @code{\"@value{OCTAVEHOME}/bin/octave\"}, and @code{program_name} would\n\ |
|
889 have the value @code{\"octave\"}.\n\ |
|
890 \n\ |
|
891 If executing a script from the command line (e.g., @code{octave foo.m})\n\ |
|
892 or using an executable Octave script, the program name is set to the\n\ |
3402
|
893 name of the script. @xref{Executable Octave Programs}, for an example of\n\ |
3332
|
894 how to create an executable Octave script.\n\ |
3333
|
895 @end defvr"); |
3020
|
896 |
3141
|
897 DEFCONST (program_name, octave_env::get_program_name (), |
3332
|
898 "-*- texinfo -*-\n\ |
|
899 @defvr {Built-in Variable} program_invocation_name\n\ |
|
900 @defvrx {Built-in Variable} program_name\n\ |
|
901 When Octave starts, the value of the built-in variable\n\ |
|
902 @code{program_invocation_name} is automatically set to the name that was\n\ |
|
903 typed at the shell prompt to run Octave, and the value of\n\ |
|
904 @code{program_name} is automatically set to the final component of\n\ |
|
905 @code{program_invocation_name}. For example, if you typed\n\ |
|
906 @samp{@value{OCTAVEHOME}/bin/octave} to start Octave,\n\ |
|
907 @code{program_invocation_name} would have the value\n\ |
|
908 @code{\"@value{OCTAVEHOME}/bin/octave\"}, and @code{program_name} would\n\ |
|
909 have the value @code{\"octave\"}.\n\ |
|
910 \n\ |
|
911 If executing a script from the command line (e.g., @code{octave foo.m})\n\ |
|
912 or using an executable Octave script, the program name is set to the\n\ |
3402
|
913 name of the script. @xref{Executable Octave Programs}, for an example of\n\ |
3332
|
914 how to create an executable Octave script.\n\ |
3333
|
915 @end defvr"); |
|
916 |
2806
|
917 } |
|
918 |
1683
|
919 /* |
|
920 ;;; Local Variables: *** |
|
921 ;;; mode: C++ *** |
|
922 ;;; End: *** |
|
923 */ |