print.h 7.7 KB

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