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