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]; |
|
228 sprintf (putenv_cmd, "PATH=%s:%s:%s", shell_path, arch_dir, bin_dir); |
|
229 } |
|
230 else |
|
231 { |
|
232 putenv_cmd = new char [len]; |
|
233 sprintf (putenv_cmd, "PATH=%s:%s", arch_dir, bin_dir); |
|
234 } |
686
|
235 |
798
|
236 putenv (putenv_cmd); |
686
|
237 |
1
|
238 raw_prog_name = strsave (name); |
|
239 prog_name = strsave ("octave"); |
|
240 |
|
241 load_path = default_path (); |
186
|
242 |
|
243 info_file = default_info_file (); |
195
|
244 |
|
245 editor = default_editor (); |
1
|
246 } |
|
247 |
|
248 void |
|
249 parse_and_execute (FILE *f, int print) |
|
250 { |
|
251 begin_unwind_frame ("parse_and_execute"); |
|
252 |
|
253 YY_BUFFER_STATE old_buf = current_buffer (); |
|
254 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
255 |
|
256 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
257 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
258 |
|
259 switch_to_buffer (new_buf); |
|
260 |
|
261 unwind_protect_int (echo_input); |
|
262 unwind_protect_int (using_readline); |
|
263 unwind_protect_int (saving_history); |
|
264 |
|
265 echo_input = 0; |
|
266 using_readline = 0; |
|
267 saving_history = 0; |
|
268 |
|
269 unwind_protect_ptr (curr_sym_tab); |
|
270 |
|
271 int retval; |
|
272 do |
|
273 { |
191
|
274 reset_parser (); |
1005
|
275 |
1
|
276 retval = yyparse (); |
1005
|
277 |
529
|
278 if (retval == 0 && global_command) |
1
|
279 { |
|
280 global_command->eval (print); |
|
281 delete global_command; |
|
282 } |
|
283 } |
|
284 while (retval == 0); |
|
285 |
|
286 run_unwind_frame ("parse_and_execute"); |
|
287 } |
|
288 |
|
289 void |
578
|
290 parse_and_execute (char *s, int print, int verbose) |
1
|
291 { |
|
292 begin_unwind_frame ("parse_and_execute_2"); |
|
293 |
|
294 unwind_protect_int (reading_script_file); |
|
295 |
|
296 reading_script_file = 1; |
|
297 |
|
298 FILE *f = get_input_from_file (s, 0); |
529
|
299 if (f) |
1
|
300 { |
|
301 unwind_protect_int (input_line_number); |
|
302 unwind_protect_int (current_input_column); |
|
303 unwind_protect_int (echo_input); |
|
304 |
|
305 input_line_number = 0; |
143
|
306 current_input_column = 1; |
1
|
307 echo_input = 0; |
|
308 |
578
|
309 if (verbose) |
|
310 { |
793
|
311 cout << "reading commands from " << s << " ... "; |
578
|
312 reading_startup_message_printed = 1; |
|
313 cout.flush (); |
|
314 } |
|
315 |
1
|
316 parse_and_execute (f, print); |
578
|
317 |
1103
|
318 fclose (f); |
|
319 |
578
|
320 if (verbose) |
|
321 cout << "done." << endl; |
1
|
322 } |
|
323 |
|
324 run_unwind_frame ("parse_and_execute_2"); |
|
325 } |
|
326 |
581
|
327 // Initialize by reading startup files. |
|
328 |
1
|
329 static void |
|
330 execute_startup_files (void) |
|
331 { |
315
|
332 begin_unwind_frame ("execute_startup_files"); |
|
333 |
|
334 unwind_protect_int (input_from_startup_file); |
|
335 input_from_startup_file = 1; |
|
336 |
793
|
337 int verbose = (verbose_flag && ! inhibit_startup_message); |
578
|
338 |
1
|
339 // Execute commands from the site-wide configuration file. |
|
340 |
|
341 char *sd = get_site_defaults (); |
|
342 |
578
|
343 parse_and_execute (sd, 0, verbose); |
1
|
344 |
|
345 // Try to execute commands from $HOME/.octaverc and ./.octaverc. |
|
346 |
529
|
347 char *home_rc = 0; |
|
348 if (home_directory) |
1
|
349 { |
85
|
350 home_rc = strconcat (home_directory, "/.octaverc"); |
578
|
351 parse_and_execute (home_rc, 0, verbose); |
1
|
352 } |
|
353 |
85
|
354 // Names alone are not enough. |
|
355 |
|
356 struct stat home_rc_statbuf; |
|
357 stat (home_rc, &home_rc_statbuf); |
|
358 delete [] home_rc; |
|
359 |
|
360 struct stat dot_rc_statbuf; |
|
361 stat ("./.octaverc", &dot_rc_statbuf); |
|
362 |
|
363 if (home_rc_statbuf.st_ino != dot_rc_statbuf.st_ino) |
578
|
364 parse_and_execute ("./.octaverc", 0, verbose); |
315
|
365 |
|
366 run_unwind_frame ("execute_startup_files"); |
1
|
367 } |
|
368 |
581
|
369 // Usage message with extra help. |
|
370 |
1
|
371 static void |
|
372 verbose_usage (void) |
|
373 { |
|
374 cout << "\n" |
|
375 << " Octave, version " << version_string |
1009
|
376 << ". Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton.\n" |
1
|
377 << " This is free software with ABSOLUTELY NO WARRANTY.\n" |
|
378 << "\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 { |
|
439 cout << "octave, version " << version_string << "\n"; |
|
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) |
|
549 cout << "Octave, version " << version_string |
1009
|
550 << ". Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton.\n" |
578
|
551 << "This is free software with ABSOLUTELY NO WARRANTY.\n" |
|
552 << "For details, type `warranty'.\n" << endl; |
|
553 |
1
|
554 if (read_init_files) |
|
555 { |
|
556 saving_history = 0; |
|
557 execute_startup_files (); |
|
558 saving_history = 1; |
|
559 } |
|
560 |
578
|
561 if (! inhibit_startup_message && reading_startup_message_printed) |
|
562 cout << endl; |
|
563 |
1
|
564 // Avoid counting commands executed from startup files. |
|
565 current_command_number = 1; |
|
566 |
|
567 // If there is an extra argument, see if it names a file to read. |
|
568 |
149
|
569 int remaining_args = argc - optind; |
|
570 if (remaining_args > 1) |
|
571 { |
|
572 usage (); |
|
573 } |
|
574 else if (remaining_args == 1) |
1
|
575 { |
984
|
576 reading_script_file = 1; |
|
577 curr_fcn_file_name = argv[optind]; |
|
578 FILE *infile = get_input_from_file (curr_fcn_file_name); |
529
|
579 if (infile) |
287
|
580 { |
|
581 rl_blink_matching_paren = 0; |
|
582 switch_to_buffer (create_buffer (infile)); |
|
583 } |
529
|
584 else |
|
585 clean_up_and_exit (1); |
1
|
586 } |
|
587 else |
|
588 { |
149
|
589 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
|
590 |
1
|
591 // Is input coming from a terminal? If so, we are probably |
|
592 // interactive. |
|
593 |
|
594 interactive = (isatty (fileno (stdin)) && isatty (fileno (stdout))); |
|
595 } |
|
596 |
|
597 // Force input to be echoed if not really interactive, but the user |
|
598 // has forced interactive behavior. |
|
599 |
|
600 if (!interactive && forced_interactive) |
287
|
601 { |
|
602 rl_blink_matching_paren = 0; |
|
603 echo_input = 1; |
|
604 } |
1
|
605 |
|
606 if (! (interactive || forced_interactive)) |
|
607 using_readline = 0; |
|
608 |
|
609 // Allow the user to interrupt us without exiting. |
|
610 |
|
611 volatile sig_handler *saved_sigint_handler = signal (SIGINT, SIG_IGN); |
|
612 |
|
613 if (setjmp (toplevel) != 0) |
|
614 { |
|
615 raw_mode (0); |
176
|
616 |
1
|
617 cout << "\n"; |
|
618 } |
|
619 |
|
620 can_interrupt = 1; |
|
621 |
|
622 signal (SIGINT, saved_sigint_handler); |
|
623 |
|
624 // The big loop. |
|
625 |
|
626 int retval; |
|
627 do |
|
628 { |
287
|
629 curr_sym_tab = top_level_sym_tab; |
|
630 |
1
|
631 reset_parser (); |
287
|
632 |
1
|
633 retval = yyparse (); |
287
|
634 |
529
|
635 if (retval == 0 && global_command) |
1
|
636 { |
|
637 global_command->eval (1); |
|
638 delete global_command; |
|
639 current_command_number++; |
|
640 } |
|
641 } |
|
642 while (retval == 0); |
|
643 |
1005
|
644 if (retval == 1 && ! error_state) |
|
645 retval = 0; |
|
646 |
1
|
647 clean_up_and_exit (retval); |
|
648 } |
|
649 |
529
|
650 DEFUN_TEXT ("casesen", Fcasesen, Scasesen, 2, 1, |
|
651 "casesen [on|off]") |
|
652 { |
|
653 Octave_object retval; |
|
654 |
|
655 DEFINE_ARGV("casesen"); |
|
656 |
|
657 if (argc == 1 || (argc > 1 && strcmp (argv[1], "off") == 0)) |
|
658 warning ("casesen: sorry, Octave is always case sensitive"); |
|
659 else if (argc > 1 && strcmp (argv[1], "on") == 0) |
|
660 ; // ok. |
|
661 else |
|
662 print_usage ("casesen"); |
|
663 |
|
664 DELETE_ARGV; |
|
665 |
|
666 return retval; |
|
667 } |
|
668 |
666
|
669 DEFUN ("computer", Fcomputer, Scomputer, 1, 0, |
|
670 "computer ():\n\ |
|
671 \n\ |
|
672 Have Octave ask the system, \"What kind of computer are you?\"") |
|
673 { |
|
674 Octave_object retval; |
|
675 |
712
|
676 int nargin = args.length (); |
|
677 |
|
678 if (nargin != 0) |
666
|
679 warning ("computer: ignoring extra arguments"); |
|
680 |
|
681 ostrstream output_buf; |
|
682 |
|
683 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
684 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
685 else |
|
686 output_buf << TARGET_HOST_TYPE; |
|
687 |
|
688 if (nargout == 0) |
|
689 { |
|
690 output_buf << "\n" << ends; |
|
691 maybe_page_output (output_buf); |
|
692 } |
|
693 else |
|
694 { |
949
|
695 output_buf << ends; |
666
|
696 char *msg = output_buf.str (); |
|
697 retval = msg; |
|
698 delete [] msg; |
|
699 } |
|
700 |
|
701 return retval; |
|
702 } |
|
703 |
712
|
704 DEFUN ("flops", Fflops, Sflops, 0, 1, |
529
|
705 "flops (): count floating point operations") |
|
706 { |
|
707 int nargin = args.length (); |
|
708 |
712
|
709 if (nargin > 0) |
529
|
710 print_usage ("flops"); |
|
711 |
|
712 warning ("flops is a flop, always returning zero"); |
|
713 |
|
714 return 0.0; |
|
715 } |
|
716 |
712
|
717 DEFUN ("quit", Fquit, Squit, 0, 0, |
529
|
718 "quit (): exit Octave gracefully") |
|
719 { |
|
720 Octave_object retval; |
|
721 quitting_gracefully = 1; |
|
722 clean_up_and_exit (0); |
|
723 return retval; |
|
724 } |
|
725 |
549
|
726 DEFALIAS (exit, quit); |
|
727 |
712
|
728 DEFUN ("warranty", Fwarranty, Swarranty, 0, 0, |
529
|
729 "warranty (): describe copying conditions") |
|
730 { |
|
731 Octave_object retval; |
|
732 |
|
733 ostrstream output_buf; |
|
734 output_buf << "\n Octave, version " << version_string |
1009
|
735 << ". Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton\n" |
529
|
736 << "\n\ |
|
737 This program is free software; you can redistribute it and/or modify\n\ |
|
738 it under the terms of the GNU General Public License as published by\n\ |
|
739 the Free Software Foundation; either version 2 of the License, or\n\ |
|
740 (at your option) any later version.\n\ |
|
741 \n\ |
|
742 This program is distributed in the hope that it will be useful,\n\ |
|
743 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
744 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
745 GNU General Public License for more details.\n\ |
|
746 \n\ |
|
747 You should have received a copy of the GNU General Public License\n\ |
|
748 along with this program. If not, write to the Free Software\n\ |
|
749 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.\n\ |
|
750 \n"; |
|
751 |
|
752 output_buf << ends; |
|
753 maybe_page_output (output_buf); |
|
754 |
|
755 return retval; |
|
756 } |
|
757 |
|
758 // XXX FIXME XXX -- this may not be the best place for these... |
|
759 |
|
760 Octave_object |
|
761 feval (const Octave_object& args, int nargout) |
|
762 { |
|
763 Octave_object retval; |
|
764 |
712
|
765 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
529
|
766 if (fcn) |
|
767 { |
712
|
768 int tmp_nargin = args.length () - 1; |
565
|
769 Octave_object tmp_args; |
712
|
770 tmp_args.resize (tmp_nargin); |
|
771 for (int i = 0; i < tmp_nargin; i++) |
529
|
772 tmp_args(i) = args(i+1); |
|
773 retval = fcn->eval (0, nargout, tmp_args); |
|
774 } |
|
775 |
|
776 return retval; |
|
777 } |
|
778 |
|
779 DEFUN ("feval", Ffeval, Sfeval, -1, 1, |
|
780 "feval (NAME, ARGS, ...)\n\ |
|
781 \n\ |
|
782 evaluate NAME as a function, passing ARGS as its arguments") |
|
783 { |
|
784 Octave_object retval; |
|
785 |
|
786 int nargin = args.length (); |
|
787 |
712
|
788 if (nargin > 0) |
529
|
789 retval = feval (args, nargout); |
|
790 else |
|
791 print_usage ("feval"); |
|
792 |
|
793 return retval; |
|
794 } |
|
795 |
672
|
796 static Octave_object |
529
|
797 eval_string (const char *string, int print, int ans_assign, |
672
|
798 int& parse_status, int nargout) |
529
|
799 { |
|
800 begin_unwind_frame ("eval_string"); |
|
801 |
|
802 unwind_protect_int (get_input_from_eval_string); |
|
803 unwind_protect_ptr (global_command); |
|
804 unwind_protect_ptr (current_eval_string); |
|
805 |
|
806 get_input_from_eval_string = 1; |
|
807 current_eval_string = string; |
|
808 |
|
809 YY_BUFFER_STATE old_buf = current_buffer (); |
|
810 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
811 |
|
812 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
813 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
814 |
|
815 switch_to_buffer (new_buf); |
|
816 |
|
817 unwind_protect_ptr (curr_sym_tab); |
|
818 |
|
819 reset_parser (); |
|
820 |
|
821 parse_status = yyparse (); |
|
822 |
|
823 // Important to reset the idea of where input is coming from before |
|
824 // trying to eval the command we just parsed -- it might contain the |
|
825 // name of an function file that still needs to be parsed! |
|
826 |
578
|
827 tree_statement_list *command = global_command; |
529
|
828 |
|
829 run_unwind_frame ("eval_string"); |
|
830 |
672
|
831 Octave_object retval; |
529
|
832 |
|
833 if (parse_status == 0 && command) |
|
834 { |
672
|
835 retval = command->eval (print, nargout); |
529
|
836 delete command; |
|
837 } |
|
838 |
|
839 return retval; |
|
840 } |
|
841 |
|
842 tree_constant |
672
|
843 eval_string (const char *string, int print, int ans_assign, |
|
844 int& parse_status) |
|
845 { |
|
846 tree_constant retval; |
|
847 |
|
848 Octave_object tmp = eval_string (string, print, ans_assign, |
|
849 parse_status, 1); |
|
850 |
|
851 retval = tmp(0); |
|
852 |
|
853 return retval; |
|
854 } |
|
855 |
|
856 static Octave_object |
|
857 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
529
|
858 { |
636
|
859 char *string = arg.string_value (); |
|
860 |
|
861 if (error_state) |
529
|
862 { |
|
863 error ("eval: expecting string argument"); |
672
|
864 return -1.0; |
529
|
865 } |
|
866 |
|
867 // Yes Virginia, we always print here... |
|
868 |
672
|
869 return eval_string (string, 1, 1, parse_status, nargout); |
529
|
870 } |
|
871 |
712
|
872 DEFUN ("eval", Feval, Seval, 2, 1, |
672
|
873 "eval (TRY, CATCH)\n\ |
|
874 \n\ |
|
875 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
876 string CATCH.") |
529
|
877 { |
|
878 Octave_object retval; |
|
879 |
|
880 int nargin = args.length (); |
|
881 |
712
|
882 if (nargin > 0) |
529
|
883 { |
672
|
884 begin_unwind_frame ("Feval"); |
|
885 |
712
|
886 if (nargin > 1) |
672
|
887 { |
|
888 unwind_protect_int (suppress_octave_error_messages); |
|
889 suppress_octave_error_messages = 1; |
|
890 } |
|
891 |
529
|
892 int parse_status = 0; |
672
|
893 |
712
|
894 retval = eval_string (args(0), parse_status, nargout); |
672
|
895 |
712
|
896 if (nargin > 1 && (parse_status != 0 || error_state)) |
672
|
897 { |
|
898 error_state = 0; |
712
|
899 eval_string (args(1), parse_status, nargout); |
672
|
900 retval = Octave_object (); |
|
901 } |
|
902 |
|
903 run_unwind_frame ("Feval"); |
529
|
904 } |
|
905 else |
|
906 print_usage ("eval"); |
|
907 |
|
908 return retval; |
|
909 } |
|
910 |
581
|
911 // Execute a shell command. |
|
912 |
703
|
913 DEFUN ("system", Fsystem, Ssystem, 2, 1, |
|
914 "system (string [, return_output]): execute shell commands") |
560
|
915 { |
|
916 Octave_object retval; |
|
917 |
|
918 int nargin = args.length (); |
|
919 |
712
|
920 if (nargin < 1 || nargin > 2) |
560
|
921 { |
712
|
922 print_usage ("system"); |
624
|
923 return retval; |
|
924 } |
|
925 |
712
|
926 tree_constant tc_command = args(0); |
624
|
927 |
636
|
928 char *tmp_str = tc_command.string_value (); |
|
929 |
|
930 if (error_state) |
624
|
931 { |
712
|
932 error ("system: expecting string as first argument"); |
636
|
933 } |
|
934 else |
|
935 { |
|
936 iprocstream cmd (tmp_str); |
624
|
937 |
|
938 ostrstream output_buf; |
|
939 |
560
|
940 char ch; |
|
941 while (cmd.get (ch)) |
|
942 output_buf.put (ch); |
624
|
943 |
560
|
944 output_buf << ends; |
624
|
945 |
560
|
946 int status = cmd.close (); |
624
|
947 |
1022
|
948 // The value in status is as returned by waitpid. If the process |
|
949 // exited normally, extract the actual exit status of the command. |
|
950 // Otherwise, return 127 as a failure code. |
|
951 |
|
952 if ((status & 0xff) == 0) |
|
953 status = (status & 0xff00) >> 8; |
|
954 else |
|
955 status = 127; |
|
956 |
712
|
957 if (nargout > 0 || nargin > 1) |
560
|
958 { |
624
|
959 char *msg = output_buf.str (); |
|
960 |
|
961 retval(1) = (double) status; |
|
962 retval(0) = msg; |
|
963 |
|
964 delete [] msg; |
560
|
965 } |
624
|
966 else |
|
967 maybe_page_output (output_buf); |
560
|
968 } |
|
969 |
|
970 return retval; |
|
971 } |
|
972 |
703
|
973 DEFALIAS (shell_cmd, system); |
|
974 |
560
|
975 /* |
1
|
976 ;;; Local Variables: *** |
|
977 ;;; mode: C++ *** |
|
978 ;;; page-delimiter: "^/\\*" *** |
|
979 ;;; End: *** |
|
980 */ |