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