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"
25 #include "utils.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(const 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 *);
66 static void do_parseconfig(const char *, int);
68 %}
70 %token ATTR
71 %token BIND BG
72 %token CONT
73 %token FG
74 %token PRFX PROXY
75 %token SET STYLE
76 %token UNBIND
77 %token VIA
79 %token <str> STRING
80 %token <num> NUMBER
82 %%
84 grammar : /* empty */
85 | grammar '\n'
86 | grammar rule '\n'
87 | error '\n'
88 ;
90 rule : set
91 | style {
92 free(current_style);
93 current_style = NULL;
94 }
95 | bind
96 | unbind
97 | proxy
98 ;
100 set : SET STRING '=' STRING { setvars($2, $4); }
101 | SET STRING '=' NUMBER { setvari($2, $4); }
104 style : STYLE STRING { current_style = $2; } stylespec ;
105 stylespec : styleopt | '{' optnl styleopts '}' ;
107 styleopts : /* empty */
108 | styleopts styleopt optnl
111 styleopt : PRFX STRING { setprfx($2, $2); }
112 | PRFX STRING STRING { setprfx($2, $3); }
113 | BG { color_type = BG; } colorspec
114 | FG { color_type = FG; } colorspec
115 | ATTR attr
118 colorspec : STRING { setcolor($1, $1, $1); free($1); }
119 | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
120 | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
123 attr : STRING { setattr($1, $1, $1); free($1); }
124 | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
125 | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
128 bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
131 unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
134 proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
137 optnl : '\n' optnl /* zero or more newlines */
138 | /* empty */
141 %%
143 void
144 yyerror(const char *fmt, ...)
146 va_list va;
148 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
149 va_start(va, fmt);
150 vfprintf(stderr, fmt, va);
151 va_end(va);
152 fprintf(stderr, "\n");
153 parse_errors++;
156 static struct keyword {
157 const char *word;
158 int token;
159 } keywords[] = {
160 { "attr", ATTR },
161 { "bg", BG },
162 { "bind", BIND },
163 { "cont", CONT },
164 { "fg", FG },
165 { "prefix", PRFX },
166 { "proxy", PROXY },
167 { "set", SET },
168 { "style", STYLE },
169 { "unbind", UNBIND },
170 { "via", VIA },
171 };
173 int
174 yylex(void)
176 char buf[1024], *ebuf, *p, *str;
177 const char *errstr;
178 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
179 size_t i;
181 p = buf;
182 ebuf = buf + sizeof(buf);
184 repeat:
185 /* skip whitespace first */
186 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
187 yylval.colno++;
189 /* check for special one-character constructions */
190 switch (c) {
191 case '\r':
192 /* silently eat up any \r */
193 goto repeat;
194 case '\n':
195 yylval.colno = 0;
196 yylval.lineno++;
197 /* fallthrough */
198 case '{':
199 case '}':
200 case '=':
201 return c;
202 case '#':
203 /* skip comments; NUL is allowed; no continuation */
204 while ((c = getc(yyfp)) != '\n')
205 if (c == EOF)
206 goto eof;
207 yylval.colno = 0;
208 yylval.lineno++;
209 return c;
210 case EOF:
211 goto eof;
214 /* parsing next word */
215 for (;; c = getc(yyfp), yylval.colno++) {
216 switch (c) {
217 case '\0':
218 yyerror("unallowed character NULL in column %d",
219 yylval.colno+1);
220 escape = 0;
221 continue;
222 case '\\':
223 escape = !escape;
224 if (escape)
225 continue;
226 break;
227 case '\r':
228 /* ignore \r here too */
229 continue;
230 case '\n':
231 if (quotes)
232 yyerror("unterminated quotes in column %d",
233 yylval.colno+1);
234 if (escape) {
235 nonkw = 1;
236 escape = 0;
237 yylval.colno = 0;
238 yylval.lineno++;
240 goto eow;
241 case EOF:
242 if (escape)
243 yyerror("unterminated escape in column %d",
244 yylval.colno);
245 if (quotes)
246 yyerror("unterminated quotes in column %d",
247 qpos + 1);
248 goto eow;
249 case '{':
250 case '}':
251 case '=':
252 case '#':
253 case ' ':
254 case '\t':
255 if (!escape && !quotes)
256 goto eow;
257 break;
258 case '"':
259 if (!escape) {
260 quotes = !quotes;
261 if (quotes) {
262 nonkw = 1;
263 qpos = yylval.colno;
265 continue;
268 *p++ = c;
269 if (p == ebuf) {
270 yyerror("line too long");
271 p = buf;
273 escape = 0;
276 eow:
277 *p = 0;
278 if (c != EOF)
279 ungetc(c, yyfp);
280 if (p == buf) {
281 /*
282 * There could be a number of reason for empty buffer,
283 * and we handle all of them here, to avoid cluttering
284 * the main loop.
285 */
286 if (c == EOF)
287 goto eof;
288 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
289 goto repeat;
291 if (!nonkw) {
292 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
293 if (strcmp(buf, keywords[i].word) == 0)
294 return keywords[i].token;
297 c = *buf;
298 if (!nonkw && (c == '-' || isdigit(c))) {
299 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
300 if (errstr != NULL)
301 yyerror("number is %s: %s", errstr, buf);
302 return NUMBER;
304 if ((str = strdup(buf)) == NULL)
305 err(1, "%s", __func__);
306 yylval.str = str;
307 return STRING;
309 eof:
310 if (ferror(yyfp))
311 yyerror("input error reading config");
312 return 0;
315 static void
316 setprfx(const char *prfx, const char *cont)
318 assert(current_style != NULL);
320 if (!config_setprfx(current_style, prfx, cont))
321 yyerror("invalid style %s", current_style);
324 static void
325 setvari(char *var, int val)
327 if (!config_setvari(var, val))
328 yyerror("invalid variable or value: %s = %d",
329 var, val);
331 free(var);
334 static void
335 setvars(char *var, char *val)
337 if (!config_setvars(var, val))
338 yyerror("invalid variable or value: %s = \"%s\"",
339 var, val);
341 free(var);
344 static int
345 colorname(const char *name)
347 struct {
348 const char *name;
349 short val;
350 } *i, colors[] = {
351 { "default", -1 },
352 { "black", COLOR_BLACK },
353 { "red", COLOR_RED },
354 { "green", COLOR_GREEN },
355 { "yellow", COLOR_YELLOW },
356 { "blue", COLOR_BLUE },
357 { "magenta", COLOR_MAGENTA },
358 { "cyan", COLOR_CYAN },
359 { "white", COLOR_WHITE },
360 { NULL, 0 },
361 };
362 const char *errstr;
363 int n;
365 if (!strncmp(name, "colo", 4)) {
366 /* people are strange */
367 if (!strncmp(name, "color", 5))
368 name += 5;
369 else if (!strncmp(name, "colour", 6))
370 name += 6;
371 else
372 goto err;
374 n = strtonum(name, 0, 256, &errstr);
375 if (errstr != NULL)
376 yyerror("color number is %s: %s", errstr, name);
377 return n;
380 for (i = colors; i->name != NULL; ++i) {
381 if (!strcmp(i->name, name))
382 return i->val;
385 err:
386 yyerror("unknown color name \"%s\"", name);
387 return -1;
390 void
391 setcolor(const char *prfx, const char *line, const char *trail)
393 int p, l, t;
395 assert(current_style != NULL);
397 p = colorname(prfx);
398 l = colorname(line);
399 t = colorname(trail);
401 if (!config_setcolor(color_type == BG, current_style, p, l, t))
402 yyerror("invalid style %s", current_style);
405 static int
406 attrname(const char *n)
408 struct {
409 const char *name;
410 unsigned int val;
411 } *i, attrs[] = {
412 { "normal", A_NORMAL },
413 { "standout", A_STANDOUT },
414 { "underline", A_UNDERLINE },
415 { "reverse", A_REVERSE },
416 { "blink", A_BLINK },
417 { "dim", A_DIM },
418 { "bold", A_BOLD },
419 { NULL, 0 },
420 };
421 int ret, found;
422 char *ap, *dup, *orig;
424 if ((dup = strdup(n)) == NULL)
425 err(1, "strdup");
427 orig = dup;
429 ret = 0;
430 while ((ap = strsep(&dup, ",")) != NULL) {
431 if (*ap == '\0')
432 continue;
434 found = 0;
435 for (i = attrs; i ->name != NULL; ++i) {
436 if (strcmp(i->name, ap))
437 continue;
438 ret |= i->val;
439 found = 1;
440 break;
443 if (!found)
444 yyerror("unknown attribute \"%s\" at col %d",
445 ap, yylval.colno+1);
448 free(orig);
449 return ret;
452 static void
453 setattr(char *prfx, char *line, char *trail)
455 int p, l, t;
457 assert(current_style != NULL);
459 p = attrname(prfx);
460 l = attrname(line);
461 t = attrname(trail);
463 if (!config_setattr(current_style, p, l, t))
464 yyerror("invalid style %s", current_style);
467 static void
468 add_proxy(char *proto, char *proxy)
470 struct proxy *p;
471 struct phos_uri uri;
473 if (!phos_parse_absolute_uri(proxy, &uri)) {
474 yyerror("can't parse URL: %s", proxy);
475 return;
478 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
479 yyerror("proxy url can't have path, query or fragments");
480 return;
483 if (strcmp(uri.scheme, "gemini")) {
484 yyerror("disallowed proxy protocol %s", uri.scheme);
485 return;
488 if ((p = calloc(1, sizeof(*p))) == NULL)
489 err(1, "calloc");
491 p->match_proto = proto;
492 p->proto = PROTO_GEMINI;
494 if ((p->host = strdup(uri.host)) == NULL)
495 err(1, "strdup");
497 if ((p->port = strdup(uri.port)) == NULL)
498 err(1, "strdup");
500 TAILQ_INSERT_HEAD(&proxies, p, proxies);
503 static interactivefn *
504 cmdname(const char *name)
506 struct cmd *cmd;
508 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
509 if (!strcmp(cmd->cmd, name))
510 return cmd->fn;
513 return NULL;
516 static void
517 bindkey(const char *map, const char *key, const char *cmd)
519 struct kmap *kmap;
520 interactivefn *fn;
522 if (!strcmp(map, "global-map"))
523 kmap = &global_map;
524 else if (!strcmp(map, "minibuffer-map"))
525 kmap = &minibuffer_map;
526 else {
527 yyerror("unknown map: %s", map);
528 return;
531 if ((fn = cmdname(cmd)) == NULL) {
532 yyerror("unknown cmd: %s", fn);
533 return;
536 if (!kmap_define_key(kmap, key, fn))
537 yyerror("failed to bind %s %s %s", map, key, cmd);
540 static void
541 do_parseconfig(const char *filename, int fonf)
543 if ((yyfp = fopen(filename, "r")) == NULL) {
544 if (fonf)
545 err(1, "%s", filename);
546 return;
549 path = filename;
550 yyparse();
551 fclose(yyfp);
552 if (parse_errors)
553 exit(1);
556 void
557 parseconfig(const char *filename, int fonf)
559 char altconf[PATH_MAX], *term;
561 /* load the given config file */
562 do_parseconfig(filename, fonf);
564 /* then try to load file-TERM */
566 if ((term = getenv("TERM")) == NULL)
567 return;
569 strlcpy(altconf, filename, sizeof(altconf));
570 strlcat(altconf, "-", sizeof(altconf));
571 strlcat(altconf, term, sizeof(altconf));
573 do_parseconfig(altconf, 0);