]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.y
bar config: fall back to the i3 font (Thanks pl)
[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_FONT                "font (bar)"
709 %token                  TOK_BAR_WORKSPACE_BUTTONS   "workspace_buttons"
710 %token                  TOK_BAR_VERBOSE             "verbose"
711 %token                  TOK_BAR_COLORS              "colors"
712 %token                  TOK_BAR_COLOR_BACKGROUND    "background"
713 %token                  TOK_BAR_COLOR_STATUSLINE    "statusline"
714 %token                  TOK_BAR_COLOR_FOCUSED_WORKSPACE "focused_workspace"
715 %token                  TOK_BAR_COLOR_ACTIVE_WORKSPACE "active_workspace"
716 %token                  TOK_BAR_COLOR_INACTIVE_WORKSPACE "inactive_workspace"
717 %token                  TOK_BAR_COLOR_URGENT_WORKSPACE "urgent_workspace"
718
719 %token              TOK_MARK            "mark"
720 %token              TOK_CLASS           "class"
721 %token              TOK_INSTANCE        "instance"
722 %token              TOK_WINDOW_ROLE     "window_role"
723 %token              TOK_ID              "id"
724 %token              TOK_CON_ID          "con_id"
725 %token              TOK_TITLE           "title"
726
727 %type   <binding>       binding
728 %type   <binding>       bindcode
729 %type   <binding>       bindsym
730 %type   <number>        binding_modifiers
731 %type   <number>        binding_modifier
732 %type   <number>        direction
733 %type   <number>        layout_mode
734 %type   <number>        border_style
735 %type   <number>        new_window
736 %type   <number>        new_float
737 %type   <number>        colorpixel
738 %type   <number>        bool
739 %type   <number>        popup_setting
740 %type   <number>        bar_position_position
741 %type   <number>        bar_mode_mode
742 %type   <string>        command
743 %type   <string>        word_or_number
744 %type   <string>        optional_workspace_name
745 %type   <string>        workspace_name
746 %type   <string>        window_class
747
748 %%
749
750 lines: /* empty */
751     | lines error
752     | lines line
753     ;
754
755 line:
756     bindline
757     | for_window
758     | mode
759     | bar
760     | floating_modifier
761     | orientation
762     | workspace_layout
763     | new_window
764     | new_float
765     | focus_follows_mouse
766     | force_focus_wrapping
767     | force_xinerama
768     | workspace_back_and_forth
769     | workspace_bar
770     | workspace
771     | assign
772     | ipcsocket
773     | restart_state
774     | exec
775     | exec_always
776     | single_color
777     | color
778     | terminal
779     | font
780     | comment
781     | popup_during_fullscreen
782     ;
783
784 comment:
785     TOKCOMMENT
786     ;
787
788 command:
789     STR
790     ;
791
792 bindline:
793     binding
794     {
795         TAILQ_INSERT_TAIL(bindings, $1, bindings);
796     }
797     ;
798
799 binding:
800     TOKBINDCODE bindcode         { $$ = $2; }
801     | TOKBINDSYM bindsym         { $$ = $2; }
802     ;
803
804 bindcode:
805     binding_modifiers NUMBER command
806     {
807         printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
808         Binding *new = scalloc(sizeof(Binding));
809
810         new->keycode = $2;
811         new->mods = $1;
812         new->command = $3;
813
814         $$ = new;
815     }
816     ;
817
818 bindsym:
819     binding_modifiers word_or_number command
820     {
821         printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
822         Binding *new = scalloc(sizeof(Binding));
823
824         new->symbol = $2;
825         new->mods = $1;
826         new->command = $3;
827
828         $$ = new;
829     }
830     ;
831
832 for_window:
833     TOK_FOR_WINDOW match command
834     {
835         if (match_is_empty(&current_match)) {
836             ELOG("Match is empty, ignoring this for_window statement\n");
837             break;
838         }
839         printf("\t should execute command %s for the criteria mentioned above\n", $3);
840         Assignment *assignment = scalloc(sizeof(Assignment));
841         assignment->type = A_COMMAND;
842         assignment->match = current_match;
843         assignment->dest.command = $3;
844         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
845     }
846     ;
847
848 match:
849     | matchstart criteria matchend
850     {
851         printf("match parsed\n");
852     }
853     ;
854
855 matchstart:
856     '['
857     {
858         printf("start\n");
859         match_init(&current_match);
860     }
861     ;
862
863 matchend:
864     ']'
865     {
866         printf("match specification finished\n");
867     }
868     ;
869
870 criteria:
871     criteria criterion
872     | criterion
873     ;
874
875 criterion:
876     TOK_CLASS '=' STR
877     {
878         printf("criteria: class = %s\n", $3);
879         current_match.class = regex_new($3);
880         free($3);
881     }
882     | TOK_INSTANCE '=' STR
883     {
884         printf("criteria: instance = %s\n", $3);
885         current_match.instance = regex_new($3);
886         free($3);
887     }
888     | TOK_WINDOW_ROLE '=' STR
889     {
890         printf("criteria: window_role = %s\n", $3);
891         current_match.role = regex_new($3);
892         free($3);
893     }
894     | TOK_CON_ID '=' STR
895     {
896         printf("criteria: id = %s\n", $3);
897         char *end;
898         long parsed = strtol($3, &end, 10);
899         if (parsed == LONG_MIN ||
900             parsed == LONG_MAX ||
901             parsed < 0 ||
902             (end && *end != '\0')) {
903             ELOG("Could not parse con id \"%s\"\n", $3);
904         } else {
905             current_match.con_id = (Con*)parsed;
906             printf("id as int = %p\n", current_match.con_id);
907         }
908     }
909     | TOK_ID '=' STR
910     {
911         printf("criteria: window id = %s\n", $3);
912         char *end;
913         long parsed = strtol($3, &end, 10);
914         if (parsed == LONG_MIN ||
915             parsed == LONG_MAX ||
916             parsed < 0 ||
917             (end && *end != '\0')) {
918             ELOG("Could not parse window id \"%s\"\n", $3);
919         } else {
920             current_match.id = parsed;
921             printf("window id as int = %d\n", current_match.id);
922         }
923     }
924     | TOK_MARK '=' STR
925     {
926         printf("criteria: mark = %s\n", $3);
927         current_match.mark = regex_new($3);
928         free($3);
929     }
930     | TOK_TITLE '=' STR
931     {
932         printf("criteria: title = %s\n", $3);
933         current_match.title = regex_new($3);
934         free($3);
935     }
936     ;
937
938
939
940 word_or_number:
941     WORD
942     | NUMBER
943     {
944         sasprintf(&$$, "%d", $1);
945     }
946     ;
947
948 mode:
949     TOKMODE QUOTEDSTRING '{' modelines '}'
950     {
951         if (strcasecmp($2, "default") == 0) {
952             printf("You cannot use the name \"default\" for your mode\n");
953             exit(1);
954         }
955         printf("\t now in mode %s\n", $2);
956         printf("\t current bindings = %p\n", current_bindings);
957         Binding *binding;
958         TAILQ_FOREACH(binding, current_bindings, bindings) {
959             printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
960                             binding->mods, binding->keycode, binding->symbol, binding->command);
961         }
962
963         struct Mode *mode = scalloc(sizeof(struct Mode));
964         mode->name = $2;
965         mode->bindings = current_bindings;
966         current_bindings = NULL;
967         SLIST_INSERT_HEAD(&modes, mode, modes);
968     }
969     ;
970
971
972 modelines:
973     /* empty */
974     | modelines modeline
975     ;
976
977 modeline:
978     comment
979     | binding
980     {
981         if (current_bindings == NULL) {
982             current_bindings = scalloc(sizeof(struct bindings_head));
983             TAILQ_INIT(current_bindings);
984         }
985
986         TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
987     }
988     ;
989
990 bar:
991     TOK_BAR '{' barlines '}'
992     {
993         printf("\t new bar configuration finished, saving.\n");
994         /* Generate a unique ID for this bar */
995         current_bar.id = sstrdup("bar-XXXXXX");
996         /* This works similar to mktemp in that it replaces the last six X with
997          * random letters, but without the restriction that the given buffer
998          * has to contain a valid path name. */
999         char *x = current_bar.id + strlen("bar-");
1000         while (*x != '\0') {
1001             *(x++) = (rand() % 26) + 'a';
1002         }
1003
1004         /* If no font was explicitly set, we use the i3 font as default */
1005         if (!current_bar.font)
1006             current_bar.font = sstrdup(font_pattern);
1007
1008         /* Copy the current (static) structure into a dynamically allocated
1009          * one, then cleanup our static one. */
1010         Barconfig *bar_config = scalloc(sizeof(Barconfig));
1011         memcpy(bar_config, &current_bar, sizeof(Barconfig));
1012         TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
1013
1014         memset(&current_bar, '\0', sizeof(Barconfig));
1015     }
1016     ;
1017
1018 barlines:
1019     /* empty */
1020     | barlines barline
1021     ;
1022
1023 barline:
1024     comment
1025     | bar_status_command
1026     | bar_output
1027     | bar_tray_output
1028     | bar_position
1029     | bar_mode
1030     | bar_font
1031     | bar_workspace_buttons
1032     | bar_verbose
1033     | bar_socket_path
1034     | bar_colors
1035     | bar_color_background
1036     | bar_color_statusline
1037     | bar_color_focused_workspace
1038     | bar_color_active_workspace
1039     | bar_color_inactive_workspace
1040     | bar_color_urgent_workspace
1041     ;
1042
1043 bar_status_command:
1044     TOK_BAR_STATUS_COMMAND STR
1045     {
1046         DLOG("should add status command %s\n", $2);
1047         FREE(current_bar.status_command);
1048         current_bar.status_command = $2;
1049     }
1050     ;
1051
1052 bar_output:
1053     TOK_BAR_OUTPUT STR
1054     {
1055         DLOG("bar output %s\n", $2);
1056         int new_outputs = current_bar.num_outputs + 1;
1057         current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
1058         current_bar.outputs[current_bar.num_outputs] = $2;
1059         current_bar.num_outputs = new_outputs;
1060     }
1061     ;
1062
1063 bar_tray_output:
1064     TOK_BAR_TRAY_OUTPUT STR
1065     {
1066         DLOG("tray %s\n", $2);
1067         FREE(current_bar.tray_output);
1068         current_bar.tray_output = $2;
1069     }
1070     ;
1071
1072 bar_position:
1073     TOK_BAR_POSITION bar_position_position
1074     {
1075         DLOG("position %d\n", $2);
1076         current_bar.position = $2;
1077     }
1078     ;
1079
1080 bar_position_position:
1081     TOK_BAR_TOP      { $$ = P_TOP; }
1082     | TOK_BAR_BOTTOM { $$ = P_BOTTOM; }
1083     ;
1084
1085 bar_mode:
1086     TOK_BAR_MODE bar_mode_mode
1087     {
1088         DLOG("mode %d\n", $2);
1089         current_bar.mode = $2;
1090     }
1091     ;
1092
1093 bar_mode_mode:
1094     TOK_BAR_HIDE   { $$ = M_HIDE; }
1095     | TOK_BAR_DOCK { $$ = M_DOCK; }
1096     ;
1097
1098 bar_font:
1099     TOK_BAR_FONT STR
1100     {
1101         DLOG("font %s\n", $2);
1102         FREE(current_bar.font);
1103         current_bar.font = $2;
1104     }
1105     ;
1106
1107 bar_workspace_buttons:
1108     TOK_BAR_WORKSPACE_BUTTONS bool
1109     {
1110         DLOG("workspace_buttons = %d\n", $2);
1111         /* We store this inverted to make the default setting right when
1112          * initializing the struct with zero. */
1113         current_bar.hide_workspace_buttons = !($2);
1114     }
1115     ;
1116
1117 bar_verbose:
1118     TOK_BAR_VERBOSE bool
1119     {
1120         DLOG("verbose = %d\n", $2);
1121         current_bar.verbose = $2;
1122     }
1123     ;
1124
1125 bar_socket_path:
1126     TOK_BAR_SOCKET_PATH STR
1127     {
1128         DLOG("socket_path = %s\n", $2);
1129         FREE(current_bar.socket_path);
1130         current_bar.socket_path = $2;
1131     }
1132     ;
1133
1134 bar_colors:
1135     TOK_BAR_COLORS '{' barlines '}'
1136     {
1137         /* At the moment, the TOK_BAR_COLORS token is only to make the config
1138          * friendlier for humans. We might change this in the future if it gets
1139          * more complex. */
1140     }
1141     ;
1142
1143 bar_color_background:
1144     TOK_BAR_COLOR_BACKGROUND HEXCOLOR
1145     {
1146         DLOG("background = %s\n", $2);
1147         current_bar.colors.background = $2;
1148     }
1149     ;
1150
1151 bar_color_statusline:
1152     TOK_BAR_COLOR_STATUSLINE HEXCOLOR
1153     {
1154         DLOG("statusline = %s\n", $2);
1155         current_bar.colors.statusline = $2;
1156     }
1157     ;
1158
1159 bar_color_focused_workspace:
1160     TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR
1161     {
1162         DLOG("focused_ws = %s and %s\n", $2, $3);
1163         current_bar.colors.focused_workspace_text = $2;
1164         current_bar.colors.focused_workspace_bg = $3;
1165     }
1166     ;
1167
1168 bar_color_active_workspace:
1169     TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1170     {
1171         DLOG("active_ws = %s and %s\n", $2, $3);
1172         current_bar.colors.active_workspace_text = $2;
1173         current_bar.colors.active_workspace_bg = $3;
1174     }
1175     ;
1176
1177 bar_color_inactive_workspace:
1178     TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1179     {
1180         DLOG("inactive_ws = %s and %s\n", $2, $3);
1181         current_bar.colors.inactive_workspace_text = $2;
1182         current_bar.colors.inactive_workspace_bg = $3;
1183     }
1184     ;
1185
1186 bar_color_urgent_workspace:
1187     TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR
1188     {
1189         DLOG("urgent_ws = %s and %s\n", $2, $3);
1190         current_bar.colors.urgent_workspace_text = $2;
1191         current_bar.colors.urgent_workspace_bg = $3;
1192     }
1193     ;
1194
1195 floating_modifier:
1196     TOKFLOATING_MODIFIER binding_modifiers
1197     {
1198         DLOG("floating modifier = %d\n", $2);
1199         config.floating_modifier = $2;
1200     }
1201     ;
1202
1203 orientation:
1204     TOK_ORIENTATION direction
1205     {
1206         DLOG("New containers should start with split direction %d\n", $2);
1207         config.default_orientation = $2;
1208     }
1209     ;
1210
1211 direction:
1212     TOK_HORIZ       { $$ = HORIZ; }
1213     | TOK_VERT      { $$ = VERT; }
1214     | TOK_AUTO      { $$ = NO_ORIENTATION; }
1215     ;
1216
1217 workspace_layout:
1218     TOK_WORKSPACE_LAYOUT layout_mode
1219     {
1220         DLOG("new containers will be in mode %d\n", $2);
1221         config.default_layout = $2;
1222
1223 #if 0
1224         /* We also need to change the layout of the already existing
1225          * workspaces here. Workspaces may exist at this point because
1226          * of the other directives which are modifying workspaces
1227          * (setting the preferred screen or name). While the workspace
1228          * objects are already created, they have never been used.
1229          * Thus, the user very likely awaits the default container mode
1230          * to trigger in this case, regardless of where it is inside
1231          * his configuration file. */
1232         Workspace *ws;
1233         TAILQ_FOREACH(ws, workspaces, workspaces) {
1234                 if (ws->table == NULL)
1235                         continue;
1236                 switch_layout_mode(global_conn,
1237                                    ws->table[0][0],
1238                                    config.container_mode);
1239         }
1240 #endif
1241     }
1242     | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
1243     {
1244         DLOG("stack-limit %d with val %d\n", $3, $4);
1245         config.container_stack_limit = $3;
1246         config.container_stack_limit_value = $4;
1247
1248 #if 0
1249         /* See the comment above */
1250         Workspace *ws;
1251         TAILQ_FOREACH(ws, workspaces, workspaces) {
1252                 if (ws->table == NULL)
1253                         continue;
1254                 Container *con = ws->table[0][0];
1255                 con->stack_limit = config.container_stack_limit;
1256                 con->stack_limit_value = config.container_stack_limit_value;
1257         }
1258 #endif
1259     }
1260     ;
1261
1262 layout_mode:
1263     TOK_DEFAULT       { $$ = L_DEFAULT; }
1264     | TOK_STACKING    { $$ = L_STACKED; }
1265     | TOK_TABBED      { $$ = L_TABBED; }
1266     ;
1267
1268 new_window:
1269     TOKNEWWINDOW border_style
1270     {
1271         DLOG("new windows should start with border style %d\n", $2);
1272         config.default_border = $2;
1273     }
1274     ;
1275
1276 new_float:
1277     TOKNEWFLOAT border_style
1278     {
1279        DLOG("new floating windows should start with border style %d\n", $2);
1280        config.default_floating_border = $2;
1281     }
1282     ;
1283
1284 border_style:
1285     TOK_NORMAL      { $$ = BS_NORMAL; }
1286     | TOK_NONE      { $$ = BS_NONE; }
1287     | TOK_1PIXEL    { $$ = BS_1PIXEL; }
1288     ;
1289
1290 bool:
1291     NUMBER
1292     {
1293         $$ = ($1 == 1);
1294     }
1295     | WORD
1296     {
1297         DLOG("checking word \"%s\"\n", $1);
1298         $$ = (strcasecmp($1, "yes") == 0 ||
1299               strcasecmp($1, "true") == 0 ||
1300               strcasecmp($1, "on") == 0 ||
1301               strcasecmp($1, "enable") == 0 ||
1302               strcasecmp($1, "active") == 0);
1303     }
1304     ;
1305
1306 focus_follows_mouse:
1307     TOKFOCUSFOLLOWSMOUSE bool
1308     {
1309         DLOG("focus follows mouse = %d\n", $2);
1310         config.disable_focus_follows_mouse = !($2);
1311     }
1312     ;
1313
1314 force_focus_wrapping:
1315     TOK_FORCE_FOCUS_WRAPPING bool
1316     {
1317         DLOG("force focus wrapping = %d\n", $2);
1318         config.force_focus_wrapping = $2;
1319     }
1320     ;
1321
1322 force_xinerama:
1323     TOK_FORCE_XINERAMA bool
1324     {
1325         DLOG("force xinerama = %d\n", $2);
1326         config.force_xinerama = $2;
1327     }
1328     ;
1329
1330 workspace_back_and_forth:
1331     TOK_WORKSPACE_AUTO_BAF bool
1332     {
1333         DLOG("automatic workspace back-and-forth = %d\n", $2);
1334         config.workspace_auto_back_and_forth = $2;
1335     }
1336     ;
1337
1338 workspace_bar:
1339     TOKWORKSPACEBAR bool
1340     {
1341         DLOG("workspace bar = %d\n", $2);
1342         config.disable_workspace_bar = !($2);
1343     }
1344     ;
1345
1346 workspace:
1347     TOKWORKSPACE NUMBER TOKOUTPUT OUTPUT optional_workspace_name
1348     {
1349         int ws_num = $2;
1350         if (ws_num < 1) {
1351             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1352         } else {
1353             char *ws_name = NULL;
1354             if ($5 == NULL) {
1355                 sasprintf(&ws_name, "%d", ws_num);
1356             } else {
1357                 ws_name = $5;
1358             }
1359
1360             DLOG("Should assign workspace %s to output %s\n", ws_name, $4);
1361             /* Check for earlier assignments of the same workspace so that we
1362              * don’t have assignments of a single workspace to different
1363              * outputs */
1364             struct Workspace_Assignment *assignment;
1365             bool duplicate = false;
1366             TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1367                 if (strcasecmp(assignment->name, ws_name) == 0) {
1368                     ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
1369                          ws_name);
1370                     assignment->output = $4;
1371                     duplicate = true;
1372                 }
1373             }
1374             if (!duplicate) {
1375                 assignment = scalloc(sizeof(struct Workspace_Assignment));
1376                 assignment->name = ws_name;
1377                 assignment->output = $4;
1378                 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
1379             }
1380         }
1381     }
1382     | TOKWORKSPACE NUMBER workspace_name
1383     {
1384         int ws_num = $2;
1385         if (ws_num < 1) {
1386             DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1387         } else {
1388             DLOG("workspace name to: %s\n", $3);
1389 #if 0
1390             if ($<string>3 != NULL) {
1391                     workspace_set_name(workspace_get(ws_num - 1), $<string>3);
1392                     free($<string>3);
1393             }
1394 #endif
1395         }
1396     }
1397     ;
1398
1399 optional_workspace_name:
1400     /* empty */          { $$ = NULL; }
1401     | workspace_name     { $$ = $1; }
1402     ;
1403
1404 workspace_name:
1405     QUOTEDSTRING         { $$ = $1; }
1406     | STR                { $$ = $1; }
1407     | WORD               { $$ = $1; }
1408     ;
1409
1410 assign:
1411     TOKASSIGN window_class STR
1412     {
1413         /* This is the old, deprecated form of assignments. It’s provided for
1414          * compatibility in version (4.1, 4.2, 4.3) and will be removed
1415          * afterwards. It triggers an i3-nagbar warning starting from 4.1. */
1416         ELOG("You are using the old assign syntax (without criteria). "
1417              "Please see the User's Guide for the new syntax and fix "
1418              "your config file.\n");
1419         context->has_warnings = true;
1420         printf("assignment of %s to *%s*\n", $2, $3);
1421         char *workspace = $3;
1422         char *criteria = $2;
1423
1424         Assignment *assignment = scalloc(sizeof(Assignment));
1425         Match *match = &(assignment->match);
1426         match_init(match);
1427
1428         char *separator = NULL;
1429         if ((separator = strchr(criteria, '/')) != NULL) {
1430             *(separator++) = '\0';
1431             char *pattern;
1432             sasprintf(&pattern, "(?i)%s", separator);
1433             match->title = regex_new(pattern);
1434             free(pattern);
1435             printf("  title = %s\n", separator);
1436         }
1437         if (*criteria != '\0') {
1438             char *pattern;
1439             sasprintf(&pattern, "(?i)%s", criteria);
1440             match->class = regex_new(pattern);
1441             free(pattern);
1442             printf("  class = %s\n", criteria);
1443         }
1444         free(criteria);
1445
1446         /* Compatibility with older versions: If the assignment target starts
1447          * with ~, we create the equivalent of:
1448          *
1449          * for_window [class="foo"] floating enable
1450          */
1451         if (*workspace == '~') {
1452             workspace++;
1453             if (*workspace == '\0') {
1454                 /* This assignment was *only* for floating */
1455                 assignment->type = A_COMMAND;
1456                 assignment->dest.command = sstrdup("floating enable");
1457                 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1458                 break;
1459             } else {
1460                 /* Create a new assignment and continue afterwards */
1461                 Assignment *floating = scalloc(sizeof(Assignment));
1462                 match_copy(&(floating->match), match);
1463                 floating->type = A_COMMAND;
1464                 floating->dest.command = sstrdup("floating enable");
1465                 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1466             }
1467         }
1468
1469         assignment->type = A_TO_WORKSPACE;
1470         assignment->dest.workspace = workspace;
1471         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1472     }
1473     | TOKASSIGN match STR
1474     {
1475         if (match_is_empty(&current_match)) {
1476             ELOG("Match is empty, ignoring this assignment\n");
1477             break;
1478         }
1479         printf("new assignment, using above criteria, to workspace %s\n", $3);
1480         Assignment *assignment = scalloc(sizeof(Assignment));
1481         assignment->match = current_match;
1482         assignment->type = A_TO_WORKSPACE;
1483         assignment->dest.workspace = $3;
1484         TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1485     }
1486     ;
1487
1488 window_class:
1489     QUOTEDSTRING
1490     | STR_NG
1491     ;
1492
1493 ipcsocket:
1494     TOKIPCSOCKET STR
1495     {
1496         config.ipc_socket_path = $2;
1497     }
1498     ;
1499
1500 restart_state:
1501     TOKRESTARTSTATE STR
1502     {
1503         config.restart_state_path = $2;
1504     }
1505     ;
1506
1507 exec:
1508     TOKEXEC STR
1509     {
1510         struct Autostart *new = smalloc(sizeof(struct Autostart));
1511         new->command = $2;
1512         TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1513     }
1514     ;
1515
1516 exec_always:
1517     TOKEXEC_ALWAYS STR
1518     {
1519         struct Autostart *new = smalloc(sizeof(struct Autostart));
1520         new->command = $2;
1521         TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1522     }
1523     ;
1524
1525 terminal:
1526     TOKTERMINAL STR
1527     {
1528         ELOG("The terminal option is DEPRECATED and has no effect. "
1529             "Please remove it from your configuration file.\n");
1530     }
1531     ;
1532
1533 font:
1534     TOKFONT STR
1535     {
1536         config.font = load_font($2, true);
1537         printf("font %s\n", $2);
1538         FREE(font_pattern);
1539         font_pattern = $2;
1540     }
1541     ;
1542
1543 single_color:
1544     TOKSINGLECOLOR colorpixel
1545     {
1546         uint32_t *dest = $1;
1547         *dest = $2;
1548     }
1549     ;
1550
1551 color:
1552     TOKCOLOR colorpixel colorpixel colorpixel
1553     {
1554         struct Colortriple *dest = $1;
1555
1556         dest->border = $2;
1557         dest->background = $3;
1558         dest->text = $4;
1559     }
1560     ;
1561
1562 colorpixel:
1563     HEXCOLOR
1564     {
1565         $$ = get_colorpixel($1);
1566         free($1);
1567     }
1568     ;
1569
1570
1571 binding_modifiers:
1572     /* NULL */                               { $$ = 0; }
1573     | binding_modifier
1574     | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1575     | binding_modifiers '+'                  { $$ = $1; }
1576     ;
1577
1578 binding_modifier:
1579     MODIFIER        { $$ = $1; }
1580     | TOKCONTROL    { $$ = BIND_CONTROL; }
1581     | TOKSHIFT      { $$ = BIND_SHIFT; }
1582     ;
1583
1584 popup_during_fullscreen:
1585     TOK_POPUP_DURING_FULLSCREEN popup_setting
1586     {
1587         DLOG("popup_during_fullscreen setting: %d\n", $2);
1588         config.popup_during_fullscreen = $2;
1589     }
1590     ;
1591
1592 popup_setting:
1593     TOK_IGNORE              { $$ = PDF_IGNORE; }
1594     | TOK_LEAVE_FULLSCREEN  { $$ = PDF_LEAVE_FULLSCREEN; }
1595     ;