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;
48 78894e73 2021-08-12 op TAILQ_INIT(&p->head);
52 81839fee 2021-07-25 op gm_parse_selector(char *line, struct gm_selector *s)
54 81839fee 2021-07-25 op s->type = *line++;
56 6c739557 2021-08-13 op s->selector = "";
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->selector = 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->addr = line;
70 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
72 81839fee 2021-07-25 op *line++ = '\0';
73 81839fee 2021-07-25 op s->port = line;
77 81839fee 2021-07-25 op gm_parse(struct parser *p, const char *buf, size_t size)
79 81839fee 2021-07-25 op return parser_foreach_line(p, buf, size, gm_foreach_line);
82 81839fee 2021-07-25 op static inline int
83 81839fee 2021-07-25 op emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
85 81839fee 2021-07-25 op struct line *l;
86 81839fee 2021-07-25 op char buf[LINE_MAX], b[2] = {0};
88 81839fee 2021-07-25 op if ((l = calloc(1, sizeof(*l))) == NULL)
91 81839fee 2021-07-25 op if ((l->line = strdup(s->ds)) == NULL)
94 81839fee 2021-07-25 op switch (l->type = type) {
95 81839fee 2021-07-25 op case LINE_LINK:
96 81839fee 2021-07-25 op if (s->type == 'h' && has_prefix(s->selector, "URL:")) {
97 81839fee 2021-07-25 op strlcpy(buf, s->selector+4, sizeof(buf));
99 81839fee 2021-07-25 op strlcpy(buf, "gopher://", sizeof(buf));
100 81839fee 2021-07-25 op strlcat(buf, s->addr, sizeof(buf));
101 81839fee 2021-07-25 op strlcat(buf, ":", sizeof(buf));
102 81839fee 2021-07-25 op strlcat(buf, s->port, sizeof(buf));
103 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
104 81839fee 2021-07-25 op b[0] = s->type;
105 81839fee 2021-07-25 op strlcat(buf, b, sizeof(buf));
106 81839fee 2021-07-25 op if (*s->selector != '/')
107 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
108 81839fee 2021-07-25 op strlcat(buf, s->selector, sizeof(buf));
111 81839fee 2021-07-25 op if ((l->alt = strdup(buf)) == NULL)
119 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
124 81839fee 2021-07-25 op if (l != NULL) {
125 81839fee 2021-07-25 op free(l->line);
126 81839fee 2021-07-25 op free(l->alt);
133 81839fee 2021-07-25 op gm_foreach_line(struct parser *p, const char *line, size_t linelen)
135 81839fee 2021-07-25 op char buf[LINE_MAX] = {0};
136 81839fee 2021-07-25 op struct gm_selector s = {0};
138 81839fee 2021-07-25 op memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
139 81839fee 2021-07-25 op gm_parse_selector(buf, &s);
141 81839fee 2021-07-25 op switch (s.type) {
142 81839fee 2021-07-25 op case '0': /* text file */
143 81839fee 2021-07-25 op case '1': /* gopher submenu */
144 81839fee 2021-07-25 op case '2': /* CCSO nameserver */
145 81839fee 2021-07-25 op case '4': /* binhex-encoded file */
146 81839fee 2021-07-25 op case '5': /* DOS file */
147 81839fee 2021-07-25 op case '6': /* uuencoded file */
148 81839fee 2021-07-25 op case '7': /* full-text search */
149 81839fee 2021-07-25 op case '8': /* telnet */
150 81839fee 2021-07-25 op case '9': /* binary file */
151 81839fee 2021-07-25 op case '+': /* mirror or alternate server */
152 81839fee 2021-07-25 op case 'g': /* gif */
153 81839fee 2021-07-25 op case 'I': /* image */
154 81839fee 2021-07-25 op case 'T': /* telnet 3270 */
155 81839fee 2021-07-25 op case ':': /* gopher+: bitmap image */
156 81839fee 2021-07-25 op case ';': /* gopher+: movie file */
157 81839fee 2021-07-25 op case 'd': /* non-canonical: doc */
158 81839fee 2021-07-25 op case 'h': /* non-canonical: html file */
159 81839fee 2021-07-25 op case 's': /* non-canonical: sound file */
160 81839fee 2021-07-25 op if (!emit_line(p, LINE_LINK, &s))
166 81839fee 2021-07-25 op case 'i': /* non-canonical: message */
167 81839fee 2021-07-25 op if (!emit_line(p, LINE_TEXT, &s))
171 81839fee 2021-07-25 op case '3': /* error code */
172 81839fee 2021-07-25 op if (!emit_line(p, LINE_QUOTE, &s))
181 81839fee 2021-07-25 op gm_free(struct parser *p)
183 81839fee 2021-07-25 op /* flush the buffer */
184 81839fee 2021-07-25 op if (p->len != 0)
185 81839fee 2021-07-25 op gm_foreach_line(p, p->buf, p->len);
187 81839fee 2021-07-25 op free(p->buf);