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