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 /* |
|
24 |
|
25 The function builtin_pwd adapted from a similar function from GNU |
|
26 Bash, the Bourne Again SHell, copyright (C) 1987, 1989, 1991 Free |
|
27 Software Foundation, Inc. |
|
28 |
|
29 */ |
|
30 |
|
31 #ifdef HAVE_CONFIG_H |
|
32 #include <config.h> |
|
33 #endif |
|
34 |
2439
|
35 #include <iostream.h> |
|
36 |
2203
|
37 #include <cstdlib> |
|
38 |
|
39 #include <string> |
|
40 |
|
41 #ifdef HAVE_UNISTD_H |
2442
|
42 #ifdef HAVE_SYS_TYPES_H |
2203
|
43 #include <sys/types.h> |
2442
|
44 #endif |
2203
|
45 #include <unistd.h> |
|
46 #endif |
|
47 |
2926
|
48 #include "oct-env.h" |
|
49 |
2492
|
50 #include <defaults.h> |
2203
|
51 #include "defun.h" |
|
52 #include "error.h" |
|
53 #include "gripes.h" |
|
54 #include "help.h" |
2390
|
55 #include "ov.h" |
2203
|
56 #include "toplev.h" |
|
57 #include "variables.h" |
2492
|
58 #include <version.h> |
2203
|
59 |
|
60 string Voctave_home; |
|
61 |
|
62 string Vbin_dir; |
|
63 string Vlib_dir; |
|
64 string Vinfo_dir; |
|
65 string Varch_lib_dir; |
2439
|
66 string Vlocal_arch_lib_dir; |
2203
|
67 string Vfcn_file_dir; |
|
68 |
|
69 // The path that will be searched for programs that we execute. |
|
70 // (--exec-path path) |
|
71 string Vexec_path; |
|
72 |
|
73 // Load path specified on command line. |
|
74 // (--path path; -p path) |
|
75 string Vload_path; |
|
76 |
|
77 // Name of the editor to be invoked by the edit_history command. |
|
78 string Veditor; |
|
79 |
|
80 string Vimagepath; |
|
81 |
|
82 string Vlocal_site_defaults_file; |
|
83 string Vsite_defaults_file; |
|
84 |
|
85 static string |
|
86 subst_octave_home (const string& s) |
|
87 { |
|
88 string retval; |
|
89 |
|
90 string prefix = OCTAVE_PREFIX; |
|
91 |
|
92 retval = s; |
|
93 |
|
94 if (Voctave_home != prefix) |
|
95 { |
|
96 int len = prefix.length (); |
|
97 size_t start = 0; |
2522
|
98 while ((start = retval.find (prefix, start)) != NPOS) |
2203
|
99 { |
|
100 retval.replace (start, len, Voctave_home); |
2522
|
101 start += len; |
2203
|
102 } |
|
103 } |
|
104 |
|
105 return retval; |
|
106 } |
|
107 |
|
108 static void |
|
109 set_octave_home (void) |
|
110 { |
2926
|
111 string oh = octave_env::getenv ("OCTAVE_HOME"); |
2203
|
112 |
2926
|
113 Voctave_home = oh.empty () ? string (OCTAVE_PREFIX) : oh; |
2203
|
114 } |
|
115 |
|
116 static void |
|
117 set_default_info_dir (void) |
|
118 { |
|
119 Vinfo_dir = subst_octave_home (OCTAVE_INFODIR); |
|
120 } |
|
121 |
|
122 static void |
|
123 set_default_arch_lib_dir (void) |
|
124 { |
|
125 Varch_lib_dir = subst_octave_home (OCTAVE_ARCHLIBDIR); |
|
126 } |
|
127 |
|
128 static void |
2439
|
129 set_default_local_arch_lib_dir (void) |
|
130 { |
|
131 Vlocal_arch_lib_dir = subst_octave_home (OCTAVE_LOCALARCHLIBDIR); |
|
132 } |
|
133 |
|
134 static void |
2203
|
135 set_default_fcn_file_dir (void) |
|
136 { |
|
137 Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR); |
|
138 } |
|
139 |
|
140 static void |
|
141 set_default_bin_dir (void) |
|
142 { |
|
143 Vbin_dir = subst_octave_home (OCTAVE_BINDIR); |
|
144 } |
|
145 |
|
146 static void |
|
147 set_default_lib_dir (void) |
|
148 { |
|
149 Vlib_dir = subst_octave_home (OCTAVE_LIBDIR); |
|
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 ()) |
2203
|
202 { |
|
203 Vinfo_prog = Varch_lib_dir; |
|
204 Vinfo_prog.append ("/info"); |
|
205 } |
2926
|
206 else |
|
207 Vinfo_prog = string (oct_info_prog); |
2203
|
208 } |
|
209 |
|
210 static void |
|
211 set_default_editor (void) |
|
212 { |
|
213 Veditor = "vi"; |
|
214 |
2926
|
215 string env_editor = octave_env::getenv ("EDITOR"); |
2203
|
216 |
2926
|
217 if (! env_editor.empty ()) |
|
218 Veditor = env_editor; |
2203
|
219 } |
|
220 |
|
221 static void |
|
222 set_local_site_defaults_file (void) |
|
223 { |
|
224 Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR); |
|
225 Vlocal_site_defaults_file.append ("/octaverc"); |
|
226 } |
|
227 |
|
228 static void |
|
229 set_site_defaults_file (void) |
|
230 { |
|
231 Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR); |
|
232 Vsite_defaults_file.append ("/octaverc"); |
|
233 } |
|
234 |
|
235 string |
|
236 maybe_add_default_load_path (const string& pathstring) |
|
237 { |
|
238 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
|
239 |
|
240 string retval; |
|
241 |
|
242 if (! pathstring.empty ()) |
|
243 { |
|
244 if (pathstring[0] == SEPCHAR) |
|
245 { |
|
246 retval = std_path; |
|
247 retval.append (pathstring); |
|
248 } |
|
249 else |
|
250 retval = pathstring; |
|
251 |
|
252 if (pathstring[pathstring.length () - 1] == SEPCHAR) |
|
253 retval.append (std_path); |
|
254 } |
|
255 |
|
256 return retval; |
|
257 } |
|
258 |
|
259 void |
|
260 install_defaults (void) |
|
261 { |
|
262 // OCTAVE_HOME must be set first! |
|
263 |
|
264 set_octave_home (); |
|
265 |
|
266 set_default_info_dir (); |
|
267 |
|
268 set_default_arch_lib_dir (); |
|
269 |
2439
|
270 set_default_local_arch_lib_dir (); |
|
271 |
2203
|
272 set_default_fcn_file_dir (); |
|
273 |
|
274 set_default_bin_dir (); |
|
275 |
|
276 set_default_lib_dir (); |
|
277 |
|
278 set_default_exec_path (); |
|
279 |
|
280 set_default_path (); |
|
281 |
|
282 set_default_info_file (); |
|
283 |
|
284 set_default_info_prog (); |
|
285 |
|
286 set_default_editor (); |
|
287 |
|
288 set_local_site_defaults_file (); |
|
289 |
|
290 set_site_defaults_file (); |
|
291 } |
|
292 |
|
293 static int |
|
294 editor (void) |
|
295 { |
|
296 int status = 0; |
|
297 |
|
298 string s = builtin_string_variable ("EDITOR"); |
|
299 |
|
300 if (s.empty ()) |
|
301 { |
|
302 gripe_invalid_value_specified ("EDITOR"); |
|
303 status = -1; |
|
304 } |
|
305 else |
|
306 Veditor = s; |
|
307 |
|
308 return status; |
|
309 } |
|
310 |
|
311 static int |
|
312 exec_path (void) |
|
313 { |
|
314 int status = 0; |
|
315 |
2439
|
316 string s = builtin_string_variable ("EXEC_PATH"); |
|
317 |
|
318 if (s.empty ()) |
2203
|
319 { |
|
320 gripe_invalid_value_specified ("EXEC_PATH"); |
|
321 status = -1; |
|
322 } |
|
323 else |
|
324 { |
2439
|
325 Vexec_path = s; |
|
326 |
|
327 string std_path = Vlocal_arch_lib_dir; |
|
328 std_path.append (SEPCHAR_STR); |
|
329 std_path.append (Varch_lib_dir); |
|
330 |
2926
|
331 string path; |
2203
|
332 |
|
333 int eplen = Vexec_path.length (); |
|
334 |
|
335 if (eplen > 0) |
|
336 { |
2926
|
337 bool prepend = (Vexec_path[0] == ':'); |
|
338 bool append = (eplen > 1 && Vexec_path[eplen-1] == ':'); |
2203
|
339 |
|
340 if (prepend) |
|
341 { |
2926
|
342 path = std_path + Vexec_path; |
|
343 |
2203
|
344 if (append) |
2926
|
345 path.append (std_path); |
2203
|
346 } |
|
347 else |
|
348 { |
2926
|
349 path = Vexec_path; |
|
350 |
2203
|
351 if (append) |
2926
|
352 path.append (std_path); |
2203
|
353 } |
|
354 } |
|
355 else |
2926
|
356 path = std_path; |
2203
|
357 |
2926
|
358 octave_env::putenv ("PATH", path); |
2203
|
359 } |
|
360 |
|
361 return status; |
|
362 } |
|
363 |
|
364 static int |
|
365 imagepath (void) |
|
366 { |
|
367 int status = 0; |
|
368 |
|
369 string s = builtin_string_variable ("IMAGEPATH"); |
|
370 |
|
371 if (s.empty ()) |
|
372 { |
|
373 gripe_invalid_value_specified ("IMAGEPATH"); |
|
374 status = -1; |
|
375 } |
|
376 else |
|
377 Vimagepath = s; |
|
378 |
|
379 return status; |
|
380 } |
|
381 |
|
382 static int |
|
383 loadpath (void) |
|
384 { |
|
385 int status = 0; |
|
386 |
|
387 string s = builtin_string_variable ("LOADPATH"); |
|
388 |
|
389 if (s.empty ()) |
|
390 { |
|
391 gripe_invalid_value_specified ("LOADPATH"); |
|
392 status = -1; |
|
393 } |
|
394 else |
|
395 Vload_path = maybe_add_default_load_path (s); |
|
396 |
|
397 return status; |
|
398 } |
|
399 |
|
400 void |
|
401 symbols_of_defaults (void) |
|
402 { |
|
403 DEFVAR (EDITOR, Veditor, 0, editor, |
|
404 "name of the editor to be invoked by the edit_history command"); |
|
405 |
|
406 DEFVAR (EXEC_PATH, Vexec_path, 0, exec_path, |
|
407 "colon separated list of directories to search for programs to run"); |
|
408 |
|
409 DEFVAR (LOADPATH, Vload_path, 0, loadpath, |
|
410 "colon separated list of directories to search for scripts"); |
|
411 |
|
412 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, 0, imagepath, |
|
413 "colon separated list of directories to search for image files"); |
|
414 |
2831
|
415 DEFCONST (OCTAVE_HOME, Voctave_home, 0, 0, |
|
416 "top-level Octave installation directory"); |
|
417 |
2203
|
418 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, 0, 0, |
|
419 "Octave version"); |
|
420 } |
|
421 |
|
422 /* |
|
423 ;;; Local Variables: *** |
|
424 ;;; mode: C++ *** |
|
425 ;;; End: *** |
|
426 */ |