Blame


1 81839fee 2021-07-25 op /*
2 81839fee 2021-07-25 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 81839fee 2021-07-25 op *
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.
7 81839fee 2021-07-25 op *
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.
15 81839fee 2021-07-25 op */
16 81839fee 2021-07-25 op
17 81839fee 2021-07-25 op #include "compat.h"
18 81839fee 2021-07-25 op
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>
22 81839fee 2021-07-25 op
23 81839fee 2021-07-25 op #include "parser.h"
24 9d65b1d9 2022-01-11 op #include "utils.h"
25 81839fee 2021-07-25 op
26 81839fee 2021-07-25 op struct gm_selector {
27 81839fee 2021-07-25 op char type;
28 81839fee 2021-07-25 op const char *ds;
29 81839fee 2021-07-25 op const char *selector;
30 81839fee 2021-07-25 op const char *addr;
31 81839fee 2021-07-25 op const char *port;
32 81839fee 2021-07-25 op };
33 81839fee 2021-07-25 op
34 81839fee 2021-07-25 op static void gm_parse_selector(char *, struct gm_selector *);
35 81839fee 2021-07-25 op
36 81839fee 2021-07-25 op static int gm_parse(struct parser *, const char *, size_t);
37 81839fee 2021-07-25 op static int gm_foreach_line(struct parser *, const char *, size_t);
38 81839fee 2021-07-25 op static int gm_free(struct parser *);
39 81839fee 2021-07-25 op
40 81839fee 2021-07-25 op void
41 81839fee 2021-07-25 op gophermap_initparser(struct parser *p)
42 81839fee 2021-07-25 op {
43 81839fee 2021-07-25 op memset(p, 0, sizeof(*p));
44 81839fee 2021-07-25 op
45 81839fee 2021-07-25 op p->name = "gophermap";
46 81839fee 2021-07-25 op p->parse = &gm_parse;
47 81839fee 2021-07-25 op p->free = &gm_free;
48 78894e73 2021-08-12 op
49 78894e73 2021-08-12 op TAILQ_INIT(&p->head);
50 81839fee 2021-07-25 op }
51 81839fee 2021-07-25 op
52 81839fee 2021-07-25 op static void
53 81839fee 2021-07-25 op gm_parse_selector(char *line, struct gm_selector *s)
54 81839fee 2021-07-25 op {
55 81839fee 2021-07-25 op s->type = *line++;
56 81839fee 2021-07-25 op s->ds = line;
57 6c739557 2021-08-13 op s->selector = "";
58 6c739557 2021-08-13 op s->addr = "";
59 6c739557 2021-08-13 op s->port = "";
60 81839fee 2021-07-25 op
61 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
62 81839fee 2021-07-25 op return;
63 95a8c791 2021-08-26 op *line++ = '\0';
64 81839fee 2021-07-25 op s->selector = line;
65 81839fee 2021-07-25 op
66 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
67 81839fee 2021-07-25 op return;
68 81839fee 2021-07-25 op *line++ = '\0';
69 81839fee 2021-07-25 op s->addr = line;
70 81839fee 2021-07-25 op
71 81839fee 2021-07-25 op if ((line = strchr(line, '\t')) == NULL)
72 81839fee 2021-07-25 op return;
73 81839fee 2021-07-25 op *line++ = '\0';
74 81839fee 2021-07-25 op s->port = line;
75 81839fee 2021-07-25 op }
76 81839fee 2021-07-25 op
77 81839fee 2021-07-25 op static int
78 81839fee 2021-07-25 op gm_parse(struct parser *p, const char *buf, size_t size)
79 81839fee 2021-07-25 op {
80 81839fee 2021-07-25 op return parser_foreach_line(p, buf, size, gm_foreach_line);
81 81839fee 2021-07-25 op }
82 81839fee 2021-07-25 op
83 81839fee 2021-07-25 op static inline int
84 81839fee 2021-07-25 op emit_line(struct parser *p, enum line_type type, struct gm_selector *s)
85 81839fee 2021-07-25 op {
86 81839fee 2021-07-25 op struct line *l;
87 81839fee 2021-07-25 op char buf[LINE_MAX], b[2] = {0};
88 81839fee 2021-07-25 op
89 81839fee 2021-07-25 op if ((l = calloc(1, sizeof(*l))) == NULL)
90 95a8c791 2021-08-26 op goto err;
91 81839fee 2021-07-25 op
92 81839fee 2021-07-25 op if ((l->line = strdup(s->ds)) == NULL)
93 81839fee 2021-07-25 op goto err;
94 81839fee 2021-07-25 op
95 81839fee 2021-07-25 op switch (l->type = type) {
96 81839fee 2021-07-25 op case LINE_LINK:
97 81839fee 2021-07-25 op if (s->type == 'h' && has_prefix(s->selector, "URL:")) {
98 81839fee 2021-07-25 op strlcpy(buf, s->selector+4, sizeof(buf));
99 81839fee 2021-07-25 op } else {
100 81839fee 2021-07-25 op strlcpy(buf, "gopher://", sizeof(buf));
101 81839fee 2021-07-25 op strlcat(buf, s->addr, sizeof(buf));
102 81839fee 2021-07-25 op strlcat(buf, ":", sizeof(buf));
103 81839fee 2021-07-25 op strlcat(buf, s->port, sizeof(buf));
104 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
105 81839fee 2021-07-25 op b[0] = s->type;
106 81839fee 2021-07-25 op strlcat(buf, b, sizeof(buf));
107 81839fee 2021-07-25 op if (*s->selector != '/')
108 81839fee 2021-07-25 op strlcat(buf, "/", sizeof(buf));
109 81839fee 2021-07-25 op strlcat(buf, s->selector, sizeof(buf));
110 81839fee 2021-07-25 op }
111 81839fee 2021-07-25 op
112 81839fee 2021-07-25 op if ((l->alt = strdup(buf)) == NULL)
113 81839fee 2021-07-25 op goto err;
114 81839fee 2021-07-25 op break;
115 81839fee 2021-07-25 op
116 81839fee 2021-07-25 op default:
117 81839fee 2021-07-25 op break;
118 81839fee 2021-07-25 op }
119 81839fee 2021-07-25 op
120 32ac17a4 2021-08-12 op TAILQ_INSERT_TAIL(&p->head, l, lines);
121 81839fee 2021-07-25 op
122 81839fee 2021-07-25 op return 1;
123 81839fee 2021-07-25 op
124 81839fee 2021-07-25 op err:
125 81839fee 2021-07-25 op if (l != NULL) {
126 81839fee 2021-07-25 op free(l->line);
127 81839fee 2021-07-25 op free(l->alt);
128 81839fee 2021-07-25 op free(l);
129 81839fee 2021-07-25 op }
130 81839fee 2021-07-25 op return 0;
131 81839fee 2021-07-25 op }
132 81839fee 2021-07-25 op
133 81839fee 2021-07-25 op static int
134 81839fee 2021-07-25 op gm_foreach_line(struct parser *p, const char *line, size_t linelen)
135 81839fee 2021-07-25 op {
136 81839fee 2021-07-25 op char buf[LINE_MAX] = {0};
137 81839fee 2021-07-25 op struct gm_selector s = {0};
138 81839fee 2021-07-25 op
139 81839fee 2021-07-25 op memcpy(buf, line, MIN(sizeof(buf)-1, linelen));
140 81839fee 2021-07-25 op gm_parse_selector(buf, &s);
141 81839fee 2021-07-25 op
142 81839fee 2021-07-25 op switch (s.type) {
143 81839fee 2021-07-25 op case '0': /* text file */
144 81839fee 2021-07-25 op case '1': /* gopher submenu */
145 81839fee 2021-07-25 op case '2': /* CCSO nameserver */
146 81839fee 2021-07-25 op case '4': /* binhex-encoded file */
147 81839fee 2021-07-25 op case '5': /* DOS file */
148 81839fee 2021-07-25 op case '6': /* uuencoded file */
149 81839fee 2021-07-25 op case '7': /* full-text search */
150 81839fee 2021-07-25 op case '8': /* telnet */
151 81839fee 2021-07-25 op case '9': /* binary file */
152 81839fee 2021-07-25 op case '+': /* mirror or alternate server */
153 81839fee 2021-07-25 op case 'g': /* gif */
154 81839fee 2021-07-25 op case 'I': /* image */
155 81839fee 2021-07-25 op case 'T': /* telnet 3270 */
156 81839fee 2021-07-25 op case ':': /* gopher+: bitmap image */
157 81839fee 2021-07-25 op case ';': /* gopher+: movie file */
158 81839fee 2021-07-25 op case 'd': /* non-canonical: doc */
159 81839fee 2021-07-25 op case 'h': /* non-canonical: html file */
160 81839fee 2021-07-25 op case 's': /* non-canonical: sound file */
161 81839fee 2021-07-25 op if (!emit_line(p, LINE_LINK, &s))
162 81839fee 2021-07-25 op return 0;
163 81839fee 2021-07-25 op break;
164 81839fee 2021-07-25 op
165 81839fee 2021-07-25 op break;
166 81839fee 2021-07-25 op
167 81839fee 2021-07-25 op case 'i': /* non-canonical: message */
168 81839fee 2021-07-25 op if (!emit_line(p, LINE_TEXT, &s))
169 81839fee 2021-07-25 op return 0;
170 81839fee 2021-07-25 op break;
171 81839fee 2021-07-25 op
172 81839fee 2021-07-25 op case '3': /* error code */
173 81839fee 2021-07-25 op if (!emit_line(p, LINE_QUOTE, &s))
174 81839fee 2021-07-25 op return 0;
175 81839fee 2021-07-25 op break;
176 81839fee 2021-07-25 op }
177 81839fee 2021-07-25 op
178 81839fee 2021-07-25 op return 1;
179 81839fee 2021-07-25 op }
180 81839fee 2021-07-25 op
181 81839fee 2021-07-25 op static int
182 81839fee 2021-07-25 op gm_free(struct parser *p)
183 81839fee 2021-07-25 op {
184 81839fee 2021-07-25 op /* flush the buffer */
185 81839fee 2021-07-25 op if (p->len != 0)
186 81839fee 2021-07-25 op gm_foreach_line(p, p->buf, p->len);
187 81839fee 2021-07-25 op
188 81839fee 2021-07-25 op free(p->buf);
189 81839fee 2021-07-25 op
190 81839fee 2021-07-25 op return 1;
191 81839fee 2021-07-25 op }