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.
24 #include "opkg_utils.h"
25 #include "pkg_parse.h"
26 #include "pkg_depends.h"
27 #include "libbb/libbb.h"
29 #include "file_util.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
)
50 char file_name
[1024], md5sum
[85];
52 if (sscanf(cstr
, "%1023s %84s", file_name
, md5sum
) != 2) {
53 opkg_msg(ERROR
, "Failed to parse Conffiles line for %s\n",
58 cl
= pkg_get_ptr(pkg
, PKG_CONFFILES
);
61 conffile_list_append(cl
, file_name
, md5sum
);
64 int parse_version(pkg_t
* pkg
, const char *vstr
)
66 char *colon
, *dup
, *rev
;
68 if (strncmp(vstr
, "Version:", 8) == 0)
71 while (*vstr
&& isspace(*vstr
))
74 colon
= strchr(vstr
, ':');
77 pkg_set_int(pkg
, PKG_EPOCH
, strtoul(vstr
, NULL
, 10));
79 opkg_perror(ERROR
, "%s: invalid epoch", pkg
->name
);
86 rev
= strrchr(dup
, '-');
90 pkg_set_string(pkg
, PKG_REVISION
, rev
);
93 pkg_set_string(pkg
, PKG_VERSION
, dup
);
99 static char *parse_architecture(pkg_t
*pkg
, const char *str
)
109 while (e
> s
&& isspace(*e
))
112 return pkg_set_architecture(pkg
, s
, e
- s
);
115 static void parse_alternatives(pkg_t
*pkg
, char *list
)
118 struct pkg_alternatives
*pkg_alts
;
119 struct pkg_alternative
**alts
;
122 pkg_alts
= pkg_get_ptr(pkg
, PKG_ALTERNATIVES
);
127 nalts
= pkg_alts
->nalts
;
128 alts
= pkg_alts
->alts
;
131 for (item
= strtok_r(list
, ",", &tok
);
133 item
= strtok_r(NULL
, ",", &tok
)) {
134 enum pkg_alternative_field i
;
136 /* the assignment was intended to quash the -Wmaybe-uninitialized warnings */
138 char *path
= path
, *altpath
= altpath
;
140 for (i
= PAF_PRIO
, val
= strtok_r(item
, ":", &tok1
);
141 val
&& i
< __PAF_MAX
;
142 val
= strtok_r(NULL
, ":", &tok1
), i
++) {
157 if (!val
&& i
== __PAF_MAX
) {
158 char *_path
, *_altpath
;
159 struct pkg_alternative
*alt
;
162 * - path must be absolute
163 * - altpath must be non-empty
165 if (path
[0] != '/' || !altpath
[0])
168 alt
= calloc_a(sizeof(*alt
),
169 &_path
, strlen(path
) + 1,
170 &_altpath
, strlen(altpath
) + 1);
174 strcpy(_altpath
, altpath
);
177 alt
->altpath
= _altpath
;
178 alts
= xrealloc(alts
, sizeof(*alts
) * (nalts
+ 1));
185 pkg_alts
= xmalloc(sizeof(*pkg_alts
));
186 pkg_alts
->nalts
= nalts
;
187 pkg_alts
->alts
= alts
;
188 pkg_set_ptr(pkg
, PKG_ALTERNATIVES
, pkg_alts
);
192 int pkg_parse_line(void *ptr
, char *line
, uint mask
)
194 pkg_t
*pkg
= (pkg_t
*) ptr
;
195 abstract_pkg_t
*ab_pkg
= NULL
;
198 /* these flags are a bit hackish... */
199 static int reading_conffiles
= 0, reading_description
= 0;
200 static char *description
= NULL
;
203 /* Exclude globally masked fields. */
206 /* Flip the semantics of the mask. */
211 if ((mask
& PFM_ABIVERSION
) && is_field("ABIVersion", line
))
212 pkg_set_string(pkg
, PKG_ABIVERSION
, line
+ strlen("ABIVersion") + 1);
213 else if ((mask
& PFM_ALTERNATIVES
) && is_field("Alternatives", line
))
214 parse_alternatives(pkg
, line
+ strlen("Alternatives") + 1);
215 else if ((mask
& PFM_ARCHITECTURE
) && is_field("Architecture", line
))
216 parse_architecture(pkg
, line
+ strlen("Architecture") + 1);
217 else if ((mask
& PFM_AUTO_INSTALLED
)
218 && is_field("Auto-Installed", line
)) {
219 char *tmp
= parse_simple("Auto-Installed", line
);
220 if (strcmp(tmp
, "yes") == 0)
221 pkg
->auto_installed
= 1;
227 if ((mask
& PFM_CONFFILES
) && is_field("Conffiles", line
)) {
228 reading_conffiles
= 1;
229 reading_description
= 0;
231 cl
= xcalloc(1, sizeof(*cl
));
232 conffile_list_init(cl
);
233 pkg_set_ptr(pkg
, PKG_CONFFILES
, cl
);
235 goto dont_reset_flags
;
236 } else if ((mask
& PFM_CONFLICTS
)
237 && is_field("Conflicts", line
))
238 parse_deplist(pkg
, CONFLICTS
, line
+ strlen("Conflicts") + 1);
242 if ((mask
& PFM_DESCRIPTION
) && is_field("Description", line
)) {
243 description
= parse_simple("Description", line
);
244 reading_conffiles
= 0;
245 reading_description
= 1;
246 goto dont_reset_flags
;
247 } else if ((mask
& PFM_DEPENDS
) && is_field("Depends", line
))
248 parse_deplist(pkg
, DEPEND
, line
+ strlen("Depends") + 1);
252 if ((mask
& PFM_ESSENTIAL
) && is_field("Essential", line
)) {
253 char *tmp
= parse_simple("Essential", line
);
254 if (strcmp(tmp
, "yes") == 0)
261 if ((mask
& PFM_FILENAME
) && is_field("Filename", line
))
262 pkg_set_string(pkg
, PKG_FILENAME
, line
+ strlen("Filename") + 1);
266 if ((mask
& PFM_INSTALLED_SIZE
)
267 && is_field("Installed-Size", line
)) {
268 pkg_set_int(pkg
, PKG_INSTALLED_SIZE
, strtoul(line
+ strlen("Installed-Size") + 1, NULL
, 0));
269 } else if ((mask
& PFM_INSTALLED_TIME
)
270 && is_field("Installed-Time", line
)) {
271 pkg_set_int(pkg
, PKG_INSTALLED_TIME
, strtoul(line
+ strlen("Installed-Time") + 1, NULL
, 0));
276 if ((mask
& PFM_MD5SUM
) && (is_field("MD5sum:", line
) || is_field("MD5Sum:", line
)))
277 pkg_set_md5(pkg
, line
+ strlen("MD5sum") + 1);
278 else if ((mask
& PFM_MAINTAINER
)
279 && is_field("Maintainer", line
))
280 pkg_set_string(pkg
, PKG_MAINTAINER
, line
+ strlen("Maintainer") + 1);
284 if ((mask
& PFM_PACKAGE
) && is_field("Package", line
)) {
285 pkg
->name
= parse_simple("Package", line
);
286 ab_pkg
= abstract_pkg_fetch_by_name(pkg
->name
);
288 if (ab_pkg
&& (ab_pkg
->state_flag
& SF_NEED_DETAIL
)) {
289 if (!(pkg
->state_flag
& SF_NEED_DETAIL
)) {
290 opkg_msg(DEBUG
, "propagating abpkg flag to pkg %s\n", pkg
->name
);
291 pkg
->state_flag
|= SF_NEED_DETAIL
;
295 else if ((mask
& PFM_PRIORITY
) && is_field("Priority", line
))
296 pkg_set_string(pkg
, PKG_PRIORITY
, line
+ strlen("Priority") + 1);
297 else if ((mask
& PFM_PROVIDES
) && is_field("Provides", line
))
298 parse_providelist(pkg
, line
+ strlen("Provides") + 1);
299 else if ((mask
& PFM_PRE_DEPENDS
)
300 && is_field("Pre-Depends", line
))
301 parse_deplist(pkg
, PREDEPEND
, line
+ strlen("Pre-Depends") + 1);
305 if ((mask
& PFM_RECOMMENDS
) && is_field("Recommends", line
))
306 parse_deplist(pkg
, RECOMMEND
, line
+ strlen("Recommends") + 1);
307 else if ((mask
& PFM_REPLACES
) && is_field("Replaces", line
))
308 parse_replacelist(pkg
, line
+ strlen("Replaces") + 1);
312 if ((mask
& PFM_SECTION
) && is_field("Section", line
))
313 pkg_set_string(pkg
, PKG_SECTION
, line
+ strlen("Section") + 1);
314 else if ((mask
& PFM_SHA256SUM
) && is_field("SHA256sum", line
))
315 pkg_set_sha256(pkg
, line
+ strlen("SHA256sum") + 1);
316 else if ((mask
& PFM_SIZE
) && is_field("Size", line
)) {
317 pkg_set_int(pkg
, PKG_SIZE
, strtoul(line
+ strlen("Size") + 1, NULL
, 0));
318 } else if ((mask
& PFM_SOURCE
) && is_field("Source", line
))
319 pkg_set_string(pkg
, PKG_SOURCE
, line
+ strlen("Source") + 1);
320 else if ((mask
& PFM_STATUS
) && is_field("Status", line
))
321 parse_status(pkg
, line
);
322 else if ((mask
& PFM_SUGGESTS
) && is_field("Suggests", line
))
323 parse_deplist(pkg
, SUGGEST
, line
+ strlen("Suggests") + 1);
327 if ((mask
& PFM_TAGS
) && is_field("Tags", line
))
328 pkg_set_string(pkg
, PKG_TAGS
, line
+ strlen("Tags") + 1);
332 if ((mask
& PFM_VERSION
) && is_field("Version", line
))
333 parse_version(pkg
, line
);
337 if ((mask
& PFM_DESCRIPTION
) && reading_description
) {
338 size_t len
= (description
? strlen(description
) : 0)
339 + (isatty(1) ? 1 : 0) + strlen(line
) + 1;
341 description
= description
? xrealloc(description
, len
)
345 strcat(description
, "\n");
347 strcat(description
, line
);
348 goto dont_reset_flags
;
349 } else if ((mask
& PFM_CONFFILES
) && reading_conffiles
) {
350 parse_conffiles(pkg
, line
);
351 goto dont_reset_flags
;
356 /* For package lists, signifies end of package. */
357 if (line_is_blank(line
)) {
363 if (reading_description
&& description
) {
364 pkg_set_string(pkg
, PKG_DESCRIPTION
, description
);
366 reading_description
= 0;
370 reading_conffiles
= 0;
377 int pkg_parse_from_stream(pkg_t
* pkg
, FILE * fp
, uint mask
)
381 const size_t len
= 4096;
385 parse_from_stream_nomalloc(pkg_parse_line
, pkg
, fp
, mask
, &buf
,
389 if (pkg
->name
== NULL
) {
390 /* probably just a blank line */