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 *);
66 static void do_parseconfig(const char *, int);
68 %}
70 %token TSET
71 %token TSTYLE TPRFX TCONT TBG TFG TATTR
72 %token TBIND TUNBIND
73 %token TPROXY TVIA
75 %token <str> TSTRING
76 %token <num> TNUMBER
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 : TSET TSTRING '=' TSTRING { setvars($2, $4); }
97 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
98 ;
100 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
101 stylespec : styleopt | '{' styleopts '}' ;
103 styleopts : /* empty */
104 | styleopts optnl
105 | styleopts styleopt optnl
108 styleopt : TPRFX TSTRING { setprfx($2, $2); }
109 | TPRFX TSTRING TSTRING { setprfx($2, $3); }
110 | TBG { color_type = TBG; } colorspec
111 | TFG { color_type = TFG; } colorspec
112 | TATTR attr
115 colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
116 | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
117 | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
120 attr : TSTRING { setattr($1, $1, $1); free($1); }
121 | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
122 | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
125 bind : TBIND TSTRING TSTRING TSTRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
128 unbind : TUNBIND TSTRING TSTRING { yyerror("TODO: unbind %s %s", $2, $3); }
131 proxy : TPROXY TSTRING TVIA TSTRING { add_proxy($2, $4); free($4); }
134 optnl : '\n' optnl /* zero or more newlines */
135 | /* empty */
138 %%
140 void
141 yyerror(const char *fmt, ...)
143 va_list va;
145 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
146 va_start(va, fmt);
147 vfprintf(stderr, fmt, va);
148 va_end(va);
149 fprintf(stderr, "\n");
150 parse_errors++;
153 static struct keyword {
154 const char *word;
155 int token;
156 } keywords[] = {
157 { "attr", TATTR },
158 { "bg", TBG },
159 { "bind", TBIND },
160 { "cont", TCONT },
161 { "fg", TFG },
162 { "prefix", TPRFX },
163 { "proxy", TPROXY },
164 { "set", TSET },
165 { "style", TSTYLE },
166 { "unbind", TUNBIND },
167 { "via", TVIA },
168 };
170 int
171 yylex(void)
173 char buf[1024], *ebuf, *p, *str;
174 const char *errstr;
175 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
176 size_t i;
178 p = buf;
179 ebuf = buf + sizeof(buf);
181 repeat:
182 /* skip whitespace first */
183 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
184 yylval.colno++;
186 /* check for special one-character constructions */
187 switch (c) {
188 case '\r':
189 /* silently eat up any \r */
190 goto repeat;
191 case '\n':
192 yylval.colno = 0;
193 yylval.lineno++;
194 /* fallthrough */
195 case '{':
196 case '}':
197 case '=':
198 return c;
199 case '#':
200 /* skip comments; NUL is allowed; no continuation */
201 while ((c = getc(yyfp)) != '\n')
202 if (c == EOF)
203 goto eof;
204 yylval.colno = 0;
205 yylval.lineno++;
206 return c;
207 case EOF:
208 goto eof;
211 /* parsing next word */
212 for (;; c = getc(yyfp), yylval.colno++) {
213 switch (c) {
214 case '\0':
215 yyerror("unallowed character NULL in column %d",
216 yylval.colno+1);
217 escape = 0;
218 continue;
219 case '\\':
220 escape = !escape;
221 if (escape)
222 continue;
223 break;
224 case '\r':
225 /* ignore \r here too */
226 continue;
227 case '\n':
228 if (quotes)
229 yyerror("unterminated quotes in column %d",
230 yylval.colno+1);
231 if (escape) {
232 nonkw = 1;
233 escape = 0;
234 yylval.colno = 0;
235 yylval.lineno++;
237 goto eow;
238 case EOF:
239 if (escape)
240 yyerror("unterminated escape in column %d",
241 yylval.colno);
242 if (quotes)
243 yyerror("unterminated quotes in column %d",
244 qpos + 1);
245 goto eow;
246 case '{':
247 case '}':
248 case '=':
249 case '#':
250 case ' ':
251 case '\t':
252 if (!escape && !quotes)
253 goto eow;
254 break;
255 case '"':
256 if (!escape) {
257 quotes = !quotes;
258 if (quotes) {
259 nonkw = 1;
260 qpos = yylval.colno;
262 continue;
265 *p++ = c;
266 if (p == ebuf) {
267 yyerror("line too long");
268 p = buf;
270 escape = 0;
273 eow:
274 *p = 0;
275 if (c != EOF)
276 ungetc(c, yyfp);
277 if (p == buf) {
278 /*
279 * There could be a number of reason for empty buffer,
280 * and we handle all of them here, to avoid cluttering
281 * the main loop.
282 */
283 if (c == EOF)
284 goto eof;
285 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
286 goto repeat;
288 if (!nonkw) {
289 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
290 if (strcmp(buf, keywords[i].word) == 0)
291 return keywords[i].token;
294 c = *buf;
295 if (!nonkw && (c == '-' || isdigit(c))) {
296 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
297 if (errstr != NULL)
298 yyerror("number is %s: %s", errstr, buf);
299 return TNUMBER;
301 if ((str = strdup(buf)) == NULL)
302 err(1, "%s", __func__);
303 yylval.str = str;
304 return TSTRING;
306 eof:
307 if (ferror(yyfp))
308 yyerror("input error reading config");
309 return 0;
312 static void
313 setprfx(const char *prfx, const char *cont)
315 assert(current_style != NULL);
317 if (!config_setprfx(current_style, prfx, cont))
318 yyerror("invalid style %s", current_style);
321 static void
322 setvari(char *var, int val)
324 if (!config_setvari(var, val))
325 yyerror("invalid variable or value: %s = %d",
326 var, val);
328 free(var);
331 static void
332 setvars(char *var, char *val)
334 if (!config_setvars(var, val))
335 yyerror("invalid variable or value: %s = \"%s\"",
336 var, val);
338 free(var);
341 static int
342 colorname(const char *name)
344 struct {
345 const char *name;
346 short val;
347 } *i, colors[] = {
348 { "default", -1 },
349 { "black", COLOR_BLACK },
350 { "red", COLOR_RED },
351 { "green", COLOR_GREEN },
352 { "yellow", COLOR_YELLOW },
353 { "blue", COLOR_BLUE },
354 { "magenta", COLOR_MAGENTA },
355 { "cyan", COLOR_CYAN },
356 { "white", COLOR_WHITE },
357 { NULL, 0 },
358 };
359 const char *errstr;
360 int n;
362 if (has_prefix(name, "colo")) {
363 /* people are strange */
364 if (has_prefix(name, "color"))
365 name += 5;
366 else if (has_prefix(name, "colour"))
367 name += 6;
368 else
369 goto err;
371 n = strtonum(name, 0, 256, &errstr);
372 if (errstr != NULL)
373 yyerror("color number is %s: %s", errstr, name);
374 return n;
377 for (i = colors; i->name != NULL; ++i) {
378 if (!strcmp(i->name, name))
379 return i->val;
382 err:
383 yyerror("unknown color name \"%s\"", name);
384 return -1;
387 void
388 setcolor(const char *prfx, const char *line, const char *trail)
390 int p, l, t;
392 assert(current_style != NULL);
394 p = colorname(prfx);
395 l = colorname(line);
396 t = colorname(trail);
398 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
399 yyerror("invalid style %s", current_style);
402 static int
403 attrname(char *n)
405 struct {
406 const char *name;
407 unsigned int val;
408 } *i, attrs[] = {
409 { "normal", A_NORMAL },
410 { "standout", A_STANDOUT },
411 { "underline", A_UNDERLINE },
412 { "reverse", A_REVERSE },
413 { "blink", A_BLINK },
414 { "dim", A_DIM },
415 { "bold", A_BOLD },
416 { NULL, 0 },
417 };
418 int ret, found;
419 char *ap;
421 ret = 0;
422 while ((ap = strsep(&n, ",")) != NULL) {
423 if (*ap == '\0')
424 continue;
426 found = 0;
427 for (i = attrs; i ->name != NULL; ++i) {
428 if (strcmp(i->name, ap))
429 continue;
430 ret |= i->val;
431 found = 1;
432 break;
435 if (!found)
436 yyerror("unknown attribute \"%s\" at col %d",
437 ap, yylval.colno+1);
440 return ret;
443 static void
444 setattr(char *prfx, char *line, char *trail)
446 int p, l, t;
448 assert(current_style != NULL);
450 p = attrname(prfx);
451 l = attrname(line);
452 t = attrname(trail);
454 if (!config_setattr(current_style, p, l, t))
455 yyerror("invalid style %s", current_style);
458 static void
459 add_proxy(char *proto, char *proxy)
461 struct proxy *p;
462 struct phos_uri uri;
464 if (!phos_parse_absolute_uri(proxy, &uri)) {
465 yyerror("can't parse URL: %s", proxy);
466 return;
469 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
470 yyerror("proxy url can't have path, query or fragments");
471 return;
474 if (strcmp(uri.scheme, "gemini")) {
475 yyerror("disallowed proxy protocol %s", uri.scheme);
476 return;
479 if ((p = calloc(1, sizeof(*p))) == NULL)
480 err(1, "calloc");
482 p->match_proto = proto;
483 p->proto = PROTO_GEMINI;
485 if ((p->host = strdup(uri.host)) == NULL)
486 err(1, "strdup");
488 if ((p->port = strdup(uri.port)) == NULL)
489 err(1, "strdup");
491 TAILQ_INSERT_HEAD(&proxies, p, proxies);
494 static interactivefn *
495 cmdname(const char *name)
497 struct cmd *cmd;
499 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
500 if (!strcmp(cmd->cmd, name))
501 return cmd->fn;
504 return NULL;
507 static void
508 bindkey(const char *map, const char *key, const char *cmd)
510 struct kmap *kmap;
511 interactivefn *fn;
513 if (!strcmp(map, "global-map"))
514 kmap = &global_map;
515 else if (!strcmp(map, "minibuffer-map"))
516 kmap = &minibuffer_map;
517 else {
518 yyerror("unknown map: %s", map);
519 return;
522 if ((fn = cmdname(cmd)) == NULL) {
523 yyerror("unknown cmd: %s", fn);
524 return;
527 if (!kmap_define_key(kmap, key, fn))
528 yyerror("failed to bind %s %s %s", map, key, cmd);
531 static void
532 do_parseconfig(const char *filename, int fonf)
534 if ((yyfp = fopen(filename, "r")) == NULL) {
535 if (fonf)
536 err(1, "%s", filename);
537 return;
540 path = filename;
541 yyparse();
542 fclose(yyfp);
543 if (parse_errors)
544 exit(1);
547 void
548 parseconfig(const char *filename, int fonf)
550 char altconf[PATH_MAX], *term;
552 /* load the given config file */
553 do_parseconfig(filename, fonf);
555 /* then try to load file-TERM */
557 if ((term = getenv("TERM")) == NULL)
558 return;
560 strlcpy(altconf, filename, sizeof(altconf));
561 strlcat(altconf, "-", sizeof(altconf));
562 strlcat(altconf, term, sizeof(altconf));
564 do_parseconfig(altconf, 0);