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