cli: clean up exit status code returned by the cli commands

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.
This commit is contained in:
Jani Nikula 2014-01-10 23:28:53 +02:00 committed by David Bremner
parent 17e44cd584
commit c745377306
13 changed files with 132 additions and 136 deletions

View file

@ -42,7 +42,7 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])
opt_index = parse_arguments (argc, argv, options, 1); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0) if (opt_index < 0)
return 1; return EXIT_FAILURE;
if (! quiet) if (! quiet)
printf ("Compacting database...\n"); 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); quiet ? NULL : status_update_cb, NULL);
if (ret) { if (ret) {
fprintf (stderr, "Compaction failed: %s\n", notmuch_status_to_string (ret)); fprintf (stderr, "Compaction failed: %s\n", notmuch_status_to_string (ret));
return 1; return EXIT_FAILURE;
} }
if (! quiet) { if (! quiet) {
@ -60,5 +60,5 @@ notmuch_compact_command (notmuch_config_t *config, int argc, char *argv[])
printf ("Done.\n"); printf ("Done.\n");
} }
return 0; return EXIT_SUCCESS;
} }

View file

@ -839,34 +839,39 @@ notmuch_config_command_list (notmuch_config_t *config)
int int
notmuch_config_command (notmuch_config_t *config, int argc, char *argv[]) notmuch_config_command (notmuch_config_t *config, int argc, char *argv[])
{ {
int ret;
argc--; argv++; /* skip subcommand argument */ argc--; argv++; /* skip subcommand argument */
if (argc < 1) { if (argc < 1) {
fprintf (stderr, "Error: notmuch config requires at least one argument.\n"); fprintf (stderr, "Error: notmuch config requires at least one argument.\n");
return 1; return EXIT_FAILURE;
} }
if (strcmp (argv[0], "get") == 0) { if (strcmp (argv[0], "get") == 0) {
if (argc != 2) { if (argc != 2) {
fprintf (stderr, "Error: notmuch config get requires exactly " fprintf (stderr, "Error: notmuch config get requires exactly "
"one argument.\n"); "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) { } else if (strcmp (argv[0], "set") == 0) {
if (argc < 2) { if (argc < 2) {
fprintf (stderr, "Error: notmuch config set requires at least " fprintf (stderr, "Error: notmuch config set requires at least "
"one argument.\n"); "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) { } 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", fprintf (stderr, "Unrecognized argument for notmuch config: %s\n",
argv[0]); argv[0]);
return 1; return EXIT_FAILURE;
}
return ret ? EXIT_FAILURE : EXIT_SUCCESS;
} }
notmuch_bool_t notmuch_bool_t

View file

@ -150,10 +150,8 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
}; };
opt_index = parse_arguments (argc, argv, options, 1); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0)
if (opt_index < 0) { return EXIT_FAILURE;
return 1;
}
if (input_file_name) { if (input_file_name) {
batch = TRUE; batch = TRUE;
@ -161,23 +159,23 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
if (input == NULL) { if (input == NULL) {
fprintf (stderr, "Error opening %s for reading: %s\n", fprintf (stderr, "Error opening %s for reading: %s\n",
input_file_name, strerror (errno)); input_file_name, strerror (errno));
return 1; return EXIT_FAILURE;
} }
} }
if (batch && opt_index != argc) { if (batch && opt_index != argc) {
fprintf (stderr, "--batch and query string are not compatible\n"); 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), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch)) NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
return 1; return EXIT_FAILURE;
query_str = query_string_from_args (config, argc-opt_index, argv+opt_index); query_str = query_string_from_args (config, argc-opt_index, argv+opt_index);
if (query_str == NULL) { if (query_str == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return EXIT_FAILURE;
} }
if (exclude == EXCLUDE_TRUE) { if (exclude == EXCLUDE_TRUE) {
@ -197,5 +195,5 @@ notmuch_count_command (notmuch_config_t *config, int argc, char *argv[])
if (input != stdin) if (input != stdin)
fclose (input); fclose (input);
return ret; return ret ? EXIT_FAILURE : EXIT_SUCCESS;
} }

View file

@ -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), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch)) NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
return 1; return EXIT_FAILURE;
char *output_file_name = NULL; char *output_file_name = NULL;
int opt_index; 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); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0)
if (opt_index < 0) { return EXIT_FAILURE;
/* diagnostics already printed */
return 1;
}
if (output_file_name) { if (output_file_name) {
output = fopen (output_file_name, "w"); output = fopen (output_file_name, "w");
if (output == NULL) { if (output == NULL) {
fprintf (stderr, "Error opening %s for writing: %s\n", fprintf (stderr, "Error opening %s for writing: %s\n",
output_file_name, strerror (errno)); 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); query_str = query_string_from_args (notmuch, argc - opt_index, argv + opt_index);
if (query_str == NULL) { if (query_str == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return EXIT_FAILURE;
} }
} }
query = notmuch_query_create (notmuch, query_str); query = notmuch_query_create (notmuch, query_str);
if (query == NULL) { if (query == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return EXIT_FAILURE;
} }
/* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the /* Don't ask xapian to sort by Message-ID. Xapian optimizes returning the
* first results quickly at the expense of total time. * 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) { &buffer, &buffer_size) != HEX_SUCCESS) {
fprintf (stderr, "Error: failed to hex-encode tag %s\n", fprintf (stderr, "Error: failed to hex-encode tag %s\n",
tag_str); tag_str);
return 1; return EXIT_FAILURE;
} }
fprintf (output, "+%s", buffer); fprintf (output, "+%s", buffer);
} }
@ -144,7 +141,7 @@ notmuch_dump_command (notmuch_config_t *config, int argc, char *argv[])
&buffer, &buffer_size)) { &buffer, &buffer_size)) {
fprintf (stderr, "Error quoting message id %s: %s\n", fprintf (stderr, "Error quoting message id %s: %s\n",
message_id, strerror (errno)); message_id, strerror (errno));
return 1; return EXIT_FAILURE;
} }
fprintf (output, " -- %s\n", buffer); 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_query_destroy (query);
notmuch_database_destroy (notmuch); notmuch_database_destroy (notmuch);
return 0; return EXIT_SUCCESS;
} }

View file

@ -418,11 +418,8 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
}; };
opt_index = parse_arguments (argc, argv, options, 1); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0)
if (opt_index < 0) { return EXIT_FAILURE;
/* diagnostics already printed */
return 1;
}
db_path = notmuch_config_get_database_path (config); db_path = notmuch_config_get_database_path (config);
new_tags = notmuch_config_get_new_tags (config, &new_tags_length); 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); tag_ops = tag_op_list_create (config);
if (tag_ops == NULL) { if (tag_ops == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return EXIT_FAILURE;
} }
for (i = 0; i < new_tags_length; i++) { for (i = 0; i < new_tags_length; i++) {
if (tag_op_list_append (tag_ops, new_tags[i], FALSE)) 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, if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
&query_string, tag_ops)) &query_string, tag_ops))
return 1; return EXIT_FAILURE;
if (*query_string != '\0') { if (*query_string != '\0') {
fprintf (stderr, "Error: unexpected query string: %s\n", query_string); fprintf (stderr, "Error: unexpected query string: %s\n", query_string);
return 1; return EXIT_FAILURE;
} }
if (folder == NULL) { if (folder == NULL) {
@ -452,17 +449,17 @@ notmuch_insert_command (notmuch_config_t *config, int argc, char *argv[])
} else { } else {
if (! check_folder_name (folder)) { if (! check_folder_name (folder)) {
fprintf (stderr, "Error: bad folder name: %s\n", folder); fprintf (stderr, "Error: bad folder name: %s\n", folder);
return 1; return EXIT_FAILURE;
} }
maildir = talloc_asprintf (config, "%s/%s", db_path, folder); maildir = talloc_asprintf (config, "%s/%s", db_path, folder);
if (! maildir) { if (! maildir) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return EXIT_FAILURE;
} }
if (create_folder && ! maildir_create_folder (config, maildir)) { if (create_folder && ! maildir_create_folder (config, maildir)) {
fprintf (stderr, "Error: creating maildir %s: %s\n", fprintf (stderr, "Error: creating maildir %s: %s\n",
maildir, strerror (errno)); 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), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch)) NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
return 1; return EXIT_FAILURE;
ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops, ret = insert_message (config, notmuch, STDIN_FILENO, maildir, tag_ops,
synchronize_flags); synchronize_flags);
notmuch_database_destroy (notmuch); notmuch_database_destroy (notmuch);
return (ret) ? 0 : 1; return ret ? EXIT_SUCCESS : EXIT_FAILURE;
} }

