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);
106 int parseVersion(pkg_t *pkg, const char *raw)
108 char *colon, *eepochcolon;
113 fprintf(stderr, "%s: ERROR: version string is empty", __FUNCTION__);
117 if (strncmp(raw, "Version:", 8) == 0) {
120 while (*raw && isspace(*raw)) {
124 colon= strchr(raw,':');
126 epoch= strtoul(raw,&eepochcolon,10);
127 if (colon != eepochcolon) {
128 fprintf(stderr, "%s: ERROR: epoch in version is not number", __FUNCTION__);
132 fprintf(stderr, "%s: ERROR: nothing after colon in version number", __FUNCTION__);
145 pkg->version= xcalloc(1, strlen(raw)+1);
146 strcpy(pkg->version, raw);
149 hyphen= strrchr(pkg->version,'-');
153 pkg->revision = hyphen;
160 pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
162 /* these flags are a bit hackish... */
163 static int reading_conffiles = 0, reading_description = 0;
167 if((mask & PFM_ARCHITECTURE ) && isGenericFieldType("Architecture:", line))
168 pkg->architecture = parseGenericFieldType("Architecture", line);
169 else if((mask & PFM_AUTO_INSTALLED) && isGenericFieldType("Auto-Installed:", line)) {
170 char *auto_installed_value;
171 auto_installed_value = parseGenericFieldType("Auto-Installed:", line);
172 if (strcmp(auto_installed_value, "yes") == 0) {
173 pkg->auto_installed = 1;
175 free(auto_installed_value);
180 if((mask & PFM_CONFFILES) && isGenericFieldType("Conffiles", line)){
181 parseConffiles(pkg, line);
182 reading_conffiles = 1;
183 reading_description = 0;
184 goto dont_reset_flags;
186 else if((mask & PFM_CONFLICTS) && isGenericFieldType("Conflicts", line))
187 pkg->conflicts_str = parseDependsString(line, &pkg->conflicts_count);
191 if((mask & PFM_DESCRIPTION) && isGenericFieldType("Description", line)) {
192 pkg->description = parseGenericFieldType("Description", line);
193 reading_conffiles = 0;
194 reading_description = 1;
195 goto dont_reset_flags;
197 else if((mask & PFM_DEPENDS) && isGenericFieldType("Depends", line))
198 pkg->depends_str = parseDependsString(line, &pkg->depends_count);
202 if((mask & PFM_ESSENTIAL) && isGenericFieldType("Essential:", line)) {
203 char *essential_value;
204 essential_value = parseGenericFieldType("Essential", line);
205 if (strcmp(essential_value, "yes") == 0) {
208 free(essential_value);
213 if((mask & PFM_FILENAME) && isGenericFieldType("Filename:", line))
214 pkg->filename = parseGenericFieldType("Filename", line);
218 if((mask && PFM_INSTALLED_SIZE) && isGenericFieldType("Installed-Size:", line))
219 pkg->installed_size = parseGenericFieldType("Installed-Size", line);
220 else if((mask && PFM_INSTALLED_TIME) && isGenericFieldType("Installed-Time:", line)) {
221 char *time_str = parseGenericFieldType("Installed-Time", line);
222 pkg->installed_time = strtoul(time_str, NULL, 0);
228 if(mask && PFM_MD5SUM) {
229 if (isGenericFieldType("MD5sum:", line))
230 pkg->md5sum = parseGenericFieldType("MD5sum", line);
231 /* The old opkg wrote out status files with the wrong
232 * case for MD5sum, let's parse it either way */
233 else if(isGenericFieldType("MD5Sum:", line))
234 pkg->md5sum = parseGenericFieldType("MD5Sum", line);
235 } else if((mask & PFM_MAINTAINER) && isGenericFieldType("Maintainer", line))
236 pkg->maintainer = parseGenericFieldType("Maintainer", line);
240 if((mask & PFM_PACKAGE) && isGenericFieldType("Package:", line))
241 pkg->name = parseGenericFieldType("Package", line);
242 else if((mask & PFM_PRIORITY) && isGenericFieldType("Priority:", line))
243 pkg->priority = parseGenericFieldType("Priority", line);
244 else if((mask & PFM_PROVIDES) && isGenericFieldType("Provides", line)){
245 pkg->provides_str = parseDependsString(line, &pkg->provides_count);
247 else if((mask & PFM_PRE_DEPENDS) && isGenericFieldType("Pre-Depends", line))
248 pkg->pre_depends_str = parseDependsString(line, &pkg->pre_depends_count);
252 if((mask & PFM_RECOMMENDS) && isGenericFieldType("Recommends", line))
253 pkg->recommends_str = parseDependsString(line, &pkg->recommends_count);
254 else if((mask & PFM_REPLACES) && isGenericFieldType("Replaces", line))
255 pkg->replaces_str = parseDependsString(line, &pkg->replaces_count);
260 if((mask & PFM_SECTION) && isGenericFieldType("Section:", line))
261 pkg->section = parseGenericFieldType("Section", line);
263 else if((mask & PFM_SHA256SUM) && isGenericFieldType("SHA256sum:", line))
264 pkg->sha256sum = parseGenericFieldType("SHA256sum", line);
266 else if((mask & PFM_SIZE) && isGenericFieldType("Size:", line))
267 pkg->size = parseGenericFieldType("Size", line);
268 else if((mask & PFM_SOURCE) && isGenericFieldType("Source:", line))
269 pkg->source = parseGenericFieldType("Source", line);
270 else if((mask & PFM_STATUS) && isGenericFieldType("Status", line))
271 parseStatus(pkg, line);
272 else if((mask & PFM_SUGGESTS) && isGenericFieldType("Suggests", line))
273 pkg->suggests_str = parseDependsString(line, &pkg->suggests_count);
277 if((mask & PFM_TAGS) && isGenericFieldType("Tags:", line))
278 pkg->tags = parseGenericFieldType("Tags", line);
282 if((mask & PFM_VERSION) && isGenericFieldType("Version", line))
283 parseVersion(pkg, line);
287 if((mask & PFM_DESCRIPTION) && reading_description) {
288 /* we already know it's not blank, so the rest of description */
289 pkg->description = xrealloc(pkg->description,
290 strlen(pkg->description)
291 + 1 + strlen(line) + 1);
292 strcat(pkg->description, "\n");
293 strcat(pkg->description, (line));
294 goto dont_reset_flags;
296 else if((mask && PFM_CONFFILES) && reading_conffiles) {
297 parseConffiles(pkg, line);
298 goto dont_reset_flags;
303 /* For package lists, signifies end of package. */
304 if(line_is_blank(line)) {
309 reading_description = 0;
310 reading_conffiles = 0;
318 pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
319 char **buf0, size_t buf0len)
333 if (fgets(buf, buflen, fp) == NULL) {
335 fprintf(stderr, "%s: fgets: %s\n",
336 __FUNCTION__, strerror(errno));
338 } else if (strlen(*buf0) == buflen-1) {
339 fprintf(stderr, "%s: missing new line character"
340 " at end of file!\n",
342 pkg_parse_line(pkg, *buf0, mask);
347 nl = strchr(buf, '\n');
349 if (strlen(buf) < buflen-1) {
351 * Line could be exactly buflen-1 long and
352 * missing a newline, but we won't know until
353 * fgets fails to read more data.
355 fprintf(stderr, "%s: missing new line character"
356 " at end of file!\n",
358 pkg_parse_line(pkg, *buf0, mask);
361 if (buf0len >= EXCESSIVE_LINE_LEN) {
362 fprintf(stderr, "%s: excessively long line at "
363 "%d. Corrupt file?\n",
364 __FUNCTION__, lineno);
370 * Realloc and move buf past the data already read.
371 * |<--------------- buf0len ----------------->|
372 * | |<------- buflen ---->|
373 * |---------------------|---------------------|
378 *buf0 = xrealloc(*buf0, buf0len);
379 buf = *buf0 + buflen -1;
388 if (pkg_parse_line(pkg, *buf0, mask))
392 fprintf(stderr, "%s: %s\n", __FUNCTION__, pkg->name);
399 if (pkg->name == NULL) {
400 /* probably just a blank line */
408 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
412 const size_t len = 4096;
415 ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);