2
0

print.h 7.9 KB

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