1683
|
1 /* |
|
2 |
1884
|
3 Copyright (C) 1996 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 <csetjmp> |
|
29 #include <csignal> |
|
30 #include <cstdlib> |
|
31 #include <cstring> |
|
32 #include <ctime> |
|
33 |
1728
|
34 #include <string> |
|
35 |
1683
|
36 #include <fstream.h> |
|
37 #include <iostream.h> |
|
38 #include <strstream.h> |
|
39 |
|
40 #ifdef HAVE_UNISTD_H |
|
41 #include <sys/types.h> |
|
42 #include <unistd.h> |
|
43 #endif |
|
44 |
|
45 #include "lo-error.h" |
1755
|
46 #include "str-vec.h" |
1683
|
47 |
|
48 #include "builtins.h" |
|
49 #include "defaults.h" |
|
50 #include "defun.h" |
|
51 #include "dynamic-ld.h" |
|
52 #include "error.h" |
|
53 #include "file-io.h" |
|
54 #include "help.h" |
|
55 #include "input.h" |
|
56 #include "lex.h" |
2162
|
57 #include "oct-conf.h" |
1742
|
58 #include "oct-hist.h" |
2162
|
59 #include "oct-map.h" |
1683
|
60 #include "pager.h" |
|
61 #include "parse.h" |
|
62 #include "pathsearch.h" |
|
63 #include "procstream.h" |
1750
|
64 #include "pt-const.h" |
|
65 #include "pt-misc.h" |
|
66 #include "pt-plot.h" |
1683
|
67 #include "sighandlers.h" |
|
68 #include "sysdep.h" |
1750
|
69 #include "toplev.h" |
1683
|
70 #include "unwind-prot.h" |
|
71 #include "user-prefs.h" |
|
72 #include "utils.h" |
|
73 #include "variables.h" |
|
74 #include "version.h" |
|
75 |
|
76 // argv[0] for this program. |
1755
|
77 string raw_prog_name; |
1683
|
78 |
|
79 // Cleaned-up name of this program, not including path information. |
1755
|
80 string prog_name; |
1683
|
81 |
|
82 // Login name for user running this program. |
1755
|
83 string user_name; |
1683
|
84 |
|
85 // Name of the host we are running on. |
1755
|
86 string host_name; |
1683
|
87 |
|
88 // User's home directory. |
1755
|
89 string home_directory; |
1683
|
90 |
|
91 // Guess what? |
1755
|
92 string the_current_working_directory; |
1683
|
93 |
|
94 // The path that will be searched for programs that we execute. |
|
95 // (--exec-path path) |
1755
|
96 string exec_path; |
1683
|
97 |
|
98 // Load path specified on command line. |
|
99 // (--path path; -p path) |
1755
|
100 string load_path; |
1683
|
101 |
|
102 // Name of the info file specified on command line. |
|
103 // (--info-file file) |
1755
|
104 string info_file; |
1683
|
105 |
|
106 // Name of the info reader we'd like to use. |
|
107 // (--info-program program) |
1755
|
108 string info_prog; |
1683
|
109 |
|
110 // Name of the editor to be invoked by the edit_history command. |
1755
|
111 string editor; |
1683
|
112 |
1820
|
113 // Nonzero means we are using readline. |
|
114 // (--no-line-editing) |
|
115 #if defined (USE_READLINE) |
|
116 int using_readline = 1; |
|
117 #else |
|
118 int using_readline = 0; |
|
119 #endif |
1683
|
120 |
|
121 // Nonzero means we printed messages about reading startup files. |
|
122 int reading_startup_message_printed = 0; |
|
123 |
|
124 // Command number, counting from the beginning of this session. |
|
125 int current_command_number = 1; |
|
126 |
|
127 // Nonzero means we are exiting via the builtin exit or quit functions. |
|
128 int quitting_gracefully = 0; |
|
129 |
|
130 // Current command to execute. |
|
131 tree_statement_list *global_command = 0; |
|
132 |
|
133 // Pointer to function that is currently being evaluated. |
|
134 tree_function *curr_function = 0; |
|
135 |
|
136 // Nonzero means input is coming from startup file. |
|
137 int input_from_startup_file = 0; |
|
138 |
|
139 // Nonzero means that input is coming from a file that was named on |
|
140 // the command line. |
|
141 int input_from_command_line_file = 1; |
|
142 |
1907
|
143 // Top level context (?) |
|
144 jmp_buf toplevel; |
|
145 |
1683
|
146 void |
|
147 parse_and_execute (FILE *f, int print) |
|
148 { |
|
149 begin_unwind_frame ("parse_and_execute"); |
|
150 |
|
151 YY_BUFFER_STATE old_buf = current_buffer (); |
|
152 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
153 |
|
154 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
155 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
156 |
|
157 switch_to_buffer (new_buf); |
|
158 |
|
159 unwind_protect_int (using_readline); |
|
160 unwind_protect_int (input_from_command_line_file); |
|
161 |
|
162 using_readline = 0; |
|
163 input_from_command_line_file = 0; |
|
164 |
|
165 unwind_protect_ptr (curr_sym_tab); |
|
166 |
|
167 int retval; |
|
168 do |
|
169 { |
|
170 reset_parser (); |
|
171 |
|
172 retval = yyparse (); |
|
173 |
|
174 if (retval == 0 && global_command) |
|
175 { |
|
176 global_command->eval (print); |
|
177 delete global_command; |
|
178 } |
|
179 } |
|
180 while (retval == 0); |
|
181 |
|
182 run_unwind_frame ("parse_and_execute"); |
|
183 } |
|
184 |
|
185 void |
1750
|
186 parse_and_execute (const string& s, int print, int verbose, |
1683
|
187 const char *warn_for) |
|
188 { |
|
189 begin_unwind_frame ("parse_and_execute_2"); |
|
190 |
|
191 unwind_protect_int (reading_script_file); |
1755
|
192 unwind_protect_str (curr_fcn_file_full_name); |
1683
|
193 |
|
194 reading_script_file = 1; |
1755
|
195 curr_fcn_file_full_name = s; |
1683
|
196 |
|
197 FILE *f = get_input_from_file (s, 0); |
1750
|
198 |
1683
|
199 if (f) |
|
200 { |
|
201 unwind_protect_int (input_line_number); |
|
202 unwind_protect_int (current_input_column); |
|
203 |
|
204 input_line_number = 0; |
|
205 current_input_column = 1; |
|
206 |
|
207 if (verbose) |
|
208 { |
|
209 cout << "reading commands from " << s << " ... "; |
|
210 reading_startup_message_printed = 1; |
|
211 cout.flush (); |
|
212 } |
|
213 |
|
214 parse_and_execute (f, print); |
|
215 |
|
216 fclose (f); |
|
217 |
|
218 if (verbose) |
|
219 cout << "done." << endl; |
|
220 } |
|
221 else if (warn_for) |
1750
|
222 error ("%s: unable to open file `%s'", warn_for, s.c_str ()); |
1683
|
223 |
|
224 run_unwind_frame ("parse_and_execute_2"); |
|
225 } |
|
226 |
1907
|
227 int |
|
228 main_loop (void) |
|
229 { |
|
230 // Allow the user to interrupt us without exiting. |
|
231 |
2016
|
232 octave_save_signal_mask (); |
|
233 |
1907
|
234 if (setjmp (toplevel) != 0) |
|
235 { |
|
236 raw_mode (0); |
|
237 |
|
238 cout << "\n"; |
2016
|
239 |
|
240 octave_restore_signal_mask (); |
1907
|
241 } |
|
242 |
|
243 can_interrupt = 1; |
|
244 |
|
245 catch_interrupts (); |
|
246 |
|
247 // The big loop. |
|
248 |
|
249 int retval; |
|
250 do |
|
251 { |
|
252 curr_sym_tab = top_level_sym_tab; |
|
253 |
|
254 reset_parser (); |
|
255 |
|
256 retval = yyparse (); |
|
257 |
|
258 if (retval == 0 && global_command) |
|
259 { |
|
260 global_command->eval (1); |
|
261 delete global_command; |
|
262 current_command_number++; |
|
263 } |
|
264 } |
|
265 while (retval == 0); |
|
266 |
|
267 return retval; |
|
268 } |
|
269 |
1957
|
270 DEFUN (source, args, , |
1683
|
271 "source (FILE)\n\ |
|
272 \n\ |
|
273 Parse and execute the contents of FILE. Like executing commands in a\n\ |
|
274 script file but without requiring the file to be named `FILE.m'.") |
|
275 { |
2086
|
276 octave_value_list retval; |
1683
|
277 |
|
278 int nargin = args.length (); |
|
279 |
|
280 if (nargin == 1) |
|
281 { |
1750
|
282 string file = args(0).string_value (); |
1683
|
283 |
|
284 if (! error_state) |
|
285 { |
1750
|
286 file = oct_tilde_expand (file); |
1683
|
287 |
|
288 parse_and_execute (file, 1, 0, "source"); |
|
289 |
|
290 if (error_state) |
1751
|
291 error ("source: error sourcing file `%s'", file.c_str ()); |
1683
|
292 } |
|
293 else |
|
294 error ("source: expecting file name as argument"); |
|
295 } |
|
296 else |
|
297 print_usage ("source"); |
|
298 |
|
299 return retval; |
|
300 } |
|
301 |
|
302 // Fix up things before exiting. |
|
303 |
|
304 void |
|
305 clean_up_and_exit (int retval) |
|
306 { |
|
307 raw_mode (0); |
|
308 |
1799
|
309 octave_command_history.clean_up_and_save (); |
1683
|
310 |
|
311 close_plot_stream (); |
|
312 |
|
313 close_files (); |
|
314 |
|
315 cleanup_tmp_files (); |
|
316 |
|
317 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
318 cout << "\n"; |
|
319 |
|
320 if (retval == EOF) |
|
321 retval = 0; |
|
322 |
|
323 exit (retval); |
|
324 |
|
325 // This is bogus but should prevent g++ from giving a warning saying |
|
326 // that this volatile function does return. |
|
327 |
|
328 panic_impossible (); |
|
329 } |
|
330 |
1957
|
331 DEFUN_TEXT (casesen, args, , |
1683
|
332 "casesen [on|off]") |
|
333 { |
2086
|
334 octave_value_list retval; |
1683
|
335 |
1755
|
336 int argc = args.length () + 1; |
|
337 |
1965
|
338 string_vector argv = args.make_argv ("casesen"); |
1683
|
339 |
1755
|
340 if (error_state) |
|
341 return retval; |
|
342 |
|
343 if (argc == 1 || (argc > 1 && argv[1] == "off")) |
1683
|
344 warning ("casesen: sorry, Octave is always case sensitive"); |
1755
|
345 else if (argc > 1 && argv[1] == "on") |
1683
|
346 ; // ok. |
|
347 else |
|
348 print_usage ("casesen"); |
|
349 |
|
350 return retval; |
|
351 } |
|
352 |
1957
|
353 DEFUN (computer, args, nargout, |
1683
|
354 "computer ():\n\ |
|
355 \n\ |
|
356 Have Octave ask the system, \"What kind of computer are you?\"") |
|
357 { |
2086
|
358 octave_value_list retval; |
1683
|
359 |
|
360 int nargin = args.length (); |
|
361 |
|
362 if (nargin != 0) |
|
363 warning ("computer: ignoring extra arguments"); |
|
364 |
2095
|
365 string msg; |
1683
|
366 |
|
367 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
2095
|
368 msg = "Hi Dave, I'm a HAL-9000"; |
1683
|
369 else |
2095
|
370 msg = TARGET_HOST_TYPE; |
1683
|
371 |
|
372 if (nargout == 0) |
2095
|
373 octave_stdout << msg << "\n"; |
1683
|
374 else |
2095
|
375 retval = msg; |
1683
|
376 |
|
377 return retval; |
|
378 } |
|
379 |
2068
|
380 DEFUN (quit, args, , |
|
381 "quit (STATUS): exit Octave gracefully, returning STATUS to the system.\n\ |
|
382 \n\ |
|
383 STATUS should be an integer value. If STATUS is missing, 0 is assumed.") |
1683
|
384 { |
2086
|
385 octave_value_list retval; |
2068
|
386 |
|
387 int exit_status = 0; |
|
388 |
|
389 quitting_gracefully = 1; |
|
390 |
1683
|
391 int nargin = args.length (); |
|
392 |
|
393 if (nargin > 0) |
2068
|
394 { |
|
395 // XXX FIXME XXX -- need a safe uniform way to do this. |
1683
|
396 |
2068
|
397 double tmp = args(0).double_value (); |
1683
|
398 |
2068
|
399 if (! error_state && ! xisnan (tmp)) |
|
400 exit_status = NINT (tmp); |
|
401 } |
|
402 |
|
403 clean_up_and_exit (exit_status); |
|
404 |
1683
|
405 return retval; |
|
406 } |
|
407 |
|
408 DEFALIAS (exit, quit); |
|
409 |
1957
|
410 DEFUN (warranty, , , |
1683
|
411 "warranty (): describe copying conditions") |
|
412 { |
2086
|
413 octave_value_list retval; |
1683
|
414 |
2095
|
415 octave_stdout << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
1683
|
416 This program is free software; you can redistribute it and/or modify\n\ |
|
417 it under the terms of the GNU General Public License as published by\n\ |
|
418 the Free Software Foundation; either version 2 of the License, or\n\ |
|
419 (at your option) any later version.\n\ |
|
420 \n\ |
|
421 This program is distributed in the hope that it will be useful,\n\ |
|
422 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
423 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
424 GNU General Public License for more details.\n\ |
|
425 \n\ |
|
426 You should have received a copy of the GNU General Public License\n\ |
|
427 along with this program. If not, write to the Free Software\n\ |
|
428 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
|
429 \n"; |
|
430 |
|
431 return retval; |
|
432 } |
|
433 |
|
434 // XXX FIXME XXX -- this may not be the best place for these... |
|
435 |
2086
|
436 octave_value_list |
|
437 feval (const octave_value_list& args, int nargout) |
1683
|
438 { |
2086
|
439 octave_value_list retval; |
1683
|
440 |
|
441 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
|
442 if (fcn) |
|
443 { |
|
444 int tmp_nargin = args.length () - 1; |
2086
|
445 octave_value_list tmp_args; |
1683
|
446 tmp_args.resize (tmp_nargin); |
|
447 for (int i = 0; i < tmp_nargin; i++) |
|
448 tmp_args(i) = args(i+1); |
|
449 retval = fcn->eval (0, nargout, tmp_args); |
|
450 } |
|
451 |
|
452 return retval; |
|
453 } |
|
454 |
1957
|
455 DEFUN (feval, args, nargout, |
1683
|
456 "feval (NAME, ARGS, ...)\n\ |
|
457 \n\ |
|
458 evaluate NAME as a function, passing ARGS as its arguments") |
|
459 { |
2086
|
460 octave_value_list retval; |
1683
|
461 |
|
462 int nargin = args.length (); |
|
463 |
|
464 if (nargin > 0) |
|
465 retval = feval (args, nargout); |
|
466 else |
|
467 print_usage ("feval"); |
|
468 |
|
469 return retval; |
|
470 } |
|
471 |
2086
|
472 static octave_value_list |
1755
|
473 eval_string (const string& s, int print, int& parse_status, |
1683
|
474 int nargout) |
|
475 { |
|
476 begin_unwind_frame ("eval_string"); |
|
477 |
|
478 unwind_protect_int (get_input_from_eval_string); |
|
479 unwind_protect_int (input_from_command_line_file); |
|
480 unwind_protect_ptr (global_command); |
1755
|
481 unwind_protect_str (current_eval_string); |
1683
|
482 |
|
483 get_input_from_eval_string = 1; |
|
484 input_from_command_line_file = 0; |
1755
|
485 current_eval_string = s; |
1683
|
486 |
|
487 YY_BUFFER_STATE old_buf = current_buffer (); |
|
488 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
489 |
|
490 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
491 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
492 |
|
493 switch_to_buffer (new_buf); |
|
494 |
|
495 unwind_protect_ptr (curr_sym_tab); |
|
496 |
|
497 reset_parser (); |
|
498 |
|
499 parse_status = yyparse (); |
|
500 |
|
501 // Important to reset the idea of where input is coming from before |
|
502 // trying to eval the command we just parsed -- it might contain the |
|
503 // name of an function file that still needs to be parsed! |
|
504 |
|
505 tree_statement_list *command = global_command; |
|
506 |
|
507 run_unwind_frame ("eval_string"); |
|
508 |
2086
|
509 octave_value_list retval; |
1683
|
510 |
|
511 if (parse_status == 0 && command) |
|
512 { |
|
513 retval = command->eval (print, nargout); |
|
514 delete command; |
|
515 } |
|
516 |
|
517 return retval; |
|
518 } |
|
519 |
2086
|
520 octave_value |
1755
|
521 eval_string (const string& s, int print, int& parse_status) |
1683
|
522 { |
2086
|
523 octave_value retval; |
1683
|
524 |
2086
|
525 octave_value_list tmp = eval_string (s, print, parse_status, 1); |
1683
|
526 |
|
527 retval = tmp(0); |
|
528 |
|
529 return retval; |
|
530 } |
|
531 |
2086
|
532 static octave_value_list |
|
533 eval_string (const octave_value& arg, int& parse_status, int nargout) |
1683
|
534 { |
1755
|
535 string s = arg.string_value (); |
1683
|
536 |
|
537 if (error_state) |
|
538 { |
|
539 error ("eval: expecting string argument"); |
|
540 return -1.0; |
|
541 } |
|
542 |
|
543 // Yes Virginia, we always print here... |
|
544 |
1755
|
545 return eval_string (s, 1, parse_status, nargout); |
1683
|
546 } |
|
547 |
1957
|
548 DEFUN (eval, args, nargout, |
1683
|
549 "eval (TRY, CATCH)\n\ |
|
550 \n\ |
|
551 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
552 string CATCH.") |
|
553 { |
2086
|
554 octave_value_list retval; |
1683
|
555 |
|
556 int nargin = args.length (); |
|
557 |
|
558 if (nargin > 0) |
|
559 { |
|
560 begin_unwind_frame ("Feval"); |
|
561 |
|
562 if (nargin > 1) |
|
563 { |
|
564 unwind_protect_int (buffer_error_messages); |
|
565 buffer_error_messages = 1; |
|
566 } |
|
567 |
|
568 int parse_status = 0; |
|
569 |
|
570 retval = eval_string (args(0), parse_status, nargout); |
|
571 |
|
572 if (nargin > 1 && (parse_status != 0 || error_state)) |
|
573 { |
|
574 error_state = 0; |
|
575 |
|
576 // Set up for letting the user print any messages from |
|
577 // errors that occurred in the first part of this eval(). |
|
578 |
|
579 buffer_error_messages = 0; |
|
580 bind_global_error_variable (); |
|
581 add_unwind_protect (clear_global_error_variable, 0); |
|
582 |
|
583 eval_string (args(1), parse_status, nargout); |
|
584 |
2086
|
585 retval = octave_value_list (); |
1683
|
586 } |
|
587 |
|
588 run_unwind_frame ("Feval"); |
|
589 } |
|
590 else |
|
591 print_usage ("eval"); |
|
592 |
|
593 return retval; |
|
594 } |
|
595 |
|
596 // Execute a shell command. |
|
597 |
1965
|
598 static void |
|
599 cleanup_iprocstream (void *p) |
|
600 { |
|
601 delete (iprocstream *) p; |
|
602 } |
|
603 |
2086
|
604 static octave_value_list |
2083
|
605 do_system (const string& cmd_str, bool return_output) |
|
606 { |
2086
|
607 octave_value_list retval; |
2083
|
608 |
|
609 iprocstream *cmd = new iprocstream (cmd_str.c_str ()); |
|
610 |
|
611 add_unwind_protect (cleanup_iprocstream, cmd); |
|
612 |
|
613 int status = 127; |
|
614 |
|
615 if (cmd && *cmd) |
|
616 { |
|
617 ostrstream output_buf; |
2095
|
618 |
2083
|
619 char ch; |
|
620 while (cmd->get (ch)) |
2084
|
621 { |
2095
|
622 if (return_output) |
2084
|
623 output_buf.put (ch); |
2145
|
624 else |
2095
|
625 octave_stdout.put (ch); |
2084
|
626 } |
2083
|
627 |
|
628 status = cmd->close (); |
|
629 |
|
630 // The value in status is as returned by waitpid. If the |
|
631 // process exited normally, extract the actual exit status of |
|
632 // the command. Otherwise, return 127 as a failure code. |
|
633 |
|
634 if ((status & 0xff) == 0) |
|
635 status = (status & 0xff00) >> 8; |
|
636 |
|
637 if (return_output) |
|
638 { |
2145
|
639 output_buf << ends; |
|
640 |
2083
|
641 char *msg = output_buf.str (); |
|
642 |
|
643 retval(1) = (double) status; |
|
644 retval(0) = msg; |
|
645 |
|
646 delete [] msg; |
|
647 } |
|
648 } |
|
649 else |
|
650 error ("unable to start subprocess for `%s'", cmd_str.c_str ()); |
|
651 |
|
652 run_unwind_protect (); |
|
653 |
|
654 return retval; |
|
655 } |
|
656 |
1957
|
657 DEFUN (system, args, nargout, |
2083
|
658 "system (STRING [, RETURN_OUTPUT] [, TYPE])\n\ |
|
659 \n\ |
|
660 Execute the shell command specified by STRING.\n\ |
|
661 \n\ |
|
662 If TYPE is \"async\", the process is started in the background and the\n\ |
|
663 pid of the child proces is returned immediately. Otherwise, the\n\ |
|
664 process is started, and Octave waits until it exits. If TYPE argument\n\ |
|
665 is omitted, a value of \"sync\" is assumed.\n\ |
|
666 \n\ |
|
667 If NARGIN == 2 (the actual value of RETURN_OUTPUT is irrelevant) and\n\ |
|
668 the subprocess is started synchronously, or if system() is called with\n\ |
|
669 NARGIN == 1 and NARGOUT > 0, the output from the command is returned.\n\ |
|
670 Otherwise, if the subprocess is executed synchronously, it's output is\n\ |
|
671 sent to Octave's standard output (possibly being passed through the\n\ |
|
672 pager).") |
1683
|
673 { |
2086
|
674 octave_value_list retval; |
1683
|
675 |
|
676 int nargin = args.length (); |
|
677 |
2083
|
678 if (nargin > 0 && nargin < 4) |
1683
|
679 { |
2083
|
680 bool return_output = (nargout > 0 || nargin > 1); |
|
681 |
|
682 string cmd_str = args(0).string_value (); |
|
683 |
|
684 enum exec_type { sync, async }; |
|
685 |
|
686 exec_type type = sync; |
|
687 |
|
688 if (! error_state) |
|
689 { |
|
690 if (nargin > 2) |
|
691 { |
|
692 string type_str = args(2).string_value (); |
1683
|
693 |
2083
|
694 if (! error_state) |
|
695 { |
|
696 if (type_str == "sync") |
|
697 type = sync; |
|
698 else if (type_str == "async") |
|
699 type = async; |
|
700 else |
|
701 error ("system: third arg must be \"sync\" or \"async\""); |
|
702 } |
|
703 else |
|
704 error ("system: third argument must be a string"); |
|
705 } |
|
706 } |
|
707 else |
|
708 error ("system: expecting string as first argument"); |
1683
|
709 |
2083
|
710 if (! error_state) |
|
711 { |
|
712 if (type == async) |
|
713 { |
|
714 pid_t pid = fork (); |
|
715 |
|
716 if (pid < 0) |
|
717 error ("system: fork failed -- can't create child process"); |
|
718 else if (pid == 0) |
|
719 { |
|
720 system (cmd_str.c_str ()); |
|
721 exit (0); |
|
722 retval(0) = 0.0; |
|
723 } |
|
724 else |
|
725 retval(0) = (double) pid; |
|
726 } |
|
727 else |
|
728 retval = do_system (cmd_str, return_output); |
|
729 } |
1683
|
730 } |
|
731 else |
2083
|
732 print_usage ("system"); |
1683
|
733 |
|
734 return retval; |
|
735 } |
|
736 |
|
737 DEFALIAS (shell_cmd, system); |
|
738 |
2077
|
739 static SLStack<string> octave_atexit_functions; |
|
740 |
|
741 void |
|
742 do_octave_atexit (void) |
|
743 { |
|
744 while (! octave_atexit_functions.empty ()) |
|
745 { |
2086
|
746 octave_value_list fcn = octave_atexit_functions.pop (); |
2077
|
747 |
|
748 feval (fcn, 0); |
|
749 } |
|
750 } |
|
751 |
2162
|
752 DEFUN (atexit, args, , |
2077
|
753 "atexit (NAME): register NAME as a function to call when Octave exits\n\ |
|
754 \n\ |
|
755 Functions are called with no arguments in the reverse of the order in |
|
756 which they were registered with atexit()") |
|
757 { |
2086
|
758 octave_value_list retval; |
2077
|
759 |
|
760 #if defined (HAVE_ATEXIT) || defined (HAVE_ON_EXIT) |
|
761 int nargin = args.length (); |
|
762 |
|
763 if (nargin == 1) |
|
764 { |
|
765 string arg = args(0).string_value (); |
|
766 |
|
767 if (! error_state) |
|
768 octave_atexit_functions.push (arg); |
|
769 else |
|
770 error ("atexit: argument must be a string"); |
|
771 } |
|
772 else |
|
773 print_usage ("atexit"); |
|
774 #else |
2078
|
775 gripe_not_supported ("atexit"); |
2077
|
776 #endif |
|
777 |
|
778 return retval; |
|
779 } |
|
780 |
2162
|
781 DEFUN (octave_config_info, , , |
|
782 "return a structure containing configuration information") |
|
783 { |
|
784 Octave_map m; |
|
785 |
|
786 m ["default_pager"] = DEFAULT_PAGER; |
|
787 m ["prefix"] = OCTAVE_PREFIX; |
|
788 m ["exec_prefix"] = OCTAVE_EXEC_PREFIX; |
|
789 m ["datadir"] = OCTAVE_DATADIR; |
|
790 m ["libdir"] = OCTAVE_LIBDIR; |
|
791 m ["bindir"] = OCTAVE_BINDIR; |
|
792 m ["infodir"] = OCTAVE_INFODIR; |
|
793 m ["fcnfiledir"] = OCTAVE_FCNFILEDIR; |
|
794 m ["localfcnfiledir"] = OCTAVE_LOCALFCNFILEDIR; |
|
795 m ["localstartupfiledir"] = OCTAVE_LOCALSTARTUPFILEDIR; |
|
796 m ["startupfiledir"] = OCTAVE_STARTUPFILEDIR; |
|
797 m ["localfcnfilepath"] = OCTAVE_LOCALFCNFILEPATH; |
|
798 m ["archlibdir"] = OCTAVE_ARCHLIBDIR; |
|
799 m ["octfiledir"] = OCTAVE_OCTFILEDIR; |
|
800 m ["localoctfilepath"] = OCTAVE_LOCALOCTFILEPATH; |
|
801 m ["fcnfilepath"] = OCTAVE_FCNFILEPATH; |
|
802 m ["imagepath"] = OCTAVE_IMAGEPATH; |
|
803 m ["target_host_type"] = TARGET_HOST_TYPE; |
|
804 m ["configure_options"] = config_opts; |
|
805 m ["F77"] = F77; |
|
806 m ["FFLAGS"] = FFLAGS; |
|
807 m ["FPICFLAG"] = FPICFLAG; |
|
808 m ["F2C"] = F2C; |
|
809 m ["F2CFLAGS"] = F2CFLAGS; |
|
810 m ["FLIBS"] = FLIBS; |
|
811 m ["CPPFLAGS"] = CPPFLAGS; |
|
812 m ["INCFLAGS"] = INCFLAGS; |
|
813 m ["CC"] = CC " " CC_VERSION; |
|
814 m ["CFLAGS"] = CFLAGS; |
|
815 m ["CPICFLAG"] = CPICFLAG; |
|
816 m ["CXX"] = CXX " " CXX_VERSION; |
|
817 m ["CXXFLAGS"] = CXXFLAGS; |
|
818 m ["CXXPICFLAG"] = CXXPICFLAG; |
|
819 m ["LDFLAGS"] = LDFLAGS; |
|
820 m ["LIBFLAGS"] = LIBFLAGS; |
|
821 m ["RLD_FLAG"] = RLD_FLAG; |
|
822 m ["CXXLIBS"] = CXXLIBS; |
|
823 m ["TERMLIBS"] = TERMLIBS; |
|
824 m ["LIBS"] = LIBS; |
|
825 m ["LEXLIB"] = LEXLIB; |
|
826 m ["LIBPLPLOT"] = LIBPLPLOT; |
|
827 m ["LIBDLFCN"] = LIBDLFCN; |
|
828 m ["DEFS"] = DEFS; |
|
829 |
|
830 return octave_value (m); |
|
831 } |
|
832 |
1683
|
833 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
834 int debug_new_delete = 0; |
|
835 |
|
836 typedef void (*vfp)(void); |
|
837 extern vfp __new_handler; |
|
838 |
|
839 void * |
|
840 __builtin_new (size_t sz) |
|
841 { |
|
842 void *p; |
|
843 |
|
844 /* malloc (0) is unpredictable; avoid it. */ |
|
845 if (sz == 0) |
|
846 sz = 1; |
|
847 p = (void *) malloc (sz); |
|
848 while (p == 0) |
|
849 { |
|
850 (*__new_handler) (); |
|
851 p = (void *) malloc (sz); |
|
852 } |
|
853 |
|
854 if (debug_new_delete) |
|
855 cout << "__builtin_new: " << p << endl; |
|
856 |
|
857 return p; |
|
858 } |
|
859 |
|
860 void |
|
861 __builtin_delete (void *ptr) |
|
862 { |
|
863 if (debug_new_delete) |
|
864 cout << "__builtin_delete: " << ptr << endl; |
|
865 |
|
866 if (ptr) |
|
867 free (ptr); |
|
868 } |
|
869 #endif |
|
870 |
|
871 /* |
|
872 ;;; Local Variables: *** |
|
873 ;;; mode: C++ *** |
|
874 ;;; End: *** |
|
875 */ |