2 81839fee 2021-07-25 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 81839fee 2021-07-25 op * Permission to use, copy, modify, and distribute this software for any
5 81839fee 2021-07-25 op * purpose with or without fee is hereby granted, provided that the above
6 81839fee 2021-07-25 op * copyright notice and this permission notice appear in all copies.
8 81839fee 2021-07-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 81839fee 2021-07-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 81839fee 2021-07-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 81839fee 2021-07-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 81839fee 2021-07-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 81839fee 2021-07-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 81839fee 2021-07-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 81839fee 2021-07-25 op #include "compat.h"
19 81839fee 2021-07-25 op #include <stdio.h>
20 81839fee 2021-07-25 op #include <stdlib.h>
21 81839fee 2021-07-25 op #include <string.h>
23 81839fee 2021-07-25 op #include "parser.h"
24 9d65b1d9 2022-01-11 op #include "utils.h"
26 81839fee 2021-07-25 op struct gm_selector {
28 81839fee 2021-07-25 op const char *ds;
29 81839fee 2021-07-25 op const char *selector;
30 81839fee 2021-07-25 op const char *addr;
31 81839fee 2021-07-25 op const char *port;
34 81839fee 2021-07-25 op static void gm_parse_selector(char *, struct gm_selector *);
36 81839fee 2021-07-25 op static int gm_parse(struct parser *, const char *, size_t);
37 81839fee 2021-07-25 op static int gm_foreach_line(struct parser *, const char *, size_t);
38 81839fee 2021-07-25 op static int gm_free(struct parser *);
41 81839fee 2021-07-25 op gophermap_initparser(struct parser *p)
43 81839fee 2021-07-25 op memset(p, 0, sizeof(*p));
45 81839fee 2021-07-25 op p->name = "gophermap";
46 81839fee 2021-07-25 op p->parse = &gm_parse;
47 81839fee 2021-07-25 op p->free = &gm_free;
49 78894e73 2021-08-12 op TAILQ_INIT(&p->head);
53 81839fee 2021-07-25 op gm_parse_selector(char *line, struct gm_selector *s)
55 81839fee 2021-07-25 op s->type = *line++;
57 6c739557 2021-08-13 op s->selector = "";
61 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
63 95a8c791 2021-08-26 op *line++ = '\0';
64 81839fee 2021-07-25 op s->selector = line;
66 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
68 81839fee 2021-07-25 op *line++ = '\0';
69 81839fee 2021-07-25 op s->addr = line;
71 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
73 81839fee 2021-07-25 op *line++ = '\0';
74 81839fee 2021-07-25 op s->port = line;
78 81839fee 2021-07-25 op gm_parse(struct parser *p, const char *buf, size_t size)
80 81839fee 2021-07-25 op return parser_foreach_line(p, buf, size, gm_foreach_line);
83 81839fee 2021-07-25 op static inline int
84 81839fee 2021-07-25 op emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
86 81839fee 2021-07-25 op struct line *l;
87 81839fee 2021-07-25 op char buf[LINE_MAX], b[2] = {0};
89 81839fee 2021-07-25 op if ((l = calloc(1, sizeof(*l))) == NULL)
92 81839fee 2021-07-25 op if ((l->line = strdup(s->ds)) == NULL)
95 81839fee 2021-07-25 op switch (l->type = type) {
96 81839fee 2021-07-25 op case LINE_LINK:
97 81839fee 2021-07-25 op if (s->type == 'h' && has_prefix(s->selector, "URL:")) {
98 81839fee 2021-07-25 op strlcpy(buf, s->selector+4, sizeof(buf));
100 81839fee 2021-07-25 op strlcpy(buf, "gopher://", sizeof(buf));
101 81839fee 2021-07-25 op strlcat(buf, s->addr, sizeof(buf));
102 81839fee 2021-07-25 op strlcat(buf, ":", sizeof(buf));
103 81839fee 2021-07-25 op strlcat(buf, s->port, sizeof(buf));
104 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
105 81839fee 2021-07-25 op b[0] = s->type;
106 81839fee 2021-07-25 op strlcat(buf, b, sizeof(buf));
107 81839fee 2021-07-25 op if (*s->selector != '/')
108 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
109 81839fee 2021-07-25 op strlcat(buf, s->selector, sizeof(buf));
112 81839fee 2021-07-25 op if ((l->alt = strdup(buf)) == NULL)
120 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
125 81839fee 2021-07-25 op if (l != NULL) {
126 81839fee 2021-07-25 op free(l->line);
127 81839fee 2021-07-25 op free(l->alt);
134 81839fee 2021-07-25 op gm_foreach_line(struct parser *p, const char *line, size_t linelen)
136 81839fee 2021-07-25 op char buf[LINE_MAX] = {0};
137 81839fee 2021-07-25 op struct gm_selector s = {0};
139 81839fee 2021-07-25 op memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
140 81839fee 2021-07-25 op gm_parse_selector(buf, &s);
142 81839fee 2021-07-25 op switch (s.type) {
143 81839fee 2021-07-25 op case '0': /* text file */
144 81839fee 2021-07-25 op case '1': /* gopher submenu */
145 81839fee 2021-07-25 op case '2': /* CCSO nameserver */
146 81839fee 2021-07-25 op case '4': /* binhex-encoded file */
147 81839fee 2021-07-25 op case '5': /* DOS file */
148 81839fee 2021-07-25 op case '6': /* uuencoded file */
149 81839fee 2021-07-25 op case '7': /* full-text search */
150 81839fee 2021-07-25 op case '8': /* telnet */
151 81839fee 2021-07-25 op case '9': /* binary file */
152 81839fee 2021-07-25 op case '+': /* mirror or alternate server */
153 81839fee 2021-07-25 op case 'g': /* gif */
154 81839fee 2021-07-25 op case 'I': /* image */
155 81839fee 2021-07-25 op case 'T': /* telnet 3270 */
156 81839fee 2021-07-25 op case ':': /* gopher+: bitmap image */
157 81839fee 2021-07-25 op case ';': /* gopher+: movie file */
158 81839fee 2021-07-25 op case 'd': /* non-canonical: doc */
159 81839fee 2021-07-25 op case 'h': /* non-canonical: html file */
160 81839fee 2021-07-25 op case 's': /* non-canonical: sound file */
161 81839fee 2021-07-25 op if (!emit_line(p, LINE_LINK, &s))
167 81839fee 2021-07-25 op case 'i': /* non-canonical: message */
168 81839fee 2021-07-25 op if (!emit_line(p, LINE_TEXT, &s))
172 81839fee 2021-07-25 op case '3': /* error code */
173 81839fee 2021-07-25 op if (!emit_line(p, LINE_QUOTE, &s))
182 81839fee 2021-07-25 op gm_free(struct parser *p)
184 81839fee 2021-07-25 op /* flush the buffer */
185 81839fee 2021-07-25 op if (p->len != 0)
186 81839fee 2021-07-25 op gm_foreach_line(p, p->buf, p->len);
188 81839fee 2021-07-25 op free(p->buf);