View file

@ -898,10 +898,8 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])
}; };
opt_index = parse_arguments (argc, argv, options, 1); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0) { if (opt_index < 0)
/* diagnostics already printed */ return EXIT_FAILURE;
return 1;
}
add_files_state.new_tags = notmuch_config_get_new_tags (config, &add_files_state.new_tags_length); 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); 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) { if (!no_hooks) {
ret = notmuch_run_hook (db_path, "pre-new"); ret = notmuch_run_hook (db_path, "pre-new");
if (ret) if (ret)
return ret; return EXIT_FAILURE;
} }
dot_notmuch_path = talloc_asprintf (config, "%s/%s", db_path, ".notmuch"); 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 = 0;
count_files (db_path, &count, &add_files_state); count_files (db_path, &count, &add_files_state);
if (interrupted) if (interrupted)
return 1; return EXIT_FAILURE;
printf ("Found %d total files (that's not much mail).\n", count); printf ("Found %d total files (that's not much mail).\n", count);
if (notmuch_database_create (db_path, &notmuch)) if (notmuch_database_create (db_path, &notmuch))
return 1; return EXIT_FAILURE;
add_files_state.total_files = count; add_files_state.total_files = count;
} else { } else {
if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE, if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,
&notmuch)) &notmuch))
return 1; return EXIT_FAILURE;
if (notmuch_database_needs_upgrade (notmuch)) { if (notmuch_database_needs_upgrade (notmuch)) {
printf ("Welcome to a new version of notmuch! Your database will now be upgraded.\n"); 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) if (notmuch == NULL)
return 1; return EXIT_FAILURE;
/* Setup our handler for SIGINT. We do this after having /* Setup our handler for SIGINT. We do this after having
* potentially done a database upgrade we this interrupt handler * 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) if (!no_hooks && !ret && !interrupted)
ret = notmuch_run_hook (db_path, "post-new"); ret = notmuch_run_hook (db_path, "post-new");
return ret || interrupted; return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;
} }

View file

@ -704,7 +704,7 @@ notmuch_reply_command (notmuch_config_t *config, int argc, char *argv[])
notmuch_database_t *notmuch; notmuch_database_t *notmuch;
notmuch_query_t *query; notmuch_query_t *query;
char *query_string; char *query_string;
int opt_index, ret = 0; int opt_index;
int (*reply_format_func) (void *ctx, int (*reply_format_func) (void *ctx,
notmuch_config_t *config, notmuch_config_t *config,
notmuch_query_t *query, 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); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0) { if (opt_index < 0)
/* diagnostics already printed */ return EXIT_FAILURE;
return 1;
}
if (format == FORMAT_HEADERS_ONLY) { if (format == FORMAT_HEADERS_ONLY) {
reply_format_func = notmuch_reply_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); query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
if (query_string == NULL) { if (query_string == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return EXIT_FAILURE;
} }
if (*query_string == '\0') { if (*query_string == '\0') {
fprintf (stderr, "Error: notmuch reply requires at least one search term.\n"); 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), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch)) NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
return 1; return EXIT_FAILURE;
query = notmuch_query_create (notmuch, query_string); query = notmuch_query_create (notmuch, query_string);
if (query == NULL) { if (query == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return EXIT_FAILURE;
} }
if (reply_format_func (config, config, query, &params, reply_all, sp) != 0) if (reply_format_func (config, config, query, &params, reply_all, sp) != 0)
return 1; return EXIT_FAILURE;
notmuch_crypto_cleanup (&params.crypto); notmuch_crypto_cleanup (&params.crypto);
notmuch_query_destroy (query); notmuch_query_destroy (query);
notmuch_database_destroy (notmuch); notmuch_database_destroy (notmuch);
return ret; return EXIT_SUCCESS;
} }

