]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
doveadm: Added "pager" formatter and a function to output streamed output values.
authorTimo Sirainen <tss@iki.fi>
Mon, 21 Jun 2010 20:46:20 +0000 (21:46 +0100)
committerTimo Sirainen <tss@iki.fi>
Mon, 21 Jun 2010 20:46:20 +0000 (21:46 +0100)
--HG--
branch : HEAD

src/doveadm/Makefile.am
src/doveadm/doveadm-print-flow.c
src/doveadm/doveadm-print-pager.c [new file with mode: 0644]
src/doveadm/doveadm-print-private.h
src/doveadm/doveadm-print-tab.c
src/doveadm/doveadm-print-table.c
src/doveadm/doveadm-print.c
src/doveadm/doveadm-print.h

index 36dc756e357a5cd3dca881a964281c76c32e9cce..805a369c54311e7ca845c4503a32c8a0796788a1 100644 (file)
@@ -66,6 +66,7 @@ doveadm_SOURCES = \
        doveadm-penalty.c \
        doveadm-print.c \
        doveadm-print-flow.c \
+       doveadm-print-pager.c \
        doveadm-print-tab.c \
        doveadm-print-table.c \
        doveadm-pw.c \
index fb24a159553200af1c77649591c2d26d33cacf54..4449f8684dcc469eee69b01210d9c9f64fcc63b7 100644 (file)
@@ -15,6 +15,8 @@ struct doveadm_print_flow_context {
        pool_t pool;
        ARRAY_DEFINE(headers, struct doveadm_print_flow_header);
        unsigned int header_idx;
+
+       unsigned int streaming:1;
 };
 
 static struct doveadm_print_flow_context *ctx;
@@ -29,6 +31,16 @@ doveadm_print_flow_header(const struct doveadm_print_header *hdr)
        fhdr->flags = hdr->flags;
 }
 
+static void flow_next_hdr(void)
+{
+       if (++ctx->header_idx < array_count(&ctx->headers))
+               printf(" ");
+       else {
+               ctx->header_idx = 0;
+               printf("\n");
+       }
+}
+
 static void doveadm_print_flow_print(const char *value)
 {
        const struct doveadm_print_flow_header *hdr =
@@ -37,12 +49,24 @@ static void doveadm_print_flow_print(const char *value)
        if ((hdr->flags & DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE) == 0)
                printf("%s=", hdr->title);
        printf("%s", value);
+       flow_next_hdr();
+}
 
-       if (++ctx->header_idx < array_count(&ctx->headers))
-               printf(" ");
-       else {
-               ctx->header_idx = 0;
-               printf("\n");
+static void
+doveadm_print_flow_print_stream(const unsigned char *value, size_t size)
+{
+       const struct doveadm_print_flow_header *hdr =
+               array_idx(&ctx->headers, ctx->header_idx);
+
+       if (!ctx->streaming) {
+               ctx->streaming = TRUE;
+               if ((hdr->flags & DOVEADM_PRINT_HEADER_FLAG_HIDE_TITLE) == 0)
+                       printf("%s=", hdr->title);
+       }
+       printf("%.*s", (int)size, value);
+       if (size == 0) {
+               flow_next_hdr();
+               ctx->streaming = FALSE;
        }
 }
 
@@ -77,5 +101,6 @@ struct doveadm_print_vfuncs doveadm_print_flow_vfuncs = {
        doveadm_print_flow_deinit,
        doveadm_print_flow_header,
        doveadm_print_flow_print,
+       doveadm_print_flow_print_stream,
        doveadm_print_flow_flush
 };
diff --git a/src/doveadm/doveadm-print-pager.c b/src/doveadm/doveadm-print-pager.c
new file mode 100644 (file)
index 0000000..6705a0c
--- /dev/null
@@ -0,0 +1,99 @@
+/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "array.h"
+#include "doveadm-print-private.h"
+
+#include <stdio.h>
+
+struct doveadm_print_pager_header {
+       const char *title;
+};
+
+struct doveadm_print_pager_context {
+       pool_t pool;
+       ARRAY_DEFINE(headers, struct doveadm_print_pager_header);
+       unsigned int header_idx;
+
+       unsigned int streaming:1;
+};
+
+static struct doveadm_print_pager_context *ctx;
+
+static void
+doveadm_print_pager_header(const struct doveadm_print_header *hdr)
+{
+       struct doveadm_print_pager_header *fhdr;
+
+       fhdr = array_append_space(&ctx->headers);
+       fhdr->title = p_strdup(ctx->pool, hdr->title);
+}
+
+static void pager_next_hdr(void)
+{
+       if (++ctx->header_idx == array_count(&ctx->headers)) {
+               ctx->header_idx = 0;
+               printf("\x0c"); /* ^L */
+       }
+}
+
+static void doveadm_print_pager_print(const char *value)
+{
+       const struct doveadm_print_pager_header *hdr =
+               array_idx(&ctx->headers, ctx->header_idx);
+
+       printf("%s: %s\n", hdr->title, value);
+       pager_next_hdr();
+}
+
+static void
+doveadm_print_pager_print_stream(const unsigned char *value, size_t size)
+{
+       const struct doveadm_print_pager_header *hdr =
+               array_idx(&ctx->headers, ctx->header_idx);
+
+       if (!ctx->streaming) {
+               ctx->streaming = TRUE;
+               printf("%s:\n", hdr->title);
+       }
+       printf("%.*s", (int)size, value);
+       if (size == 0) {
+               pager_next_hdr();
+               ctx->streaming = FALSE;
+       }
+}
+
+static void doveadm_print_pager_init(void)
+{
+       pool_t pool;
+
+       pool = pool_alloconly_create("doveadm print pager", 1024);
+       ctx = p_new(pool, struct doveadm_print_pager_context, 1);
+       ctx->pool = pool;
+       p_array_init(&ctx->headers, pool, 16);
+}
+
+static void doveadm_print_pager_flush(void)
+{
+       if (ctx->header_idx != 0) {
+               printf("\n");
+               ctx->header_idx = 0;
+       }
+}
+
+static void doveadm_print_pager_deinit(void)
+{
+       pool_unref(&ctx->pool);
+       ctx = NULL;
+}
+
+struct doveadm_print_vfuncs doveadm_print_pager_vfuncs = {
+       "pager",
+
+       doveadm_print_pager_init,
+       doveadm_print_pager_deinit,
+       doveadm_print_pager_header,
+       doveadm_print_pager_print,
+       doveadm_print_pager_print_stream,
+       doveadm_print_pager_flush
+};
index 17c8ae8f72ac07d35a91dae55ad9b5a7674a0dad..854bfc70e74622560eb0af3f7ce9545e069398ee 100644 (file)
@@ -17,11 +17,13 @@ struct doveadm_print_vfuncs {
 
        void (*header)(const struct doveadm_print_header *hdr);
        void (*print)(const char *value);
+       void (*print_stream)(const unsigned char *value, size_t size);
        void (*flush)(void);
 };
 
 extern struct doveadm_print_vfuncs doveadm_print_flow_vfuncs;
 extern struct doveadm_print_vfuncs doveadm_print_tab_vfuncs;
 extern struct doveadm_print_vfuncs doveadm_print_table_vfuncs;
+extern struct doveadm_print_vfuncs doveadm_print_pager_vfuncs;
 
 #endif
index 35ececa2e04ec7bbd79821aff767809cda2fef57..51d68b86018548952173b37c9ca94b8320d3eeed 100644 (file)
@@ -40,6 +40,22 @@ static void doveadm_print_tab_print(const char *value)
        }
 }
 
