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"
29 #include "parse_util.h"
32 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",
37 sw_str
, sf_str
, ss_str
) != 3) {
38 opkg_msg(ERROR
, "Failed to parse Status line for %s\n",
43 pkg
->state_want
= pkg_state_want_from_str(sw_str
);
44 pkg
->state_flag
= pkg_state_flag_from_str(sf_str
);
45 pkg
->state_status
= pkg_state_status_from_str(ss_str
);
49 parse_conffiles(pkg_t
*pkg
, const char *cstr
)
51 char file_name
[1024], md5sum
[35];
53 if (sscanf(cstr
, "%1023s %34s", file_name
, md5sum
) != 2) {
54 opkg_msg(ERROR
, "Failed to parse Conffiles line for %s\n",
59 conffile_list_append(&pkg
->conffiles
, file_name
, md5sum
);
63 parse_version(pkg_t
*pkg
, const char *vstr
)
67 if (strncmp(vstr
, "Version:", 8) == 0)
70 while (*vstr
&& isspace(*vstr
))
73 colon
= strchr(vstr
, ':');
76 pkg
->epoch
= strtoul(vstr
, NULL
, 10);
78 opkg_perror(ERROR
, "%s: invalid epoch", pkg
->name
);
85 pkg
->version
= xstrdup(vstr
);
86 pkg
->revision
= strrchr(pkg
->version
,'-');
89 *pkg
->revision
++ = '\0';
95 get_arch_priority(const char *arch
)
97 nv_pair_list_elt_t
*l
;
99 list_for_each_entry(l
, &conf
->arch_list
.head
, node
) {
100 nv_pair_t
*nv
= (nv_pair_t
*)l
->data
;
101 if (strcmp(nv
->name
, arch
) == 0)
102 return strtol(nv
->value
, NULL
, 0);
108 pkg_parse_line(pkg_t
*pkg
, const char *line
, uint mask
)
110 /* these flags are a bit hackish... */
111 static int reading_conffiles
= 0, reading_description
= 0;
114 /* Exclude globally masked fields. */
117 /* Flip the semantics of the mask. */
122 if ((mask
& PFM_ARCHITECTURE
) && is_field("Architecture", line
)) {
123 pkg
->architecture
= parse_simple("Architecture", line
);
124 pkg
->arch_priority
= get_arch_priority(pkg
->architecture
);
125 } else if ((mask
& PFM_AUTO_INSTALLED
) && is_field("Auto-Installed", line
)) {
126 char *tmp
= parse_simple("Auto-Installed", line
);
127 if (strcmp(tmp
, "yes") == 0)
128 pkg
->auto_installed
= 1;
134 if ((mask
& PFM_CONFFILES
) && is_field("Conffiles", line
)) {
135 reading_conffiles
= 1;
136 reading_description
= 0;
137 goto dont_reset_flags
;
139 else if ((mask
& PFM_CONFLICTS
) && is_field("Conflicts", line
))
140 pkg
->conflicts_str
= parse_list(line
, &pkg
->conflicts_count
, ',', 0);
144 if ((mask
& PFM_DESCRIPTION
) && is_field("Description", line
)) {
145 pkg
->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
))
150 pkg
->depends_str
= parse_list(line
, &pkg
->depends_count
, ',', 0);
154 if((mask
& PFM_ESSENTIAL
) && is_field("Essential", line
)) {
155 char *tmp
= parse_simple("Essential", line
);
156 if (strcmp(tmp
, "yes") == 0)
163 if((mask
& PFM_FILENAME
) && is_field("Filename", line
))
164 pkg
->filename
= parse_simple("Filename", line
);
168 if ((mask
& PFM_INSTALLED_SIZE
) && is_field("Installed-Size", line
)) {
169 char *tmp
= parse_simple("Installed-Size", line
);
170 pkg
->installed_size
= strtoul(tmp
, NULL
, 0);
172 } else if ((mask
& PFM_INSTALLED_TIME
) && is_field("Installed-Time", line
)) {
173 char *tmp
= parse_simple("Installed-Time", line
);
174 pkg
->installed_time
= strtoul(tmp
, NULL
, 0);
180 if ((mask
& PFM_MD5SUM
) && is_field("MD5sum:", line
))
181 pkg
->md5sum
= parse_simple("MD5sum", line
);
182 /* The old opkg wrote out status files with the wrong
183 * case for MD5sum, let's parse it either way */
184 else if ((mask
& PFM_MD5SUM
) && is_field("MD5Sum:", line
))
185 pkg
->md5sum
= parse_simple("MD5Sum", line
);
186 else if((mask
& PFM_MAINTAINER
) && is_field("Maintainer", line
))
187 pkg
->maintainer
= parse_simple("Maintainer", line
);
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
->priority
= parse_simple("Priority", line
);
195 else if ((mask
& PFM_PROVIDES
) && is_field("Provides", line
))
196 pkg
->provides_str
= parse_list(line
, &pkg
->provides_count
, ',', 0);
197 else if ((mask
& PFM_PRE_DEPENDS
) && is_field("Pre-Depends", line
))
198 pkg
->pre_depends_str
= parse_list(line
, &pkg
->pre_depends_count
, ',', 0);
202 if ((mask
& PFM_RECOMMENDS
) && is_field("Recommends", line
))
203 pkg
->recommends_str
= parse_list(line
, &pkg
->recommends_count
, ',', 0);
204 else if ((mask
& PFM_REPLACES
) && is_field("Replaces", line
))
205 pkg
->replaces_str
= parse_list(line
, &pkg
->replaces_count
, ',', 0);
210 if ((mask
& PFM_SECTION
) && is_field("Section", line
))
211 pkg
->section
= parse_simple("Section", line
);
213 else if ((mask
& PFM_SHA256SUM
) && is_field("SHA256sum", line
))
214 pkg
->sha256sum
= parse_simple("SHA256sum", line
);
216 else if ((mask
& PFM_SIZE
) && is_field("Size", line
)) {
217 char *tmp
= parse_simple("Size", line
);
218 pkg
->size
= strtoul(tmp
, NULL
, 0);
220 } else if ((mask
& PFM_SOURCE
) && is_field("Source", line
))
221 pkg
->source
= parse_simple("Source", line
);
222 else if ((mask
& PFM_STATUS
) && is_field("Status", line
))
223 parse_status(pkg
, line
);
224 else if ((mask
& PFM_SUGGESTS
) && is_field("Suggests", line
))
225 pkg
->suggests_str
= parse_list(line
, &pkg
->suggests_count
, ',', 0);
229 if ((mask
& PFM_TAGS
) && is_field("Tags", line
))
230 pkg
->tags
= parse_simple("Tags", line
);
234 if ((mask
& PFM_VERSION
) && is_field("Version", line
))
235 parse_version(pkg
, line
);
239 if ((mask
& PFM_DESCRIPTION
) && reading_description
) {
240 pkg
->description
= xrealloc(pkg
->description
,
241 strlen(pkg
->description
)
242 + 1 + strlen(line
) + 1);
243 strcat(pkg
->description
, "\n");
244 strcat(pkg
->description
, (line
));
245 goto dont_reset_flags
;
246 } else if ((mask
& PFM_CONFFILES
) && reading_conffiles
) {
247 parse_conffiles(pkg
, line
);
248 goto dont_reset_flags
;
253 /* For package lists, signifies end of package. */
254 if(line_is_blank(line
)) {
260 reading_description
= 0;
261 reading_conffiles
= 0;
269 pkg_parse_from_stream_nomalloc(pkg_t
*pkg
, FILE *fp
, uint mask
,
270 char **buf0
, size_t buf0len
)
284 if (fgets(buf
, (int)buflen
, fp
) == NULL
) {
286 opkg_perror(ERROR
, "fgets");
288 } else if (strlen(*buf0
) == buf0len
-1) {
289 opkg_msg(ERROR
, "Missing new line character"
290 " at end of file!\n");
291 pkg_parse_line(pkg
, *buf0
, mask
);
296 nl
= strchr(buf
, '\n');
298 if (strlen(buf
) < buflen
-1) {
300 * Line could be exactly buflen-1 long and
301 * missing a newline, but we won't know until
302 * fgets fails to read more data.
304 opkg_msg(ERROR
, "Missing new line character"
305 " at end of file!\n");
306 pkg_parse_line(pkg
, *buf0
, mask
);
309 if (buf0len
>= EXCESSIVE_LINE_LEN
) {
310 opkg_msg(ERROR
, "Excessively long line at "
311 "%d. Corrupt file?\n",
318 * Realloc and point buf past the data already read,
319 * at the NULL terminator inserted by fgets.
320 * |<--------------- buf0len ----------------->|
321 * | |<------- buflen ---->|
322 * |---------------------|---------------------|
327 *buf0
= xrealloc(*buf0
, buf0len
);
328 buf
= *buf0
+ buflen
-2;
337 if (pkg_parse_line(pkg
, *buf0
, mask
))
345 if (pkg
->name
== NULL
) {
346 /* probably just a blank line */
354 pkg_parse_from_stream(pkg_t
*pkg
, FILE *fp
, uint mask
)
358 const size_t len
= 4096;
361 ret
= pkg_parse_from_stream_nomalloc(pkg
, fp
, mask
, &buf
, len
);