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