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