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