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