Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 83905431FAF for ; Sat, 24 Nov 2012 05:21:12 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0.201 X-Spam-Level: X-Spam-Status: No, score=0.201 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id Ze9zCkWgspL2 for ; Sat, 24 Nov 2012 05:21:10 -0800 (PST) Received: from mail-wg0-f45.google.com (mail-wg0-f45.google.com [74.125.82.45]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 822C5431FC0 for ; Sat, 24 Nov 2012 05:21:05 -0800 (PST) Received: by mail-wg0-f45.google.com with SMTP id dq11so3973532wgb.2 for ; Sat, 24 Nov 2012 05:21:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=FCVh/gxBXG1O6Lt2mYXnoXzaGl1oLD6pAd/1YfJkoYQ=; b=P3zP59r3xa6mMS8lmvIgf0p3w9QvSstl9xWYH+x+cQS2TA0tAClDAW35vOvgGp0Ss2 Ybv6gtR26rT6LN1iLFPyBmb0WjDftRAL3Vlv9Ci4q4yhqoVgM6/b+Zug86tJY9qTlvdf G7WTUafRTZGpiScp8JtyOXRHjrJmH3dD6yQZveEMo4QqcQdrwZD39My6VVbkeAZlM0RK rbBOqIEw5/AaVJiQbrWK0fB9l7x/cEuuJHRyyzbgId7YAgpiTiBVIHM2O1pRtb8jQVsM LAq0MsjZBCgApGg6sCiohiT+KOHzb3Ge5W+2uEeyNi4QAVg67ooc75o1rGLXAeqAq9Br YNzw== Received: by 10.180.86.7 with SMTP id l7mr10271711wiz.5.1353763262987; Sat, 24 Nov 2012 05:21:02 -0800 (PST) Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31]) by mx.google.com with ESMTPS id gz3sm12685328wib.2.2012.11.24.05.21.01 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 24 Nov 2012 05:21:02 -0800 (PST) From: markwalters1009 To: notmuch@notmuchmail.org Subject: [PATCH v2 1/7] cli: allow query to come from stdin Date: Sat, 24 Nov 2012 13:20:50 +0000 Message-Id: <1353763256-32336-2-git-send-email-markwalters1009@gmail.com> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1353763256-32336-1-git-send-email-markwalters1009@gmail.com> References: <1353763256-32336-1-git-send-email-markwalters1009@gmail.com> X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 24 Nov 2012 13:21:13 -0000 From: Mark Walters After this series there will be times when a caller will want to pass a very large query string to notmuch (eg a list of 10,000 message-ids) and this can exceed the size of ARG_MAX. Hence allow notmuch to take the query from stdin (if the query is -). --- query-string.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 files changed, 41 insertions(+), 0 deletions(-) diff --git a/query-string.c b/query-string.c index 6536512..b1fbdeb 100644 --- a/query-string.c +++ b/query-string.c @@ -20,6 +20,44 @@ #include "notmuch-client.h" +/* Read a single query string from STDIN, using + * 'ctx' as the talloc owner for all allocations. + * + * This function returns NULL in case of insufficient memory or read + * errors. + */ +static char * +query_string_from_stdin (void *ctx) +{ + char *query_string; + char buf[4096]; + ssize_t remain; + + query_string = talloc_strdup (ctx, ""); + if (query_string == NULL) + return NULL; + + for (;;) { + remain = read (STDIN_FILENO, buf, sizeof(buf) - 1); + if (remain == 0) + break; + if (remain < 0) { + if (errno == EINTR) + continue; + fprintf (stderr, "Error: reading from standard input: %s\n", + strerror (errno)); + return NULL; + } + + buf[remain] = '\0'; + query_string = talloc_strdup_append (query_string, buf); + if (query_string == NULL) + return NULL; + } + + return query_string; +} + /* Construct a single query string from the passed arguments, using * 'ctx' as the talloc owner for all allocations. * @@ -35,6 +73,9 @@ query_string_from_args (void *ctx, int argc, char *argv[]) char *query_string; int i; + if ((argc == 1) && (strcmp ("-", argv[0]) == 0)) + return query_string_from_stdin (ctx); + query_string = talloc_strdup (ctx, ""); if (query_string == NULL) return NULL; -- 1.7.9.1