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 69bdd906 2021-06-15 op #include "telescope.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 69bdd906 2021-06-15 op typedef struct {
42 69bdd906 2021-06-15 op #define YYSTYPE yystype
44 69e1d1b6 2021-06-15 op static char *current_style;
45 d89b86d6 2021-06-21 op static int color_type;
47 69bdd906 2021-06-15 op static const char *path;
51 69bdd906 2021-06-15 op int parse_errors = 0;
53 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
54 69bdd906 2021-06-15 op static int yylex(void);
55 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
56 86e7d7b4 2021-06-19 op static void setvari(char *, int);
57 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
58 74a2587f 2021-06-21 op static int colorname(const char *);
59 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
60 d0149485 2021-06-22 op static int attrname(char *);
61 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
66 d0149485 2021-06-22 op %token TSTYLE TPRFX TCONT TBG TFG TATTR
67 69bdd906 2021-06-15 op %token TBIND TUNBIND
69 69bdd906 2021-06-15 op %token <str> TSTRING
70 69bdd906 2021-06-15 op %token <num> TNUMBER
74 69bdd906 2021-06-15 op grammar : /* empty */
75 69bdd906 2021-06-15 op | grammar '\n'
76 69bdd906 2021-06-15 op | grammar rule '\n'
82 74a2587f 2021-06-21 op free(current_style);
83 74a2587f 2021-06-21 op current_style = NULL;
89 86e7d7b4 2021-06-19 op set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
90 86e7d7b4 2021-06-19 op | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
93 74a2587f 2021-06-21 op style : TSTYLE TSTRING { current_style = $2; } stylespec ;
94 74a2587f 2021-06-21 op stylespec : styleopt | '{' styleopts '}' ;
96 69bdd906 2021-06-15 op styleopts : /* empty */
97 69bdd906 2021-06-15 op | styleopts '\n'
98 69bdd906 2021-06-15 op | styleopts styleopt '\n'
101 d89b86d6 2021-06-21 op styleopt : TPRFX TSTRING { setprfx($2, $2); }
102 d89b86d6 2021-06-21 op | TPRFX TSTRING TSTRING { setprfx($2, $2); }
103 d89b86d6 2021-06-21 op | TBG { color_type = TBG; } colorspec
104 d89b86d6 2021-06-21 op | TFG { color_type = TFG; } colorspec
108 d89b86d6 2021-06-21 op colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
109 d89b86d6 2021-06-21 op | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
110 d89b86d6 2021-06-21 op | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
113 d0149485 2021-06-22 op attr : TSTRING { setattr($1, $1, $1); free($1); }
114 d0149485 2021-06-22 op | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
115 d0149485 2021-06-22 op | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
118 69bdd906 2021-06-15 op bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
121 69bdd906 2021-06-15 op unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
127 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
131 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
132 69bdd906 2021-06-15 op va_start(va, fmt);
133 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
135 69bdd906 2021-06-15 op fprintf(stderr, "\n");
136 69bdd906 2021-06-15 op parse_errors++;
139 69bdd906 2021-06-15 op static struct keyword {
140 69bdd906 2021-06-15 op const char *word;
142 69bdd906 2021-06-15 op } keywords[] = {
143 69bdd906 2021-06-15 op { "set", TSET },
144 69bdd906 2021-06-15 op { "style", TSTYLE },
145 69bdd906 2021-06-15 op { "prefix", TPRFX },
146 69bdd906 2021-06-15 op { "cont", TCONT },
147 74a2587f 2021-06-21 op { "bg", TBG },
148 74a2587f 2021-06-21 op { "fg", TFG },
149 69bdd906 2021-06-15 op { "attr", TATTR },
150 69bdd906 2021-06-15 op { "bind", TBIND },
151 69bdd906 2021-06-15 op { "unbind", TUNBIND },
157 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
158 69bdd906 2021-06-15 op const char *errstr;
159 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
163 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
166 69bdd906 2021-06-15 op /* skip whitespace first */
167 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
168 69bdd906 2021-06-15 op yylval.colno++;
170 69bdd906 2021-06-15 op /* check for special one-character constructions */
173 69bdd906 2021-06-15 op yylval.colno = 0;
174 69bdd906 2021-06-15 op yylval.lineno++;
175 69bdd906 2021-06-15 op /* fallthrough */
181 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
182 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
183 69bdd906 2021-06-15 op if (c == EOF)
185 69bdd906 2021-06-15 op yylval.colno = 0;
186 69bdd906 2021-06-15 op yylval.lineno++;
192 69bdd906 2021-06-15 op /* parsing next word */
193 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
196 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
197 69bdd906 2021-06-15 op yylval.colno+1);
201 69bdd906 2021-06-15 op escape = !escape;
207 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
208 69bdd906 2021-06-15 op yylval.colno+1);
209 69bdd906 2021-06-15 op if (escape) {
212 69bdd906 2021-06-15 op yylval.colno = 0;
213 69bdd906 2021-06-15 op yylval.lineno++;
218 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
219 69bdd906 2021-06-15 op yylval.colno);
221 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
230 69bdd906 2021-06-15 op if (!escape && !quotes)
234 69bdd906 2021-06-15 op if (!escape) {
235 69bdd906 2021-06-15 op quotes = !quotes;
236 69bdd906 2021-06-15 op if (quotes) {
238 69bdd906 2021-06-15 op qpos = yylval.colno;
244 69bdd906 2021-06-15 op if (p == ebuf) {
245 69bdd906 2021-06-15 op yyerror("line too long");
253 69bdd906 2021-06-15 op if (c != EOF)
254 69bdd906 2021-06-15 op ungetc(c, yyfp);
255 69bdd906 2021-06-15 op if (p == buf) {
257 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
258 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
259 69bdd906 2021-06-15 op * the main loop.
261 69bdd906 2021-06-15 op if (c == EOF)
263 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
266 69bdd906 2021-06-15 op if (!nonkw) {
267 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
268 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
269 69bdd906 2021-06-15 op return keywords[i].token;
273 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
274 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
275 69bdd906 2021-06-15 op if (errstr != NULL)
276 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
277 69bdd906 2021-06-15 op return TNUMBER;
279 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
280 69bdd906 2021-06-15 op err(1, "%s", __func__);
281 69bdd906 2021-06-15 op yylval.str = str;
282 69bdd906 2021-06-15 op return TSTRING;
285 69bdd906 2021-06-15 op if (ferror(yyfp))
286 69bdd906 2021-06-15 op yyerror("input error reading config");
291 d89b86d6 2021-06-21 op setprfx(const char *prfx, const char *cont)
293 74a2587f 2021-06-21 op assert(current_style != NULL);
295 d89b86d6 2021-06-21 op if (!config_setprfx(current_style, prfx, cont))
296 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
300 86e7d7b4 2021-06-19 op setvari(char *var, int val)
302 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
303 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
310 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
312 86e7d7b4 2021-06-19 op if (!config_setvars(var, val))
313 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
320 74a2587f 2021-06-21 op colorname(const char *name)
323 74a2587f 2021-06-21 op const char *name;
325 74a2587f 2021-06-21 op } *i, colors[] = {
326 160abe04 2021-06-21 op { "default", -1 },
327 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
328 74a2587f 2021-06-21 op { "red", COLOR_RED },
329 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
330 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
331 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
332 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
333 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
334 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
337 27766d48 2021-06-22 op const char *errstr;
340 27766d48 2021-06-22 op if (has_prefix(name, "colo")) {
341 27766d48 2021-06-22 op /* people are strange */
342 27766d48 2021-06-22 op if (has_prefix(name, "color"))
344 27766d48 2021-06-22 op else if (has_prefix(name, "colour"))
349 27766d48 2021-06-22 op n = strtonum(name, 0, 256, &errstr);
350 27766d48 2021-06-22 op if (errstr != NULL)
351 27766d48 2021-06-22 op yyerror("color number is %s: %s", errstr, name);
355 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
356 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
357 74a2587f 2021-06-21 op return i->val;
361 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
366 d89b86d6 2021-06-21 op setcolor(const char *prfx, const char *line, const char *trail)
370 74a2587f 2021-06-21 op assert(current_style != NULL);
372 160abe04 2021-06-21 op p = colorname(prfx);
373 160abe04 2021-06-21 op l = colorname(line);
374 160abe04 2021-06-21 op t = colorname(trail);
376 d89b86d6 2021-06-21 op if (!config_setcolor(color_type == TBG, current_style, p, l, t))
377 d0149485 2021-06-22 op yyerror("invalid style %s", current_style);
381 d0149485 2021-06-22 op attrname(char *n)
384 d0149485 2021-06-22 op const char *name;
385 d0149485 2021-06-22 op unsigned int val;
386 d0149485 2021-06-22 op } *i, attrs[] = {
387 d0149485 2021-06-22 op { "normal", A_NORMAL },
388 d0149485 2021-06-22 op { "standout", A_STANDOUT },
389 d0149485 2021-06-22 op { "underline", A_UNDERLINE },
390 d0149485 2021-06-22 op { "reverse", A_REVERSE },
391 d0149485 2021-06-22 op { "blink", A_BLINK },
392 d0149485 2021-06-22 op { "dim", A_DIM },
393 d0149485 2021-06-22 op { "bold", A_BOLD },
396 d0149485 2021-06-22 op int ret, found;
400 d0149485 2021-06-22 op while ((ap = strsep(&n, ",")) != NULL) {
401 d0149485 2021-06-22 op if (*ap == '\0')
405 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
406 d0149485 2021-06-22 op if (strcmp(i->name, ap))
408 d0149485 2021-06-22 op ret |= i->val;
414 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
415 d0149485 2021-06-22 op ap, yylval.colno+1);
422 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
426 d0149485 2021-06-22 op assert(current_style != NULL);
428 d0149485 2021-06-22 op p = attrname(prfx);
429 d0149485 2021-06-22 op l = attrname(line);
430 d0149485 2021-06-22 op t = attrname(trail);
432 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
433 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
437 69bdd906 2021-06-15 op parseconfig(const char *filename, int fonf)
439 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
441 69bdd906 2021-06-15 op err(1, "%s", filename);
445 69bdd906 2021-06-15 op path = filename;
447 69bdd906 2021-06-15 op fclose(yyfp);
448 69bdd906 2021-06-15 op if (parse_errors)