]> git.sur5r.net Git - i3/i3/blobdiff - src/config_parser.c
Ensure config variables match on longest-length (#2306)
[i3/i3] / src / config_parser.c
index 2b4572b017fb32dc061cea9ae0f19088dcbe291f..5ee69e8d170388c830c6347aa7881dd7a8dcd217 100644 (file)
@@ -122,7 +122,7 @@ static void push_string(const char *identifier, const char *str) {
     /* When we arrive here, the stack is full. This should not happen and
      * means there’s either a bug in this parser or the specification
      * contains a command with more than 10 identified tokens. */
-    fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
+    fprintf(stderr, "BUG: config_parser stack full. This means either a bug "
                     "in the code, or a new command which contains more than "
                     "10 identified tokens.\n");
     exit(1);
@@ -142,7 +142,7 @@ static void push_long(const char *identifier, long num) {
     /* When we arrive here, the stack is full. This should not happen and
      * means there’s either a bug in this parser or the specification
      * contains a command with more than 10 identified tokens. */
-    fprintf(stderr, "BUG: commands_parser stack full. This means either a bug "
+    fprintf(stderr, "BUG: config_parser stack full. This means either a bug "
                     "in the code, or a new command which contains more than "
                     "10 identified tokens.\n");
     exit(1);
@@ -178,53 +178,6 @@ static void clear_stack(void) {
     }
 }
 
-// TODO: remove this if it turns out we don’t need it for testing.
-#if 0
-/*******************************************************************************
- * A dynamically growing linked list which holds the criteria for the current
- * command.
- ******************************************************************************/
-
-typedef struct criterion {
-    char *type;
-    char *value;
-
-    TAILQ_ENTRY(criterion) criteria;
-} criterion;
-
-static TAILQ_HEAD(criteria_head, criterion) criteria =
-  TAILQ_HEAD_INITIALIZER(criteria);
-
-/*
- * Stores the given type/value in the list of criteria.
- * Accepts a pointer as first argument, since it is 'call'ed by the parser.
- *
- */
-static void push_criterion(void *unused_criteria, const char *type,
-                           const char *value) {
-    struct criterion *criterion = smalloc(sizeof(struct criterion));
-    criterion->type = sstrdup(type);
-    criterion->value = sstrdup(value);
-    TAILQ_INSERT_TAIL(&criteria, criterion, criteria);
-}
-
-/*
- * Clears the criteria linked list.
- * Accepts a pointer as first argument, since it is 'call'ed by the parser.
- *
- */
-static void clear_criteria(void *unused_criteria) {
-    struct criterion *criterion;
-    while (!TAILQ_EMPTY(&criteria)) {
-        criterion = TAILQ_FIRST(&criteria);
-        free(criterion->type);
-        free(criterion->value);
-        TAILQ_REMOVE(&criteria, criterion, criteria);
-        free(criterion);
-    }
-}
-#endif
-
 /*******************************************************************************
  * The parser itself.
  ******************************************************************************/
@@ -890,7 +843,7 @@ bool parse_file(const char *f, bool use_nagbar) {
                 break;
             die("Could not read configuration file\n");
         }
-        if (buffer[strlen(buffer) - 1] != '\n') {
+        if (buffer[strlen(buffer) - 1] != '\n' && !feof(fstr)) {
             ELOG("Your line continuation is too long, it exceeds %zd bytes\n", sizeof(buffer));
         }
         continuation = strstr(buffer, "\\\n");
@@ -926,9 +879,23 @@ bool parse_file(const char *f, bool use_nagbar) {
                 v_value++;
 
             struct Variable *new = scalloc(1, sizeof(struct Variable));
+            struct Variable *test = NULL, *loc = NULL;
             new->key = sstrdup(v_key);
             new->value = sstrdup(v_value);
-            SLIST_INSERT_HEAD(&variables, new, variables);
+            /* ensure that the correct variable is matched in case of one being
+             * the prefix of another */
+            SLIST_FOREACH(test, &variables, variables) {
+                if (strlen(new->key) >= strlen(test->key))
+                    break;
+                loc = test;
+            }
+
+            if (loc == NULL) {
+                SLIST_INSERT_HEAD(&variables, new, variables);
+            } else {
+                SLIST_INSERT_AFTER(loc, new, variables);
+            }
+
             DLOG("Got new variable %s = %s\n", v_key, v_value);
             continue;
         }
@@ -957,7 +924,7 @@ bool parse_file(const char *f, bool use_nagbar) {
     FREE(bufcopy);
 
     /* Then, allocate a new buffer and copy the file over to the new one,
-     * but replace occurences of our variables */
+     * but replace occurrences of our variables */
     char *walk = buf, *destwalk;
     char *new = smalloc(stbuf.st_size + extra_bytes + 1);
     destwalk = new;