comparison src/octave.cc @ 2926:66ef74ee5d9f

[project @ 1997-05-05 03:20:52 by jwe]
author jwe
date Mon, 05 May 1997 03:40:21 +0000
parents 1da1af95082e
children 20f5cec4f11c
comparison
equal deleted inserted replaced
2925:f0665dac8e33 2926:66ef74ee5d9f
41 #include <sys/types.h> 41 #include <sys/types.h>
42 #endif 42 #endif
43 #include <unistd.h> 43 #include <unistd.h>
44 #endif 44 #endif
45 45
46 #ifdef HAVE_PWD_H 46 #include "cmd-edit.h"
47 #include <pwd.h> 47 #include "file-stat.h"
48 #endif
49
50 #include "lo-error.h" 48 #include "lo-error.h"
49 #include "oct-env.h"
51 #include "str-vec.h" 50 #include "str-vec.h"
52 51
53 #include <defaults.h> 52 #include <defaults.h>
54 #include "defun.h" 53 #include "defun.h"
55 #include "dynamic-ld.h" 54 #include "dynamic-ld.h"
56 #include "error.h" 55 #include "error.h"
57 #include "file-io.h" 56 #include "file-io.h"
58 #include "file-ops.h"
59 #include "help.h" 57 #include "help.h"
60 #include "input.h" 58 #include "input.h"
61 #include "lex.h" 59 #include "lex.h"
62 #include "oct-hist.h" 60 #include "oct-hist.h"
63 #include "ops.h" 61 #include "ops.h"
88 extern char *program_invocation_name; 86 extern char *program_invocation_name;
89 extern char *program_invocation_short_name; 87 extern char *program_invocation_short_name;
90 #else 88 #else
91 char *program_invocation_name; 89 char *program_invocation_name;
92 char *program_invocation_short_name; 90 char *program_invocation_short_name;
93 #endif
94
95 #if defined (USE_READLINE)
96 // This is from readline's paren.c:
97 extern int rl_blink_matching_paren;
98 #endif 91 #endif
99 92
100 // The command-line options. 93 // The command-line options.
101 static string_vector octave_argv; 94 static string_vector octave_argv;
102 95
167 // Store the command-line options for later use. 160 // Store the command-line options for later use.
168 161
169 static void 162 static void
170 intern_argv (int argc, char **argv) 163 intern_argv (int argc, char **argv)
171 { 164 {
165 octave_env::set_program_name (argv[0]);
166
167 // XXX FIXME XXX -- Kpathsea needs this.
168
169 #if ! defined (HAVE_PROGRAM_INVOCATION_NAME)
170 program_invocation_name
171 = strsave (octave_env::get_program_invocation_name () . c_str ());
172
173 program_invocation_short_name
174 = strsave (octave_env::get_program_name () . c_str ());
175 #endif
176
172 if (argc > 1) 177 if (argc > 1)
173 { 178 {
174 // Skip program name in argv. 179 // Skip program name in argv.
175 180
176 octave_argv = string_vector (argv+1, argc-1); 181 octave_argv = string_vector (argv+1, argc-1);
180 } 185 }
181 186
182 bind_builtin_variable ("nargin", static_cast<double> (argc-1), 1, 1, 0); 187 bind_builtin_variable ("nargin", static_cast<double> (argc-1), 1, 1, 0);
183 } 188 }
184 189
185 // Initialize some global variables for later use.
186
187 static void
188 initialize_globals (const string& name)
189 {
190 // Kpathsea needs this.
191
192 #if ! defined (HAVE_PROGRAM_INVOCATION_NAME)
193 program_invocation_name = strsave (name.c_str ());
194 program_invocation_short_name = strrchr (program_invocation_name, '/');
195 if (! program_invocation_short_name)
196 program_invocation_short_name = program_invocation_name;
197 #endif
198
199 Vprogram_invocation_name = name;
200 size_t pos = Vprogram_invocation_name.rfind ('/');
201 Vprogram_name = (pos == NPOS)
202 ? Vprogram_invocation_name : Vprogram_invocation_name.substr (pos+1);
203
204 struct passwd *entry = getpwuid (getuid ());
205 Vuser_name = entry ? entry->pw_name : "I have no name!";
206
207 char hostname[256];
208 int status = gethostname (hostname, 255);
209 Vhost_name = (status < 0) ? "I have no host!" : hostname;
210
211 char *hd = getenv ("HOME");
212 Vhome_directory = hd ? hd : "I have no home!";
213
214 install_defaults ();
215 }
216
217 static void 190 static void
218 initialize_pathsearch (void) 191 initialize_pathsearch (void)
219 { 192 {
220 // This may seem odd, but doing it this way means that we don't have 193 // This may seem odd, but doing it this way means that we don't have
221 // to modify the kpathsea library... 194 // to modify the kpathsea library...
222 195
223 char *odb = getenv ("OCTAVE_DB_DIR"); 196 string odb = octave_env::getenv ("OCTAVE_DB_DIR");
224 197
225 if (odb) 198 if (odb.empty ())
226 oct_putenv ("TEXMF", odb); 199 {
200 string oh = octave_env::getenv ("OCTAVE_HOME");
201
202 if (oh.empty ())
203 octave_env::putenv ("TEXMF", OCTAVE_DATADIR "/octave");
204 else
205 {
206 oh.append ("/lib/octave");
207 octave_env::putenv ("TEXMF", oh);
208 }
209 }
227 else 210 else
228 { 211 octave_env::putenv ("TEXMF", odb);
229 char *oh = getenv ("OCTAVE_HOME");
230
231 if (oh)
232 {
233 int len = strlen (oh) + 12;
234 char *putenv_val = new char [len];
235 sprintf (putenv_val, "%s/lib/octave", oh);
236 oct_putenv ("TEXMF", putenv_val);
237 }
238 else
239 oct_putenv ("TEXMF", OCTAVE_DATADIR "/octave");
240 }
241 } 212 }
242 213
243 // Initialize by reading startup files. 214 // Initialize by reading startup files.
244 215
245 static void 216 static void
274 // $OCTAVE_INITFILE. If $OCTAVE_INITFILE is not set, .octaverc 245 // $OCTAVE_INITFILE. If $OCTAVE_INITFILE is not set, .octaverc
275 // is assumed. 246 // is assumed.
276 247
277 int home_rc_already_executed = 0; 248 int home_rc_already_executed = 0;
278 249
279 const char *initfile = getenv ("OCTAVE_INITFILE"); 250 string initfile = octave_env::getenv ("OCTAVE_INITFILE");
280 251
281 if (! initfile) 252 if (initfile.empty ())
282 initfile = ".octaverc"; 253 initfile = ".octaverc";
283 254
284 string home_rc = Vhome_directory + "/" + initfile; 255 string home_dir = octave_env::get_home_directory ();
256
257 string home_rc = home_dir + "/" + initfile;
285 string local_rc = string ("./") + initfile; 258 string local_rc = string ("./") + initfile;
286 259
287 if (! Vhome_directory.empty ()) 260 if (! home_dir.empty ())
288 { 261 {
289 parse_and_execute (home_rc, verbose); 262 parse_and_execute (home_rc, verbose);
290 263
291 // Names alone are not enough. 264 // Names alone are not enough.
292 265
394 367
395 int 368 int
396 main (int argc, char **argv) 369 main (int argc, char **argv)
397 { 370 {
398 // The order of these calls is important. The call to 371 // The order of these calls is important. The call to
399 // initialize_globals must come before install_builtins because 372 // install_defaults must come before install_builtins because
400 // default variable values must be available for the varaibles to be 373 // default variable values must be available for the varaibles to be
401 // installed, and the call to install_builtins must come before the 374 // installed, and the call to install_builtins must come before the
402 // options are processed because some command line options override 375 // options are processed because some command line options override
403 // defaults by calling bind_builtin_variable. 376 // defaults by calling bind_builtin_variable.
404 377
405 sysdep_init (); 378 sysdep_init ();
406 379
407 initialize_error_handlers (); 380 initialize_error_handlers ();
408 381
409 initialize_globals (argv[0]); 382 install_defaults ();
410 383
411 initialize_pathsearch (); 384 initialize_pathsearch ();
412 385
413 install_signal_handlers (); 386 install_signal_handlers ();
414 387
489 case NO_INIT_FILE_OPTION: 462 case NO_INIT_FILE_OPTION:
490 read_init_files = false; 463 read_init_files = false;
491 break; 464 break;
492 465
493 case NO_LINE_EDITING_OPTION: 466 case NO_LINE_EDITING_OPTION:
494 using_readline = false; 467 line_editing = 0;
495 break; 468 break;
496 469
497 case NO_SITE_FILE_OPTION: 470 case NO_SITE_FILE_OPTION:
498 read_site_files = 0; 471 read_site_files = 0;
499 break; 472 break;
519 #endif 492 #endif
520 493
521 // These can come after command line args since none of them set any 494 // These can come after command line args since none of them set any
522 // defaults that might be changed by command line options. 495 // defaults that might be changed by command line options.
523 496
524 initialize_readline (); 497 initialize_command_input ();
525 498
526 if (! inhibit_startup_message) 499 if (! inhibit_startup_message)
527 cout << OCTAVE_STARTUP_MESSAGE "\n" << endl; 500 cout << OCTAVE_STARTUP_MESSAGE "\n" << endl;
528 501
529 if (traditional) 502 if (traditional)
530 maximum_braindamage (); 503 maximum_braindamage ();
531 504
532 execute_startup_files (); 505 execute_startup_files ();
533 506
534 octave_command_history.read (false); 507 command_history::read (false);
535 508
536 if (! inhibit_startup_message && reading_startup_message_printed) 509 if (! inhibit_startup_message && reading_startup_message_printed)
537 cout << endl; 510 cout << endl;
538 511
539 // Avoid counting commands executed from startup files. 512 // Avoid counting commands executed from startup files.
540 513
541 current_command_number = 1; 514 command_editor::reset_current_command_number (1);
542 515
543 // If there is an extra argument, see if it names a file to read. 516 // If there is an extra argument, see if it names a file to read.
544 // Additional arguments are taken as command line options for the 517 // Additional arguments are taken as command line options for the
545 // script. 518 // script.
546 519
568 541
569 bind_builtin_variable ("program_name", tmp); 542 bind_builtin_variable ("program_name", tmp);
570 543
571 intern_argv (remaining_args, argv+last_arg_idx); 544 intern_argv (remaining_args, argv+last_arg_idx);
572 545
573 rl_blink_matching_paren = 0; 546 command_editor::blink_matching_paren (false);
547
574 switch_to_buffer (create_buffer (infile)); 548 switch_to_buffer (create_buffer (infile));
575 } 549 }
576 else 550 else
577 clean_up_and_exit (1); 551 clean_up_and_exit (1);
578 } 552 }
591 // Force input to be echoed if not really interactive, but the user 565 // Force input to be echoed if not really interactive, but the user
592 // has forced interactive behavior. 566 // has forced interactive behavior.
593 567
594 if (! interactive && forced_interactive) 568 if (! interactive && forced_interactive)
595 { 569 {
596 rl_blink_matching_paren = 0; 570 command_editor::blink_matching_paren (false);
597 571
598 // XXX FIXME XXX -- is this the right thing to do? 572 // XXX FIXME XXX -- is this the right thing to do?
599 573
600 bind_builtin_variable ("echo_executing_commands", 574 bind_builtin_variable ("echo_executing_commands",
601 static_cast<double> (ECHO_CMD_LINE)); 575 static_cast<double> (ECHO_CMD_LINE));
602 } 576 }
603 577
604 if (! interactive) 578 if (! interactive)
605 using_readline = 0; 579 line_editing = 0;
606 580
607 int retval = main_loop (); 581 int retval = main_loop ();
608 582
609 if (retval == 1 && ! error_state) 583 if (retval == 1 && ! error_state)
610 retval = 0; 584 retval = 0;