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