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