From 24b9d0e486461a05c15b7ff6a5a2013767e1fc38 Mon Sep 17 00:00:00 2001 From: Raghuram Subramani Date: Sun, 3 Aug 2025 12:24:54 +0530 Subject: engine: split into multiple files --- src/engine/engine.c | 129 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 src/engine/engine.c (limited to 'src/engine/engine.c') diff --git a/src/engine/engine.c b/src/engine/engine.c new file mode 100644 index 0000000..b1dccf4 --- /dev/null +++ b/src/engine/engine.c @@ -0,0 +1,129 @@ +/* + * msg + * Copyright (C) 2025 Raghuram Subramani + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +extern msg_t *msg; + +engine_t * +engine_ingest(char **buffer) +{ + engine_t *engine = malloc(sizeof(engine_t)); + engine->config = NULL; + + char *p = strstr(*buffer, "---"); + if (p != NULL) { + char *config; + asprintf(&config, "%.*s\n", (int) (p - *buffer), *buffer); + engine->config = config_parse(config); + free(config); + + char *tempbuffer = strdup(p); + + free(*buffer); + asprintf(buffer, "%s", tempbuffer + strlen("---")); + + free(tempbuffer); + } + + key_match_t *match; + engine->content_headers = list_create(sizeof(contentfor_operand_t)); + if (engine->content_headers == NULL) { + printf("Could not create content_headers\n"); + return NULL; + } + + size_t skip = 0; + while (true) { + match = find_next_key(*buffer, skip); + if (match == NULL) + break; + +#ifdef DEBUG + printf("Match: %.*s LENGTH(%d) OFFSET(%d)\n", + match->length, + *buffer + match->offset, + match->length, + match->offset); +#endif + + directive_t *directive = find_directive(*buffer, match); + if (directive == NULL) { + printf( + "Unknown directive: %.*s\n", match->length, *buffer + match->offset); + + break; + } + + switch (directive->type) { + case INCLUDE: + handle_include(buffer, match, directive); + break; + case CONTENTFOR: + handle_contentfor(buffer, match, directive, engine->content_headers); + break; + case EACHDO: + handle_eachdo(buffer, match, directive); + break; + + case PUTPAGE: + /* TODO: handle */ + case PUT: + case ENDEACHDO: + case BODY: + case CONTENT: + case ENDCONTENT: + case _RAW: + skip++; + break; + } + + directive_delete(directive); + free(match); + } + + return engine; +} + +void +engine_delete(engine_t *engine) +{ + if (engine->config != NULL) + config_delete(engine->config); + + for (size_t i = 0; i < engine->content_headers->size; i++) { + contentfor_operand_t *operand = list_get(engine->content_headers, i); + free(operand->content); + free(operand->key); + } + list_delete(engine->content_headers); + free(engine); +} -- cgit v1.2.3