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