[PATCH 1/2] cli: clean up exit status code returned by the cli commands

Jani Nikula jani at nikula.org
Fri Jan 10 13:28:53 PST 2014


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, &notmuch))
-	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, &notmuch))
-	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, &notmuch))
-	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, &notmuch))
-	    return 1;
+	    return EXIT_FAILURE;
 	add_files_state.total_files = count;
     } else {
 	if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
 				   &notmuch))
-	    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, &notmuch))
-	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, &params, reply_all, sp) != 0)
-	return 1;
+	return EXIT_FAILURE;
 
     notmuch_crypto_cleanup (&params.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, &notmuch))
-	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, &notmuch))
-	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, &notmuch))
-	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, &notmuch))
-	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



More information about the notmuch mailing list