File: envvar.c

package info (click to toggle)
fvwm 1:2.6.8-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,804 kB
  • sloc: ansic: 145,770; xml: 17,093; perl: 7,302; sh: 4,921; makefile: 1,094; yacc: 688; lex: 187; sed: 11
file content (489 lines) | stat: -rw-r--r-- 12,297 bytes parent folder | download
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* -*-c-*- */
/* This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see: <http://www.gnu.org/licenses/>
 */

/*
 *  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
 *
 *  UPDATED         migo - 21/Jun/1999 - added getFirstEnv, some changes
 *
 */

/* ---------------------------- included header files ---------------------- */

#include "config.h"
#include <stdio.h>
#include <ctype.h>

#include "fvwmlib.h"
#include "envvar.h"

/* ---------------------------- local definitions -------------------------- */

#ifdef HAVE_UNSETENV
#define FHaveUnsetenv 1
#else
#define unsetenv(x) do { } while (0)
#define FHaveUnsetenv 0
#endif

/* ---------------------------- local macros ------------------------------- */

#define ENV_LIST_INC 10
#ifndef NULL
#define NULL 0
#endif

/* ---------------------------- imports ------------------------------------ */

/* ---------------------------- included code files ------------------------ */

/* ---------------------------- local types -------------------------------- */

typedef struct
{
	char *var;
	char *env;
} env_list_item;

/* ---------------------------- forward declarations ----------------------- */

/* ---------------------------- local variables ---------------------------- */

/* ---------------------------- exported variables (globals) --------------- */

/* ---------------------------- local functions ---------------------------- */

/*-------------------------------------------------------------------------
 *
 *  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-- > 0)
		*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;
}

/*-------------------------------------------------------------------------
 *
 *  FUNCTION      Look up environment variable.
 *
 *  INPUT         name    name of environment variable to look up. This
 *                        may include $ and { }.
 *                len     length for environment variable name (0 - ignore).
 *
 *  RETURNS       The variable contents, or "" if not found.
 *
 */
static const char *getEnv(const char *name, int len)
{
	static char *empty = "";
	char   *ret = NULL, *tmp, *p, *p2;

	if ((tmp = safestrdup(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 (len > 0 && len < strlen(tmp)) tmp[len] = '\0';
	if ((ret = getenv(p)) == NULL)
		ret = empty;
	free(tmp);
	return ret;
}

/* ---------------------------- interface functions ------------------------ */

/*
 *  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;
	const char *env;
	int   len, ret = 0;

	s2 = s;
	while ((var = findEnvVar(s2, &len)) != NULL) {
		++ret;
		env = getEnv(var, len);
		strDel(s, var - s, len);
		strIns(s, env, var - s, maxstrlen);
		s2 = var + strlen(env);
	}
	return ret;
}

/*
 *  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;
	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) {
		env = getEnv(var, len);
		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.
	 */
	strcpy(ret, s);
	envExpand(ret, bufflen - extra);

	return ret;
}

/*
 *  FUNCTION      Search for the first environment variable and return
 *                its contents and coordinates in the given string.
 *
 *  INPUT         s       the string to scan.
 *                        may include $ and { } that introduce variable.
 *
 *  OUTPUT        beg     index in the string of matching $.
 *                end     index in the string, first after matching var.
 *
 *  RETURNS       The variable contents; "" if env variable has legal name,
 *                but does not exist; or NULL if no env variables found.
 *                Returned constant string must not be deallocated.
 *
 *  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.
 *                If nothing is found returns NULL and sets beg and end to 0.
 *
 *  EXAMPLE       getFirstEnv("echo $HOME/.fvwm/config", &beg, &end)
 *                returns "/home/username" and beg=5, end=10.
 *
 */
const char* getFirstEnv(const char *s, int *beg, int *end)
{
	char *var;
	const char *env;
	int len;

	*beg = *end = 0;
	if ((var = findEnvVar(s, &len)) == NULL) return NULL;
	env = getEnv(var, len);

	*beg = var - s;
	*end = *beg + len;

	return env;
}

/* If env is NULL, var is removed from the environment list */
static void add_to_envlist(char *var, char *env)
{
	static env_list_item *env_list = NULL;
	static unsigned int env_len = 0;
	static unsigned int env_len_allocated = 0;
	unsigned int i;

	/* find string in list */
	if (env_list && env_len)
	{
		for (i = 0; i < env_len; i++)
		{
			if (strcmp(var, env_list[i].var) != 0)
			{
				continue;
			}
			/* found it - replace old string */
			free(env_list[i].var);
			free(env_list[i].env);
			if (env == NULL)
			{
				/* delete */
				env_len--;
				env_list[i].var =
					env_list[env_len].var;
				env_list[i].env =
					env_list[env_len].env;
			}
			else
			{
				/* replace */
				env_list[i].var = var;
				env_list[i].env = env;
			}

			return;
		}
	}
	if (env == NULL)
	{
		return;
	}
	/* not found */
	if (env_list == NULL)
	{
		/* list is still empty */
		env_len_allocated = ENV_LIST_INC;
		env_list = (env_list_item *)safecalloc(
			sizeof(env_list_item), env_len_allocated);
	}
	else if (env_len >= env_len_allocated && env != NULL)
	{
		/* need more memory */
		env_len_allocated = env_len + ENV_LIST_INC;
		env_list = (env_list_item *)saferealloc(
			(void *)env_list, (env_len_allocated) *
			sizeof(env_list_item));
	}
	env_list[env_len].var = var;
	env_list[env_len].env = env;
	env_len++;

	return;
}

/* This function keeps a list of all strings that were set in the environment.
 * If a variable is written again, the old memory is freed.  This function
 * should be called instead of putenv().
 *
 *   var - environement variable name
 *   env - environment string ("variable=value")
 *
 * Both arguments are copied internally and should be freed after calling this
 * function.
 */
void flib_putenv(char *var, char *env)
{
	char *s;

	s = safestrdup(var);
	var = s;
	s = safestrdup(env);
	env = s;
	putenv(env);
	add_to_envlist(var, env);

	return;
}

void flib_unsetenv(const char *name)
{
	if (FHaveUnsetenv)
	{
		unsetenv(name);
	}
	else
	{
		int rc;

		/* try putenv without '=' */
		rc = putenv((char *)name);
		if (rc == 0 || getenv(name) != NULL)
		{
			/* failed, write empty string */
			flib_putenv((char *)name, "");
			return;
		}
	}
	add_to_envlist((char *)name, NULL);

	return;
}