2203
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
2203
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <cstdlib> |
|
28 |
3503
|
29 #include <iostream> |
2203
|
30 #include <string> |
|
31 |
|
32 #ifdef HAVE_UNISTD_H |
2442
|
33 #ifdef HAVE_SYS_TYPES_H |
2203
|
34 #include <sys/types.h> |
2442
|
35 #endif |
2203
|
36 #include <unistd.h> |
|
37 #endif |
|
38 |
2926
|
39 #include "oct-env.h" |
3174
|
40 #include "pathsearch.h" |
2926
|
41 |
2492
|
42 #include <defaults.h> |
2203
|
43 #include "defun.h" |
|
44 #include "error.h" |
3040
|
45 #include "file-ops.h" |
2203
|
46 #include "gripes.h" |
|
47 #include "help.h" |
3185
|
48 #include "oct-obj.h" |
2390
|
49 #include "ov.h" |
2203
|
50 #include "toplev.h" |
|
51 #include "variables.h" |
2492
|
52 #include <version.h> |
2203
|
53 |
3523
|
54 std::string Voctave_home; |
2203
|
55 |
3523
|
56 std::string Vbin_dir; |
|
57 std::string Vinfo_dir; |
|
58 std::string Vdata_dir; |
|
59 std::string Vlibexec_dir; |
|
60 std::string Varch_lib_dir; |
|
61 std::string Vlocal_arch_lib_dir; |
3597
|
62 std::string Vlocal_ver_arch_lib_dir; |
3523
|
63 std::string Vfcn_file_dir; |
2203
|
64 |
|
65 // The path that will be searched for programs that we execute. |
|
66 // (--exec-path path) |
3523
|
67 std::string Vexec_path; |
2203
|
68 |
|
69 // Load path specified on command line. |
|
70 // (--path path; -p path) |
3523
|
71 static std::string Vload_path; |
2203
|
72 |
3192
|
73 // The default load path with OCTAVE_HOME appropriately substituted. |
3523
|
74 static std::string Vdefault_load_path; |
3192
|
75 |
3174
|
76 // And the cached directory path corresponding to Vload_path. |
|
77 dir_path Vload_path_dir_path; |
|
78 |
2203
|
79 // Name of the editor to be invoked by the edit_history command. |
3523
|
80 std::string Veditor; |
2203
|
81 |
3523
|
82 std::string Vimagepath; |
2203
|
83 |
3523
|
84 std::string Vlocal_site_defaults_file; |
|
85 std::string Vsite_defaults_file; |
2203
|
86 |
3535
|
87 static std::string |
3523
|
88 subst_octave_home (const std::string& s) |
2203
|
89 { |
3523
|
90 std::string retval; |
2203
|
91 |
3523
|
92 std::string prefix = OCTAVE_PREFIX; |
2203
|
93 |
|
94 retval = s; |
|
95 |
|
96 if (Voctave_home != prefix) |
|
97 { |
|
98 int len = prefix.length (); |
|
99 size_t start = 0; |
2522
|
100 while ((start = retval.find (prefix, start)) != NPOS) |
2203
|
101 { |
|
102 retval.replace (start, len, Voctave_home); |
2522
|
103 start += len; |
2203
|
104 } |
|
105 } |
|
106 |
|
107 return retval; |
|
108 } |
|
109 |
|
110 static void |
|
111 set_octave_home (void) |
|
112 { |
3523
|
113 std::string oh = octave_env::getenv ("OCTAVE_HOME"); |
2203
|
114 |
3523
|
115 Voctave_home = oh.empty () ? std::string (OCTAVE_PREFIX) : oh; |
2203
|
116 } |
|
117 |
|
118 static void |
|
119 set_default_info_dir (void) |
|
120 { |
|
121 Vinfo_dir = subst_octave_home (OCTAVE_INFODIR); |
|
122 } |
|
123 |
|
124 static void |
3141
|
125 set_default_data_dir (void) |
|
126 { |
|
127 Vdata_dir = subst_octave_home (OCTAVE_DATADIR); |
|
128 } |
|
129 |
|
130 static void |
|
131 set_default_libexec_dir (void) |
|
132 { |
|
133 Vlibexec_dir = subst_octave_home (OCTAVE_LIBEXECDIR); |
|
134 } |
|
135 |
|
136 static void |
2203
|
137 set_default_arch_lib_dir (void) |
|
138 { |
|
139 Varch_lib_dir = subst_octave_home (OCTAVE_ARCHLIBDIR); |
|
140 } |
|
141 |
|
142 static void |
2439
|
143 set_default_local_arch_lib_dir (void) |
|
144 { |
|
145 Vlocal_arch_lib_dir = subst_octave_home (OCTAVE_LOCALARCHLIBDIR); |
|
146 } |
|
147 |
|
148 static void |
3597
|
149 set_default_local_ver_arch_lib_dir (void) |
|
150 { |
|
151 Vlocal_ver_arch_lib_dir = subst_octave_home (OCTAVE_LOCALVERARCHLIBDIR); |
|
152 } |
|
153 |
|
154 static void |
2203
|
155 set_default_fcn_file_dir (void) |
|
156 { |
|
157 Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR); |
|
158 } |
|
159 |
|
160 static void |
|
161 set_default_bin_dir (void) |
|
162 { |
|
163 Vbin_dir = subst_octave_home (OCTAVE_BINDIR); |
|
164 } |
|
165 |
|
166 static void |
|
167 set_default_exec_path (void) |
|
168 { |
3523
|
169 std::string octave_exec_path = octave_env::getenv ("OCTAVE_EXEC_PATH"); |
2203
|
170 |
2926
|
171 if (octave_exec_path.empty ()) |
2203
|
172 { |
3523
|
173 std::string shell_path = octave_env::getenv ("PATH"); |
2203
|
174 |
2926
|
175 if (! shell_path.empty ()) |
2203
|
176 { |
3523
|
177 Vexec_path = std::string (":"); |
2203
|
178 Vexec_path.append (shell_path); |
|
179 } |
|
180 } |
2926
|
181 else |
3523
|
182 Vexec_path = std::string (octave_exec_path); |
2203
|
183 } |
|
184 |
|
185 static void |
|
186 set_default_path (void) |
|
187 { |
3192
|
188 Vdefault_load_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
2203
|
189 |
3523
|
190 std::string oct_path = octave_env::getenv ("OCTAVE_PATH"); |
2203
|
191 |
3523
|
192 Vload_path = oct_path.empty () ? std::string (":") : oct_path; |
3174
|
193 |
3195
|
194 Vload_path_dir_path = dir_path (Vload_path, Vdefault_load_path); |
2203
|
195 } |
|
196 |
|
197 static void |
|
198 set_default_info_file (void) |
|
199 { |
3523
|
200 std::string std_info_file = subst_octave_home (OCTAVE_INFOFILE); |
2512
|
201 |
3523
|
202 std::string oct_info_file = octave_env::getenv ("OCTAVE_INFO_FILE"); |
2203
|
203 |
2926
|
204 Vinfo_file = oct_info_file.empty () ? std_info_file : oct_info_file; |
2203
|
205 } |
|
206 |
|
207 static void |
|
208 set_default_info_prog (void) |
|
209 { |
3523
|
210 std::string oct_info_prog = octave_env::getenv ("OCTAVE_INFO_PROGRAM"); |
2203
|
211 |
2926
|
212 if (oct_info_prog.empty ()) |
3141
|
213 Vinfo_prog = "info"; |
2926
|
214 else |
3523
|
215 Vinfo_prog = std::string (oct_info_prog); |
2203
|
216 } |
|
217 |
|
218 static void |
|
219 set_default_editor (void) |
|
220 { |
2993
|
221 Veditor = "emacs"; |
2203
|
222 |
3523
|
223 std::string env_editor = octave_env::getenv ("EDITOR"); |
2203
|
224 |
2926
|
225 if (! env_editor.empty ()) |
|
226 Veditor = env_editor; |
2203
|
227 } |
|
228 |
|
229 static void |
|
230 set_local_site_defaults_file (void) |
|
231 { |
|
232 Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR); |
|
233 Vlocal_site_defaults_file.append ("/octaverc"); |
|
234 } |
|
235 |
|
236 static void |
|
237 set_site_defaults_file (void) |
|
238 { |
|
239 Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR); |
|
240 Vsite_defaults_file.append ("/octaverc"); |
|
241 } |
|
242 |
3534
|
243 std::string |
3523
|
244 maybe_add_default_load_path (const std::string& pathstring) |
2203
|
245 { |
3523
|
246 std::string retval; |
2203
|
247 |
|
248 if (! pathstring.empty ()) |
|
249 { |
|
250 if (pathstring[0] == SEPCHAR) |
|
251 { |
3192
|
252 retval = Vdefault_load_path; |
2203
|
253 retval.append (pathstring); |
|
254 } |
|
255 else |
|
256 retval = pathstring; |
|
257 |
|
258 if (pathstring[pathstring.length () - 1] == SEPCHAR) |
3192
|
259 retval.append (Vdefault_load_path); |
|
260 |
|
261 size_t pos = 0; |
|
262 do |
|
263 { |
|
264 pos = retval.find ("::"); |
|
265 |
|
266 if (pos != NPOS) |
|
267 retval.insert (pos+1, Vdefault_load_path); |
|
268 } |
|
269 while (pos != NPOS); |
2203
|
270 } |
|
271 |
|
272 return retval; |
|
273 } |
|
274 |
|
275 void |
|
276 install_defaults (void) |
|
277 { |
|
278 // OCTAVE_HOME must be set first! |
|
279 |
|
280 set_octave_home (); |
|
281 |
|
282 set_default_info_dir (); |
|
283 |
3141
|
284 set_default_data_dir (); |
|
285 |
|
286 set_default_libexec_dir (); |
|
287 |
2203
|
288 set_default_arch_lib_dir (); |
|
289 |
2439
|
290 set_default_local_arch_lib_dir (); |
|
291 |
3597
|
292 set_default_local_ver_arch_lib_dir (); |
|
293 |
2203
|
294 set_default_fcn_file_dir (); |
|
295 |
|
296 set_default_bin_dir (); |
|
297 |
|
298 set_default_exec_path (); |
|
299 |
|
300 set_default_path (); |
|
301 |
|
302 set_default_info_file (); |
|
303 |
|
304 set_default_info_prog (); |
|
305 |
|
306 set_default_editor (); |
|
307 |
|
308 set_local_site_defaults_file (); |
|
309 |
|
310 set_site_defaults_file (); |
|
311 } |
|
312 |
|
313 static int |
|
314 editor (void) |
|
315 { |
|
316 int status = 0; |
|
317 |
3523
|
318 std::string s = builtin_string_variable ("EDITOR"); |
2203
|
319 |
|
320 if (s.empty ()) |
|
321 { |
|
322 gripe_invalid_value_specified ("EDITOR"); |
|
323 status = -1; |
|
324 } |
|
325 else |
|
326 Veditor = s; |
|
327 |
|
328 return status; |
|
329 } |
|
330 |
|
331 static int |
|
332 exec_path (void) |
|
333 { |
|
334 int status = 0; |
|
335 |
3523
|
336 std::string s = builtin_string_variable ("EXEC_PATH"); |
2439
|
337 |
|
338 if (s.empty ()) |
2203
|
339 { |
|
340 gripe_invalid_value_specified ("EXEC_PATH"); |
|
341 status = -1; |
|
342 } |
|
343 else |
|
344 { |
2439
|
345 Vexec_path = s; |
|
346 |
3597
|
347 std::string std_path |
|
348 = Vlocal_ver_arch_lib_dir + std::string (SEPCHAR_STR) |
|
349 + Vlocal_arch_lib_dir + std::string (SEPCHAR_STR) |
|
350 + Varch_lib_dir + std::string (SEPCHAR_STR) |
|
351 + Vbin_dir; |
2439
|
352 |
3523
|
353 std::string path; |
2203
|
354 |
|
355 int eplen = Vexec_path.length (); |
|
356 |
|
357 if (eplen > 0) |
|
358 { |
2926
|
359 bool prepend = (Vexec_path[0] == ':'); |
|
360 bool append = (eplen > 1 && Vexec_path[eplen-1] == ':'); |
2203
|
361 |
|
362 if (prepend) |
|
363 { |
2926
|
364 path = std_path + Vexec_path; |
|
365 |
2203
|
366 if (append) |
2926
|
367 path.append (std_path); |
2203
|
368 } |
|
369 else |
|
370 { |
2926
|
371 path = Vexec_path; |
|
372 |
2203
|
373 if (append) |
2926
|
374 path.append (std_path); |
2203
|
375 } |
|
376 } |
|
377 else |
2926
|
378 path = std_path; |
2203
|
379 |
2926
|
380 octave_env::putenv ("PATH", path); |
2203
|
381 } |
|
382 |
|
383 return status; |
|
384 } |
|
385 |
|
386 static int |
|
387 imagepath (void) |
|
388 { |
|
389 int status = 0; |
|
390 |
3523
|
391 std::string s = builtin_string_variable ("IMAGEPATH"); |
2203
|
392 |
|
393 if (s.empty ()) |
|
394 { |
|
395 gripe_invalid_value_specified ("IMAGEPATH"); |
|
396 status = -1; |
|
397 } |
|
398 else |
|
399 Vimagepath = s; |
|
400 |
|
401 return status; |
|
402 } |
|
403 |
|
404 static int |
|
405 loadpath (void) |
|
406 { |
|
407 int status = 0; |
|
408 |
3523
|
409 std::string s = builtin_string_variable ("LOADPATH"); |
2203
|
410 |
|
411 if (s.empty ()) |
|
412 { |
|
413 gripe_invalid_value_specified ("LOADPATH"); |
|
414 status = -1; |
|
415 } |
|
416 else |
3174
|
417 { |
3195
|
418 Vload_path = s; |
3174
|
419 |
3195
|
420 Vload_path_dir_path = dir_path (Vload_path, Vdefault_load_path); |
3174
|
421 } |
2203
|
422 |
|
423 return status; |
|
424 } |
|
425 |
|
426 void |
|
427 symbols_of_defaults (void) |
|
428 { |
3258
|
429 DEFVAR (EDITOR, Veditor, editor, |
3368
|
430 "-*- texinfo -*-\n\ |
|
431 @defvr {Built-in Variable} EDITOR\n\ |
|
432 A string naming the editor to use with the @code{edit_history} command.\n\ |
|
433 If the environment variable @code{EDITOR} is set when Octave starts, its\n\ |
|
434 value is used as the default. Otherwise, @code{EDITOR} is set to\n\ |
|
435 @code{\"emacs\"}.\n\ |
|
436 @end defvr"); |
2203
|
437 |
3258
|
438 DEFVAR (EXEC_PATH, Vexec_path, exec_path, |
3301
|
439 "-*- texinfo -*-\n\ |
|
440 @defvr {Built-in Variable} EXEC_PATH\n\ |
|
441 The variable @code{EXEC_PATH} is a colon separated list of directories\n\ |
|
442 to search when executing subprograms. Its initial value is taken from\n\ |
|
443 the environment variable @code{OCTAVE_EXEC_PATH} (if it exists) or\n\ |
|
444 @code{PATH}, but that value can be overridden by the command line\n\ |
|
445 argument @code{--exec-path PATH}, or by setting the value of\n\ |
|
446 @code{EXEC_PATH} in a startup script. If the value of @code{EXEC_PATH}\n\ |
|
447 begins (ends) with a colon, the directories\n\ |
|
448 \n\ |
|
449 @example\n\ |
|
450 @group\n\ |
|
451 @var{octave-home}/libexec/octave/site/exec/@var{arch}\n\ |
|
452 @var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}\n\ |
|
453 @end group\n\ |
|
454 @end example\n\ |
|
455 \n\ |
|
456 @noindent\n\ |
|
457 are prepended (appended) to @code{EXEC_PATH}, where @var{octave-home}\n\ |
|
458 is the top-level directory where all of Octave is installed\n\ |
|
459 (the default value is @file{@value{OCTAVEHOME}}). If you don't specify\n\ |
|
460 a value for @code{EXEC_PATH} explicitly, these special directories are\n\ |
|
461 prepended to your shell path.\n\ |
|
462 @end defvr"); |
2203
|
463 |
3258
|
464 DEFVAR (LOADPATH, Vload_path, loadpath, |
3371
|
465 "-*- texinfo -*-\n\ |
|
466 @defvr {Built-in Variable} LOADPATH\n\ |
|
467 A colon separated list of directories in which to search for function\n\ |
|
468 files. @xref{Functions and Scripts}. The value of @code{LOADPATH}\n\ |
|
469 overrides the environment variable @code{OCTAVE_PATH}. @xref{Installation}.\n\ |
|
470 \n\ |
|
471 @code{LOADPATH} is now handled in the same way as @TeX{} handles\n\ |
|
472 @code{TEXINPUTS}. Leading, trailing, or doubled colons that appear in\n\ |
|
473 @code{LOADPATH} are replaced by the value of @code{DEFAULT_LOADPATH}.\n\ |
|
474 The default value of @code{LOADPATH} is @code{\":\"}, which tells Octave\n\ |
|
475 to search in the directories specified by @code{DEFAULT_LOADPATH}.\n\ |
|
476 \n\ |
|
477 In addition, if any path element ends in @samp{//}, that directory and\n\ |
|
478 all subdirectories it contains are searched recursively for function\n\ |
|
479 files. This can result in a slight delay as Octave caches the lists of\n\ |
|
480 files found in the @code{LOADPATH} the first time Octave searches for a\n\ |
|
481 function. After that, searching is usually much faster because Octave\n\ |
|
482 normally only needs to search its internal cache for files.\n\ |
|
483 \n\ |
|
484 To improve performance of recursive directory searching, it is best for\n\ |
|
485 each directory that is to be searched recursively to contain\n\ |
|
486 @emph{either} additional subdirectories @emph{or} function files, but\n\ |
|
487 not a mixture of both.\n\ |
|
488 \n\ |
3402
|
489 @xref{Organization of Functions}, for a description of the function file\n\ |
3371
|
490 directories that are distributed with Octave.\n\ |
|
491 @end defvr"); |
2203
|
492 |
3192
|
493 DEFCONST (DEFAULT_LOADPATH, Vdefault_load_path, |
3371
|
494 "-*- texinfo -*-\n\ |
|
495 @defvr {Built-in Variable} DEFAULT_LOADPATH\n\ |
|
496 A colon separated list of directories in which to search for function\n\ |
|
497 files by default. The value of this variable is also automatically\n\ |
|
498 substituted for leading, trailing, or doubled colons that appear in the\n\ |
|
499 built-in variable @code{LOADPATH}.\n\ |
|
500 @end defvr"); |
3192
|
501 |
3258
|
502 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, imagepath, |
3373
|
503 "-*- texinfo -*-\n\ |
3532
|
504 @defvr {Built-in Variable} IMAGEPATH\n\ |
3373
|
505 A colon separated list of directories in which to search for image\n\ |
|
506 files.\n\ |
|
507 @end defvr"); |
2203
|
508 |
3141
|
509 DEFCONST (OCTAVE_HOME, Voctave_home, |
3446
|
510 "-*- texinfo -*-\n\ |
|
511 @defvr {Built-in Variable} OCTAVE_HOME\n\ |
|
512 The name of the top-level Octave installation directory.\n\ |
|
513 @end defvr"); |
2831
|
514 |
3141
|
515 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, |
3301
|
516 "-*- texinfo -*-\n\ |
3373
|
517 @defvr {Built-in Variable} OCTAVE_VERSION\n\ |
3301
|
518 The version number of Octave, as a string.\n\ |
|
519 @end defvr"); |
3446
|
520 |
2203
|
521 } |
|
522 |
3185
|
523 DEFUN (rehash, , , |
3446
|
524 "-*- texinfo -*-\n\ |
|
525 @deftypefn {Built-in Function} {} rehash ()\n\ |
|
526 Reinitialize Octave's @code{LOADPATH} directory cache.\n\ |
|
527 @end deftypefn") |
3185
|
528 { |
|
529 octave_value_list retval; |
|
530 |
|
531 Vload_path_dir_path.rehash (); |
|
532 |
|
533 return retval; |
|
534 } |
|
535 |
2203
|
536 /* |
|
537 ;;; Local Variables: *** |
|
538 ;;; mode: C++ *** |
|
539 ;;; End: *** |
|
540 */ |