hachem.wtf
aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: bc29a4cc63f602cd37b26743945685fe305ea5c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#define _POSIX_C_SOURCE 200809L

#include <dirent.h>
#include <zlib.h>

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LINE_SIZE 8192

struct MatchList
{
    char** paths;
    size_t count;
    size_t capacity;
};

static bool match_list_add(struct MatchList* matches, const char* path)
{
    if (matches->count == matches->capacity)
    {
        size_t new_capacity = matches->capacity == 0 ? 16 : matches->capacity * 2;
        char** grown = realloc(matches->paths, new_capacity * sizeof(char*));
        if (grown == NULL)
            return false;

        matches->paths = grown;
        matches->capacity = new_capacity;
    }

    char* copy = strdup(path);
    if (copy == NULL)
        return false;

    matches->paths[matches->count] = copy;
    matches->count++;
    return true;
}

static void match_list_free(struct MatchList* matches)
{
    for (size_t index = 0; index < matches->count; index++)
        free(matches->paths[index]);

    free(matches->paths);
}

static bool ends_with(const char* string, const char* suffix)
{
    size_t string_length = strlen(string);
    size_t suffix_length = strlen(suffix);

    if (suffix_length > string_length)
        return false;

    return strcmp(string + string_length - suffix_length, suffix) == 0;
}

static bool plain_page_contains(const char* path, const char* query)
{
    FILE* page = fopen(path, "r");
    if (page == NULL)
        return false;

    char line[LINE_SIZE];
    bool found = false;
    while (fgets(line, sizeof(line), page) != NULL)
        if (strstr(line, query) != NULL)
        {
            found = true;
            break;
        }

    (void) fclose(page);
    return found;
}

static bool gzip_page_contains(const char* path, const char* query)
{
    gzFile page = gzopen(path, "r");
    if (page == NULL)
        return false;

    char line[LINE_SIZE];
    bool found = false;
    while (gzgets(page, line, LINE_SIZE) != NULL)
        if (strstr(line, query) != NULL)
        {
            found = true;
            break;
        }

    (void) gzclose(page);
    return found;
}

static bool page_contains(const char* path, const char* query)
{
    if (ends_with(path, ".gz"))
        return gzip_page_contains(path, query);

    return plain_page_contains(path, query);
}

static void list_pages(const char* section, const char* query, struct MatchList* matches)
{
    DIR* section_stream = opendir(section);
    if (section_stream == NULL)
        return;

    for (const struct dirent* entry = readdir(section_stream);
         entry != NULL;
         entry = readdir(section_stream))
        if (entry->d_name[0] != '.')
        {
            char path[4096];
            (void) snprintf(path, sizeof(path), "%s/%s", section, entry->d_name);
            if (page_contains(path, query))
                (void) match_list_add(matches, path);
        }

    (void) closedir(section_stream);
}

static void list_sections(const char* directory, const char* query, struct MatchList* matches)
{
    DIR* directory_stream = opendir(directory);
    if (directory_stream == NULL)
        return;

    for (const struct dirent* entry = readdir(directory_stream);
         entry != NULL;
         entry = readdir(directory_stream))
        if (strncmp(entry->d_name, "man", 3) == 0)
        {
            char section[4096];
            (void) snprintf(section, sizeof(section), "%s/%s", directory, entry->d_name);
            list_pages(section, query, matches);
        }

    (void) closedir(directory_stream);
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        (void) fprintf(stderr, "usage: %s <symbol>\n", argv[0]);
        return 1;
    }

    const char* query = argv[1];

    FILE* manpath_pipe = popen("manpath", "r");
    if (manpath_pipe == NULL)
    {
        (void) fprintf(stderr, "jafm: failed to run manpath\n");
        return 1;
    }

    char buffer[4096];
    if (fgets(buffer, sizeof(buffer), manpath_pipe) == NULL)
    {
        (void) fprintf(stderr, "jafm: failed to read manpath\n");
        (void) pclose(manpath_pipe);
        return 1;
    }

    (void) pclose(manpath_pipe);

    buffer[strcspn(buffer, "\n")] = '\0';

    struct MatchList matches = {0};

    char* save_pointer = NULL;
    for (const char* directory = strtok_r(buffer, ":", &save_pointer);
         directory != NULL;
         directory = strtok_r(NULL, ":", &save_pointer))
        list_sections(directory, query, &matches);

    for (size_t index = 0; index < matches.count; index++)
        (void) printf("%zu) %s\n", index + 1, matches.paths[index]);

    match_list_free(&matches);
    return 0;
}