From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp11.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id 0Dh2JlHNqWJG1gAAbAwnHQ (envelope-from ) for ; Wed, 15 Jun 2022 14:15:13 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp11.migadu.com with LMTPS id 4GBhJlHNqWK9SwAA9RJhRA (envelope-from ) for ; Wed, 15 Jun 2022 14:15:13 +0200 Received: from mail.notmuchmail.org (yantan.tethera.net [IPv6:2a01:4f9:c011:7a79::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id F2D211B743 for ; Wed, 15 Jun 2022 14:15:10 +0200 (CEST) Received: from yantan.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 1C68C5E537; Wed, 15 Jun 2022 12:15:02 +0000 (UTC) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 81FA25E226 for ; Wed, 15 Jun 2022 12:14:57 +0000 (UTC) Received: by fethera.tethera.net (Postfix, from userid 1001) id E2FCE5FC01; Wed, 15 Jun 2022 08:14:56 -0400 (EDT) Received: (nullmailer pid 1465806 invoked by uid 1000); Wed, 15 Jun 2022 12:14:54 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [PATCH v2 2/2] lib/sexp: add parameter expansion for regex and wildcard Date: Wed, 15 Jun 2022 09:14:47 -0300 Message-Id: <20220615121447.1465635-3-david@tethera.net> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20220615121447.1465635-1-david@tethera.net> References: <20220615121447.1465635-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: DXDAJ2JNNRXIQIAN7Z6EB5FKDLELA5KS X-Message-ID-Hash: DXDAJ2JNNRXIQIAN7Z6EB5FKDLELA5KS 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; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: DE ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1655295311; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to: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-owner:list-unsubscribe:list-subscribe:list-post; bh=2X/9e6l1K5JHFwohaWPzwR4ijujjsa0JVq3pA61KEAs=; b=F1SkNUt9WT65KHL1XulQwadyHA9Bc2CwQzzkviB8Ms6C6L7kP1ZjIU0odhyLlKL5OoCOrF xYvlGziIXkSyu8/P6ZeyDWIIjLr1CZFgMZ28/dX3sGhlws4p+m+La9NkURvgK9gpN2bLYN f7kFfGSK88MCkkNaF5YIzvdQ/BJUy+twkxU1HGgMmxCsva9/qCirYwE36AE5YDClidyVAA wtolYgvtq9FJZmqen7Fdmz26lPAIsJsdIv+oYNe80G0Nmuyid+0qTkkfOQ3LqJK2QAoSa/ u0eRo5z6ZNaJ1VmgBJzmWizGWimokj3R1zbrUIcC7iwD+UqMXKggQAz6hMQ6qA== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1655295311; a=rsa-sha256; cv=none; b=CNX80Ch8S87dv264+vAMhvsa3E3ulK+0XHeOe9ePSuMXihtDxBSpkHULrBekgdNF/TVa6n mNeKNlrPJ2ZIrJIXRnz1O8rubcQL9JrPBA3Wq0v7Q77vSr0WaMsG3uwj4Nc9BOapD2qqfM vzh0QqFgfDncyM4dEWx8aleGV8Jv0qA8JqrLx9rCpN1NG8QVJXOqoaJ4SsOSxjpT09U60y G/vxpdyMYdRv1BBS686FBC70yYgYUC9NQTeFSbRtXtc2SfAJl7ZArzg3ubd2+5V5Uif/KI 3XSQdmNzwZnX8DyuHEYuKftjC/tdzVTKD1RlWMGubyoRj5tCN3Y0YAoNNWyz+A== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.62 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: F2D211B743 X-Spam-Score: -1.62 X-Migadu-Scanner: scn0.migadu.com X-TUID: rTpmvAt9s78j Fix the bug reported at [1]. The parameter expansion for regex and wildcard modifiers has to be done a bit differently, because their arguments are not s-expressions defining complete Xapian queries. [1]: id:87o7yxqxy6.fsf@code.pm --- lib/parse-sexp.cc | 91 +++++++++++++++++++++++++++++++++------ test/T081-sexpr-search.sh | 18 ++++---- 2 files changed, 87 insertions(+), 22 deletions(-) diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc index 08fd7037..ef55812d 100644 --- a/lib/parse-sexp.cc +++ b/lib/parse-sexp.cc @@ -187,6 +187,55 @@ _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query & return NOTMUCH_STATUS_SUCCESS; } +static notmuch_status_t +resolve_binding (notmuch_database_t *notmuch, const _sexp_binding_t *env, const char *name, + const _sexp_binding_t **out) +{ + for (; env; env = env->next) { + if (strcmp (name, env->name) == 0) { + *out = env; + return NOTMUCH_STATUS_SUCCESS; + } + } + + _notmuch_database_log (notmuch, "undefined parameter '%s'\n", name); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; +} + +static notmuch_status_t +_sexp_expand_term (notmuch_database_t *notmuch, + const _sexp_prefix_t *prefix, + const _sexp_binding_t *env, + const sexp_t *sx, + const char **out) +{ + notmuch_status_t status; + + if (! out) + return NOTMUCH_STATUS_NULL_POINTER; + + while (sx->ty == SEXP_VALUE && sx->aty == SEXP_BASIC && sx->val[0] == ',') { + const char *name = sx->val + 1; + const _sexp_binding_t *binding; + + status = resolve_binding (notmuch, env, name, &binding); + if (status) + return status; + + sx = binding->sx; + env = binding->context; + } + + if (sx->ty != SEXP_VALUE) { + _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n", + prefix->name); + return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + } + + *out = sx->val; + return NOTMUCH_STATUS_SUCCESS; +} + static notmuch_status_t _sexp_parse_wildcard (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, @@ -227,8 +276,8 @@ _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, cons notmuch_status_t _sexp_parse_regex (notmuch_database_t *notmuch, const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent, - unused(const _sexp_binding_t *env), - std::string val, Xapian::Query &output) + const _sexp_binding_t *env, + const sexp_t *term, Xapian::Query &output) { if (! parent) { _notmuch_database_log (notmuch, "illegal '%s' outside field\n", @@ -243,9 +292,15 @@ _sexp_parse_regex (notmuch_database_t *notmuch, } std::string msg; /* ignored */ + const char *str; + notmuch_status_t status; + + status = _sexp_expand_term (notmuch, prefix, env, term, &str); + if (status) + return status; return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name, - val, output, msg); + str, output, msg); } @@ -444,14 +499,16 @@ _sexp_expand_param (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const _sexp_binding_t *env, const char *name, Xapian::Query &output) { - for (; env; env = env->next) { - if (strcmp (name, env->name) == 0) { - return _sexp_to_xapian_query (notmuch, parent, env->context, env->sx, - output); - } - } - _notmuch_database_log (notmuch, "undefined parameter %s\n", name); - return NOTMUCH_STATUS_BAD_QUERY_SYNTAX; + notmuch_status_t status; + + const _sexp_binding_t *binding; + + status = resolve_binding (notmuch, env, name, &binding); + if (status) + return status; + + return _sexp_to_xapian_query (notmuch, parent, binding->context, binding->sx, + output); } static notmuch_status_t @@ -638,11 +695,17 @@ _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output); } - if (prefix->xapian_op == Xapian::Query::OP_WILDCARD) - return _sexp_parse_wildcard (notmuch, parent, env, sx->list->next->val, output); + if (prefix->xapian_op == Xapian::Query::OP_WILDCARD) { + const char *str; + status = _sexp_expand_term (notmuch, prefix, env, sx->list->next, &str); + if (status) + return status; + + return _sexp_parse_wildcard (notmuch, parent, env, str, output); + } if (prefix->flags & SEXP_FLAG_DO_REGEX) { - return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next->val, output); + return _sexp_parse_regex (notmuch, prefix, parent, env, sx->list->next, output); } if (prefix->flags & SEXP_FLAG_DO_EXPAND) { diff --git a/test/T081-sexpr-search.sh b/test/T081-sexpr-search.sh index d28e5b76..c089e382 100755 --- a/test/T081-sexpr-search.sh +++ b/test/T081-sexpr-search.sh @@ -1116,7 +1116,6 @@ EOF test_expect_equal_file EXPECTED OUTPUT test_begin_subtest "Saved Search: bad parameter syntax 5" -test_subtest_known_broken notmuch config set squery.Bad5 '(macro (thing) (tag (rx ,thing)))' notmuch search --query=sexp '(Bad5 (1 2))' >OUTPUT 2>&1 cat < EXPECTED @@ -1126,7 +1125,6 @@ EOF test_expect_equal_file EXPECTED OUTPUT test_begin_subtest "Saved Search: bad parameter syntax 6" -test_subtest_known_broken notmuch config set squery.Bad6 '(macro (thing) (tag (starts-with ,thing)))' notmuch search --query=sexp '(Bad6 (1 2))' >OUTPUT 2>&1 cat < EXPECTED @@ -1135,6 +1133,14 @@ notmuch search: Syntax error in query EOF test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "Saved Search: bad parameter syntax 7" +notmuch search --query=sexp '(subject (rx ,unknown))' >OUTPUT 2>&1 +cat < EXPECTED +notmuch search: Syntax error in query +undefined parameter 'unknown' +EOF +test_expect_equal_file EXPECTED OUTPUT + test_begin_subtest "Saved Search: macro without body" notmuch config set squery.Bad3 '(macro (a b))' notmuch search --query=sexp '(Bad3)' >OUTPUT 2>&1 @@ -1164,7 +1170,7 @@ notmuch config set squery.Bad6 '(macro (a) (and ,b (subject maildir)))' notmuch search --query=sexp '(Bad6 foo)' >OUTPUT 2>&1 cat < EXPECTED notmuch search: Syntax error in query -undefined parameter b +undefined parameter 'b' EOF test_expect_equal_file EXPECTED OUTPUT @@ -1187,14 +1193,12 @@ notmuch search --query=sexp '(TagSubject2 inbox maildir)' | notmuch_search_sanit test_expect_equal_file EXPECTED OUTPUT test_begin_subtest "macro in regex" -test_subtest_known_broken notmuch search tag:inbox and date:2009-11-17 | notmuch_search_sanitize > EXPECTED notmuch config set squery.D '(macro (tagname) (and (date 2009-11-17) (tag (rx ,tagname))))' notmuch search --query=sexp '(D inbo)' | notmuch_search_sanitize > OUTPUT test_expect_equal_file_nonempty EXPECTED OUTPUT test_begin_subtest "macro in wildcard" -test_subtest_known_broken notmuch search tag:inbox and date:2009-11-17 | notmuch_search_sanitize > EXPECTED notmuch config set squery.W '(macro (tagname) (and (date 2009-11-17) (tag (starts-with ,tagname))))' notmuch search --query=sexp '(W inbo)' | notmuch_search_sanitize > OUTPUT @@ -1213,12 +1217,11 @@ notmuch config set squery.Outer2 '(macro (x y) (and (tag ,x) (Inner2 ,y)))' notmuch search --query=sexp '(Outer2 inbox maildir)' > OUTPUT 2>&1 cat < EXPECTED notmuch search: Syntax error in query -undefined parameter y +undefined parameter 'y' EOF test_expect_equal_file EXPECTED OUTPUT test_begin_subtest "nested macros (shadowing, regex)" -test_subtest_known_broken notmuch search tag:/inbo/ and subject:/Maildi/ | notmuch_search_sanitize > EXPECTED notmuch config set squery.Inner3 '(macro (x) (subject (rx ,x)))' notmuch config set squery.Outer3 '(macro (x y) (and (tag (rx ,x)) (Inner3 ,y)))' @@ -1226,7 +1229,6 @@ notmuch search --query=sexp '(Outer3 inbo Maildi)' | notmuch_search_sanitize > O test_expect_equal_file_nonempty EXPECTED OUTPUT test_begin_subtest "nested macros (shadowing, wildcard)" -test_subtest_known_broken notmuch search tag:inbox and subject:maildir | notmuch_search_sanitize > EXPECTED notmuch config set squery.Inner4 '(macro (x) (subject (starts-with ,x)))' notmuch config set squery.Outer4 '(macro (x y) (and (tag (starts-with ,x)) (Inner4 ,y)))' -- 2.35.2