1 /* pkg_parse.c - the opkg package management system
5 Copyright (C) 2002 Compaq Computer Corporation
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.
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.
23 #include "opkg_utils.h"
24 #include "pkg_parse.h"
25 #include "libbb/libbb.h"
28 is_field(char *type, const char *line)
30 if (!strncmp(line, type, strlen(type)))
36 parse_simple(char *type, const char *line)
38 return trim_xstrdup(line + strlen(type) + 1);
42 * Parse a comma separated string into an array.
45 parse_comma_separated(const char *raw, int *count)
47 char **depends = NULL;
48 const char *start, *end;
51 /* skip past the "Field:" marker */
52 while (*raw && *raw != ':')
56 if (line_is_blank(raw)) {
62 depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
68 while (*raw != ',' && *raw)
72 while (end > start && isspace(*end))
75 depends[line_count] = xstrndup(start, end-start);
87 parse_status(pkg_t *pkg, const char *sstr)
89 char sw_str[64], sf_str[64], ss_str[64];
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);
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);
104 parse_conffiles(pkg_t *pkg, const char *cstr)
106 char file_name[1024], md5sum[35];
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);
114 conffile_list_append(&pkg->conffiles, file_name, md5sum);
118 parse_version(pkg_t *pkg, const char *vstr)
122 if (strncmp(vstr, "Version:", 8) == 0)
125 while (*vstr && isspace(*vstr))
128 colon = strchr(vstr, ':');
131 pkg->epoch = strtoul(vstr, NULL, 10);
133 fprintf(stderr, "%s: %s: invalid epoch: %s\n",
134 __FUNCTION__, pkg->name, strerror(errno));
141 pkg->version= xstrdup(vstr);
142 pkg->revision = strrchr(pkg->version,'-');
145 *pkg->revision++ = '\0';
151 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
153 /* these flags are a bit hackish... */
154 static int reading_conffiles = 0, reading_description = 0;
159 if ((mask & PFM_ARCHITECTURE ) && is_field("Architecture", line))
160 pkg->architecture = parse_simple("Architecture", line);
161 else if ((mask & PFM_AUTO_INSTALLED) && is_field("Auto-Installed", line)) {
162 char *tmp = parse_simple("Auto-Installed", line);
163 if (strcmp(tmp, "yes") == 0)
164 pkg->auto_installed = 1;
170 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
171 reading_conffiles = 1;
172 reading_description = 0;
173 goto dont_reset_flags;
175 else if ((mask & PFM_CONFLICTS) && is_field("Conflicts", line))
176 pkg->conflicts_str = parse_comma_separated(line, &pkg->conflicts_count);
180 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
181 pkg->description = parse_simple("Description", line);
182 reading_conffiles = 0;
183 reading_description = 1;
184 goto dont_reset_flags;
185 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
186 pkg->depends_str = parse_comma_separated(line, &pkg->depends_count);
190 if((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
191 char *tmp = parse_simple("Essential", line);
192 if (strcmp(tmp, "yes") == 0)
199 if((mask & PFM_FILENAME) && is_field("Filename", line))
200 pkg->filename = parse_simple("Filename", line);
204 if ((mask && PFM_INSTALLED_SIZE) && is_field("Installed-Size", line))
205 pkg->installed_size = parse_simple("Installed-Size", line);
206 else if ((mask && PFM_INSTALLED_TIME) && is_field("Installed-Time", line)) {
207 char *tmp = parse_simple("Installed-Time", line);
208 pkg->installed_time = strtoul(tmp, NULL, 0);
214 if (mask && PFM_MD5SUM) {
215 if (is_field("MD5sum:", line))
216 pkg->md5sum = parse_simple("MD5sum", line);
217 /* The old opkg wrote out status files with the wrong
218 * case for MD5sum, let's parse it either way */
219 else if (is_field("MD5Sum:", line))
220 pkg->md5sum = parse_simple("MD5Sum", line);
221 } else if((mask & PFM_MAINTAINER) && is_field("Maintainer", line))
222 pkg->maintainer = parse_simple("Maintainer", line);
226 if ((mask & PFM_PACKAGE) && is_field("Package", line))
227 pkg->name = parse_simple("Package", line);
228 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
229 pkg->priority = parse_simple("Priority", line);
230 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
231 pkg->provides_str = parse_comma_separated(line, &pkg->provides_count);
232 else if ((mask & PFM_PRE_DEPENDS) && is_field("Pre-Depends", line))
233 pkg->pre_depends_str = parse_comma_separated(line, &pkg->pre_depends_count);
237 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
238 pkg->recommends_str = parse_comma_separated(line, &pkg->recommends_count);
239 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
240 pkg->replaces_str = parse_comma_separated(line, &pkg->replaces_count);
245 if ((mask & PFM_SECTION) && is_field("Section", line))
246 pkg->section = parse_simple("Section", line);
248 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
249 pkg->sha256sum = parse_simple("SHA256sum", line);
251 else if ((mask & PFM_SIZE) && is_field("Size", line))
252 pkg->size = parse_simple("Size", line);
253 else if ((mask & PFM_SOURCE) && is_field("Source", line))
254 pkg->source = parse_simple("Source", line);
255 else if ((mask & PFM_STATUS) && is_field("Status", line))
256 parse_status(pkg, line);
257 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
258 pkg->suggests_str = parse_comma_separated(line, &pkg->suggests_count);
262 if ((mask & PFM_TAGS) && is_field("Tags", line))
263 pkg->tags = parse_simple("Tags", line);
267 if ((mask & PFM_VERSION) && is_field("Version", line))
268 parse_version(pkg, line);
272 if ((mask & PFM_DESCRIPTION) && reading_description) {
273 pkg->description = xrealloc(pkg->description,
274 strlen(pkg->description)
275 + 1 + strlen(line) + 1);
276 strcat(pkg->description, "\n");
277 strcat(pkg->description, (line));
278 goto dont_reset_flags;
279 } else if ((mask && PFM_CONFFILES) && reading_conffiles) {
280 parse_conffiles(pkg, line);
281 goto dont_reset_flags;
286 /* For package lists, signifies end of package. */
287 if(line_is_blank(line)) {
293 reading_description = 0;
294 reading_conffiles = 0;
302 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
303 char **buf0, size_t buf0len)
317 if (fgets(buf, buflen, fp) == NULL) {
319 fprintf(stderr, "%s: fgets: %s\n",
320 __FUNCTION__, strerror(errno));
322 } else if (strlen(*buf0) == buf0len-1) {
323 fprintf(stderr, "%s: missing new line character"
324 " at end of file!\n",
326 pkg_parse_line(pkg, *buf0, mask);
331 nl = strchr(buf, '\n');
333 if (strlen(buf) < buflen-1) {
335 * Line could be exactly buflen-1 long and
336 * missing a newline, but we won't know until
337 * fgets fails to read more data.
339 fprintf(stderr, "%s: missing new line character"
340 " at end of file!\n",
342 pkg_parse_line(pkg, *buf0, mask);
345 if (buf0len >= EXCESSIVE_LINE_LEN) {
346 fprintf(stderr, "%s: excessively long line at "
347 "%d. Corrupt file?\n",
348 __FUNCTION__, lineno);
354 * Realloc and move buf past the data already read.
355 * |<--------------- buf0len ----------------->|
356 * | |<------- buflen ---->|
357 * |---------------------|---------------------|
362 *buf0 = xrealloc(*buf0, buf0len);
363 buf = *buf0 + buflen -1;
372 if (pkg_parse_line(pkg, *buf0, mask))
380 if (pkg->name == NULL) {
381 /* probably just a blank line */
389 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
393 const size_t len = 4096;
396 ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);