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 current_bg, current_fg;
47 static const char *path;
53 static void yyerror(const char *, ...);
54 static int yylex(void);
55 static void setprfx(int, const char *);
56 static void setvari(char *, int);
57 static void setvars(char *, char *);
58 static int colorname(const char *);
59 static void setcolor(int, int, 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(0, $2); }
100 | TCONT TSTRING { setprfx(1, $2); }
111 | TATTR TBOLD { printf("style attr setted to bold\n"); }
112 | TATTR TUNDERLINE { printf("style attr setted to underline\n"); }
115 bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
118 unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
124 yyerror(const char *fmt, ...)
128 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
130 vfprintf(stderr, fmt, va);
132 fprintf(stderr, "\n");
136 static struct keyword {
148 { "underline", TUNDERLINE },
150 { "unbind", TUNBIND },
156 char buf[1024], *ebuf, *p, *str;
158 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
162 ebuf = buf + sizeof(buf);
165 /* skip whitespace first */
166 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
169 /* check for special one-character constructions */
180 /* skip comments; NUL is allowed; no continuation */
181 while ((c = getc(yyfp)) != '\n')
191 /* parsing next word */
192 for (;; c = getc(yyfp), yylval.colno++) {
195 yyerror("unallowed character NULL in column %d",
206 yyerror("unterminated quotes in column %d",
217 yyerror("unterminated escape in column %d",
220 yyerror("unterminated quotes in column %d",
229 if (!escape && !quotes)
244 yyerror("line too long");
256 * There could be a number of reason for empty buffer,
257 * and we handle all of them here, to avoid cluttering
262 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
266 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
267 if (strcmp(buf, keywords[i].word) == 0)
268 return keywords[i].token;
272 if (!nonkw && (c == '-' || isdigit(c))) {
273 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
275 yyerror("number is %s: %s", errstr, buf);
278 if ((str = strdup(buf)) == NULL)
279 err(1, "%s", __func__);
285 yyerror("input error reading config");
290 setprfx(int cont, const char *name)
292 assert(current_style != NULL);
294 if (!config_setprfx(current_style, cont, name))
295 yyerror("invalid style %s", name);
299 setvari(char *var, int val)
301 if (!config_setvari(var, val))
302 yyerror("invalid variable or value: %s = %d",
309 setvars(char *var, char *val)
311 if (!config_setvars(var, val))
312 yyerror("invalid variable or value: %s = \"%s\"",
319 colorname(const char *name)
326 { "black", COLOR_BLACK },
327 { "red", COLOR_RED },
328 { "green", COLOR_GREEN },
329 { "yellow", COLOR_YELLOW },
330 { "blue", COLOR_BLUE },
331 { "magenta", COLOR_MAGENTA },
332 { "cyan", COLOR_CYAN },
333 { "white", COLOR_WHITE },
337 for (i = colors; i->name != NULL; ++i) {
338 if (!strcmp(i->name, name))
342 yyerror("unknown color name \"%s\"", name);
347 setcolor(int prfx, int bg, const char *color)
351 assert(current_style != NULL);
353 if ((c = colorname(color)) == -1)
356 if (!config_setcolor(current_style, prfx, bg, c))
357 yyerror("can't set color \"%s\" for %s", color,
362 parseconfig(const char *filename, int fonf)
366 printf("proceed?\n");
370 if ((yyfp = fopen(filename, "r")) == NULL) {
372 err(1, "%s", filename);