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 *);
67 %}
69 %token TSET
70 %token TSTYLE TPRFX TCONT TBG TFG TATTR
71 %token TBIND TUNBIND
72 %token TPROXY TVIA
74 %token <str> TSTRING
75 %token <num> TNUMBER
77 %%
79 grammar : /* empty */
80 | grammar '\n'
81 | grammar rule '\n'
82 | error '\n'
83 ;
85 rule : set
86 | style {
87 free(current_style);
88 current_style = NULL;
89 }
90 | bind
91 | unbind
92 | proxy
93 ;
95 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
96 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
97 ;
99 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
100 stylespec : styleopt | '{' styleopts '}' ;
102 styleopts : /* empty */
103 | styleopts '\n'
104 | styleopts styleopt '\n'
107 styleopt : TPRFX TSTRING { setprfx($2, $2); }
108 | TPRFX TSTRING TSTRING { setprfx($2, $3); }
109 | TBG { color_type = TBG; } colorspec
110 | TFG { color_type = TFG; } colorspec
111 | TATTR attr
114 colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
115 | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
116 | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
119 attr : TSTRING { setattr($1, $1, $1); free($1); }
120 | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
121 | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
124 bind : TBIND TSTRING TSTRING TSTRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
127 unbind : TUNBIND TSTRING TSTRING { yyerror("TODO: unbind %s %s", $2, $3); }
130 proxy : TPROXY TSTRING TVIA TSTRING { add_proxy($2, $4); free($4); }
133 %%
135 void
136 yyerror(const char *fmt, ...)
138 va_list va;
140 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
141 va_start(va, fmt);
142 vfprintf(stderr, fmt, va);
143 va_end(va);
144 fprintf(stderr, "\n");
145 parse_errors++;
148 static struct keyword {
149 const char *word;
150 int token;
151 } keywords[] = {
152 { "attr", TATTR },
153 { "bg", TBG },
154 { "bind", TBIND },
155 { "cont", TCONT },
156 { "fg", TFG },
157 { "prefix", TPRFX },
158 { "proxy", TPROXY },
159 { "set", TSET },
160 { "style", TSTYLE },
161 { "unbind", TUNBIND },
162 { "via", TVIA },
163 };
165 int
166 yylex(void)
168 char buf[1024], *ebuf, *p, *str;
169 const char *errstr;
170 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
171 size_t i;
173 p = buf;
174 ebuf = buf + sizeof(buf);
176 repeat:
177 /* skip whitespace first */
178 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
179 yylval.colno++;
181 /* check for special one-character constructions */
182 switch (c) {
183 case '\r':
184 /* silently eat up any \r */
185 goto repeat;
186 case '\n':
187 yylval.colno = 0;
188 yylval.lineno++;
189 /* fallthrough */
190 case '{':
191 case '}':
192 case '=':
193 return c;
194 case '#':
195 /* skip comments; NUL is allowed; no continuation */
196 while ((c = getc(yyfp)) != '\n')
197 if (c == EOF)
198 goto eof;
199 yylval.colno = 0;
200 yylval.lineno++;
201 return c;
202 case EOF:
203 goto eof;
206 /* parsing next word */
207 for (;; c = getc(yyfp), yylval.colno++) {
208 switch (c) {
209 case '\0':
210 yyerror("unallowed character NULL in column %d",
211 yylval.colno+1);
212 escape = 0;
213 continue;
214 case '\\':
215 escape = !escape;
216 if (escape)
217 continue;
218 break;
219 case '\r':
220 /* ignore \r here too */
221 continue;
222 case '\n':
223 if (quotes)
224 yyerror("unterminated quotes in column %d",
225 yylval.colno+1);
226 if (escape) {
227 nonkw = 1;
228 escape = 0;
229 yylval.colno = 0;
230 yylval.lineno++;
232 goto eow;
233 case EOF:
234 if (escape)
235 yyerror("unterminated escape in column %d",
236 yylval.colno);
237 if (quotes)
238 yyerror("unterminated quotes in column %d",
239 qpos + 1);
240 goto eow;
241 case '{':
242 case '}':
243 case '=':
244 case '#':
245 case ' ':
246 case '\t':
247 if (!escape && !quotes)
248 goto eow;
249 break;
250 case '"':
251 if (!escape) {
252 quotes = !quotes;
253 if (quotes) {
254 nonkw = 1;
255 qpos = yylval.colno;
257 continue;
260 *p++ = c;
261 if (p == ebuf) {
262 yyerror("line too long");
263 p = buf;
265 escape = 0;
268 eow:
269 *p = 0;
270 if (c != EOF)
271 ungetc(c, yyfp);
272 if (p == buf) {
273 /*
274 * There could be a number of reason for empty buffer,
275 * and we handle all of them here, to avoid cluttering
276 * the main loop.
277 */
278 if (c == EOF)
279 goto eof;
280 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
281 goto repeat;
283 if (!nonkw) {
284 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
285 if (strcmp(buf, keywords[i].word) == 0)
286 return keywords[i].token;
289 c = *buf;
290 if (!nonkw && (c == '-' || isdigit(c))) {
291 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
292 if (errstr != NULL)
293 yyerror("number is %s: %s", errstr, buf);
294 return TNUMBER;
296 if ((str = strdup(buf)) == NULL)
297 err(1, "%s", __func__);
298 yylval.str = str;
299 return TSTRING;
301 eof:
302 if (ferror(yyfp))
303 yyerror("input error reading config");
304 return 0;
307 static void
308 setprfx(const char *prfx, const char *cont)
310 assert(current_style != NULL);
312 if (!config_setprfx(current_style, prfx, cont))
313 yyerror("invalid style %s", current_style);
316 static void
317 setvari(char *var, int val)
319 if (!config_setvari(var, val))
320 yyerror("invalid variable or value: %s = %d",
321 var, val);
323 free(var);
326 static void
327 setvars(char *var, char *val)
329 if (!config_setvars(var, val))
330 yyerror("invalid variable or value: %s = \"%s\"",
331 var, val);
333 free(var);
336 static int
337 colorname(const char *name)
339 struct {
340 const char *name;
341 short val;
342 } *i, colors[] = {
343 { "default", -1 },
344 { "black", COLOR_BLACK },
345 { "red", COLOR_RED },
346 { "green", COLOR_GREEN },
347 { "yellow", COLOR_YELLOW },
348 { "blue", COLOR_BLUE },
349 { "magenta", COLOR_MAGENTA },
350 { "cyan", COLOR_CYAN },
351 { "white", COLOR_WHITE },
352 { NULL, 0 },
353 };
354 const char *errstr;
355 int n;
357 if (has_prefix(name, "colo")) {
358 /* people are strange */
359 if (has_prefix(name, "color"))
360 name += 5;
361 else if (has_prefix(name, "colour"))
362 name += 6;
363 else
364 goto err;
366 n = strtonum(name, 0, 256, &errstr);
367 if (errstr != NULL)
368 yyerror("color number is %s: %s", errstr, name);
369 return n;
372 for (i = colors; i->name != NULL; ++i) {
373 if (!strcmp(i->name, name))
374 return i->val;
377 err:
378 yyerror("unknown color name \"%s\"", name);
379 return -1;
382 void
383 setcolor(const char *prfx, const char *line, const char *trail)
385 int p, l, t;
387 assert(current_style != NULL);
389 p = colorname(prfx);
390 l = colorname(line);
391 t = colorname(trail);
393 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
394 yyerror("invalid style %s", current_style);
397 static int
398 attrname(char *n)
400 struct {
401 const char *name;
402 unsigned int val;
403 } *i, attrs[] = {
404 { "normal", A_NORMAL },
405 { "standout", A_STANDOUT },
406 { "underline", A_UNDERLINE },
407 { "reverse", A_REVERSE },
408 { "blink", A_BLINK },
409 { "dim", A_DIM },
410 { "bold", A_BOLD },
411 { NULL, 0 },
412 };
413 int ret, found;
414 char *ap;
416 ret = 0;
417 while ((ap = strsep(&n, ",")) != NULL) {
418 if (*ap == '\0')
419 continue;
421 found = 0;
422 for (i = attrs; i ->name != NULL; ++i) {
423 if (strcmp(i->name, ap))
424 continue;
425 ret |= i->val;
426 found = 1;
427 break;
430 if (!found)
431 yyerror("unknown attribute \"%s\" at col %d",
432 ap, yylval.colno+1);
435 return ret;
438 static void
439 setattr(char *prfx, char *line, char *trail)
441 int p, l, t;
443 assert(current_style != NULL);
445 p = attrname(prfx);
446 l = attrname(line);
447 t = attrname(trail);
449 if (!config_setattr(current_style, p, l, t))
450 yyerror("invalid style %s", current_style);
453 static void
454 add_proxy(char *proto, char *proxy)
456 struct proxy *p;
457 struct phos_uri uri;
459 if (!phos_parse_absolute_uri(proxy, &uri)) {
460 yyerror("can't parse URL: %s", proxy);
461 return;
464 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
465 yyerror("proxy url can't have path, query or fragments");
466 return;
469 if (strcmp(uri.scheme, "gemini")) {
470 yyerror("disallowed proxy protocol %s", uri.scheme);
471 return;
474 if ((p = calloc(1, sizeof(*p))) == NULL)
475 err(1, "calloc");
477 p->match_proto = proto;
479 if ((p->host = strdup(uri.host)) == NULL)
480 err(1, "strdup");
482 if ((p->port = strdup(uri.port)) == NULL)
483 err(1, "strdup");
485 TAILQ_INSERT_HEAD(&proxies, p, proxies);
488 static interactivefn *
489 cmdname(const char *name)
491 struct cmd *cmd;
493 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
494 if (!strcmp(cmd->cmd, name))
495 return cmd->fn;
498 return NULL;
501 static void
502 bindkey(const char *map, const char *key, const char *cmd)
504 struct kmap *kmap;
505 interactivefn *fn;
507 if (!strcmp(map, "global-map"))
508 kmap = &global_map;
509 else if (!strcmp(map, "minibuffer-map"))
510 kmap = &minibuffer_map;
511 else {
512 yyerror("unknown map: %s", map);
513 return;
516 if ((fn = cmdname(cmd)) == NULL) {
517 yyerror("unknown cmd: %s", fn);
518 return;
521 if (!kmap_define_key(kmap, key, fn))
522 yyerror("failed to bind %s %s %s", map, key, cmd);
525 void
526 parseconfig(const char *filename, int fonf)
528 if ((yyfp = fopen(filename, "r")) == NULL) {
529 if (fonf)
530 err(1, "%s", filename);
531 return;
534 path = filename;
535 yyparse();
536 fclose(yyfp);
537 if (parse_errors)
538 exit(1);