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