From 4043285413000703c2363243f0ca203ad01098bf Mon Sep 17 00:00:00 2001 From: Jordan Bancino Date: Sun, 14 May 2023 22:15:48 +0000 Subject: [PATCH] Telodendria and all tools now use the Cytoplasm runtime stub. This reduces the amount of manual work programs have to do, and gives us some free features, like automatic leak reports in all tools. --- src/Main.c | 26 +++++++------------------- tools/bin/td | 8 ++++---- tools/src/http-debug-server.c | 2 +- tools/src/http.c | 30 +++++++++++++++--------------- tools/src/json.c | 18 ++++++++---------- 5 files changed, 35 insertions(+), 49 deletions(-) diff --git a/src/Main.c b/src/Main.c index a94c7db..dd30159 100644 --- a/src/Main.c +++ b/src/Main.c @@ -33,7 +33,7 @@ #include #include -#include +#include #include #include #include @@ -46,6 +46,7 @@ #include #include +#include #include #include #include @@ -90,11 +91,12 @@ typedef enum ArgFlag } ArgFlag; int -main(int argc, char **argv) +Main(Array *args) { int exit; /* Arg parsing */ + ArgParseState arg; int opt; int flags; char *dbPath; @@ -148,12 +150,13 @@ start: TelodendriaPrintHeader(); - while ((opt = getopt(argc, argv, "d:Vv")) != -1) + ArgParseStateInit(&arg); + while ((opt = ArgParse(&arg, args, "d:Vv")) != -1) { switch (opt) { case 'd': - dbPath = optarg; + dbPath = arg.optArg; break; case 'V': flags |= ARG_VERSION; @@ -591,7 +594,6 @@ finish: */ MemoryHook(NULL, NULL); - LogConfigFree(LogConfigGlobal()); StreamClose(logFile); if (restart) @@ -607,19 +609,5 @@ finish: goto start; } - StreamClose(StreamStdout()); - - /* Standard error should never have been opened, but just in case - * it was, this doesn't hurt anything. */ - StreamClose(StreamStderr()); - - /* Generate a memory report if any leaks occurred. At this point no - * memory should be allocated. */ - TelodendriaGenerateMemReport(); - - /* Free any leaked memory now, just in case the operating system - * we're running on won't do it for us. */ - MemoryFreeAll(); - return exit; } diff --git a/tools/bin/td b/tools/bin/td index 37c6064..4b36acf 100644 --- a/tools/bin/td +++ b/tools/bin/td @@ -127,7 +127,7 @@ recipe_cytoplasm() { cd Cytoplasm export TLS_IMPL sh make.sh - CYTOPLASM_FLAGS="-LCytoplasm/out/lib -lcytoplasm $(sh make.sh libs)" + CYTOPLASM_FLAGS="Cytoplasm/out/lib/cytoplasm.o -LCytoplasm/out/lib -lcytoplasm $(sh make.sh libs)" cd - > /dev/null } @@ -193,9 +193,9 @@ recipe_run() { if [ -f "build/$PROG" ]; then "build/$PROG" -d data - if [ -f "data/Memory.txt" ]; then - echo "WARNING: Memory.txt exists in the data directory; this means" - echo "Telodendria is leaking memory. Please fix memory leaks." + if [ -f "data/telodendria-leaked.txt" ]; then + echo "WARNING: Telodendria is leaking memory." + echo "Please fix memory leaks." fi else echo "build/$PROG does not exist; build it first." diff --git a/tools/src/http-debug-server.c b/tools/src/http-debug-server.c index 7fc0741..b113252 100644 --- a/tools/src/http-debug-server.c +++ b/tools/src/http-debug-server.c @@ -70,7 +70,7 @@ HttpHandle(HttpServerContext * cx, void *args) } int -main(void) +Main(void) { struct sigaction sa; HttpServerConfig cfg; diff --git a/tools/src/http.c b/tools/src/http.c index 32a51a9..9dfba5b 100644 --- a/tools/src/http.c +++ b/tools/src/http.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -43,7 +44,7 @@ usage(char *prog) } int -main(int argc, char **argv) +Main(Array *args) { HttpClientContext *cx; HttpStatus res; @@ -55,12 +56,14 @@ main(int argc, char **argv) char *key; char *val; + ArgParseState arg; int flags = 0; int requestFlags = HTTP_FLAG_NONE; int ch; - while ((ch = getopt(argc, argv, "iH:X:d:")) != -1) + ArgParseStateInit(&arg); + while ((ch = ArgParse(&arg, args, "iH:X:d:")) != -1) { switch (ch) { @@ -68,16 +71,16 @@ main(int argc, char **argv) flags |= FLAG_HEADERS; break; case 'X': - method = HttpRequestMethodFromString(optarg); + method = HttpRequestMethodFromString(arg.optArg); if (!method) { - StreamPrintf(StreamStderr(), "Unknown request method: %s\n", optarg); + StreamPrintf(StreamStderr(), "Unknown request method: %s\n", arg.optArg); return 1; } break; case 'H': - key = optarg; - val = optarg; + key = arg.optArg; + val = arg.optArg; while (*val && *val != ':') { @@ -95,24 +98,24 @@ main(int argc, char **argv) HashMapSet(requestHeaders, key, StrDuplicate(val)); break; case 'd': - data = optarg; + data = arg.optArg; break; default: - usage(argv[0]); + usage(ArrayGet(args, 0)); return 1; } } - if (argc - optind < 1) + if (ArraySize(args) - arg.optInd < 1) { - usage(argv[0]); + usage(ArrayGet(args, 0)); return 1; } - uri = UriParse(argv[optind]); + uri = UriParse(ArrayGet(args, arg.optInd)); if (!uri) { - StreamPrintf(StreamStderr(), "Failed to parse URI: %s\n", argv[optind]); + StreamPrintf(StreamStderr(), "Failed to parse URI: %s\n", ArrayGet(args, arg.optInd)); return 1; } @@ -250,8 +253,5 @@ main(int argc, char **argv) HttpClientContextFree(cx); UriFree(uri); - StreamClose(StreamStdout()); - StreamClose(StreamStderr()); - return !(res == HTTP_OK); } diff --git a/tools/src/json.c b/tools/src/json.c index c4d8f0f..dd4469b 100644 --- a/tools/src/json.c +++ b/tools/src/json.c @@ -25,6 +25,7 @@ #include #include +#include #include #include #include @@ -177,27 +178,29 @@ encode(char *str) } int -main(int argc, char **argv) +Main(Array *args) { HashMap *json; int flag = 0; int ch; char *input = NULL; + ArgParseState arg; - while ((ch = getopt(argc, argv, "s:e:")) != -1) + ArgParseStateInit(&arg); + while ((ch = ArgParse(&arg, args, "s:e:")) != -1) { switch (ch) { case 's': flag = FLAG_SELECT; - input = optarg; + input = arg.optArg; break; case 'e': flag = FLAG_ENCODE; - input = optarg; + input = arg.optArg; break; default: - usage(argv[0]); + usage(ArrayGet(args, 0)); return 1; } } @@ -227,10 +230,5 @@ main(int argc, char **argv) break; } - StreamClose(StreamStdout()); - StreamClose(StreamStderr()); - StreamClose(StreamStdin()); - - MemoryFreeAll(); return 0; }