2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include "telescope.h"
24 static int check_for_utf8(char*);
26 static struct parser_table {
27 const char *mediatype;
28 void (*parserinit)(struct parser*);
30 { "text/gemini", gemtext_initparser },
31 { "text/*", textplain_initparser },
36 check_for_utf8(char *b)
39 while (*b != '\0' && isspace(*b))
43 if (!has_prefix(b, "charset=")) {
44 while (*b != '\0' && *b != ';')
53 b += strlen("charset=");
54 /* TODO: improve the matching */
55 return has_prefix(b, "ASCII") || has_prefix(b, "ascii") ||
56 has_prefix(b, "UTF-8") || has_prefix(b, "utf-8");
63 setup_parser_for(struct tab *tab)
65 char *b, buf[GEMINI_URL_LEN] = {0};
66 struct parser_table *t;
68 memcpy(buf, tab->meta, sizeof(tab->meta));
70 for (b = buf; *b != ';' && *b != '\0'; ++b)
78 if (!check_for_utf8(b))
81 for (t = ptable; t->mediatype != NULL; ++t) {
82 if (!fnmatch(t->mediatype, buf, 0)) {
83 t->parserinit(&tab->buffer.page);