aboutsummaryrefslogtreecommitdiff
path: root/booki.c
blob: 060a4afd5cb62261f4d1aa2739319140908ae060 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <getopt.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

#define BOOKI_FILE_REL ".local/share/booki/books.toml"

#define MAX_SEARCH_OPTS 5

// STRUCTS
struct es {
    int len;
    char* ptr;
};

struct es default_es = { 0, NULL };

struct Book {
    struct es id;
    struct es title;
    struct es author;
    int pages;
    struct es isbn;
    struct es language;
    struct es translator;
    int published;

    // maybe?
    //struct es authors[MAX_AUTHORS];
    //struct es translators[MAX_TRANSLATORS];
};

struct Book new_book() {
    struct Book book;
    book.id = default_es;
    book.title = default_es;
    book.author = default_es;
    book.pages = 0;
    book.isbn = default_es;
    struct es language = { 7, "English" };  // default language
    book.language = language;
    book.translator = default_es;
    book.published = 0;
    return book;
}

char* BOOKI_FILE;

int PARSE_LINE = 1;

const char* get_last_word(const char* str) {
    const char* last_space = strrchr(str, ' ');
    if ((last_space - str) > 0)
        return last_space + 1;
    else
        return 0;
}

struct Book parse_book(char* current_pos) {
    struct Book book = new_book();
    char* attr;
    char* value;
    char c = *current_pos;
    // loop until we hit the extra newline
    while (c != '\n') {
        // we start at the beginning of a line
        attr = current_pos;
        while ((c = *current_pos) != '=') {
            current_pos++;
        }

        // attr should be the name of the attribute, with (possibly) trailing spaces
        if (strncmp(attr, "title", 5) == 0) {
            // leading spaces
            while ((c = *current_pos) != '"') {
                current_pos++;
            }

            // go past the quote and set the position of the start of value
            current_pos++;
            value = current_pos;

            // until the next quote
            while ((c = *current_pos) != '"') {
                current_pos++;
            }

            // set the attribute
            struct es title;
            title.len = current_pos - value;
            title.ptr = value;
            book.title = title;
        } else if (strncmp(attr, "author", 6) == 0) {
            // leading spaces
            while ((c = *current_pos) != '"') {
                current_pos++;
            }

            // go past the quote and set the position of the start of value
            current_pos++;
            value = current_pos;

            // until the next quote
            while ((c = *current_pos) != '"') {
                current_pos++;
            }

            // set the attribute
            struct es author;
            author.len = current_pos - value;
            author.ptr = value;
            book.author = author;
        }

        // go to (and then past) the newline
        while ((c = *current_pos) != '\n') {
            current_pos++;
        }
        PARSE_LINE += 1;
        c = *(++current_pos);
    }
    return book;
}

char* next_book(char* current_pos) {
    char c;
    while(*current_pos) {
        // first opening bracket
        c = *current_pos++;
        if (c != '[') {
            continue;
        }
        
        // second opening bracket
        c = *current_pos++;
        if (c != '[') {
            continue;
        }

        if (strncmp(current_pos, "books", 5) != 0) {
            printf("thought i had it, but it's not a book\n");
            continue;
        }
        current_pos += 5;

        // seek past the closing brackets and the next newline
        current_pos += 2;
        if (*current_pos != '\n') {
            printf("expecting a newline...\n");
            continue;
        }

        PARSE_LINE += 1;
        current_pos++;
        return current_pos;
    }
    return NULL;
}

bool regex_match(const char* pattern, const struct es text) {

    // empty pattern matches everything
    if (!*pattern) return true;

    // get lengths
    int pattern_length = strlen(pattern);

    if (pattern_length > text.len) return false;

    // we only need to compare while remaining text is
    // as long or longer than pattern (without special)
    for (int i = 0; i <= (text.len - pattern_length); i++) {
        if (strncasecmp(pattern, text.ptr + i, pattern_length) == 0) return true;
    }

    return false;
}

char* load_file(char* filename) {
    // open the file
    FILE* fp = fopen(filename, "r");
    if (!fp) {
        printf("bad file\n");
        return NULL;
    }

    // seek to the end
    fseek(fp, 0, SEEK_END);
    int size = ftell(fp);
    rewind(fp);

    char* data = malloc(size);
    if (!data) {
        printf("couldn't malloc\n");
        return NULL;
    }

    int read_size = fread(data, 1, size, fp);
    if (read_size != size) {
        printf("didn't read everything -- %d read of %d\n", read_size, size);
        return NULL;
    }

    fclose(fp);

    return data;
}

