-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcpv.c
More file actions
277 lines (239 loc) · 6.98 KB
/
cpv.c
File metadata and controls
277 lines (239 loc) · 6.98 KB
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
#include "elib.h"
#define cpv_error(cpv_ptr, code) \
do { \
set_ebuild_errno(code); \
cpv_free(cpv_ptr); \
return NULL; \
} while(0)
void cpv_print(const CPV *cpv)
{
if (!cpv) {
printf("NULL\n");
return;
}
printf("P: %s\n", cpv->P);
printf("PN: %s\n", cpv->PN);
printf("PV: %s\n", cpv->PV);
printf("PR: %lld\n", cpv->PR_int);
printf("PVR: %s\n", cpv->PVR);
printf("PF: %s\n", cpv->PF);
printf("CATEGORY: %s\n", cpv->CATEGORY);
printf("letter: %c\n", cpv->letter);
printf("suffixes: ");
int i;
for (i = 0; cpv->suffixes[i].suffix != SUF_NORM; ++i) {
printf("%s", version_suffixes_str[cpv->suffixes[i].suffix]);
printf("%lu ", cpv->suffixes[i].val);
}
printf("\n");
}
static CPV *cpv_alloc_versioned(const char *cpv_string)
{
CPV *ret;
char *ptr, *tmp_ptr, *end_ptr;
size_t m_len, cpv_len, cpv_string_len;
int id, sid, sid_len;
ebuild_errno = E_OK;
// CPV + P + PF + PVR + (CATEGORY,PN,PV,PR)
cpv_len = sizeof(CPV);
cpv_string_len = strlen(cpv_string) + 1;
m_len = cpv_len + cpv_string_len * 4;
ret = malloc(m_len);
if (ret == NULL)
cpv_error(ret, E_NOMEM);
memset(ret, 0, m_len);
ptr = (char*)ret;
ret->P = ptr + cpv_len;
ret->PF = ret->P + cpv_string_len;
ret->PVR = ret->PF + cpv_string_len;
ret->CATEGORY = ret->PVR + cpv_string_len;
strcpy(ret->CATEGORY, cpv_string);
// category
ptr = ret->CATEGORY;
end_ptr = ptr + cpv_string_len - 2;
if (INVALID_FIRST_CHAR(*ptr))
cpv_error(ret, E_INVALID_CATEGORY_FIRST_CHAR);
while (*++ptr != '/')
if (!VALID_CHAR(*ptr))
cpv_error(ret, E_INVALID_CATEGORY);
*ptr = '\0';
ret->PN = ptr + 1;
if (INVALID_FIRST_CHAR(*(ret->PN)))
cpv_error(ret, E_INVALID_PN_FIRST_CHAR);
strcpy(ret->PF, ret->PN);
tmp_ptr = NULL;
ptr = end_ptr;
ret->PV = end_ptr + 1;
ret->PR_int = 0;
// version
while (ptr > ret->PN) {
if (ptr[0] == '-' && isdigit(ptr[1])) {
end_ptr = ptr;
ret->PV = &ptr[1];
break;
}
ptr--;
}
if (!isvalid_version(ret->PV))
cpv_error(ret, E_INVALID_VERSION);
else {
// revision
if (ptr = strchr(ret->PV, '-')) {
ret->PR_int = atoll(&ptr[2]);
if (ret->PR_int) {
strcpy(ret->PVR, ret->PV);
ptr[0] = '\0';
} else {
ptr[0] = '\0';
strcpy(ret->PVR, ret->PV);
}
tmp_ptr = ptr - 1;
} else
strcpy(ret->PVR, ret->PV);
strcpy(ret->P, ret->PN);
*end_ptr = '\0';
}
ptr = ret->PN;
while (*++ptr)
if (!VALID_CHAR(*ptr))
cpv_error(ret, E_INVALID_PN);
// pkgname shouldn't end with a hyphen followed by a valid version
else if (ptr[0] == '-' && isdigit(ptr[1]) && isvalid_version(&ptr[1]))
cpv_error(ret, E_INVALID_PN_VERSIONED_SUF);
// optional version letter
if (ptr = strchr(ret->PV, '_')) {
if (isalpha(ptr[-1]))
ret->letter = ptr[-1];
} else if (tmp_ptr) {
if (isalpha(*tmp_ptr))
ret->letter = *tmp_ptr;
} else if ((tmp_ptr = &ret->PV[strlen(ret->PV)-1]) && (isalpha(*tmp_ptr)))
ret->letter = *tmp_ptr;
// suffixes
id = 0;
ret->suffixes = realloc(ret->suffixes, sizeof(suffix_ver) * (id + 1));
if (ret->suffixes == NULL)
cpv_error(ret, E_NOMEM);
ret->suffixes[id].suffix = SUF_NORM;
ret->suffixes[id].val = 0;
while (ptr && *ptr) {
if (!(tmp_ptr = strchr(&ptr[1], '_')))
tmp_ptr = ptr + strlen(ptr);
sid = getsuffix(&ptr[1]);
ret->suffixes[id].suffix = sid;
sid_len = strlen(version_suffixes_str[sid]);
ptr += sid_len;
char num[tmp_ptr - ptr];
strncpy(num, &ptr[1], tmp_ptr - ptr - 1);
num[tmp_ptr - ptr] = '\0';
ret->suffixes[id].val = atoll(num);
id++;
ret->suffixes = realloc(ret->suffixes, sizeof(suffix_ver) * (id + 1));
if (ret->suffixes == NULL)
cpv_error(ret, E_NOMEM);
ret->suffixes[id].suffix = SUF_NORM;
ret->suffixes[id].val = 0;
ptr = tmp_ptr;
}
return ret;
}
static CPV *cpv_alloc_unversioned(const char *cpv_string)
{
CPV *ret;
char *ptr, *tmp_ptr;
size_t m_len, cpv_len, cpv_string_len;
ebuild_errno = E_OK;
// CPV + PF + (CATEGORY,PN)
cpv_len = sizeof(CPV);
cpv_string_len = strlen(cpv_string) + 1;
m_len = cpv_len + cpv_string_len * 2;
ret = malloc(m_len);
if (ret == NULL)
cpv_error(ret, E_NOMEM);
memset(ret, 0, m_len);
ptr = (char*)ret;
ret->PF = ptr + cpv_len;
ret->CATEGORY = ret->PF + cpv_string_len;
strcpy(ret->CATEGORY, cpv_string);
ptr = ret->CATEGORY;
// set empty version
ret->P = ptr + cpv_string_len - 1;
ret->PV = ret->P;
ret->PR_int = 0;
ret->PVR = ret->P;
// category
if (INVALID_FIRST_CHAR(*ptr))
cpv_error(ret, E_INVALID_CATEGORY);
while (*++ptr != '/')
if (!VALID_CHAR(*ptr))
cpv_error(ret, E_INVALID_CATEGORY);
*ptr = '\0';
ret->PN = ptr + 1;
if (INVALID_FIRST_CHAR(*(ret->PN)))
cpv_error(ret, E_INVALID_PN);
strcpy(ret->PF, ret->PN);
ptr = ret->PN;
while (*++ptr)
if (!VALID_CHAR(*ptr))
cpv_error(ret, E_INVALID_PN);
// pkgname shouldn't end with a hyphen followed by a valid version
else if (ptr[0] == '-' && isdigit(ptr[1]) && isvalid_version(&ptr[1]))
cpv_error(ret, E_INVALID_PN_VERSIONED_SUF);
ret->suffixes = malloc(sizeof(suffix_ver));
if (ret->suffixes == NULL)
cpv_error(ret, E_NOMEM);
ret->suffixes[0].suffix = SUF_NORM;
ret->suffixes[0].val = 0;
return ret;
}
CPV *cpv_alloc(const char *cpv_string, int versioned)
{
if (versioned)
return cpv_alloc_versioned(cpv_string);
else
return cpv_alloc_unversioned(cpv_string);
}
void cpv_free(CPV *cpv)
{
if (!cpv) return;
free(cpv->suffixes);
free(cpv);
}
cmp_code cpv_cmp(const CPV *c1, const CPV *c2)
{
if (!c1 || !c2)
return ERROR;
int ret;
if (ret = strcmp(c1->CATEGORY, c2->CATEGORY))
return ret > 0 ? NEWER : OLDER;
if (ret = strcmp(c1->PN, c2->PN))
return ret > 0 ? NEWER : OLDER;
if (!*c1->PVR) {
if (!*c2->PVR)
return EQUAL;
else
return OLDER;
} else if (!*c2->PVR)
return NEWER;
return version_cmp(c1->PVR, c2->PVR);
}
cmp_code cpv_cmp_str(const char *s1, const char *s2)
{
cmp_code ret;
char *ptr;
CPV *c1, *c2;
if (!(c1 = cpv_alloc(s1, 1)))
c1 = cpv_alloc(s1, 0);
if (!c1)
return ERROR;
if (!(c2 = cpv_alloc(s2, 1)))
c2 = cpv_alloc(s2, 0);
if (!c2)
goto cpv_error;
ret = cpv_cmp(c1, c2);
cpv_free(c2);
cpv_error:
cpv_free(c1);
return ret;
}
#undef cpv_error