2 * Copyright (c) 2021, 2024 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.
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 *);
47 gophermap_initparser(struct parser *p)
49 memset(p, 0, sizeof(*p));
51 p->name = "gophermap";
54 p->serialize = &gm_serialize;
60 gm_parse_selector(char *line, struct gm_selector *s)
68 if ((line = strchr(line, '\t')) == NULL)
73 if ((line = strchr(line, '\t')) == NULL)
78 if ((line = strchr(line, '\t')) == NULL)
85 gm_parse(struct parser *p, const char *buf, size_t size)
87 return parser_foreach_line(p, buf, size, gm_foreach_line);
91 selector2uri(struct gm_selector *s, char *buf, size_t len)
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)
102 return (iri_urlescape(s->selector, buf, len));
106 emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
111 if ((l = calloc(1, sizeof(*l))) == NULL)
114 if ((l->line = strdup(s->ds)) == NULL)
117 switch (l->type = type) {
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)
124 if ((l->alt = strdup(buf)) == NULL)
132 TAILQ_INSERT_TAIL(&p->head, l, lines);
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);
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 */
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))
177 case 'i': /* non-canonical: message */
178 if (!emit_line(p, LINE_TEXT, &s))
182 case '3': /* error code */
183 if (!emit_line(p, LINE_QUOTE, &s))
192 gm_free(struct parser *p)
194 /* flush the buffer */
196 gm_foreach_line(p, p->buf, p->len);
203 static inline const char *
204 gopher_skip_selector(const char *path, int *ret_type)
208 if (!strcmp(path, "/") || *path == '\0') {
217 switch (*ret_type = *path) {
233 serialize_link(struct line *line, const char *text, FILE *fp)
237 const char *uri, *endhost, *port, *path, *colon;
239 if ((uri = line->alt) == NULL)
242 if (strncmp(uri, "gopher://", 9) != 0)
243 return fprintf(fp, "h%s\tURL:%s\terror.host\t1\n",
246 uri += 9; /* skip gopher:// */
248 path = strchr(uri, '/');
249 colon = strchr(uri, ':');
251 if (path != NULL && colon > path)
254 if ((endhost = colon) == NULL &&
255 (endhost = path) == NULL)
256 endhost = strchr(uri, '\0');
259 for (port = colon+1; *port && *port != '/'; ++port)
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);
278 gm_serialize(struct parser *p, FILE *fp)
284 TAILQ_FOREACH(line, &p->head, lines) {
285 if ((text = line->line) == NULL)
288 switch (line->type) {
290 r = serialize_link(line, text, fp);
294 r = fprintf(fp, "i%s\t\terror.host\t1\n", text);
298 r = fprintf(fp, "3%s\t\terror.host\t1\n", text);