aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-07-27 15:57:38 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-07-27 15:57:38 +0530
commitb268e78ec0897e5f37616c180eb0e899a2c8f031 (patch)
treeaa91a15cc93b3a627d1b29139cea79c11ece4727
parent7c13ed35bba1bc52cb21fd44ae9a99d3ddf348f4 (diff)
main: introduce global msg_t and don't hardcode directory
-rw-r--r--include/copy.h1
-rw-r--r--include/main.h8
-rw-r--r--src/copy.c5
-rw-r--r--src/engine.c5
-rw-r--r--src/main.c24
-rw-r--r--src/template.c8
6 files changed, 39 insertions, 12 deletions
diff --git a/include/copy.h b/include/copy.h
index 80fc5fb..ac5d335 100644
--- a/include/copy.h
+++ b/include/copy.h
@@ -5,7 +5,6 @@
#include <sys/stat.h>
#define OUTPUT "dist"
-#define DIRECTORY "compromyse.xyz"
typedef struct FTW FTW;
diff --git a/include/main.h b/include/main.h
new file mode 100644
index 0000000..df4bae5
--- /dev/null
+++ b/include/main.h
@@ -0,0 +1,8 @@
+#ifndef __MAIN_H
+#define __MAIN_H
+
+typedef struct {
+ char *base_directory;
+} msg_t;
+
+#endif
diff --git a/src/copy.c b/src/copy.c
index ddada6a..9fd812f 100644
--- a/src/copy.c
+++ b/src/copy.c
@@ -4,11 +4,14 @@
#include <fcntl.h>
#include <filehandler.h>
#include <ftw.h>
+#include <main.h>
#include <string.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <unistd.h>
+extern msg_t *msg;
+
int
copy_recursively(const char *fpath,
const struct stat *sb,
@@ -18,7 +21,7 @@ copy_recursively(const char *fpath,
(void) sb;
(void) ftwbuf;
- const char *path = fpath + strlen(DIRECTORY) + 1;
+ const char *path = fpath + strlen(msg->base_directory) + 1;
char *output_path = NULL;
asprintf(&output_path, "%s/%s", OUTPUT, path);
diff --git a/src/engine.c b/src/engine.c
index 413b2d1..5f4c51a 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -5,17 +5,20 @@
#include <filehandler.h>
#include <lexer.h>
#include <list.h>
+#include <main.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+extern msg_t *msg;
+
void
handle_include(char **buffer, key_match_t *match, directive_t *directive)
{
char *operand = directive->operands;
char *partial_path;
- asprintf(&partial_path, "%s/%s/%s", DIRECTORY, PARTIALS, operand);
+ asprintf(&partial_path, "%s/%s/%s", msg->base_directory, PARTIALS, operand);
FILE *f = fopen(partial_path, "r");
if (f == NULL) {
diff --git a/src/main.c b/src/main.c
index 2519a2b..b40150a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -9,6 +9,7 @@
#include <lexer.h>
#include <libgen.h>
#include <list.h>
+#include <main.h>
#include <mkdio.h>
#include <stdio.h>
#include <stdlib.h>
@@ -20,6 +21,7 @@
#define ASSETS "assets"
template_t *base_template;
+msg_t *msg;
void
handle_file(const char *path)
@@ -27,7 +29,7 @@ handle_file(const char *path)
char *inpath;
char *outpath;
- asprintf(&inpath, "%s/%s", DIRECTORY, path);
+ asprintf(&inpath, "%s/%s", msg->base_directory, path);
char *dot = strrchr(inpath, '.');
if (dot && strcmp(dot, ".md") == 0) {
@@ -92,12 +94,16 @@ handle_file(const char *path)
int
main(int argc, char **argv)
{
- (void) argc;
- (void) 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(DIRECTORY, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
- printf("%s does not exist.\n", DIRECTORY);
+ if (stat(msg->base_directory, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
+ printf("%s does not exist.\n", msg->base_directory);
return EXIT_FAILURE;
}
@@ -109,11 +115,12 @@ main(int argc, char **argv)
return EXIT_FAILURE;
}
- nftw(
- DIRECTORY "/" ASSETS, copy_recursively, 64, FTW_PHYS | FTW_ACTIONRETVAL);
+ 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.cfg");
-
list_t *resources = list_find_corresponding_value_from_ptr_wrapper(
config->keys, config->array_values, "resources");
@@ -131,5 +138,6 @@ main(int argc, char **argv)
config_delete(config);
+ free(msg);
return EXIT_SUCCESS;
}
diff --git a/src/template.c b/src/template.c
index 26e492e..b3536cc 100644
--- a/src/template.c
+++ b/src/template.c
@@ -4,18 +4,24 @@
#include <engine.h>
#include <filehandler.h>
#include <lexer.h>
+#include <main.h>
#include <mkdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <template.h>
+extern msg_t *msg;
+
template_t *
template_create(void)
{
template_t *template = malloc(sizeof(template_t));
- FILE *base = fopen(DIRECTORY "/" BASE_TEMPLATE, "r");
+ char *path;
+ asprintf(&path, "%s/%s", msg->base_directory, BASE_TEMPLATE);
+ FILE *base = fopen(path, "r");
+ free(path);
unsigned int size = fsize(base);
char *buffer = fcontent(base, size);