1 /* pkg_parse.c - the opkg package management system
5 Copyright (C) 2002 Compaq Computer Corporation
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
23 #include "opkg_utils.h"
24 #include "pkg_parse.h"
25 #include "libbb/libbb.h"
27 static int isGenericFieldType(char * type, const char * line)
29 if(!strncmp(line, type, strlen(type)))
34 static char * parseGenericFieldType(char * type, const char * raw)
36 const char * field_value = raw + (strlen(type) + 1);
37 return trim_alloc(field_value);
40 static void parseStatus(pkg_t *pkg, const char * raw)
42 char sw_str[64], sf_str[64], ss_str[64];
44 sscanf(raw, "Status: %s %s %s", sw_str, sf_str, ss_str);
45 pkg->state_want = pkg_state_want_from_str(sw_str);
46 pkg->state_flag = pkg_state_flag_from_str(sf_str);
47 pkg->state_status = pkg_state_status_from_str(ss_str);
50 static char ** parseDependsString(const char * raw, int * depends_count)
52 char ** depends = NULL;
54 char buff[2048], * dest;
56 while(raw && *raw && !isspace(*raw)) {
60 if(line_is_blank(raw)){
61 *depends_count = line_count;
65 depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
67 while(isspace(*raw)) raw++;
70 while((*raw != ',') && *raw)
74 depends[line_count] = trim_alloc(buff);
75 if(depends[line_count] ==NULL)
81 *depends_count = line_count;
86 parseConffiles(pkg_t *pkg, const char *cstr)
88 char file_name[1024], md5sum[35];
90 if (sscanf(cstr, "%1023s %34s", file_name, md5sum) != 2) {
91 fprintf(stderr, "%s: failed to parse Conffiles line for %s\n",
92 __FUNCTION__, pkg->name);
96 conffile_list_append(&pkg->conffiles, file_name, md5sum);
100 parseVersion(pkg_t *pkg, const char *vstr)
104 if (strncmp(vstr, "Version:", 8) == 0)
107 while (*vstr && isspace(*vstr))
110 colon = strchr(vstr, ':');
113 pkg->epoch = strtoul(vstr, NULL, 10);
115 fprintf(stderr, "%s: %s: invalid epoch: %s\n",
116 __FUNCTION__, pkg->name, strerror(errno));
123 pkg->version= xstrdup(vstr);
124 pkg->revision = strrchr(pkg->version,'-');
127 *pkg->revision++ = '\0';
133 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
135 /* these flags are a bit hackish... */
136 static int reading_conffiles = 0, reading_description = 0;
140 if((mask & PFM_ARCHITECTURE ) && isGenericFieldType("Architecture:", line))
141 pkg->architecture = parseGenericFieldType("Architecture", line);
142 else if((mask & PFM_AUTO_INSTALLED) && isGenericFieldType("Auto-Installed:", line)) {
143 char *auto_installed_value;
144 auto_installed_value = parseGenericFieldType("Auto-Installed:", line);
145 if (strcmp(auto_installed_value, "yes") == 0) {
146 pkg->auto_installed = 1;
148 free(auto_installed_value);
153 if((mask & PFM_CONFFILES) && isGenericFieldType("Conffiles", line)){
154 reading_conffiles = 1;
155 reading_description = 0;
156 goto dont_reset_flags;
158 else if((mask & PFM_CONFLICTS) && isGenericFieldType("Conflicts", line))
159 pkg->conflicts_str = parseDependsString(line, &pkg->conflicts_count);
163 if((mask & PFM_DESCRIPTION) && isGenericFieldType("Description", line)) {
164 pkg->description = parseGenericFieldType("Description", line);
165 reading_conffiles = 0;
166 reading_description = 1;
167 goto dont_reset_flags;
169 else if((mask & PFM_DEPENDS) && isGenericFieldType("Depends", line))
170 pkg->depends_str = parseDependsString(line, &pkg->depends_count);
174 if((mask & PFM_ESSENTIAL) && isGenericFieldType("Essential:", line)) {
175 char *essential_value;
176 essential_value = parseGenericFieldType("Essential", line);
177 if (strcmp(essential_value, "yes") == 0) {
180 free(essential_value);
185 if((mask & PFM_FILENAME) && isGenericFieldType("Filename:", line))
186 pkg->filename = parseGenericFieldType("Filename", line);
190 if((mask && PFM_INSTALLED_SIZE) && isGenericFieldType("Installed-Size:", line))
191 pkg->installed_size = parseGenericFieldType("Installed-Size", line);
192 else if((mask && PFM_INSTALLED_TIME) && isGenericFieldType("Installed-Time:", line)) {
193 char *time_str = parseGenericFieldType("Installed-Time", line);
194 pkg->installed_time = strtoul(time_str, NULL, 0);
200 if(mask && PFM_MD5SUM) {
201 if (isGenericFieldType("MD5sum:", line))
202 pkg->md5sum = parseGenericFieldType("MD5sum", line);
203 /* The old opkg wrote out status files with the wrong
204 * case for MD5sum, let's parse it either way */
205 else if(isGenericFieldType("MD5Sum:", line))
206 pkg->md5sum = parseGenericFieldType("MD5Sum", line);
207 } else if((mask & PFM_MAINTAINER) && isGenericFieldType("Maintainer", line))
208 pkg->maintainer = parseGenericFieldType("Maintainer", line);
212 if((mask & PFM_PACKAGE) && isGenericFieldType("Package:", line))
213 pkg->name = parseGenericFieldType("Package", line);
214 else if((mask & PFM_PRIORITY) && isGenericFieldType("Priority:", line))
215 pkg->priority = parseGenericFieldType("Priority", line);
216 else if((mask & PFM_PROVIDES) && isGenericFieldType("Provides", line)){
217 pkg->provides_str = parseDependsString(line, &pkg->provides_count);
219 else if((mask & PFM_PRE_DEPENDS) && isGenericFieldType("Pre-Depends", line))
220 pkg->pre_depends_str = parseDependsString(line, &pkg->pre_depends_count);
224 if((mask & PFM_RECOMMENDS) && isGenericFieldType("Recommends", line))
225 pkg->recommends_str = parseDependsString(line, &pkg->recommends_count);
226 else if((mask & PFM_REPLACES) && isGenericFieldType("Replaces", line))
227 pkg->replaces_str = parseDependsString(line, &pkg->replaces_count);
232 if((mask & PFM_SECTION) && isGenericFieldType("Section:", line))
233 pkg->section = parseGenericFieldType("Section", line);
235 else if((mask & PFM_SHA256SUM) && isGenericFieldType("SHA256sum:", line))
236 pkg->sha256sum = parseGenericFieldType("SHA256sum", line);
238 else if((mask & PFM_SIZE) && isGenericFieldType("Size:", line))
239 pkg->size = parseGenericFieldType("Size", line);
240 else if((mask & PFM_SOURCE) && isGenericFieldType("Source:", line))
241 pkg->source = parseGenericFieldType("Source", line);
242 else if((mask & PFM_STATUS) && isGenericFieldType("Status", line))
243 parseStatus(pkg, line);
244 else if((mask & PFM_SUGGESTS) && isGenericFieldType("Suggests", line))
245 pkg->suggests_str = parseDependsString(line, &pkg->suggests_count);
249 if((mask & PFM_TAGS) && isGenericFieldType("Tags:", line))
250 pkg->tags = parseGenericFieldType("Tags", line);
254 if((mask & PFM_VERSION) && isGenericFieldType("Version", line))
255 parseVersion(pkg, line);
259 if((mask & PFM_DESCRIPTION) && reading_description) {
260 /* we already know it's not blank, so the rest of description */
261 pkg->description = xrealloc(pkg->description,
262 strlen(pkg->description)
263 + 1 + strlen(line) + 1);
264 strcat(pkg->description, "\n");
265 strcat(pkg->description, (line));
266 goto dont_reset_flags;
268 else if((mask && PFM_CONFFILES) && reading_conffiles) {
269 parseConffiles(pkg, line);
270 goto dont_reset_flags;
275 /* For package lists, signifies end of package. */
276 if(line_is_blank(line)) {
281 reading_description = 0;
282 reading_conffiles = 0;
290 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
291 char **buf0, size_t buf0len)
305 if (fgets(buf, buflen, fp) == NULL) {
307 fprintf(stderr, "%s: fgets: %s\n",
308 __FUNCTION__, strerror(errno));
310 } else if (strlen(*buf0) == buflen-1) {
311 fprintf(stderr, "%s: missing new line character"
312 " at end of file!\n",
314 pkg_parse_line(pkg, *buf0, mask);
319 nl = strchr(buf, '\n');
321 if (strlen(buf) < buflen-1) {
323 * Line could be exactly buflen-1 long and
324 * missing a newline, but we won't know until
325 * fgets fails to read more data.
327 fprintf(stderr, "%s: missing new line character"
328 " at end of file!\n",
330 pkg_parse_line(pkg, *buf0, mask);
333 if (buf0len >= EXCESSIVE_LINE_LEN) {
334 fprintf(stderr, "%s: excessively long line at "
335 "%d. Corrupt file?\n",
336 __FUNCTION__, lineno);
342 * Realloc and move buf past the data already read.
343 * |<--------------- buf0len ----------------->|
344 * | |<------- buflen ---->|
345 * |---------------------|---------------------|
350 *buf0 = xrealloc(*buf0, buf0len);
351 buf = *buf0 + buflen -1;
360 if (pkg_parse_line(pkg, *buf0, mask))
368 if (pkg->name == NULL) {
369 /* probably just a blank line */
377 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
381 const size_t len = 4096;
384 ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);