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