aboutsummaryrefslogtreecommitdiff
path: root/src/engine/engine.c
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-08-03 12:24:54 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-08-03 12:24:54 +0530
commit24b9d0e486461a05c15b7ff6a5a2013767e1fc38 (patch)
treedc228cd088f2f852290ba477d742f361be0e7408 /src/engine/engine.c
parent1a060cc2e39e6ded798a7347a3fc086ba4d73064 (diff)
engine: split into multiple files
Diffstat (limited to 'src/engine/engine.c')
-rw-r--r--src/engine/engine.c129
1 files changed, 129 insertions, 0 deletions
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 <raghus2247@gmail.com>
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#define _GNU_SOURCE
+
+#include <config.h>
+#include <copy.h>
+#include <engine.h>
+#include <filehandler.h>
+#include <lexer.h>
+#include <list.h>
+#include <msg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <util.h>
+
+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);
+}