2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
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 *);
46 gophermap_initparser(struct parser *p)
48 memset(p, 0, sizeof(*p));
50 p->name = "gophermap";
53 p->serialize = &gm_serialize;
59 gm_parse_selector(char *line, struct gm_selector *s)
67 if ((line = strchr(line, '\t')) == NULL)
72 if ((line = strchr(line, '\t')) == NULL)
77 if ((line = strchr(line, '\t')) == NULL)
84 gm_parse(struct parser *p, const char *buf, size_t size)
86 return parser_foreach_line(p, buf, size, gm_foreach_line);
90 emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
93 char buf[LINE_MAX], b[2] = {0};
95 if ((l = calloc(1, sizeof(*l))) == NULL)
98 if ((l->line = strdup(s->ds)) == NULL)
101 switch (l->type = type) {
103 if (s->type == 'h' && !strncmp(s->selector, "URL:", 4)) {
104 strlcpy(buf, s->selector+4, sizeof(buf));
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));
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)
126 TAILQ_INSERT_TAIL(&p->head, l, lines);
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);
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 */
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))
171 case 'i': /* non-canonical: message */
172 if (!emit_line(p, LINE_TEXT, &s))
176 case '3': /* error code */
177 if (!emit_line(p, LINE_QUOTE, &s))
186 gm_free(struct parser *p)
188 /* flush the buffer */
190 gm_foreach_line(p, p->buf, p->len);
197 static inline const char *
198 gopher_skip_selector(const char *path, int *ret_type)
202 if (!strcmp(path, "/") || *path == '\0') {
211 switch (*ret_type = *path) {
227 serialize_link(struct line *line, const char *text, FILE *fp)
231 const char *uri, *endhost, *port, *path, *colon;
233 if ((uri = line->alt) == NULL)
236 if (strncmp(uri, "gopher://", 9) != 0)
237 return fprintf(fp, "h%s\tURL:%s\terror.host\t1\n",
240 uri += 9; /* skip gopher:// */
242 path = strchr(uri, '/');
243 colon = strchr(uri, ':');
245 if (path != NULL && colon > path)
248 if ((endhost = colon) == NULL &&
249 (endhost = path) == NULL)
250 endhost = strchr(uri, '\0');
253 for (port = colon+1; *port && *port != '/'; ++port)
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);
272 gm_serialize(struct parser *p, FILE *fp)
278 TAILQ_FOREACH(line, &p->head, lines) {
279 if ((text = line->line) == NULL)
282 switch (line->type) {
284 r = serialize_link(line, text, fp);
288 r = fprintf(fp, "i%s\t\terror.host\t1\n", text);
292 r = fprintf(fp, "3%s\t\terror.host\t1\n", text);