view src/doveadm/doveadm-print-tab.c @ 19604:c996bc091c6b

master: Do not close stdout if going foreground This lets one to use /dev/stdout for logging. Mainly useful for testing purposes where we can generate log output to stdout and use tee to write it to a file for later examination.
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Mon, 18 Jan 2016 15:50:23 +0200
parents 0f22db71df7a
children e7e593e68fce
line wrap: on
line source

/* Copyright (c) 2010-2016 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "array.h"
#include "doveadm-print-private.h"

#include <stdio.h>

struct doveadm_print_tab_context {
	unsigned int header_idx, header_count;

	unsigned int header_written:1;
};

static struct doveadm_print_tab_context ctx;

static void doveadm_print_tab_flush_header(void)
{
	if (!ctx.header_written) {
		if (!doveadm_print_hide_titles)
			printf("\n");
		ctx.header_written = TRUE;
	}
}

static void
doveadm_print_tab_header(const struct doveadm_print_header *hdr)
{
	ctx.header_count++;
	if (!doveadm_print_hide_titles) {
		if (ctx.header_count > 1)
			printf("\t");
		printf("%s", hdr->title);
	}
}

static void doveadm_print_tab_print(const char *value)
{
	doveadm_print_tab_flush_header();
	if (ctx.header_idx > 0)
		printf("\t");
	printf("%s", value);

	if (++ctx.header_idx == ctx.header_count) {
		ctx.header_idx = 0;
		printf("\n");
	}
}

static void
doveadm_print_tab_print_stream(const unsigned char *value, size_t size)
{
	if (size == 0) {
		doveadm_print_tab_print("");
		return;
	}
	doveadm_print_tab_flush_header();
	if (ctx.header_idx > 0)
		printf("\t");
	fwrite(value, 1, size, stdout);
}

static void doveadm_print_tab_flush(void)
{
	doveadm_print_tab_flush_header();
}

struct doveadm_print_vfuncs doveadm_print_tab_vfuncs = {
	"tab",

	NULL,
	NULL,
	doveadm_print_tab_header,
	doveadm_print_tab_print,
	doveadm_print_tab_print_stream,
	doveadm_print_tab_flush
};