Mercurial > hg > octave-nkf
comparison src/defaults.cc @ 5832:5e41e06f6a78
[project @ 2006-05-26 21:41:32 by jwe]
author | jwe |
---|---|
date | Fri, 26 May 2006 21:42:22 +0000 |
parents | 080c08b192d8 |
children | a6a2423a9c25 |
comparison
equal
deleted
inserted
replaced
5831:b0d4ff99a0c5 | 5832:5e41e06f6a78 |
---|---|
48 #include "error.h" | 48 #include "error.h" |
49 #include "file-ops.h" | 49 #include "file-ops.h" |
50 #include "gripes.h" | 50 #include "gripes.h" |
51 #include "help.h" | 51 #include "help.h" |
52 #include "input.h" | 52 #include "input.h" |
53 #include "load-path.h" | |
53 #include "oct-obj.h" | 54 #include "oct-obj.h" |
54 #include "ov.h" | 55 #include "ov.h" |
55 #include "parse.h" | 56 #include "parse.h" |
56 #include "toplev.h" | 57 #include "toplev.h" |
57 #include "unwind-prot.h" | 58 #include "unwind-prot.h" |
83 | 84 |
84 // The path that will be searched for programs that we execute. | 85 // The path that will be searched for programs that we execute. |
85 // (--exec-path path) | 86 // (--exec-path path) |
86 static std::string VEXEC_PATH; | 87 static std::string VEXEC_PATH; |
87 | 88 |
88 // Load path specified on command line. | |
89 // (--path path; -p path) | |
90 static std::string VLOADPATH; | |
91 | |
92 // The default load path with OCTAVE_HOME appropriately substituted. | |
93 static std::string VDEFAULT_LOADPATH; | |
94 | |
95 // And the cached directory path corresponding to Vload_path. | |
96 dir_path Vload_path_dir_path; | |
97 | |
98 // Name of the editor to be invoked by the edit_history command. | 89 // Name of the editor to be invoked by the edit_history command. |
99 std::string VEDITOR; | 90 std::string VEDITOR; |
100 | 91 |
101 static std::string VIMAGE_PATH; | 92 static std::string VIMAGE_PATH; |
102 | 93 |
103 std::string Vlocal_site_defaults_file; | 94 std::string Vlocal_site_defaults_file; |
104 std::string Vsite_defaults_file; | 95 std::string Vsite_defaults_file; |
105 | 96 |
106 // Name of the FFTW wisdom program. | 97 // Name of the FFTW wisdom program. |
107 std::string Vfftw_wisdom_program; | 98 std::string Vfftw_wisdom_program; |
108 | |
109 // Each element of A and B should be directory names. For each | |
110 // element of A not in the list B, execute SCRIPT_FILE in that | |
111 // directory if it exists. | |
112 | |
113 static void | |
114 maybe_add_or_del_packages (const string_vector& a, | |
115 const string_vector& b, | |
116 const std::string& script_file) | |
117 { | |
118 if (! octave_interpreter_ready) | |
119 return; | |
120 | |
121 unwind_protect::begin_frame ("maybe_add_or_del_packages"); | |
122 | |
123 unwind_protect_bool (input_from_startup_file); | |
124 | |
125 input_from_startup_file = true; | |
126 | |
127 octave_idx_type a_len = a.length (); | |
128 octave_idx_type b_len = b.length (); | |
129 | |
130 for (octave_idx_type i = 0; i < a_len; i++) | |
131 { | |
132 std::string a_dir = a[i]; | |
133 | |
134 bool found = false; | |
135 | |
136 for (octave_idx_type j = 0; j < b_len; j++) | |
137 { | |
138 if (b[j] == a_dir) | |
139 { | |
140 found = true; | |
141 break; | |
142 } | |
143 } | |
144 | |
145 if (! found) | |
146 { | |
147 std::string file = a_dir + file_ops::dir_sep_str + script_file; | |
148 | |
149 file_stat fs = file_stat (file); | |
150 | |
151 if (fs.exists ()) | |
152 source_file (file); | |
153 | |
154 if (error_state) | |
155 return; | |
156 } | |
157 } | |
158 | |
159 unwind_protect::run_frame ("maybe_add_or_del_packages"); | |
160 } | |
161 | |
162 static void | |
163 update_load_path_dir_path (void) | |
164 { | |
165 string_vector old_dirs = Vload_path_dir_path.all_directories (); | |
166 | |
167 Vload_path_dir_path = dir_path (VLOADPATH, ""); | |
168 | |
169 string_vector new_dirs = Vload_path_dir_path.all_directories (); | |
170 | |
171 maybe_add_or_del_packages (old_dirs, new_dirs, "PKG_DEL"); | |
172 | |
173 if (! error_state) | |
174 maybe_add_or_del_packages (new_dirs, old_dirs, "PKG_ADD"); | |
175 } | |
176 | |
177 void | |
178 execute_default_pkg_add_files (void) | |
179 { | |
180 string_vector old_dirs; | |
181 string_vector new_dirs = Vload_path_dir_path.all_directories (); | |
182 | |
183 maybe_add_or_del_packages (new_dirs, old_dirs, "PKG_ADD"); | |
184 } | |
185 | 99 |
186 static std::string | 100 static std::string |
187 subst_octave_home (const std::string& s) | 101 subst_octave_home (const std::string& s) |
188 { | 102 { |
189 std::string retval; | 103 std::string retval; |
335 VEXEC_PATH = tpath + dir_path::path_sep_str + VEXEC_PATH; | 249 VEXEC_PATH = tpath + dir_path::path_sep_str + VEXEC_PATH; |
336 | 250 |
337 octave_env::putenv ("PATH", VEXEC_PATH); | 251 octave_env::putenv ("PATH", VEXEC_PATH); |
338 } | 252 } |
339 | 253 |
340 static std::string | |
341 genpath (const std::string& dirname) | |
342 { | |
343 std::string retval; | |
344 | |
345 std::string full_dirname = file_ops::tilde_expand (dirname); | |
346 | |
347 dir_entry dir (full_dirname); | |
348 | |
349 if (dir) | |
350 { | |
351 retval = dirname; | |
352 | |
353 string_vector dirlist = dir.read (); | |
354 | |
355 octave_idx_type len = dirlist.length (); | |
356 | |
357 for (octave_idx_type i = 0; i < len; i++) | |
358 { | |
359 std::string elt = dirlist[i]; | |
360 | |
361 if (elt != "." && elt != ".." && elt != "private") | |
362 { | |
363 std::string nm = full_dirname + file_ops::dir_sep_str + elt; | |
364 | |
365 file_stat fs (nm); | |
366 | |
367 if (fs && fs.is_dir ()) | |
368 retval += dir_path::path_sep_str + genpath (nm); | |
369 } | |
370 } | |
371 } | |
372 | |
373 return retval; | |
374 } | |
375 | |
376 static void | |
377 maybe_add_path_elts (std::string& pathvar, const std::string& dir) | |
378 { | |
379 std::string tpath = genpath (dir); | |
380 | |
381 if (! tpath.empty ()) | |
382 pathvar += dir_path::path_sep_str + tpath; | |
383 } | |
384 | |
385 void | |
386 set_load_path (const std::string& path) | |
387 { | |
388 VDEFAULT_LOADPATH = ":"; | |
389 | |
390 maybe_add_path_elts (VDEFAULT_LOADPATH, Vlocal_ver_oct_file_dir); | |
391 maybe_add_path_elts (VDEFAULT_LOADPATH, Vlocal_api_oct_file_dir); | |
392 maybe_add_path_elts (VDEFAULT_LOADPATH, Vlocal_oct_file_dir); | |
393 maybe_add_path_elts (VDEFAULT_LOADPATH, Vlocal_ver_fcn_file_dir); | |
394 maybe_add_path_elts (VDEFAULT_LOADPATH, Vlocal_api_fcn_file_dir); | |
395 maybe_add_path_elts (VDEFAULT_LOADPATH, Vlocal_fcn_file_dir); | |
396 maybe_add_path_elts (VDEFAULT_LOADPATH, Voct_file_dir); | |
397 maybe_add_path_elts (VDEFAULT_LOADPATH, Vfcn_file_dir); | |
398 | |
399 std::string tpath = path; | |
400 | |
401 if (tpath.empty ()) | |
402 tpath = octave_env::getenv ("OCTAVE_LOADPATH"); | |
403 | |
404 VLOADPATH = "."; | |
405 | |
406 if (! tpath.empty ()) | |
407 VLOADPATH += dir_path::path_sep_str + tpath; | |
408 | |
409 if (VDEFAULT_LOADPATH != ":") | |
410 VLOADPATH += VDEFAULT_LOADPATH; | |
411 | |
412 update_load_path_dir_path (); | |
413 } | |
414 | |
415 void | 254 void |
416 set_image_path (const std::string& path) | 255 set_image_path (const std::string& path) |
417 { | 256 { |
418 VIMAGE_PATH = "."; | 257 VIMAGE_PATH = "."; |
419 | 258 |
423 tpath = octave_env::getenv ("OCTAVE_IMAGE_PATH"); | 262 tpath = octave_env::getenv ("OCTAVE_IMAGE_PATH"); |
424 | 263 |
425 if (! tpath.empty ()) | 264 if (! tpath.empty ()) |
426 VIMAGE_PATH += dir_path::path_sep_str + tpath; | 265 VIMAGE_PATH += dir_path::path_sep_str + tpath; |
427 | 266 |
428 maybe_add_path_elts (VIMAGE_PATH, Vimage_dir); | 267 tpath = genpath (Vimage_dir, ""); |
268 | |
269 if (! tpath.empty ()) | |
270 VIMAGE_PATH += dir_path::path_sep_str + tpath; | |
429 } | 271 } |
430 | 272 |
431 static void | 273 static void |
432 set_default_info_file (void) | 274 set_default_info_file (void) |
433 { | 275 { |
533 | 375 |
534 set_default_bin_dir (); | 376 set_default_bin_dir (); |
535 | 377 |
536 set_exec_path (); | 378 set_exec_path (); |
537 | 379 |
538 set_load_path (); | |
539 | |
540 set_image_path (); | 380 set_image_path (); |
541 | 381 |
542 set_default_info_file (); | 382 set_default_info_file (); |
543 | 383 |
544 set_default_info_prog (); | 384 set_default_info_prog (); |
548 set_default_editor (); | 388 set_default_editor (); |
549 | 389 |
550 set_local_site_defaults_file (); | 390 set_local_site_defaults_file (); |
551 | 391 |
552 set_site_defaults_file (); | 392 set_site_defaults_file (); |
553 } | |
554 | |
555 DEFUN (genpath, args, , | |
556 "-*- texinfo -*-\n\ | |
557 @deftypefn {Built-in Function} {} genpath (@var{dir})\n\ | |
558 Return a path constructed from @var{dir} and all its subdiretories.\n\ | |
559 @end deftypefn") | |
560 { | |
561 octave_value retval; | |
562 | |
563 if (args.length () == 1) | |
564 { | |
565 std::string dirname = args(0).string_value (); | |
566 | |
567 if (! error_state) | |
568 retval = genpath (dirname); | |
569 else | |
570 error ("genpath: expecting argument to be a character string"); | |
571 } | |
572 else | |
573 print_usage (); | |
574 | |
575 return retval; | |
576 } | |
577 | |
578 DEFUN (rehash, , , | |
579 "-*- texinfo -*-\n\ | |
580 @deftypefn {Built-in Function} {} rehash ()\n\ | |
581 Reinitialize Octave's @code{LOADPATH} directory cache.\n\ | |
582 @end deftypefn") | |
583 { | |
584 octave_value_list retval; | |
585 | |
586 Vload_path_dir_path.rehash (); | |
587 | |
588 return retval; | |
589 } | 393 } |
590 | 394 |
591 DEFUN (EDITOR, args, nargout, | 395 DEFUN (EDITOR, args, nargout, |
592 "-*- texinfo -*-\n\ | 396 "-*- texinfo -*-\n\ |
593 @deftypefn {Built-in Function} {@var{val} =} EDITOR ()\n\ | 397 @deftypefn {Built-in Function} {@var{val} =} EDITOR ()\n\ |
652 @end deftypefn") | 456 @end deftypefn") |
653 { | 457 { |
654 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (IMAGE_PATH); | 458 return SET_NONEMPTY_INTERNAL_STRING_VARIABLE (IMAGE_PATH); |
655 } | 459 } |
656 | 460 |
657 DEFUN (path, args, nargout, | |
658 "-*- texinfo -*-\n\ | |
659 @deftypefn {Function File} {} path (@dots{})\n\ | |
660 Modify or display Octave's @code{LOADPATH}.\n\ | |
661 \n\ | |
662 If @var{nargin} and @var{nargout} are zero, display the elements of\n\ | |
663 Octave's @code{LOADPATH} in an easy to read format.\n\ | |
664 \n\ | |
665 If @var{nargin} is zero and nargout is greater than zero, return the\n\ | |
666 current value of @code{LOADPATH}.\n\ | |
667 \n\ | |
668 If @var{nargin} is greater than zero, concatenate the arguments,\n\ | |
669 separating them with @code{pathsep()}. Set the internal search path\n\ | |
670 to the result and return it.\n\ | |
671 \n\ | |
672 No checks are made for duplicate elements.\n\ | |
673 @seealso{addpath, rmpath, genpath, pathdef, savepath, pathsep}\n\ | |
674 @end deftypefn") | |
675 { | |
676 octave_value retval; | |
677 | |
678 int argc = args.length () + 1; | |
679 | |
680 string_vector argv = args.make_argv ("path"); | |
681 | |
682 if (! error_state) | |
683 { | |
684 if (argc > 1) | |
685 { | |
686 std::string path = argv[1]; | |
687 | |
688 for (int i = 2; i < argc; i++) | |
689 path += dir_path::path_sep_str; | |
690 | |
691 size_t plen = path.length (); | |
692 | |
693 if (! ((plen == 1 && path[0] == ':') | |
694 || (plen > 1 | |
695 && path.substr (0, 2) == ("." + dir_path::path_sep_str)))) | |
696 path = "." + dir_path::path_sep_str + path; | |
697 | |
698 VLOADPATH = path; | |
699 | |
700 // By resetting the last prompt time variable, we will force | |
701 // checks for out of date symbols even if the change to | |
702 // LOADPATH and subsequent function calls happen between | |
703 // prompts. | |
704 | |
705 // FIXME -- maybe we should rename | |
706 // Vlast_prompt_time_stamp since the new usage doesn't really | |
707 // fit with the current name? | |
708 | |
709 Vlast_prompt_time.stamp (); | |
710 | |
711 update_load_path_dir_path (); | |
712 } | |
713 | |
714 if (nargout > 0) | |
715 retval = VLOADPATH; | |
716 else if (argc == 1 && nargout == 0) | |
717 { | |
718 octave_stdout << "\nOctave's search path contains the following directories:\n\n"; | |
719 | |
720 string_vector sv = Vload_path_dir_path.all_directories (); | |
721 | |
722 sv.list_in_columns (octave_stdout); | |
723 | |
724 octave_stdout << "\n"; | |
725 } | |
726 } | |
727 | |
728 return retval; | |
729 } | |
730 | |
731 DEFUN (pathdef, , , | |
732 "-*- texinfo -*-\n\ | |
733 @deftypefn {Built-in Function} {@var{val} =} DEFAULT_LOADPATH ()\n\ | |
734 Return the default list of directories in which to search for function\n\ | |
735 files.\n\ | |
736 @seealso{LOADPATH}\n\ | |
737 @end deftypefn") | |
738 { | |
739 return octave_value (VDEFAULT_LOADPATH); | |
740 } | |
741 | |
742 DEFUN (OCTAVE_HOME, args, , | 461 DEFUN (OCTAVE_HOME, args, , |
743 "-*- texinfo -*-\n\ | 462 "-*- texinfo -*-\n\ |
744 @deftypefn {Built-in Function} {} OCTAVE_HOME ()\n\ | 463 @deftypefn {Built-in Function} {} OCTAVE_HOME ()\n\ |
745 Return the name of the top-level Octave installation directory.\n\ | 464 Return the name of the top-level Octave installation directory.\n\ |
746 @end deftypefn") | 465 @end deftypefn") |