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