]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
3fba50578fc6d15e9db54b3eadfb8c181b8e154f
[i3/i3] / src / cfgparse.y
1 %{
2 /*
3  * vim:ts=8:expandtab
4  *
5  */
6 #include <stdio.h>
7 #include <string.h>
8 #include <xcb/xcb.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <unistd.h>
12 #include <fcntl.h>
13 #include <stdlib.h>
14 #include <errno.h>
15
16 #include "data.h"
17 #include "config.h"
18 #include "i3.h"
19 #include "util.h"
20 #include "queue.h"
21 #include "table.h"
22 #include "workspace.h"
23 #include "xcb.h"
24
25 extern int yylex(void);
26 extern FILE *yyin;
27
28 int yydebug = 1;
29
30 void yyerror(const char *str) {
31         fprintf(stderr,"error: %s\n",str);
32 }
33
34 int yywrap() {
35         return 1;
36 }
37
38 void parse_file(const char *f) {
39         SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
40         int fd, ret, read_bytes = 0;
41         struct stat stbuf;
42         char *buf;
43         FILE *fstr;
44         char buffer[1026], key[512], value[512];
45
46         if ((fd = open(f, O_RDONLY)) == -1)
47                 die("Could not open configuration file: %s\n", strerror(errno));
48
49         if (fstat(fd, &stbuf) == -1)
50                 die("Could not fstat file: %s\n", strerror(errno));
51
52         buf = smalloc(stbuf.st_size * sizeof(char));
53         while (read_bytes < stbuf.st_size) {
54                 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
55                         die("Could not read(): %s\n", strerror(errno));
56                 read_bytes += ret;
57         }
58
59         if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
60                 die("Could not lseek: %s\n", strerror(errno));
61
62         if ((fstr = fdopen(fd, "r")) == NULL)
63                 die("Could not fdopen: %s\n", strerror(errno));
64
65         while (!feof(fstr)) {
66                 if (fgets(buffer, 1024, fstr) == NULL) {
67                         if (feof(fstr))
68                                 break;
69                         die("Could not read configuration file\n");
70                 }
71
72                 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
73                 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
74                     key[0] == '#' || strlen(key) < 3)
75                         continue;
76
77                 if (strcasecmp(key, "set") == 0) {
78                         if (value[0] != '$')
79                                 die("Malformed variable assignment, name has to start with $\n");
80
81                         /* get key/value for this variable */
82                         char *v_key = value, *v_value;
83                         if ((v_value = strstr(value, " ")) == NULL)
84                                 die("Malformed variable assignment, need a value\n");
85
86                         *(v_value++) = '\0';
87
88                         struct Variable *new = scalloc(sizeof(struct Variable));
89                         new->key = sstrdup(v_key);
90                         new->value = sstrdup(v_value);
91                         SLIST_INSERT_HEAD(&variables, new, variables);
92                         LOG("Got new variable %s = %s\n", v_key, v_value);
93                         continue;
94                 }
95         }
96
97         /* For every custom variable, see how often it occurs in the file and
98          * how much extra bytes it requires when replaced. */
99         struct Variable *current, *nearest;
100         int extra_bytes = 0;
101         SLIST_FOREACH(current, &variables, variables) {
102                 int extra = (strlen(current->value) - strlen(current->key));
103                 char *next;
104                 for (next = buf;
105                      (next = strcasestr(buf + (next - buf), current->key)) != NULL;
106                      next += strlen(current->key))
107                         extra_bytes += extra;
108         }
109
110         /* Then, allocate a new buffer and copy the file over to the new one,
111          * but replace occurences of our variables */
112         char *walk = buf, *destwalk;
113         char *new = smalloc((stbuf.st_size + extra_bytes) * sizeof(char));
114         destwalk = new;
115         while (walk < (buf + stbuf.st_size)) {
116                 /* Find the next variable */
117                 SLIST_FOREACH(current, &variables, variables)
118                         current->next_match = strcasestr(walk, current->key);
119                 nearest = NULL;
120                 int distance = stbuf.st_size;
121                 SLIST_FOREACH(current, &variables, variables) {
122                         if (current->next_match == NULL)
123                                 continue;
124                         if ((current->next_match - walk) < distance) {
125                                 distance = (current->next_match - walk);
126                                 nearest = current;
127                         }
128                 }
129                 if (nearest == NULL) {
130                         /* If there are no more variables, we just copy the rest */
131                         strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
132                         destwalk += (buf + stbuf.st_size) - walk;
133                         *destwalk = '\0';
134                         break;
135                 } else {
136                         /* Copy until the next variable, then copy its value */
137                         strncpy(destwalk, walk, distance);
138                         strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
139                         walk += distance + strlen(nearest->key);
140                         destwalk += distance + strlen(nearest->value);
141                 }
142         }
143
144         yy_scan_string(new);
145
146         if (yyparse() != 0) {
147                 fprintf(stderr, "Could not parse configfile\n");
148                 exit(1);
149         }
150
151         free(new);
152         free(buf);
153 }
154
155 %}
156
157 %union {
158         int number;
159         char *string;
160         struct Colortriple *color;
161         struct Assignment *assignment;
162 }
163
164 %token <number>NUMBER
165 %token <string>WORD
166 %token <string>STR
167 %token <string>STR_NG
168 %token <string>HEX
169 %token TOKBIND
170 %token TOKTERMINAL
171 %token TOKCOMMENT
172 %token TOKFONT
173 %token TOKBINDSYM
174 %token MODIFIER
175 %token TOKCONTROL
176 %token TOKSHIFT
177 %token WHITESPACE
178 %token TOKFLOATING_MODIFIER
179 %token QUOTEDSTRING
180 %token TOKWORKSPACE
181 %token TOKSCREEN
182 %token TOKASSIGN
183 %token TOKSET
184 %token TOKIPCSOCKET
185 %token TOKEXEC
186 %token TOKCOLOR
187 %token TOKARROW
188
189 %%
190
191 lines: /* empty */
192         | lines WHITESPACE line
193         | lines line
194         ;
195
196 line:
197         bind
198         | bindsym
199         | floating_modifier
200         | workspace
201         | assign
202         | ipcsocket
203         | exec
204         | color
205         | terminal
206         | font
207         | comment
208         ;
209
210 comment:
211         TOKCOMMENT
212         ;
213
214 command:
215         STR
216         ;
217
218 bind:
219         TOKBIND WHITESPACE binding_modifiers NUMBER WHITESPACE command
220         {
221                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>3, $4, $<string>6);
222                 Binding *new = scalloc(sizeof(Binding));
223
224                 new->keycode = $<number>4;
225                 new->mods = $<number>3;
226                 new->command = sstrdup($<string>6);
227
228                 TAILQ_INSERT_TAIL(&bindings, new, bindings);
229         }
230         ;
231
232 bindsym:
233         TOKBINDSYM WHITESPACE binding_modifiers WORD WHITESPACE command
234         {
235                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>3, $4, $<string>6);
236                 Binding *new = scalloc(sizeof(Binding));
237
238                 new->symbol = sstrdup($4);
239                 new->mods = $<number>3;
240                 new->command = sstrdup($<string>6);
241
242                 TAILQ_INSERT_TAIL(&bindings, new, bindings);
243         }
244         ;
245
246 floating_modifier:
247         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
248         {
249                 LOG("floating modifier = %d\n", $<number>3);
250                 config.floating_modifier = $<number>3;
251         }
252         ;
253
254 workspace:
255         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen workspace_name
256         {
257                 int ws_num = $<number>3;
258                 if (ws_num < 1 || ws_num > 10) {
259                         LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
260                 } else {
261                         workspaces[ws_num - 1].preferred_screen = sstrdup($<string>7);
262                         if ($<string>8 != NULL)
263                                 workspace_set_name(&(workspaces[ws_num - 1]), $<string>8);
264                 }
265         }
266         | TOKWORKSPACE WHITESPACE NUMBER workspace_name
267         {
268                 int ws_num = $<number>3;
269                 if (ws_num < 1 || ws_num > 10) {
270                         LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
271                 } else {
272                         if ($<string>4 != NULL)
273                                         workspace_set_name(&(workspaces[ws_num - 1]), $<string>4);
274                 }
275         }
276         ;
277
278 workspace_name:
279         /* NULL */                      { $<string>$ = NULL; }
280         | WHITESPACE QUOTEDSTRING       { $<string>$ = $<string>2; }
281         | WHITESPACE STR                { $<string>$ = $<string>2; }
282         ;
283
284 screen:
285         NUMBER              { asprintf(&$<string>$, "%d", $<number>1); }
286         | NUMBER 'x'        { asprintf(&$<string>$, "%d", $<number>1); }
287         | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
288         | 'x' NUMBER        { asprintf(&$<string>$, "x%d", $<number>2); }
289         ;
290
291 assign:
292         TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
293         {
294                 printf("assignment of %s to %d\n", $<string>3, $<number>6);
295
296                 struct Assignment *new = $<assignment>6;
297                 new->windowclass_title = strdup($<string>3);
298                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
299         }
300         ;
301
302 assign_target:
303         NUMBER
304         {
305                 struct Assignment *new = scalloc(sizeof(struct Assignment));
306                 new->workspace = $<number>1;
307                 new->floating = ASSIGN_FLOATING_NO;
308                 $<assignment>$ = new;
309         }
310         | '~'
311         {
312                 struct Assignment *new = scalloc(sizeof(struct Assignment));
313                 new->floating = ASSIGN_FLOATING_ONLY;
314                 $<assignment>$ = new;
315         }
316         | '~' NUMBER
317         {
318                 struct Assignment *new = scalloc(sizeof(struct Assignment));
319                 new->workspace = $<number>2;
320                 new->floating = ASSIGN_FLOATING;
321                 $<assignment>$ = new;
322         }
323         ;
324
325 window_class:
326         QUOTEDSTRING
327         | STR_NG
328         ;
329
330 optional_arrow:
331         /* NULL */
332         | TOKARROW WHITESPACE
333         ;
334
335 ipcsocket:
336         TOKIPCSOCKET WHITESPACE STR
337         {
338                 config.ipc_socket_path = sstrdup($<string>3);
339         }
340         ;
341
342 exec:
343         TOKEXEC WHITESPACE STR
344         {
345                 struct Autostart *new = smalloc(sizeof(struct Autostart));
346                 new->command = sstrdup($<string>3);
347                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
348         }
349         ;
350
351 terminal:
352         TOKTERMINAL WHITESPACE STR
353         {
354                 config.terminal = sstrdup($<string>3);
355                 printf("terminal %s\n", config.terminal);
356         }
357         ;
358
359 font:
360         TOKFONT WHITESPACE STR
361         {
362                 config.font = sstrdup($<string>3);
363                 printf("font %s\n", config.font);
364         }
365         ;
366
367
368 color:
369         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
370         {
371                 struct Colortriple *dest = $<color>1;
372
373                 dest->border = $<number>3;
374                 dest->background = $<number>5;
375                 dest->text = $<number>7;
376         }
377         ;
378
379 colorpixel:
380         '#' HEX         { $<number>$ = get_colorpixel(global_conn, $<string>2); }
381         ;
382
383
384 binding_modifiers:
385         /* NULL */                               { $<number>$ = 0; }
386         | binding_modifier
387         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
388         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
389         ;
390
391 binding_modifier:
392         MODIFIER        { $<number>$ = $<number>1; }
393         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
394         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
395         ;