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.
24 #include "telescope.h"
46 #define YYSTYPE yystype
48 static char *current_style;
49 static int color_type;
51 static const char *path;
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);
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
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 */
146 yyerror(const char *fmt, ...)
150 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
152 vfprintf(stderr, fmt, va);
154 fprintf(stderr, "\n");
158 static struct keyword {
171 { "unbind", UNBIND },
178 char buf[1024], *ebuf, *p, *str;
180 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
184 ebuf = buf + sizeof(buf);
187 /* skip whitespace first */
188 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
191 /* check for special one-character constructions */
194 /* silently eat up any \r */
205 /* skip comments; NUL is allowed; no continuation */
206 while ((c = getc(yyfp)) != '\n')
216 /* parsing next word */
217 for (;; c = getc(yyfp), yylval.colno++) {
220 yyerror("unallowed character NULL in column %d",
230 /* ignore \r here too */
234 yyerror("unterminated quotes in column %d",
245 yyerror("unterminated escape in column %d",
248 yyerror("unterminated quotes in column %d",
257 if (!escape && !quotes)
272 yyerror("line too long");
284 * There could be a number of reason for empty buffer,
285 * and we handle all of them here, to avoid cluttering
290 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
294 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
295 if (strcmp(buf, keywords[i].word) == 0)
296 return keywords[i].token;
300 if (!nonkw && (c == '-' || isdigit(c))) {
301 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
303 yyerror("number is %s: %s", errstr, buf);
306 if ((str = strdup(buf)) == NULL)
307 err(1, "%s", __func__);
313 yyerror("input error reading config");
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);
327 setvari(char *var, int val)
329 if (!config_setvari(var, val))
330 yyerror("invalid variable or value: %s = %d",
337 setvars(char *var, char *val)
339 if (!config_setvars(var, val))
340 yyerror("invalid variable or value: %s = \"%s\"",
347 colorname(const char *name)
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 },
367 if (!strncmp(name, "colo", 4)) {
368 /* people are strange */
369 if (!strncmp(name, "color", 5))
371 else if (!strncmp(name, "colour", 6))
376 n = strtonum(name, 0, 256, &errstr);
378 yyerror("color number is %s: %s", errstr, name);
382 for (i = colors; i->name != NULL; ++i) {
383 if (!strcmp(i->name, name))
388 yyerror("unknown color name \"%s\"", name);
393 setcolor(const char *prfx, const char *line, const char *trail)
397 assert(current_style != NULL);
401 t = colorname(trail);
403 if (!config_setcolor(color_type == BG, current_style, p, l, t))
404 yyerror("invalid style %s", current_style);
408 attrname(const char *n)
414 { "normal", A_NORMAL },
415 { "standout", A_STANDOUT },
416 { "underline", A_UNDERLINE },
417 { "reverse", A_REVERSE },
418 { "blink", A_BLINK },
424 char *ap, *dup, *orig;
426 if ((dup = strdup(n)) == NULL)
432 while ((ap = strsep(&dup, ",")) != NULL) {
437 for (i = attrs; i ->name != NULL; ++i) {
438 if (strcmp(i->name, ap))
446 yyerror("unknown attribute \"%s\" at col %d",
455 setattr(char *prfx, char *line, char *trail)
459 assert(current_style != NULL);
465 if (!config_setattr(current_style, p, l, t))
466 yyerror("invalid style %s", current_style);
470 add_proxy(char *proto, char *proxy)
472 static struct iri iri;
475 if (iri_parse(NULL, proxy, &iri) == -1) {
476 yyerror("can't parse URL: %s", proxy);
480 if (iri.iri_flags & (IH_PATH|IH_QUERY)) {
481 yyerror("proxy url can't have path, query or fragments");
485 if (strcmp(iri.iri_scheme, "gemini")) {
486 yyerror("disallowed proxy protocol %s", iri.iri_scheme);
490 if ((p = calloc(1, sizeof(*p))) == NULL)
493 p->match_proto = proto;
494 p->proto = PROTO_GEMINI;
496 if ((p->host = strdup(iri.iri_host)) == NULL)
499 if ((p->port = strdup(iri.iri_portstr)) == NULL)
502 TAILQ_INSERT_HEAD(&proxies, p, proxies);
505 static interactivefn *
506 cmdname(const char *name)
510 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
511 if (!strcmp(cmd->cmd, name))
519 bindkey(const char *map, const char *key, const char *cmd)
524 if (!strcmp(map, "global-map"))
526 else if (!strcmp(map, "minibuffer-map"))
527 kmap = &minibuffer_map;
529 yyerror("unknown map: %s", map);
533 if ((fn = cmdname(cmd)) == NULL) {
534 yyerror("unknown cmd: %s", fn);
538 if (!kmap_define_key(kmap, key, fn))
539 yyerror("failed to bind %s %s %s", map, key, cmd);
543 do_parseconfig(const char *filename, int fonf)
545 if ((yyfp = fopen(filename, "r")) == NULL) {
547 err(1, "%s", filename);
559 parseconfig(const char *filename, int fonf)
561 char altconf[PATH_MAX], *term;
563 /* load the given config file */
564 do_parseconfig(filename, fonf);
566 /* then try to load file-TERM */
568 if ((term = getenv("TERM")) == NULL)
571 strlcpy(altconf, filename, sizeof(altconf));
572 strlcat(altconf, "-", sizeof(altconf));
573 strlcat(altconf, term, sizeof(altconf));
575 do_parseconfig(altconf, 0);