View file

@ -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), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch)) NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
return 1; return EXIT_FAILURE;
if (notmuch_config_get_maildir_synchronize_flags (config)) if (notmuch_config_get_maildir_synchronize_flags (config))
flags |= TAG_FLAG_MAILDIR_SYNC; 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); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0)
if (opt_index < 0) { return EXIT_FAILURE;
/* diagnostics already printed */
return 1;
}
if (! accumulate) if (! accumulate)
flags |= TAG_FLAG_REMOVE_ALL; flags |= TAG_FLAG_REMOVE_ALL;
@ -171,21 +168,19 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])
if (input == NULL) { if (input == NULL) {
fprintf (stderr, "Error opening %s for reading: %s\n", fprintf (stderr, "Error opening %s for reading: %s\n",
input_file_name, strerror (errno)); input_file_name, strerror (errno));
return 1; return EXIT_FAILURE;
} }
} }
if (opt_index < argc) { if (opt_index < argc) {
fprintf (stderr, fprintf (stderr, "Unused positional parameter: %s\n", argv[opt_index]);
"Unused positional parameter: %s\n", return EXIT_FAILURE;
argv[opt_index]);
return 1;
} }
tag_ops = tag_op_list_create (config); tag_ops = tag_op_list_create (config);
if (tag_ops == NULL) { if (tag_ops == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return EXIT_FAILURE;
} }
do { do {
@ -193,7 +188,7 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])
/* empty input file not considered an error */ /* empty input file not considered an error */
if (line_len < 0) if (line_len < 0)
return 0; return EXIT_SUCCESS;
} while ((line_len == 0) || } while ((line_len == 0) ||
(line[0] == '#') || (line[0] == '#') ||
@ -275,5 +270,5 @@ notmuch_restore_command (notmuch_config_t *config, int argc, char *argv[])
if (input != stdin) if (input != stdin)
fclose (input); fclose (input);
return ret; return ret ? EXIT_FAILURE : EXIT_SUCCESS;
} }

View file

@ -401,10 +401,8 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
}; };
opt_index = parse_arguments (argc, argv, options, 1); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0)
if (opt_index < 0) { return EXIT_FAILURE;
return 1;
}
switch (format_sel) { switch (format_sel) {
case NOTMUCH_FORMAT_TEXT: case NOTMUCH_FORMAT_TEXT:
@ -413,7 +411,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
case NOTMUCH_FORMAT_TEXT0: case NOTMUCH_FORMAT_TEXT0:
if (output == OUTPUT_SUMMARY) { if (output == OUTPUT_SUMMARY) {
fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n"); fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
return 1; return EXIT_FAILURE;
} }
format = sprinter_text0_create (config, stdout); format = sprinter_text0_create (config, stdout);
break; 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), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch)) NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
return 1; return EXIT_FAILURE;
query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index); query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);
if (query_str == NULL) { if (query_str == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return EXIT_FAILURE;
} }
if (*query_str == '\0') { if (*query_str == '\0') {
fprintf (stderr, "Error: notmuch search requires at least one search term.\n"); fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
return 1; return EXIT_FAILURE;
} }
query = notmuch_query_create (notmuch, query_str); query = notmuch_query_create (notmuch, query_str);
if (query == NULL) { if (query == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return EXIT_FAILURE;
} }
notmuch_query_set_sort (query, sort); notmuch_query_set_sort (query, sort);
@ -491,5 +489,5 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
talloc_free (format); talloc_free (format);
return ret; return ret ? EXIT_FAILURE : EXIT_SUCCESS;
} }

View file

@ -140,7 +140,7 @@ notmuch_setup_command (notmuch_config_t *config,
fflush (stdout); \ fflush (stdout); \
if (getline (&response, &response_size, stdin) < 0) { \ if (getline (&response, &response_size, stdin) < 0) { \
printf ("Exiting.\n"); \ printf ("Exiting.\n"); \
exit (1); \ exit (EXIT_FAILURE); \
} \ } \
chomp_newline (response); \ chomp_newline (response); \
} while (0) } while (0)
@ -223,12 +223,11 @@ notmuch_setup_command (notmuch_config_t *config,
g_ptr_array_free (tags, TRUE); 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)) if (notmuch_config_is_new (config))
welcome_message_post_setup (); welcome_message_post_setup ();
return 0;
} else { return EXIT_SUCCESS;
return 1;
}
} }

View file

