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