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(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 SET
71 %token STYLE PRFX CONT BG FG ATTR
72 %token BIND UNBIND
73 %token PROXY VIA
75 %token <str> STRING
76 %token <num> NUMBER
78 %%
80 grammar : /* empty */
81 | grammar '\n'
82 | grammar rule '\n'
83 | error '\n'
84 ;
86 rule : set
87 | style {
88 free(current_style);
89 current_style = NULL;
90 }
91 | bind
92 | unbind
93 | proxy
94 ;
96 set : SET STRING '=' STRING { setvars($2, $4); }
97 | SET STRING '=' NUMBER { setvari($2, $4); }
98 ;
100 style : STYLE STRING { current_style = $2; } stylespec ;
101 stylespec : styleopt | '{' optnl styleopts '}' ;
103 styleopts : /* empty */
104 | styleopts styleopt optnl
107 styleopt : PRFX STRING { setprfx($2, $2); }
108 | PRFX STRING STRING { setprfx($2, $3); }
109 | BG { color_type = BG; } colorspec
110 | FG { color_type = FG; } colorspec
111 | ATTR attr
114 colorspec : STRING { setcolor($1, $1, $1); free($1); }
115 | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
116 | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
119 attr : STRING { setattr($1, $1, $1); free($1); }
120 | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
121 | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
124 bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
127 unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
130 proxy : PROXY STRING VIA STRING { 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", ATTR },
157 { "bg", BG },
158 { "bind", BIND },
159 { "cont", CONT },
160 { "fg", FG },
161 { "prefix", PRFX },
162 { "proxy", PROXY },
163 { "set", SET },
164 { "style", STYLE },
165 { "unbind", UNBIND },
166 { "via", VIA },
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 NUMBER;
300 if ((str = strdup(buf)) == NULL)
301 err(1, "%s", __func__);
302 yylval.str = str;
303 return STRING;
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 == BG, current_style, p, l, t))
398 yyerror("invalid style %s", current_style);
401 static int
402 attrname(const 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, *dup, *orig;
420 if ((dup = strdup(n)) == NULL)
421 err(1, "strdup");
423 orig = dup;
425 ret = 0;
426 while ((ap = strsep(&dup, ",")) != NULL) {
427 if (*ap == '\0')
428 continue;
430 found = 0;
431 for (i = attrs; i ->name != NULL; ++i) {
432 if (strcmp(i->name, ap))
433 continue;
434 ret |= i->val;
435 found = 1;
436 break;
439 if (!found)
440 yyerror("unknown attribute \"%s\" at col %d",
441 ap, yylval.colno+1);
444 free(orig);
445 return ret;
448 static void
449 setattr(char *prfx, char *line, char *trail)
451 int p, l, t;
453 assert(current_style != NULL);
455 p = attrname(prfx);
456 l = attrname(line);
457 t = attrname(trail);
459 if (!config_setattr(current_style, p, l, t))
460 yyerror("invalid style %s", current_style);
463 static void
464 add_proxy(char *proto, char *proxy)
466 struct proxy *p;
467 struct phos_uri uri;
469 if (!phos_parse_absolute_uri(proxy, &uri)) {
470 yyerror("can't parse URL: %s", proxy);
471 return;
474 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
475 yyerror("proxy url can't have path, query or fragments");
476 return;
479 if (strcmp(uri.scheme, "gemini")) {
480 yyerror("disallowed proxy protocol %s", uri.scheme);
481 return;
484 if ((p = calloc(1, sizeof(*p))) == NULL)
485 err(1, "calloc");
487 p->match_proto = proto;
488 p->proto = PROTO_GEMINI;
490 if ((p->host = strdup(uri.host)) == NULL)
491 err(1, "strdup");
493 if ((p->port = strdup(uri.port)) == NULL)
494 err(1, "strdup");
496 TAILQ_INSERT_HEAD(&proxies, p, proxies);
499 static interactivefn *
500 cmdname(const char *name)
502 struct cmd *cmd;
504 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
505 if (!strcmp(cmd->cmd, name))
506 return cmd->fn;
509 return NULL;
512 static void
513 bindkey(const char *map, const char *key, const char *cmd)
515 struct kmap *kmap;
516 interactivefn *fn;
518 if (!strcmp(map, "global-map"))
519 kmap = &global_map;
520 else if (!strcmp(map, "minibuffer-map"))
521 kmap = &minibuffer_map;
522 else {
523 yyerror("unknown map: %s", map);
524 return;
527 if ((fn = cmdname(cmd)) == NULL) {
528 yyerror("unknown cmd: %s", fn);
529 return;
532 if (!kmap_define_key(kmap, key, fn))
533 yyerror("failed to bind %s %s %s", map, key, cmd);
536 static void
537 do_parseconfig(const char *filename, int fonf)
539 if ((yyfp = fopen(filename, "r")) == NULL) {
540 if (fonf)
541 err(1, "%s", filename);
542 return;
545 path = filename;
546 yyparse();
547 fclose(yyfp);
548 if (parse_errors)
549 exit(1);
552 void
553 parseconfig(const char *filename, int fonf)
555 char altconf[PATH_MAX], *term;
557 /* load the given config file */
558 do_parseconfig(filename, fonf);
560 /* then try to load file-TERM */
562 if ((term = getenv("TERM")) == NULL)
563 return;
565 strlcpy(altconf, filename, sizeof(altconf));
566 strlcat(altconf, "-", sizeof(altconf));
567 strlcat(altconf, term, sizeof(altconf));
569 do_parseconfig(altconf, 0);