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