2 69bdd906 2021-06-15 op * Much of the design is taken from doas (parse.y rev 1.29)
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>
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.
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.
23 69bdd906 2021-06-15 op #include "telescope.h"
25 69bdd906 2021-06-15 op #include <ctype.h>
26 69bdd906 2021-06-15 op #include <limits.h>
27 69bdd906 2021-06-15 op #include <stdarg.h>
28 69bdd906 2021-06-15 op #include <stdio.h>
29 69bdd906 2021-06-15 op #include <stdlib.h>
30 69bdd906 2021-06-15 op #include <string.h>
32 69bdd906 2021-06-15 op typedef struct {
40 69bdd906 2021-06-15 op #define YYSTYPE yystype
42 69e1d1b6 2021-06-15 op static char *current_style;
43 69bdd906 2021-06-15 op static const char *path;
47 69bdd906 2021-06-15 op int parse_errors = 0;
49 69bdd906 2021-06-15 op static void yyerror(const char *, ...);
50 69bdd906 2021-06-15 op static int yylex(void);
51 2ca05f73 2021-06-18 op static void setprfx(int, const char *);
52 86e7d7b4 2021-06-19 op static void setvari(char *, int);
53 86e7d7b4 2021-06-19 op static void setvars(char *, char *);
58 69bdd906 2021-06-15 op %token TSTYLE TPRFX TCONT TBG TFG TATTR TBOLD TUNDERLINE
59 69bdd906 2021-06-15 op %token TBIND TUNBIND
61 69bdd906 2021-06-15 op %token <str> TSTRING
62 69bdd906 2021-06-15 op %token <num> TNUMBER
66 69bdd906 2021-06-15 op grammar : /* empty */
67 69bdd906 2021-06-15 op | grammar '\n'
68 69bdd906 2021-06-15 op | grammar rule '\n'
73 69e1d1b6 2021-06-15 op | style { free(current_style); current_style = NULL; }
78 86e7d7b4 2021-06-19 op set : TSET TSTRING '=' TSTRING { setvars($2, $4); }
79 86e7d7b4 2021-06-19 op | TSET TSTRING '=' TNUMBER { setvari($2, $4); }
82 69e1d1b6 2021-06-15 op style : TSTYLE TSTRING { current_style = $2; } styleopt
83 69e1d1b6 2021-06-15 op | TSTYLE TSTRING { current_style = $2; } '{' styleopts '}'
86 69bdd906 2021-06-15 op styleopts : /* empty */
87 69bdd906 2021-06-15 op | styleopts '\n'
88 69bdd906 2021-06-15 op | styleopts styleopt '\n'
91 69e1d1b6 2021-06-15 op styleopt : TPRFX TSTRING { setprfx(0, $2); }
92 69e1d1b6 2021-06-15 op | TCONT TSTRING { setprfx(1, $2); }
93 69bdd906 2021-06-15 op | TBG TSTRING { printf("style background setted to \"%s\"\n", $2); }
94 69bdd906 2021-06-15 op | TFG TSTRING { printf("style foreground setted to \"%s\"\n", $2); }
95 69bdd906 2021-06-15 op | TATTR TBOLD { printf("style attr setted to bold\n"); }
96 69bdd906 2021-06-15 op | TATTR TUNDERLINE { printf("style attr setted to underline\n"); }
99 69bdd906 2021-06-15 op bind : TBIND TSTRING TSTRING TSTRING { printf("TODO: bind %s %s %s\n", $2, $3, $4); }
102 69bdd906 2021-06-15 op unbind : TUNBIND TSTRING TSTRING { printf("TODO: unbind %s %s\n", $2, $3); }
108 69bdd906 2021-06-15 op yyerror(const char *fmt, ...)
112 69bdd906 2021-06-15 op fprintf(stderr, "%s:%d ", path, yylval.lineno+1);
113 69bdd906 2021-06-15 op va_start(va, fmt);
114 69bdd906 2021-06-15 op vfprintf(stderr, fmt, va);
116 69bdd906 2021-06-15 op fprintf(stderr, "\n");
117 69bdd906 2021-06-15 op parse_errors++;
120 69bdd906 2021-06-15 op static struct keyword {
121 69bdd906 2021-06-15 op const char *word;
123 69bdd906 2021-06-15 op } keywords[] = {
124 69bdd906 2021-06-15 op { "set", TSET },
125 69bdd906 2021-06-15 op { "style", TSTYLE },
126 69bdd906 2021-06-15 op { "prefix", TPRFX },
127 69bdd906 2021-06-15 op { "cont", TCONT },
128 69bdd906 2021-06-15 op { "background", TBG },
129 69bdd906 2021-06-15 op { "foreground", TFG },
130 69bdd906 2021-06-15 op { "attr", TATTR },
131 69bdd906 2021-06-15 op { "bold", TBOLD },
132 69bdd906 2021-06-15 op { "underline", TUNDERLINE },
133 69bdd906 2021-06-15 op { "bind", TBIND },
134 69bdd906 2021-06-15 op { "unbind", TUNBIND },
140 69bdd906 2021-06-15 op char buf[1024], *ebuf, *p, *str;
141 69bdd906 2021-06-15 op const char *errstr;
142 69bdd906 2021-06-15 op int c, quotes = 0, escape = 0, qpos = -1, nonkw = 0;
146 69bdd906 2021-06-15 op ebuf = buf + sizeof(buf);
149 69bdd906 2021-06-15 op /* skip whitespace first */
150 69bdd906 2021-06-15 op for (c = getc(yyfp); c == ' ' || c == '\t' || c == '\f'; c = getc(yyfp))
151 69bdd906 2021-06-15 op yylval.colno++;
153 69bdd906 2021-06-15 op /* check for special one-character constructions */
156 69bdd906 2021-06-15 op yylval.colno = 0;
157 69bdd906 2021-06-15 op yylval.lineno++;
158 69bdd906 2021-06-15 op /* fallthrough */
164 69bdd906 2021-06-15 op /* skip comments; NUL is allowed; no continuation */
165 69bdd906 2021-06-15 op while ((c = getc(yyfp)) != '\n')
166 69bdd906 2021-06-15 op if (c == EOF)
168 69bdd906 2021-06-15 op yylval.colno = 0;
169 69bdd906 2021-06-15 op yylval.lineno++;
175 69bdd906 2021-06-15 op /* parsing next word */
176 69bdd906 2021-06-15 op for (;; c = getc(yyfp), yylval.colno++) {
179 69bdd906 2021-06-15 op yyerror("unallowed character NULL in column %d",
180 69bdd906 2021-06-15 op yylval.colno+1);
184 69bdd906 2021-06-15 op escape = !escape;
190 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
191 69bdd906 2021-06-15 op yylval.colno+1);
192 69bdd906 2021-06-15 op if (escape) {
195 69bdd906 2021-06-15 op yylval.colno = 0;
196 69bdd906 2021-06-15 op yylval.lineno++;
201 69bdd906 2021-06-15 op yyerror("unterminated escape in column %d",
202 69bdd906 2021-06-15 op yylval.colno);
204 69bdd906 2021-06-15 op yyerror("unterminated quotes in column %d",
213 69bdd906 2021-06-15 op if (!escape && !quotes)
217 69bdd906 2021-06-15 op if (!escape) {
218 69bdd906 2021-06-15 op quotes = !quotes;
219 69bdd906 2021-06-15 op if (quotes) {
221 69bdd906 2021-06-15 op qpos = yylval.colno;
227 69bdd906 2021-06-15 op if (p == ebuf) {
228 69bdd906 2021-06-15 op yyerror("line too long");
236 69bdd906 2021-06-15 op if (c != EOF)
237 69bdd906 2021-06-15 op ungetc(c, yyfp);
238 69bdd906 2021-06-15 op if (p == buf) {
240 69bdd906 2021-06-15 op * There could be a number of reason for empty buffer,
241 69bdd906 2021-06-15 op * and we handle all of them here, to avoid cluttering
242 69bdd906 2021-06-15 op * the main loop.
244 69bdd906 2021-06-15 op if (c == EOF)
246 69bdd906 2021-06-15 op else if (qpos == -1) /* accept, e.g., empty args: cmd foo args "" */
249 69bdd906 2021-06-15 op if (!nonkw) {
250 69bdd906 2021-06-15 op for (i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
251 69bdd906 2021-06-15 op if (strcmp(buf, keywords[i].word) == 0)
252 69bdd906 2021-06-15 op return keywords[i].token;
256 69bdd906 2021-06-15 op if (!nonkw && (c == '-' || isdigit(c))) {
257 69bdd906 2021-06-15 op yylval.num = strtonum(buf, INT_MIN, INT_MAX, &errstr);
258 69bdd906 2021-06-15 op if (errstr != NULL)
259 69bdd906 2021-06-15 op yyerror("number is %s: %s", errstr, buf);
260 69bdd906 2021-06-15 op return TNUMBER;
262 69bdd906 2021-06-15 op if ((str = strdup(buf)) == NULL)
263 69bdd906 2021-06-15 op err(1, "%s", __func__);
264 69bdd906 2021-06-15 op yylval.str = str;
265 69bdd906 2021-06-15 op return TSTRING;
268 69bdd906 2021-06-15 op if (ferror(yyfp))
269 69bdd906 2021-06-15 op yyerror("input error reading config");
274 69e1d1b6 2021-06-15 op setprfx(int cont, const char *name)
276 69e1d1b6 2021-06-15 op if (current_style == NULL) {
277 69e1d1b6 2021-06-15 op warnx("current_style = NULL!");
281 69e1d1b6 2021-06-15 op if (!config_setprfx(current_style, cont, name))
282 69e1d1b6 2021-06-15 op yyerror("invalid style %s", name);
286 86e7d7b4 2021-06-19 op setvari(char *var, int val)
288 86e7d7b4 2021-06-19 op if (!config_setvari(var, val))
289 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = %d",
296 86e7d7b4 2021-06-19 op setvars(char *var, char *val)
298 86e7d7b4 2021-06-19 op if (!config_setvars(var, val))
299 86e7d7b4 2021-06-19 op yyerror("invalid variable or value: %s = \"%s\"",
306 69bdd906 2021-06-15 op parseconfig(const char *filename, int fonf)
308 69bdd906 2021-06-15 op if ((yyfp = fopen(filename, "r")) == NULL) {
310 69bdd906 2021-06-15 op err(1, "%s", filename);
314 69bdd906 2021-06-15 op path = filename;
316 69bdd906 2021-06-15 op fclose(yyfp);
317 69bdd906 2021-06-15 op if (parse_errors)