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