2 81839fee 2021-07-25 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 81839fee 2021-07-25 op * Permission to use, copy, modify, and distribute this software for any
5 81839fee 2021-07-25 op * purpose with or without fee is hereby granted, provided that the above
6 81839fee 2021-07-25 op * copyright notice and this permission notice appear in all copies.
8 81839fee 2021-07-25 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 81839fee 2021-07-25 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 81839fee 2021-07-25 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 81839fee 2021-07-25 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 81839fee 2021-07-25 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 81839fee 2021-07-25 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 81839fee 2021-07-25 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 81839fee 2021-07-25 op #include "compat.h"
19 81839fee 2021-07-25 op #include <stdio.h>
20 81839fee 2021-07-25 op #include <stdlib.h>
21 81839fee 2021-07-25 op #include <string.h>
23 81839fee 2021-07-25 op #include "parser.h"
25 81839fee 2021-07-25 op struct gm_selector {
27 81839fee 2021-07-25 op const char *ds;
28 81839fee 2021-07-25 op const char *selector;
29 81839fee 2021-07-25 op const char *addr;
30 81839fee 2021-07-25 op const char *port;
33 81839fee 2021-07-25 op static void gm_parse_selector(char *, struct gm_selector *);
35 81839fee 2021-07-25 op static int gm_parse(struct parser *, const char *, size_t);
36 81839fee 2021-07-25 op static int gm_foreach_line(struct parser *, const char *, size_t);
37 81839fee 2021-07-25 op static int gm_free(struct parser *);
40 81839fee 2021-07-25 op gophermap_initparser(struct parser *p)
42 81839fee 2021-07-25 op memset(p, 0, sizeof(*p));
44 81839fee 2021-07-25 op p->name = "gophermap";
45 81839fee 2021-07-25 op p->parse = &gm_parse;
46 81839fee 2021-07-25 op p->free = &gm_free;
50 81839fee 2021-07-25 op gm_parse_selector(char *line, struct gm_selector *s)
52 81839fee 2021-07-25 op s->type = *line++;
55 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
57 81839fee 2021-07-25 op *line++ = '\0';
58 81839fee 2021-07-25 op s->selector = line;
60 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
62 81839fee 2021-07-25 op *line++ = '\0';
63 81839fee 2021-07-25 op s->addr = line;
65 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
67 81839fee 2021-07-25 op *line++ = '\0';
68 81839fee 2021-07-25 op s->port = line;
72 81839fee 2021-07-25 op gm_parse(struct parser *p, const char *buf, size_t size)
74 81839fee 2021-07-25 op return parser_foreach_line(p, buf, size, gm_foreach_line);
77 81839fee 2021-07-25 op static inline int
78 81839fee 2021-07-25 op emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
80 81839fee 2021-07-25 op struct line *l;
81 81839fee 2021-07-25 op char buf[LINE_MAX], b[2] = {0};
83 81839fee 2021-07-25 op if ((l = calloc(1, sizeof(*l))) == NULL)
86 81839fee 2021-07-25 op if ((l->line = strdup(s->ds)) == NULL)
89 81839fee 2021-07-25 op switch (l->type = type) {
90 81839fee 2021-07-25 op case LINE_LINK:
91 81839fee 2021-07-25 op if (s->type == 'h' && has_prefix(s->selector, "URL:")) {
92 81839fee 2021-07-25 op strlcpy(buf, s->selector+4, sizeof(buf));
94 81839fee 2021-07-25 op strlcpy(buf, "gopher://", sizeof(buf));
95 81839fee 2021-07-25 op strlcat(buf, s->addr, sizeof(buf));
96 81839fee 2021-07-25 op strlcat(buf, ":", sizeof(buf));
97 81839fee 2021-07-25 op strlcat(buf, s->port, sizeof(buf));
98 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
99 81839fee 2021-07-25 op b[0] = s->type;
100 81839fee 2021-07-25 op strlcat(buf, b, sizeof(buf));
101 81839fee 2021-07-25 op if (*s->selector != '/')
102 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
103 81839fee 2021-07-25 op strlcat(buf, s->selector, sizeof(buf));
106 81839fee 2021-07-25 op if ((l->alt = strdup(buf)) == NULL)
114 81839fee 2021-07-25 op if (TAILQ_EMPTY(&p->head))
115 81839fee 2021-07-25 op TAILQ_INSERT_HEAD(&p->head, l, lines);
117 81839fee 2021-07-25 op TAILQ_INSERT_TAIL(&p->head, l, lines);
122 81839fee 2021-07-25 op if (l != NULL) {
123 81839fee 2021-07-25 op free(l->line);
124 81839fee 2021-07-25 op free(l->alt);
131 81839fee 2021-07-25 op gm_foreach_line(struct parser *p, const char *line, size_t linelen)
133 81839fee 2021-07-25 op char buf[LINE_MAX] = {0};
134 81839fee 2021-07-25 op struct gm_selector s = {0};
136 81839fee 2021-07-25 op memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
137 81839fee 2021-07-25 op gm_parse_selector(buf, &s);
139 81839fee 2021-07-25 op switch (s.type) {
140 81839fee 2021-07-25 op case '0': /* text file */
141 81839fee 2021-07-25 op case '1': /* gopher submenu */
142 81839fee 2021-07-25 op case '2': /* CCSO nameserver */
143 81839fee 2021-07-25 op case '4': /* binhex-encoded file */
144 81839fee 2021-07-25 op case '5': /* DOS file */
145 81839fee 2021-07-25 op case '6': /* uuencoded file */
146 81839fee 2021-07-25 op case '7': /* full-text search */
147 81839fee 2021-07-25 op case '8': /* telnet */
148 81839fee 2021-07-25 op case '9': /* binary file */
149 81839fee 2021-07-25 op case '+': /* mirror or alternate server */
150 81839fee 2021-07-25 op case 'g': /* gif */
151 81839fee 2021-07-25 op case 'I': /* image */
152 81839fee 2021-07-25 op case 'T': /* telnet 3270 */
153 81839fee 2021-07-25 op case ':': /* gopher+: bitmap image */
154 81839fee 2021-07-25 op case ';': /* gopher+: movie file */
155 81839fee 2021-07-25 op case 'd': /* non-canonical: doc */
156 81839fee 2021-07-25 op case 'h': /* non-canonical: html file */
157 81839fee 2021-07-25 op case 's': /* non-canonical: sound file */
158 81839fee 2021-07-25 op if (!emit_line(p, LINE_LINK, &s))
164 81839fee 2021-07-25 op case 'i': /* non-canonical: message */
165 81839fee 2021-07-25 op if (!emit_line(p, LINE_TEXT, &s))
169 81839fee 2021-07-25 op case '3': /* error code */
170 81839fee 2021-07-25 op if (!emit_line(p, LINE_QUOTE, &s))
179 81839fee 2021-07-25 op gm_free(struct parser *p)
181 81839fee 2021-07-25 op /* flush the buffer */
182 81839fee 2021-07-25 op if (p->len != 0)
183 81839fee 2021-07-25 op gm_foreach_line(p, p->buf, p->len);
185 81839fee 2021-07-25 op free(p->buf);