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 7203A431FBC for ; Fri, 15 Jan 2010 02:14:00 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: -2.825 X-Spam-Level: X-Spam-Status: No, score=-2.825 tagged_above=-999 required=5 tests=[AWL=1.174, BAYES_50=0.001, RCVD_IN_DNSWL_MED=-4] autolearn=ham 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 rBMqHZMtURkN for ; Fri, 15 Jan 2010 02:13:59 -0800 (PST) Received: from fmsmga101.fm.intel.com (mga05.intel.com [192.55.52.89]) by olra.theworths.org (Postfix) with ESMTP id 3F05F431FAE for ; Fri, 15 Jan 2010 02:13:59 -0800 (PST) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP; 15 Jan 2010 02:13:37 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="4.49,281,1262592000"; d="scan'208";a="764481457" Received: from unknown (HELO localhost.localdomain) ([10.255.17.127]) by fmsmga001.fm.intel.com with ESMTP; 15 Jan 2010 02:13:55 -0800 From: Chris Wilson To: notmuch@notmuchmail.org Date: Fri, 15 Jan 2010 10:13:50 +0000 Message-Id: <1263550430-14223-1-git-send-email-chris@chris-wilson.co.uk> X-Mailer: git-send-email 1.6.6 Subject: [notmuch] [PATCH] [RFC] notmuch search: Return a non-zero exitcode if the search returns no hits. 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: Fri, 15 Jan 2010 10:14:00 -0000 If the user is explicitly searching for a message, then if notmuch fails to find it, it is useful to set a failure exit code. This makes it easier for scripting. However, we will want to then distinguish between fatal errors (such as out-of-memory, invalid arguments, corrupt db, etc) from the simple no message. To that end, we introduce a set of enumerated exit-codes which may prove useful to use consistently across notmuch. (I'm not that convinced highly differentiated errors is particularly useful, though separate CONFIG_ERROR and DATABASE_ERROR would seem to have value for scripts.) --- notmuch-search.c | 34 ++++++++++++++++++++++++---------- 1 files changed, 24 insertions(+), 10 deletions(-) diff --git a/notmuch-search.c b/notmuch-search.c index dc44eb6..6ca903b 100644 --- a/notmuch-search.c +++ b/notmuch-search.c @@ -20,7 +20,17 @@ #include "notmuch-client.h" -static void +/* XXX After some review, make these universal? */ +enum { + NOTMUCH_EXIT_SUCCESS = 0, + NOTMUCH_EXIT_NOT_FOUND, + NOTMUCH_EXIT_INVALID_ARGUMENT, + NOTMUCH_EXIT_SYSTEM_ERROR, /* XXX just out-of-memory? */ + NOTMUCH_EXIT_CONFIG_ERROR, + NOTMUCH_EXIT_DATABASE_ERROR, +}; + +static int do_search_threads (const void *ctx, notmuch_query_t *query, notmuch_sort_t sort) @@ -30,6 +40,7 @@ do_search_threads (const void *ctx, notmuch_tags_t *tags; time_t date; const char *relative_date; + int count = 0; for (threads = notmuch_query_search_threads (query); notmuch_threads_has_more (threads); @@ -67,7 +78,10 @@ do_search_threads (const void *ctx, printf (")\n"); notmuch_thread_destroy (thread); + count++; } + + return count; } int @@ -94,11 +108,11 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) sort = NOTMUCH_SORT_NEWEST_FIRST; } else { fprintf (stderr, "Invalid value for --sort: %s\n", opt); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } } else { fprintf (stderr, "Unrecognized option: %s\n", argv[i]); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } } @@ -107,35 +121,35 @@ notmuch_search_command (void *ctx, int argc, char *argv[]) config = notmuch_config_open (ctx, NULL, NULL); if (config == NULL) - return 1; + return NOTMUCH_EXIT_CONFIG_ERROR; notmuch = notmuch_database_open (notmuch_config_get_database_path (config), NOTMUCH_DATABASE_MODE_READ_ONLY); if (notmuch == NULL) - return 1; + return NOTMUCH_EXIT_DATABASE_ERROR; query_str = query_string_from_args (ctx, argc, argv); if (query_str == NULL) { fprintf (stderr, "Out of memory.\n"); - return 1; + return NOTMUCH_EXIT_SYSTEM_ERROR; } if (*query_str == '\0') { fprintf (stderr, "Error: notmuch search requires at least one search term.\n"); - return 1; + return NOTMUCH_EXIT_INVALID_ARGUMENT; } query = notmuch_query_create (notmuch, query_str); if (query == NULL) { fprintf (stderr, "Out of memory\n"); - return 1; + return NOTMUCH_EXIT_SYSTEM_ERROR; } notmuch_query_set_sort (query, sort); - do_search_threads (ctx, query, sort); + i = do_search_threads (ctx, query, sort); notmuch_query_destroy (query); notmuch_database_close (notmuch); - return 0; + return i ? NOTMUCH_EXIT_SUCCESS : NOTMUCH_EXIT_NOT_FOUND; } -- 1.6.6