libopkg: do not rely on getline()
[project/opkg-lede.git] / libopkg / pkg_parse.c
index f1725de3a33ad09ebcf2feee3c701b2bc54a1c95..b54ad9aead061af2bc51c33a16dbe464f76c83fc 100644 (file)
 #include "pkg_parse.h"
 #include "libbb/libbb.h"
 
-static int
-is_field(const char *type, const char *line)
-{
-       if (!strncmp(line, type, strlen(type)))
-               return 1;
-       return 0;
-}
-
-static char *
-parse_simple(const char *type, const char *line)
-{
-       return trim_xstrdup(line + strlen(type) + 1);
-}
-
-/*
- * Parse a comma separated string into an array.
- */
-static char **
-parse_comma_separated(const char *raw, unsigned int *count)
-{
-       char **depends = NULL;
-       const char *start, *end;
-       int line_count = 0;
-
-       /* skip past the "Field:" marker */
-       while (*raw && *raw != ':')
-               raw++;
-       raw++;
-
-       if (line_is_blank(raw)) {
-               *count = line_count;
-               return NULL;
-       }
-
-       while (*raw) {
-               depends = xrealloc(depends, sizeof(char *) * (line_count + 1));
-       
-               while (isspace(*raw))
-                       raw++;
-
-               start = raw;
-               while (*raw != ',' && *raw)
-                       raw++;
-               end = raw;
-
-               while (end > start && isspace(*end))
-                       end--;
-
-               depends[line_count] = xstrndup(start, end-start);
-
-               line_count++;
-               if (*raw == ',')
-                   raw++;
-       }
-
-       *count = line_count;
-       return depends;
-}
+#include "parse_util.h"
 
 static void
 parse_status(pkg_t *pkg, const char *sstr)
@@ -139,7 +82,7 @@ parse_version(pkg_t *pkg, const char *vstr)
                pkg->epoch= 0;
        }
 
-       pkg->version= xstrdup(vstr);
+       pkg->version = xstrdup(vstr);
        pkg->revision = strrchr(pkg->version,'-');
 
        if (pkg->revision)
@@ -161,9 +104,11 @@ get_arch_priority(const char *arch)
        return 0;
 }
 
-static int
-pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
+int
+pkg_parse_line(void *ptr, const char *line, uint mask)
 {
+       pkg_t *pkg = (pkg_t *) ptr;
+
        /* these flags are a bit hackish... */
        static int reading_conffiles = 0, reading_description = 0;
        int ret = 0;
@@ -194,7 +139,7 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                        goto dont_reset_flags;
                }
                else if ((mask & PFM_CONFLICTS) && is_field("Conflicts", line))
-                       pkg->conflicts_str = parse_comma_separated(line, &pkg->conflicts_count);
+                       pkg->conflicts_str = parse_list(line, &pkg->conflicts_count, ',', 0);
                break;
 
        case 'D':
@@ -204,7 +149,7 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                        reading_description = 1;
                        goto dont_reset_flags;
                } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
-                       pkg->depends_str = parse_comma_separated(line, &pkg->depends_count);
+                       pkg->depends_str = parse_list(line, &pkg->depends_count, ',', 0);
                break;
 
        case 'E':
@@ -222,45 +167,44 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                break;
 
        case 'I':
-               if ((mask && PFM_INSTALLED_SIZE) && is_field("Installed-Size", line)) {
+               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);
-               } else if ((mask && PFM_INSTALLED_TIME) && is_field("Installed-Time", line)) {
+               } 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);
-               }           
+               }
                break;
 
        case 'M':
-               if (mask && PFM_MD5SUM) {
-                       if (is_field("MD5sum:", line))
-                               pkg->md5sum = parse_simple("MD5sum", line);
+               if ((mask & PFM_MD5SUM) && is_field("MD5sum:", line))
+                       pkg->md5sum = parse_simple("MD5sum", line);
                        /* The old opkg wrote out status files with the wrong
                        * case for MD5sum, let's parse it either way */
-                       else if (is_field("MD5Sum:", line))
-                               pkg->md5sum = parse_simple("MD5Sum", line);
-               else if((mask & PFM_MAINTAINER) && is_field("Maintainer", line))
+               else if ((mask & PFM_MD5SUM) && is_field("MD5Sum:", line)) 
+                       pkg->md5sum = parse_simple("MD5Sum", line);
+               else if((mask & PFM_MAINTAINER) && is_field("Maintainer", line))
                        pkg->maintainer = parse_simple("Maintainer", line);
                break;
 
        case 'P':
-               if ((mask & PFM_PACKAGE) && is_field("Package", line)) 
+               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);
                else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
