aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
diff options
context:
space:
mode:
authorRaghuram Subramani <raghus2247@gmail.com>2025-07-31 11:15:37 +0530
committerRaghuram Subramani <raghus2247@gmail.com>2025-07-31 11:15:37 +0530
commit14536c0da07233a7a4deddf8c73305ce918af3a7 (patch)
tree5687a7889274904eb320844f9e14614f6c03b3dd /src/main.c
parent2735cc7eadcc84aaf3954930a78dbb63da3cca44 (diff)
main: add -w (watch) option instead of using watch.sh
Diffstat (limited to 'src/main.c')
-rw-r--r--src/main.c68
1 files changed, 64 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index 703c584..2fb2d2b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,18 +18,24 @@
#define _GNU_SOURCE
+#include <limits.h>
#include <msg.h>
+#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
+#include <sys/inotify.h>
#include <unistd.h>
+#define BUFFER_SIZE ((sizeof(struct inotify_event) + NAME_MAX + 1) * 1024)
+
msg_t *msg;
void
usage(char *program)
{
- printf("Usage: %s [-o <output>] [-h] <directory>\n", program);
+ printf("Usage: %s [-h] [-w] [-o <output>] <directory>\n", program);
printf("\t-h : Help\n");
+ printf("\t-w : Watch working directory for changes\n");
printf("\t-o <output>: Output directory\n");
printf("\t<directory>: Working directory\n");
}
@@ -46,16 +52,21 @@ main(int argc, char **argv)
{
printf("msg: The Minimal Static Site Generator\n\n");
+ bool watch = false;
+
int opt;
msg = malloc(sizeof(msg_t));
msg->base_directory = ".";
msg->output_directory = "dist";
- while ((opt = getopt(argc, argv, "o:h")) != -1) {
+ while ((opt = getopt(argc, argv, "o:hw")) != -1) {
switch (opt) {
case 'o':
msg->output_directory = optarg;
break;
+ case 'w':
+ watch = true;
+ break;
case 'h':
default:
usage(argv[0]);
@@ -69,6 +80,55 @@ main(int argc, char **argv)
config();
int r = run();
- free(msg);
- return r;
+ if (!watch) {
+ free(msg);
+ return r;
+ }
+
+ int fd = inotify_init1(IN_NONBLOCK);
+ if (fd < 0) {
+ perror("inotify");
+ return EXIT_FAILURE;
+ }
+
+ int wd = inotify_add_watch(
+ fd, msg->base_directory, IN_MODIFY | IN_CREATE | IN_DELETE);
+ if (wd < 0) {
+ perror("inotify_add_watch");
+ return EXIT_FAILURE;
+ }
+
+ char *buffer = malloc(BUFFER_SIZE);
+
+ while (true) {
+ size_t i = 0;
+ size_t length = read(fd, buffer, BUFFER_SIZE);
+ if (length == 0) {
+ }
+
+ if (length < 0) {
+ perror("read");
+ return EXIT_FAILURE;
+ }
+
+ char *p;
+ for (p = buffer; p < buffer + length;) {
+ struct inotify_event *event = (struct inotify_event *) p;
+
+ if (event->len) {
+ switch (event->mask) {
+ case IN_MODIFY:
+ case IN_CREATE:
+ case IN_DELETE:
+ printf("\n\n");
+ run();
+ break;
+
+ default:
+ break;
+ }
+ }
+ p += sizeof(struct inotify_event) + event->len;
+ }
+ }
}