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) |
|
71 string Vload_path; |
|
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 // Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
180 // If the path starts with `:', prepend the standard path. If it ends |
|
181 // with `:' append the standard path. If it begins and ends with |
|
182 // `:', do both (which is useless, but the luser asked for it...). |
|
183 |
|
184 static void |
|
185 set_default_path (void) |
|
186 { |
3192
|
187 Vdefault_load_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
2203
|
188 |
2926
|
189 string oct_path = octave_env::getenv ("OCTAVE_PATH"); |
2203
|
190 |
3192
|
191 Vload_path = oct_path.empty () |
|
192 ? Vdefault_load_path : maybe_add_default_path (oct_path); |
3174
|
193 |
|
194 Vload_path_dir_path = dir_path (Vload_path); |
2203
|
195 } |
|
196 |
|
197 static void |
|
198 set_default_info_file (void) |
|
199 { |
2512
|
200 string std_info_file = subst_octave_home (OCTAVE_INFOFILE); |
|
201 |
2926
|
202 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 { |
2926
|
210 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 |
|
215 Vinfo_prog = string (oct_info_prog); |
2203
|
216 } |
|
217 |
|
218 static void |
|
219 set_default_editor (void) |
|
220 { |
2993
|
221 Veditor = "emacs"; |
2203
|
222 |
2926
|
223 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 |
|
243 string |
|
244 maybe_add_default_load_path (const string& pathstring) |
|
245 { |
|
246 string retval; |
|
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 |
2203
|
292 set_default_fcn_file_dir (); |
|
293 |
|
294 set_default_bin_dir (); |
|
295 |
|
296 set_default_exec_path (); |
|
297 |
|
298 set_default_path (); |
|
299 |
|
300 set_default_info_file (); |
|
301 |
|
302 set_default_info_prog (); |
|
303 |
|
304 set_default_editor (); |
|
305 |
|
306 set_local_site_defaults_file (); |
|
307 |
|
308 set_site_defaults_file (); |
|
309 } |
|
310 |
|
311 static int |
|
312 editor (void) |
|
313 { |
|
314 int status = 0; |
|
315 |
|
316 string s = builtin_string_variable ("EDITOR"); |
|
317 |
|
318 if (s.empty ()) |
|
319 { |
|
320 gripe_invalid_value_specified ("EDITOR"); |
|
321 status = -1; |
|
322 } |
|
323 else |
|
324 Veditor = s; |
|
325 |
|
326 return status; |
|
327 } |
|
328 |
|
329 static int |
|
330 exec_path (void) |
|
331 { |
|
332 int status = 0; |
|
333 |
2439
|
334 string s = builtin_string_variable ("EXEC_PATH"); |
|
335 |
|
336 if (s.empty ()) |
2203
|
337 { |
|
338 gripe_invalid_value_specified ("EXEC_PATH"); |
|
339 status = -1; |
|
340 } |
|
341 else |
|
342 { |
2439
|
343 Vexec_path = s; |
|
344 |
3141
|
345 string std_path = Vlocal_arch_lib_dir + string (SEPCHAR_STR) |
|
346 + Varch_lib_dir + string (SEPCHAR_STR) + Vbin_dir; |
2439
|
347 |
2926
|
348 string path; |
2203
|
349 |
|
350 int eplen = Vexec_path.length (); |
|
351 |
|
352 if (eplen > 0) |
|
353 { |
2926
|
354 bool prepend = (Vexec_path[0] == ':'); |
|
355 bool append = (eplen > 1 && Vexec_path[eplen-1] == ':'); |
2203
|
356 |
|
357 if (prepend) |
|
358 { |
2926
|
359 path = std_path + Vexec_path; |
|
360 |
2203
|
361 if (append) |
2926
|
362 path.append (std_path); |
2203
|
363 } |
|
364 else |
|
365 { |
2926
|
366 path = Vexec_path; |
|
367 |
2203
|
368 if (append) |
2926
|
369 path.append (std_path); |
2203
|
370 } |
|
371 } |
|
372 else |
2926
|
373 path = std_path; |
2203
|
374 |
2926
|
375 octave_env::putenv ("PATH", path); |
2203
|
376 } |
|
377 |
|
378 return status; |
|
379 } |
|
380 |
|
381 static int |
|
382 imagepath (void) |
|
383 { |
|
384 int status = 0; |
|
385 |
|
386 string s = builtin_string_variable ("IMAGEPATH"); |
|
387 |
|
388 if (s.empty ()) |
|
389 { |
|
390 gripe_invalid_value_specified ("IMAGEPATH"); |
|
391 status = -1; |
|
392 } |
|
393 else |
|
394 Vimagepath = s; |
|
395 |
|
396 return status; |
|
397 } |
|
398 |
|
399 static int |
|
400 loadpath (void) |
|
401 { |
|
402 int status = 0; |
|
403 |
|
404 string s = builtin_string_variable ("LOADPATH"); |
|
405 |
|
406 if (s.empty ()) |
|
407 { |
|
408 gripe_invalid_value_specified ("LOADPATH"); |
|
409 status = -1; |
|
410 } |
|
411 else |
3174
|
412 { |
|
413 Vload_path = maybe_add_default_load_path (s); |
|
414 |
|
415 Vload_path_dir_path = dir_path (Vload_path); |
|
416 } |
2203
|
417 |
|
418 return status; |
|
419 } |
|
420 |
|
421 void |
|
422 symbols_of_defaults (void) |
|
423 { |
|
424 DEFVAR (EDITOR, Veditor, 0, editor, |
|
425 "name of the editor to be invoked by the edit_history command"); |
|
426 |
|
427 DEFVAR (EXEC_PATH, Vexec_path, 0, exec_path, |
|
428 "colon separated list of directories to search for programs to run"); |
|
429 |
3192
|
430 DEFVAR (LOADPATH, ":", 0, octave_loadpath, |
|
431 "colon separated list of directories to search for scripts.\n\ |
|
432 The default value is \":\", which means to search the default list\n\ |
|
433 of directories. The default list of directories may be found in\n\ |
|
434 the built-in constant DEFAULT_LOADPATH"); |
2203
|
435 |
3192
|
436 DEFCONST (DEFAULT_LOADPATH, Vdefault_load_path, |
|
437 "the default colon separated list of directories to search for scripts"); |
|
438 |
2203
|
439 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, 0, imagepath, |
|
440 "colon separated list of directories to search for image files"); |
|
441 |
3141
|
442 DEFCONST (OCTAVE_HOME, Voctave_home, |
2831
|
443 "top-level Octave installation directory"); |
|
444 |
3141
|
445 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, |
2203
|
446 "Octave version"); |
|
447 } |
|
448 |
3185
|
449 DEFUN (rehash, , , |
|
450 "rehash (): reinitialize LOADPATH directory cache") |
|
451 { |
|
452 octave_value_list retval; |
|
453 |
|
454 Vload_path_dir_path.rehash (); |
|
455 |
|
456 return retval; |
|
457 } |
|
458 |
2203
|
459 /* |
|
460 ;;; Local Variables: *** |
|
461 ;;; mode: C++ *** |
|
462 ;;; End: *** |
|
463 */ |