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