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 "keymap.h"
24 #include "telescope.h"
26 #include <phos/phos.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <limits.h>
31 #include <ncurses.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
37 typedef struct {
38 union {
39 char *str;
40 int num;
41 };
42 int lineno;
43 int colno;
44 } yystype;
45 #define YYSTYPE yystype
47 static char *current_style;
48 static int color_type;
50 static const char *path;
52 FILE *yyfp;
54 int parse_errors = 0;
56 static void yyerror(const char *, ...);
57 static int yylex(void);
58 static void setprfx(const char *, const char *);
59 static void setvari(char *, int);
60 static void setvars(char *, char *);
61 static int colorname(const char *);
62 static void setcolor(const char *, const char *, const char *);
63 static int attrname(const char *);
64 static void setattr(char *, char *, char *);
65 static void add_proxy(char *, char *);
66 static void bindkey(const char *, const char *, const char *);
67 static void do_parseconfig(const char *, int);
69 %}
71 %token ATTR
72 %token BIND BG
73 %token CONT
74 %token FG
75 %token PRFX PROXY
76 %token SET STYLE
77 %token UNBIND
78 %token VIA
80 %token <str> STRING
81 %token <num> NUMBER
83 %%
85 grammar : /* empty */
86 | grammar '\n'
87 | grammar rule '\n'
88 | error '\n'
89 ;
91 rule : set
92 | style {
93 free(current_style);
94 current_style = NULL;
95 }
96 | bind
97 | unbind
98 | proxy
99 ;
101 set : SET STRING '=' STRING { setvars($2, $4); }
102 | SET STRING '=' NUMBER { setvari($2, $4); }
105 style : STYLE STRING { current_style = $2; } stylespec ;
106 stylespec : styleopt | '{' optnl styleopts '}' ;
108 styleopts : /* empty */
109 | styleopts styleopt optnl
112 styleopt : PRFX STRING { setprfx($2, $2); }
113 | PRFX STRING STRING { setprfx($2, $3); }
114 | BG { color_type = BG; } colorspec
115 | FG { color_type = FG; } colorspec
116 | ATTR attr
119 colorspec : STRING { setcolor($1, $1, $1); free($1); }
120 | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
121 | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
124 attr : STRING { setattr($1, $1, $1); free($1); }
125 | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
126 | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
129 bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
132 unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
135 proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
138 optnl : '\n' optnl /* zero or more newlines */
139 | /* empty */
142 %%
144 void
145 yyerror(const char *fmt, ...)
147 va_list va;
149 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
150 va_start(va, fmt);
151 vfprintf(stderr, fmt, va);
152 va_end(va);
153 fprintf(stderr, "\n");
154 parse_errors++;
157 static struct keyword {
158 const char *word;
159 int token;
160 } keywords[] = {
161 { "attr", ATTR },
162 { "bg", BG },
163 { "bind", BIND },
164 { "cont", CONT },
165 { "fg", FG },
166 { "prefix", PRFX },
167 { "proxy", PROXY },
168 { "set", SET },
169 { "style", STYLE },
170 { "unbind", UNBIND },
171 { "via", VIA },
172 };
174 int
175 yylex(void)
177 char buf[1024], *ebuf, *p, *str;
178 const char *errstr;
179 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
180 size_t i;
182 p = buf;
183 ebuf = buf + sizeof(buf);
185 repeat:
186 /* skip whitespace first */
187 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
188 yylval.colno++;
190 /* check for special one-character constructions */
191 switch (c) {
192 case '\r':
193 /* silently eat up any \r */
194 goto repeat;
195 case '\n':
196 yylval.colno = 0;
197 yylval.lineno++;
198 /* fallthrough */
199 case '{':
200 case '}':
201 case '=':
202 return c;
203 case '#':
204 /* skip comments; NUL is allowed; no continuation */
205 while ((c = getc(yyfp)) != '\n')
206 if (c == EOF)
207 goto eof;
208 yylval.colno = 0;
209 yylval.lineno++;
210 return c;
211 case EOF:
212 goto eof;
215 /* parsing next word */
216 for (;; c = getc(yyfp), yylval.colno++) {
217 switch (c) {
218 case '\0':
219 yyerror("unallowed character NULL in column %d",
220 yylval.colno+1);
221 escape = 0;
222 continue;
223 case '\\':
224 escape = !escape;
225 if (escape)
226 continue;
227 break;
228 case '\r':
229 /* ignore \r here too */
230 continue;
231 case '\n':
232 if (quotes)
233 yyerror("unterminated quotes in column %d",
234 yylval.colno+1);
235 if (escape) {
236 nonkw = 1;
237 escape = 0;
238 yylval.colno = 0;
239 yylval.lineno++;
241 goto eow;
242 case EOF:
243 if (escape)
244 yyerror("unterminated escape in column %d",
245 yylval.colno);
246 if (quotes)
247 yyerror("unterminated quotes in column %d",
248 qpos + 1);
249 goto eow;
250 case '{':
251 case '}':
252 case '=':
253 case '#':
254 case ' ':
255 case '\t':
256 if (!escape && !quotes)
257 goto eow;
258 break;
259 case '"':
260 if (!escape) {
261 quotes = !quotes;
262 if (quotes) {
263 nonkw = 1;
264 qpos = yylval.colno;
266 continue;
269 *p++ = c;
270 if (p == ebuf) {
271 yyerror("line too long");
272 p = buf;
274 escape = 0;
277 eow:
278 *p = 0;
279 if (c != EOF)
280 ungetc(c, yyfp);
281 if (p == buf) {
282 /*
283 * There could be a number of reason for empty buffer,
284 * and we handle all of them here, to avoid cluttering
285 * the main loop.
286 */
287 if (c == EOF)
288 goto eof;
289 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
290 goto repeat;
292 if (!nonkw) {
293 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
294 if (strcmp(buf, keywords[i].word) == 0)
295 return keywords[i].token;
298 c = *buf;
299 if (!nonkw && (c == '-' || isdigit(c))) {
300 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
301 if (errstr != NULL)
302 yyerror("number is %s: %s", errstr, buf);
303 return NUMBER;
305 if ((str = strdup(buf)) == NULL)
306 err(1, "%s", __func__);
307 yylval.str = str;
308 return STRING;
310 eof:
311 if (ferror(yyfp))
312 yyerror("input error reading config");
313 return 0;
316 static void
317 setprfx(const char *prfx, const char *cont)
319 assert(current_style != NULL);
321 if (!config_setprfx(current_style, prfx, cont))
322 yyerror("invalid style %s", current_style);
325 static void
326 setvari(char *var, int val)
328 if (!config_setvari(var, val))
329 yyerror("invalid variable or value: %s = %d",
330 var, val);
332 free(var);
335 static void
336 setvars(char *var, char *val)
338 if (!config_setvars(var, val))
339 yyerror("invalid variable or value: %s = \"%s\"",
340 var, val);
342 free(var);
345 static int
346 colorname(const char *name)
348 struct {
349 const char *name;
350 short val;
351 } *i, colors[] = {
352 { "default", -1 },
353 { "black", COLOR_BLACK },
354 { "red", COLOR_RED },
355 { "green", COLOR_GREEN },
356 { "yellow", COLOR_YELLOW },
357 { "blue", COLOR_BLUE },
358 { "magenta", COLOR_MAGENTA },
359 { "cyan", COLOR_CYAN },
360 { "white", COLOR_WHITE },
361 { NULL, 0 },
362 };
363 const char *errstr;
364 int n;
366 if (has_prefix(name, "colo")) {
367 /* people are strange */
368 if (has_prefix(name, "color"))
369 name += 5;
370 else if (has_prefix(name, "colour"))
371 name += 6;
372 else
373 goto err;
375 n = strtonum(name, 0, 256, &errstr);
376 if (errstr != NULL)
377 yyerror("color number is %s: %s", errstr, name);
378 return n;
381 for (i = colors; i->name != NULL; ++i) {
382 if (!strcmp(i->name, name))
383 return i->val;
386 err:
387 yyerror("unknown color name \"%s\"", name);
388 return -1;
391 void
392 setcolor(const char *prfx, const char *line, const char *trail)
394 int p, l, t;
396 assert(current_style != NULL);
398 p = colorname(prfx);
399 l = colorname(line);
400 t = colorname(trail);
402 if (!config_setcolor(color_type == BG, current_style, p, l, t))
403 yyerror("invalid style %s", current_style);
406 static int
407 attrname(const char *n)
409 struct {
410 const char *name;
411 unsigned int val;
412 } *i, attrs[] = {
413 { "normal", A_NORMAL },
414 { "standout", A_STANDOUT },
415 { "underline", A_UNDERLINE },
416 { "reverse", A_REVERSE },
417 { "blink", A_BLINK },
418 { "dim", A_DIM },
419 { "bold", A_BOLD },
420 { NULL, 0 },
421 };
422 int ret, found;
423 char *ap, *dup, *orig;
425 if ((dup = strdup(n)) == NULL)
426 err(1, "strdup");
428 orig = dup;
430 ret = 0;
431 while ((ap = strsep(&dup, ",")) != NULL) {
432 if (*ap == '\0')
433 continue;
435 found = 0;
436 for (i = attrs; i ->name != NULL; ++i) {
437 if (strcmp(i->name, ap))
438 continue;
439 ret |= i->val;
440 found = 1;
441 break;
444 if (!found)
445 yyerror("unknown attribute \"%s\" at col %d",
446 ap, yylval.colno+1);
449 free(orig);
450 return ret;
453 static void
454 setattr(char *prfx, char *line, char *trail)
456 int p, l, t;
458 assert(current_style != NULL);
460 p = attrname(prfx);
461 l = attrname(line);
462 t = attrname(trail);
464 if (!config_setattr(current_style, p, l, t))
465 yyerror("invalid style %s", current_style);
468 static void
469 add_proxy(char *proto, char *proxy)
471 struct proxy *p;
472 struct phos_uri uri;
474 if (!phos_parse_absolute_uri(proxy, &uri)) {
475 yyerror("can't parse URL: %s", proxy);
476 return;
479 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
480 yyerror("proxy url can't have path, query or fragments");
481 return;
484 if (strcmp(uri.scheme, "gemini")) {
485 yyerror("disallowed proxy protocol %s", uri.scheme);
486 return;
489 if ((p = calloc(1, sizeof(*p))) == NULL)
490 err(1, "calloc");
492 p->match_proto = proto;
493 p->proto = PROTO_GEMINI;
495 if ((p->host = strdup(uri.host)) == NULL)
496 err(1, "strdup");
498 if ((p->port = strdup(uri.port)) == NULL)
499 err(1, "strdup");
501 TAILQ_INSERT_HEAD(&proxies, p, proxies);
504 static interactivefn *
505 cmdname(const char *name)
507 struct cmd *cmd;
509 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
510 if (!strcmp(cmd->cmd, name))
511 return cmd->fn;
514 return NULL;
517 static void
518 bindkey(const char *map, const char *key, const char *cmd)
520 struct kmap *kmap;
521 interactivefn *fn;
523 if (!strcmp(map, "global-map"))
524 kmap = &global_map;
525 else if (!strcmp(map, "minibuffer-map"))
526 kmap = &minibuffer_map;
527 else {
528 yyerror("unknown map: %s", map);
529 return;
532 if ((fn = cmdname(cmd)) == NULL) {
533 yyerror("unknown cmd: %s", fn);
534 return;
537 if (!kmap_define_key(kmap, key, fn))
538 yyerror("failed to bind %s %s %s", map, key, cmd);
541 static void
542 do_parseconfig(const char *filename, int fonf)
544 if ((yyfp = fopen(filename, "r")) == NULL) {
545 if (fonf)
546 err(1, "%s", filename);
547 return;
550 path = filename;
551 yyparse();
552 fclose(yyfp);
553 if (parse_errors)
554 exit(1);
557 void
558 parseconfig(const char *filename, int fonf)
560 char altconf[PATH_MAX], *term;
562 /* load the given config file */
563 do_parseconfig(filename, fonf);
565 /* then try to load file-TERM */
567 if ((term = getenv("TERM")) == NULL)
568 return;
570 strlcpy(altconf, filename, sizeof(altconf));
571 strlcat(altconf, "-", sizeof(altconf));
572 strlcat(altconf, term, sizeof(altconf));
574 do_parseconfig(altconf, 0);