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 |
578
|
355 parse_and_execute (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 |
581
|
390 // Initialize by reading startup files. |
|
391 |
1
|
392 static void |
|
393 execute_startup_files (void) |
|
394 { |
315
|
395 begin_unwind_frame ("execute_startup_files"); |
|
396 |
1588
|
397 unwind_protect_int (user_pref.echo_executing_commands); |
315
|
398 unwind_protect_int (input_from_startup_file); |
1588
|
399 |
|
400 user_pref.echo_executing_commands = ECHO_OFF; |
315
|
401 input_from_startup_file = 1; |
|
402 |
793
|
403 int verbose = (verbose_flag && ! inhibit_startup_message); |
578
|
404 |
1477
|
405 // Execute commands from the site-wide configuration file. First |
|
406 // from the file $(prefix)/lib/octave/site/m/octaverc (if it exists), |
|
407 // then from the file $(prefix)/lib/octave/$(version)/m/octaverc (if |
|
408 // it exists). |
|
409 |
|
410 char *lsd = get_local_site_defaults (); |
|
411 parse_and_execute (lsd, 0, verbose); |
1
|
412 |
|
413 char *sd = get_site_defaults (); |
578
|
414 parse_and_execute (sd, 0, verbose); |
1
|
415 |
1358
|
416 // Try to execute commands from $HOME/.octaverc and ./.octaverc. |
1
|
417 |
529
|
418 char *home_rc = 0; |
|
419 if (home_directory) |
1
|
420 { |
85
|
421 home_rc = strconcat (home_directory, "/.octaverc"); |
578
|
422 parse_and_execute (home_rc, 0, verbose); |
1
|
423 } |
|
424 |
1358
|
425 // Names alone are not enough. |
85
|
426 |
|
427 struct stat home_rc_statbuf; |
|
428 stat (home_rc, &home_rc_statbuf); |
|
429 delete [] home_rc; |
|
430 |
|
431 struct stat dot_rc_statbuf; |
|
432 stat ("./.octaverc", &dot_rc_statbuf); |
|
433 |
|
434 if (home_rc_statbuf.st_ino != dot_rc_statbuf.st_ino) |
578
|
435 parse_and_execute ("./.octaverc", 0, verbose); |
315
|
436 |
|
437 run_unwind_frame ("execute_startup_files"); |
1
|
438 } |
|
439 |
581
|
440 // Usage message with extra help. |
|
441 |
1
|
442 static void |
|
443 verbose_usage (void) |
|
444 { |
1119
|
445 cout << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
446 Usage: " << usage_string << "\n\ |
|
447 \n\ |
|
448 -d, --debug enter parser debugging mode\n\ |
1120
|
449 -f, --ignore-init-file don't read any initialization files\n\ |
1119
|
450 -h, -?, --help print short help message and exit\n\ |
|
451 -i, --interactive force interactive behavior\n\ |
1120
|
452 --info-file FILE use top-level info file FILE\n\ |
|
453 -p PATH, --path PATH set initial LOADPATH to PATH\n\ |
1119
|
454 -q, --silent don't print message at startup\n\ |
1410
|
455 --traditional set compatibility variables\n\ |
1119
|
456 -V, --verbose enable verbose output in some cases\n\ |
|
457 -v, --version print version number and exit\n\ |
|
458 -x, --echo-commands echo commands as they are executed\n\ |
|
459 \n\ |
1120
|
460 FILE execute commands from FILE\n\ |
1119
|
461 \n"; |
285
|
462 |
613
|
463 exit (0); |
1
|
464 } |
|
465 |
581
|
466 // Terse usage messsage. |
|
467 |
1
|
468 static void |
|
469 usage (void) |
|
470 { |
613
|
471 cerr << "usage: " << usage_string << "\n"; |
1
|
472 exit (1); |
|
473 } |
|
474 |
581
|
475 // Fix up things before exiting. |
|
476 |
195
|
477 void |
1
|
478 clean_up_and_exit (int retval) |
|
479 { |
|
480 raw_mode (0); |
176
|
481 |
1
|
482 clean_up_history (); |
176
|
483 |
1
|
484 close_plot_stream (); |
176
|
485 |
581
|
486 close_diary_file (); |
|
487 |
1
|
488 close_files (); |
|
489 |
318
|
490 cleanup_tmp_files (); |
|
491 |
1
|
492 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
493 cout << "\n"; |
|
494 |
|
495 if (retval == EOF) |
|
496 retval = 0; |
|
497 |
|
498 exit (retval); |
195
|
499 |
1358
|
500 // This is bogus but should prevent g++ from giving a warning saying |
|
501 // that this volatile function does return. |
195
|
502 |
|
503 panic_impossible (); |
1
|
504 } |
|
505 |
|
506 static void |
|
507 print_version_and_exit (void) |
|
508 { |
1107
|
509 cout << OCTAVE_NAME_AND_VERSION << "\n"; |
1
|
510 exit (0); |
|
511 } |
|
512 |
721
|
513 static void |
|
514 initialize_error_handlers () |
|
515 { |
|
516 set_Complex_error_handler (octave_Complex_error_handler); |
|
517 |
|
518 set_liboctave_error_handler (error); |
|
519 } |
|
520 |
1410
|
521 // What happens on --traditional. |
|
522 |
|
523 static void |
|
524 maximum_braindamage (void) |
|
525 { |
|
526 bind_builtin_variable ("PS1", ">> "); |
|
527 bind_builtin_variable ("PS2", ""); |
1423
|
528 bind_builtin_variable ("beep_on_error", "true"); |
1410
|
529 bind_builtin_variable ("default_save_format", "mat-binary"); |
|
530 bind_builtin_variable ("define_all_return_values", "true"); |
|
531 bind_builtin_variable ("do_fortran_indexing", "true"); |
|
532 bind_builtin_variable ("empty_list_elements_ok", "true"); |
|
533 bind_builtin_variable ("implicit_str_to_num_ok", "true"); |
|
534 bind_builtin_variable ("ok_to_lose_imaginary_part", "true"); |
|
535 bind_builtin_variable ("page_screen_output", "false"); |
|
536 bind_builtin_variable ("prefer_column_vectors", "false"); |
|
537 bind_builtin_variable ("prefer_zero_one_indexing", "true"); |
|
538 bind_builtin_variable ("print_empty_dimensions", "false"); |
|
539 bind_builtin_variable ("treat_neg_dim_as_zero", "true"); |
|
540 bind_builtin_variable ("warn_function_name_clash", "false"); |
|
541 bind_builtin_variable ("whitespace_in_literal_matrix", "traditional"); |
|
542 } |
|
543 |
581
|
544 // You guessed it. |
|
545 |
1
|
546 int |
|
547 main (int argc, char **argv) |
|
548 { |
1588
|
549 int echo_commands = ECHO_OFF; |
|
550 |
1358
|
551 // The order of these calls is important, and initialize_globals |
|
552 // must come before the options are processed because some command |
|
553 // line options override defaults. |
721
|
554 |
|
555 init_user_prefs (); |
|
556 |
|
557 initialize_pager (); |
|
558 |
1
|
559 sysdep_init (); |
|
560 |
721
|
561 initialize_error_handlers (); |
223
|
562 |
1
|
563 initialize_globals (argv[0]); |
|
564 |
139
|
565 int optc; |
|
566 while ((optc = getopt_long (argc, argv, short_opts, long_opts, 0)) != EOF) |
1
|
567 { |
139
|
568 switch (optc) |
1
|
569 { |
793
|
570 case 'V': |
|
571 verbose_flag++; |
|
572 break; |
|
573 |
1
|
574 case 'd': |
|
575 yydebug++; |
|
576 break; |
777
|
577 |
1
|
578 case 'f': |
|
579 read_init_files = 0; |
|
580 break; |
777
|
581 |
1
|
582 case 'h': |
|
583 case '?': |
|
584 verbose_usage (); |
|
585 break; |
777
|
586 |
1
|
587 case 'i': |
|
588 forced_interactive = 1; |
|
589 break; |
777
|
590 |
1
|
591 case 'p': |
529
|
592 if (optarg) |
139
|
593 load_path = strsave (optarg); |
1
|
594 break; |
777
|
595 |
1
|
596 case 'q': |
|
597 inhibit_startup_message = 1; |
|
598 break; |
777
|
599 |
1
|
600 case 'x': |
1588
|
601 echo_commands = (ECHO_SCRIPTS | ECHO_FUNCTIONS | ECHO_CMD_LINE); |
1
|
602 break; |
777
|
603 |
1
|
604 case 'v': |
|
605 print_version_and_exit (); |
|
606 break; |
777
|
607 |
186
|
608 case INFO_FILE_OPTION: |
529
|
609 if (optarg) |
186
|
610 info_file = strsave (optarg); |
|
611 break; |
777
|
612 |
1410
|
613 case TRADITIONAL_OPTION: |
|
614 traditional = 1; |
|
615 break; |
|
616 |
1
|
617 default: |
|
618 usage (); |
|
619 break; |
|
620 } |
|
621 } |
|
622 |
318
|
623 #if defined (HAVE_ATEXIT) || (HAVE_ON_EXIT) |
1358
|
624 // Make sure we clean up when we exit. If we don't have atexit or |
|
625 // on_exit, we're going to leave some junk files around if we exit |
|
626 // abnormally. |
1
|
627 atexit (cleanup_tmp_files); |
318
|
628 #endif |
1
|
629 |
1358
|
630 // These can come after command line args since none of them set any |
|
631 // defaults that might be changed by command line options. |
581
|
632 |
636
|
633 install_signal_handlers (); |
|
634 |
1
|
635 initialize_history (); |
|
636 |
|
637 initialize_file_io (); |
|
638 |
581
|
639 initialize_symbol_tables (); |
1
|
640 |
|
641 install_builtins (); |
|
642 |
315
|
643 initialize_readline (); |
|
644 |
721
|
645 init_dynamic_linker (); |
|
646 |
578
|
647 if (! inhibit_startup_message) |
1106
|
648 cout << OCTAVE_STARTUP_MESSAGE "\n" << endl; |
578
|
649 |
1410
|
650 if (traditional) |
|
651 maximum_braindamage (); |
|
652 |
1588
|
653 bind_builtin_variable ("echo_executing_commands", |
|
654 (double) echo_commands); |
|
655 |
1
|
656 if (read_init_files) |
|
657 { |
|
658 saving_history = 0; |
|
659 execute_startup_files (); |
|
660 saving_history = 1; |
|
661 } |
|
662 |
578
|
663 if (! inhibit_startup_message && reading_startup_message_printed) |
|
664 cout << endl; |
|
665 |
1358
|
666 // Avoid counting commands executed from startup files. |
|
667 |
1
|
668 current_command_number = 1; |
|
669 |
1358
|
670 // If there is an extra argument, see if it names a file to read. |
|
671 // Additional arguments are taken as command line options for the |
|
672 // script. |
1
|
673 |
149
|
674 int remaining_args = argc - optind; |
1355
|
675 if (remaining_args > 0) |
149
|
676 { |
984
|
677 reading_script_file = 1; |
|
678 curr_fcn_file_name = argv[optind]; |
1411
|
679 |
984
|
680 FILE *infile = get_input_from_file (curr_fcn_file_name); |
1411
|
681 |
529
|
682 if (infile) |
287
|
683 { |
1516
|
684 input_from_command_line_file = 1; |
|
685 |
1411
|
686 bind_builtin_variable ("program_invocation_name", |
|
687 curr_fcn_file_name); |
|
688 |
|
689 char *tmp = strrchr (curr_fcn_file_name, '/'); |
|
690 tmp = tmp ? tmp+1 : curr_fcn_file_name; |
|
691 |
|
692 bind_builtin_variable ("program_name", tmp); |
|
693 |
|
694 intern_argv (remaining_args, argv+optind); |
|
695 |
287
|
696 rl_blink_matching_paren = 0; |
|
697 switch_to_buffer (create_buffer (infile)); |
|
698 } |
529
|
699 else |
|
700 clean_up_and_exit (1); |
1
|
701 } |
|
702 else |
|
703 { |
1355
|
704 // Is input coming from a terminal? If so, we are probably |
|
705 // interactive. |
1
|
706 |
|
707 interactive = (isatty (fileno (stdin)) && isatty (fileno (stdout))); |
1355
|
708 |
|
709 intern_argv (argc, argv); |
|
710 |
|
711 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
1
|
712 } |
|
713 |
1358
|
714 // Force input to be echoed if not really interactive, but the user |
|
715 // has forced interactive behavior. |
1
|
716 |
|
717 if (!interactive && forced_interactive) |
287
|
718 { |
|
719 rl_blink_matching_paren = 0; |
1588
|
720 |
|
721 // XXX FIXME XXX -- is this the right thing to do? |
|
722 |
|
723 bind_builtin_variable ("echo_executing_commands", |
|
724 (double) ECHO_CMD_LINE); |
287
|
725 } |
1
|
726 |
1410
|
727 if (! interactive) |
1
|
728 using_readline = 0; |
|
729 |
1358
|
730 // Allow the user to interrupt us without exiting. |
1
|
731 |
1443
|
732 volatile sig_handler *saved_sigint_handler |
|
733 = octave_set_signal_handler (SIGINT, SIG_IGN); |
1
|
734 |
|
735 if (setjmp (toplevel) != 0) |
|
736 { |
|
737 raw_mode (0); |
176
|
738 |
1
|
739 cout << "\n"; |
|
740 } |
|
741 |
|
742 can_interrupt = 1; |
|
743 |
1443
|
744 octave_set_signal_handler (SIGINT, saved_sigint_handler); |
1
|
745 |
1358
|
746 // The big loop. |
1
|
747 |
|
748 int retval; |
|
749 do |
|
750 { |
287
|
751 curr_sym_tab = top_level_sym_tab; |
|
752 |
1
|
753 reset_parser (); |
287
|
754 |
1
|
755 retval = yyparse (); |
287
|
756 |
529
|
757 if (retval == 0 && global_command) |
1
|
758 { |
|
759 global_command->eval (1); |
|
760 delete global_command; |
|
761 current_command_number++; |
|
762 } |
|
763 } |
|
764 while (retval == 0); |
|
765 |
1005
|
766 if (retval == 1 && ! error_state) |
|
767 retval = 0; |
|
768 |
1
|
769 clean_up_and_exit (retval); |
|
770 } |
|
771 |
1488
|
772 DEFUN_TEXT ("casesen", Fcasesen, Scasesen, 10, |
529
|
773 "casesen [on|off]") |
|
774 { |
|
775 Octave_object retval; |
|
776 |
|
777 DEFINE_ARGV("casesen"); |
|
778 |
|
779 if (argc == 1 || (argc > 1 && strcmp (argv[1], "off") == 0)) |
|
780 warning ("casesen: sorry, Octave is always case sensitive"); |
|
781 else if (argc > 1 && strcmp (argv[1], "on") == 0) |
|
782 ; // ok. |
|
783 else |
|
784 print_usage ("casesen"); |
|
785 |
|
786 DELETE_ARGV; |
|
787 |
|
788 return retval; |
|
789 } |
|
790 |
1488
|
791 DEFUN ("computer", Fcomputer, Scomputer, 11, |
666
|
792 "computer ():\n\ |
|
793 \n\ |
|
794 Have Octave ask the system, \"What kind of computer are you?\"") |
|
795 { |
|
796 Octave_object retval; |
|
797 |
712
|
798 int nargin = args.length (); |
|
799 |
|
800 if (nargin != 0) |
666
|
801 warning ("computer: ignoring extra arguments"); |
|
802 |
|
803 ostrstream output_buf; |
|
804 |
|
805 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
806 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
807 else |
|
808 output_buf << TARGET_HOST_TYPE; |
|
809 |
|
810 if (nargout == 0) |
|
811 { |
|
812 output_buf << "\n" << ends; |
|
813 maybe_page_output (output_buf); |
|
814 } |
|
815 else |
|
816 { |
949
|
817 output_buf << ends; |
666
|
818 char *msg = output_buf.str (); |
|
819 retval = msg; |
|
820 delete [] msg; |
|
821 } |
|
822 |
|
823 return retval; |
|
824 } |
|
825 |
1488
|
826 DEFUN ("flops", Fflops, Sflops, 10, |
529
|
827 "flops (): count floating point operations") |
|
828 { |
|
829 int nargin = args.length (); |
|
830 |
712
|
831 if (nargin > 0) |
529
|
832 print_usage ("flops"); |
|
833 |
|
834 warning ("flops is a flop, always returning zero"); |
|
835 |
|
836 return 0.0; |
|
837 } |
|
838 |
1488
|
839 DEFUN ("quit", Fquit, Squit, 00, |
529
|
840 "quit (): exit Octave gracefully") |
|
841 { |
|
842 Octave_object retval; |
|
843 quitting_gracefully = 1; |
|
844 clean_up_and_exit (0); |
|
845 return retval; |
|
846 } |
|
847 |
549
|
848 DEFALIAS (exit, quit); |
|
849 |
1488
|
850 DEFUN ("warranty", Fwarranty, Swarranty, 00, |
529
|
851 "warranty (): describe copying conditions") |
|
852 { |
|
853 Octave_object retval; |
|
854 |
|
855 ostrstream output_buf; |
1110
|
856 output_buf << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
857 This program is free software; you can redistribute it and/or modify\n\ |
|
858 it under the terms of the GNU General Public License as published by\n\ |
|
859 the Free Software Foundation; either version 2 of the License, or\n\ |
|
860 (at your option) any later version.\n\ |
529
|
861 \n\ |
1110
|
862 This program is distributed in the hope that it will be useful,\n\ |
|
863 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
864 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
865 GNU General Public License for more details.\n\ |
529
|
866 \n\ |
1110
|
867 You should have received a copy of the GNU General Public License\n\ |
|
868 along with this program. If not, write to the Free Software\n\ |
1315
|
869 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
529
|
870 \n"; |
|
871 |
|
872 output_buf << ends; |
|
873 maybe_page_output (output_buf); |
|
874 |
|
875 return retval; |
|
876 } |
|
877 |
|
878 // XXX FIXME XXX -- this may not be the best place for these... |
|
879 |
|
880 Octave_object |
|
881 feval (const Octave_object& args, int nargout) |
|
882 { |
|
883 Octave_object retval; |
|
884 |
712
|
885 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
529
|
886 if (fcn) |
|
887 { |
712
|
888 int tmp_nargin = args.length () - 1; |
565
|
889 Octave_object tmp_args; |
712
|
890 tmp_args.resize (tmp_nargin); |
|
891 for (int i = 0; i < tmp_nargin; i++) |
529
|
892 tmp_args(i) = args(i+1); |
|
893 retval = fcn->eval (0, nargout, tmp_args); |
|
894 } |
|
895 |
|
896 return retval; |
|
897 } |
|
898 |
1488
|
899 DEFUN ("feval", Ffeval, Sfeval, 11, |
529
|
900 "feval (NAME, ARGS, ...)\n\ |
|
901 \n\ |
|
902 evaluate NAME as a function, passing ARGS as its arguments") |
|
903 { |
|
904 Octave_object retval; |
|
905 |
|
906 int nargin = args.length (); |
|
907 |
712
|
908 if (nargin > 0) |
529
|
909 retval = feval (args, nargout); |
|
910 else |
|
911 print_usage ("feval"); |
|
912 |
|
913 return retval; |
|
914 } |
|
915 |
672
|
916 static Octave_object |
1488
|
917 eval_string (const char *string, int print, int& parse_status, |
|
918 int nargout) |
529
|
919 { |
|
920 begin_unwind_frame ("eval_string"); |
|
921 |
|
922 unwind_protect_int (get_input_from_eval_string); |
1516
|
923 unwind_protect_int (input_from_command_line_file); |
529
|
924 unwind_protect_ptr (global_command); |
|
925 unwind_protect_ptr (current_eval_string); |
|
926 |
|
927 get_input_from_eval_string = 1; |
1516
|
928 input_from_command_line_file = 0; |
529
|
929 current_eval_string = string; |
|
930 |
|
931 YY_BUFFER_STATE old_buf = current_buffer (); |
|
932 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
933 |
|
934 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
935 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
936 |
|
937 switch_to_buffer (new_buf); |
|
938 |
|
939 unwind_protect_ptr (curr_sym_tab); |
|
940 |
|
941 reset_parser (); |
|
942 |
|
943 parse_status = yyparse (); |
|
944 |
1358
|
945 // Important to reset the idea of where input is coming from before |
|
946 // trying to eval the command we just parsed -- it might contain the |
|
947 // name of an function file that still needs to be parsed! |
529
|
948 |
578
|
949 tree_statement_list *command = global_command; |
529
|
950 |
|
951 run_unwind_frame ("eval_string"); |
|
952 |
672
|
953 Octave_object retval; |
529
|
954 |
|
955 if (parse_status == 0 && command) |
|
956 { |
672
|
957 retval = command->eval (print, nargout); |
529
|
958 delete command; |
|
959 } |
|
960 |
|
961 return retval; |
|
962 } |
|
963 |
|
964 tree_constant |
1488
|
965 eval_string (const char *string, int print, int& parse_status) |
672
|
966 { |
|
967 tree_constant retval; |
|
968 |
1488
|
969 Octave_object tmp = eval_string (string, print, parse_status, 1); |
672
|
970 |
|
971 retval = tmp(0); |
|
972 |
|
973 return retval; |
|
974 } |
|
975 |
|
976 static Octave_object |
|
977 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
529
|
978 { |
1363
|
979 const char *string = arg.string_value (); |
636
|
980 |
|
981 if (error_state) |
529
|
982 { |
|
983 error ("eval: expecting string argument"); |
672
|
984 return -1.0; |
529
|
985 } |
|
986 |
1358
|
987 // Yes Virginia, we always print here... |
529
|
988 |
1488
|
989 return eval_string (string, 1, parse_status, nargout); |
529
|
990 } |
|
991 |
1488
|
992 DEFUN ("eval", Feval, Seval, 11, |
672
|
993 "eval (TRY, CATCH)\n\ |
|
994 \n\ |
|
995 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
996 string CATCH.") |
529
|
997 { |
|
998 Octave_object retval; |
|
999 |
|
1000 int nargin = args.length (); |
|
1001 |
712
|
1002 if (nargin > 0) |
529
|
1003 { |
672
|
1004 begin_unwind_frame ("Feval"); |
|
1005 |
712
|
1006 if (nargin > 1) |
672
|
1007 { |
1489
|
1008 unwind_protect_int (buffer_error_messages); |
|
1009 buffer_error_messages = 1; |
672
|
1010 } |
|
1011 |
529
|
1012 int parse_status = 0; |
672
|
1013 |
712
|
1014 retval = eval_string (args(0), parse_status, nargout); |
672
|
1015 |
712
|
1016 if (nargin > 1 && (parse_status != 0 || error_state)) |
672
|
1017 { |
|
1018 error_state = 0; |
1489
|
1019 |
|
1020 // Set up for letting the user print any messages from |
|
1021 // errors that occurred in the first part of this eval(). |
|
1022 |
|
1023 buffer_error_messages = 0; |
|
1024 bind_global_error_variable (); |
|
1025 add_unwind_protect (clear_global_error_variable, 0); |
|
1026 |
712
|
1027 eval_string (args(1), parse_status, nargout); |
1489
|
1028 |
672
|
1029 retval = Octave_object (); |
|
1030 } |
|
1031 |
|
1032 run_unwind_frame ("Feval"); |
529
|
1033 } |
|
1034 else |
|
1035 print_usage ("eval"); |
|
1036 |
|
1037 return retval; |
|
1038 } |
|
1039 |
581
|
1040 // Execute a shell command. |
|
1041 |
1488
|
1042 DEFUN ("system", Fsystem, Ssystem, 11, |
703
|
1043 "system (string [, return_output]): execute shell commands") |
560
|
1044 { |
|
1045 Octave_object retval; |
|
1046 |
|
1047 int nargin = args.length (); |
|
1048 |
712
|
1049 if (nargin < 1 || nargin > 2) |
560
|
1050 { |
712
|
1051 print_usage ("system"); |
624
|
1052 return retval; |
|
1053 } |
|
1054 |
712
|
1055 tree_constant tc_command = args(0); |
624
|
1056 |
1363
|
1057 const char *tmp_str = tc_command.string_value (); |
636
|
1058 |
|
1059 if (error_state) |
624
|
1060 { |
712
|
1061 error ("system: expecting string as first argument"); |
636
|
1062 } |
|
1063 else |
|
1064 { |
1449
|
1065 iprocstream *cmd = new iprocstream (tmp_str); |
624
|
1066 |
1449
|
1067 add_unwind_protect (cleanup_iprocstream, cmd); |
|
1068 |
|
1069 int status = 127; |
624
|
1070 |
1449
|
1071 if (cmd && *cmd) |
|
1072 { |
|
1073 ostrstream output_buf; |
624
|
1074 |
1449
|
1075 char ch; |
|
1076 while (cmd->get (ch)) |
|
1077 output_buf.put (ch); |
624
|
1078 |
1449
|
1079 output_buf << ends; |
|
1080 |
|
1081 status = cmd->close (); |
624
|
1082 |
1449
|
1083 // The value in status is as returned by waitpid. If the |
|
1084 // process exited normally, extract the actual exit status of |
|
1085 // the command. Otherwise, return 127 as a failure code. |
1022
|
1086 |
1449
|
1087 if ((status & 0xff) == 0) |
|
1088 status = (status & 0xff00) >> 8; |
1022
|
1089 |
1449
|
1090 if (nargout > 0 || nargin > 1) |
|
1091 { |
|
1092 char *msg = output_buf.str (); |
624
|
1093 |
1449
|
1094 retval(1) = (double) status; |
|
1095 retval(0) = msg; |
624
|
1096 |
1449
|
1097 delete [] msg; |
|
1098 } |
|
1099 else |
|
1100 maybe_page_output (output_buf); |
560
|
1101 } |
624
|
1102 else |
1449
|
1103 error ("unable to start subprocess for `%s'", tmp_str); |
|
1104 |
|
1105 run_unwind_protect (); |
560
|
1106 } |
|
1107 |
|
1108 return retval; |
|
1109 } |
|
1110 |
703
|
1111 DEFALIAS (shell_cmd, system); |
|
1112 |
1266
|
1113 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
1114 int debug_new_delete = 0; |
|
1115 |
|
1116 typedef void (*vfp)(void); |
|
1117 extern vfp __new_handler; |
|
1118 |
|
1119 void * |
|
1120 __builtin_new (size_t sz) |
|
1121 { |
|
1122 void *p; |
|
1123 |
|
1124 /* malloc (0) is unpredictable; avoid it. */ |
|
1125 if (sz == 0) |
|
1126 sz = 1; |
|
1127 p = (void *) malloc (sz); |
|
1128 while (p == 0) |
|
1129 { |
|
1130 (*__new_handler) (); |
|
1131 p = (void *) malloc (sz); |
|
1132 } |
|
1133 |
|
1134 if (debug_new_delete) |
|
1135 cout << "__builtin_new: " << p << endl; |
|
1136 |
|
1137 return p; |
|
1138 } |
|
1139 |
|
1140 void |
|
1141 __builtin_delete (void *ptr) |
|
1142 { |
|
1143 if (debug_new_delete) |
|
1144 cout << "__builtin_delete: " << ptr << endl; |
|
1145 |
|
1146 if (ptr) |
|
1147 free (ptr); |
|
1148 } |
|
1149 #endif |
|
1150 |
560
|
1151 /* |
1
|
1152 ;;; Local Variables: *** |
|
1153 ;;; mode: C++ *** |
|
1154 ;;; page-delimiter: "^/\\*" *** |
|
1155 ;;; End: *** |
|
1156 */ |