Blob


1 /*
2 * Much of the design is taken from doas (parse.y rev 1.29)
3 *
4 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
6 *
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.
10 *
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.
18 *
19 */
21 %{
23 #include "telescope.h"
25 #include <assert.h>
26 #include <ctype.h>
27 #include <limits.h>
28 #include <ncurses.h>
29 #include <stdarg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 typedef struct {
35 union {
36 char *str;
37 int num;
38 };
39 int lineno;
40 int colno;
41 } yystype;
42 #define YYSTYPE yystype
44 static char *current_style;
45 static int color_type;
47 static const char *path;
49 FILE *yyfp;
51 int parse_errors = 0;
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 *);
63 %}
65 %token TSET
66 %token TSTYLE TPRFX TCONT TBG TFG TATTR
67 %token TBIND TUNBIND
69 %token <str> TSTRING
70 %token <num> TNUMBER
72 %%
74 grammar : /* empty */
75 | grammar '\n'
76 | grammar rule '\n'
77 | error '\n'
78 ;
80 rule : set
81 | style {
82 free(current_style);
83 current_style = NULL;
84 }
85 | bind
86 | unbind
87 ;
89 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
90 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
91 ;
93 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
94 stylespec : styleopt | '{' styleopts '}' ;
96 styleopts : /* empty */
97 | styleopts '\n'
98 | styleopts styleopt '\n'
99 ;
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
105 | TATTR attr
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); }
124 %%
126 void
127 yyerror(const char *fmt, ...)
129 va_list va;
131 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
132 va_start(va, fmt);
133 vfprintf(stderr, fmt, va);
134 va_end(va);
135 fprintf(stderr, "\n");
136 parse_errors++;
139 static struct keyword {
140 const char *word;
141 int token;
142 } keywords[] = {
143 { "set", TSET },
144 { "style", TSTYLE },
145 { "prefix", TPRFX },
146 { "cont", TCONT },
147 { "bg", TBG },
148 { "fg", TFG },
149 { "attr", TATTR },
150 { "bind", TBIND },
151 { "unbind", TUNBIND },
152 };
154 int
155 yylex(void)
157 char buf[1024], *ebuf, *p, *str;
158 const char *errstr;
159 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
160 size_t i;
162 p = buf;
163 ebuf = buf + sizeof(buf);
165 repeat:
166 /* skip whitespace first */
167 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
168 yylval.colno++;
170 /* check for special one-character constructions */
171 switch (c) {
172 case '\n':
173 yylval.colno = 0;
174 yylval.lineno++;
175 /* fallthrough */
176 case '{':
177 case '}':
178 case '=':
179 return c;
180 case '#':
181 /* skip comments; NUL is allowed; no continuation */
182 while ((c = getc(yyfp)) != '\n')
183 if (c == EOF)
184 goto eof;
185 yylval.colno = 0;
186 yylval.lineno++;
187 return c;
188 case EOF:
189 goto eof;
192 /* parsing next word */
193 for (;; c = getc(yyfp), yylval.colno++) {
194 switch (c) {
195 case '\0':
196 yyerror("unallowed character NULL in column %d",
197 yylval.colno+1);
198 escape = 0;
199 continue;
200 case '\\':
201 escape = !escape;
202 if (escape)
203 continue;
204 break;
205 case '\n':
206 if (quotes)
207 yyerror("unterminated quotes in column %d",
208 yylval.colno+1);
209 if (escape) {
210 nonkw = 1;
211 escape = 0;
212 yylval.colno = 0;
213 yylval.lineno++;
215 goto eow;
216 case EOF:
217 if (escape)
218 yyerror("unterminated escape in column %d",
219 yylval.colno);
220 if (quotes)
221 yyerror("unterminated quotes in column %d",
222 qpos + 1);
223 goto eow;
224 case '{':
225 case '}':
226 case '=':
227 case '#':
228 case ' ':
229 case '\t':
230 if (!escape && !quotes)
231 goto eow;
232 break;
233 case '"':
234 if (!escape) {
235 quotes = !quotes;
236 if (quotes) {
237 nonkw = 1;
238 qpos = yylval.colno;
240 continue;
243 *p++ = c;
244 if (p == ebuf) {
245 yyerror("line too long");
246 p = buf;
248 escape = 0;
251 eow:
252 *p = 0;
253 if (c != EOF)
254 ungetc(c, yyfp);
255 if (p == buf) {
256 /*
257 * There could be a number of reason for empty buffer,
258 * and we handle all of them here, to avoid cluttering
259 * the main loop.
260 */
261 if (c == EOF)
262 goto eof;
263 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
264 goto repeat;
266 if (!nonkw) {
267 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
268 if (strcmp(buf, keywords[i].word) == 0)
269 return keywords[i].token;
272 c = *buf;
273 if (!nonkw && (c == '-' || isdigit(c))) {
274 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
275 if (errstr != NULL)
276 yyerror("number is %s: %s", errstr, buf);
277 return TNUMBER;
279 if ((str = strdup(buf)) == NULL)
280 err(1, "%s", __func__);
281 yylval.str = str;
282 return TSTRING;
284 eof:
285 if (ferror(yyfp))
286 yyerror("input error reading config");
287 return 0;
290 static void
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);
299 static void
300 setvari(char *var, int val)
302 if (!config_setvari(var, val))
303 yyerror("invalid variable or value: %s = %d",
304 var, val);
306 free(var);
309 static void
310 setvars(char *var, char *val)
312 if (!config_setvars(var, val))
313 yyerror("invalid variable or value: %s = \"%s\"",
314 var, val);
316 free(var);
319 static int
320 colorname(const char *name)
322 struct {
323 const char *name;
324 short val;
325 } *i, colors[] = {
326 { "default", -1 },
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 },
335 { NULL, 0 },
336 };
337 const char *errstr;
338 int n;
340 if (has_prefix(name, "colo")) {
341 /* people are strange */
342 if (has_prefix(name, "color"))
343 name += 5;
344 else if (has_prefix(name, "colour"))
345 name += 6;
346 else
347 goto err;
349 n = strtonum(name, 0, 256, &errstr);
350 if (errstr != NULL)
351 yyerror("color number is %s: %s", errstr, name);
352 return n;
355 for (i = colors; i->name != NULL; ++i) {
356 if (!strcmp(i->name, name))
357 return i->val;
360 err:
361 yyerror("unknown color name \"%s\"", name);
362 return -1;
365 void
366 setcolor(const char *prfx, const char *line, const char *trail)
368 int p, l, t;
370 assert(current_style != NULL);
372 p = colorname(prfx);
373 l = colorname(line);
374 t = colorname(trail);
376 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
377 yyerror("invalid style %s", current_style);
380 static int
381 attrname(char *n)
383 struct {
384 const char *name;
385 unsigned int val;
386 } *i, attrs[] = {
387 { "normal", A_NORMAL },
388 { "standout", A_STANDOUT },
389 { "underline", A_UNDERLINE },
390 { "reverse", A_REVERSE },
391 { "blink", A_BLINK },
392 { "dim", A_DIM },
393 { "bold", A_BOLD },
394 { NULL, 0 },
395 };
396 int ret, found;
397 char *ap;
399 ret = 0;
400 while ((ap = strsep(&n, ",")) != NULL) {
401 if (*ap == '\0')
402 continue;
404 found = 0;
405 for (i = attrs; i ->name != NULL; ++i) {
406 if (strcmp(i->name, ap))
407 continue;
408 ret |= i->val;
409 found = 1;
410 break;
413 if (!found)
414 yyerror("unknown attribute \"%s\" at col %d",
415 ap, yylval.colno+1);
418 return ret;
421 static void
422 setattr(char *prfx, char *line, char *trail)
424 int p, l, t;
426 assert(current_style != NULL);
428 p = attrname(prfx);
429 l = attrname(line);
430 t = attrname(trail);
432 if (!config_setattr(current_style, p, l, t))
433 yyerror("invalid style %s", current_style);
436 void
437 parseconfig(const char *filename, int fonf)
439 if ((yyfp = fopen(filename, "r")) == NULL) {
440 if (fonf)
441 err(1, "%s", filename);
442 return;
445 path = filename;
446 yyparse();
447 fclose(yyfp);
448 if (parse_errors)
449 exit(1);