Blob


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