pkg: move active_list structure out of pkg_t
[oweals/opkg-lede.git] / libopkg / pkg_parse.c
index f9da0cc2c308affd5be17679641739e44a68021f..cfa551e100bf82c98dab243677e5287befea3bea 100644 (file)
@@ -25,6 +25,7 @@
 #include "pkg.h"
 #include "opkg_utils.h"
 #include "pkg_parse.h"
+#include "pkg_depends.h"
 #include "libbb/libbb.h"
 
 #include "parse_util.h"
@@ -46,6 +47,7 @@ static void parse_status(pkg_t * pkg, const char *sstr)
 
 static void parse_conffiles(pkg_t * pkg, const char *cstr)
 {
+       conffile_list_t *cl;
        char file_name[1024], md5sum[85];
 
        if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
@@ -54,12 +56,15 @@ static void parse_conffiles(pkg_t * pkg, const char *cstr)
                return;
        }
 
-       conffile_list_append(&pkg->conffiles, file_name, md5sum);
+       cl = pkg_get_ptr(pkg, PKG_CONFFILES);
+
+       if (cl)
+               conffile_list_append(cl, file_name, md5sum);
 }
 
 int parse_version(pkg_t * pkg, const char *vstr)
 {
-       char *colon;
+       char *colon, *rev;
 
        if (strncmp(vstr, "Version:", 8) == 0)
                vstr += 8;
@@ -70,20 +75,21 @@ int parse_version(pkg_t * pkg, const char *vstr)
        colon = strchr(vstr, ':');
        if (colon) {
                errno = 0;
-               pkg->epoch = strtoul(vstr, NULL, 10);
+               pkg_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10));
                if (errno) {
                        opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
                }
                vstr = ++colon;
-       } else {
-               pkg->epoch = 0;
        }
 
-       pkg->version = xstrdup(vstr);
-       pkg->revision = strrchr(pkg->version, '-');
+       rev = strrchr(vstr, '-');
+
+       if (rev) {
+               *rev++ = '\0';
+               pkg_set_string(pkg, PKG_REVISION, rev);
+       }
 
-       if (pkg->revision)
-               *pkg->revision++ = '\0';
+       pkg_set_string(pkg, PKG_VERSION, vstr);
 
        return 0;
 }
@@ -103,9 +109,11 @@ static int get_arch_priority(const char *arch)
 int pkg_parse_line(void *ptr, const char *line, uint mask)
 {
        pkg_t *pkg = (pkg_t *) ptr;
+       abstract_pkg_t *ab_pkg = NULL;
 
        /* these flags are a bit hackish... */
        static int reading_conffiles = 0, reading_description = 0;
+       static char *description = NULL;
        int ret = 0;
 
        /* Exclude globally masked fields. */
@@ -117,9 +125,9 @@ int pkg_parse_line(void *ptr, const char *line, uint mask)
        switch (*line) {
        case 'A':
                if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line)) {
-                       pkg->architecture = parse_simple("Architecture", line);
-                       pkg->arch_priority =
-                           get_arch_priority(pkg->architecture);
+                       pkg_set_int(pkg, PKG_ARCH_PRIORITY, get_arch_priority(
+                               pkg_set_string(pkg, PKG_ARCHITECTURE, line + strlen("Architecture") + 1)));
+
                } else if ((mask & PFM_AUTO_INSTALLED)
                           && is_field("Auto-Installed", line)) {
                        char *tmp = parse_simple("Auto-Installed", line);
@@ -136,19 +144,17 @@ int pkg_parse_line(void *ptr, const char *line, uint mask)
                        goto dont_reset_flags;
                } else if ((mask & PFM_CONFLICTS)
                           && is_field("Conflicts", line))
-                       pkg->conflicts_str =
-                           parse_list(line, &pkg->conflicts_count, ',', 0);
+                       parse_deplist(pkg, CONFLICTS, line + strlen("Conflicts") + 1);
                break;
 
        case 'D':
                if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
-                       pkg->description = parse_simple("Description", line);
+                       description = parse_simple("Description", line);
                        reading_conffiles = 0;
                        reading_description = 1;
                        goto dont_reset_flags;
                } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
-                       pkg->depends_str =
-                           parse_list(line, &pkg->depends_count, ',', 0);
+                       parse_deplist(pkg, DEPEND, line + strlen("Depends") + 1);
                break;
 
        case 'E':
@@ -162,82 +168,70 @@ int pkg_parse_line(void *ptr, const char *line, uint mask)
 
        case 'F':
                if ((mask & PFM_FILENAME) && is_field("Filename", line))
