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 |
|
42 #include <sys/types.h> |
|
43 #include <unistd.h> |
|
44 #endif |
|
45 |
|
46 #include "defaults.h" |
|
47 #include "defun.h" |
|
48 #include "error.h" |
|
49 #include "gripes.h" |
|
50 #include "help.h" |
2390
|
51 #include "ov.h" |
2203
|
52 #include "toplev.h" |
|
53 #include "variables.h" |
|
54 #include "version.h" |
|
55 |
|
56 string Voctave_home; |
|
57 |
|
58 string Vbin_dir; |
|
59 string Vlib_dir; |
|
60 string Vinfo_dir; |
|
61 string Varch_lib_dir; |
2439
|
62 string Vlocal_arch_lib_dir; |
2203
|
63 string Vfcn_file_dir; |
|
64 |
|
65 // The path that will be searched for programs that we execute. |
|
66 // (--exec-path path) |
|
67 string Vexec_path; |
|
68 |
|
69 // Load path specified on command line. |
|
70 // (--path path; -p path) |
|
71 string Vload_path; |
|
72 |
|
73 // Name of the editor to be invoked by the edit_history command. |
|
74 string Veditor; |
|
75 |
|
76 string Vimagepath; |
|
77 |
|
78 string Vlocal_site_defaults_file; |
|
79 string Vsite_defaults_file; |
|
80 |
|
81 static string |
|
82 subst_octave_home (const string& s) |
|
83 { |
|
84 string retval; |
|
85 |
|
86 string prefix = OCTAVE_PREFIX; |
|
87 |
|
88 retval = s; |
|
89 |
|
90 if (Voctave_home != prefix) |
|
91 { |
|
92 int len = prefix.length (); |
|
93 size_t start = 0; |
|
94 while ((start = s.find (prefix)) != NPOS) |
|
95 { |
|
96 retval.replace (start, len, Voctave_home); |
|
97 start++; |
|
98 } |
|
99 } |
|
100 |
|
101 return retval; |
|
102 } |
|
103 |
|
104 static void |
|
105 set_octave_home (void) |
|
106 { |
|
107 char *oh = getenv ("OCTAVE_HOME"); |
|
108 |
|
109 Voctave_home = oh ? string (oh) : string (OCTAVE_PREFIX); |
|
110 } |
|
111 |
|
112 static void |
|
113 set_default_info_dir (void) |
|
114 { |
|
115 Vinfo_dir = subst_octave_home (OCTAVE_INFODIR); |
|
116 } |
|
117 |
|
118 static void |
|
119 set_default_arch_lib_dir (void) |
|
120 { |
|
121 Varch_lib_dir = subst_octave_home (OCTAVE_ARCHLIBDIR); |
|
122 } |
|
123 |
|
124 static void |
2439
|
125 set_default_local_arch_lib_dir (void) |
|
126 { |
|
127 Vlocal_arch_lib_dir = subst_octave_home (OCTAVE_LOCALARCHLIBDIR); |
|
128 } |
|
129 |
|
130 static void |
2203
|
131 set_default_fcn_file_dir (void) |
|
132 { |
|
133 Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR); |
|
134 } |
|
135 |
|
136 static void |
|
137 set_default_bin_dir (void) |
|
138 { |
|
139 Vbin_dir = subst_octave_home (OCTAVE_BINDIR); |
|
140 } |
|
141 |
|
142 static void |
|
143 set_default_lib_dir (void) |
|
144 { |
|
145 Vlib_dir = subst_octave_home (OCTAVE_LIBDIR); |
|
146 } |
|
147 |
|
148 static void |
|
149 set_default_exec_path (void) |
|
150 { |
|
151 char *octave_exec_path = getenv ("OCTAVE_EXEC_PATH"); |
|
152 |
|
153 if (octave_exec_path) |
|
154 Vexec_path = string (octave_exec_path); |
|
155 else |
|
156 { |
|
157 char *shell_path = getenv ("PATH"); |
|
158 |
|
159 if (shell_path) |
|
160 { |
|
161 Vexec_path = string (":"); |
|
162 Vexec_path.append (shell_path); |
|
163 } |
|
164 } |
|
165 } |
|
166 |
|
167 // Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS. |
|
168 // If the path starts with `:', prepend the standard path. If it ends |
|
169 // with `:' append the standard path. If it begins and ends with |
|
170 // `:', do both (which is useless, but the luser asked for it...). |
|
171 |
|
172 static void |
|
173 set_default_path (void) |
|
174 { |
|
175 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH); |
|
176 |
|
177 char *oct_path = getenv ("OCTAVE_PATH"); |
|
178 |
|
179 Vload_path = oct_path ? string (oct_path) : std_path; |
|
180 } |
|
181 |
|
182 static void |
|
183 set_default_info_file (void) |
|
184 { |
|
185 char *oct_info_file = getenv ("OCTAVE_INFO_FILE"); |
|
186 |
|
187 if (oct_info_file) |
|
188 Vinfo_file = string (oct_info_file); |
|
189 else |
|
190 { |
|
191 Vinfo_file = Vinfo_dir; |
|
192 Vinfo_file.append ("/octave.info"); |
|
193 } |
|
194 } |
|
195 |
|
196 static void |
|
197 set_default_info_prog (void) |
|
198 { |
|
199 char *oct_info_prog = getenv ("OCTAVE_INFO_PROGRAM"); |
|
200 |
|
201 if (oct_info_prog) |
|
202 Vinfo_prog = string (oct_info_prog); |
|
203 else |
|
204 { |
|
205 Vinfo_prog = Varch_lib_dir; |
|
206 Vinfo_prog.append ("/info"); |
|
207 } |
|
208 } |
|
209 |
|
210 static void |
|
211 set_default_editor (void) |
|
212 { |
|
213 Veditor = "vi"; |
|
214 |
|
215 char *env_editor = getenv ("EDITOR"); |
|
216 |
|
217 if (env_editor && *env_editor) |
|
218 Veditor = string (env_editor); |
|
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 std_path.append (SEPCHAR_STR); |
|
331 std_path.append (Vbin_dir); |
|
332 |
|
333 int std_len = std_path.length (); |
2203
|
334 |
|
335 static char *putenv_cmd = 0; |
|
336 |
|
337 delete [] putenv_cmd; |
|
338 |
|
339 putenv_cmd = 0; |
|
340 |
|
341 int eplen = Vexec_path.length (); |
|
342 |
|
343 if (eplen > 0) |
|
344 { |
|
345 int prepend = (Vexec_path[0] == ':'); |
|
346 int append = (eplen > 1 && Vexec_path[eplen-1] == ':'); |
|
347 |
|
348 if (prepend) |
|
349 { |
|
350 if (append) |
|
351 { |
2439
|
352 putenv_cmd = new char [2 * std_len + eplen + 6]; |
|
353 sprintf (putenv_cmd, "PATH=%s%s%s", |
|
354 std_path.c_str (), Vexec_path.c_str (), |
|
355 std_path.c_str ()); |
2203
|
356 } |
|
357 else |
|
358 { |
2439
|
359 putenv_cmd = new char [std_len + eplen + 6]; |
|
360 sprintf (putenv_cmd, "PATH=%s%s", |
|
361 std_path.c_str (), Vexec_path.c_str ()); |
2203
|
362 } |
|
363 } |
|
364 else |
|
365 { |
|
366 if (append) |
|
367 { |
2439
|
368 putenv_cmd = new char [std_len + eplen + 6]; |
|
369 sprintf (putenv_cmd, "PATH=%s%s", |
|
370 Vexec_path.c_str (), std_path.c_str ()); |
2203
|
371 } |
|
372 else |
|
373 { |
2439
|
374 putenv_cmd = new char [eplen + 6]; |
2203
|
375 sprintf (putenv_cmd, "PATH=%s", Vexec_path.c_str ()); |
|
376 } |
|
377 } |
|
378 } |
|
379 else |
|
380 { |
2439
|
381 putenv_cmd = new char [std_len+6]; |
|
382 sprintf (putenv_cmd, "PATH=%s", std_path.c_str ()); |
2203
|
383 } |
|
384 |
|
385 putenv (putenv_cmd); |
|
386 } |
|
387 |
|
388 return status; |
|
389 } |
|
390 |
|
391 static int |
|
392 imagepath (void) |
|
393 { |
|
394 int status = 0; |
|
395 |
|
396 string s = builtin_string_variable ("IMAGEPATH"); |
|
397 |
|
398 if (s.empty ()) |
|
399 { |
|
400 gripe_invalid_value_specified ("IMAGEPATH"); |
|
401 status = -1; |
|
402 } |
|
403 else |
|
404 Vimagepath = s; |
|
405 |
|
406 return status; |
|
407 } |
|
408 |
|
409 static int |
|
410 loadpath (void) |
|
411 { |
|
412 int status = 0; |
|
413 |
|
414 string s = builtin_string_variable ("LOADPATH"); |
|
415 |
|
416 if (s.empty ()) |
|
417 { |
|
418 gripe_invalid_value_specified ("LOADPATH"); |
|
419 status = -1; |
|
420 } |
|
421 else |
|
422 Vload_path = maybe_add_default_load_path (s); |
|
423 |
|
424 return status; |
|
425 } |
|
426 |
|
427 void |
|
428 symbols_of_defaults (void) |
|
429 { |
|
430 DEFVAR (EDITOR, Veditor, 0, editor, |
|
431 "name of the editor to be invoked by the edit_history command"); |
|
432 |
|
433 DEFVAR (EXEC_PATH, Vexec_path, 0, exec_path, |
|
434 "colon separated list of directories to search for programs to run"); |
|
435 |
|
436 DEFVAR (LOADPATH, Vload_path, 0, loadpath, |
|
437 "colon separated list of directories to search for scripts"); |
|
438 |
|
439 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, 0, imagepath, |
|
440 "colon separated list of directories to search for image files"); |
|
441 |
|
442 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, 0, 0, |
|
443 "Octave version"); |
|
444 } |
|
445 |
|
446 /* |
|
447 ;;; Local Variables: *** |
|
448 ;;; mode: C++ *** |
|
449 ;;; End: *** |
|
450 */ |