+ format->begin_list (format);
+
+ for (;
+ notmuch_tags_valid (tags);
+ notmuch_tags_move_to_next (tags))
+ {
+ tag = notmuch_tags_get (tags);
+
+ format->string (format, tag);
+ format->separator (format);
+
+ }
+
+ notmuch_tags_destroy (tags);
+
+ if (messages)
+ notmuch_messages_destroy (messages);
+
+ format->end (format);
+
+ return 0;
+}
+
+int
+notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
+{
+ notmuch_database_t *notmuch;
+ notmuch_query_t *query;
+ char *query_str;
+ notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;
+ sprinter_t *format = NULL;
+ int opt_index, ret;
+ output_t output = OUTPUT_SUMMARY;
+ int offset = 0;
+ int limit = -1; /* unlimited */
+ notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;
+ int dupe = -1;
+ unsigned int i;
+
+ enum {
+ NOTMUCH_FORMAT_JSON,
+ NOTMUCH_FORMAT_TEXT,
+ NOTMUCH_FORMAT_TEXT0,
+ NOTMUCH_FORMAT_SEXP
+ } format_sel = NOTMUCH_FORMAT_TEXT;
+
+ notmuch_opt_desc_t options[] = {
+ { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',
+ (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
+ { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_KEYWORD, &format_sel, "format", 'f',
+ (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
+ { "sexp", NOTMUCH_FORMAT_SEXP },
+ { "text", NOTMUCH_FORMAT_TEXT },
+ { "text0", NOTMUCH_FORMAT_TEXT0 },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_INT, ¬much_format_version, "format-version", 0, 0 },
+ { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',
+ (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
+ { "threads", OUTPUT_THREADS },
+ { "messages", OUTPUT_MESSAGES },
+ { "files", OUTPUT_FILES },
+ { "tags", OUTPUT_TAGS },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_KEYWORD, &exclude, "exclude", 'x',
+ (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
+ { "false", NOTMUCH_EXCLUDE_FALSE },
+ { "flag", NOTMUCH_EXCLUDE_FLAG },
+ { "all", NOTMUCH_EXCLUDE_ALL },
+ { 0, 0 } } },
+ { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },
+ { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0 },
+ { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0 },
+ { 0, 0, 0, 0, 0 }
+ };
+
+ opt_index = parse_arguments (argc, argv, options, 1);
+ if (opt_index < 0)
+ return EXIT_FAILURE;
+
+ switch (format_sel) {
+ case NOTMUCH_FORMAT_TEXT:
+ format = sprinter_text_create (config, stdout);
+ break;
+ case NOTMUCH_FORMAT_TEXT0:
+ if (output == OUTPUT_SUMMARY) {
+ fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
+ return EXIT_FAILURE;
+ }
+ format = sprinter_text0_create (config, stdout);
+ break;
+ case NOTMUCH_FORMAT_JSON:
+ format = sprinter_json_create (config, stdout);
+ break;
+ case NOTMUCH_FORMAT_SEXP:
+ format = sprinter_sexp_create (config, stdout);
+ break;
+ default:
+ /* this should never happen */
+ INTERNAL_ERROR("no output format selected");
+ }
+
+ notmuch_exit_if_unsupported_format ();
+
+ if (notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_ONLY, ¬much))
+ return EXIT_FAILURE;
+
+ query_str = query_string_from_args (notmuch, argc-opt_index, argv+opt_index);