1
|
1 // octave.cc -*- C++ -*- |
|
2 /* |
|
3 |
268
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
1
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 // Born February 20, 1992. |
|
25 |
240
|
26 #ifdef HAVE_CONFIG_H |
|
27 #include "config.h" |
1
|
28 #endif |
|
29 |
|
30 #include <sys/types.h> |
139
|
31 #ifdef HAVE_UNISTD_H |
|
32 #include <unistd.h> |
|
33 #endif |
85
|
34 #include <sys/stat.h> |
1
|
35 #include <time.h> |
|
36 #include <pwd.h> |
|
37 #include <stdlib.h> |
|
38 #include <string.h> |
|
39 #include <signal.h> |
|
40 #include <assert.h> |
|
41 #include <iostream.h> |
529
|
42 #include <strstream.h> |
1
|
43 #include <fstream.h> |
139
|
44 |
636
|
45 extern "C" |
|
46 { |
|
47 #include <setjmp.h> |
|
48 } |
|
49 |
139
|
50 #include "getopt.h" |
1
|
51 |
240
|
52 #include "lo-error.h" |
223
|
53 |
1
|
54 #include "sighandlers.h" |
|
55 #include "variables.h" |
|
56 #include "error.h" |
703
|
57 #include "dynamic-ld.h" |
578
|
58 #include "tree-misc.h" |
1
|
59 #include "tree-const.h" |
529
|
60 #include "tree-plot.h" |
1
|
61 #include "utils.h" |
|
62 #include "input.h" |
|
63 #include "pager.h" |
|
64 #include "lex.h" |
542
|
65 #include "help.h" |
1
|
66 #include "octave.h" |
|
67 #include "parse.h" |
666
|
68 #include "defaults.h" |
721
|
69 #include "user-prefs.h" |
562
|
70 #include "procstream.h" |
1
|
71 #include "unwind-prot.h" |
|
72 #include "octave-hist.h" |
529
|
73 #include "builtins.h" |
1
|
74 #include "version.h" |
|
75 #include "file-io.h" |
|
76 #include "sysdep.h" |
529
|
77 #include "defun.h" |
1
|
78 |
318
|
79 #if !defined (HAVE_ATEXIT) && defined (HAVE_ON_EXIT) |
1
|
80 extern "C" { int on_exit (); } |
|
81 #define atexit on_exit |
|
82 #endif |
|
83 |
|
84 // argv[0] for this program. |
529
|
85 char *raw_prog_name = 0; |
1
|
86 |
|
87 // Cleaned-up name of this program, not including path information. |
529
|
88 char *prog_name = 0; |
1
|
89 |
|
90 // Login name for user running this program. |
529
|
91 char *user_name = 0; |
1
|
92 |
|
93 // Name of the host we are running on. |
529
|
94 char *host_name = 0; |
1
|
95 |
|
96 // User's home directory. |
529
|
97 char *home_directory = 0; |
1
|
98 |
|
99 // Guess what? |
529
|
100 char *the_current_working_directory = 0; |
1
|
101 |
616
|
102 // Load path specified on command line. (--path path; -p path) |
529
|
103 char *load_path = 0; |
1
|
104 |
186
|
105 // Name of the info file specified on command line. |
616
|
106 // (--info-file file; -i file) |
529
|
107 char *info_file = 0; |
186
|
108 |
195
|
109 // Name of the editor to be invoked by the edit_history command. |
529
|
110 char *editor = 0; |
195
|
111 |
1
|
112 // If nonzero, don't do fancy line editing. |
|
113 int no_line_editing = 0; |
|
114 |
|
115 // Command number, counting from the beginning of this session. |
|
116 int current_command_number = 1; |
|
117 |
|
118 // Nonzero means we are exiting via the builtin exit or quit functions. |
|
119 int quitting_gracefully = 0; |
|
120 |
|
121 // Current command to execute. |
578
|
122 tree_statement_list *global_command = 0; |
1
|
123 |
206
|
124 // Pointer to function that is currently being evaluated. |
529
|
125 tree_function *curr_function = 0; |
206
|
126 |
315
|
127 // Nonzero means input is coming from startup file. |
|
128 int input_from_startup_file = 0; |
|
129 |
1
|
130 // Top level context (?) |
|
131 jmp_buf toplevel; |
|
132 |
195
|
133 // This is not really the right place to do this... |
|
134 typedef void (*one_arg_error_handler_t) (const char*); |
|
135 extern one_arg_error_handler_t set_Complex_error_handler |
|
136 (one_arg_error_handler_t f); |
|
137 |
287
|
138 // This is from readline's paren.c: |
|
139 extern int rl_blink_matching_paren; |
|
140 |
195
|
141 static void |
|
142 octave_Complex_error_handler (const char* msg) |
|
143 { |
|
144 warning (msg); |
|
145 } |
|
146 |
1
|
147 // Nonzero means we read ~/.octaverc and ./.octaverc. |
616
|
148 // (--norc; --ignore-init-file; -f) |
1
|
149 static int read_init_files = 1; |
|
150 |
578
|
151 // Nonzero means we printed messages about reading startup files. |
|
152 static int reading_startup_message_printed = 0; |
|
153 |
1
|
154 // Nonzero means we don\'t print the usual startup message. |
616
|
155 // (--quiet; --silent; -q) |
1
|
156 static int inhibit_startup_message = 0; |
|
157 |
|
158 // Usage message |
139
|
159 static const char *usage_string = |
|
160 "octave [-?dfhiqvx] [-p path] [--debug] [--help] [--interactive]\n\ |
186
|
161 [--info-file file] [--norc] [--path path] [--quiet] [--version]\n\ |
|
162 [--echo-commands] [file]"; |
1
|
163 |
|
164 // This is here so that it\'s more likely that the usage message and |
|
165 // the real set of options will agree. |
139
|
166 static const char *short_opts = "?dfhip:qvx"; |
|
167 |
|
168 // Long options. |
186
|
169 #define INFO_FILE_OPTION 1 |
139
|
170 static struct option long_opts[] = |
|
171 { |
|
172 { "debug", 0, 0, 'd' }, |
|
173 { "help", 0, 0, 'h' }, |
|
174 { "interactive", 0, 0, 'i' }, |
186
|
175 { "info-file", 1, 0, INFO_FILE_OPTION }, |
139
|
176 { "norc", 0, 0, 'f' }, |
614
|
177 { "ignore-init-file", 0, 0, 'f' }, |
139
|
178 { "path", 1, 0, 'p' }, |
|
179 { "quiet", 0, 0, 'q' }, |
614
|
180 { "silent", 0, 0, 'q' }, |
139
|
181 { "version", 0, 0, 'v' }, |
|
182 { "echo-commands", 0, 0, 'x' }, |
|
183 { 0, 0, 0, 0 } |
|
184 }; |
1
|
185 |
581
|
186 // Initialize some global variables for later use. |
|
187 |
1
|
188 static void |
|
189 initialize_globals (char *name) |
|
190 { |
|
191 struct passwd *entry = getpwuid (getuid ()); |
|
192 if (entry) |
|
193 user_name = strsave (entry->pw_name); |
|
194 else |
|
195 user_name = strsave ("I have no name!"); |
|
196 endpwent (); |
|
197 |
|
198 char hostname[256]; |
|
199 if (gethostname (hostname, 255) < 0) |
|
200 host_name = strsave ("I have no host!"); |
|
201 else |
|
202 host_name = strsave (hostname); |
|
203 |
|
204 char *hd = getenv ("HOME"); |
529
|
205 if (hd) |
|
206 home_directory = strsave (hd); |
1
|
207 else |
529
|
208 home_directory = strsave ("I have no home!"); |
1
|
209 |
686
|
210 char *shell_path = getenv ("PATH"); |
|
211 char *arch_dir = octave_arch_lib_dir (); |
|
212 char *tmp = strconcat (shell_path, ":"); |
|
213 |
|
214 shell_path = shell_path ? strconcat (tmp, arch_dir) |
|
215 : strsave (arch_dir); |
|
216 |
|
217 delete [] tmp; |
|
218 |
|
219 char *putenv_command = strconcat ("PATH=", shell_path); |
|
220 |
|
221 putenv (putenv_command); |
|
222 |
1
|
223 raw_prog_name = strsave (name); |
|
224 prog_name = strsave ("octave"); |
|
225 |
|
226 load_path = default_path (); |
186
|
227 |
|
228 info_file = default_info_file (); |
195
|
229 |
|
230 editor = default_editor (); |
1
|
231 } |
|
232 |
|
233 void |
|
234 parse_and_execute (FILE *f, int print) |
|
235 { |
|
236 begin_unwind_frame ("parse_and_execute"); |
|
237 |
|
238 YY_BUFFER_STATE old_buf = current_buffer (); |
|
239 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
240 |
|
241 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
242 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
243 |
|
244 switch_to_buffer (new_buf); |
|
245 |
|
246 unwind_protect_int (echo_input); |
|
247 unwind_protect_int (using_readline); |
|
248 unwind_protect_int (saving_history); |
|
249 |
|
250 echo_input = 0; |
|
251 using_readline = 0; |
|
252 saving_history = 0; |
|
253 |
|
254 unwind_protect_ptr (curr_sym_tab); |
|
255 |
|
256 int retval; |
|
257 do |
|
258 { |
191
|
259 reset_parser (); |
1
|
260 retval = yyparse (); |
529
|
261 if (retval == 0 && global_command) |
1
|
262 { |
|
263 global_command->eval (print); |
|
264 delete global_command; |
|
265 } |
|
266 } |
|
267 while (retval == 0); |
|
268 |
|
269 run_unwind_frame ("parse_and_execute"); |
|
270 } |
|
271 |
|
272 void |
578
|
273 parse_and_execute (char *s, int print, int verbose) |
1
|
274 { |
|
275 begin_unwind_frame ("parse_and_execute_2"); |
|
276 |
|
277 unwind_protect_int (reading_script_file); |
|
278 |
|
279 reading_script_file = 1; |
|
280 |
|
281 FILE *f = get_input_from_file (s, 0); |
529
|
282 if (f) |
1
|
283 { |
|
284 unwind_protect_int (input_line_number); |
|
285 unwind_protect_int (current_input_column); |
|
286 unwind_protect_int (echo_input); |
|
287 |
|
288 input_line_number = 0; |
143
|
289 current_input_column = 1; |
1
|
290 echo_input = 0; |
|
291 |
578
|
292 if (verbose) |
|
293 { |
|
294 cout << "Reading commands from " << s << " ... "; |
|
295 reading_startup_message_printed = 1; |
|
296 cout.flush (); |
|
297 } |
|
298 |
1
|
299 parse_and_execute (f, print); |
578
|
300 |
|
301 if (verbose) |
|
302 cout << "done." << endl; |
1
|
303 } |
|
304 |
|
305 run_unwind_frame ("parse_and_execute_2"); |
|
306 } |
|
307 |
581
|
308 // Initialize by reading startup files. |
|
309 |
1
|
310 static void |
|
311 execute_startup_files (void) |
|
312 { |
315
|
313 begin_unwind_frame ("execute_startup_files"); |
|
314 |
|
315 unwind_protect_int (input_from_startup_file); |
|
316 input_from_startup_file = 1; |
|
317 |
578
|
318 int verbose = ! inhibit_startup_message; |
|
319 |
1
|
320 // Execute commands from the site-wide configuration file. |
|
321 |
|
322 char *sd = get_site_defaults (); |
|
323 |
578
|
324 parse_and_execute (sd, 0, verbose); |
1
|
325 |
|
326 // Try to execute commands from $HOME/.octaverc and ./.octaverc. |
|
327 |
529
|
328 char *home_rc = 0; |
|
329 if (home_directory) |
1
|
330 { |
85
|
331 home_rc = strconcat (home_directory, "/.octaverc"); |
578
|
332 parse_and_execute (home_rc, 0, verbose); |
1
|
333 } |
|
334 |
85
|
335 // Names alone are not enough. |
|
336 |
|
337 struct stat home_rc_statbuf; |
|
338 stat (home_rc, &home_rc_statbuf); |
|
339 delete [] home_rc; |
|
340 |
|
341 struct stat dot_rc_statbuf; |
|
342 stat ("./.octaverc", &dot_rc_statbuf); |
|
343 |
|
344 if (home_rc_statbuf.st_ino != dot_rc_statbuf.st_ino) |
578
|
345 parse_and_execute ("./.octaverc", 0, verbose); |
315
|
346 |
|
347 run_unwind_frame ("execute_startup_files"); |
1
|
348 } |
|
349 |
581
|
350 // Usage message with extra help. |
|
351 |
1
|
352 static void |
|
353 verbose_usage (void) |
|
354 { |
|
355 cout << "\n" |
|
356 << " Octave, version " << version_string |
268
|
357 << ". Copyright (C) 1992, 1993, 1994 John W. Eaton.\n" |
1
|
358 << " This is free software with ABSOLUTELY NO WARRANTY.\n" |
|
359 << "\n" |
139
|
360 << " usage: " << usage_string |
|
361 << "\n\n" |
1
|
362 << " d : enter parser debugging mode\n" |
|
363 << " f : don't read ~/.octaverc or .octaverc at startup\n" |
|
364 << " h|? : print short help message and exit\n" |
|
365 << " i : force interactive behavior\n" |
|
366 << " q : don't print message at startup\n" |
139
|
367 << " v : print version number and exit\n" |
1
|
368 << " x : echo commands as they are executed\n" |
|
369 << "\n" |
|
370 << " file : execute commands from named file\n" |
285
|
371 << "\n"; |
|
372 |
613
|
373 exit (0); |
1
|
374 } |
|
375 |
581
|
376 // Terse usage messsage. |
|
377 |
1
|
378 static void |
|
379 usage (void) |
|
380 { |
613
|
381 cerr << "usage: " << usage_string << "\n"; |
1
|
382 exit (1); |
|
383 } |
|
384 |
581
|
385 // Fix up things before exiting. |
|
386 |
195
|
387 void |
1
|
388 clean_up_and_exit (int retval) |
|
389 { |
|
390 raw_mode (0); |
176
|
391 |
1
|
392 clean_up_history (); |
176
|
393 |
1
|
394 close_plot_stream (); |
176
|
395 |
581
|
396 close_diary_file (); |
|
397 |
1
|
398 close_files (); |
|
399 |
318
|
400 cleanup_tmp_files (); |
|
401 |
1
|
402 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
403 cout << "\n"; |
|
404 |
|
405 if (retval == EOF) |
|
406 retval = 0; |
|
407 |
|
408 exit (retval); |
195
|
409 |
|
410 // This is bogus but should prevent g++ from giving a warning saying |
|
411 // that this volatile function does return. |
|
412 |
|
413 panic_impossible (); |
1
|
414 } |
|
415 |
|
416 static void |
|
417 print_version_and_exit (void) |
|
418 { |
|
419 cout << "octave, version " << version_string << "\n"; |
|
420 exit (0); |
|
421 } |
|
422 |
721
|
423 static void |
|
424 initialize_error_handlers () |
|
425 { |
|
426 set_Complex_error_handler (octave_Complex_error_handler); |
|
427 |
|
428 set_liboctave_error_handler (error); |
|
429 } |
|
430 |
581
|
431 // You guessed it. |
|
432 |
1
|
433 int |
|
434 main (int argc, char **argv) |
|
435 { |
721
|
436 // The order of these calls is important, and initialize globals must |
|
437 // come before the options are processed because some command line |
|
438 // options override defaults. |
|
439 |
|
440 init_user_prefs (); |
|
441 |
|
442 initialize_pager (); |
|
443 |
1
|
444 sysdep_init (); |
|
445 |
721
|
446 initialize_error_handlers (); |
223
|
447 |
1
|
448 initialize_globals (argv[0]); |
|
449 |
139
|
450 int optc; |
|
451 while ((optc = getopt_long (argc, argv, short_opts, long_opts, 0)) != EOF) |
1
|
452 { |
139
|
453 switch (optc) |
1
|
454 { |
|
455 case 'd': |
|
456 yydebug++; |
|
457 break; |
777
|
458 |
1
|
459 case 'f': |
|
460 read_init_files = 0; |
|
461 break; |
777
|
462 |
1
|
463 case 'h': |
|
464 case '?': |
|
465 verbose_usage (); |
|
466 break; |
777
|
467 |
1
|
468 case 'i': |
|
469 forced_interactive = 1; |
|
470 break; |
777
|
471 |
1
|
472 case 'p': |
529
|
473 if (optarg) |
139
|
474 load_path = strsave (optarg); |
1
|
475 break; |
777
|
476 |
1
|
477 case 'q': |
|
478 inhibit_startup_message = 1; |
|
479 break; |
777
|
480 |
1
|
481 case 'x': |
|
482 echo_input = 1; |
|
483 break; |
777
|
484 |
1
|
485 case 'v': |
|
486 print_version_and_exit (); |
|
487 break; |
777
|
488 |
186
|
489 case INFO_FILE_OPTION: |
529
|
490 if (optarg) |
186
|
491 info_file = strsave (optarg); |
|
492 break; |
777
|
493 |
1
|
494 default: |
|
495 usage (); |
|
496 break; |
|
497 } |
|
498 } |
|
499 |
318
|
500 #if defined (HAVE_ATEXIT) || (HAVE_ON_EXIT) |
|
501 // Make sure we clean up when we exit. If we don't have atexit or |
|
502 // on_exit, we're going to leave some junk files around if we exit |
|
503 // abnormally. |
1
|
504 atexit (cleanup_tmp_files); |
318
|
505 #endif |
1
|
506 |
721
|
507 // These can come after command line args since none of them set any |
|
508 // defaults that might be changed by command line options. |
581
|
509 |
636
|
510 install_signal_handlers (); |
|
511 |
1
|
512 initialize_history (); |
|
513 |
|
514 initialize_file_io (); |
|
515 |
581
|
516 initialize_symbol_tables (); |
1
|
517 |
|
518 install_builtins (); |
|
519 |
315
|
520 initialize_readline (); |
|
521 |
721
|
522 init_dynamic_linker (); |
|
523 |
578
|
524 if (! inhibit_startup_message) |
|
525 cout << "Octave, version " << version_string |
|
526 << ". Copyright (C) 1992, 1993, 1994 John W. Eaton.\n" |
|
527 << "This is free software with ABSOLUTELY NO WARRANTY.\n" |
|
528 << "For details, type `warranty'.\n" << endl; |
|
529 |
1
|
530 if (read_init_files) |
|
531 { |
|
532 saving_history = 0; |
|
533 execute_startup_files (); |
|
534 saving_history = 1; |
|
535 } |
|
536 |
578
|
537 if (! inhibit_startup_message && reading_startup_message_printed) |
|
538 cout << endl; |
|
539 |
1
|
540 // Avoid counting commands executed from startup files. |
|
541 current_command_number = 1; |
|
542 |
|
543 // If there is an extra argument, see if it names a file to read. |
|
544 |
149
|
545 int remaining_args = argc - optind; |
|
546 if (remaining_args > 1) |
|
547 { |
|
548 usage (); |
|
549 } |
|
550 else if (remaining_args == 1) |
1
|
551 { |
139
|
552 FILE *infile = get_input_from_file (argv[optind]); |
529
|
553 if (infile) |
287
|
554 { |
|
555 rl_blink_matching_paren = 0; |
|
556 switch_to_buffer (create_buffer (infile)); |
|
557 } |
529
|
558 else |
|
559 clean_up_and_exit (1); |
1
|
560 } |
|
561 else |
|
562 { |
149
|
563 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
|
564 |
1
|
565 // Is input coming from a terminal? If so, we are probably |
|
566 // interactive. |
|
567 |
|
568 interactive = (isatty (fileno (stdin)) && isatty (fileno (stdout))); |
|
569 } |
|
570 |
|
571 // Force input to be echoed if not really interactive, but the user |
|
572 // has forced interactive behavior. |
|
573 |
|
574 if (!interactive && forced_interactive) |
287
|
575 { |
|
576 rl_blink_matching_paren = 0; |
|
577 echo_input = 1; |
|
578 } |
1
|
579 |
|
580 if (! (interactive || forced_interactive)) |
|
581 using_readline = 0; |
|
582 |
|
583 // Allow the user to interrupt us without exiting. |
|
584 |
|
585 volatile sig_handler *saved_sigint_handler = signal (SIGINT, SIG_IGN); |
|
586 |
|
587 if (setjmp (toplevel) != 0) |
|
588 { |
|
589 raw_mode (0); |
176
|
590 |
1
|
591 cout << "\n"; |
|
592 } |
|
593 |
|
594 can_interrupt = 1; |
|
595 |
|
596 signal (SIGINT, saved_sigint_handler); |
|
597 |
|
598 // The big loop. |
|
599 |
|
600 int retval; |
|
601 do |
|
602 { |
287
|
603 curr_sym_tab = top_level_sym_tab; |
|
604 |
1
|
605 reset_parser (); |
287
|
606 |
1
|
607 retval = yyparse (); |
287
|
608 |
529
|
609 if (retval == 0 && global_command) |
1
|
610 { |
|
611 global_command->eval (1); |
|
612 delete global_command; |
|
613 current_command_number++; |
|
614 } |
|
615 } |
|
616 while (retval == 0); |
|
617 |
|
618 clean_up_and_exit (retval); |
|
619 } |
|
620 |
529
|
621 DEFUN_TEXT ("casesen", Fcasesen, Scasesen, 2, 1, |
|
622 "casesen [on|off]") |
|
623 { |
|
624 Octave_object retval; |
|
625 |
|
626 DEFINE_ARGV("casesen"); |
|
627 |
|
628 if (argc == 1 || (argc > 1 && strcmp (argv[1], "off") == 0)) |
|
629 warning ("casesen: sorry, Octave is always case sensitive"); |
|
630 else if (argc > 1 && strcmp (argv[1], "on") == 0) |
|
631 ; // ok. |
|
632 else |
|
633 print_usage ("casesen"); |
|
634 |
|
635 DELETE_ARGV; |
|
636 |
|
637 return retval; |
|
638 } |
|
639 |
666
|
640 DEFUN ("computer", Fcomputer, Scomputer, 1, 0, |
|
641 "computer ():\n\ |
|
642 \n\ |
|
643 Have Octave ask the system, \"What kind of computer are you?\"") |
|
644 { |
|
645 Octave_object retval; |
|
646 |
712
|
647 int nargin = args.length (); |
|
648 |
|
649 if (nargin != 0) |
666
|
650 warning ("computer: ignoring extra arguments"); |
|
651 |
|
652 ostrstream output_buf; |
|
653 |
|
654 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
655 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
656 else |
|
657 output_buf << TARGET_HOST_TYPE; |
|
658 |
|
659 if (nargout == 0) |
|
660 { |
|
661 output_buf << "\n" << ends; |
|
662 maybe_page_output (output_buf); |
|
663 } |
|
664 else |
|
665 { |
|
666 char *msg = output_buf.str (); |
|
667 retval = msg; |
|
668 delete [] msg; |
|
669 } |
|
670 |
|
671 return retval; |
|
672 } |
|
673 |
712
|
674 DEFUN ("flops", Fflops, Sflops, 0, 1, |
529
|
675 "flops (): count floating point operations") |
|
676 { |
|
677 int nargin = args.length (); |
|
678 |
712
|
679 if (nargin > 0) |
529
|
680 print_usage ("flops"); |
|
681 |
|
682 warning ("flops is a flop, always returning zero"); |
|
683 |
|
684 return 0.0; |
|
685 } |
|
686 |
712
|
687 DEFUN ("quit", Fquit, Squit, 0, 0, |
529
|
688 "quit (): exit Octave gracefully") |
|
689 { |
|
690 Octave_object retval; |
|
691 quitting_gracefully = 1; |
|
692 clean_up_and_exit (0); |
|
693 return retval; |
|
694 } |
|
695 |
549
|
696 DEFALIAS (exit, quit); |
|
697 |
712
|
698 DEFUN ("warranty", Fwarranty, Swarranty, 0, 0, |
529
|
699 "warranty (): describe copying conditions") |
|
700 { |
|
701 Octave_object retval; |
|
702 |
|
703 ostrstream output_buf; |
|
704 output_buf << "\n Octave, version " << version_string |
|
705 << ". Copyright (C) 1992, 1993, 1994 John W. Eaton\n" |
|
706 << "\n\ |
|
707 This program is free software; you can redistribute it and/or modify\n\ |
|
708 it under the terms of the GNU General Public License as published by\n\ |
|
709 the Free Software Foundation; either version 2 of the License, or\n\ |
|
710 (at your option) any later version.\n\ |
|
711 \n\ |
|
712 This program is distributed in the hope that it will be useful,\n\ |
|
713 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
714 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
715 GNU General Public License for more details.\n\ |
|
716 \n\ |
|
717 You should have received a copy of the GNU General Public License\n\ |
|
718 along with this program. If not, write to the Free Software\n\ |
|
719 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.\n\ |
|
720 \n"; |
|
721 |
|
722 output_buf << ends; |
|
723 maybe_page_output (output_buf); |
|
724 |
|
725 return retval; |
|
726 } |
|
727 |
|
728 // XXX FIXME XXX -- this may not be the best place for these... |
|
729 |
|
730 Octave_object |
|
731 feval (const Octave_object& args, int nargout) |
|
732 { |
|
733 Octave_object retval; |
|
734 |
712
|
735 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
529
|
736 if (fcn) |
|
737 { |
712
|
738 int tmp_nargin = args.length () - 1; |
565
|
739 Octave_object tmp_args; |
712
|
740 tmp_args.resize (tmp_nargin); |
|
741 for (int i = 0; i < tmp_nargin; i++) |
529
|
742 tmp_args(i) = args(i+1); |
|
743 retval = fcn->eval (0, nargout, tmp_args); |
|
744 } |
|
745 |
|
746 return retval; |
|
747 } |
|
748 |
|
749 DEFUN ("feval", Ffeval, Sfeval, -1, 1, |
|
750 "feval (NAME, ARGS, ...)\n\ |
|
751 \n\ |
|
752 evaluate NAME as a function, passing ARGS as its arguments") |
|
753 { |
|
754 Octave_object retval; |
|
755 |
|
756 int nargin = args.length (); |
|
757 |
712
|
758 if (nargin > 0) |
529
|
759 retval = feval (args, nargout); |
|
760 else |
|
761 print_usage ("feval"); |
|
762 |
|
763 return retval; |
|
764 } |
|
765 |
672
|
766 static Octave_object |
529
|
767 eval_string (const char *string, int print, int ans_assign, |
672
|
768 int& parse_status, int nargout) |
529
|
769 { |
|
770 begin_unwind_frame ("eval_string"); |
|
771 |
|
772 unwind_protect_int (get_input_from_eval_string); |
|
773 unwind_protect_ptr (global_command); |
|
774 unwind_protect_ptr (current_eval_string); |
|
775 |
|
776 get_input_from_eval_string = 1; |
|
777 current_eval_string = string; |
|
778 |
|
779 YY_BUFFER_STATE old_buf = current_buffer (); |
|
780 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
781 |
|
782 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
783 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
784 |
|
785 switch_to_buffer (new_buf); |
|
786 |
|
787 unwind_protect_ptr (curr_sym_tab); |
|
788 |
|
789 reset_parser (); |
|
790 |
|
791 parse_status = yyparse (); |
|
792 |
|
793 // Important to reset the idea of where input is coming from before |
|
794 // trying to eval the command we just parsed -- it might contain the |
|
795 // name of an function file that still needs to be parsed! |
|
796 |
578
|
797 tree_statement_list *command = global_command; |
529
|
798 |
|
799 run_unwind_frame ("eval_string"); |
|
800 |
672
|
801 Octave_object retval; |
529
|
802 |
|
803 if (parse_status == 0 && command) |
|
804 { |
672
|
805 retval = command->eval (print, nargout); |
529
|
806 delete command; |
|
807 } |
|
808 |
|
809 return retval; |
|
810 } |
|
811 |
|
812 tree_constant |
672
|
813 eval_string (const char *string, int print, int ans_assign, |
|
814 int& parse_status) |
|
815 { |
|
816 tree_constant retval; |
|
817 |
|
818 Octave_object tmp = eval_string (string, print, ans_assign, |
|
819 parse_status, 1); |
|
820 |
|
821 retval = tmp(0); |
|
822 |
|
823 return retval; |
|
824 } |
|
825 |
|
826 static Octave_object |
|
827 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
529
|
828 { |
636
|
829 char *string = arg.string_value (); |
|
830 |
|
831 if (error_state) |
529
|
832 { |
|
833 error ("eval: expecting string argument"); |
672
|
834 return -1.0; |
529
|
835 } |
|
836 |
|
837 // Yes Virginia, we always print here... |
|
838 |
672
|
839 return eval_string (string, 1, 1, parse_status, nargout); |
529
|
840 } |
|
841 |
712
|
842 DEFUN ("eval", Feval, Seval, 2, 1, |
672
|
843 "eval (TRY, CATCH)\n\ |
|
844 \n\ |
|
845 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
846 string CATCH.") |
529
|
847 { |
|
848 Octave_object retval; |
|
849 |
|
850 int nargin = args.length (); |
|
851 |
712
|
852 if (nargin > 0) |
529
|
853 { |
672
|
854 begin_unwind_frame ("Feval"); |
|
855 |
712
|
856 if (nargin > 1) |
672
|
857 { |
|
858 unwind_protect_int (suppress_octave_error_messages); |
|
859 suppress_octave_error_messages = 1; |
|
860 } |
|
861 |
529
|
862 int parse_status = 0; |
672
|
863 |
712
|
864 retval = eval_string (args(0), parse_status, nargout); |
672
|
865 |
712
|
866 if (nargin > 1 && (parse_status != 0 || error_state)) |
672
|
867 { |
|
868 error_state = 0; |
712
|
869 eval_string (args(1), parse_status, nargout); |
672
|
870 retval = Octave_object (); |
|
871 } |
|
872 |
|
873 run_unwind_frame ("Feval"); |
529
|
874 } |
|
875 else |
|
876 print_usage ("eval"); |
|
877 |
|
878 return retval; |
|
879 } |
|
880 |
581
|
881 // Execute a shell command. |
|
882 |
703
|
883 DEFUN ("system", Fsystem, Ssystem, 2, 1, |
|
884 "system (string [, return_output]): execute shell commands") |
560
|
885 { |
|
886 Octave_object retval; |
|
887 |
|
888 int nargin = args.length (); |
|
889 |
712
|
890 if (nargin < 1 || nargin > 2) |
560
|
891 { |
712
|
892 print_usage ("system"); |
624
|
893 return retval; |
|
894 } |
|
895 |
712
|
896 tree_constant tc_command = args(0); |
624
|
897 |
636
|
898 char *tmp_str = tc_command.string_value (); |
|
899 |
|
900 if (error_state) |
624
|
901 { |
712
|
902 error ("system: expecting string as first argument"); |
636
|
903 } |
|
904 else |
|
905 { |
|
906 iprocstream cmd (tmp_str); |
624
|
907 |
|
908 ostrstream output_buf; |
|
909 |
560
|
910 char ch; |
|
911 while (cmd.get (ch)) |
|
912 output_buf.put (ch); |
624
|
913 |
560
|
914 output_buf << ends; |
624
|
915 |
560
|
916 int status = cmd.close (); |
624
|
917 |
712
|
918 if (nargout > 0 || nargin > 1) |
560
|
919 { |
624
|
920 char *msg = output_buf.str (); |
|
921 |
|
922 retval(1) = (double) status; |
|
923 retval(0) = msg; |
|
924 |
|
925 delete [] msg; |
560
|
926 } |
624
|
927 else |
|
928 maybe_page_output (output_buf); |
560
|
929 } |
|
930 |
|
931 return retval; |
|
932 } |
|
933 |
703
|
934 DEFALIAS (shell_cmd, system); |
|
935 |
560
|
936 /* |
1
|
937 ;;; Local Variables: *** |
|
938 ;;; mode: C++ *** |
|
939 ;;; page-delimiter: "^/\\*" *** |
|
940 ;;; End: *** |
|
941 */ |