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