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 *);
65 static void bindkey(const char *, const char *, const char *);
67 %}
69 %token TSET
70 %token TSTYLE TPRFX TCONT TBG TFG TATTR
71 %token TBIND TUNBIND
72 %token TPROXY TVIA
74 %token <str> TSTRING
75 %token <num> TNUMBER
77 %%
79 grammar : /* empty */
80 | grammar '\n'
81 | grammar rule '\n'
82 | error '\n'
83 ;
85 rule : set
86 | style {
87 free(current_style);
88 current_style = NULL;
89 }
90 | bind
91 | unbind
92 | proxy
93 ;
95 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
96 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
97 ;
99 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
100 stylespec : styleopt | '{' styleopts '}' ;
102 styleopts : /* empty */
103 | styleopts optnl
104 | styleopts styleopt optnl
107 styleopt : TPRFX TSTRING { setprfx($2, $2); }
108 | TPRFX TSTRING TSTRING { setprfx($2, $3); }
109 | TBG { color_type = TBG; } colorspec
110 | TFG { color_type = TFG; } colorspec
111 | TATTR attr
114 colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
115 | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
116 | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
119 attr : TSTRING { setattr($1, $1, $1); free($1); }
120 | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
121 | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
124 bind : TBIND TSTRING TSTRING TSTRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
127 unbind : TUNBIND TSTRING TSTRING { yyerror("TODO: unbind %s %s", $2, $3); }
130 proxy : TPROXY TSTRING TVIA TSTRING { add_proxy($2, $4); free($4); }
133 optnl : '\n' optnl /* zero or more newlines */
134 | /* empty */
137 %%
139 void
140 yyerror(const char *fmt, ...)
142 va_list va;
144 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
145 va_start(va, fmt);
146 vfprintf(stderr, fmt, va);
147 va_end(va);
148 fprintf(stderr, "\n");
149 parse_errors++;
152 static struct keyword {
153 const char *word;
154 int token;
155 } keywords[] = {
156 { "attr", TATTR },
157 { "bg", TBG },
158 { "bind", TBIND },
159 { "cont", TCONT },
160 { "fg", TFG },
161 { "prefix", TPRFX },
162 { "proxy", TPROXY },
163 { "set", TSET },
164 { "style", TSTYLE },
165 { "unbind", TUNBIND },
166 { "via", TVIA },
167 };
169 int
170 yylex(void)
172 char buf[1024], *ebuf, *p, *str;
173 const char *errstr;
174 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
175 size_t i;
177 p = buf;
178 ebuf = buf + sizeof(buf);
180 repeat:
181 /* skip whitespace first */
182 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
183 yylval.colno++;
185 /* check for special one-character constructions */
186 switch (c) {
187 case '\r':
188 /* silently eat up any \r */
189 goto repeat;
190 case '\n':
191 yylval.colno = 0;
192 yylval.lineno++;
193 /* fallthrough */
194 case '{':
195 case '}':
196 case '=':
197 return c;
198 case '#':
199 /* skip comments; NUL is allowed; no continuation */
200 while ((c = getc(yyfp)) != '\n')
201 if (c == EOF)
202 goto eof;
203 yylval.colno = 0;
204 yylval.lineno++;
205 return c;
206 case EOF:
207 goto eof;
210 /* parsing next word */
211 for (;; c = getc(yyfp), yylval.colno++) {
212 switch (c) {
213 case '\0':
214 yyerror("unallowed character NULL in column %d",
215 yylval.colno+1);
216 escape = 0;
217 continue;
218 case '\\':
219 escape = !escape;
220 if (escape)
221 continue;
222 break;
223 case '\r':
224 /* ignore \r here too */
225 continue;
226 case '\n':
227 if (quotes)
228 yyerror("unterminated quotes in column %d",
229 yylval.colno+1);
230 if (escape) {
231 nonkw = 1;
232 escape = 0;
233 yylval.colno = 0;
234 yylval.lineno++;
236 goto eow;
237 case EOF:
238 if (escape)
239 yyerror("unterminated escape in column %d",
240 yylval.colno);
241 if (quotes)
242 yyerror("unterminated quotes in column %d",
243 qpos + 1);
244 goto eow;
245 case '{':
246 case '}':
247 case '=':
248 case '#':
249 case ' ':
250 case '\t':
251 if (!escape && !quotes)
252 goto eow;
253 break;
254 case '"':
255 if (!escape) {
256 quotes = !quotes;
257 if (quotes) {
258 nonkw = 1;
259 qpos = yylval.colno;
261 continue;
264 *p++ = c;
265 if (p == ebuf) {
266 yyerror("line too long");
267 p = buf;
269 escape = 0;
272 eow:
273 *p = 0;
274 if (c != EOF)
275 ungetc(c, yyfp);
276 if (p == buf) {
277 /*
278 * There could be a number of reason for empty buffer,
279 * and we handle all of them here, to avoid cluttering
280 * the main loop.
281 */
282 if (c == EOF)
283 goto eof;
284 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
285 goto repeat;
287 if (!nonkw) {
288 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
289 if (strcmp(buf, keywords[i].word) == 0)
290 return keywords[i].token;
293 c = *buf;
294 if (!nonkw && (c == '-' || isdigit(c))) {
295 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
296 if (errstr != NULL)
297 yyerror("number is %s: %s", errstr, buf);
298 return TNUMBER;
300 if ((str = strdup(buf)) == NULL)
301 err(1, "%s", __func__);
302 yylval.str = str;
303 return TSTRING;
305 eof:
306 if (ferror(yyfp))
307 yyerror("input error reading config");
308 return 0;
311 static void
312 setprfx(const char *prfx, const char *cont)
314 assert(current_style != NULL);
316 if (!config_setprfx(current_style, prfx, cont))
317 yyerror("invalid style %s", current_style);
320 static void
321 setvari(char *var, int val)
323 if (!config_setvari(var, val))
324 yyerror("invalid variable or value: %s = %d",
325 var, val);
327 free(var);
330 static void
331 setvars(char *var, char *val)
333 if (!config_setvars(var, val))
334 yyerror("invalid variable or value: %s = \"%s\"",
335 var, val);
337 free(var);
340 static int
341 colorname(const char *name)
343 struct {
344 const char *name;
345 short val;
346 } *i, colors[] = {
347 { "default", -1 },
348 { "black", COLOR_BLACK },
349 { "red", COLOR_RED },
350 { "green", COLOR_GREEN },
351 { "yellow", COLOR_YELLOW },
352 { "blue", COLOR_BLUE },
353 { "magenta", COLOR_MAGENTA },
354 { "cyan", COLOR_CYAN },
355 { "white", COLOR_WHITE },
356 { NULL, 0 },
357 };
358 const char *errstr;
359 int n;
361 if (has_prefix(name, "colo")) {
362 /* people are strange */
363 if (has_prefix(name, "color"))
364 name += 5;
365 else if (has_prefix(name, "colour"))
366 name += 6;
367 else
368 goto err;
370 n = strtonum(name, 0, 256, &errstr);
371 if (errstr != NULL)
372 yyerror("color number is %s: %s", errstr, name);
373 return n;
376 for (i = colors; i->name != NULL; ++i) {
377 if (!strcmp(i->name, name))
378 return i->val;
381 err:
382 yyerror("unknown color name \"%s\"", name);
383 return -1;
386 void
387 setcolor(const char *prfx, const char *line, const char *trail)
389 int p, l, t;
391 assert(current_style != NULL);
393 p = colorname(prfx);
394 l = colorname(line);
395 t = colorname(trail);
397 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
398 yyerror("invalid style %s", current_style);
401 static int
402 attrname(char *n)
404 struct {
405 const char *name;
406 unsigned int val;
407 } *i, attrs[] = {
408 { "normal", A_NORMAL },
409 { "standout", A_STANDOUT },
410 { "underline", A_UNDERLINE },
411 { "reverse", A_REVERSE },
412 { "blink", A_BLINK },
413 { "dim", A_DIM },
414 { "bold", A_BOLD },
415 { NULL, 0 },
416 };
417 int ret, found;
418 char *ap;
420 ret = 0;
421 while ((ap = strsep(&n, ",")) != NULL) {
422 if (*ap == '\0')
423 continue;
425 found = 0;
426 for (i = attrs; i ->name != NULL; ++i) {
427 if (strcmp(i->name, ap))
428 continue;
429 ret |= i->val;
430 found = 1;
431 break;
434 if (!found)
435 yyerror("unknown attribute \"%s\" at col %d",
436 ap, yylval.colno+1);
439 return ret;
442 static void
443 setattr(char *prfx, char *line, char *trail)
445 int p, l, t;
447 assert(current_style != NULL);
449 p = attrname(prfx);
450 l = attrname(line);
451 t = attrname(trail);
453 if (!config_setattr(current_style, p, l, t))
454 yyerror("invalid style %s", current_style);
457 static void
458 add_proxy(char *proto, char *proxy)
460 struct proxy *p;
461 struct phos_uri uri;
463 if (!phos_parse_absolute_uri(proxy, &uri)) {
464 yyerror("can't parse URL: %s", proxy);
465 return;
468 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
469 yyerror("proxy url can't have path, query or fragments");
470 return;
473 if (strcmp(uri.scheme, "gemini")) {
474 yyerror("disallowed proxy protocol %s", uri.scheme);
475 return;
478 if ((p = calloc(1, sizeof(*p))) == NULL)
479 err(1, "calloc");
481 p->match_proto = proto;
483 if ((p->host = strdup(uri.host)) == NULL)
484 err(1, "strdup");
486 if ((p->port = strdup(uri.port)) == NULL)
487 err(1, "strdup");
489 TAILQ_INSERT_HEAD(&proxies, p, proxies);
492 static interactivefn *
493 cmdname(const char *name)
495 struct cmd *cmd;
497 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
498 if (!strcmp(cmd->cmd, name))
499 return cmd->fn;
502 return NULL;
505 static void
506 bindkey(const char *map, const char *key, const char *cmd)
508 struct kmap *kmap;
509 interactivefn *fn;
511 if (!strcmp(map, "global-map"))
512 kmap = &global_map;
513 else if (!strcmp(map, "minibuffer-map"))
514 kmap = &minibuffer_map;
515 else {
516 yyerror("unknown map: %s", map);
517 return;
520 if ((fn = cmdname(cmd)) == NULL) {
521 yyerror("unknown cmd: %s", fn);
522 return;
525 if (!kmap_define_key(kmap, key, fn))
526 yyerror("failed to bind %s %s %s", map, key, cmd);
529 void
530 parseconfig(const char *filename, int fonf)
532 if ((yyfp = fopen(filename, "r")) == NULL) {
533 if (fonf)
534 err(1, "%s", filename);
535 return;
538 path = filename;
539 yyparse();
540 fclose(yyfp);
541 if (parse_errors)
542 exit(1);