@ -1113,10 +1113,8 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
}; };
opt_index = parse_arguments (argc, argv, options, 1); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0) { if (opt_index < 0)
/* diagnostics already printed */ return EXIT_FAILURE;
return 1;
}
/* decryption implies verification */ /* decryption implies verification */
if (params.crypto.decrypt) if (params.crypto.decrypt)
@ -1143,7 +1141,7 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
case NOTMUCH_FORMAT_MBOX: case NOTMUCH_FORMAT_MBOX:
if (params.part > 0) { if (params.part > 0) {
fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n"); fprintf (stderr, "Error: specifying parts is incompatible with mbox output format.\n");
return 1; return EXIT_FAILURE;
} }
format = &format_mbox; 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); query_string = query_string_from_args (config, argc-opt_index, argv+opt_index);
if (query_string == NULL) { if (query_string == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return EXIT_FAILURE;
} }
if (*query_string == '\0') { if (*query_string == '\0') {
fprintf (stderr, "Error: notmuch show requires at least one search term.\n"); 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), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch)) NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch))
return 1; return EXIT_FAILURE;
query = notmuch_query_create (notmuch, query_string); query = notmuch_query_create (notmuch, query_string);
if (query == NULL) { if (query == NULL) {
fprintf (stderr, "Out of memory\n"); fprintf (stderr, "Out of memory\n");
return 1; return EXIT_FAILURE;
} }
/* Create structure printer. */ /* Create structure printer. */
@ -1242,5 +1240,5 @@ notmuch_show_command (notmuch_config_t *config, int argc, char *argv[])
notmuch_query_destroy (query); notmuch_query_destroy (query);
notmuch_database_destroy (notmuch); notmuch_database_destroy (notmuch);
return ret; return ret ? EXIT_FAILURE : EXIT_SUCCESS;
} }

View file

@ -193,7 +193,7 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
FILE *input = stdin; FILE *input = stdin;
char *input_file_name = NULL; char *input_file_name = NULL;
int opt_index; int opt_index;
int ret = 0; int ret;
/* Setup our handler for SIGINT */ /* Setup our handler for SIGINT */
memset (&action, 0, sizeof (struct sigaction)); 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); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0) if (opt_index < 0)
return 1; return EXIT_FAILURE;
if (input_file_name) { if (input_file_name) {
batch = TRUE; batch = TRUE;
@ -219,44 +219,44 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
if (input == NULL) { if (input == NULL) {
fprintf (stderr, "Error opening %s for reading: %s\n", fprintf (stderr, "Error opening %s for reading: %s\n",
input_file_name, strerror (errno)); input_file_name, strerror (errno));
return 1; return EXIT_FAILURE;
} }
} }
if (batch) { if (batch) {
if (opt_index != argc) { if (opt_index != argc) {
fprintf (stderr, "Can't specify both cmdline and stdin!\n"); fprintf (stderr, "Can't specify both cmdline and stdin!\n");
return 1; return EXIT_FAILURE;
} }
if (remove_all) { if (remove_all) {
fprintf (stderr, "Can't specify both --remove-all and --batch\n"); fprintf (stderr, "Can't specify both --remove-all and --batch\n");
return 1; return EXIT_FAILURE;
} }
} else { } else {
tag_ops = tag_op_list_create (config); tag_ops = tag_op_list_create (config);
if (tag_ops == NULL) { if (tag_ops == NULL) {
fprintf (stderr, "Out of memory.\n"); fprintf (stderr, "Out of memory.\n");
return 1; return EXIT_FAILURE;
} }
if (parse_tag_command_line (config, argc - opt_index, argv + opt_index, if (parse_tag_command_line (config, argc - opt_index, argv + opt_index,
&query_string, tag_ops)) &query_string, tag_ops))
return 1; return EXIT_FAILURE;
if (tag_op_list_size (tag_ops) == 0 && ! remove_all) { 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"); fprintf (stderr, "Error: 'notmuch tag' requires at least one tag to add or remove.\n");
return 1; return EXIT_FAILURE;
} }
if (*query_string == '\0') { if (*query_string == '\0') {
fprintf (stderr, "Error: notmuch tag requires at least one search term.\n"); 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), if (notmuch_database_open (notmuch_config_get_database_path (config),
NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch)) NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch))
return 1; return EXIT_FAILURE;
if (notmuch_config_get_maildir_synchronize_flags (config)) if (notmuch_config_get_maildir_synchronize_flags (config))
tag_flags |= TAG_FLAG_MAILDIR_SYNC; tag_flags |= TAG_FLAG_MAILDIR_SYNC;
@ -274,5 +274,5 @@ notmuch_tag_command (notmuch_config_t *config, int argc, char *argv[])
if (input != stdin) if (input != stdin)
fclose (input); fclose (input);
return ret || interrupted; return ret || interrupted ? EXIT_FAILURE : EXIT_SUCCESS;
} }

