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 case 'i': /* non-canonical: message */
168 if (!emit_line(p, LINE_TEXT, &s))
169 return 0;
170 break;
172 case '3': /* error code */
173 if (!emit_line(p, LINE_QUOTE, &s))
174 return 0;
175 break;
178 return 1;
181 static int
182 gm_free(struct parser *p)
184 /* flush the buffer */
185 if (p->len != 0)
186 gm_foreach_line(p, p->buf, p->len);
188 free(p->buf);
190 return 1;
193 static inline const char *
194 gopher_skip_selector(const char *path, int *ret_type)
196 *ret_type = 0;
198 if (!strcmp(path, "/") || *path == '\0') {
199 *ret_type = '1';
200 return path;
203 if (*path != '/')
204 return path;
205 path++;
207 switch (*ret_type = *path) {
208 case '0':
209 case '1':
210 case '7':
211 break;
213 default:
214 *ret_type = 0;
215 path -= 1;
216 return path;
219 return ++path;
222 static int
223 serialize_link(struct line *line, const char *text, FILE *fp)
225 size_t portlen = 0;
226 int type;
227 const char *uri, *endhost, *port, *path, *colon;
229 if ((uri = line->alt) == NULL)
230 return -1;
232 if (strncmp(uri, "gopher://", 9) != 0)
233 return fprintf(fp, "h%s\tURL:%s\terror.host\t1\n",
234 text, line->alt);
236 uri += 9; /* skip gopher:// */
238 path = strchr(uri, '/');
239 colon = strchr(uri, ':');
241 if (path != NULL && colon > path)
242 colon = NULL;
244 if ((endhost = colon) == NULL &&
245 (endhost = path) == NULL)
246 endhost = strchr(uri, '\0');
248 if (colon != NULL) {
249 for (port = colon+1; *port && *port != '/'; ++port)
250 ++portlen;
251 port = colon+1;
252 } else {
253 port = "70";
254 portlen = 2;
257 if (path == NULL) {
258 type = '1';
259 path = "";
260 } else
261 path = gopher_skip_selector(path, &type);
263 return fprintf(fp, "%c%s\t%s\t%.*s\t%.*s\n", type, text,
264 path, (int)(endhost - uri), uri, (int)portlen, port);
267 static int
268 gm_serialize(struct parser *p, FILE *fp)
270 struct line *line;
271 const char *text;
272 int r;
274 TAILQ_FOREACH(line, &p->head, lines) {
275 if ((text = line->line) == NULL)
276 text = "";
278 switch (line->type) {
279 case LINE_LINK:
280 r = serialize_link(line, text, fp);
281 break;
283 case LINE_TEXT:
284 r = fprintf(fp, "i%s\t\terror.host\t1\n", text);
285 break;
287 case LINE_QUOTE:
288 r = fprintf(fp, "3%s\t\terror.host\t1\n", text);
289 break;
291 default:
292 /* unreachable */
293 abort();
296 if (r == -1)
297 return 0;
300 return 1;