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