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 '\r':
183 /* silently eat up any \r */
184 goto repeat;
185 case '\n':
186 yylval.colno = 0;
187 yylval.lineno++;
188 /* fallthrough */
189 case '{':
190 case '}':
191 case '=':
192 return c;
193 case '#':
194 /* skip comments; NUL is allowed; no continuation */
195 while ((c = getc(yyfp)) != '\n')
196 if (c == EOF)
197 goto eof;
198 yylval.colno = 0;
199 yylval.lineno++;
200 return c;
201 case EOF:
202 goto eof;
205 /* parsing next word */
206 for (;; c = getc(yyfp), yylval.colno++) {
207 switch (c) {
208 case '\0':
209 yyerror("unallowed character NULL in column %d",
210 yylval.colno+1);
211 escape = 0;
212 continue;
213 case '\\':
214 escape = !escape;
215 if (escape)
216 continue;
217 break;
218 case '\r':
219 /* ignore \r here too */
220 continue;
221 case '\n':
222 if (quotes)
223 yyerror("unterminated quotes in column %d",
224 yylval.colno+1);
225 if (escape) {
226 nonkw = 1;
227 escape = 0;
228 yylval.colno = 0;
229 yylval.lineno++;
231 goto eow;
232 case EOF:
233 if (escape)
234 yyerror("unterminated escape in column %d",
235 yylval.colno);
236 if (quotes)
237 yyerror("unterminated quotes in column %d",
238 qpos + 1);
239 goto eow;
240 case '{':
241 case '}':
242 case '=':
243 case '#':
244 case ' ':
245 case '\t':
246 if (!escape && !quotes)
247 goto eow;
248 break;
249 case '"':
250 if (!escape) {
251 quotes = !quotes;
252 if (quotes) {
253 nonkw = 1;
254 qpos = yylval.colno;
256 continue;
259 *p++ = c;
260 if (p == ebuf) {
261 yyerror("line too long");
262 p = buf;
264 escape = 0;
267 eow:
268 *p = 0;
269 if (c != EOF)
270 ungetc(c, yyfp);
271 if (p == buf) {
272 /*
273 * There could be a number of reason for empty buffer,
274 * and we handle all of them here, to avoid cluttering
275 * the main loop.
276 */
277 if (c == EOF)
278 goto eof;
279 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
280 goto repeat;
282 if (!nonkw) {
283 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
284 if (strcmp(buf, keywords[i].word) == 0)
285 return keywords[i].token;
288 c = *buf;
289 if (!nonkw && (c == '-' || isdigit(c))) {
290 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
291 if (errstr != NULL)
292 yyerror("number is %s: %s", errstr, buf);
293 return TNUMBER;
295 if ((str = strdup(buf)) == NULL)
296 err(1, "%s", __func__);
297 yylval.str = str;
298 return TSTRING;
300 eof:
301 if (ferror(yyfp))
302 yyerror("input error reading config");
303 return 0;
306 static void
307 setprfx(const char *prfx, const char *cont)
309 assert(current_style != NULL);
311 if (!config_setprfx(current_style, prfx, cont))
312 yyerror("invalid style %s", current_style);
315 static void
316 setvari(char *var, int val)
318 if (!config_setvari(var, val))
319 yyerror("invalid variable or value: %s = %d",
320 var, val);
322 free(var);
325 static void
326 setvars(char *var, char *val)
328 if (!config_setvars(var, val))
329 yyerror("invalid variable or value: %s = \"%s\"",
330 var, val);
332 free(var);
335 static int
336 colorname(const char *name)
338 struct {
339 const char *name;
340 short val;
341 } *i, colors[] = {
342 { "default", -1 },
343 { "black", COLOR_BLACK },
344 { "red", COLOR_RED },
345 { "green", COLOR_GREEN },
346 { "yellow", COLOR_YELLOW },
347 { "blue", COLOR_BLUE },
348 { "magenta", COLOR_MAGENTA },
349 { "cyan", COLOR_CYAN },
350 { "white", COLOR_WHITE },
351 { NULL, 0 },
352 };
353 const char *errstr;
354 int n;
356 if (has_prefix(name, "colo")) {
357 /* people are strange */
358 if (has_prefix(name, "color"))
359 name += 5;
360 else if (has_prefix(name, "colour"))
361 name += 6;
362 else
363 goto err;
365 n = strtonum(name, 0, 256, &errstr);
366 if (errstr != NULL)
367 yyerror("color number is %s: %s", errstr, name);
368 return n;
371 for (i = colors; i->name != NULL; ++i) {
372 if (!strcmp(i->name, name))
373 return i->val;
376 err:
377 yyerror("unknown color name \"%s\"", name);
378 return -1;
381 void
382 setcolor(const char *prfx, const char *line, const char *trail)
384 int p, l, t;
386 assert(current_style != NULL);
388 p = colorname(prfx);
389 l = colorname(line);
390 t = colorname(trail);
392 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
393 yyerror("invalid style %s", current_style);
396 static int
397 attrname(char *n)
399 struct {
400 const char *name;
401 unsigned int val;
402 } *i, attrs[] = {
403 { "normal", A_NORMAL },
404 { "standout", A_STANDOUT },
405 { "underline", A_UNDERLINE },
406 { "reverse", A_REVERSE },
407 { "blink", A_BLINK },
408 { "dim", A_DIM },
409 { "bold", A_BOLD },
410 { NULL, 0 },
411 };
412 int ret, found;
413 char *ap;
415 ret = 0;
416 while ((ap = strsep(&n, ",")) != NULL) {
417 if (*ap == '\0')
418 continue;
420 found = 0;
421 for (i = attrs; i ->name != NULL; ++i) {
422 if (strcmp(i->name, ap))
423 continue;
424 ret |= i->val;
425 found = 1;
426 break;
429 if (!found)
430 yyerror("unknown attribute \"%s\" at col %d",
431 ap, yylval.colno+1);
434 return ret;
437 static void
438 setattr(char *prfx, char *line, char *trail)
440 int p, l, t;
442 assert(current_style != NULL);
444 p = attrname(prfx);
445 l = attrname(line);
446 t = attrname(trail);
448 if (!config_setattr(current_style, p, l, t))
449 yyerror("invalid style %s", current_style);
452 static void
453 add_proxy(char *proto, char *proxy)
455 struct proxy *p;
456 struct phos_uri uri;
458 if (!phos_parse_absolute_uri(proxy, &uri)) {
459 yyerror("can't parse URL: %s", proxy);
460 return;
463 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
464 yyerror("proxy url can't have path, query or fragments");
465 return;
468 if (strcmp(uri.scheme, "gemini")) {
469 yyerror("disallowed proxy protocol %s", uri.scheme);
470 return;
473 if ((p = calloc(1, sizeof(*p))) == NULL)
474 err(1, "calloc");
476 p->match_proto = proto;
478 if ((p->host = strdup(uri.host)) == NULL)
479 err(1, "strdup");
481 if ((p->port = strdup(uri.port)) == NULL)
482 err(1, "strdup");
484 TAILQ_INSERT_HEAD(&proxies, p, proxies);
487 void
488 parseconfig(const char *filename, int fonf)
490 if ((yyfp = fopen(filename, "r")) == NULL) {
491 if (fonf)
492 err(1, "%s", filename);
493 return;
496 path = filename;
497 yyparse();
498 fclose(yyfp);
499 if (parse_errors)
500 exit(1);