]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
ff96bb5b9d3b90b32a3c352ab3e0a73844c45dbb
[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 %expect 1
164
165 %union {
166         int number;
167         char *string;
168         struct Colortriple *color;
169         struct Assignment *assignment;
170         struct Binding *binding;
171 }
172
173 %token <number>NUMBER
174 %token <string>WORD
175 %token <string>STR
176 %token <string>STR_NG
177 %token <string>HEX
178 %token TOKBIND
179 %token TOKTERMINAL
180 %token TOKCOMMENT
181 %token TOKFONT
182 %token TOKBINDSYM
183 %token MODIFIER
184 %token TOKCONTROL
185 %token TOKSHIFT
186 %token WHITESPACE
187 %token TOKFLOATING_MODIFIER
188 %token QUOTEDSTRING
189 %token TOKWORKSPACE
190 %token TOKSCREEN
191 %token TOKASSIGN
192 %token TOKSET
193 %token TOKIPCSOCKET
194 %token TOKEXEC
195 %token TOKCOLOR
196 %token TOKARROW
197 %token TOKMODE
198 %token TOKNEWCONTAINER
199 %token TOKNEWWINDOW
200 %token TOKCONTAINERMODE
201 %token TOKSTACKLIMIT
202
203 %%
204
205 lines: /* empty */
206         | lines WHITESPACE line
207         | lines line
208         ;
209
210 line:
211         bindline
212         | mode
213         | floating_modifier
214         | new_container
215         | new_window
216         | workspace
217         | assign
218         | ipcsocket
219         | exec
220         | color
221         | terminal
222         | font
223         | comment
224         ;
225
226 comment:
227         TOKCOMMENT
228         ;
229
230 command:
231         STR
232         ;
233
234 bindline:
235         binding
236         {
237                 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
238         }
239         ;
240
241 binding:
242         TOKBIND WHITESPACE bind                 { $<binding>$ = $<binding>3; }
243         | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
244         ;
245
246 bind:
247         binding_modifiers NUMBER WHITESPACE command
248         {
249                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
250                 Binding *new = scalloc(sizeof(Binding));
251
252                 new->keycode = $<number>2;
253                 new->mods = $<number>1;
254                 new->command = sstrdup($<string>4);
255
256                 $<binding>$ = new;
257         }
258         ;
259
260 bindsym:
261         binding_modifiers word_or_number WHITESPACE command
262         {
263                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
264                 Binding *new = scalloc(sizeof(Binding));
265
266                 new->symbol = sstrdup($<string>2);
267                 new->mods = $<number>1;
268                 new->command = sstrdup($<string>4);
269
270                 $<binding>$ = new;
271         }
272         ;
273
274 word_or_number:
275         WORD
276         | NUMBER
277         {
278                 asprintf(&$<string>$, "%d", $1);
279         }
280         ;
281
282 mode:
283         TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
284         {
285                 if (strcasecmp($<string>3, "default") == 0) {
286                         printf("You cannot use the name \"default\" for your mode\n");
287                         exit(1);
288                 }
289                 printf("\t now in mode %s\n", $<string>3);
290                 printf("\t current bindings = %p\n", current_bindings);
291                 Binding *binding;
292                 TAILQ_FOREACH(binding, current_bindings, bindings) {
293                         printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
294                                         binding->mods, binding->keycode, binding->symbol, binding->command);
295                 }
296
297                 struct Mode *mode = scalloc(sizeof(struct Mode));
298                 mode->name = strdup($<string>3);
299                 mode->bindings = current_bindings;
300                 current_bindings = NULL;
301                 SLIST_INSERT_HEAD(&modes, mode, modes);
302         }
303         ;
304
305
306 modelines:
307         /* empty */
308         | modelines modeline
309         ;
310
311 modeline:
312         WHITESPACE
313         | comment
314         | binding
315         {
316                 if (current_bindings == NULL) {
317                         current_bindings = scalloc(sizeof(struct bindings_head));
318                         TAILQ_INIT(current_bindings);
319                 }
320
321                 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
322         }
323         ;
324
325 floating_modifier:
326         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
327         {
328                 LOG("floating modifier = %d\n", $<number>3);
329                 config.floating_modifier = $<number>3;
330         }
331         ;
332
333 new_container:
334         TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
335         {
336                 LOG("new containers will be in mode %d\n", $<number>3);
337                 config.container_mode = $<number>3;
338
339                 /* We also need to change the layout of the already existing
340                  * workspaces here. Workspaces may exist at this point because
341                  * of the other directives which are modifying workspaces
342                  * (setting the preferred screen or name). While the workspace
343                  * objects are already created, they have never been used.
344                  * Thus, the user very likely awaits the default container mode
345                  * to trigger in this case, regardless of where it is inside
346                  * his configuration file. */
347                 Workspace *ws;
348                 TAILQ_FOREACH(ws, workspaces, workspaces) {
349                         if (ws->table == NULL)
350                                 continue;
351                         switch_layout_mode(global_conn,
352                                            ws->table[0][0],
353                                            config.container_mode);
354                 }
355         }
356         | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
357         {
358                 LOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
359                 config.container_stack_limit = $<number>5;
360                 config.container_stack_limit_value = $<number>7;
361
362                 /* See the comment above */
363                 Workspace *ws;
364                 TAILQ_FOREACH(ws, workspaces, workspaces) {
365                         if (ws->table == NULL)
366                                 continue;
367                         Container *con = ws->table[0][0];
368                         con->stack_limit = config.container_stack_limit;
369                         con->stack_limit_value = config.container_stack_limit_value;
370                 }
371         }
372         ;
373
374 new_window:
375         TOKNEWWINDOW WHITESPACE WORD
376         {
377                 LOG("new windows should start in mode %s\n", $<string>3);
378                 config.default_border = strdup($<string>3);
379         }
380         ;
381
382 workspace:
383         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen optional_workspace_name
384         {
385                 int ws_num = $<number>3;
386                 if (ws_num < 1) {
387                         LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
388                 } else {
389                         Workspace *ws = workspace_get(ws_num - 1);
390                         ws->preferred_screen = sstrdup($<string>7);
391                         if ($<string>8 != NULL)
392                                 workspace_set_name(ws, $<string>8);
393                 }
394         }
395         | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
396         {
397                 int ws_num = $<number>3;
398                 if (ws_num < 1) {
399                         LOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
400                 } else {
401                         if ($<string>5 != NULL)
402                                 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
403                 }
404         }
405         ;
406
407 optional_workspace_name:
408         /* empty */                     { $<string>$ = NULL; }
409         | WHITESPACE workspace_name     { $<string>$ = $<string>2; }
410         ;
411
412 workspace_name:
413         QUOTEDSTRING         { $<string>$ = $<string>1; }
414         | STR                { $<string>$ = $<string>1; }
415         ;
416
417 screen:
418         NUMBER              { asprintf(&$<string>$, "%d", $<number>1); }
419         | NUMBER 'x'        { asprintf(&$<string>$, "%d", $<number>1); }
420         | NUMBER 'x' NUMBER { asprintf(&$<string>$, "%dx%d", $<number>1, $<number>3); }
421         | 'x' NUMBER        { asprintf(&$<string>$, "x%d", $<number>2); }
422         ;
423
424 assign:
425         TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
426         {
427                 printf("assignment of %s\n", $<string>3);
428
429                 struct Assignment *new = $<assignment>6;
430                 printf("  to %d\n", new->workspace);
431                 printf("  floating = %d\n", new->floating);
432                 new->windowclass_title = strdup($<string>3);
433                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
434         }
435         ;
436
437 assign_target:
438         NUMBER
439         {
440                 struct Assignment *new = scalloc(sizeof(struct Assignment));
441                 new->workspace = $<number>1;
442                 new->floating = ASSIGN_FLOATING_NO;
443                 $<assignment>$ = new;
444         }
445         | '~'
446         {
447                 struct Assignment *new = scalloc(sizeof(struct Assignment));
448                 new->floating = ASSIGN_FLOATING_ONLY;
449                 $<assignment>$ = new;
450         }
451         | '~' NUMBER
452         {
453                 struct Assignment *new = scalloc(sizeof(struct Assignment));
454                 new->workspace = $<number>2;
455                 new->floating = ASSIGN_FLOATING;
456                 $<assignment>$ = new;
457         }
458         ;
459
460 window_class:
461         QUOTEDSTRING
462         | STR_NG
463         ;
464
465 optional_arrow:
466         /* NULL */
467         | TOKARROW WHITESPACE
468         ;
469
470 ipcsocket:
471         TOKIPCSOCKET WHITESPACE STR
472         {
473                 config.ipc_socket_path = sstrdup($<string>3);
474         }
475         ;
476
477 exec:
478         TOKEXEC WHITESPACE STR
479         {
480                 struct Autostart *new = smalloc(sizeof(struct Autostart));
481                 new->command = sstrdup($<string>3);
482                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
483         }
484         ;
485
486 terminal:
487         TOKTERMINAL WHITESPACE STR
488         {
489                 LOG("The terminal option is DEPRECATED and has no effect. "
490                     "Please remove it from your configuration file.");
491         }
492         ;
493
494 font:
495         TOKFONT WHITESPACE STR
496         {
497                 config.font = sstrdup($<string>3);
498                 printf("font %s\n", config.font);
499         }
500         ;
501
502
503 color:
504         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
505         {
506                 struct Colortriple *dest = $<color>1;
507
508                 dest->border = $<number>3;
509                 dest->background = $<number>5;
510                 dest->text = $<number>7;
511         }
512         ;
513
514 colorpixel:
515         '#' HEX
516         {
517                 char *hex;
518                 if (asprintf(&hex, "#%s", $<string>2) == -1)
519                         die("asprintf()");
520                 $<number>$ = get_colorpixel(global_conn, hex);
521                 free(hex);
522         }
523         ;
524
525
526 binding_modifiers:
527         /* NULL */                               { $<number>$ = 0; }
528         | binding_modifier
529         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
530         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
531         ;
532
533 binding_modifier:
534         MODIFIER        { $<number>$ = $<number>1; }
535         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
536         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
537         ;