1 /* pkg_parse.c - the opkg package management system
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
6 Copyright (C) 2002 Compaq Computer Corporation
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.
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.
26 #include "opkg_utils.h"
27 #include "pkg_parse.h"
28 #include "pkg_depends.h"
29 #include "libbb/libbb.h"
31 #include "file_util.h"
32 #include "parse_util.h"
34 static void parse_status(pkg_t * pkg, const char *sstr)
36 char sw_str[64], sf_str[64], ss_str[64];
38 if (sscanf(sstr, "Status: %63s %63s %63s", sw_str, sf_str, ss_str) != 3) {
39 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
44 pkg->state_want = pkg_state_want_from_str(sw_str);
45 pkg->state_flag |= pkg_state_flag_from_str(sf_str);
46 pkg->state_status = pkg_state_status_from_str(ss_str);
49 static void parse_conffiles(pkg_t * pkg, const char *cstr)
52 char file_name[1024], md5sum[85];
54 if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
55 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
60 cl = pkg_get_ptr(pkg, PKG_CONFFILES);
63 conffile_list_append(cl, file_name, md5sum);
66 int parse_version(pkg_t * pkg, const char *vstr)
70 if (strncmp(vstr, "Version:", 8) == 0)
73 while (*vstr && isspace(*vstr))
76 colon = strchr(vstr, ':');
79 pkg_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10));
81 opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
86 rev = strrchr(vstr, '-');
90 pkg_set_string(pkg, PKG_REVISION, rev);
93 pkg_set_string(pkg, PKG_VERSION, vstr);
98 static char *parse_architecture(pkg_t *pkg, const char *str)
108 while (e > s && isspace(*e))
111 return pkg_set_architecture(pkg, s, e - s);
114 int pkg_parse_line(void *ptr, const char *line, uint mask)
116 pkg_t *pkg = (pkg_t *) ptr;
117 abstract_pkg_t *ab_pkg = NULL;
119 /* these flags are a bit hackish... */
120 static int reading_conffiles = 0, reading_description = 0;
121 static char *description = NULL;
125 /* Exclude globally masked fields. */
128 /* Flip the semantics of the mask. */
133 if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line))
134 parse_architecture(pkg, line + strlen("Architecture") + 1);
135 else if ((mask & PFM_AUTO_INSTALLED)
136 && is_field("Auto-Installed", line)) {
137 char *tmp = parse_simple("Auto-Installed", line);
138 if (strcmp(tmp, "yes") == 0)
139 pkg->auto_installed = 1;
145 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
146 reading_conffiles = 1;
147 reading_description = 0;
148 goto dont_reset_flags;
149 } else if ((mask & PFM_CONFLICTS)
150 && is_field("Conflicts", line))
151 parse_deplist(pkg, CONFLICTS, line + strlen("Conflicts") + 1);
155 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
156 description = parse_simple("Description", line);
157 reading_conffiles = 0;
158 reading_description = 1;
159 goto dont_reset_flags;
160 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
161 parse_deplist(pkg, DEPEND, line + strlen("Depends") + 1);
165 if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
166 char *tmp = parse_simple("Essential", line);
167 if (strcmp(tmp, "yes") == 0)
174 if ((mask & PFM_FILENAME) && is_field("Filename", line))
175 pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1);
179 if ((mask & PFM_INSTALLED_SIZE)
180 && is_field("Installed-Size", line)) {
181 pkg_set_int(pkg, PKG_INSTALLED_SIZE, strtoul(line + strlen("Installed-Size") + 1, NULL, 0));
182 } else if ((mask & PFM_INSTALLED_TIME)
183 && is_field("Installed-Time", line)) {
184 pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0));
189 if ((mask & PFM_MD5SUM) && (is_field("MD5sum:", line) || is_field("MD5Sum:", line)))
190 pkg_set_md5(pkg, line + strlen("MD5sum") + 1);
191 else if ((mask & PFM_MAINTAINER)
192 && is_field("Maintainer", line))
193 pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1);
197 if ((mask & PFM_PACKAGE) && is_field("Package", line)) {
198 pkg->name = parse_simple("Package", line);
199 ab_pkg = abstract_pkg_fetch_by_name(pkg->name);
201 if (ab_pkg && (ab_pkg->state_flag & SF_NEED_DETAIL)) {
202 if (!(pkg->state_flag & SF_NEED_DETAIL)) {
203 opkg_msg(DEPEND, "propagating abpkg flag to pkg %s\n", pkg->name);
204 pkg->state_flag |= SF_NEED_DETAIL;
208 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
209 pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
210 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
211 parse_providelist(pkg, line + strlen("Provides") + 1);
212 else if ((mask & PFM_PRE_DEPENDS)
213 && is_field("Pre-Depends", line))
214 parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1);
218 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
219 parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1);
220 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
221 parse_replacelist(pkg, line + strlen("Replaces") + 1);
225 if ((mask & PFM_SECTION) && is_field("Section", line))
226 pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
228 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
229 pkg_set_sha256(pkg, line + strlen("SHA256sum") + 1);
231 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
232 pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0));
233 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
234 pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
235 else if ((mask & PFM_STATUS) && is_field("Status", line))
236 parse_status(pkg, line);
237 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
238 parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1);
242 if ((mask & PFM_TAGS) && is_field("Tags", line))
243 pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
247 if ((mask & PFM_VERSION) && is_field("Version", line))
248 parse_version(pkg, line);
252 if ((mask & PFM_DESCRIPTION) && reading_description) {
254 description = xrealloc(description,
258 strcat(description, "\n");
260 description = xrealloc(description,
264 strcat(description, (line));
265 goto dont_reset_flags;
266 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
267 parse_conffiles(pkg, line);
268 goto dont_reset_flags;
273 /* For package lists, signifies end of package. */
274 if (line_is_blank(line)) {
280 if (reading_description && description) {
281 pkg_set_string(pkg, PKG_DESCRIPTION, description);
283 reading_description = 0;
287 reading_conffiles = 0;
294 int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask)
298 const size_t len = 4096;
302 parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf,
306 if (pkg->name == NULL) {
307 /* probably just a blank line */