Blame


1 69bdd906 2021-06-15 op /*
2 69bdd906 2021-06-15 op * Much of the design is taken from doas (parse.y rev 1.29)
3 69bdd906 2021-06-15 op *
4 69bdd906 2021-06-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 69bdd906 2021-06-15 op * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
6 69bdd906 2021-06-15 op *
7 69bdd906 2021-06-15 op * Permission to use, copy, modify, and distribute this software for any
8 69bdd906 2021-06-15 op * purpose with or without fee is hereby granted, provided that the above
9 69bdd906 2021-06-15 op * copyright notice and this permission notice appear in all copies.
10 69bdd906 2021-06-15 op *
11 69bdd906 2021-06-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 69bdd906 2021-06-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 69bdd906 2021-06-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 69bdd906 2021-06-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 69bdd906 2021-06-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 69bdd906 2021-06-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 69bdd906 2021-06-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 69bdd906 2021-06-15 op *
19 69bdd906 2021-06-15 op */
20 69bdd906 2021-06-15 op
21 69bdd906 2021-06-15 op %{
22 69bdd906 2021-06-15 op
23 69bdd906 2021-06-15 op #include "telescope.h"
24 69bdd906 2021-06-15 op
25 984245ce 2021-06-23 op #include <phos/phos.h>
26 984245ce 2021-06-23 op
27 74a2587f 2021-06-21 op #include <assert.h>
28 69bdd906 2021-06-15 op #include <ctype.h>
29 69bdd906 2021-06-15 op #include <limits.h>
30 74a2587f 2021-06-21 op #include <ncurses.h>
31 69bdd906 2021-06-15 op #include <stdarg.h>
32 69bdd906 2021-06-15 op #include <stdio.h>
33 69bdd906 2021-06-15 op #include <stdlib.h>
34 69bdd906 2021-06-15 op #include <string.h>
35 69bdd906 2021-06-15 op
36 69bdd906 2021-06-15 op typedef struct {
37 69bdd906 2021-06-15 op union {
38 69bdd906 2021-06-15 op char *str;
39 69bdd906 2021-06-15 op int num;
40 69bdd906 2021-06-15 op };
41 69bdd906 2021-06-15 op int lineno;
42 69bdd906 2021-06-15 op int colno;
43 69bdd906 2021-06-15 op } yystype;
44 69bdd906 2021-06-15 op #define YYSTYPE yystype
45 69bdd906 2021-06-15 op
46 69e1d1b6 2021-06-15 op static char *current_style;
47 d89b86d6 2021-06-21 op static int color_type;
48 74a2587f 2021-06-21 op
49 69bdd906 2021-06-15 op static const char *path;
50 69bdd906 2021-06-15 op
51 69bdd906 2021-06-15 op FILE *yyfp;
52 69bdd906 2021-06-15 op
53 69bdd906 2021-06-15 op int parse_errors = 0;
54 69bdd906 2021-06-15 op
55 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
56 69bdd906 2021-06-15 op static int yylex(void);
57 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
58 86e7d7b4 2021-06-19 op static void setvari(char *, int);
59 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
60 74a2587f 2021-06-21 op static int colorname(const char *);
61 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
62 d0149485 2021-06-22 op static int attrname(char *);
63 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
64 984245ce 2021-06-23 op static void add_proxy(char *, char *);
65 69bdd906 2021-06-15 op
66 69bdd906 2021-06-15 op %}
67 69bdd906 2021-06-15 op
68 69bdd906 2021-06-15 op %token TSET
69 d0149485 2021-06-22 op %token TSTYLE TPRFX TCONT TBG TFG TATTR
70 69bdd906 2021-06-15 op %token TBIND TUNBIND
71 984245ce 2021-06-23 op %token TPROXY TVIA
72 69bdd906 2021-06-15 op
73 69bdd906 2021-06-15 op %token <str> TSTRING
74 69bdd906 2021-06-15 op %token <num> TNUMBER
75 69bdd906 2021-06-15 op
76 69bdd906 2021-06-15 op %%
77 69bdd906 2021-06-15 op
78 69bdd906 2021-06-15 op grammar : /* empty */
79 69bdd906 2021-06-15 op | grammar '\n'
80 69bdd906 2021-06-15 op | grammar rule '\n'
81 69bdd906 2021-06-15 op | error '\n'
82 69bdd906 2021-06-15 op ;
83 69bdd906 2021-06-15 op
84 69e1d1b6 2021-06-15 op rule : set
85 74a2587f 2021-06-21 op | style {
86 74a2587f 2021-06-21 op free(current_style);
87 74a2587f 2021-06-21 op current_style = NULL;
88 74a2587f 2021-06-21 op }
89 69e1d1b6 2021-06-15 op | bind
90 69e1d1b6 2021-06-15 op | unbind
91 984245ce 2021-06-23 op | proxy
92 69e1d1b6 2021-06-15 op ;
93 69bdd906 2021-06-15 op
94 86e7d7b4 2021-06-19 op set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
95 86e7d7b4 2021-06-19 op | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
96 69bdd906 2021-06-15 op ;
97 69bdd906 2021-06-15 op
98 74a2587f 2021-06-21 op style : TSTYLE TSTRING { current_style = $2; } stylespec ;
99 74a2587f 2021-06-21 op stylespec : styleopt | '{' styleopts '}' ;
100 69bdd906 2021-06-15 op
101 69bdd906 2021-06-15 op styleopts : /* empty */
102 69bdd906 2021-06-15 op | styleopts '\n'
103 69bdd906 2021-06-15 op | styleopts styleopt '\n'
104 69bdd906 2021-06-15 op ;
105 69bdd906 2021-06-15 op
106 d89b86d6 2021-06-21 op styleopt : TPRFX TSTRING { setprfx($2, $2); }
107 d89b86d6 2021-06-21 op | TPRFX TSTRING TSTRING { setprfx($2, $2); }
108 d89b86d6 2021-06-21 op | TBG { color_type = TBG; } colorspec
109 d89b86d6 2021-06-21 op | TFG { color_type = TFG; } colorspec
110 d0149485 2021-06-22 op | TATTR attr
111 d89b86d6 2021-06-21 op ;
112 d89b86d6 2021-06-21 op
113 d89b86d6 2021-06-21 op colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
114 d89b86d6 2021-06-21 op | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
115 d89b86d6 2021-06-21 op | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
116 69bdd906 2021-06-15 op ;
117 69bdd906 2021-06-15 op
118 d0149485 2021-06-22 op attr : TSTRING { setattr($1, $1, $1); free($1); }
119 d0149485 2021-06-22 op | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
120 d0149485 2021-06-22 op | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
121 d0149485 2021-06-22 op ;
122 d0149485 2021-06-22 op
123 69bdd906 2021-06-15 op bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
124 69bdd906 2021-06-15 op ;
125 69bdd906 2021-06-15 op
126 69bdd906 2021-06-15 op unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
127 69bdd906 2021-06-15 op ;
128 69bdd906 2021-06-15 op
129 984245ce 2021-06-23 op proxy : TPROXY TSTRING TVIA TSTRING { add_proxy($2, $4); free($4); }
130 984245ce 2021-06-23 op ;
131 984245ce 2021-06-23 op
132 69bdd906 2021-06-15 op %%
133 69bdd906 2021-06-15 op
134 69bdd906 2021-06-15 op void
135 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
136 69bdd906 2021-06-15 op {
137 69bdd906 2021-06-15 op va_list va;
138 69bdd906 2021-06-15 op
139 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
140 69bdd906 2021-06-15 op va_start(va, fmt);
141 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
142 69bdd906 2021-06-15 op va_end(va);
143 69bdd906 2021-06-15 op fprintf(stderr, "\n");
144 69bdd906 2021-06-15 op parse_errors++;
145 69bdd906 2021-06-15 op }
146 69bdd906 2021-06-15 op
147 69bdd906 2021-06-15 op static struct keyword {
148 69bdd906 2021-06-15 op const char *word;
149 69bdd906 2021-06-15 op int token;
150 69bdd906 2021-06-15 op } keywords[] = {
151 69bdd906 2021-06-15 op { "attr", TATTR },
152 984245ce 2021-06-23 op { "bg", TBG },
153 69bdd906 2021-06-15 op { "bind", TBIND },
154 984245ce 2021-06-23 op { "cont", TCONT },
155 984245ce 2021-06-23 op { "fg", TFG },
156 984245ce 2021-06-23 op { "prefix", TPRFX },
157 984245ce 2021-06-23 op { "proxy", TPROXY },
158 984245ce 2021-06-23 op { "set", TSET },
159 984245ce 2021-06-23 op { "style", TSTYLE },
160 69bdd906 2021-06-15 op { "unbind", TUNBIND },
161 984245ce 2021-06-23 op { "via", TVIA },
162 69bdd906 2021-06-15 op };
163 69bdd906 2021-06-15 op
164 69bdd906 2021-06-15 op int
165 69bdd906 2021-06-15 op yylex(void)
166 69bdd906 2021-06-15 op {
167 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
168 69bdd906 2021-06-15 op const char *errstr;
169 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
170 69bdd906 2021-06-15 op size_t i;
171 69bdd906 2021-06-15 op
172 69bdd906 2021-06-15 op p = buf;
173 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
174 69bdd906 2021-06-15 op
175 69bdd906 2021-06-15 op repeat:
176 69bdd906 2021-06-15 op /* skip whitespace first */
177 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
178 69bdd906 2021-06-15 op yylval.colno++;
179 69bdd906 2021-06-15 op
180 69bdd906 2021-06-15 op /* check for special one-character constructions */
181 69bdd906 2021-06-15 op switch (c) {
182 c86d164e 2021-06-24 op case '\r':
183 c86d164e 2021-06-24 op /* silently eat up any \r */
184 c86d164e 2021-06-24 op goto repeat;
185 69bdd906 2021-06-15 op case '\n':
186 69bdd906 2021-06-15 op yylval.colno = 0;
187 69bdd906 2021-06-15 op yylval.lineno++;
188 69bdd906 2021-06-15 op /* fallthrough */
189 69bdd906 2021-06-15 op case '{':
190 69bdd906 2021-06-15 op case '}':
191 69bdd906 2021-06-15 op case '=':
192 69bdd906 2021-06-15 op return c;
193 69bdd906 2021-06-15 op case '#':
194 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
195 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
196 69bdd906 2021-06-15 op if (c == EOF)
197 69bdd906 2021-06-15 op goto eof;
198 69bdd906 2021-06-15 op yylval.colno = 0;
199 69bdd906 2021-06-15 op yylval.lineno++;
200 69bdd906 2021-06-15 op return c;
201 69bdd906 2021-06-15 op case EOF:
202 69bdd906 2021-06-15 op goto eof;
203 69bdd906 2021-06-15 op }
204 69bdd906 2021-06-15 op
205 69bdd906 2021-06-15 op /* parsing next word */
206 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
207 69bdd906 2021-06-15 op switch (c) {
208 69bdd906 2021-06-15 op case '\0':
209 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
210 69bdd906 2021-06-15 op yylval.colno+1);
211 69bdd906 2021-06-15 op escape = 0;
212 69bdd906 2021-06-15 op continue;
213 69bdd906 2021-06-15 op case '\\':
214 69bdd906 2021-06-15 op escape = !escape;
215 69bdd906 2021-06-15 op if (escape)
216 69bdd906 2021-06-15 op continue;
217 69bdd906 2021-06-15 op break;
218 69bdd906 2021-06-15 op case '\n':
219 69bdd906 2021-06-15 op if (quotes)
220 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
221 69bdd906 2021-06-15 op yylval.colno+1);
222 69bdd906 2021-06-15 op if (escape) {
223 69bdd906 2021-06-15 op nonkw = 1;
224 69bdd906 2021-06-15 op escape = 0;
225 69bdd906 2021-06-15 op yylval.colno = 0;
226 69bdd906 2021-06-15 op yylval.lineno++;
227 69bdd906 2021-06-15 op }
228 69bdd906 2021-06-15 op goto eow;
229 69bdd906 2021-06-15 op case EOF:
230 69bdd906 2021-06-15 op if (escape)
231 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
232 69bdd906 2021-06-15 op yylval.colno);
233 69bdd906 2021-06-15 op if (quotes)
234 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
235 69bdd906 2021-06-15 op qpos + 1);
236 69bdd906 2021-06-15 op goto eow;
237 69bdd906 2021-06-15 op case '{':
238 69bdd906 2021-06-15 op case '}':
239 69bdd906 2021-06-15 op case '=':
240 69bdd906 2021-06-15 op case '#':
241 69bdd906 2021-06-15 op case ' ':
242 69bdd906 2021-06-15 op case '\t':
243 69bdd906 2021-06-15 op if (!escape && !quotes)
244 69bdd906 2021-06-15 op goto eow;
245 69bdd906 2021-06-15 op break;
246 69bdd906 2021-06-15 op case '"':
247 69bdd906 2021-06-15 op if (!escape) {
248 69bdd906 2021-06-15 op quotes = !quotes;
249 69bdd906 2021-06-15 op if (quotes) {
250 69bdd906 2021-06-15 op nonkw = 1;
251 69bdd906 2021-06-15 op qpos = yylval.colno;
252 69bdd906 2021-06-15 op }
253 69bdd906 2021-06-15 op continue;
254 69bdd906 2021-06-15 op }
255 69bdd906 2021-06-15 op }
256 69bdd906 2021-06-15 op *p++ = c;
257 69bdd906 2021-06-15 op if (p == ebuf) {
258 69bdd906 2021-06-15 op yyerror("line too long");
259 69bdd906 2021-06-15 op p = buf;
260 69bdd906 2021-06-15 op }
261 69bdd906 2021-06-15 op escape = 0;
262 69bdd906 2021-06-15 op }
263 69bdd906 2021-06-15 op
264 69bdd906 2021-06-15 op eow:
265 69bdd906 2021-06-15 op *p = 0;
266 69bdd906 2021-06-15 op if (c != EOF)
267 69bdd906 2021-06-15 op ungetc(c, yyfp);
268 69bdd906 2021-06-15 op if (p == buf) {
269 69bdd906 2021-06-15 op /*
270 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
271 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
272 69bdd906 2021-06-15 op * the main loop.
273 69bdd906 2021-06-15 op */
274 69bdd906 2021-06-15 op if (c == EOF)
275 69bdd906 2021-06-15 op goto eof;
276 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
277 69bdd906 2021-06-15 op goto repeat;
278 69bdd906 2021-06-15 op }
279 69bdd906 2021-06-15 op if (!nonkw) {
280 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
281 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
282 69bdd906 2021-06-15 op return keywords[i].token;
283 69bdd906 2021-06-15 op }
284 69bdd906 2021-06-15 op }
285 69bdd906 2021-06-15 op c = *buf;
286 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
287 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
288 69bdd906 2021-06-15 op if (errstr != NULL)
289 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
290 69bdd906 2021-06-15 op return TNUMBER;
291 69bdd906 2021-06-15 op }
292 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
293 69bdd906 2021-06-15 op err(1, "%s", __func__);
294 69bdd906 2021-06-15 op yylval.str = str;
295 69bdd906 2021-06-15 op return TSTRING;
296 69bdd906 2021-06-15 op
297 69bdd906 2021-06-15 op eof:
298 69bdd906 2021-06-15 op if (ferror(yyfp))
299 69bdd906 2021-06-15 op yyerror("input error reading config");
300 69bdd906 2021-06-15 op return 0;
301 69bdd906 2021-06-15 op }
302 69bdd906 2021-06-15 op
303 69e1d1b6 2021-06-15 op static void
304 d89b86d6 2021-06-21 op setprfx(const char *prfx, const char *cont)
305 69e1d1b6 2021-06-15 op {
306 74a2587f 2021-06-21 op assert(current_style != NULL);
307 69e1d1b6 2021-06-15 op
308 d89b86d6 2021-06-21 op if (!config_setprfx(current_style, prfx, cont))
309 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
310 86e7d7b4 2021-06-19 op }
311 86e7d7b4 2021-06-19 op
312 86e7d7b4 2021-06-19 op static void
313 86e7d7b4 2021-06-19 op setvari(char *var, int val)
314 86e7d7b4 2021-06-19 op {
315 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
316 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
317 86e7d7b4 2021-06-19 op var, val);
318 86e7d7b4 2021-06-19 op
319 86e7d7b4 2021-06-19 op free(var);
320 69e1d1b6 2021-06-15 op }
321 69e1d1b6 2021-06-15 op
322 86e7d7b4 2021-06-19 op static void
323 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
324 86e7d7b4 2021-06-19 op {
325 86e7d7b4 2021-06-19 op if (!config_setvars(var, val))
326 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
327 86e7d7b4 2021-06-19 op var, val);
328 86e7d7b4 2021-06-19 op
329 86e7d7b4 2021-06-19 op free(var);
330 74a2587f 2021-06-21 op }
331 74a2587f 2021-06-21 op
332 74a2587f 2021-06-21 op static int
333 74a2587f 2021-06-21 op colorname(const char *name)
334 74a2587f 2021-06-21 op {
335 74a2587f 2021-06-21 op struct {
336 74a2587f 2021-06-21 op const char *name;
337 74a2587f 2021-06-21 op short val;
338 74a2587f 2021-06-21 op } *i, colors[] = {
339 160abe04 2021-06-21 op { "default", -1 },
340 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
341 74a2587f 2021-06-21 op { "red", COLOR_RED },
342 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
343 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
344 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
345 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
346 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
347 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
348 74a2587f 2021-06-21 op { NULL, 0 },
349 74a2587f 2021-06-21 op };
350 27766d48 2021-06-22 op const char *errstr;
351 27766d48 2021-06-22 op int n;
352 27766d48 2021-06-22 op
353 27766d48 2021-06-22 op if (has_prefix(name, "colo")) {
354 27766d48 2021-06-22 op /* people are strange */
355 27766d48 2021-06-22 op if (has_prefix(name, "color"))
356 27766d48 2021-06-22 op name += 5;
357 27766d48 2021-06-22 op else if (has_prefix(name, "colour"))
358 27766d48 2021-06-22 op name += 6;
359 27766d48 2021-06-22 op else
360 27766d48 2021-06-22 op goto err;
361 74a2587f 2021-06-21 op
362 27766d48 2021-06-22 op n = strtonum(name, 0, 256, &errstr);
363 27766d48 2021-06-22 op if (errstr != NULL)
364 27766d48 2021-06-22 op yyerror("color number is %s: %s", errstr, name);
365 27766d48 2021-06-22 op return n;
366 27766d48 2021-06-22 op }
367 27766d48 2021-06-22 op
368 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
369 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
370 74a2587f 2021-06-21 op return i->val;
371 74a2587f 2021-06-21 op }
372 74a2587f 2021-06-21 op
373 27766d48 2021-06-22 op err:
374 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
375 74a2587f 2021-06-21 op return -1;
376 86e7d7b4 2021-06-19 op }
377 86e7d7b4 2021-06-19 op
378 69bdd906 2021-06-15 op void
379 d89b86d6 2021-06-21 op setcolor(const char *prfx, const char *line, const char *trail)
380 74a2587f 2021-06-21 op {
381 d89b86d6 2021-06-21 op int p, l, t;
382 74a2587f 2021-06-21 op
383 74a2587f 2021-06-21 op assert(current_style != NULL);
384 74a2587f 2021-06-21 op
385 160abe04 2021-06-21 op p = colorname(prfx);
386 160abe04 2021-06-21 op l = colorname(line);
387 160abe04 2021-06-21 op t = colorname(trail);
388 74a2587f 2021-06-21 op
389 d89b86d6 2021-06-21 op if (!config_setcolor(color_type == TBG, current_style, p, l, t))
390 d0149485 2021-06-22 op yyerror("invalid style %s", current_style);
391 d0149485 2021-06-22 op }
392 d0149485 2021-06-22 op
393 d0149485 2021-06-22 op static int
394 d0149485 2021-06-22 op attrname(char *n)
395 d0149485 2021-06-22 op {
396 d0149485 2021-06-22 op struct {
397 d0149485 2021-06-22 op const char *name;
398 d0149485 2021-06-22 op unsigned int val;
399 d0149485 2021-06-22 op } *i, attrs[] = {
400 d0149485 2021-06-22 op { "normal", A_NORMAL },
401 d0149485 2021-06-22 op { "standout", A_STANDOUT },
402 d0149485 2021-06-22 op { "underline", A_UNDERLINE },
403 d0149485 2021-06-22 op { "reverse", A_REVERSE },
404 d0149485 2021-06-22 op { "blink", A_BLINK },
405 d0149485 2021-06-22 op { "dim", A_DIM },
406 d0149485 2021-06-22 op { "bold", A_BOLD },
407 d0149485 2021-06-22 op { NULL, 0 },
408 d0149485 2021-06-22 op };
409 d0149485 2021-06-22 op int ret, found;
410 d0149485 2021-06-22 op char *ap;
411 d0149485 2021-06-22 op
412 d0149485 2021-06-22 op ret = 0;
413 d0149485 2021-06-22 op while ((ap = strsep(&n, ",")) != NULL) {
414 d0149485 2021-06-22 op if (*ap == '\0')
415 d0149485 2021-06-22 op continue;
416 d0149485 2021-06-22 op
417 d0149485 2021-06-22 op found = 0;
418 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
419 d0149485 2021-06-22 op if (strcmp(i->name, ap))
420 d0149485 2021-06-22 op continue;
421 d0149485 2021-06-22 op ret |= i->val;
422 d0149485 2021-06-22 op found = 1;
423 d0149485 2021-06-22 op break;
424 d0149485 2021-06-22 op }
425 d0149485 2021-06-22 op
426 d0149485 2021-06-22 op if (!found)
427 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
428 d0149485 2021-06-22 op ap, yylval.colno+1);
429 d0149485 2021-06-22 op }
430 d0149485 2021-06-22 op
431 d0149485 2021-06-22 op return ret;
432 d0149485 2021-06-22 op }
433 d0149485 2021-06-22 op
434 d0149485 2021-06-22 op static void
435 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
436 d0149485 2021-06-22 op {
437 d0149485 2021-06-22 op int p, l, t;
438 d0149485 2021-06-22 op
439 d0149485 2021-06-22 op assert(current_style != NULL);
440 d0149485 2021-06-22 op
441 d0149485 2021-06-22 op p = attrname(prfx);
442 d0149485 2021-06-22 op l = attrname(line);
443 d0149485 2021-06-22 op t = attrname(trail);
444 d0149485 2021-06-22 op
445 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
446 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
447 74a2587f 2021-06-21 op }
448 74a2587f 2021-06-21 op
449 984245ce 2021-06-23 op static void
450 984245ce 2021-06-23 op add_proxy(char *proto, char *proxy)
451 984245ce 2021-06-23 op {
452 984245ce 2021-06-23 op struct proxy *p;
453 984245ce 2021-06-23 op struct phos_uri uri;
454 984245ce 2021-06-23 op
455 984245ce 2021-06-23 op if (!phos_parse_absolute_uri(proxy, &uri)) {
456 984245ce 2021-06-23 op yyerror("can't parse URL: %s", proxy);
457 984245ce 2021-06-23 op return;
458 984245ce 2021-06-23 op }
459 984245ce 2021-06-23 op
460 984245ce 2021-06-23 op if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
461 984245ce 2021-06-23 op yyerror("proxy url can't have path, query or fragments");
462 984245ce 2021-06-23 op return;
463 984245ce 2021-06-23 op }
464 984245ce 2021-06-23 op
465 984245ce 2021-06-23 op if (strcmp(uri.scheme, "gemini")) {
466 984245ce 2021-06-23 op yyerror("disallowed proxy protocol %s", uri.scheme);
467 984245ce 2021-06-23 op return;
468 984245ce 2021-06-23 op }
469 984245ce 2021-06-23 op
470 984245ce 2021-06-23 op if ((p = calloc(1, sizeof(*p))) == NULL)
471 984245ce 2021-06-23 op err(1, "calloc");
472 984245ce 2021-06-23 op
473 984245ce 2021-06-23 op p->match_proto = proto;
474 984245ce 2021-06-23 op
475 984245ce 2021-06-23 op if ((p->host = strdup(uri.host)) == NULL)
476 984245ce 2021-06-23 op err(1, "strdup");
477 984245ce 2021-06-23 op
478 984245ce 2021-06-23 op if ((p->port = strdup(uri.port)) == NULL)
479 984245ce 2021-06-23 op err(1, "strdup");
480 984245ce 2021-06-23 op
481 984245ce 2021-06-23 op TAILQ_INSERT_HEAD(&proxies, p, proxies);
482 984245ce 2021-06-23 op }
483 984245ce 2021-06-23 op
484 74a2587f 2021-06-21 op void
485 69bdd906 2021-06-15 op parseconfig(const char *filename, int fonf)
486 69bdd906 2021-06-15 op {
487 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
488 69bdd906 2021-06-15 op if (fonf)
489 69bdd906 2021-06-15 op err(1, "%s", filename);
490 69bdd906 2021-06-15 op return;
491 69bdd906 2021-06-15 op }
492 69bdd906 2021-06-15 op
493 69bdd906 2021-06-15 op path = filename;
494 69bdd906 2021-06-15 op yyparse();
495 69bdd906 2021-06-15 op fclose(yyfp);
496 69bdd906 2021-06-15 op if (parse_errors)
497 69bdd906 2021-06-15 op exit(1);
498 69bdd906 2021-06-15 op }