]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
a33ecaf11f5c73ed996784a3841fe7318701fa46
[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
26 typedef struct yy_buffer_state *YY_BUFFER_STATE;
27 extern int yylex(void);
28 extern int yyparse(void);
29 extern FILE *yyin;
30 YY_BUFFER_STATE yy_scan_string(const char *);
31
32 static struct bindings_head *current_bindings;
33
34 int yydebug = 1;
35
36 void yyerror(const char *str) {
37         fprintf(stderr,"error: %s\n",str);
38 }
39
40 int yywrap() {
41         return 1;
42 }
43
44 void parse_file(const char *f) {
45         SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
46         int fd, ret, read_bytes = 0;
47         struct stat stbuf;
48         char *buf;
49         FILE *fstr;
50         char buffer[1026], key[512], value[512];
51
52         if ((fd = open(f, O_RDONLY)) == -1)
53                 die("Could not open configuration file: %s\n", strerror(errno));
54
55         if (fstat(fd, &stbuf) == -1)
56                 die("Could not fstat file: %s\n", strerror(errno));
57
58         buf = smalloc(stbuf.st_size * sizeof(char));
59         while (read_bytes < stbuf.st_size) {
60                 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
61                         die("Could not read(): %s\n", strerror(errno));
62                 read_bytes += ret;
63         }
64
65         if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
66                 die("Could not lseek: %s\n", strerror(errno));
67
68         if ((fstr = fdopen(fd, "r")) == NULL)
69                 die("Could not fdopen: %s\n", strerror(errno));
70
71         while (!feof(fstr)) {
72                 if (fgets(buffer, 1024, fstr) == NULL) {
73                         if (feof(fstr))
74                                 break;
75                         die("Could not read configuration file\n");
76                 }
77
78                 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
79                 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
80                     key[0] == '#' || strlen(key) < 3)
81                         continue;
82
83                 if (strcasecmp(key, "set") == 0) {
84                         if (value[0] != '$')
85                                 die("Malformed variable assignment, name has to start with $\n");
86
87                         /* get key/value for this variable */
88                         char *v_key = value, *v_value;
89                         if ((v_value = strstr(value, " ")) == NULL)
90                                 die("Malformed variable assignment, need a value\n");
91
92                         *(v_value++) = '\0';
93
94                         struct Variable *new = scalloc(sizeof(struct Variable));
95                         new->key = sstrdup(v_key);
96                         new->value = sstrdup(v_value);
97                         SLIST_INSERT_HEAD(&variables, new, variables);
98                         LOG("Got new variable %s = %s\n", v_key, v_value);
99                         continue;
100                 }
101         }
102
103         /* For every custom variable, see how often it occurs in the file and
104          * how much extra bytes it requires when replaced. */
105         struct Variable *current, *nearest;
106         int extra_bytes = 0;
107         SLIST_FOREACH(current, &variables, variables) {
108                 int extra = (strlen(current->value) - strlen(current->key));
109                 char *next;
110                 for (next = buf;
111                      (next = strcasestr(buf + (next - buf), current->key)) != NULL;
112                      next += strlen(current->key))
113                         extra_bytes += extra;
114         }
115
116         /* Then, allocate a new buffer and copy the file over to the new one,
117          * but replace occurences of our variables */
118         char *walk = buf, *destwalk;
119         char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
120         destwalk = new;
121         while (walk < (buf + stbuf.st_size)) {
122                 /* Find the next variable */
123                 SLIST_FOREACH(current, &variables, variables)
124                         current->next_match = strcasestr(walk, current->key);
125                 nearest = NULL;
126                 int distance = stbuf.st_size;
127                 SLIST_FOREACH(current, &variables, variables) {
128                         if (current->next_match == NULL)
129                                 continue;
130                         if ((current->next_match - walk) < distance) {
131                                 distance = (current->next_match - walk);
132                                 nearest = current;
133                         }
134                 }
135                 if (nearest == NULL) {
136                         /* If there are no more variables, we just copy the rest */
137                         strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
138                         destwalk += (buf + stbuf.st_size) - walk;
139                         *destwalk = '\0';
140                         break;
141                 } else {
142                         /* Copy until the next variable, then copy its value */
143                         strncpy(destwalk, walk, distance);
144                         strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
145                         walk += distance + strlen(nearest->key);
146                         destwalk += distance + strlen(nearest->value);
147                 }
148         }
149
150         yy_scan_string(new);
151
152         if (yyparse() != 0) {
153                 fprintf(stderr, "Could not parse configfile\n");
154                 exit(1);
155         }
156
157         free(new);
158         free(buf);
159 }
160
161 %}
162
163 %union {
164         int number;
165         char *string;
166         struct Colortriple *color;
167         struct Assignment *assignment;
168         struct Binding *binding;
169 }
170
171 %token <number>NUMBER
172 %token <string>WORD
173 %token <string>STR
174 %token <string>STR_NG
175 %token <string>HEX
176 %token TOKBIND
177 %token TOKTERMINAL
178 %token TOKCOMMENT
179 %token TOKFONT
180 %token TOKBINDSYM
181 %token MODIFIER
182 %token TOKCONTROL
183 %token TOKSHIFT
184 %token WHITESPACE
185 %token TOKFLOATING_MODIFIER
186 %token QUOTEDSTRING
187 %token TOKWORKSPACE
188 %token TOKSCREEN
189 %token TOKASSIGN
190 %token TOKSET
191 %token TOKIPCSOCKET
192 %token TOKEXEC
193 %token TOKCOLOR
194 %token TOKARROW
195 %token TOKMODE
196 %token TOKNEWCONTAINER
197 %token TOKNEWWINDOW
198 %token TOKCONTAINERMODE
199 %token TOKSTACKLIMIT
200
201 %%
202
203 lines: /* empty */
204         | lines WHITESPACE line
205         | lines line
206         ;
207
208 line:
209         bindline
210         | mode
211         | floating_modifier
212         | new_container
213         | new_window
214         | workspace
215         | assign
216         | ipcsocket
217         | exec
218         | color
219         | terminal
220         | font
221         | comment
222         ;
223
224 comment:
225         TOKCOMMENT
226         ;
227
228 command:
229         STR
230         ;
231
232 bindline:
233         binding
234         {
235                 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
236         }
237         ;
238
239 binding:
240         TOKBIND WHITESPACE bind                 { $<binding>$ = $<binding>3; }
241         | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
242         ;
243
244 bind:
245         binding_modifiers NUMBER WHITESPACE command
246         {
247                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
248                 Binding *new = scalloc(sizeof(Binding));
249
250                 new->keycode = $<number>2;
251                 new->mods = $<number>1;
252                 new->command = sstrdup($<string>4);
253
254                 $<binding>$ = new;
255         }
256         ;
257
258 bindsym:
259         binding_modifiers word_or_number WHITESPACE command
260         {
261                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
262                 Binding *new = scalloc(sizeof(Binding));
263
264                 new->symbol = sstrdup($<string>2);
265                 new->mods = $<number>1;
266                 new->command = sstrdup($<string>4);
267
268                 $<binding>$ = new;
269         }
270         ;
271
272 word_or_number:
273         WORD
274         | NUMBER
275         {
276                 asprintf(&$<string>$, "%d", $1);
277         }
278         ;
279
280 mode:
281         TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
282         {
283                 if (strcasecmp($<string>3, "default") == 0) {
284                         printf("You cannot use the name \"default\" for your mode\n");
285                         exit(1);
286                 }
287                 printf("\t now in mode %s\n", $<string>3);
288                 printf("\t current bindings = %p\n", current_bindings);
289                 Binding *binding;
290                 TAILQ_FOREACH(binding, current_bindings, bindings) {
291                         printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
292                                         binding->mods, binding->keycode, binding->symbol, binding->command);
293                 }
294
295                 struct Mode *mode = scalloc(sizeof(struct Mode));
296                 mode->name = strdup($<string>3);
297                 mode->bindings = current_bindings;
298                 current_bindings = NULL;
299                 SLIST_INSERT_HEAD(&modes, mode, modes);
300         }
301         ;
302
303
304 modelines:
305         /* empty */
306         | modelines modeline
307         ;
308
309 modeline:
310         WHITESPACE
311         | comment
312         | binding
313         {
314                 if (current_bindings == NULL) {
315                         current_bindings = scalloc(sizeof(struct bindings_head));
316                         TAILQ_INIT(current_bindings);
317                 }
318
319                 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
320         }
321         ;
322
323 floating_modifier:
324         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
325         {
326                 LOG("floating modifier = %d\n", $<number>3);
327                 config.floating_modifier = $<number>3;
328         }
329         ;
330
331 new_container:
332         TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
333         {
334                 LOG("new containers will be in mode %d\n", $<number>3);
335                 config.container_mode = $<number>3;
336
337                 /* We also need to change the layout of the already existing
338                  * workspaces here. Workspaces may exist at this point because
339                  * of the other directives which are modifying workspaces
340                  * (setting the preferred screen or name). While the workspace
341                  * objects are already created, they have never been used.
342                  * Thus, the user very likely awaits the default container mode
343                  * to trigger in this case, regardless of where it is inside
344                  * his configuration file. */
345                 Workspace *ws;
346                 TAILQ_FOREACH(ws, workspaces, workspaces) {
347                         if (ws->table == NULL)
348                                 continue;
349                         switch_layout_mode(global_conn,
350                                            ws->table[0][0],
351                                            config.container_mode);
352                 }
353         }
354         | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
355         {
356                 LOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
357                 config.container_stack_limit = $<number>5;
358                 config.container_stack_limit_value = $<number>7;
359
360                 /* See the comment above */
361                 Workspace *ws;
362                 TAILQ_FOREACH(ws, workspaces, workspaces) {
363                         if (ws->table == NULL)
364                                 continue;
365                         Container *con = ws->table[0][0];
366                         con->stack_limit = config.container_stack_limit;
367                         con->stack_limit_value = config.container_stack_limit_value;
368                 }
369         }
370         ;
371
372 new_window:
373         TOKNEWWINDOW WHITESPACE WORD
374         {
375                 LOG("new windows should start in mode %s\n", $<string>3);
376                 config.default_border = strdup($<string>3);
377         }
378         ;
379
380 workspace:
381         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen optional_workspace_name
382         {
383                 int ws_num = $<number>3;
384                 if (ws_num < 1) {
385                         LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
386                 } else {
387                         Workspace *ws = workspace_get(ws_num - 1);
388                         ws->preferred_screen = sstrdup($<string>7);
389                         if ($<string>8 != NULL)
390                                 workspace_set_name(ws, $<string>8);
391                 }
392         }
393         | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
394         {
395                 int ws_num = $<number>3;
396                 if (ws_num < 1) {
397                         LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
398                 } else {
399                         if ($<string>5 != NULL)
400                                 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
401                 }
402         }
403         ;
404
405 optional_workspace_name:
406         /* empty */                     { $<string>$ = NULL; }
407         | WHITESPACE workspace_name     { $<string>$ = $<string>1; }
408         ;
409
410 workspace_name:
411         QUOTEDSTRING         { $<string>$ = $<string>1; }
412         | STR                { $<string>$ = $<string>1; }
413         ;
414
415 screen:
416         NUMBER              { asprintf(&$<string>$, "%d", $<number>1); }
417         | NUMBER 'x'        { asprintf(&$<string>$, "%d", $<number>1); }
418         | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
419         | 'x' NUMBER        { asprintf(&$<string>$, "x%d", $<number>2); }
420         ;
421
422 assign:
423         TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
424         {
425                 printf("assignment of %s\n", $<string>3);
426
427                 struct Assignment *new = $<assignment>6;
428                 printf("  to %d\n", new->workspace);
429                 printf("  floating = %d\n", new->floating);
430                 new->windowclass_title = strdup($<string>3);
431                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
432         }
433         ;
434
435 assign_target:
436         NUMBER
437         {
438                 struct Assignment *new = scalloc(sizeof(struct Assignment));
439                 new->workspace = $<number>1;
440                 new->floating = ASSIGN_FLOATING_NO;
441                 $<assignment>$ = new;
442         }
443         | '~'
444         {
445                 struct Assignment *new = scalloc(sizeof(struct Assignment));
446                 new->floating = ASSIGN_FLOATING_ONLY;
447                 $<assignment>$ = new;
448         }
449         | '~' NUMBER
450         {
451                 struct Assignment *new = scalloc(sizeof(struct Assignment));
452                 new->workspace = $<number>2;
453                 new->floating = ASSIGN_FLOATING;
454                 $<assignment>$ = new;
455         }
456         ;
457
458 window_class:
459         QUOTEDSTRING
460         | STR_NG
461         ;
462
463 optional_arrow:
464         /* NULL */
465         | TOKARROW WHITESPACE
466         ;
467
468 ipcsocket:
469         TOKIPCSOCKET WHITESPACE STR
470         {
471                 config.ipc_socket_path = sstrdup($<string>3);
472         }
473         ;
474
475 exec:
476         TOKEXEC WHITESPACE STR
477         {
478                 struct Autostart *new = smalloc(sizeof(struct Autostart));
479                 new->command = sstrdup($<string>3);
480                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
481         }
482         ;
483
484 terminal:
485         TOKTERMINAL WHITESPACE STR
486         {
487                 config.terminal = sstrdup($<string>3);
488                 printf("terminal %s\n", config.terminal);
489         }
490         ;
491
492 font:
493         TOKFONT WHITESPACE STR
494         {
495                 config.font = sstrdup($<string>3);
496                 printf("font %s\n", config.font);
497         }
498         ;
499
500
501 color:
502         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
503         {
504                 struct Colortriple *dest = $<color>1;
505
506                 dest->border = $<number>3;
507                 dest->background = $<number>5;
508                 dest->text = $<number>7;
509         }
510         ;
511
512 colorpixel:
513         '#' HEX
514         {
515                 char *hex;
516                 if (asprintf(&hex, "#%s", $<string>2) == -1)
517                         die("asprintf()");
518                 $<number>$ = get_colorpixel(global_conn, hex);
519                 free(hex);
520         }
521         ;
522
523
524 binding_modifiers:
525         /* NULL */                               { $<number>$ = 0; }
526         | binding_modifier
527         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
528         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
529         ;
530
531 binding_modifier:
532         MODIFIER        { $<number>$ = $<number>1; }
533         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
534         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
535         ;