+/*
+ * This function does a very simple replacement of each instance of key with value.
+ *
+ */
+static void replace_variable(char *buffer, const char *key, const char *value) {
+ char *pos;
+ /* To prevent endless recursions when the user makes an error configuring,
+ * we stop after 100 replacements. That should be vastly more than enough. */
+ int c = 0;
+ LOG("Replacing %s with %s\n", key, value);
+ while ((pos = strcasestr(buffer, key)) != NULL && c++ < 100) {
+ LOG("replacing variable %s in \"%s\" with \"%s\"\n", key, buffer, value);
+ char *rest = pos + strlen(key);
+ *pos = '\0';
+ char *replaced;
+ asprintf(&replaced, "%s%s%s", buffer, value, rest);
+ /* Hm, this is a bit ugly, but sizeof(buffer) = 4, as it’s just a pointer.
+ * So we need to hard-code the dimensions here. */
+ strncpy(buffer, replaced, 1026);
+ free(replaced);
+ }
+}