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