]> git.notmuchmail.org Git - notmuch/blob - test/hex-xcode.c
cli: use designated initializers for opt desc
[notmuch] / test / hex-xcode.c
1 /* No, nothing to to with IDE from Apple Inc.
2  * testbed for ../util/hex-escape.c.
3  *
4  * usage:
5  * hex-xcode [--direction=(encode|decode)] [--omit-newline] < file
6  * hex-xcode [--direction=(encode|decode)] [--omit-newline] [--in-place] arg1 arg2 arg3 ...
7  *
8  */
9
10 #include "notmuch-client.h"
11 #include "hex-escape.h"
12 #include <assert.h>
13
14 enum direction {
15     ENCODE,
16     DECODE
17 };
18
19 static int inplace = FALSE;
20
21 static int
22 xcode (void *ctx, enum direction dir, char *in, char **buf_p, size_t *size_p)
23 {
24     hex_status_t status;
25
26     if (dir == ENCODE)
27         status = hex_encode (ctx, in, buf_p, size_p);
28     else
29         if (inplace) {
30             status = hex_decode_inplace (in);
31             *buf_p = in;
32             *size_p = strlen(in);
33         } else {
34             status = hex_decode (ctx, in, buf_p, size_p);
35         }
36
37     if (status == HEX_SUCCESS)
38         fputs (*buf_p, stdout);
39
40     return status;
41 }
42
43 int
44 main (int argc, char **argv)
45 {
46
47     int dir = DECODE;
48     int omit_newline = FALSE;
49
50     notmuch_opt_desc_t options[] = {
51         { .opt_keyword = &dir, .name = "direction", .keywords =
52           (notmuch_keyword_t []){ { "encode", ENCODE },
53                                   { "decode", DECODE },
54                                   { 0, 0 } } },
55         { .opt_bool = &omit_newline, .name = "omit-newline" },
56         { .opt_bool = &inplace, .name = "in-place" },
57         { }
58     };
59
60     int opt_index = parse_arguments (argc, argv, options, 1);
61
62     if (opt_index < 0)
63         exit (1);
64
65     void *ctx = talloc_new (NULL);
66
67     char *line = NULL;
68     size_t line_size;
69     ssize_t line_len;
70
71     char *buffer = NULL;
72     size_t buf_size = 0;
73
74     notmuch_bool_t read_stdin = TRUE;
75
76     for (; opt_index < argc; opt_index++) {
77
78         if (xcode (ctx, dir, argv[opt_index],
79                    &buffer, &buf_size) != HEX_SUCCESS)
80             return 1;
81
82         if (! omit_newline)
83             putchar ('\n');
84
85         read_stdin = FALSE;
86     }
87
88     if (! read_stdin)
89         return 0;
90
91     while ((line_len = getline (&line, &line_size, stdin)) != -1) {
92
93         chomp_newline (line);
94
95         if (xcode (ctx, dir, line, &buffer, &buf_size) != HEX_SUCCESS)
96             return 1;
97
98         if (! omit_newline)
99             putchar ('\n');
100
101     }
102
103     if (line)
104         free (line);
105
106     talloc_free (ctx);
107
108     return 0;
109 }