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"
25 #include "utils.h"
27 static int check_for_utf8(char*);
29 static const struct parser_table {
30 const char *mediatype;
31 void (*parserinit)(struct parser*);
32 } ptable[] = {
33 { "text/gemini", gemtext_initparser },
34 { "text/x-patch", textpatch_initparser },
35 { "text/x-diff", textpatch_initparser },
36 { "application/x-patch",textpatch_initparser },
37 { "text/*", textplain_initparser },
38 { NULL, NULL}
39 };
41 static int
42 check_for_utf8(char *b)
43 {
44 for (;;) {
45 while (*b != '\0' && isspace(*b))
46 b++;
47 if (*b == '\0')
48 break;
49 if (strncmp(b, "charset=", 8) != 0) {
50 while (*b != '\0' && *b != ';')
51 b++;
52 if (*b == '\0')
53 break;
54 b++;
55 continue;
56 }
58 /* is charset= */
59 b += strlen("charset=");
60 /* TODO: improve the matching */
61 return !strncmp(b, "ASCII", 5) || !strncmp(b, "ascii", 5) ||
62 !strncmp(b, "UTF-8", 5) || !strncmp(b, "utf-8", 5);
63 }
65 return 1;
66 }
68 int
69 setup_parser_for(struct tab *tab)
70 {
71 char *b, buf[GEMINI_URL_LEN] = {0};
72 const struct parser_table *t;
74 memcpy(buf, tab->meta, sizeof(tab->meta));
76 for (b = buf; *b != ';' && *b != '\0'; ++b)
77 ;
79 if (*b == ';') {
80 *b = '\0';
81 ++b;
82 }
84 if (!check_for_utf8(b))
85 return 0;
87 for (t = ptable; t->mediatype != NULL; ++t) {
88 if (!fnmatch(t->mediatype, buf, 0)) {
89 parser_init(tab, t->parserinit);
90 return 1;
91 }
92 }
94 return 0;
95 }