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 748b15fa 2021-03-12 op #include <ctype.h>
18 748b15fa 2021-03-12 op #include <fnmatch.h>
19 748b15fa 2021-03-12 op #include <string.h>
21 395b9f4e 2021-07-12 op #include "parser.h"
22 395b9f4e 2021-07-12 op #include "telescope.h"
24 748b15fa 2021-03-12 op static int check_for_utf8(char*);
26 748b15fa 2021-03-12 op static struct parser_table {
27 748b15fa 2021-03-12 op const char *mediatype;
28 748b15fa 2021-03-12 op void (*parserinit)(struct parser*);
29 748b15fa 2021-03-12 op } ptable[] = {
30 c49d61bc 2021-03-12 op { "text/gemini", gemtext_initparser },
31 c49d61bc 2021-03-12 op { "text/*", textplain_initparser },
36 748b15fa 2021-03-12 op check_for_utf8(char *b)
39 748b15fa 2021-03-12 op while (*b != '\0' && isspace(*b))
41 748b15fa 2021-03-12 op if (*b == '\0')
43 748b15fa 2021-03-12 op if (!has_prefix(b, "charset=")) {
44 748b15fa 2021-03-12 op while (*b != '\0' && *b != ';')
46 748b15fa 2021-03-12 op if (*b == '\0')
52 748b15fa 2021-03-12 op /* is charset= */
53 748b15fa 2021-03-12 op b += strlen("charset=");
54 748b15fa 2021-03-12 op /* TODO: improve the matching */
55 748b15fa 2021-03-12 op return has_prefix(b, "ASCII") || has_prefix(b, "ascii") ||
56 748b15fa 2021-03-12 op has_prefix(b, "UTF-8") || has_prefix(b, "utf-8");
63 748b15fa 2021-03-12 op setup_parser_for(struct tab *tab)
65 748b15fa 2021-03-12 op char *b, buf[GEMINI_URL_LEN] = {0};
66 748b15fa 2021-03-12 op struct parser_table *t;
68 748b15fa 2021-03-12 op memcpy(buf, tab->meta, sizeof(tab->meta));
70 748b15fa 2021-03-12 op for (b = buf; *b != ';' && *b != '\0'; ++b)
73 748b15fa 2021-03-12 op if (*b == ';') {
78 748b15fa 2021-03-12 op if (!check_for_utf8(b))
81 748b15fa 2021-03-12 op for (t = ptable; t->mediatype != NULL; ++t) {
82 748b15fa 2021-03-12 op if (!fnmatch(t->mediatype, buf, 0)) {
83 46f6e974 2021-05-17 op t->parserinit(&tab->buffer.page);