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"
40 #define YYSTYPE yystype
42 static char *current_style;
43 static const char *path;
49 static void yyerror(const char *, ...);
50 static int yylex(void);
55 %token TSTYLE TPRFX TCONT TBG TFG TATTR TBOLD TUNDERLINE
70 | style { free(current_style); current_style = NULL; }
75 set : TSET TSTRING '=' TSTRING { printf("set %s = \"%s\"\n", $2, $4); }
76 | TSET TSTRING '=' TNUMBER { printf("set %s = %d\n", $2, $4); }
79 style : TSTYLE TSTRING { current_style = $2; } styleopt
80 | TSTYLE TSTRING { current_style = $2; } '{' styleopts '}'
83 styleopts : /* empty */
85 | styleopts styleopt '\n'
88 styleopt : TPRFX TSTRING { setprfx(0, $2); }
89 | TCONT TSTRING { setprfx(1, $2); }
90 | TBG TSTRING { printf("style background setted to \"%s\"\n", $2); }
91 | TFG TSTRING { printf("style foreground setted to \"%s\"\n", $2); }
92 | TATTR TBOLD { printf("style attr setted to bold\n"); }
93 | TATTR TUNDERLINE { printf("style attr setted to underline\n"); }
96 bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
99 unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
105 yyerror(const char *fmt, ...)
109 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
111 vfprintf(stderr, fmt, va);
113 fprintf(stderr, "\n");
117 static struct keyword {
125 { "background", TBG },
126 { "foreground", TFG },
129 { "underline", TUNDERLINE },
131 { "unbind", TUNBIND },
137 char buf[1024], *ebuf, *p, *str;
139 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
143 ebuf = buf + sizeof(buf);
146 /* skip whitespace first */
147 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
150 /* check for special one-character constructions */
161 /* skip comments; NUL is allowed; no continuation */
162 while ((c = getc(yyfp)) != '\n')
172 /* parsing next word */
173 for (;; c = getc(yyfp), yylval.colno++) {
176 yyerror("unallowed character NULL in column %d",
187 yyerror("unterminated quotes in column %d",
198 yyerror("unterminated escape in column %d",
201 yyerror("unterminated quotes in column %d",
210 if (!escape && !quotes)
225 yyerror("line too long");
237 * There could be a number of reason for empty buffer,
238 * and we handle all of them here, to avoid cluttering
243 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
247 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
248 if (strcmp(buf, keywords[i].word) == 0)
249 return keywords[i].token;
253 if (!nonkw && (c == '-' || isdigit(c))) {
254 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
256 yyerror("number is %s: %s", errstr, buf);
259 if ((str = strdup(buf)) == NULL)
260 err(1, "%s", __func__);
266 yyerror("input error reading config");
271 setprfx(int cont, const char *name)
273 if (current_style == NULL) {
274 warnx("current_style = NULL!");
278 if (!config_setprfx(current_style, cont, name))
279 yyerror("invalid style %s", name);
283 parseconfig(const char *filename, int fonf)
285 if ((yyfp = fopen(filename, "r")) == NULL) {
287 err(1, "%s", filename);