aboutsummaryrefslogtreecommitdiff
path: root/src/environ.c
blob: 20e0808b4536b67ef0ea16f57fa3f49aef1dd90f (plain)
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
/* This file is part of Jumper
   Copyright (C) 2013, 2017 Sergey Poznyakoff
  
   Jumper 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 3, or (at your option)
   any later version.
  
   Jumper 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 Jumper.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "jumper.h"

extern char **environ;    /* Environment */

#define DEBUG_ENVIRON(l,env) do {				\
	if (config.debug_level >= (l)) {				\
		diag(LOG_DEBUG, "environment: ");		\
		for (i = 0; (env)[i]; i++)			\
			diag(LOG_DEBUG, "%s ", (env)[i]);	\
		diag(LOG_DEBUG, "\n");				\
	}							\
	} while (0)

static char *
find_env(const char *name, int val)
{
	if (environ) {
		int nlen = strcspn(name, "+=");
		int i;

		for (i = 0; environ[i]; i++) {
			size_t elen = strcspn(environ[i], "=");
			if (elen == nlen &&
			    memcmp(name, environ[i], nlen) == 0)
				return val ? environ[i] + elen + 1 : environ[i];
		}
	}
	return NULL;
}

static int
locate_unset(char **env, const char *name)
{
	int i;
	int nlen = strcspn(name, "=");

	for (i = 0; env[i]; i++) {
		if (env[i][0] == '-') {
			size_t elen = strcspn (env[i] + 1, "=");
			if (elen == nlen &&
			    memcmp (name, env[i] + 1, nlen) == 0) {
				if (env[i][nlen + 1])
					return strcmp(name + nlen,
						      env[i] + 1 + nlen) == 0;
				else
					return 1;
			}
		}
	}
	return 0;
}

static char *
env_concat(const char *name, size_t namelen, const char *a, const char *b)
{
	char *res;
	size_t len;
        
	if (a && b) {
		res = emalloc(namelen + 1 + strlen(a) + strlen(b) + 1);
		strcpy(res + namelen + 1, a);
		strcat(res, b);
	} else if (a) {
		len = strlen(a);
		if (ispunct(a[len-1]))
			len--;
		res = emalloc(namelen + 1 + len + 1);
		memcpy(res + namelen + 1, a, len);
		res[namelen + 1 + len] = 0;
	}
	else { /* if (a == NULL) */
		if (ispunct(b[0]))
			b++;
		len = strlen(b);
		res = emalloc(namelen + 1 + len + 1);
		strcpy(res + namelen + 1, b);
	}
	memcpy(res, name, namelen);
	res[namelen] = '=';
	return res;
}

static char *defenv[] = {
	"JUMPER=${program}",
	"JUMPER_VERSION=" PACKAGE_VERSION,
	"JUMPER_LOCUS=${file}:${line}",
	"JUMPER_EVENT=${event}",
	"JUMPER_PID=${pid}",
	NULL,
};

char **
environ_setup(char **hint, char **kve)
{
	char *empty[1] = { NULL };
	char **old_env = environ;
	char **new_env;
	char **addenv = defenv;
	char *var;
	size_t count, i, n;
	struct wordsplit ws;
	int wsflags = WRDSF_NOCMD | WRDSF_QUOTE | WRDSF_NOSPLIT |
		      WRDSF_ENV | WRDSF_ENV_KV;

	ws.ws_env = (const char **) kve;

	if (!hint)
		hint = empty;
	else if (strcmp(hint[0], "-") == 0 || strcmp(hint[0], "--") == 0) {
		old_env = NULL;
		if (hint[0][1] == '-')
			addenv = empty;
		hint++;
        }
	
	/* Count new environment size */
	count = 0;
	if (old_env)
		for (i = 0; old_env[i]; i++)
			count++;

	for (i = 0; addenv[i]; i++)
		count++;
	
	for (i = 0; hint[i]; i++)
		count++;
	

	/* Allocate new environment. */
	new_env = ecalloc(count + 1, sizeof new_env[0]);
  
	/* Populate the environment. */
	n = 0;
  
	if (old_env)
		for (i = 0; old_env[i]; i++) {
			if (!locate_unset(hint, old_env[i]))
				new_env[n++] = old_env[i];
		}

	for (i = 0; addenv[i]; i++)
		if (!locate_unset(hint, addenv[i])) {
			if (wordsplit(addenv[i], &ws, wsflags)) {
				diag(LOG_CRIT, "wordsplit: %s",
				     wordsplit_strerror(&ws));
				_exit(127);
			}
			wsflags |= WRDSF_REUSE;
			new_env[n++] = estrdup(ws.ws_wordv[0]);
		}
		
	for (i = 0; hint[i]; i++) {
		char *p;

		if (hint[i][0] == '-') {
			/* Skip unset directives. */
			continue;
		}

		if (wordsplit(hint[i], &ws, wsflags)) {
			diag(LOG_CRIT, "wordsplit: %s",
			     wordsplit_strerror(&ws));
			_exit(127);
		}
		wsflags |= WRDSF_REUSE;
		var = ws.ws_wordv[0];
		
		if ((p = strchr(var, '='))) {
			if (p == var)
				continue; /* Ignore erroneous entry */
			if (p[-1] == '+') 
				new_env[n++] = env_concat(var,
							  p - var - 1,
							  find_env(var, 1),
							  p + 1);
			else if (p[1] == '+')
				new_env[n++] = env_concat(var,
							  p - var,
							  p + 2,
							  find_env(var, 1));
			else
				new_env[n++] = estrdup(var);
		} else {
			p = find_env(var, 0);
			if (p)
				new_env[n++] = p;
		}
	}
	new_env[n] = NULL;

	if (wsflags & WRDSF_REUSE)
		wordsplit_free(&ws);
	return new_env;
}

int
envcmp(char **a, char **b)
{
	if (!a)
		return !!b;
	else if (!b)
		return !!a;
	for (; *a && *b && strcmp(*a, *b) == 0; a++, b++);
	if (!*a && !*b)
		return 0;
	return 1;
}

void
envfree(char **a)
{
	int i;
	
	if (!a)
		return;
	for (i = 0; a[i]; i++)
		free(a[i]);
	free(a);
}

Return to:

Send suggestions and report system problems to the System administrator.