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 *);
64 %token TSTYLE TPRFX TCONT TBG TFG TATTR TBOLD TUNDERLINE
87 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
88 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
91 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
92 stylespec : styleopt | '{' styleopts '}' ;
94 styleopts : /* empty */
96 | styleopts styleopt '\n'
99 styleopt : TPRFX TSTRING { setprfx($2, $2); }
100 | TPRFX TSTRING TSTRING { setprfx($2, $2); }
101 | TBG { color_type = TBG; } colorspec
102 | TFG { color_type = TFG; } colorspec
103 | TATTR TBOLD { printf("style attr setted to bold\n"); }
104 | TATTR TUNDERLINE { printf("style attr setted to underline\n"); }
107 colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
108 | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
109 | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
112 bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
115 unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
121 yyerror(const char *fmt, ...)
125 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
127 vfprintf(stderr, fmt, va);
129 fprintf(stderr, "\n");
133 static struct keyword {
145 { "underline", TUNDERLINE },
147 { "unbind", TUNBIND },
153 char buf[1024], *ebuf, *p, *str;
155 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
159 ebuf = buf + sizeof(buf);
162 /* skip whitespace first */
163 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
166 /* check for special one-character constructions */
177 /* skip comments; NUL is allowed; no continuation */
178 while ((c = getc(yyfp)) != '\n')
188 /* parsing next word */
189 for (;; c = getc(yyfp), yylval.colno++) {
192 yyerror("unallowed character NULL in column %d",
203 yyerror("unterminated quotes in column %d",
214 yyerror("unterminated escape in column %d",
217 yyerror("unterminated quotes in column %d",
226 if (!escape && !quotes)
241 yyerror("line too long");
253 * There could be a number of reason for empty buffer,
254 * and we handle all of them here, to avoid cluttering
259 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
263 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
264 if (strcmp(buf, keywords[i].word) == 0)
265 return keywords[i].token;
269 if (!nonkw && (c == '-' || isdigit(c))) {
270 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
272 yyerror("number is %s: %s", errstr, buf);
275 if ((str = strdup(buf)) == NULL)
276 err(1, "%s", __func__);
282 yyerror("input error reading config");
287 setprfx(const char *prfx, const char *cont)
289 assert(current_style != NULL);
291 if (!config_setprfx(current_style, prfx, cont))
292 yyerror("invalid style %s", current_style);
296 setvari(char *var, int val)
298 if (!config_setvari(var, val))
299 yyerror("invalid variable or value: %s = %d",
306 setvars(char *var, char *val)
308 if (!config_setvars(var, val))
309 yyerror("invalid variable or value: %s = \"%s\"",
316 colorname(const char *name)
323 { "black", COLOR_BLACK },
324 { "red", COLOR_RED },
325 { "green", COLOR_GREEN },
326 { "yellow", COLOR_YELLOW },
327 { "blue", COLOR_BLUE },
328 { "magenta", COLOR_MAGENTA },
329 { "cyan", COLOR_CYAN },
330 { "white", COLOR_WHITE },
334 for (i = colors; i->name != NULL; ++i) {
335 if (!strcmp(i->name, name))
339 yyerror("unknown color name \"%s\"", name);
344 setcolor(const char *prfx, const char *line, const char *trail)
348 assert(current_style != NULL);
352 t = colorname(trail);
354 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
355 yyerror("invalid style %s", current_style);
359 parseconfig(const char *filename, int fonf)
361 if ((yyfp = fopen(filename, "r")) == NULL) {
363 err(1, "%s", filename);