1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/* variable.c: variable expansion.
Copyright 1993, 1994, 1995, 1996, 2008 Karl Berry.
Copyright 1997, 1999, 2001, 2002, 2005 Olaf Weber.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this library; if not, see <http://www.gnu.org/licenses/>. */
#include <kpathsea/config.h>
#include <kpathsea/c-ctype.h>
#include <kpathsea/cnf.h>
#include <kpathsea/fn.h>
#include <kpathsea/tilde.h>
#include <kpathsea/variable.h>
/* Here's the simple one, when a program just wants a value. */
string
kpse_var_value P1C(const_string, var)
{
string vtry, ret;
assert (kpse_program_name);
/* First look for VAR.progname. */
vtry = concat3 (var, ".", kpse_program_name);
ret = getenv (vtry);
free (vtry);
if (!ret || !*ret) {
/* Now look for VAR_progname. */
vtry = concat3 (var, "_", kpse_program_name);
ret = getenv (vtry);
free (vtry);
}
/* Just plain VAR. */
if (!ret || !*ret)
ret = getenv (var);
/* Not in the environment; check a config file. */
if (!ret || !*ret)
ret = kpse_cnf_get (var);
/* We have a value; do variable and tilde expansion. We want to use ~
in the cnf files, to adapt nicely to Windows and to avoid extra /'s
(see tilde.c), but we also want kpsewhich -var-value=foo to not
have any literal ~ characters, so our shell scripts don't have to
worry about doing the ~ expansion. */
if (ret) {
string tmp = kpse_var_expand (ret);
if (tmp != ret) {
free (ret);
}
ret = kpse_tilde_expand (tmp);
if (ret != tmp) {
free (tmp);
}
}
#ifdef KPSE_DEBUG
if (KPSE_DEBUG_P (KPSE_DEBUG_VARS))
DEBUGF2("variable: %s = %s\n", var, ret ? ret : "(nil)");
#endif
return ret;
}
/* We have to keep track of variables being expanded, otherwise
constructs like TEXINPUTS = $TEXINPUTS result in an infinite loop.
(Or indirectly recursive variables, etc.) Our simple solution is to
add to a list each time an expansion is started, and check the list
before expanding. */
typedef struct {
const_string var;
boolean expanding;
} expansion_type;
static expansion_type *expansions; /* The sole variable of this type. */
static unsigned expansion_len = 0;
static void
expanding P2C(const_string, var, boolean, xp)
{
unsigned e;
for (e = 0; e < expansion_len; e++) {
if (STREQ (expansions[e].var, var)) {
expansions[e].expanding = xp;
return;
}
}
/* New variable, add it to the list. */
expansion_len++;
XRETALLOC (expansions, expansion_len, expansion_type);
expansions[expansion_len - 1].var = xstrdup (var);
expansions[expansion_len - 1].expanding = xp;
}
/* Return whether VAR is currently being expanding. */
static boolean
expanding_p P1C(const_string, var)
{
unsigned e;
for (e = 0; e < expansion_len; e++) {
if (STREQ (expansions[e].var, var))
return expansions[e].expanding;
}
return false;
}
/* Append the result of value of `var' to EXPANSION, where `var' begins
at START and ends at END. If `var' is not set, do not complain.
This is a subroutine for the more complicated expansion function. */
static void
expand P3C(fn_type *, expansion, const_string, start, const_string, end)
{
string value;
unsigned len = end - start + 1;
string var = (string)xmalloc (len + 1);
strncpy (var, start, len);
var[len] = 0;
if (expanding_p (var)) {
WARNING1 ("kpathsea: variable `%s' references itself (eventually)", var);
} else {
string vtry = concat3 (var, "_", kpse_program_name);
/* Check for an environment variable. */
value = getenv (vtry);
free (vtry);
if (!value || !*value)
value = getenv (var);
/* If no envvar, check the config files. */
if (!value || !*value)
value = kpse_cnf_get (var);
if (value) {
expanding (var, true);
value = kpse_var_expand (value);
expanding (var, false);
fn_grow (expansion, value, strlen (value));
free (value);
}
free (var);
}
}
/* Can't think of when it would be useful to change these (and the
diagnostic messages assume them), but ... */
#ifndef IS_VAR_START /* starts all variable references */
#define IS_VAR_START(c) ((c) == '$')
#endif
#ifndef IS_VAR_CHAR /* variable name constituent */
#define IS_VAR_CHAR(c) (ISALNUM (c) || (c) == '_')
#endif
#ifndef IS_VAR_BEGIN_DELIMITER /* start delimited variable name (after $) */
#define IS_VAR_BEGIN_DELIMITER(c) ((c) == '{')
#endif
#ifndef IS_VAR_END_DELIMITER
#define IS_VAR_END_DELIMITER(c) ((c) == '}')
#endif
/* Maybe we should support some or all of the various shell ${...}
constructs, especially ${var-value}. */
string
kpse_var_expand P1C(const_string, src)
{
const_string s;
string ret;
fn_type expansion;
expansion = fn_init ();
/* Copy everything but variable constructs. */
for (s = src; *s; s++) {
if (IS_VAR_START (*s)) {
s++;
/* Three cases: `$VAR', `${VAR}', `$<anything-else>'. */
if (IS_VAR_CHAR (*s)) {
/* $V: collect name constituents, then expand. */
const_string var_end = s;
do {
var_end++;
} while (IS_VAR_CHAR (*var_end));
var_end--; /* had to go one past */
expand (&expansion, s, var_end);
s = var_end;
} else if (IS_VAR_BEGIN_DELIMITER (*s)) {
/* ${: scan ahead for matching delimiter, then expand. */
const_string var_end = ++s;
while (*var_end && !IS_VAR_END_DELIMITER (*var_end))
var_end++;
if (! *var_end) {
WARNING1 ("%s: No matching } for ${", src);
s = var_end - 1; /* will incr to null at top of loop */
} else {
expand (&expansion, s, var_end - 1);
s = var_end; /* will incr past } at top of loop*/
}
} else {
/* $<something-else>: error. */
WARNING2 ("%s: Unrecognized variable construct `$%c'", src, *s);
/* Just ignore those chars and keep going. */
}
} else
fn_1grow (&expansion, *s);
}
fn_1grow (&expansion, 0);
ret = FN_STRING (expansion);
return ret;
}
#ifdef TEST
static void
test_var (string test, string right_answer)
{
string result = kpse_var_expand (test);
printf ("expansion of `%s'\t=> %s", test, result);
if (!STREQ (result, right_answer))
printf (" [should be `%s']", right_answer);
putchar ('\n');
}
int
main ()
{
test_var ("a", "a");
test_var ("$foo", "");
test_var ("a$foo", "a");
test_var ("$foo a", " a");
test_var ("a$foo b", "a b");
xputenv ("FOO", "foo value");
test_var ("a$FOO", "afoo value");
xputenv ("Dollar", "$");
test_var ("$Dollar a", "$ a");
test_var ("a${FOO}b", "afoo valueb");
test_var ("a${}b", "ab");
test_var ("$$", ""); /* and error */
test_var ("a${oops", "a"); /* and error */
return 0;
}
#endif /* TEST */
/*
Local variables:
standalone-compile-command: "gcc -g -I. -I.. -DTEST variable.c kpathsea.a"
End:
*/
|