2 748b15fa 2021-03-12 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 748b15fa 2021-03-12 op * Permission to use, copy, modify, and distribute this software for any
5 748b15fa 2021-03-12 op * purpose with or without fee is hereby granted, provided that the above
6 748b15fa 2021-03-12 op * copyright notice and this permission notice appear in all copies.
8 748b15fa 2021-03-12 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 748b15fa 2021-03-12 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 748b15fa 2021-03-12 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 748b15fa 2021-03-12 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 748b15fa 2021-03-12 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 748b15fa 2021-03-12 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 748b15fa 2021-03-12 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 786e6deb 2021-07-21 op #include "compat.h"
19 748b15fa 2021-03-12 op #include <ctype.h>
20 748b15fa 2021-03-12 op #include <fnmatch.h>
21 748b15fa 2021-03-12 op #include <string.h>
23 395b9f4e 2021-07-12 op #include "parser.h"
24 395b9f4e 2021-07-12 op #include "telescope.h"
25 9d65b1d9 2022-01-11 op #include "utils.h"
27 748b15fa 2021-03-12 op static int check_for_utf8(char*);
29 0d877486 2022-02-24 op static const struct parser_table {
30 fd1c80ce 2024-06-14 op const char *mediatype;
31 fd1c80ce 2024-06-14 op const struct parser *parser;
32 748b15fa 2021-03-12 op } ptable[] = {
33 c1d27b0e 2024-06-14 op { "text/gemini", &gemtext_parser },
34 c1d27b0e 2024-06-14 op { "text/x-patch", &textpatch_parser },
35 c1d27b0e 2024-06-14 op { "text/x-diff", &textpatch_parser },
36 c1d27b0e 2024-06-14 op { "application/x-patch",&textpatch_parser },
37 c1d27b0e 2024-06-14 op { "text/*", &textplain_parser },
42 748b15fa 2021-03-12 op check_for_utf8(char *b)
45 748b15fa 2021-03-12 op while (*b != '\0' && isspace(*b))
47 748b15fa 2021-03-12 op if (*b == '\0')
49 d89eb764 2022-04-24 op if (strncmp(b, "charset=", 8) != 0) {
50 748b15fa 2021-03-12 op while (*b != '\0' && *b != ';')
52 748b15fa 2021-03-12 op if (*b == '\0')
58 748b15fa 2021-03-12 op /* is charset= */
59 748b15fa 2021-03-12 op b += strlen("charset=");
60 748b15fa 2021-03-12 op /* TODO: improve the matching */
61 d89eb764 2022-04-24 op return !strncmp(b, "ASCII", 5) || !strncmp(b, "ascii", 5) ||
62 d89eb764 2022-04-24 op !strncmp(b, "UTF-8", 5) || !strncmp(b, "utf-8", 5);
69 748b15fa 2021-03-12 op setup_parser_for(struct tab *tab)
71 0d877486 2022-02-24 op char *b, buf[GEMINI_URL_LEN] = {0};
72 0d877486 2022-02-24 op const struct parser_table *t;
74 95a8c791 2021-08-26 op memcpy(buf, tab->meta, sizeof(tab->meta));
76 748b15fa 2021-03-12 op for (b = buf; *b != ';' && *b != '\0'; ++b)
79 748b15fa 2021-03-12 op if (*b == ';') {
84 748b15fa 2021-03-12 op if (!check_for_utf8(b))
87 748b15fa 2021-03-12 op for (t = ptable; t->mediatype != NULL; ++t) {
88 748b15fa 2021-03-12 op if (!fnmatch(t->mediatype, buf, 0)) {
89 c1d27b0e 2024-06-14 op parser_init(&tab->buffer, t->parser);