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 #include "iri.h"
38 typedef struct {
39 union {
40 char *str;
41 int num;
42 };
43 int lineno;
44 int colno;
45 } yystype;
46 #define YYSTYPE yystype
48 static char *current_style;
49 static int color_type;
51 static const char *path;
53 FILE *yyfp;
55 int parse_errors = 0;
57 static void yyerror(const char *, ...);
58 static int yylex(void);
59 static void setprfx(const char *, const char *);
60 static void setvari(char *, int);
61 static void setvars(char *, char *);
62 static int colorname(const char *);
63 static void setcolor(const char *, const char *, const char *);
64 static int attrname(const char *);
65 static void setattr(char *, char *, char *);
66 static void add_proxy(char *, char *);
67 static void bindkey(const char *, const char *, const char *);
68 static void do_parseconfig(const char *, int);
70 %}
72 %token ATTR
73 %token BIND BG
74 %token CONT
75 %token FG
76 %token PRFX PROXY
77 %token SET STYLE
78 %token UNBIND
79 %token VIA
81 %token <str> STRING
82 %token <num> NUMBER
84 %%
86 grammar : /* empty */
87 | grammar '\n'
88 | grammar rule '\n'
89 | error '\n'
90 ;
92 rule : set
93 | style {
94 free(current_style);
95 current_style = NULL;
96 }
97 | bind
98 | unbind
99 | proxy
102 set : SET STRING '=' STRING { setvars($2, $4); }
103 | SET STRING '=' NUMBER { setvari($2, $4); }
106 style : STYLE STRING { current_style = $2; } stylespec ;
107 stylespec : styleopt | '{' optnl styleopts '}' ;
109 styleopts : /* empty */
110 | styleopts styleopt optnl
113 styleopt : PRFX STRING { setprfx($2, $2); }
114 | PRFX STRING STRING { setprfx($2, $3); }
115 | BG { color_type = BG; } colorspec
116 | FG { color_type = FG; } colorspec
117 | ATTR attr
120 colorspec : STRING { setcolor($1, $1, $1); free($1); }
121 | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
122 | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
125 attr : STRING { setattr($1, $1, $1); free($1); }
126 | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
127 | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
130 bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
133 unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
136 proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
139 optnl : '\n' optnl /* zero or more newlines */
140 | /* empty */
143 %%
145 void
146 yyerror(const char *fmt, ...)
148 va_list va;
150 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
151 va_start(va, fmt);
152 vfprintf(stderr, fmt, va);
153 va_end(va);
154 fprintf(stderr, "\n");
155 parse_errors++;
158 static struct keyword {
159 const char *word;
160 int token;
161 } keywords[] = {
162 { "attr", ATTR },
163 { "bg", BG },
164 { "bind", BIND },
165 { "cont", CONT },
166 { "fg", FG },
167 { "prefix", PRFX },
168 { "proxy", PROXY },
169 { "set", SET },
170 { "style", STYLE },
171 { "unbind", UNBIND },
172 { "via", VIA },
173 };
175 int
176 yylex(void)
178 char buf[1024], *ebuf, *p, *str;
179 const char *errstr;
180 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
181 size_t i;
183 p = buf;
184 ebuf = buf + sizeof(buf);
186 repeat:
187 /* skip whitespace first */
188 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
189 yylval.colno++;
191 /* check for special one-character constructions */
192 switch (c) {
193 case '\r':
194 /* silently eat up any \r */
195 goto repeat;
196 case '\n':
197 yylval.colno = 0;
198 yylval.lineno++;
199 /* fallthrough */
200 case '{':
201 case '}':
202 case '=':
203 return c;
204 case '#':
205 /* skip comments; NUL is allowed; no continuation */
206 while ((c = getc(yyfp)) != '\n')
207 if (c == EOF)
208 goto eof;
209 yylval.colno = 0;
210 yylval.lineno++;
211 return c;
212 case EOF:
213 goto eof;
216 /* parsing next word */
217 for (;; c = getc(yyfp), yylval.colno++) {
218 switch (c) {
219 case '\0':
220 yyerror("unallowed character NULL in column %d",
221 yylval.colno+1);
222 escape = 0;
223 continue;
224 case '\\':
225 escape = !escape;
226 if (escape)
227 continue;
228 break;
229 case '\r':
230 /* ignore \r here too */
231 continue;
232 case '\n':
233 if (quotes)
234 yyerror("unterminated quotes in column %d",
235 yylval.colno+1);
236 if (escape) {
237 nonkw = 1;
238 escape = 0;
239 yylval.colno = 0;
240 yylval.lineno++;
242 goto eow;
243 case EOF:
244 if (escape)
245 yyerror("unterminated escape in column %d",
246 yylval.colno);
247 if (quotes)
248 yyerror("unterminated quotes in column %d",
249 qpos + 1);
250 goto eow;
251 case '{':
252 case '}':
253 case '=':
254 case '#':
255 case ' ':
256 case '\t':
257 if (!escape && !quotes)
258 goto eow;
259 break;
260 case '"':
261 if (!escape) {
262 quotes = !quotes;
263 if (quotes) {
264 nonkw = 1;
265 qpos = yylval.colno;
267 continue;
270 *p++ = c;
271 if (p == ebuf) {
272 yyerror("line too long");
273 p = buf;
275 escape = 0;
278 eow:
279 *p = 0;
280 if (c != EOF)
281 ungetc(c, yyfp);
282 if (p == buf) {
283 /*
284 * There could be a number of reason for empty buffer,
285 * and we handle all of them here, to avoid cluttering
286 * the main loop.
287 */
288 if (c == EOF)
289 goto eof;
290 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
291 goto repeat;
293 if (!nonkw) {
294 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
295 if (strcmp(buf, keywords[i].word) == 0)
296 return keywords[i].token;
299 c = *buf;
300 if (!nonkw && (c == '-' || isdigit(c))) {
301 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
302 if (errstr != NULL)
303 yyerror("number is %s: %s", errstr, buf);
304 return NUMBER;
306 if ((str = strdup(buf)) == NULL)
307 err(1, "%s", __func__);
308 yylval.str = str;
309 return STRING;
311 eof:
312 if (ferror(yyfp))
313 yyerror("input error reading config");
314 return 0;
317 static void
318 setprfx(const char *prfx, const char *cont)
320 assert(current_style != NULL);
322 if (!config_setprfx(current_style, prfx, cont))
323 yyerror("invalid style %s", current_style);
326 static void
327 setvari(char *var, int val)
329 if (!config_setvari(var, val))
330 yyerror("invalid variable or value: %s = %d",
331 var, val);
333 free(var);
336 static void
337 setvars(char *var, char *val)
339 if (!config_setvars(var, val))
340 yyerror("invalid variable or value: %s = \"%s\"",
341 var, val);
343 free(var);
346 static int
347 colorname(const char *name)
349 struct {
350 const char *name;
351 short val;
352 } *i, colors[] = {
353 { "default", -1 },
354 { "black", COLOR_BLACK },
355 { "red", COLOR_RED },
356 { "green", COLOR_GREEN },
357 { "yellow", COLOR_YELLOW },
358 { "blue", COLOR_BLUE },
359 { "magenta", COLOR_MAGENTA },
360 { "cyan", COLOR_CYAN },
361 { "white", COLOR_WHITE },
362 { NULL, 0 },
363 };
364 const char *errstr;
365 int n;
367 if (!strncmp(name, "colo", 4)) {
368 /* people are strange */
369 if (!strncmp(name, "color", 5))
370 name += 5;
371 else if (!strncmp(name, "colour", 6))
372 name += 6;
373 else
374 goto err;
376 n = strtonum(name, 0, 256, &errstr);
377 if (errstr != NULL)
378 yyerror("color number is %s: %s", errstr, name);
379 return n;
382 for (i = colors; i->name != NULL; ++i) {
383 if (!strcmp(i->name, name))
384 return i->val;
387 err:
388 yyerror("unknown color name \"%s\"", name);
389 return -1;
392 void
393 setcolor(const char *prfx, const char *line, const char *trail)
395 int p, l, t;
397 assert(current_style != NULL);
399 p = colorname(prfx);
400 l = colorname(line);
401 t = colorname(trail);
403 if (!config_setcolor(color_type == BG, current_style, p, l, t))
404 yyerror("invalid style %s", current_style);
407 static int
408 attrname(const char *n)
410 struct {
411 const char *name;
412 unsigned int val;
413 } *i, attrs[] = {
414 { "normal", A_NORMAL },
415 { "standout", A_STANDOUT },
416 { "underline", A_UNDERLINE },
417 { "reverse", A_REVERSE },
418 { "blink", A_BLINK },
419 { "dim", A_DIM },
420 { "bold", A_BOLD },
421 { NULL, 0 },
422 };
423 int ret, found;
424 char *ap, *dup, *orig;
426 if ((dup = strdup(n)) == NULL)
427 err(1, "strdup");
429 orig = dup;
431 ret = 0;
432 while ((ap = strsep(&dup, ",")) != NULL) {
433 if (*ap == '\0')
434 continue;
436 found = 0;
437 for (i = attrs; i ->name != NULL; ++i) {
438 if (strcmp(i->name, ap))
439 continue;
440 ret |= i->val;
441 found = 1;
442 break;
445 if (!found)
446 yyerror("unknown attribute \"%s\" at col %d",
447 ap, yylval.colno+1);
450 free(orig);
451 return ret;
454 static void
455 setattr(char *prfx, char *line, char *trail)
457 int p, l, t;
459 assert(current_style != NULL);
461 p = attrname(prfx);
462 l = attrname(line);
463 t = attrname(trail);
465 if (!config_setattr(current_style, p, l, t))
466 yyerror("invalid style %s", current_style);
469 static void
470 add_proxy(char *proto, char *proxy)
472 static struct iri iri;
473 struct proxy *p;
475 if (iri_parse(NULL, proxy, &iri) == -1) {
476 yyerror("can't parse URL: %s", proxy);
477 return;
480 if ((iri.iri_flags & (IH_QUERY|IH_FRAGMENT)) ||
481 iri.iri_path[0] != '\0') {
482 yyerror("proxy url can't have path, query or fragments");
483 return;
486 if (strcmp(iri.iri_scheme, "gemini")) {
487 yyerror("disallowed proxy protocol %s", iri.iri_scheme);
488 return;
491 if ((p = calloc(1, sizeof(*p))) == NULL)
492 err(1, "calloc");
494 p->match_proto = proto;
495 p->proto = PROTO_GEMINI;
497 if ((p->host = strdup(iri.iri_host)) == NULL)
498 err(1, "strdup");
500 if ((p->port = strdup(iri.iri_portstr)) == NULL)
501 err(1, "strdup");
503 TAILQ_INSERT_HEAD(&proxies, p, proxies);
506 static interactivefn *
507 cmdname(const char *name)
509 struct cmd *cmd;
511 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
512 if (!strcmp(cmd->cmd, name))
513 return cmd->fn;
516 return NULL;
519 static void
520 bindkey(const char *map, const char *key, const char *cmd)
522 struct kmap *kmap;
523 interactivefn *fn;
525 if (!strcmp(map, "global-map"))
526 kmap = &global_map;
527 else if (!strcmp(map, "minibuffer-map"))
528 kmap = &minibuffer_map;
529 else {
530 yyerror("unknown map: %s", map);
531 return;
534 if ((fn = cmdname(cmd)) == NULL) {
535 yyerror("unknown cmd: %s", fn);
536 return;
539 if (!kmap_define_key(kmap, key, fn))
540 yyerror("failed to bind %s %s %s", map, key, cmd);
543 static void
544 do_parseconfig(const char *filename, int fonf)
546 if ((yyfp = fopen(filename, "r")) == NULL) {
547 if (fonf)
548 err(1, "%s", filename);
549 return;
552 path = filename;
553 yyparse();
554 fclose(yyfp);
555 if (parse_errors)
556 exit(1);
559 void
560 parseconfig(const char *filename, int fonf)
562 char altconf[PATH_MAX], *term;
564 /* load the given config file */
565 do_parseconfig(filename, fonf);
567 /* then try to load file-TERM */
569 if ((term = getenv("TERM")) == NULL)
570 return;
572 strlcpy(altconf, filename, sizeof(altconf));
573 strlcat(altconf, "-", sizeof(altconf));
574 strlcat(altconf, term, sizeof(altconf));
576 do_parseconfig(altconf, 0);