diff options
| author | czjstmax <jstmaxlol@disroot.org> | 2026-05-14 14:44:57 +0200 |
|---|---|---|
| committer | czjstmax <jstmaxlol@disroot.org> | 2026-05-14 14:44:57 +0200 |
| commit | d5be1b2375c4adcf4840ac379fe88aa2fae45771 (patch) | |
| tree | 27a5f6b500450f4e23b3818b82c942370663eb7b | |
| parent | 15264922e75422865eb23eb2b471f1289ebdb549 (diff) | |
rf ver 1.1.20
Signed-off-by: czjstmax <jstmaxlol@disroot.org>
| -rw-r--r-- | MAINTAINERS | 7 | ||||
| -rw-r--r-- | Makefile | 22 | ||||
| -rw-r--r-- | rf.c | 121 | ||||
| -rw-r--r-- | rfile | 10 |
4 files changed, 135 insertions, 25 deletions
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) + @@ -8,16 +8,22 @@ * czjstmax * <jstmaxlol at * disroot dot org> + * + * check MAINTAINERS file */ #define _POSIX_C_SOURCE 200809L -#define RUNFILE_VERSION 1 - -#include <stdio.h> +#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 <stdio.h> /* to be important */ +#include <ctype.h> #include <stdlib.h> #include <string.h> #include <unistd.h> +#include <stdbool.h> #include <wordexp.h> #include <sys/wait.h> @@ -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("<rf> 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 ); } @@ -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 +} + |