]> rtime.felk.cvut.cz Git - notmuch.git/blob - notmuch-search.c
cli: address: Add --output=count
[notmuch.git] / notmuch-search.c
1 /* notmuch - Not much of an email program, (just index and search)
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-client.h"
22 #include "sprinter.h"
23 #include "string-util.h"
24
25 typedef enum {
26     /* Search command */
27     OUTPUT_SUMMARY      = 1 << 0,
28     OUTPUT_THREADS      = 1 << 1,
29     OUTPUT_MESSAGES     = 1 << 2,
30     OUTPUT_FILES        = 1 << 3,
31     OUTPUT_TAGS         = 1 << 4,
32
33     /* Address command */
34     OUTPUT_SENDER       = 1 << 5,
35     OUTPUT_RECIPIENTS   = 1 << 6,
36     OUTPUT_COUNT        = 1 << 7,
37 } output_t;
38
39 typedef enum {
40     NOTMUCH_FORMAT_JSON,
41     NOTMUCH_FORMAT_TEXT,
42     NOTMUCH_FORMAT_TEXT0,
43     NOTMUCH_FORMAT_SEXP
44 } format_sel_t;
45
46 typedef struct {
47     notmuch_database_t *notmuch;
48     format_sel_t format_sel;
49     sprinter_t *format;
50     notmuch_exclude_t exclude;
51     notmuch_query_t *query;
52     notmuch_sort_t sort;
53     output_t output;
54     int offset;
55     int limit;
56     int dupe;
57     GHashTable *addresses;
58 } search_context_t;
59
60 typedef struct {
61     const char *name;
62     const char *addr;
63     int count;
64 } mailbox_t;
65
66 /* Return two stable query strings that identify exactly the matched
67  * and unmatched messages currently in thread.  If there are no
68  * matched or unmatched messages, the returned buffers will be
69  * NULL. */
70 static int
71 get_thread_query (notmuch_thread_t *thread,
72                   char **matched_out, char **unmatched_out)
73 {
74     notmuch_messages_t *messages;
75     char *escaped = NULL;
76     size_t escaped_len = 0;
77
78     *matched_out = *unmatched_out = NULL;
79
80     for (messages = notmuch_thread_get_messages (thread);
81          notmuch_messages_valid (messages);
82          notmuch_messages_move_to_next (messages))
83     {
84         notmuch_message_t *message = notmuch_messages_get (messages);
85         const char *mid = notmuch_message_get_message_id (message);
86         /* Determine which query buffer to extend */
87         char **buf = notmuch_message_get_flag (
88             message, NOTMUCH_MESSAGE_FLAG_MATCH) ? matched_out : unmatched_out;
89         /* Add this message's id: query.  Since "id" is an exclusive
90          * prefix, it is implicitly 'or'd together, so we only need to
91          * join queries with a space. */
92         if (make_boolean_term (thread, "id", mid, &escaped, &escaped_len) < 0)
93             return -1;
94         if (*buf)
95             *buf = talloc_asprintf_append_buffer (*buf, " %s", escaped);
96         else
97             *buf = talloc_strdup (thread, escaped);
98         if (!*buf)
99             return -1;
100     }
101     talloc_free (escaped);
102     return 0;
103 }
104
105 static int
106 do_search_threads (search_context_t *ctx)
107 {
108     notmuch_thread_t *thread;
109     notmuch_threads_t *threads;
110     notmuch_tags_t *tags;
111     sprinter_t *format = ctx->format;
112     time_t date;
113     int i;
114
115     if (ctx->offset < 0) {
116         ctx->offset += notmuch_query_count_threads (ctx->query);
117         if (ctx->offset < 0)
118             ctx->offset = 0;
119     }
120
121     threads = notmuch_query_search_threads (ctx->query);
122     if (threads == NULL)
123         return 1;
124
125     format->begin_list (format);
126
127     for (i = 0;
128          notmuch_threads_valid (threads) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
129          notmuch_threads_move_to_next (threads), i++)
130     {
131         thread = notmuch_threads_get (threads);
132
133         if (i < ctx->offset) {
134             notmuch_thread_destroy (thread);
135             continue;
136         }
137
138         if (ctx->output == OUTPUT_THREADS) {
139             format->set_prefix (format, "thread");
140             format->string (format,
141                             notmuch_thread_get_thread_id (thread));
142             format->separator (format);
143         } else { /* output == OUTPUT_SUMMARY */
144             void *ctx_quote = talloc_new (thread);
145             const char *authors = notmuch_thread_get_authors (thread);
146             const char *subject = notmuch_thread_get_subject (thread);
147             const char *thread_id = notmuch_thread_get_thread_id (thread);
148             int matched = notmuch_thread_get_matched_messages (thread);
149             int total = notmuch_thread_get_total_messages (thread);
150             const char *relative_date = NULL;
151             notmuch_bool_t first_tag = TRUE;
152
153             format->begin_map (format);
154
155             if (ctx->sort == NOTMUCH_SORT_OLDEST_FIRST)
156                 date = notmuch_thread_get_oldest_date (thread);
157             else
158                 date = notmuch_thread_get_newest_date (thread);
159
160             relative_date = notmuch_time_relative_date (ctx_quote, date);
161
162             if (format->is_text_printer) {
163                 /* Special case for the text formatter */
164                 printf ("thread:%s %12s [%d/%d] %s; %s (",
165                         thread_id,
166                         relative_date,
167                         matched,
168                         total,
169                         sanitize_string (ctx_quote, authors),
170                         sanitize_string (ctx_quote, subject));
171             } else { /* Structured Output */
172                 format->map_key (format, "thread");
173                 format->string (format, thread_id);
174                 format->map_key (format, "timestamp");
175                 format->integer (format, date);
176                 format->map_key (format, "date_relative");
177                 format->string (format, relative_date);
178                 format->map_key (format, "matched");
179                 format->integer (format, matched);
180                 format->map_key (format, "total");
181                 format->integer (format, total);
182                 format->map_key (format, "authors");
183                 format->string (format, authors);
184                 format->map_key (format, "subject");
185                 format->string (format, subject);
186                 if (notmuch_format_version >= 2) {
187                     char *matched_query, *unmatched_query;
188                     if (get_thread_query (thread, &matched_query,
189                                           &unmatched_query) < 0) {
190                         fprintf (stderr, "Out of memory\n");
191                         return 1;
192                     }
193                     format->map_key (format, "query");
194                     format->begin_list (format);
195                     if (matched_query)
196                         format->string (format, matched_query);
197                     else
198                         format->null (format);
199                     if (unmatched_query)
200                         format->string (format, unmatched_query);
201                     else
202                         format->null (format);
203                     format->end (format);
204                 }
205             }
206
207             talloc_free (ctx_quote);
208
209             format->map_key (format, "tags");
210             format->begin_list (format);
211
212             for (tags = notmuch_thread_get_tags (thread);
213                  notmuch_tags_valid (tags);
214                  notmuch_tags_move_to_next (tags))
215             {
216                 const char *tag = notmuch_tags_get (tags);
217
218                 if (format->is_text_printer) {
219                   /* Special case for the text formatter */
220                     if (first_tag)
221                         first_tag = FALSE;
222                     else
223                         fputc (' ', stdout);
224                     fputs (tag, stdout);
225                 } else { /* Structured Output */
226                     format->string (format, tag);
227                 }
228             }
229
230             if (format->is_text_printer)
231                 printf (")");
232
233             format->end (format);
234             format->end (format);
235             format->separator (format);
236         }
237
238         notmuch_thread_destroy (thread);
239     }
240
241     format->end (format);
242
243     return 0;
244 }
245
246 /* Returns TRUE iff name and addr is duplicate. */
247 static notmuch_bool_t
248 is_duplicate (const search_context_t *ctx, const char *name, const char *addr)
249 {
250     notmuch_bool_t duplicate;
251     char *key;
252     mailbox_t *mailbox;
253
254     key = talloc_asprintf (ctx->format, "%s <%s>", name, addr);
255     if (! key)
256         return FALSE;
257
258     duplicate = g_hash_table_lookup_extended (ctx->addresses, key, NULL, (gpointer)&mailbox);
259
260     if (! duplicate) {
261         mailbox = talloc (ctx->format, mailbox_t);
262         mailbox->name = talloc_strdup (mailbox, name);
263         mailbox->addr = talloc_strdup (mailbox, addr);
264         mailbox->count = 1;
265         g_hash_table_insert (ctx->addresses, key, mailbox);
266     } else {
267         mailbox->count++;
268         talloc_free (key);
269     }
270
271     return duplicate;
272 }
273
274 static void
275 print_mailbox (const search_context_t *ctx, const mailbox_t *mailbox)
276 {
277     const char *name = mailbox->name;
278     const char *addr = mailbox->addr;
279     int count = mailbox->count;
280     sprinter_t *format = ctx->format;
281     InternetAddress *ia = internet_address_mailbox_new (name, addr);
282     char *name_addr;
283
284     /* name_addr has the name part quoted if necessary. Compare
285      * 'John Doe <john@doe.com>' vs. '"Doe, John" <john@doe.com>' */
286     name_addr = internet_address_to_string (ia, FALSE);
287
288     if (format->is_text_printer) {
289         if (count > 0) {
290             format->integer (format, count);
291             format->string (format, "\t");
292         }
293         format->string (format, name_addr);
294         format->separator (format);
295     } else {
296         format->begin_map (format);
297         format->map_key (format, "name");
298         format->string (format, name);
299         format->map_key (format, "address");
300         format->string (format, addr);
301         format->map_key (format, "name-addr");
302         format->string (format, name_addr);
303         if (count > 0) {
304             format->map_key (format, "count");
305             format->integer (format, count);
306         }
307         format->end (format);
308         format->separator (format);
309     }
310
311     g_object_unref (ia);
312     g_free (name_addr);
313 }
314
315 /* Print or prepare for printing addresses from InternetAddressList. */
316 static void
317 process_address_list (const search_context_t *ctx,
318                       InternetAddressList *list)
319 {
320     InternetAddress *address;
321     int i;
322
323     for (i = 0; i < internet_address_list_length (list); i++) {
324         address = internet_address_list_get_address (list, i);
325         if (INTERNET_ADDRESS_IS_GROUP (address)) {
326             InternetAddressGroup *group;
327             InternetAddressList *group_list;
328
329             group = INTERNET_ADDRESS_GROUP (address);
330             group_list = internet_address_group_get_members (group);
331             if (group_list == NULL)
332                 continue;
333
334             process_address_list (ctx, group_list);
335         } else {
336             InternetAddressMailbox *mailbox = INTERNET_ADDRESS_MAILBOX (address);
337             mailbox_t mbx = {
338                 .name = internet_address_get_name (address),
339                 .addr = internet_address_mailbox_get_addr (mailbox),
340                 .count = 0,
341             };
342
343             if (is_duplicate (ctx, mbx.name, mbx.addr))
344                 continue;
345
346             if (ctx->output & OUTPUT_COUNT)
347                 continue;
348
349             print_mailbox (ctx, &mbx);
350         }
351     }
352 }
353
354 /* Print or prepare for printing addresses from a message header. */
355 static void
356 process_address_header (const search_context_t *ctx, const char *value)
357 {
358     InternetAddressList *list;
359
360     if (value == NULL)
361         return;
362
363     list = internet_address_list_parse_string (value);
364     if (list == NULL)
365         return;
366
367     process_address_list (ctx, list);
368
369     g_object_unref (list);
370 }
371
372 static void
373 _my_talloc_free_for_g_hash (void *ptr)
374 {
375     talloc_free (ptr);
376 }
377
378 static void
379 print_hash_value (unused (gpointer key), gpointer value, gpointer user_data)
380 {
381     const mailbox_t *mailbox = value;
382     search_context_t *ctx = user_data;
383
384     print_mailbox (ctx, mailbox);
385 }
386
387 static int
388 do_search_messages (search_context_t *ctx)
389 {
390     notmuch_message_t *message;
391     notmuch_messages_t *messages;
392     notmuch_filenames_t *filenames;
393     sprinter_t *format = ctx->format;
394     int i;
395
396     if (ctx->offset < 0) {
397         ctx->offset += notmuch_query_count_messages (ctx->query);
398         if (ctx->offset < 0)
399             ctx->offset = 0;
400     }
401
402     messages = notmuch_query_search_messages (ctx->query);
403     if (messages == NULL)
404         return 1;
405
406     format->begin_list (format);
407
408     for (i = 0;
409          notmuch_messages_valid (messages) && (ctx->limit < 0 || i < ctx->offset + ctx->limit);
410          notmuch_messages_move_to_next (messages), i++)
411     {
412         if (i < ctx->offset)
413             continue;
414
415         message = notmuch_messages_get (messages);
416
417         if (ctx->output == OUTPUT_FILES) {
418             int j;
419             filenames = notmuch_message_get_filenames (message);
420
421             for (j = 1;
422                  notmuch_filenames_valid (filenames);
423                  notmuch_filenames_move_to_next (filenames), j++)
424             {
425                 if (ctx->dupe < 0 || ctx->dupe == j) {
426                     format->string (format, notmuch_filenames_get (filenames));
427                     format->separator (format);
428                 }
429             }
430             
431             notmuch_filenames_destroy( filenames );
432
433         } else if (ctx->output == OUTPUT_MESSAGES) {
434             format->set_prefix (format, "id");
435             format->string (format,
436                             notmuch_message_get_message_id (message));
437             format->separator (format);
438         } else {
439             if (ctx->output & OUTPUT_SENDER) {
440                 const char *addrs;
441
442                 addrs = notmuch_message_get_header (message, "from");
443                 process_address_header (ctx, addrs);
444             }
445
446             if (ctx->output & OUTPUT_RECIPIENTS) {
447                 const char *hdrs[] = { "to", "cc", "bcc" };
448                 const char *addrs;
449                 size_t j;
450
451                 for (j = 0; j < ARRAY_SIZE (hdrs); j++) {
452                     addrs = notmuch_message_get_header (message, hdrs[j]);
453                     process_address_header (ctx, addrs);
454                 }
455             }
456         }
457
458         notmuch_message_destroy (message);
459     }
460
461     if (ctx->addresses && ctx->output & OUTPUT_COUNT)
462         g_hash_table_foreach (ctx->addresses, print_hash_value, ctx);
463
464     notmuch_messages_destroy (messages);
465
466     format->end (format);
467
468     return 0;
469 }
470
471 static int
472 do_search_tags (const search_context_t *ctx)
473 {
474     notmuch_messages_t *messages = NULL;
475     notmuch_tags_t *tags;
476     const char *tag;
477     sprinter_t *format = ctx->format;
478     notmuch_query_t *query = ctx->query;
479     notmuch_database_t *notmuch = ctx->notmuch;
480
481     /* should the following only special case if no excluded terms
482      * specified? */
483
484     /* Special-case query of "*" for better performance. */
485     if (strcmp (notmuch_query_get_query_string (query), "*") == 0) {
486         tags = notmuch_database_get_all_tags (notmuch);
487     } else {
488         messages = notmuch_query_search_messages (query);
489         if (messages == NULL)
490             return 1;
491
492         tags = notmuch_messages_collect_tags (messages);
493     }
494     if (tags == NULL)
495         return 1;
496
497     format->begin_list (format);
498
499     for (;
500          notmuch_tags_valid (tags);
501          notmuch_tags_move_to_next (tags))
502     {
503         tag = notmuch_tags_get (tags);
504
505         format->string (format, tag);
506         format->separator (format);
507
508     }
509
510     notmuch_tags_destroy (tags);
511
512     if (messages)
513         notmuch_messages_destroy (messages);
514
515     format->end (format);
516
517     return 0;
518 }
519
520 static int
521 _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int argc, char *argv[])
522 {
523     char *query_str;
524     unsigned int i;
525
526     switch (ctx->format_sel) {
527     case NOTMUCH_FORMAT_TEXT:
528         ctx->format = sprinter_text_create (config, stdout);
529         break;
530     case NOTMUCH_FORMAT_TEXT0:
531         if (ctx->output == OUTPUT_SUMMARY) {
532             fprintf (stderr, "Error: --format=text0 is not compatible with --output=summary.\n");
533             return EXIT_FAILURE;
534         }
535         ctx->format = sprinter_text0_create (config, stdout);
536         break;
537     case NOTMUCH_FORMAT_JSON:
538         ctx->format = sprinter_json_create (config, stdout);
539         break;
540     case NOTMUCH_FORMAT_SEXP:
541         ctx->format = sprinter_sexp_create (config, stdout);
542         break;
543     default:
544         /* this should never happen */
545         INTERNAL_ERROR("no output format selected");
546     }
547
548     notmuch_exit_if_unsupported_format ();
549
550     if (notmuch_database_open (notmuch_config_get_database_path (config),
551                                NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))
552         return EXIT_FAILURE;
553
554     query_str = query_string_from_args (ctx->notmuch, argc, argv);
555     if (query_str == NULL) {
556         fprintf (stderr, "Out of memory.\n");
557         return EXIT_FAILURE;
558     }
559     if (*query_str == '\0') {
560         fprintf (stderr, "Error: notmuch search requires at least one search term.\n");
561         return EXIT_FAILURE;
562     }
563
564     ctx->query = notmuch_query_create (ctx->notmuch, query_str);
565     if (ctx->query == NULL) {
566         fprintf (stderr, "Out of memory\n");
567         return EXIT_FAILURE;
568     }
569
570     notmuch_query_set_sort (ctx->query, ctx->sort);
571
572     if (ctx->exclude == NOTMUCH_EXCLUDE_FLAG && ctx->output != OUTPUT_SUMMARY) {
573         /* If we are not doing summary output there is nowhere to
574          * print the excluded flag so fall back on including the
575          * excluded messages. */
576         fprintf (stderr, "Warning: this output format cannot flag excluded messages.\n");
577         ctx->exclude = NOTMUCH_EXCLUDE_FALSE;
578     }
579
580     if (ctx->exclude != NOTMUCH_EXCLUDE_FALSE) {
581         const char **search_exclude_tags;
582         size_t search_exclude_tags_length;
583
584         search_exclude_tags = notmuch_config_get_search_exclude_tags
585             (config, &search_exclude_tags_length);
586         for (i = 0; i < search_exclude_tags_length; i++)
587             notmuch_query_add_tag_exclude (ctx->query, search_exclude_tags[i]);
588         notmuch_query_set_omit_excluded (ctx->query, ctx->exclude);
589     }
590
591     return 0;
592 }
593
594 static void
595 _notmuch_search_cleanup (search_context_t *ctx)
596 {
597     notmuch_query_destroy (ctx->query);
598     notmuch_database_destroy (ctx->notmuch);
599
600     talloc_free (ctx->format);
601 }
602
603 static search_context_t search_context = {
604     .format_sel = NOTMUCH_FORMAT_TEXT,
605     .exclude = NOTMUCH_EXCLUDE_TRUE,
606     .sort = NOTMUCH_SORT_NEWEST_FIRST,
607     .output = 0,
608     .offset = 0,
609     .limit = -1, /* unlimited */
610     .dupe = -1,
611 };
612
613 static const notmuch_opt_desc_t common_options[] = {
614     { NOTMUCH_OPT_KEYWORD, &search_context.sort, "sort", 's',
615       (notmuch_keyword_t []){ { "oldest-first", NOTMUCH_SORT_OLDEST_FIRST },
616                               { "newest-first", NOTMUCH_SORT_NEWEST_FIRST },
617                               { 0, 0 } } },
618     { NOTMUCH_OPT_KEYWORD, &search_context.format_sel, "format", 'f',
619       (notmuch_keyword_t []){ { "json", NOTMUCH_FORMAT_JSON },
620                               { "sexp", NOTMUCH_FORMAT_SEXP },
621                               { "text", NOTMUCH_FORMAT_TEXT },
622                               { "text0", NOTMUCH_FORMAT_TEXT0 },
623                               { 0, 0 } } },
624     { NOTMUCH_OPT_INT, &notmuch_format_version, "format-version", 0, 0 },
625     { NOTMUCH_OPT_KEYWORD, &search_context.exclude, "exclude", 'x',
626       (notmuch_keyword_t []){ { "true", NOTMUCH_EXCLUDE_TRUE },
627                               { "false", NOTMUCH_EXCLUDE_FALSE },
628                               { "flag", NOTMUCH_EXCLUDE_FLAG },
629                               { "all", NOTMUCH_EXCLUDE_ALL },
630                               { 0, 0 } } },
631     { 0, 0, 0, 0, 0 }
632 };
633
634 int
635 notmuch_search_command (notmuch_config_t *config, int argc, char *argv[])
636 {
637     search_context_t *ctx = &search_context;
638     int opt_index, ret;
639
640     notmuch_opt_desc_t options[] = {
641         { NOTMUCH_OPT_KEYWORD, &ctx->output, "output", 'o',
642           (notmuch_keyword_t []){ { "summary", OUTPUT_SUMMARY },
643                                   { "threads", OUTPUT_THREADS },
644                                   { "messages", OUTPUT_MESSAGES },
645                                   { "files", OUTPUT_FILES },
646                                   { "tags", OUTPUT_TAGS },
647                                   { 0, 0 } } },
648         { NOTMUCH_OPT_INT, &ctx->offset, "offset", 'O', 0 },
649         { NOTMUCH_OPT_INT, &ctx->limit, "limit", 'L', 0  },
650         { NOTMUCH_OPT_INT, &ctx->dupe, "duplicate", 'D', 0  },
651         { NOTMUCH_OPT_INHERIT, &common_options, NULL, 0, 0 },
652         { 0, 0, 0, 0, 0 }
653     };
654
655     ctx->output = OUTPUT_SUMMARY;
656     opt_index = parse_arguments (argc, argv, options, 1);
657     if (opt_index < 0)
658         return EXIT_FAILURE;
659
660     if (_notmuch_search_prepare (ctx, config,
661                                  argc - opt_index, argv + opt_index))
662         return EXIT_FAILURE;
663
664     switch (ctx->output) {
665     case OUTPUT_SUMMARY:
666     case OUTPUT_THREADS:
667         ret = do_search_threads (ctx);
668         break;
669     case OUTPUT_MESSAGES:
670     case OUTPUT_FILES:
671         ret = do_search_messages (ctx);
672         break;
673     case OUTPUT_TAGS:
674         ret = do_search_tags (ctx);
675         break;
676     default:
677         INTERNAL_ERROR ("Unexpected output");
678     }
679
680     _notmuch_search_cleanup (ctx);
681
682     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
683 }
684
685 int
686 notmuch_address_command (notmuch_config_t *config, int argc, char *argv[])
687 {
688     search_context_t *ctx = &search_context;
689     int opt_index, ret;
690
691     notmuch_opt_desc_t options[] = {
692         { NOTMUCH_OPT_KEYWORD_FLAGS, &ctx->output, "output", 'o',
693           (notmuch_keyword_t []){ { "sender", OUTPUT_SENDER },
694                                   { "recipients", OUTPUT_RECIPIENTS },
695                                   { "count", OUTPUT_COUNT },
696                                   { 0, 0 } } },
697         { NOTMUCH_OPT_INHERIT, &common_options, NULL, 0, 0 },
698         { 0, 0, 0, 0, 0 }
699     };
700
701     opt_index = parse_arguments (argc, argv, options, 1);
702     if (opt_index < 0)
703         return EXIT_FAILURE;
704
705     if (! ctx->output)
706         search_context.output = OUTPUT_SENDER | OUTPUT_RECIPIENTS;
707
708     if (_notmuch_search_prepare (ctx, config,
709                                  argc - opt_index, argv + opt_index))
710         return EXIT_FAILURE;
711
712     ctx->addresses = g_hash_table_new_full (g_str_hash, g_str_equal,
713                                             _my_talloc_free_for_g_hash, _my_talloc_free_for_g_hash);
714
715     ret = do_search_messages (ctx);
716
717     g_hash_table_unref (ctx->addresses);
718
719
720     _notmuch_search_cleanup (ctx);
721
722     return ret ? EXIT_FAILURE : EXIT_SUCCESS;
723 }