aboutsummaryrefslogtreecommitdiff
path: root/rf.c
diff options
context:
space:
mode:
Diffstat (limited to 'rf.c')
-rw-r--r--rf.c121
1 files changed, 97 insertions, 24 deletions
diff --git a/rf.c b/rf.c
index 64489e4..eced3e5 100644
--- a/rf.c
+++ b/rf.c
@@ -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
);
}