From f2a78d6ff89d9c56406c191a8caa0b57f72ceb0bb24e18019e3dd43a0fabd112 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Wed, 9 Oct 2024 12:58:19 +0300 Subject: [PATCH] Better C printer --- cyac/README | 2 +- cyac/clean | 2 +- cyac/{example => cmd}/.gitignore | 2 +- cyac/cmd/all.do | 1 + cyac/cmd/clean | 3 ++ cyac/{example => cmd}/default.do | 0 cyac/{example/print-map.c => cmd/print.c} | 44 +++++++++++------------ cyac/{example => cmd}/test-vector.c | 0 cyac/example/all.do | 1 - cyac/example/clean | 3 -- cyac/iter.c | 12 +++---- 11 files changed, 33 insertions(+), 37 deletions(-) rename cyac/{example => cmd}/.gitignore (54%) create mode 100644 cyac/cmd/all.do create mode 100755 cyac/cmd/clean rename cyac/{example => cmd}/default.do (100%) rename cyac/{example/print-map.c => cmd/print.c} (86%) rename cyac/{example => cmd}/test-vector.c (100%) delete mode 100644 cyac/example/all.do delete mode 100755 cyac/example/clean diff --git a/cyac/README b/cyac/README index 41c7d66..c12c3fb 100644 --- a/cyac/README +++ b/cyac/README @@ -17,7 +17,7 @@ enc.* contains encoders for various atoms. Containers and blobs must be made manually, by finishing them with proper EOC/BIN and sorting the keys. enctai.* contains converter from UTC to TAI64. -example/test-vector.c is the same structure as in tyac/test-vector.tcl. +cmd/test-vector.c is the same structure as in tyac/test-vector.tcl. Their outputs must be the same. Project uses redo (http://cr.yp.to/redo.html) build system, but it is diff --git a/cyac/clean b/cyac/clean index 4ced8c3..593bb0e 100755 --- a/cyac/clean +++ b/cyac/clean @@ -1,4 +1,4 @@ #!/bin/sh -e -( cd example ; ./clean ) +( cd cmd ; ./clean ) exec rm -f *.o *.a compile_flags.txt diff --git a/cyac/example/.gitignore b/cyac/cmd/.gitignore similarity index 54% rename from cyac/example/.gitignore rename to cyac/cmd/.gitignore index 4681da6..141e7b8 100644 --- a/cyac/example/.gitignore +++ b/cyac/cmd/.gitignore @@ -1,2 +1,2 @@ -/print-map +/print /test-vector diff --git a/cyac/cmd/all.do b/cyac/cmd/all.do new file mode 100644 index 0000000..f4beb87 --- /dev/null +++ b/cyac/cmd/all.do @@ -0,0 +1 @@ +redo-ifchange print test-vector diff --git a/cyac/cmd/clean b/cyac/cmd/clean new file mode 100755 index 0000000..f59dba6 --- /dev/null +++ b/cyac/cmd/clean @@ -0,0 +1,3 @@ +#!/bin/sh -e + +exec rm -f print test-vector diff --git a/cyac/example/default.do b/cyac/cmd/default.do similarity index 100% rename from cyac/example/default.do rename to cyac/cmd/default.do diff --git a/cyac/example/print-map.c b/cyac/cmd/print.c similarity index 86% rename from cyac/example/print-map.c rename to cyac/cmd/print.c index 62d8aec..214fc3f 100644 --- a/cyac/example/print-map.c +++ b/cyac/cmd/print.c @@ -69,6 +69,7 @@ myCb( return YACErrUnexpectedEOC; } } + printf("%04zd ", *off); for (ptrdiff_t i = 0; i < state->indent; i++) { fputs(" ", stdout); } @@ -83,7 +84,6 @@ myCb( enum YACErr err = YACErrInvalid; switch (atom->typ) { case YACItemEOC: - fputs("]\n", stdout); break; case YACItemNIL: fputs("NIL\n", stdout); @@ -96,7 +96,7 @@ myCb( break; case YACItemUUID: printf( - "UUID[%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x]\n", + "UUID(%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x)\n", atom->val.buf[0], atom->val.buf[1], atom->val.buf[2], @@ -121,28 +121,31 @@ myCb( fprintf(stdout, "%zd\n", atom->val.sint); break; case YACItemList: - fputs("LIST [\n", stdout); + fputs("[\n", stdout); state->indent++; err = YACIterList(cbState, atom, off, buf, len, myCb); if (err != YACErrNo) { return err; } + fputs("]\n", stdout); break; case YACItemMap: - fputs("MAP [\n", stdout); + fputs("{\n", stdout); state->indent++; err = YACIterMap(cbState, atom, off, buf, len, myCb); if (err != YACErrNo) { return err; } + fputs("}\n", stdout); break; case YACItemBlob: - fputs("BLOB [\n", stdout); + fputs("BLOB(\n", stdout); state->indent++; err = YACIterBlob(cbState, atom, off, buf, len, myCb); if (err != YACErrNo) { return err; } + fputs(")\n", stdout); break; case YACItemFloat: fputs("FLOAT: TODO\n", stdout); @@ -150,23 +153,23 @@ myCb( case YACItemTAI64: if ((atom->len) == 16) { hex = HexEnc(atom->val.buf, atom->len); - fprintf(stdout, "TAI64NA: %s\n", hex); + fprintf(stdout, "TAI64NA(%s)\n", hex); free(hex); break; } switch (atom->len) { case 8: - fputs("TAI64: ", stdout); + fputs("TAI64(", stdout); break; case 12: - fputs("TAI64N: ", stdout); + fputs("TAI64N(", stdout); break; } struct timeval tv; err = YACTAI64ToTimeval(&tv, atom->val.buf, atom->len); if (err == YACErrTAI64BadNsec) { hex = HexEnc(atom->val.buf, atom->len); - fprintf(stdout, "unrepresentable: %s\n", hex); + fprintf(stdout, "unrepresentable: %s)\n", hex); free(hex); break; } @@ -177,7 +180,7 @@ myCb( struct tm *tm = localtime(&t); if (tm == NULL) { hex = HexEnc(atom->val.buf, atom->len); - fprintf(stdout, "unrepresentable: %s\n", hex); + fprintf(stdout, "unrepresentable: %s)\n", hex); free(hex); break; } @@ -187,11 +190,11 @@ myCb( if ((atom->len) == 12) { fprintf(stdout, ".%zu", tv.tv_usec); } - fputs("\n", stdout); + fputs(")\n", stdout); break; case YACItemBin: hex = HexEnc(atom->val.buf, atom->len); - fprintf(stdout, "BIN(%s)\n", hex); + fprintf(stdout, "%zu:%s\n", atom->len, hex); free(hex); break; case YACItemStr: @@ -201,15 +204,15 @@ myCb( break; case YACItemChunk: hex = HexEnc(atom->val.buf, atom->len); - fprintf(stdout, "CHUNK(%s)\n", hex); + fprintf(stdout, "%s\n", hex); free(hex); break; case YACItemChunkLen: - fprintf(stdout, "chunkLen: %zu\n", atom->val.uint); + fprintf(stdout, "l=%zu\n", atom->val.uint); break; case YACItemRaw: hex = HexEnc(atom->val.buf, atom->len); - fprintf(stdout, "RAW: 0x%X %s\n", atom->tag, hex); + fprintf(stdout, "(t=0x%X l=%zu v=%s)\n", atom->tag, atom->len, hex); free(hex); break; default: @@ -240,16 +243,11 @@ main(int argc, char **argv) fprintf(stderr, "map err: %d\n", err); return EXIT_FAILURE; } - if (atom.typ != YACItemMap) { - fputs("non map\n", stderr); - return EXIT_FAILURE; - } off += atom.off; - struct CbState cbState = {.indent = 1}; - fputs("MAP [\n", stdout); - err = YACIterMap(&cbState, &atom, &off, buf, len, myCb); + struct CbState cbState = {.indent = 0}; + err = myCb(NULL, 0, -1, &cbState, &atom, &off, buf, len); if (err != YACErrNo) { - fprintf(stderr, "iter err: %d\n", err); + fprintf(stderr, "err: %d\n", err); return EXIT_FAILURE; } assert(cbState.indent == 0); diff --git a/cyac/example/test-vector.c b/cyac/cmd/test-vector.c similarity index 100% rename from cyac/example/test-vector.c rename to cyac/cmd/test-vector.c diff --git a/cyac/example/all.do b/cyac/example/all.do deleted file mode 100644 index e32e2ab..0000000 --- a/cyac/example/all.do +++ /dev/null @@ -1 +0,0 @@ -redo-ifchange print-map test-vector diff --git a/cyac/example/clean b/cyac/example/clean deleted file mode 100755 index 2403ed9..0000000 --- a/cyac/example/clean +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/sh -e - -exec rm -f print-map test-vector diff --git a/cyac/iter.c b/cyac/iter.c index a7071cc..9405cca 100644 --- a/cyac/iter.c +++ b/cyac/iter.c @@ -127,7 +127,7 @@ YACIterBlob( return YACErrLenTooBig; } atom->typ = YACItemChunkLen; - err = cb(NULL, 0, 0, cbState, atom, off, buf, len); + err = cb(NULL, 0, -1, cbState, atom, off, buf, len); if (err != YACErrNo) { return err; } @@ -143,6 +143,7 @@ YACIterBlob( if (((ptrdiff_t)len - *off) <= (ptrdiff_t)chunkLen) { return YACErrBlobShortChunk; } + atom->typ = YACItemChunk; atom->val.buf = buf + *off; atom->len = chunkLen; (*off) += chunkLen; @@ -171,12 +172,9 @@ YACIterBlob( default: return YACErrBlobBadAtom; } - if ((atom->len) > 0) { - atom->typ = YACItemChunk; - err = cb(NULL, 0, (ptrdiff_t)n, cbState, atom, off, buf, len); - if (err != YACErrNo) { - return err; - } + err = cb(NULL, 0, (ptrdiff_t)n, cbState, atom, off, buf, len); + if (err != YACErrNo) { + return err; } } atom->typ = YACItemEOC; -- 2.48.1