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.
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 *);
42 gophermap_initparser(struct parser *p)
44 memset(p, 0, sizeof(*p));
46 p->name = "gophermap";
49 p->serialize = &gm_serialize;
55 gm_parse_selector(char *line, struct gm_selector *s)
63 if ((line = strchr(line, '\t')) == NULL)
68 if ((line = strchr(line, '\t')) == NULL)
73 if ((line = strchr(line, '\t')) == NULL)
80 gm_parse(struct parser *p, const char *buf, size_t size)
82 return parser_foreach_line(p, buf, size, gm_foreach_line);
86 emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
89 char buf[LINE_MAX], b[2] = {0};
91 if ((l = calloc(1, sizeof(*l))) == NULL)
94 if ((l->line = strdup(s->ds)) == NULL)
97 switch (l->type = type) {
99 if (s->type == 'h' && !strncmp(s->selector, "URL:", 4)) {
100 strlcpy(buf, s->selector+4, sizeof(buf));
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));
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)
122 TAILQ_INSERT_TAIL(&p->head, l, lines);
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);
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 */
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))
167 case 'i': /* non-canonical: message */
168 if (!emit_line(p, LINE_TEXT, &s))
172 case '3': /* error code */
173 if (!emit_line(p, LINE_QUOTE, &s))
182 gm_free(struct parser *p)
184 /* flush the buffer */
186 gm_foreach_line(p, p->buf, p->len);
193 static inline const char *
194 gopher_skip_selector(const char *path, int *ret_type)
198 if (!strcmp(path, "/") || *path == '\0') {
207 switch (*ret_type = *path) {
223 serialize_link(struct line *line, const char *text, FILE *fp)
227 const char *uri, *endhost, *port, *path, *colon;
229 if ((uri = line->alt) == NULL)
232 if (strncmp(uri, "gopher://", 9) != 0)
233 return fprintf(fp, "h%s\tURL:%s\terror.host\t1\n",
236 uri += 9; /* skip gopher:// */
238 path = strchr(uri, '/');
239 colon = strchr(uri, ':');
241 if (path != NULL && colon > path)
244 if ((endhost = colon) == NULL &&
245 (endhost = path) == NULL)
246 endhost = strchr(uri, '\0');
249 for (port = colon+1; *port && *port != '/'; ++port)
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);
268 gm_serialize(struct parser *p, FILE *fp)
274 TAILQ_FOREACH(line, &p->head, lines) {
275 if ((text = line->line) == NULL)
278 switch (line->type) {
280 r = serialize_link(line, text, fp);
284 r = fprintf(fp, "i%s\t\terror.host\t1\n", text);
288 r = fprintf(fp, "3%s\t\terror.host\t1\n", text);