aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-07-30 13:01:44 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-07-30 13:01:44 +0530
commit1c63c8b794a7bf85acad42637757e6b1e2f10e03 (patch)
treea5f26bc36469b8f3340df25f6804c9d751bc0d04
parent7e754c2410fed09cfb30bc9a4799829cd804b063 (diff)
main: move msg into a separate context, and let main call it
-rw-r--r--CMakeLists.txt1
-rw-r--r--include/copy.h3
-rw-r--r--include/engine.h2
-rw-r--r--include/msg.h (renamed from include/main.h)5
-rw-r--r--include/template.h2
-rw-r--r--src/copy.c2
-rw-r--r--src/engine.c2
-rw-r--r--src/main.c138
-rw-r--r--src/msg.c161
-rw-r--r--src/template.c2
10 files changed, 172 insertions, 146 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e2ed34e..ead810f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -9,6 +9,7 @@ set(SRC
src/lexer.c
src/list.c
src/main.c
+ src/msg.c
src/template.c
src/util.c
)
diff --git a/include/copy.h b/include/copy.h
index 2e61f36..050ad80 100644
--- a/include/copy.h
+++ b/include/copy.h
@@ -16,15 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
#ifndef __COPY_H
#define __COPY_H
#include <ftw.h>
#include <sys/stat.h>
-#define OUTPUT "dist"
-
typedef struct FTW FTW;
int copy_recursively(const char *fpath,
diff --git a/include/engine.h b/include/engine.h
index 17b05ea..86aa167 100644
--- a/include/engine.h
+++ b/include/engine.h
@@ -22,8 +22,6 @@
#include <lexer.h>
#include <list.h>
-#define PARTIALS "partials"
-
list_t *engine_ingest(char **buffer);
void handle_include(char **buffer, key_match_t *match, directive_t *directive);
void handle_contentfor(char **buffer,
diff --git a/include/main.h b/include/msg.h
index 9ac266f..631c981 100644
--- a/include/main.h
+++ b/include/msg.h
@@ -21,9 +21,14 @@
#define ASSETS "assets"
#define CONFIG_FILE "config.cfg"
+#define BASE_TEMPLATE "base.html"
+#define OUTPUT "dist"
+#define PARTIALS "partials"
typedef struct {
char *base_directory;
} msg_t;
+int run(int argc, char **argv);
+
#endif
diff --git a/include/template.h b/include/template.h
index 4b656f8..c489e45 100644
--- a/include/template.h
+++ b/include/template.h
@@ -23,8 +23,6 @@
#include <stdbool.h>
#include <stdio.h>
-#define BASE_TEMPLATE "base.html"
-
typedef struct {
list_t *components;
} template_t;
diff --git a/src/copy.c b/src/copy.c
index 525d369..9a2816c 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -22,7 +22,7 @@
#include <fcntl.h>
#include <filehandler.h>
#include <ftw.h>
-#include <main.h>
+#include <msg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/sendfile.h>
diff --git a/src/engine.c b/src/engine.c
index 778c2ab..b9825bf 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -25,7 +25,7 @@
#include <filehandler.h>
#include <lexer.h>
#include <list.h>
-#include <main.h>
+#include <msg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
diff --git a/src/main.c b/src/main.c
index 0d041c6..5744874 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,144 +18,10 @@
#define _GNU_SOURCE
-#include <config.h>
-#include <copy.h>
-#include <engine.h>
-#include <errno.h>
-#include <filehandler.h>
-#include <ftw.h>
-#include <lexer.h>
-#include <libgen.h>
-#include <list.h>
-#include <main.h>
-#include <mkdio.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/stat.h>
-#include <template.h>
-#include <util.h>
-
-template_t *base_template;
-msg_t *msg;
-
-void
-handle_file(const char *path)
-{
- char *inpath;
- char *outpath;
-
- asprintf(&inpath, "%s/%s", msg->base_directory, path);
-
- char *dot = strrchr(inpath, '.');
- if (dot && strcmp(dot, ".md") == 0) {
- asprintf(&outpath, "%s/%.*s.html", OUTPUT, (int) strlen(path) - 3, path);
- } else {
- asprintf(&outpath, "%s/%s", OUTPUT, path);
- }
-
- char *temp_outpath = strdup(outpath);
- char *directory = dirname(temp_outpath);
- char *next = calloc(strlen(directory) + 1, sizeof(char));
- strcpy(next, "");
-
- char *token;
- for (token = strtok(directory, "/"); token != NULL;
- token = strtok(NULL, "/")) {
- if (strcmp(next, "") != 0) {
- strcat(next, "/");
- }
-
- strcat(next, token);
- mkdir(next, 0700);
- }
-
- free(next);
- free(temp_outpath);
-
- FILE *in = fopen(inpath, "r");
- if (in == NULL) {
- printf("Failed to open %s\n", inpath);
- return;
- }
-
- FILE *out = fopen(outpath, "w");
- if (out == NULL) {
- printf("Failed to open %s\n", outpath);
- fclose(in);
- return;
- }
-
- unsigned int size = fsize(in);
- char *buffer = fcontent(in, size);
-
- if (dot && strcmp(dot, ".md") == 0) {
- MMIOT *doc = mkd_string(buffer, size, 0);
- template_write(base_template, NULL, out, doc, true);
- } else if (strlen(buffer) != 0) {
- list_t *content_headers = engine_ingest(&buffer);
- template_write(base_template, content_headers, out, buffer, false);
- list_delete(content_headers);
- }
-
- free(buffer);
-
- fclose(in);
- fclose(out);
-
- free(inpath);
- free(outpath);
-}
+#include <msg.h>
int
main(int argc, char **argv)
{
- if (argc < 2) {
- printf("Usage: %s [directory]\n", argv[0]);
- return EXIT_FAILURE;
- }
- msg = malloc(sizeof(msg_t));
- msg->base_directory = argv[1];
-
- struct stat sb;
- if (stat(msg->base_directory, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
- printf("%s does not exist.\n", msg->base_directory);
- return EXIT_FAILURE;
- }
-
- base_template = template_create();
-
- int err = mkdir(OUTPUT, 0700);
- if (err != 0 && errno != EEXIST) {
- perror("mkdir");
- return EXIT_FAILURE;
- }
-
- char *assets_directory;
- asprintf(&assets_directory, "%s/%s", msg->base_directory, ASSETS);
- nftw(assets_directory, copy_recursively, 64, FTW_PHYS | FTW_ACTIONRETVAL);
- free(assets_directory);
-
- config_t *config = config_fetch_and_parse(CONFIG_FILE);
- list_t *resources
- = get_wrapped(list_find_corresponding_value_from_ptr_wrapper(
- config->keys, config->array_values, "resources"));
-
- if (resources == NULL) {
- printf("Could not find resources in config.cfg\n");
- return EXIT_FAILURE;
- }
-
- for (size_t i = 0; i < resources->size; i++) {
- ptr_wrapper_t *value = list_get(resources, i);
- char *path = value->ptr;
- printf("HANDLING: %s\n", path);
- handle_file(path);
- }
-
- template_delete(base_template);
- config_delete(config);
-
- free(msg);
- return EXIT_SUCCESS;
+ return run(argc, argv);
}
diff --git a/src/msg.c b/src/msg.c
new file mode 100644
index 0000000..dc525fd
--- /dev/null
+++ b/src/msg.c
@@ -0,0 +1,161 @@
+/*
+ * 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 <errno.h>
+#include <filehandler.h>
+#include <ftw.h>
+#include <lexer.h>
+#include <libgen.h>
+#include <list.h>
+#include <mkdio.h>
+#include <msg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <template.h>
+#include <util.h>
+
+template_t *base_template;
+msg_t *msg;
+
+void
+handle_file(const char *path)
+{
+ char *inpath;
+ char *outpath;
+
+ asprintf(&inpath, "%s/%s", msg->base_directory, path);
+
+ char *dot = strrchr(inpath, '.');
+ if (dot && strcmp(dot, ".md") == 0) {
+ asprintf(&outpath, "%s/%.*s.html", OUTPUT, (int) strlen(path) - 3, path);
+ } else {
+ asprintf(&outpath, "%s/%s", OUTPUT, path);
+ }
+
+ char *temp_outpath = strdup(outpath);
+ char *directory = dirname(temp_outpath);
+ char *next = calloc(strlen(directory) + 1, sizeof(char));
+ strcpy(next, "");
+
+ char *token;
+ for (token = strtok(directory, "/"); token != NULL;
+ token = strtok(NULL, "/")) {
+ if (strcmp(next, "") != 0) {
+ strcat(next, "/");
+ }
+
+ strcat(next, token);
+ mkdir(next, 0700);
+ }
+
+ free(next);
+ free(temp_outpath);
+
+ FILE *in = fopen(inpath, "r");
+ if (in == NULL) {
+ printf("Failed to open %s\n", inpath);
+ return;
+ }
+
+ FILE *out = fopen(outpath, "w");
+ if (out == NULL) {
+ printf("Failed to open %s\n", outpath);
+ fclose(in);
+ return;
+ }
+
+ unsigned int size = fsize(in);
+ char *buffer = fcontent(in, size);
+
+ if (dot && strcmp(dot, ".md") == 0) {
+ MMIOT *doc = mkd_string(buffer, size, 0);
+ template_write(base_template, NULL, out, doc, true);
+ } else if (strlen(buffer) != 0) {
+ list_t *content_headers = engine_ingest(&buffer);
+ template_write(base_template, content_headers, out, buffer, false);
+ list_delete(content_headers);
+ }
+
+ free(buffer);
+
+ fclose(in);
+ fclose(out);
+
+ free(inpath);
+ free(outpath);
+}
+
+int
+run(int argc, char **argv)
+{
+ if (argc < 2) {
+ printf("Usage: %s [directory]\n", argv[0]);
+ return EXIT_FAILURE;
+ }
+ msg = malloc(sizeof(msg_t));
+ msg->base_directory = argv[1];
+
+ struct stat sb;
+ if (stat(msg->base_directory, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
+ printf("%s does not exist.\n", msg->base_directory);
+ return EXIT_FAILURE;
+ }
+
+ base_template = template_create();
+
+ int err = mkdir(OUTPUT, 0700);
+ if (err != 0 && errno != EEXIST) {
+ perror("mkdir");
+ return EXIT_FAILURE;
+ }
+
+ char *assets_directory;
+ asprintf(&assets_directory, "%s/%s", msg->base_directory, ASSETS);
+ nftw(assets_directory, copy_recursively, 64, FTW_PHYS | FTW_ACTIONRETVAL);
+ free(assets_directory);
+
+ config_t *config = config_fetch_and_parse(CONFIG_FILE);
+ list_t *resources
+ = get_wrapped(list_find_corresponding_value_from_ptr_wrapper(
+ config->keys, config->array_values, "resources"));
+
+ if (resources == NULL) {
+ printf("Could not find resources in config.cfg\n");
+ return EXIT_FAILURE;
+ }
+
+ for (size_t i = 0; i < resources->size; i++) {
+ ptr_wrapper_t *value = list_get(resources, i);
+ char *path = value->ptr;
+ printf("HANDLING: %s\n", path);
+ handle_file(path);
+ }
+
+ template_delete(base_template);
+ config_delete(config);
+
+ free(msg);
+ return EXIT_SUCCESS;
+}
diff --git a/src/template.c b/src/template.c
index d4155c4..960ebc6 100644
--- a/src/template.c
+++ b/src/template.c
@@ -22,7 +22,7 @@
#include <engine.h>
#include <filehandler.h>
#include <lexer.h>
-#include <main.h>
+#include <msg.h>
#include <mkdio.h>
#include <stdio.h>
#include <stdlib.h>