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]; |
1411
|
213 |
|
214 bind_builtin_variable ("argv", octave_argv, 1, 1, 0); |
1355
|
215 } |
|
216 } |
|
217 |
581
|
218 // Initialize some global variables for later use. |
|
219 |
1
|
220 static void |
|
221 initialize_globals (char *name) |
|
222 { |
1355
|
223 raw_prog_name = strsave (name); |
|
224 char *tmp = strrchr (raw_prog_name, '/'); |
|
225 prog_name = tmp ? strsave (tmp+1) : strsave (raw_prog_name); |
|
226 |
|
227 kpse_set_progname (name); |
|
228 |
1
|
229 struct passwd *entry = getpwuid (getuid ()); |
|
230 if (entry) |
|
231 user_name = strsave (entry->pw_name); |
|
232 else |
|
233 user_name = strsave ("I have no name!"); |
|
234 endpwent (); |
|
235 |
|
236 char hostname[256]; |
|
237 if (gethostname (hostname, 255) < 0) |
|
238 host_name = strsave ("I have no host!"); |
|
239 else |
|
240 host_name = strsave (hostname); |
|
241 |
|
242 char *hd = getenv ("HOME"); |
529
|
243 if (hd) |
|
244 home_directory = strsave (hd); |
1
|
245 else |
529
|
246 home_directory = strsave ("I have no home!"); |
1
|
247 |
686
|
248 char *shell_path = getenv ("PATH"); |
|
249 char *arch_dir = octave_arch_lib_dir (); |
798
|
250 char *bin_dir = octave_bin_dir (); |
686
|
251 |
798
|
252 int len = strlen (arch_dir) + strlen (bin_dir) + 7; |
|
253 |
|
254 char *putenv_cmd = 0; |
686
|
255 |
798
|
256 if (shell_path) |
|
257 { |
|
258 len += strlen (shell_path) + 1; |
|
259 putenv_cmd = new char [len]; |
1104
|
260 sprintf (putenv_cmd, |
|
261 "PATH=%s" SEPCHAR_STR "%s" SEPCHAR_STR "%s", |
|
262 shell_path, arch_dir, bin_dir); |
798
|
263 } |
|
264 else |
|
265 { |
|
266 putenv_cmd = new char [len]; |
1104
|
267 sprintf (putenv_cmd, "PATH=%s" SEPCHAR_STR "%s", arch_dir, bin_dir); |
798
|
268 } |
686
|
269 |
798
|
270 putenv (putenv_cmd); |
686
|
271 |
1358
|
272 // This may seem odd, but doing it this way means that we don't have |
|
273 // to modify the kpathsea library... |
1153
|
274 |
|
275 char *odb = getenv ("OCTAVE_DB_DIR"); |
|
276 |
|
277 if (odb) |
|
278 putenv (strconcat ("TEXMF=", odb)); |
|
279 else |
|
280 { |
|
281 char *oh = getenv ("OCTAVE_HOME"); |
|
282 |
|
283 if (oh) |
|
284 { |
|
285 len = strlen (oh) + 18; |
|
286 putenv_cmd = new char [len]; |
|
287 sprintf (putenv_cmd, "TEXMF=%s/lib/octave", oh); |
|
288 putenv (putenv_cmd); |
|
289 } |
|
290 else |
|
291 putenv (strsave ("TEXMF=" OCTAVE_DATADIR "/octave")); |
|
292 } |
|
293 |
1
|
294 load_path = default_path (); |
186
|
295 |
|
296 info_file = default_info_file (); |
195
|
297 |
|
298 editor = default_editor (); |
1
|
299 } |
|
300 |
|
301 void |
|
302 parse_and_execute (FILE *f, int print) |
|
303 { |
|
304 begin_unwind_frame ("parse_and_execute"); |
|
305 |
|
306 YY_BUFFER_STATE old_buf = current_buffer (); |
|
307 YY_BUFFER_STATE new_buf = create_buffer (f); |
|
308 |
|
309 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
310 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
311 |
|
312 switch_to_buffer (new_buf); |
|
313 |
|
314 unwind_protect_int (echo_input); |
|
315 unwind_protect_int (using_readline); |
|
316 unwind_protect_int (saving_history); |
|
317 |
|
318 echo_input = 0; |
|
319 using_readline = 0; |
|
320 saving_history = 0; |
|
321 |
|
322 unwind_protect_ptr (curr_sym_tab); |
|
323 |
|
324 int retval; |
|
325 do |
|
326 { |
191
|
327 reset_parser (); |
1005
|
328 |
1
|
329 retval = yyparse (); |
1005
|
330 |
529
|
331 if (retval == 0 && global_command) |
1
|
332 { |
|
333 global_command->eval (print); |
|
334 delete global_command; |
|
335 } |
|
336 } |
|
337 while (retval == 0); |
|
338 |
|
339 run_unwind_frame ("parse_and_execute"); |
|
340 } |
|
341 |
|
342 void |
578
|
343 parse_and_execute (char *s, int print, int verbose) |
1
|
344 { |
|
345 begin_unwind_frame ("parse_and_execute_2"); |
|
346 |
|
347 unwind_protect_int (reading_script_file); |
|
348 |
|
349 reading_script_file = 1; |
|
350 |
|
351 FILE *f = get_input_from_file (s, 0); |
529
|
352 if (f) |
1
|
353 { |
|
354 unwind_protect_int (input_line_number); |
|
355 unwind_protect_int (current_input_column); |
|
356 unwind_protect_int (echo_input); |
|
357 |
|
358 input_line_number = 0; |
143
|
359 current_input_column = 1; |
1
|
360 echo_input = 0; |
|
361 |
578
|
362 if (verbose) |
|
363 { |
793
|
364 cout << "reading commands from " << s << " ... "; |
578
|
365 reading_startup_message_printed = 1; |
|
366 cout.flush (); |
|
367 } |
|
368 |
1
|
369 parse_and_execute (f, print); |
578
|
370 |
1103
|
371 fclose (f); |
|
372 |
578
|
373 if (verbose) |
|
374 cout << "done." << endl; |
1
|
375 } |
|
376 |
|
377 run_unwind_frame ("parse_and_execute_2"); |
|
378 } |
|
379 |
581
|
380 // Initialize by reading startup files. |
|
381 |
1
|
382 static void |
|
383 execute_startup_files (void) |
|
384 { |
315
|
385 begin_unwind_frame ("execute_startup_files"); |
|
386 |
|
387 unwind_protect_int (input_from_startup_file); |
|
388 input_from_startup_file = 1; |
|
389 |
793
|
390 int verbose = (verbose_flag && ! inhibit_startup_message); |
578
|
391 |
1358
|
392 // Execute commands from the site-wide configuration file. |
1
|
393 |
|
394 char *sd = get_site_defaults (); |
|
395 |
578
|
396 parse_and_execute (sd, 0, verbose); |
1
|
397 |
1358
|
398 // Try to execute commands from $HOME/.octaverc and ./.octaverc. |
1
|
399 |
529
|
400 char *home_rc = 0; |
|
401 if (home_directory) |
1
|
402 { |
85
|
403 home_rc = strconcat (home_directory, "/.octaverc"); |
578
|
404 parse_and_execute (home_rc, 0, verbose); |
1
|
405 } |
|
406 |
1358
|
407 // Names alone are not enough. |
85
|
408 |
|
409 struct stat home_rc_statbuf; |
|
410 stat (home_rc, &home_rc_statbuf); |
|
411 delete [] home_rc; |
|
412 |
|
413 struct stat dot_rc_statbuf; |
|
414 stat ("./.octaverc", &dot_rc_statbuf); |
|
415 |
|
416 if (home_rc_statbuf.st_ino != dot_rc_statbuf.st_ino) |
578
|
417 parse_and_execute ("./.octaverc", 0, verbose); |
315
|
418 |
|
419 run_unwind_frame ("execute_startup_files"); |
1
|
420 } |
|
421 |
581
|
422 // Usage message with extra help. |
|
423 |
1
|
424 static void |
|
425 verbose_usage (void) |
|
426 { |
1119
|
427 cout << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
428 Usage: " << usage_string << "\n\ |
|
429 \n\ |
|
430 -d, --debug enter parser debugging mode\n\ |
1120
|
431 -f, --ignore-init-file don't read any initialization files\n\ |
1119
|
432 -h, -?, --help print short help message and exit\n\ |
|
433 -i, --interactive force interactive behavior\n\ |
1120
|
434 --info-file FILE use top-level info file FILE\n\ |
|
435 -p PATH, --path PATH set initial LOADPATH to PATH\n\ |
1119
|
436 -q, --silent don't print message at startup\n\ |
1410
|
437 --traditional set compatibility variables\n\ |
1119
|
438 -V, --verbose enable verbose output in some cases\n\ |
|
439 -v, --version print version number and exit\n\ |
|
440 -x, --echo-commands echo commands as they are executed\n\ |
|
441 \n\ |
1120
|
442 FILE execute commands from FILE\n\ |
1119
|
443 \n"; |
285
|
444 |
613
|
445 exit (0); |
1
|
446 } |
|
447 |
581
|
448 // Terse usage messsage. |
|
449 |
1
|
450 static void |
|
451 usage (void) |
|
452 { |
613
|
453 cerr << "usage: " << usage_string << "\n"; |
1
|
454 exit (1); |
|
455 } |
|
456 |
581
|
457 // Fix up things before exiting. |
|
458 |
195
|
459 void |
1
|
460 clean_up_and_exit (int retval) |
|
461 { |
|
462 raw_mode (0); |
176
|
463 |
1
|
464 clean_up_history (); |
176
|
465 |
1
|
466 close_plot_stream (); |
176
|
467 |
581
|
468 close_diary_file (); |
|
469 |
1
|
470 close_files (); |
|
471 |
318
|
472 cleanup_tmp_files (); |
|
473 |
1
|
474 if (!quitting_gracefully && (interactive || forced_interactive)) |
|
475 cout << "\n"; |
|
476 |
|
477 if (retval == EOF) |
|
478 retval = 0; |
|
479 |
|
480 exit (retval); |
195
|
481 |
1358
|
482 // This is bogus but should prevent g++ from giving a warning saying |
|
483 // that this volatile function does return. |
195
|
484 |
|
485 panic_impossible (); |
1
|
486 } |
|
487 |
|
488 static void |
|
489 print_version_and_exit (void) |
|
490 { |
1107
|
491 cout << OCTAVE_NAME_AND_VERSION << "\n"; |
1
|
492 exit (0); |
|
493 } |
|
494 |
721
|
495 static void |
|
496 initialize_error_handlers () |
|
497 { |
|
498 set_Complex_error_handler (octave_Complex_error_handler); |
|
499 |
|
500 set_liboctave_error_handler (error); |
|
501 } |
|
502 |
1410
|
503 // What happens on --traditional. |
|
504 |
|
505 static void |
|
506 maximum_braindamage (void) |
|
507 { |
|
508 bind_builtin_variable ("PS1", ">> "); |
|
509 bind_builtin_variable ("PS2", ""); |
1423
|
510 bind_builtin_variable ("beep_on_error", "true"); |
1410
|
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 { |
984
|
654 reading_script_file = 1; |
|
655 curr_fcn_file_name = argv[optind]; |
1411
|
656 |
984
|
657 FILE *infile = get_input_from_file (curr_fcn_file_name); |
1411
|
658 |
529
|
659 if (infile) |
287
|
660 { |
1411
|
661 bind_builtin_variable ("program_invocation_name", |
|
662 curr_fcn_file_name); |
|
663 |
|
664 char *tmp = strrchr (curr_fcn_file_name, '/'); |
|
665 tmp = tmp ? tmp+1 : curr_fcn_file_name; |
|
666 |
|
667 bind_builtin_variable ("program_name", tmp); |
|
668 |
|
669 intern_argv (remaining_args, argv+optind); |
|
670 |
287
|
671 rl_blink_matching_paren = 0; |
|
672 switch_to_buffer (create_buffer (infile)); |
|
673 } |
529
|
674 else |
|
675 clean_up_and_exit (1); |
1
|
676 } |
|
677 else |
|
678 { |
1355
|
679 // Is input coming from a terminal? If so, we are probably |
|
680 // interactive. |
1
|
681 |
|
682 interactive = (isatty (fileno (stdin)) && isatty (fileno (stdout))); |
1355
|
683 |
|
684 intern_argv (argc, argv); |
|
685 |
|
686 switch_to_buffer (create_buffer (get_input_from_stdin ())); |
1
|
687 } |
|
688 |
1358
|
689 // Force input to be echoed if not really interactive, but the user |
|
690 // has forced interactive behavior. |
1
|
691 |
|
692 if (!interactive && forced_interactive) |
287
|
693 { |
|
694 rl_blink_matching_paren = 0; |
|
695 echo_input = 1; |
|
696 } |
1
|
697 |
1410
|
698 if (! interactive) |
1
|
699 using_readline = 0; |
|
700 |
1358
|
701 // Allow the user to interrupt us without exiting. |
1
|
702 |
|
703 volatile sig_handler *saved_sigint_handler = signal (SIGINT, SIG_IGN); |
|
704 |
|
705 if (setjmp (toplevel) != 0) |
|
706 { |
|
707 raw_mode (0); |
176
|
708 |
1
|
709 cout << "\n"; |
|
710 } |
|
711 |
|
712 can_interrupt = 1; |
|
713 |
|
714 signal (SIGINT, saved_sigint_handler); |
|
715 |
1358
|
716 // The big loop. |
1
|
717 |
|
718 int retval; |
|
719 do |
|
720 { |
287
|
721 curr_sym_tab = top_level_sym_tab; |
|
722 |
1
|
723 reset_parser (); |
287
|
724 |
1
|
725 retval = yyparse (); |
287
|
726 |
529
|
727 if (retval == 0 && global_command) |
1
|
728 { |
|
729 global_command->eval (1); |
|
730 delete global_command; |
|
731 current_command_number++; |
|
732 } |
|
733 } |
|
734 while (retval == 0); |
|
735 |
1005
|
736 if (retval == 1 && ! error_state) |
|
737 retval = 0; |
|
738 |
1
|
739 clean_up_and_exit (retval); |
|
740 } |
|
741 |
529
|
742 DEFUN_TEXT ("casesen", Fcasesen, Scasesen, 2, 1, |
|
743 "casesen [on|off]") |
|
744 { |
|
745 Octave_object retval; |
|
746 |
|
747 DEFINE_ARGV("casesen"); |
|
748 |
|
749 if (argc == 1 || (argc > 1 && strcmp (argv[1], "off") == 0)) |
|
750 warning ("casesen: sorry, Octave is always case sensitive"); |
|
751 else if (argc > 1 && strcmp (argv[1], "on") == 0) |
|
752 ; // ok. |
|
753 else |
|
754 print_usage ("casesen"); |
|
755 |
|
756 DELETE_ARGV; |
|
757 |
|
758 return retval; |
|
759 } |
|
760 |
666
|
761 DEFUN ("computer", Fcomputer, Scomputer, 1, 0, |
|
762 "computer ():\n\ |
|
763 \n\ |
|
764 Have Octave ask the system, \"What kind of computer are you?\"") |
|
765 { |
|
766 Octave_object retval; |
|
767 |
712
|
768 int nargin = args.length (); |
|
769 |
|
770 if (nargin != 0) |
666
|
771 warning ("computer: ignoring extra arguments"); |
|
772 |
|
773 ostrstream output_buf; |
|
774 |
|
775 if (strcmp (TARGET_HOST_TYPE, "unknown") == 0) |
|
776 output_buf << "Hi Dave, I'm a HAL-9000"; |
|
777 else |
|
778 output_buf << TARGET_HOST_TYPE; |
|
779 |
|
780 if (nargout == 0) |
|
781 { |
|
782 output_buf << "\n" << ends; |
|
783 maybe_page_output (output_buf); |
|
784 } |
|
785 else |
|
786 { |
949
|
787 output_buf << ends; |
666
|
788 char *msg = output_buf.str (); |
|
789 retval = msg; |
|
790 delete [] msg; |
|
791 } |
|
792 |
|
793 return retval; |
|
794 } |
|
795 |
712
|
796 DEFUN ("flops", Fflops, Sflops, 0, 1, |
529
|
797 "flops (): count floating point operations") |
|
798 { |
|
799 int nargin = args.length (); |
|
800 |
712
|
801 if (nargin > 0) |
529
|
802 print_usage ("flops"); |
|
803 |
|
804 warning ("flops is a flop, always returning zero"); |
|
805 |
|
806 return 0.0; |
|
807 } |
|
808 |
712
|
809 DEFUN ("quit", Fquit, Squit, 0, 0, |
529
|
810 "quit (): exit Octave gracefully") |
|
811 { |
|
812 Octave_object retval; |
|
813 quitting_gracefully = 1; |
|
814 clean_up_and_exit (0); |
|
815 return retval; |
|
816 } |
|
817 |
549
|
818 DEFALIAS (exit, quit); |
|
819 |
712
|
820 DEFUN ("warranty", Fwarranty, Swarranty, 0, 0, |
529
|
821 "warranty (): describe copying conditions") |
|
822 { |
|
823 Octave_object retval; |
|
824 |
|
825 ostrstream output_buf; |
1110
|
826 output_buf << "\n" OCTAVE_NAME_VERSION_AND_COPYRIGHT "\n\n\ |
|
827 This program is free software; you can redistribute it and/or modify\n\ |
|
828 it under the terms of the GNU General Public License as published by\n\ |
|
829 the Free Software Foundation; either version 2 of the License, or\n\ |
|
830 (at your option) any later version.\n\ |
529
|
831 \n\ |
1110
|
832 This program is distributed in the hope that it will be useful,\n\ |
|
833 but WITHOUT ANY WARRANTY; without even the implied warranty of\n\ |
|
834 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\ |
|
835 GNU General Public License for more details.\n\ |
529
|
836 \n\ |
1110
|
837 You should have received a copy of the GNU General Public License\n\ |
|
838 along with this program. If not, write to the Free Software\n\ |
1315
|
839 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n\ |
529
|
840 \n"; |
|
841 |
|
842 output_buf << ends; |
|
843 maybe_page_output (output_buf); |
|
844 |
|
845 return retval; |
|
846 } |
|
847 |
|
848 // XXX FIXME XXX -- this may not be the best place for these... |
|
849 |
|
850 Octave_object |
|
851 feval (const Octave_object& args, int nargout) |
|
852 { |
|
853 Octave_object retval; |
|
854 |
712
|
855 tree_fvc *fcn = is_valid_function (args(0), "feval", 1); |
529
|
856 if (fcn) |
|
857 { |
712
|
858 int tmp_nargin = args.length () - 1; |
565
|
859 Octave_object tmp_args; |
712
|
860 tmp_args.resize (tmp_nargin); |
|
861 for (int i = 0; i < tmp_nargin; i++) |
529
|
862 tmp_args(i) = args(i+1); |
|
863 retval = fcn->eval (0, nargout, tmp_args); |
|
864 } |
|
865 |
|
866 return retval; |
|
867 } |
|
868 |
|
869 DEFUN ("feval", Ffeval, Sfeval, -1, 1, |
|
870 "feval (NAME, ARGS, ...)\n\ |
|
871 \n\ |
|
872 evaluate NAME as a function, passing ARGS as its arguments") |
|
873 { |
|
874 Octave_object retval; |
|
875 |
|
876 int nargin = args.length (); |
|
877 |
712
|
878 if (nargin > 0) |
529
|
879 retval = feval (args, nargout); |
|
880 else |
|
881 print_usage ("feval"); |
|
882 |
|
883 return retval; |
|
884 } |
|
885 |
672
|
886 static Octave_object |
529
|
887 eval_string (const char *string, int print, int ans_assign, |
672
|
888 int& parse_status, int nargout) |
529
|
889 { |
|
890 begin_unwind_frame ("eval_string"); |
|
891 |
|
892 unwind_protect_int (get_input_from_eval_string); |
|
893 unwind_protect_ptr (global_command); |
|
894 unwind_protect_ptr (current_eval_string); |
|
895 |
|
896 get_input_from_eval_string = 1; |
|
897 current_eval_string = string; |
|
898 |
|
899 YY_BUFFER_STATE old_buf = current_buffer (); |
|
900 YY_BUFFER_STATE new_buf = create_buffer (0); |
|
901 |
|
902 add_unwind_protect (restore_input_buffer, (void *) old_buf); |
|
903 add_unwind_protect (delete_input_buffer, (void *) new_buf); |
|
904 |
|
905 switch_to_buffer (new_buf); |
|
906 |
|
907 unwind_protect_ptr (curr_sym_tab); |
|
908 |
|
909 reset_parser (); |
|
910 |
|
911 parse_status = yyparse (); |
|
912 |
1358
|
913 // Important to reset the idea of where input is coming from before |
|
914 // trying to eval the command we just parsed -- it might contain the |
|
915 // name of an function file that still needs to be parsed! |
529
|
916 |
578
|
917 tree_statement_list *command = global_command; |
529
|
918 |
|
919 run_unwind_frame ("eval_string"); |
|
920 |
672
|
921 Octave_object retval; |
529
|
922 |
|
923 if (parse_status == 0 && command) |
|
924 { |
672
|
925 retval = command->eval (print, nargout); |
529
|
926 delete command; |
|
927 } |
|
928 |
|
929 return retval; |
|
930 } |
|
931 |
|
932 tree_constant |
672
|
933 eval_string (const char *string, int print, int ans_assign, |
|
934 int& parse_status) |
|
935 { |
|
936 tree_constant retval; |
|
937 |
|
938 Octave_object tmp = eval_string (string, print, ans_assign, |
|
939 parse_status, 1); |
|
940 |
|
941 retval = tmp(0); |
|
942 |
|
943 return retval; |
|
944 } |
|
945 |
|
946 static Octave_object |
|
947 eval_string (const tree_constant& arg, int& parse_status, int nargout) |
529
|
948 { |
1363
|
949 const char *string = arg.string_value (); |
636
|
950 |
|
951 if (error_state) |
529
|
952 { |
|
953 error ("eval: expecting string argument"); |
672
|
954 return -1.0; |
529
|
955 } |
|
956 |
1358
|
957 // Yes Virginia, we always print here... |
529
|
958 |
672
|
959 return eval_string (string, 1, 1, parse_status, nargout); |
529
|
960 } |
|
961 |
712
|
962 DEFUN ("eval", Feval, Seval, 2, 1, |
672
|
963 "eval (TRY, CATCH)\n\ |
|
964 \n\ |
|
965 Evaluate the string TRY as octave code. If that fails, evaluate the\n\ |
|
966 string CATCH.") |
529
|
967 { |
|
968 Octave_object retval; |
|
969 |
|
970 int nargin = args.length (); |
|
971 |
712
|
972 if (nargin > 0) |
529
|
973 { |
672
|
974 begin_unwind_frame ("Feval"); |
|
975 |
712
|
976 if (nargin > 1) |
672
|
977 { |
|
978 unwind_protect_int (suppress_octave_error_messages); |
|
979 suppress_octave_error_messages = 1; |
|
980 } |
|
981 |
529
|
982 int parse_status = 0; |
672
|
983 |
712
|
984 retval = eval_string (args(0), parse_status, nargout); |
672
|
985 |
712
|
986 if (nargin > 1 && (parse_status != 0 || error_state)) |
672
|
987 { |
|
988 error_state = 0; |
712
|
989 eval_string (args(1), parse_status, nargout); |
672
|
990 retval = Octave_object (); |
|
991 } |
|
992 |
|
993 run_unwind_frame ("Feval"); |
529
|
994 } |
|
995 else |
|
996 print_usage ("eval"); |
|
997 |
|
998 return retval; |
|
999 } |
|
1000 |
581
|
1001 // Execute a shell command. |
|
1002 |
703
|
1003 DEFUN ("system", Fsystem, Ssystem, 2, 1, |
|
1004 "system (string [, return_output]): execute shell commands") |
560
|
1005 { |
|
1006 Octave_object retval; |
|
1007 |
|
1008 int nargin = args.length (); |
|
1009 |
712
|
1010 if (nargin < 1 || nargin > 2) |
560
|
1011 { |
712
|
1012 print_usage ("system"); |
624
|
1013 return retval; |
|
1014 } |
|
1015 |
712
|
1016 tree_constant tc_command = args(0); |
624
|
1017 |
1363
|
1018 const char *tmp_str = tc_command.string_value (); |
636
|
1019 |
|
1020 if (error_state) |
624
|
1021 { |
712
|
1022 error ("system: expecting string as first argument"); |
636
|
1023 } |
|
1024 else |
|
1025 { |
|
1026 iprocstream cmd (tmp_str); |
624
|
1027 |
|
1028 ostrstream output_buf; |
|
1029 |
560
|
1030 char ch; |
|
1031 while (cmd.get (ch)) |
|
1032 output_buf.put (ch); |
624
|
1033 |
560
|
1034 output_buf << ends; |
624
|
1035 |
560
|
1036 int status = cmd.close (); |
624
|
1037 |
1358
|
1038 // The value in status is as returned by waitpid. If the |
|
1039 // process exited normally, extract the actual exit status of |
|
1040 // the command. Otherwise, return 127 as a failure code. |
1022
|
1041 |
|
1042 if ((status & 0xff) == 0) |
|
1043 status = (status & 0xff00) >> 8; |
|
1044 else |
|
1045 status = 127; |
|
1046 |
712
|
1047 if (nargout > 0 || nargin > 1) |
560
|
1048 { |
624
|
1049 char *msg = output_buf.str (); |
|
1050 |
|
1051 retval(1) = (double) status; |
|
1052 retval(0) = msg; |
|
1053 |
|
1054 delete [] msg; |
560
|
1055 } |
624
|
1056 else |
|
1057 maybe_page_output (output_buf); |
560
|
1058 } |
|
1059 |
|
1060 return retval; |
|
1061 } |
|
1062 |
703
|
1063 DEFALIAS (shell_cmd, system); |
|
1064 |
1266
|
1065 #if defined (__GNUG__) && defined (DEBUG_NEW_DELETE) |
|
1066 int debug_new_delete = 0; |
|
1067 |
|
1068 typedef void (*vfp)(void); |
|
1069 extern vfp __new_handler; |
|
1070 |
|
1071 void * |
|
1072 __builtin_new (size_t sz) |
|
1073 { |
|
1074 void *p; |
|
1075 |
|
1076 /* malloc (0) is unpredictable; avoid it. */ |
|
1077 if (sz == 0) |
|
1078 sz = 1; |
|
1079 p = (void *) malloc (sz); |
|
1080 while (p == 0) |
|
1081 { |
|
1082 (*__new_handler) (); |
|
1083 p = (void *) malloc (sz); |
|
1084 } |
|
1085 |
|
1086 if (debug_new_delete) |
|
1087 cout << "__builtin_new: " << p << endl; |
|
1088 |
|
1089 return p; |
|
1090 } |
|
1091 |
|
1092 void |
|
1093 __builtin_delete (void *ptr) |
|
1094 { |
|
1095 if (debug_new_delete) |
|
1096 cout << "__builtin_delete: " << ptr << endl; |
|
1097 |
|
1098 if (ptr) |
|
1099 free (ptr); |
|
1100 } |
|
1101 #endif |
|
1102 |
560
|
1103 /* |
1
|
1104 ;;; Local Variables: *** |
|
1105 ;;; mode: C++ *** |
|
1106 ;;; page-delimiter: "^/\\*" *** |
|
1107 ;;; End: *** |
|
1108 */ |