-                       pkg->filename = parse_simple("Filename", line);
+                       pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1);
                break;
 
        case 'I':
                if ((mask & PFM_INSTALLED_SIZE)
                    && is_field("Installed-Size", line)) {
-                       char *tmp = parse_simple("Installed-Size", line);
-                       pkg->installed_size = strtoul(tmp, NULL, 0);
-                       free(tmp);
+                       pkg_set_int(pkg, PKG_INSTALLED_SIZE, strtoul(line + strlen("Installed-Size") + 1, NULL, 0));
                } else if ((mask & PFM_INSTALLED_TIME)
                           && is_field("Installed-Time", line)) {
-                       char *tmp = parse_simple("Installed-Time", line);
-                       pkg->installed_time = strtoul(tmp, NULL, 0);
-                       free(tmp);
+                       pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0));
                }
                break;
 
        case 'M':
                if ((mask & PFM_MD5SUM) && is_field("MD5sum:", line))
-                       pkg->md5sum = parse_simple("MD5sum", line);
+                       pkg_set_string(pkg, PKG_MD5SUM, line + strlen("MD5sum") + 1);
                /* The old opkg wrote out status files with the wrong
                 * case for MD5sum, let's parse it either way */
                else if ((mask & PFM_MD5SUM) && is_field("MD5Sum:", line))
-                       pkg->md5sum = parse_simple("MD5Sum", line);
+                       pkg_set_string(pkg, PKG_MD5SUM, line + strlen("MD5Sum") + 1);
                else if ((mask & PFM_MAINTAINER)
                         && is_field("Maintainer", line))
-                       pkg->maintainer = parse_simple("Maintainer", line);
+                       pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1);
                break;
 
        case 'P':
                if ((mask & PFM_PACKAGE) && is_field("Package", line))
                        pkg->name = parse_simple("Package", line);
                else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
-                       pkg->priority = parse_simple("Priority", line);
+                       pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
                else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
-                       pkg->provides_str =
-                           parse_list(line, &pkg->provides_count, ',', 0);
+                       parse_providelist(pkg, line + strlen("Provides") + 1);
                else if ((mask & PFM_PRE_DEPENDS)
                         && is_field("Pre-Depends", line))
-                       pkg->pre_depends_str =
-                           parse_list(line, &pkg->pre_depends_count, ',', 0);
+                       parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1);
                break;
 
        case 'R':
                if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
-                       pkg->recommends_str =
-                           parse_list(line, &pkg->recommends_count, ',', 0);
+                       parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1);
                else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
-                       pkg->replaces_str =
-                           parse_list(line, &pkg->replaces_count, ',', 0);
-
+                       parse_replacelist(pkg, line + strlen("Replaces") + 1);
                break;
 
        case 'S':
                if ((mask & PFM_SECTION) && is_field("Section", line))
-                       pkg->section = parse_simple("Section", line);
+                       pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
 #ifdef HAVE_SHA256
                else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
-                       pkg->sha256sum = parse_simple("SHA256sum", line);
+                       pkg_set_string(pkg, PKG_SHA256SUM, line + strlen("SHA256sum") + 1);
 #endif
                else if ((mask & PFM_SIZE) && is_field("Size", line)) {
-                       char *tmp = parse_simple("Size", line);
-                       pkg->size = strtoul(tmp, NULL, 0);
-                       free(tmp);
+                       pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0));
                } else if ((mask & PFM_SOURCE) && is_field("Source", line))
-                       pkg->source = parse_simple("Source", line);
+                       pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
                else if ((mask & PFM_STATUS) && is_field("Status", line))
                        parse_status(pkg, line);
                else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
-                       pkg->suggests_str =
-                           parse_list(line, &pkg->suggests_count, ',', 0);
+                       parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1);
                break;
 
        case 'T':
                if ((mask & PFM_TAGS) && is_field("Tags", line))
-                       pkg->tags = parse_simple("Tags", line);
+                       pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
                break;
 
        case 'V':
@@ -248,19 +242,17 @@ int pkg_parse_line(void *ptr, const char *line, uint mask)
        case ' ':
                if ((mask & PFM_DESCRIPTION) && reading_description) {
                        if (isatty(1)) {
-                               pkg->description = xrealloc(pkg->description,
-                                                           strlen(pkg->
-                                                                  description)
+                               description = xrealloc(description,
+                                                           strlen(description)
                                                            + 1 + strlen(line) +
                                                            1);
-                               strcat(pkg->description, "\n");
+                               strcat(description, "\n");
                        } else {
-                               pkg->description = xrealloc(pkg->description,
-                                                           strlen(pkg->
-                                                                  description)
+                               description = xrealloc(description,
+                                                           strlen(description)
                                                            + 1 + strlen(line));
                        }
-                       strcat(pkg->description, (line));
+                       strcat(description, (line));
                        goto dont_reset_flags;
                } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
                        parse_conffiles(pkg, line);
@@ -276,7 +268,13 @@ int pkg_parse_line(void *ptr, const char *line, uint mask)
                }
        }
 
-       reading_description = 0;
+       if (reading_description && description) {
+               pkg_set_string(pkg, PKG_DESCRIPTION, description);
+               free(description);
+               reading_description = 0;
+               description = NULL;
+       }
+
        reading_conffiles = 0;
 
 dont_reset_flags: