[RFC 4/5] cli: add notmuch config append command to append to string list configs

Jani Nikula jani at nikula.org
Tue Nov 3 11:49:32 PST 2015


Make it easy to add new entries to string list configs. For example,
'notmuch config append new.ignore my-non-mail-file'.
---
 notmuch-config.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)

diff --git a/notmuch-config.c b/notmuch-config.c
index d252bb25d4bb..96c73b0cc2ac 100644
--- a/notmuch-config.c
+++ b/notmuch-config.c
@@ -831,6 +831,46 @@ notmuch_config_command_set (notmuch_config_t *config, char *item, int argc, char
 }
 
 static int
+notmuch_config_command_append (notmuch_config_t *config, char *item,
+			       int argc, char *argv[])
+{
+    char **value, **new_value;
+    size_t i, length, new_length;
+    char *group, *key;
+
+    if (_item_split (item, &group, &key))
+	return 1;
+
+    value = g_key_file_get_string_list (config->key_file, group, key,
+					&length, NULL);
+    if (value == NULL)
+	length = 0;
+
+    new_length = length + argc;
+    new_value = talloc_size (NULL, new_length * sizeof (char *));
+    if (! new_value)
+	return 1;
+
+    for (i = 0; i < new_length; i++) {
+	if (i < length)
+	    new_value[i] = value[i];
+	else
+	    new_value[i] = argv[i - length];
+    }
+
+    if (new_length == 1)
+	g_key_file_set_string (config->key_file, group, key, new_value[0]);
+    else
+	g_key_file_set_string_list (config->key_file, group, key,
+				    (const gchar **) new_value, new_length);
+
+    g_strfreev (value);
+    talloc_free (new_value);
+
+    return notmuch_config_save (config);
+}
+
+static int
 notmuch_config_command_list (notmuch_config_t *config)
 {
     char **groups;
@@ -905,6 +945,13 @@ notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])
 	    return EXIT_FAILURE;
 	}
 	ret = notmuch_config_command_set (config, argv[1], argc - 2, argv + 2);
+    } else if (strcmp (argv[0], "append") == 0) {
+	if (argc < 3) {
+	    fprintf (stderr, "Error: notmuch config append requires at least "
+		     "two arguments.\n");
+	    return EXIT_FAILURE;
+	}
+	ret = notmuch_config_command_append (config, argv[1], argc - 2, argv + 2);
     } else if (strcmp (argv[0], "list") == 0) {
 	ret = notmuch_config_command_list (config);
     } else {
-- 
2.1.4



More information about the notmuch mailing list