aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore6
-rw-r--r--CMakeLists.txt30
-rw-r--r--Makefile16
-rw-r--r--flake.nix2
-rw-r--r--include/filehandler.h9
-rw-r--r--src/filehandler.c28
-rw-r--r--src/main.c (renamed from msg.c)43
7 files changed, 85 insertions, 49 deletions
diff --git a/.gitignore b/.gitignore
index e283403..12bed7b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,5 @@
.direnv/
-msg
-dist/ \ No newline at end of file
+.cache/
+build/
+dist/
+compile_commands.json
diff --git a/CMakeLists.txt b/CMakeLists.txt
new file mode 100644
index 0000000..2b1f6cd
--- /dev/null
+++ b/CMakeLists.txt
@@ -0,0 +1,30 @@
+cmake_minimum_required(VERSION 3.21)
+project(msg C)
+
+set(SRC
+ src/filehandler.c
+ src/main.c
+)
+
+add_executable(msg ${SRC})
+target_include_directories(msg PRIVATE include)
+target_link_libraries(msg markdown)
+
+set(C_COMPILE_OPTIONS
+ # -O3
+ -Og
+
+ -Wall
+ -Wextra
+ # -Werror
+
+ -g
+
+ -std=c99
+
+ -Wno-unused-result
+)
+
+target_compile_options(msg PRIVATE
+ $<$<COMPILE_LANGUAGE:C>: ${C_COMPILE_OPTIONS}>
+)
diff --git a/Makefile b/Makefile
deleted file mode 100644
index f84ab7c..0000000
--- a/Makefile
+++ /dev/null
@@ -1,16 +0,0 @@
-CC := clang
-CFLAGS := -std=c99
-CFLAGS += -Wall -Wextra -Wpedantic
-CFLAGS += -lmarkdown
-
-all: CFLAGS += -O3
-all: clean msg
-
-debug: CFLAGS += -O0 -g3 -glldb
-debug: clean msg
-
-msg: msg.c
- $(CC) $(CFLAGS) $^ -o $@
-
-clean:
- rm -f msg
diff --git a/flake.nix b/flake.nix
index c323e78..d806b26 100644
--- a/flake.nix
+++ b/flake.nix
@@ -12,7 +12,7 @@
clang-tools
clang
lldb
- gnumake
+ cmake
discount
];
diff --git a/include/filehandler.h b/include/filehandler.h
new file mode 100644
index 0000000..8868af7
--- /dev/null
+++ b/include/filehandler.h
@@ -0,0 +1,9 @@
+#ifndef __FILEHANDLER_H
+#define __FILEHANDLER_H
+
+#include <stdio.h>
+
+char *fcontent(FILE *f, unsigned int size);
+unsigned int fsize(FILE *f);
+
+#endif
diff --git a/src/filehandler.c b/src/filehandler.c
new file mode 100644
index 0000000..154e2f3
--- /dev/null
+++ b/src/filehandler.c
@@ -0,0 +1,28 @@
+#include <filehandler.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+char *
+fcontent(FILE *f, unsigned int size)
+{
+ char *buffer = (char *) calloc(size, sizeof(char));
+
+ fseek(f, 0, SEEK_SET);
+ int bytesread = fread(buffer, sizeof(char), size, f);
+ if (bytesread < 0)
+ return NULL;
+
+ return buffer;
+}
+
+unsigned int
+fsize(FILE *f)
+{
+ unsigned int current = ftell(f);
+
+ fseek(f, 0, SEEK_END);
+ unsigned int s = ftell(f);
+ fseek(f, current, SEEK_SET);
+
+ return s + 1;
+}
diff --git a/msg.c b/src/main.c
index a16407d..c560fec 100644
--- a/msg.c
+++ b/src/main.c
@@ -2,6 +2,7 @@
#include <ctype.h>
#include <fcntl.h>
+#include <filehandler.h>
#include <ftw.h>
#include <libgen.h>
#include <mkdio.h>
@@ -13,11 +14,11 @@
#include <sys/stat.h>
#include <unistd.h>
-#include "config.h"
+#include "../config.h"
#define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
-typedef enum { CONTENT, INCLUDE } directive_e;
+typedef enum { INCLUDE } directive_e;
typedef struct {
unsigned int offset;
@@ -29,10 +30,8 @@ typedef struct {
void *operands;
} directive_t;
-char *fcontent(FILE *f, unsigned int size);
directive_t *find_directive(char *content, key_match_t *match);
key_match_t *find_next_key(char *buffer);
-unsigned int fsize(FILE *f);
void handle_file(const char *path);
void ingest(char **buffer);
@@ -48,8 +47,10 @@ find_next_key(char *buffer)
if (buffer[i] == '{' && buffer[i + 1] == '{')
match->offset = i;
- if (i == strlen(buffer) - 1)
+ if (i == strlen(buffer) - 1) {
+ free(match);
return NULL;
+ }
}
char *subbuffer = buffer + match->offset;
@@ -59,6 +60,7 @@ find_next_key(char *buffer)
if (i == strlen(buffer) - 1) {
printf("Unterminated Key\n");
+ free(match);
return NULL;
}
}
@@ -152,31 +154,6 @@ ingest(char **buffer)
}
}
-unsigned int
-fsize(FILE *f)
-{
- unsigned int current = ftell(f);
-
- fseek(f, 0, SEEK_END);
- unsigned int s = ftell(f);
- fseek(f, current, SEEK_SET);
-
- return s + 1;
-}
-
-char *
-fcontent(FILE *f, unsigned int size)
-{
- char *buffer = (char *) calloc(size, sizeof(char));
-
- fseek(f, 0, SEEK_SET);
- int bytesread = fread(buffer, sizeof(char), size, f);
- if (bytesread < 0)
- return NULL;
-
- return buffer;
-}
-
void
handle_file(const char *path)
{
@@ -289,6 +266,12 @@ main(int argc, char **argv)
(void) argc;
(void) argv;
+ struct stat sb;
+ if (stat(DIRECTORY, &sb) != 0 || !S_ISDIR(sb.st_mode)) {
+ printf("%s does not exist.\n", DIRECTORY);
+ return EXIT_FAILURE;
+ }
+
FILE *base = fopen(DIRECTORY "/" BASE_TEMPLATE, "r");
unsigned int size = fsize(base);