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 { |
3505
|
139 ::octave_kpse_set_progname (nm.c_str ()); |
3833
|
140 |
|
141 // Calling kpse_set_progname has the unfortunate side-effect of |
|
142 // exporting the following variables. We make them empty here so |
|
143 // that they will not interfere with TeX, if it is run as a |
|
144 // subprocess of Octave. |
|
145 // |
|
146 // XXX FIXME XXX -- is there a reasonable way to actually remove |
|
147 // them from the environment? |
|
148 |
|
149 octave_env::putenv ("SELFAUTOLOC", ""); |
|
150 octave_env::putenv ("SELFAUTODIR", ""); |
|
151 octave_env::putenv ("SELFAUTOPARENT", ""); |
|
152 octave_env::putenv ("TEXMFDBS", ""); |
3024
|
153 } |
|
154 |
|
155 void |
1786
|
156 dir_path::init (void) |
|
157 { |
3174
|
158 if (! octave_kpathsea_initialized) |
1786
|
159 { |
|
160 char *s = getenv ("KPATHSEA_DEBUG"); |
|
161 |
|
162 if (s) |
|
163 kpathsea_debug |= atoi (s); |
3174
|
164 |
|
165 octave_kpathsea_initialized = true; |
1786
|
166 } |
|
167 |
3505
|
168 ::octave_kpse_clear_dir_cache (); |
3185
|
169 |
3196
|
170 char *t1 = 0; |
|
171 |
|
172 if (p_default.empty ()) |
3505
|
173 t1 = ::octave_kpse_path_expand (p_orig.c_str ()); |
3196
|
174 else |
3174
|
175 { |
3505
|
176 char *t2 |
|
177 = ::octave_kpse_expand_default (p_orig.c_str (), p_default.c_str ()); |
3196
|
178 |
3505
|
179 t1 = ::octave_kpse_path_expand (t2); |
3196
|
180 |
|
181 if (t2) |
|
182 free (t2); |
|
183 } |
|
184 |
|
185 if (t1) |
|
186 { |
|
187 p = t1; |
|
188 free (t1); |
3174
|
189 } |
|
190 else |
3504
|
191 p = std::string (); |
3174
|
192 |
1786
|
193 int count = 0; |
3505
|
194 char *path_elt = ::octave_kpse_path_element (p.c_str ()); |
1786
|
195 while (path_elt) |
|
196 { |
3505
|
197 path_elt = ::octave_kpse_path_element (0); |
1786
|
198 count++; |
|
199 } |
|
200 |
|
201 pv.resize (count); |
|
202 |
3505
|
203 path_elt = ::octave_kpse_path_element (p.c_str ()); |
1786
|
204 |
|
205 for (int i = 0; i < count; i++) |
|
206 { |
|
207 pv[i] = path_elt; |
3505
|
208 path_elt = ::octave_kpse_path_element (0); |
1786
|
209 } |
|
210 |
|
211 initialized = true; |
|
212 } |
|
213 |
|
214 /* |
|
215 ;;; Local Variables: *** |
|
216 ;;; mode: C++ *** |
|
217 ;;; End: *** |
|
218 */ |