From d5be1b2375c4adcf4840ac379fe88aa2fae45771 Mon Sep 17 00:00:00 2001 From: czjstmax Date: Thu, 14 May 2026 14:44:57 +0200 Subject: rf ver 1.1.20 Signed-off-by: czjstmax --- MAINTAINERS | 7 ++++ Makefile | 22 +++++++++++ rf.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++------------ rfile | 10 ++++- 4 files changed, 135 insertions(+), 25 deletions(-) create mode 100644 MAINTAINERS create mode 100644 Makefile diff --git a/MAINTAINERS b/MAINTAINERS new file mode 100644 index 0000000..2ca8dd8 --- /dev/null +++ b/MAINTAINERS @@ -0,0 +1,7 @@ +M: czjstmax +E: jstmaxlol@disroot.org +F: /* +S: active +T: developer, reviewer +P: email + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4d7f511 --- /dev/null +++ b/Makefile @@ -0,0 +1,22 @@ +CC = gcc +OPTS = -Wall -Wextra -pedantic -std=c99 +FIN = rf.c +FOUT = rf +PREFIX ?= /usr +DESTDIR ?= + +.PHONY: all run install clean + +all: + $(CC) $(OPTS) $(FIN) -o $(FOUT) + +run: all + ./$(FOUT) + +install: all + sudo install -Dm755 $(FOUT) $(DESTDIR)$(PREFIX)/bin/$(FOUT) + $(MAKE) clean + +clean: + rm -v $(FOUT) + diff --git a/rf.c b/rf.c index 64489e4..eced3e5 100644 --- a/rf.c +++ b/rf.c @@ -8,16 +8,22 @@ * czjstmax * + * + * check MAINTAINERS file */ #define _POSIX_C_SOURCE 200809L -#define RUNFILE_VERSION 1 - -#include +#define RUNFILE_VER_MAJ 1 /* anything that changes how rfile are written in the slightest */ +#define RUNFILE_VER_MIN 1 /* anything that varies the UX of the program (i.e. CLI flags) */ +#define RUNFILE_VER_FIX 20 /* optimizations, slight logic tweaks, anything that is not */ + /* noticeable by the user but impacts the dev experience enough */ +#include /* to be important */ +#include #include #include #include +#include #include #include @@ -25,20 +31,29 @@ void usage(); int main(int argc, char **argv) { - if (argc > 2) - usage(); - else if (argc >= 2) { + char *section = NULL; + bool in_section = false; + bool should_run = false; + + // check cli arguments + // if (argc < 2) { + // printf(" not enough arguments.\n"); + // usage(); + // return 1; + // } + if (argc >= 2) { for (int i = 1; i < argc; i++) { - if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) + if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { usage(); + return 0; + } } - } FILE *f = fopen("rfile", "r"); if (!f) { - printf("no rfile found.\n"); + fprintf(stderr, "no rfile found.\n"); return 1; } @@ -48,32 +63,89 @@ int main(int argc, char **argv) while (getline(&line, &len, f) != -1) { line[strcspn(line, "\n")] = '\0'; - printf("$ %s\n", line); + char *cmd = line; - wordexp(line, &p, 0); + while (isspace(*cmd)) + cmd++; - pid_t pid = fork(); + if (*cmd == '\0') + continue; + else if (cmd[0] == '#') + continue; + else if (strncmp(cmd, "@:", 2) == 0) { + for (size_t i = 0; i < strlen(cmd); i++) + printf("-"); + + printf("\n%s\n", cmd); + + for (size_t i = 0; i < strlen(cmd); i++) + printf("-"); + + printf("\n\n"); + continue; + } + else if (strcmp(cmd, "}") == 0 || strcmp(line, "}") == 0) { + in_section = false; + free(section); + section = NULL; + + continue; + } + + char *brace = strchr(cmd, '{'); + if (brace) { + *brace = '\0'; + + size_t l = strlen(cmd); + + while (l > 0 && (cmd[l-1] == ' ' || cmd[l-1] == '\t')) { + cmd[l-1] = '\0'; + l--; + } + + free(section); + section = strdup(cmd); + in_section = true; + + continue; + } + + should_run = false; + + if (argc == 1) { + should_run = !in_section; + } + else { + should_run = in_section && + section && + strcmp(argv[1], section) == 0; + } + + if (!should_run) + continue; + + printf("$ %s\n", cmd); + + if (wordexp(cmd, &p, 0) != 0) { + fprintf(stderr, "wordexp failed\n"); + continue; + } + + pid_t pid = fork(); if (pid == 0) { execvp(p.we_wordv[0], p.we_wordv); perror("failed to run command"); - wordfree(&p); - free(line); + fclose(f); _exit(1); } - else if (pid > 0) { - int status; - waitpid(pid, &status, 0); - } - else { - perror("fork failed"); - return 1; - } + + waitpid(pid, NULL, 0); + wordfree(&p); } - wordfree(&p); free(line); fclose(f); return 0; @@ -82,8 +154,9 @@ int main(int argc, char **argv) void usage() { printf( - "< runfile usage >\n" + "runfile usage - version %d.%d.%d\n" "syn: rf [section]\n" + , RUNFILE_VER_MAJ, RUNFILE_VER_MIN, RUNFILE_VER_FIX ); } diff --git a/rfile b/rfile index 0fa09b0..97ba695 100644 --- a/rfile +++ b/rfile @@ -1 +1,9 @@ -cc -o rf rf.c -Wall -Wextra -pedantic +# rfile for rfile +@: hello from rfile + +all { + cc -o rf rf.c -Wall -Wextra -pedantic + sudo install -Dm755 rf /usr/bin/rf + rm ./rf +} + -- cgit v1.3.1