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