From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp1 ([2001:41d0:2:bcc0::]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id kNZNMq4RbWBzHwEAgWs5BA (envelope-from ) for ; Wed, 07 Apr 2021 03:58:06 +0200 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp1 with LMTPS id UHI+LK4RbWCaSAAAbx9fmQ (envelope-from ) for ; Wed, 07 Apr 2021 01:58:06 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 5813BAEB0 for ; Wed, 7 Apr 2021 03:58:06 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 4F6DC290C8; Tue, 6 Apr 2021 21:56:49 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id DFD8C2905E for ; Tue, 6 Apr 2021 21:56:02 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id D8E015FA96; Tue, 6 Apr 2021 21:56:02 -0400 (EDT) Received: (nullmailer pid 2964219 invoked by uid 1000); Wed, 07 Apr 2021 01:55:35 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 07/28] lib/config: add config_pairs iterators Date: Tue, 6 Apr 2021 22:55:09 -0300 Message-Id: <20210407015530.2964017-8-david@tethera.net> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210407015530.2964017-1-david@tethera.net> References: <20210407015530.2964017-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: H5D44UPTIN6AHFAU3EVYEKSJ4E6FQ5TJ X-Message-ID-Hash: H5D44UPTIN6AHFAU3EVYEKSJ4E6FQ5TJ X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1617760686; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=Y3kqGsBkHZfjhcJytk8+OZI6tnO1OYslHyaHQ30GCI4=; b=gtETLmY3ElBXC8uQU0bQefl0LNhWJRGFhPS5/TVSdDTCW6jWEDzmEyJnToXLE8mlM5jCpr X9FJLbQiBTNd93nKslHx3LvAnp6ZEwb4MWwkaWCm1tWr0EAcqlQM3Xw/lv6Jkr3KSpV6pU WjqYZGtJKfObXefAHHriqKPlFRau5Oc3kd3MShsEcM3idFKz0NKTHGnWzdImKUHKgG1ubR RpHkAT9aGQd2uBA0I2FVSMfVPTA81eRqvSE4nHDdG+Ih2hJZwyhTj491jY45+a7sPb/wzA G2h98PQLq0y4lPtz4FGGgxYrZ4LX5YYiuXsxRtoX0g/+ALmsdscSYtbW6Osygg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1617760686; a=rsa-sha256; cv=none; b=GFvStE8QFb/KlpGH/6DwTav4m65DvdBnlgQtsW6mXD4xx3Fule2Yo+qQS1Y9q8JB7opTbm TvAsALchh7/TL4cmU/KVXSo1bCdpJ1RanzfSATOJLSZDxcDpsGiYAhGG8innUJh7caFXkH Igl2adQQdTlzYqTfAq4tQfl0v8pnfX9WQY3wETy5Amvr5eptAvInM1uankTK1LkpQLVbW/ g8IPYTuLfSqElmstJ9YPz0vNCDARtI4FLc8jZ3yaeWIjwfhE9K0XTNw6Mu3evlw+v5dPMU tgHppIrV3fRNtyyWqZDoJ87eiCvFAnIKTKuE2zhub6UZVOD/PAjzP3O0rvPGEg== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -0.99 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 5813BAEB0 X-Spam-Score: -0.99 X-Migadu-Scanner: scn0.migadu.com X-TUID: tSYiGU11FCTF The layer of shims here seems a bit wasteful compared to just calling the corresponding string map functions directly, but it allows control over the API (calling with notmuch_database_t *) and flexibility for future changes. --- lib/config.cc | 45 +++++++++++++++++++++++++ lib/notmuch.h | 75 ++++++++++++++++++++++++++++++++++++++++++ test/T590-libconfig.sh | 54 ++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+) diff --git a/lib/config.cc b/lib/config.cc index 24418f98..ab45ae7b 100644 --- a/lib/config.cc +++ b/lib/config.cc @@ -38,6 +38,10 @@ struct _notmuch_config_values { void *children; /* talloc_context */ }; +struct _notmuch_config_pairs { + notmuch_string_map_iterator_t *iter; +}; + static const char *_notmuch_config_key_to_string (notmuch_config_key_t key); static int @@ -339,6 +343,47 @@ notmuch_config_values_destroy (notmuch_config_values_t *values) talloc_free (values); } +notmuch_config_pairs_t * +notmuch_config_get_pairs (notmuch_database_t *notmuch, + const char *prefix) +{ + notmuch_config_pairs_t *pairs = talloc (notmuch, notmuch_config_pairs_t); + + pairs->iter = _notmuch_string_map_iterator_create (notmuch->config, prefix, false); + return pairs; +} + +notmuch_bool_t +notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs) +{ + return _notmuch_string_map_iterator_valid (pairs->iter); +} + +void +notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs) +{ + _notmuch_string_map_iterator_move_to_next (pairs->iter); +} + +const char * +notmuch_config_pairs_key (notmuch_config_pairs_t *pairs) +{ + return _notmuch_string_map_iterator_key (pairs->iter); +} + +const char * +notmuch_config_pairs_value (notmuch_config_pairs_t *pairs) +{ + return _notmuch_string_map_iterator_value (pairs->iter); +} + +void +notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs) +{ + _notmuch_string_map_iterator_destroy (pairs->iter); + talloc_free (pairs); +} + notmuch_status_t _notmuch_config_load_from_file (notmuch_database_t *notmuch, GKeyFile *file) diff --git a/lib/notmuch.h b/lib/notmuch.h index dd3d06a1..2a013be0 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -245,6 +245,7 @@ typedef struct _notmuch_directory notmuch_directory_t; typedef struct _notmuch_filenames notmuch_filenames_t; typedef struct _notmuch_config_list notmuch_config_list_t; typedef struct _notmuch_config_values notmuch_config_values_t; +typedef struct _notmuch_config_pairs notmuch_config_pairs_t; typedef struct _notmuch_indexopts notmuch_indexopts_t; #endif /* __DOXYGEN__ */ @@ -2639,6 +2640,80 @@ notmuch_config_values_start (notmuch_config_values_t *values); void notmuch_config_values_destroy (notmuch_config_values_t *values); + +/** + * Returns an iterator for a (key, value) configuration pairs + * + * @param[in] notmuch database + * @param[in] prefix prefix for keys. Pass "" for all keys. + * + * @since libnotmuch 5.4 (notmuch 0.32) + * + * @retval NULL in case of error. + */ +notmuch_config_pairs_t * +notmuch_config_get_pairs (notmuch_database_t *notmuch, + const char *prefix); + +/** + * Is the given 'config_pairs' iterator pointing at a valid element. + * + * @param[in] pairs iterator + * + * @since libnotmuch 5.4 (notmuch 0.32) + * + * @retval FALSE if passed a NULL pointer, or the iterator is exhausted. + * + */ +notmuch_bool_t +notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs); + +/** + * Move the 'config_pairs' iterator to the next element + * + * @param[in,out] pairs iterator + * + * @since libnotmuch 5.4 (notmuch 0.32) + * + */ +void +notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs); + +/** + * Get the current key from the 'config_pairs' iterator + * + * @param[in] pairs iterator + * + * @since libnotmuch 5.4 (notmuch 0.32) + * + * @retval a string with the same lifetime as the iterator + */ +const char * +notmuch_config_pairs_key (notmuch_config_pairs_t *pairs); + +/** + * Get the current value from the 'config_pairs' iterator + * + * @param[in] pairs iterator + * + * @since libnotmuch 5.4 (notmuch 0.32) + * + * @retval a string with the same lifetime as the iterator + */ +const char * +notmuch_config_pairs_value (notmuch_config_pairs_t *pairs); + +/** + * Destroy a config_pairs iterator, along with any associated + * resources. + * + * @param[in,out] pairs iterator + * + * @since libnotmuch 5.4 (notmuch 0.32) + */ +void +notmuch_config_pairs_destroy (notmuch_config_pairs_t *pairs); + /** * get a configuration value from an open database as Boolean * diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh index edbba6c4..49cc382a 100755 --- a/test/T590-libconfig.sh +++ b/test/T590-libconfig.sh @@ -770,5 +770,59 @@ EOF test_expect_equal_file EXPECTED OUTPUT restore_database +test_begin_subtest "notmuch_config_get_pairs: prefix (ndlc)" +cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL% +{ + notmuch_config_pairs_t *list; + for (list = notmuch_config_get_pairs (db, "user."); + notmuch_config_pairs_valid (list); + notmuch_config_pairs_move_to_next (list)) { + printf("%s %s\n", notmuch_config_pairs_key (list), notmuch_config_pairs_value(list)); + } + notmuch_config_pairs_destroy (list); +} +EOF +cat <<'EOF' >EXPECTED +== stdout == +user.name Notmuch Test Suite +user.other_email test_suite_other@notmuchmail.org;test_suite@otherdomain.org +user.primary_email test_suite@notmuchmail.org +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "notmuch_config_get_pairs: all pairs (ndlc)" +cat c_head2 - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} %NULL% +{ + notmuch_config_pairs_t *list; + for (list = notmuch_config_get_pairs (db, ""); + notmuch_config_pairs_valid (list); + notmuch_config_pairs_move_to_next (list)) { + printf("%s %s\n", notmuch_config_pairs_key (list), notmuch_config_pairs_value(list)); + } + notmuch_config_pairs_destroy (list); +} +EOF +cat <<'EOF' >EXPECTED +== stdout == +aaabefore beforeval +database.backup_dir MAIL_DIR/.notmuch/backups +database.hook_dir MAIL_DIR/.notmuch/hooks +database.mail_root MAIL_DIR +database.path MAIL_DIR +key with spaces value, with, spaces! +maildir.synchronize_flags true +new.ignore sekrit_junk +new.tags unread;inbox; +search.exclude_tags foo;bar;fub +test.key1 testvalue1 +test.key2 testvalue2 +user.name Notmuch Test Suite +user.other_email test_suite_other@notmuchmail.org;test_suite@otherdomain.org +user.primary_email test_suite@notmuchmail.org +zzzafter afterval +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT test_done -- 2.30.2