[PATCH] Add support for ~/foo style relative paths

Subject: [PATCH] Add support for ~/foo style relative paths

Date: Tue, 29 Oct 2024 23:15:20 +0100

To: notmuch@notmuchmail.org

Cc: Ralph Seichter

From: Ralph Seichter


In addition to the existing implicit syntax of non-absolute paths
being interpreted as relative to users' $HOME directories, also
allow the explicit syntax "~/some/relative/path". This well known
variant improves readability of configuration files.

Signed-off-by: Ralph Seichter <ralph@seichter.de>
---
 lib/config.cc | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/lib/config.cc b/lib/config.cc
index acb397ec..2537b451 100644
--- a/lib/config.cc
+++ b/lib/config.cc
@@ -400,15 +400,21 @@ notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs)
 static char *
 _expand_path (void *ctx, const char *key, const char *val)
 {
-    char *expanded_val;
+    char *expanded_val = NULL;
 
-    if ((strcmp (key, "database.path") == 0 ||
+    if (strcmp (key, "database.path") == 0 ||
 	 strcmp (key, "database.mail_root") == 0 ||
 	 strcmp (key, "database.hook_dir") == 0 ||
-	 strcmp (key, "database.backup_path") == 0 ) &&
-	val[0] != '/')
-	expanded_val = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), val);
-    else
+	 strcmp (key, "database.backup_path") == 0) {
+	if (val[0] == '~')
+	    /* Explicit syntax for "~/some/relative/path" */
+	    expanded_val = talloc_asprintf (ctx, "%s%s", getenv ("HOME"), &val[1]);
+	else if (val[0] != '/')
+	    /* Implicit syntax for "some/relative/path" */
+	    expanded_val = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), val);
+    }
+    if (! expanded_val)
+	/* Catchall, no special expansion */
 	expanded_val = talloc_strdup (ctx, val);
 
     return expanded_val;
-- 
2.47.0

_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: