3 * vim:ts=4:sw=4:expandtab
14 static pid_t configerror_pid = -1;
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;
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);
28 YY_BUFFER_STATE yy_scan_string(const char *);
30 static struct bindings_head *current_bindings;
31 static struct context *context;
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. */
38 void yyerror(const char *error_message) {
39 context->has_errors = true;
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);
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.
64 static int detect_version(char *buf) {
67 while (*walk != '\0') {
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);
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, ' ');
87 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
91 if ((bind = strchr(bind, ' ')) == NULL)
93 while ((*bind == ' ' || *bind == '\t') && *bind != '\0')
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 strncasecmp(bind, "--no-startup-id", strlen("--no-startup-id")) == 0 ||
108 strncasecmp(bind, "bar", strlen("bar")) == 0) {
109 printf("deciding for version 4 due to this line: %.*s\n", (int)(walk-line), line);
115 /* advance to the next line */
124 * Calls i3-migrate-config-to-v4 to migrate a configuration file (input
127 * Returns the converted config file or NULL if there was an error (for
128 * example the script could not be found in $PATH or the i3 executable’s
132 static char *migrate_config(char *input, off_t size) {
136 if (pipe(writepipe) != 0 ||
137 pipe(readpipe) != 0) {
138 warn("migrate_config: Could not create pipes");
144 warn("Could not fork()");
150 /* close writing end of writepipe, connect reading side to stdin */
152 dup2(writepipe[0], 0);
154 /* close reading end of readpipe, connect writing side to stdout */
156 dup2(readpipe[1], 1);
158 static char *argv[] = {
159 NULL, /* will be replaced by the executable path */
162 exec_i3_utility("i3-migrate-config-to-v4", argv);
167 /* close reading end of the writepipe (connected to the script’s stdin) */
170 /* write the whole config file to the pipe, the script will read everything
174 while (written < size) {
175 if ((ret = write(writepipe[1], input + written, size - written)) < 0) {
176 warn("Could not write to pipe");
183 /* close writing end of the readpipe (connected to the script’s stdout) */
186 /* read the script’s output */
187 int conv_size = 65535;
188 char *converted = malloc(conv_size);
191 if (read_bytes == conv_size) {
193 converted = realloc(converted, conv_size);
195 ret = read(readpipe[0], converted + read_bytes, conv_size - read_bytes);
197 warn("Cannot read from pipe");
204 /* get the returncode */
207 if (!WIFEXITED(status)) {
208 fprintf(stderr, "Child did not terminate normally, using old config file (will lead to broken behaviour)\n");
212 int returncode = WEXITSTATUS(status);
213 if (returncode != 0) {
214 fprintf(stderr, "Migration process exit code was != 0\n");
215 if (returncode == 2) {
216 fprintf(stderr, "could not start the migration script\n");
217 /* TODO: script was not found. tell the user to fix his system or create a v4 config */
218 } else if (returncode == 1) {
219 fprintf(stderr, "This already was a v4 config. Please add the following line to your config file:\n");
220 fprintf(stderr, "# i3 config file (v4)\n");
221 /* TODO: nag the user with a message to include a hint for i3 in his config file */
230 * Handler which will be called when we get a SIGCHLD for the nagbar, meaning
231 * it exited (or could not be started, depending on the exit code).
234 static void nagbar_exited(EV_P_ ev_child *watcher, int revents) {
235 ev_child_stop(EV_A_ watcher);
236 if (!WIFEXITED(watcher->rstatus)) {
237 fprintf(stderr, "ERROR: i3-nagbar did not exit normally.\n");
241 int exitcode = WEXITSTATUS(watcher->rstatus);
242 printf("i3-nagbar process exited with status %d\n", exitcode);
244 fprintf(stderr, "ERROR: i3-nagbar could not be found. Is it correctly installed on your system?\n");
247 configerror_pid = -1;
250 /* We need ev >= 4 for the following code. Since it is not *that* important (it
251 * only makes sure that there are no i3-nagbar instances left behind) we still
252 * support old systems with libev 3. */
253 #if EV_VERSION_MAJOR >= 4
255 * Cleanup handler. Will be called when i3 exits. Kills i3-nagbar with signal
256 * SIGKILL (9) to make sure there are no left-over i3-nagbar processes.
259 static void nagbar_cleanup(EV_P_ ev_cleanup *watcher, int revent) {
260 if (configerror_pid != -1) {
261 LOG("Sending SIGKILL (9) to i3-nagbar with PID %d\n", configerror_pid);
262 kill(configerror_pid, SIGKILL);
268 * Starts an i3-nagbar process which alerts the user that his configuration
269 * file contains one or more errors. Also offers two buttons: One to launch an
270 * $EDITOR on the config file and another one to launch a $PAGER on the error
274 static void start_configerror_nagbar(const char *config_path) {
275 if (only_check_config)
278 fprintf(stderr, "Starting i3-nagbar due to configuration errors\n");
279 configerror_pid = fork();
280 if (configerror_pid == -1) {
281 warn("Could not fork()");
286 if (configerror_pid == 0) {
289 sasprintf(&editaction, "i3-sensible-terminal -e sh -c \"i3-sensible-editor \\\"%s\\\" && i3-msg reload\"", config_path);
290 sasprintf(&pageraction, "i3-sensible-terminal -e i3-sensible-pager \"%s\"", errorfilename);
292 NULL, /* will be replaced by the executable path */
294 (context->has_errors ? "error" : "warning"),
296 (context->has_errors ?
297 "You have an error in your i3 config file!" :
298 "Your config is outdated. Please fix the warnings to make sure everything works."),
302 (errorfilename ? "-b" : NULL),
303 (context->has_errors ? "show errors" : "show warnings"),
307 exec_i3_utility("i3-nagbar", argv);
311 /* install a child watcher */
312 ev_child *child = smalloc(sizeof(ev_child));
313 ev_child_init(child, &nagbar_exited, configerror_pid, 0);
314 ev_child_start(main_loop, child);
316 /* We need ev >= 4 for the following code. Since it is not *that* important (it
317 * only makes sure that there are no i3-nagbar instances left behind) we still
318 * support old systems with libev 3. */
319 #if EV_VERSION_MAJOR >= 4
320 /* install a cleanup watcher (will be called when i3 exits and i3-nagbar is
322 ev_cleanup *cleanup = smalloc(sizeof(ev_cleanup));
323 ev_cleanup_init(cleanup, nagbar_cleanup);
324 ev_cleanup_start(main_loop, cleanup);
329 * Kills the configerror i3-nagbar process, if any.
331 * Called when reloading/restarting.
333 * If wait_for_it is set (restarting), this function will waitpid(), otherwise,
334 * ev is assumed to handle it (reloading).
337 void kill_configerror_nagbar(bool wait_for_it) {
338 if (configerror_pid == -1)
341 if (kill(configerror_pid, SIGTERM) == -1)
342 warn("kill(configerror_nagbar) failed");
347 /* When restarting, we don’t enter the ev main loop anymore and after the
348 * exec(), our old pid is no longer watched. So, ev won’t handle SIGCHLD
349 * for us and we would end up with a <defunct> process. Therefore we
351 waitpid(configerror_pid, NULL, 0);
355 * Checks for duplicate key bindings (the same keycode or keysym is configured
356 * more than once). If a duplicate binding is found, a message is printed to
357 * stderr and the has_errors variable is set to true, which will start
361 static void check_for_duplicate_bindings(struct context *context) {
362 Binding *bind, *current;
363 TAILQ_FOREACH(current, bindings, bindings) {
364 TAILQ_FOREACH(bind, bindings, bindings) {
365 /* Abort when we reach the current keybinding, only check the
370 /* Check if one is using keysym while the other is using bindsym.
372 /* XXX: It should be checked at a later place (when translating the
373 * keysym to keycodes) if there are any duplicates */
374 if ((bind->symbol == NULL && current->symbol != NULL) ||
375 (bind->symbol != NULL && current->symbol == NULL))
378 /* If bind is NULL, current has to be NULL, too (see above).
379 * If the keycodes differ, it can't be a duplicate. */
380 if (bind->symbol != NULL &&
381 strcasecmp(bind->symbol, current->symbol) != 0)
384 /* Check if the keycodes or modifiers are different. If so, they
385 * can't be duplicate */
386 if (bind->keycode != current->keycode ||
387 bind->mods != current->mods)
389 context->has_errors = true;
390 if (current->keycode != 0) {
391 ELOG("Duplicate keybinding in config file:\n modmask %d with keycode %d, command \"%s\"\n",
392 current->mods, current->keycode, current->command);
394 ELOG("Duplicate keybinding in config file:\n modmask %d with keysym %s, command \"%s\"\n",
395 current->mods, current->symbol, current->command);
401 static void migrate_i3bar_exec(struct Autostart *exec) {
402 ELOG("**********************************************************************\n");
403 ELOG("IGNORING exec command: %s\n", exec->command);
404 ELOG("It contains \"i3bar\". Since i3 v4.1, i3bar will be automatically started\n");
405 ELOG("for each 'bar' configuration block in your i3 config. Please remove the exec\n");
406 ELOG("line and add the following to your i3 config:\n");
409 ELOG(" status_command i3status\n");
411 ELOG("**********************************************************************\n");
413 /* Generate a dummy bar configuration */
414 Barconfig *bar_config = scalloc(sizeof(Barconfig));
415 /* The hard-coded ID is not a problem. It does not conflict with the
416 * auto-generated bar IDs and having multiple hard-coded IDs is irrelevant
417 * – they all just contain status_command = i3status */
418 bar_config->id = sstrdup("migrate-bar");
419 bar_config->status_command = sstrdup("i3status");
420 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
422 /* Trigger an i3-nagbar */
423 context->has_warnings = true;
426 void parse_file(const char *f) {
427 SLIST_HEAD(variables_head, Variable) variables = SLIST_HEAD_INITIALIZER(&variables);
428 int fd, ret, read_bytes = 0;
432 char buffer[1026], key[512], value[512];
434 if ((fd = open(f, O_RDONLY)) == -1)
435 die("Could not open configuration file: %s\n", strerror(errno));
437 if (fstat(fd, &stbuf) == -1)
438 die("Could not fstat file: %s\n", strerror(errno));
440 buf = scalloc((stbuf.st_size + 1) * sizeof(char));
441 while (read_bytes < stbuf.st_size) {
442 if ((ret = read(fd, buf + read_bytes, (stbuf.st_size - read_bytes))) < 0)
443 die("Could not read(): %s\n", strerror(errno));
447 if (lseek(fd, 0, SEEK_SET) == (off_t)-1)
448 die("Could not lseek: %s\n", strerror(errno));
450 if ((fstr = fdopen(fd, "r")) == NULL)
451 die("Could not fdopen: %s\n", strerror(errno));
453 while (!feof(fstr)) {
454 if (fgets(buffer, 1024, fstr) == NULL) {
457 die("Could not read configuration file\n");
460 /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
461 if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
462 key[0] == '#' || strlen(key) < 3)
465 if (strcasecmp(key, "set") == 0) {
466 if (value[0] != '$') {
467 ELOG("Malformed variable assignment, name has to start with $\n");
471 /* get key/value for this variable */
472 char *v_key = value, *v_value;
473 if (strstr(value, " ") == NULL && strstr(value, "\t") == NULL) {
474 ELOG("Malformed variable assignment, need a value\n");
478 if (!(v_value = strstr(value, " ")))
479 v_value = strstr(value, "\t");
483 struct Variable *new = scalloc(sizeof(struct Variable));
484 new->key = sstrdup(v_key);
485 new->value = sstrdup(v_value);
486 SLIST_INSERT_HEAD(&variables, new, variables);
487 DLOG("Got new variable %s = %s\n", v_key, v_value);
493 /* For every custom variable, see how often it occurs in the file and
494 * how much extra bytes it requires when replaced. */
495 struct Variable *current, *nearest;
497 /* We need to copy the buffer because we need to invalidate the
498 * variables (otherwise we will count them twice, which is bad when
499 * 'extra' is negative) */
500 char *bufcopy = sstrdup(buf);
501 SLIST_FOREACH(current, &variables, variables) {
502 int extra = (strlen(current->value) - strlen(current->key));
505 next < (bufcopy + stbuf.st_size) &&
506 (next = strcasestr(next, current->key)) != NULL;
507 next += strlen(current->key)) {
509 extra_bytes += extra;
514 /* Then, allocate a new buffer and copy the file over to the new one,
515 * but replace occurences of our variables */
516 char *walk = buf, *destwalk;
517 char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
519 while (walk < (buf + stbuf.st_size)) {
520 /* Find the next variable */
521 SLIST_FOREACH(current, &variables, variables)
522 current->next_match = strcasestr(walk, current->key);
524 int distance = stbuf.st_size;
525 SLIST_FOREACH(current, &variables, variables) {
526 if (current->next_match == NULL)
528 if ((current->next_match - walk) < distance) {
529 distance = (current->next_match - walk);
533 if (nearest == NULL) {
534 /* If there are no more variables, we just copy the rest */
535 strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
536 destwalk += (buf + stbuf.st_size) - walk;
540 /* Copy until the next variable, then copy its value */
541 strncpy(destwalk, walk, distance);
542 strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
543 walk += distance + strlen(nearest->key);
544 destwalk += distance + strlen(nearest->value);
548 /* analyze the string to find out whether this is an old config file (3.x)
549 * or a new config file (4.x). If it’s old, we run the converter script. */
550 int version = detect_version(buf);
552 /* We need to convert this v3 configuration */
553 char *converted = migrate_config(new, stbuf.st_size);
554 if (converted != NULL) {
556 ELOG("****************************************************************\n");
557 ELOG("NOTE: Automatically converted configuration file from v3 to v4.\n");
559 ELOG("Please convert your config file to v4. You can use this command:\n");
560 ELOG(" mv %s %s.O\n", f, f);
561 ELOG(" i3-migrate-config-to-v4 %s.O > %s\n", f, f);
562 ELOG("****************************************************************\n");
568 printf("**********************************************************************\n");
569 printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4\n");
570 printf("was not correctly installed on your system?\n");
571 printf("**********************************************************************\n");
576 /* now lex/parse it */
579 context = scalloc(sizeof(struct context));
580 context->filename = f;
582 if (yyparse() != 0) {
583 fprintf(stderr, "Could not parse configfile\n");
587 check_for_duplicate_bindings(context);
589 /* XXX: The following code will be removed in i3 v4.3 (three releases from
590 * now, as of 2011-10-22) */
591 /* Check for any exec or exec_always lines starting i3bar. We remove these
592 * and add a bar block instead. Additionally, a i3-nagbar warning (not an
593 * error) will be displayed so that users update their config file. */
594 struct Autostart *exec, *next;
595 for (exec = TAILQ_FIRST(&autostarts); exec; ) {
596 next = TAILQ_NEXT(exec, autostarts);
597 if (strstr(exec->command, "i3bar") != NULL) {
598 migrate_i3bar_exec(exec);
599 TAILQ_REMOVE(&autostarts, exec, autostarts);
604 for (exec = TAILQ_FIRST(&autostarts_always); exec; ) {
605 next = TAILQ_NEXT(exec, autostarts_always);
606 if (strstr(exec->command, "i3bar") != NULL) {
607 migrate_i3bar_exec(exec);
608 TAILQ_REMOVE(&autostarts_always, exec, autostarts_always);
613 if (context->has_errors || context->has_warnings) {
614 ELOG("FYI: You are using i3 version " I3_VERSION "\n");
616 ELOG("Please convert your configfile first, then fix any remaining errors (see above).\n");
617 start_configerror_nagbar(f);
621 FREE(context->line_copy);
627 while (!SLIST_EMPTY(&variables)) {
628 current = SLIST_FIRST(&variables);
630 FREE(current->value);
631 SLIST_REMOVE_HEAD(&variables, variables);
639 %lex-param { struct context *context }
644 uint32_t *single_color;
645 struct Colortriple *color;
647 struct Binding *binding;
650 %token <number> NUMBER "<number>"
651 %token <string> WORD "<word>"
652 %token <string> STR "<string>"
653 %token <string> STR_NG "<string (non-greedy)>"
654 %token <string> HEXCOLOR "#<hex>"
655 %token <string> OUTPUT "<RandR output>"
658 %token TOKCOMMENT "<comment>"
659 %token TOKFONT "font"
660 %token TOKBINDSYM "bindsym"
661 %token <number> MODIFIER "<modifier>"
662 %token TOKCONTROL "control"
663 %token TOKSHIFT "shift"
664 %token TOKFLOATING_MODIFIER "floating_modifier"
665 %token <string> QUOTEDSTRING "<quoted string>"
666 %token TOKWORKSPACE "workspace"
667 %token TOKOUTPUT "output"
668 %token TOKASSIGN "assign"
670 %token TOKIPCSOCKET "ipc_socket"
671 %token TOKRESTARTSTATE "restart_state"
672 %token TOKEXEC "exec"
673 %token TOKEXEC_ALWAYS "exec_always"
674 %token <single_color> TOKSINGLECOLOR
675 %token <color> TOKCOLOR
677 %token TOKMODE "mode"
679 %token TOK_ORIENTATION "default_orientation"
680 %token TOK_HORIZ "horizontal"
681 %token TOK_VERT "vertical"
682 %token TOK_AUTO "auto"
683 %token TOK_WORKSPACE_LAYOUT "workspace_layout"
684 %token TOKNEWWINDOW "new_window"
685 %token TOKNEWFLOAT "new_float"
686 %token TOK_NORMAL "normal"
687 %token TOK_NONE "none"
688 %token TOK_1PIXEL "1pixel"
689 %token TOKFOCUSFOLLOWSMOUSE "focus_follows_mouse"
690 %token TOK_FORCE_FOCUS_WRAPPING "force_focus_wrapping"
691 %token TOK_FORCE_XINERAMA "force_xinerama"
692 %token TOK_WORKSPACE_AUTO_BAF "workspace_auto_back_and_forth"
693 %token TOKWORKSPACEBAR "workspace_bar"
694 %token TOK_DEFAULT "default"
695 %token TOK_STACKING "stacking"
696 %token TOK_TABBED "tabbed"
697 %token <number> TOKSTACKLIMIT "stack-limit"
698 %token TOK_POPUP_DURING_FULLSCREEN "popup_during_fullscreen"
699 %token TOK_IGNORE "ignore"
700 %token TOK_LEAVE_FULLSCREEN "leave_fullscreen"
701 %token TOK_FOR_WINDOW "for_window"
703 %token TOK_BAR_OUTPUT "output (bar)"
704 %token TOK_BAR_TRAY_OUTPUT "tray_output"
705 %token TOK_BAR_SOCKET_PATH "socket_path"
706 %token TOK_BAR_MODE "mode (bar)"
707 %token TOK_BAR_HIDE "hide"
708 %token TOK_BAR_DOCK "dock"
709 %token TOK_BAR_POSITION "position"
710 %token TOK_BAR_BOTTOM "bottom"
711 %token TOK_BAR_TOP "top"
712 %token TOK_BAR_STATUS_COMMAND "status_command"
713 %token TOK_BAR_FONT "font (bar)"
714 %token TOK_BAR_WORKSPACE_BUTTONS "workspace_buttons"
715 %token TOK_BAR_VERBOSE "verbose"
716 %token TOK_BAR_COLORS "colors"
717 %token TOK_BAR_COLOR_BACKGROUND "background"
718 %token TOK_BAR_COLOR_STATUSLINE "statusline"
719 %token TOK_BAR_COLOR_FOCUSED_WORKSPACE "focused_workspace"
720 %token TOK_BAR_COLOR_ACTIVE_WORKSPACE "active_workspace"
721 %token TOK_BAR_COLOR_INACTIVE_WORKSPACE "inactive_workspace"
722 %token TOK_BAR_COLOR_URGENT_WORKSPACE "urgent_workspace"
723 %token TOK_NO_STARTUP_ID "--no-startup-id"
725 %token TOK_MARK "mark"
726 %token TOK_CLASS "class"
727 %token TOK_INSTANCE "instance"
728 %token TOK_WINDOW_ROLE "window_role"
730 %token TOK_CON_ID "con_id"
731 %token TOK_TITLE "title"
733 %type <binding> binding
734 %type <binding> bindcode
735 %type <binding> bindsym
736 %type <number> binding_modifiers
737 %type <number> binding_modifier
738 %type <number> direction
739 %type <number> layout_mode
740 %type <number> border_style
741 %type <number> new_window
742 %type <number> new_float
743 %type <number> colorpixel
745 %type <number> popup_setting
746 %type <number> bar_position_position
747 %type <number> bar_mode_mode
748 %type <number> optional_no_startup_id
749 %type <string> command
750 %type <string> word_or_number
751 %type <string> qstring_or_number
752 %type <string> optional_workspace_name
753 %type <string> workspace_name
754 %type <string> window_class
773 | focus_follows_mouse
774 | force_focus_wrapping
776 | workspace_back_and_forth
789 | popup_during_fullscreen
803 TAILQ_INSERT_TAIL(bindings, $1, bindings);
808 TOKBINDCODE bindcode { $$ = $2; }
809 | TOKBINDSYM bindsym { $$ = $2; }
813 binding_modifiers NUMBER command
815 printf("\tFound keycode binding mod%d with key %d and command %s\n", $1, $2, $3);
816 Binding *new = scalloc(sizeof(Binding));
827 binding_modifiers word_or_number command
829 printf("\tFound keysym binding mod%d with key %s and command %s\n", $1, $2, $3);
830 Binding *new = scalloc(sizeof(Binding));
841 TOK_FOR_WINDOW match command
843 if (match_is_empty(¤t_match)) {
844 ELOG("Match is empty, ignoring this for_window statement\n");
847 printf("\t should execute command %s for the criteria mentioned above\n", $3);
848 Assignment *assignment = scalloc(sizeof(Assignment));
849 assignment->type = A_COMMAND;
850 assignment->match = current_match;
851 assignment->dest.command = $3;
852 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
857 | matchstart criteria matchend
859 printf("match parsed\n");
867 match_init(¤t_match);
874 printf("match specification finished\n");
886 printf("criteria: class = %s\n", $3);
887 current_match.class = regex_new($3);
890 | TOK_INSTANCE '=' STR
892 printf("criteria: instance = %s\n", $3);
893 current_match.instance = regex_new($3);
896 | TOK_WINDOW_ROLE '=' STR
898 printf("criteria: window_role = %s\n", $3);
899 current_match.role = regex_new($3);
904 printf("criteria: id = %s\n", $3);
906 long parsed = strtol($3, &end, 10);
907 if (parsed == LONG_MIN ||
908 parsed == LONG_MAX ||
910 (end && *end != '\0')) {
911 ELOG("Could not parse con id \"%s\"\n", $3);
913 current_match.con_id = (Con*)parsed;
914 printf("id as int = %p\n", current_match.con_id);
919 printf("criteria: window id = %s\n", $3);
921 long parsed = strtol($3, &end, 10);
922 if (parsed == LONG_MIN ||
923 parsed == LONG_MAX ||
925 (end && *end != '\0')) {
926 ELOG("Could not parse window id \"%s\"\n", $3);
928 current_match.id = parsed;
929 printf("window id as int = %d\n", current_match.id);
934 printf("criteria: mark = %s\n", $3);
935 current_match.mark = regex_new($3);
940 printf("criteria: title = %s\n", $3);
941 current_match.title = regex_new($3);
948 | NUMBER { sasprintf(&$$, "%d", $1); }
955 sasprintf(&$$, "%d", $1);
960 TOKMODE QUOTEDSTRING '{' modelines '}'
962 if (strcasecmp($2, "default") == 0) {
963 printf("You cannot use the name \"default\" for your mode\n");
966 printf("\t now in mode %s\n", $2);
967 printf("\t current bindings = %p\n", current_bindings);
969 TAILQ_FOREACH(binding, current_bindings, bindings) {
970 printf("got binding on mods %d, keycode %d, symbol %s, command %s\n",
971 binding->mods, binding->keycode, binding->symbol, binding->command);
974 struct Mode *mode = scalloc(sizeof(struct Mode));
976 mode->bindings = current_bindings;
977 current_bindings = NULL;
978 SLIST_INSERT_HEAD(&modes, mode, modes);
992 if (current_bindings == NULL) {
993 current_bindings = scalloc(sizeof(struct bindings_head));
994 TAILQ_INIT(current_bindings);
997 TAILQ_INSERT_TAIL(current_bindings, $1, bindings);
1002 TOK_BAR '{' barlines '}'
1004 printf("\t new bar configuration finished, saving.\n");
1005 /* Generate a unique ID for this bar */
1006 current_bar.id = sstrdup("bar-XXXXXX");
1007 /* This works similar to mktemp in that it replaces the last six X with
1008 * random letters, but without the restriction that the given buffer
1009 * has to contain a valid path name. */
1010 char *x = current_bar.id + strlen("bar-");
1011 while (*x != '\0') {
1012 *(x++) = (rand() % 26) + 'a';
1015 /* If no font was explicitly set, we use the i3 font as default */
1016 if (!current_bar.font && font_pattern)
1017 current_bar.font = sstrdup(font_pattern);
1019 /* Copy the current (static) structure into a dynamically allocated
1020 * one, then cleanup our static one. */
1021 Barconfig *bar_config = scalloc(sizeof(Barconfig));
1022 memcpy(bar_config, ¤t_bar, sizeof(Barconfig));
1023 TAILQ_INSERT_TAIL(&barconfigs, bar_config, configs);
1025 memset(¤t_bar, '\0', sizeof(Barconfig));
1036 | bar_status_command
1042 | bar_workspace_buttons
1046 | bar_color_background
1047 | bar_color_statusline
1048 | bar_color_focused_workspace
1049 | bar_color_active_workspace
1050 | bar_color_inactive_workspace
1051 | bar_color_urgent_workspace
1055 TOK_BAR_STATUS_COMMAND STR
1057 DLOG("should add status command %s\n", $2);
1058 FREE(current_bar.status_command);
1059 current_bar.status_command = $2;
1066 DLOG("bar output %s\n", $2);
1067 int new_outputs = current_bar.num_outputs + 1;
1068 current_bar.outputs = srealloc(current_bar.outputs, sizeof(char*) * new_outputs);
1069 current_bar.outputs[current_bar.num_outputs] = $2;
1070 current_bar.num_outputs = new_outputs;
1075 TOK_BAR_TRAY_OUTPUT STR
1077 DLOG("tray %s\n", $2);
1078 FREE(current_bar.tray_output);
1079 current_bar.tray_output = $2;
1084 TOK_BAR_POSITION bar_position_position
1086 DLOG("position %d\n", $2);
1087 current_bar.position = $2;
1091 bar_position_position:
1092 TOK_BAR_TOP { $$ = P_TOP; }
1093 | TOK_BAR_BOTTOM { $$ = P_BOTTOM; }
1097 TOK_BAR_MODE bar_mode_mode
1099 DLOG("mode %d\n", $2);
1100 current_bar.mode = $2;
1105 TOK_BAR_HIDE { $$ = M_HIDE; }
1106 | TOK_BAR_DOCK { $$ = M_DOCK; }
1112 DLOG("font %s\n", $2);
1113 FREE(current_bar.font);
1114 current_bar.font = $2;
1118 bar_workspace_buttons:
1119 TOK_BAR_WORKSPACE_BUTTONS bool
1121 DLOG("workspace_buttons = %d\n", $2);
1122 /* We store this inverted to make the default setting right when
1123 * initializing the struct with zero. */
1124 current_bar.hide_workspace_buttons = !($2);
1129 TOK_BAR_VERBOSE bool
1131 DLOG("verbose = %d\n", $2);
1132 current_bar.verbose = $2;
1137 TOK_BAR_SOCKET_PATH STR
1139 DLOG("socket_path = %s\n", $2);
1140 FREE(current_bar.socket_path);
1141 current_bar.socket_path = $2;
1146 TOK_BAR_COLORS '{' barlines '}'
1148 /* At the moment, the TOK_BAR_COLORS token is only to make the config
1149 * friendlier for humans. We might change this in the future if it gets
1154 bar_color_background:
1155 TOK_BAR_COLOR_BACKGROUND HEXCOLOR
1157 DLOG("background = %s\n", $2);
1158 current_bar.colors.background = $2;
1162 bar_color_statusline:
1163 TOK_BAR_COLOR_STATUSLINE HEXCOLOR
1165 DLOG("statusline = %s\n", $2);
1166 current_bar.colors.statusline = $2;
1170 bar_color_focused_workspace:
1171 TOK_BAR_COLOR_FOCUSED_WORKSPACE HEXCOLOR HEXCOLOR
1173 DLOG("focused_ws = %s and %s\n", $2, $3);
1174 current_bar.colors.focused_workspace_text = $2;
1175 current_bar.colors.focused_workspace_bg = $3;
1179 bar_color_active_workspace:
1180 TOK_BAR_COLOR_ACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1182 DLOG("active_ws = %s and %s\n", $2, $3);
1183 current_bar.colors.active_workspace_text = $2;
1184 current_bar.colors.active_workspace_bg = $3;
1188 bar_color_inactive_workspace:
1189 TOK_BAR_COLOR_INACTIVE_WORKSPACE HEXCOLOR HEXCOLOR
1191 DLOG("inactive_ws = %s and %s\n", $2, $3);
1192 current_bar.colors.inactive_workspace_text = $2;
1193 current_bar.colors.inactive_workspace_bg = $3;
1197 bar_color_urgent_workspace:
1198 TOK_BAR_COLOR_URGENT_WORKSPACE HEXCOLOR HEXCOLOR
1200 DLOG("urgent_ws = %s and %s\n", $2, $3);
1201 current_bar.colors.urgent_workspace_text = $2;
1202 current_bar.colors.urgent_workspace_bg = $3;
1207 TOKFLOATING_MODIFIER binding_modifiers
1209 DLOG("floating modifier = %d\n", $2);
1210 config.floating_modifier = $2;
1215 TOK_ORIENTATION direction
1217 DLOG("New containers should start with split direction %d\n", $2);
1218 config.default_orientation = $2;
1223 TOK_HORIZ { $$ = HORIZ; }
1224 | TOK_VERT { $$ = VERT; }
1225 | TOK_AUTO { $$ = NO_ORIENTATION; }
1229 TOK_WORKSPACE_LAYOUT layout_mode
1231 DLOG("new containers will be in mode %d\n", $2);
1232 config.default_layout = $2;
1235 /* We also need to change the layout of the already existing
1236 * workspaces here. Workspaces may exist at this point because
1237 * of the other directives which are modifying workspaces
1238 * (setting the preferred screen or name). While the workspace
1239 * objects are already created, they have never been used.
1240 * Thus, the user very likely awaits the default container mode
1241 * to trigger in this case, regardless of where it is inside
1242 * his configuration file. */
1244 TAILQ_FOREACH(ws, workspaces, workspaces) {
1245 if (ws->table == NULL)
1247 switch_layout_mode(global_conn,
1249 config.container_mode);
1253 | TOK_WORKSPACE_LAYOUT TOKSTACKLIMIT TOKSTACKLIMIT NUMBER
1255 DLOG("stack-limit %d with val %d\n", $3, $4);
1256 config.container_stack_limit = $3;
1257 config.container_stack_limit_value = $4;
1260 /* See the comment above */
1262 TAILQ_FOREACH(ws, workspaces, workspaces) {
1263 if (ws->table == NULL)
1265 Container *con = ws->table[0][0];
1266 con->stack_limit = config.container_stack_limit;
1267 con->stack_limit_value = config.container_stack_limit_value;
1274 TOK_DEFAULT { $$ = L_DEFAULT; }
1275 | TOK_STACKING { $$ = L_STACKED; }
1276 | TOK_TABBED { $$ = L_TABBED; }
1280 TOKNEWWINDOW border_style
1282 DLOG("new windows should start with border style %d\n", $2);
1283 config.default_border = $2;
1288 TOKNEWFLOAT border_style
1290 DLOG("new floating windows should start with border style %d\n", $2);
1291 config.default_floating_border = $2;
1296 TOK_NORMAL { $$ = BS_NORMAL; }
1297 | TOK_NONE { $$ = BS_NONE; }
1298 | TOK_1PIXEL { $$ = BS_1PIXEL; }
1308 DLOG("checking word \"%s\"\n", $1);
1309 $$ = (strcasecmp($1, "yes") == 0 ||
1310 strcasecmp($1, "true") == 0 ||
1311 strcasecmp($1, "on") == 0 ||
1312 strcasecmp($1, "enable") == 0 ||
1313 strcasecmp($1, "active") == 0);
1317 focus_follows_mouse:
1318 TOKFOCUSFOLLOWSMOUSE bool
1320 DLOG("focus follows mouse = %d\n", $2);
1321 config.disable_focus_follows_mouse = !($2);
1325 force_focus_wrapping:
1326 TOK_FORCE_FOCUS_WRAPPING bool
1328 DLOG("force focus wrapping = %d\n", $2);
1329 config.force_focus_wrapping = $2;
1334 TOK_FORCE_XINERAMA bool
1336 DLOG("force xinerama = %d\n", $2);
1337 config.force_xinerama = $2;
1341 workspace_back_and_forth:
1342 TOK_WORKSPACE_AUTO_BAF bool
1344 DLOG("automatic workspace back-and-forth = %d\n", $2);
1345 config.workspace_auto_back_and_forth = $2;
1350 TOKWORKSPACEBAR bool
1352 DLOG("workspace bar = %d\n", $2);
1353 config.disable_workspace_bar = !($2);
1358 TOKWORKSPACE qstring_or_number TOKOUTPUT OUTPUT optional_workspace_name
1363 ELOG("The old (v3) syntax workspace <number> output <output> <name> is deprecated.\n");
1364 ELOG("Please use the new syntax: workspace \"<workspace>\" output <output>\n");
1365 ELOG("In your case, the following should work:\n");
1366 ELOG(" workspace \"%s\" output %s\n", $5, $4);
1368 context->has_warnings = true;
1371 DLOG("Assigning workspace \"%s\" to output \"%s\"\n", ws_name, $4);
1372 /* Check for earlier assignments of the same workspace so that we
1373 * don’t have assignments of a single workspace to different
1375 struct Workspace_Assignment *assignment;
1376 bool duplicate = false;
1377 TAILQ_FOREACH(assignment, &ws_assignments, ws_assignments) {
1378 if (strcasecmp(assignment->name, ws_name) == 0) {
1379 ELOG("You have a duplicate workspace assignment for workspace \"%s\"\n",
1381 assignment->output = $4;
1386 assignment = scalloc(sizeof(struct Workspace_Assignment));
1387 assignment->name = ws_name;
1388 assignment->output = $4;
1389 TAILQ_INSERT_TAIL(&ws_assignments, assignment, ws_assignments);
1392 | TOKWORKSPACE NUMBER workspace_name
1396 DLOG("Invalid workspace assignment, workspace number %d out of range\n", ws_num);
1398 DLOG("workspace name to: %s\n", $3);
1400 if ($<string>3 != NULL) {
1401 workspace_set_name(workspace_get(ws_num - 1), $<string>3);
1409 optional_workspace_name:
1410 /* empty */ { $$ = NULL; }
1411 | workspace_name { $$ = $1; }
1415 QUOTEDSTRING { $$ = $1; }
1421 TOKASSIGN window_class STR
1423 /* This is the old, deprecated form of assignments. It’s provided for
1424 * compatibility in version (4.1, 4.2, 4.3) and will be removed
1425 * afterwards. It triggers an i3-nagbar warning starting from 4.1. */
1426 ELOG("You are using the old assign syntax (without criteria). "
1427 "Please see the User's Guide for the new syntax and fix "
1428 "your config file.\n");
1429 context->has_warnings = true;
1430 printf("assignment of %s to *%s*\n", $2, $3);
1431 char *workspace = $3;
1432 char *criteria = $2;
1434 Assignment *assignment = scalloc(sizeof(Assignment));
1435 Match *match = &(assignment->match);
1438 char *separator = NULL;
1439 if ((separator = strchr(criteria, '/')) != NULL) {
1440 *(separator++) = '\0';
1442 sasprintf(&pattern, "(?i)%s", separator);
1443 match->title = regex_new(pattern);
1445 printf(" title = %s\n", separator);
1447 if (*criteria != '\0') {
1449 sasprintf(&pattern, "(?i)%s", criteria);
1450 match->class = regex_new(pattern);
1452 printf(" class = %s\n", criteria);
1456 /* Compatibility with older versions: If the assignment target starts
1457 * with ~, we create the equivalent of:
1459 * for_window [class="foo"] floating enable
1461 if (*workspace == '~') {
1463 if (*workspace == '\0') {
1464 /* This assignment was *only* for floating */
1465 assignment->type = A_COMMAND;
1466 assignment->dest.command = sstrdup("floating enable");
1467 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1470 /* Create a new assignment and continue afterwards */
1471 Assignment *floating = scalloc(sizeof(Assignment));
1472 match_copy(&(floating->match), match);
1473 floating->type = A_COMMAND;
1474 floating->dest.command = sstrdup("floating enable");
1475 TAILQ_INSERT_TAIL(&assignments, floating, assignments);
1479 assignment->type = A_TO_WORKSPACE;
1480 assignment->dest.workspace = workspace;
1481 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1483 | TOKASSIGN match STR
1485 if (match_is_empty(¤t_match)) {
1486 ELOG("Match is empty, ignoring this assignment\n");
1489 printf("new assignment, using above criteria, to workspace %s\n", $3);
1490 Assignment *assignment = scalloc(sizeof(Assignment));
1491 assignment->match = current_match;
1492 assignment->type = A_TO_WORKSPACE;
1493 assignment->dest.workspace = $3;
1494 TAILQ_INSERT_TAIL(&assignments, assignment, assignments);
1506 config.ipc_socket_path = $2;
1513 config.restart_state_path = $2;
1518 TOKEXEC optional_no_startup_id STR
1520 struct Autostart *new = smalloc(sizeof(struct Autostart));
1522 new->no_startup_id = $2;
1523 TAILQ_INSERT_TAIL(&autostarts, new, autostarts);
1528 TOKEXEC_ALWAYS optional_no_startup_id STR
1530 struct Autostart *new = smalloc(sizeof(struct Autostart));
1532 new->no_startup_id = $2;
1533 TAILQ_INSERT_TAIL(&autostarts_always, new, autostarts_always);
1537 optional_no_startup_id:
1538 /* empty */ { $$ = false; }
1539 | TOK_NO_STARTUP_ID { $$ = true; }
1545 ELOG("The terminal option is DEPRECATED and has no effect. "
1546 "Please remove it from your configuration file.\n");
1553 config.font = load_font($2, true);
1554 printf("font %s\n", $2);
1561 TOKSINGLECOLOR colorpixel
1563 uint32_t *dest = $1;
1569 TOKCOLOR colorpixel colorpixel colorpixel
1571 struct Colortriple *dest = $1;
1574 dest->background = $3;
1582 $$ = get_colorpixel($1);
1589 /* NULL */ { $$ = 0; }
1591 | binding_modifiers '+' binding_modifier { $$ = $1 | $3; }
1592 | binding_modifiers '+' { $$ = $1; }
1596 MODIFIER { $$ = $1; }
1597 | TOKCONTROL { $$ = BIND_CONTROL; }
1598 | TOKSHIFT { $$ = BIND_SHIFT; }
1601 popup_during_fullscreen:
1602 TOK_POPUP_DURING_FULLSCREEN popup_setting
1604 DLOG("popup_during_fullscreen setting: %d\n", $2);
1605 config.popup_during_fullscreen = $2;
1610 TOK_IGNORE { $$ = PDF_IGNORE; }
1611 | TOK_LEAVE_FULLSCREEN { $$ = PDF_LEAVE_FULLSCREEN; }