cli: implement --force-checksum
[oweals/opkg-lede.git] / libopkg / pkg_parse.c
index e0d7fce11f8ada0892cbb03b08bcb97d20bc8420..5337f349841b5c11a047af8c1babbec45d541cf9 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <stdio.h>
 #include <ctype.h>
+#include <unistd.h>
 
 #include "pkg.h"
 #include "opkg_utils.h"
@@ -104,9 +105,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;
@@ -237,10 +240,16 @@ pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
 
        case ' ':
                if ((mask & PFM_DESCRIPTION) && reading_description) {
-                       pkg->description = xrealloc(pkg->description,
-                                               strlen(pkg->description)
-                                               + 1 + strlen(line) + 1);
-                       strcat(pkg->description, "\n");
+                       if (isatty(1)) {
+                               pkg->description = xrealloc(pkg->description,
+                                                       strlen(pkg->description)
+                                                       + 1 + strlen(line) + 1);
+                               strcat(pkg->description, "\n");
+                       } else {
+                               pkg->description = xrealloc(pkg->description,
+                                                       strlen(pkg->description)
+                                                       + 1 + strlen(line));
+                       }
                        strcat(pkg->description, (line));
                        goto dont_reset_flags;
                } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
@@ -265,91 +274,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)
 {
@@ -358,8 +282,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;
 }