Blame


1 69bdd906 2021-06-15 op /*
2 69bdd906 2021-06-15 op * Much of the design is taken from doas (parse.y rev 1.29)
3 69bdd906 2021-06-15 op *
4 69bdd906 2021-06-15 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
5 69bdd906 2021-06-15 op * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
6 69bdd906 2021-06-15 op *
7 69bdd906 2021-06-15 op * Permission to use, copy, modify, and distribute this software for any
8 69bdd906 2021-06-15 op * purpose with or without fee is hereby granted, provided that the above
9 69bdd906 2021-06-15 op * copyright notice and this permission notice appear in all copies.
10 69bdd906 2021-06-15 op *
11 69bdd906 2021-06-15 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 69bdd906 2021-06-15 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 69bdd906 2021-06-15 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 69bdd906 2021-06-15 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 69bdd906 2021-06-15 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 69bdd906 2021-06-15 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 69bdd906 2021-06-15 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 69bdd906 2021-06-15 op *
19 69bdd906 2021-06-15 op */
20 69bdd906 2021-06-15 op
21 69bdd906 2021-06-15 op %{
22 69bdd906 2021-06-15 op
23 69bdd906 2021-06-15 op #include "telescope.h"
24 69bdd906 2021-06-15 op
25 74a2587f 2021-06-21 op #include <assert.h>
26 69bdd906 2021-06-15 op #include <ctype.h>
27 69bdd906 2021-06-15 op #include <limits.h>
28 74a2587f 2021-06-21 op #include <ncurses.h>
29 69bdd906 2021-06-15 op #include <stdarg.h>
30 69bdd906 2021-06-15 op #include <stdio.h>
31 69bdd906 2021-06-15 op #include <stdlib.h>
32 69bdd906 2021-06-15 op #include <string.h>
33 69bdd906 2021-06-15 op
34 69bdd906 2021-06-15 op typedef struct {
35 69bdd906 2021-06-15 op union {
36 69bdd906 2021-06-15 op char *str;
37 69bdd906 2021-06-15 op int num;
38 69bdd906 2021-06-15 op };
39 69bdd906 2021-06-15 op int lineno;
40 69bdd906 2021-06-15 op int colno;
41 69bdd906 2021-06-15 op } yystype;
42 69bdd906 2021-06-15 op #define YYSTYPE yystype
43 69bdd906 2021-06-15 op
44 69e1d1b6 2021-06-15 op static char *current_style;
45 d89b86d6 2021-06-21 op static int color_type;
46 74a2587f 2021-06-21 op
47 69bdd906 2021-06-15 op static const char *path;
48 69bdd906 2021-06-15 op
49 69bdd906 2021-06-15 op FILE *yyfp;
50 69bdd906 2021-06-15 op
51 69bdd906 2021-06-15 op int parse_errors = 0;
52 69bdd906 2021-06-15 op
53 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
54 69bdd906 2021-06-15 op static int yylex(void);
55 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
56 86e7d7b4 2021-06-19 op static void setvari(char *, int);
57 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
58 74a2587f 2021-06-21 op static int colorname(const char *);
59 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
60 d0149485 2021-06-22 op static int attrname(char *);
61 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
62 69bdd906 2021-06-15 op
63 69bdd906 2021-06-15 op %}
64 69bdd906 2021-06-15 op
65 69bdd906 2021-06-15 op %token TSET
66 d0149485 2021-06-22 op %token TSTYLE TPRFX TCONT TBG TFG TATTR
67 69bdd906 2021-06-15 op %token TBIND TUNBIND
68 69bdd906 2021-06-15 op
69 69bdd906 2021-06-15 op %token <str> TSTRING
70 69bdd906 2021-06-15 op %token <num> TNUMBER
71 69bdd906 2021-06-15 op
72 69bdd906 2021-06-15 op %%
73 69bdd906 2021-06-15 op
74 69bdd906 2021-06-15 op grammar : /* empty */
75 69bdd906 2021-06-15 op | grammar '\n'
76 69bdd906 2021-06-15 op | grammar rule '\n'
77 69bdd906 2021-06-15 op | error '\n'
78 69bdd906 2021-06-15 op ;
79 69bdd906 2021-06-15 op
80 69e1d1b6 2021-06-15 op rule : set
81 74a2587f 2021-06-21 op | style {
82 74a2587f 2021-06-21 op free(current_style);
83 74a2587f 2021-06-21 op current_style = NULL;
84 74a2587f 2021-06-21 op }
85 69e1d1b6 2021-06-15 op | bind
86 69e1d1b6 2021-06-15 op | unbind
87 69e1d1b6 2021-06-15 op ;
88 69bdd906 2021-06-15 op
89 86e7d7b4 2021-06-19 op set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
90 86e7d7b4 2021-06-19 op | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
91 69bdd906 2021-06-15 op ;
92 69bdd906 2021-06-15 op
93 74a2587f 2021-06-21 op style : TSTYLE TSTRING { current_style = $2; } stylespec ;
94 74a2587f 2021-06-21 op stylespec : styleopt | '{' styleopts '}' ;
95 69bdd906 2021-06-15 op
96 69bdd906 2021-06-15 op styleopts : /* empty */
97 69bdd906 2021-06-15 op | styleopts '\n'
98 69bdd906 2021-06-15 op | styleopts styleopt '\n'
99 69bdd906 2021-06-15 op ;
100 69bdd906 2021-06-15 op
101 d89b86d6 2021-06-21 op styleopt : TPRFX TSTRING { setprfx($2, $2); }
102 d89b86d6 2021-06-21 op | TPRFX TSTRING TSTRING { setprfx($2, $2); }
103 d89b86d6 2021-06-21 op | TBG { color_type = TBG; } colorspec
104 d89b86d6 2021-06-21 op | TFG { color_type = TFG; } colorspec
105 d0149485 2021-06-22 op | TATTR attr
106 d89b86d6 2021-06-21 op ;
107 d89b86d6 2021-06-21 op
108 d89b86d6 2021-06-21 op colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
109 d89b86d6 2021-06-21 op | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
110 d89b86d6 2021-06-21 op | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
111 69bdd906 2021-06-15 op ;
112 69bdd906 2021-06-15 op
113 d0149485 2021-06-22 op attr : TSTRING { setattr($1, $1, $1); free($1); }
114 d0149485 2021-06-22 op | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
115 d0149485 2021-06-22 op | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
116 d0149485 2021-06-22 op ;
117 d0149485 2021-06-22 op
118 69bdd906 2021-06-15 op bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
119 69bdd906 2021-06-15 op ;
120 69bdd906 2021-06-15 op
121 69bdd906 2021-06-15 op unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
122 69bdd906 2021-06-15 op ;
123 69bdd906 2021-06-15 op
124 69bdd906 2021-06-15 op %%
125 69bdd906 2021-06-15 op
126 69bdd906 2021-06-15 op void
127 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
128 69bdd906 2021-06-15 op {
129 69bdd906 2021-06-15 op va_list va;
130 69bdd906 2021-06-15 op
131 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
132 69bdd906 2021-06-15 op va_start(va, fmt);
133 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
134 69bdd906 2021-06-15 op va_end(va);
135 69bdd906 2021-06-15 op fprintf(stderr, "\n");
136 69bdd906 2021-06-15 op parse_errors++;
137 69bdd906 2021-06-15 op }
138 69bdd906 2021-06-15 op
139 69bdd906 2021-06-15 op static struct keyword {
140 69bdd906 2021-06-15 op const char *word;
141 69bdd906 2021-06-15 op int token;
142 69bdd906 2021-06-15 op } keywords[] = {
143 69bdd906 2021-06-15 op { "set", TSET },
144 69bdd906 2021-06-15 op { "style", TSTYLE },
145 69bdd906 2021-06-15 op { "prefix", TPRFX },
146 69bdd906 2021-06-15 op { "cont", TCONT },
147 74a2587f 2021-06-21 op { "bg", TBG },
148 74a2587f 2021-06-21 op { "fg", TFG },
149 69bdd906 2021-06-15 op { "attr", TATTR },
150 69bdd906 2021-06-15 op { "bind", TBIND },
151 69bdd906 2021-06-15 op { "unbind", TUNBIND },
152 69bdd906 2021-06-15 op };
153 69bdd906 2021-06-15 op
154 69bdd906 2021-06-15 op int
155 69bdd906 2021-06-15 op yylex(void)
156 69bdd906 2021-06-15 op {
157 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
158 69bdd906 2021-06-15 op const char *errstr;
159 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
160 69bdd906 2021-06-15 op size_t i;
161 69bdd906 2021-06-15 op
162 69bdd906 2021-06-15 op p = buf;
163 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
164 69bdd906 2021-06-15 op
165 69bdd906 2021-06-15 op repeat:
166 69bdd906 2021-06-15 op /* skip whitespace first */
167 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
168 69bdd906 2021-06-15 op yylval.colno++;
169 69bdd906 2021-06-15 op
170 69bdd906 2021-06-15 op /* check for special one-character constructions */
171 69bdd906 2021-06-15 op switch (c) {
172 69bdd906 2021-06-15 op case '\n':
173 69bdd906 2021-06-15 op yylval.colno = 0;
174 69bdd906 2021-06-15 op yylval.lineno++;
175 69bdd906 2021-06-15 op /* fallthrough */
176 69bdd906 2021-06-15 op case '{':
177 69bdd906 2021-06-15 op case '}':
178 69bdd906 2021-06-15 op case '=':
179 69bdd906 2021-06-15 op return c;
180 69bdd906 2021-06-15 op case '#':
181 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
182 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
183 69bdd906 2021-06-15 op if (c == EOF)
184 69bdd906 2021-06-15 op goto eof;
185 69bdd906 2021-06-15 op yylval.colno = 0;
186 69bdd906 2021-06-15 op yylval.lineno++;
187 69bdd906 2021-06-15 op return c;
188 69bdd906 2021-06-15 op case EOF:
189 69bdd906 2021-06-15 op goto eof;
190 69bdd906 2021-06-15 op }
191 69bdd906 2021-06-15 op
192 69bdd906 2021-06-15 op /* parsing next word */
193 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
194 69bdd906 2021-06-15 op switch (c) {
195 69bdd906 2021-06-15 op case '\0':
196 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
197 69bdd906 2021-06-15 op yylval.colno+1);
198 69bdd906 2021-06-15 op escape = 0;
199 69bdd906 2021-06-15 op continue;
200 69bdd906 2021-06-15 op case '\\':
201 69bdd906 2021-06-15 op escape = !escape;
202 69bdd906 2021-06-15 op if (escape)
203 69bdd906 2021-06-15 op continue;
204 69bdd906 2021-06-15 op break;
205 69bdd906 2021-06-15 op case '\n':
206 69bdd906 2021-06-15 op if (quotes)
207 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
208 69bdd906 2021-06-15 op yylval.colno+1);
209 69bdd906 2021-06-15 op if (escape) {
210 69bdd906 2021-06-15 op nonkw = 1;
211 69bdd906 2021-06-15 op escape = 0;
212 69bdd906 2021-06-15 op yylval.colno = 0;
213 69bdd906 2021-06-15 op yylval.lineno++;
214 69bdd906 2021-06-15 op }
215 69bdd906 2021-06-15 op goto eow;
216 69bdd906 2021-06-15 op case EOF:
217 69bdd906 2021-06-15 op if (escape)
218 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
219 69bdd906 2021-06-15 op yylval.colno);
220 69bdd906 2021-06-15 op if (quotes)
221 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
222 69bdd906 2021-06-15 op qpos + 1);
223 69bdd906 2021-06-15 op goto eow;
224 69bdd906 2021-06-15 op case '{':
225 69bdd906 2021-06-15 op case '}':
226 69bdd906 2021-06-15 op case '=':
227 69bdd906 2021-06-15 op case '#':
228 69bdd906 2021-06-15 op case ' ':
229 69bdd906 2021-06-15 op case '\t':
230 69bdd906 2021-06-15 op if (!escape && !quotes)
231 69bdd906 2021-06-15 op goto eow;
232 69bdd906 2021-06-15 op break;
233 69bdd906 2021-06-15 op case '"':
234 69bdd906 2021-06-15 op if (!escape) {
235 69bdd906 2021-06-15 op quotes = !quotes;
236 69bdd906 2021-06-15 op if (quotes) {
237 69bdd906 2021-06-15 op nonkw = 1;
238 69bdd906 2021-06-15 op qpos = yylval.colno;
239 69bdd906 2021-06-15 op }
240 69bdd906 2021-06-15 op continue;
241 69bdd906 2021-06-15 op }
242 69bdd906 2021-06-15 op }
243 69bdd906 2021-06-15 op *p++ = c;
244 69bdd906 2021-06-15 op if (p == ebuf) {
245 69bdd906 2021-06-15 op yyerror("line too long");
246 69bdd906 2021-06-15 op p = buf;
247 69bdd906 2021-06-15 op }
248 69bdd906 2021-06-15 op escape = 0;
249 69bdd906 2021-06-15 op }
250 69bdd906 2021-06-15 op
251 69bdd906 2021-06-15 op eow:
252 69bdd906 2021-06-15 op *p = 0;
253 69bdd906 2021-06-15 op if (c != EOF)
254 69bdd906 2021-06-15 op ungetc(c, yyfp);
255 69bdd906 2021-06-15 op if (p == buf) {
256 69bdd906 2021-06-15 op /*
257 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
258 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
259 69bdd906 2021-06-15 op * the main loop.
260 69bdd906 2021-06-15 op */
261 69bdd906 2021-06-15 op if (c == EOF)
262 69bdd906 2021-06-15 op goto eof;
263 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
264 69bdd906 2021-06-15 op goto repeat;
265 69bdd906 2021-06-15 op }
266 69bdd906 2021-06-15 op if (!nonkw) {
267 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
268 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
269 69bdd906 2021-06-15 op return keywords[i].token;
270 69bdd906 2021-06-15 op }
271 69bdd906 2021-06-15 op }
272 69bdd906 2021-06-15 op c = *buf;
273 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
274 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
275 69bdd906 2021-06-15 op if (errstr != NULL)
276 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
277 69bdd906 2021-06-15 op return TNUMBER;
278 69bdd906 2021-06-15 op }
279 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
280 69bdd906 2021-06-15 op err(1, "%s", __func__);
281 69bdd906 2021-06-15 op yylval.str = str;
282 69bdd906 2021-06-15 op return TSTRING;
283 69bdd906 2021-06-15 op
284 69bdd906 2021-06-15 op eof:
285 69bdd906 2021-06-15 op if (ferror(yyfp))
286 69bdd906 2021-06-15 op yyerror("input error reading config");
287 69bdd906 2021-06-15 op return 0;
288 69bdd906 2021-06-15 op }
289 69bdd906 2021-06-15 op
290 69e1d1b6 2021-06-15 op static void
291 d89b86d6 2021-06-21 op setprfx(const char *prfx, const char *cont)
292 69e1d1b6 2021-06-15 op {
293 74a2587f 2021-06-21 op assert(current_style != NULL);
294 69e1d1b6 2021-06-15 op
295 d89b86d6 2021-06-21 op if (!config_setprfx(current_style, prfx, cont))
296 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
297 86e7d7b4 2021-06-19 op }
298 86e7d7b4 2021-06-19 op
299 86e7d7b4 2021-06-19 op static void
300 86e7d7b4 2021-06-19 op setvari(char *var, int val)
301 86e7d7b4 2021-06-19 op {
302 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
303 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
304 86e7d7b4 2021-06-19 op var, val);
305 86e7d7b4 2021-06-19 op
306 86e7d7b4 2021-06-19 op free(var);
307 69e1d1b6 2021-06-15 op }
308 69e1d1b6 2021-06-15 op
309 86e7d7b4 2021-06-19 op static void
310 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
311 86e7d7b4 2021-06-19 op {
312 86e7d7b4 2021-06-19 op if (!config_setvars(var, val))
313 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
314 86e7d7b4 2021-06-19 op var, val);
315 86e7d7b4 2021-06-19 op
316 86e7d7b4 2021-06-19 op free(var);
317 74a2587f 2021-06-21 op }
318 74a2587f 2021-06-21 op
319 74a2587f 2021-06-21 op static int
320 74a2587f 2021-06-21 op colorname(const char *name)
321 74a2587f 2021-06-21 op {
322 74a2587f 2021-06-21 op struct {
323 74a2587f 2021-06-21 op const char *name;
324 74a2587f 2021-06-21 op short val;
325 74a2587f 2021-06-21 op } *i, colors[] = {
326 160abe04 2021-06-21 op { "default", -1 },
327 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
328 74a2587f 2021-06-21 op { "red", COLOR_RED },
329 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
330 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
331 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
332 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
333 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
334 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
335 74a2587f 2021-06-21 op { NULL, 0 },
336 74a2587f 2021-06-21 op };
337 27766d48 2021-06-22 op const char *errstr;
338 27766d48 2021-06-22 op int n;
339 27766d48 2021-06-22 op
340 27766d48 2021-06-22 op if (has_prefix(name, "colo")) {
341 27766d48 2021-06-22 op /* people are strange */
342 27766d48 2021-06-22 op if (has_prefix(name, "color"))
343 27766d48 2021-06-22 op name += 5;
344 27766d48 2021-06-22 op else if (has_prefix(name, "colour"))
345 27766d48 2021-06-22 op name += 6;
346 27766d48 2021-06-22 op else
347 27766d48 2021-06-22 op goto err;
348 74a2587f 2021-06-21 op
349 27766d48 2021-06-22 op n = strtonum(name, 0, 256, &errstr);
350 27766d48 2021-06-22 op if (errstr != NULL)
351 27766d48 2021-06-22 op yyerror("color number is %s: %s", errstr, name);
352 27766d48 2021-06-22 op return n;
353 27766d48 2021-06-22 op }
354 27766d48 2021-06-22 op
355 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
356 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
357 74a2587f 2021-06-21 op return i->val;
358 74a2587f 2021-06-21 op }
359 74a2587f 2021-06-21 op
360 27766d48 2021-06-22 op err:
361 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
362 74a2587f 2021-06-21 op return -1;
363 86e7d7b4 2021-06-19 op }
364 86e7d7b4 2021-06-19 op
365 69bdd906 2021-06-15 op void
366 d89b86d6 2021-06-21 op setcolor(const char *prfx, const char *line, const char *trail)
367 74a2587f 2021-06-21 op {
368 d89b86d6 2021-06-21 op int p, l, t;
369 74a2587f 2021-06-21 op
370 74a2587f 2021-06-21 op assert(current_style != NULL);
371 74a2587f 2021-06-21 op
372 160abe04 2021-06-21 op p = colorname(prfx);
373 160abe04 2021-06-21 op l = colorname(line);
374 160abe04 2021-06-21 op t = colorname(trail);
375 74a2587f 2021-06-21 op
376 d89b86d6 2021-06-21 op if (!config_setcolor(color_type == TBG, current_style, p, l, t))
377 d0149485 2021-06-22 op yyerror("invalid style %s", current_style);
378 d0149485 2021-06-22 op }
379 d0149485 2021-06-22 op
380 d0149485 2021-06-22 op static int
381 d0149485 2021-06-22 op attrname(char *n)
382 d0149485 2021-06-22 op {
383 d0149485 2021-06-22 op struct {
384 d0149485 2021-06-22 op const char *name;
385 d0149485 2021-06-22 op unsigned int val;
386 d0149485 2021-06-22 op } *i, attrs[] = {
387 d0149485 2021-06-22 op { "normal", A_NORMAL },
388 d0149485 2021-06-22 op { "standout", A_STANDOUT },
389 d0149485 2021-06-22 op { "underline", A_UNDERLINE },
390 d0149485 2021-06-22 op { "reverse", A_REVERSE },
391 d0149485 2021-06-22 op { "blink", A_BLINK },
392 d0149485 2021-06-22 op { "dim", A_DIM },
393 d0149485 2021-06-22 op { "bold", A_BOLD },
394 d0149485 2021-06-22 op { NULL, 0 },
395 d0149485 2021-06-22 op };
396 d0149485 2021-06-22 op int ret, found;
397 d0149485 2021-06-22 op char *ap;
398 d0149485 2021-06-22 op
399 d0149485 2021-06-22 op ret = 0;
400 d0149485 2021-06-22 op while ((ap = strsep(&n, ",")) != NULL) {
401 d0149485 2021-06-22 op if (*ap == '\0')
402 d0149485 2021-06-22 op continue;
403 d0149485 2021-06-22 op
404 d0149485 2021-06-22 op found = 0;
405 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
406 d0149485 2021-06-22 op if (strcmp(i->name, ap))
407 d0149485 2021-06-22 op continue;
408 d0149485 2021-06-22 op ret |= i->val;
409 d0149485 2021-06-22 op found = 1;
410 d0149485 2021-06-22 op break;
411 d0149485 2021-06-22 op }
412 d0149485 2021-06-22 op
413 d0149485 2021-06-22 op if (!found)
414 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
415 d0149485 2021-06-22 op ap, yylval.colno+1);
416 d0149485 2021-06-22 op }
417 d0149485 2021-06-22 op
418 d0149485 2021-06-22 op return ret;
419 d0149485 2021-06-22 op }
420 d0149485 2021-06-22 op
421 d0149485 2021-06-22 op static void
422 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
423 d0149485 2021-06-22 op {
424 d0149485 2021-06-22 op int p, l, t;
425 d0149485 2021-06-22 op
426 d0149485 2021-06-22 op assert(current_style != NULL);
427 d0149485 2021-06-22 op
428 d0149485 2021-06-22 op p = attrname(prfx);
429 d0149485 2021-06-22 op l = attrname(line);
430 d0149485 2021-06-22 op t = attrname(trail);
431 d0149485 2021-06-22 op
432 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
433 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
434 74a2587f 2021-06-21 op }
435 74a2587f 2021-06-21 op
436 74a2587f 2021-06-21 op void
437 69bdd906 2021-06-15 op parseconfig(const char *filename, int fonf)
438 69bdd906 2021-06-15 op {
439 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
440 69bdd906 2021-06-15 op if (fonf)
441 69bdd906 2021-06-15 op err(1, "%s", filename);
442 69bdd906 2021-06-15 op return;
443 69bdd906 2021-06-15 op }
444 69bdd906 2021-06-15 op
445 69bdd906 2021-06-15 op path = filename;
446 69bdd906 2021-06-15 op yyparse();
447 69bdd906 2021-06-15 op fclose(yyfp);
448 69bdd906 2021-06-15 op if (parse_errors)
449 69bdd906 2021-06-15 op exit(1);
450 69bdd906 2021-06-15 op }