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