]> git.sur5r.net Git - i3/i3/commitdiff
Allow checking for duplicate bindings with -C 3669/head
authorOrestis Floros <orestisf1993@gmail.com>
Fri, 29 Mar 2019 00:36:24 +0000 (02:36 +0200)
committerOrestis Floros <orestisf1993@gmail.com>
Fri, 29 Mar 2019 10:30:04 +0000 (12:30 +0200)
- Having both parse_configuration and parse_file is excessive now
- We detect if we are parsing only by checking if conn is NULL, not with
use_nagbar
- font.pattern needs to be set to NULL because it is freed in
free_font()

Fixes #3660

include/configuration.h
libi3/font.c
src/commands.c
src/config.c
src/main.c
testcases/t/235-check-config-no-x.t

index 1971146b81b738feed3ee12ae7003bb425a67451..872f11c8d75a710a81d4522a75f84061cebf31b3 100644 (file)
@@ -400,19 +400,11 @@ struct tray_output_t {
     tray_outputs;
 };
 
-/**
- * Finds the configuration file to use (either the one specified by
- * override_configpath), the user’s one or the system default) and calls
- * parse_file().
- *
- * If you specify override_configpath, only this path is used to look for a
- * configuration file.
- *
- * If use_nagbar is false, don't try to start i3-nagbar but log the errors to
- * stdout/stderr instead.
- *
- */
-bool parse_configuration(const char *override_configpath, bool use_nagbar);
+typedef enum {
+    C_VALIDATE,
+    C_LOAD,
+    C_RELOAD,
+} config_load_t;
 
 /**
  * (Re-)loads the configuration file (sets useful defaults before).
@@ -420,8 +412,12 @@ bool parse_configuration(const char *override_configpath, bool use_nagbar);
  * If you specify override_configpath, only this path is used to look for a
  * configuration file.
  *
+ * load_type specifies the type of loading: C_VALIDATE is used to only verify
+ * the correctness of the config file (used with the flag -C). C_LOAD will load
+ * the config for normal use and display errors in the nagbar. C_RELOAD will
+ * also clear the previous config.
  */
-void load_configuration(const char *override_configfile, bool reload);
+bool load_configuration(const char *override_configfile, config_load_t load_type);
 
 /**
  * Ungrabs all keys, to be called before re-grabbing the keys because of a
index c06bae00c86fd6a32be6f897fd36d03ae01c24bc..e16ce85e7294657d948a7ede3dd9cec581028d8e 100644 (file)
@@ -163,6 +163,7 @@ i3Font load_font(const char *pattern, const bool fallback) {
 
     i3Font font;
     font.type = FONT_TYPE_NONE;
+    font.pattern = NULL;
 
     /* No XCB connction, return early because we're just validating the
      * configuration file. */
