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"
25 9d65b1d9 2022-01-11 op #include "utils.h"
27 74a2587f 2021-06-21 op #include <assert.h>
28 69bdd906 2021-06-15 op #include <ctype.h>
29 69bdd906 2021-06-15 op #include <limits.h>
30 74a2587f 2021-06-21 op #include <ncurses.h>
31 69bdd906 2021-06-15 op #include <stdarg.h>
32 69bdd906 2021-06-15 op #include <stdio.h>
33 69bdd906 2021-06-15 op #include <stdlib.h>
34 69bdd906 2021-06-15 op #include <string.h>
36 46102ea3 2022-12-23 op #include "iri.h"
38 69bdd906 2021-06-15 op typedef struct {
46 69bdd906 2021-06-15 op #define YYSTYPE yystype
48 69e1d1b6 2021-06-15 op static char *current_style;
49 d89b86d6 2021-06-21 op static int color_type;
51 69bdd906 2021-06-15 op static const char *path;
55 69bdd906 2021-06-15 op int parse_errors = 0;
57 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
58 69bdd906 2021-06-15 op static int yylex(void);
59 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
60 86e7d7b4 2021-06-19 op static void setvari(char *, int);
61 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
62 74a2587f 2021-06-21 op static int colorname(const char *);
63 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
64 3198f30f 2021-08-26 op static int attrname(const char *);
65 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
66 984245ce 2021-06-23 op static void add_proxy(char *, char *);
67 e3427d18 2021-06-25 op static void bindkey(const char *, const char *, const char *);
68 21404dd9 2021-07-15 op static void do_parseconfig(const char *, int);
73 bd3e6837 2021-11-03 op %token BIND BG
76 bd3e6837 2021-11-03 op %token PRFX PROXY
77 bd3e6837 2021-11-03 op %token SET STYLE
81 9cc9d696 2021-11-03 op %token <str> STRING
82 9cc9d696 2021-11-03 op %token <num> NUMBER
86 69bdd906 2021-06-15 op grammar : /* empty */
87 69bdd906 2021-06-15 op | grammar '\n'
88 69bdd906 2021-06-15 op | grammar rule '\n'
94 74a2587f 2021-06-21 op free(current_style);
95 74a2587f 2021-06-21 op current_style = NULL;
102 9cc9d696 2021-11-03 op set : SET STRING '=' STRING { setvars($2, $4); }
103 9cc9d696 2021-11-03 op | SET STRING '=' NUMBER { setvari($2, $4); }
106 9cc9d696 2021-11-03 op style : STYLE STRING { current_style = $2; } stylespec ;
107 5c38fd5f 2021-08-30 op stylespec : styleopt | '{' optnl styleopts '}' ;
109 69bdd906 2021-06-15 op styleopts : /* empty */
110 0a53787c 2021-07-09 op | styleopts styleopt optnl
113 9cc9d696 2021-11-03 op styleopt : PRFX STRING { setprfx($2, $2); }
114 9cc9d696 2021-11-03 op | PRFX STRING STRING { setprfx($2, $3); }
115 9cc9d696 2021-11-03 op | BG { color_type = BG; } colorspec
116 9cc9d696 2021-11-03 op | FG { color_type = FG; } colorspec
120 9cc9d696 2021-11-03 op colorspec : STRING { setcolor($1, $1, $1); free($1); }
121 9cc9d696 2021-11-03 op | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
122 9cc9d696 2021-11-03 op | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
125 9cc9d696 2021-11-03 op attr : STRING { setattr($1, $1, $1); free($1); }
126 9cc9d696 2021-11-03 op | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
127 9cc9d696 2021-11-03 op | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
130 9cc9d696 2021-11-03 op bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
133 9cc9d696 2021-11-03 op unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
136 9cc9d696 2021-11-03 op proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
139 0a53787c 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
140 0a53787c 2021-07-09 op | /* empty */
146 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
150 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
151 69bdd906 2021-06-15 op va_start(va, fmt);
152 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
154 69bdd906 2021-06-15 op fprintf(stderr, "\n");
155 69bdd906 2021-06-15 op parse_errors++;
158 69bdd906 2021-06-15 op static struct keyword {
159 69bdd906 2021-06-15 op const char *word;
161 69bdd906 2021-06-15 op } keywords[] = {
162 9cc9d696 2021-11-03 op { "attr", ATTR },
163 9cc9d696 2021-11-03 op { "bg", BG },
164 9cc9d696 2021-11-03 op { "bind", BIND },
165 9cc9d696 2021-11-03 op { "cont", CONT },
166 9cc9d696 2021-11-03 op { "fg", FG },
167 9cc9d696 2021-11-03 op { "prefix", PRFX },
168 9cc9d696 2021-11-03 op { "proxy", PROXY },
169 9cc9d696 2021-11-03 op { "set", SET },
170 9cc9d696 2021-11-03 op { "style", STYLE },
171 9cc9d696 2021-11-03 op { "unbind", UNBIND },
172 9cc9d696 2021-11-03 op { "via", VIA },
178 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
179 69bdd906 2021-06-15 op const char *errstr;
180 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
184 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
187 69bdd906 2021-06-15 op /* skip whitespace first */
188 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
189 69bdd906 2021-06-15 op yylval.colno++;
191 69bdd906 2021-06-15 op /* check for special one-character constructions */
194 c86d164e 2021-06-24 op /* silently eat up any \r */
197 69bdd906 2021-06-15 op yylval.colno = 0;
198 69bdd906 2021-06-15 op yylval.lineno++;
199 69bdd906 2021-06-15 op /* fallthrough */
205 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
206 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
207 69bdd906 2021-06-15 op if (c == EOF)
209 69bdd906 2021-06-15 op yylval.colno = 0;
210 69bdd906 2021-06-15 op yylval.lineno++;
216 69bdd906 2021-06-15 op /* parsing next word */
217 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
220 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
221 69bdd906 2021-06-15 op yylval.colno+1);
225 69bdd906 2021-06-15 op escape = !escape;
230 6ba9e5a8 2021-06-24 op /* ignore \r here too */
234 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
235 69bdd906 2021-06-15 op yylval.colno+1);
236 69bdd906 2021-06-15 op if (escape) {
239 69bdd906 2021-06-15 op yylval.colno = 0;
240 69bdd906 2021-06-15 op yylval.lineno++;
245 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
246 69bdd906 2021-06-15 op yylval.colno);
248 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
257 69bdd906 2021-06-15 op if (!escape && !quotes)
261 69bdd906 2021-06-15 op if (!escape) {
262 69bdd906 2021-06-15 op quotes = !quotes;
263 69bdd906 2021-06-15 op if (quotes) {
265 69bdd906 2021-06-15 op qpos = yylval.colno;
271 69bdd906 2021-06-15 op if (p == ebuf) {
272 69bdd906 2021-06-15 op yyerror("line too long");
280 69bdd906 2021-06-15 op if (c != EOF)
281 69bdd906 2021-06-15 op ungetc(c, yyfp);
282 69bdd906 2021-06-15 op if (p == buf) {
284 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
285 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
286 69bdd906 2021-06-15 op * the main loop.
288 69bdd906 2021-06-15 op if (c == EOF)
290 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
293 69bdd906 2021-06-15 op if (!nonkw) {
294 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
295 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
296 69bdd906 2021-06-15 op return keywords[i].token;
300 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
301 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
302 69bdd906 2021-06-15 op if (errstr != NULL)
303 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
304 9cc9d696 2021-11-03 op return NUMBER;
306 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
307 69bdd906 2021-06-15 op err(1, "%s", __func__);
308 69bdd906 2021-06-15 op yylval.str = str;
309 9cc9d696 2021-11-03 op return STRING;
312 69bdd906 2021-06-15 op if (ferror(yyfp))
313 69bdd906 2021-06-15 op yyerror("input error reading config");
318 d89b86d6 2021-06-21 op setprfx(const char *prfx, const char *cont)
320 74a2587f 2021-06-21 op assert(current_style != NULL);
322 d89b86d6 2021-06-21 op if (!config_setprfx(current_style, prfx, cont))
323 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
327 86e7d7b4 2021-06-19 op setvari(char *var, int val)
329 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
330 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
337 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
339 95a8c791 2021-08-26 op if (!config_setvars(var, val))
340 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
347 74a2587f 2021-06-21 op colorname(const char *name)
350 74a2587f 2021-06-21 op const char *name;
352 74a2587f 2021-06-21 op } *i, colors[] = {
353 160abe04 2021-06-21 op { "default", -1 },
354 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
355 74a2587f 2021-06-21 op { "red", COLOR_RED },
356 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
357 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
358 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
359 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
360 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
361 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
364 27766d48 2021-06-22 op const char *errstr;
367 d89eb764 2022-04-24 op if (!strncmp(name, "colo", 4)) {
368 27766d48 2021-06-22 op /* people are strange */
369 d89eb764 2022-04-24 op if (!strncmp(name, "color", 5))
371 d89eb764 2022-04-24 op else if (!strncmp(name, "colour", 6))
376 27766d48 2021-06-22 op n = strtonum(name, 0, 256, &errstr);
377 27766d48 2021-06-22 op if (errstr != NULL)
378 27766d48 2021-06-22 op yyerror("color number is %s: %s", errstr, name);
382 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
383 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
384 74a2587f 2021-06-21 op return i->val;
388 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
393 d89b86d6 2021-06-21 op setcolor(const char *prfx, const char *line, const char *trail)
397 74a2587f 2021-06-21 op assert(current_style != NULL);
399 160abe04 2021-06-21 op p = colorname(prfx);
400 160abe04 2021-06-21 op l = colorname(line);
401 160abe04 2021-06-21 op t = colorname(trail);
403 9cc9d696 2021-11-03 op if (!config_setcolor(color_type == BG, current_style, p, l, t))
404 d0149485 2021-06-22 op yyerror("invalid style %s", current_style);
408 3198f30f 2021-08-26 op attrname(const char *n)
411 d0149485 2021-06-22 op const char *name;
412 d0149485 2021-06-22 op unsigned int val;
413 d0149485 2021-06-22 op } *i, attrs[] = {
414 d0149485 2021-06-22 op { "normal", A_NORMAL },
415 d0149485 2021-06-22 op { "standout", A_STANDOUT },
416 d0149485 2021-06-22 op { "underline", A_UNDERLINE },
417 d0149485 2021-06-22 op { "reverse", A_REVERSE },
418 d0149485 2021-06-22 op { "blink", A_BLINK },
419 d0149485 2021-06-22 op { "dim", A_DIM },
420 d0149485 2021-06-22 op { "bold", A_BOLD },
423 d0149485 2021-06-22 op int ret, found;
424 3198f30f 2021-08-26 op char *ap, *dup, *orig;
426 3198f30f 2021-08-26 op if ((dup = strdup(n)) == NULL)
427 3198f30f 2021-08-26 op err(1, "strdup");
432 3198f30f 2021-08-26 op while ((ap = strsep(&dup, ",")) != NULL) {
433 d0149485 2021-06-22 op if (*ap == '\0')
437 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
438 d0149485 2021-06-22 op if (strcmp(i->name, ap))
440 d0149485 2021-06-22 op ret |= i->val;
446 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
447 d0149485 2021-06-22 op ap, yylval.colno+1);
455 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
459 d0149485 2021-06-22 op assert(current_style != NULL);
461 d0149485 2021-06-22 op p = attrname(prfx);
462 d0149485 2021-06-22 op l = attrname(line);
463 d0149485 2021-06-22 op t = attrname(trail);
465 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
466 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
470 984245ce 2021-06-23 op add_proxy(char *proto, char *proxy)
472 46102ea3 2022-12-23 op static struct iri iri;
473 984245ce 2021-06-23 op struct proxy *p;
475 46102ea3 2022-12-23 op if (iri_parse(NULL, proxy, &iri) == -1) {
476 984245ce 2021-06-23 op yyerror("can't parse URL: %s", proxy);
480 46102ea3 2022-12-23 op if (iri.iri_flags & (IH_PATH|IH_QUERY)) {
481 984245ce 2021-06-23 op yyerror("proxy url can't have path, query or fragments");
485 46102ea3 2022-12-23 op if (strcmp(iri.iri_scheme, "gemini")) {
486 46102ea3 2022-12-23 op yyerror("disallowed proxy protocol %s", iri.iri_scheme);
490 984245ce 2021-06-23 op if ((p = calloc(1, sizeof(*p))) == NULL)
491 984245ce 2021-06-23 op err(1, "calloc");
493 984245ce 2021-06-23 op p->match_proto = proto;
494 8da4eaff 2021-07-25 op p->proto = PROTO_GEMINI;
496 46102ea3 2022-12-23 op if ((p->host = strdup(iri.iri_host)) == NULL)
497 984245ce 2021-06-23 op err(1, "strdup");
499 46102ea3 2022-12-23 op if ((p->port = strdup(iri.iri_portstr)) == NULL)
500 984245ce 2021-06-23 op err(1, "strdup");
502 984245ce 2021-06-23 op TAILQ_INSERT_HEAD(&proxies, p, proxies);
505 e3427d18 2021-06-25 op static interactivefn *
506 e3427d18 2021-06-25 op cmdname(const char *name)
508 e3427d18 2021-06-25 op struct cmd *cmd;
510 95a8c791 2021-08-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
511 e3427d18 2021-06-25 op if (!strcmp(cmd->cmd, name))
512 e3427d18 2021-06-25 op return cmd->fn;
519 e3427d18 2021-06-25 op bindkey(const char *map, const char *key, const char *cmd)
521 e3427d18 2021-06-25 op struct kmap *kmap;
522 e3427d18 2021-06-25 op interactivefn *fn;
524 e3427d18 2021-06-25 op if (!strcmp(map, "global-map"))
525 e3427d18 2021-06-25 op kmap = &global_map;
526 e3427d18 2021-06-25 op else if (!strcmp(map, "minibuffer-map"))
527 e3427d18 2021-06-25 op kmap = &minibuffer_map;
529 e3427d18 2021-06-25 op yyerror("unknown map: %s", map);
533 e3427d18 2021-06-25 op if ((fn = cmdname(cmd)) == NULL) {
534 e3427d18 2021-06-25 op yyerror("unknown cmd: %s", fn);
538 e3427d18 2021-06-25 op if (!kmap_define_key(kmap, key, fn))
539 e3427d18 2021-06-25 op yyerror("failed to bind %s %s %s", map, key, cmd);
543 21404dd9 2021-07-15 op do_parseconfig(const char *filename, int fonf)
545 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
547 69bdd906 2021-06-15 op err(1, "%s", filename);
551 69bdd906 2021-06-15 op path = filename;
553 69bdd906 2021-06-15 op fclose(yyfp);
554 69bdd906 2021-06-15 op if (parse_errors)
559 21404dd9 2021-07-15 op parseconfig(const char *filename, int fonf)
561 21404dd9 2021-07-15 op char altconf[PATH_MAX], *term;
563 21404dd9 2021-07-15 op /* load the given config file */
564 21404dd9 2021-07-15 op do_parseconfig(filename, fonf);
566 21404dd9 2021-07-15 op /* then try to load file-TERM */
568 21404dd9 2021-07-15 op if ((term = getenv("TERM")) == NULL)
571 21404dd9 2021-07-15 op strlcpy(altconf, filename, sizeof(altconf));
572 21404dd9 2021-07-15 op strlcat(altconf, "-", sizeof(altconf));
573 21404dd9 2021-07-15 op strlcat(altconf, term, sizeof(altconf));
575 21404dd9 2021-07-15 op do_parseconfig(altconf, 0);