+ /* sscanf implicitly strips whitespace. Also, we skip comments and empty lines. */
+ if (sscanf(buffer, "%s %[^\n]", key, value) < 1 ||
+ key[0] == '#' || strlen(key) < 3)
+ continue;
+
+ if (strcasecmp(key, "set") == 0) {
+ if (value[0] != '$')
+ die("Malformed variable assignment, name has to start with $\n");
+
+ /* get key/value for this variable */
+ char *v_key = value, *v_value;
+ if ((v_value = strstr(value, " ")) == NULL)
+ die("Malformed variable assignment, need a value\n");
+
+ *(v_value++) = '\0';
+
+ struct Variable *new = scalloc(sizeof(struct Variable));
+ new->key = sstrdup(v_key);
+ new->value = sstrdup(v_value);
+ SLIST_INSERT_HEAD(&variables, new, variables);
+ DLOG("Got new variable %s = %s\n", v_key, v_value);
+ continue;
+ }
+ }
+ fclose(fstr);
+
+ /* For every custom variable, see how often it occurs in the file and
+ * how much extra bytes it requires when replaced. */
+ struct Variable *current, *nearest;
+ int extra_bytes = 0;
+ /* We need to copy the buffer because we need to invalidate the
+ * variables (otherwise we will count them twice, which is bad when
+ * 'extra' is negative) */
+ char *bufcopy = sstrdup(buf);
+ SLIST_FOREACH(current, &variables, variables) {
+ int extra = (strlen(current->value) - strlen(current->key));
+ char *next;
+ for (next = bufcopy;
+ (next = strcasestr(bufcopy + (next - bufcopy), current->key)) != NULL;
+ next += strlen(current->key)) {
+ *next = '_';
+ extra_bytes += extra;
+ }
+ }
+ FREE(bufcopy);
+
+ /* Then, allocate a new buffer and copy the file over to the new one,
+ * but replace occurences of our variables */
+ char *walk = buf, *destwalk;
+ char *new = smalloc((stbuf.st_size + extra_bytes + 1) * sizeof(char));
+ destwalk = new;
+ while (walk < (buf + stbuf.st_size)) {
+ /* Find the next variable */
+ SLIST_FOREACH(current, &variables, variables)
+ current->next_match = strcasestr(walk, current->key);
+ nearest = NULL;
+ int distance = stbuf.st_size;
+ SLIST_FOREACH(current, &variables, variables) {
+ if (current->next_match == NULL)
+ continue;
+ if ((current->next_match - walk) < distance) {
+ distance = (current->next_match - walk);
+ nearest = current;
+ }
+ }
+ if (nearest == NULL) {
+ /* If there are no more variables, we just copy the rest */
+ strncpy(destwalk, walk, (buf + stbuf.st_size) - walk);
+ destwalk += (buf + stbuf.st_size) - walk;
+ *destwalk = '\0';
+ break;
+ } else {
+ /* Copy until the next variable, then copy its value */
+ strncpy(destwalk, walk, distance);
+ strncpy(destwalk + distance, nearest->value, strlen(nearest->value));
+ walk += distance + strlen(nearest->key);
+ destwalk += distance + strlen(nearest->value);
+ }
+ }
+
+ /* analyze the string to find out whether this is an old config file (3.x)
+ * or a new config file (4.x). If it’s old, we run the converter script. */
+ int version = detect_version(buf);
+ if (version == 3) {
+ /* We need to convert this v3 configuration */
+ char *converted = migrate_config(new, stbuf.st_size);
+ if (converted != NULL) {
+ printf("\n");
+ printf("****************************************************************\n");
+ printf("NOTE: Automatically converted configuration file from v3 to v4.\n");
+ printf("\n");
+ printf("Please convert your config file to v4. You can use this command:\n");
+ printf(" mv %s %s.O\n", f, f);
+ printf(" i3-migrate-config-to-v4.pl %s.O > %s\n", f, f);
+ printf("****************************************************************\n");
+ printf("\n");
+ free(new);
+ new = converted;
+ } else {
+ printf("\n");
+ printf("**********************************************************************\n");
+ printf("ERROR: Could not convert config file. Maybe i3-migrate-config-to-v4.pl\n");
+ printf("was not correctly installed on your system?\n");
+ printf("**********************************************************************\n");
+ printf("\n");