aboutsummaryrefslogtreecommitdiff
blob: e52f1498424017c634bfa37fd70df033dce30167 (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
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
/*
 * Copyright 2005-2014 Gentoo Foundation
 * Distributed under the terms of the GNU General Public License v2
 *
 * Copyright 2005-2008 Ned Ludd        - <solar@gentoo.org>
 * Copyright 2005-2014 Mike Frysinger  - <vapier@gentoo.org>
 */

typedef enum { VER_ALPHA=0, VER_BETA, VER_PRE, VER_RC, VER_NORM, VER_P } atom_suffixes;
const char * const atom_suffixes_str[] = { "_alpha", "_beta", "_pre", "_rc", "_/*bogus*/", "_p" };

typedef struct {
	atom_suffixes suffix;
	uint64_t sint;
} atom_suffix;

const char * const atom_op_str[] = { "", ">", ">=", "=", "<=", "<", "~", "!", "!!", "*" };
typedef enum {
	/*    */ ATOM_OP_NONE = 0,
	/* >  */ ATOM_OP_NEWER,
	/* >= */ ATOM_OP_NEWER_EQUAL,
	/* =  */ ATOM_OP_EQUAL,
	/* <= */ ATOM_OP_OLDER_EQUAL,
	/* <  */ ATOM_OP_OLDER,
	/* ~  */ ATOM_OP_PV_EQUAL,
	/* !  */ ATOM_OP_BLOCK,
	/* !! */ ATOM_OP_BLOCK_HARD,
	/* *  */ ATOM_OP_STAR,
} atom_operator;

typedef struct {
	/* XXX: we don't provide PF ... */
	atom_operator pfx_op, sfx_op;
	char *CATEGORY;
	char *PN;
	unsigned int PR_int;
	char letter;
	atom_suffix *suffixes;
	char *PV, *PVR;
	char *P, *SLOT, *REPO;
} depend_atom;

#ifdef EBUG
static void
atom_print(const depend_atom *atom)
{
	if (atom->CATEGORY)
		printf("%s/", atom->CATEGORY);
	printf("%s", atom->P);
	if (atom->PR_int)
		printf("-r%i", atom->PR_int);
	if (atom->SLOT)
		printf(":%s", atom->SLOT);
	if (atom->REPO)
		printf("::%s", atom->REPO);
}
#endif

#ifdef _USE_CACHE
static depend_atom *_atom_cache = NULL;
static size_t _atom_cache_len = 0;
#endif

static depend_atom *
atom_explode(const char *atom)
{
	depend_atom *ret;
	char *ptr;
	size_t len, slen, idx, sidx;

	/* we allocate mem for atom struct and two strings (strlen(atom)).
	 * the first string is for CAT/PN/PV while the second is for PVR.
	 * PVR needs 3 extra bytes for possible implicit '-r0'. */
	slen = strlen(atom);
	len = sizeof(*ret) + (slen + 1) * sizeof(*atom) * 3 + 3;
#ifdef _USE_CACHE
	if (len <= _atom_cache_len) {
		ret = _atom_cache;
		memset(ret, 0x00, len);
	} else {
		free(_atom_cache);
		_atom_cache = ret = xzalloc(len);
		_atom_cache_len = len;
	}
#else
	ret = xzalloc(len);
#endif
	ptr = (char*)ret;
	ret->P = ptr + sizeof(*ret);
	ret->PVR = ret->P + slen + 1;
	ret->CATEGORY = ret->PVR + slen + 1 + 3;

	/* eat any prefix operators */
	switch (atom[0]) {
	case '>':
		++atom;
		if (atom[0] == '=') {
			++atom;
			ret->pfx_op = ATOM_OP_NEWER_EQUAL;
		} else
			ret->pfx_op = ATOM_OP_NEWER;
		break;
	case '=':
		++atom;
		ret->pfx_op = ATOM_OP_EQUAL;
		break;
	case '<':
		++atom;
		if (atom[0] == '=') {
			++atom;
			ret->pfx_op = ATOM_OP_OLDER_EQUAL;
		} else
			ret->pfx_op = ATOM_OP_OLDER;
		break;
	case '~':
		++atom;
		ret->pfx_op = ATOM_OP_PV_EQUAL;
		break;
	case '!':
		++atom;
		switch (atom[0]) {
		case '!':
			++atom;
			ret->pfx_op = ATOM_OP_BLOCK_HARD;
			break;
		case '>':
			++atom;
			if (atom[0] == '=') {
				++atom;
				ret->pfx_op = ATOM_OP_OLDER;
			} else
				ret->pfx_op = ATOM_OP_OLDER_EQUAL;
			break;
		case '<':
			++atom;
			if (atom[0] == '=') {
				++atom;
				ret->pfx_op = ATOM_OP_NEWER;
			} else
				ret->pfx_op = ATOM_OP_NEWER_EQUAL;
			break;
		default:
			ret->pfx_op = ATOM_OP_BLOCK;
			break;
		}
		break;
	}
	strcpy(ret->CATEGORY, atom);

	/* eat file name crap when given an (autocompleted) path */
	if ((ptr = strstr(ret->CATEGORY, ".ebuild")) != NULL)
		*ptr = '\0';

	/* chip off the trailing [::REPO] as needed */
	if ((ptr = strstr(ret->CATEGORY, "::")) != NULL) {
		ret->REPO = ptr + 2;
		*ptr = '\0';
	}

	/* chip off the trailing [:SLOT] as needed */
	if ((ptr = strrchr(ret->CATEGORY, ':')) != NULL) {
		ret->SLOT = ptr + 1;
		*ptr = '\0';
	}

	/* see if we have any suffix operators */
	if ((ptr = strrchr(ret->CATEGORY, '*')) != NULL) {
		/* make sure it's the last byte */
		if (ptr[1] == '\0') {
			ret->sfx_op = ATOM_OP_STAR;
			*ptr = '\0';
		}
	}

	/* break up the CATEGORY and PVR */
	if ((ptr = strrchr(ret->CATEGORY, '/')) != NULL) {
		ret->PN = ptr + 1;
		*ptr = '\0';
		/* eat extra crap in case it exists, this is a feature to allow
		 * /path/to/pkg.ebuild */
		if ((ptr = strrchr(ret->CATEGORY, '/')) != NULL)
			ret->CATEGORY = ptr + 1;
	} else {
		ret->PN = ret->CATEGORY;
		ret->CATEGORY = NULL;
	}

	/* CATEGORY should be all set here, PN contains everything up to
	 * SLOT, REPO or '*'
	 * PN must not end in a hyphen followed by anything matching version
	 * syntax, version syntax starts with a number, so "-[0-9]" is a
	 * separator from PN to PV* */

	ptr = ret->PN;
	while ((ptr = strchr(ptr, '-')) != NULL) {
		ptr++;
		if (*ptr >= '0' && *ptr <= '9')
			break;
	}

	if (ptr == NULL) {
		/* atom has no version, this is it */
		return ret;
	}
	ret->PV = ptr;

	/* find -r# */
	ptr = ret->PV + strlen(ret->PV) - 1;
	while (*ptr && ptr > ret->PV) {
		if (!isdigit(*ptr)) {
			if (ptr[0] == 'r' && ptr[-1] == '-') {
				ret->PR_int = atoi(ptr + 1);
				ptr[-1] = '\0';
			}
			break;
		}
		--ptr;
	}
	strcpy(ret->P, ret->PN);
	ret->PV[-1] = '\0';

	/* break out all the suffixes */
	sidx = 0;
	ret->suffixes = xrealloc(ret->suffixes, sizeof(atom_suffix) * (sidx + 1));
	ret->suffixes[sidx].sint = 0;
	ret->suffixes[sidx].suffix = VER_NORM;
	ptr = ret->PV + strlen(ret->PV) - 1;
	while (ptr-- > ret->PV) {
		if (*ptr != '_')
			continue;
		for (idx = 0; idx < ARRAY_SIZE(atom_suffixes_str); ++idx) {
			if (strncmp(ptr, atom_suffixes_str[idx],
						strlen(atom_suffixes_str[idx])))
				continue;

			ret->suffixes[sidx].sint =
				atoll(ptr + strlen(atom_suffixes_str[idx]));
			ret->suffixes[sidx].suffix = idx;

			++sidx;

			ret->suffixes = xrealloc(ret->suffixes,
					sizeof(atom_suffix) * (sidx + 1));
			ret->suffixes[sidx].sint = 0;
			ret->suffixes[sidx].suffix = VER_NORM;
			break;
		}
	}
	if (sidx)
		--sidx;
	for (idx = 0; idx < sidx; ++idx, --sidx) {
		atom_suffix t = ret->suffixes[sidx];
		ret->suffixes[sidx] = ret->suffixes[idx];
		ret->suffixes[idx] = t;
	}

	/* allow for 1 optional suffix letter, must be following a number */
	ptr = ret->PV + strlen(ret->PV);
	if (ptr[-1] >= 'a' && ptr[-1] <= 'z' &&
			ptr - 2 > ret->PV && ptr[-2] >= '0' && ptr[-2] <= '9')
	{
		ret->letter = ptr[-1];
		--ptr;
	}

	/* eat the trailing version number [-.0-9]+ */
	while (--ptr > ret->PV) {
		if (*ptr == '-') {
			*ptr = '\0';
			break;
		} else if (*ptr != '.' && !isdigit(*ptr))
			break;
	}

	ptr = stpcpy(ret->PVR, ret->PV);
	sprintf(ptr, "-r%i", ret->PR_int);

	return ret;
}

static void
atom_implode(depend_atom *atom)
{
	if (!atom)
		errf("Atom is empty !");
	free(atom->suffixes);
#ifndef _USE_CACHE
	free(atom);
#endif
}