// 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;
-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
--- /dev/null
+#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;
+}
--- /dev/null
+#include <stdbool.h>
+#include <stddef.h>
+
+bool
+stdinread(unsigned char **buf, size_t *len, size_t *cap);
#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";
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"
}
argc -= optind;
argv += optind;
- if (argc < 1) {
- usage();
- }
if (getenv("NO_COLOR") != NULL) {
ColourRed = "";
}
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)));
assert(off == lenExpected);
assert(memcmp(dst, buf, lenExpected) == 0);
}
+ free(buf);
return EXIT_SUCCESS;
}
-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
// 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:
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);
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;
-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