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