doveadm-print-pager.c revision 80fbf2394fb1071e2bcc50a8c1da1fe876a961a2
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi/* Copyright (c) 2010-2012 Dovecot authors, see the included COPYING file */
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi#include "lib.h"
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi#include "array.h"
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi#include "doveadm-print-private.h"
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi#include <stdio.h>
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashistruct doveadm_print_pager_header {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi const char *title;
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen};
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowenstruct doveadm_print_pager_context {
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen pool_t pool;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi ARRAY_DEFINE(headers, struct doveadm_print_pager_header);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi unsigned int header_idx;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
d229f940abfb2490dee17979e9a5ff31b7012eb5rbowen unsigned int streaming:1;
3f08db06526d6901aa08c110b5bc7dde6bc39905nd};
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashistatic struct doveadm_print_pager_context *ctx;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
3f08db06526d6901aa08c110b5bc7dde6bc39905ndstatic void
1d980e5489836e977ba59b419e27b0ec875c4bd3takashidoveadm_print_pager_header(const struct doveadm_print_header *hdr)
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi{
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi struct doveadm_print_pager_header *fhdr;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
11495c9f0bd33e51a25b4d532beadfbcf9b944a3nilgun fhdr = array_append_space(&ctx->headers);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi fhdr->title = p_strdup(ctx->pool, hdr->title);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi}
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung
1d980e5489836e977ba59b419e27b0ec875c4bd3takashistatic void pager_next_hdr(void)
645cf915f6bc22be17750bc5bb34ade8de6744dfnd{
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi if (++ctx->header_idx == array_count(&ctx->headers)) {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi ctx->header_idx = 0;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi printf("\f\n");
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi }
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi}
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashistatic void doveadm_print_pager_print(const char *value)
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi{
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi const struct doveadm_print_pager_header *hdr =
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi array_idx(&ctx->headers, ctx->header_idx);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi printf("%s: %s\n", hdr->title, value);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi pager_next_hdr();
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi}
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashistatic void
1d980e5489836e977ba59b419e27b0ec875c4bd3takashidoveadm_print_pager_print_stream(const unsigned char *value, size_t size)
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi{
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi const struct doveadm_print_pager_header *hdr =
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi array_idx(&ctx->headers, ctx->header_idx);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi if (!ctx->streaming) {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi ctx->streaming = TRUE;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi printf("%s:\n", hdr->title);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi }
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi fwrite(value, 1, size, stdout);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi if (size == 0) {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi pager_next_hdr();
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi ctx->streaming = FALSE;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi }
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi}
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgun
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgunstatic void doveadm_print_pager_init(void)
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi{
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi pool_t pool;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi pool = pool_alloconly_create("doveadm print pager", 1024);
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgun ctx = p_new(pool, struct doveadm_print_pager_context, 1);
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgun ctx->pool = pool;
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgun p_array_init(&ctx->headers, pool, 16);
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgun}
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashistatic void doveadm_print_pager_flush(void)
b9f522ae1c0ed2bf3fc4444245bf28b2e2449a65nd{
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi if (ctx->header_idx != 0) {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi printf("\n");
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi ctx->header_idx = 0;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi }
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi}
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgun
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedoohstatic void doveadm_print_pager_deinit(void)
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi{
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi pool_unref(&ctx->pool);
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi ctx = NULL;
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi}
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgunstruct doveadm_print_vfuncs doveadm_print_pager_vfuncs = {
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi "pager",
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi doveadm_print_pager_init,
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi doveadm_print_pager_deinit,
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi doveadm_print_pager_header,
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi doveadm_print_pager_print,
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi doveadm_print_pager_print_stream,
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi doveadm_print_pager_flush
b2a930a0c94e9fd25f8d2b3a2c53573235db3f06nilgun};
1d980e5489836e977ba59b419e27b0ec875c4bd3takashi