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