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(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 *);
70 %token TSTYLE TPRFX TCONT TBG TFG TATTR
95 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
96 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
99 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
100 stylespec : styleopt | '{' styleopts '}' ;
102 styleopts : /* empty */
104 | styleopts styleopt '\n'
107 styleopt : TPRFX TSTRING { setprfx($2, $2); }
108 | TPRFX TSTRING TSTRING { setprfx($2, $2); }
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); }
136 yyerror(const char *fmt, ...)
140 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
142 vfprintf(stderr, fmt, va);
144 fprintf(stderr, "\n");
148 static struct keyword {
161 { "unbind", TUNBIND },
168 char buf[1024], *ebuf, *p, *str;
170 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
174 ebuf = buf + sizeof(buf);
177 /* skip whitespace first */
178 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
181 /* check for special one-character constructions */
184 /* silently eat up any \r */
195 /* skip comments; NUL is allowed; no continuation */
196 while ((c = getc(yyfp)) != '\n')
206 /* parsing next word */
207 for (;; c = getc(yyfp), yylval.colno++) {
210 yyerror("unallowed character NULL in column %d",
220 /* ignore \r here too */
224 yyerror("unterminated quotes in column %d",
235 yyerror("unterminated escape in column %d",
238 yyerror("unterminated quotes in column %d",
247 if (!escape && !quotes)
262 yyerror("line too long");
274 * There could be a number of reason for empty buffer,
275 * and we handle all of them here, to avoid cluttering
280 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
284 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
285 if (strcmp(buf, keywords[i].word) == 0)
286 return keywords[i].token;
290 if (!nonkw && (c == '-' || isdigit(c))) {
291 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
293 yyerror("number is %s: %s", errstr, buf);
296 if ((str = strdup(buf)) == NULL)
297 err(1, "%s", __func__);
303 yyerror("input error reading config");
308 setprfx(const char *prfx, const char *cont)
310 assert(current_style != NULL);
312 if (!config_setprfx(current_style, prfx, cont))
313 yyerror("invalid style %s", current_style);
317 setvari(char *var, int val)
319 if (!config_setvari(var, val))
320 yyerror("invalid variable or value: %s = %d",
327 setvars(char *var, char *val)
329 if (!config_setvars(var, val))
330 yyerror("invalid variable or value: %s = \"%s\"",
337 colorname(const char *name)
344 { "black", COLOR_BLACK },
345 { "red", COLOR_RED },
346 { "green", COLOR_GREEN },
347 { "yellow", COLOR_YELLOW },
348 { "blue", COLOR_BLUE },
349 { "magenta", COLOR_MAGENTA },
350 { "cyan", COLOR_CYAN },
351 { "white", COLOR_WHITE },
357 if (has_prefix(name, "colo")) {
358 /* people are strange */
359 if (has_prefix(name, "color"))
361 else if (has_prefix(name, "colour"))
366 n = strtonum(name, 0, 256, &errstr);
368 yyerror("color number is %s: %s", errstr, name);
372 for (i = colors; i->name != NULL; ++i) {
373 if (!strcmp(i->name, name))
378 yyerror("unknown color name \"%s\"", name);
383 setcolor(const char *prfx, const char *line, const char *trail)
387 assert(current_style != NULL);
391 t = colorname(trail);
393 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
394 yyerror("invalid style %s", current_style);
404 { "normal", A_NORMAL },
405 { "standout", A_STANDOUT },
406 { "underline", A_UNDERLINE },
407 { "reverse", A_REVERSE },
408 { "blink", A_BLINK },
417 while ((ap = strsep(&n, ",")) != NULL) {
422 for (i = attrs; i ->name != NULL; ++i) {
423 if (strcmp(i->name, ap))
431 yyerror("unknown attribute \"%s\" at col %d",
439 setattr(char *prfx, char *line, char *trail)
443 assert(current_style != NULL);
449 if (!config_setattr(current_style, p, l, t))
450 yyerror("invalid style %s", current_style);
454 add_proxy(char *proto, char *proxy)
459 if (!phos_parse_absolute_uri(proxy, &uri)) {
460 yyerror("can't parse URL: %s", proxy);
464 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
465 yyerror("proxy url can't have path, query or fragments");
469 if (strcmp(uri.scheme, "gemini")) {
470 yyerror("disallowed proxy protocol %s", uri.scheme);
474 if ((p = calloc(1, sizeof(*p))) == NULL)
477 p->match_proto = proto;
479 if ((p->host = strdup(uri.host)) == NULL)
482 if ((p->port = strdup(uri.port)) == NULL)
485 TAILQ_INSERT_HEAD(&proxies, p, proxies);
488 static interactivefn *
489 cmdname(const char *name)
493 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
494 if (!strcmp(cmd->cmd, name))
502 bindkey(const char *map, const char *key, const char *cmd)
507 if (!strcmp(map, "global-map"))
509 else if (!strcmp(map, "minibuffer-map"))
510 kmap = &minibuffer_map;
512 yyerror("unknown map: %s", map);
516 if ((fn = cmdname(cmd)) == NULL) {
517 yyerror("unknown cmd: %s", fn);
521 if (!kmap_define_key(kmap, key, fn))
522 yyerror("failed to bind %s %s %s", map, key, cmd);
526 parseconfig(const char *filename, int fonf)
528 if ((yyfp = fopen(filename, "r")) == NULL) {
530 err(1, "%s", filename);