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 |
3060
|
390 char ch; |
3147
|
391 |
4489
|
392 for (;;) |
3147
|
393 { |
4489
|
394 if (cmd->get (ch)) |
|
395 output_buf.put (ch); |
|
396 else |
|
397 { |
|
398 if (! cmd->eof () && errno == EAGAIN) |
|
399 { |
|
400 cmd->clear (); |
3147
|
401 |
4489
|
402 octave_usleep (100); |
|
403 } |
|
404 else |
|
405 break; |
|
406 } |
3147
|
407 } |
|
408 |
3252
|
409 int cmd_status = cmd->close (); |
2083
|
410 |
3060
|
411 if (WIFEXITED (cmd_status)) |
|
412 cmd_status = WEXITSTATUS (cmd_status); |
|
413 else |
|
414 cmd_status = 127; |
2083
|
415 |
4051
|
416 output_buf << OSSTREAM_ENDS; |
2083
|
417 |
3060
|
418 retval(1) = (double) cmd_status; |
4051
|
419 retval(0) = OSSTREAM_STR (output_buf); |
2083
|
420 |
4051
|
421 OSSTREAM_FREEZE (output_buf); |
3060
|
422 } |
|
423 |
|
424 unwind_protect::run (); |
2083
|
425 } |
|
426 else |
|
427 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); |
|
428 |
|
429 return retval; |
|
430 } |
|
431 |
1957
|
432 DEFUN (system, args, nargout, |
3301
|
433 "-*- texinfo -*-\n\ |
|
434 @deftypefn {Built-in Function} {} system (@var{string}, @var{return_output}, @var{type})\n\ |
|
435 Execute a shell command specified by @var{string}. The second\n\ |
|
436 argument is optional. If @var{type} is @code{\"async\"}, the process\n\ |
|
437 is started in the background and the process id of the child process\n\ |
|
438 is returned immediately. Otherwise, the process is started, and\n\ |
|
439 Octave waits until it exits. If @var{type} argument is omitted, a\n\ |
|
440 value of @code{\"sync\"} is assumed.\n\ |
2083
|
441 \n\ |
3301
|
442 If two input arguments are given (the actual value of\n\ |
|
443 @var{return_output} is irrelevant) and the subprocess is started\n\ |
|
444 synchronously, or if @var{system} is called with one input argument and\n\ |
|
445 one or more output arguments, the output from the command is returned.\n\ |
2083
|
446 Otherwise, if the subprocess is executed synchronously, it's output is\n\ |
2321
|
447 sent to the standard output. To send the output of a command executed\n\ |
3301
|
448 with @var{system} through the pager, use a command like\n\ |
2321
|
449 \n\ |
3301
|
450 @example\n\ |
|
451 disp (system (cmd, 1));\n\ |
|
452 @end example\n\ |
2321
|
453 \n\ |
3301
|
454 @noindent\n\ |
2321
|
455 or\n\ |
|
456 \n\ |
3301
|
457 @example\n\ |
|
458 printf (\"%s\n\", system (cmd, 1));\n\ |
|
459 @end example\n\ |
|
460 \n\ |
|
461 The @code{system} function can return two values. The first is any\n\ |
|
462 output from the command that was written to the standard output stream,\n\ |
|
463 and the second is the output status of the command. For example,\n\ |
|
464 \n\ |
|
465 @example\n\ |
|
466 [output, status] = system (\"echo foo; exit 2\");\n\ |
|
467 @end example\n\ |
|
468 \n\ |
|
469 @noindent\n\ |
|
470 will set the variable @code{output} to the string @samp{foo}, and the\n\ |
|
471 variable @code{status} to the integer @samp{2}.\n\ |
|
472 @end deftypefn") |
1683
|
473 { |
2086
|
474 octave_value_list retval; |
1683
|
475 |
3834
|
476 unwind_protect::begin_frame ("Fsystem"); |
|
477 |
1683
|
478 int nargin = args.length (); |
|
479 |
2083
|
480 if (nargin > 0 && nargin < 4) |
1683
|
481 { |
2083
|
482 bool return_output = (nargout > 0 || nargin > 1); |
|
483 |
3523
|
484 std::string cmd_str = args(0).string_value (); |
2083
|
485 |
|
486 enum exec_type { sync, async }; |
|
487 |
|
488 exec_type type = sync; |
|
489 |
|
490 if (! error_state) |
|
491 { |
|
492 if (nargin > 2) |
|
493 { |
3523
|
494 std::string type_str = args(2).string_value (); |
1683
|
495 |
2083
|
496 if (! error_state) |
|
497 { |
|
498 if (type_str == "sync") |
|
499 type = sync; |
|
500 else if (type_str == "async") |
|
501 type = async; |
|
502 else |
|
503 error ("system: third arg must be \"sync\" or \"async\""); |
|
504 } |
|
505 else |
|
506 error ("system: third argument must be a string"); |
|
507 } |
|
508 } |
|
509 else |
3523
|
510 error ("system: expecting std::string as first argument"); |
1683
|
511 |
2083
|
512 if (! error_state) |
|
513 { |
|
514 if (type == async) |
|
515 { |
4086
|
516 #ifdef HAVE_FORK |
2083
|
517 pid_t pid = fork (); |
|
518 |
|
519 if (pid < 0) |
|
520 error ("system: fork failed -- can't create child process"); |
|
521 else if (pid == 0) |
|
522 { |
3273
|
523 // XXX FIXME XXX -- should probably replace this |
|
524 // call with something portable. |
|
525 |
|
526 execl ("/bin/sh", "sh", "-c", cmd_str.c_str (), 0); |
|
527 |
|
528 panic_impossible (); |
2083
|
529 } |
|
530 else |
4254
|
531 retval(0) = pid; |
4086
|
532 #else |
|
533 error ("asynchronous system calls are not supported"); |
|
534 #endif |
2083
|
535 } |
2321
|
536 else if (return_output) |
|
537 retval = run_command_and_return_output (cmd_str); |
2083
|
538 else |
2321
|
539 { |
|
540 int status = system (cmd_str.c_str ()); |
|
541 |
|
542 // The value in status is as returned by waitpid. If |
|
543 // the process exited normally, extract the actual exit |
|
544 // status of the command. Otherwise, return 127 as a |
|
545 // failure code. |
|
546 |
2693
|
547 if (WIFEXITED (status)) |
|
548 status = WEXITSTATUS (status); |
2321
|
549 |
4233
|
550 retval(0) = status; |
2321
|
551 } |
2083
|
552 } |
1683
|
553 } |
|
554 else |
2083
|
555 print_usage ("system"); |
1683
|
556 |
3834
|
557 unwind_protect::run_frame ("Fsystem"); |
|
558 |
1683
|
559 return retval; |
|
560 } |
|
561 |
|
562 DEFALIAS (shell_cmd, system); |
|
563 |
2614
|
564 // XXX FIXME XXX -- this should really be static, but that causes |
|
565 // problems on some systems. |
4214
|
566 std::stack<std::string> octave_atexit_functions; |
2077
|
567 |
|
568 void |
|
569 do_octave_atexit (void) |
|
570 { |
3216
|
571 static bool deja_vu = false; |
|
572 |
2077
|
573 while (! octave_atexit_functions.empty ()) |
|
574 { |
4233
|
575 std::string fcn = octave_atexit_functions.top (); |
4214
|
576 |
|
577 octave_atexit_functions.pop (); |
2077
|
578 |
4233
|
579 feval (fcn, octave_value_list (), 0); |
3215
|
580 |
|
581 flush_octave_stdout (); |
2077
|
582 } |
3216
|
583 |
|
584 if (! deja_vu) |
|
585 { |
|
586 deja_vu = true; |
|
587 |
|
588 command_editor::restore_terminal_state (); |
|
589 |
|
590 // XXX FIXME XXX -- is this needed? Can it cause any trouble? |
|
591 raw_mode (0); |
|
592 |
|
593 command_history::clean_up_and_save (); |
|
594 |
|
595 close_plot_stream (); |
|
596 |
|
597 close_files (); |
|
598 |
|
599 cleanup_tmp_files (); |
|
600 |
|
601 flush_octave_stdout (); |
|
602 |
|
603 if (!quitting_gracefully && (interactive || forced_interactive)) |
3531
|
604 std::cout << "\n"; |
3216
|
605 } |
2077
|
606 } |
|
607 |
2162
|
608 DEFUN (atexit, args, , |
3332
|
609 "-*- texinfo -*-\n\ |
|
610 @deftypefn {Built-in Function} {} atexit (@var{fcn})\n\ |
|
611 Register a function to be called when Octave exits. For example,\n\ |
2077
|
612 \n\ |
3332
|
613 @example\n\ |
|
614 @group\n\ |
3686
|
615 function print_fortune ()\n\ |
3332
|
616 printf (\"\\n%s\\n\", system (\"fortune\"));\n\ |
|
617 fflush (stdout);\n\ |
|
618 endfunction\n\ |
3686
|
619 atexit (\"print_fortune\");\n\ |
3332
|
620 @end group\n\ |
|
621 @end example\n\ |
|
622 \n\ |
|
623 @noindent\n\ |
|
624 will print a message when Octave exits.\n\ |
3333
|
625 @end deftypefn") |
2077
|
626 { |
2086
|
627 octave_value_list retval; |
2077
|
628 |
|
629 int nargin = args.length (); |
|
630 |
|
631 if (nargin == 1) |
|
632 { |
2475
|
633 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
3523
|
634 std::string arg = args(0).string_value (); |
2077
|
635 |
|
636 if (! error_state) |
|
637 octave_atexit_functions.push (arg); |
|
638 else |
|
639 error ("atexit: argument must be a string"); |
2475
|
640 #else |
|
641 gripe_not_supported ("atexit"); |
|
642 #endif |
2077
|
643 } |
|
644 else |
|
645 print_usage ("atexit"); |
|
646 |
|
647 return retval; |
|
648 } |
|
649 |
2689
|
650 DEFUN (octave_config_info, args, , |
3301
|
651 "-*- texinfo -*-\n\ |
|
652 @deftypefn {Built-in Function} {} octave_config_info (@var{option})\n\ |
|
653 Return a structure containing configuration and installation\n\ |
|
654 information for Octave.\n\ |
2689
|
655 \n\ |
3301
|
656 if @var{option} is a string, return the configuration information for the\n\ |
2689
|
657 specified option.\n\ |
|
658 \n\ |
3301
|
659 @end deftypefn") |
2162
|
660 { |
2689
|
661 octave_value retval; |
|
662 |
4128
|
663 #if defined (ENABLE_DYNAMIC_LINKING) |
2689
|
664 bool octave_supports_dynamic_linking = true; |
|
665 #else |
|
666 bool octave_supports_dynamic_linking = false; |
|
667 #endif |
|
668 |
4357
|
669 // We do it this way instead of using a series of "m[KEY](0) = VAL" |
|
670 // statements to avoid problems with gcc 2.96 (and possibly other |
|
671 // compilers) that can't handle a long series of lines like that. |
|
672 |
|
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 { |
|
785 m ["dld"](0) = octave_value (octave_supports_dynamic_linking); |
|
786 |
|
787 int i = 0; |
4440
|
788 |
|
789 while (true) |
4357
|
790 { |
|
791 const char *key = conf_info[i++]; |
|
792 |
|
793 if (key) |
|
794 m [key](0) = octave_value (conf_info[i++]); |
|
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) |
4233
|
809 retval = m [arg.c_str ()](0); |
2689
|
810 } |
|
811 else if (nargin == 0) |
4233
|
812 retval = m; |
2689
|
813 else |
|
814 print_usage ("octave_config_info"); |
|
815 |
|
816 return retval; |
2162
|
817 } |
|
818 |
1683
|
819 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
2806
|
820 |
1683
|
821 int debug_new_delete = 0; |
|
822 |
|
823 typedef void (*vfp)(void); |
|
824 extern vfp __new_handler; |
|
825 |
|
826 void * |
|
827 __builtin_new (size_t sz) |
|
828 { |
|
829 void *p; |
|
830 |
|
831 /* malloc (0) is unpredictable; avoid it. */ |
|
832 if (sz == 0) |
|
833 sz = 1; |
2800
|
834 p = malloc (sz); |
1683
|
835 while (p == 0) |
|
836 { |
|
837 (*__new_handler) (); |
2800
|
838 p = malloc (sz); |
1683
|
839 } |
|
840 |
|
841 if (debug_new_delete) |
3538
|
842 std::cout << "__builtin_new: " << p << std::endl; |
1683
|
843 |
|
844 return p; |
|
845 } |
|
846 |
|
847 void |
|
848 __builtin_delete (void *ptr) |
|
849 { |
|
850 if (debug_new_delete) |
3538
|
851 std::cout << "__builtin_delete: " << ptr << std::endl; |
1683
|
852 |
|
853 if (ptr) |
|
854 free (ptr); |
|
855 } |
2806
|
856 |
1683
|
857 #endif |
|
858 |
2806
|
859 void |
|
860 symbols_of_toplev (void) |
|
861 { |
3141
|
862 DEFCONST (argv, , |
3332
|
863 "-*- texinfo -*-\n\ |
|
864 @defvr {Built-in Variable} argv\n\ |
|
865 The command line arguments passed to Octave are available in this\n\ |
|
866 variable. For example, if you invoked Octave using the command\n\ |
|
867 \n\ |
|
868 @example\n\ |
|
869 octave --no-line-editing --silent\n\ |
|
870 @end example\n\ |
|
871 \n\ |
|
872 @noindent\n\ |
3971
|
873 @code{argv} would be a cell array of strings with the elements\n\ |
3332
|
874 @code{--no-line-editing} and @code{--silent}.\n\ |
|
875 \n\ |
|
876 If you write an executable Octave script, @code{argv} will contain the\n\ |
3402
|
877 list of arguments passed to the script. @xref{Executable Octave Programs},\n\ |
|
878 for an example of how to create an executable Octave script.\n\ |
3333
|
879 @end defvr"); |
2806
|
880 |
3020
|
881 DEFCONST (program_invocation_name, |
3141
|
882 octave_env::get_program_invocation_name (), |
3332
|
883 "-*- texinfo -*-\n\ |
|
884 @defvr {Built-in Variable} program_invocation_name\n\ |
|
885 @defvrx {Built-in Variable} program_name\n\ |
|
886 When Octave starts, the value of the built-in variable\n\ |
|
887 @code{program_invocation_name} is automatically set to the name that was\n\ |
|
888 typed at the shell prompt to run Octave, and the value of\n\ |
|
889 @code{program_name} is automatically set to the final component of\n\ |
|
890 @code{program_invocation_name}. For example, if you typed\n\ |
|
891 @samp{@value{OCTAVEHOME}/bin/octave} to start Octave,\n\ |
|
892 @code{program_invocation_name} would have the value\n\ |
|
893 @code{\"@value{OCTAVEHOME}/bin/octave\"}, and @code{program_name} would\n\ |
|
894 have the value @code{\"octave\"}.\n\ |
|
895 \n\ |
|
896 If executing a script from the command line (e.g., @code{octave foo.m})\n\ |
|
897 or using an executable Octave script, the program name is set to the\n\ |
3402
|
898 name of the script. @xref{Executable Octave Programs}, for an example of\n\ |
3332
|
899 how to create an executable Octave script.\n\ |
3333
|
900 @end defvr"); |
3020
|
901 |
3141
|
902 DEFCONST (program_name, octave_env::get_program_name (), |
3332
|
903 "-*- texinfo -*-\n\ |
|
904 @defvr {Built-in Variable} program_invocation_name\n\ |
|
905 @defvrx {Built-in Variable} program_name\n\ |
|
906 When Octave starts, the value of the built-in variable\n\ |
|
907 @code{program_invocation_name} is automatically set to the name that was\n\ |
|
908 typed at the shell prompt to run Octave, and the value of\n\ |
|
909 @code{program_name} is automatically set to the final component of\n\ |
|
910 @code{program_invocation_name}. For example, if you typed\n\ |
|
911 @samp{@value{OCTAVEHOME}/bin/octave} to start Octave,\n\ |
|
912 @code{program_invocation_name} would have the value\n\ |
|
913 @code{\"@value{OCTAVEHOME}/bin/octave\"}, and @code{program_name} would\n\ |
|
914 have the value @code{\"octave\"}.\n\ |
|
915 \n\ |
|
916 If executing a script from the command line (e.g., @code{octave foo.m})\n\ |
|
917 or using an executable Octave script, the program name is set to the\n\ |
3402
|
918 name of the script. @xref{Executable Octave Programs}, for an example of\n\ |
3332
|
919 how to create an executable Octave script.\n\ |
3333
|
920 @end defvr"); |
|
921 |
2806
|
922 } |
|
923 |
1683
|
924 /* |
|
925 ;;; Local Variables: *** |
|
926 ;;; mode: C++ *** |
|
927 ;;; End: *** |
|
928 */ |