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"
41 69bdd906 2021-06-15 op typedef struct {
49 69bdd906 2021-06-15 op #define YYSTYPE yystype
51 69e1d1b6 2021-06-15 op static char *current_style;
52 d89b86d6 2021-06-21 op static int color_type;
54 69bdd906 2021-06-15 op static const char *path;
58 69bdd906 2021-06-15 op int parse_errors = 0;
60 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
61 69bdd906 2021-06-15 op static int yylex(void);
62 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
63 86e7d7b4 2021-06-19 op static void setvari(char *, int);
64 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
65 2513365f 2024-01-16 op static void setvarb(char *, int);
66 74a2587f 2021-06-21 op static int colorname(const char *);
67 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
68 3198f30f 2021-08-26 op static int attrname(const char *);
69 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
70 984245ce 2021-06-23 op static void add_proxy(char *, char *);
71 e3427d18 2021-06-25 op static void bindkey(const char *, const char *, const char *);
72 21404dd9 2021-07-15 op static void do_parseconfig(const char *, int);
77 bd3e6837 2021-11-03 op %token BIND BG
80 bd3e6837 2021-11-03 op %token PRFX PROXY
81 bd3e6837 2021-11-03 op %token SET STYLE
85 2513365f 2024-01-16 op /* Sigh... they conflict with ncurses TRUE and FALSE */
86 2513365f 2024-01-16 op %token TOK_TRUE TOK_FALSE
88 9cc9d696 2021-11-03 op %token <str> STRING
89 9cc9d696 2021-11-03 op %token <num> NUMBER
90 2513365f 2024-01-16 op %type <num> bool
94 69bdd906 2021-06-15 op grammar : /* empty */
95 69bdd906 2021-06-15 op | grammar '\n'
96 69bdd906 2021-06-15 op | grammar rule '\n'
100 2513365f 2024-01-16 op bool : TOK_TRUE { $$ = 1; }
101 2513365f 2024-01-16 op | TOK_FALSE { $$ = 0; }
106 74a2587f 2021-06-21 op free(current_style);
107 74a2587f 2021-06-21 op current_style = NULL;
114 9cc9d696 2021-11-03 op set : SET STRING '=' STRING { setvars($2, $4); }
115 9cc9d696 2021-11-03 op | SET STRING '=' NUMBER { setvari($2, $4); }
116 2513365f 2024-01-16 op | SET STRING '=' bool { setvarb($2, $4); }
119 9cc9d696 2021-11-03 op style : STYLE STRING { current_style = $2; } stylespec ;
120 5c38fd5f 2021-08-30 op stylespec : styleopt | '{' optnl styleopts '}' ;
122 69bdd906 2021-06-15 op styleopts : /* empty */
123 0a53787c 2021-07-09 op | styleopts styleopt optnl
126 9cc9d696 2021-11-03 op styleopt : PRFX STRING { setprfx($2, $2); }
127 9cc9d696 2021-11-03 op | PRFX STRING STRING { setprfx($2, $3); }
128 9cc9d696 2021-11-03 op | BG { color_type = BG; } colorspec
129 9cc9d696 2021-11-03 op | FG { color_type = FG; } colorspec
133 9cc9d696 2021-11-03 op colorspec : STRING { setcolor($1, $1, $1); free($1); }
134 9cc9d696 2021-11-03 op | STRING STRING { setcolor($1, $2, $1); free($1); free($2); }
135 9cc9d696 2021-11-03 op | STRING STRING STRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
138 9cc9d696 2021-11-03 op attr : STRING { setattr($1, $1, $1); free($1); }
139 9cc9d696 2021-11-03 op | STRING STRING { setattr($1, $2, $1); free($1); free($2); }
140 9cc9d696 2021-11-03 op | STRING STRING STRING { setattr($1, $2, $3); free($1); free($2); free($3); }
143 9cc9d696 2021-11-03 op bind : BIND STRING STRING STRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
146 9cc9d696 2021-11-03 op unbind : UNBIND STRING STRING { yyerror("TODO: unbind %s %s", $2, $3); }
149 9cc9d696 2021-11-03 op proxy : PROXY STRING VIA STRING { add_proxy($2, $4); free($4); }
152 0a53787c 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
153 0a53787c 2021-07-09 op | /* empty */
159 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
163 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
164 69bdd906 2021-06-15 op va_start(va, fmt);
165 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
167 69bdd906 2021-06-15 op fprintf(stderr, "\n");
168 69bdd906 2021-06-15 op parse_errors++;
171 69bdd906 2021-06-15 op static struct keyword {
172 69bdd906 2021-06-15 op const char *word;
174 69bdd906 2021-06-15 op } keywords[] = {
175 9cc9d696 2021-11-03 op { "attr", ATTR },
176 9cc9d696 2021-11-03 op { "bg", BG },
177 9cc9d696 2021-11-03 op { "bind", BIND },
178 9cc9d696 2021-11-03 op { "cont", CONT },
179 2513365f 2024-01-16 op { "false", TOK_FALSE },
180 9cc9d696 2021-11-03 op { "fg", FG },
181 9cc9d696 2021-11-03 op { "prefix", PRFX },
182 9cc9d696 2021-11-03 op { "proxy", PROXY },
183 9cc9d696 2021-11-03 op { "set", SET },
184 9cc9d696 2021-11-03 op { "style", STYLE },
185 2513365f 2024-01-16 op { "true", TOK_TRUE },
186 9cc9d696 2021-11-03 op { "unbind", UNBIND },
187 9cc9d696 2021-11-03 op { "via", VIA },
193 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
194 69bdd906 2021-06-15 op const char *errstr;
195 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
199 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
202 69bdd906 2021-06-15 op /* skip whitespace first */
203 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
204 69bdd906 2021-06-15 op yylval.colno++;
206 69bdd906 2021-06-15 op /* check for special one-character constructions */
209 c86d164e 2021-06-24 op /* silently eat up any \r */
212 69bdd906 2021-06-15 op yylval.colno = 0;
213 69bdd906 2021-06-15 op yylval.lineno++;
214 69bdd906 2021-06-15 op /* fallthrough */
220 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
221 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
222 69bdd906 2021-06-15 op if (c == EOF)
224 69bdd906 2021-06-15 op yylval.colno = 0;
225 69bdd906 2021-06-15 op yylval.lineno++;
231 69bdd906 2021-06-15 op /* parsing next word */
232 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
235 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
236 69bdd906 2021-06-15 op yylval.colno+1);
240 69bdd906 2021-06-15 op escape = !escape;
245 6ba9e5a8 2021-06-24 op /* ignore \r here too */
249 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
250 69bdd906 2021-06-15 op yylval.colno+1);
251 69bdd906 2021-06-15 op if (escape) {
254 69bdd906 2021-06-15 op yylval.colno = 0;
255 69bdd906 2021-06-15 op yylval.lineno++;
260 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
261 69bdd906 2021-06-15 op yylval.colno);
263 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
272 69bdd906 2021-06-15 op if (!escape && !quotes)
276 69bdd906 2021-06-15 op if (!escape) {
277 69bdd906 2021-06-15 op quotes = !quotes;
278 69bdd906 2021-06-15 op if (quotes) {
280 69bdd906 2021-06-15 op qpos = yylval.colno;
286 69bdd906 2021-06-15 op if (p == ebuf) {
287 69bdd906 2021-06-15 op yyerror("line too long");
295 69bdd906 2021-06-15 op if (c != EOF)
296 69bdd906 2021-06-15 op ungetc(c, yyfp);
297 69bdd906 2021-06-15 op if (p == buf) {
299 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
300 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
301 69bdd906 2021-06-15 op * the main loop.
303 69bdd906 2021-06-15 op if (c == EOF)
305 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
308 69bdd906 2021-06-15 op if (!nonkw) {
309 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
310 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
311 69bdd906 2021-06-15 op return keywords[i].token;
315 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
316 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
317 69bdd906 2021-06-15 op if (errstr != NULL)
318 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
319 9cc9d696 2021-11-03 op return NUMBER;
321 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
322 69bdd906 2021-06-15 op err(1, "%s", __func__);
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 3198f30f 2021-08-26 op if ((dup = strdup(n)) == NULL)
457 3198f30f 2021-08-26 op err(1, "strdup");
462 3198f30f 2021-08-26 op while ((ap = strsep(&dup, ",")) != NULL) {
463 d0149485 2021-06-22 op if (*ap == '\0')
467 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
468 d0149485 2021-06-22 op if (strcmp(i->name, ap))
470 d0149485 2021-06-22 op ret |= i->val;
476 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
477 d0149485 2021-06-22 op ap, yylval.colno+1);
485 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
489 d0149485 2021-06-22 op assert(current_style != NULL);
491 d0149485 2021-06-22 op p = attrname(prfx);
492 d0149485 2021-06-22 op l = attrname(line);
493 d0149485 2021-06-22 op t = attrname(trail);
495 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
496 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
500 984245ce 2021-06-23 op add_proxy(char *proto, char *proxy)
502 46102ea3 2022-12-23 op static struct iri iri;
503 984245ce 2021-06-23 op struct proxy *p;
505 46102ea3 2022-12-23 op if (iri_parse(NULL, proxy, &iri) == -1) {
506 984245ce 2021-06-23 op yyerror("can't parse URL: %s", proxy);
510 4f147cdf 2022-12-25 op if ((iri.iri_flags & (IH_QUERY|IH_FRAGMENT)) ||
511 4f147cdf 2022-12-25 op iri.iri_path[0] != '\0') {
512 984245ce 2021-06-23 op yyerror("proxy url can't have path, query or fragments");
516 46102ea3 2022-12-23 op if (strcmp(iri.iri_scheme, "gemini")) {
517 46102ea3 2022-12-23 op yyerror("disallowed proxy protocol %s", iri.iri_scheme);
521 984245ce 2021-06-23 op if ((p = calloc(1, sizeof(*p))) == NULL)
522 984245ce 2021-06-23 op err(1, "calloc");
524 984245ce 2021-06-23 op p->match_proto = proto;
525 8da4eaff 2021-07-25 op p->proto = PROTO_GEMINI;
527 46102ea3 2022-12-23 op if ((p->host = strdup(iri.iri_host)) == NULL)
528 984245ce 2021-06-23 op err(1, "strdup");
530 46102ea3 2022-12-23 op if ((p->port = strdup(iri.iri_portstr)) == NULL)
531 984245ce 2021-06-23 op err(1, "strdup");
533 984245ce 2021-06-23 op TAILQ_INSERT_HEAD(&proxies, p, proxies);
536 e3427d18 2021-06-25 op static interactivefn *
537 e3427d18 2021-06-25 op cmdname(const char *name)
539 e3427d18 2021-06-25 op struct cmd *cmd;
541 95a8c791 2021-08-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
542 e3427d18 2021-06-25 op if (!strcmp(cmd->cmd, name))
543 e3427d18 2021-06-25 op return cmd->fn;
550 e3427d18 2021-06-25 op bindkey(const char *map, const char *key, const char *cmd)
552 e3427d18 2021-06-25 op struct kmap *kmap;
553 e3427d18 2021-06-25 op interactivefn *fn;
555 e3427d18 2021-06-25 op if (!strcmp(map, "global-map"))
556 e3427d18 2021-06-25 op kmap = &global_map;
557 e3427d18 2021-06-25 op else if (!strcmp(map, "minibuffer-map"))
558 e3427d18 2021-06-25 op kmap = &minibuffer_map;
560 e3427d18 2021-06-25 op yyerror("unknown map: %s", map);
564 e3427d18 2021-06-25 op if ((fn = cmdname(cmd)) == NULL) {
565 e3427d18 2021-06-25 op yyerror("unknown cmd: %s", fn);
569 e3427d18 2021-06-25 op if (!kmap_define_key(kmap, key, fn))
570 e3427d18 2021-06-25 op yyerror("failed to bind %s %s %s", map, key, cmd);
574 21404dd9 2021-07-15 op do_parseconfig(const char *filename, int fonf)
576 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
578 69bdd906 2021-06-15 op err(1, "%s", filename);
582 69bdd906 2021-06-15 op path = filename;
584 69bdd906 2021-06-15 op fclose(yyfp);
585 69bdd906 2021-06-15 op if (parse_errors)
590 21404dd9 2021-07-15 op parseconfig(const char *filename, int fonf)
592 21404dd9 2021-07-15 op char altconf[PATH_MAX], *term;
594 21404dd9 2021-07-15 op /* load the given config file */
595 21404dd9 2021-07-15 op do_parseconfig(filename, fonf);
597 21404dd9 2021-07-15 op /* then try to load file-TERM */
599 21404dd9 2021-07-15 op if ((term = getenv("TERM")) == NULL)
602 21404dd9 2021-07-15 op strlcpy(altconf, filename, sizeof(altconf));
603 21404dd9 2021-07-15 op strlcat(altconf, "-", sizeof(altconf));
604 21404dd9 2021-07-15 op strlcat(altconf, term, sizeof(altconf));
606 21404dd9 2021-07-15 op do_parseconfig(altconf, 0);