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