0149860bb7076c5db216ee43c96c8a4727f6acc6
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.
25 #include "opkg_utils.h"
26 #include "pkg_parse.h"
27 #include "libbb/libbb.h"
30 is_field(const char *type
, const char *line
)
32 if (!strncmp(line
, type
, strlen(type
)))
38 parse_simple(const char *type
, const char *line
)
40 return trim_xstrdup(line
+ strlen(type
) + 1);
44 * Parse a comma separated string into an array.
47 parse_comma_separated(const char *raw
, unsigned int *count
)
49 char **depends
= NULL
;
50 const char *start
, *end
;
53 /* skip past the "Field:" marker */
54 while (*raw
&& *raw
!= ':')
58 if (line_is_blank(raw
)) {
64 depends
= xrealloc(depends
, sizeof(char *) * (line_count
+ 1));
70 while (*raw
!= ',' && *raw
)
74 while (end
> start
&& isspace(*end
))
77 depends
[line_count
] = xstrndup(start
, end
-start
);
89 parse_status(pkg_t
*pkg
, const char *sstr
)
91 char sw_str
[64], sf_str
[64], ss_str
[64];
93 if (sscanf(sstr
, "Status: %63s %63s %63s",
94 sw_str
, sf_str
, ss_str
) != 3) {
95 opkg_msg(ERROR
, "Failed to parse Status line for %s\n",
100 pkg
->state_want
= pkg_state_want_from_str(sw_str
);
101 pkg
->state_flag
= pkg_state_flag_from_str(sf_str
);
102 pkg
->state_status
= pkg_state_status_from_str(ss_str
);
106 parse_conffiles(pkg_t
*pkg
, const char *cstr
)
108 char file_name
[1024], md5sum
[35];
110 if (sscanf(cstr
, "%1023s %34s", file_name
, md5sum
) != 2) {
111 opkg_msg(ERROR
, "Failed to parse Conffiles line for %s\n",
116 conffile_list_append(&pkg
->conffiles
, file_name
, md5sum
);
120 parse_version(pkg_t
*pkg
, const char *vstr
)
124 if (strncmp(vstr
, "Version:", 8) == 0)
127 while (*vstr
&& isspace(*vstr
))
130 colon
= strchr(vstr
, ':');
133 pkg
->epoch
= strtoul(vstr
, NULL
, 10);
135 opkg_perror(ERROR
, "%s: invalid epoch", pkg
->name
);
142 pkg
->version
= xstrdup(vstr
);
143 pkg
->revision
= strrchr(pkg
->version
,'-');
146 *pkg
->revision
++ = '\0';
152 get_arch_priority(const char *arch
)
154 nv_pair_list_elt_t
*l
;
156 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
157 nv_pair_t
*nv
= (nv_pair_t
*)l
->data
;
158 if (strcmp(nv
->name
, arch
) == 0)
159 return strtol(nv
->value
, NULL
, 0);
165 pkg_parse_line(pkg_t
*pkg
, const char *line
, uint mask
)
167 /* these flags are a bit hackish... */
168 static int reading_conffiles
= 0, reading_description
= 0;
171 /* Exclude globally masked fields. */
174 /* Flip the semantics of the mask. */
179 if ((mask
& PFM_ARCHITECTURE
) && is_field("Architecture", line
)) {
180 pkg
->architecture
= parse_simple("Architecture", line
);
181 pkg
->arch_priority
= get_arch_priority(pkg
->architecture
);
182 } else if ((mask
& PFM_AUTO_INSTALLED
) && is_field("Auto-Installed", line
)) {
183 char *tmp
= parse_simple("Auto-Installed", line
);
184 if (strcmp(tmp
, "yes") == 0)
185 pkg
->auto_installed
= 1;
191 if ((mask
& PFM_CONFFILES
) && is_field("Conffiles", line
)) {
192 reading_conffiles
= 1;
193 reading_description
= 0;
194 goto dont_reset_flags
;
196 else if ((mask
& PFM_CONFLICTS
) && is_field("Conflicts", line
))
197 pkg
->conflicts_str
= parse_comma_separated(line
, &pkg
->conflicts_count
);
201 if ((mask
& PFM_DESCRIPTION
) && is_field("Description", line
)) {
202 pkg
->description
= parse_simple("Description", line
);
203 reading_conffiles
= 0;
204 reading_description
= 1;
205 goto dont_reset_flags
;
206 } else if ((mask
& PFM_DEPENDS
) && is_field("Depends", line
))
207 pkg
->depends_str
= parse_comma_separated(line
, &pkg
->depends_count
);
211 if((mask
& PFM_ESSENTIAL
) && is_field("Essential", line
)) {
212 char *tmp
= parse_simple("Essential", line
);
213 if (strcmp(tmp
, "yes") == 0)
220 if((mask
& PFM_FILENAME
) && is_field("Filename", line
))
221 pkg
->filename
= parse_simple("Filename", line
);
225 if ((mask
&& PFM_INSTALLED_SIZE
) && is_field("Installed-Size", line
)) {
226 char *tmp
= parse_simple("Installed-Size", line
);
227 pkg
->installed_size
= strtoul(tmp
, NULL
, 0);
229 } else if ((mask
&& PFM_INSTALLED_TIME
) && is_field("Installed-Time", line
)) {
230 char *tmp
= parse_simple("Installed-Time", line
);
231 pkg
->installed_time
= strtoul(tmp
, NULL
, 0);
237 if (mask
&& PFM_MD5SUM
) {
238 if (is_field("MD5sum:", line
))
239 pkg
->md5sum
= parse_simple("MD5sum", line
);
240 /* The old opkg wrote out status files with the wrong
241 * case for MD5sum, let's parse it either way */
242 else if (is_field("MD5Sum:", line
))
243 pkg
->md5sum
= parse_simple("MD5Sum", line
);
244 } else if((mask
& PFM_MAINTAINER
) && is_field("Maintainer", line
))
245 pkg
->maintainer
= parse_simple("Maintainer", line
);
249 if ((mask
& PFM_PACKAGE
) && is_field("Package", line
))
250 pkg
->name
= parse_simple("Package", line
);
251 else if ((mask
& PFM_PRIORITY
) && is_field("Priority", line
))
252 pkg
->priority
= parse_simple("Priority", line
);
253 else if ((mask
& PFM_PROVIDES
) && is_field("Provides", line
))
254 pkg
->provides_str
= parse_comma_separated(line
, &pkg
->provides_count
);
255 else if ((mask
& PFM_PRE_DEPENDS
) && is_field("Pre-Depends", line
))
256 pkg
->pre_depends_str
= parse_comma_separated(line
, &pkg
->pre_depends_count
);
260 if ((mask
& PFM_RECOMMENDS
) && is_field("Recommends", line
))
261 pkg
->recommends_str
= parse_comma_separated(line
, &pkg
->recommends_count
);
262 else if ((mask
& PFM_REPLACES
) && is_field("Replaces", line
))
263 pkg
->replaces_str
= parse_comma_separated(line
, &pkg
->replaces_count
);
268 if ((mask
& PFM_SECTION
) && is_field("Section", line
))
269 pkg
->section
= parse_simple("Section", line
);
271 else if ((mask
& PFM_SHA256SUM
) && is_field("SHA256sum", line
))
272 pkg
->sha256sum
= parse_simple("SHA256sum", line
);
274 else if ((mask
& PFM_SIZE
) && is_field("Size", line
)) {
275 char *tmp
= parse_simple("Size", line
);
276 pkg
->size
= strtoul(tmp
, NULL
, 0);
278 } else if ((mask
& PFM_SOURCE
) && is_field("Source", line
))
279 pkg
->source
= parse_simple("Source", line
);
280 else if ((mask
& PFM_STATUS
) && is_field("Status", line
))
281 parse_status(pkg
, line
);
282 else if ((mask
& PFM_SUGGESTS
) && is_field("Suggests", line
))
283 pkg
->suggests_str
= parse_comma_separated(line
, &pkg
->suggests_count
);
287 if ((mask
& PFM_TAGS
) && is_field("Tags", line
))
288 pkg
->tags
= parse_simple("Tags", line
);
292 if ((mask
& PFM_VERSION
) && is_field("Version", line
))
293 parse_version(pkg
, line
);
297 if ((mask
& PFM_DESCRIPTION
) && reading_description
) {
298 pkg
->description
= xrealloc(pkg
->description
,
299 strlen(pkg
->description
)
300 + 1 + strlen(line
) + 1);
301 strcat(pkg
->description
, "\n");
302 strcat(pkg
->description
, (line
));
303 goto dont_reset_flags
;
304 } else if ((mask
&& PFM_CONFFILES
) && reading_conffiles
) {
305 parse_conffiles(pkg
, line
);
306 goto dont_reset_flags
;
311 /* For package lists, signifies end of package. */
312 if(line_is_blank(line
)) {
318 reading_description
= 0;
319 reading_conffiles
= 0;
327 pkg_parse_from_stream_nomalloc(pkg_t
*pkg
, FILE *fp
, uint mask
,
328 char **buf0
, size_t buf0len
)
342 if (fgets(buf
, (int)buflen
, fp
) == NULL
) {
344 opkg_perror(ERROR
, "fgets");
346 } else if (strlen(*buf0
) == buf0len
-1) {
347 opkg_msg(ERROR
, "Missing new line character"
348 " at end of file!\n");
349 pkg_parse_line(pkg
, *buf0
, mask
);
354 nl
= strchr(buf
, '\n');
356 if (strlen(buf
) < buflen
-1) {
358 * Line could be exactly buflen-1 long and
359 * missing a newline, but we won't know until
360 * fgets fails to read more data.
362 opkg_msg(ERROR
, "Missing new line character"
363 " at end of file!\n");
364 pkg_parse_line(pkg
, *buf0
, mask
);
367 if (buf0len
>= EXCESSIVE_LINE_LEN
) {
368 opkg_msg(ERROR
, "Excessively long line at "
369 "%d. Corrupt file?\n",
376 * Realloc and point buf past the data already read,
377 * at the NULL terminator inserted by fgets.
378 * |<--------------- buf0len ----------------->|
379 * | |<------- buflen ---->|
380 * |---------------------|---------------------|
385 *buf0
= xrealloc(*buf0
, buf0len
);
386 buf
= *buf0
+ buflen
-2;
395 if (pkg_parse_line(pkg
, *buf0
, mask
))
403 if (pkg
->name
== NULL
) {
404 /* probably just a blank line */
412 pkg_parse_from_stream(pkg_t
*pkg
, FILE *fp
, uint mask
)
416 const size_t len
= 4096;
419 ret
= pkg_parse_from_stream_nomalloc(pkg
, fp
, mask
, &buf
, len
);