1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* Copyright (c) 2010-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "array.h"
#include "istream.h"
#include "ostream.h"
#include "doveadm-print-private.h"
struct doveadm_print_header_context {
const char *key;
char *sticky_value;
bool sticky;
};
struct doveadm_print_context {
pool_t pool;
ARRAY(struct doveadm_print_header_context) headers;
const struct doveadm_print_vfuncs *v;
unsigned int header_idx;
bool print_stream_open;
};
bool doveadm_print_hide_titles = FALSE;
struct ostream *doveadm_print_ostream = NULL;
static struct doveadm_print_context *ctx;
bool doveadm_print_is_initialized(void)
{
return ctx != NULL;
}
void doveadm_print_header(const char *key, const char *title,
enum doveadm_print_header_flags flags)
{
struct doveadm_print_header hdr;
struct doveadm_print_header_context *hdr_ctx;
i_assert(title != NULL);
i_zero(&hdr);
hdr.key = key;
hdr.title = title;
hdr.flags = flags;
ctx->v->header(&hdr);
hdr_ctx = array_append_space(&ctx->headers);
hdr_ctx->key = p_strdup(ctx->pool, key);
hdr_ctx->sticky = (flags & DOVEADM_PRINT_HEADER_FLAG_STICKY) != 0;
}
void doveadm_print_header_simple(const char *key_title)
{
doveadm_print_header(key_title, key_title, 0);
}
unsigned int doveadm_print_get_headers_count(void)
{
return array_count(&ctx->headers);
}
static void doveadm_print_sticky_headers(void)
{
const struct doveadm_print_header_context *headers;
unsigned int count;
headers = array_get(&ctx->headers, &count);
i_assert(count > 0);
for (;;) {
if (ctx->header_idx == count)
ctx->header_idx = 0;
else if (headers[ctx->header_idx].sticky) {
ctx->v->print(headers[ctx->header_idx].sticky_value);
ctx->header_idx++;
} else {
break;
}
}
}
void doveadm_print(const char *value)
{
i_assert(!ctx->print_stream_open);
doveadm_print_sticky_headers();
ctx->v->print(value);
ctx->header_idx++;
}
void doveadm_print_num(uintmax_t value)
{
T_BEGIN {
doveadm_print(dec2str(value));
} T_END;
}
void doveadm_print_stream(const void *value, size_t size)
{
if (!ctx->print_stream_open) {
doveadm_print_sticky_headers();
ctx->print_stream_open = TRUE;
}
ctx->v->print_stream(value, size);
if (size == 0) {
ctx->header_idx++;
ctx->print_stream_open = FALSE;
}
}
int doveadm_print_istream(struct istream *input)
{
const unsigned char *data;
size_t size;
ssize_t ret;
while ((ret = i_stream_read_more(input, &data, &size)) > 0) {
doveadm_print_stream(data, size);
i_stream_skip(input, size);
}
i_assert(ret == -1);
doveadm_print_stream("", 0);
if (input->stream_errno != 0) {
/* caller will log the error */
return -1;
}
return 0;
}
void doveadm_print_sticky(const char *key, const char *value)
{
struct doveadm_print_header_context *hdr;
if (ctx == NULL) {
/* command doesn't really print anything */
return;
}
array_foreach_modifiable(&ctx->headers, hdr) {
if (strcmp(hdr->key, key) == 0) {
i_free(hdr->sticky_value);
hdr->sticky_value = i_strdup(value);
return;
}
}
i_unreached();
}
void doveadm_print_flush(void)
{
if (ctx != NULL && ctx->v->flush != NULL)
ctx->v->flush();
o_stream_uncork(doveadm_print_ostream);
o_stream_cork(doveadm_print_ostream);
}
void doveadm_print_unstick_headers(void)
{
struct doveadm_print_header_context *hdr;
if (ctx != NULL) {
array_foreach_modifiable(&ctx->headers, hdr)
hdr->sticky = FALSE;
}
}
void doveadm_print_init(const char *name)
{
pool_t pool;
unsigned int i;
if (ctx != NULL) {
/* already forced the type */
return;
}
pool = pool_alloconly_create("doveadm print", 1024);
ctx = p_new(pool, struct doveadm_print_context, 1);
ctx->pool = pool;
p_array_init(&ctx->headers, pool, 16);
for (i = 0; doveadm_print_vfuncs_all[i] != NULL; i++) {
if (strcmp(doveadm_print_vfuncs_all[i]->name, name) == 0) {
ctx->v = doveadm_print_vfuncs_all[i];
break;
}
}
if (ctx->v == NULL)
i_fatal("Unknown print formatter: %s", name);
if (ctx->v->init != NULL)
ctx->v->init();
}
void doveadm_print_deinit(void)
{
struct doveadm_print_header_context *hdr;
if (ctx == NULL)
return;
if (ctx->v->flush != NULL && doveadm_print_ostream != NULL) {
ctx->v->flush();
o_stream_uncork(doveadm_print_ostream);
}
if (ctx->v->deinit != NULL)
ctx->v->deinit();
array_foreach_modifiable(&ctx->headers, hdr)
i_free(hdr->sticky_value);
pool_unref(&ctx->pool);
ctx = NULL;
}
|