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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
/**************************************************************************
*
* FILE envvar.c
* MODULE OF fvwm
*
* DESCRIPTION Routines to expand environment-variables into strings.
* Will understand both $ENV and ${ENV} -type variables.
*
* WRITTEN BY Sverre H. Huseby
* sverrehu@ifi.uio.no
*
* CREATED 1995/10/3
*
**************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "fvwmlib.h"
#ifndef NULL
#define NULL 0
#endif
/**************************************************************************
* *
* P R I V A T E D A T A *
* *
**************************************************************************/
/**************************************************************************
* *
* P R I V A T E F U N C T I O N S *
* *
**************************************************************************/
/*-------------------------------------------------------------------------
*
* NAME strDel
*
* FUNCTION Delete characters from a string.
*
* INPUT s the string to delete characters from.
* idx index of first character to delete.
* n number of characters to delete.
*
* OUTPUT s string with characters deleted.
*
* DESCRIPTION Deletes characters from a string by moving following
* characters back.
*
*/
static void strDel(char *s, int idx, int n)
{
int l;
char *p;
if (idx >= (l = strlen(s)))
return;
if (idx + n > l)
n = l - idx;
s += idx;
p = s + n;
do {
*s++ = *p;
} while (*p++);
}
/*-------------------------------------------------------------------------
*
* NAME strIns
*
* FUNCTION Insert a string into a string.
*
* INPUT s the string to insert into.
* ins the string to insert.
* idx index of where to insert the string.
* maxstrlen max length of s, including '\0'.
*
* OUTPUT s string with characters inserted.
*
* DESCRIPTION The insertion will be done even if the string gets to
* long, but characters will be sacrificed at the end of s.
* The string is always '\0'-terminated.
*
*/
static void strIns(char *s, const char *ins, int idx, int maxstrlen)
{
int l, li, move;
char *p1, *p2;
if (idx > (l = strlen(s)))
idx = l;
li = strlen(ins);
move = l - idx + 1; /* include '\0' in move */
p1 = s + l;
p2 = p1 + li;
while (p2 >= s + maxstrlen) {
--p1;
--p2;
--move;
}
while (move-- > 0)
*p2-- = *p1--;
p1 = s + idx;
if (idx + li >= maxstrlen)
li = maxstrlen - idx - 1;
while (li--)
*p1++ = *ins++;
s[maxstrlen - 1] = '\0';
}
/*-------------------------------------------------------------------------
*
* NAME findEnvVar
*
* FUNCTION Find first environment variable in a string.
*
* INPUT s the string to scan.
*
* OUTPUT len length of variable, including $ and { }.
*
* RETURNS Pointer to the $ that introduces the variable, or NULL
* if no variable is found.
*
* DESCRIPTION Searches for matches like $NAME and ${NAME}, where NAME is
* a sequence of characters, digits and underscores, of which
* the first can not be a digit.
*
* NOTE This function will only return `legal' variables. There
* may be $'s in the string that are not followed by what
* is considered a legal variable name introducer. Such
* occurrences are skipped.
*
*/
static char *findEnvVar(const char *s, int *len)
{
int brace = 0;
char *ret = NULL;
const char *next;
if (!s)
return NULL;
while (*s) {
next = s + 1;
if (*s == '$' && (isalpha(*next) || *next == '_' || *next == '{')) {
ret = (char *) s++;
if (*s == '{') {
brace = 1;
++s;
}
while (*s && (isalnum(*s) || *s == '_'))
++s;
*len = s - ret;
if (brace) {
if (*s == '}') {
++*len;
break;
}
ret = NULL;
} else
break;
}
++s;
}
return ret;
}
/*-------------------------------------------------------------------------
*
* NAME getEnv
*
* FUNCTION Look up environment variable.
*
* INPUT name name of environment variable to look up. This
* may include $ and { }.
*
* RETURNS The variable contents, or "" if not found.
*
*/
static const char *getEnv(const char *name)
{
static char *empty = "";
char *ret, *tmp, *p, *p2;
if ((tmp = strdup(name)) == NULL)
return empty; /* better than no test at all. */
p = tmp;
if (*p == '$')
++p;
if (*p == '{') {
++p;
if ((p2 = strchr(p, '}')) != NULL)
*p2 = '\0';
}
if ((ret = getenv(p)) == NULL)
ret = empty;
free(tmp);
return ret;
}
/**************************************************************************
* *
* P U B L I C F U N C T I O N S *
* *
**************************************************************************/
/*-------------------------------------------------------------------------
*
* NAME envExpand
*
* FUNCTION Expand environment variables in a string.
*
* SYNOPSIS #include "envvar.h"
* int envExpand(char *s, int maxstrlen);
*
* INPUT s string to expand environment variables in.
* maxstrlen max length of string, including '\0'.
*
* OUTPUT s the string with environment variables expanded.
*
* RETURNS Number of changes done.
*
* NOTES A non-existing variable is substituted with the empty
* string.
*
*/
int envExpand(char *s, int maxstrlen)
{
char *var, *s2, save;
const char *env;
int len, ret = 0;
s2 = s;
while ((var = findEnvVar(s2, &len)) != NULL) {
++ret;
save = var[len];
var[len] = '\0';
env = getEnv(var);
var[len] = save;
strDel(s, var - s, len);
strIns(s, env, var - s, maxstrlen);
s2 = var + strlen(env);
}
return ret;
}
/*-------------------------------------------------------------------------
*
* NAME envDupExpand
*
* FUNCTION Expand environment variables into a new string.
*
* SYNOPSIS #include "envvar.h"
* char *envDupExpand(const char *s, int extra);
*
* INPUT s string to expand environment variables in.
* extra number of extra bytes to allocate in the
* string, in addition to the string contents
* and the terminating '\0'.
*
* RETURNS A dynamically allocated string with environment
* variables expanded.
* Use free() to deallocate the buffer when it is no
* longer needed.
* NULL is returned if there is not enough memory.
*
* NOTES A non-existing variable is substituted with the empty
* string.
*
*/
char *envDupExpand(const char *s, int extra)
{
char *var, *ret, save;
const char *env, *s2;
int len, slen, elen, bufflen;
/*
* calculate length needed.
*/
s2 = s;
slen = strlen(s);
bufflen = slen + 1 + extra;
while ((var = findEnvVar(s2, &len)) != NULL) {
save = var[len];
var[len] = '\0';
env = getEnv(var);
var[len] = save;
elen = strlen(env);
/* need to make a buffer the maximum possible size, else we
* may get trouble while expanding. */
bufflen += len > elen ? len : elen;
s2 = var + len;
}
if (bufflen < slen + 1)
bufflen = slen + 1;
ret = safemalloc(bufflen);
/*
* now do the real expansion.
*/
strlcpy(ret, s,bufflen);
envExpand(ret, bufflen - extra);
return ret;
}
|