2 1ac119fb 2024-01-23 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
17 1ac119fb 2024-01-23 op #include "compat.h"
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>
23 1ac119fb 2024-01-23 op #include "parser.h"
24 1ac119fb 2024-01-23 op #include "utils.h"
26 e5e04904 2024-02-06 op #ifndef LINE_MAX
27 e5e04904 2024-02-06 op #define LINE_MAX 2048
30 1ac119fb 2024-01-23 op struct gm_selector {
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;
38 1ac119fb 2024-01-23 op static void gm_parse_selector(char *, struct gm_selector *);
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 *);
46 1ac119fb 2024-01-23 op gophermap_initparser(struct parser *p)
48 1ac119fb 2024-01-23 op memset(p, 0, sizeof(*p));
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;
55 1ac119fb 2024-01-23 op TAILQ_INIT(&p->head);
59 1ac119fb 2024-01-23 op gm_parse_selector(char *line, struct gm_selector *s)
61 1ac119fb 2024-01-23 op s->type = *line++;
63 1ac119fb 2024-01-23 op s->selector = "";
67 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
69 1ac119fb 2024-01-23 op *line++ = '\0';
70 1ac119fb 2024-01-23 op s->selector = line;
72 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
74 1ac119fb 2024-01-23 op *line++ = '\0';
75 1ac119fb 2024-01-23 op s->addr = line;
77 1ac119fb 2024-01-23 op if ((line = strchr(line, '\t')) == NULL)
79 1ac119fb 2024-01-23 op *line++ = '\0';
80 1ac119fb 2024-01-23 op s->port = line;
84 1ac119fb 2024-01-23 op gm_parse(struct parser *p, const char *buf, size_t size)
86 1ac119fb 2024-01-23 op return parser_foreach_line(p, buf, size, gm_foreach_line);
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)
92 1ac119fb 2024-01-23 op struct line *l;
93 1ac119fb 2024-01-23 op char buf[LINE_MAX], b[2] = {0};
95 1ac119fb 2024-01-23 op if ((l = calloc(1, sizeof(*l))) == NULL)
98 1ac119fb 2024-01-23 op if ((l->line = strdup(s->ds)) == NULL)
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));
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));
118 1ac119fb 2024-01-23 op if ((l->alt = strdup(buf)) == NULL)
126 1ac119fb 2024-01-23 op TAILQ_INSERT_TAIL(&p->head, l, lines);
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);
140 1ac119fb 2024-01-23 op gm_foreach_line(struct parser *p, const char *line, size_t linelen)
142 1ac119fb 2024-01-23 op char buf[LINE_MAX] = {0};
143 1ac119fb 2024-01-23 op struct gm_selector s = {0};
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);
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))
171 1ac119fb 2024-01-23 op case 'i': /* non-canonical: message */
172 1ac119fb 2024-01-23 op if (!emit_line(p, LINE_TEXT, &s))
176 1ac119fb 2024-01-23 op case '3': /* error code */
177 1ac119fb 2024-01-23 op if (!emit_line(p, LINE_QUOTE, &s))
186 1ac119fb 2024-01-23 op gm_free(struct parser *p)
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);
192 1ac119fb 2024-01-23 op free(p->buf);
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)
200 1ac119fb 2024-01-23 op *ret_type = 0;
202 1ac119fb 2024-01-23 op if (!strcmp(path, "/") || *path == '\0') {
203 1ac119fb 2024-01-23 op *ret_type = '1';
207 1ac119fb 2024-01-23 op if (*path != '/')
211 1ac119fb 2024-01-23 op switch (*ret_type = *path) {
218 1ac119fb 2024-01-23 op *ret_type = 0;
223 1ac119fb 2024-01-23 op return ++path;
227 1ac119fb 2024-01-23 op serialize_link(struct line *line, const char *text, FILE *fp)
229 1ac119fb 2024-01-23 op size_t portlen = 0;
231 1ac119fb 2024-01-23 op const char *uri, *endhost, *port, *path, *colon;
233 1ac119fb 2024-01-23 op if ((uri = line->alt) == NULL)
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);
240 1ac119fb 2024-01-23 op uri += 9; /* skip gopher:// */
242 1ac119fb 2024-01-23 op path = strchr(uri, '/');
243 1ac119fb 2024-01-23 op colon = strchr(uri, ':');
245 1ac119fb 2024-01-23 op if (path != NULL && colon > path)
246 1ac119fb 2024-01-23 op colon = NULL;
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');
252 1ac119fb 2024-01-23 op if (colon != NULL) {
253 1ac119fb 2024-01-23 op for (port = colon+1; *port && *port != '/'; ++port)
255 1ac119fb 2024-01-23 op port = colon+1;
261 1ac119fb 2024-01-23 op if (path == NULL) {
265 1ac119fb 2024-01-23 op path = gopher_skip_selector(path, &type);
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);
272 1ac119fb 2024-01-23 op gm_serialize(struct parser *p, FILE *fp)
274 1ac119fb 2024-01-23 op struct line *line;
275 1ac119fb 2024-01-23 op const char *text;
278 1ac119fb 2024-01-23 op TAILQ_FOREACH(line, &p->head, lines) {
279 1ac119fb 2024-01-23 op if ((text = line->line) == NULL)
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);
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);
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);
296 1ac119fb 2024-01-23 op /* unreachable */