View file

@ -22,6 +22,12 @@
#include "notmuch-client.h" #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 int (*command_function_t) (notmuch_config_t *config, int argc, char *argv[]);
typedef struct command { typedef struct command {
@ -156,7 +162,7 @@ notmuch_help_command (notmuch_config_t *config, int argc, char *argv[])
if (argc == 0) { if (argc == 0) {
printf ("The notmuch mail system.\n\n"); printf ("The notmuch mail system.\n\n");
usage (stdout); usage (stdout);
return 0; return EXIT_SUCCESS;
} }
if (strcmp (argv[0], "help") == 0) { 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" "\tof difficulties check that MANPATH includes the pages\n"
"\tinstalled by notmuch.\n\n" "\tinstalled by notmuch.\n\n"
"\tTry \"notmuch help\" for a list of topics.\n"); "\tTry \"notmuch help\" for a list of topics.\n");
return 0; return EXIT_SUCCESS;
} }
command = find_command (argv[0]); command = find_command (argv[0]);
@ -183,7 +189,7 @@ notmuch_help_command (notmuch_config_t *config, int argc, char *argv[])
fprintf (stderr, fprintf (stderr,
"\nSorry, %s is not a known command. There's not much I can do to help.\n\n", "\nSorry, %s is not a known command. There's not much I can do to help.\n\n",
argv[0]); argv[0]);
return 1; return EXIT_FAILURE;
} }
/* Handle the case of "notmuch" being invoked with no command /* Handle the case of "notmuch" being invoked with no command
@ -211,7 +217,7 @@ notmuch_command (notmuch_config_t *config,
if (errno != ENOENT) { if (errno != ENOENT) {
fprintf (stderr, "Error looking for notmuch database at %s: %s\n", fprintf (stderr, "Error looking for notmuch database at %s: %s\n",
db_path, strerror (errno)); 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", printf ("Notmuch is configured, but there's not yet a database at\n\n\t%s\n\n",
db_path); 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" "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" "and that the resulting database will use roughly the same amount of\n"
"storage space as the email being indexed.\n\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" 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_name (config),
notmuch_config_get_user_primary_email (config)); notmuch_config_get_user_primary_email (config));
return 0; return EXIT_SUCCESS;
} }
int int
@ -253,7 +259,7 @@ main (int argc, char *argv[])
notmuch_config_t *config; notmuch_config_t *config;
notmuch_bool_t print_help=FALSE, print_version=FALSE; notmuch_bool_t print_help=FALSE, print_version=FALSE;
int opt_index; int opt_index;
int ret = 0; int ret;
notmuch_opt_desc_t options[] = { notmuch_opt_desc_t options[] = {
{ NOTMUCH_OPT_BOOLEAN, &print_help, "help", 'h', 0 }, { 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); opt_index = parse_arguments (argc, argv, options, 1);
if (opt_index < 0) { if (opt_index < 0) {
/* diagnostics already printed */ ret = EXIT_FAILURE;
return 1; goto DONE;
} }
if (print_help) if (print_help) {
return notmuch_help_command (NULL, argc - 1, &argv[1]); ret = notmuch_help_command (NULL, argc - 1, &argv[1]);
goto DONE;
}
if (print_version) { if (print_version) {
printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n"); printf ("notmuch " STRINGIFY(NOTMUCH_VERSION) "\n");
return 0; ret = EXIT_SUCCESS;
goto DONE;
} }
if (opt_index < argc) if (opt_index < argc)
@ -295,12 +304,15 @@ main (int argc, char *argv[])
if (!command) { if (!command) {
fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n", fprintf (stderr, "Error: Unknown command '%s' (see \"notmuch help\")\n",
command_name); command_name);
return 1; ret = EXIT_FAILURE;
goto DONE;
} }
config = notmuch_config_open (local, config_file_name, command->create_config); config = notmuch_config_open (local, config_file_name, command->create_config);
if (!config) if (!config) {
return 1; ret = EXIT_FAILURE;
goto DONE;
}
ret = (command->function)(config, argc - opt_index, argv + opt_index); ret = (command->function)(config, argc - opt_index, argv + opt_index);
@ -322,6 +334,7 @@ main (int argc, char *argv[])
} }
} }
DONE:
talloc_free (local); talloc_free (local);
return ret; return ret;