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;
85 static void parseConffiles(pkg_t * pkg, const char * raw)
87 char file_name[1048], md5sum[1048]; /* please tell me there aren't any longer that 1k */
89 if(!strncmp(raw, "Conffiles:", 10))
90 raw += strlen("Conffiles:");
92 while(*raw && (sscanf(raw, "%s%s", file_name, md5sum) == 2)){
93 conffile_list_append(&pkg->conffiles, file_name, md5sum);
94 /* fprintf(stderr, "%s %s ", file_name, md5sum);*/
95 while (*raw && isspace(*raw)) {
98 raw += strlen(file_name);
99 while (*raw && isspace(*raw)) {
102 raw += strlen(md5sum);
107 parseVersion(pkg_t *pkg, const char *vstr)
111 if (strncmp(vstr, "Version:", 8) == 0)
114 while (*vstr && isspace(*vstr))
117 colon = strchr(vstr, ':');
120 pkg->epoch = strtoul(vstr, NULL, 10);
122 fprintf(stderr, "%s: %s: invalid epoch: %s\n",
123 __FUNCTION__, pkg->name, strerror(errno));
130 pkg->version= xstrdup(vstr);
131 pkg->revision = strrchr(pkg->version,'-');
134 *pkg->revision++ = '\0';
140 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
142 /* these flags are a bit hackish... */
143 static int reading_conffiles = 0, reading_description = 0;
147 if((mask & PFM_ARCHITECTURE ) && isGenericFieldType("Architecture:", line))
148 pkg->architecture = parseGenericFieldType("Architecture", line);
149 else if((mask & PFM_AUTO_INSTALLED) && isGenericFieldType("Auto-Installed:", line)) {
150 char *auto_installed_value;
151 auto_installed_value = parseGenericFieldType("Auto-Installed:", line);
152 if (strcmp(auto_installed_value, "yes") == 0) {
153 pkg->auto_installed = 1;
155 free(auto_installed_value);
160 if((mask & PFM_CONFFILES) && isGenericFieldType("Conffiles", line)){
161 parseConffiles(pkg, line);
162 reading_conffiles = 1;
163 reading_description = 0;
164 goto dont_reset_flags;
166 else if((mask & PFM_CONFLICTS) && isGenericFieldType("Conflicts", line))
167 pkg->conflicts_str = parseDependsString(line, &pkg->conflicts_count);
171 if((mask & PFM_DESCRIPTION) && isGenericFieldType("Description", line)) {
172 pkg->description = parseGenericFieldType("Description", line);
173 reading_conffiles = 0;
174 reading_description = 1;
175 goto dont_reset_flags;
177 else if((mask & PFM_DEPENDS) && isGenericFieldType("Depends", line))
178 pkg->depends_str = parseDependsString(line, &pkg->depends_count);
182 if((mask & PFM_ESSENTIAL) && isGenericFieldType("Essential:", line)) {
183 char *essential_value;
184 essential_value = parseGenericFieldType("Essential", line);
185 if (strcmp(essential_value, "yes") == 0) {
188 free(essential_value);
193 if((mask & PFM_FILENAME) && isGenericFieldType("Filename:", line))
194 pkg->filename = parseGenericFieldType("Filename", line);
198 if((mask && PFM_INSTALLED_SIZE) && isGenericFieldType("Installed-Size:", line))
199 pkg->installed_size = parseGenericFieldType("Installed-Size", line);
200 else if((mask && PFM_INSTALLED_TIME) && isGenericFieldType("Installed-Time:", line)) {
201 char *time_str = parseGenericFieldType("Installed-Time", line);
202 pkg->installed_time = strtoul(time_str, NULL, 0);
208 if(mask && PFM_MD5SUM) {
209 if (isGenericFieldType("MD5sum:", line))
210 pkg->md5sum = parseGenericFieldType("MD5sum", line);
211 /* The old opkg wrote out status files with the wrong
212 * case for MD5sum, let's parse it either way */
213 else if(isGenericFieldType("MD5Sum:", line))
214 pkg->md5sum = parseGenericFieldType("MD5Sum", line);
215 } else if((mask & PFM_MAINTAINER) && isGenericFieldType("Maintainer", line))
216 pkg->maintainer = parseGenericFieldType("Maintainer", line);
220 if((mask & PFM_PACKAGE) && isGenericFieldType("Package:", line))
221 pkg->name = parseGenericFieldType("Package", line);
222 else if((mask & PFM_PRIORITY) && isGenericFieldType("Priority:", line))
223 pkg->priority = parseGenericFieldType("Priority", line);
224 else if((mask & PFM_PROVIDES) && isGenericFieldType("Provides", line)){
225 pkg->provides_str = parseDependsString(line, &pkg->provides_count);
227 else if((mask & PFM_PRE_DEPENDS) && isGenericFieldType("Pre-Depends", line))
228 pkg->pre_depends_str = parseDependsString(line, &pkg->pre_depends_count);
232 if((mask & PFM_RECOMMENDS) && isGenericFieldType("Recommends", line))
233 pkg->recommends_str = parseDependsString(line, &pkg->recommends_count);
234 else if((mask & PFM_REPLACES) && isGenericFieldType("Replaces", line))
235 pkg->replaces_str = parseDependsString(line, &pkg->replaces_count);
240 if((mask & PFM_SECTION) && isGenericFieldType("Section:", line))
241 pkg->section = parseGenericFieldType("Section", line);
243 else if((mask & PFM_SHA256SUM) && isGenericFieldType("SHA256sum:", line))
244 pkg->sha256sum = parseGenericFieldType("SHA256sum", line);
246 else if((mask & PFM_SIZE) && isGenericFieldType("Size:", line))
247 pkg->size = parseGenericFieldType("Size", line);
248 else if((mask & PFM_SOURCE) && isGenericFieldType("Source:", line))
249 pkg->source = parseGenericFieldType("Source", line);
250 else if((mask & PFM_STATUS) && isGenericFieldType("Status", line))
251 parseStatus(pkg, line);
252 else if((mask & PFM_SUGGESTS) && isGenericFieldType("Suggests", line))
253 pkg->suggests_str = parseDependsString(line, &pkg->suggests_count);
257 if((mask & PFM_TAGS) && isGenericFieldType("Tags:", line))
258 pkg->tags = parseGenericFieldType("Tags", line);
262 if((mask & PFM_VERSION) && isGenericFieldType("Version", line))
263 parseVersion(pkg, line);
267 if((mask & PFM_DESCRIPTION) && reading_description) {
268 /* we already know it's not blank, so the rest of description */
269 pkg->description = xrealloc(pkg->description,
270 strlen(pkg->description)
271 + 1 + strlen(line) + 1);
272 strcat(pkg->description, "\n");
273 strcat(pkg->description, (line));
274 goto dont_reset_flags;
276 else if((mask && PFM_CONFFILES) && reading_conffiles) {
277 parseConffiles(pkg, line);
278 goto dont_reset_flags;
283 /* For package lists, signifies end of package. */
284 if(line_is_blank(line)) {
289 reading_description = 0;
290 reading_conffiles = 0;
298 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
299 char **buf0, size_t buf0len)
313 if (fgets(buf, buflen, fp) == NULL) {
315 fprintf(stderr, "%s: fgets: %s\n",
316 __FUNCTION__, strerror(errno));
318 } else if (strlen(*buf0) == buflen-1) {
319 fprintf(stderr, "%s: missing new line character"
320 " at end of file!\n",
322 pkg_parse_line(pkg, *buf0, mask);
327 nl = strchr(buf, '\n');
329 if (strlen(buf) < buflen-1) {
331 * Line could be exactly buflen-1 long and
332 * missing a newline, but we won't know until
333 * fgets fails to read more data.
335 fprintf(stderr, "%s: missing new line character"
336 " at end of file!\n",
338 pkg_parse_line(pkg, *buf0, mask);
341 if (buf0len >= EXCESSIVE_LINE_LEN) {
342 fprintf(stderr, "%s: excessively long line at "
343 "%d. Corrupt file?\n",
344 __FUNCTION__, lineno);
350 * Realloc and move buf past the data already read.
351 * |<--------------- buf0len ----------------->|
352 * | |<------- buflen ---->|
353 * |---------------------|---------------------|
358 *buf0 = xrealloc(*buf0, buf0len);
359 buf = *buf0 + buflen -1;
368 if (pkg_parse_line(pkg, *buf0, mask))
376 if (pkg->name == NULL) {
377 /* probably just a blank line */
385 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
389 const size_t len = 4096;
392 ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);