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