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