Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
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.
7 *
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.
15 */
17 #include "compat.h"
19 #include <ctype.h>
20 #include <fnmatch.h>
21 #include <string.h>
23 #include "parser.h"
24 #include "telescope.h"
26 static int check_for_utf8(char*);
28 static struct parser_table {
29 const char *mediatype;
30 void (*parserinit)(struct parser*);
31 } ptable[] = {
32 { "text/gemini", gemtext_initparser },
33 { "text/*", textplain_initparser },
34 { NULL, NULL}
35 };
37 static int
38 check_for_utf8(char *b)
39 {
40 for (;;) {
41 while (*b != '\0' && isspace(*b))
42 b++;
43 if (*b == '\0')
44 break;
45 if (!has_prefix(b, "charset=")) {
46 while (*b != '\0' && *b != ';')
47 b++;
48 if (*b == '\0')
49 break;
50 b++;
51 continue;
52 }
54 /* is charset= */
55 b += strlen("charset=");
56 /* TODO: improve the matching */
57 return has_prefix(b, "ASCII") || has_prefix(b, "ascii") ||
58 has_prefix(b, "UTF-8") || has_prefix(b, "utf-8");
59 }
61 return 1;
62 }
64 int
65 setup_parser_for(struct tab *tab)
66 {
67 char *b, buf[GEMINI_URL_LEN] = {0};
68 struct parser_table *t;
70 memcpy(buf, tab->meta, sizeof(tab->meta));
72 for (b = buf; *b != ';' && *b != '\0'; ++b)
73 ;
75 if (*b == ';') {
76 *b = '\0';
77 ++b;
78 }
80 if (!check_for_utf8(b))
81 return 0;
83 for (t = ptable; t->mediatype != NULL; ++t) {
84 if (!fnmatch(t->mediatype, buf, 0)) {
85 t->parserinit(&tab->buffer.page);
86 return 1;
87 }
88 }
90 return 0;
91 }