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