summaryrefslogtreecommitdiff
path: root/usr/src/lib/libpkg/common/mappath.c
blob: b0ae99d827aad54eb45aada7b2360cf6292fec93 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */



#include <limits.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>

/* 0 = both upper and lower case */
/* 1 = initial lower case only (build variables) */
/* 2 = initial upper case only (install variables) */
#define	mode(flag, pt)	(!flag || ((flag == 1) && islower(pt[1])) || \
			((flag == 2) && isupper(pt[1])))

/*
 * For next and last functions below, values indicate whether resolution
 * was possible.
 *
 *	0 = all OK - the variable resolved within the established parameters
 *		or it wasn't time for the variable to bind.
 *	1 = parameter did not resolve because there was no value in the
 *		environment or because it was a build variable at install
 *		time.
 */

/*
 * This gets a raw path which may contain shell variables and returns in path
 * a pathname with all appropriate parameters resolved. If it comes in
 * relative, it goes out relative.
 */
int
mappath(int flag, char *path)
{
	char buffer[PATH_MAX];
	char varname[64];
	char *npt, *pt, *pt2, *copy;
	char *token;
	int retvalue = 0;

	copy = buffer;

	/*
	 * For each "/" separated token. If the token contains an environment
	 * variable, then evaluate the variable and insert it into path.
	 */
	for (pt = path; *pt; /* void */) {
		/*
		 * If this is a token and it's an environment variable
		 * properly situated in the path...
		 */
		if ((*pt == '$') && isalpha(pt[1]) &&
		    ((pt == path) || (pt[-1] == '/'))) {
			/* ... and it's the right time to evaluate it... */
			if (mode(flag, pt)) {
				/* replace the parameter with its value. */
				pt2 = varname;
				for (npt = pt+1; *npt && (*npt != '/');
				    /* void */)
					*pt2++ = *npt++;
				*pt2 = '\0';
				/*
				 * At this point EVERY token should evaluate
				 * to a value. If it doesn't, there's an
				 * error.
				 */
				if ((token = getenv(varname)) != NULL &&
				    *token != NULL) {
					/* copy in parameter value */
					while (*token)
						*copy++ = *token++;
					pt = npt;
				} else {
					retvalue = 1;
					*copy++ = *pt++;
				}
			/*
			 * If evaluate time is wrong, determine of this is an
			 * error.
			 */
			} else {
				if (flag == 2) {	/* install-time. */
					/*
					 * ALL variables MUST evaluate at
					 * install time.
					 */
					*copy++ = *pt++;
					retvalue = 1;
				} else if (flag == 1 &&	/* build-time */
				    islower(pt[1])) {
					/*
					 * All build-time variables must
					 * evaluate at build time.
					 */
					retvalue = 1;
					*copy++ = *pt++;
				} else	/* no problem. */
					*copy++ = *pt++;
			}
		/*
		 * If it's a separator, copy it over to the target buffer and
		 * move to the start of the next token.
		 */
		} else if (*pt == '/') {
			while (pt[1] == '/')
				pt++;
			if ((pt[1] == '\0') && (pt > path))
				break;
			*copy++ = *pt++;
		/*
		 * If we're in the middle of a non-parametric token, copy
		 * that character over and try the next character.
		 */
		} else
			*copy++ = *pt++;
	}
	*copy = '\0';
	(void) strcpy(path, buffer);
	return (retvalue);
}

/*
 * This function resolves the path into an absolute path referred to
 * an install root of ir.
 */
void
basepath(char *path, char *basedir, char *ir)
{
	char buffer[PATH_MAX];

	/* For a relative path, prepend the basedir */
	if (*path != '/') {
		(void) strcpy(buffer, path);
		if (ir && *ir) {
			while (*ir)
				*path++ = *ir++;
			if (path[-1] == '/')
				path--;
		}
		if (basedir && *basedir) {
			if (ir && *ir && *basedir != '/')
				*path++ = '/';
			while (*basedir)
				*path++ = *basedir++;
			if (path[-1] == '/')
				path--;
		}
		*path++ = '/';
		(void) strcpy(path, buffer);

	/* For an absolute path, just prepend the install root */
	} else {
		if (ir && *ir) {
			(void) strcpy(buffer, path);
			while (*ir)
				*path++ = *ir++;
			if (path[-1] == '/')
				path--;
			(void) strcpy(path, buffer);
		}
	}
}

/*
 * Evaluate varname and return with environment variables resolved.
 * NOTE: This assumes that varname is a buffer long enough to hold the
 * evaluated string.
 */
int
mapvar(int flag, char *varname)
{
	char	*token;
	int retvalue = 0;

	/* If its a parametric entry beginning with an alpha character. */
	if (*varname == '$' && isalpha(varname[1])) {
		/* ...and it's the right time to evaluate it... */
		if (mode(flag, varname)) {
			/*
			 * then it MUST be possible to evaluate it. If not,
			 * there's an error.
			 */
			if (((token = getenv(&varname[1])) != NULL) &&
			    *token) {
				/* copy token into varname */
				while (*token)
					*varname++ = *token++;
				*varname = '\0';
			} else
				retvalue = 1;
		} else {
			if (flag == 2) /* install-time. */
				/*
				 * ALL variables MUST evaluate at install
				 * time.
				 */
				retvalue = 1;
			else if (flag == 1 &&	/* build-time */
			    islower(varname[1]))
				/*
				 * all build-time variables must evaluate at
				 * build time.
				 */
				retvalue = 1;
		}
	}
	return (retvalue);
}