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