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