doveadm-print-server.c revision d29c67900d3d104e24ed213ed4a10d204decdf18
1008N/A/* Copyright (c) 2010-2017 Dovecot authors, see the included COPYING file */
1008N/A
1008N/A#include "lib.h"
1008N/A#include "array.h"
1008N/A#include "str.h"
1008N/A#include "strescape.h"
1008N/A#include "ostream.h"
1008N/A#include "doveadm.h"
1008N/A#include "doveadm-print-private.h"
1008N/A#include "client-connection.h"
1008N/A
1008N/Astruct doveadm_print_server_context {
1008N/A unsigned int header_idx, header_count;
1008N/A
1008N/A string_t *str;
1008N/A};
1008N/A
1008N/Astatic struct doveadm_print_server_context ctx;
1008N/A
1008N/Astatic void doveadm_print_server_flush(void);
1008N/A
1008N/Astatic void doveadm_print_server_init(void)
1008N/A{
1008N/A ctx.str = str_new(default_pool, 256);
4129N/A}
1008N/A
1008N/Astatic void doveadm_print_server_deinit(void)
2371N/A{
1008N/A str_free(&ctx.str);
1008N/A}
2371N/A
1008N/Astatic void
1008N/Adoveadm_print_server_header(const struct doveadm_print_header *hdr ATTR_UNUSED)
1008N/A{
1008N/A /* no need to transfer these. the client should already know what
2371N/A it's getting */
2371N/A ctx.header_count++;
1008N/A}
2371N/A
1008N/Astatic void doveadm_print_server_print(const char *value)
4129N/A{
1008N/A str_append_tabescaped(ctx.str, value);
2371N/A str_append_c(ctx.str, '\t');
1008N/A
1008N/A if (++ctx.header_idx == ctx.header_count) {
4129N/A ctx.header_idx = 0;
4129N/A doveadm_print_server_flush();
1008N/A }
1008N/A}
2371N/A
2371N/Astatic void
1008N/Adoveadm_print_server_print_stream(const unsigned char *value, size_t size)
1008N/A{
1008N/A if (size == 0) {
2371N/A doveadm_print_server_print("");
1008N/A return;
2371N/A }
1008N/A str_append_tabescaped_n(ctx.str, value, size);
1008N/A
1008N/A if (str_len(ctx.str) >= IO_BLOCK_SIZE)
1008N/A doveadm_print_server_flush();
3065N/A}
1008N/A
1008N/Astatic void doveadm_print_server_flush(void)
1008N/A{
1008N/A o_stream_nsend(doveadm_print_ostream,
1008N/A str_data(ctx.str), str_len(ctx.str));
1008N/A str_truncate(ctx.str, 0);
1008N/A}
1008N/A
2371N/Astruct doveadm_print_vfuncs doveadm_print_server_vfuncs = {
2371N/A DOVEADM_PRINT_TYPE_SERVER,
2371N/A
2371N/A doveadm_print_server_init,
2371N/A doveadm_print_server_deinit,
2371N/A doveadm_print_server_header,
2371N/A doveadm_print_server_print,
1008N/A doveadm_print_server_print_stream,
1008N/A doveadm_print_server_flush
1008N/A};
1008N/A