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 <ctype.h>
18 #include <fnmatch.h>
19 #include <string.h>
21 #include "parser.h"
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*);
29 } ptable[] = {
30 { "text/gemini", gemtext_initparser },
31 { "text/*", textplain_initparser },
32 { NULL, NULL}
33 };
35 static int
36 check_for_utf8(char *b)
37 {
38 for (;;) {
39 while (*b != '\0' && isspace(*b))
40 b++;
41 if (*b == '\0')
42 break;
43 if (!has_prefix(b, "charset=")) {
44 while (*b != '\0' && *b != ';')
45 b++;
46 if (*b == '\0')
47 break;
48 b++;
49 continue;
50 }
52 /* is charset= */
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");
57 }
59 return 1;
60 }
62 int
63 setup_parser_for(struct tab *tab)
64 {
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)
71 ;
73 if (*b == ';') {
74 *b = '\0';
75 ++b;
76 }
78 if (!check_for_utf8(b))
79 return 0;
81 for (t = ptable; t->mediatype != NULL; ++t) {
82 if (!fnmatch(t->mediatype, buf, 0)) {
83 t->parserinit(&tab->buffer.page);
84 return 1;
85 }
86 }
88 return 0;
89 }