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 #ifndef LINE_MAX
27 #define LINE_MAX 2048
28 #endif
30 struct gm_selector {
31 char type;
32 const char *ds;
33 const char *selector;
34 const char *addr;
35 const char *port;
36 };
38 static void gm_parse_selector(char *, struct gm_selector *);
40 static int gm_parse(struct parser *, const char *, size_t);
41 static int gm_foreach_line(struct parser *, const char *, size_t);
42 static int gm_free(struct parser *);
43 static int gm_serialize(struct parser *, FILE *);
45 void
46 gophermap_initparser(struct parser *p)
47 {
48 memset(p, 0, sizeof(*p));
50 p->name = "gophermap";
51 p->parse = &gm_parse;
52 p->free = &gm_free;
53 p->serialize = &gm_serialize;
55 TAILQ_INIT(&p->head);
56 }
58 static void
59 gm_parse_selector(char *line, struct gm_selector *s)
60 {
61 s->type = *line++;
62 s->ds = line;
63 s->selector = "";
64 s->addr = "";
65 s->port = "";
67 if ((line = strchr(line, '\t')) == NULL)
68 return;
69 *line++ = '\0';
70 s->selector = line;
72 if ((line = strchr(line, '\t')) == NULL)
73 return;
74 *line++ = '\0';
75 s->addr = line;
77 if ((line = strchr(line, '\t')) == NULL)
78 return;
79 *line++ = '\0';
80 s->port = line;
81 }
83 static int
84 gm_parse(struct parser *p, const char *buf, size_t size)
85 {
86 return parser_foreach_line(p, buf, size, gm_foreach_line);
87 }
89 static inline int
90 emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
91 {
92 struct line *l;
93 char buf[LINE_MAX], b[2] = {0};
95 if ((l = calloc(1, sizeof(*l))) == NULL)
96 goto err;
98 if ((l->line = strdup(s->ds)) == NULL)
99 goto err;
101 switch (l->type = type) {
102 case LINE_LINK:
103 if (s->type == 'h' && !strncmp(s->selector, "URL:", 4)) {
104 strlcpy(buf, s->selector+4, sizeof(buf));
105 } else {
106 strlcpy(buf, "gopher://", sizeof(buf));
107 strlcat(buf, s->addr, sizeof(buf));
108 strlcat(buf, ":", sizeof(buf));
109 strlcat(buf, s->port, sizeof(buf));
110 strlcat(buf, "/", sizeof(buf));
111 b[0] = s->type;
112 strlcat(buf, b, sizeof(buf));
113 if (*s->selector != '/')
114 strlcat(buf, "/", sizeof(buf));
115 strlcat(buf, s->selector, sizeof(buf));
118 if ((l->alt = strdup(buf)) == NULL)
119 goto err;
120 break;
122 default:
123 break;
126 TAILQ_INSERT_TAIL(&p->head, l, lines);
128 return 1;
130 err:
131 if (l != NULL) {
132 free(l->line);
133 free(l->alt);
134 free(l);
136 return 0;
139 static int
140 gm_foreach_line(struct parser *p, const char *line, size_t linelen)
142 char buf[LINE_MAX] = {0};
143 struct gm_selector s = {0};
145 memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
146 gm_parse_selector(buf, &s);
148 switch (s.type) {
149 case '0': /* text file */
150 case '1': /* gopher submenu */
151 case '2': /* CCSO nameserver */
152 case '4': /* binhex-encoded file */
153 case '5': /* DOS file */
154 case '6': /* uuencoded file */
155 case '7': /* full-text search */
156 case '8': /* telnet */
157 case '9': /* binary file */
158 case '+': /* mirror or alternate server */
159 case 'g': /* gif */
160 case 'I': /* image */
161 case 'T': /* telnet 3270 */
162 case ':': /* gopher+: bitmap image */
163 case ';': /* gopher+: movie file */
164 case 'd': /* non-canonical: doc */
165 case 'h': /* non-canonical: html file */
166 case 's': /* non-canonical: sound file */
167 if (!emit_line(p, LINE_LINK, &s))
168 return 0;
169 break;
171 case 'i': /* non-canonical: message */
172 if (!emit_line(p, LINE_TEXT, &s))
173 return 0;
174 break;
176 case '3': /* error code */
177 if (!emit_line(p, LINE_QUOTE, &s))
178 return 0;
179 break;
182 return 1;
185 static int
186 gm_free(struct parser *p)
188 /* flush the buffer */
189 if (p->len != 0)
190 gm_foreach_line(p, p->buf, p->len);
192 free(p->buf);
194 return 1;
197 static inline const char *
198 gopher_skip_selector(const char *path, int *ret_type)
200 *ret_type = 0;
202 if (!strcmp(path, "/") || *path == '\0') {
203 *ret_type = '1';
204 return path;
207 if (*path != '/')
208 return path;
209 path++;
211 switch (*ret_type = *path) {
212 case '0':
213 case '1':
214 case '7':
215 break;
217 default:
218 *ret_type = 0;
219 path -= 1;
220 return path;
223 return ++path;
226 static int
227 serialize_link(struct line *line, const char *text, FILE *fp)
229 size_t portlen = 0;
230 int type;
231 const char *uri, *endhost, *port, *path, *colon;
233 if ((uri = line->alt) == NULL)
234 return -1;
236 if (strncmp(uri, "gopher://", 9) != 0)
237 return fprintf(fp, "h%s\tURL:%s\terror.host\t1\n",
238 text, line->alt);
240 uri += 9; /* skip gopher:// */
242 path = strchr(uri, '/');
243 colon = strchr(uri, ':');
245 if (path != NULL && colon > path)
246 colon = NULL;
248 if ((endhost = colon) == NULL &&
249 (endhost = path) == NULL)
250 endhost = strchr(uri, '\0');
252 if (colon != NULL) {
253 for (port = colon+1; *port && *port != '/'; ++port)
254 ++portlen;
255 port = colon+1;
256 } else {
257 port = "70";
258 portlen = 2;
261 if (path == NULL) {
262 type = '1';
263 path = "";
264 } else
265 path = gopher_skip_selector(path, &type);
267 return fprintf(fp, "%c%s\t%s\t%.*s\t%.*s\n", type, text,
268 path, (int)(endhost - uri), uri, (int)portlen, port);
271 static int
272 gm_serialize(struct parser *p, FILE *fp)
274 struct line *line;
275 const char *text;
276 int r;
278 TAILQ_FOREACH(line, &p->head, lines) {
279 if ((text = line->line) == NULL)
280 text = "";
282 switch (line->type) {
283 case LINE_LINK:
284 r = serialize_link(line, text, fp);
285 break;
287 case LINE_TEXT:
288 r = fprintf(fp, "i%s\t\terror.host\t1\n", text);
289 break;
291 case LINE_QUOTE:
292 r = fprintf(fp, "3%s\t\terror.host\t1\n", text);
293 break;
295 default:
296 /* unreachable */
297 abort();
300 if (r == -1)
301 return 0;
304 return 1;