2 * Much of the design is taken from doas (parse.y rev 1.29)
4 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
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.
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.
23 #include "telescope.h"
25 #include <phos/phos.h>
44 #define YYSTYPE yystype
46 static char *current_style;
47 static int color_type;
49 static const char *path;
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 *);
69 %token TSTYLE TPRFX TCONT TBG TFG TATTR
94 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
95 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
98 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
99 stylespec : styleopt | '{' styleopts '}' ;
101 styleopts : /* empty */
103 | styleopts styleopt '\n'
106 styleopt : TPRFX TSTRING { setprfx($2, $2); }
107 | TPRFX TSTRING TSTRING { setprfx($2, $2); }
108 | TBG { color_type = TBG; } colorspec
109 | TFG { color_type = TFG; } colorspec
113 colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
114 | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
115 | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
118 attr : TSTRING { setattr($1, $1, $1); free($1); }
119 | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
120 | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
123 bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
126 unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
129 proxy : TPROXY TSTRING TVIA TSTRING { add_proxy($2, $4); free($4); }
135 yyerror(const char *fmt, ...)
139 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
141 vfprintf(stderr, fmt, va);
143 fprintf(stderr, "\n");
147 static struct keyword {
160 { "unbind", TUNBIND },
167 char buf[1024], *ebuf, *p, *str;
169 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
173 ebuf = buf + sizeof(buf);
176 /* skip whitespace first */
177 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
180 /* check for special one-character constructions */
183 /* silently eat up any \r */
194 /* skip comments; NUL is allowed; no continuation */
195 while ((c = getc(yyfp)) != '\n')
205 /* parsing next word */
206 for (;; c = getc(yyfp), yylval.colno++) {
209 yyerror("unallowed character NULL in column %d",
220 yyerror("unterminated quotes in column %d",
231 yyerror("unterminated escape in column %d",
234 yyerror("unterminated quotes in column %d",
243 if (!escape && !quotes)
258 yyerror("line too long");
270 * There could be a number of reason for empty buffer,
271 * and we handle all of them here, to avoid cluttering
276 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
280 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
281 if (strcmp(buf, keywords[i].word) == 0)
282 return keywords[i].token;
286 if (!nonkw && (c == '-' || isdigit(c))) {
287 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
289 yyerror("number is %s: %s", errstr, buf);
292 if ((str = strdup(buf)) == NULL)
293 err(1, "%s", __func__);
299 yyerror("input error reading config");
304 setprfx(const char *prfx, const char *cont)
306 assert(current_style != NULL);
308 if (!config_setprfx(current_style, prfx, cont))
309 yyerror("invalid style %s", current_style);
313 setvari(char *var, int val)
315 if (!config_setvari(var, val))
316 yyerror("invalid variable or value: %s = %d",
323 setvars(char *var, char *val)
325 if (!config_setvars(var, val))
326 yyerror("invalid variable or value: %s = \"%s\"",
333 colorname(const char *name)
340 { "black", COLOR_BLACK },
341 { "red", COLOR_RED },
342 { "green", COLOR_GREEN },
343 { "yellow", COLOR_YELLOW },
344 { "blue", COLOR_BLUE },
345 { "magenta", COLOR_MAGENTA },
346 { "cyan", COLOR_CYAN },
347 { "white", COLOR_WHITE },
353 if (has_prefix(name, "colo")) {
354 /* people are strange */
355 if (has_prefix(name, "color"))
357 else if (has_prefix(name, "colour"))
362 n = strtonum(name, 0, 256, &errstr);
364 yyerror("color number is %s: %s", errstr, name);
368 for (i = colors; i->name != NULL; ++i) {
369 if (!strcmp(i->name, name))
374 yyerror("unknown color name \"%s\"", name);
379 setcolor(const char *prfx, const char *line, const char *trail)
383 assert(current_style != NULL);
387 t = colorname(trail);
389 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
390 yyerror("invalid style %s", current_style);
400 { "normal", A_NORMAL },
401 { "standout", A_STANDOUT },
402 { "underline", A_UNDERLINE },
403 { "reverse", A_REVERSE },
404 { "blink", A_BLINK },
413 while ((ap = strsep(&n, ",")) != NULL) {
418 for (i = attrs; i ->name != NULL; ++i) {
419 if (strcmp(i->name, ap))
427 yyerror("unknown attribute \"%s\" at col %d",
435 setattr(char *prfx, char *line, char *trail)
439 assert(current_style != NULL);
445 if (!config_setattr(current_style, p, l, t))
446 yyerror("invalid style %s", current_style);
450 add_proxy(char *proto, char *proxy)
455 if (!phos_parse_absolute_uri(proxy, &uri)) {
456 yyerror("can't parse URL: %s", proxy);
460 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
461 yyerror("proxy url can't have path, query or fragments");
465 if (strcmp(uri.scheme, "gemini")) {
466 yyerror("disallowed proxy protocol %s", uri.scheme);
470 if ((p = calloc(1, sizeof(*p))) == NULL)
473 p->match_proto = proto;
475 if ((p->host = strdup(uri.host)) == NULL)
478 if ((p->port = strdup(uri.port)) == NULL)
481 TAILQ_INSERT_HEAD(&proxies, p, proxies);
485 parseconfig(const char *filename, int fonf)
487 if ((yyfp = fopen(filename, "r")) == NULL) {
489 err(1, "%s", filename);