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 |
2492
|
48 #include <defaults.h> |
2203
|
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" |
2492
|
56 #include <version.h> |
2203
|
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; |
2522
|
96 while ((start = retval.find (prefix, start)) != NPOS) |
2203
|
97 { |
|
98 retval.replace (start, len, Voctave_home); |
2522
|
99 start += len; |
2203
|
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 { |
2512
|
187 string std_info_file = subst_octave_home (OCTAVE_INFOFILE); |
|
188 |
2203
|
189 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
|
190 |
2512
|
191 Vinfo_file = oct_info_file ? string (oct_info_file) : std_info_file; |
2203
|
192 } |
|
193 |
|
194 static void |
|
195 set_default_info_prog (void) |
|
196 { |
|
197 char *oct_info_prog = getenv ("OCTAVE_INFO_PROGRAM"); |
|
198 |
|
199 if (oct_info_prog) |
|
200 Vinfo_prog = string (oct_info_prog); |
|
201 else |
|
202 { |
|
203 Vinfo_prog = Varch_lib_dir; |
|
204 Vinfo_prog.append ("/info"); |
|
205 } |
|
206 } |
|
207 |
|
208 static void |
|
209 set_default_editor (void) |
|
210 { |
|
211 Veditor = "vi"; |
|
212 |
|
213 char *env_editor = getenv ("EDITOR"); |
|
214 |
|
215 if (env_editor && *env_editor) |
|
216 Veditor = string (env_editor); |
|
217 } |
|
218 |
|
219 static void |
|
220 set_local_site_defaults_file (void) |
|
221 { |
|
222 Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR); |
|
223 Vlocal_site_defaults_file.append ("/octaverc"); |
|
224 } |
|
225 |
|
226 static void |
|
227 set_site_defaults_file (void) |
|
228 { |
|
229 Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR); |
|
230 Vsite_defaults_file.append ("/octaverc"); |
|
231 } |
|
232 |
|
233 string |
|
234 maybe_add_default_load_path (const string& pathstring) |
|
235 { |
|
236 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
|
237 |
|
238 string retval; |
|
239 |
|
240 if (! pathstring.empty ()) |
|
241 { |
|
242 if (pathstring[0] == SEPCHAR) |
|
243 { |
|
244 retval = std_path; |
|
245 retval.append (pathstring); |
|
246 } |
|
247 else |
|
248 retval = pathstring; |
|
249 |
|
250 if (pathstring[pathstring.length () - 1] == SEPCHAR) |
|
251 retval.append (std_path); |
|
252 } |
|
253 |
|
254 return retval; |
|
255 } |
|
256 |
|
257 void |
|
258 install_defaults (void) |
|
259 { |
|
260 // OCTAVE_HOME must be set first! |
|
261 |
|
262 set_octave_home (); |
|
263 |
|
264 set_default_info_dir (); |
|
265 |
|
266 set_default_arch_lib_dir (); |
|
267 |
2439
|
268 set_default_local_arch_lib_dir (); |
|
269 |
2203
|
270 set_default_fcn_file_dir (); |
|
271 |
|
272 set_default_bin_dir (); |
|
273 |
|
274 set_default_lib_dir (); |
|
275 |
|
276 set_default_exec_path (); |
|
277 |
|
278 set_default_path (); |
|
279 |
|
280 set_default_info_file (); |
|
281 |
|
282 set_default_info_prog (); |
|
283 |
|
284 set_default_editor (); |
|
285 |
|
286 set_local_site_defaults_file (); |
|
287 |
|
288 set_site_defaults_file (); |
|
289 } |
|
290 |
|
291 static int |
|
292 editor (void) |
|
293 { |
|
294 int status = 0; |
|
295 |
|
296 string s = builtin_string_variable ("EDITOR"); |
|
297 |
|
298 if (s.empty ()) |
|
299 { |
|
300 gripe_invalid_value_specified ("EDITOR"); |
|
301 status = -1; |
|
302 } |
|
303 else |
|
304 Veditor = s; |
|
305 |
|
306 return status; |
|
307 } |
|
308 |
|
309 static int |
|
310 exec_path (void) |
|
311 { |
|
312 int status = 0; |
|
313 |
2439
|
314 string s = builtin_string_variable ("EXEC_PATH"); |
|
315 |
|
316 if (s.empty ()) |
2203
|
317 { |
|
318 gripe_invalid_value_specified ("EXEC_PATH"); |
|
319 status = -1; |
|
320 } |
|
321 else |
|
322 { |
2439
|
323 Vexec_path = s; |
|
324 |
|
325 string std_path = Vlocal_arch_lib_dir; |
|
326 std_path.append (SEPCHAR_STR); |
|
327 std_path.append (Varch_lib_dir); |
|
328 std_path.append (SEPCHAR_STR); |
|
329 std_path.append (Vbin_dir); |
|
330 |
|
331 int std_len = std_path.length (); |
2203
|
332 |
|
333 static char *putenv_cmd = 0; |
|
334 |
|
335 delete [] putenv_cmd; |
|
336 |
|
337 putenv_cmd = 0; |
|
338 |
|
339 int eplen = Vexec_path.length (); |
|
340 |
|
341 if (eplen > 0) |
|
342 { |
|
343 int prepend = (Vexec_path[0] == ':'); |
|
344 int append = (eplen > 1 && Vexec_path[eplen-1] == ':'); |
|
345 |
|
346 if (prepend) |
|
347 { |
|
348 if (append) |
|
349 { |
2439
|
350 putenv_cmd = new char [2 * std_len + eplen + 6]; |
|
351 sprintf (putenv_cmd, "PATH=%s%s%s", |
|
352 std_path.c_str (), Vexec_path.c_str (), |
|
353 std_path.c_str ()); |
2203
|
354 } |
|
355 else |
|
356 { |
2439
|
357 putenv_cmd = new char [std_len + eplen + 6]; |
|
358 sprintf (putenv_cmd, "PATH=%s%s", |
|
359 std_path.c_str (), Vexec_path.c_str ()); |
2203
|
360 } |
|
361 } |
|
362 else |
|
363 { |
|
364 if (append) |
|
365 { |
2439
|
366 putenv_cmd = new char [std_len + eplen + 6]; |
|
367 sprintf (putenv_cmd, "PATH=%s%s", |
|
368 Vexec_path.c_str (), std_path.c_str ()); |
2203
|
369 } |
|
370 else |
|
371 { |
2439
|
372 putenv_cmd = new char [eplen + 6]; |
2203
|
373 sprintf (putenv_cmd, "PATH=%s", Vexec_path.c_str ()); |
|
374 } |
|
375 } |
|
376 } |
|
377 else |
|
378 { |
2439
|
379 putenv_cmd = new char [std_len+6]; |
|
380 sprintf (putenv_cmd, "PATH=%s", std_path.c_str ()); |
2203
|
381 } |
|
382 |
|
383 putenv (putenv_cmd); |
|
384 } |
|
385 |
|
386 return status; |
|
387 } |
|
388 |
|
389 static int |
|
390 imagepath (void) |
|
391 { |
|
392 int status = 0; |
|
393 |
|
394 string s = builtin_string_variable ("IMAGEPATH"); |
|
395 |
|
396 if (s.empty ()) |
|
397 { |
|
398 gripe_invalid_value_specified ("IMAGEPATH"); |
|
399 status = -1; |
|
400 } |
|
401 else |
|
402 Vimagepath = s; |
|
403 |
|
404 return status; |
|
405 } |
|
406 |
|
407 static int |
|
408 loadpath (void) |
|
409 { |
|
410 int status = 0; |
|
411 |
|
412 string s = builtin_string_variable ("LOADPATH"); |
|
413 |
|
414 if (s.empty ()) |
|
415 { |
|
416 gripe_invalid_value_specified ("LOADPATH"); |
|
417 status = -1; |
|
418 } |
|
419 else |
|
420 Vload_path = maybe_add_default_load_path (s); |
|
421 |
|
422 return status; |
|
423 } |
|
424 |
|
425 void |
|
426 symbols_of_defaults (void) |
|
427 { |
|
428 DEFVAR (EDITOR, Veditor, 0, editor, |
|
429 "name of the editor to be invoked by the edit_history command"); |
|
430 |
|
431 DEFVAR (EXEC_PATH, Vexec_path, 0, exec_path, |
|
432 "colon separated list of directories to search for programs to run"); |
|
433 |
|
434 DEFVAR (LOADPATH, Vload_path, 0, loadpath, |
|
435 "colon separated list of directories to search for scripts"); |
|
436 |
|
437 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, 0, imagepath, |
|
438 "colon separated list of directories to search for image files"); |
|
439 |
|
440 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, 0, 0, |
|
441 "Octave version"); |
|
442 } |
|
443 |
|
444 /* |
|
445 ;;; Local Variables: *** |
|
446 ;;; mode: C++ *** |
|
447 ;;; End: *** |
|
448 */ |