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