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 *);
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 | '{' styleopts '}' ;
103 styleopts : /* empty */
105 | styleopts styleopt optnl
108 styleopt : TPRFX TSTRING { setprfx($2, $2); }
109 | TPRFX TSTRING TSTRING { setprfx($2, $3); }
110 | TBG { color_type = TBG; } colorspec
111 | TFG { color_type = TFG; } colorspec
115 colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
116 | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
117 | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
120 attr : TSTRING { setattr($1, $1, $1); free($1); }
121 | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
122 | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
125 bind : TBIND TSTRING TSTRING TSTRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
128 unbind : TUNBIND TSTRING TSTRING { yyerror("TODO: unbind %s %s", $2, $3); }
131 proxy : TPROXY TSTRING TVIA TSTRING { add_proxy($2, $4); free($4); }
134 optnl : '\n' optnl /* zero or more newlines */
141 yyerror(const char *fmt, ...)
145 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
147 vfprintf(stderr, fmt, va);
149 fprintf(stderr, "\n");
153 static struct keyword {
166 { "unbind", TUNBIND },
173 char buf[1024], *ebuf, *p, *str;
175 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
179 ebuf = buf + sizeof(buf);
182 /* skip whitespace first */
183 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
186 /* check for special one-character constructions */
189 /* silently eat up any \r */
200 /* skip comments; NUL is allowed; no continuation */
201 while ((c = getc(yyfp)) != '\n')
211 /* parsing next word */
212 for (;; c = getc(yyfp), yylval.colno++) {
215 yyerror("unallowed character NULL in column %d",
225 /* ignore \r here too */
229 yyerror("unterminated quotes in column %d",
240 yyerror("unterminated escape in column %d",
243 yyerror("unterminated quotes in column %d",
252 if (!escape && !quotes)
267 yyerror("line too long");
279 * There could be a number of reason for empty buffer,
280 * and we handle all of them here, to avoid cluttering
285 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
289 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
290 if (strcmp(buf, keywords[i].word) == 0)
291 return keywords[i].token;
295 if (!nonkw && (c == '-' || isdigit(c))) {
296 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
298 yyerror("number is %s: %s", errstr, buf);
301 if ((str = strdup(buf)) == NULL)
302 err(1, "%s", __func__);
308 yyerror("input error reading config");
313 setprfx(const char *prfx, const char *cont)
315 assert(current_style != NULL);
317 if (!config_setprfx(current_style, prfx, cont))
318 yyerror("invalid style %s", current_style);
322 setvari(char *var, int val)
324 if (!config_setvari(var, val))
325 yyerror("invalid variable or value: %s = %d",
332 setvars(char *var, char *val)
334 if (!config_setvars(var, val))
335 yyerror("invalid variable or value: %s = \"%s\"",
342 colorname(const char *name)
349 { "black", COLOR_BLACK },
350 { "red", COLOR_RED },
351 { "green", COLOR_GREEN },
352 { "yellow", COLOR_YELLOW },
353 { "blue", COLOR_BLUE },
354 { "magenta", COLOR_MAGENTA },
355 { "cyan", COLOR_CYAN },
356 { "white", COLOR_WHITE },
362 if (has_prefix(name, "colo")) {
363 /* people are strange */
364 if (has_prefix(name, "color"))
366 else if (has_prefix(name, "colour"))
371 n = strtonum(name, 0, 256, &errstr);
373 yyerror("color number is %s: %s", errstr, name);
377 for (i = colors; i->name != NULL; ++i) {
378 if (!strcmp(i->name, name))
383 yyerror("unknown color name \"%s\"", name);
388 setcolor(const char *prfx, const char *line, const char *trail)
392 assert(current_style != NULL);
396 t = colorname(trail);
398 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
399 yyerror("invalid style %s", current_style);
409 { "normal", A_NORMAL },
410 { "standout", A_STANDOUT },
411 { "underline", A_UNDERLINE },
412 { "reverse", A_REVERSE },
413 { "blink", A_BLINK },
422 while ((ap = strsep(&n, ",")) != NULL) {
427 for (i = attrs; i ->name != NULL; ++i) {
428 if (strcmp(i->name, ap))
436 yyerror("unknown attribute \"%s\" at col %d",
444 setattr(char *prfx, char *line, char *trail)
448 assert(current_style != NULL);
454 if (!config_setattr(current_style, p, l, t))
455 yyerror("invalid style %s", current_style);
459 add_proxy(char *proto, char *proxy)
464 if (!phos_parse_absolute_uri(proxy, &uri)) {
465 yyerror("can't parse URL: %s", proxy);
469 if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
470 yyerror("proxy url can't have path, query or fragments");
474 if (strcmp(uri.scheme, "gemini")) {
475 yyerror("disallowed proxy protocol %s", uri.scheme);
479 if ((p = calloc(1, sizeof(*p))) == NULL)
482 p->match_proto = proto;
484 if ((p->host = strdup(uri.host)) == NULL)
487 if ((p->port = strdup(uri.port)) == NULL)
490 TAILQ_INSERT_HEAD(&proxies, p, proxies);
493 static interactivefn *
494 cmdname(const char *name)
498 for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
499 if (!strcmp(cmd->cmd, name))
507 bindkey(const char *map, const char *key, const char *cmd)
512 if (!strcmp(map, "global-map"))
514 else if (!strcmp(map, "minibuffer-map"))
515 kmap = &minibuffer_map;
517 yyerror("unknown map: %s", map);
521 if ((fn = cmdname(cmd)) == NULL) {
522 yyerror("unknown cmd: %s", fn);
526 if (!kmap_define_key(kmap, key, fn))
527 yyerror("failed to bind %s %s %s", map, key, cmd);
531 do_parseconfig(const char *filename, int fonf)
533 if ((yyfp = fopen(filename, "r")) == NULL) {
535 err(1, "%s", filename);
547 parseconfig(const char *filename, int fonf)
549 char altconf[PATH_MAX], *term;
551 /* load the given config file */
552 do_parseconfig(filename, fonf);
554 /* then try to load file-TERM */
556 if ((term = getenv("TERM")) == NULL)
559 strlcpy(altconf, filename, sizeof(altconf));
560 strlcat(altconf, "-", sizeof(altconf));
561 strlcat(altconf, term, sizeof(altconf));
563 do_parseconfig(altconf, 0);