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; |
|
458 case 'f': |
|
459 read_init_files = 0; |
|
460 break; |
|
461 case 'h': |
|
462 case '?': |
|
463 verbose_usage (); |
|
464 break; |
|
465 case 'i': |
|
466 forced_interactive = 1; |
|
467 break; |
|
468 case 'p': |
529
|
469 if (optarg) |
139
|
470 load_path = strsave (optarg); |
1
|
471 break; |
|
472 case 'q': |
|
473 inhibit_startup_message = 1; |
|
474 break; |
|
475 case 'x': |
|
476 echo_input = 1; |
|
477 break; |
|
478 case 'v': |
|
479 print_version_and_exit (); |
|
480 break; |
186
|
481 case INFO_FILE_OPTION: |
529
|
482 if (optarg) |
186
|
483 info_file = strsave (optarg); |
|
484 break; |
1
|
485 default: |
|
486 usage (); |
|
487 break; |
|
488 } |
|
489 } |
|
490 |
318
|
491 #if defined (HAVE_ATEXIT) || (HAVE_ON_EXIT) |
|
492 // Make sure we clean up when we exit. If we don't have atexit or |
|
493 // on_exit, we're going to leave some junk files around if we exit |
|
494 // abnormally. |
1
|
495 atexit (cleanup_tmp_files); |
318
|
496 #endif |
1
|
497 |
721
|
498 // These can come after command line args since none of them set any |
|
499 // defaults that might be changed by command line options. |
581
|
500 |
636
|
501 install_signal_handlers (); |
|
502 |
1
|
503 initialize_history (); |
|
504 |
|
505 initialize_file_io (); |
|
506 |
581
|
507 initialize_symbol_tables (); |
1
|
508 |
|
509 install_builtins (); |
|
510 |
315
|
511 initialize_readline (); |
|
512 |
721
|
513 init_dynamic_linker (); |
|
514 |
578
|
515 if (! inhibit_startup_message) |
|
516 cout << "Octave, version " << version_string |
|
517 << ". Copyright (C) 1992, 1993, 1994 John W. Eaton.\n" |
|
518 << "This is free software with ABSOLUTELY NO WARRANTY.\n" |
|
519 << "For details, type `warranty'.\n" << endl; |
|
520 |
1
|
521 if (read_init_files) |
|
522 { |
|
523 saving_history = 0; |
|
524 execute_startup_files (); |
|
525 saving_history = 1; |
|
526 } |
|
527 |
578
|
528 if (! inhibit_startup_message && reading_startup_message_printed) |
|
529 cout << endl; |
|
530 |
1
|
531 // Avoid counting commands executed from startup files. |
|
532 current_command_number = 1; |
|
533 |
|
534 // If there is an extra argument, see if it names a file to read. |
|
535 |
149
|
536 int remaining_args = argc - optind; |
|
537 if (remaining_args > 1) |
|
538 { |
|
539 usage (); |
|
540 } |
|
541 else if (remaining_args == 1) |
1
|
542 { |
139
|
543 FILE *infile = get_input_from_file (argv[optind]); |
529
|
544 if (infile) |
287
|
545 { |
|
546 rl_blink_matching_paren = 0; |
|
547 switch_to_buffer (create_buffer (infile)); |
|
548 } |
529
|
549 else |
|
550 clean_up_and_exit (1); |
1
|
551 } |
|
552 else |
|
553 { |
149
|
554 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
|
555 |
1
|
556 // Is input coming from a terminal? If so, we are probably |
|
557 // interactive. |
|
558 |
|
559 interactive = (isatty (fileno (stdin)) && isatty (fileno (stdout))); |
|
560 } |
|
561 |
|
562 // Force input to be echoed if not really interactive, but the user |
|
563 // has forced interactive behavior. |
|
564 |
|
565 if (!interactive && forced_interactive) |
287
|
566 { |
|
567 rl_blink_matching_paren = 0; |
|
568 echo_input = 1; |
|
569 } |
1
|
570 |
|
571 if (! (interactive || forced_interactive)) |
|
572 using_readline = 0; |
|
573 |
|
574 // Allow the user to interrupt us without exiting. |
|
575 |
|
576 volatile sig_handler *saved_sigint_handler = signal (SIGINT, SIG_IGN); |
|
577 |
|
578 if (setjmp (toplevel) != 0) |
|
579 { |
|
580 raw_mode (0); |
176
|
581 |
1
|
582 cout << "\n"; |
|
583 } |
|
584 |
|
585 can_interrupt = 1; |
|
586 |
|
587 signal (SIGINT, saved_sigint_handler); |
|
588 |
|
589 // The big loop. |
|
590 |
|
591 int retval; |
|
592 do |
|
593 { |
287
|
594 curr_sym_tab = top_level_sym_tab; |
|
595 |
1
|
596 reset_parser (); |
287
|
597 |
1
|
598 retval = yyparse (); |
287
|
599 |
529
|
600 if (retval == 0 && global_command) |
1
|
601 { |
|
602 global_command->eval (1); |
|
603 delete global_command; |
|
604 current_command_number++; |
|
605 } |
|
606 } |
|
607 while (retval == 0); |
|
608 |
|
609 clean_up_and_exit (retval); |
|
610 } |
|
611 |
529
|
612 DEFUN_TEXT ("casesen", Fcasesen, Scasesen, 2, 1, |
|
613 "casesen [on|off]") |
|
614 { |
|
615 Octave_object retval; |
|
616 |
|
617 DEFINE_ARGV("casesen"); |
|
618 |
|
619 if (argc == 1 || (argc > 1 && strcmp (argv[1], "off") == 0)) |
|
620 warning ("casesen: sorry, Octave is always case sensitive"); |
|
621 else if (argc > 1 && strcmp (argv[1], "on") == 0) |
|
622 ; // ok. |
|
623 else |
|
624 print_usage ("casesen"); |
|
625 |
|
626 DELETE_ARGV; |
|
627 |
|
628 return retval; |
|
629 } |
|
630 |
666
|
631 DEFUN ("computer", Fcomputer, Scomputer, 1, 0, |
|
632 "computer ():\n\ |
|
633 \n\ |
|
634 Have Octave ask the system, \"What kind of computer are you?\"") |
|
635 { |
|
636 Octave_object retval; |
|
637 |
712
|
638 int nargin = args.length (); |
|
639 |
|
640 if (nargin != 0) |
666
|
641 warning ("computer: ignoring extra arguments"); |
|
642 |
|
643 ostrstream output_buf; |
|
644 |
|
645 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
646 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
647 else |
|
648 output_buf << TARGET_HOST_TYPE; |
|
649 |
|
650 if (nargout == 0) |
|
651 { |
|
652 output_buf << "\n" << ends; |
|
653 maybe_page_output (output_buf); |
|
654 } |
|
655 else |
|
656 { |
|
657 char *msg = output_buf.str (); |
|
658 retval = msg; |
|
659 delete [] msg; |
|
660 } |
|
661 |
|
662 return retval; |
|
663 } |
|
664 |
712
|
665 DEFUN ("flops", Fflops, Sflops, 0, 1, |
529
|
666 "flops (): count floating point operations") |
|
667 { |
|
668 int nargin = args.length (); |
|
669 |
712
|
670 if (nargin > 0) |
529
|
671 print_usage ("flops"); |
|
672 |
|
673 warning ("flops is a flop, always returning zero"); |
|
674 |
|
675 return 0.0; |
|
676 } |
|
677 |
712
|
678 DEFUN ("quit", Fquit, Squit, 0, 0, |
529
|
679 "quit (): exit Octave gracefully") |
|
680 { |
|
681 Octave_object retval; |
|
682 quitting_gracefully = 1; |
|
683 clean_up_and_exit (0); |
|
684 return retval; |
|
685 } |
|
686 |
549
|
687 DEFALIAS (exit, quit); |
|
688 |
712
|
689 DEFUN ("warranty", Fwarranty, Swarranty, 0, 0, |
529
|
690 "warranty (): describe copying conditions") |
|
691 { |
|
692 Octave_object retval; |
|
693 |
|
694 ostrstream output_buf; |
|
695 output_buf << "\n Octave, version " << version_string |
|
696 << ". Copyright (C) 1992, 1993, 1994 John W. Eaton\n" |
|
697 << "\n\ |
|
698 This program is free software; you can redistribute it and/or modify\n\ |
|
699 it under the terms of the GNU General Public License as published by\n\ |
|
700 the Free Software Foundation; either version 2 of the License, or\n\ |
|
701 (at your option) any later version.\n\ |
|
702 \n\ |
|
703 This program is distributed in the hope that it will be useful,\n\ |
|
704 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
705 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
706 GNU General Public License for more details.\n\ |
|
707 \n\ |
|
708 You should have received a copy of the GNU General Public License\n\ |
|
709 along with this program. If not, write to the Free Software\n\ |
|
710 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.\n\ |
|
711 \n"; |
|
712 |
|
713 output_buf << ends; |
|
714 maybe_page_output (output_buf); |
|
715 |
|
716 return retval; |
|
717 } |
|
718 |
|
719 // XXX FIXME XXX -- this may not be the best place for these... |
|
720 |
|
721 Octave_object |
|
722 feval (const Octave_object& args, int nargout) |
|
723 { |
|
724 Octave_object retval; |
|
725 |
712
|
726 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
529
|
727 if (fcn) |
|
728 { |
712
|
729 int tmp_nargin = args.length () - 1; |
565
|
730 Octave_object tmp_args; |
712
|
731 tmp_args.resize (tmp_nargin); |
|
732 for (int i = 0; i < tmp_nargin; i++) |
529
|
733 tmp_args(i) = args(i+1); |
|
734 retval = fcn->eval (0, nargout, tmp_args); |
|
735 } |
|
736 |
|
737 return retval; |
|
738 } |
|
739 |
|
740 DEFUN ("feval", Ffeval, Sfeval, -1, 1, |
|
741 "feval (NAME, ARGS, ...)\n\ |
|
742 \n\ |
|
743 evaluate NAME as a function, passing ARGS as its arguments") |
|
744 { |
|
745 Octave_object retval; |
|
746 |
|
747 int nargin = args.length (); |
|
748 |
712
|
749 if (nargin > 0) |
529
|
750 retval = feval (args, nargout); |
|
751 else |
|
752 print_usage ("feval"); |
|
753 |
|
754 return retval; |
|
755 } |
|
756 |
672
|
757 static Octave_object |
529
|
758 eval_string (const char *string, int print, int ans_assign, |
672
|
759 int& parse_status, int nargout) |
529
|
760 { |
|
761 begin_unwind_frame ("eval_string"); |
|
762 |
|
763 unwind_protect_int (get_input_from_eval_string); |
|
764 unwind_protect_ptr (global_command); |
|
765 unwind_protect_ptr (current_eval_string); |
|
766 |
|
767 get_input_from_eval_string = 1; |
|
768 current_eval_string = string; |
|
769 |
|
770 YY_BUFFER_STATE old_buf = current_buffer (); |
|
771 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
772 |
|
773 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
774 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
775 |
|
776 switch_to_buffer (new_buf); |
|
777 |
|
778 unwind_protect_ptr (curr_sym_tab); |
|
779 |
|
780 reset_parser (); |
|
781 |
|
782 parse_status = yyparse (); |
|
783 |
|
784 // Important to reset the idea of where input is coming from before |
|
785 // trying to eval the command we just parsed -- it might contain the |
|
786 // name of an function file that still needs to be parsed! |
|
787 |
578
|
788 tree_statement_list *command = global_command; |
529
|
789 |
|
790 run_unwind_frame ("eval_string"); |
|
791 |
672
|
792 Octave_object retval; |
529
|
793 |
|
794 if (parse_status == 0 && command) |
|
795 { |
672
|
796 retval = command->eval (print, nargout); |
529
|
797 delete command; |
|
798 } |
|
799 |
|
800 return retval; |
|
801 } |
|
802 |
|
803 tree_constant |
672
|
804 eval_string (const char *string, int print, int ans_assign, |
|
805 int& parse_status) |
|
806 { |
|
807 tree_constant retval; |
|
808 |
|
809 Octave_object tmp = eval_string (string, print, ans_assign, |
|
810 parse_status, 1); |
|
811 |
|
812 retval = tmp(0); |
|
813 |
|
814 return retval; |
|
815 } |
|
816 |
|
817 static Octave_object |
|
818 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
529
|
819 { |
636
|
820 char *string = arg.string_value (); |
|
821 |
|
822 if (error_state) |
529
|
823 { |
|
824 error ("eval: expecting string argument"); |
672
|
825 return -1.0; |
529
|
826 } |
|
827 |
|
828 // Yes Virginia, we always print here... |
|
829 |
672
|
830 return eval_string (string, 1, 1, parse_status, nargout); |
529
|
831 } |
|
832 |
712
|
833 DEFUN ("eval", Feval, Seval, 2, 1, |
672
|
834 "eval (TRY, CATCH)\n\ |
|
835 \n\ |
|
836 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
837 string CATCH.") |
529
|
838 { |
|
839 Octave_object retval; |
|
840 |
|
841 int nargin = args.length (); |
|
842 |
712
|
843 if (nargin > 0) |
529
|
844 { |
672
|
845 begin_unwind_frame ("Feval"); |
|
846 |
712
|
847 if (nargin > 1) |
672
|
848 { |
|
849 unwind_protect_int (suppress_octave_error_messages); |
|
850 suppress_octave_error_messages = 1; |
|
851 } |
|
852 |
529
|
853 int parse_status = 0; |
672
|
854 |
712
|
855 retval = eval_string (args(0), parse_status, nargout); |
672
|
856 |
712
|
857 if (nargin > 1 && (parse_status != 0 || error_state)) |
672
|
858 { |
|
859 error_state = 0; |
712
|
860 eval_string (args(1), parse_status, nargout); |
672
|
861 retval = Octave_object (); |
|
862 } |
|
863 |
|
864 run_unwind_frame ("Feval"); |
529
|
865 } |
|
866 else |
|
867 print_usage ("eval"); |
|
868 |
|
869 return retval; |
|
870 } |
|
871 |
581
|
872 // Execute a shell command. |
|
873 |
703
|
874 DEFUN ("system", Fsystem, Ssystem, 2, 1, |
|
875 "system (string [, return_output]): execute shell commands") |
560
|
876 { |
|
877 Octave_object retval; |
|
878 |
|
879 int nargin = args.length (); |
|
880 |
712
|
881 if (nargin < 1 || nargin > 2) |
560
|
882 { |
712
|
883 print_usage ("system"); |
624
|
884 return retval; |
|
885 } |
|
886 |
712
|
887 tree_constant tc_command = args(0); |
624
|
888 |
636
|
889 char *tmp_str = tc_command.string_value (); |
|
890 |
|
891 if (error_state) |
624
|
892 { |
712
|
893 error ("system: expecting string as first argument"); |
636
|
894 } |
|
895 else |
|
896 { |
|
897 iprocstream cmd (tmp_str); |
624
|
898 |
|
899 ostrstream output_buf; |
|
900 |
560
|
901 char ch; |
|
902 while (cmd.get (ch)) |
|
903 output_buf.put (ch); |
624
|
904 |
560
|
905 output_buf << ends; |
624
|
906 |
560
|
907 int status = cmd.close (); |
624
|
908 |
712
|
909 if (nargout > 0 || nargin > 1) |
560
|
910 { |
624
|
911 char *msg = output_buf.str (); |
|
912 |
|
913 retval(1) = (double) status; |
|
914 retval(0) = msg; |
|
915 |
|
916 delete [] msg; |
560
|
917 } |
624
|
918 else |
|
919 maybe_page_output (output_buf); |
560
|
920 } |
|
921 |
|
922 return retval; |
|
923 } |
|
924 |
703
|
925 DEFALIAS (shell_cmd, system); |
|
926 |
560
|
927 /* |
1
|
928 ;;; Local Variables: *** |
|
929 ;;; mode: C++ *** |
|
930 ;;; page-delimiter: "^/\\*" *** |
|
931 ;;; End: *** |
|
932 */ |