Blame


1 1ac119fb 2024-01-23 op /*
2 1ac119fb 2024-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 1ac119fb 2024-01-23 op *
4 1ac119fb 2024-01-23 op * Permission to use, copy, modify, and distribute this software for any
5 1ac119fb 2024-01-23 op * purpose with or without fee is hereby granted, provided that the above
6 1ac119fb 2024-01-23 op * copyright notice and this permission notice appear in all copies.
7 1ac119fb 2024-01-23 op *
8 1ac119fb 2024-01-23 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 1ac119fb 2024-01-23 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 1ac119fb 2024-01-23 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 1ac119fb 2024-01-23 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 1ac119fb 2024-01-23 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 1ac119fb 2024-01-23 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 1ac119fb 2024-01-23 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 1ac119fb 2024-01-23 op */
16 1ac119fb 2024-01-23 op
17 1ac119fb 2024-01-23 op #include "compat.h"
18 1ac119fb 2024-01-23 op
19 1ac119fb 2024-01-23 op #include <stdio.h>
20 1ac119fb 2024-01-23 op #include <stdlib.h>
21 1ac119fb 2024-01-23 op #include <string.h>
22 1ac119fb 2024-01-23 op
23 1ac119fb 2024-01-23 op #include "parser.h"
24 1ac119fb 2024-01-23 op #include "utils.h"
25 e5e04904 2024-02-06 op
26 e5e04904 2024-02-06 op #ifndef LINE_MAX
27 e5e04904 2024-02-06 op #define LINE_MAX 2048
28 e5e04904 2024-02-06 op #endif
29 1ac119fb 2024-01-23 op
30 1ac119fb 2024-01-23 op struct gm_selector {
31 1ac119fb 2024-01-23 op char type;
32 1ac119fb 2024-01-23 op const char *ds;
33 1ac119fb 2024-01-23 op const char *selector;
34 1ac119fb 2024-01-23 op const char *addr;
35 1ac119fb 2024-01-23 op const char *port;
36 1ac119fb 2024-01-23 op };
37 1ac119fb 2024-01-23 op
38 1ac119fb 2024-01-23 op static void gm_parse_selector(char *, struct gm_selector *);
39 1ac119fb 2024-01-23 op
40 1ac119fb 2024-01-23 op static int gm_parse(struct parser *, const char *, size_t);
41 1ac119fb 2024-01-23 op static int gm_foreach_line(struct parser *, const char *, size_t);
42 1ac119fb 2024-01-23 op static int gm_free(struct parser *);
43 1ac119fb 2024-01-23 op static int gm_serialize(struct parser *, FILE *);
44 1ac119fb 2024-01-23 op
45 1ac119fb 2024-01-23 op void
46 1ac119fb 2024-01-23 op gophermap_initparser(struct parser *p)
47 1ac119fb 2024-01-23 op {
48 1ac119fb 2024-01-23 op memset(p, 0, sizeof(*p));
49 1ac119fb 2024-01-23 op
50 1ac119fb 2024-01-23 op p->name = "gophermap";
51 1ac119fb 2024-01-23 op p->parse = &gm_parse;
52 1ac119fb 2024-01-23 op p->free = &gm_free;
53 1ac119fb 2024-01-23 op p->serialize = &gm_serialize;
54 1ac119fb 2024-01-23 op
55 1ac119fb 2024-01-23 op TAILQ_INIT(&p->head);
56 1ac119fb 2024-01-23 op }
57 1ac119fb 2024-01-23 op
58 1ac119fb 2024-01-23 op static void
59 1ac119fb 2024-01-23 op gm_parse_selector(char *line, struct gm_selector *s)
60 1ac119fb 2024-01-23 op {
61 1ac119fb 2024-01-23 op s->type = *line++;
62 1ac119fb 2024-01-23 op s->ds = line;
63 1ac119fb 2024-01-23 op s->selector = "";
64 1ac119fb 2024-01-23 op s->addr = "";
65 1ac119fb 2024-01-23 op s->port = "";
66 1ac119fb 2024-01-23 op
67 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
68 1ac119fb 2024-01-23 op return;
69 1ac119fb 2024-01-23 op *line++ = '\0';
70 1ac119fb 2024-01-23 op s->selector = line;
71 1ac119fb 2024-01-23 op
72 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
73 1ac119fb 2024-01-23 op return;
74 1ac119fb 2024-01-23 op *line++ = '\0';
75 1ac119fb 2024-01-23 op s->addr = line;
76 1ac119fb 2024-01-23 op
77 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
78 1ac119fb 2024-01-23 op return;
79 1ac119fb 2024-01-23 op *line++ = '\0';
80 1ac119fb 2024-01-23 op s->port = line;
81 1ac119fb 2024-01-23 op }
82 1ac119fb 2024-01-23 op
83 1ac119fb 2024-01-23 op static int
84 1ac119fb 2024-01-23 op gm_parse(struct parser *p, const char *buf, size_t size)
85 1ac119fb 2024-01-23 op {
86 1ac119fb 2024-01-23 op return parser_foreach_line(p, buf, size, gm_foreach_line);
87 1ac119fb 2024-01-23 op }
88 1ac119fb 2024-01-23 op
89 1ac119fb 2024-01-23 op static inline int
90 1ac119fb 2024-01-23 op emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
91 1ac119fb 2024-01-23 op {
92 1ac119fb 2024-01-23 op struct line *l;
93 1ac119fb 2024-01-23 op char buf[LINE_MAX], b[2] = {0};
94 1ac119fb 2024-01-23 op
95 1ac119fb 2024-01-23 op if ((l = calloc(1, sizeof(*l))) == NULL)
96 1ac119fb 2024-01-23 op goto err;
97 1ac119fb 2024-01-23 op
98 1ac119fb 2024-01-23 op if ((l->line = strdup(s->ds)) == NULL)
99 1ac119fb 2024-01-23 op goto err;
100 1ac119fb 2024-01-23 op
101 1ac119fb 2024-01-23 op switch (l->type = type) {
102 1ac119fb 2024-01-23 op case LINE_LINK:
103 1ac119fb 2024-01-23 op if (s->type == 'h' && !strncmp(s->selector, "URL:", 4)) {
104 1ac119fb 2024-01-23 op strlcpy(buf, s->selector+4, sizeof(buf));
105 1ac119fb 2024-01-23 op } else {
106 1ac119fb 2024-01-23 op strlcpy(buf, "gopher://", sizeof(buf));
107 1ac119fb 2024-01-23 op strlcat(buf, s->addr, sizeof(buf));
108 1ac119fb 2024-01-23 op strlcat(buf, ":", sizeof(buf));
109 1ac119fb 2024-01-23 op strlcat(buf, s->port, sizeof(buf));
110 1ac119fb 2024-01-23 op strlcat(buf, "/", sizeof(buf));
111 1ac119fb 2024-01-23 op b[0] = s->type;
112 1ac119fb 2024-01-23 op strlcat(buf, b, sizeof(buf));
113 1ac119fb 2024-01-23 op if (*s->selector != '/')
114 1ac119fb 2024-01-23 op strlcat(buf, "/", sizeof(buf));
115 1ac119fb 2024-01-23 op strlcat(buf, s->selector, sizeof(buf));
116 1ac119fb 2024-01-23 op }
117 1ac119fb 2024-01-23 op
118 1ac119fb 2024-01-23 op if ((l->alt = strdup(buf)) == NULL)
119 1ac119fb 2024-01-23 op goto err;
120 1ac119fb 2024-01-23 op break;
121 1ac119fb 2024-01-23 op
122 1ac119fb 2024-01-23 op default:
123 1ac119fb 2024-01-23 op break;
124 1ac119fb 2024-01-23 op }
125 1ac119fb 2024-01-23 op
126 1ac119fb 2024-01-23 op TAILQ_INSERT_TAIL(&p->head, l, lines);
127 1ac119fb 2024-01-23 op
128 1ac119fb 2024-01-23 op return 1;
129 1ac119fb 2024-01-23 op
130 1ac119fb 2024-01-23 op err:
131 1ac119fb 2024-01-23 op if (l != NULL) {
132 1ac119fb 2024-01-23 op free(l->line);
133 1ac119fb 2024-01-23 op free(l->alt);
134 1ac119fb 2024-01-23 op free(l);
135 1ac119fb 2024-01-23 op }
136 1ac119fb 2024-01-23 op return 0;
137 1ac119fb 2024-01-23 op }
138 1ac119fb 2024-01-23 op
139 1ac119fb 2024-01-23 op static int
140 1ac119fb 2024-01-23 op gm_foreach_line(struct parser *p, const char *line, size_t linelen)
141 1ac119fb 2024-01-23 op {
142 1ac119fb 2024-01-23 op char buf[LINE_MAX] = {0};
143 1ac119fb 2024-01-23 op struct gm_selector s = {0};
144 1ac119fb 2024-01-23 op
145 1ac119fb 2024-01-23 op memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
146 1ac119fb 2024-01-23 op gm_parse_selector(buf, &s);
147 1ac119fb 2024-01-23 op
148 1ac119fb 2024-01-23 op switch (s.type) {
149 1ac119fb 2024-01-23 op case '0': /* text file */
150 1ac119fb 2024-01-23 op case '1': /* gopher submenu */
151 1ac119fb 2024-01-23 op case '2': /* CCSO nameserver */
152 1ac119fb 2024-01-23 op case '4': /* binhex-encoded file */
153 1ac119fb 2024-01-23 op case '5': /* DOS file */
154 1ac119fb 2024-01-23 op case '6': /* uuencoded file */
155 1ac119fb 2024-01-23 op case '7': /* full-text search */
156 1ac119fb 2024-01-23 op case '8': /* telnet */
157 1ac119fb 2024-01-23 op case '9': /* binary file */
158 1ac119fb 2024-01-23 op case '+': /* mirror or alternate server */
159 1ac119fb 2024-01-23 op case 'g': /* gif */
160 1ac119fb 2024-01-23 op case 'I': /* image */
161 1ac119fb 2024-01-23 op case 'T': /* telnet 3270 */
162 1ac119fb 2024-01-23 op case ':': /* gopher+: bitmap image */
163 1ac119fb 2024-01-23 op case ';': /* gopher+: movie file */
164 1ac119fb 2024-01-23 op case 'd': /* non-canonical: doc */
165 1ac119fb 2024-01-23 op case 'h': /* non-canonical: html file */
166 1ac119fb 2024-01-23 op case 's': /* non-canonical: sound file */
167 1ac119fb 2024-01-23 op if (!emit_line(p, LINE_LINK, &s))
168 1ac119fb 2024-01-23 op return 0;
169 1ac119fb 2024-01-23 op break;
170 1ac119fb 2024-01-23 op
171 1ac119fb 2024-01-23 op case 'i': /* non-canonical: message */
172 1ac119fb 2024-01-23 op if (!emit_line(p, LINE_TEXT, &s))
173 1ac119fb 2024-01-23 op return 0;
174 1ac119fb 2024-01-23 op break;
175 1ac119fb 2024-01-23 op
176 1ac119fb 2024-01-23 op case '3': /* error code */
177 1ac119fb 2024-01-23 op if (!emit_line(p, LINE_QUOTE, &s))
178 1ac119fb 2024-01-23 op return 0;
179 1ac119fb 2024-01-23 op break;
180 1ac119fb 2024-01-23 op }
181 1ac119fb 2024-01-23 op
182 1ac119fb 2024-01-23 op return 1;
183 1ac119fb 2024-01-23 op }
184 1ac119fb 2024-01-23 op
185 1ac119fb 2024-01-23 op static int
186 1ac119fb 2024-01-23 op gm_free(struct parser *p)
187 1ac119fb 2024-01-23 op {
188 1ac119fb 2024-01-23 op /* flush the buffer */
189 1ac119fb 2024-01-23 op if (p->len != 0)
190 1ac119fb 2024-01-23 op gm_foreach_line(p, p->buf, p->len);
191 1ac119fb 2024-01-23 op
192 1ac119fb 2024-01-23 op free(p->buf);
193 1ac119fb 2024-01-23 op
194 1ac119fb 2024-01-23 op return 1;
195 1ac119fb 2024-01-23 op }
196 1ac119fb 2024-01-23 op
197 1ac119fb 2024-01-23 op static inline const char *
198 1ac119fb 2024-01-23 op gopher_skip_selector(const char *path, int *ret_type)
199 1ac119fb 2024-01-23 op {
200 1ac119fb 2024-01-23 op *ret_type = 0;
201 1ac119fb 2024-01-23 op
202 1ac119fb 2024-01-23 op if (!strcmp(path, "/") || *path == '\0') {
203 1ac119fb 2024-01-23 op *ret_type = '1';
204 1ac119fb 2024-01-23 op return path;
205 1ac119fb 2024-01-23 op }
206 1ac119fb 2024-01-23 op
207 1ac119fb 2024-01-23 op if (*path != '/')
208 1ac119fb 2024-01-23 op return path;
209 1ac119fb 2024-01-23 op path++;
210 1ac119fb 2024-01-23 op
211 1ac119fb 2024-01-23 op switch (*ret_type = *path) {
212 1ac119fb 2024-01-23 op case '0':
213 1ac119fb 2024-01-23 op case '1':
214 1ac119fb 2024-01-23 op case '7':
215 1ac119fb 2024-01-23 op break;
216 1ac119fb 2024-01-23 op
217 1ac119fb 2024-01-23 op default:
218 1ac119fb 2024-01-23 op *ret_type = 0;
219 1ac119fb 2024-01-23 op path -= 1;
220 1ac119fb 2024-01-23 op return path;
221 1ac119fb 2024-01-23 op }
222 1ac119fb 2024-01-23 op
223 1ac119fb 2024-01-23 op return ++path;
224 1ac119fb 2024-01-23 op }
225 1ac119fb 2024-01-23 op
226 1ac119fb 2024-01-23 op static int
227 1ac119fb 2024-01-23 op serialize_link(struct line *line, const char *text, FILE *fp)
228 1ac119fb 2024-01-23 op {
229 1ac119fb 2024-01-23 op size_t portlen = 0;
230 1ac119fb 2024-01-23 op int type;
231 1ac119fb 2024-01-23 op const char *uri, *endhost, *port, *path, *colon;
232 1ac119fb 2024-01-23 op
233 1ac119fb 2024-01-23 op if ((uri = line->alt) == NULL)
234 1ac119fb 2024-01-23 op return -1;
235 1ac119fb 2024-01-23 op
236 1ac119fb 2024-01-23 op if (strncmp(uri, "gopher://", 9) != 0)
237 1ac119fb 2024-01-23 op return fprintf(fp, "h%s\tURL:%s\terror.host\t1\n",
238 1ac119fb 2024-01-23 op text, line->alt);
239 1ac119fb 2024-01-23 op
240 1ac119fb 2024-01-23 op uri += 9; /* skip gopher:// */
241 1ac119fb 2024-01-23 op
242 1ac119fb 2024-01-23 op path = strchr(uri, '/');
243 1ac119fb 2024-01-23 op colon = strchr(uri, ':');
244 1ac119fb 2024-01-23 op
245 1ac119fb 2024-01-23 op if (path != NULL && colon > path)
246 1ac119fb 2024-01-23 op colon = NULL;
247 1ac119fb 2024-01-23 op
248 1ac119fb 2024-01-23 op if ((endhost = colon) == NULL &&
249 1ac119fb 2024-01-23 op (endhost = path) == NULL)
250 1ac119fb 2024-01-23 op endhost = strchr(uri, '\0');
251 1ac119fb 2024-01-23 op
252 1ac119fb 2024-01-23 op if (colon != NULL) {
253 1ac119fb 2024-01-23 op for (port = colon+1; *port && *port != '/'; ++port)
254 1ac119fb 2024-01-23 op ++portlen;
255 1ac119fb 2024-01-23 op port = colon+1;
256 1ac119fb 2024-01-23 op } else {
257 1ac119fb 2024-01-23 op port = "70";
258 1ac119fb 2024-01-23 op portlen = 2;
259 1ac119fb 2024-01-23 op }
260 1ac119fb 2024-01-23 op
261 1ac119fb 2024-01-23 op if (path == NULL) {
262 1ac119fb 2024-01-23 op type = '1';
263 1ac119fb 2024-01-23 op path = "";
264 1ac119fb 2024-01-23 op } else
265 1ac119fb 2024-01-23 op path = gopher_skip_selector(path, &type);
266 1ac119fb 2024-01-23 op
267 1ac119fb 2024-01-23 op return fprintf(fp, "%c%s\t%s\t%.*s\t%.*s\n", type, text,
268 1ac119fb 2024-01-23 op path, (int)(endhost - uri), uri, (int)portlen, port);
269 1ac119fb 2024-01-23 op }
270 1ac119fb 2024-01-23 op
271 1ac119fb 2024-01-23 op static int
272 1ac119fb 2024-01-23 op gm_serialize(struct parser *p, FILE *fp)
273 1ac119fb 2024-01-23 op {
274 1ac119fb 2024-01-23 op struct line *line;
275 1ac119fb 2024-01-23 op const char *text;
276 1ac119fb 2024-01-23 op int r;
277 1ac119fb 2024-01-23 op
278 1ac119fb 2024-01-23 op TAILQ_FOREACH(line, &p->head, lines) {
279 1ac119fb 2024-01-23 op if ((text = line->line) == NULL)
280 1ac119fb 2024-01-23 op text = "";
281 1ac119fb 2024-01-23 op
282 1ac119fb 2024-01-23 op switch (line->type) {
283 1ac119fb 2024-01-23 op case LINE_LINK:
284 1ac119fb 2024-01-23 op r = serialize_link(line, text, fp);
285 1ac119fb 2024-01-23 op break;
286 1ac119fb 2024-01-23 op
287 1ac119fb 2024-01-23 op case LINE_TEXT:
288 1ac119fb 2024-01-23 op r = fprintf(fp, "i%s\t\terror.host\t1\n", text);
289 1ac119fb 2024-01-23 op break;
290 1ac119fb 2024-01-23 op
291 1ac119fb 2024-01-23 op case LINE_QUOTE:
292 1ac119fb 2024-01-23 op r = fprintf(fp, "3%s\t\terror.host\t1\n", text);
293 1ac119fb 2024-01-23 op break;
294 1ac119fb 2024-01-23 op
295 1ac119fb 2024-01-23 op default:
296 1ac119fb 2024-01-23 op /* unreachable */
297 1ac119fb 2024-01-23 op abort();
298 1ac119fb 2024-01-23 op }
299 1ac119fb 2024-01-23 op
300 1ac119fb 2024-01-23 op if (r == -1)
301 1ac119fb 2024-01-23 op return 0;
302 1ac119fb 2024-01-23 op }
303 1ac119fb 2024-01-23 op
304 1ac119fb 2024-01-23 op return 1;
305 1ac119fb 2024-01-23 op }