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 "libbb/libbb.h"
30 #include "parse_util.h"
32 static void parse_status(pkg_t
* pkg
, const char *sstr
)
34 char sw_str
[64], sf_str
[64], ss_str
[64];
36 if (sscanf(sstr
, "Status: %63s %63s %63s", sw_str
, sf_str
, ss_str
) != 3) {
37 opkg_msg(ERROR
, "Failed to parse Status line for %s\n",
42 pkg
->state_want
= pkg_state_want_from_str(sw_str
);
43 pkg
->state_flag
= pkg_state_flag_from_str(sf_str
);
44 pkg
->state_status
= pkg_state_status_from_str(ss_str
);
47 static void parse_conffiles(pkg_t
* pkg
, const char *cstr
)
49 char file_name
[1024], md5sum
[85];
51 if (sscanf(cstr
, "%1023s %84s", file_name
, md5sum
) != 2) {
52 opkg_msg(ERROR
, "Failed to parse Conffiles line for %s\n",
57 conffile_list_append(&pkg
->conffiles
, file_name
, md5sum
);
60 int parse_version(pkg_t
* pkg
, const char *vstr
)
64 if (strncmp(vstr
, "Version:", 8) == 0)
67 while (*vstr
&& isspace(*vstr
))
70 colon
= strchr(vstr
, ':');
73 pkg_set_int(pkg
, PKG_EPOCH
, strtoul(vstr
, NULL
, 10));
75 opkg_perror(ERROR
, "%s: invalid epoch", pkg
->name
);
80 rev
= strrchr(pkg_set_string(pkg
, PKG_VERSION
, vstr
), '-');
84 pkg_set_ptr(pkg
, PKG_REVISION
, rev
);
90 static int get_arch_priority(const char *arch
)
92 nv_pair_list_elt_t
*l
;
94 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
95 nv_pair_t
*nv
= (nv_pair_t
*) l
->data
;
96 if (strcmp(nv
->name
, arch
) == 0)
97 return strtol(nv
->value
, NULL
, 0);
102 int pkg_parse_line(void *ptr
, const char *line
, uint mask
)
104 pkg_t
*pkg
= (pkg_t
*) ptr
;
106 /* these flags are a bit hackish... */
107 static int reading_conffiles
= 0, reading_description
= 0;
108 static char *description
= NULL
;
111 /* Exclude globally masked fields. */
114 /* Flip the semantics of the mask. */
119 if ((mask
& PFM_ARCHITECTURE
) && is_field("Architecture", line
)) {
120 pkg
->arch_priority
= get_arch_priority(
121 pkg_set_string(pkg
, PKG_ARCHITECTURE
, line
+ strlen("Architecture") + 1));
123 } else if ((mask
& PFM_AUTO_INSTALLED
)
124 && is_field("Auto-Installed", line
)) {
125 char *tmp
= parse_simple("Auto-Installed", line
);
126 if (strcmp(tmp
, "yes") == 0)
127 pkg
->auto_installed
= 1;
133 if ((mask
& PFM_CONFFILES
) && is_field("Conffiles", line
)) {
134 reading_conffiles
= 1;
135 reading_description
= 0;
136 goto dont_reset_flags
;
137 } else if ((mask
& PFM_CONFLICTS
)
138 && is_field("Conflicts", line
))
140 parse_list(line
, &pkg
->conflicts_count
, ',', 0);
144 if ((mask
& PFM_DESCRIPTION
) && is_field("Description", line
)) {
145 description
= parse_simple("Description", line
);
146 reading_conffiles
= 0;
147 reading_description
= 1;
148 goto dont_reset_flags
;
149 } else if ((mask
& PFM_DEPENDS
) && is_field("Depends", line
))
151 parse_list(line
, &pkg
->depends_count
, ',', 0);
155 if ((mask
& PFM_ESSENTIAL
) && is_field("Essential", line
)) {
156 char *tmp
= parse_simple("Essential", line
);
157 if (strcmp(tmp
, "yes") == 0)
164 if ((mask
& PFM_FILENAME
) && is_field("Filename", line
))
165 pkg_set_string(pkg
, PKG_FILENAME
, line
+ strlen("Filename") + 1);
169 if ((mask
& PFM_INSTALLED_SIZE
)
170 && is_field("Installed-Size", line
)) {
171 pkg_set_int(pkg
, PKG_INSTALLED_SIZE
, strtoul(line
+ strlen("Installed-Size") + 1, NULL
, 0));
172 } else if ((mask
& PFM_INSTALLED_TIME
)
173 && is_field("Installed-Time", line
)) {
174 pkg_set_int(pkg
, PKG_INSTALLED_TIME
, strtoul(line
+ strlen("Installed-Time") + 1, NULL
, 0));
179 if ((mask
& PFM_MD5SUM
) && is_field("MD5sum:", line
))
180 pkg_set_string(pkg
, PKG_MD5SUM
, line
+ strlen("MD5sum") + 1);
181 /* The old opkg wrote out status files with the wrong
182 * case for MD5sum, let's parse it either way */
183 else if ((mask
& PFM_MD5SUM
) && is_field("MD5Sum:", line
))
184 pkg_set_string(pkg
, PKG_MD5SUM
, line
+ strlen("MD5Sum") + 1);
185 else if ((mask
& PFM_MAINTAINER
)
186 && is_field("Maintainer", line
))
187 pkg_set_string(pkg
, PKG_MAINTAINER
, line
+ strlen("Maintainer") + 1);
191 if ((mask
& PFM_PACKAGE
) && is_field("Package", line
))
192 pkg
->name
= parse_simple("Package", line
);
193 else if ((mask
& PFM_PRIORITY
) && is_field("Priority", line
))
194 pkg_set_string(pkg
, PKG_PRIORITY
, line
+ strlen("Priority") + 1);
195 else if ((mask
& PFM_PROVIDES
) && is_field("Provides", line
))
197 parse_list(line
, &pkg
->provides_count
, ',', 0);
198 else if ((mask
& PFM_PRE_DEPENDS
)
199 && is_field("Pre-Depends", line
))
200 pkg
->pre_depends_str
=
201 parse_list(line
, &pkg
->pre_depends_count
, ',', 0);
205 if ((mask
& PFM_RECOMMENDS
) && is_field("Recommends", line
))
206 pkg
->recommends_str
=
207 parse_list(line
, &pkg
->recommends_count
, ',', 0);
208 else if ((mask
& PFM_REPLACES
) && is_field("Replaces", line
))
210 parse_list(line
, &pkg
->replaces_count
, ',', 0);
215 if ((mask
& PFM_SECTION
) && is_field("Section", line
))
216 pkg_set_string(pkg
, PKG_SECTION
, line
+ strlen("Section") + 1);
218 else if ((mask
& PFM_SHA256SUM
) && is_field("SHA256sum", line
))
219 pkg_set_string(pkg
, PKG_SHA256SUM
, line
+ strlen("SHA256sum") + 1);
221 else if ((mask
& PFM_SIZE
) && is_field("Size", line
)) {
222 pkg_set_int(pkg
, PKG_SIZE
, strtoul(line
+ strlen("Size") + 1, NULL
, 0));
223 } else if ((mask
& PFM_SOURCE
) && is_field("Source", line
))
224 pkg_set_string(pkg
, PKG_SOURCE
, line
+ strlen("Source") + 1);
225 else if ((mask
& PFM_STATUS
) && is_field("Status", line
))
226 parse_status(pkg
, line
);
227 else if ((mask
& PFM_SUGGESTS
) && is_field("Suggests", line
))
229 parse_list(line
, &pkg
->suggests_count
, ',', 0);
233 if ((mask
& PFM_TAGS
) && is_field("Tags", line
))
234 pkg_set_string(pkg
, PKG_TAGS
, line
+ strlen("Tags") + 1);
238 if ((mask
& PFM_VERSION
) && is_field("Version", line
))
239 parse_version(pkg
, line
);
243 if ((mask
& PFM_DESCRIPTION
) && reading_description
) {
245 description
= xrealloc(description
,
249 strcat(description
, "\n");
251 description
= xrealloc(description
,
255 strcat(description
, (line
));
256 goto dont_reset_flags
;
257 } else if ((mask
& PFM_CONFFILES
) && reading_conffiles
) {
258 parse_conffiles(pkg
, line
);
259 goto dont_reset_flags
;
264 /* For package lists, signifies end of package. */
265 if (line_is_blank(line
)) {
271 if (reading_description
&& description
) {
272 pkg_set_string(pkg
, PKG_DESCRIPTION
, description
);
274 reading_description
= 0;
278 reading_conffiles
= 0;
285 int pkg_parse_from_stream(pkg_t
* pkg
, FILE * fp
, uint mask
)
289 const size_t len
= 4096;
293 parse_from_stream_nomalloc(pkg_parse_line
, pkg
, fp
, mask
, &buf
,
297 if (pkg
->name
== NULL
) {
298 /* probably just a blank line */