]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
33b8f7740efc2aa7e476995c5a765f5234fd8fa5
[i3/i3] / src / cfgparse.y
1 %{
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10
11 #include "all.h"
12
13 typedef struct yy_buffer_state *YY_BUFFER_STATE;
14 extern int yylex(struct context *context);
15 extern int yyparse(void);
16 extern FILE *yyin;
17 YY_BUFFER_STATE yy_scan_string(const char *);
18
19 static struct bindings_head *current_bindings;
20 static struct context *context;
21
22 /* We don’t need yydebug for now, as we got decent error messages using
23  * yyerror(). Should you ever want to extend the parser, it might be handy
24  * to just comment it in again, so it stays here. */
25 //int yydebug = 1;
26
27 void yyerror(const char *error_message) {
28     ELOG("\n");
29     ELOG("CONFIG: %s\n", error_message);
30     ELOG("CONFIG: in file \"%s\", line %d:\n",
31         context->filename, context->line_number);
32     ELOG("CONFIG:   %s\n", context->line_copy);
33     ELOG("CONFIG:   ");
34     for (int c = 1; c <= context->last_column; c++)
35         if (c >= context->first_column)
36             printf("^");
37         else printf(" ");
38     printf("\n");
39     ELOG("\n");
40 }
41
42 int yywrap() {
43     return 1;
44 }
45
46 void parse_file(const char *f) {
47     SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
48     int fd, ret, read_bytes = 0;
49     struct stat stbuf;
50     char *buf;
51     FILE *fstr;
52     char buffer[1026], key[512], value[512];
53
54     if ((fd = open(f, O_RDONLY)) == -1)
55         die("Could not open configuration file: %s\n", strerror(errno));
56
57     if (fstat(fd, &stbuf) == -1)
58         die("Could not fstat file: %s\n", strerror(errno));
59
60     buf = scalloc((stbuf.st_size + 1) * sizeof(char));
61     while (read_bytes < stbuf.st_size) {
62         if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
63             die("Could not read(): %s\n", strerror(errno));
64         read_bytes += ret;
65     }
66
67     if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
68         die("Could not lseek: %s\n", strerror(errno));
69
70     if ((fstr = fdopen(fd, "r")) == NULL)
71         die("Could not fdopen: %s\n", strerror(errno));
72
73     while (!feof(fstr)) {
74         if (fgets(buffer, 1024, fstr) == NULL) {
75             if (feof(fstr))
76                 break;
77             die("Could not read configuration file\n");
78         }
79
80         /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
81         if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
82             key[0] == '#' || strlen(key) < 3)
83             continue;
84
85         if (strcasecmp(key, "set") == 0) {
86             if (value[0] != '$')
87                 die("Malformed variable assignment, name has to start with $\n");
88
89             /* get key/value for this variable */
90             char *v_key = value, *v_value;
91             if ((v_value = strstr(value, " ")) == NULL)
92                 die("Malformed variable assignment, need a value\n");
93
94             *(v_value++) = '\0';
95
96             struct Variable *new = scalloc(sizeof(struct Variable));
97             new->key = sstrdup(v_key);
98             new->value = sstrdup(v_value);
99             SLIST_INSERT_HEAD(&variables, new, variables);
100             DLOG("Got new variable %s = %s\n", v_key, v_value);
101             continue;
102         }
103     }
104     fclose(fstr);
105
106     /* For every custom variable, see how often it occurs in the file and
107      * how much extra bytes it requires when replaced. */
108     struct Variable *current, *nearest;
109     int extra_bytes = 0;
110     /* We need to copy the buffer because we need to invalidate the
111      * variables (otherwise we will count them twice, which is bad when
112      * 'extra' is negative) */
113     char *bufcopy = sstrdup(buf);
114     SLIST_FOREACH(current, &variables, variables) {
115         int extra = (strlen(current->value) - strlen(current->key));
116         char *next;
117         for (next = bufcopy;
118              (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
119              next += strlen(current->key)) {
120             *next = '_';
121             extra_bytes += extra;
122         }
123     }
124     FREE(bufcopy);
125
126     /* Then, allocate a new buffer and copy the file over to the new one,
127      * but replace occurences of our variables */
128     char *walk = buf, *destwalk;
129     char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
130     destwalk = new;
131     while (walk < (buf + stbuf.st_size)) {
132         /* Find the next variable */
133         SLIST_FOREACH(current, &variables, variables)
134             current->next_match = strcasestr(walk, current->key);
135         nearest = NULL;
136         int distance = stbuf.st_size;
137         SLIST_FOREACH(current, &variables, variables) {
138             if (current->next_match == NULL)
139                 continue;
140             if ((current->next_match - walk) < distance) {
141                 distance = (current->next_match - walk);
142                 nearest = current;
143             }
144         }
145         if (nearest == NULL) {
146             /* If there are no more variables, we just copy the rest */
147             strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
148             destwalk += (buf + stbuf.st_size) - walk;
149             *destwalk = '\0';
150             break;
151         } else {
152             /* Copy until the next variable, then copy its value */
153             strncpy(destwalk, walk, distance);
154             strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
155             walk += distance + strlen(nearest->key);
156             destwalk += distance + strlen(nearest->value);
157         }
158     }
159
160     yy_scan_string(new);
161
162     context = scalloc(sizeof(struct context));
163     context->filename = f;
164
165     if (yyparse() != 0) {
166         fprintf(stderr, "Could not parse configfile\n");
167         exit(1);
168     }
169
170     FREE(context->line_copy);
171     free(context);
172     free(new);
173     free(buf);
174
175     while (!SLIST_EMPTY(&variables)) {
176         current = SLIST_FIRST(&variables);
177         FREE(current->key);
178         FREE(current->value);
179         SLIST_REMOVE_HEAD(&variables, variables);
180         FREE(current);
181     }
182 }
183
184 %}
185
186 %expect 1
187 %error-verbose
188 %lex-param { struct context *context }
189
190 %union {
191     int number;
192     char *string;
193     uint32_t *single_color;
194     struct Colortriple *color;
195     Match *match;
196     struct Binding *binding;
197 }
198
199 %token <number>NUMBER "<number>"
200 %token <string>WORD "<word>"
201 %token <string>STR "<string>"
202 %token <string>STR_NG "<string (non-greedy)>"
203 %token <string>HEX "<hex>"
204 %token <string>OUTPUT "<RandR output>"
205 %token TOKBINDCODE
206 %token TOKTERMINAL
207 %token TOKCOMMENT "<comment>"
208 %token TOKFONT "font"
209 %token TOKBINDSYM "bindsym"
210 %token MODIFIER "<modifier>"
211 %token TOKCONTROL "control"
212 %token TOKSHIFT "shift"
213 %token WHITESPACE "<whitespace>"
214 %token TOKFLOATING_MODIFIER "floating_modifier"
215 %token QUOTEDSTRING "<quoted string>"
216 %token TOKWORKSPACE "workspace"
217 %token TOKOUTPUT "output"
218 %token TOKASSIGN "assign"
219 %token TOKSET
220 %token TOKIPCSOCKET "ipc_socket"
221 %token TOKRESTARTSTATE "restart_state"
222 %token TOKEXEC "exec"
223 %token TOKSINGLECOLOR
224 %token TOKCOLOR
225 %token TOKARROW "→"
226 %token TOKMODE "mode"
227 %token TOK_ORIENTATION "default_orientation"
228 %token TOK_HORIZ "horizontal"
229 %token TOK_VERT "vertical"
230 %token TOK_AUTO "auto"
231 %token TOK_WORKSPACE_LAYOUT "workspace_layout"
232 %token TOKNEWWINDOW "new_window"
233 %token TOK_NORMAL "normal"
234 %token TOK_NONE "none"
235 %token TOK_1PIXEL "1pixel"
236 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
237 %token TOKWORKSPACEBAR "workspace_bar"
238 %token TOK_DEFAULT "default"
239 %token TOK_STACKING "stacking"
240 %token TOK_TABBED "tabbed"
241 %token TOKSTACKLIMIT "stack-limit"
242 %token TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
243 %token TOK_IGNORE "ignore"
244 %token TOK_LEAVE_FULLSCREEN "leave_fullscreen"
245
246 %%
247
248 lines: /* empty */
249     | lines WHITESPACE line
250     | lines error
251     | lines line
252     ;
253
254 line:
255     bindline
256     | mode
257     | floating_modifier
258     | orientation
259     | workspace_layout
260     | new_window
261     | focus_follows_mouse
262     | workspace_bar
263     | workspace
264     | assign
265     | ipcsocket
266     | restart_state
267     | exec
268     | single_color
269     | color
270     | terminal
271     | font
272     | comment
273     | popup_during_fullscreen
274     ;
275
276 comment:
277     TOKCOMMENT
278     ;
279
280 command:
281     STR
282     ;
283
284 bindline:
285     binding
286     {
287         TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
288     }
289     ;
290
291 binding:
292     TOKBINDCODE WHITESPACE bindcode         { $<binding>$ = $<binding>3; }
293     | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
294     ;
295
296 bindcode:
297     binding_modifiers NUMBER WHITESPACE command
298     {
299         printf("\tFound keycode binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
300         Binding *new = scalloc(sizeof(Binding));
301
302         new->keycode = $<number>2;
303         new->mods = $<number>1;
304         new->command = $<string>4;
305
306         $<binding>$ = new;
307     }
308     ;
309
310 bindsym:
311     binding_modifiers word_or_number WHITESPACE command
312     {
313         printf("\tFound keysym binding mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
314         Binding *new = scalloc(sizeof(Binding));
315
316         new->symbol = $<string>2;
317         new->mods = $<number>1;
318         new->command = $<string>4;
319
320         $<binding>$ = new;
321     }
322     ;
323
324 word_or_number:
325     WORD
326     | NUMBER
327     {
328         asprintf(&$<string>$, "%d", $1);
329     }
330     ;
331
332 mode:
333     TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
334     {
335         if (strcasecmp($<string>3, "default") == 0) {
336             printf("You cannot use the name \"default\" for your mode\n");
337             exit(1);
338         }
339         printf("\t now in mode %s\n", $<string>3);
340         printf("\t current bindings = %p\n", current_bindings);
341         Binding *binding;
342         TAILQ_FOREACH(binding, current_bindings, bindings) {
343             printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
344                             binding->mods, binding->keycode, binding->symbol, binding->command);
345         }
346
347         struct Mode *mode = scalloc(sizeof(struct Mode));
348         mode->name = $<string>3;
349         mode->bindings = current_bindings;
350         current_bindings = NULL;
351         SLIST_INSERT_HEAD(&modes, mode, modes);
352     }
353     ;
354
355
356 modelines:
357     /* empty */
358     | modelines modeline
359     ;
360
361 modeline:
362     WHITESPACE
363     | comment
364     | binding
365     {
366         if (current_bindings == NULL) {
367             current_bindings = scalloc(sizeof(struct bindings_head));
368             TAILQ_INIT(current_bindings);
369         }
370
371         TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
372     }
373     ;
374
375 floating_modifier:
376     TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
377     {
378         DLOG("floating modifier = %d\n", $<number>3);
379         config.floating_modifier = $<number>3;
380     }
381     ;
382
383 orientation:
384     TOK_ORIENTATION WHITESPACE direction
385     {
386         DLOG("New containers should start with split direction %d\n", $<number>3);
387         config.default_orientation = $<number>3;
388     }
389     ;
390
391 direction:
392     TOK_HORIZ       { $<number>$ = HORIZ; }
393     | TOK_VERT      { $<number>$ = VERT; }
394     | TOK_AUTO      { $<number>$ = NO_ORIENTATION; }
395     ;
396
397 workspace_layout:
398     TOK_WORKSPACE_LAYOUT WHITESPACE layout_mode
399     {
400         DLOG("new containers will be in mode %d\n", $<number>3);
401         config.default_layout = $<number>3;
402
403 #if 0
404         /* We also need to change the layout of the already existing
405          * workspaces here. Workspaces may exist at this point because
406          * of the other directives which are modifying workspaces
407          * (setting the preferred screen or name). While the workspace
408          * objects are already created, they have never been used.
409          * Thus, the user very likely awaits the default container mode
410          * to trigger in this case, regardless of where it is inside
411          * his configuration file. */
412         Workspace *ws;
413         TAILQ_FOREACH(ws, workspaces, workspaces) {
414                 if (ws->table == NULL)
415                         continue;
416                 switch_layout_mode(global_conn,
417                                    ws->table[0][0],
418                                    config.container_mode);
419         }
420 #endif
421     }
422     | TOK_WORKSPACE_LAYOUT WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
423     {
424         DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
425         config.container_stack_limit = $<number>5;
426         config.container_stack_limit_value = $<number>7;
427
428 #if 0
429         /* See the comment above */
430         Workspace *ws;
431         TAILQ_FOREACH(ws, workspaces, workspaces) {
432                 if (ws->table == NULL)
433                         continue;
434                 Container *con = ws->table[0][0];
435                 con->stack_limit = config.container_stack_limit;
436                 con->stack_limit_value = config.container_stack_limit_value;
437         }
438 #endif
439     }
440     ;
441
442 layout_mode:
443     TOK_DEFAULT       { $<number>$ = L_DEFAULT; }
444     | TOK_STACKING    { $<number>$ = L_STACKED; }
445     | TOK_TABBED      { $<number>$ = L_TABBED; }
446     ;
447
448 new_window:
449     TOKNEWWINDOW WHITESPACE border_style
450     {
451         DLOG("new windows should start with border style %d\n", $<number>3);
452         config.default_border = $<number>3;
453     }
454     ;
455
456 border_style:
457     TOK_NORMAL      { $<number>$ = BS_NORMAL; }
458     | TOK_NONE      { $<number>$ = BS_NONE; }
459     | TOK_1PIXEL    { $<number>$ = BS_1PIXEL; }
460     ;
461
462 bool:
463     NUMBER
464     {
465         $<number>$ = ($<number>1 == 1);
466     }
467     | WORD
468     {
469         DLOG("checking word \"%s\"\n", $<string>1);
470         $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
471                       strcasecmp($<string>1, "true") == 0 ||
472                       strcasecmp($<string>1, "on") == 0 ||
473                       strcasecmp($<string>1, "enable") == 0 ||
474                       strcasecmp($<string>1, "active") == 0);
475     }
476     ;
477
478 focus_follows_mouse:
479     TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
480     {
481         DLOG("focus follows mouse = %d\n", $<number>3);
482         config.disable_focus_follows_mouse = !($<number>3);
483     }
484     ;
485
486 workspace_bar:
487     TOKWORKSPACEBAR WHITESPACE bool
488     {
489         DLOG("workspace bar = %d\n", $<number>3);
490         config.disable_workspace_bar = !($<number>3);
491     }
492     ;
493
494 workspace:
495     TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
496     {
497         int ws_num = $<number>3;
498         if (ws_num < 1) {
499             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
500         } else {
501 #if 0
502             Workspace *ws = workspace_get(ws_num - 1);
503             ws->preferred_output = $<string>7;
504             if ($<string>8 != NULL) {
505                     workspace_set_name(ws, $<string>8);
506                     free($<string>8);
507             }
508 #endif
509         }
510     }
511     | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
512     {
513         int ws_num = $<number>3;
514         if (ws_num < 1) {
515             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
516         } else {
517             DLOG("workspace name to: %s\n", $<string>5);
518 #if 0
519             if ($<string>5 != NULL) {
520                     workspace_set_name(workspace_get(ws_num - 1), $<string>5);
521                     free($<string>5);
522             }
523 #endif
524         }
525     }
526     ;
527
528 optional_workspace_name:
529     /* empty */                     { $<string>$ = NULL; }
530     | WHITESPACE workspace_name     { $<string>$ = $<string>2; }
531     ;
532
533 workspace_name:
534     QUOTEDSTRING         { $<string>$ = $<string>1; }
535     | STR                { $<string>$ = $<string>1; }
536     | WORD               { $<string>$ = $<string>1; }
537     ;
538
539 assign:
540     TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
541     {
542         printf("assignment of %s\n", $<string>3);
543
544         struct Match *match = $<match>6;
545
546         char *separator = NULL;
547         if ((separator = strchr($<string>3, '/')) != NULL) {
548             *(separator++) = '\0';
549             match->title = sstrdup(separator);
550         }
551         if (*$<string>3 != '\0')
552             match->class = sstrdup($<string>3);
553         free($<string>3);
554
555         printf("  class = %s\n", match->class);
556         printf("  title = %s\n", match->title);
557         if (match->insert_where == M_ASSIGN_WS)
558             printf("  to ws %s\n", match->target_ws);
559         TAILQ_INSERT_TAIL(&assignments, match, assignments);
560     }
561     ;
562
563 assign_target:
564     NUMBER
565     {
566         /* TODO: named workspaces */
567         Match *match = smalloc(sizeof(Match));
568         match_init(match);
569         match->insert_where = M_ASSIGN_WS;
570         asprintf(&(match->target_ws), "%d", $<number>1);
571         $<match>$ = match;
572     }
573     | '~'
574     {
575         /* TODO: compatiblity */
576 #if 0
577         struct Assignment *new = scalloc(sizeof(struct Assignment));
578         new->floating = ASSIGN_FLOATING_ONLY;
579         $<assignment>$ = new;
580 #endif
581     }
582     | '~' NUMBER
583     {
584         /* TODO: compatiblity */
585 #if 0
586         struct Assignment *new = scalloc(sizeof(struct Assignment));
587         new->workspace = $<number>2;
588         new->floating = ASSIGN_FLOATING;
589         $<assignment>$ = new;
590 #endif
591     }
592     ;
593
594 window_class:
595     QUOTEDSTRING
596     | STR_NG
597     ;
598
599 optional_arrow:
600     /* NULL */
601     | TOKARROW WHITESPACE
602     ;
603
604 ipcsocket:
605     TOKIPCSOCKET WHITESPACE STR
606     {
607         config.ipc_socket_path = $<string>3;
608     }
609     ;
610
611 restart_state:
612     TOKRESTARTSTATE WHITESPACE STR
613     {
614         config.restart_state_path = $<string>3;
615     }
616     ;
617
618 exec:
619     TOKEXEC WHITESPACE STR
620     {
621         struct Autostart *new = smalloc(sizeof(struct Autostart));
622         new->command = $<string>3;
623         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
624     }
625     ;
626
627 terminal:
628     TOKTERMINAL WHITESPACE STR
629     {
630         ELOG("The terminal option is DEPRECATED and has no effect. "
631             "Please remove it from your configuration file.\n");
632     }
633     ;
634
635 font:
636     TOKFONT WHITESPACE STR
637     {
638         config.font = load_font($<string>3, true);
639         printf("font %s\n", $<string>3);
640     }
641     ;
642
643 single_color:
644     TOKSINGLECOLOR WHITESPACE colorpixel
645     {
646         uint32_t *dest = $<single_color>1;
647         *dest = $<number>3;
648     }
649     ;
650
651 color:
652     TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
653     {
654         struct Colortriple *dest = $<color>1;
655
656         dest->border = $<number>3;
657         dest->background = $<number>5;
658         dest->text = $<number>7;
659     }
660     ;
661
662 colorpixel:
663     '#' HEX
664     {
665         char *hex;
666         if (asprintf(&hex, "#%s", $<string>2) == -1)
667             die("asprintf()");
668         $<number>$ = get_colorpixel(hex);
669         free(hex);
670     }
671     ;
672
673
674 binding_modifiers:
675     /* NULL */                               { $<number>$ = 0; }
676     | binding_modifier
677     | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
678     | binding_modifiers '+'                  { $<number>$ = $<number>1; }
679     ;
680
681 binding_modifier:
682     MODIFIER        { $<number>$ = $<number>1; }
683     | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
684     | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
685     ;
686
687 popup_during_fullscreen:
688     TOK_POPUP_DURING_FULLSCREEN WHITESPACE popup_setting
689     {
690         DLOG("popup_during_fullscreen setting: %d\n", $<number>3);
691         config.popup_during_fullscreen = $<number>3;
692     }
693     ;
694
695 popup_setting:
696     TOK_IGNORE              { $<number>$ = PDF_IGNORE; }
697     | TOK_LEAVE_FULLSCREEN  { $<number>$ = PDF_LEAVE_FULLSCREEN; }
698     ;