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