diff options
| author | hachem <im@hachem.wtf> | 2026-09-02 14:19:01 +0200 |
|---|---|---|
| committer | hachem <im@hachem.wtf> | 2026-09-02 14:19:01 +0200 |
| commit | e12f6224e317dd666e3a2721f57b3c5b7517d304 (patch) | |
| tree | 5b74e3816b608b199e06ac8caf7e168a1e39b283 | |
| parent | d246c66681768c9217ff29137dadaabd69106592 (diff) | |
feat: open with man
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | Makefile | 20 | ||||
| -rw-r--r-- | jafm.c (renamed from src/main.c) | 41 |
3 files changed, 48 insertions, 17 deletions
@@ -1,8 +1,10 @@ -bin/ +jafm +*.dSYM/ compile_commands.json compile_flags.txt .clangd +.cache/ .DS_Store @@ -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 @@ -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); } |
