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 "libbb/libbb.h"
29 is_field(const char *type
, const char *line
)
31 if (!strncmp(line
, type
, strlen(type
)))
37 parse_simple(const char *type
, const char *line
)
39 return trim_xstrdup(line
+ strlen(type
) + 1);
43 * Parse a comma separated string into an array.
46 parse_comma_separated(const char *raw
, unsigned int *count
)
48 char **depends
= NULL
;
49 const char *start
, *end
;
52 /* skip past the "Field:" marker */
53 while (*raw
&& *raw
!= ':')
57 if (line_is_blank(raw
)) {
63 depends
= xrealloc(depends
, sizeof(char *) * (line_count
+ 1));
69 while (*raw
!= ',' && *raw
)
73 while (end
> start
&& isspace(*end
))
76 depends
[line_count
] = xstrndup(start
, end
-start
);
88 parse_status(pkg_t
*pkg
, const char *sstr
)
90 char sw_str
[64], sf_str
[64], ss_str
[64];
92 if (sscanf(sstr
, "Status: %63s %63s %63s",
93 sw_str
, sf_str
, ss_str
) != 3) {
94 opkg_msg(ERROR
, "Failed to parse Status line for %s\n",
99 pkg
->state_want
= pkg_state_want_from_str(sw_str
);
100 pkg
->state_flag
= pkg_state_flag_from_str(sf_str
);
101 pkg
->state_status
= pkg_state_status_from_str(ss_str
);
105 parse_conffiles(pkg_t
*pkg
, const char *cstr
)
107 char file_name
[1024], md5sum
[35];
109 if (sscanf(cstr
, "%1023s %34s", file_name
, md5sum
) != 2) {
110 opkg_msg(ERROR
, "Failed to parse Conffiles line for %s\n",
115 conffile_list_append(&pkg
->conffiles
, file_name
, md5sum
);
119 parse_version(pkg_t
*pkg
, const char *vstr
)
123 if (strncmp(vstr
, "Version:", 8) == 0)
126 while (*vstr
&& isspace(*vstr
))
129 colon
= strchr(vstr
, ':');
132 pkg
->epoch
= strtoul(vstr
, NULL
, 10);
134 opkg_perror(ERROR
, "%s: invalid epoch", pkg
->name
);
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;
157 /* Exclude globally masked fields. */
160 /* Flip the semantics of the mask. */
165 if ((mask
& PFM_ARCHITECTURE
) && is_field("Architecture", line
))
166 pkg
->architecture
= parse_simple("Architecture", line
);
167 else if ((mask
& PFM_AUTO_INSTALLED
) && is_field("Auto-Installed", line
)) {
168 char *tmp
= parse_simple("Auto-Installed", line
);
169 if (strcmp(tmp
, "yes") == 0)
170 pkg
->auto_installed
= 1;
176 if ((mask
& PFM_CONFFILES
) && is_field("Conffiles", line
)) {
177 reading_conffiles
= 1;
178 reading_description
= 0;
179 goto dont_reset_flags
;
181 else if ((mask
& PFM_CONFLICTS
) && is_field("Conflicts", line
))
182 pkg
->conflicts_str
= parse_comma_separated(line
, &pkg
->conflicts_count
);
186 if ((mask
& PFM_DESCRIPTION
) && is_field("Description", line
)) {
187 pkg
->description
= parse_simple("Description", line
);
188 reading_conffiles
= 0;
189 reading_description
= 1;
190 goto dont_reset_flags
;
191 } else if ((mask
& PFM_DEPENDS
) && is_field("Depends", line
))
192 pkg
->depends_str
= parse_comma_separated(line
, &pkg
->depends_count
);
196 if((mask
& PFM_ESSENTIAL
) && is_field("Essential", line
)) {
197 char *tmp
= parse_simple("Essential", line
);
198 if (strcmp(tmp
, "yes") == 0)
205 if((mask
& PFM_FILENAME
) && is_field("Filename", line
))
206 pkg
->filename
= parse_simple("Filename", line
);
210 if ((mask
&& PFM_INSTALLED_SIZE
) && is_field("Installed-Size", line
)) {
211 char *tmp
= parse_simple("Installed-Size", line
);
212 pkg
->installed_size
= strtoul(tmp
, NULL
, 0);
214 } else if ((mask
&& PFM_INSTALLED_TIME
) && is_field("Installed-Time", line
)) {
215 char *tmp
= parse_simple("Installed-Time", line
);
216 pkg
->installed_time
= strtoul(tmp
, NULL
, 0);
222 if (mask
&& PFM_MD5SUM
) {
223 if (is_field("MD5sum:", line
))
224 pkg
->md5sum
= parse_simple("MD5sum", line
);
225 /* The old opkg wrote out status files with the wrong
226 * case for MD5sum, let's parse it either way */
227 else if (is_field("MD5Sum:", line
))
228 pkg
->md5sum
= parse_simple("MD5Sum", line
);
229 } else if((mask
& PFM_MAINTAINER
) && is_field("Maintainer", line
))
230 pkg
->maintainer
= parse_simple("Maintainer", line
);
234 if ((mask
& PFM_PACKAGE
) && is_field("Package", line
))
235 pkg
->name
= parse_simple("Package", line
);
236 else if ((mask
& PFM_PRIORITY
) && is_field("Priority", line
))
237 pkg
->priority
= parse_simple("Priority", line
);
238 else if ((mask
& PFM_PROVIDES
) && is_field("Provides", line
))
239 pkg
->provides_str
= parse_comma_separated(line
, &pkg
->provides_count
);
240 else if ((mask
& PFM_PRE_DEPENDS
) && is_field("Pre-Depends", line
))
241 pkg
->pre_depends_str
= parse_comma_separated(line
, &pkg
->pre_depends_count
);
245 if ((mask
& PFM_RECOMMENDS
) && is_field("Recommends", line
))
246 pkg
->recommends_str
= parse_comma_separated(line
, &pkg
->recommends_count
);
247 else if ((mask
& PFM_REPLACES
) && is_field("Replaces", line
))
248 pkg
->replaces_str
= parse_comma_separated(line
, &pkg
->replaces_count
);
253 if ((mask
& PFM_SECTION
) && is_field("Section", line
))
254 pkg
->section
= parse_simple("Section", line
);
256 else if ((mask
& PFM_SHA256SUM
) && is_field("SHA256sum", line
))
257 pkg
->sha256sum
= parse_simple("SHA256sum", line
);
259 else if ((mask
& PFM_SIZE
) && is_field("Size", line
)) {
260 char *tmp
= parse_simple("Size", line
);
261 pkg
->size
= strtoul(tmp
, NULL
, 0);
263 } else if ((mask
& PFM_SOURCE
) && is_field("Source", line
))
264 pkg
->source
= parse_simple("Source", line
);
265 else if ((mask
& PFM_STATUS
) && is_field("Status", line
))
266 parse_status(pkg
, line
);
267 else if ((mask
& PFM_SUGGESTS
) && is_field("Suggests", line
))
268 pkg
->suggests_str
= parse_comma_separated(line
, &pkg
->suggests_count
);
272 if ((mask
& PFM_TAGS
) && is_field("Tags", line
))
273 pkg
->tags
= parse_simple("Tags", line
);
277 if ((mask
& PFM_VERSION
) && is_field("Version", line
))
278 parse_version(pkg
, line
);
282 if ((mask
& PFM_DESCRIPTION
) && reading_description
) {
283 pkg
->description
= xrealloc(pkg
->description
,
284 strlen(pkg
->description
)
285 + 1 + strlen(line
) + 1);
286 strcat(pkg
->description
, "\n");
287 strcat(pkg
->description
, (line
));
288 goto dont_reset_flags
;
289 } else if ((mask
&& PFM_CONFFILES
) && reading_conffiles
) {
290 parse_conffiles(pkg
, line
);
291 goto dont_reset_flags
;
296 /* For package lists, signifies end of package. */
297 if(line_is_blank(line
)) {
303 reading_description
= 0;
304 reading_conffiles
= 0;
312 pkg_parse_from_stream_nomalloc(pkg_t
*pkg
, FILE *fp
, uint mask
,
313 char **buf0
, size_t buf0len
)
327 if (fgets(buf
, (int)buflen
, fp
) == NULL
) {
329 opkg_perror(ERROR
, "fgets");
331 } else if (strlen(*buf0
) == buf0len
-1) {
332 opkg_msg(ERROR
, "Missing new line character"
333 " at end of file!\n");
334 pkg_parse_line(pkg
, *buf0
, mask
);
339 nl
= strchr(buf
, '\n');
341 if (strlen(buf
) < buflen
-1) {
343 * Line could be exactly buflen-1 long and
344 * missing a newline, but we won't know until
345 * fgets fails to read more data.
347 opkg_msg(ERROR
, "Missing new line character"
348 " at end of file!\n");
349 pkg_parse_line(pkg
, *buf0
, mask
);
352 if (buf0len
>= EXCESSIVE_LINE_LEN
) {
353 opkg_msg(ERROR
, "Excessively long line at "
354 "%d. Corrupt file?\n",
361 * Realloc and point buf past the data already read,
362 * at the NULL terminator inserted by fgets.
363 * |<--------------- buf0len ----------------->|
364 * | |<------- buflen ---->|
365 * |---------------------|---------------------|
370 *buf0
= xrealloc(*buf0
, buf0len
);
371 buf
= *buf0
+ buflen
-2;
380 if (pkg_parse_line(pkg
, *buf0
, mask
))
388 if (pkg
->name
== NULL
) {
389 /* probably just a blank line */
397 pkg_parse_from_stream(pkg_t
*pkg
, FILE *fp
, uint mask
)
401 const size_t len
= 4096;
404 ret
= pkg_parse_from_stream_nomalloc(pkg
, fp
, mask
, &buf
, len
);