1786
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1786
|
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 |
2021
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
1786
|
27 #include <cstdlib> |
|
28 |
|
29 #include <string> |
|
30 |
3024
|
31 #include "lo-utils.h" |
3833
|
32 #include "oct-env.h" |
3505
|
33 #include "oct-kpse.h" |
1786
|
34 #include "pathsearch.h" |
|
35 #include "str-vec.h" |
3024
|
36 #include "str-vec.h" |
1786
|
37 |
3174
|
38 static bool octave_kpathsea_initialized = false; |
1786
|
39 |
|
40 string_vector |
|
41 dir_path::elements (void) |
|
42 { |
|
43 return initialized ? pv : string_vector (); |
|
44 } |
|
45 |
|
46 string_vector |
|
47 dir_path::all_directories (void) |
|
48 { |
|
49 int count = 0; |
|
50 string_vector retval; |
|
51 |
|
52 if (initialized) |
|
53 { |
|
54 int len = pv.length (); |
|
55 |
|
56 int nmax = len > 32 ? len : 32; |
|
57 |
|
58 retval.resize (len); |
|
59 |
|
60 for (int i = 0; i < len; i++) |
|
61 { |
3505
|
62 str_llist_type *elt_dirs |
|
63 = ::octave_kpse_element_dirs (pv[i].c_str ()); |
1786
|
64 |
3415
|
65 if (elt_dirs) |
1786
|
66 { |
3415
|
67 str_llist_elt_type *dir; |
1786
|
68 |
3415
|
69 for (dir = *elt_dirs; dir; dir = STR_LLIST_NEXT (*dir)) |
1786
|
70 { |
3415
|
71 char *elt_dir = STR_LLIST (*dir); |
1786
|
72 |
3415
|
73 if (elt_dir) |
|
74 { |
|
75 if (count == nmax) |
|
76 nmax *= 2; |
1786
|
77 |
3415
|
78 retval.resize (nmax); |
|
79 |
|
80 retval[count++] = elt_dir; |
|
81 } |
1786
|
82 } |
|
83 } |
|
84 } |
|
85 |
|
86 retval.resize (count); |
|
87 } |
|
88 |
|
89 return retval; |
|
90 } |
|
91 |
3504
|
92 std::string |
|
93 dir_path::find_first (const std::string& nm) |
1786
|
94 { |
3504
|
95 std::string retval; |
1786
|
96 |
|
97 if (initialized) |
|
98 { |
3505
|
99 char *tmp = ::octave_kpse_path_search (p.c_str (), nm.c_str (), true); |
1786
|
100 if (tmp) |
|
101 { |
|
102 retval = tmp; |
|
103 free (tmp); |
|
104 } |
|
105 } |
|
106 |
|
107 return retval; |
|
108 } |
|
109 |
|
110 string_vector |
3504
|
111 dir_path::find_all (const std::string& nm) |
1786
|
112 { |
|
113 string_vector retval; |
|
114 |
3174
|
115 if (initialized) |
1786
|
116 { |
3505
|
117 char **tmp = ::octave_kpse_all_path_search (p.c_str (), nm.c_str ()); |
1786
|
118 |
3174
|
119 if (tmp) |
|
120 { |
|
121 int count = 0; |
3505
|
122 char **ptr = tmp; |
3174
|
123 while (*ptr++) |
|
124 count++; |
1786
|
125 |
3174
|
126 retval.resize (count); |
|
127 |
|
128 for (int i = 0; i < count; i++) |
|
129 retval[i] = tmp[i]; |
|
130 } |
1786
|
131 } |
|
132 |
|
133 return retval; |
|
134 } |
|
135 |
|
136 void |
3504
|
137 dir_path::set_program_name (const std::string& nm) |
3024
|
138 { |
3834
|
139 std::string selfautodir = octave_env::getenv ("SELFAUTODIR"); |
|
140 std::string selfautoloc = octave_env::getenv ("SELFAUTOLOC"); |
|
141 std::string selfautoparent = octave_env::getenv ("SELFAUTOPARENT"); |
|
142 |
3505
|
143 ::octave_kpse_set_progname (nm.c_str ()); |
3833
|
144 |
|
145 // Calling kpse_set_progname has the unfortunate side-effect of |
3834
|
146 // exporting the following variables. If they were empty when we |
|
147 // started, we make them empty again so that they will not interfere |
|
148 // with TeX if it is run as a subprocess of Octave (if they were set |
|
149 // before, we want to preserve their values). |
3833
|
150 // |
|
151 // XXX FIXME XXX -- is there a reasonable way to actually remove |
|
152 // them from the environment? |
|
153 |
3834
|
154 if (selfautodir.empty ()) |
|
155 octave_env::putenv ("SELFAUTODIR", ""); |
|
156 |
|
157 if (selfautoloc.empty ()) |
|
158 octave_env::putenv ("SELFAUTOLOC", ""); |
|
159 |
|
160 if (selfautoparent.empty ()) |
|
161 octave_env::putenv ("SELFAUTOPARENT", ""); |
3024
|
162 } |
|
163 |
|
164 void |
1786
|
165 dir_path::init (void) |
|
166 { |
3174
|
167 if (! octave_kpathsea_initialized) |
1786
|
168 { |
|
169 char *s = getenv ("KPATHSEA_DEBUG"); |
|
170 |
|
171 if (s) |
|
172 kpathsea_debug |= atoi (s); |
3174
|
173 |
|
174 octave_kpathsea_initialized = true; |
1786
|
175 } |
|
176 |
3196
|
177 char *t1 = 0; |
|
178 |
|
179 if (p_default.empty ()) |
3505
|
180 t1 = ::octave_kpse_path_expand (p_orig.c_str ()); |
3196
|
181 else |
3174
|
182 { |
3505
|
183 char *t2 |
|
184 = ::octave_kpse_expand_default (p_orig.c_str (), p_default.c_str ()); |
3196
|
185 |
3505
|
186 t1 = ::octave_kpse_path_expand (t2); |
3196
|
187 |
|
188 if (t2) |
|
189 free (t2); |
|
190 } |
|
191 |
|
192 if (t1) |
|
193 { |
|
194 p = t1; |
|
195 free (t1); |
3174
|
196 } |
|
197 else |
3504
|
198 p = std::string (); |
3174
|
199 |
1786
|
200 int count = 0; |
3505
|
201 char *path_elt = ::octave_kpse_path_element (p.c_str ()); |
1786
|
202 while (path_elt) |
|
203 { |
3505
|
204 path_elt = ::octave_kpse_path_element (0); |
1786
|
205 count++; |
|
206 } |
|
207 |
|
208 pv.resize (count); |
|
209 |
3505
|
210 path_elt = ::octave_kpse_path_element (p.c_str ()); |
1786
|
211 |
|
212 for (int i = 0; i < count; i++) |
|
213 { |
|
214 pv[i] = path_elt; |
3505
|
215 path_elt = ::octave_kpse_path_element (0); |
1786
|
216 } |
|
217 |
|
218 initialized = true; |
|
219 } |
|
220 |
|
221 /* |
|
222 ;;; Local Variables: *** |
|
223 ;;; mode: C++ *** |
|
224 ;;; End: *** |
|
225 */ |