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);
71 %token TSTYLE TPRFX TCONT TBG TFG TATTR
96 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
97 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
100 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
101 stylespec : styleopt | '{' optnl styleopts '}' ;
103 styleopts : /* empty */
104 | styleopts styleopt optnl
107 styleopt : TPRFX TSTRING { setprfx($2, $2); }
108 | TPRFX TSTRING TSTRING { setprfx($2, $3); }
109 | TBG { color_type = TBG; } colorspec
110 | TFG { color_type = TFG; } colorspec
114 colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
115 | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
116 | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
119 attr : TSTRING { setattr($1, $1, $1); free($1); }
120 | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
121 | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
124 bind : TBIND TSTRING TSTRING TSTRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
127 unbind : TUNBIND TSTRING TSTRING { yyerror("TODO: unbind %s %s", $2, $3); }
130 proxy : TPROXY TSTRING TVIA TSTRING { add_proxy($2, $4); free($4); }
133 optnl : '\n' optnl /* zero or more newlines */
140 yyerror(const char *fmt, ...)
144 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
146 vfprintf(stderr, fmt, va);
148 fprintf(stderr, "\n");
152 static struct keyword {
165 { "unbind", TUNBIND },
172 char buf[1024], *ebuf, *p, *str;
174 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
178 ebuf = buf + sizeof(buf);
181 /* skip whitespace first */
182 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
185 /* check for special one-character constructions */
188 /* silently eat up any \r */
199 /* skip comments; NUL is allowed; no continuation */
200 while ((c = getc(yyfp)) != '\n')
210 /* parsing next word */
211 for (;; c = getc(yyfp), yylval.colno++) {
214 yyerror("unallowed character NULL in column %d",
224 /* ignore \r here too */
228 yyerror("unterminated quotes in column %d",
239 yyerror("unterminated escape in column %d",
242 yyerror("unterminated quotes in column %d",
251 if (!escape && !quotes)
266 yyerror("line too long");
278 * There could be a number of reason for empty buffer,
279 * and we handle all of them here, to avoid cluttering
284 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
288 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
289 if (strcmp(buf, keywords[i].word) == 0)
290 return keywords[i].token;
294 if (!nonkw && (c == '-' || isdigit(c))) {
295 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
297 yyerror("number is %s: %s", errstr, buf);
300 if ((str = strdup(buf)) == NULL)
301 err(1, "%s", __func__);
307 yyerror("input error reading config");
312 setprfx(const char *prfx, const char *cont)
314 assert(current_style != NULL);
316 if (!config_setprfx(current_style, prfx, cont))
317 yyerror("invalid style %s", current_style);
321 setvari(char *var, int val)
323 if (!config_setvari(var, val))
324 yyerror("invalid variable or value: %s = %d",
331 setvars(char *var, char *val)
333 if (!config_setvars(var, val))
334 yyerror("invalid variable or value: %s = \"%s\"",
341 colorname(const char *name)
348 { "black", COLOR_BLACK },
349 { "red", COLOR_RED },
350 { "green", COLOR_GREEN },
351 { "yellow", COLOR_YELLOW },
352 { "blue", COLOR_BLUE },
353 { "magenta", COLOR_MAGENTA },
354 { "cyan", COLOR_CYAN },
355 { "white", COLOR_WHITE },
361 if (has_prefix(name, "colo")) {
362 /* people are strange */
363 if (has_prefix(name, "color"))
365 else if (has_prefix(name, "colour"))
370 n = strtonum(name, 0, 256, &errstr);
372 yyerror("color number is %s: %s", errstr, name);
376 for (i = colors; i->name != NULL; ++i) {
377 if (!strcmp(i->name, name))
382 yyerror("unknown color name \"%s\"", name);
387 setcolor(const char *prfx, const char *line, const char *trail)
391 assert(current_style != NULL);
395 t = colorname(trail);
397 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
398 yyerror("invalid style %s", current_style);
402 attrname(const char *n)
408 { "normal", A_NORMAL },
409 { "standout", A_STANDOUT },
410 { "underline", A_UNDERLINE },
411 { "reverse", A_REVERSE },
412 { "blink", A_BLINK },
418 char *ap, *dup, *orig;
420 if ((dup = strdup(n)) == NULL)
426 while ((ap = strsep(&dup, ",")) != NULL) {
431 for (i = attrs; i ->name != NULL; ++i) {
432 if (strcmp(i->name, ap))
440 yyerror("unknown attribute \"%s\" at col %d",
449 setattr(char *prfx, char *line, char *trail)
453 assert(current_style != NULL);
459 if (!config_setattr(current_style, p, l, t))
460 yyerror("invalid style %s", current_style);
464 add_proxy(char *proto, char *proxy)
469 if (!phos_parse_absolute_uri(proxy, &uri)) {
470 yyerror("can't parse URL: %s", proxy);
474 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
475 yyerror("proxy url can't have path, query or fragments");
479 if (strcmp(uri.scheme, "gemini")) {
480 yyerror("disallowed proxy protocol %s", uri.scheme);
484 if ((p = calloc(1, sizeof(*p))) == NULL)
487 p->match_proto = proto;
488 p->proto = PROTO_GEMINI;
490 if ((p->host = strdup(uri.host)) == NULL)
493 if ((p->port = strdup(uri.port)) == NULL)
496 TAILQ_INSERT_HEAD(&proxies, p, proxies);
499 static interactivefn *
500 cmdname(const char *name)
504 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
505 if (!strcmp(cmd->cmd, name))
513 bindkey(const char *map, const char *key, const char *cmd)
518 if (!strcmp(map, "global-map"))
520 else if (!strcmp(map, "minibuffer-map"))
521 kmap = &minibuffer_map;
523 yyerror("unknown map: %s", map);
527 if ((fn = cmdname(cmd)) == NULL) {
528 yyerror("unknown cmd: %s", fn);
532 if (!kmap_define_key(kmap, key, fn))
533 yyerror("failed to bind %s %s %s", map, key, cmd);
537 do_parseconfig(const char *filename, int fonf)
539 if ((yyfp = fopen(filename, "r")) == NULL) {
541 err(1, "%s", filename);
553 parseconfig(const char *filename, int fonf)
555 char altconf[PATH_MAX], *term;
557 /* load the given config file */
558 do_parseconfig(filename, fonf);
560 /* then try to load file-TERM */
562 if ((term = getenv("TERM")) == NULL)
565 strlcpy(altconf, filename, sizeof(altconf));
566 strlcat(altconf, "-", sizeof(altconf));
567 strlcat(altconf, term, sizeof(altconf));
569 do_parseconfig(altconf, 0);