From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id KP/gGpbaQGB1bgAA0tVLHw (envelope-from ) for ; Thu, 04 Mar 2021 13:03:18 +0000 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id +CG2FpbaQGCjIAAA1q6Kng (envelope-from ) for ; Thu, 04 Mar 2021 13:03:18 +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 CC4C422193 for ; Thu, 4 Mar 2021 14:03:17 +0100 (CET) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 9F2272757D; Thu, 4 Mar 2021 08:02:14 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 067122705A for ; Thu, 4 Mar 2021 08:01:57 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id F1B275FA96; Thu, 4 Mar 2021 08:01:56 -0500 (EST) Received: (nullmailer pid 4047173 invoked by uid 1000); Thu, 04 Mar 2021 13:01:45 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 06/27] lib/config: add config_pairs iterators Date: Thu, 4 Mar 2021 09:01:18 -0400 Message-Id: <20210304130139.4046183-7-david@tethera.net> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210304130139.4046183-1-david@tethera.net> References: <20210304130139.4046183-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: JYA6RG6HFOJQFDE2X5KFDU3VVBSQSYIR X-Message-ID-Hash: JYA6RG6HFOJQFDE2X5KFDU3VVBSQSYIR 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=1614862998; 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=Wz4OICs9iTtR4RsiUhMnaJaPbjK0UgEVWDPqw4ukc28=; b=Li1oarnYQVlIdiCbwECKCjB5VnrZ+onk58E0FSvq52I8EoiqKjsdasPRTnc8SXG62u3fVA cssw9yLiVBEYg5regsbsNgMsUeaHVDnu4FDR/tdLgHlTuaSwUsGVTW/fKXrONVtwK5chym BuuDde3QbD8gSctwHvvut+AlUCQDw7yqWZFp+ZULXiVLKNlmTCECf2It2gma365pIfjAA6 xOw/scF25rryD9Tergwaa5B7VwJhZ+VSqXi5zUcDNZzRpI2V4PvJrMYcUDwjlle5EOXS2o F/olImyRQTf6N97RSMRC5LdjkZ0EzGXGtNeX5VxiTHAf1iq2dMeNviwBiv+gDQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1614862998; a=rsa-sha256; cv=none; b=N36vgRW5lAHoopUm4RpUxbgmKOlFxX5vlEHezKvTC3+MYgVLENPdX+Ece9OgXHDONQLBtp 70z+oU74A1ksbycW3Unrm2rlw5Ax82VCUPE6RCMAimMBW1dJUPpaD5dx30E3k6AbZUHha0 mQd73ZqssvrgYqua/X8tjzBMa6PM7o1AS7Jy6GNUjYVsT9Eep9MlKK3UA8mmFl7GCxKG4g hOz4Y2cOyQvS4Jr4KOL3nnTobiX+1/k7/vr0ouXorErHAVlHQup2U9GT32LZgPsa+3XgiI 9Wh+DceslcDIuC12YjW+E+vfw3lP/uSq5U9TDsMW9jWSlu9GVNWWA8gK0i8+pA== 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.88 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: CC4C422193 X-Spam-Score: -0.88 X-Migadu-Scanner: scn1.migadu.com X-TUID: aJhD02C+46P5 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 | 39 ++++++++++++++++++++++++++++++ lib/notmuch.h | 21 ++++++++++++++++ test/T590-libconfig.sh | 54 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) diff --git a/lib/config.cc b/lib/config.cc index cec2e9de..de919b87 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 @@ -331,6 +335,41 @@ 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 9a4f94f5..f050d2b6 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__ */ @@ -2638,6 +2639,26 @@ notmuch_config_values_start (notmuch_config_values_t *values); void notmuch_config_values_destroy (notmuch_config_values_t *values); + +notmuch_config_pairs_t * +notmuch_config_get_pairs (notmuch_database_t *notmuch, + const char *prefix); + +notmuch_bool_t +notmuch_config_pairs_valid (notmuch_config_pairs_t *pairs); + +void +notmuch_config_pairs_move_to_next (notmuch_config_pairs_t *pairs); + +const char * +notmuch_config_pairs_key (notmuch_config_pairs_t *pairs); + +const char * +notmuch_config_pairs_value (notmuch_config_pairs_t *pairs); + +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.1