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