diff options
author | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-27 15:57:38 +0530 |
---|---|---|
committer | Raghuram Subramani <raghus2247@gmail.com> | 2025-07-27 15:57:38 +0530 |
commit | b268e78ec0897e5f37616c180eb0e899a2c8f031 (patch) | |
tree | aa91a15cc93b3a627d1b29139cea79c11ece4727 /src | |
parent | 7c13ed35bba1bc52cb21fd44ae9a99d3ddf348f4 (diff) |
main: introduce global msg_t and don't hardcode directory
Diffstat (limited to 'src')
-rw-r--r-- | src/copy.c | 5 | ||||
-rw-r--r-- | src/engine.c | 5 | ||||
-rw-r--r-- | src/main.c | 24 | ||||
-rw-r--r-- | src/template.c | 8 |
4 files changed, 31 insertions, 11 deletions
@@ -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) { @@ -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); |