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.
This commit is contained in:
Jordan Bancino 2023-05-14 22:15:48 +00:00
parent 8021cff122
commit 4043285413
5 changed files with 35 additions and 49 deletions

View file

@ -33,7 +33,7 @@
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <Telodendria.h> #include <Args.h>
#include <Memory.h> #include <Memory.h>
#include <Config.h> #include <Config.h>
#include <Log.h> #include <Log.h>
@ -46,6 +46,7 @@
#include <Util.h> #include <Util.h>
#include <Str.h> #include <Str.h>
#include <Telodendria.h>
#include <Matrix.h> #include <Matrix.h>
#include <User.h> #include <User.h>
#include <RegToken.h> #include <RegToken.h>
@ -90,11 +91,12 @@ typedef enum ArgFlag
} ArgFlag; } ArgFlag;
int int
main(int argc, char **argv) Main(Array *args)
{ {
int exit; int exit;
/* Arg parsing */ /* Arg parsing */
ArgParseState arg;
int opt; int opt;
int flags; int flags;
char *dbPath; char *dbPath;
@ -148,12 +150,13 @@ start:
TelodendriaPrintHeader(); TelodendriaPrintHeader();
while ((opt = getopt(argc, argv, "d:Vv")) != -1) ArgParseStateInit(&arg);
while ((opt = ArgParse(&arg, args, "d:Vv")) != -1)
{ {
switch (opt) switch (opt)
{ {
case 'd': case 'd':
dbPath = optarg; dbPath = arg.optArg;
break; break;
case 'V': case 'V':
flags |= ARG_VERSION; flags |= ARG_VERSION;
@ -591,7 +594,6 @@ finish:
*/ */
MemoryHook(NULL, NULL); MemoryHook(NULL, NULL);
LogConfigFree(LogConfigGlobal());
StreamClose(logFile); StreamClose(logFile);
if (restart) if (restart)
@ -607,19 +609,5 @@ finish:
goto start; 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; return exit;
} }

View file

@ -127,7 +127,7 @@ recipe_cytoplasm() {
cd Cytoplasm cd Cytoplasm
export TLS_IMPL export TLS_IMPL
sh make.sh 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 cd - > /dev/null
} }
@ -193,9 +193,9 @@ recipe_run() {
if [ -f "build/$PROG" ]; then if [ -f "build/$PROG" ]; then
"build/$PROG" -d data "build/$PROG" -d data
if [ -f "data/Memory.txt" ]; then if [ -f "data/telodendria-leaked.txt" ]; then
echo "WARNING: Memory.txt exists in the data directory; this means" echo "WARNING: Telodendria is leaking memory."
echo "Telodendria is leaking memory. Please fix memory leaks." echo "Please fix memory leaks."
fi fi
else else
echo "build/$PROG does not exist; build it first." echo "build/$PROG does not exist; build it first."

View file

@ -70,7 +70,7 @@ HttpHandle(HttpServerContext * cx, void *args)
} }
int int
main(void) Main(void)
{ {
struct sigaction sa; struct sigaction sa;
HttpServerConfig cfg; HttpServerConfig cfg;

View file

@ -28,6 +28,7 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <Args.h>
#include <Memory.h> #include <Memory.h>
#include <Str.h> #include <Str.h>
#include <HashMap.h> #include <HashMap.h>
@ -43,7 +44,7 @@ usage(char *prog)
} }
int int
main(int argc, char **argv) Main(Array *args)
{ {
HttpClientContext *cx; HttpClientContext *cx;
HttpStatus res; HttpStatus res;
@ -55,12 +56,14 @@ main(int argc, char **argv)
char *key; char *key;
char *val; char *val;
ArgParseState arg;
int flags = 0; int flags = 0;
int requestFlags = HTTP_FLAG_NONE; int requestFlags = HTTP_FLAG_NONE;
int ch; int ch;
while ((ch = getopt(argc, argv, "iH:X:d:")) != -1) ArgParseStateInit(&arg);
while ((ch = ArgParse(&arg, args, "iH:X:d:")) != -1)
{ {
switch (ch) switch (ch)
{ {
@ -68,16 +71,16 @@ main(int argc, char **argv)
flags |= FLAG_HEADERS; flags |= FLAG_HEADERS;
break; break;
case 'X': case 'X':
method = HttpRequestMethodFromString(optarg); method = HttpRequestMethodFromString(arg.optArg);
if (!method) if (!method)
{ {
StreamPrintf(StreamStderr(), "Unknown request method: %s\n", optarg); StreamPrintf(StreamStderr(), "Unknown request method: %s\n", arg.optArg);
return 1; return 1;
} }
break; break;
case 'H': case 'H':
key = optarg; key = arg.optArg;
val = optarg; val = arg.optArg;
while (*val && *val != ':') while (*val && *val != ':')
{ {
@ -95,24 +98,24 @@ main(int argc, char **argv)
HashMapSet(requestHeaders, key, StrDuplicate(val)); HashMapSet(requestHeaders, key, StrDuplicate(val));
break; break;
case 'd': case 'd':
data = optarg; data = arg.optArg;
break; break;
default: default:
usage(argv[0]); usage(ArrayGet(args, 0));
return 1; return 1;
} }
} }
if (argc - optind < 1) if (ArraySize(args) - arg.optInd < 1)
{ {
usage(argv[0]); usage(ArrayGet(args, 0));
return 1; return 1;
} }
uri = UriParse(argv[optind]); uri = UriParse(ArrayGet(args, arg.optInd));
if (!uri) 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; return 1;
} }
@ -250,8 +253,5 @@ main(int argc, char **argv)
HttpClientContextFree(cx); HttpClientContextFree(cx);
UriFree(uri); UriFree(uri);
StreamClose(StreamStdout());
StreamClose(StreamStderr());
return !(res == HTTP_OK); return !(res == HTTP_OK);
} }

View file

@ -25,6 +25,7 @@
#include <string.h> #include <string.h>
#include <getopt.h> #include <getopt.h>
#include <Args.h>
#include <Array.h> #include <Array.h>
#include <HashMap.h> #include <HashMap.h>
#include <Str.h> #include <Str.h>
@ -177,27 +178,29 @@ encode(char *str)
} }
int int
main(int argc, char **argv) Main(Array *args)
{ {
HashMap *json; HashMap *json;
int flag = 0; int flag = 0;
int ch; int ch;
char *input = NULL; 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) switch (ch)
{ {
case 's': case 's':
flag = FLAG_SELECT; flag = FLAG_SELECT;
input = optarg; input = arg.optArg;
break; break;
case 'e': case 'e':
flag = FLAG_ENCODE; flag = FLAG_ENCODE;
input = optarg; input = arg.optArg;
break; break;
default: default:
usage(argv[0]); usage(ArrayGet(args, 0));
return 1; return 1;
} }
} }
@ -227,10 +230,5 @@ main(int argc, char **argv)
break; break;
} }
StreamClose(StreamStdout());
StreamClose(StreamStderr());
StreamClose(StreamStdin());
MemoryFreeAll();
return 0; return 0;
} }