static struct option search_options[] = {
    {"show", no_argument, 0, 's'},
    {0, 0, 0, 0}  // marks the end of the array
};

struct search_opt {
    int show;
    int count;
    char* opts[MAX_SEARCH_OPTS];
    char* args[MAX_SEARCH_OPTS];
};

struct search_opt parse_search_options(int argc, char* argv[]) {

    // return struct
    struct search_opt opt_out;
    int count = 0;
    int show = false;

    // opt options
    int opt;
    int opt_idx = 0;
    opterr = 0;  // turn off getopt error messages

    // look at each option
    while ((opt = getopt_long_only(argc, argv, "", search_options, &opt_idx)) != -1) {
        switch(opt) {
            case 's':
                show = true;
                break;
            case '?':
                // optind points at the argument (one past the option)
                opt_out.opts[count] = argv[optind-1] + 2;  // '--example' -> 'example'
                opt_out.args[count] = argv[optind];
                count++;
                break;
            default:
                printf("something went wrong with parsing!\n");
                break;
        }
    }

    // set the count/show values
    opt_out.count = count;
    opt_out.show = show;

    return opt_out;
}

void print_book(struct Book book, bool all_fields) {
    printf("%.*s by %.*s\n", book.title.len, book.title.ptr, book.author.len, book.author.ptr);
    if (all_fields) {
        char* esfmt = " - %s: %.*s\n";
        char* intfmt = " - %s: %d\n";
        if (book.isbn.ptr)
            printf(esfmt, "isbn", book.isbn.len, book.isbn.ptr);
        if (book.pages != 0)
            printf(intfmt, "pages", book.pages);
        if (book.published)
            printf(esfmt, "published", book.published);
        if (book.language.ptr)
            printf(esfmt, "language", book.language.len, book.language.ptr);
        if (book.translator.ptr)
            printf(esfmt, "translator", book.translator.len, book.translator.ptr);

    }
}

char** search(int argc, char* argv[]) {
    struct search_opt search_opts = parse_search_options(argc, argv);
    bool print = false;

    // get the books array
    char* data = load_file(BOOKI_FILE);
    if (!data) {
        printf("no such array: 'books'");
        return NULL;
    }

    // book loop
    struct Book book;
    char* cur_data = data;
    while ((cur_data = next_book(cur_data)) != NULL) {
        book = parse_book(cur_data);

        char* field;
        int i;
        bool print = true;
        for (i = 0; i < search_opts.count; i++) {
            field = search_opts.opts[i];

            // compare fields
            if (strcmp(field, "title") == 0) {
                if (!regex_match(search_opts.args[i], book.title))
                    break;
            } else if (strcmp(field, "author") == 0) {
                if (!regex_match(search_opts.args[i], book.author))
                    break;
            } else {
                printf("unsupported field: %s\n", field);
                break;
            }
        }
        print = i == search_opts.count;

        if (print) {
            print_book(book, search_opts.show);
        }
    }

    free(data);

    return argv + optind;
}


void open() {

    char* home = getenv("HOME");
    if (!home) {
        printf("no home?\n");
        home = "/";  // no way
    }
    char* editor = getenv("EDITOR");
    if (!editor)
        editor = "nano";

    pid_t pid;
    int status;
    switch ((pid = fork())) {
        case -1:
            printf("fork has failed!\n");
            break;
        case 0:
            execlp(editor, editor, BOOKI_FILE, NULL);
            printf("child failed :(\n");
            break;
        default:
            // wait for the child to finish
            pid = wait(&status);
            break;
    }
}

void help(bool err) {
    printf("booki! it's a thing\n");
    if (err) {
        printf("you did something wrong\n");
    }
}

int main(int argc, char* argv[]) {

    char* home = getenv("HOME");
    char booki_file[strlen(home) + strlen(BOOKI_FILE_REL) + 1];
    sprintf(booki_file, "%s/%s", home, BOOKI_FILE_REL);
    BOOKI_FILE = booki_file;
    char** remaining = NULL;

    if (argc == 1) {
        help(false);
        return 0;
    } else if (strcmp(argv[1], "open") == 0) {
        open();
    } else if (strcmp(argv[1], "search") == 0) {
        remaining = search(argc - 1, argv + 1);
    }

    return 0;
}