doveadm-print-server.c revision d29c67900d3d104e24ed213ed4a10d204decdf18
2454dfa32c93c20a8522c6ed42fe057baaac9f9aStephan Bosch/* Copyright (c) 2010-2017 Dovecot authors, see the included COPYING file */
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "lib.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "array.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "str.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "strescape.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "ostream.h"
d29c67900d3d104e24ed213ed4a10d204decdf18Aki Tuomi#include "doveadm.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen#include "doveadm-print-private.h"
d29c67900d3d104e24ed213ed4a10d204decdf18Aki Tuomi#include "client-connection.h"
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstruct doveadm_print_server_context {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen unsigned int header_idx, header_count;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen string_t *str;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen};
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic struct doveadm_print_server_context ctx;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void doveadm_print_server_flush(void);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void doveadm_print_server_init(void)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen ctx.str = str_new(default_pool, 256);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void doveadm_print_server_deinit(void)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen str_free(&ctx.str);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainendoveadm_print_server_header(const struct doveadm_print_header *hdr ATTR_UNUSED)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen /* no need to transfer these. the client should already know what
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen it's getting */
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen ctx.header_count++;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void doveadm_print_server_print(const char *value)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
d03a871a77f8ec36f48f5fea98d810e51b186fdbTimo Sirainen str_append_tabescaped(ctx.str, value);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen str_append_c(ctx.str, '\t');
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen if (++ctx.header_idx == ctx.header_count) {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen ctx.header_idx = 0;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_flush();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen }
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainendoveadm_print_server_print_stream(const unsigned char *value, size_t size)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen if (size == 0) {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_print("");
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen return;
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen }
cde4b81189fb795542b14a89edefee7e5fc71949Aki Tuomi str_append_tabescaped_n(ctx.str, value, size);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen if (str_len(ctx.str) >= IO_BLOCK_SIZE)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_flush();
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstatic void doveadm_print_server_flush(void)
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen{
57591c5ea045b6829ebcfed9b145d719d63f935aTimo Sirainen o_stream_nsend(doveadm_print_ostream,
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen str_data(ctx.str), str_len(ctx.str));
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen str_truncate(ctx.str, 0);
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen}
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainenstruct doveadm_print_vfuncs doveadm_print_server_vfuncs = {
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen DOVEADM_PRINT_TYPE_SERVER,
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_init,
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_deinit,
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_header,
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_print,
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_print_stream,
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen doveadm_print_server_flush
71df09024cea5f2faa93da3bb9513ee96ba6bf22Timo Sirainen};