/* * msg * Copyright (C) 2025 Raghuram Subramani * * 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 . */ #define _GNU_SOURCE #include #include #include #include #include #include #include #define BUFFER_SIZE ((sizeof(struct inotify_event) + NAME_MAX + 1) * 1024) msg_t *msg; void usage(char *program) { printf("Usage: %s [-h] [-w] [-o ] \n", program); printf("\t-h : Help\n"); printf("\t-w : Watch working directory for changes\n"); printf("\t-o : Output directory\n"); printf("\t: Working directory\n"); } void config(void) { printf("Base Directory: %s\n", msg->base_directory); printf("Output Directory: %s\n\n", msg->output_directory); } int 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:hw")) != -1) { switch (opt) { case 'o': msg->output_directory = optarg; break; case 'w': watch = true; break; case 'h': default: usage(argv[0]); return EXIT_SUCCESS; } } if (optind < argc) msg->base_directory = argv[optind]; config(); int r = run(); 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; } } }