Use uppercase M for printing maintainer field, to be consistent.
[project/opkg-lede.git] / libopkg / pkg_parse.c
1 /* pkg_parse.c - the opkg package management system
2
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5 Steven M. Ayer
6 Copyright (C) 2002 Compaq Computer Corporation
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
12
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17 */
18
19 #include "config.h"
20
21 #include <stdio.h>
22 #include <ctype.h>
23
24 #include "pkg.h"
25 #include "opkg_utils.h"
26 #include "pkg_parse.h"
27 #include "libbb/libbb.h"
28
29 static int
30 is_field(const char *type, const char *line)
31 {
32 if (!strncmp(line, type, strlen(type)))
33 return 1;
34 return 0;
35 }
36
37 static char *
38 parse_simple(const char *type, const char *line)
39 {
40 return trim_xstrdup(line + strlen(type) + 1);
41 }
42
43 /*
44 * Parse a comma separated string into an array.
45 */
46 static char **
47 parse_comma_separated(const char *raw, unsigned int *count)
48 {
49 char **depends = NULL;
50 const char *start, *end;
51 int line_count = 0;
52
53 /* skip past the "Field:" marker */
54 while (*raw && *raw != ':')
55 raw++;
56 raw++;
57
58 if (line_is_blank(raw)) {
59 *count = line_count;
60 return NULL;
61 }
62
63 while (*raw) {
64 depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
65
66 while (isspace(*raw))
67 raw++;
68
69 start = raw;
70 while (*raw != ',' && *raw)
71 raw++;
72 end = raw;
73
74 while (end > start && isspace(*end))
75 end--;
76
77 depends[line_count] = xstrndup(start, end-start);
78
79 line_count++;
80 if (*raw == ',')
81 raw++;
82 }
83
84 *count = line_count;
85 return depends;
86 }
87
88 static void
89 parse_status(pkg_t *pkg, const char *sstr)
90 {
91 char sw_str[64], sf_str[64], ss_str[64];
92
93 if (sscanf(sstr, "Status: %63s %63s %63s",
94 sw_str, sf_str, ss_str) != 3) {
95 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
96 pkg->name);
97 return;
98 }
99
100 pkg->state_want = pkg_state_want_from_str(sw_str);
101 pkg->state_flag = pkg_state_flag_from_str(sf_str);
102 pkg->state_status = pkg_state_status_from_str(ss_str);
103 }
104
105 static void
106 parse_conffiles(pkg_t *pkg, const char *cstr)
107 {
108 char file_name[1024], md5sum[35];
109
110 if (sscanf(cstr, "%1023s %34s", file_name, md5sum) != 2) {
111 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
112 pkg->name);
113 return;
114 }
115
116 conffile_list_append(&pkg->conffiles, file_name, md5sum);
117 }
118
119 int
120 parse_version(pkg_t *pkg, const char *vstr)
121 {
122 char *colon;
123
124 if (strncmp(vstr, "Version:", 8) == 0)
125 vstr += 8;
126
127 while (*vstr && isspace(*vstr))
128 vstr++;
129
130 colon = strchr(vstr, ':');
131 if (colon) {
132 errno = 0;
133 pkg->epoch = strtoul(vstr, NULL, 10);
134 if (errno) {
135 opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
136 }
137 vstr = ++colon;
138 } else {
139 pkg->epoch= 0;
140 }
141
142 pkg->version= xstrdup(vstr);
143 pkg->revision = strrchr(pkg->version,'-');
144
145 if (pkg->revision)
146 *pkg->revision++ = '\0';
147
148 return 0;
149 }
150
151 static int
152 get_arch_priority(const char *arch)
153 {
154 nv_pair_list_elt_t *l;
155
156 list_for_each_entry(l , &conf->arch_list.head, node) {
157 nv_pair_t *nv = (nv_pair_t *)l->data;
158 if (strcmp(nv->name, arch) == 0)
159 return strtol(nv->value, NULL, 0);
160 }
161 return 0;
162 }
163
164 static int
165 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
166 {
167 /* these flags are a bit hackish... */
168 static int reading_conffiles = 0, reading_description = 0;
169 int ret = 0;
170
171 /* Exclude globally masked fields. */
172 mask |= conf->pfm;
173
174 /* Flip the semantics of the mask. */
175 mask ^= PFM_ALL;
176
177 switch (*line) {
178 case 'A':
179 if ((mask & PFM_ARCHITECTURE ) && is_field("Architecture", line)) {
180 pkg->architecture = parse_simple("Architecture", line);
181 pkg->arch_priority = get_arch_priority(pkg->architecture);
182 } else if ((mask & PFM_AUTO_INSTALLED) && is_field("Auto-Installed", line)) {
183 char *tmp = parse_simple("Auto-Installed", line);
184 if (strcmp(tmp, "yes") == 0)
185 pkg->auto_installed = 1;
186 free(tmp);
187 }
188 break;
189
190 case 'C':
191 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
192 reading_conffiles = 1;
193 reading_description = 0;
194 goto dont_reset_flags;
195 }
196 else if ((mask & PFM_CONFLICTS) && is_field("Conflicts", line))
197 pkg->conflicts_str = parse_comma_separated(line, &pkg->conflicts_count);
198 break;
199
200 case 'D':
201 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
202 pkg->description = parse_simple("Description", line);
203 reading_conffiles = 0;
204 reading_description = 1;
205 goto dont_reset_flags;
206 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
207 pkg->depends_str = parse_comma_separated(line, &pkg->depends_count);
208 break;
209
210 case 'E':
211 if((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
212 char *tmp = parse_simple("Essential", line);
213 if (strcmp(tmp, "yes") == 0)
214 pkg->essential = 1;
215 free(tmp);
216 }
217 break;
218
219 case 'F':
220 if((mask & PFM_FILENAME) && is_field("Filename", line))
221 pkg->filename = parse_simple("Filename", line);
222 break;
223
224 case 'I':
225 if ((mask & PFM_INSTALLED_SIZE) && is_field("Installed-Size", line)) {
226 char *tmp = parse_simple("Installed-Size", line);
227 pkg->installed_size = strtoul(tmp, NULL, 0);
228 free (tmp);
229 } else if ((mask & PFM_INSTALLED_TIME) && is_field("Installed-Time", line)) {
230 char *tmp = parse_simple("Installed-Time", line);
231 pkg->installed_time = strtoul(tmp, NULL, 0);
232 free (tmp);
233 }
234 break;
235
236 case 'M':
237 if ((mask & PFM_MD5SUM) && is_field("MD5sum:", line))
238 pkg->md5sum = parse_simple("MD5sum", line);
239 /* The old opkg wrote out status files with the wrong
240 * case for MD5sum, let's parse it either way */
241 else if ((mask & PFM_MD5SUM) && is_field("MD5Sum:", line))
242 pkg->md5sum = parse_simple("MD5Sum", line);
243 else if((mask & PFM_MAINTAINER) && is_field("Maintainer", line))
244 pkg->maintainer = parse_simple("Maintainer", line);
245 break;
246
247 case 'P':
248 if ((mask & PFM_PACKAGE) && is_field("Package", line))
249 pkg->name = parse_simple("Package", line);
250 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
251 pkg->priority = parse_simple("Priority", line);
252 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
253 pkg->provides_str = parse_comma_separated(line, &pkg->provides_count);
254 else if ((mask & PFM_PRE_DEPENDS) && is_field("Pre-Depends", line))
255 pkg->pre_depends_str = parse_comma_separated(line, &pkg->pre_depends_count);
256 break;
257
258 case 'R':
259 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
260 pkg->recommends_str = parse_comma_separated(line, &pkg->recommends_count);
261 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
262 pkg->replaces_str = parse_comma_separated(line, &pkg->replaces_count);
263
264 break;
265
266 case 'S':
267 if ((mask & PFM_SECTION) && is_field("Section", line))
268 pkg->section = parse_simple("Section", line);
269 #ifdef HAVE_SHA256
270 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
271 pkg->sha256sum = parse_simple("SHA256sum", line);
272 #endif
273 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
274 char *tmp = parse_simple("Size", line);
275 pkg->size = strtoul(tmp, NULL, 0);
276 free (tmp);
277 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
278 pkg->source = parse_simple("Source", line);
279 else if ((mask & PFM_STATUS) && is_field("Status", line))
280 parse_status(pkg, line);
281 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
282 pkg->suggests_str = parse_comma_separated(line, &pkg->suggests_count);
283 break;
284
285 case 'T':
286 if ((mask & PFM_TAGS) && is_field("Tags", line))
287 pkg->tags = parse_simple("Tags", line);
288 break;
289
290 case 'V':
291 if ((mask & PFM_VERSION) && is_field("Version", line))
292 parse_version(pkg, line);
293 break;
294
295 case ' ':
296 if ((mask & PFM_DESCRIPTION) && reading_description) {
297 pkg->description = xrealloc(pkg->description,
298 strlen(pkg->description)
299 + 1 + strlen(line) + 1);
300 strcat(pkg->description, "\n");
301 strcat(pkg->description, (line));
302 goto dont_reset_flags;
303 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
304 parse_conffiles(pkg, line);
305 goto dont_reset_flags;
306 }
307
308 /* FALLTHROUGH */
309 default:
310 /* For package lists, signifies end of package. */
311 if(line_is_blank(line)) {
312 ret = 1;
313 break;
314 }
315 }
316
317 reading_description = 0;
318 reading_conffiles = 0;
319
320 dont_reset_flags:
321
322 return ret;
323 }
324
325 int
326 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
327 char **buf0, size_t buf0len)
328 {
329 int ret, lineno;
330 char *buf, *nl;
331 size_t buflen;
332
333 lineno = 1;
334 ret = 0;
335
336 buflen = buf0len;
337 buf = *buf0;
338 buf[0] = '\0';
339
340 while (1) {
341 if (fgets(buf, (int)buflen, fp) == NULL) {
342 if (ferror(fp)) {
343 opkg_perror(ERROR, "fgets");
344 ret = -1;
345 } else if (strlen(*buf0) == buf0len-1) {
346 opkg_msg(ERROR, "Missing new line character"
347 " at end of file!\n");
348 pkg_parse_line(pkg, *buf0, mask);
349 }
350 break;
351 }
352
353 nl = strchr(buf, '\n');
354 if (nl == NULL) {
355 if (strlen(buf) < buflen-1) {
356 /*
357 * Line could be exactly buflen-1 long and
358 * missing a newline, but we won't know until
359 * fgets fails to read more data.
360 */
361 opkg_msg(ERROR, "Missing new line character"
362 " at end of file!\n");
363 pkg_parse_line(pkg, *buf0, mask);
364 break;
365 }
366 if (buf0len >= EXCESSIVE_LINE_LEN) {
367 opkg_msg(ERROR, "Excessively long line at "
368 "%d. Corrupt file?\n",
369 lineno);
370 ret = -1;
371 break;
372 }
373
374 /*
375 * Realloc and point buf past the data already read,
376 * at the NULL terminator inserted by fgets.
377 * |<--------------- buf0len ----------------->|
378 * | |<------- buflen ---->|
379 * |---------------------|---------------------|
380 * buf0 buf
381 */
382 buflen = buf0len +1;
383 buf0len *= 2;
384 *buf0 = xrealloc(*buf0, buf0len);
385 buf = *buf0 + buflen -2;
386
387 continue;
388 }
389
390 *nl = '\0';
391
392 lineno++;
393
394 if (pkg_parse_line(pkg, *buf0, mask))
395 break;
396
397 buf = *buf0;
398 buflen = buf0len;
399 buf[0] = '\0';
400 }
401
402 if (pkg->name == NULL) {
403 /* probably just a blank line */
404 ret = 1;
405 }
406
407 return ret;
408 }
409
410 int
411 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
412 {
413 int ret;
414 char *buf;
415 const size_t len = 4096;
416
417 buf = xmalloc(len);
418 ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
419 free(buf);
420
421 return ret;
422 }