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 *);
61 %}
63 %token TSET
64 %token TSTYLE TPRFX TCONT TBG TFG TATTR TBOLD TUNDERLINE
65 %token TBIND TUNBIND
67 %token <str> TSTRING
68 %token <num> TNUMBER
70 %%
72 grammar : /* empty */
73 | grammar '\n'
74 | grammar rule '\n'
75 | error '\n'
76 ;
78 rule : set
79 | style {
80 free(current_style);
81 current_style = NULL;
82 }
83 | bind
84 | unbind
85 ;
87 set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
88 | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
89 ;
91 style : TSTYLE TSTRING { current_style = $2; } stylespec ;
92 stylespec : styleopt | '{' styleopts '}' ;
94 styleopts : /* empty */
95 | styleopts '\n'
96 | styleopts styleopt '\n'
97 ;
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); }
118 %%
120 void
121 yyerror(const char *fmt, ...)
123 va_list va;
125 fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
126 va_start(va, fmt);
127 vfprintf(stderr, fmt, va);
128 va_end(va);
129 fprintf(stderr, "\n");
130 parse_errors++;
133 static struct keyword {
134 const char *word;
135 int token;
136 } keywords[] = {
137 { "set", TSET },
138 { "style", TSTYLE },
139 { "prefix", TPRFX },
140 { "cont", TCONT },
141 { "bg", TBG },
142 { "fg", TFG },
143 { "attr", TATTR },
144 { "bold", TBOLD },
145 { "underline", TUNDERLINE },
146 { "bind", TBIND },
147 { "unbind", TUNBIND },
148 };
150 int
151 yylex(void)
153 char buf[1024], *ebuf, *p, *str;
154 const char *errstr;
155 int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
156 size_t i;
158 p = buf;
159 ebuf = buf + sizeof(buf);
161 repeat:
162 /* skip whitespace first */
163 for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
164 yylval.colno++;
166 /* check for special one-character constructions */
167 switch (c) {
168 case '\n':
169 yylval.colno = 0;
170 yylval.lineno++;
171 /* fallthrough */
172 case '{':
173 case '}':
174 case '=':
175 return c;
176 case '#':
177 /* skip comments; NUL is allowed; no continuation */
178 while ((c = getc(yyfp)) != '\n')
179 if (c == EOF)
180 goto eof;
181 yylval.colno = 0;
182 yylval.lineno++;
183 return c;
184 case EOF:
185 goto eof;
188 /* parsing next word */
189 for (;; c = getc(yyfp), yylval.colno++) {
190 switch (c) {
191 case '\0':
192 yyerror("unallowed character NULL in column %d",
193 yylval.colno+1);
194 escape = 0;
195 continue;
196 case '\\':
197 escape = !escape;
198 if (escape)
199 continue;
200 break;
201 case '\n':
202 if (quotes)
203 yyerror("unterminated quotes in column %d",
204 yylval.colno+1);
205 if (escape) {
206 nonkw = 1;
207 escape = 0;
208 yylval.colno = 0;
209 yylval.lineno++;
211 goto eow;
212 case EOF:
213 if (escape)
214 yyerror("unterminated escape in column %d",
215 yylval.colno);
216 if (quotes)
217 yyerror("unterminated quotes in column %d",
218 qpos + 1);
219 goto eow;
220 case '{':
221 case '}':
222 case '=':
223 case '#':
224 case ' ':
225 case '\t':
226 if (!escape && !quotes)
227 goto eow;
228 break;
229 case '"':
230 if (!escape) {
231 quotes = !quotes;
232 if (quotes) {
233 nonkw = 1;
234 qpos = yylval.colno;
236 continue;
239 *p++ = c;
240 if (p == ebuf) {
241 yyerror("line too long");
242 p = buf;
244 escape = 0;
247 eow:
248 *p = 0;
249 if (c != EOF)
250 ungetc(c, yyfp);
251 if (p == buf) {
252 /*
253 * There could be a number of reason for empty buffer,
254 * and we handle all of them here, to avoid cluttering
255 * the main loop.
256 */
257 if (c == EOF)
258 goto eof;
259 else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
260 goto repeat;
262 if (!nonkw) {
263 for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
264 if (strcmp(buf, keywords[i].word) == 0)
265 return keywords[i].token;
268 c = *buf;
269 if (!nonkw && (c == '-' || isdigit(c))) {
270 yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
271 if (errstr != NULL)
272 yyerror("number is %s: %s", errstr, buf);
273 return TNUMBER;
275 if ((str = strdup(buf)) == NULL)
276 err(1, "%s", __func__);
277 yylval.str = str;
278 return TSTRING;
280 eof:
281 if (ferror(yyfp))
282 yyerror("input error reading config");
283 return 0;
286 static void
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);
295 static void
296 setvari(char *var, int val)
298 if (!config_setvari(var, val))
299 yyerror("invalid variable or value: %s = %d",
300 var, val);
302 free(var);
305 static void
306 setvars(char *var, char *val)
308 if (!config_setvars(var, val))
309 yyerror("invalid variable or value: %s = \"%s\"",
310 var, val);
312 free(var);
315 static int
316 colorname(const char *name)
318 struct {
319 const char *name;
320 short val;
321 } *i, colors[] = {
322 { "default", -1 },
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 },
331 { NULL, 0 },
332 };
334 for (i = colors; i->name != NULL; ++i) {
335 if (!strcmp(i->name, name))
336 return i->val;
339 yyerror("unknown color name \"%s\"", name);
340 return -1;
343 void
344 setcolor(const char *prfx, const char *line, const char *trail)
346 int p, l, t;
348 assert(current_style != NULL);
350 p = colorname(prfx);
351 l = colorname(line);
352 t = colorname(trail);
354 if (!config_setcolor(color_type == TBG, current_style, p, l, t))
355 yyerror("invalid style %s", current_style);
358 void
359 parseconfig(const char *filename, int fonf)
361 if ((yyfp = fopen(filename, "r")) == NULL) {
362 if (fonf)
363 err(1, "%s", filename);
364 return;
367 path = filename;
368 yyparse();
369 fclose(yyfp);
370 if (parse_errors)
371 exit(1);