+static void
+doveadm_print_tab_print_stream(const unsigned char *value, size_t size)
+{
+       if (size == 0) {
+               doveadm_print_tab_print("");
+               return;
+       }
+       if (!ctx.header_written) {
+               printf("\n");
+               ctx.header_written = TRUE;
+       }
+       if (ctx.header_idx > 0)
+               printf("\t");
+       printf("%.*s", (int)size, value);
+}
+
 static void doveadm_print_tab_flush(void)
 {
        if (!ctx.header_written) {
@@ -55,5 +71,6 @@ struct doveadm_print_vfuncs doveadm_print_tab_vfuncs = {
        NULL,
        doveadm_print_tab_header,
        doveadm_print_tab_print,
+       doveadm_print_tab_print_stream,
        doveadm_print_tab_flush
 };
index 3fb972a09b8d4dbbe39b8807799e7f7089c921a1..17f6cbdaa3fa080e9063f22b123b6f24502b1af4 100644 (file)
@@ -171,6 +171,13 @@ static void doveadm_print_table_print(const char *value)
        doveadm_print_next(value);
 }
 
+static void
+doveadm_print_table_print_stream(const unsigned char *value ATTR_UNUSED,
+                                size_t size ATTR_UNUSED)
+{
+       i_fatal("table formatter doesn't support multi-line values");
+}
+
 static void doveadm_print_table_flush(void)
 {
        if (!ctx->lengths_set && array_count(&ctx->headers) > 0)
@@ -209,5 +216,6 @@ struct doveadm_print_vfuncs doveadm_print_table_vfuncs = {
        doveadm_print_table_deinit,
        doveadm_print_table_header,
        doveadm_print_table_print,
+       doveadm_print_table_print_stream,
        doveadm_print_table_flush
 };
index fbd5b6a40d77355b48520e9395326c11e41fb424..0d47c27160ac0330030bde775465ca279f64b755 100644 (file)
@@ -24,7 +24,8 @@ static struct doveadm_print_context *ctx;
 static const struct doveadm_print_vfuncs *doveadm_print_vfuncs_all[] = {
        &doveadm_print_flow_vfuncs,
        &doveadm_print_tab_vfuncs,
-       &doveadm_print_table_vfuncs
+       &doveadm_print_table_vfuncs,
+       &doveadm_print_pager_vfuncs
 };
 
 bool doveadm_print_is_initialized(void)
@@ -85,6 +86,11 @@ void doveadm_print_num(uintmax_t value)
        } T_END;
 }
 
+void doveadm_print_stream(const void *value, size_t size)
+{
+       ctx->v->print_stream(value, size);
+}
+
 void doveadm_print_sticky(const char *key, const char *value)
 {
        struct doveadm_print_header_context *hdr;
index 33e8a13c1a4bf2a7589922c1612b9228bf46f64c..cf5c9a42e4212e268890f49b2b00ef98b230fbfd 100644 (file)
@@ -17,6 +17,8 @@ void doveadm_print_header(const char *key, const char *title,
 void doveadm_print_header_simple(const char *key_title);
 void doveadm_print(const char *value);
 void doveadm_print_num(uintmax_t value);
+/* Stream for same field continues until len=0 */
+void doveadm_print_stream(const void *value, size_t size);
 void doveadm_print_sticky(const char *key, const char *value);
 void doveadm_print_flush(void);