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"
26 #include <phos/phos.h>
45 #define YYSTYPE yystype
47 static char *current_style;
48 static int color_type;
50 static const char *path;
56 static void yyerror(const char *, ...);
57 static int yylex(void);
58 static void setprfx(const char *, const char *);
59 static void setvari(char *, int);
60 static void setvars(char *, char *);
61 static int colorname(const char *);
62 static void setcolor(const char *, const char *, const char *);
63 static int attrname(const char *);
64 static void setattr(char *, char *, char *);
65 static void add_proxy(char *, char *);
66 static void bindkey(const char *, const char *, const char *);
67 static void do_parseconfig(const char *, int);
101 set : SET STRING '=' STRING { setvars($2, $4); }
102 | SET STRING '=' NUMBER { setvari($2, $4); }
105 style : STYLE STRING { current_style = $2; } stylespec ;
106 stylespec : styleopt | '{' optnl styleopts '}' ;
108 styleopts : /* empty */
109 | styleopts styleopt optnl
112 styleopt : PRFX STRING { setprfx($2, $2); }
113 | PRFX STRING STRING { setprfx($2, $3); }
114 | BG { color_type = BG; } colorspec
115 | FG { color_type = FG; } colorspec
119 colorspec : STRING { setcolor($1, $1, $1); free($1); }
120 | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
121 | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
124 attr : STRING { setattr($1, $1, $1); free($1); }
125 | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
126 | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
129 bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
132 unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
135 proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
138 optnl : '\n' optnl /* zero or more newlines */
145 yyerror(const char *fmt, ...)
149 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
151 vfprintf(stderr, fmt, va);
153 fprintf(stderr, "\n");
157 static struct keyword {
170 { "unbind", UNBIND },
177 char buf[1024], *ebuf, *p, *str;
179 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
183 ebuf = buf + sizeof(buf);
186 /* skip whitespace first */
187 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
190 /* check for special one-character constructions */
193 /* silently eat up any \r */
204 /* skip comments; NUL is allowed; no continuation */
205 while ((c = getc(yyfp)) != '\n')
215 /* parsing next word */
216 for (;; c = getc(yyfp), yylval.colno++) {
219 yyerror("unallowed character NULL in column %d",
229 /* ignore \r here too */
233 yyerror("unterminated quotes in column %d",
244 yyerror("unterminated escape in column %d",
247 yyerror("unterminated quotes in column %d",
256 if (!escape && !quotes)
271 yyerror("line too long");
283 * There could be a number of reason for empty buffer,
284 * and we handle all of them here, to avoid cluttering
289 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
293 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
294 if (strcmp(buf, keywords[i].word) == 0)
295 return keywords[i].token;
299 if (!nonkw && (c == '-' || isdigit(c))) {
300 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
302 yyerror("number is %s: %s", errstr, buf);
305 if ((str = strdup(buf)) == NULL)
306 err(1, "%s", __func__);
312 yyerror("input error reading config");
317 setprfx(const char *prfx, const char *cont)
319 assert(current_style != NULL);
321 if (!config_setprfx(current_style, prfx, cont))
322 yyerror("invalid style %s", current_style);
326 setvari(char *var, int val)
328 if (!config_setvari(var, val))
329 yyerror("invalid variable or value: %s = %d",
336 setvars(char *var, char *val)
338 if (!config_setvars(var, val))
339 yyerror("invalid variable or value: %s = \"%s\"",
346 colorname(const char *name)
353 { "black", COLOR_BLACK },
354 { "red", COLOR_RED },
355 { "green", COLOR_GREEN },
356 { "yellow", COLOR_YELLOW },
357 { "blue", COLOR_BLUE },
358 { "magenta", COLOR_MAGENTA },
359 { "cyan", COLOR_CYAN },
360 { "white", COLOR_WHITE },
366 if (has_prefix(name, "colo")) {
367 /* people are strange */
368 if (has_prefix(name, "color"))
370 else if (has_prefix(name, "colour"))
375 n = strtonum(name, 0, 256, &errstr);
377 yyerror("color number is %s: %s", errstr, name);
381 for (i = colors; i->name != NULL; ++i) {
382 if (!strcmp(i->name, name))
387 yyerror("unknown color name \"%s\"", name);
392 setcolor(const char *prfx, const char *line, const char *trail)
396 assert(current_style != NULL);
400 t = colorname(trail);
402 if (!config_setcolor(color_type == BG, current_style, p, l, t))
403 yyerror("invalid style %s", current_style);
407 attrname(const char *n)
413 { "normal", A_NORMAL },
414 { "standout", A_STANDOUT },
415 { "underline", A_UNDERLINE },
416 { "reverse", A_REVERSE },
417 { "blink", A_BLINK },
423 char *ap, *dup, *orig;
425 if ((dup = strdup(n)) == NULL)
431 while ((ap = strsep(&dup, ",")) != NULL) {
436 for (i = attrs; i ->name != NULL; ++i) {
437 if (strcmp(i->name, ap))
445 yyerror("unknown attribute \"%s\" at col %d",
454 setattr(char *prfx, char *line, char *trail)
458 assert(current_style != NULL);
464 if (!config_setattr(current_style, p, l, t))
465 yyerror("invalid style %s", current_style);
469 add_proxy(char *proto, char *proxy)
474 if (!phos_parse_absolute_uri(proxy, &uri)) {
475 yyerror("can't parse URL: %s", proxy);
479 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
480 yyerror("proxy url can't have path, query or fragments");
484 if (strcmp(uri.scheme, "gemini")) {
485 yyerror("disallowed proxy protocol %s", uri.scheme);
489 if ((p = calloc(1, sizeof(*p))) == NULL)
492 p->match_proto = proto;
493 p->proto = PROTO_GEMINI;
495 if ((p->host = strdup(uri.host)) == NULL)
498 if ((p->port = strdup(uri.port)) == NULL)
501 TAILQ_INSERT_HEAD(&proxies, p, proxies);
504 static interactivefn *
505 cmdname(const char *name)
509 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
510 if (!strcmp(cmd->cmd, name))
518 bindkey(const char *map, const char *key, const char *cmd)
523 if (!strcmp(map, "global-map"))
525 else if (!strcmp(map, "minibuffer-map"))
526 kmap = &minibuffer_map;
528 yyerror("unknown map: %s", map);
532 if ((fn = cmdname(cmd)) == NULL) {
533 yyerror("unknown cmd: %s", fn);
537 if (!kmap_define_key(kmap, key, fn))
538 yyerror("failed to bind %s %s %s", map, key, cmd);
542 do_parseconfig(const char *filename, int fonf)
544 if ((yyfp = fopen(filename, "r")) == NULL) {
546 err(1, "%s", filename);
558 parseconfig(const char *filename, int fonf)
560 char altconf[PATH_MAX], *term;
562 /* load the given config file */
563 do_parseconfig(filename, fonf);
565 /* then try to load file-TERM */
567 if ((term = getenv("TERM")) == NULL)
570 strlcpy(altconf, filename, sizeof(altconf));
571 strlcat(altconf, "-", sizeof(altconf));
572 strlcat(altconf, term, sizeof(altconf));
574 do_parseconfig(altconf, 0);