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