+ _config_set_list (config, "search", "exclude_tags", list, length);
+}
+
+
+/* Given a configuration item of the form <group>.<key> return the
+ * component group and key. If any error occurs, print a message on
+ * stderr and return 1. Otherwise, return 0.
+ *
+ * Note: This function modifies the original 'item' string.
+ */
+static int
+_item_split (char *item, char **group, char **key)
+{
+ char *period;
+
+ *group = item;
+
+ period = strchr (item, '.');
+ if (period == NULL || *(period + 1) == '\0') {
+ fprintf (stderr,
+ "Invalid configuration name: %s\n"
+ "(Should be of the form <section>.<item>)\n", item);
+ return 1;
+ }
+
+ *period = '\0';
+ *key = period + 1;
+
+ return 0;
+}
+
+/* These are more properly called Xapian fields, but the user facing
+ * docs call them prefixes, so make the error message match */
+static bool
+validate_field_name (const char *str)
+{
+ const char *key;
+
+ if (! g_utf8_validate (str, -1, NULL)) {
+ fprintf (stderr, "Invalid utf8: %s\n", str);
+ return false;
+ }
+
+ key = g_utf8_strrchr (str, -1, '.');
+ if (! key ) {
+ INTERNAL_ERROR ("Impossible code path on input: %s\n", str);
+ }
+
+ key++;
+
+ if (! *key) {
+ fprintf (stderr, "Empty prefix name: %s\n", str);
+ return false;
+ }
+
+ if (! unicode_word_utf8 (key)) {
+ fprintf (stderr, "Non-word character in prefix name: %s\n", key);
+ return false;
+ }
+
+ if (key[0] >= 'a' && key[0] <= 'z') {
+ fprintf (stderr, "Prefix names starting with lower case letters are reserved: %s\n", key);
+ return false;
+ }
+
+ return true;
+}
+
+#define BUILT_WITH_PREFIX "built_with."
+
+typedef struct config_key {
+ const char *name;
+ bool prefix;
+ bool (*validate)(const char *);
+} config_key_info_t;
+
+static struct config_key
+ config_key_table[] = {
+ { "index.decrypt", false, NULL },
+ { "index.header.", true, validate_field_name },
+ { "query.", true, NULL },
+};
+
+static config_key_info_t *
+_config_key_info (const char *item)
+{
+ for (size_t i = 0; i < ARRAY_SIZE (config_key_table); i++) {
+ if (config_key_table[i].prefix &&
+ strncmp (item, config_key_table[i].name,
+ strlen (config_key_table[i].name)) == 0)
+ return config_key_table + i;
+ if (strcmp (item, config_key_table[i].name) == 0)
+ return config_key_table + i;
+ }
+ return NULL;
+}
+
+static int
+notmuch_config_command_get (notmuch_database_t *notmuch, char *item)
+{
+ notmuch_config_values_t *list;
+
+ for (list = notmuch_config_get_values_string (notmuch, item);
+ notmuch_config_values_valid (list);
+ notmuch_config_values_move_to_next (list)) {
+ const char *val = notmuch_config_values_get (list);
+ puts (val);
+ }
+ return EXIT_SUCCESS;
+}
+
+static int
+_set_db_config (notmuch_database_t *notmuch, const char *key, int argc, char **argv)
+{
+ const char *val = "";
+
+ if (argc > 1) {
+ /* XXX handle lists? */
+ fprintf (stderr, "notmuch config set: at most one value expected for %s\n", key);
+ return EXIT_FAILURE;
+ }
+
+ if (argc > 0) {
+ val = argv[0];
+ }
+
+ if (print_status_database ("notmuch config", notmuch,
+ notmuch_database_reopen (notmuch,
+ NOTMUCH_DATABASE_MODE_READ_WRITE)))
+ return EXIT_FAILURE;
+
+ if (print_status_database ("notmuch config", notmuch,
+ notmuch_database_set_config (notmuch, key, val)))
+ return EXIT_FAILURE;
+
+ if (print_status_database ("notmuch config", notmuch,
+ notmuch_database_close (notmuch)))
+ return EXIT_FAILURE;
+
+ return EXIT_SUCCESS;
+}
+
+static int
+notmuch_config_command_set (notmuch_database_t *notmuch,
+ int argc, char *argv[])
+{
+ char *group, *key;
+ config_key_info_t *key_info;
+ notmuch_conffile_t *config;
+ bool update_database = false;
+ int opt_index, ret;
+ char *item;
+
+ notmuch_opt_desc_t options[] = {
+ { .opt_bool = &update_database, .name = "database" },
+ { }
+ };
+
+ opt_index = parse_arguments (argc, argv, options, 1);
+ if (opt_index < 0)
+ return EXIT_FAILURE;
+
+ argc -= opt_index;
+ argv += opt_index;
+
+ if (argc < 1) {
+ fprintf (stderr, "Error: notmuch config set requires at least "
+ "one argument.\n");
+ return EXIT_FAILURE;
+ }
+
+ item = argv[0];
+ argv++;
+ argc--;
+
+ if (STRNCMP_LITERAL (item, BUILT_WITH_PREFIX) == 0) {
+ fprintf (stderr, "Error: read only option: %s\n", item);
+ return 1;
+ }
+
+ key_info = _config_key_info (item);
+ if (key_info && key_info->validate && (! key_info->validate (item)))
+ return 1;
+
+ if (update_database) {
+ return _set_db_config (notmuch, item, argc, argv);
+ }
+
+ if (_item_split (item, &group, &key))
+ return 1;
+
+ config = notmuch_conffile_open (notmuch,
+ notmuch_config_path (notmuch), false);
+ if (! config)
+ return 1;
+
+ /* With only the name of an item, we clear it from the
+ * configuration file.
+ *
+ * With a single value, we set it as a string.
+ *
+ * With multiple values, we set them as a string list.
+ */
+ switch (argc) {
+ case 0:
+ g_key_file_remove_key (config->key_file, group, key, NULL);
+ break;
+ case 1:
+ g_key_file_set_string (config->key_file, group, key, argv[0]);
+ break;
+ default:
+ g_key_file_set_string_list (config->key_file, group, key,
+ (const gchar **) argv, argc);
+ break;