2 69bdd906 2021-06-15 op * Much of the design is taken from doas (parse.y rev 1.29)
4 69bdd906 2021-06-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 69bdd906 2021-06-15 op * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
7 69bdd906 2021-06-15 op * Permission to use, copy, modify, and distribute this software for any
8 69bdd906 2021-06-15 op * purpose with or without fee is hereby granted, provided that the above
9 69bdd906 2021-06-15 op * copyright notice and this permission notice appear in all copies.
11 69bdd906 2021-06-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 69bdd906 2021-06-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 69bdd906 2021-06-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 69bdd906 2021-06-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 69bdd906 2021-06-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 69bdd906 2021-06-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 69bdd906 2021-06-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 b7d58e0b 2021-11-05 op #include "keymap.h"
24 69bdd906 2021-06-15 op #include "telescope.h"
26 984245ce 2021-06-23 op #include <phos/phos.h>
28 74a2587f 2021-06-21 op #include <assert.h>
29 69bdd906 2021-06-15 op #include <ctype.h>
30 69bdd906 2021-06-15 op #include <limits.h>
31 74a2587f 2021-06-21 op #include <ncurses.h>
32 69bdd906 2021-06-15 op #include <stdarg.h>
33 69bdd906 2021-06-15 op #include <stdio.h>
34 69bdd906 2021-06-15 op #include <stdlib.h>
35 69bdd906 2021-06-15 op #include <string.h>
37 69bdd906 2021-06-15 op typedef struct {
45 69bdd906 2021-06-15 op #define YYSTYPE yystype
47 69e1d1b6 2021-06-15 op static char *current_style;
48 d89b86d6 2021-06-21 op static int color_type;
50 69bdd906 2021-06-15 op static const char *path;
54 69bdd906 2021-06-15 op int parse_errors = 0;
56 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
57 69bdd906 2021-06-15 op static int yylex(void);
58 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
59 86e7d7b4 2021-06-19 op static void setvari(char *, int);
60 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
61 74a2587f 2021-06-21 op static int colorname(const char *);
62 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
63 3198f30f 2021-08-26 op static int attrname(const char *);
64 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
65 984245ce 2021-06-23 op static void add_proxy(char *, char *);
66 e3427d18 2021-06-25 op static void bindkey(const char *, const char *, const char *);
67 21404dd9 2021-07-15 op static void do_parseconfig(const char *, int);
72 bd3e6837 2021-11-03 op %token BIND BG
75 bd3e6837 2021-11-03 op %token PRFX PROXY
76 bd3e6837 2021-11-03 op %token SET STYLE
80 9cc9d696 2021-11-03 op %token <str> STRING
81 9cc9d696 2021-11-03 op %token <num> NUMBER
85 69bdd906 2021-06-15 op grammar : /* empty */
86 69bdd906 2021-06-15 op | grammar '\n'
87 69bdd906 2021-06-15 op | grammar rule '\n'
93 74a2587f 2021-06-21 op free(current_style);
94 74a2587f 2021-06-21 op current_style = NULL;
101 9cc9d696 2021-11-03 op set : SET STRING '=' STRING { setvars($2, $4); }
102 9cc9d696 2021-11-03 op | SET STRING '=' NUMBER { setvari($2, $4); }
105 9cc9d696 2021-11-03 op style : STYLE STRING { current_style = $2; } stylespec ;
106 5c38fd5f 2021-08-30 op stylespec : styleopt | '{' optnl styleopts '}' ;
108 69bdd906 2021-06-15 op styleopts : /* empty */
109 0a53787c 2021-07-09 op | styleopts styleopt optnl
112 9cc9d696 2021-11-03 op styleopt : PRFX STRING { setprfx($2, $2); }
113 9cc9d696 2021-11-03 op | PRFX STRING STRING { setprfx($2, $3); }
114 9cc9d696 2021-11-03 op | BG { color_type = BG; } colorspec
115 9cc9d696 2021-11-03 op | FG { color_type = FG; } colorspec
119 9cc9d696 2021-11-03 op colorspec : STRING { setcolor($1, $1, $1); free($1); }
120 9cc9d696 2021-11-03 op | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
121 9cc9d696 2021-11-03 op | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
124 9cc9d696 2021-11-03 op attr : STRING { setattr($1, $1, $1); free($1); }
125 9cc9d696 2021-11-03 op | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
126 9cc9d696 2021-11-03 op | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
129 9cc9d696 2021-11-03 op bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
132 9cc9d696 2021-11-03 op unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
135 9cc9d696 2021-11-03 op proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
138 0a53787c 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
139 0a53787c 2021-07-09 op | /* empty */
145 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
149 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
150 69bdd906 2021-06-15 op va_start(va, fmt);
151 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
153 69bdd906 2021-06-15 op fprintf(stderr, "\n");
154 69bdd906 2021-06-15 op parse_errors++;
157 69bdd906 2021-06-15 op static struct keyword {
158 69bdd906 2021-06-15 op const char *word;
160 69bdd906 2021-06-15 op } keywords[] = {
161 9cc9d696 2021-11-03 op { "attr", ATTR },
162 9cc9d696 2021-11-03 op { "bg", BG },
163 9cc9d696 2021-11-03 op { "bind", BIND },
164 9cc9d696 2021-11-03 op { "cont", CONT },
165 9cc9d696 2021-11-03 op { "fg", FG },
166 9cc9d696 2021-11-03 op { "prefix", PRFX },
167 9cc9d696 2021-11-03 op { "proxy", PROXY },
168 9cc9d696 2021-11-03 op { "set", SET },
169 9cc9d696 2021-11-03 op { "style", STYLE },
170 9cc9d696 2021-11-03 op { "unbind", UNBIND },
171 9cc9d696 2021-11-03 op { "via", VIA },
177 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
178 69bdd906 2021-06-15 op const char *errstr;
179 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
183 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
186 69bdd906 2021-06-15 op /* skip whitespace first */
187 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
188 69bdd906 2021-06-15 op yylval.colno++;
190 69bdd906 2021-06-15 op /* check for special one-character constructions */
193 c86d164e 2021-06-24 op /* silently eat up any \r */
196 69bdd906 2021-06-15 op yylval.colno = 0;
197 69bdd906 2021-06-15 op yylval.lineno++;
198 69bdd906 2021-06-15 op /* fallthrough */
204 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
205 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
206 69bdd906 2021-06-15 op if (c == EOF)
208 69bdd906 2021-06-15 op yylval.colno = 0;
209 69bdd906 2021-06-15 op yylval.lineno++;
215 69bdd906 2021-06-15 op /* parsing next word */
216 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
219 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
220 69bdd906 2021-06-15 op yylval.colno+1);
224 69bdd906 2021-06-15 op escape = !escape;
229 6ba9e5a8 2021-06-24 op /* ignore \r here too */
233 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
234 69bdd906 2021-06-15 op yylval.colno+1);
235 69bdd906 2021-06-15 op if (escape) {
238 69bdd906 2021-06-15 op yylval.colno = 0;
239 69bdd906 2021-06-15 op yylval.lineno++;
244 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
245 69bdd906 2021-06-15 op yylval.colno);
247 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
256 69bdd906 2021-06-15 op if (!escape && !quotes)
260 69bdd906 2021-06-15 op if (!escape) {
261 69bdd906 2021-06-15 op quotes = !quotes;
262 69bdd906 2021-06-15 op if (quotes) {
264 69bdd906 2021-06-15 op qpos = yylval.colno;
270 69bdd906 2021-06-15 op if (p == ebuf) {
271 69bdd906 2021-06-15 op yyerror("line too long");
279 69bdd906 2021-06-15 op if (c != EOF)
280 69bdd906 2021-06-15 op ungetc(c, yyfp);
281 69bdd906 2021-06-15 op if (p == buf) {
283 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
284 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
285 69bdd906 2021-06-15 op * the main loop.
287 69bdd906 2021-06-15 op if (c == EOF)
289 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
292 69bdd906 2021-06-15 op if (!nonkw) {
293 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
294 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
295 69bdd906 2021-06-15 op return keywords[i].token;
299 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
300 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
301 69bdd906 2021-06-15 op if (errstr != NULL)
302 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
303 9cc9d696 2021-11-03 op return NUMBER;
305 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
306 69bdd906 2021-06-15 op err(1, "%s", __func__);
307 69bdd906 2021-06-15 op yylval.str = str;
308 9cc9d696 2021-11-03 op return STRING;
311 69bdd906 2021-06-15 op if (ferror(yyfp))
312 69bdd906 2021-06-15 op yyerror("input error reading config");
317 d89b86d6 2021-06-21 op setprfx(const char *prfx, const char *cont)
319 74a2587f 2021-06-21 op assert(current_style != NULL);
321 d89b86d6 2021-06-21 op if (!config_setprfx(current_style, prfx, cont))
322 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
326 86e7d7b4 2021-06-19 op setvari(char *var, int val)
328 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
329 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
336 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
338 95a8c791 2021-08-26 op if (!config_setvars(var, val))
339 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
346 74a2587f 2021-06-21 op colorname(const char *name)
349 74a2587f 2021-06-21 op const char *name;
351 74a2587f 2021-06-21 op } *i, colors[] = {
352 160abe04 2021-06-21 op { "default", -1 },
353 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
354 74a2587f 2021-06-21 op { "red", COLOR_RED },
355 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
356 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
357 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
358 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
359 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
360 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
363 27766d48 2021-06-22 op const char *errstr;
366 27766d48 2021-06-22 op if (has_prefix(name, "colo")) {
367 27766d48 2021-06-22 op /* people are strange */
368 27766d48 2021-06-22 op if (has_prefix(name, "color"))
370 27766d48 2021-06-22 op else if (has_prefix(name, "colour"))
375 27766d48 2021-06-22 op n = strtonum(name, 0, 256, &errstr);
376 27766d48 2021-06-22 op if (errstr != NULL)
377 27766d48 2021-06-22 op yyerror("color number is %s: %s", errstr, name);
381 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
382 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
383 74a2587f 2021-06-21 op return i->val;
387 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
392 d89b86d6 2021-06-21 op setcolor(const char *prfx, const char *line, const char *trail)
396 74a2587f 2021-06-21 op assert(current_style != NULL);
398 160abe04 2021-06-21 op p = colorname(prfx);
399 160abe04 2021-06-21 op l = colorname(line);
400 160abe04 2021-06-21 op t = colorname(trail);
402 9cc9d696 2021-11-03 op if (!config_setcolor(color_type == BG, current_style, p, l, t))
403 d0149485 2021-06-22 op yyerror("invalid style %s", current_style);
407 3198f30f 2021-08-26 op attrname(const char *n)
410 d0149485 2021-06-22 op const char *name;
411 d0149485 2021-06-22 op unsigned int val;
412 d0149485 2021-06-22 op } *i, attrs[] = {
413 d0149485 2021-06-22 op { "normal", A_NORMAL },
414 d0149485 2021-06-22 op { "standout", A_STANDOUT },
415 d0149485 2021-06-22 op { "underline", A_UNDERLINE },
416 d0149485 2021-06-22 op { "reverse", A_REVERSE },
417 d0149485 2021-06-22 op { "blink", A_BLINK },
418 d0149485 2021-06-22 op { "dim", A_DIM },
419 d0149485 2021-06-22 op { "bold", A_BOLD },
422 d0149485 2021-06-22 op int ret, found;
423 3198f30f 2021-08-26 op char *ap, *dup, *orig;
425 3198f30f 2021-08-26 op if ((dup = strdup(n)) == NULL)
426 3198f30f 2021-08-26 op err(1, "strdup");
431 3198f30f 2021-08-26 op while ((ap = strsep(&dup, ",")) != NULL) {
432 d0149485 2021-06-22 op if (*ap == '\0')
436 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
437 d0149485 2021-06-22 op if (strcmp(i->name, ap))
439 d0149485 2021-06-22 op ret |= i->val;
445 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
446 d0149485 2021-06-22 op ap, yylval.colno+1);
454 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
458 d0149485 2021-06-22 op assert(current_style != NULL);
460 d0149485 2021-06-22 op p = attrname(prfx);
461 d0149485 2021-06-22 op l = attrname(line);
462 d0149485 2021-06-22 op t = attrname(trail);
464 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
465 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
469 984245ce 2021-06-23 op add_proxy(char *proto, char *proxy)
471 984245ce 2021-06-23 op struct proxy *p;
472 984245ce 2021-06-23 op struct phos_uri uri;
474 984245ce 2021-06-23 op if (!phos_parse_absolute_uri(proxy, &uri)) {
475 984245ce 2021-06-23 op yyerror("can't parse URL: %s", proxy);
479 984245ce 2021-06-23 op if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
480 984245ce 2021-06-23 op yyerror("proxy url can't have path, query or fragments");
484 984245ce 2021-06-23 op if (strcmp(uri.scheme, "gemini")) {
485 984245ce 2021-06-23 op yyerror("disallowed proxy protocol %s", uri.scheme);
489 984245ce 2021-06-23 op if ((p = calloc(1, sizeof(*p))) == NULL)
490 984245ce 2021-06-23 op err(1, "calloc");
492 984245ce 2021-06-23 op p->match_proto = proto;
493 8da4eaff 2021-07-25 op p->proto = PROTO_GEMINI;
495 984245ce 2021-06-23 op if ((p->host = strdup(uri.host)) == NULL)
496 984245ce 2021-06-23 op err(1, "strdup");
498 984245ce 2021-06-23 op if ((p->port = strdup(uri.port)) == NULL)
499 984245ce 2021-06-23 op err(1, "strdup");
501 984245ce 2021-06-23 op TAILQ_INSERT_HEAD(&proxies, p, proxies);
504 e3427d18 2021-06-25 op static interactivefn *
505 e3427d18 2021-06-25 op cmdname(const char *name)
507 e3427d18 2021-06-25 op struct cmd *cmd;
509 95a8c791 2021-08-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
510 e3427d18 2021-06-25 op if (!strcmp(cmd->cmd, name))
511 e3427d18 2021-06-25 op return cmd->fn;
518 e3427d18 2021-06-25 op bindkey(const char *map, const char *key, const char *cmd)
520 e3427d18 2021-06-25 op struct kmap *kmap;
521 e3427d18 2021-06-25 op interactivefn *fn;
523 e3427d18 2021-06-25 op if (!strcmp(map, "global-map"))
524 e3427d18 2021-06-25 op kmap = &global_map;
525 e3427d18 2021-06-25 op else if (!strcmp(map, "minibuffer-map"))
526 e3427d18 2021-06-25 op kmap = &minibuffer_map;
528 e3427d18 2021-06-25 op yyerror("unknown map: %s", map);
532 e3427d18 2021-06-25 op if ((fn = cmdname(cmd)) == NULL) {
533 e3427d18 2021-06-25 op yyerror("unknown cmd: %s", fn);
537 e3427d18 2021-06-25 op if (!kmap_define_key(kmap, key, fn))
538 e3427d18 2021-06-25 op yyerror("failed to bind %s %s %s", map, key, cmd);
542 21404dd9 2021-07-15 op do_parseconfig(const char *filename, int fonf)
544 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
546 69bdd906 2021-06-15 op err(1, "%s", filename);
550 69bdd906 2021-06-15 op path = filename;
552 69bdd906 2021-06-15 op fclose(yyfp);
553 69bdd906 2021-06-15 op if (parse_errors)
558 21404dd9 2021-07-15 op parseconfig(const char *filename, int fonf)
560 21404dd9 2021-07-15 op char altconf[PATH_MAX], *term;
562 21404dd9 2021-07-15 op /* load the given config file */
563 21404dd9 2021-07-15 op do_parseconfig(filename, fonf);
565 21404dd9 2021-07-15 op /* then try to load file-TERM */
567 21404dd9 2021-07-15 op if ((term = getenv("TERM")) == NULL)
570 21404dd9 2021-07-15 op strlcpy(altconf, filename, sizeof(altconf));
571 21404dd9 2021-07-15 op strlcat(altconf, "-", sizeof(altconf));
572 21404dd9 2021-07-15 op strlcat(altconf, term, sizeof(altconf));
574 21404dd9 2021-07-15 op do_parseconfig(altconf, 0);