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 |
2439
|
27 #include <iostream.h> |
|
28 |
2203
|
29 #include <cstdlib> |
|
30 |
|
31 #include <string> |
|
32 |
|
33 #ifdef HAVE_UNISTD_H |
2442
|
34 #ifdef HAVE_SYS_TYPES_H |
2203
|
35 #include <sys/types.h> |
2442
|
36 #endif |
2203
|
37 #include <unistd.h> |
|
38 #endif |
|
39 |
2926
|
40 #include "oct-env.h" |
3174
|
41 #include "pathsearch.h" |
2926
|
42 |
2492
|
43 #include <defaults.h> |
2203
|
44 #include "defun.h" |
|
45 #include "error.h" |
3040
|
46 #include "file-ops.h" |
2203
|
47 #include "gripes.h" |
|
48 #include "help.h" |
3185
|
49 #include "oct-obj.h" |
2390
|
50 #include "ov.h" |
2203
|
51 #include "toplev.h" |
|
52 #include "variables.h" |
2492
|
53 #include <version.h> |
2203
|
54 |
|
55 string Voctave_home; |
|
56 |
|
57 string Vbin_dir; |
|
58 string Vinfo_dir; |
3141
|
59 string Vdata_dir; |
|
60 string Vlibexec_dir; |
2203
|
61 string Varch_lib_dir; |
2439
|
62 string Vlocal_arch_lib_dir; |
2203
|
63 string Vfcn_file_dir; |
|
64 |
|
65 // The path that will be searched for programs that we execute. |
|
66 // (--exec-path path) |
|
67 string Vexec_path; |
|
68 |
|
69 // Load path specified on command line. |
|
70 // (--path path; -p path) |
3195
|
71 static string Vload_path; |
2203
|
72 |
3192
|
73 // The default load path with OCTAVE_HOME appropriately substituted. |
|
74 static string Vdefault_load_path; |
|
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. |
|
80 string Veditor; |
|
81 |
|
82 string Vimagepath; |
|
83 |
|
84 string Vlocal_site_defaults_file; |
|
85 string Vsite_defaults_file; |
|
86 |
|
87 static string |
|
88 subst_octave_home (const string& s) |
|
89 { |
|
90 string retval; |
|
91 |
|
92 string prefix = OCTAVE_PREFIX; |
|
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 { |
2926
|
113 string oh = octave_env::getenv ("OCTAVE_HOME"); |
2203
|
114 |
2926
|
115 Voctave_home = oh.empty () ? 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 |
2203
|
149 set_default_fcn_file_dir (void) |
|
150 { |
|
151 Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR); |
|
152 } |
|
153 |
|
154 static void |
|
155 set_default_bin_dir (void) |
|
156 { |
|
157 Vbin_dir = subst_octave_home (OCTAVE_BINDIR); |
|
158 } |
|
159 |
|
160 static void |
|
161 set_default_exec_path (void) |
|
162 { |
2926
|
163 string octave_exec_path = octave_env::getenv ("OCTAVE_EXEC_PATH"); |
2203
|
164 |
2926
|
165 if (octave_exec_path.empty ()) |
2203
|
166 { |
2926
|
167 string shell_path = octave_env::getenv ("PATH"); |
2203
|
168 |
2926
|
169 if (! shell_path.empty ()) |
2203
|
170 { |
|
171 Vexec_path = string (":"); |
|
172 Vexec_path.append (shell_path); |
|
173 } |
|
174 } |
2926
|
175 else |
|
176 Vexec_path = string (octave_exec_path); |
2203
|
177 } |
|
178 |
|
179 static void |
|
180 set_default_path (void) |
|
181 { |
3192
|
182 Vdefault_load_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
2203
|
183 |
2926
|
184 string oct_path = octave_env::getenv ("OCTAVE_PATH"); |
2203
|
185 |
3195
|
186 Vload_path = oct_path.empty () ? string (":") : oct_path; |
3174
|
187 |
3195
|
188 Vload_path_dir_path = dir_path (Vload_path, Vdefault_load_path); |
2203
|
189 } |
|
190 |
|
191 static void |
|
192 set_default_info_file (void) |
|
193 { |
2512
|
194 string std_info_file = subst_octave_home (OCTAVE_INFOFILE); |
|
195 |
2926
|
196 string oct_info_file = octave_env::getenv ("OCTAVE_INFO_FILE"); |
2203
|
197 |
2926
|
198 Vinfo_file = oct_info_file.empty () ? std_info_file : oct_info_file; |
2203
|
199 } |
|
200 |
|
201 static void |
|
202 set_default_info_prog (void) |
|
203 { |
2926
|
204 string oct_info_prog = octave_env::getenv ("OCTAVE_INFO_PROGRAM"); |
2203
|
205 |
2926
|
206 if (oct_info_prog.empty ()) |
3141
|
207 Vinfo_prog = "info"; |
2926
|
208 else |
|
209 Vinfo_prog = string (oct_info_prog); |
2203
|
210 } |
|
211 |
|
212 static void |
|
213 set_default_editor (void) |
|
214 { |
2993
|
215 Veditor = "emacs"; |
2203
|
216 |
2926
|
217 string env_editor = octave_env::getenv ("EDITOR"); |
2203
|
218 |
2926
|
219 if (! env_editor.empty ()) |
|
220 Veditor = env_editor; |
2203
|
221 } |
|
222 |
|
223 static void |
|
224 set_local_site_defaults_file (void) |
|
225 { |
|
226 Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR); |
|
227 Vlocal_site_defaults_file.append ("/octaverc"); |
|
228 } |
|
229 |
|
230 static void |
|
231 set_site_defaults_file (void) |
|
232 { |
|
233 Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR); |
|
234 Vsite_defaults_file.append ("/octaverc"); |
|
235 } |
|
236 |
|
237 string |
|
238 maybe_add_default_load_path (const string& pathstring) |
|
239 { |
|
240 string retval; |
|
241 |
|
242 if (! pathstring.empty ()) |
|
243 { |
|
244 if (pathstring[0] == SEPCHAR) |
|
245 { |
3192
|
246 retval = Vdefault_load_path; |
2203
|
247 retval.append (pathstring); |
|
248 } |
|
249 else |
|
250 retval = pathstring; |
|
251 |
|
252 if (pathstring[pathstring.length () - 1] == SEPCHAR) |
3192
|
253 retval.append (Vdefault_load_path); |
|
254 |
|
255 size_t pos = 0; |
|
256 do |
|
257 { |
|
258 pos = retval.find ("::"); |
|
259 |
|
260 if (pos != NPOS) |
|
261 retval.insert (pos+1, Vdefault_load_path); |
|
262 } |
|
263 while (pos != NPOS); |
2203
|
264 } |
|
265 |
|
266 return retval; |
|
267 } |
|
268 |
|
269 void |
|
270 install_defaults (void) |
|
271 { |
|
272 // OCTAVE_HOME must be set first! |
|
273 |
|
274 set_octave_home (); |
|
275 |
|
276 set_default_info_dir (); |
|
277 |
3141
|
278 set_default_data_dir (); |
|
279 |
|
280 set_default_libexec_dir (); |
|
281 |
2203
|
282 set_default_arch_lib_dir (); |
|
283 |
2439
|
284 set_default_local_arch_lib_dir (); |
|
285 |
2203
|
286 set_default_fcn_file_dir (); |
|
287 |
|
288 set_default_bin_dir (); |
|
289 |
|
290 set_default_exec_path (); |
|
291 |
|
292 set_default_path (); |
|
293 |
|
294 set_default_info_file (); |
|
295 |
|
296 set_default_info_prog (); |
|
297 |
|
298 set_default_editor (); |
|
299 |
|
300 set_local_site_defaults_file (); |
|
301 |
|
302 set_site_defaults_file (); |
|
303 } |
|
304 |
|
305 static int |
|
306 editor (void) |
|
307 { |
|
308 int status = 0; |
|
309 |
|
310 string s = builtin_string_variable ("EDITOR"); |
|
311 |
|
312 if (s.empty ()) |
|
313 { |
|
314 gripe_invalid_value_specified ("EDITOR"); |
|
315 status = -1; |
|
316 } |
|
317 else |
|
318 Veditor = s; |
|
319 |
|
320 return status; |
|
321 } |
|
322 |
|
323 static int |
|
324 exec_path (void) |
|
325 { |
|
326 int status = 0; |
|
327 |
2439
|
328 string s = builtin_string_variable ("EXEC_PATH"); |
|
329 |
|
330 if (s.empty ()) |
2203
|
331 { |
|
332 gripe_invalid_value_specified ("EXEC_PATH"); |
|
333 status = -1; |
|
334 } |
|
335 else |
|
336 { |
2439
|
337 Vexec_path = s; |
|
338 |
3141
|
339 string std_path = Vlocal_arch_lib_dir + string (SEPCHAR_STR) |
|
340 + Varch_lib_dir + string (SEPCHAR_STR) + Vbin_dir; |
2439
|
341 |
2926
|
342 string path; |
2203
|
343 |
|
344 int eplen = Vexec_path.length (); |
|
345 |
|
346 if (eplen > 0) |
|
347 { |
2926
|
348 bool prepend = (Vexec_path[0] == ':'); |
|
349 bool append = (eplen > 1 && Vexec_path[eplen-1] == ':'); |
2203
|
350 |
|
351 if (prepend) |
|
352 { |
2926
|
353 path = std_path + Vexec_path; |
|
354 |
2203
|
355 if (append) |
2926
|
356 path.append (std_path); |
2203
|
357 } |
|
358 else |
|
359 { |
2926
|
360 path = Vexec_path; |
|
361 |
2203
|
362 if (append) |
2926
|
363 path.append (std_path); |
2203
|
364 } |
|
365 } |
|
366 else |
2926
|
367 path = std_path; |
2203
|
368 |
2926
|
369 octave_env::putenv ("PATH", path); |
2203
|
370 } |
|
371 |
|
372 return status; |
|
373 } |
|
374 |
|
375 static int |
|
376 imagepath (void) |
|
377 { |
|
378 int status = 0; |
|
379 |
|
380 string s = builtin_string_variable ("IMAGEPATH"); |
|
381 |
|
382 if (s.empty ()) |
|
383 { |
|
384 gripe_invalid_value_specified ("IMAGEPATH"); |
|
385 status = -1; |
|
386 } |
|
387 else |
|
388 Vimagepath = s; |
|
389 |
|
390 return status; |
|
391 } |
|
392 |
|
393 static int |
|
394 loadpath (void) |
|
395 { |
|
396 int status = 0; |
|
397 |
|
398 string s = builtin_string_variable ("LOADPATH"); |
|
399 |
|
400 if (s.empty ()) |
|
401 { |
|
402 gripe_invalid_value_specified ("LOADPATH"); |
|
403 status = -1; |
|
404 } |
|
405 else |
3174
|
406 { |
3195
|
407 Vload_path = s; |
3174
|
408 |
3195
|
409 Vload_path_dir_path = dir_path (Vload_path, Vdefault_load_path); |
3174
|
410 } |
2203
|
411 |
|
412 return status; |
|
413 } |
|
414 |
|
415 void |
|
416 symbols_of_defaults (void) |
|
417 { |
3258
|
418 DEFVAR (EDITOR, Veditor, editor, |
2203
|
419 "name of the editor to be invoked by the edit_history command"); |
|
420 |
3258
|
421 DEFVAR (EXEC_PATH, Vexec_path, exec_path, |
3301
|
422 "-*- texinfo -*-\n\ |
|
423 @defvr {Built-in Variable} EXEC_PATH\n\ |
|
424 The variable @code{EXEC_PATH} is a colon separated list of directories\n\ |
|
425 to search when executing subprograms. Its initial value is taken from\n\ |
|
426 the environment variable @code{OCTAVE_EXEC_PATH} (if it exists) or\n\ |
|
427 @code{PATH}, but that value can be overridden by the command line\n\ |
|
428 argument @code{--exec-path PATH}, or by setting the value of\n\ |
|
429 @code{EXEC_PATH} in a startup script. If the value of @code{EXEC_PATH}\n\ |
|
430 begins (ends) with a colon, the directories\n\ |
|
431 \n\ |
|
432 @example\n\ |
|
433 @group\n\ |
|
434 @var{octave-home}/libexec/octave/site/exec/@var{arch}\n\ |
|
435 @var{octave-home}/libexec/octave/@var{version}/exec/@var{arch}\n\ |
|
436 @end group\n\ |
|
437 @end example\n\ |
|
438 \n\ |
|
439 @noindent\n\ |
|
440 are prepended (appended) to @code{EXEC_PATH}, where @var{octave-home}\n\ |
|
441 is the top-level directory where all of Octave is installed\n\ |
|
442 (the default value is @file{@value{OCTAVEHOME}}). If you don't specify\n\ |
|
443 a value for @code{EXEC_PATH} explicitly, these special directories are\n\ |
|
444 prepended to your shell path.\n\ |
|
445 @end defvr"); |
2203
|
446 |
3258
|
447 DEFVAR (LOADPATH, Vload_path, loadpath, |
3192
|
448 "colon separated list of directories to search for scripts.\n\ |
|
449 The default value is \":\", which means to search the default list\n\ |
|
450 of directories. The default list of directories may be found in\n\ |
|
451 the built-in constant DEFAULT_LOADPATH"); |
2203
|
452 |
3192
|
453 DEFCONST (DEFAULT_LOADPATH, Vdefault_load_path, |
|
454 "the default colon separated list of directories to search for scripts"); |
|
455 |
3258
|
456 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, imagepath, |
2203
|
457 "colon separated list of directories to search for image files"); |
|
458 |
3141
|
459 DEFCONST (OCTAVE_HOME, Voctave_home, |
2831
|
460 "top-level Octave installation directory"); |
|
461 |
3141
|
462 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, |
3301
|
463 "-*- texinfo -*-\n\ |
|
464 @defvr\n\ |
|
465 The version number of Octave, as a string.\n\ |
|
466 @end defvr"); |
2203
|
467 } |
|
468 |
3185
|
469 DEFUN (rehash, , , |
|
470 "rehash (): reinitialize LOADPATH directory cache") |
|
471 { |
|
472 octave_value_list retval; |
|
473 |
|
474 Vload_path_dir_path.rehash (); |
|
475 |
|
476 return retval; |
|
477 } |
|
478 |
2203
|
479 /* |
|
480 ;;; Local Variables: *** |
|
481 ;;; mode: C++ *** |
|
482 ;;; End: *** |
|
483 */ |