Re: [PATCH v4 01/16] add util/search-path.{c, h} to test for executables in $PATH
[notmuch-archives.git] / 2a / f4e72547b916ed968a2a1ab2460f83f2d7fa61
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 066BA431FD0\r
6         for <notmuch@notmuchmail.org>; Sun,  5 Oct 2014 13:52:44 -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 JHA42nhWu2ZQ for <notmuch@notmuchmail.org>;\r
16         Sun,  5 Oct 2014 13:52:41 -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 19065431FBC\r
19         for <notmuch@notmuchmail.org>; Sun,  5 Oct 2014 13:52:38 -0700 (PDT)\r
20 Received: from localhost (unknown [192.168.200.7])\r
21         by max.feld.cvut.cz (Postfix) with ESMTP id 47E103CFE96\r
22         for <notmuch@notmuchmail.org>; Sun,  5 Oct 2014 22:52:37 +0200 (CEST)\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 tfLCh8Wd-afi for <notmuch@notmuchmail.org>;\r
27         Sun,  5 Oct 2014 22:52:32 +0200 (CEST)\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 5B0043CFEA3\r
30         for <notmuch@notmuchmail.org>; Sun,  5 Oct 2014 22:52:31 +0200 (CEST)\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 1Xasn2-0005E0-Pm; Sun, 05 Oct 2014 22:52:24 +0200\r
34 From: Michal Sojka <sojkam1@fel.cvut.cz>\r
35 To: notmuch@notmuchmail.org\r
36 Subject: [PATCH v2 1/4] cli: Refactor option passing in the search command\r
37 Date: Sun,  5 Oct 2014 22:51:56 +0200\r
38 Message-Id: <1412542319-20017-2-git-send-email-sojkam1@fel.cvut.cz>\r
39 X-Mailer: git-send-email 2.1.1\r
40 In-Reply-To: <1412542319-20017-1-git-send-email-sojkam1@fel.cvut.cz>\r
41 References: <874mvqxrnp.fsf@nikula.org>\r
42         <1412542319-20017-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, 05 Oct 2014 20:52:44 -0000\r
56 \r
57 Many functions that implement the search command need to access command\r
58 line options. Instead of passing each option in a separate variable, put\r
59 them in a structure and pass only this structure.\r
60 \r
61 This will become handy in the following patches.\r
62 ---\r
63  notmuch-search.c | 122 ++++++++++++++++++++++++++++---------------------------\r
64  1 file changed, 62 insertions(+), 60 deletions(-)\r
65 \r
66 diff --git a/notmuch-search.c b/notmuch-search.c\r
67 index bc9be45..5ac2a26 100644\r
68 --- a/notmuch-search.c\r
69 +++ b/notmuch-search.c\r
70 @@ -30,6 +30,16 @@ typedef enum {\r
71      OUTPUT_TAGS\r
72  } output_t;\r
73  \r
74 +typedef struct {\r
75 +    sprinter_t *format;\r
76 +    notmuch_query_t *query;\r
77 +    notmuch_sort_t sort;\r
78 +    output_t output;\r
79 +    int offset;\r
80 +    int limit;\r
81 +    int dupe;\r
82 +} search_options_t;\r
83 +\r
84  /* Return two stable query strings that identify exactly the matched\r
85   * and unmatched messages currently in thread.  If there are no\r
86   * matched or unmatched messages, the returned buffers will be\r
87 @@ -70,46 +80,42 @@ get_thread_query (notmuch_thread_t *thread,\r
88  }\r
89  \r
90  static int\r
91 -do_search_threads (sprinter_t *format,\r
92 -                  notmuch_query_t *query,\r
93 -                  notmuch_sort_t sort,\r
94 -                  output_t output,\r
95 -                  int offset,\r
96 -                  int limit)\r
97 +do_search_threads (search_options_t *o)\r
98  {\r
99      notmuch_thread_t *thread;\r
100      notmuch_threads_t *threads;\r
101      notmuch_tags_t *tags;\r
102 +    sprinter_t *format = o->format;\r
103      time_t date;\r
104      int i;\r
105  \r
106 -    if (offset < 0) {\r
107 -       offset += notmuch_query_count_threads (query);\r
108 -       if (offset < 0)\r
109 -           offset = 0;\r
110 +    if (o->offset < 0) {\r
111 +       o->offset += notmuch_query_count_threads (o->query);\r
112 +       if (o->offset < 0)\r
113 +           o->offset = 0;\r
114      }\r
115  \r
116 -    threads = notmuch_query_search_threads (query);\r
117 +    threads = notmuch_query_search_threads (o->query);\r
118      if (threads == NULL)\r
119         return 1;\r
120  \r
121      format->begin_list (format);\r
122  \r
123      for (i = 0;\r
124 -        notmuch_threads_valid (threads) && (limit < 0 || i < offset + limit);\r
125 +        notmuch_threads_valid (threads) && (o->limit < 0 || i < o->offset + o->limit);\r
126          notmuch_threads_move_to_next (threads), i++)\r
127      {\r
128         thread = notmuch_threads_get (threads);\r
129  \r
130 -       if (i < offset) {\r
131 +       if (i < o->offset) {\r
132             notmuch_thread_destroy (thread);\r
133             continue;\r
134         }\r
135  \r
136 -       if (output == OUTPUT_THREADS) {\r
137 +       if (o->output == OUTPUT_THREADS) {\r
138             format->set_prefix (format, "thread");\r
139             format->string (format,\r
140 -                           notmuch_thread_get_thread_id (thread));\r
141 +                              notmuch_thread_get_thread_id (thread));\r
142             format->separator (format);\r
143         } else { /* output == OUTPUT_SUMMARY */\r
144             void *ctx_quote = talloc_new (thread);\r
145 @@ -123,7 +129,7 @@ do_search_threads (sprinter_t *format,\r
146  \r
147             format->begin_map (format);\r
148  \r
149 -           if (sort == NOTMUCH_SORT_OLDEST_FIRST)\r
150 +           if (o->sort == NOTMUCH_SORT_OLDEST_FIRST)\r
151                 date = notmuch_thread_get_oldest_date (thread);\r
152             else\r
153                 date = notmuch_thread_get_newest_date (thread);\r
154 @@ -215,40 +221,36 @@ do_search_threads (sprinter_t *format,\r
155  }\r
156  \r
157  static int\r
158 -do_search_messages (sprinter_t *format,\r
159 -                   notmuch_query_t *query,\r
160 -                   output_t output,\r
161 -                   int offset,\r
162 -                   int limit,\r
163 -                   int dupe)\r
164 +do_search_messages (search_options_t *o)\r
165  {\r
166      notmuch_message_t *message;\r
167      notmuch_messages_t *messages;\r
168      notmuch_filenames_t *filenames;\r
169 +    sprinter_t *format = o->format;\r
170      int i;\r
171  \r
172 -    if (offset < 0) {\r
173 -       offset += notmuch_query_count_messages (query);\r
174 -       if (offset < 0)\r
175 -           offset = 0;\r
176 +    if (o->offset < 0) {\r
177 +       o->offset += notmuch_query_count_messages (o->query);\r
178 +       if (o->offset < 0)\r
179 +           o->offset = 0;\r
180      }\r
181  \r
182 -    messages = notmuch_query_search_messages (query);\r
183 +    messages = notmuch_query_search_messages (o->query);\r
184      if (messages == NULL)\r
185         return 1;\r
186  \r
187      format->begin_list (format);\r
188  \r
189      for (i = 0;\r
190 -        notmuch_messages_valid (messages) && (limit < 0 || i < offset + limit);\r
191 +        notmuch_messages_valid (messages) && (o->limit < 0 || i < o->offset + o->limit);\r
192          notmuch_messages_move_to_next (messages), i++)\r
193      {\r
194 -       if (i < offset)\r
195 +       if (i < o->offset)\r
196             continue;\r
197  \r
198         message = notmuch_messages_get (messages);\r
199  \r
200 -       if (output == OUTPUT_FILES) {\r
201 +       if (o->output == OUTPUT_FILES) {\r
202             int j;\r
203             filenames = notmuch_message_get_filenames (message);\r
204  \r
205 @@ -256,7 +258,7 @@ do_search_messages (sprinter_t *format,\r
206                  notmuch_filenames_valid (filenames);\r
207                  notmuch_filenames_move_to_next (filenames), j++)\r
208             {\r
209 -               if (dupe < 0 || dupe == j) {\r
210 +               if (o->dupe < 0 || o->dupe == j) {\r
211                     format->string (format, notmuch_filenames_get (filenames));\r
212                     format->separator (format);\r
213                 }\r
214 @@ -333,16 +335,16 @@ int\r
215  notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
216  {\r
217      notmuch_database_t *notmuch;\r
218 -    notmuch_query_t *query;\r
219 +    search_options_t o = {\r
220 +       .sort = NOTMUCH_SORT_NEWEST_FIRST,\r
221 +       .output = OUTPUT_SUMMARY,\r
222 +       .offset = 0,\r
223 +       .limit = -1, /* unlimited */\r
224 +       .dupe = -1,\r
225 +    };\r
226      char *query_str;\r
227 -    notmuch_sort_t sort = NOTMUCH_SORT_NEWEST_FIRST;\r
228 -    sprinter_t *format = NULL;\r
229      int opt_index, ret;\r
230 -    output_t output = OUTPUT_SUMMARY;\r
231 -    int offset = 0;\r
232 -    int limit = -1; /* unlimited */\r
233      notmuch_exclude_t exclude = NOTMUCH_EXCLUDE_TRUE;\r
234 -    int dupe = -1;\r
235      unsigned int i;\r
236  \r
237      enum {\r
238 @@ -353,7 +355,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
239      } format_sel = NOTMUCH_FORMAT_TEXT;\r
240  \r
241      notmuch_opt_desc_t options[] = {\r
242 -       { NOTMUCH_OPT_KEYWORD, &sort, "sort", 's',\r
243 +       { NOTMUCH_OPT_KEYWORD, &o.sort, "sort", 's',\r
244           (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },\r
245                                   { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },\r
246                                   { 0, 0 } } },\r
247 @@ -364,7 +366,7 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
248                                   { "text0", NOTMUCH_FORMAT_TEXT0 },\r
249                                   { 0, 0 } } },\r
250         { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },\r
251 -       { NOTMUCH_OPT_KEYWORD, &output, "output", 'o',\r
252 +       { NOTMUCH_OPT_KEYWORD, &o.output, "output", 'o',\r
253           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },\r
254                                   { "threads", OUTPUT_THREADS },\r
255                                   { "messages", OUTPUT_MESSAGES },\r
256 @@ -377,9 +379,9 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
257                                    { "flag", NOTMUCH_EXCLUDE_FLAG },\r
258                                    { "all", NOTMUCH_EXCLUDE_ALL },\r
259                                    { 0, 0 } } },\r
260 -       { NOTMUCH_OPT_INT, &offset, "offset", 'O', 0 },\r
261 -       { NOTMUCH_OPT_INT, &limit, "limit", 'L', 0  },\r
262 -       { NOTMUCH_OPT_INT, &dupe, "duplicate", 'D', 0  },\r
263 +       { NOTMUCH_OPT_INT, &o.offset, "offset", 'O', 0 },\r
264 +       { NOTMUCH_OPT_INT, &o.limit, "limit", 'L', 0  },\r
265 +       { NOTMUCH_OPT_INT, &o.dupe, "duplicate", 'D', 0  },\r
266         { 0, 0, 0, 0, 0 }\r
267      };\r
268  \r
269 @@ -389,20 +391,20 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
270  \r
271      switch (format_sel) {\r
272      case NOTMUCH_FORMAT_TEXT:\r
273 -       format = sprinter_text_create (config, stdout);\r
274 +       o.format = sprinter_text_create (config, stdout);\r
275         break;\r
276      case NOTMUCH_FORMAT_TEXT0:\r
277 -       if (output == OUTPUT_SUMMARY) {\r
278 +       if (o.output == OUTPUT_SUMMARY) {\r
279             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");\r
280             return EXIT_FAILURE;\r
281         }\r
282 -       format = sprinter_text0_create (config, stdout);\r
283 +       o.format = sprinter_text0_create (config, stdout);\r
284         break;\r
285      case NOTMUCH_FORMAT_JSON:\r
286 -       format = sprinter_json_create (config, stdout);\r
287 +       o.format = sprinter_json_create (config, stdout);\r
288         break;\r
289      case NOTMUCH_FORMAT_SEXP:\r
290 -       format = sprinter_sexp_create (config, stdout);\r
291 +       o.format = sprinter_sexp_create (config, stdout);\r
292         break;\r
293      default:\r
294         /* this should never happen */\r
295 @@ -425,15 +427,15 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
296         return EXIT_FAILURE;\r
297      }\r
298  \r
299 -    query = notmuch_query_create (notmuch, query_str);\r
300 -    if (query == NULL) {\r
301 +    o.query = notmuch_query_create (notmuch, query_str);\r
302 +    if (o.query == NULL) {\r
303         fprintf (stderr, "Out of memory\n");\r
304         return EXIT_FAILURE;\r
305      }\r
306  \r
307 -    notmuch_query_set_sort (query, sort);\r
308 +    notmuch_query_set_sort (o.query, o.sort);\r
309  \r
310 -    if (exclude == NOTMUCH_EXCLUDE_FLAG && output != OUTPUT_SUMMARY) {\r
311 +    if (exclude == NOTMUCH_EXCLUDE_FLAG && o.output != OUTPUT_SUMMARY) {\r
312         /* If we are not doing summary output there is nowhere to\r
313          * print the excluded flag so fall back on including the\r
314          * excluded messages. */\r
315 @@ -448,29 +450,29 @@ notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])\r
316         search_exclude_tags = notmuch_config_get_search_exclude_tags\r
317             (config, &search_exclude_tags_length);\r
318         for (i = 0; i < search_exclude_tags_length; i++)\r
319 -           notmuch_query_add_tag_exclude (query, search_exclude_tags[i]);\r
320 -       notmuch_query_set_omit_excluded (query, exclude);\r
321 +           notmuch_query_add_tag_exclude (o.query, search_exclude_tags[i]);\r
322 +       notmuch_query_set_omit_excluded (o.query, exclude);\r
323      }\r
324  \r
325 -    switch (output) {\r
326 +    switch (o.output) {\r
327      default:\r
328      case OUTPUT_SUMMARY:\r
329      case OUTPUT_THREADS:\r
330 -       ret = do_search_threads (format, query, sort, output, offset, limit);\r
331 +       ret = do_search_threads (&o);\r
332         break;\r
333      case OUTPUT_MESSAGES:\r
334      case OUTPUT_FILES:\r
335 -       ret = do_search_messages (format, query, output, offset, limit, dupe);\r
336 +       ret = do_search_messages (&o);\r
337         break;\r
338      case OUTPUT_TAGS:\r
339 -       ret = do_search_tags (notmuch, format, query);\r
340 +       ret = do_search_tags (notmuch, o.format, o.query);\r
341         break;\r
342      }\r
343  \r
344 -    notmuch_query_destroy (query);\r
345 +    notmuch_query_destroy (o.query);\r
346      notmuch_database_destroy (notmuch);\r
347  \r
348 -    talloc_free (format);\r
349 +    talloc_free (o.format);\r
350  \r
351      return ret ? EXIT_FAILURE : EXIT_SUCCESS;\r
352  }\r
353 -- \r
354 2.1.1\r
355 \r