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 b66e7d4d 2024-02-06 op #include "compat.h"
25 74a2587f 2021-06-21 op #include <assert.h>
26 69bdd906 2021-06-15 op #include <ctype.h>
27 69bdd906 2021-06-15 op #include <limits.h>
28 74a2587f 2021-06-21 op #include <ncurses.h>
29 69bdd906 2021-06-15 op #include <stdarg.h>
30 69bdd906 2021-06-15 op #include <stdio.h>
31 69bdd906 2021-06-15 op #include <stdlib.h>
32 69bdd906 2021-06-15 op #include <string.h>
34 b66e7d4d 2024-02-06 op #include "cmd.h"
35 2f51c5bd 2024-01-16 op #include "defaults.h"
36 46102ea3 2022-12-23 op #include "iri.h"
37 b66e7d4d 2024-02-06 op #include "keymap.h"
38 b66e7d4d 2024-02-06 op #include "telescope.h"
39 b66e7d4d 2024-02-06 op #include "utils.h"
40 3d89457c 2024-06-18 thomas.ad #include "xwrapper.h"
42 69bdd906 2021-06-15 op typedef struct {
50 69bdd906 2021-06-15 op #define YYSTYPE yystype
52 69e1d1b6 2021-06-15 op static char *current_style;
53 d89b86d6 2021-06-21 op static int color_type;
55 69bdd906 2021-06-15 op static const char *path;
59 69bdd906 2021-06-15 op int parse_errors = 0;
61 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
62 69bdd906 2021-06-15 op static int yylex(void);
63 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
64 86e7d7b4 2021-06-19 op static void setvari(char *, int);
65 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
66 2513365f 2024-01-16 op static void setvarb(char *, int);
67 74a2587f 2021-06-21 op static int colorname(const char *);
68 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
69 3198f30f 2021-08-26 op static int attrname(const char *);
70 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
71 984245ce 2021-06-23 op static void add_proxy(char *, char *);
72 e3427d18 2021-06-25 op static void bindkey(const char *, const char *, const char *);
73 21404dd9 2021-07-15 op static void do_parseconfig(const char *, int);
78 bd3e6837 2021-11-03 op %token BIND BG
81 bd3e6837 2021-11-03 op %token PRFX PROXY
82 bd3e6837 2021-11-03 op %token SET STYLE
86 2513365f 2024-01-16 op /* Sigh... they conflict with ncurses TRUE and FALSE */
87 2513365f 2024-01-16 op %token TOK_TRUE TOK_FALSE
89 9cc9d696 2021-11-03 op %token <str> STRING
90 9cc9d696 2021-11-03 op %token <num> NUMBER
91 2513365f 2024-01-16 op %type <num> bool
95 69bdd906 2021-06-15 op grammar : /* empty */
96 69bdd906 2021-06-15 op | grammar '\n'
97 69bdd906 2021-06-15 op | grammar rule '\n'
101 2513365f 2024-01-16 op bool : TOK_TRUE { $$ = 1; }
102 2513365f 2024-01-16 op | TOK_FALSE { $$ = 0; }
107 74a2587f 2021-06-21 op free(current_style);
108 74a2587f 2021-06-21 op current_style = NULL;
115 9cc9d696 2021-11-03 op set : SET STRING '=' STRING { setvars($2, $4); }
116 9cc9d696 2021-11-03 op | SET STRING '=' NUMBER { setvari($2, $4); }
117 2513365f 2024-01-16 op | SET STRING '=' bool { setvarb($2, $4); }
120 9cc9d696 2021-11-03 op style : STYLE STRING { current_style = $2; } stylespec ;
121 5c38fd5f 2021-08-30 op stylespec : styleopt | '{' optnl styleopts '}' ;
123 69bdd906 2021-06-15 op styleopts : /* empty */
124 0a53787c 2021-07-09 op | styleopts styleopt optnl
127 9cc9d696 2021-11-03 op styleopt : PRFX STRING { setprfx($2, $2); }
128 9cc9d696 2021-11-03 op | PRFX STRING STRING { setprfx($2, $3); }
129 9cc9d696 2021-11-03 op | BG { color_type = BG; } colorspec
130 9cc9d696 2021-11-03 op | FG { color_type = FG; } colorspec
134 9cc9d696 2021-11-03 op colorspec : STRING { setcolor($1, $1, $1); free($1); }
135 9cc9d696 2021-11-03 op | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
136 9cc9d696 2021-11-03 op | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
139 9cc9d696 2021-11-03 op attr : STRING { setattr($1, $1, $1); free($1); }
140 9cc9d696 2021-11-03 op | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
141 9cc9d696 2021-11-03 op | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
144 9cc9d696 2021-11-03 op bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
147 9cc9d696 2021-11-03 op unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
150 9cc9d696 2021-11-03 op proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
153 0a53787c 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
154 0a53787c 2021-07-09 op | /* empty */
160 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
164 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
165 69bdd906 2021-06-15 op va_start(va, fmt);
166 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
168 69bdd906 2021-06-15 op fprintf(stderr, "\n");
169 69bdd906 2021-06-15 op parse_errors++;
172 69bdd906 2021-06-15 op static struct keyword {
173 69bdd906 2021-06-15 op const char *word;
175 69bdd906 2021-06-15 op } keywords[] = {
176 9cc9d696 2021-11-03 op { "attr", ATTR },
177 9cc9d696 2021-11-03 op { "bg", BG },
178 9cc9d696 2021-11-03 op { "bind", BIND },
179 9cc9d696 2021-11-03 op { "cont", CONT },
180 2513365f 2024-01-16 op { "false", TOK_FALSE },
181 9cc9d696 2021-11-03 op { "fg", FG },
182 9cc9d696 2021-11-03 op { "prefix", PRFX },
183 9cc9d696 2021-11-03 op { "proxy", PROXY },
184 9cc9d696 2021-11-03 op { "set", SET },
185 9cc9d696 2021-11-03 op { "style", STYLE },
186 2513365f 2024-01-16 op { "true", TOK_TRUE },
187 9cc9d696 2021-11-03 op { "unbind", UNBIND },
188 9cc9d696 2021-11-03 op { "via", VIA },
194 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
195 69bdd906 2021-06-15 op const char *errstr;
196 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
200 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
203 69bdd906 2021-06-15 op /* skip whitespace first */
204 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
205 69bdd906 2021-06-15 op yylval.colno++;
207 69bdd906 2021-06-15 op /* check for special one-character constructions */
210 c86d164e 2021-06-24 op /* silently eat up any \r */
213 69bdd906 2021-06-15 op yylval.colno = 0;
214 69bdd906 2021-06-15 op yylval.lineno++;
215 69bdd906 2021-06-15 op /* fallthrough */
221 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
222 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
223 69bdd906 2021-06-15 op if (c == EOF)
225 69bdd906 2021-06-15 op yylval.colno = 0;
226 69bdd906 2021-06-15 op yylval.lineno++;
232 69bdd906 2021-06-15 op /* parsing next word */
233 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
236 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
237 69bdd906 2021-06-15 op yylval.colno+1);
241 69bdd906 2021-06-15 op escape = !escape;
246 6ba9e5a8 2021-06-24 op /* ignore \r here too */
250 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
251 69bdd906 2021-06-15 op yylval.colno+1);
252 69bdd906 2021-06-15 op if (escape) {
255 69bdd906 2021-06-15 op yylval.colno = 0;
256 69bdd906 2021-06-15 op yylval.lineno++;
261 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
262 69bdd906 2021-06-15 op yylval.colno);
264 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
273 69bdd906 2021-06-15 op if (!escape && !quotes)
277 69bdd906 2021-06-15 op if (!escape) {
278 69bdd906 2021-06-15 op quotes = !quotes;
279 69bdd906 2021-06-15 op if (quotes) {
281 69bdd906 2021-06-15 op qpos = yylval.colno;
287 69bdd906 2021-06-15 op if (p == ebuf) {
288 69bdd906 2021-06-15 op yyerror("line too long");
296 69bdd906 2021-06-15 op if (c != EOF)
297 69bdd906 2021-06-15 op ungetc(c, yyfp);
298 69bdd906 2021-06-15 op if (p == buf) {
300 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
301 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
302 69bdd906 2021-06-15 op * the main loop.
304 69bdd906 2021-06-15 op if (c == EOF)
306 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
309 69bdd906 2021-06-15 op if (!nonkw) {
310 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
311 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
312 69bdd906 2021-06-15 op return keywords[i].token;
316 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
317 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
318 69bdd906 2021-06-15 op if (errstr != NULL)
319 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
320 9cc9d696 2021-11-03 op return NUMBER;
322 3d89457c 2024-06-18 thomas.ad str = xstrdup(buf);
323 69bdd906 2021-06-15 op yylval.str = str;
324 9cc9d696 2021-11-03 op return STRING;
327 69bdd906 2021-06-15 op if (ferror(yyfp))
328 69bdd906 2021-06-15 op yyerror("input error reading config");
333 d89b86d6 2021-06-21 op setprfx(const char *prfx, const char *cont)
335 74a2587f 2021-06-21 op assert(current_style != NULL);
337 d89b86d6 2021-06-21 op if (!config_setprfx(current_style, prfx, cont))
338 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
342 86e7d7b4 2021-06-19 op setvari(char *var, int val)
345 2513365f 2024-01-16 op * For some time, fall back to a boolean as compat
346 2513365f 2024-01-16 op * with telescope 0.8 and previous.
348 2513365f 2024-01-16 op if (!config_setvari(var, val) &&
349 2513365f 2024-01-16 op !config_setvarb(var, val))
350 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
357 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
359 95a8c791 2021-08-26 op if (!config_setvars(var, val))
360 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
367 2513365f 2024-01-16 op setvarb(char *var, int val)
369 2513365f 2024-01-16 op if (!config_setvarb(var, val))
370 2513365f 2024-01-16 op yyerror("invalid variable or value: %s = %s",
371 2513365f 2024-01-16 op var, val ? "true" : "false");
377 74a2587f 2021-06-21 op colorname(const char *name)
380 74a2587f 2021-06-21 op const char *name;
382 74a2587f 2021-06-21 op } *i, colors[] = {
383 160abe04 2021-06-21 op { "default", -1 },
384 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
385 74a2587f 2021-06-21 op { "red", COLOR_RED },
386 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
387 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
388 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
389 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
390 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
391 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
394 27766d48 2021-06-22 op const char *errstr;
397 d89eb764 2022-04-24 op if (!strncmp(name, "colo", 4)) {
398 27766d48 2021-06-22 op /* people are strange */
399 d89eb764 2022-04-24 op if (!strncmp(name, "color", 5))
401 d89eb764 2022-04-24 op else if (!strncmp(name, "colour", 6))
406 27766d48 2021-06-22 op n = strtonum(name, 0, 256, &errstr);
407 27766d48 2021-06-22 op if (errstr != NULL)
408 27766d48 2021-06-22 op yyerror("color number is %s: %s", errstr, name);
412 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
413 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
414 74a2587f 2021-06-21 op return i->val;
418 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
423 d89b86d6 2021-06-21 op setcolor(const char *prfx, const char *line, const char *trail)
427 74a2587f 2021-06-21 op assert(current_style != NULL);
429 160abe04 2021-06-21 op p = colorname(prfx);
430 160abe04 2021-06-21 op l = colorname(line);
431 160abe04 2021-06-21 op t = colorname(trail);
433 9cc9d696 2021-11-03 op if (!config_setcolor(color_type == BG, current_style, p, l, t))
434 d0149485 2021-06-22 op yyerror("invalid style %s", current_style);
438 3198f30f 2021-08-26 op attrname(const char *n)
441 d0149485 2021-06-22 op const char *name;
442 d0149485 2021-06-22 op unsigned int val;
443 d0149485 2021-06-22 op } *i, attrs[] = {
444 d0149485 2021-06-22 op { "normal", A_NORMAL },
445 d0149485 2021-06-22 op { "standout", A_STANDOUT },
446 d0149485 2021-06-22 op { "underline", A_UNDERLINE },
447 d0149485 2021-06-22 op { "reverse", A_REVERSE },
448 d0149485 2021-06-22 op { "blink", A_BLINK },
449 d0149485 2021-06-22 op { "dim", A_DIM },
450 d0149485 2021-06-22 op { "bold", A_BOLD },
453 d0149485 2021-06-22 op int ret, found;
454 3198f30f 2021-08-26 op char *ap, *dup, *orig;
456 3d89457c 2024-06-18 thomas.ad dup = xstrdup(n);
461 3198f30f 2021-08-26 op while ((ap = strsep(&dup, ",")) != NULL) {
462 d0149485 2021-06-22 op if (*ap == '\0')
466 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
467 d0149485 2021-06-22 op if (strcmp(i->name, ap))
469 d0149485 2021-06-22 op ret |= i->val;
475 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
476 d0149485 2021-06-22 op ap, yylval.colno+1);
484 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
488 d0149485 2021-06-22 op assert(current_style != NULL);
490 d0149485 2021-06-22 op p = attrname(prfx);
491 d0149485 2021-06-22 op l = attrname(line);
492 d0149485 2021-06-22 op t = attrname(trail);
494 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
495 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
499 984245ce 2021-06-23 op add_proxy(char *proto, char *proxy)
501 46102ea3 2022-12-23 op static struct iri iri;
502 984245ce 2021-06-23 op struct proxy *p;
504 46102ea3 2022-12-23 op if (iri_parse(NULL, proxy, &iri) == -1) {
505 984245ce 2021-06-23 op yyerror("can't parse URL: %s", proxy);
509 4f147cdf 2022-12-25 op if ((iri.iri_flags & (IH_QUERY|IH_FRAGMENT)) ||
510 4f147cdf 2022-12-25 op iri.iri_path[0] != '\0') {
511 984245ce 2021-06-23 op yyerror("proxy url can't have path, query or fragments");
515 46102ea3 2022-12-23 op if (strcmp(iri.iri_scheme, "gemini")) {
516 46102ea3 2022-12-23 op yyerror("disallowed proxy protocol %s", iri.iri_scheme);
520 3d89457c 2024-06-18 thomas.ad p = xcalloc(1, sizeof(*p));
522 984245ce 2021-06-23 op p->match_proto = proto;
523 8da4eaff 2021-07-25 op p->proto = PROTO_GEMINI;
525 3d89457c 2024-06-18 thomas.ad p->host = xstrdup(iri.iri_host);
527 3d89457c 2024-06-18 thomas.ad p->port = xstrdup(iri.iri_portstr);
529 984245ce 2021-06-23 op TAILQ_INSERT_HEAD(&proxies, p, proxies);
532 e3427d18 2021-06-25 op static interactivefn *
533 e3427d18 2021-06-25 op cmdname(const char *name)
535 e3427d18 2021-06-25 op struct cmd *cmd;
537 95a8c791 2021-08-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
538 e3427d18 2021-06-25 op if (!strcmp(cmd->cmd, name))
539 e3427d18 2021-06-25 op return cmd->fn;
546 e3427d18 2021-06-25 op bindkey(const char *map, const char *key, const char *cmd)
548 e3427d18 2021-06-25 op struct kmap *kmap;
549 e3427d18 2021-06-25 op interactivefn *fn;
551 e3427d18 2021-06-25 op if (!strcmp(map, "global-map"))
552 e3427d18 2021-06-25 op kmap = &global_map;
553 e3427d18 2021-06-25 op else if (!strcmp(map, "minibuffer-map"))
554 e3427d18 2021-06-25 op kmap = &minibuffer_map;
556 e3427d18 2021-06-25 op yyerror("unknown map: %s", map);
560 e3427d18 2021-06-25 op if ((fn = cmdname(cmd)) == NULL) {
561 e3427d18 2021-06-25 op yyerror("unknown cmd: %s", fn);
565 e3427d18 2021-06-25 op if (!kmap_define_key(kmap, key, fn))
566 e3427d18 2021-06-25 op yyerror("failed to bind %s %s %s", map, key, cmd);
570 21404dd9 2021-07-15 op do_parseconfig(const char *filename, int fonf)
572 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
574 69bdd906 2021-06-15 op err(1, "%s", filename);
578 69bdd906 2021-06-15 op path = filename;
580 69bdd906 2021-06-15 op fclose(yyfp);
581 69bdd906 2021-06-15 op if (parse_errors)
586 21404dd9 2021-07-15 op parseconfig(const char *filename, int fonf)
588 21404dd9 2021-07-15 op char altconf[PATH_MAX], *term;
590 21404dd9 2021-07-15 op /* load the given config file */
591 21404dd9 2021-07-15 op do_parseconfig(filename, fonf);
593 21404dd9 2021-07-15 op /* then try to load file-TERM */
595 21404dd9 2021-07-15 op if ((term = getenv("TERM")) == NULL)
598 21404dd9 2021-07-15 op strlcpy(altconf, filename, sizeof(altconf));
599 21404dd9 2021-07-15 op strlcat(altconf, "-", sizeof(altconf));
600 21404dd9 2021-07-15 op strlcat(altconf, term, sizeof(altconf));
602 21404dd9 2021-07-15 op do_parseconfig(altconf, 0);