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