]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
50058680f2924a45a571eab57b8b21462c9f71c7
[i3/i3] / src / cfgparse.y
1 %{
2 /*
3  * vim:ts=8: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         struct Assignment *assignment;
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 TOKBIND
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 "new_container_orientation"
228 %token TOK_HORIZ "horizontal"
229 %token TOK_VERT "vertical"
230 %token TOK_AUTO "auto"
231 %token TOKNEWCONTAINER "new_container"
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 TOKCONTAINERMODE "default/stacking/tabbed"
239 %token TOKSTACKLIMIT "stack-limit"
240 %token TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
241 %token TOK_IGNORE "ignore"
242 %token TOK_LEAVE_FULLSCREEN "leave_fullscreen"
243
244 %%
245
246 lines: /* empty */
247         | lines WHITESPACE line
248         | lines error
249         | lines line
250         ;
251
252 line:
253         bindline
254         | mode
255         | floating_modifier
256         | orientation
257         | new_container
258         | new_window
259         | focus_follows_mouse
260         | workspace_bar
261         | workspace
262         | assign
263         | ipcsocket
264         | restart_state
265         | exec
266         | single_color
267         | color
268         | terminal
269         | font
270         | comment
271         | popup_during_fullscreen
272         ;
273
274 comment:
275         TOKCOMMENT
276         ;
277
278 command:
279         STR
280         ;
281
282 bindline:
283         binding
284         {
285                 TAILQ_INSERT_TAIL(bindings, $<binding>1, bindings);
286         }
287         ;
288
289 binding:
290         TOKBIND WHITESPACE bind                 { $<binding>$ = $<binding>3; }
291         | TOKBINDSYM WHITESPACE bindsym         { $<binding>$ = $<binding>3; }
292         ;
293
294 bind:
295         binding_modifiers NUMBER WHITESPACE command
296         {
297                 printf("\tFound binding mod%d with key %d and command %s\n", $<number>1, $2, $<string>4);
298                 Binding *new = scalloc(sizeof(Binding));
299
300                 new->keycode = $<number>2;
301                 new->mods = $<number>1;
302                 new->command = $<string>4;
303
304                 $<binding>$ = new;
305         }
306         ;
307
308 bindsym:
309         binding_modifiers word_or_number WHITESPACE command
310         {
311                 printf("\tFound symbolic mod%d with key %s and command %s\n", $<number>1, $<string>2, $<string>4);
312                 Binding *new = scalloc(sizeof(Binding));
313
314                 new->symbol = $<string>2;
315                 new->mods = $<number>1;
316                 new->command = $<string>4;
317
318                 $<binding>$ = new;
319         }
320         ;
321
322 word_or_number:
323         WORD
324         | NUMBER
325         {
326                 asprintf(&$<string>$, "%d", $1);
327         }
328         ;
329
330 mode:
331         TOKMODE WHITESPACE QUOTEDSTRING WHITESPACE '{' modelines '}'
332         {
333                 if (strcasecmp($<string>3, "default") == 0) {
334                         printf("You cannot use the name \"default\" for your mode\n");
335                         exit(1);
336                 }
337                 printf("\t now in mode %s\n", $<string>3);
338                 printf("\t current bindings = %p\n", current_bindings);
339                 Binding *binding;
340                 TAILQ_FOREACH(binding, current_bindings, bindings) {
341                         printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
342                                         binding->mods, binding->keycode, binding->symbol, binding->command);
343                 }
344
345                 struct Mode *mode = scalloc(sizeof(struct Mode));
346                 mode->name = $<string>3;
347                 mode->bindings = current_bindings;
348                 current_bindings = NULL;
349                 SLIST_INSERT_HEAD(&modes, mode, modes);
350         }
351         ;
352
353
354 modelines:
355         /* empty */
356         | modelines modeline
357         ;
358
359 modeline:
360         WHITESPACE
361         | comment
362         | binding
363         {
364                 if (current_bindings == NULL) {
365                         current_bindings = scalloc(sizeof(struct bindings_head));
366                         TAILQ_INIT(current_bindings);
367                 }
368
369                 TAILQ_INSERT_TAIL(current_bindings, $<binding>1, bindings);
370         }
371         ;
372
373 floating_modifier:
374         TOKFLOATING_MODIFIER WHITESPACE binding_modifiers
375         {
376                 DLOG("floating modifier = %d\n", $<number>3);
377                 config.floating_modifier = $<number>3;
378         }
379         ;
380
381 orientation:
382         TOK_ORIENTATION WHITESPACE direction
383         {
384                 DLOG("New containers should start with split direction %d\n", $<number>3);
385                 config.default_orientation = $<number>3;
386         }
387         ;
388
389 direction:
390         TOK_HORIZ       { $<number>$ = HORIZ; }
391         | TOK_VERT      { $<number>$ = VERT; }
392         | TOK_AUTO      { $<number>$ = NO_ORIENTATION; }
393         ;
394
395 new_container:
396         TOKNEWCONTAINER WHITESPACE TOKCONTAINERMODE
397         {
398                 DLOG("new containers will be in mode %d\n", $<number>3);
399                 config.container_mode = $<number>3;
400
401 #if 0
402                 /* We also need to change the layout of the already existing
403                  * workspaces here. Workspaces may exist at this point because
404                  * of the other directives which are modifying workspaces
405                  * (setting the preferred screen or name). While the workspace
406                  * objects are already created, they have never been used.
407                  * Thus, the user very likely awaits the default container mode
408                  * to trigger in this case, regardless of where it is inside
409                  * his configuration file. */
410                 Workspace *ws;
411                 TAILQ_FOREACH(ws, workspaces, workspaces) {
412                         if (ws->table == NULL)
413                                 continue;
414                         switch_layout_mode(global_conn,
415                                            ws->table[0][0],
416                                            config.container_mode);
417                 }
418 #endif
419         }
420         | TOKNEWCONTAINER WHITESPACE TOKSTACKLIMIT WHITESPACE TOKSTACKLIMIT WHITESPACE NUMBER
421         {
422                 DLOG("stack-limit %d with val %d\n", $<number>5, $<number>7);
423                 config.container_stack_limit = $<number>5;
424                 config.container_stack_limit_value = $<number>7;
425
426 #if 0
427                 /* See the comment above */
428                 Workspace *ws;
429                 TAILQ_FOREACH(ws, workspaces, workspaces) {
430                         if (ws->table == NULL)
431                                 continue;
432                         Container *con = ws->table[0][0];
433                         con->stack_limit = config.container_stack_limit;
434                         con->stack_limit_value = config.container_stack_limit_value;
435                 }
436 #endif
437         }
438         ;
439
440 new_window:
441         TOKNEWWINDOW WHITESPACE border_style
442         {
443                 DLOG("new windows should start with border style %d\n", $<number>3);
444                 config.default_border = $<number>3;
445         }
446         ;
447
448 border_style:
449         TOK_NORMAL      { $<number>$ = BS_NORMAL; }
450         | TOK_NONE      { $<number>$ = BS_NONE; }
451         | TOK_1PIXEL    { $<number>$ = BS_1PIXEL; }
452         ;
453
454 bool:
455         NUMBER
456         {
457                 $<number>$ = ($<number>1 == 1);
458         }
459         | WORD
460         {
461                 DLOG("checking word \"%s\"\n", $<string>1);
462                 $<number>$ = (strcasecmp($<string>1, "yes") == 0 ||
463                               strcasecmp($<string>1, "true") == 0 ||
464                               strcasecmp($<string>1, "on") == 0 ||
465                               strcasecmp($<string>1, "enable") == 0 ||
466                               strcasecmp($<string>1, "active") == 0);
467         }
468         ;
469
470 focus_follows_mouse:
471         TOKFOCUSFOLLOWSMOUSE WHITESPACE bool
472         {
473                 DLOG("focus follows mouse = %d\n", $<number>3);
474                 config.disable_focus_follows_mouse = !($<number>3);
475         }
476         ;
477
478 workspace_bar:
479         TOKWORKSPACEBAR WHITESPACE bool
480         {
481                 DLOG("workspace bar = %d\n", $<number>3);
482                 config.disable_workspace_bar = !($<number>3);
483         }
484         ;
485
486 workspace:
487         TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKOUTPUT WHITESPACE OUTPUT optional_workspace_name
488         {
489                 int ws_num = $<number>3;
490                 if (ws_num < 1) {
491                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
492                 } else {
493 #if 0
494                         Workspace *ws = workspace_get(ws_num - 1);
495                         ws->preferred_output = $<string>7;
496                         if ($<string>8 != NULL) {
497                                 workspace_set_name(ws, $<string>8);
498                                 free($<string>8);
499                         }
500 #endif
501                 }
502         }
503         | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE workspace_name
504         {
505                 int ws_num = $<number>3;
506                 if (ws_num < 1) {
507                         DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
508                 } else {
509                         DLOG("workspace name to: %s\n", $<string>5);
510 #if 0
511                         if ($<string>5 != NULL) {
512                                 workspace_set_name(workspace_get(ws_num - 1), $<string>5);
513                                 free($<string>5);
514                         }
515 #endif
516                 }
517         }
518         ;
519
520 optional_workspace_name:
521         /* empty */                     { $<string>$ = NULL; }
522         | WHITESPACE workspace_name     { $<string>$ = $<string>2; }
523         ;
524
525 workspace_name:
526         QUOTEDSTRING         { $<string>$ = $<string>1; }
527         | STR                { $<string>$ = $<string>1; }
528         | WORD               { $<string>$ = $<string>1; }
529         ;
530
531 assign:
532         TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow assign_target
533         {
534 #if 0
535                 printf("assignment of %s\n", $<string>3);
536
537                 struct Assignment *new = $<assignment>6;
538                 printf("  to %d\n", new->workspace);
539                 printf("  floating = %d\n", new->floating);
540                 new->windowclass_title = $<string>3;
541                 TAILQ_INSERT_TAIL(&assignments, new, assignments);
542 #endif
543         }
544         ;
545
546 assign_target:
547         NUMBER
548         {
549 #if 0
550                 struct Assignment *new = scalloc(sizeof(struct Assignment));
551                 new->workspace = $<number>1;
552                 new->floating = ASSIGN_FLOATING_NO;
553                 $<assignment>$ = new;
554 #endif
555         }
556         | '~'
557         {
558 #if 0
559                 struct Assignment *new = scalloc(sizeof(struct Assignment));
560                 new->floating = ASSIGN_FLOATING_ONLY;
561                 $<assignment>$ = new;
562 #endif
563         }
564         | '~' NUMBER
565         {
566 #if 0
567                 struct Assignment *new = scalloc(sizeof(struct Assignment));
568                 new->workspace = $<number>2;
569                 new->floating = ASSIGN_FLOATING;
570                 $<assignment>$ = new;
571 #endif
572         }
573         ;
574
575 window_class:
576         QUOTEDSTRING
577         | STR_NG
578         ;
579
580 optional_arrow:
581         /* NULL */
582         | TOKARROW WHITESPACE
583         ;
584
585 ipcsocket:
586         TOKIPCSOCKET WHITESPACE STR
587         {
588                 config.ipc_socket_path = $<string>3;
589         }
590         ;
591
592 restart_state:
593         TOKRESTARTSTATE WHITESPACE STR
594         {
595                 config.restart_state_path = $<string>3;
596         }
597         ;
598
599 exec:
600         TOKEXEC WHITESPACE STR
601         {
602                 struct Autostart *new = smalloc(sizeof(struct Autostart));
603                 new->command = $<string>3;
604                 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
605         }
606         ;
607
608 terminal:
609         TOKTERMINAL WHITESPACE STR
610         {
611                 ELOG("The terminal option is DEPRECATED and has no effect. "
612                     "Please remove it from your configuration file.\n");
613         }
614         ;
615
616 font:
617         TOKFONT WHITESPACE STR
618         {
619                 config.font = load_font($<string>3, true);
620                 printf("font %s\n", $<string>3);
621         }
622         ;
623
624 single_color:
625         TOKSINGLECOLOR WHITESPACE colorpixel
626         {
627                 uint32_t *dest = $<single_color>1;
628                 *dest = $<number>3;
629         }
630         ;
631
632 color:
633         TOKCOLOR WHITESPACE colorpixel WHITESPACE colorpixel WHITESPACE colorpixel
634         {
635                 struct Colortriple *dest = $<color>1;
636
637                 dest->border = $<number>3;
638                 dest->background = $<number>5;
639                 dest->text = $<number>7;
640         }
641         ;
642
643 colorpixel:
644         '#' HEX
645         {
646                 char *hex;
647                 if (asprintf(&hex, "#%s", $<string>2) == -1)
648                         die("asprintf()");
649                 $<number>$ = get_colorpixel(hex);
650                 free(hex);
651         }
652         ;
653
654
655 binding_modifiers:
656         /* NULL */                               { $<number>$ = 0; }
657         | binding_modifier
658         | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
659         | binding_modifiers '+'                  { $<number>$ = $<number>1; }
660         ;
661
662 binding_modifier:
663         MODIFIER        { $<number>$ = $<number>1; }
664         | TOKCONTROL    { $<number>$ = BIND_CONTROL; }
665         | TOKSHIFT      { $<number>$ = BIND_SHIFT; }
666         ;
667
668 popup_during_fullscreen:
669         TOK_POPUP_DURING_FULLSCREEN WHITESPACE popup_setting
670         {
671                 DLOG("popup_during_fullscreen setting: %d\n", $<number>3);
672                 config.popup_during_fullscreen = $<number>3;
673         }
674         ;
675
676 popup_setting:
677         TOK_IGNORE              { $<number>$ = PDF_IGNORE; }
678         | TOK_LEAVE_FULLSCREEN  { $<number>$ = PDF_LEAVE_FULLSCREEN; }
679         ;