1/*-------------------------------------------------------------------------
2 *
3 * Query-result printing support for frontend code
4 *
5 *
6 * Portions Copyright (c) 1996-2019, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
8 *
9 * src/include/fe_utils/print.h
10 *
11 *-------------------------------------------------------------------------
12 */
13#ifndef PRINT_H
14#define PRINT_H
15
16#include "libpq-fe.h"
17
18
19/* This is not a particularly great place for this ... */
20#ifndef __CYGWIN__
21#define DEFAULT_PAGER "more"
22#else
23#define DEFAULT_PAGER "less"
24#endif
25
26enum printFormat
27{
28 PRINT_NOTHING = 0, /* to make sure someone initializes this */
29 PRINT_ALIGNED,
30 PRINT_ASCIIDOC,
31 PRINT_CSV,
32 PRINT_HTML,
33 PRINT_LATEX,
34 PRINT_LATEX_LONGTABLE,
35 PRINT_TROFF_MS,
36 PRINT_UNALIGNED,
37 PRINT_WRAPPED
38 /* add your favourite output format here ... */
39};
40
41typedef struct printTextLineFormat
42{
43 /* Line drawing characters to be used in various contexts */
44 const char *hrule; /* horizontal line character */
45 const char *leftvrule; /* left vertical line (+horizontal) */
46 const char *midvrule; /* intra-column vertical line (+horizontal) */
47 const char *rightvrule; /* right vertical line (+horizontal) */
48} printTextLineFormat;
49
50typedef enum printTextRule
51{
52 /* Additional context for selecting line drawing characters */
53 PRINT_RULE_TOP, /* top horizontal line */
54 PRINT_RULE_MIDDLE, /* intra-data horizontal line */
55 PRINT_RULE_BOTTOM, /* bottom horizontal line */
56 PRINT_RULE_DATA /* data line (hrule is unused here) */
57} printTextRule;
58
59typedef enum printTextLineWrap
60{
61 /* Line wrapping conditions */
62 PRINT_LINE_WRAP_NONE, /* No wrapping */
63 PRINT_LINE_WRAP_WRAP, /* Wraparound due to overlength line */
64 PRINT_LINE_WRAP_NEWLINE /* Newline in data */
65} printTextLineWrap;
66
67typedef struct printTextFormat
68{
69 /* A complete line style */
70 const char *name; /* for display purposes */
71 printTextLineFormat lrule[4]; /* indexed by enum printTextRule */
72 const char *midvrule_nl; /* vertical line for continue after newline */
73 const char *midvrule_wrap; /* vertical line for wrapped data */
74 const char *midvrule_blank; /* vertical line for blank data */
75 const char *header_nl_left; /* left mark after newline */
76 const char *header_nl_right; /* right mark for newline */
77 const char *nl_left; /* left mark after newline */
78 const char *nl_right; /* right mark for newline */
79 const char *wrap_left; /* left mark after wrapped data */
80 const char *wrap_right; /* right mark for wrapped data */
81 bool wrap_right_border; /* use right-hand border for wrap marks
82 * when border=0? */
83} printTextFormat;
84
85typedef enum unicode_linestyle
86{
87 UNICODE_LINESTYLE_SINGLE = 0,
88 UNICODE_LINESTYLE_DOUBLE
89} unicode_linestyle;
90
91struct separator
92{
93 char *separator;
94 bool separator_zero;
95};
96
97typedef struct printTableOpt
98{
99 enum printFormat format; /* see enum above */
100 unsigned short int expanded; /* expanded/vertical output (if supported
101 * by output format); 0=no, 1=yes, 2=auto */
102 unsigned short int border; /* Print a border around the table. 0=none,
103 * 1=dividing lines, 2=full */
104 unsigned short int pager; /* use pager for output (if to stdout and
105 * stdout is a tty) 0=off 1=on 2=always */
106 int pager_min_lines; /* don't use pager unless there are at
107 * least this many lines */
108 bool tuples_only; /* don't output headers, row counts, etc. */
109 bool start_table; /* print start decoration, eg <table> */
110 bool stop_table; /* print stop decoration, eg </table> */
111 bool default_footer; /* allow "(xx rows)" default footer */
112 unsigned long prior_records; /* start offset for record counters */
113 const printTextFormat *line_style; /* line style (NULL for default) */
114 struct separator fieldSep; /* field separator for unaligned text mode */
115 struct separator recordSep; /* record separator for unaligned text mode */
116 char csvFieldSep[2]; /* field separator for csv format */
117 bool numericLocale; /* locale-aware numeric units separator and
118 * decimal marker */
119 char *tableAttr; /* attributes for HTML <table ...> */
120 int encoding; /* character encoding */
121 int env_columns; /* $COLUMNS on psql start, 0 is unset */
122 int columns; /* target width for wrapped format */
123 unicode_linestyle unicode_border_linestyle;
124 unicode_linestyle unicode_column_linestyle;
125 unicode_linestyle unicode_header_linestyle;
126} printTableOpt;
127
128/*
129 * Table footers are implemented as a singly-linked list.
130 *
131 * This is so that you don't need to know the number of footers in order to
132 * initialise the printTableContent struct, which is very convenient when
133 * preparing complex footers (as in describeOneTableDetails).
134 */
135typedef struct printTableFooter
136{
137 char *data;
138 struct printTableFooter *next;
139} printTableFooter;
140
141/*
142 * The table content struct holds all the information which will be displayed
143 * by printTable().
144 */
145typedef struct printTableContent
146{
147 const printTableOpt *opt;
148 const char *title; /* May be NULL */
149 int ncolumns; /* Specified in Init() */
150 int nrows; /* Specified in Init() */
151 const char **headers; /* NULL-terminated array of header strings */
152 const char **header; /* Pointer to the last added header */
153 const char **cells; /* NULL-terminated array of cell content
154 * strings */
155 const char **cell; /* Pointer to the last added cell */
156 long cellsadded; /* Number of cells added this far */
157 bool *cellmustfree; /* true for cells that need to be free()d */
158 printTableFooter *footers; /* Pointer to the first footer */
159 printTableFooter *footer; /* Pointer to the last added footer */
160 char *aligns; /* Array of alignment specifiers; 'l' or 'r',
161 * one per column */
162 char *align; /* Pointer to the last added alignment */
163} printTableContent;
164
165typedef struct printQueryOpt
166{
167 printTableOpt topt; /* the options above */
168 char *nullPrint; /* how to print null entities */
169 char *title; /* override title */
170 char **footers; /* override footer (default is "(xx rows)") */
171 bool translate_header; /* do gettext on column headers */
172 const bool *translate_columns; /* translate_columns[i-1] => do gettext on
173 * col i */
174 int n_translate_columns; /* length of translate_columns[] */
175} printQueryOpt;
176
177
178extern volatile bool cancel_pressed;
179
180extern const printTextFormat pg_asciiformat;
181extern const printTextFormat pg_asciiformat_old;
182extern printTextFormat pg_utf8format; /* ideally would be const, but... */
183
184
185extern void disable_sigpipe_trap(void);
186extern void restore_sigpipe_trap(void);
187extern void set_sigpipe_trap_state(bool ignore);
188
189extern FILE *PageOutput(int lines, const printTableOpt *topt);
190extern void ClosePager(FILE *pagerpipe);
191
192extern void html_escaped_print(const char *in, FILE *fout);
193
194extern void printTableInit(printTableContent *const content,
195 const printTableOpt *opt, const char *title,
196 const int ncolumns, const int nrows);
197extern void printTableAddHeader(printTableContent *const content,
198 char *header, const bool translate, const char align);
199extern void printTableAddCell(printTableContent *const content,
200 char *cell, const bool translate, const bool mustfree);
201extern void printTableAddFooter(printTableContent *const content,
202 const char *footer);
203extern void printTableSetFooter(printTableContent *const content,
204 const char *footer);
205extern void printTableCleanup(printTableContent *const content);
206extern void printTable(const printTableContent *cont,
207 FILE *fout, bool is_pager, FILE *flog);
208extern void printQuery(const PGresult *result, const printQueryOpt *opt,
209 FILE *fout, bool is_pager, FILE *flog);
210
211extern char column_type_alignment(Oid);
212
213extern void setDecimalLocale(void);
214extern const printTextFormat *get_line_style(const printTableOpt *opt);
215extern void refresh_utf8format(const printTableOpt *opt);
216
217#endif /* PRINT_H */
218