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