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.
38 #include "telescope.h"
50 #define YYSTYPE yystype
52 static char *current_style;
53 static int color_type;
55 static const char *path;
61 static void yyerror(const char *, ...);
62 static int yylex(void);
63 static void setprfx(const char *, const char *);
64 static void setvari(char *, int);
65 static void setvars(char *, char *);
66 static void setvarb(char *, int);
67 static int colorname(const char *);
68 static void setcolor(const char *, const char *, const char *);
69 static int attrname(const char *);
70 static void setattr(char *, char *, char *);
71 static void add_proxy(char *, char *);
72 static void bindkey(const char *, const char *, const char *);
73 static void do_parseconfig(const char *, int);
86 /* Sigh... they conflict with ncurses TRUE and FALSE */
87 %token TOK_TRUE TOK_FALSE
101 bool : TOK_TRUE { $$ = 1; }
102 | TOK_FALSE { $$ = 0; }
108 current_style = NULL;
115 set : SET STRING '=' STRING { setvars($2, $4); }
116 | SET STRING '=' NUMBER { setvari($2, $4); }
117 | SET STRING '=' bool { setvarb($2, $4); }
120 style : STYLE STRING { current_style = $2; } stylespec ;
121 stylespec : styleopt | '{' optnl styleopts '}' ;
123 styleopts : /* empty */
124 | styleopts styleopt optnl
127 styleopt : PRFX STRING { setprfx($2, $2); }
128 | PRFX STRING STRING { setprfx($2, $3); }
129 | BG { color_type = BG; } colorspec
130 | FG { color_type = FG; } colorspec
134 colorspec : STRING { setcolor($1, $1, $1); free($1); }
135 | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
136 | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
139 attr : STRING { setattr($1, $1, $1); free($1); }
140 | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
141 | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
144 bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
147 unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
150 proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
153 optnl : '\n' optnl /* zero or more newlines */
160 yyerror(const char *fmt, ...)
164 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
166 vfprintf(stderr, fmt, va);
168 fprintf(stderr, "\n");
172 static struct keyword {
180 { "false", TOK_FALSE },
186 { "true", TOK_TRUE },
187 { "unbind", UNBIND },
194 char buf[1024], *ebuf, *p, *str;
196 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
200 ebuf = buf + sizeof(buf);
203 /* skip whitespace first */
204 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
207 /* check for special one-character constructions */
210 /* silently eat up any \r */
221 /* skip comments; NUL is allowed; no continuation */
222 while ((c = getc(yyfp)) != '\n')
232 /* parsing next word */
233 for (;; c = getc(yyfp), yylval.colno++) {
236 yyerror("unallowed character NULL in column %d",
246 /* ignore \r here too */
250 yyerror("unterminated quotes in column %d",
261 yyerror("unterminated escape in column %d",
264 yyerror("unterminated quotes in column %d",
273 if (!escape && !quotes)
288 yyerror("line too long");
300 * There could be a number of reason for empty buffer,
301 * and we handle all of them here, to avoid cluttering
306 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
310 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
311 if (strcmp(buf, keywords[i].word) == 0)
312 return keywords[i].token;
316 if (!nonkw && (c == '-' || isdigit(c))) {
317 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
319 yyerror("number is %s: %s", errstr, buf);
328 yyerror("input error reading config");
333 setprfx(const char *prfx, const char *cont)
335 assert(current_style != NULL);
337 if (!config_setprfx(current_style, prfx, cont))
338 yyerror("invalid style %s", current_style);
342 setvari(char *var, int val)
345 * For some time, fall back to a boolean as compat
346 * with telescope 0.8 and previous.
348 if (!config_setvari(var, val) &&
349 !config_setvarb(var, val))
350 yyerror("invalid variable or value: %s = %d",
357 setvars(char *var, char *val)
359 if (!config_setvars(var, val))
360 yyerror("invalid variable or value: %s = \"%s\"",
367 setvarb(char *var, int val)
369 if (!config_setvarb(var, val))
370 yyerror("invalid variable or value: %s = %s",
371 var, val ? "true" : "false");
377 colorname(const char *name)
384 { "black", COLOR_BLACK },
385 { "red", COLOR_RED },
386 { "green", COLOR_GREEN },
387 { "yellow", COLOR_YELLOW },
388 { "blue", COLOR_BLUE },
389 { "magenta", COLOR_MAGENTA },
390 { "cyan", COLOR_CYAN },
391 { "white", COLOR_WHITE },
397 if (!strncmp(name, "colo", 4)) {
398 /* people are strange */
399 if (!strncmp(name, "color", 5))
401 else if (!strncmp(name, "colour", 6))
406 n = strtonum(name, 0, 256, &errstr);
408 yyerror("color number is %s: %s", errstr, name);
412 for (i = colors; i->name != NULL; ++i) {
413 if (!strcmp(i->name, name))
418 yyerror("unknown color name \"%s\"", name);
423 setcolor(const char *prfx, const char *line, const char *trail)
427 assert(current_style != NULL);
431 t = colorname(trail);
433 if (!config_setcolor(color_type == BG, current_style, p, l, t))
434 yyerror("invalid style %s", current_style);
438 attrname(const char *n)
444 { "normal", A_NORMAL },
445 { "standout", A_STANDOUT },
446 { "underline", A_UNDERLINE },
447 { "reverse", A_REVERSE },
448 { "blink", A_BLINK },
454 char *ap, *dup, *orig;
461 while ((ap = strsep(&dup, ",")) != NULL) {
466 for (i = attrs; i ->name != NULL; ++i) {
467 if (strcmp(i->name, ap))
475 yyerror("unknown attribute \"%s\" at col %d",
484 setattr(char *prfx, char *line, char *trail)
488 assert(current_style != NULL);
494 if (!config_setattr(current_style, p, l, t))
495 yyerror("invalid style %s", current_style);
499 add_proxy(char *proto, char *proxy)
501 static struct iri iri;
504 if (iri_parse(NULL, proxy, &iri) == -1) {
505 yyerror("can't parse URL: %s", proxy);
509 if ((iri.iri_flags & (IH_QUERY|IH_FRAGMENT)) ||
510 iri.iri_path[0] != '\0') {
511 yyerror("proxy url can't have path, query or fragments");
515 if (strcmp(iri.iri_scheme, "gemini")) {
516 yyerror("disallowed proxy protocol %s", iri.iri_scheme);
520 p = xcalloc(1, sizeof(*p));
522 p->match_proto = proto;
523 p->proto = PROTO_GEMINI;
525 p->host = xstrdup(iri.iri_host);
527 p->port = xstrdup(iri.iri_portstr);
529 TAILQ_INSERT_HEAD(&proxies, p, proxies);
532 static interactivefn *
533 cmdname(const char *name)
537 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
538 if (!strcmp(cmd->cmd, name))
546 bindkey(const char *map, const char *key, const char *cmd)
551 if (!strcmp(map, "global-map"))
553 else if (!strcmp(map, "minibuffer-map"))
554 kmap = &minibuffer_map;
556 yyerror("unknown map: %s", map);
560 if ((fn = cmdname(cmd)) == NULL) {
561 yyerror("unknown cmd: %s", fn);
565 if (!kmap_define_key(kmap, key, fn))
566 yyerror("failed to bind %s %s %s", map, key, cmd);
570 do_parseconfig(const char *filename, int fonf)
572 if ((yyfp = fopen(filename, "r")) == NULL) {
574 err(1, "%s", filename);
586 parseconfig(const char *filename, int fonf)
588 char altconf[PATH_MAX], *term;
590 /* load the given config file */
591 do_parseconfig(filename, fonf);
593 /* then try to load file-TERM */
595 if ((term = getenv("TERM")) == NULL)
598 strlcpy(altconf, filename, sizeof(altconf));
599 strlcat(altconf, "-", sizeof(altconf));
600 strlcat(altconf, term, sizeof(altconf));
602 do_parseconfig(altconf, 0);