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