2 * Much of the design is taken from doas (parse.y rev 1.29)
4 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 #include "telescope.h"
42 #define YYSTYPE yystype
44 static char *current_style;
45 static int color_type;
47 static const char *path;
53 static void yyerror(const char *, ...);
54 static int yylex(void);
55 static void setprfx(const char *, const char *);
56 static void setvari(char *, int);
57 static void setvars(char *, char *);
58 static int colorname(const char *);
59 static void setcolor(const char *, const char *, const char *);
60 static int attrname(char *);
61 static void setattr(char *, char *, char *);
66 %token TSTYLE TPRFX TCONT TBG TFG TATTR
89 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
90 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
93 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
94 stylespec : styleopt | '{' styleopts '}' ;
96 styleopts : /* empty */
98 | styleopts styleopt '\n'
101 styleopt : TPRFX TSTRING { setprfx($2, $2); }
102 | TPRFX TSTRING TSTRING { setprfx($2, $2); }
103 | TBG { color_type = TBG; } colorspec
104 | TFG { color_type = TFG; } colorspec
108 colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
109 | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
110 | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
113 attr : TSTRING { setattr($1, $1, $1); free($1); }
114 | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
115 | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
118 bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
121 unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
127 yyerror(const char *fmt, ...)
131 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
133 vfprintf(stderr, fmt, va);
135 fprintf(stderr, "\n");
139 static struct keyword {
151 { "unbind", TUNBIND },
157 char buf[1024], *ebuf, *p, *str;
159 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
163 ebuf = buf + sizeof(buf);
166 /* skip whitespace first */
167 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
170 /* check for special one-character constructions */
181 /* skip comments; NUL is allowed; no continuation */
182 while ((c = getc(yyfp)) != '\n')
192 /* parsing next word */
193 for (;; c = getc(yyfp), yylval.colno++) {
196 yyerror("unallowed character NULL in column %d",
207 yyerror("unterminated quotes in column %d",
218 yyerror("unterminated escape in column %d",
221 yyerror("unterminated quotes in column %d",
230 if (!escape && !quotes)
245 yyerror("line too long");
257 * There could be a number of reason for empty buffer,
258 * and we handle all of them here, to avoid cluttering
263 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
267 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
268 if (strcmp(buf, keywords[i].word) == 0)
269 return keywords[i].token;
273 if (!nonkw && (c == '-' || isdigit(c))) {
274 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
276 yyerror("number is %s: %s", errstr, buf);
279 if ((str = strdup(buf)) == NULL)
280 err(1, "%s", __func__);
286 yyerror("input error reading config");
291 setprfx(const char *prfx, const char *cont)
293 assert(current_style != NULL);
295 if (!config_setprfx(current_style, prfx, cont))
296 yyerror("invalid style %s", current_style);
300 setvari(char *var, int val)
302 if (!config_setvari(var, val))
303 yyerror("invalid variable or value: %s = %d",
310 setvars(char *var, char *val)
312 if (!config_setvars(var, val))
313 yyerror("invalid variable or value: %s = \"%s\"",
320 colorname(const char *name)
327 { "black", COLOR_BLACK },
328 { "red", COLOR_RED },
329 { "green", COLOR_GREEN },
330 { "yellow", COLOR_YELLOW },
331 { "blue", COLOR_BLUE },
332 { "magenta", COLOR_MAGENTA },
333 { "cyan", COLOR_CYAN },
334 { "white", COLOR_WHITE },
340 if (has_prefix(name, "colo")) {
341 /* people are strange */
342 if (has_prefix(name, "color"))
344 else if (has_prefix(name, "colour"))
349 n = strtonum(name, 0, 256, &errstr);
351 yyerror("color number is %s: %s", errstr, name);
355 for (i = colors; i->name != NULL; ++i) {
356 if (!strcmp(i->name, name))
361 yyerror("unknown color name \"%s\"", name);
366 setcolor(const char *prfx, const char *line, const char *trail)
370 assert(current_style != NULL);
374 t = colorname(trail);
376 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
377 yyerror("invalid style %s", current_style);
387 { "normal", A_NORMAL },
388 { "standout", A_STANDOUT },
389 { "underline", A_UNDERLINE },
390 { "reverse", A_REVERSE },
391 { "blink", A_BLINK },
400 while ((ap = strsep(&n, ",")) != NULL) {
405 for (i = attrs; i ->name != NULL; ++i) {
406 if (strcmp(i->name, ap))
414 yyerror("unknown attribute \"%s\" at col %d",
422 setattr(char *prfx, char *line, char *trail)
426 assert(current_style != NULL);
432 if (!config_setattr(current_style, p, l, t))
433 yyerror("invalid style %s", current_style);
437 parseconfig(const char *filename, int fonf)
439 if ((yyfp = fopen(filename, "r")) == NULL) {
441 err(1, "%s", filename);