hachem.wtf
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhachem <im@hachem.wtf>2026-09-02 14:19:01 +0200
committerhachem <im@hachem.wtf>2026-09-02 14:19:01 +0200
commite12f6224e317dd666e3a2721f57b3c5b7517d304 (patch)
tree5b74e3816b608b199e06ac8caf7e168a1e39b283
parentd246c66681768c9217ff29137dadaabd69106592 (diff)
feat: open with man
-rw-r--r--.gitignore4
-rw-r--r--Makefile20
-rw-r--r--jafm.c (renamed from src/main.c)41
3 files changed, 48 insertions, 17 deletions
diff --git a/.gitignore b/.gitignore
index 8b13325..f46bdf8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,8 +1,10 @@
-bin/
+jafm
+*.dSYM/
compile_commands.json
compile_flags.txt
.clangd
+.cache/
.DS_Store
diff --git a/Makefile b/Makefile
index 9e1472f..812ee65 100644
--- a/Makefile
+++ b/Makefile
@@ -11,8 +11,7 @@ CFLAGS := -Wall \
-g \
-fno-omit-frame-pointer \
-fsanitize=address,undefined \
- -std=c17 \
- -Isrc
+ -std=c17
LDFLAGS := -fsanitize=address,undefined \
-lz
@@ -21,26 +20,19 @@ CPPCHECKFLAGS := --project=compile_commands.json \
--enable=warning,style,performance,portability \
--error-exitcode=1
-TARGET := bin/jafm
-
-SRC := $(shell find src -name '*.c')
-OBJ := $(SRC:src/%.c=bin/%.o)
+TARGET := jafm
+SRC := jafm.c
all: $(TARGET)
-$(TARGET): $(OBJ)
- @mkdir -p $(@D)
- $(CC) $^ -o $@ $(LDFLAGS)
-
-bin/%.o: src/%.c
- @mkdir -p $(@D)
- $(CC) $(CFLAGS) -c $< -o $@
+$(TARGET): $(SRC)
+ $(CC) $(CFLAGS) $(SRC) -o $@ $(LDFLAGS)
check:
bear -- make clean all
cppcheck $(CPPCHECKFLAGS)
clean:
- rm -rf bin
+ rm -rf $(TARGET) $(TARGET).dSYM
.PHONY: all check clean
diff --git a/src/main.c b/jafm.c
index 4a52100..abfb044 100644
--- a/src/main.c
+++ b/jafm.c
@@ -431,6 +431,42 @@ static void list_sections(const char* directory, const char* query, struct Match
(void) closedir(directory_stream);
}
+static int open_page(const char* path)
+{
+ char work[4096];
+ (void) snprintf(work, sizeof(work), "%s", path);
+
+ size_t length = strlen(work);
+ if (length > 3 && strcmp(work + length - 3, ".gz") == 0)
+ work[length - 3] = '\0';
+
+ char* name_slash = strrchr(work, '/');
+ if (name_slash == NULL)
+ return 1;
+
+ *name_slash = '\0';
+ char* name = name_slash + 1;
+
+ char* section_dot = strrchr(name, '.');
+ if (section_dot != NULL)
+ *section_dot = '\0';
+
+ char* root_slash = strrchr(work, '/');
+ if (root_slash == NULL)
+ return 1;
+
+ *root_slash = '\0';
+ char* root = work;
+ char* section = root_slash + 1;
+ if (strncmp(section, "man", 3) == 0)
+ section += 3;
+
+ (void) execlp("man", "man", "-M", root, section, name, (char*) NULL);
+
+ (void) fprintf(stderr, "jafm: failed to run man\n");
+ return 1;
+}
+
int main(int argc, char** argv)
{
if (argc != 2)
@@ -495,8 +531,9 @@ int main(int argc, char** argv)
return 1;
}
- (void) printf("selected: %s\n", matches.items[selected].path);
+ char chosen[4096];
+ (void) snprintf(chosen, sizeof(chosen), "%s", matches.items[selected].path);
match_list_free(&matches);
- return 0;
+ return open_page(chosen);
}