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 984245ce 2021-06-23 op #include <phos/phos.h>
26 984245ce 2021-06-23 op
27 74a2587f 2021-06-21 op #include <assert.h>
28 69bdd906 2021-06-15 op #include <ctype.h>
29 69bdd906 2021-06-15 op #include <limits.h>
30 74a2587f 2021-06-21 op #include <ncurses.h>
31 69bdd906 2021-06-15 op #include <stdarg.h>
32 69bdd906 2021-06-15 op #include <stdio.h>
33 69bdd906 2021-06-15 op #include <stdlib.h>
34 69bdd906 2021-06-15 op #include <string.h>
35 69bdd906 2021-06-15 op
36 69bdd906 2021-06-15 op typedef struct {
37 69bdd906 2021-06-15 op union {
38 69bdd906 2021-06-15 op char *str;
39 69bdd906 2021-06-15 op int num;
40 69bdd906 2021-06-15 op };
41 69bdd906 2021-06-15 op int lineno;
42 69bdd906 2021-06-15 op int colno;
43 69bdd906 2021-06-15 op } yystype;
44 69bdd906 2021-06-15 op #define YYSTYPE yystype
45 69bdd906 2021-06-15 op
46 69e1d1b6 2021-06-15 op static char *current_style;
47 d89b86d6 2021-06-21 op static int color_type;
48 74a2587f 2021-06-21 op
49 69bdd906 2021-06-15 op static const char *path;
50 69bdd906 2021-06-15 op
51 69bdd906 2021-06-15 op FILE *yyfp;
52 69bdd906 2021-06-15 op
53 69bdd906 2021-06-15 op int parse_errors = 0;
54 69bdd906 2021-06-15 op
55 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
56 69bdd906 2021-06-15 op static int yylex(void);
57 d89b86d6 2021-06-21 op static void setprfx(const char *, const char *);
58 86e7d7b4 2021-06-19 op static void setvari(char *, int);
59 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
60 74a2587f 2021-06-21 op static int colorname(const char *);
61 d89b86d6 2021-06-21 op static void setcolor(const char *, const char *, const char *);
62 3198f30f 2021-08-26 op static int attrname(const char *);
63 d0149485 2021-06-22 op static void setattr(char *, char *, char *);
64 984245ce 2021-06-23 op static void add_proxy(char *, char *);
65 e3427d18 2021-06-25 op static void bindkey(const char *, const char *, const char *);
66 21404dd9 2021-07-15 op static void do_parseconfig(const char *, int);
67 69bdd906 2021-06-15 op
68 69bdd906 2021-06-15 op %}
69 69bdd906 2021-06-15 op
70 69bdd906 2021-06-15 op %token TSET
71 d0149485 2021-06-22 op %token TSTYLE TPRFX TCONT TBG TFG TATTR
72 69bdd906 2021-06-15 op %token TBIND TUNBIND
73 984245ce 2021-06-23 op %token TPROXY TVIA
74 69bdd906 2021-06-15 op
75 69bdd906 2021-06-15 op %token <str> TSTRING
76 69bdd906 2021-06-15 op %token <num> TNUMBER
77 69bdd906 2021-06-15 op
78 69bdd906 2021-06-15 op %%
79 69bdd906 2021-06-15 op
80 69bdd906 2021-06-15 op grammar : /* empty */
81 69bdd906 2021-06-15 op | grammar '\n'
82 69bdd906 2021-06-15 op | grammar rule '\n'
83 69bdd906 2021-06-15 op | error '\n'
84 69bdd906 2021-06-15 op ;
85 69bdd906 2021-06-15 op
86 69e1d1b6 2021-06-15 op rule : set
87 74a2587f 2021-06-21 op | style {
88 74a2587f 2021-06-21 op free(current_style);
89 74a2587f 2021-06-21 op current_style = NULL;
90 74a2587f 2021-06-21 op }
91 69e1d1b6 2021-06-15 op | bind
92 69e1d1b6 2021-06-15 op | unbind
93 984245ce 2021-06-23 op | proxy
94 69e1d1b6 2021-06-15 op ;
95 69bdd906 2021-06-15 op
96 86e7d7b4 2021-06-19 op set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
97 86e7d7b4 2021-06-19 op | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
98 69bdd906 2021-06-15 op ;
99 69bdd906 2021-06-15 op
100 74a2587f 2021-06-21 op style : TSTYLE TSTRING { current_style = $2; } stylespec ;
101 5c38fd5f 2021-08-30 op stylespec : styleopt | '{' optnl styleopts '}' ;
102 69bdd906 2021-06-15 op
103 69bdd906 2021-06-15 op styleopts : /* empty */
104 0a53787c 2021-07-09 op | styleopts styleopt optnl
105 69bdd906 2021-06-15 op ;
106 69bdd906 2021-06-15 op
107 d89b86d6 2021-06-21 op styleopt : TPRFX TSTRING { setprfx($2, $2); }
108 eff23f5d 2021-06-25 op | TPRFX TSTRING TSTRING { setprfx($2, $3); }
109 d89b86d6 2021-06-21 op | TBG { color_type = TBG; } colorspec
110 d89b86d6 2021-06-21 op | TFG { color_type = TFG; } colorspec
111 d0149485 2021-06-22 op | TATTR attr
112 d89b86d6 2021-06-21 op ;
113 d89b86d6 2021-06-21 op
114 d89b86d6 2021-06-21 op colorspec : TSTRING { setcolor($1, $1, $1); free($1); }
115 d89b86d6 2021-06-21 op | TSTRING TSTRING { setcolor($1, $2, $1); free($1); free($2); }
116 d89b86d6 2021-06-21 op | TSTRING TSTRING TSTRING { setcolor($1, $2, $3); free($1); free($2); free($3); }
117 69bdd906 2021-06-15 op ;
118 69bdd906 2021-06-15 op
119 d0149485 2021-06-22 op attr : TSTRING { setattr($1, $1, $1); free($1); }
120 d0149485 2021-06-22 op | TSTRING TSTRING { setattr($1, $2, $1); free($1); free($2); }
121 d0149485 2021-06-22 op | TSTRING TSTRING TSTRING { setattr($1, $2, $3); free($1); free($2); free($3); }
122 d0149485 2021-06-22 op ;
123 d0149485 2021-06-22 op
124 e3427d18 2021-06-25 op bind : TBIND TSTRING TSTRING TSTRING { bindkey($2, $3, $4); free($2); free($3); free($4); }
125 69bdd906 2021-06-15 op ;
126 69bdd906 2021-06-15 op
127 e3427d18 2021-06-25 op unbind : TUNBIND TSTRING TSTRING { yyerror("TODO: unbind %s %s", $2, $3); }
128 69bdd906 2021-06-15 op ;
129 69bdd906 2021-06-15 op
130 984245ce 2021-06-23 op proxy : TPROXY TSTRING TVIA TSTRING { add_proxy($2, $4); free($4); }
131 0a53787c 2021-07-09 op ;
132 0a53787c 2021-07-09 op
133 0a53787c 2021-07-09 op optnl : '\n' optnl /* zero or more newlines */
134 0a53787c 2021-07-09 op | /* empty */
135 984245ce 2021-06-23 op ;
136 984245ce 2021-06-23 op
137 69bdd906 2021-06-15 op %%
138 69bdd906 2021-06-15 op
139 69bdd906 2021-06-15 op void
140 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
141 69bdd906 2021-06-15 op {
142 69bdd906 2021-06-15 op va_list va;
143 69bdd906 2021-06-15 op
144 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
145 69bdd906 2021-06-15 op va_start(va, fmt);
146 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
147 69bdd906 2021-06-15 op va_end(va);
148 69bdd906 2021-06-15 op fprintf(stderr, "\n");
149 69bdd906 2021-06-15 op parse_errors++;
150 69bdd906 2021-06-15 op }
151 69bdd906 2021-06-15 op
152 69bdd906 2021-06-15 op static struct keyword {
153 69bdd906 2021-06-15 op const char *word;
154 69bdd906 2021-06-15 op int token;
155 69bdd906 2021-06-15 op } keywords[] = {
156 69bdd906 2021-06-15 op { "attr", TATTR },
157 984245ce 2021-06-23 op { "bg", TBG },
158 69bdd906 2021-06-15 op { "bind", TBIND },
159 984245ce 2021-06-23 op { "cont", TCONT },
160 984245ce 2021-06-23 op { "fg", TFG },
161 984245ce 2021-06-23 op { "prefix", TPRFX },
162 984245ce 2021-06-23 op { "proxy", TPROXY },
163 984245ce 2021-06-23 op { "set", TSET },
164 984245ce 2021-06-23 op { "style", TSTYLE },
165 69bdd906 2021-06-15 op { "unbind", TUNBIND },
166 984245ce 2021-06-23 op { "via", TVIA },
167 69bdd906 2021-06-15 op };
168 69bdd906 2021-06-15 op
169 69bdd906 2021-06-15 op int
170 69bdd906 2021-06-15 op yylex(void)
171 69bdd906 2021-06-15 op {
172 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
173 69bdd906 2021-06-15 op const char *errstr;
174 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
175 69bdd906 2021-06-15 op size_t i;
176 69bdd906 2021-06-15 op
177 69bdd906 2021-06-15 op p = buf;
178 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
179 69bdd906 2021-06-15 op
180 69bdd906 2021-06-15 op repeat:
181 69bdd906 2021-06-15 op /* skip whitespace first */
182 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
183 69bdd906 2021-06-15 op yylval.colno++;
184 69bdd906 2021-06-15 op
185 69bdd906 2021-06-15 op /* check for special one-character constructions */
186 69bdd906 2021-06-15 op switch (c) {
187 c86d164e 2021-06-24 op case '\r':
188 c86d164e 2021-06-24 op /* silently eat up any \r */
189 c86d164e 2021-06-24 op goto repeat;
190 69bdd906 2021-06-15 op case '\n':
191 69bdd906 2021-06-15 op yylval.colno = 0;
192 69bdd906 2021-06-15 op yylval.lineno++;
193 69bdd906 2021-06-15 op /* fallthrough */
194 69bdd906 2021-06-15 op case '{':
195 69bdd906 2021-06-15 op case '}':
196 69bdd906 2021-06-15 op case '=':
197 69bdd906 2021-06-15 op return c;
198 69bdd906 2021-06-15 op case '#':
199 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
200 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
201 69bdd906 2021-06-15 op if (c == EOF)
202 69bdd906 2021-06-15 op goto eof;
203 69bdd906 2021-06-15 op yylval.colno = 0;
204 69bdd906 2021-06-15 op yylval.lineno++;
205 69bdd906 2021-06-15 op return c;
206 69bdd906 2021-06-15 op case EOF:
207 69bdd906 2021-06-15 op goto eof;
208 69bdd906 2021-06-15 op }
209 69bdd906 2021-06-15 op
210 69bdd906 2021-06-15 op /* parsing next word */
211 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
212 69bdd906 2021-06-15 op switch (c) {
213 69bdd906 2021-06-15 op case '\0':
214 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
215 69bdd906 2021-06-15 op yylval.colno+1);
216 69bdd906 2021-06-15 op escape = 0;
217 69bdd906 2021-06-15 op continue;
218 69bdd906 2021-06-15 op case '\\':
219 69bdd906 2021-06-15 op escape = !escape;
220 69bdd906 2021-06-15 op if (escape)
221 69bdd906 2021-06-15 op continue;
222 69bdd906 2021-06-15 op break;
223 6ba9e5a8 2021-06-24 op case '\r':
224 6ba9e5a8 2021-06-24 op /* ignore \r here too */
225 6ba9e5a8 2021-06-24 op continue;
226 69bdd906 2021-06-15 op case '\n':
227 69bdd906 2021-06-15 op if (quotes)
228 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
229 69bdd906 2021-06-15 op yylval.colno+1);
230 69bdd906 2021-06-15 op if (escape) {
231 69bdd906 2021-06-15 op nonkw = 1;
232 69bdd906 2021-06-15 op escape = 0;
233 69bdd906 2021-06-15 op yylval.colno = 0;
234 69bdd906 2021-06-15 op yylval.lineno++;
235 69bdd906 2021-06-15 op }
236 69bdd906 2021-06-15 op goto eow;
237 69bdd906 2021-06-15 op case EOF:
238 69bdd906 2021-06-15 op if (escape)
239 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
240 69bdd906 2021-06-15 op yylval.colno);
241 69bdd906 2021-06-15 op if (quotes)
242 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
243 69bdd906 2021-06-15 op qpos + 1);
244 69bdd906 2021-06-15 op goto eow;
245 69bdd906 2021-06-15 op case '{':
246 69bdd906 2021-06-15 op case '}':
247 69bdd906 2021-06-15 op case '=':
248 69bdd906 2021-06-15 op case '#':
249 69bdd906 2021-06-15 op case ' ':
250 69bdd906 2021-06-15 op case '\t':
251 69bdd906 2021-06-15 op if (!escape && !quotes)
252 69bdd906 2021-06-15 op goto eow;
253 69bdd906 2021-06-15 op break;
254 69bdd906 2021-06-15 op case '"':
255 69bdd906 2021-06-15 op if (!escape) {
256 69bdd906 2021-06-15 op quotes = !quotes;
257 69bdd906 2021-06-15 op if (quotes) {
258 69bdd906 2021-06-15 op nonkw = 1;
259 69bdd906 2021-06-15 op qpos = yylval.colno;
260 69bdd906 2021-06-15 op }
261 69bdd906 2021-06-15 op continue;
262 69bdd906 2021-06-15 op }
263 69bdd906 2021-06-15 op }
264 69bdd906 2021-06-15 op *p++ = c;
265 69bdd906 2021-06-15 op if (p == ebuf) {
266 69bdd906 2021-06-15 op yyerror("line too long");
267 69bdd906 2021-06-15 op p = buf;
268 69bdd906 2021-06-15 op }
269 69bdd906 2021-06-15 op escape = 0;
270 69bdd906 2021-06-15 op }
271 69bdd906 2021-06-15 op
272 69bdd906 2021-06-15 op eow:
273 69bdd906 2021-06-15 op *p = 0;
274 69bdd906 2021-06-15 op if (c != EOF)
275 69bdd906 2021-06-15 op ungetc(c, yyfp);
276 69bdd906 2021-06-15 op if (p == buf) {
277 69bdd906 2021-06-15 op /*
278 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
279 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
280 69bdd906 2021-06-15 op * the main loop.
281 69bdd906 2021-06-15 op */
282 69bdd906 2021-06-15 op if (c == EOF)
283 69bdd906 2021-06-15 op goto eof;
284 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
285 69bdd906 2021-06-15 op goto repeat;
286 69bdd906 2021-06-15 op }
287 69bdd906 2021-06-15 op if (!nonkw) {
288 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
289 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
290 69bdd906 2021-06-15 op return keywords[i].token;
291 69bdd906 2021-06-15 op }
292 69bdd906 2021-06-15 op }
293 69bdd906 2021-06-15 op c = *buf;
294 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
295 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
296 69bdd906 2021-06-15 op if (errstr != NULL)
297 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
298 69bdd906 2021-06-15 op return TNUMBER;
299 69bdd906 2021-06-15 op }
300 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
301 69bdd906 2021-06-15 op err(1, "%s", __func__);
302 69bdd906 2021-06-15 op yylval.str = str;
303 69bdd906 2021-06-15 op return TSTRING;
304 69bdd906 2021-06-15 op
305 69bdd906 2021-06-15 op eof:
306 69bdd906 2021-06-15 op if (ferror(yyfp))
307 69bdd906 2021-06-15 op yyerror("input error reading config");
308 69bdd906 2021-06-15 op return 0;
309 69bdd906 2021-06-15 op }
310 69bdd906 2021-06-15 op
311 69e1d1b6 2021-06-15 op static void
312 d89b86d6 2021-06-21 op setprfx(const char *prfx, const char *cont)
313 69e1d1b6 2021-06-15 op {
314 74a2587f 2021-06-21 op assert(current_style != NULL);
315 69e1d1b6 2021-06-15 op
316 d89b86d6 2021-06-21 op if (!config_setprfx(current_style, prfx, cont))
317 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
318 86e7d7b4 2021-06-19 op }
319 86e7d7b4 2021-06-19 op
320 86e7d7b4 2021-06-19 op static void
321 86e7d7b4 2021-06-19 op setvari(char *var, int val)
322 86e7d7b4 2021-06-19 op {
323 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
324 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
325 86e7d7b4 2021-06-19 op var, val);
326 86e7d7b4 2021-06-19 op
327 86e7d7b4 2021-06-19 op free(var);
328 69e1d1b6 2021-06-15 op }
329 69e1d1b6 2021-06-15 op
330 86e7d7b4 2021-06-19 op static void
331 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
332 86e7d7b4 2021-06-19 op {
333 95a8c791 2021-08-26 op if (!config_setvars(var, val))
334 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
335 86e7d7b4 2021-06-19 op var, val);
336 86e7d7b4 2021-06-19 op
337 86e7d7b4 2021-06-19 op free(var);
338 74a2587f 2021-06-21 op }
339 74a2587f 2021-06-21 op
340 74a2587f 2021-06-21 op static int
341 74a2587f 2021-06-21 op colorname(const char *name)
342 74a2587f 2021-06-21 op {
343 74a2587f 2021-06-21 op struct {
344 74a2587f 2021-06-21 op const char *name;
345 74a2587f 2021-06-21 op short val;
346 74a2587f 2021-06-21 op } *i, colors[] = {
347 160abe04 2021-06-21 op { "default", -1 },
348 74a2587f 2021-06-21 op { "black", COLOR_BLACK },
349 74a2587f 2021-06-21 op { "red", COLOR_RED },
350 74a2587f 2021-06-21 op { "green", COLOR_GREEN },
351 74a2587f 2021-06-21 op { "yellow", COLOR_YELLOW },
352 74a2587f 2021-06-21 op { "blue", COLOR_BLUE },
353 74a2587f 2021-06-21 op { "magenta", COLOR_MAGENTA },
354 74a2587f 2021-06-21 op { "cyan", COLOR_CYAN },
355 74a2587f 2021-06-21 op { "white", COLOR_WHITE },
356 74a2587f 2021-06-21 op { NULL, 0 },
357 74a2587f 2021-06-21 op };
358 27766d48 2021-06-22 op const char *errstr;
359 27766d48 2021-06-22 op int n;
360 27766d48 2021-06-22 op
361 27766d48 2021-06-22 op if (has_prefix(name, "colo")) {
362 27766d48 2021-06-22 op /* people are strange */
363 27766d48 2021-06-22 op if (has_prefix(name, "color"))
364 27766d48 2021-06-22 op name += 5;
365 27766d48 2021-06-22 op else if (has_prefix(name, "colour"))
366 27766d48 2021-06-22 op name += 6;
367 27766d48 2021-06-22 op else
368 27766d48 2021-06-22 op goto err;
369 74a2587f 2021-06-21 op
370 27766d48 2021-06-22 op n = strtonum(name, 0, 256, &errstr);
371 27766d48 2021-06-22 op if (errstr != NULL)
372 27766d48 2021-06-22 op yyerror("color number is %s: %s", errstr, name);
373 27766d48 2021-06-22 op return n;
374 27766d48 2021-06-22 op }
375 27766d48 2021-06-22 op
376 74a2587f 2021-06-21 op for (i = colors; i->name != NULL; ++i) {
377 74a2587f 2021-06-21 op if (!strcmp(i->name, name))
378 74a2587f 2021-06-21 op return i->val;
379 74a2587f 2021-06-21 op }
380 74a2587f 2021-06-21 op
381 27766d48 2021-06-22 op err:
382 74a2587f 2021-06-21 op yyerror("unknown color name \"%s\"", name);
383 74a2587f 2021-06-21 op return -1;
384 86e7d7b4 2021-06-19 op }
385 86e7d7b4 2021-06-19 op
386 69bdd906 2021-06-15 op void
387 d89b86d6 2021-06-21 op setcolor(const char *prfx, const char *line, const char *trail)
388 74a2587f 2021-06-21 op {
389 d89b86d6 2021-06-21 op int p, l, t;
390 74a2587f 2021-06-21 op
391 74a2587f 2021-06-21 op assert(current_style != NULL);
392 74a2587f 2021-06-21 op
393 160abe04 2021-06-21 op p = colorname(prfx);
394 160abe04 2021-06-21 op l = colorname(line);
395 160abe04 2021-06-21 op t = colorname(trail);
396 74a2587f 2021-06-21 op
397 d89b86d6 2021-06-21 op if (!config_setcolor(color_type == TBG, current_style, p, l, t))
398 d0149485 2021-06-22 op yyerror("invalid style %s", current_style);
399 d0149485 2021-06-22 op }
400 d0149485 2021-06-22 op
401 d0149485 2021-06-22 op static int
402 3198f30f 2021-08-26 op attrname(const char *n)
403 d0149485 2021-06-22 op {
404 d0149485 2021-06-22 op struct {
405 d0149485 2021-06-22 op const char *name;
406 d0149485 2021-06-22 op unsigned int val;
407 d0149485 2021-06-22 op } *i, attrs[] = {
408 d0149485 2021-06-22 op { "normal", A_NORMAL },
409 d0149485 2021-06-22 op { "standout", A_STANDOUT },
410 d0149485 2021-06-22 op { "underline", A_UNDERLINE },
411 d0149485 2021-06-22 op { "reverse", A_REVERSE },
412 d0149485 2021-06-22 op { "blink", A_BLINK },
413 d0149485 2021-06-22 op { "dim", A_DIM },
414 d0149485 2021-06-22 op { "bold", A_BOLD },
415 d0149485 2021-06-22 op { NULL, 0 },
416 d0149485 2021-06-22 op };
417 d0149485 2021-06-22 op int ret, found;
418 3198f30f 2021-08-26 op char *ap, *dup, *orig;
419 3198f30f 2021-08-26 op
420 3198f30f 2021-08-26 op if ((dup = strdup(n)) == NULL)
421 3198f30f 2021-08-26 op err(1, "strdup");
422 d0149485 2021-06-22 op
423 3198f30f 2021-08-26 op orig = dup;
424 3198f30f 2021-08-26 op
425 d0149485 2021-06-22 op ret = 0;
426 3198f30f 2021-08-26 op while ((ap = strsep(&dup, ",")) != NULL) {
427 d0149485 2021-06-22 op if (*ap == '\0')
428 d0149485 2021-06-22 op continue;
429 d0149485 2021-06-22 op
430 d0149485 2021-06-22 op found = 0;
431 d0149485 2021-06-22 op for (i = attrs; i ->name != NULL; ++i) {
432 d0149485 2021-06-22 op if (strcmp(i->name, ap))
433 d0149485 2021-06-22 op continue;
434 d0149485 2021-06-22 op ret |= i->val;
435 d0149485 2021-06-22 op found = 1;
436 d0149485 2021-06-22 op break;
437 d0149485 2021-06-22 op }
438 d0149485 2021-06-22 op
439 d0149485 2021-06-22 op if (!found)
440 d0149485 2021-06-22 op yyerror("unknown attribute \"%s\" at col %d",
441 d0149485 2021-06-22 op ap, yylval.colno+1);
442 d0149485 2021-06-22 op }
443 d0149485 2021-06-22 op
444 3198f30f 2021-08-26 op free(orig);
445 d0149485 2021-06-22 op return ret;
446 d0149485 2021-06-22 op }
447 d0149485 2021-06-22 op
448 d0149485 2021-06-22 op static void
449 d0149485 2021-06-22 op setattr(char *prfx, char *line, char *trail)
450 d0149485 2021-06-22 op {
451 d0149485 2021-06-22 op int p, l, t;
452 d0149485 2021-06-22 op
453 d0149485 2021-06-22 op assert(current_style != NULL);
454 d0149485 2021-06-22 op
455 d0149485 2021-06-22 op p = attrname(prfx);
456 d0149485 2021-06-22 op l = attrname(line);
457 d0149485 2021-06-22 op t = attrname(trail);
458 d0149485 2021-06-22 op
459 d0149485 2021-06-22 op if (!config_setattr(current_style, p, l, t))
460 d89b86d6 2021-06-21 op yyerror("invalid style %s", current_style);
461 74a2587f 2021-06-21 op }
462 74a2587f 2021-06-21 op
463 984245ce 2021-06-23 op static void
464 984245ce 2021-06-23 op add_proxy(char *proto, char *proxy)
465 984245ce 2021-06-23 op {
466 984245ce 2021-06-23 op struct proxy *p;
467 984245ce 2021-06-23 op struct phos_uri uri;
468 984245ce 2021-06-23 op
469 984245ce 2021-06-23 op if (!phos_parse_absolute_uri(proxy, &uri)) {
470 984245ce 2021-06-23 op yyerror("can't parse URL: %s", proxy);
471 984245ce 2021-06-23 op return;
472 984245ce 2021-06-23 op }
473 984245ce 2021-06-23 op
474 984245ce 2021-06-23 op if (*uri.path != '\0' || *uri.query != '\0' || *uri.fragment != '\0') {
475 984245ce 2021-06-23 op yyerror("proxy url can't have path, query or fragments");
476 984245ce 2021-06-23 op return;
477 984245ce 2021-06-23 op }
478 984245ce 2021-06-23 op
479 984245ce 2021-06-23 op if (strcmp(uri.scheme, "gemini")) {
480 984245ce 2021-06-23 op yyerror("disallowed proxy protocol %s", uri.scheme);
481 984245ce 2021-06-23 op return;
482 984245ce 2021-06-23 op }
483 984245ce 2021-06-23 op
484 984245ce 2021-06-23 op if ((p = calloc(1, sizeof(*p))) == NULL)
485 984245ce 2021-06-23 op err(1, "calloc");
486 984245ce 2021-06-23 op
487 984245ce 2021-06-23 op p->match_proto = proto;
488 8da4eaff 2021-07-25 op p->proto = PROTO_GEMINI;
489 984245ce 2021-06-23 op
490 984245ce 2021-06-23 op if ((p->host = strdup(uri.host)) == NULL)
491 984245ce 2021-06-23 op err(1, "strdup");
492 984245ce 2021-06-23 op
493 984245ce 2021-06-23 op if ((p->port = strdup(uri.port)) == NULL)
494 984245ce 2021-06-23 op err(1, "strdup");
495 984245ce 2021-06-23 op
496 984245ce 2021-06-23 op TAILQ_INSERT_HEAD(&proxies, p, proxies);
497 e3427d18 2021-06-25 op }
498 e3427d18 2021-06-25 op
499 e3427d18 2021-06-25 op static interactivefn *
500 e3427d18 2021-06-25 op cmdname(const char *name)
501 e3427d18 2021-06-25 op {
502 e3427d18 2021-06-25 op struct cmd *cmd;
503 e3427d18 2021-06-25 op
504 95a8c791 2021-08-26 op for (cmd = cmds; cmd->cmd != NULL; ++cmd) {
505 e3427d18 2021-06-25 op if (!strcmp(cmd->cmd, name))
506 e3427d18 2021-06-25 op return cmd->fn;
507 e3427d18 2021-06-25 op }
508 e3427d18 2021-06-25 op
509 e3427d18 2021-06-25 op return NULL;
510 e3427d18 2021-06-25 op }
511 e3427d18 2021-06-25 op
512 e3427d18 2021-06-25 op static void
513 e3427d18 2021-06-25 op bindkey(const char *map, const char *key, const char *cmd)
514 e3427d18 2021-06-25 op {
515 e3427d18 2021-06-25 op struct kmap *kmap;
516 e3427d18 2021-06-25 op interactivefn *fn;
517 e3427d18 2021-06-25 op
518 e3427d18 2021-06-25 op if (!strcmp(map, "global-map"))
519 e3427d18 2021-06-25 op kmap = &global_map;
520 e3427d18 2021-06-25 op else if (!strcmp(map, "minibuffer-map"))
521 e3427d18 2021-06-25 op kmap = &minibuffer_map;
522 e3427d18 2021-06-25 op else {
523 e3427d18 2021-06-25 op yyerror("unknown map: %s", map);
524 e3427d18 2021-06-25 op return;
525 e3427d18 2021-06-25 op }
526 e3427d18 2021-06-25 op
527 e3427d18 2021-06-25 op if ((fn = cmdname(cmd)) == NULL) {
528 e3427d18 2021-06-25 op yyerror("unknown cmd: %s", fn);
529 e3427d18 2021-06-25 op return;
530 e3427d18 2021-06-25 op }
531 e3427d18 2021-06-25 op
532 e3427d18 2021-06-25 op if (!kmap_define_key(kmap, key, fn))
533 e3427d18 2021-06-25 op yyerror("failed to bind %s %s %s", map, key, cmd);
534 984245ce 2021-06-23 op }
535 984245ce 2021-06-23 op
536 21404dd9 2021-07-15 op static void
537 21404dd9 2021-07-15 op do_parseconfig(const char *filename, int fonf)
538 69bdd906 2021-06-15 op {
539 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
540 69bdd906 2021-06-15 op if (fonf)
541 69bdd906 2021-06-15 op err(1, "%s", filename);
542 69bdd906 2021-06-15 op return;
543 69bdd906 2021-06-15 op }
544 69bdd906 2021-06-15 op
545 69bdd906 2021-06-15 op path = filename;
546 69bdd906 2021-06-15 op yyparse();
547 69bdd906 2021-06-15 op fclose(yyfp);
548 69bdd906 2021-06-15 op if (parse_errors)
549 69bdd906 2021-06-15 op exit(1);
550 69bdd906 2021-06-15 op }
551 21404dd9 2021-07-15 op
552 21404dd9 2021-07-15 op void
553 21404dd9 2021-07-15 op parseconfig(const char *filename, int fonf)
554 21404dd9 2021-07-15 op {
555 21404dd9 2021-07-15 op char altconf[PATH_MAX], *term;
556 21404dd9 2021-07-15 op
557 21404dd9 2021-07-15 op /* load the given config file */
558 21404dd9 2021-07-15 op do_parseconfig(filename, fonf);
559 21404dd9 2021-07-15 op
560 21404dd9 2021-07-15 op /* then try to load file-TERM */
561 21404dd9 2021-07-15 op
562 21404dd9 2021-07-15 op if ((term = getenv("TERM")) == NULL)
563 21404dd9 2021-07-15 op return;
564 21404dd9 2021-07-15 op
565 21404dd9 2021-07-15 op strlcpy(altconf, filename, sizeof(altconf));
566 21404dd9 2021-07-15 op strlcat(altconf, "-", sizeof(altconf));
567 21404dd9 2021-07-15 op strlcat(altconf, term, sizeof(altconf));
568 21404dd9 2021-07-15 op
569 21404dd9 2021-07-15 op do_parseconfig(altconf, 0);
570 21404dd9 2021-07-15 op }