Blob


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