02
2021-03-12
op
* Copyright (c) 2021 Omar Polo <op@omarpolo.com>
04
2021-03-12
op
* Permission to use, copy, modify, and distribute this software for any
05
2021-03-12
op
* purpose with or without fee is hereby granted, provided that the above
06
2021-03-12
op
* copyright notice and this permission notice appear in all copies.
08
2021-03-12
op
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
09
2021-03-12
op
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
2021-03-12
op
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
2021-03-12
op
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
2021-03-12
op
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
2021-03-12
op
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
2021-03-12
op
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
2021-07-21
op
#include "compat.h"
19
2021-03-12
op
#include <ctype.h>
20
2021-03-12
op
#include <fnmatch.h>
21
2021-03-12
op
#include <string.h>
23
2021-07-12
op
#include "parser.h"
24
2021-07-12
op
#include "telescope.h"
25
2022-01-11
op
#include "utils.h"
27
2021-03-12
op
static int check_for_utf8(char*);
29
2022-02-24
op
static const struct parser_table {
30
2021-03-12
op
const char *mediatype;
31
2021-03-12
op
void (*parserinit)(struct parser*);
32
2021-03-12
op
} ptable[] = {
33
2021-03-12
op
{ "text/gemini", gemtext_initparser },
34
2021-07-30
op
{ "text/x-patch", textpatch_initparser },
35
2021-07-30
op
{ "text/x-diff", textpatch_initparser },
36
2021-07-30
op
{ "application/x-patch",textpatch_initparser },
37
2021-03-12
op
{ "text/*", textplain_initparser },
38
2021-03-12
op
{ NULL, NULL}
42
2021-03-12
op
check_for_utf8(char *b)
44
2021-03-12
op
for (;;) {
45
2021-03-12
op
while (*b != '\0' && isspace(*b))
47
2021-03-12
op
if (*b == '\0')
49
2022-04-24
op
if (strncmp(b, "charset=", 8) != 0) {
50
2021-03-12
op
while (*b != '\0' && *b != ';')
52
2021-03-12
op
if (*b == '\0')
58
2021-03-12
op
/* is charset= */
59
2021-03-12
op
b += strlen("charset=");
60
2021-03-12
op
/* TODO: improve the matching */
61
2022-04-24
op
return !strncmp(b, "ASCII", 5) || !strncmp(b, "ascii", 5) ||
62
2022-04-24
op
!strncmp(b, "UTF-8", 5) || !strncmp(b, "utf-8", 5);
69
2021-03-12
op
setup_parser_for(struct tab *tab)
71
2022-02-24
op
char *b, buf[GEMINI_URL_LEN] = {0};
72
2022-02-24
op
const struct parser_table *t;
74
2021-08-26
op
memcpy(buf, tab->meta, sizeof(tab->meta));
76
2021-03-12
op
for (b = buf; *b != ';' && *b != '\0'; ++b)
79
2021-03-12
op
if (*b == ';') {
80
2021-03-12
op
*b = '\0';
84
2021-03-12
op
if (!check_for_utf8(b))
87
2021-03-12
op
for (t = ptable; t->mediatype != NULL; ++t) {
88
2021-03-12
op
if (!fnmatch(t->mediatype, buf, 0)) {
89
2022-01-19
op
parser_init(tab, t->parserinit);