]> Cypherpunks repositories - keks.git/commitdiff
Use stdin where appropriate
authorSergey Matveev <stargrave@stargrave.org>
Wed, 14 May 2025 08:11:56 +0000 (11:11 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 14 May 2025 08:11:56 +0000 (11:11 +0300)
c/cmd/deatomiser/deatomiser.c
c/cmd/deatomiser/deatomiser.do
c/cmd/lib/stdinread.c [new file with mode: 0644]
c/cmd/lib/stdinread.h [new file with mode: 0644]
c/cmd/pp/pp.c
c/cmd/pp/pp.do
c/cmd/schema-validate/schema-validate.c
c/cmd/schema-validate/schema-validate.do

index 0a0e5227a7612aa22947e42ec38e5a807f6cf5c01e001ab9726ee01c438e302e..9e89f6fad8da704880fb47e03d261e80cad063edd194f790035cfed816e95f67 100644 (file)
@@ -14,6 +14,7 @@
 // License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <ctype.h>
+#include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <keks/err.h>
 
 #include "../lib/hex.h"
-#include "../lib/mmap.h"
 #include "../lib/printai.h"
+#include "../lib/stdinread.h"
 #include "../lib/uuid.h"
 
 int
-main(int argc, char **argv)
+main(void)
 {
-    if (argc < 2) {
-        fputs("Usage: deatomiser FILE\n", stderr);
-        return EXIT_FAILURE;
-    }
     size_t len = 0;
+    size_t cap = (size_t)1 << (size_t)10;
     unsigned char *buf = NULL;
-    if (!Mmap(&buf, &len, argv[1])) {
+    if (!stdinread(&buf, &len, &cap)) {
+        fprintf(stderr, "stdinread(): %s\n", strerror(errno));
         exit(EXIT_FAILURE);
     }
     struct KEKSAtom atom;
index cb6ed26b1d5e32aea4add2f2ef0fc5c53581b7822b9f355a19158e1c71c287f9..f6c18f70f41bb9b5170f1cb3c833141baa6755474d2050eaee8e8d557e26a08b 100644 (file)
@@ -1,4 +1,4 @@
-deps="../lib/hex.o ../lib/mmap.o ../lib/printai.o ../lib/uuid.o"
+deps="../lib/hex.o ../lib/printai.o ../lib/stdinread.o ../lib/uuid.o"
 redo-ifchange $1.c $deps \
     ../../conf/cc ../../conf/cflags ../../conf/ldflags ../../conf/prefix
 read CC <../../conf/cc
diff --git a/c/cmd/lib/stdinread.c b/c/cmd/lib/stdinread.c
new file mode 100644 (file)
index 0000000..4fb4ad5
--- /dev/null
@@ -0,0 +1,42 @@
+#include <errno.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "stdinread.h"
+
+bool
+stdinread(unsigned char **buf, size_t *len, size_t *cap)
+{
+    *buf = malloc(*cap);
+    if (*buf == NULL) {
+        return false;
+    }
+    *len = 0;
+    ssize_t n = 0;
+    for (;;) {
+        errno = 0;
+        n = read(STDIN_FILENO, *buf + *len, *cap - *len);
+        if (n == -1) {
+            return false;
+        }
+        if (n == 0) {
+            break;
+        }
+        *len += (size_t)n;
+        if (*len == *cap) {
+            unsigned char *new = realloc(*buf, *cap * 2);
+            if (new == NULL) {
+                free(*buf);
+                *buf = NULL;
+                *len = 0;
+                return false;
+            }
+            *buf = new;
+            *cap *= 2;
+        }
+    }
+    memset(*buf + *len, 0, *cap - *len);
+    return true;
+}
diff --git a/c/cmd/lib/stdinread.h b/c/cmd/lib/stdinread.h
new file mode 100644 (file)
index 0000000..fad8cae
--- /dev/null
@@ -0,0 +1,5 @@
+#include <stdbool.h>
+#include <stddef.h>
+
+bool
+stdinread(unsigned char **buf, size_t *len, size_t *cap);
index c30d1f1e2cb81449d8be24a9b2727281a09d26d89dba63e9bcccc6316ea04b15..bbd8286e38857acea61ac976e5c9e111fc124a8075afa8087d741b916e471905 100644 (file)
@@ -39,8 +39,8 @@
 #include <keks/items.h>
 
 #include "../lib/hex.h"
-#include "../lib/mmap.h"
 #include "../lib/printai.h"
+#include "../lib/stdinread.h"
 #include "../lib/uuid.h"
 
 static char *ColourRed = "\x1b[31m";
@@ -69,7 +69,7 @@ static void
 usage(void)
 {
     fputs(
-        "Usage: pp [OPTIONS] FILE\n"
+        "Usage: pp [OPTIONS] <FILE\n"
         "  --max-str-len X -- limit output of strings to X (40 by default)\n"
         "  --do-encode     -- encode the data after decoding, just to test\n"
         "  --no-offsets    -- do not print offsets\n"
@@ -344,9 +344,6 @@ main(int argc, char **argv)
     }
     argc -= optind;
     argv += optind;
-    if (argc < 1) {
-        usage();
-    }
 
     if (getenv("NO_COLOR") != NULL) {
         ColourRed = "";
@@ -360,8 +357,10 @@ main(int argc, char **argv)
     }
 
     size_t len = 0;
+    size_t cap = (size_t)1 << (size_t)10;
     unsigned char *buf = NULL;
-    if (!Mmap(&buf, &len, argv[0])) {
+    if (!stdinread(&buf, &len, &cap)) {
+        fprintf(stderr, "stdinread(): %s\n", strerror(errno));
         exit(EXIT_FAILURE);
     }
     OffDigits = NoOffsets ? -1 : (int)(1 + floor(log10((double)len)));
@@ -442,5 +441,6 @@ main(int argc, char **argv)
         assert(off == lenExpected);
         assert(memcmp(dst, buf, lenExpected) == 0);
     }
+    free(buf);
     return EXIT_SUCCESS;
 }
index b49a791550651d609c15fd7d2ea99c1d5dc4c73586e206373e342de9bea0bd49..e3fb1afed7c28b031ff61e37468f50cdf413916d415f63c08fefb5fef423ad11 100644 (file)
@@ -1,4 +1,4 @@
-deps="../lib/hex.o ../lib/mmap.o ../lib/printai.o ../lib/uuid.o"
+deps="../lib/hex.o ../lib/printai.o ../lib/stdinread.o ../lib/uuid.o"
 redo-ifchange $1.c $deps \
     ../../conf/cc ../../conf/cflags ../../conf/ldflags ../../conf/prefix
 read CC <../../conf/cc
index 0a0b46368edc362b7bcb9ef249081ecd874747e4a12464fb78ecacc057f49344..5f50881f332cd9a8c04708cdb846d6b64091312d76d86450ff8d313a07d3d2d3 100644 (file)
@@ -13,6 +13,7 @@
 // You should have received a copy of the GNU Lesser General Public
 // License along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+#include <errno.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <stdio.h>
 #include <keks/err.h>
 #include <keks/items.h>
 #include <keks/schema.h>
+#include <string.h>
 
 #include "../lib/mmap.h"
+#include "../lib/stdinread.h"
 
 static bool
-parse(struct KEKSItems *items, const char *fn)
+parse(struct KEKSItems *items, const unsigned char *buf, const size_t len)
 {
     const ptrdiff_t itemsInitialLen = 2048;
-    size_t len = 0;
-    unsigned char *buf = NULL;
-    if (!Mmap(&buf, &len, fn)) {
-        return false;
-    }
     enum KEKSErr err = KEKSErrInvalid;
     size_t off = 0;
 RetryAfterMagic:
@@ -59,18 +57,33 @@ RetryAfterMagic:
 int
 main(int argc, char **argv)
 {
-    if (argc < 4) {
-        fputs("Usage: schema-validate SCHEMA.keks SCHEMA-NAME DATA.keks\n", stderr);
+    if (argc < 3) {
+        fputs("Usage: schema-validate SCHEMA.keks SCHEMA-NAME <DATA.keks\n", stderr);
+        return EXIT_FAILURE;
+    }
+
+    size_t schemaLen = 0;
+    unsigned char *schemaBuf = NULL;
+    if (!Mmap(&schemaBuf, &schemaLen, argv[1])) {
         return EXIT_FAILURE;
     }
-    struct KEKSItems data;
     struct KEKSItems schema;
-    if (!parse(&schema, argv[1])) {
+    if (!parse(&schema, schemaBuf, schemaLen)) {
         return EXIT_FAILURE;
     }
-    if (!parse(&data, argv[3])) {
+
+    size_t dataLen = 0;
+    size_t dataCap = (size_t)1 << (size_t)10;
+    unsigned char *dataBuf = NULL;
+    if (!stdinread(&dataBuf, &dataLen, &dataCap)) {
+        fprintf(stderr, "stdinread(): %s\n", strerror(errno));
+        exit(EXIT_FAILURE);
+    }
+    struct KEKSItems data;
+    if (!parse(&data, dataBuf, dataLen)) {
         return EXIT_FAILURE;
     }
+
     size_t idxSchema = KEKSItemsGetByKey(&schema, 0, argv[2]);
     if (idxSchema == 0) {
         fputs("can not find specified schema name\n", stderr);
@@ -93,7 +106,8 @@ main(int argc, char **argv)
         default:
             break;
         }
-        fprintf(stderr, "schema:%zu data:%zu: %s\n", err.offSchema, err.offData, err.msg);
+        fprintf(
+            stderr, "schema:%zu data:%zu: %s\n", err.offSchema, err.offData, err.msg);
         return EXIT_FAILURE;
     }
     return EXIT_SUCCESS;
index 659fe5d191bb9395062689bee903f963bd44a55244355cf495e463f1c6f79fd7..94ba68358d71176de9b16b69336b2e1f0e2b8202ad04c495c09a7918e2001ba1 100644 (file)
@@ -1,4 +1,4 @@
-deps="../lib/mmap.o"
+deps="../lib/mmap.o ../lib/stdinread.o"
 redo-ifchange $1.c $deps \
     ../../conf/cc ../../conf/cflags ../../conf/ldflags ../../conf/prefix
 read CC <../../conf/cc