Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / 89 / 66aeb8ec60356918ab5ea5173dd23970d30614
1 Return-Path: <sojkam1@fel.cvut.cz>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id C700D431FAF\r
6         for <notmuch@notmuchmail.org>; Sat,  1 Nov 2014 17:50:32 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -2.3\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-2.3 tagged_above=-999 required=5\r
12         tests=[RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id IrgtC7o427SU for <notmuch@notmuchmail.org>;\r
16         Sat,  1 Nov 2014 17:50:27 -0700 (PDT)\r
17 Received: from max.feld.cvut.cz (max.feld.cvut.cz [147.32.192.36])\r
18         by olra.theworths.org (Postfix) with ESMTP id 7F7CE431FDE\r
19         for <notmuch@notmuchmail.org>; Sat,  1 Nov 2014 17:50:15 -0700 (PDT)\r
20 Received: from localhost (unknown [192.168.200.7])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 48A295CD288\r
22         for <notmuch@notmuchmail.org>; Sun,  2 Nov 2014 01:50:11 +0100 (CET)\r
23 X-Virus-Scanned: IMAP STYX AMAVIS\r
24 Received: from max.feld.cvut.cz ([192.168.200.1])\r
25         by localhost (styx.feld.cvut.cz [192.168.200.7]) (amavisd-new,\r
26         port 10044) with ESMTP id hik63zmFrO9m for <notmuch@notmuchmail.org>;\r
27         Sun,  2 Nov 2014 01:50:06 +0100 (CET)\r
28 Received: from imap.feld.cvut.cz (imap.feld.cvut.cz [147.32.192.34])\r
29         by max.feld.cvut.cz (Postfix) with ESMTP id 7B9EF5CD28B\r
30         for <notmuch@notmuchmail.org>; Sun,  2 Nov 2014 01:50:06 +0100 (CET)\r
31 Received: from wsh by steelpick.2x.cz with local (Exim 4.84)\r
32         (envelope-from <sojkam1@fel.cvut.cz>)\r
33         id 1XkjMq-000851-FZ; Sun, 02 Nov 2014 01:50:04 +0100\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH 04/10] cli: search: Split notmuch_search_command to smaller\r
37         functions\r
38 Date: Sun,  2 Nov 2014 01:49:54 +0100\r
39 Message-Id: <1414889400-30977-5-git-send-email-sojkam1@fel.cvut.cz>\r
40 X-Mailer: git-send-email 2.1.1\r
41 In-Reply-To: <1414889400-30977-1-git-send-email-sojkam1@fel.cvut.cz>\r
42 References: <1414889400-30977-1-git-send-email-sojkam1@fel.cvut.cz>\r
43 X-BeenThere: notmuch@notmuchmail.org\r
44 X-Mailman-Version: 2.1.13\r
45 Precedence: list\r
46 List-Id: "Use and development of the notmuch mail system."\r
47         <notmuch.notmuchmail.org>\r
48 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
49         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
50 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
51 List-Post: <mailto:notmuch@notmuchmail.org>\r
52 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
53 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
54         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
55 X-List-Received-Date: Sun, 02 Nov 2014 00:50:33 -0000\r
56 \r
57 In the next commit, these functions will be used to share some\r
58 functionality between search and address commands.\r
59 ---\r
60  notmuch-search.c | 155 ++++++++++++++++++++++++++++++-------------------------\r
61  1 file changed, 86 insertions(+), 69 deletions(-)\r
62 \r
63 diff --git a/notmuch-search.c b/notmuch-search.c\r
64 index ad7a92a..d7e64eb 100644\r
65 --- a/notmuch-search.c\r
66 +++ b/notmuch-search.c\r
67 @@ -450,6 +450,89 @@ do_search_tags (const search_context_t *ctx)\r
68      return 0;\r
69  }\r
70  \r
71 +static int\r
72 +_notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])\r
73 +{\r
74 +    char *query_str;\r
75 +    unsigned int i;\r
76 +\r
77 +    switch (ctx->format_sel) {\r
78 +    case NOTMUCH_FORMAT_TEXT:\r
79 +       ctx->format = sprinter_text_create (config, stdout);\r
80 +       break;\r
81 +    case NOTMUCH_FORMAT_TEXT0:\r
82 +       if (ctx->output == OUTPUT_SUMMARY) {\r
83 +           fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
84 +           return EXIT_FAILURE;\r
85 +       }\r
86 +       ctx->format = sprinter_text0_create (config, stdout);\r
87 +       break;\r
88 +    case NOTMUCH_FORMAT_JSON:\r
89 +       ctx->format = sprinter_json_create (config, stdout);\r
90 +       break;\r
91 +    case NOTMUCH_FORMAT_SEXP:\r
92 +       ctx->format = sprinter_sexp_create (config, stdout);\r
93 +       break;\r
94 +    default:\r
95 +       /* this should never happen */\r
96 +       INTERNAL_ERROR("no output format selected");\r
97 +    }\r
98 +\r
99 +    notmuch_exit_if_unsupported_format ();\r
100 +\r
101 +    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
102 +                              NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))\r
103 +       return EXIT_FAILURE;\r
104 +\r
105 +    query_str = query_string_from_args (ctx->notmuch, argc, argv);\r
106 +    if (query_str == NULL) {\r
107 +       fprintf (stderr, "Out of memory.\n");\r
108 +       return EXIT_FAILURE;\r
109 +    }\r
110 +    if (*query_str == '\0') {\r
111 +       fprintf (stderr, "Error: notmuch search requires at least one search term.\n");\r
112 +       return EXIT_FAILURE;\r
113 +    }\r
114 +\r
115 +    ctx->query = notmuch_query_create (ctx->notmuch, query_str);\r
116 +    if (ctx->query == NULL) {\r
117 +       fprintf (stderr, "Out of memory\n");\r
118 +       return EXIT_FAILURE;\r
119 +    }\r
120 +\r
121 +    notmuch_query_set_sort (ctx->query, ctx->sort);\r
122 +\r
123 +    if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {\r
124 +       /* If we are not doing summary output there is nowhere to\r
125 +        * print the excluded flag so fall back on including the\r
126 +        * excluded messages. */\r
127 +       fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");\r
128 +       ctx->exclude = NOTMUCH_EXCLUDE_FALSE;\r
129 +    }\r
130 +\r
131 +    if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {\r
132 +       const char **search_exclude_tags;\r
133 +       size_t search_exclude_tags_length;\r
134 +\r
135 +       search_exclude_tags = notmuch_config_get_search_exclude_tags\r
136 +           (config, &search_exclude_tags_length);\r
137 +       for (i = 0; i < search_exclude_tags_length; i++)\r
138 +           notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);\r
139 +       notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);\r
140 +    }\r
141 +\r
142 +    return 0;\r
143 +}\r
144 +\r
145 +static void\r
146 +_notmuch_search_cleanup (search_context_t *ctx)\r
147 +{\r
148 +    notmuch_query_destroy (ctx->query);\r
149 +    notmuch_database_destroy (ctx->notmuch);\r
150 +\r
151 +    talloc_free (ctx->format);\r
152 +}\r
153 +\r
154  int\r
155  notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
156  {\r
157 @@ -463,9 +546,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
158         .dupe = -1,\r
159      };\r
160      search_context_t *ctx = &search_context;\r
161 -    char *query_str;\r
162      int opt_index, ret;\r
163 -    unsigned int i;\r
164  \r
165      notmuch_opt_desc_t options[] = {\r
166         { NOTMUCH_OPT_KEYWORD, &ctx->sort, "sort", 's',\r
167 @@ -507,71 +588,10 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
168      if (! ctx->output)\r
169         ctx->output = OUTPUT_SUMMARY;\r
170  \r
171 -    switch (ctx->format_sel) {\r
172 -    case NOTMUCH_FORMAT_TEXT:\r
173 -       ctx->format = sprinter_text_create (config, stdout);\r
174 -       break;\r
175 -    case NOTMUCH_FORMAT_TEXT0:\r
176 -       if (ctx->output == OUTPUT_SUMMARY) {\r
177 -           fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
178 -           return EXIT_FAILURE;\r
179 -       }\r
180 -       ctx->format = sprinter_text0_create (config, stdout);\r
181 -       break;\r
182 -    case NOTMUCH_FORMAT_JSON:\r
183 -       ctx->format = sprinter_json_create (config, stdout);\r
184 -       break;\r
185 -    case NOTMUCH_FORMAT_SEXP:\r
186 -       ctx->format = sprinter_sexp_create (config, stdout);\r
187 -       break;\r
188 -    default:\r
189 -       /* this should never happen */\r
190 -       INTERNAL_ERROR("no output format selected");\r
191 -    }\r
192 -\r
193 -    notmuch_exit_if_unsupported_format ();\r
194 -\r
195 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
196 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))\r
197 +    if (_notmuch_search_prepare (ctx, config,\r
198 +                                argc - opt_index, argv + opt_index))\r
199         return EXIT_FAILURE;\r
200  \r
201 -    query_str = query_string_from_args (ctx->notmuch, argc-opt_index, argv+opt_index);\r
202 -    if (query_str == NULL) {\r
203 -       fprintf (stderr, "Out of memory.\n");\r
204 -       return EXIT_FAILURE;\r
205 -    }\r
206 -    if (*query_str == '\0') {\r
207 -       fprintf (stderr, "Error: notmuch search requires at least one search term.\n");\r
208 -       return EXIT_FAILURE;\r
209 -    }\r
210 -\r
211 -    ctx->query = notmuch_query_create (ctx->notmuch, query_str);\r
212 -    if (ctx->query == NULL) {\r
213 -       fprintf (stderr, "Out of memory\n");\r
214 -       return EXIT_FAILURE;\r
215 -    }\r
216 -\r
217 -    notmuch_query_set_sort (ctx->query, ctx->sort);\r
218 -\r
219 -    if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {\r
220 -       /* If we are not doing summary output there is nowhere to\r
221 -        * print the excluded flag so fall back on including the\r
222 -        * excluded messages. */\r
223 -       fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");\r
224 -       ctx->exclude = NOTMUCH_EXCLUDE_FALSE;\r
225 -    }\r
226 -\r
227 -    if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {\r
228 -       const char **search_exclude_tags;\r
229 -       size_t search_exclude_tags_length;\r
230 -\r
231 -       search_exclude_tags = notmuch_config_get_search_exclude_tags\r
232 -           (config, &search_exclude_tags_length);\r
233 -       for (i = 0; i < search_exclude_tags_length; i++)\r
234 -           notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);\r
235 -       notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);\r
236 -    }\r
237 -\r
238      if (ctx->output == OUTPUT_SUMMARY ||\r
239         ctx->output == OUTPUT_THREADS)\r
240         ret = do_search_threads (ctx);\r
241 @@ -586,10 +606,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
242         ret = 1;\r
243      }\r
244  \r
245 -    notmuch_query_destroy (ctx->query);\r
246 -    notmuch_database_destroy (ctx->notmuch);\r
247 -\r
248 -    talloc_free (ctx->format);\r
249 +    _notmuch_search_cleanup (ctx);\r
250  \r
251      return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
252  }\r
253 -- \r
254 2.1.1\r
255 \r