-                       pkg->provides_str = parse_comma_separated(line, &pkg->provides_count);
+                       pkg->provides_str = parse_list(line, &pkg->provides_count, ',', 0);
                else if ((mask & PFM_PRE_DEPENDS) && is_field("Pre-Depends", line))
-                       pkg->pre_depends_str = parse_comma_separated(line, &pkg->pre_depends_count);
+                       pkg->pre_depends_str = parse_list(line, &pkg->pre_depends_count, ',', 0);
                break;
 
        case 'R':
                if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
-                       pkg->recommends_str = parse_comma_separated(line, &pkg->recommends_count);
+                       pkg->recommends_str = parse_list(line, &pkg->recommends_count, ',', 0);
                else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
-                       pkg->replaces_str = parse_comma_separated(line, &pkg->replaces_count);
+                       pkg->replaces_str = parse_list(line, &pkg->replaces_count, ',', 0);
 
                break;
 
@@ -280,7 +224,7 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                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_comma_separated(line, &pkg->suggests_count);
+                       pkg->suggests_str = parse_list(line, &pkg->suggests_count, ',', 0);
                break;
 
        case 'T':
@@ -301,7 +245,7 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
                        strcat(pkg->description, "\n");
                        strcat(pkg->description, (line));
                        goto dont_reset_flags;
-               } else if ((mask && PFM_CONFFILES) && reading_conffiles) {
+               } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
                        parse_conffiles(pkg, line);
                        goto dont_reset_flags;
                }
@@ -323,91 +267,6 @@ dont_reset_flags:
        return ret;
 }
 
-int
-pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
-                                               char **buf0, size_t buf0len)
-{
-       int ret, lineno;
-       char *buf, *nl;
-       size_t buflen;
-
-       lineno = 1;
-       ret = 0;
-
-       buflen = buf0len;
-       buf = *buf0;
-       buf[0] = '\0';
-
-       while (1) {
-               if (fgets(buf, (int)buflen, fp) == NULL) {
-                       if (ferror(fp)) {
-                               opkg_perror(ERROR, "fgets");
-                               ret = -1;
-                       } else if (strlen(*buf0) == buf0len-1) {
-                               opkg_msg(ERROR, "Missing new line character"
-                                               " at end of file!\n");
-                               pkg_parse_line(pkg, *buf0, mask);
-                       }
-                       break;
-               }
-
-               nl = strchr(buf, '\n');
-               if (nl == NULL) {
-                       if (strlen(buf) < buflen-1) {
-                               /*
-                                * Line could be exactly buflen-1 long and
-                                * missing a newline, but we won't know until
-                                * fgets fails to read more data.
-                                */
-                               opkg_msg(ERROR, "Missing new line character"
-                                               " at end of file!\n");
-                               pkg_parse_line(pkg, *buf0, mask);
-                               break;
-                       }
-                       if (buf0len >= EXCESSIVE_LINE_LEN) {
-                               opkg_msg(ERROR, "Excessively long line at "
-                                       "%d. Corrupt file?\n",
-                                       lineno);
-                               ret = -1;
-                               break;
-                       }
-
-                       /*
-                        * Realloc and point buf past the data already read,
-                        * at the NULL terminator inserted by fgets.
-                        * |<--------------- buf0len ----------------->|
-                        * |                     |<------- buflen ---->|
-                        * |---------------------|---------------------|
-                        * buf0                   buf
-                        */
-                       buflen = buf0len +1;
-                       buf0len *= 2;
-                       *buf0 = xrealloc(*buf0, buf0len);
-                       buf = *buf0 + buflen -2;
-
-                       continue;
-               }
-
-               *nl = '\0';
-
-               lineno++;
-
-               if (pkg_parse_line(pkg, *buf0, mask))
-                       break;
-
-               buf = *buf0;
-               buflen = buf0len;
-               buf[0] = '\0';
-       };
-
-       if (pkg->name == NULL) {
-               /* probably just a blank line */
-               ret = 1;
-       }
-
-       return ret;
-}
-
 int
 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
 {
@@ -416,8 +275,13 @@ pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
        const size_t len = 4096;
 
        buf = xmalloc(len);
-       ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
+       ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf, len);
        free(buf);
 
+       if (pkg->name == NULL) {
+               /* probably just a blank line */
+               ret = 1;
+       }
+
        return ret;
 }