Blame


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