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(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);
100 set : SET STRING '=' STRING { setvars($2, $4); }
101 | SET STRING '=' NUMBER { setvari($2, $4); }
104 style : STYLE STRING { current_style = $2; } stylespec ;
105 stylespec : styleopt | '{' optnl styleopts '}' ;
107 styleopts : /* empty */
108 | styleopts styleopt optnl
111 styleopt : PRFX STRING { setprfx($2, $2); }
112 | PRFX STRING STRING { setprfx($2, $3); }
113 | BG { color_type = BG; } colorspec
114 | FG { color_type = FG; } colorspec
118 colorspec : STRING { setcolor($1, $1, $1); free($1); }
119 | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
120 | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
123 attr : STRING { setattr($1, $1, $1); free($1); }
124 | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
125 | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
128 bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
131 unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
134 proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
137 optnl : '\n' optnl /* zero or more newlines */
144 yyerror(const char *fmt, ...)
148 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
150 vfprintf(stderr, fmt, va);
152 fprintf(stderr, "\n");
156 static struct keyword {
169 { "unbind", UNBIND },
176 char buf[1024], *ebuf, *p, *str;
178 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
182 ebuf = buf + sizeof(buf);
185 /* skip whitespace first */
186 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
189 /* check for special one-character constructions */
192 /* silently eat up any \r */
203 /* skip comments; NUL is allowed; no continuation */
204 while ((c = getc(yyfp)) != '\n')
214 /* parsing next word */
215 for (;; c = getc(yyfp), yylval.colno++) {
218 yyerror("unallowed character NULL in column %d",
228 /* ignore \r here too */
232 yyerror("unterminated quotes in column %d",
243 yyerror("unterminated escape in column %d",
246 yyerror("unterminated quotes in column %d",
255 if (!escape && !quotes)
270 yyerror("line too long");
282 * There could be a number of reason for empty buffer,
283 * and we handle all of them here, to avoid cluttering
288 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
292 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
293 if (strcmp(buf, keywords[i].word) == 0)
294 return keywords[i].token;
298 if (!nonkw && (c == '-' || isdigit(c))) {
299 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
301 yyerror("number is %s: %s", errstr, buf);
304 if ((str = strdup(buf)) == NULL)
305 err(1, "%s", __func__);
311 yyerror("input error reading config");
316 setprfx(const char *prfx, const char *cont)
318 assert(current_style != NULL);
320 if (!config_setprfx(current_style, prfx, cont))
321 yyerror("invalid style %s", current_style);
325 setvari(char *var, int val)
327 if (!config_setvari(var, val))
328 yyerror("invalid variable or value: %s = %d",
335 setvars(char *var, char *val)
337 if (!config_setvars(var, val))
338 yyerror("invalid variable or value: %s = \"%s\"",
345 colorname(const char *name)
352 { "black", COLOR_BLACK },
353 { "red", COLOR_RED },
354 { "green", COLOR_GREEN },
355 { "yellow", COLOR_YELLOW },
356 { "blue", COLOR_BLUE },
357 { "magenta", COLOR_MAGENTA },
358 { "cyan", COLOR_CYAN },
359 { "white", COLOR_WHITE },
365 if (has_prefix(name, "colo")) {
366 /* people are strange */
367 if (has_prefix(name, "color"))
369 else if (has_prefix(name, "colour"))
374 n = strtonum(name, 0, 256, &errstr);
376 yyerror("color number is %s: %s", errstr, name);
380 for (i = colors; i->name != NULL; ++i) {
381 if (!strcmp(i->name, name))
386 yyerror("unknown color name \"%s\"", name);
391 setcolor(const char *prfx, const char *line, const char *trail)
395 assert(current_style != NULL);
399 t = colorname(trail);
401 if (!config_setcolor(color_type == BG, current_style, p, l, t))
402 yyerror("invalid style %s", current_style);
406 attrname(const char *n)
412 { "normal", A_NORMAL },
413 { "standout", A_STANDOUT },
414 { "underline", A_UNDERLINE },
415 { "reverse", A_REVERSE },
416 { "blink", A_BLINK },
422 char *ap, *dup, *orig;
424 if ((dup = strdup(n)) == NULL)
430 while ((ap = strsep(&dup, ",")) != NULL) {
435 for (i = attrs; i ->name != NULL; ++i) {
436 if (strcmp(i->name, ap))
444 yyerror("unknown attribute \"%s\" at col %d",
453 setattr(char *prfx, char *line, char *trail)
457 assert(current_style != NULL);
463 if (!config_setattr(current_style, p, l, t))
464 yyerror("invalid style %s", current_style);
468 add_proxy(char *proto, char *proxy)
473 if (!phos_parse_absolute_uri(proxy, &uri)) {
474 yyerror("can't parse URL: %s", proxy);
478 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
479 yyerror("proxy url can't have path, query or fragments");
483 if (strcmp(uri.scheme, "gemini")) {
484 yyerror("disallowed proxy protocol %s", uri.scheme);
488 if ((p = calloc(1, sizeof(*p))) == NULL)
491 p->match_proto = proto;
492 p->proto = PROTO_GEMINI;
494 if ((p->host = strdup(uri.host)) == NULL)
497 if ((p->port = strdup(uri.port)) == NULL)
500 TAILQ_INSERT_HEAD(&proxies, p, proxies);
503 static interactivefn *
504 cmdname(const char *name)
508 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
509 if (!strcmp(cmd->cmd, name))
517 bindkey(const char *map, const char *key, const char *cmd)
522 if (!strcmp(map, "global-map"))
524 else if (!strcmp(map, "minibuffer-map"))
525 kmap = &minibuffer_map;
527 yyerror("unknown map: %s", map);
531 if ((fn = cmdname(cmd)) == NULL) {
532 yyerror("unknown cmd: %s", fn);
536 if (!kmap_define_key(kmap, key, fn))
537 yyerror("failed to bind %s %s %s", map, key, cmd);
541 do_parseconfig(const char *filename, int fonf)
543 if ((yyfp = fopen(filename, "r")) == NULL) {
545 err(1, "%s", filename);
557 parseconfig(const char *filename, int fonf)
559 char altconf[PATH_MAX], *term;
561 /* load the given config file */
562 do_parseconfig(filename, fonf);
564 /* then try to load file-TERM */
566 if ((term = getenv("TERM")) == NULL)
569 strlcpy(altconf, filename, sizeof(altconf));
570 strlcat(altconf, "-", sizeof(altconf));
571 strlcat(altconf, term, sizeof(altconf));
573 do_parseconfig(altconf, 0);