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