]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
Implement fake-outputs option (cmdline, cfg) for multi-monitor testing
[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 <sys/wait.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11
12 #include "all.h"
13
14 static pid_t configerror_pid = -1;
15
16 static Match current_match;
17 static Barconfig current_bar;
18 /* The pattern which was specified by the user, for example -misc-fixed-*. We
19  * store this in a separate variable because in the i3 config struct we just
20  * store the i3Font. */
21 static char *font_pattern;
22
23 typedef struct yy_buffer_state *YY_BUFFER_STATE;
24 extern int yylex(struct context *context);
25 extern int yyparse(void);
26 extern int yylex_destroy(void);
27 extern FILE *yyin;
28 YY_BUFFER_STATE yy_scan_string(const char *);
29
30 static struct bindings_head *current_bindings;
31 static struct context *context;
32
33 /* We don’t need yydebug for now, as we got decent error messages using
34  * yyerror(). Should you ever want to extend the parser, it might be handy
35  * to just comment it in again, so it stays here. */
36 //int yydebug = 1;
37
38 void yyerror(const char *error_message) {
39     context->has_errors = true;
40
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     char buffer[context->last_column+1];
47     buffer[context->last_column] = '\0';
48     for (int c = 1; c <= context->last_column; c++)
49         buffer[c-1] = (c >= context->first_column ? '^' : ' ');
50     ELOG("CONFIG:   %s\n", buffer);
51     ELOG("\n");
52 }
53
54 int yywrap(void) {
55     return 1;
56 }
57
58 /*
59  * Goes through each line of buf (separated by \n) and checks for statements /
60  * commands which only occur in i3 v4 configuration files. If it finds any, it
61  * returns version 4, otherwise it returns version 3.
62  *
63  */
64 static int detect_version(char *buf) {
65     char *walk = buf;
66     char *line = buf;
67     while (*walk != '\0') {
68         if (*walk != '\n') {
69             walk++;
70             continue;
71         }
72
73         /* check for some v4-only statements */
74         if (strncasecmp(line, "bindcode", strlen("bindcode")) == 0 ||
75             strncasecmp(line, "force_focus_wrapping", strlen("force_focus_wrapping")) == 0 ||
76             strncasecmp(line, "# i3 config file (v4)", strlen("# i3 config file (v4)")) == 0 ||
77             strncasecmp(line, "workspace_layout", strlen("workspace_layout")) == 0) {
78             printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
79             return 4;
80         }
81
82         /* if this is a bind statement, we can check the command */
83         if (strncasecmp(line, "bind", strlen("bind")) == 0) {
84             char *bind = strchr(line, ' ');
85             if (bind == NULL)
86                 goto next;
87             while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
88                 bind++;
89             if (*bind == '\0')
90                 goto next;
91             if ((bind = strchr(bind, ' ')) == NULL)
92                 goto next;
93             while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
94                 bind++;
95             if (*bind == '\0')
96                 goto next;
97             if (strncasecmp(bind, "layout", strlen("layout")) == 0 ||
98                 strncasecmp(bind, "floating", strlen("floating")) == 0 ||
99                 strncasecmp(bind, "workspace", strlen("workspace")) == 0 ||
100                 strncasecmp(bind, "focus left", strlen("focus left")) == 0 ||
101                 strncasecmp(bind, "focus right", strlen("focus right")) == 0 ||
102                 strncasecmp(bind, "focus up", strlen("focus up")) == 0 ||
103                 strncasecmp(bind, "focus down", strlen("focus down")) == 0 ||
104                 strncasecmp(bind, "border normal", strlen("border normal")) == 0 ||
105                 strncasecmp(bind, "border 1pixel", strlen("border 1pixel")) == 0 ||
106                 strncasecmp(bind, "border borderless", strlen("border borderless")) == 0 ||
107                 strncasecmp(bind, "--no-startup-id", strlen("--no-startup-id")) == 0 ||
108                 strncasecmp(bind, "bar", strlen("bar")) == 0) {
109                 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
110                 return 4;
111             }
112         }
113
114 next:
115         /* advance to the next line */
116         walk++;
117         line = walk;
118     }
119
120     return 3;
121 }
122
123 /*
124  * Calls i3-migrate-config-to-v4 to migrate a configuration file (input
125  * buffer).
126  *
127  * Returns the converted config file or NULL if there was an error (for
128  * example the script could not be found in $PATH or the i3 executable’s
129  * directory).
130  *
131  */
132 static char *migrate_config(char *input, off_t size) {
133     int writepipe[2];
134     int readpipe[2];
135
136     if (pipe(writepipe) != 0 ||
137         pipe(readpipe) != 0) {
138         warn("migrate_config: Could not create pipes");
139         return NULL;
140     }
141
142     pid_t pid = fork();
143     if (pid == -1) {
144         warn("Could not fork()");
145         return NULL;
146     }
147
148     /* child */
149     if (pid == 0) {
150         /* close writing end of writepipe, connect reading side to stdin */
151         close(writepipe[1]);
152         dup2(writepipe[0], 0);
153
154         /* close reading end of readpipe, connect writing side to stdout */
155         close(readpipe[0]);
156         dup2(readpipe[1], 1);
157
158         static char *argv[] = {
159             NULL, /* will be replaced by the executable path */
160             NULL
161         };
162         exec_i3_utility("i3-migrate-config-to-v4", argv);
163     }
164
165     /* parent */
166
167     /* close reading end of the writepipe (connected to the script’s stdin) */
168     close(writepipe[0]);
169
170     /* write the whole config file to the pipe, the script will read everything
171      * immediately */
172     int written = 0;
173     int ret;
174     while (written < size) {
175         if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
176             warn("Could not write to pipe");
177             return NULL;
178         }
179         written += ret;
180     }
181     close(writepipe[1]);
182
183     /* close writing end of the readpipe (connected to the script’s stdout) */
184     close(readpipe[1]);
185
186     /* read the script’s output */
187     int conv_size = 65535;
188     char *converted = malloc(conv_size);
189     int read_bytes = 0;
190     do {
191         if (read_bytes == conv_size) {
192             conv_size += 65535;
193             converted = realloc(converted, conv_size);
194         }
195         ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
196         if (ret == -1) {
197             warn("Cannot read from pipe");
198             FREE(converted);
199             return NULL;
200         }
201         read_bytes += ret;
202     } while (ret > 0);
203
204     /* get the returncode */
205     int status;
206     wait(&status);
207     if (!WIFEXITED(status)) {
208         fprintf(stderr, "Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
209         return NULL;
210     }
211
212     int returncode = WEXITSTATUS(status);
213     if (returncode != 0) {
214         fprintf(stderr, "Migration process exit code was != 0\n");
215         if (returncode == 2) {
216             fprintf(stderr, "could not start the migration script\n");
217             /* TODO: script was not found. tell the user to fix his system or create a v4 config */
218         } else if (returncode == 1) {
219             fprintf(stderr, "This already was a v4 config. Please add the following line to your config file:\n");
220             fprintf(stderr, "# i3 config file (v4)\n");
221             /* TODO: nag the user with a message to include a hint for i3 in his config file */
222         }
223         return NULL;
224     }
225
226     return converted;
227 }
228
229 /*
230  * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
231  * it exited (or could not be started, depending on the exit code).
232  *
233  */
234 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
235     ev_child_stop(EV_A_ watcher);
236     if (!WIFEXITED(watcher->rstatus)) {
237         fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
238         return;
239     }
240
241     int exitcode = WEXITSTATUS(watcher->rstatus);
242     printf("i3-nagbar process exited with status %d\n", exitcode);
243     if (exitcode == 2) {
244         fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
245     }
246
247     configerror_pid = -1;
248 }
249
250 /* We need ev >= 4 for the following code. Since it is not *that* important (it
251  * only makes sure that there are no i3-nagbar instances left behind) we still
252  * support old systems with libev 3. */
253 #if EV_VERSION_MAJOR >= 4
254 /*
255  * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
256  * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
257  *
258  */
259 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
260     if (configerror_pid != -1) {
261         LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", configerror_pid);
262         kill(configerror_pid, SIGKILL);
263     }
264 }
265 #endif
266
267 /*
268  * Starts an i3-nagbar process which alerts the user that his configuration
269  * file contains one or more errors. Also offers two buttons: One to launch an
270  * $EDITOR on the config file and another one to launch a $PAGER on the error
271  * logfile.
272  *
273  */
274 static void start_configerror_nagbar(const char *config_path) {
275     if (only_check_config)
276         return;
277
278     fprintf(stderr, "Starting i3-nagbar due to configuration errors\n");
279     configerror_pid = fork();
280     if (configerror_pid == -1) {
281         warn("Could not fork()");
282         return;
283     }
284
285     /* child */
286     if (configerror_pid == 0) {
287         char *editaction,
288              *pageraction;
289         sasprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path);
290         sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
291         char *argv[] = {
292             NULL, /* will be replaced by the executable path */
293             "-t",
294             (context->has_errors ? "error" : "warning"),
295             "-m",
296             (context->has_errors ?
297              "You have an error in your i3 config file!" :
298              "Your config is outdated. Please fix the warnings to make sure everything works."),
299             "-b",
300             "edit config",
301             editaction,
302             (errorfilename ? "-b" : NULL),
303             (context->has_errors ? "show errors" : "show warnings"),
304             pageraction,
305             NULL
306         };
307         exec_i3_utility("i3-nagbar", argv);
308     }
309
310     /* parent */
311     /* install a child watcher */
312     ev_child *child = smalloc(sizeof(ev_child));
313     ev_child_init(child, &nagbar_exited, configerror_pid, 0);
314     ev_child_start(main_loop, child);
315
316 /* We need ev >= 4 for the following code. Since it is not *that* important (it
317  * only makes sure that there are no i3-nagbar instances left behind) we still
318  * support old systems with libev 3. */
319 #if EV_VERSION_MAJOR >= 4
320     /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
321      * still running) */
322     ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
323     ev_cleanup_init(cleanup, nagbar_cleanup);
324     ev_cleanup_start(main_loop, cleanup);
325 #endif
326 }
327
328 /*
329  * Kills the configerror i3-nagbar process, if any.
330  *
331  * Called when reloading/restarting.
332  *
333  * If wait_for_it is set (restarting), this function will waitpid(), otherwise,
334  * ev is assumed to handle it (reloading).
335  *
336  */
337 void kill_configerror_nagbar(bool wait_for_it) {
338     if (configerror_pid == -1)
339         return;
340
341     if (kill(configerror_pid, SIGTERM) == -1)
342         warn("kill(configerror_nagbar) failed");
343
344     if (!wait_for_it)
345         return;
346
347     /* When restarting, we don’t enter the ev main loop anymore and after the
348      * exec(), our old pid is no longer watched. So, ev won’t handle SIGCHLD
349      * for us and we would end up with a <defunct> process. Therefore we
350      * waitpid() here. */
351     waitpid(configerror_pid, NULL, 0);
352 }
353
354 /*
355  * Checks for duplicate key bindings (the same keycode or keysym is configured
356  * more than once). If a duplicate binding is found, a message is printed to
357  * stderr and the has_errors variable is set to true, which will start
358  * i3-nagbar.
359  *
360  */
361 static void check_for_duplicate_bindings(struct context *context) {
362     Binding *bind, *current;
363     TAILQ_FOREACH(current, bindings, bindings) {
364         TAILQ_FOREACH(bind, bindings, bindings) {
365             /* Abort when we reach the current keybinding, only check the
366              * bindings before */
367             if (bind == current)
368                 break;
369
370             /* Check if one is using keysym while the other is using bindsym.
371              * If so, skip. */
372             /* XXX: It should be checked at a later place (when translating the
373              * keysym to keycodes) if there are any duplicates */
374             if ((bind->symbol == NULL && current->symbol != NULL) ||
375                 (bind->symbol != NULL && current->symbol == NULL))
376                 continue;
377
378             /* If bind is NULL, current has to be NULL, too (see above).
379              * If the keycodes differ, it can't be a duplicate. */
380             if (bind->symbol != NULL &&
381                 strcasecmp(bind->symbol, current->symbol) != 0)
382                 continue;
383
384             /* Check if the keycodes or modifiers are different. If so, they
385              * can't be duplicate */
386             if (bind->keycode != current->keycode ||
387                 bind->mods != current->mods)
388                 continue;
389             context->has_errors = true;
390             if (current->keycode != 0) {
391                 ELOG("Duplicate keybinding in config file:\n  modmask %d with keycode %d, command \"%s\"\n",
392                      current->mods, current->keycode, current->command);
393             } else {
394                 ELOG("Duplicate keybinding in config file:\n  modmask %d with keysym %s, command \"%s\"\n",
395                      current->mods, current->symbol, current->command);
396             }
397         }
398     }
399 }
400
401 static void migrate_i3bar_exec(struct Autostart *exec) {
402     ELOG("**********************************************************************\n");
403     ELOG("IGNORING exec command: %s\n", exec->command);
404     ELOG("It contains \"i3bar\". Since i3 v4.1, i3bar will be automatically started\n");
405     ELOG("for each 'bar' configuration block in your i3 config. Please remove the exec\n");
406     ELOG("line and add the following to your i3 config:\n");
407     ELOG("\n");
408     ELOG("    bar {\n");
409     ELOG("        status_command i3status\n");
410     ELOG("    }\n");
411     ELOG("**********************************************************************\n");
412
413     /* Generate a dummy bar configuration */
414     Barconfig *bar_config = scalloc(sizeof(Barconfig));
415     /* The hard-coded ID is not a problem. It does not conflict with the
416      * auto-generated bar IDs and having multiple hard-coded IDs is irrelevant
417      * – they all just contain status_command = i3status */
418     bar_config->id = sstrdup("migrate-bar");
419     bar_config->status_command = sstrdup("i3status");
420     TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
421
422     /* Trigger an i3-nagbar */
423     context->has_warnings = true;
424 }
425
426 void parse_file(const char *f) {
427     SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
428     int fd, ret, read_bytes = 0;
429     struct stat stbuf;
430     char *buf;
431     FILE *fstr;
432     char buffer[1026], key[512], value[512];
433
434     if ((fd = open(f, O_RDONLY)) == -1)
435         die("Could not open configuration file: %s\n", strerror(errno));
436
437     if (fstat(fd, &stbuf) == -1)
438         die("Could not fstat file: %s\n", strerror(errno));
439
440     buf = scalloc((stbuf.st_size + 1) * sizeof(char));
441     while (read_bytes < stbuf.st_size) {
442         if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
443             die("Could not read(): %s\n", strerror(errno));
444         read_bytes += ret;
445     }
446
447     if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
448         die("Could not lseek: %s\n", strerror(errno));
449
450     if ((fstr = fdopen(fd, "r")) == NULL)
451         die("Could not fdopen: %s\n", strerror(errno));
452
453     while (!feof(fstr)) {
454         if (fgets(buffer, 1024, fstr) == NULL) {
455             if (feof(fstr))
456                 break;
457             die("Could not read configuration file\n");
458         }
459
460         /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
461         if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
462             key[0] == '#' || strlen(key) < 3)
463             continue;
464
465         if (strcasecmp(key, "set") == 0) {
466             if (value[0] != '$') {
467                 ELOG("Malformed variable assignment, name has to start with $\n");
468                 continue;
469             }
470
471             /* get key/value for this variable */
472             char *v_key = value, *v_value;
473             if (strstr(value, " ") == NULL && strstr(value, "\t") == NULL) {
474                 ELOG("Malformed variable assignment, need a value\n");
475                 continue;
476             }
477
478             if (!(v_value = strstr(value, " ")))
479                 v_value = strstr(value, "\t");
480
481             *(v_value++) = '\0';
482             while (*v_value == '\t' || *v_value == ' ')
483                 v_value++;
484
485             struct Variable *new = scalloc(sizeof(struct Variable));
486             new->key = sstrdup(v_key);
487             new->value = sstrdup(v_value);
488             SLIST_INSERT_HEAD(&variables, new, variables);
489             DLOG("Got new variable %s = %s\n", v_key, v_value);
490             continue;
491         }
492     }
493     fclose(fstr);
494
495     /* For every custom variable, see how often it occurs in the file and
496      * how much extra bytes it requires when replaced. */
497     struct Variable *current, *nearest;
498     int extra_bytes = 0;
499     /* We need to copy the buffer because we need to invalidate the
500      * variables (otherwise we will count them twice, which is bad when
501      * 'extra' is negative) */
502     char *bufcopy = sstrdup(buf);
503     SLIST_FOREACH(current, &variables, variables) {
504         int extra = (strlen(current->value) - strlen(current->key));
505         char *next;
506         for (next = bufcopy;
507              next < (bufcopy + stbuf.st_size) &&
508              (next = strcasestr(next, current->key)) != NULL;
509              next += strlen(current->key)) {
510             *next = '_';
511             extra_bytes += extra;
512         }
513     }
514     FREE(bufcopy);
515
516     /* Then, allocate a new buffer and copy the file over to the new one,
517      * but replace occurences of our variables */
518     char *walk = buf, *destwalk;
519     char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
520     destwalk = new;
521     while (walk < (buf + stbuf.st_size)) {
522         /* Find the next variable */
523         SLIST_FOREACH(current, &variables, variables)
524             current->next_match = strcasestr(walk, current->key);
525         nearest = NULL;
526         int distance = stbuf.st_size;
527         SLIST_FOREACH(current, &variables, variables) {
528             if (current->next_match == NULL)
529                 continue;
530             if ((current->next_match - walk) < distance) {
531                 distance = (current->next_match - walk);
532                 nearest = current;
533             }
534         }
535         if (nearest == NULL) {
536             /* If there are no more variables, we just copy the rest */
537             strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
538             destwalk += (buf + stbuf.st_size) - walk;
539             *destwalk = '\0';
540             break;
541         } else {
542             /* Copy until the next variable, then copy its value */
543             strncpy(destwalk, walk, distance);
544             strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
545             walk += distance + strlen(nearest->key);
546             destwalk += distance + strlen(nearest->value);
547         }
548     }
549
550     /* analyze the string to find out whether this is an old config file (3.x)
551      * or a new config file (4.x). If it’s old, we run the converter script. */
552     int version = detect_version(buf);
553     if (version == 3) {
554         /* We need to convert this v3 configuration */
555         char *converted = migrate_config(new, stbuf.st_size);
556         if (converted != NULL) {
557             ELOG("\n");
558             ELOG("****************************************************************\n");
559             ELOG("NOTE: Automatically converted configuration file from v3 to v4.\n");
560             ELOG("\n");
561             ELOG("Please convert your config file to v4. You can use this command:\n");
562             ELOG("    mv %s %s.O\n", f, f);
563             ELOG("    i3-migrate-config-to-v4 %s.O > %s\n", f, f);
564             ELOG("****************************************************************\n");
565             ELOG("\n");
566             free(new);
567             new = converted;
568         } else {
569             printf("\n");
570             printf("**********************************************************************\n");
571             printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
572             printf("was not correctly installed on your system?\n");
573             printf("**********************************************************************\n");
574             printf("\n");
575         }
576     }
577
578     /* now lex/parse it */
579     yy_scan_string(new);
580
581     context = scalloc(sizeof(struct context));
582     context->filename = f;
583
584     if (yyparse() != 0) {
585         fprintf(stderr, "Could not parse configfile\n");
586         exit(1);
587     }
588
589     check_for_duplicate_bindings(context);
590
591     /* XXX: The following code will be removed in i3 v4.3 (three releases from
592      * now, as of 2011-10-22) */
593     /* Check for any exec or exec_always lines starting i3bar. We remove these
594      * and add a bar block instead. Additionally, a i3-nagbar warning (not an
595      * error) will be displayed so that users update their config file. */
596     struct Autostart *exec, *next;
597     for (exec = TAILQ_FIRST(&autostarts); exec; ) {
598         next = TAILQ_NEXT(exec, autostarts);
599         if (strstr(exec->command, "i3bar") != NULL) {
600             migrate_i3bar_exec(exec);
601             TAILQ_REMOVE(&autostarts, exec, autostarts);
602         }
603         exec = next;
604     }
605
606     for (exec = TAILQ_FIRST(&autostarts_always); exec; ) {
607         next = TAILQ_NEXT(exec, autostarts_always);
608         if (strstr(exec->command, "i3bar") != NULL) {
609             migrate_i3bar_exec(exec);
610             TAILQ_REMOVE(&autostarts_always, exec, autostarts_always);
611         }
612         exec = next;
613     }
614
615     if (context->has_errors || context->has_warnings) {
616         ELOG("FYI: You are using i3 version " I3_VERSION "\n");
617         if (version == 3)
618             ELOG("Please convert your configfile first, then fix any remaining errors (see above).\n");
619         start_configerror_nagbar(f);
620     }
621
622     yylex_destroy();
623     FREE(context->line_copy);
624     free(context);
625     FREE(font_pattern);
626     free(new);
627     free(buf);
628
629     while (!SLIST_EMPTY(&variables)) {
630         current = SLIST_FIRST(&variables);
631         FREE(current->key);
632         FREE(current->value);
633         SLIST_REMOVE_HEAD(&variables, variables);
634         FREE(current);
635     }
636 }
637
638 %}
639
640 %error-verbose
641 %lex-param { struct context *context }
642
643 %union {
644     int number;
645     char *string;
646     uint32_t *single_color;
647     struct Colortriple *color;
648     Match *match;
649     struct Binding *binding;
650 }
651
652 %token  <number>        NUMBER                      "<number>"
653 %token  <string>        WORD                        "<word>"
654 %token  <string>        STR                         "<string>"
655 %token  <string>        STR_NG                      "<string (non-greedy)>"
656 %token  <string>        HEXCOLOR                    "#<hex>"
657 %token  <string>        OUTPUT                      "<RandR output>"
658 %token                  TOKBINDCODE
659 %token                  TOKTERMINAL
660 %token                  TOKCOMMENT                  "<comment>"
661 %token                  TOKFONT                     "font"
662 %token                  TOKBINDSYM                  "bindsym"
663 %token  <number>        MODIFIER                    "<modifier>"
664 %token                  TOKCONTROL                  "control"
665 %token                  TOKSHIFT                    "shift"
666 %token                  TOKFLOATING_MODIFIER        "floating_modifier"
667 %token                  TOKFLOATING_MAXIMUM_SIZE    "floating_maximum_size"
668 %token                  TOKFLOATING_MINIMUM_SIZE    "floating_minimum_size"
669 %token  <string>        QUOTEDSTRING                "<quoted string>"
670 %token                  TOKWORKSPACE                "workspace"
671 %token                  TOKOUTPUT                   "output"
672 %token                  TOKASSIGN                   "assign"
673 %token                  TOKSET
674 %token                  TOKIPCSOCKET                "ipc_socket"
675 %token                  TOKRESTARTSTATE             "restart_state"
676 %token                  TOKEXEC                     "exec"
677 %token                  TOKEXEC_ALWAYS              "exec_always"
678 %token  <single_color>  TOKSINGLECOLOR
679 %token  <color>         TOKCOLOR
680 %token                  TOKARROW                    "→"
681 %token                  TOKMODE                     "mode"
682 %token                  TOK_BAR                     "bar"
683 %token                  TOK_ORIENTATION             "default_orientation"
684 %token                  TOK_HORIZ                   "horizontal"
685 %token                  TOK_VERT                    "vertical"
686 %token                  TOK_AUTO                    "auto"
687 %token                  TOK_WORKSPACE_LAYOUT        "workspace_layout"
688 %token                  TOKNEWWINDOW                "new_window"
689 %token                  TOKNEWFLOAT                 "new_float"
690 %token                  TOK_NORMAL                  "normal"
691 %token                  TOK_NONE                    "none"
692 %token                  TOK_1PIXEL                  "1pixel"
693 %token                  TOKFOCUSFOLLOWSMOUSE        "focus_follows_mouse"
694 %token                  TOK_FORCE_FOCUS_WRAPPING    "force_focus_wrapping"
695 %token                  TOK_FORCE_XINERAMA          "force_xinerama"
696 %token                  TOK_FAKE_OUTPUTS            "fake_outputs"
697 %token                  TOK_WORKSPACE_AUTO_BAF      "workspace_auto_back_and_forth"
698 %token                  TOKWORKSPACEBAR             "workspace_bar"
699 %token                  TOK_DEFAULT                 "default"
700 %token                  TOK_STACKING                "stacking"
701 %token                  TOK_TABBED                  "tabbed"
702 %token  <number>        TOKSTACKLIMIT               "stack-limit"
703 %token                  TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
704 %token                  TOK_IGNORE                  "ignore"
705 %token                  TOK_LEAVE_FULLSCREEN        "leave_fullscreen"
706 %token                  TOK_FOR_WINDOW              "for_window"
707
708 %token                  TOK_BAR_OUTPUT              "output (bar)"
709 %token                  TOK_BAR_TRAY_OUTPUT         "tray_output"
710 %token                  TOK_BAR_SOCKET_PATH         "socket_path"
711 %token                  TOK_BAR_MODE                "mode (bar)"
712 %token                  TOK_BAR_HIDE                "hide"
713 %token                  TOK_BAR_DOCK                "dock"
714 %token                  TOK_BAR_MODIFIER            "modifier (bar)"
715 %token                  TOK_BAR_CONTROL             "shift (bar)"
716 %token                  TOK_BAR_SHIFT               "control (bar)"
717 %token                  TOK_BAR_MOD1                "Mod1"
718 %token                  TOK_BAR_MOD2                "Mod2"
719 %token                  TOK_BAR_MOD3                "Mod3"
720 %token                  TOK_BAR_MOD4                "Mod4"
721 %token                  TOK_BAR_MOD5                "Mod5"
722 %token                  TOK_BAR_POSITION            "position"
723 %token                  TOK_BAR_BOTTOM              "bottom"
724 %token                  TOK_BAR_TOP                 "top"
725 %token                  TOK_BAR_STATUS_COMMAND      "status_command"
726 %token                  TOK_BAR_I3BAR_COMMAND       "i3bar_command"
727 %token                  TOK_BAR_FONT                "font (bar)"
728 %token                  TOK_BAR_WORKSPACE_BUTTONS   "workspace_buttons"
729 %token                  TOK_BAR_VERBOSE             "verbose"
730 %token                  TOK_BAR_COLORS              "colors"
731 %token                  TOK_BAR_COLOR_BACKGROUND    "background"
732 %token                  TOK_BAR_COLOR_STATUSLINE    "statusline"
733 %token                  TOK_BAR_COLOR_FOCUSED_WORKSPACE "focused_workspace"
734 %token                  TOK_BAR_COLOR_ACTIVE_WORKSPACE "active_workspace"
735 %token                  TOK_BAR_COLOR_INACTIVE_WORKSPACE "inactive_workspace"
736 %token                  TOK_BAR_COLOR_URGENT_WORKSPACE "urgent_workspace"
737 %token                  TOK_NO_STARTUP_ID           "--no-startup-id"
738
739 %token              TOK_MARK            "mark"
740 %token              TOK_CLASS           "class"
741 %token              TOK_INSTANCE        "instance"
742 %token              TOK_WINDOW_ROLE     "window_role"
743 %token              TOK_ID              "id"
744 %token              TOK_CON_ID          "con_id"
745 %token              TOK_TITLE           "title"
746 %token              TOK_URGENT          "urgent"
747
748 %type   <binding>       binding
749 %type   <binding>       bindcode
750 %type   <binding>       bindsym
751 %type   <number>        binding_modifiers
752 %type   <number>        binding_modifier
753 %type   <number>        direction
754 %type   <number>        layout_mode
755 %type   <number>        border_style
756 %type   <number>        new_window
757 %type   <number>        new_float
758 %type   <number>        colorpixel
759 %type   <number>        bool
760 %type   <number>        popup_setting
761 %type   <number>        bar_position_position
762 %type   <number>        bar_mode_mode
763 %type   <number>        bar_modifier_modifier
764 %type   <number>        optional_no_startup_id
765 %type   <string>        command
766 %type   <string>        word_or_number
767 %type   <string>        qstring_or_number
768 %type   <string>        optional_workspace_name
769 %type   <string>        workspace_name
770 %type   <string>        window_class
771
772 %%
773
774 lines: /* empty */
775     | lines error
776     | lines line
777     ;
778
779 line:
780     bindline
781     | for_window
782     | mode
783     | bar
784     | floating_maximum_size
785     | floating_minimum_size
786     | floating_modifier
787     | orientation
788     | workspace_layout
789     | new_window
790     | new_float
791     | focus_follows_mouse
792     | force_focus_wrapping
793     | force_xinerama
794     | fake_outputs
795     | workspace_back_and_forth
796     | workspace_bar
797     | workspace
798     | assign
799     | ipcsocket
800     | restart_state
801     | exec
802     | exec_always
803     | single_color
804     | color
805     | terminal
806     | font
807     | comment
808     | popup_during_fullscreen
809     ;
810
811 comment:
812     TOKCOMMENT
813     ;
814
815 command:
816     STR
817     ;
818
819 bindline:
820     binding
821     {
822         TAILQ_INSERT_TAIL(bindings, $1, bindings);
823     }
824     ;
825
826 binding:
827     TOKBINDCODE bindcode         { $$ = $2; }
828     | TOKBINDSYM bindsym         { $$ = $2; }
829     ;
830
831 bindcode:
832     binding_modifiers NUMBER command
833     {
834         printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
835         Binding *new = scalloc(sizeof(Binding));
836
837         new->keycode = $2;
838         new->mods = $1;
839         new->command = $3;
840
841         $$ = new;
842     }
843     ;
844
845 bindsym:
846     binding_modifiers word_or_number command
847     {
848         printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
849         Binding *new = scalloc(sizeof(Binding));
850
851         new->symbol = $2;
852         new->mods = $1;
853         new->command = $3;
854
855         $$ = new;
856     }
857     ;
858
859 for_window:
860     TOK_FOR_WINDOW match command
861     {
862         if (match_is_empty(&current_match)) {
863             ELOG("Match is empty, ignoring this for_window statement\n");
864             break;
865         }
866         printf("\t should execute command %s for the criteria mentioned above\n", $3);
867         Assignment *assignment = scalloc(sizeof(Assignment));
868         assignment->type = A_COMMAND;
869         assignment->match = current_match;
870         assignment->dest.command = $3;
871         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
872     }
873     ;
874
875 match:
876     | matchstart criteria matchend
877     {
878         printf("match parsed\n");
879     }
880     ;
881
882 matchstart:
883     '['
884     {
885         printf("start\n");
886         match_init(&current_match);
887     }
888     ;
889
890 matchend:
891     ']'
892     {
893         printf("match specification finished\n");
894     }
895     ;
896
897 criteria:
898     criteria criterion
899     | criterion
900     ;
901
902 criterion:
903     TOK_CLASS '=' STR
904     {
905         printf("criteria: class = %s\n", $3);
906         current_match.class = regex_new($3);
907         free($3);
908     }
909     | TOK_INSTANCE '=' STR
910     {
911         printf("criteria: instance = %s\n", $3);
912         current_match.instance = regex_new($3);
913         free($3);
914     }
915     | TOK_WINDOW_ROLE '=' STR
916     {
917         printf("criteria: window_role = %s\n", $3);
918         current_match.role = regex_new($3);
919         free($3);
920     }
921     | TOK_CON_ID '=' STR
922     {
923         printf("criteria: id = %s\n", $3);
924         char *end;
925         long parsed = strtol($3, &end, 10);
926         if (parsed == LONG_MIN ||
927             parsed == LONG_MAX ||
928             parsed < 0 ||
929             (end && *end != '\0')) {
930             ELOG("Could not parse con id \"%s\"\n", $3);
931         } else {
932             current_match.con_id = (Con*)parsed;
933             printf("id as int = %p\n", current_match.con_id);
934         }
935     }
936     | TOK_ID '=' STR
937     {
938         printf("criteria: window id = %s\n", $3);
939         char *end;
940         long parsed = strtol($3, &end, 10);
941         if (parsed == LONG_MIN ||
942             parsed == LONG_MAX ||
943             parsed < 0 ||
944             (end && *end != '\0')) {
945             ELOG("Could not parse window id \"%s\"\n", $3);
946         } else {
947             current_match.id = parsed;
948             printf("window id as int = %d\n", current_match.id);
949         }
950     }
951     | TOK_MARK '=' STR
952     {
953         printf("criteria: mark = %s\n", $3);
954         current_match.mark = regex_new($3);
955         free($3);
956     }
957     | TOK_TITLE '=' STR
958     {
959         printf("criteria: title = %s\n", $3);
960         current_match.title = regex_new($3);
961         free($3);
962     }
963     | TOK_URGENT '=' STR
964     {
965         printf("criteria: urgent = %s\n", $3);
966         if (strcasecmp($3, "latest") == 0 ||
967             strcasecmp($3, "newest") == 0 ||
968             strcasecmp($3, "recent") == 0 ||
969             strcasecmp($3, "last") == 0) {
970             current_match.urgent = U_LATEST;
971         } else if (strcasecmp($3, "oldest") == 0 ||
972                    strcasecmp($3, "first") == 0) {
973             current_match.urgent = U_OLDEST;
974         }
975         free($3);
976     }
977     ;
978
979 qstring_or_number:
980     QUOTEDSTRING
981     | NUMBER { sasprintf(&$$, "%d", $1); }
982     ;
983
984 word_or_number:
985     WORD
986     | NUMBER
987     {
988         sasprintf(&$$, "%d", $1);
989     }
990     ;
991
992 mode:
993     TOKMODE QUOTEDSTRING '{' modelines '}'
994     {
995         if (strcasecmp($2, "default") == 0) {
996             printf("You cannot use the name \"default\" for your mode\n");
997             exit(1);
998         }
999         printf("\t now in mode %s\n", $2);
1000         printf("\t current bindings = %p\n", current_bindings);
1001         Binding *binding;
1002         TAILQ_FOREACH(binding, current_bindings, bindings) {
1003             printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
1004                             binding->mods, binding->keycode, binding->symbol, binding->command);
1005         }
1006
1007         struct Mode *mode = scalloc(sizeof(struct Mode));
1008         mode->name = $2;
1009         mode->bindings = current_bindings;
1010         current_bindings = NULL;
1011         SLIST_INSERT_HEAD(&modes, mode, modes);
1012     }
1013     ;
1014
1015
1016 modelines:
1017     /* empty */
1018     | modelines modeline
1019     ;
1020
1021 modeline:
1022     comment
1023     | binding
1024     {
1025         if (current_bindings == NULL) {
1026             current_bindings = scalloc(sizeof(struct bindings_head));
1027             TAILQ_INIT(current_bindings);
1028         }
1029
1030         TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
1031     }
1032     ;
1033
1034 bar:
1035     TOK_BAR '{' barlines '}'
1036     {
1037         printf("\t new bar configuration finished, saving.\n");
1038         /* Generate a unique ID for this bar */
1039         current_bar.id = sstrdup("bar-XXXXXX");
1040         /* This works similar to mktemp in that it replaces the last six X with
1041          * random letters, but without the restriction that the given buffer
1042          * has to contain a valid path name. */
1043         char *x = current_bar.id + strlen("bar-");
1044         while (*x != '\0') {
1045             *(x++) = (rand() % 26) + 'a';
1046         }
1047
1048         /* If no font was explicitly set, we use the i3 font as default */
1049         if (!current_bar.font && font_pattern)
1050             current_bar.font = sstrdup(font_pattern);
1051
1052         /* Copy the current (static) structure into a dynamically allocated
1053          * one, then cleanup our static one. */
1054         Barconfig *bar_config = scalloc(sizeof(Barconfig));
1055         memcpy(bar_config, &current_bar, sizeof(Barconfig));
1056         TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
1057
1058         memset(&current_bar, '\0', sizeof(Barconfig));
1059     }
1060     ;
1061
1062 barlines:
1063     /* empty */
1064     | barlines barline
1065     ;
1066
1067 barline:
1068     comment
1069     | bar_status_command
1070     | bar_i3bar_command
1071     | bar_output
1072     | bar_tray_output
1073     | bar_position
1074     | bar_mode
1075     | bar_modifier
1076     | bar_font
1077     | bar_workspace_buttons
1078     | bar_verbose
1079     | bar_socket_path
1080     | bar_colors
1081     | bar_color_background
1082     | bar_color_statusline
1083     | bar_color_focused_workspace
1084     | bar_color_active_workspace
1085     | bar_color_inactive_workspace
1086     | bar_color_urgent_workspace
1087     ;
1088
1089 bar_status_command:
1090     TOK_BAR_STATUS_COMMAND STR
1091     {
1092         DLOG("should add status command %s\n", $2);
1093         FREE(current_bar.status_command);
1094         current_bar.status_command = $2;
1095     }
1096     ;
1097
1098 bar_i3bar_command:
1099     TOK_BAR_I3BAR_COMMAND STR
1100     {
1101         DLOG("should add i3bar_command %s\n", $2);
1102         FREE(current_bar.i3bar_command);
1103         current_bar.i3bar_command = $2;
1104     }
1105     ;
1106
1107 bar_output:
1108     TOK_BAR_OUTPUT STR
1109     {
1110         DLOG("bar output %s\n", $2);
1111         int new_outputs = current_bar.num_outputs + 1;
1112         current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
1113         current_bar.outputs[current_bar.num_outputs] = $2;
1114         current_bar.num_outputs = new_outputs;
1115     }
1116     ;
1117
1118 bar_tray_output:
1119     TOK_BAR_TRAY_OUTPUT STR
1120     {
1121         DLOG("tray %s\n", $2);
1122         FREE(current_bar.tray_output);
1123         current_bar.tray_output = $2;
1124     }
1125     ;
1126
1127 bar_position:
1128     TOK_BAR_POSITION bar_position_position
1129     {
1130         DLOG("position %d\n", $2);
1131         current_bar.position = $2;
1132     }
1133     ;
1134
1135 bar_position_position:
1136     TOK_BAR_TOP      { $$ = P_TOP; }
1137     | TOK_BAR_BOTTOM { $$ = P_BOTTOM; }
1138     ;
1139
1140 bar_mode:
1141     TOK_BAR_MODE bar_mode_mode
1142     {
1143         DLOG("mode %d\n", $2);
1144         current_bar.mode = $2;
1145     }
1146     ;
1147
1148 bar_mode_mode:
1149     TOK_BAR_HIDE   { $$ = M_HIDE; }
1150     | TOK_BAR_DOCK { $$ = M_DOCK; }
1151     ;
1152
1153 bar_modifier:
1154     TOK_BAR_MODIFIER bar_modifier_modifier
1155     {
1156         DLOG("modifier %d\n", $2);
1157         current_bar.modifier = $2;
1158     };
1159
1160 bar_modifier_modifier:
1161     TOK_BAR_CONTROL { $$ = M_CONTROL; }
1162     | TOK_BAR_SHIFT { $$ = M_SHIFT; }
1163     | TOK_BAR_MOD1  { $$ = M_MOD1; }
1164     | TOK_BAR_MOD2  { $$ = M_MOD2; }
1165     | TOK_BAR_MOD3  { $$ = M_MOD3; }
1166     | TOK_BAR_MOD4  { $$ = M_MOD4; }
1167     | TOK_BAR_MOD5  { $$ = M_MOD5; }
1168     ;
1169
1170 bar_font:
1171     TOK_BAR_FONT STR
1172     {
1173         DLOG("font %s\n", $2);
1174         FREE(current_bar.font);
1175         current_bar.font = $2;
1176     }
1177     ;
1178
1179 bar_workspace_buttons:
1180     TOK_BAR_WORKSPACE_BUTTONS bool
1181     {
1182         DLOG("workspace_buttons = %d\n", $2);
1183         /* We store this inverted to make the default setting right when
1184          * initializing the struct with zero. */
1185         current_bar.hide_workspace_buttons = !($2);
1186     }
1187     ;
1188
1189 bar_verbose:
1190     TOK_BAR_VERBOSE bool
1191     {
1192         DLOG("verbose = %d\n", $2);
1193         current_bar.verbose = $2;
1194     }
1195     ;
1196
1197 bar_socket_path:
1198     TOK_BAR_SOCKET_PATH STR
1199     {
1200         DLOG("socket_path = %s\n", $2);
1201         FREE(current_bar.socket_path);
1202         current_bar.socket_path = $2;
1203     }
1204     ;
1205
1206 bar_colors:
1207     TOK_BAR_COLORS '{' barlines '}'
1208     {
1209         /* At the moment, the TOK_BAR_COLORS token is only to make the config
1210          * friendlier for humans. We might change this in the future if it gets
1211          * more complex. */
1212     }
1213     ;
1214
1215 bar_color_background:
1216     TOK_BAR_COLOR_BACKGROUND HEXCOLOR
1217     {
1218         DLOG("background = %s\n", $2);
1219         current_bar.colors.background = $2;
1220     }
1221     ;
1222
1223 bar_color_statusline:
1224     TOK_BAR_COLOR_STATUSLINE HEXCOLOR
1225     {
1226         DLOG("statusline = %s\n", $2);
1227         current_bar.colors.statusline = $2;
1228     }
1229     ;
1230
1231 bar_color_focused_workspace:
1232     TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR
1233     {
1234         /* Old syntax: text / background */
1235         DLOG("focused_ws = %s, %s (old)\n", $2, $3);
1236         current_bar.colors.focused_workspace_bg = $3;
1237         current_bar.colors.focused_workspace_text = $2;
1238     }
1239     | TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1240     {
1241         /* New syntax: border / background / text */
1242         DLOG("focused_ws = %s, %s and %s\n", $2, $3, $4);
1243         current_bar.colors.focused_workspace_border = $2;
1244         current_bar.colors.focused_workspace_bg = $3;
1245         current_bar.colors.focused_workspace_text = $4;
1246     }
1247     ;
1248
1249 bar_color_active_workspace:
1250     TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1251     {
1252         /* Old syntax: text / background */
1253         DLOG("active_ws = %s, %s (old)\n", $2, $3);
1254         current_bar.colors.active_workspace_bg = $3;
1255         current_bar.colors.active_workspace_text = $2;
1256     }
1257     | TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1258     {
1259         /* New syntax: border / background / text */
1260         DLOG("active_ws = %s, %s and %s\n", $2, $3, $4);
1261         current_bar.colors.active_workspace_border = $2;
1262         current_bar.colors.active_workspace_bg = $3;
1263         current_bar.colors.active_workspace_text = $4;
1264     }
1265     ;
1266
1267 bar_color_inactive_workspace:
1268     TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1269     {
1270         /* Old syntax: text / background */
1271         DLOG("inactive_ws = %s, %s (old)\n", $2, $3);
1272         current_bar.colors.inactive_workspace_bg = $3;
1273         current_bar.colors.inactive_workspace_text = $2;
1274     }
1275     | TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1276     {
1277         DLOG("inactive_ws = %s, %s and %s\n", $2, $3, $4);
1278         current_bar.colors.inactive_workspace_border = $2;
1279         current_bar.colors.inactive_workspace_bg = $3;
1280         current_bar.colors.inactive_workspace_text = $4;
1281     }
1282     ;
1283
1284 bar_color_urgent_workspace:
1285     TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR
1286     {
1287         /* Old syntax: text / background */
1288         DLOG("urgent_ws = %s, %s (old)\n", $2, $3);
1289         current_bar.colors.urgent_workspace_bg = $3;
1290         current_bar.colors.urgent_workspace_text = $2;
1291     }
1292     | TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR HEXCOLOR
1293     {
1294         DLOG("urgent_ws = %s, %s and %s\n", $2, $3, $4);
1295         current_bar.colors.urgent_workspace_border = $2;
1296         current_bar.colors.urgent_workspace_bg = $3;
1297         current_bar.colors.urgent_workspace_text = $4;
1298     }
1299     ;
1300
1301 floating_maximum_size:
1302     TOKFLOATING_MAXIMUM_SIZE NUMBER WORD NUMBER
1303     {
1304         printf("floating_maximum_width = %d\n", $2);
1305         printf("floating_maximum_height = %d\n", $4);
1306         config.floating_maximum_width = $2;
1307         config.floating_maximum_height = $4;
1308     }
1309     ;
1310
1311 floating_minimum_size:
1312     TOKFLOATING_MINIMUM_SIZE NUMBER WORD NUMBER
1313     {
1314         printf("floating_minimum_width = %d\n", $2);
1315         printf("floating_minimum_height = %d\n", $4);
1316         config.floating_minimum_width = $2;
1317         config.floating_minimum_height = $4;
1318     }
1319     ;
1320
1321 floating_modifier:
1322     TOKFLOATING_MODIFIER binding_modifiers
1323     {
1324         DLOG("floating modifier = %d\n", $2);
1325         config.floating_modifier = $2;
1326     }
1327     ;
1328
1329 orientation:
1330     TOK_ORIENTATION direction
1331     {
1332         DLOG("New containers should start with split direction %d\n", $2);
1333         config.default_orientation = $2;
1334     }
1335     ;
1336
1337 direction:
1338     TOK_HORIZ       { $$ = HORIZ; }
1339     | TOK_VERT      { $$ = VERT; }
1340     | TOK_AUTO      { $$ = NO_ORIENTATION; }
1341     ;
1342
1343 workspace_layout:
1344     TOK_WORKSPACE_LAYOUT layout_mode
1345     {
1346         DLOG("new containers will be in mode %d\n", $2);
1347         config.default_layout = $2;
1348
1349 #if 0
1350         /* We also need to change the layout of the already existing
1351          * workspaces here. Workspaces may exist at this point because
1352          * of the other directives which are modifying workspaces
1353          * (setting the preferred screen or name). While the workspace
1354          * objects are already created, they have never been used.
1355          * Thus, the user very likely awaits the default container mode
1356          * to trigger in this case, regardless of where it is inside
1357          * his configuration file. */
1358         Workspace *ws;
1359         TAILQ_FOREACH(ws, workspaces, workspaces) {
1360                 if (ws->table == NULL)
1361                         continue;
1362                 switch_layout_mode(global_conn,
1363                                    ws->table[0][0],
1364                                    config.container_mode);
1365         }
1366 #endif
1367     }
1368     | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
1369     {
1370         DLOG("stack-limit %d with val %d\n", $3, $4);
1371         config.container_stack_limit = $3;
1372         config.container_stack_limit_value = $4;
1373
1374 #if 0
1375         /* See the comment above */
1376         Workspace *ws;
1377         TAILQ_FOREACH(ws, workspaces, workspaces) {
1378                 if (ws->table == NULL)
1379                         continue;
1380                 Container *con = ws->table[0][0];
1381                 con->stack_limit = config.container_stack_limit;
1382                 con->stack_limit_value = config.container_stack_limit_value;
1383         }
1384 #endif
1385     }
1386     ;
1387
1388 layout_mode:
1389     TOK_DEFAULT       { $$ = L_DEFAULT; }
1390     | TOK_STACKING    { $$ = L_STACKED; }
1391     | TOK_TABBED      { $$ = L_TABBED; }
1392     ;
1393
1394 new_window:
1395     TOKNEWWINDOW border_style
1396     {
1397         DLOG("new windows should start with border style %d\n", $2);
1398         config.default_border = $2;
1399     }
1400     ;
1401
1402 new_float:
1403     TOKNEWFLOAT border_style
1404     {
1405        DLOG("new floating windows should start with border style %d\n", $2);
1406        config.default_floating_border = $2;
1407     }
1408     ;
1409
1410 border_style:
1411     TOK_NORMAL      { $$ = BS_NORMAL; }
1412     | TOK_NONE      { $$ = BS_NONE; }
1413     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
1414     ;
1415
1416 bool:
1417     NUMBER
1418     {
1419         $$ = ($1 == 1);
1420     }
1421     | WORD
1422     {
1423         DLOG("checking word \"%s\"\n", $1);
1424         $$ = (strcasecmp($1, "yes") == 0 ||
1425               strcasecmp($1, "true") == 0 ||
1426               strcasecmp($1, "on") == 0 ||
1427               strcasecmp($1, "enable") == 0 ||
1428               strcasecmp($1, "active") == 0);
1429     }
1430     ;
1431
1432 focus_follows_mouse:
1433     TOKFOCUSFOLLOWSMOUSE bool
1434     {
1435         DLOG("focus follows mouse = %d\n", $2);
1436         config.disable_focus_follows_mouse = !($2);
1437     }
1438     ;
1439
1440 force_focus_wrapping:
1441     TOK_FORCE_FOCUS_WRAPPING bool
1442     {
1443         DLOG("force focus wrapping = %d\n", $2);
1444         config.force_focus_wrapping = $2;
1445     }
1446     ;
1447
1448 force_xinerama:
1449     TOK_FORCE_XINERAMA bool
1450     {
1451         DLOG("force xinerama = %d\n", $2);
1452         config.force_xinerama = $2;
1453     }
1454     ;
1455
1456 fake_outputs:
1457     TOK_FAKE_OUTPUTS STR
1458     {
1459         DLOG("fake outputs = %s\n", $2);
1460         config.fake_outputs = $2;
1461     }
1462     ;
1463
1464 workspace_back_and_forth:
1465     TOK_WORKSPACE_AUTO_BAF bool
1466     {
1467         DLOG("automatic workspace back-and-forth = %d\n", $2);
1468         config.workspace_auto_back_and_forth = $2;
1469     }
1470     ;
1471
1472 workspace_bar:
1473     TOKWORKSPACEBAR bool
1474     {
1475         DLOG("workspace bar = %d\n", $2);
1476         config.disable_workspace_bar = !($2);
1477     }
1478     ;
1479
1480 workspace:
1481     TOKWORKSPACE qstring_or_number TOKOUTPUT OUTPUT optional_workspace_name
1482     {
1483         char *ws_name = $2;
1484
1485         if ($5 != NULL) {
1486             ELOG("The old (v3) syntax workspace <number> output <output> <name> is deprecated.\n");
1487             ELOG("Please use the new syntax: workspace \"<workspace>\" output <output>\n");
1488             ELOG("In your case, the following should work:\n");
1489             ELOG("    workspace \"%s\" output %s\n", $5, $4);
1490             ws_name = $5;
1491             context->has_warnings = true;
1492         }
1493
1494         DLOG("Assigning workspace \"%s\" to output \"%s\"\n", ws_name, $4);
1495         /* Check for earlier assignments of the same workspace so that we
1496          * don’t have assignments of a single workspace to different
1497          * outputs */
1498         struct Workspace_Assignment *assignment;
1499         bool duplicate = false;
1500         TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1501             if (strcasecmp(assignment->name, ws_name) == 0) {
1502                 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
1503                      ws_name);
1504                 assignment->output = $4;
1505                 duplicate = true;
1506             }
1507         }
1508         if (!duplicate) {
1509             assignment = scalloc(sizeof(struct Workspace_Assignment));
1510             assignment->name = ws_name;
1511             assignment->output = $4;
1512             TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
1513         }
1514     }
1515     | TOKWORKSPACE NUMBER workspace_name
1516     {
1517         int ws_num = $2;
1518         if (ws_num < 1) {
1519             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1520         } else {
1521             DLOG("workspace name to: %s\n", $3);
1522 #if 0
1523             if ($<string>3 != NULL) {
1524                     workspace_set_name(workspace_get(ws_num - 1), $<string>3);
1525                     free($<string>3);
1526             }
1527 #endif
1528         }
1529     }
1530     ;
1531
1532 optional_workspace_name:
1533     /* empty */          { $$ = NULL; }
1534     | workspace_name     { $$ = $1; }
1535     ;
1536
1537 workspace_name:
1538     QUOTEDSTRING         { $$ = $1; }
1539     | STR                { $$ = $1; }
1540     | WORD               { $$ = $1; }
1541     ;
1542
1543 assign:
1544     TOKASSIGN window_class STR
1545     {
1546         /* This is the old, deprecated form of assignments. It’s provided for
1547          * compatibility in version (4.1, 4.2, 4.3) and will be removed
1548          * afterwards. It triggers an i3-nagbar warning starting from 4.1. */
1549         ELOG("You are using the old assign syntax (without criteria). "
1550              "Please see the User's Guide for the new syntax and fix "
1551              "your config file.\n");
1552         context->has_warnings = true;
1553         printf("assignment of %s to *%s*\n", $2, $3);
1554         char *workspace = $3;
1555         char *criteria = $2;
1556
1557         Assignment *assignment = scalloc(sizeof(Assignment));
1558         Match *match = &(assignment->match);
1559         match_init(match);
1560
1561         char *separator = NULL;
1562         if ((separator = strchr(criteria, '/')) != NULL) {
1563             *(separator++) = '\0';
1564             char *pattern;
1565             sasprintf(&pattern, "(?i)%s", separator);
1566             match->title = regex_new(pattern);
1567             free(pattern);
1568             printf("  title = %s\n", separator);
1569         }
1570         if (*criteria != '\0') {
1571             char *pattern;
1572             sasprintf(&pattern, "(?i)%s", criteria);
1573             match->class = regex_new(pattern);
1574             free(pattern);
1575             printf("  class = %s\n", criteria);
1576         }
1577         free(criteria);
1578
1579         /* Compatibility with older versions: If the assignment target starts
1580          * with ~, we create the equivalent of:
1581          *
1582          * for_window [class="foo"] floating enable
1583          */
1584         if (*workspace == '~') {
1585             workspace++;
1586             if (*workspace == '\0') {
1587                 /* This assignment was *only* for floating */
1588                 assignment->type = A_COMMAND;
1589                 assignment->dest.command = sstrdup("floating enable");
1590                 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1591                 break;
1592             } else {
1593                 /* Create a new assignment and continue afterwards */
1594                 Assignment *floating = scalloc(sizeof(Assignment));
1595                 match_copy(&(floating->match), match);
1596                 floating->type = A_COMMAND;
1597                 floating->dest.command = sstrdup("floating enable");
1598                 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1599             }
1600         }
1601
1602         assignment->type = A_TO_WORKSPACE;
1603         assignment->dest.workspace = workspace;
1604         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1605     }
1606     | TOKASSIGN match STR
1607     {
1608         if (match_is_empty(&current_match)) {
1609             ELOG("Match is empty, ignoring this assignment\n");
1610             break;
1611         }
1612         printf("new assignment, using above criteria, to workspace %s\n", $3);
1613         Assignment *assignment = scalloc(sizeof(Assignment));
1614         assignment->match = current_match;
1615         assignment->type = A_TO_WORKSPACE;
1616         assignment->dest.workspace = $3;
1617         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1618     }
1619     ;
1620
1621 window_class:
1622     QUOTEDSTRING
1623     | STR_NG
1624     ;
1625
1626 ipcsocket:
1627     TOKIPCSOCKET STR
1628     {
1629         config.ipc_socket_path = $2;
1630     }
1631     ;
1632
1633 restart_state:
1634     TOKRESTARTSTATE STR
1635     {
1636         config.restart_state_path = $2;
1637     }
1638     ;
1639
1640 exec:
1641     TOKEXEC optional_no_startup_id STR
1642     {
1643         struct Autostart *new = smalloc(sizeof(struct Autostart));
1644         new->command = $3;
1645         new->no_startup_id = $2;
1646         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1647     }
1648     ;
1649
1650 exec_always:
1651     TOKEXEC_ALWAYS optional_no_startup_id STR
1652     {
1653         struct Autostart *new = smalloc(sizeof(struct Autostart));
1654         new->command = $3;
1655         new->no_startup_id = $2;
1656         TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1657     }
1658     ;
1659
1660 optional_no_startup_id:
1661     /* empty */ { $$ = false; }
1662     | TOK_NO_STARTUP_ID  { $$ = true; }
1663     ;
1664
1665 terminal:
1666     TOKTERMINAL STR
1667     {
1668         ELOG("The terminal option is DEPRECATED and has no effect. "
1669             "Please remove it from your configuration file.\n");
1670     }
1671     ;
1672
1673 font:
1674     TOKFONT STR
1675     {
1676         config.font = load_font($2, true);
1677         set_font(&config.font);
1678         printf("font %s\n", $2);
1679         FREE(font_pattern);
1680         font_pattern = $2;
1681     }
1682     ;
1683
1684 single_color:
1685     TOKSINGLECOLOR colorpixel
1686     {
1687         uint32_t *dest = $1;
1688         *dest = $2;
1689     }
1690     ;
1691
1692 color:
1693     TOKCOLOR colorpixel colorpixel colorpixel
1694     {
1695         struct Colortriple *dest = $1;
1696
1697         dest->border = $2;
1698         dest->background = $3;
1699         dest->text = $4;
1700     }
1701     | TOKCOLOR colorpixel colorpixel colorpixel colorpixel
1702     {
1703         struct Colortriple *dest = $1;
1704
1705         dest->border = $2;
1706         dest->background = $3;
1707         dest->text = $4;
1708         dest->indicator = $5;
1709     }
1710     ;
1711
1712 colorpixel:
1713     HEXCOLOR
1714     {
1715         $$ = get_colorpixel($1);
1716         free($1);
1717     }
1718     ;
1719
1720
1721 binding_modifiers:
1722     /* NULL */                               { $$ = 0; }
1723     | binding_modifier
1724     | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1725     | binding_modifiers '+'                  { $$ = $1; }
1726     ;
1727
1728 binding_modifier:
1729     MODIFIER        { $$ = $1; }
1730     | TOKCONTROL    { $$ = BIND_CONTROL; }
1731     | TOKSHIFT      { $$ = BIND_SHIFT; }
1732     ;
1733
1734 popup_during_fullscreen:
1735     TOK_POPUP_DURING_FULLSCREEN popup_setting
1736     {
1737         DLOG("popup_during_fullscreen setting: %d\n", $2);
1738         config.popup_during_fullscreen = $2;
1739     }
1740     ;
1741
1742 popup_setting:
1743     TOK_IGNORE              { $$ = PDF_IGNORE; }
1744     | TOK_LEAVE_FULLSCREEN  { $$ = PDF_LEAVE_FULLSCREEN; }
1745     ;