comparison src/defaults.cc @ 2203:88a4d3580427

[project @ 1996-05-15 06:09:30 by jwe] Initial revision
author jwe
date Wed, 15 May 1996 06:09:30 +0000
parents
children c2c1482c34c8
comparison
equal deleted inserted replaced
2202:31b62b7c5d2d 2203:88a4d3580427
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
35 #include <cstdlib>
36
37 #include <string>
38
39 #ifdef HAVE_UNISTD_H
40 #include <sys/types.h>
41 #include <unistd.h>
42 #endif
43
44 #include "defaults.h"
45 #include "defun.h"
46 #include "error.h"
47 #include "gripes.h"
48 #include "help.h"
49 #include "pt-const.h"
50 #include "toplev.h"
51 #include "variables.h"
52 #include "version.h"
53
54 string Voctave_home;
55
56 string Vbin_dir;
57 string Vlib_dir;
58 string Vinfo_dir;
59 string Varch_lib_dir;
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;
91 while ((start = s.find (prefix)) != NPOS)
92 {
93 retval.replace (start, len, Voctave_home);
94 start++;
95 }
96 }
97
98 return retval;
99 }
100
101 static void
102 set_octave_home (void)
103 {
104 char *oh = getenv ("OCTAVE_HOME");
105
106 Voctave_home = oh ? string (oh) : string (OCTAVE_PREFIX);
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
122 set_default_fcn_file_dir (void)
123 {
124 Vfcn_file_dir = subst_octave_home (OCTAVE_FCNFILEDIR);
125 }
126
127 static void
128 set_default_bin_dir (void)
129 {
130 Vbin_dir = subst_octave_home (OCTAVE_BINDIR);
131 }
132
133 static void
134 set_default_lib_dir (void)
135 {
136 Vlib_dir = subst_octave_home (OCTAVE_LIBDIR);
137 }
138
139 static void
140 set_default_exec_path (void)
141 {
142 char *octave_exec_path = getenv ("OCTAVE_EXEC_PATH");
143
144 if (octave_exec_path)
145 Vexec_path = string (octave_exec_path);
146 else
147 {
148 char *shell_path = getenv ("PATH");
149
150 if (shell_path)
151 {
152 Vexec_path = string (":");
153 Vexec_path.append (shell_path);
154 }
155 }
156 }
157
158 // Handle OCTAVE_PATH from the environment like TeX handles TEXINPUTS.
159 // If the path starts with `:', prepend the standard path. If it ends
160 // with `:' append the standard path. If it begins and ends with
161 // `:', do both (which is useless, but the luser asked for it...).
162
163 static void
164 set_default_path (void)
165 {
166 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH);
167
168 char *oct_path = getenv ("OCTAVE_PATH");
169
170 Vload_path = oct_path ? string (oct_path) : std_path;
171 }
172
173 static void
174 set_default_info_file (void)
175 {
176 char *oct_info_file = getenv ("OCTAVE_INFO_FILE");
177
178 if (oct_info_file)
179 Vinfo_file = string (oct_info_file);
180 else
181 {
182 Vinfo_file = Vinfo_dir;
183 Vinfo_file.append ("/octave.info");
184 }
185 }
186
187 static void
188 set_default_info_prog (void)
189 {
190 char *oct_info_prog = getenv ("OCTAVE_INFO_PROGRAM");
191
192 if (oct_info_prog)
193 Vinfo_prog = string (oct_info_prog);
194 else
195 {
196 Vinfo_prog = Varch_lib_dir;
197 Vinfo_prog.append ("/info");
198 }
199 }
200
201 static void
202 set_default_editor (void)
203 {
204 Veditor = "vi";
205
206 char *env_editor = getenv ("EDITOR");
207
208 if (env_editor && *env_editor)
209 Veditor = string (env_editor);
210 }
211
212 static void
213 set_local_site_defaults_file (void)
214 {
215 Vlocal_site_defaults_file = subst_octave_home (OCTAVE_LOCALSTARTUPFILEDIR);
216 Vlocal_site_defaults_file.append ("/octaverc");
217 }
218
219 static void
220 set_site_defaults_file (void)
221 {
222 Vsite_defaults_file = subst_octave_home (OCTAVE_STARTUPFILEDIR);
223 Vsite_defaults_file.append ("/octaverc");
224 }
225
226 string
227 maybe_add_default_load_path (const string& pathstring)
228 {
229 string std_path = subst_octave_home (OCTAVE_FCNFILEPATH);
230
231 string retval;
232
233 if (! pathstring.empty ())
234 {
235 if (pathstring[0] == SEPCHAR)
236 {
237 retval = std_path;
238 retval.append (pathstring);
239 }
240 else
241 retval = pathstring;
242
243 if (pathstring[pathstring.length () - 1] == SEPCHAR)
244 retval.append (std_path);
245 }
246
247 return retval;
248 }
249
250 void
251 install_defaults (void)
252 {
253 // OCTAVE_HOME must be set first!
254
255 set_octave_home ();
256
257 set_default_info_dir ();
258
259 set_default_arch_lib_dir ();
260
261 set_default_fcn_file_dir ();
262
263 set_default_bin_dir ();
264
265 set_default_lib_dir ();
266
267 set_default_exec_path ();
268
269 set_default_path ();
270
271 set_default_info_file ();
272
273 set_default_info_prog ();
274
275 set_default_editor ();
276
277 set_local_site_defaults_file ();
278
279 set_site_defaults_file ();
280 }
281
282 static int
283 editor (void)
284 {
285 int status = 0;
286
287 string s = builtin_string_variable ("EDITOR");
288
289 if (s.empty ())
290 {
291 gripe_invalid_value_specified ("EDITOR");
292 status = -1;
293 }
294 else
295 Veditor = s;
296
297 return status;
298 }
299
300 static int
301 exec_path (void)
302 {
303 int status = 0;
304
305 if (Vexec_path.empty ())
306 {
307 gripe_invalid_value_specified ("EXEC_PATH");
308 status = -1;
309 }
310 else
311 {
312 int len = Varch_lib_dir.length () + Vbin_dir.length ()
313 + strlen (SEPCHAR_STR);
314
315 static char *putenv_cmd = 0;
316
317 delete [] putenv_cmd;
318
319 putenv_cmd = 0;
320
321 int eplen = Vexec_path.length ();
322
323 if (eplen > 0)
324 {
325 int prepend = (Vexec_path[0] == ':');
326 int append = (eplen > 1 && Vexec_path[eplen-1] == ':');
327
328 if (prepend)
329 {
330 if (append)
331 {
332 putenv_cmd = new char [2 * len + eplen + 6];
333 sprintf (putenv_cmd,
334 "PATH=%s" SEPCHAR_STR "%s%s%s" SEPCHAR_STR "%s",
335 Varch_lib_dir.c_str (), Vbin_dir.c_str (),
336 Vexec_path.c_str (), Varch_lib_dir.c_str (),
337 Vbin_dir.c_str ());
338 }
339 else
340 {
341 putenv_cmd = new char [len + eplen + 6];
342 sprintf (putenv_cmd,
343 "PATH=%s" SEPCHAR_STR "%s%s",
344 Varch_lib_dir.c_str (), Vbin_dir.c_str (),
345 Vexec_path.c_str ());
346 }
347 }
348 else
349 {
350 if (append)
351 {
352 putenv_cmd = new char [len + eplen + 6];
353 sprintf (putenv_cmd,
354 "PATH=%s%s" SEPCHAR_STR "%s",
355 Vexec_path.c_str (), Varch_lib_dir.c_str (),
356 Vbin_dir.c_str ());
357 }
358 else
359 {
360 putenv_cmd = new char [len + eplen + 6];
361 sprintf (putenv_cmd, "PATH=%s", Vexec_path.c_str ());
362 }
363 }
364 }
365 else
366 {
367 putenv_cmd = new char [len+6];
368 sprintf (putenv_cmd, "PATH=%s" SEPCHAR_STR "%s",
369 Varch_lib_dir.c_str (), Vbin_dir.c_str ());
370 }
371
372 putenv (putenv_cmd);
373 }
374
375 return status;
376 }
377
378 static int
379 imagepath (void)
380 {
381 int status = 0;
382
383 string s = builtin_string_variable ("IMAGEPATH");
384
385 if (s.empty ())
386 {
387 gripe_invalid_value_specified ("IMAGEPATH");
388 status = -1;
389 }
390 else
391 Vimagepath = s;
392
393 return status;
394 }
395
396 static int
397 loadpath (void)
398 {
399 int status = 0;
400
401 string s = builtin_string_variable ("LOADPATH");
402
403 if (s.empty ())
404 {
405 gripe_invalid_value_specified ("LOADPATH");
406 status = -1;
407 }
408 else
409 Vload_path = maybe_add_default_load_path (s);
410
411 return status;
412 }
413
414 void
415 symbols_of_defaults (void)
416 {
417 DEFVAR (EDITOR, Veditor, 0, editor,
418 "name of the editor to be invoked by the edit_history command");
419
420 DEFVAR (EXEC_PATH, Vexec_path, 0, exec_path,
421 "colon separated list of directories to search for programs to run");
422
423 DEFVAR (LOADPATH, Vload_path, 0, loadpath,
424 "colon separated list of directories to search for scripts");
425
426 DEFVAR (IMAGEPATH, OCTAVE_IMAGEPATH, 0, imagepath,
427 "colon separated list of directories to search for image files");
428
429 DEFCONSTX ("OCTAVE_VERSION", SBV_OCTAVE_VERSION, OCTAVE_VERSION, 0, 0,
430 "Octave version");
431 }
432
433 /*
434 ;;; Local Variables: ***
435 ;;; mode: C++ ***
436 ;;; End: ***
437 */