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