From 32dac1a8fac3bf936b8ab9c8ab0704299f230fa4 Mon Sep 17 00:00:00 2001 From: Jani Nikula Date: Fri, 10 Jan 2014 23:28:53 +0200 Subject: [PATCH] [PATCH 1/2] cli: clean up exit status code returned by the cli commands --- a5/885277d9b20f6628dfd704d7344a762af94de2 | 936 ++++++++++++++++++++++ 1 file changed, 936 insertions(+) create mode 100644 a5/885277d9b20f6628dfd704d7344a762af94de2 diff --git a/a5/885277d9b20f6628dfd704d7344a762af94de2 b/a5/885277d9b20f6628dfd704d7344a762af94de2 new file mode 100644 index 000000000..1716ca0aa --- /dev/null +++ b/a5/885277d9b20f6628dfd704d7344a762af94de2 @@ -0,0 +1,936 @@ +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 A6E8C431FC2 + for ; Fri, 10 Jan 2014 13:29:07 -0800 (PST) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[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 20lmf2uZgZ+T for ; + Fri, 10 Jan 2014 13:29:00 -0800 (PST) +Received: from mail-ea0-f181.google.com (mail-ea0-f181.google.com + [209.85.215.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id A4F2F431FBC + for ; Fri, 10 Jan 2014 13:28:59 -0800 (PST) +Received: by mail-ea0-f181.google.com with SMTP id m10so2281661eaj.26 + for ; Fri, 10 Jan 2014 13:28:58 -0800 (PST) +X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; + d=1e100.net; s=20130820; + h=x-gm-message-state:from:to:cc:subject:date:message-id; + bh=SX+RfRQIw3coVE71IWYONU1xHZnoMEA4MMtJVPbiEdc=; + b=AQeWyk5yDuKZ6VLpfPPbJbWVFoluuEXu+fcaPUw9yArNsuWMPnsXi2IRP8Y5mKdc2T + WIYK5qYyXAkUwFkU1VmGd9M3goT85C6Rft+fcnhx+xBP/4VnBeyyUj8dwnLmApGQMX32 + OuiR/RDb014KQ3oP9G8bxZKWW6GbjnDJlWjLS1+5cW2v2zR95vM3SJATiEtw3oTlm27a + 7RVblkqvjUo4HOlp1Na4t3O0GWKI24v8sftxpurKjKHiL+QOfZEfwfcpHGJTn7Pi27JM + 1x5mx1TwVu6R4QKxb+GLMHHS5JvrQ8531/6E+PCOSLWpRy+cLyOnBpPuffigzNaSX8gI + 5ECA== +X-Gm-Message-State: + ALoCoQmm516jAhXeo/U8rvc/t2pFtHWW/lTFnyS2nyUw6hVBblE3nq9ue66aYgX9eH6ZykBWxAYv +X-Received: by 10.14.6.5 with SMTP id 5mr11854865eem.51.1389389338429; + Fri, 10 Jan 2014 13:28:58 -0800 (PST) +Received: from localhost (dsl-hkibrasgw2-58c36f-91.dhcp.inet.fi. + [88.195.111.91]) + by mx.google.com with ESMTPSA id o1sm17716294eea.10.2014.01.10.13.28.56 + for + (version=TLSv1.2 cipher=RC4-SHA bits=128/128); + Fri, 10 Jan 2014 13:28:57 -0800 (PST) +From: Jani Nikula +To: notmuch@notmuchmail.org +Subject: [PATCH 1/2] cli: clean up exit status code returned by the cli + commands +Date: Fri, 10 Jan 2014 23:28:53 +0200 +Message-Id: <1389389334-24138-1-git-send-email-jani@nikula.org> +X-Mailer: git-send-email 1.8.5.2 +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, 10 Jan 2014 21:29:07 -0000 + +Apart from the status codes for format mismatches, the non-zero exit +status codes have been arbitrary. Make the cli consistently return +either EXIT_SUCCESS or EXIT_FAILURE. +--- + notmuch-compact.c | 6 +++--- + notmuch-config.c | 23 ++++++++++++++--------- + notmuch-count.c | 16 +++++++--------- + notmuch-dump.c | 21 +++++++++------------ + notmuch-insert.c | 25 +++++++++++-------------- + notmuch-new.c | 18 ++++++++---------- + notmuch-reply.c | 20 +++++++++----------- + notmuch-restore.c | 23 +++++++++-------------- + notmuch-search.c | 18 ++++++++---------- + notmuch-setup.c | 15 +++++++-------- + notmuch-show.c | 18 ++++++++---------- + notmuch-tag.c | 22 +++++++++++----------- + notmuch.c | 43 ++++++++++++++++++++++++++++--------------- + 13 files changed, 132 insertions(+), 136 deletions(-) + +diff --git a/notmuch-compact.c b/notmuch-compact.c +index 8b820c0..5dbae29 100644 +--- a/notmuch-compact.c ++++ b/notmuch-compact.c +@@ -42,7 +42,7 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[]) + + opt_index = parse_arguments (argc, argv, options, 1); + if (opt_index < 0) +- return 1; ++ return EXIT_FAILURE; + + if (! quiet) + printf ("Compacting database...\n"); +@@ -50,7 +50,7 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[]) + quiet ? NULL : status_update_cb, NULL); + if (ret) { + fprintf (stderr, "Compaction failed: %s\n", notmuch_status_to_string (ret)); +- return 1; ++ return EXIT_FAILURE; + } + + if (! quiet) { +@@ -60,5 +60,5 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[]) + printf ("Done.\n"); + } + +- return 0; ++ return EXIT_SUCCESS; + } +diff --git a/notmuch-config.c b/notmuch-config.c +index 6845e3c..e06142b 100644 +--- a/notmuch-config.c ++++ b/notmuch-config.c +@@ -839,34 +839,39 @@ notmuch_config_command_list (notmuch_config_t *config) + int + notmuch_config_command (notmuch_config_t *config, int argc, char *argv[]) + { ++ int ret; ++ + argc--; argv++; /* skip subcommand argument */ + + if (argc < 1) { + fprintf (stderr, "Error: notmuch config requires at least one argument.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (strcmp (argv[0], "get") == 0) { + if (argc != 2) { + fprintf (stderr, "Error: notmuch config get requires exactly " + "one argument.\n"); +- return 1; ++ return EXIT_FAILURE; + } +- return notmuch_config_command_get (config, argv[1]); ++ ret = notmuch_config_command_get (config, argv[1]); + } else if (strcmp (argv[0], "set") == 0) { + if (argc < 2) { + fprintf (stderr, "Error: notmuch config set requires at least " + "one argument.\n"); +- return 1; ++ return EXIT_FAILURE; + } +- return notmuch_config_command_set (config, argv[1], argc - 2, argv + 2); ++ ret = notmuch_config_command_set (config, argv[1], argc - 2, argv + 2); + } else if (strcmp (argv[0], "list") == 0) { +- return notmuch_config_command_list (config); ++ ret = notmuch_config_command_list (config); ++ } else { ++ fprintf (stderr, "Unrecognized argument for notmuch config: %s\n", ++ argv[0]); ++ return EXIT_FAILURE; + } + +- fprintf (stderr, "Unrecognized argument for notmuch config: %s\n", +- argv[0]); +- return 1; ++ return ret ? EXIT_FAILURE : EXIT_SUCCESS; ++ + } + + notmuch_bool_t +diff --git a/notmuch-count.c b/notmuch-count.c +index 01e4e30..6058f7c 100644 +--- a/notmuch-count.c ++++ b/notmuch-count.c +@@ -150,10 +150,8 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]) + }; + + opt_index = parse_arguments (argc, argv, options, 1); +- +- if (opt_index < 0) { +- return 1; +- } ++ if (opt_index < 0) ++ return EXIT_FAILURE; + + if (input_file_name) { + batch = TRUE; +@@ -161,23 +159,23 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]) + if (input == NULL) { + fprintf (stderr, "Error opening %s for reading: %s\n", + input_file_name, strerror (errno)); +- return 1; ++ return EXIT_FAILURE; + } + } + + if (batch && opt_index != argc) { + fprintf (stderr, "--batch and query string are not compatible\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) +- return 1; ++ return EXIT_FAILURE; + + query_str = query_string_from_args (config, argc-opt_index, argv+opt_index); + if (query_str == NULL) { + fprintf (stderr, "Out of memory.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (exclude == EXCLUDE_TRUE) { +@@ -197,5 +195,5 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[]) + if (input != stdin) + fclose (input); + +- return ret; ++ return ret ? EXIT_FAILURE : EXIT_SUCCESS; + } +diff --git a/notmuch-dump.c b/notmuch-dump.c +index 2024e30..f8edda7 100644 +--- a/notmuch-dump.c ++++ b/notmuch-dump.c +@@ -35,7 +35,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) +- return 1; ++ return EXIT_FAILURE; + + char *output_file_name = NULL; + int opt_index; +@@ -52,18 +52,15 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) + }; + + opt_index = parse_arguments (argc, argv, options, 1); +- +- if (opt_index < 0) { +- /* diagnostics already printed */ +- return 1; +- } ++ if (opt_index < 0) ++ return EXIT_FAILURE; + + if (output_file_name) { + output = fopen (output_file_name, "w"); + if (output == NULL) { + fprintf (stderr, "Error opening %s for writing: %s\n", + output_file_name, strerror (errno)); +- return 1; ++ return EXIT_FAILURE; + } + } + +@@ -72,14 +69,14 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) + query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index); + if (query_str == NULL) { + fprintf (stderr, "Out of memory.\n"); +- return 1; ++ return EXIT_FAILURE; + } + } + + query = notmuch_query_create (notmuch, query_str); + if (query == NULL) { + fprintf (stderr, "Out of memory\n"); +- return 1; ++ return EXIT_FAILURE; + } + /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the + * first results quickly at the expense of total time. +@@ -131,7 +128,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) + &buffer, &buffer_size) != HEX_SUCCESS) { + fprintf (stderr, "Error: failed to hex-encode tag %s\n", + tag_str); +- return 1; ++ return EXIT_FAILURE; + } + fprintf (output, "+%s", buffer); + } +@@ -144,7 +141,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) + &buffer, &buffer_size)) { + fprintf (stderr, "Error quoting message id %s: %s\n", + message_id, strerror (errno)); +- return 1; ++ return EXIT_FAILURE; + } + fprintf (output, " -- %s\n", buffer); + } +@@ -158,5 +155,5 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[]) + notmuch_query_destroy (query); + notmuch_database_destroy (notmuch); + +- return 0; ++ return EXIT_SUCCESS; + } +diff --git a/notmuch-insert.c b/notmuch-insert.c +index 55384e3..cd6de88 100644 +--- a/notmuch-insert.c ++++ b/notmuch-insert.c +@@ -418,11 +418,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + }; + + opt_index = parse_arguments (argc, argv, options, 1); +- +- if (opt_index < 0) { +- /* diagnostics already printed */ +- return 1; +- } ++ if (opt_index < 0) ++ return EXIT_FAILURE; + + db_path = notmuch_config_get_database_path (config); + new_tags = notmuch_config_get_new_tags (config, &new_tags_length); +@@ -431,20 +428,20 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + tag_ops = tag_op_list_create (config); + if (tag_ops == NULL) { + fprintf (stderr, "Out of memory.\n"); +- return 1; ++ return EXIT_FAILURE; + } + for (i = 0; i < new_tags_length; i++) { + if (tag_op_list_append (tag_ops, new_tags[i], FALSE)) +- return 1; ++ return EXIT_FAILURE; + } + + if (parse_tag_command_line (config, argc - opt_index, argv + opt_index, + &query_string, tag_ops)) +- return 1; ++ return EXIT_FAILURE; + + if (*query_string != '\0') { + fprintf (stderr, "Error: unexpected query string: %s\n", query_string); +- return 1; ++ return EXIT_FAILURE; + } + + if (folder == NULL) { +@@ -452,17 +449,17 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + } else { + if (! check_folder_name (folder)) { + fprintf (stderr, "Error: bad folder name: %s\n", folder); +- return 1; ++ return EXIT_FAILURE; + } + maildir = talloc_asprintf (config, "%s/%s", db_path, folder); + if (! maildir) { + fprintf (stderr, "Out of memory\n"); +- return 1; ++ return EXIT_FAILURE; + } + if (create_folder && ! maildir_create_folder (config, maildir)) { + fprintf (stderr, "Error: creating maildir %s: %s\n", + maildir, strerror (errno)); +- return 1; ++ return EXIT_FAILURE; + } + } + +@@ -476,12 +473,12 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[]) + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) +- return 1; ++ return EXIT_FAILURE; + + ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops, + synchronize_flags); + + notmuch_database_destroy (notmuch); + +- return (ret) ? 0 : 1; ++ return ret ? EXIT_SUCCESS : EXIT_FAILURE; + } +diff --git a/notmuch-new.c b/notmuch-new.c +index ba05cb4..6a8b1b2 100644 +--- a/notmuch-new.c ++++ b/notmuch-new.c +@@ -898,10 +898,8 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) + }; + + opt_index = parse_arguments (argc, argv, options, 1); +- if (opt_index < 0) { +- /* diagnostics already printed */ +- return 1; +- } ++ if (opt_index < 0) ++ return EXIT_FAILURE; + + add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length); + add_files_state.new_ignore = notmuch_config_get_new_ignore (config, &add_files_state.new_ignore_length); +@@ -911,7 +909,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) + if (!no_hooks) { + ret = notmuch_run_hook (db_path, "pre-new"); + if (ret) +- return ret; ++ return EXIT_FAILURE; + } + + dot_notmuch_path = talloc_asprintf (config, "%s/%s", db_path, ".notmuch"); +@@ -922,16 +920,16 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) + count = 0; + count_files (db_path, &count, &add_files_state); + if (interrupted) +- return 1; ++ return EXIT_FAILURE; + + printf ("Found %d total files (that's not much mail).\n", count); + if (notmuch_database_create (db_path, ¬much)) +- return 1; ++ return EXIT_FAILURE; + add_files_state.total_files = count; + } else { + if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE, + ¬much)) +- return 1; ++ return EXIT_FAILURE; + + if (notmuch_database_needs_upgrade (notmuch)) { + printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n"); +@@ -946,7 +944,7 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) + } + + if (notmuch == NULL) +- return 1; ++ return EXIT_FAILURE; + + /* Setup our handler for SIGINT. We do this after having + * potentially done a database upgrade we this interrupt handler +@@ -1072,5 +1070,5 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[]) + if (!no_hooks && !ret && !interrupted) + ret = notmuch_run_hook (db_path, "post-new"); + +- return ret || interrupted; ++ return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS; + } +diff --git a/notmuch-reply.c b/notmuch-reply.c +index 9d6f843..79cdc83 100644 +--- a/notmuch-reply.c ++++ b/notmuch-reply.c +@@ -704,7 +704,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]) + notmuch_database_t *notmuch; + notmuch_query_t *query; + char *query_string; +- int opt_index, ret = 0; ++ int opt_index; + int (*reply_format_func) (void *ctx, + notmuch_config_t *config, + notmuch_query_t *query, +@@ -739,10 +739,8 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]) + }; + + opt_index = parse_arguments (argc, argv, options, 1); +- if (opt_index < 0) { +- /* diagnostics already printed */ +- return 1; +- } ++ if (opt_index < 0) ++ return EXIT_FAILURE; + + if (format == FORMAT_HEADERS_ONLY) { + reply_format_func = notmuch_reply_format_headers_only; +@@ -761,30 +759,30 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[]) + query_string = query_string_from_args (config, argc-opt_index, argv+opt_index); + if (query_string == NULL) { + fprintf (stderr, "Out of memory\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (*query_string == '\0') { + fprintf (stderr, "Error: notmuch reply requires at least one search term.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) +- return 1; ++ return EXIT_FAILURE; + + query = notmuch_query_create (notmuch, query_string); + if (query == NULL) { + fprintf (stderr, "Out of memory\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (reply_format_func (config, config, query, ¶ms, reply_all, sp) != 0) +- return 1; ++ return EXIT_FAILURE; + + notmuch_crypto_cleanup (¶ms.crypto); + notmuch_query_destroy (query); + notmuch_database_destroy (notmuch); + +- return ret; ++ return EXIT_SUCCESS; + } +diff --git a/notmuch-restore.c b/notmuch-restore.c +index 1419621..f23ab98 100644 +--- a/notmuch-restore.c ++++ b/notmuch-restore.c +@@ -140,7 +140,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) +- return 1; ++ return EXIT_FAILURE; + + if (notmuch_config_get_maildir_synchronize_flags (config)) + flags |= TAG_FLAG_MAILDIR_SYNC; +@@ -157,11 +157,8 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) + }; + + opt_index = parse_arguments (argc, argv, options, 1); +- +- if (opt_index < 0) { +- /* diagnostics already printed */ +- return 1; +- } ++ if (opt_index < 0) ++ return EXIT_FAILURE; + + if (! accumulate) + flags |= TAG_FLAG_REMOVE_ALL; +@@ -171,21 +168,19 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) + if (input == NULL) { + fprintf (stderr, "Error opening %s for reading: %s\n", + input_file_name, strerror (errno)); +- return 1; ++ return EXIT_FAILURE; + } + } + + if (opt_index < argc) { +- fprintf (stderr, +- "Unused positional parameter: %s\n", +- argv[opt_index]); +- return 1; ++ fprintf (stderr, "Unused positional parameter: %s\n", argv[opt_index]); ++ return EXIT_FAILURE; + } + + tag_ops = tag_op_list_create (config); + if (tag_ops == NULL) { + fprintf (stderr, "Out of memory.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + do { +@@ -193,7 +188,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) + + /* empty input file not considered an error */ + if (line_len < 0) +- return 0; ++ return EXIT_SUCCESS; + + } while ((line_len == 0) || + (line[0] == '#') || +@@ -275,5 +270,5 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[]) + if (input != stdin) + fclose (input); + +- return ret; ++ return ret ? EXIT_FAILURE : EXIT_SUCCESS; + } +diff --git a/notmuch-search.c b/notmuch-search.c +index 7c973b3..91b5d10 100644 +--- a/notmuch-search.c ++++ b/notmuch-search.c +@@ -401,10 +401,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + }; + + opt_index = parse_arguments (argc, argv, options, 1); +- +- if (opt_index < 0) { +- return 1; +- } ++ if (opt_index < 0) ++ return EXIT_FAILURE; + + switch (format_sel) { + case NOTMUCH_FORMAT_TEXT: +@@ -413,7 +411,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + case NOTMUCH_FORMAT_TEXT0: + if (output == OUTPUT_SUMMARY) { + fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n"); +- return 1; ++ return EXIT_FAILURE; + } + format = sprinter_text0_create (config, stdout); + break; +@@ -432,22 +430,22 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) +- return 1; ++ return EXIT_FAILURE; + + query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index); + if (query_str == NULL) { + fprintf (stderr, "Out of memory.\n"); +- return 1; ++ return EXIT_FAILURE; + } + if (*query_str == '\0') { + fprintf (stderr, "Error: notmuch search requires at least one search term.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + query = notmuch_query_create (notmuch, query_str); + if (query == NULL) { + fprintf (stderr, "Out of memory\n"); +- return 1; ++ return EXIT_FAILURE; + } + + notmuch_query_set_sort (query, sort); +@@ -491,5 +489,5 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[]) + + talloc_free (format); + +- return ret; ++ return ret ? EXIT_FAILURE : EXIT_SUCCESS; + } +diff --git a/notmuch-setup.c b/notmuch-setup.c +index 475248b..36a6171 100644 +--- a/notmuch-setup.c ++++ b/notmuch-setup.c +@@ -140,7 +140,7 @@ notmuch_setup_command (notmuch_config_t *config, + fflush (stdout); \ + if (getline (&response, &response_size, stdin) < 0) { \ + printf ("Exiting.\n"); \ +- exit (1); \ ++ exit (EXIT_FAILURE); \ + } \ + chomp_newline (response); \ + } while (0) +@@ -223,12 +223,11 @@ notmuch_setup_command (notmuch_config_t *config, + g_ptr_array_free (tags, TRUE); + } + ++ if (notmuch_config_save (config)) ++ return EXIT_FAILURE; + +- if (! notmuch_config_save (config)) { +- if (notmuch_config_is_new (config)) +- welcome_message_post_setup (); +- return 0; +- } else { +- return 1; +- } ++ if (notmuch_config_is_new (config)) ++ welcome_message_post_setup (); ++ ++ return EXIT_SUCCESS; + } +diff --git a/notmuch-show.c b/notmuch-show.c +index c07f887..528694b 100644 +--- a/notmuch-show.c ++++ b/notmuch-show.c +@@ -1113,10 +1113,8 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) + }; + + opt_index = parse_arguments (argc, argv, options, 1); +- if (opt_index < 0) { +- /* diagnostics already printed */ +- return 1; +- } ++ if (opt_index < 0) ++ return EXIT_FAILURE; + + /* decryption implies verification */ + if (params.crypto.decrypt) +@@ -1143,7 +1141,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) + case NOTMUCH_FORMAT_MBOX: + if (params.part > 0) { + fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + format = &format_mbox; +@@ -1193,22 +1191,22 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) + query_string = query_string_from_args (config, argc-opt_index, argv+opt_index); + if (query_string == NULL) { + fprintf (stderr, "Out of memory\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (*query_string == '\0') { + fprintf (stderr, "Error: notmuch show requires at least one search term.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much)) +- return 1; ++ return EXIT_FAILURE; + + query = notmuch_query_create (notmuch, query_string); + if (query == NULL) { + fprintf (stderr, "Out of memory\n"); +- return 1; ++ return EXIT_FAILURE; + } + + /* Create structure printer. */ +@@ -1242,5 +1240,5 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[]) + notmuch_query_destroy (query); + notmuch_database_destroy (notmuch); + +- return ret; ++ return ret ? EXIT_FAILURE : EXIT_SUCCESS; + } +diff --git a/notmuch-tag.c b/notmuch-tag.c +index 3b09df9..5b2f1e4 100644 +--- a/notmuch-tag.c ++++ b/notmuch-tag.c +@@ -193,7 +193,7 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[]) + FILE *input = stdin; + char *input_file_name = NULL; + int opt_index; +- int ret = 0; ++ int ret; + + /* Setup our handler for SIGINT */ + memset (&action, 0, sizeof (struct sigaction)); +@@ -211,7 +211,7 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[]) + + opt_index = parse_arguments (argc, argv, options, 1); + if (opt_index < 0) +- return 1; ++ return EXIT_FAILURE; + + if (input_file_name) { + batch = TRUE; +@@ -219,44 +219,44 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[]) + if (input == NULL) { + fprintf (stderr, "Error opening %s for reading: %s\n", + input_file_name, strerror (errno)); +- return 1; ++ return EXIT_FAILURE; + } + } + + if (batch) { + if (opt_index != argc) { + fprintf (stderr, "Can't specify both cmdline and stdin!\n"); +- return 1; ++ return EXIT_FAILURE; + } + if (remove_all) { + fprintf (stderr, "Can't specify both --remove-all and --batch\n"); +- return 1; ++ return EXIT_FAILURE; + } + } else { + tag_ops = tag_op_list_create (config); + if (tag_ops == NULL) { + fprintf (stderr, "Out of memory.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (parse_tag_command_line (config, argc - opt_index, argv + opt_index, + &query_string, tag_ops)) +- return 1; ++ return EXIT_FAILURE; + + if (tag_op_list_size (tag_ops) == 0 && ! remove_all) { + fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n"); +- return 1; ++ return EXIT_FAILURE; + } + + if (*query_string == '\0') { + fprintf (stderr, "Error: notmuch tag requires at least one search term.\n"); +- return 1; ++ return EXIT_FAILURE; + } + } + + if (notmuch_database_open (notmuch_config_get_database_path (config), + NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much)) +- return 1; ++ return EXIT_FAILURE; + + if (notmuch_config_get_maildir_synchronize_flags (config)) + tag_flags |= TAG_FLAG_MAILDIR_SYNC; +@@ -274,5 +274,5 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[]) + if (input != stdin) + fclose (input); + +- return ret || interrupted; ++ return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS; + } +diff --git a/notmuch.c b/notmuch.c +index 54f46c6..2d7f33d 100644 +--- a/notmuch.c ++++ b/notmuch.c +@@ -22,6 +22,12 @@ + + #include "notmuch-client.h" + ++/* ++ * Notmuch subcommand hook. ++ * ++ * The return value will be used as notmuch exit status code, ++ * preferrably EXIT_SUCCESS or EXIT_FAILURE. ++ */ + typedef int (*command_function_t) (notmuch_config_t *config, int argc, char *argv[]); + + typedef struct command { +@@ -156,7 +162,7 @@ notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]) + if (argc == 0) { + printf ("The notmuch mail system.\n\n"); + usage (stdout); +- return 0; ++ return EXIT_SUCCESS; + } + + if (strcmp (argv[0], "help") == 0) { +@@ -165,7 +171,7 @@ notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]) + "\tof difficulties check that MANPATH includes the pages\n" + "\tinstalled by notmuch.\n\n" + "\tTry \"notmuch help\" for a list of topics.\n"); +- return 0; ++ return EXIT_SUCCESS; + } + + command = find_command (argv[0]); +@@ -183,7 +189,7 @@ notmuch_help_command (notmuch_config_t *config, int argc, char *argv[]) + fprintf (stderr, + "\nSorry, %s is not a known command. There's not much I can do to help.\n\n", + argv[0]); +- return 1; ++ return EXIT_FAILURE; + } + + /* Handle the case of "notmuch" being invoked with no command +@@ -211,7 +217,7 @@ notmuch_command (notmuch_config_t *config, + if (errno != ENOENT) { + fprintf (stderr, "Error looking for notmuch database at %s: %s\n", + db_path, strerror (errno)); +- return 1; ++ return EXIT_FAILURE; + } + printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n", + db_path); +@@ -219,7 +225,7 @@ notmuch_command (notmuch_config_t *config, + "Note that the first run of \"notmuch new\" can take a very long time\n" + "and that the resulting database will use roughly the same amount of\n" + "storage space as the email being indexed.\n\n"); +- return 0; ++ return EXIT_SUCCESS; + } + + printf ("Notmuch is configured and appears to have a database. Excellent!\n\n" +@@ -239,7 +245,7 @@ notmuch_command (notmuch_config_t *config, + notmuch_config_get_user_name (config), + notmuch_config_get_user_primary_email (config)); + +- return 0; ++ return EXIT_SUCCESS; + } + + int +@@ -253,7 +259,7 @@ main (int argc, char *argv[]) + notmuch_config_t *config; + notmuch_bool_t print_help=FALSE, print_version=FALSE; + int opt_index; +- int ret = 0; ++ int ret; + + notmuch_opt_desc_t options[] = { + { NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 }, +@@ -276,16 +282,19 @@ main (int argc, char *argv[]) + + opt_index = parse_arguments (argc, argv, options, 1); + if (opt_index < 0) { +- /* diagnostics already printed */ +- return 1; ++ ret = EXIT_FAILURE; ++ goto DONE; + } + +- if (print_help) +- return notmuch_help_command (NULL, argc - 1, &argv[1]); ++ if (print_help) { ++ ret = notmuch_help_command (NULL, argc - 1, &argv[1]); ++ goto DONE; ++ } + + if (print_version) { + printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n"); +- return 0; ++ ret = EXIT_SUCCESS; ++ goto DONE; + } + + if (opt_index < argc) +@@ -295,12 +304,15 @@ main (int argc, char *argv[]) + if (!command) { + fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n", + command_name); +- return 1; ++ ret = EXIT_FAILURE; ++ goto DONE; + } + + config = notmuch_config_open (local, config_file_name, command->create_config); +- if (!config) +- return 1; ++ if (!config) { ++ ret = EXIT_FAILURE; ++ goto DONE; ++ } + + ret = (command->function)(config, argc - opt_index, argv + opt_index); + +@@ -322,6 +334,7 @@ main (int argc, char *argv[]) + } + } + ++ DONE: + talloc_free (local); + + return ret; +-- +1.8.5.2 + -- 2.26.2