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