From 803935c6375e503936c32c70fbb8ca27f3d7850d Mon Sep 17 00:00:00 2001 From: Sage Vaillancourt Date: Tue, 29 Mar 2022 15:09:33 -0400 Subject: [PATCH] Move help directly into env. Fix repl leak. --- src/env.c | 18 ++++++++++-------- src/env.h | 3 --- src/pebblisp.c | 3 ++- src/plfunc.c | 10 ---------- src/plfunc.h | 8 -------- src/web.c | 10 +++++----- src/web.h | 6 ++++-- 7 files changed, 21 insertions(+), 37 deletions(-) diff --git a/src/env.c b/src/env.c index e1ce7d2..ea4d16f 100644 --- a/src/env.c +++ b/src/env.c @@ -282,13 +282,18 @@ void printColored(const char* code) } } -char* getHelp(const char* symbol) -{ +fn(help, + "Displays help text for the given function.\n" + "Currently requires the function name as a string, but future syntactic sugar may\n" + "loosen this requirement.\n" + "(? \"+\") => \"(+ 1 2) => 3\"" +) { + const char* symbol = params[0].string; for (int i = 0; i < currentHelp; i++) { struct helpText h = helpTexts[i]; if (strcmp(symbol, h.symbol) == 0) { - char* text = calloc(sizeof(char), 1024); - char* textCursor = text; + Object text = withLen(1024, TYPE_STRING); + char* textCursor = text.string; textCursor += sprintf(textCursor, "%s", h.help); for (int ti = 0; ti < h.testCount; ti += 2) { const char* test = h.tests[ti]; @@ -303,10 +308,7 @@ char* getHelp(const char* symbol) return text; } } - const char* notFound = "Help not found!"; - char* help = malloc(sizeof(char) * (strlen(notFound) + 1)); - sprintf(help, "%s", notFound); - return help; + return nullTerminated("Help not found!"); } fnn(segfault, "seg", "Induces a segfault.") diff --git a/src/env.h b/src/env.h index a198b0e..93cb070 100644 --- a/src/env.h +++ b/src/env.h @@ -49,9 +49,6 @@ struct StructDef* getStructAt(int i); void addStructDef(struct StructDef def); -/// Needs to be freed! -char* getHelp(const char* symbol); - void printColored(const char* code); int runTests(int detailed); diff --git a/src/pebblisp.c b/src/pebblisp.c index 1151205..2934e99 100644 --- a/src/pebblisp.c +++ b/src/pebblisp.c @@ -610,6 +610,7 @@ void repl(struct Environment* env) size_t length; char *output = stringObj(&o, &length); printColored(output); + free(output); printf("\n"); cleanObject(&o); free(buf); @@ -620,7 +621,7 @@ void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env) { Object args = listObject(); for (int i = 0; i < argc; i++) { - nf_addToList(&args, stringFromSlice(argv[i], strlen(argv[i]))); + nf_addToList(&args, nullTerminated(argv[i])); } addToEnv(env, "args", args); } diff --git a/src/plfunc.c b/src/plfunc.c index 5e951e0..9f50306 100644 --- a/src/plfunc.c +++ b/src/plfunc.c @@ -443,16 +443,6 @@ Object systemCall(Object* params, int length, struct Environment* env) return numberObject(255); } -Object help(Object* params, int length, struct Environment* env) -{ - Object symbol = params[0]; - - char* help = getHelp(symbol.string); - Object helpText = newObject(TYPE_STRING); - helpText.string = help; - return helpText; -} - char* readFileToString(FILE* input) { size_t capacity = 128; diff --git a/src/plfunc.h b/src/plfunc.h index ece3db5..f745567 100644 --- a/src/plfunc.h +++ b/src/plfunc.h @@ -195,14 +195,6 @@ fn(takeInput, "`(def x (input \">> \"))` wait for input, but prompt the user with '>> '.\n" ); -/// STRING => STRING -fn(help, - "Displays help text for the given function.\n" - "Currently requires the function name as a string, but future syntactic sugar may\n" - "loosen this requirement.\n" - "(? \"+\") => \"(+ 1 2) => 3\"" -); - tfn(readFileToObject, ({ isStringy, isStringy }), "Read a file into a string object." diff --git a/src/web.c b/src/web.c index 76b542f..fe26b8d 100644 --- a/src/web.c +++ b/src/web.c @@ -54,7 +54,7 @@ add_query_param(void* queryParamsV, enum MHD_ValueKind kind, const char* key, (void) kind; /* Unused. Silent compiler warning. */ Object* queryParams = queryParamsV; - Object pair = startList(stringFromSlice(key, strlen(key))); + Object pair = startList(nullTerminated(key)); Object parsed; if (isDigit(value[0])) { parsed = parseEval(value, NULL); @@ -73,6 +73,10 @@ answer_to_connection(void* cls, struct MHD_Connection* connection, const char* version, const char* upload_data, size_t* upload_data_size, void** con_cls) { + (void) cls; /* Unused. Silence compiler warning. */ + (void) upload_data_size; /* Unused. Silence compiler warning. */ + (void) con_cls; /* Unused. Silence compiler warning. */ + char* page = NULL; printf("%s :: %s URL: '%s'\n", method, version, url); if (upload_data) { @@ -107,10 +111,6 @@ answer_to_connection(void* cls, struct MHD_Connection* connection, page = "

404, Dumbass.

"; } - (void) cls; /* Unused. Silent compiler warning. */ - (void) upload_data_size; /* Unused. Silent compiler warning. */ - (void) con_cls; /* Unused. Silent compiler warning. */ - struct MHD_Response* response = MHD_create_response_from_buffer( strlen(page), (void*) page, MHD_RESPMEM_PERSISTENT); HttpResult ret = MHD_queue_response(connection, MHD_HTTP_OK, response); diff --git a/src/web.h b/src/web.h index 6e81858..9c539d7 100644 --- a/src/web.h +++ b/src/web.h @@ -10,8 +10,10 @@ fn(startServer, fn(addGetRoute, "Note: Implementation bugs currently prevent using an inline lambda.\n" - " (def homepage (fn () (\"Hello, world!\")));(get \"/\" homepage)\n" - " (def queryPage (fn (req) (req's queryParams)));(get \"/x\" queryPage)\n" + " (def homepage (fn () (\"Hello, world!\")))\n" + " (get \"/\" homepage)\n" + " (def queryPage (fn (req) (req's queryParams)))\n" + " (get \"/x\" queryPage)\n" " (serve)\n" );