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