Blob


1 /*
2 * Copyright (c) 2021 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 "utils.h"
26 struct gm_selector {
27 char type;
28 const char *ds;
29 const char *selector;
30 const char *addr;
31 const char *port;
32 };
34 static void gm_parse_selector(char *, struct gm_selector *);
36 static int gm_parse(struct parser *, const char *, size_t);
37 static int gm_foreach_line(struct parser *, const char *, size_t);
38 static int gm_free(struct parser *);
39 static int gm_serialize(struct parser *, FILE *);
41 void
42 gophermap_initparser(struct parser *p)
43 {
44 memset(p, 0, sizeof(*p));
46 p->name = "gophermap";
47 p->parse = &gm_parse;
48 p->free = &gm_free;
49 p->serialize = &gm_serialize;
51 TAILQ_INIT(&p->head);
52 }
54 static void
55 gm_parse_selector(char *line, struct gm_selector *s)
56 {
57 s->type = *line++;
58 s->ds = line;
59 s->selector = "";
60 s->addr = "";
61 s->port = "";
63 if ((line = strchr(line, '\t')) == NULL)
64 return;
65 *line++ = '\0';
66 s->selector = line;
68 if ((line = strchr(line, '\t')) == NULL)
69 return;
70 *line++ = '\0';
71 s->addr = line;
73 if ((line = strchr(line, '\t')) == NULL)
74 return;
75 *line++ = '\0';
76 s->port = line;
77 }
79 static int
80 gm_parse(struct parser *p, const char *buf, size_t size)
81 {
82 return parser_foreach_line(p, buf, size, gm_foreach_line);
83 }
85 static inline int
86 emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
87 {
88 struct line *l;
89 char buf[LINE_MAX], b[2] = {0};
91 if ((l = calloc(1, sizeof(*l))) == NULL)
92 goto err;
94 if ((l->line = strdup(s->ds)) == NULL)
95 goto err;
97 switch (l->type = type) {
98 case LINE_LINK:
99 if (s->type == 'h' && !strncmp(s->selector, "URL:", 4)) {
100 strlcpy(buf, s->selector+4, sizeof(buf));
101 } else {
102 strlcpy(buf, "gopher://", sizeof(buf));
103 strlcat(buf, s->addr, sizeof(buf));
104 strlcat(buf, ":", sizeof(buf));
105 strlcat(buf, s->port, sizeof(buf));
106 strlcat(buf, "/", sizeof(buf));
107 b[0] = s->type;
108 strlcat(buf, b, sizeof(buf));
109 if (*s->selector != '/')
110 strlcat(buf, "/", sizeof(buf));
111 strlcat(buf, s->selector, sizeof(buf));
114 if ((l->alt = strdup(buf)) == NULL)
115 goto err;
116 break;
118 default:
119 break;
122 TAILQ_INSERT_TAIL(&p->head, l, lines);
124 return 1;
126 err:
127 if (l != NULL) {
128 free(l->line);
129 free(l->alt);
130 free(l);
132 return 0;
135 static int
136 gm_foreach_line(struct parser *p, const char *line, size_t linelen)
138 char buf[LINE_MAX] = {0};
139 struct gm_selector s = {0};
141 memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
142 gm_parse_selector(buf, &s);
144 switch (s.type) {
145 case '0': /* text file */
146 case '1': /* gopher submenu */
147 case '2': /* CCSO nameserver */
148 case '4': /* binhex-encoded file */
149 case '5': /* DOS file */
150 case '6': /* uuencoded file */
151 case '7': /* full-text search */
152 case '8': /* telnet */
153 case '9': /* binary file */
154 case '+': /* mirror or alternate server */
155 case 'g': /* gif */
156 case 'I': /* image */
157 case 'T': /* telnet 3270 */
158 case ':': /* gopher+: bitmap image */
159 case ';': /* gopher+: movie file */
160 case 'd': /* non-canonical: doc */
161 case 'h': /* non-canonical: html file */
162 case 's': /* non-canonical: sound file */
163 if (!emit_line(p, LINE_LINK, &s))
164 return 0;
165 break;
167 break;
169 case 'i': /* non-canonical: message */
170 if (!emit_line(p, LINE_TEXT, &s))
171 return 0;
172 break;
174 case '3': /* error code */
175 if (!emit_line(p, LINE_QUOTE, &s))
176 return 0;
177 break;
180 return 1;
183 static int
184 gm_free(struct parser *p)
186 /* flush the buffer */
187 if (p->len != 0)
188 gm_foreach_line(p, p->buf, p->len);
190 free(p->buf);
192 return 1;
195 static inline const char *
196 gopher_skip_selector(const char *path, int *ret_type)
198 *ret_type = 0;
200 if (!strcmp(path, "/") || *path == '\0') {
201 *ret_type = '1';
202 return path;
205 if (*path != '/')
206 return path;
207 path++;
209 switch (*ret_type = *path) {
210 case '0':
211 case '1':
212 case '7':
213 break;
215 default:
216 *ret_type = 0;
217 path -= 1;
218 return path;
221 return ++path;
224 static int
225 serialize_link(struct line *line, const char *text, FILE *fp)
227 size_t portlen = 0;
228 int type;
229 const char *uri, *endhost, *port, *path, *colon;
231 if ((uri = line->alt) == NULL)
232 return -1;
234 if (strncmp(uri, "gopher://", 9) != 0)
235 return fprintf(fp, "h%s\tURL:%s\terror.host\t1\n",
236 text, line->alt);
238 uri += 9; /* skip gopher:// */
240 path = strchr(uri, '/');
241 colon = strchr(uri, ':');
243 if (path != NULL && colon > path)
244 colon = NULL;
246 if ((endhost = colon) == NULL &&
247 (endhost = path) == NULL)
248 endhost = strchr(uri, '\0');
250 if (colon != NULL) {
251 for (port = colon+1; *port && *port != '/'; ++port)
252 ++portlen;
253 port = colon+1;
254 } else {
255 port = "70";
256 portlen = 2;
259 if (path == NULL) {
260 type = '1';
261 path = "";
262 } else
263 path = gopher_skip_selector(path, &type);
265 return fprintf(fp, "%c%s\t%s\t%.*s\t%.*s\n", type, text,
266 path, (int)(endhost - uri), uri, (int)portlen, port);
269 static int
270 gm_serialize(struct parser *p, FILE *fp)
272 struct line *line;
273 const char *text;
274 int r;
276 TAILQ_FOREACH(line, &p->head, lines) {
277 if ((text = line->line) == NULL)
278 text = "";
280 switch (line->type) {
281 case LINE_LINK:
282 r = serialize_link(line, text, fp);
283 break;
285 case LINE_TEXT:
286 r = fprintf(fp, "i%s\t\terror.host\t1\n", text);
287 break;
289 case LINE_QUOTE:
290 r = fprintf(fp, "3%s\t\terror.host\t1\n", text);
291 break;
293 default:
294 /* unreachable */
295 abort();
298 if (r == -1)
299 return 0;
302 return 1;