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