]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/tt: don't hardcode stdout as output
authorKarel Zak <kzak@redhat.com>
Fri, 22 Nov 2013 10:34:12 +0000 (11:34 +0100)
committerKarel Zak <kzak@redhat.com>
Tue, 11 Mar 2014 10:35:12 +0000 (11:35 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
include/tt.h
lib/tt.c

index 6ae8de3b20fcd17bd55897d7fafbca31ab314973..11bd81e4bd8326b2664a4afbc63e0aae08905115 100644 (file)
@@ -33,6 +33,7 @@ enum {
 };
 
 struct tt {
+       FILE    *out;           /* output stream */
        size_t  ncols;          /* number of columns */
        size_t  termwidth;      /* terminal width */
        int     is_term;        /* is a tty? */
@@ -79,6 +80,7 @@ extern struct tt *tt_new_table(int flags);
 extern void tt_free_table(struct tt *tb);
 extern void tt_remove_lines(struct tt *tb);
 extern int tt_print_table(struct tt *tb);
+extern void tt_set_stream(struct tt *tb, FILE *out);
 
 extern struct tt_column *tt_define_column(struct tt *tb, const char *name,
                                                double whint, int flags);
index e701becfde3fd3504202e737e53a782563e2ed81..46fc00e8786edae42b6b93d2d2f6218dedcd8af9 100644 (file)
--- a/lib/tt.c
+++ b/lib/tt.c
@@ -201,6 +201,7 @@ struct tt *tt_new_table(int flags)
                return NULL;
 
        tb->flags = flags;
+       tb->out = stdout;
        INIT_LIST_HEAD(&tb->tb_lines);
        INIT_LIST_HEAD(&tb->tb_columns);
 
@@ -215,6 +216,13 @@ struct tt *tt_new_table(int flags)
        return tb;
 }
 
+void tt_set_stream(struct tt *tb, FILE *out)
+{
+       if (!tb)
+               return;
+       tb->out = out;
+}
+
 void tt_remove_lines(struct tt *tb)
 {
        if (!tb)
@@ -721,18 +729,18 @@ static void print_data(struct tt *tb, struct tt_column *cl, char *data)
 
        /* raw mode */
        if (tb->flags & TT_FL_RAW) {
-               tt_fputs_nonblank(data, stdout);
+               tt_fputs_nonblank(data, tb->out);
                if (!is_last_column(tb, cl))
-                       fputc(' ', stdout);
+                       fputc(' ', tb->out);
                return;
        }
 
        /* NAME=value mode */
        if (tb->flags & TT_FL_EXPORT) {
-               fprintf(stdout, "%s=", cl->name);
-               tt_fputs_quoted(data, stdout);
+               fprintf(tb->out, "%s=", cl->name);
+               tt_fputs_quoted(data, tb->out);
                if (!is_last_column(tb, cl))
-                       fputc(' ', stdout);
+                       fputc(' ', tb->out);
                return;
        }
 
@@ -763,25 +771,25 @@ static void print_data(struct tt *tb, struct tt_column *cl, char *data)
        if (data) {
                if (!(tb->flags & TT_FL_RAW) && (cl->flags & TT_FL_RIGHT)) {
                        size_t xw = cl->width;
-                       fprintf(stdout, "%*s", (int) xw, data);
+                       fprintf(tb->out, "%*s", (int) xw, data);
                        if (len < xw)
                                len = xw;
                }
                else
-                       fputs(data, stdout);
+                       fputs(data, tb->out);
        }
        for (i = len; i < width; i++)
-               fputc(' ', stdout);             /* padding */
+               fputc(' ', tb->out);            /* padding */
 
        if (!is_last_column(tb, cl)) {
                if (len > width && !(cl->flags & TT_FL_TRUNC)) {
-                       fputc('\n', stdout);
+                       fputc('\n', tb->out);
                        for (i = 0; i <= (size_t) cl->seqnum; i++) {
                                struct tt_column *x = tt_get_column(tb, i);
                                printf("%*s ", -((int)x->width), " ");
                        }
                } else
-                       fputc(' ', stdout);     /* columns separator */
+                       fputc(' ', tb->out);    /* columns separator */
        }
 
        free(buf);
@@ -799,7 +807,7 @@ static void print_line(struct tt_line *ln, char *buf, size_t bufsz)
 
                print_data(ln->table, cl, line_get_data(ln, cl, buf, bufsz));
        }
-       fputc('\n', stdout);
+       fputc('\n', ln->table->out);
 }
 
 static void print_header(struct tt *tb, char *buf, size_t bufsz)
@@ -822,7 +830,7 @@ static void print_header(struct tt *tb, char *buf, size_t bufsz)
                buf[bufsz - 1] = '\0';
                print_data(tb, cl, buf);
        }
-       fputc('\n', stdout);
+       fputc('\n', tb->out);
 }
 
 static void print_table(struct tt *tb, char *buf, size_t bufsz)
@@ -874,7 +882,7 @@ static void print_tree(struct tt *tb, char *buf, size_t bufsz)
 /*
  * @tb: table
  *
- * Prints the table to stdout
+ * Prints the table to tb->out
  */
 int tt_print_table(struct tt *tb)
 {