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 |
3174
|
73 // And the cached directory path corresponding to Vload_path. |
|
74 dir_path Vload_path_dir_path; |
|
75 |
2203
|
76 // Name of the editor to be invoked by the edit_history command. |
|
77 string Veditor; |
|
78 |
|
79 string Vimagepath; |
|
80 |
|
81 string Vlocal_site_defaults_file; |
|
82 string Vsite_defaults_file; |
|
83 |
|
84 static string |
|
85 subst_octave_home (const string& s) |
|
86 { |
|
87 string retval; |
|
88 |
|
89 string prefix = OCTAVE_PREFIX; |
|
90 |
|
91 retval = s; |
|
92 |
|
93 if (Voctave_home != prefix) |
|
94 { |
|
95 int len = prefix.length (); |
|
96 size_t start = 0; |
2522
|
97 while ((start = retval.find (prefix, start)) != NPOS) |
2203
|
98 { |
|
99 retval.replace (start, len, Voctave_home); |
2522
|
100 start += len; |
2203
|
101 } |
|
102 } |
|
103 |
|
104 return retval; |
|
105 } |
|
106 |
|
107 static void |
|
108 set_octave_home (void) |
|
109 { |
2926
|
110 string oh = octave_env::getenv ("OCTAVE_HOME"); |
2203
|
111 |
2926
|
112 Voctave_home = oh.empty () ? string (OCTAVE_PREFIX) : oh; |
2203
|
113 } |
|
114 |
|
115 static void |
|
116 set_default_info_dir (void) |
|
117 { |
|
118 Vinfo_dir = subst_octave_home (OCTAVE_INFODIR); |
|
119 } |
|
120 |
|
121 static void |
3141
|
122 set_default_data_dir (void) |
|
123 { |
|
124 Vdata_dir = subst_octave_home (OCTAVE_DATADIR); |
|
125 } |
|
126 |
|
127 static void |
|
128 set_default_libexec_dir (void) |
|
129 { |
|
130 Vlibexec_dir = subst_octave_home (OCTAVE_LIBEXECDIR); |
|
131 } |
|
132 |
|
133 static void |
2203
|
134 set_default_arch_lib_dir (void) |
|
135 { |
|
136 Varch_lib_dir = subst_octave_home (OCTAVE_ARCHLIBDIR); |
|
137 } |
|
138 |
|
139 static void |
2439
|
140 set_default_local_arch_lib_dir (void) |
|
141 { |
|
142 Vlocal_arch_lib_dir = subst_octave_home (OCTAVE_LOCALARCHLIBDIR); |
|
143 } |
|
144 |
|
145 static void |
2203
|
146 set_default_fcn_file_dir (void) |
|
147 { |
|
148 Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR); |
|
149 } |
|
150 |
|
151 static void |
|
152 set_default_bin_dir (void) |
|
153 { |
|
154 Vbin_dir = subst_octave_home (OCTAVE_BINDIR); |
|
155 } |
|
156 |
|
157 static void |
|
158 set_default_exec_path (void) |
|
159 { |
2926
|
160 string octave_exec_path = octave_env::getenv ("OCTAVE_EXEC_PATH"); |
2203
|
161 |
2926
|
162 if (octave_exec_path.empty ()) |
2203
|
163 { |
2926
|
164 string shell_path = octave_env::getenv ("PATH"); |
2203
|
165 |
2926
|
166 if (! shell_path.empty ()) |
2203
|
167 { |
|
168 Vexec_path = string (":"); |
|
169 Vexec_path.append (shell_path); |
|
170 } |
|
171 } |
2926
|
172 else |
|
173 Vexec_path = string (octave_exec_path); |
2203
|
174 } |
|
175 |
|
176 // Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
177 // If the path starts with `:', prepend the standard path. If it ends |
|
178 // with `:' append the standard path. If it begins and ends with |
|
179 // `:', do both (which is useless, but the luser asked for it...). |
|
180 |
|
181 static void |
|
182 set_default_path (void) |
|
183 { |
|
184 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
|
185 |
2926
|
186 string oct_path = octave_env::getenv ("OCTAVE_PATH"); |
2203
|
187 |
2926
|
188 Vload_path = oct_path.empty () ? std_path : oct_path; |
3174
|
189 |
|
190 Vload_path_dir_path = dir_path (Vload_path); |
2203
|
191 } |
|
192 |
|
193 static void |
|
194 set_default_info_file (void) |
|
195 { |
2512
|
196 string std_info_file = subst_octave_home (OCTAVE_INFOFILE); |
|
197 |
2926
|
198 string oct_info_file = octave_env::getenv ("OCTAVE_INFO_FILE"); |
2203
|
199 |
2926
|
200 Vinfo_file = oct_info_file.empty () ? std_info_file : oct_info_file; |
2203
|
201 } |
|
202 |
|
203 static void |
|
204 set_default_info_prog (void) |
|
205 { |
2926
|
206 string oct_info_prog = octave_env::getenv ("OCTAVE_INFO_PROGRAM"); |
2203
|
207 |
2926
|
208 if (oct_info_prog.empty ()) |
3141
|
209 Vinfo_prog = "info"; |
2926
|
210 else |
|
211 Vinfo_prog = string (oct_info_prog); |
2203
|
212 } |
|
213 |
|
214 static void |
|
215 set_default_editor (void) |
|
216 { |
2993
|
217 Veditor = "emacs"; |
2203
|
218 |
2926
|
219 string env_editor = octave_env::getenv ("EDITOR"); |
2203
|
220 |
2926
|
221 if (! env_editor.empty ()) |
|
222 Veditor = env_editor; |
2203
|
223 } |
|
224 |
|
225 static void |
|
226 set_local_site_defaults_file (void) |
|
227 { |
|
228 Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR); |
|
229 Vlocal_site_defaults_file.append ("/octaverc"); |
|
230 } |
|
231 |
|
232 static void |
|
233 set_site_defaults_file (void) |
|
234 { |
|
235 Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR); |
|
236 Vsite_defaults_file.append ("/octaverc"); |
|
237 } |
|
238 |
|
239 string |
|
240 maybe_add_default_load_path (const string& pathstring) |
|
241 { |
|
242 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
|
243 |
|
244 string retval; |
|
245 |
|
246 if (! pathstring.empty ()) |
|
247 { |
|
248 if (pathstring[0] == SEPCHAR) |
|
249 { |
|
250 retval = std_path; |
|
251 retval.append (pathstring); |
|
252 } |
|
253 else |
|
254 retval = pathstring; |
|
255 |
|
256 if (pathstring[pathstring.length () - 1] == SEPCHAR) |
|
257 retval.append (std_path); |
|
258 } |
|
259 |
|
260 return retval; |
|
261 } |
|
262 |
|
263 void |
|
264 install_defaults (void) |
|
265 { |
|
266 // OCTAVE_HOME must be set first! |
|
267 |
|
268 set_octave_home (); |
|
269 |
|
270 set_default_info_dir (); |
|
271 |
3141
|
272 set_default_data_dir (); |
|
273 |
|
274 set_default_libexec_dir (); |
|
275 |
2203
|
276 set_default_arch_lib_dir (); |
|
277 |
2439
|
278 set_default_local_arch_lib_dir (); |
|
279 |
2203
|
280 set_default_fcn_file_dir (); |
|
281 |
|
282 set_default_bin_dir (); |
|
283 |
|
284 set_default_exec_path (); |
|
285 |
|
286 set_default_path (); |
|
287 |
|
288 set_default_info_file (); |
|
289 |
|
290 set_default_info_prog (); |
|
291 |
|
292 set_default_editor (); |
|
293 |
|
294 set_local_site_defaults_file (); |
|
295 |
|
296 set_site_defaults_file (); |
|
297 } |
|
298 |
|
299 static int |
|
300 editor (void) |
|
301 { |
|
302 int status = 0; |
|
303 |
|
304 string s = builtin_string_variable ("EDITOR"); |
|
305 |
|
306 if (s.empty ()) |
|
307 { |
|
308 gripe_invalid_value_specified ("EDITOR"); |
|
309 status = -1; |
|
310 } |
|
311 else |
|
312 Veditor = s; |
|
313 |
|
314 return status; |
|
315 } |
|
316 |
|
317 static int |
|
318 exec_path (void) |
|
319 { |
|
320 int status = 0; |
|
321 |
2439
|
322 string s = builtin_string_variable ("EXEC_PATH"); |
|
323 |
|
324 if (s.empty ()) |
2203
|
325 { |
|
326 gripe_invalid_value_specified ("EXEC_PATH"); |
|
327 status = -1; |
|
328 } |
|
329 else |
|
330 { |
2439
|
331 Vexec_path = s; |
|
332 |
3141
|
333 string std_path = Vlocal_arch_lib_dir + string (SEPCHAR_STR) |
|
334 + Varch_lib_dir + string (SEPCHAR_STR) + Vbin_dir; |
2439
|
335 |
2926
|
336 string path; |
2203
|
337 |
|
338 int eplen = Vexec_path.length (); |
|
339 |
|
340 if (eplen > 0) |
|
341 { |
2926
|
342 bool prepend = (Vexec_path[0] == ':'); |
|
343 bool append = (eplen > 1 && Vexec_path[eplen-1] == ':'); |
2203
|
344 |
|
345 if (prepend) |
|
346 { |
2926
|
347 path = std_path + Vexec_path; |
|
348 |
2203
|
349 if (append) |
2926
|
350 path.append (std_path); |
2203
|
351 } |
|
352 else |
|
353 { |
2926
|
354 path = Vexec_path; |
|
355 |
2203
|
356 if (append) |
2926
|
357 path.append (std_path); |
2203
|
358 } |
|
359 } |
|
360 else |
2926
|
361 path = std_path; |
2203
|
362 |
2926
|
363 octave_env::putenv ("PATH", path); |
2203
|
364 } |
|
365 |
|
366 return status; |
|
367 } |
|
368 |
|
369 static int |
|
370 imagepath (void) |
|
371 { |
|
372 int status = 0; |
|
373 |
|
374 string s = builtin_string_variable ("IMAGEPATH"); |
|
375 |
|
376 if (s.empty ()) |
|
377 { |
|
378 gripe_invalid_value_specified ("IMAGEPATH"); |
|
379 status = -1; |
|
380 } |
|
381 else |
|
382 Vimagepath = s; |
|
383 |
|
384 return status; |
|
385 } |
|
386 |
|
387 static int |
|
388 loadpath (void) |
|
389 { |
|
390 int status = 0; |
|
391 |
|
392 string s = builtin_string_variable ("LOADPATH"); |
|
393 |
|
394 if (s.empty ()) |
|
395 { |
|
396 gripe_invalid_value_specified ("LOADPATH"); |
|
397 status = -1; |
|
398 } |
|
399 else |
3174
|
400 { |
|
401 Vload_path = maybe_add_default_load_path (s); |
|
402 |
|
403 Vload_path_dir_path = dir_path (Vload_path); |
|
404 } |
2203
|
405 |
|
406 return status; |
|
407 } |
|
408 |
|
409 void |
|
410 symbols_of_defaults (void) |
|
411 { |
|
412 DEFVAR (EDITOR, Veditor, 0, editor, |
|
413 "name of the editor to be invoked by the edit_history command"); |
|
414 |
|
415 DEFVAR (EXEC_PATH, Vexec_path, 0, exec_path, |
|
416 "colon separated list of directories to search for programs to run"); |
|
417 |
|
418 DEFVAR (LOADPATH, Vload_path, 0, loadpath, |
|
419 "colon separated list of directories to search for scripts"); |
|
420 |
|
421 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, 0, imagepath, |
|
422 "colon separated list of directories to search for image files"); |
|
423 |
3141
|
424 DEFCONST (OCTAVE_HOME, Voctave_home, |
2831
|
425 "top-level Octave installation directory"); |
|
426 |
3141
|
427 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, |
2203
|
428 "Octave version"); |
|
429 } |
|
430 |
3185
|
431 DEFUN (rehash, , , |
|
432 "rehash (): reinitialize LOADPATH directory cache") |
|
433 { |
|
434 octave_value_list retval; |
|
435 |
|
436 Vload_path_dir_path.rehash (); |
|
437 |
|
438 return retval; |
|
439 } |
|
440 |
2203
|
441 /* |
|
442 ;;; Local Variables: *** |
|
443 ;;; mode: C++ *** |
|
444 ;;; End: *** |
|
445 */ |