index 69015d92ee43a14c96482a97786c2b5fb1bc1931..624c27dbd8f4c59dbf198b87aed5ec603653bcc1 100644 (file)
@@ -1573,7 +1573,7 @@ void cmd_reload(I3_CMD) {
     LOG("reloading\n");
     kill_nagbar(&config_error_nagbar_pid, false);
     kill_nagbar(&command_error_nagbar_pid, false);
-    load_configuration(NULL, true);
+    load_configuration(NULL, C_RELOAD);
     x_set_i3_atoms();
     /* Send an IPC event just in case the ws names have changed */
     ipc_send_workspace_event("reload", NULL, NULL);
index e0eb5378e225321b869cc5c744e9fa548bb4ddbc..81919566ea25dd15f5cff16c98fa704987a55c28 100644 (file)
@@ -159,42 +159,20 @@ static void free_configuration(void) {
     free(config.fake_outputs);
 }
 
-/*
- * Finds the configuration file to use (either the one specified by
- * override_configpath), the user’s one or the system default) and calls
- * parse_file().
- *
- */
-bool parse_configuration(const char *override_configpath, bool use_nagbar) {
-    char *path = get_config_path(override_configpath, true);
-    if (path == NULL) {
-        die("Unable to find the configuration file (looked at "
-            "$XDG_CONFIG_HOME/i3/config, ~/.i3/config, $XDG_CONFIG_DIRS/i3/config "
-            "and " SYSCONFDIR "/i3/config)");
-    }
-
-    LOG("Parsing configfile %s\n", path);
-    FREE(current_configpath);
-    current_configpath = path;
-
-    /* initialize default bindings if we're just validating the config file */
-    if (!use_nagbar && bindings == NULL) {
-        bindings = scalloc(1, sizeof(struct bindings_head));
-        TAILQ_INIT(bindings);
-    }
-
-    return parse_file(path, use_nagbar);
-}
-
 /*
  * (Re-)loads the configuration file (sets useful defaults before).
  *
  * If you specify override_configpath, only this path is used to look for a
  * configuration file.
  *
+ * load_type specifies the type of loading: C_VALIDATE is used to only verify
+ * the correctness of the config file (used with the flag -C). C_LOAD will load
+ * the config for normal use and display errors in the nagbar. C_RELOAD will
+ * also clear the previous config.
+ *
  */
-void load_configuration(const char *override_configpath, bool reload) {
-    if (reload) {
+bool load_configuration(const char *override_configpath, config_load_t load_type) {
+    if (load_type == C_RELOAD) {
         free_configuration();
     }
 
@@ -250,24 +228,32 @@ void load_configuration(const char *override_configpath, bool reload) {
 
     config.focus_wrapping = FOCUS_WRAPPING_ON;
 
-    parse_configuration(override_configpath, true);
-
-    if (reload) {
-        translate_keysyms();
-        grab_all_keys(conn);
-        regrab_all_buttons(conn);
+    FREE(current_configpath);
+    current_configpath = get_config_path(override_configpath, true);
+    if (current_configpath == NULL) {
+        die("Unable to find the configuration file (looked at "
+            "$XDG_CONFIG_HOME/i3/config, ~/.i3/config, $XDG_CONFIG_DIRS/i3/config "
+            "and " SYSCONFDIR "/i3/config)");
     }
+    LOG("Parsing configfile %s\n", current_configpath);
+    const bool result = parse_file(current_configpath, load_type != C_VALIDATE);
 
-    if (config.font.type == FONT_TYPE_NONE) {
+    if (config.font.type == FONT_TYPE_NONE && load_type != C_VALIDATE) {
         ELOG("You did not specify required configuration option \"font\"\n");
         config.font = load_font("fixed", true);
         set_font(&config.font);
     }
 
-    /* Redraw the currently visible decorations on reload, so that
-     * the possibly new drawing parameters changed. */
-    if (reload) {
+    if (load_type == C_RELOAD) {
+        translate_keysyms();
+        grab_all_keys(conn);
+        regrab_all_buttons(conn);
+
+        /* Redraw the currently visible decorations on reload, so that the
+         * possibly new drawing parameters changed. */
         x_deco_recurse(croot);
         xcb_flush(conn);
     }
+
+    return result;
 }
index d7d7530a968ebc527d1ca60a13bb8241328cd71f..3ebdbf0cc0a2667f4cc4bf9c98e85d59fa8a1b90 100644 (file)
@@ -420,7 +420,7 @@ int main(int argc, char *argv[]) {
     }
 
     if (only_check_config) {
-        exit(parse_configuration(override_configpath, false) ? 0 : 1);
+        exit(load_configuration(override_configpath, C_VALIDATE) ? 0 : 1);
     }
 
     /* If the user passes more arguments, we act like i3-msg would: Just send
@@ -586,7 +586,7 @@ int main(int argc, char *argv[]) {
 #include "atoms.xmacro"
 #undef xmacro
 
-    load_configuration(override_configpath, false);
+    load_configuration(override_configpath, C_LOAD);
 
     if (config.ipc_socket_path == NULL) {
         /* Fall back to a file name in /tmp/ based on the PID */
index ef6211420a835476628b0af7c7091405dce52b64..dce70894b787b6bb06b8de0ea2ba0dccad5f4c51 100644 (file)
@@ -59,4 +59,33 @@ EOT
 is($ret, 0, "exit code == 0");
 is($out, "", 'valid config file');
 
+################################################################################
+# 3: test duplicate keybindings
+################################################################################
+
+$cfg = <<EOT;
+# i3 config file (v4)
+font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
+bindsym Shift+a nop 1
+bindsym Shift+a nop 2
+EOT
+
+($ret, $out) = check_config($cfg);
+is($ret, 1, "exit code == 1");
+like($out, qr/ERROR: *Duplicate keybinding in config file/, 'duplicate keybindings');
+
+################################################################################
+# 4: test no duplicate keybindings
+################################################################################
+
+$cfg = <<EOT;
+# i3 config file (v4)
+font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
+bindsym Shift+a nop 1
+EOT
+
+($ret, $out) = check_config($cfg);
+is($ret, 0, "exit code == 0");
+is($out, "", 'valid config file');
+
 done_testing;