PostgreSQL Source Code  git master
print.h
Go to the documentation of this file.
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 
27 {
28  PRINT_NOTHING = 0, /* to make sure someone initializes this */
38  /* add your favourite output format here ... */
39 };
40 
41 typedef 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) */
49 
50 typedef 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) */
58 
59 typedef 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 */
66 
67 typedef 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? */
84 
85 typedef enum unicode_linestyle
86 {
90 
91 struct separator
92 {
93  char *separator;
95 };
96 
97 typedef 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 */
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  */
135 typedef struct printTableFooter
136 {
137  char *data;
140 
141 /*
142  * The table content struct holds all the information which will be displayed
143  * by printTable().
144  */
145 typedef struct printTableContent
146 {
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 */
164 
165 typedef 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 
178 extern volatile bool cancel_pressed;
179 
180 extern const printTextFormat pg_asciiformat;
182 extern printTextFormat pg_utf8format; /* ideally would be const, but... */
183 
184 
185 extern void disable_sigpipe_trap(void);
186 extern void restore_sigpipe_trap(void);
187 extern void set_sigpipe_trap_state(bool ignore);
188 
189 extern FILE *PageOutput(int lines, const printTableOpt *topt);
190 extern void ClosePager(FILE *pagerpipe);
191 
192 extern void html_escaped_print(const char *in, FILE *fout);
193 
194 extern void printTableInit(printTableContent *const content,
195  const printTableOpt *opt, const char *title,
196  const int ncolumns, const int nrows);
197 extern void printTableAddHeader(printTableContent *const content,
198  char *header, const bool translate, const char align);
199 extern void printTableAddCell(printTableContent *const content,
200  char *cell, const bool translate, const bool mustfree);
201 extern void printTableAddFooter(printTableContent *const content,
202  const char *footer);
203 extern void printTableSetFooter(printTableContent *const content,
204  const char *footer);
205 extern void printTableCleanup(printTableContent *const content);
206 extern void printTable(const printTableContent *cont,
207  FILE *fout, bool is_pager, FILE *flog);
208 extern void printQuery(const PGresult *result, const printQueryOpt *opt,
209  FILE *fout, bool is_pager, FILE *flog);
210 
211 extern char column_type_alignment(Oid);
212 
213 extern void setDecimalLocale(void);
214 extern const printTextFormat *get_line_style(const printTableOpt *opt);
215 extern void refresh_utf8format(const printTableOpt *opt);
216 
217 #endif /* PRINT_H */
const char * midvrule
Definition: print.h:46
int env_columns
Definition: print.h:121
const char * title
Definition: print.h:148
char * nullPrint
Definition: print.h:168
int encoding
Definition: print.h:120
const printTextFormat * line_style
Definition: print.h:113
const char * midvrule_wrap
Definition: print.h:73
const char * header_nl_left
Definition: print.h:75
bool wrap_right_border
Definition: print.h:81
bool start_table
Definition: print.h:109
printTableOpt topt
Definition: print.h:167
unicode_linestyle
Definition: print.h:85
printTextFormat pg_utf8format
Definition: print.c:100
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3111
unicode_linestyle unicode_header_linestyle
Definition: print.h:125
void set_sigpipe_trap_state(bool ignore)
Definition: print.c:2978
void disable_sigpipe_trap(void)
Definition: print.c:2942
unsigned int Oid
Definition: postgres_ext.h:31
const printTableOpt * opt
Definition: print.h:147
const char * name
Definition: print.h:70
printTextLineWrap
Definition: print.h:59
void refresh_utf8format(const printTableOpt *opt)
Definition: print.c:3575
unsigned long prior_records
Definition: print.h:112
bool separator_zero
Definition: print.h:94
void ClosePager(FILE *pagerpipe)
Definition: print.c:3043
volatile bool cancel_pressed
Definition: print.c:44
const char * midvrule_blank
Definition: print.h:74
unicode_linestyle unicode_border_linestyle
Definition: print.h:123
Datum translate(PG_FUNCTION_ARGS)
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3331
struct printTextLineFormat printTextLineFormat
struct printTableFooter * next
Definition: print.h:138
const char ** cell
Definition: print.h:155
struct printTableContent printTableContent
const char * header_nl_right
Definition: print.h:76
bool * cellmustfree
Definition: print.h:157
unsigned short int border
Definition: print.h:102
char * aligns
Definition: print.h:160
unsigned short int expanded
Definition: print.h:100
const char * hrule
Definition: print.h:44
void restore_sigpipe_trap(void)
Definition: print.c:2965
unicode_linestyle unicode_column_linestyle
Definition: print.h:124
const char * midvrule_nl
Definition: print.h:72
bool tuples_only
Definition: print.h:108
printTextRule
Definition: print.h:50
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3151
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3199
const char ** cells
Definition: print.h:153
long cellsadded
Definition: print.h:156
void printTableCleanup(printTableContent *const content)
Definition: print.c:3242
char * tableAttr
Definition: print.h:119
bool translate_header
Definition: print.h:171
const char ** header
Definition: print.h:152
const char * leftvrule
Definition: print.h:45
struct printTableOpt printTableOpt
printTableFooter * footer
Definition: print.h:159
const printTextFormat * get_line_style(const printTableOpt *opt)
Definition: print.c:3561
unsigned short int pager
Definition: print.h:104
bool stop_table
Definition: print.h:110
char * title
Definition: print.h:169
int pager_min_lines
Definition: print.h:106
char ** footers
Definition: print.h:170
bool default_footer
Definition: print.h:111
void printQuery(const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3434
const char * nl_right
Definition: print.h:78
int n_translate_columns
Definition: print.h:174
const printTextFormat pg_asciiformat_old
Definition: print.c:78
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:209
printTableFooter * footers
Definition: print.h:158
void setDecimalLocale(void)
Definition: print.c:3525
bool numericLocale
Definition: print.h:117
void html_escaped_print(const char *in, FILE *fout)
Definition: print.c:1855
const char * nl_left
Definition: print.h:77
const char * rightvrule
Definition: print.h:47
const printTextFormat pg_asciiformat
Definition: print.c:57
const char * wrap_left
Definition: print.h:79
char * data
Definition: print.h:137
char * separator
Definition: print.h:93
char column_type_alignment(Oid)
Definition: print.c:3499
printFormat
Definition: print.h:26
const char * wrap_right
Definition: print.h:80
char * align
Definition: print.h:162
void printTableSetFooter(printTableContent *const content, const char *footer)
Definition: print.c:3224
static char format
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3074
const char ** headers
Definition: print.h:151
const bool * translate_columns
Definition: print.h:172
int columns
Definition: print.h:122
struct printQueryOpt printQueryOpt
struct printTextFormat printTextFormat
FILE * PageOutput(int lines, const printTableOpt *topt)
Definition: print.c:2992
struct printTableFooter printTableFooter