PostgreSQL Source Code  git master
print.h File Reference
#include <signal.h>
#include "libpq-fe.h"
Include dependency graph for print.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  printTextLineFormat
 
struct  printTextFormat
 
struct  separator
 
struct  printTableOpt
 
struct  printTableFooter
 
struct  printTableContent
 
struct  printQueryOpt
 

Macros

#define DEFAULT_PAGER   "more"
 

Typedefs

typedef struct printTextLineFormat printTextLineFormat
 
typedef enum printTextRule printTextRule
 
typedef enum printTextLineWrap printTextLineWrap
 
typedef struct printTextFormat printTextFormat
 
typedef enum unicode_linestyle unicode_linestyle
 
typedef struct printTableOpt printTableOpt
 
typedef struct printTableFooter printTableFooter
 
typedef struct printTableContent printTableContent
 
typedef struct printQueryOpt printQueryOpt
 

Enumerations

enum  printFormat {
  PRINT_NOTHING = 0, PRINT_ALIGNED, PRINT_ASCIIDOC, PRINT_CSV,
  PRINT_HTML, PRINT_LATEX, PRINT_LATEX_LONGTABLE, PRINT_TROFF_MS,
  PRINT_UNALIGNED, PRINT_WRAPPED
}
 
enum  printTextRule { PRINT_RULE_TOP, PRINT_RULE_MIDDLE, PRINT_RULE_BOTTOM, PRINT_RULE_DATA }
 
enum  printTextLineWrap { PRINT_LINE_WRAP_NONE, PRINT_LINE_WRAP_WRAP, PRINT_LINE_WRAP_NEWLINE }
 
enum  unicode_linestyle { UNICODE_LINESTYLE_SINGLE = 0, UNICODE_LINESTYLE_DOUBLE }
 

Functions

void disable_sigpipe_trap (void)
 
void restore_sigpipe_trap (void)
 
void set_sigpipe_trap_state (bool ignore)
 
FILE * PageOutput (int lines, const printTableOpt *topt)
 
void ClosePager (FILE *pagerpipe)
 
void html_escaped_print (const char *in, FILE *fout)
 
void printTableInit (printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
 
void printTableAddHeader (printTableContent *const content, char *header, const bool translate, const char align)
 
void printTableAddCell (printTableContent *const content, char *cell, const bool translate, const bool mustfree)
 
void printTableAddFooter (printTableContent *const content, const char *footer)
 
void printTableSetFooter (printTableContent *const content, const char *footer)
 
void printTableCleanup (printTableContent *const content)
 
void printTable (const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
 
void printQuery (const PGresult *result, const printQueryOpt *opt, FILE *fout, bool is_pager, FILE *flog)
 
char column_type_alignment (Oid)
 
void setDecimalLocale (void)
 
const printTextFormatget_line_style (const printTableOpt *opt)
 
void refresh_utf8format (const printTableOpt *opt)
 

Variables

volatile sig_atomic_t cancel_pressed
 
const printTextFormat pg_asciiformat
 
const printTextFormat pg_asciiformat_old
 
printTextFormat pg_utf8format
 

Macro Definition Documentation

◆ DEFAULT_PAGER

#define DEFAULT_PAGER   "more"

Definition at line 23 of file print.h.

Referenced by PageOutput().

Typedef Documentation

◆ printQueryOpt

typedef struct printQueryOpt printQueryOpt

◆ printTableContent

◆ printTableFooter

◆ printTableOpt

typedef struct printTableOpt printTableOpt

◆ printTextFormat

◆ printTextLineFormat

◆ printTextLineWrap

◆ printTextRule

◆ unicode_linestyle

Enumeration Type Documentation

◆ printFormat

Enumerator
PRINT_NOTHING 
PRINT_ALIGNED 
PRINT_ASCIIDOC 
PRINT_CSV 
PRINT_HTML 
PRINT_LATEX 
PRINT_LATEX_LONGTABLE 
PRINT_TROFF_MS 
PRINT_UNALIGNED 
PRINT_WRAPPED 

Definition at line 28 of file print.h.

29 {
30  PRINT_NOTHING = 0, /* to make sure someone initializes this */
33  PRINT_CSV,
34  PRINT_HTML,
40  /* add your favourite output format here ... */
41 };

◆ printTextLineWrap

Enumerator
PRINT_LINE_WRAP_NONE 
PRINT_LINE_WRAP_WRAP 
PRINT_LINE_WRAP_NEWLINE 

Definition at line 61 of file print.h.

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 */
printTextLineWrap
Definition: print.h:61

◆ printTextRule

Enumerator
PRINT_RULE_TOP 
PRINT_RULE_MIDDLE 
PRINT_RULE_BOTTOM 
PRINT_RULE_DATA 

Definition at line 52 of file print.h.

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) */
printTextRule
Definition: print.h:52

◆ unicode_linestyle

Enumerator
UNICODE_LINESTYLE_SINGLE 
UNICODE_LINESTYLE_DOUBLE 

Definition at line 87 of file print.h.

Function Documentation

◆ ClosePager()

void ClosePager ( FILE *  pagerpipe)

Definition at line 3026 of file print.c.

References _, cancel_pressed, fprintf, restore_sigpipe_trap(), and generate_unaccent_rules::stdout.

Referenced by exec_command_sf_sv(), ExecQueryUsingCursor(), helpSQL(), helpVariables(), print_aligned_text(), print_aligned_vertical(), printHistory(), printTable(), slashUsage(), and usage().

3027 {
3028  if (pagerpipe && pagerpipe != stdout)
3029  {
3030  /*
3031  * If printing was canceled midstream, warn about it.
3032  *
3033  * Some pagers like less use Ctrl-C as part of their command set. Even
3034  * so, we abort our processing and warn the user what we did. If the
3035  * pager quit as a result of the SIGINT, this message won't go
3036  * anywhere ...
3037  */
3038  if (cancel_pressed)
3039  fprintf(pagerpipe, _("Interrupted\n"));
3040 
3041  pclose(pagerpipe);
3043  }
3044 }
#define fprintf
Definition: port.h:197
void restore_sigpipe_trap(void)
Definition: print.c:2948
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
#define _(x)
Definition: elog.c:88

◆ column_type_alignment()

char column_type_alignment ( Oid  )

Definition at line 3482 of file print.c.

Referenced by printCrosstab(), and printQuery().

3483 {
3484  char align;
3485 
3486  switch (ftype)
3487  {
3488  case INT2OID:
3489  case INT4OID:
3490  case INT8OID:
3491  case FLOAT4OID:
3492  case FLOAT8OID:
3493  case NUMERICOID:
3494  case OIDOID:
3495  case XIDOID:
3496  case XID8OID:
3497  case CIDOID:
3498  case CASHOID:
3499  align = 'r';
3500  break;
3501  default:
3502  align = 'l';
3503  break;
3504  }
3505  return align;
3506 }

◆ disable_sigpipe_trap()

void disable_sigpipe_trap ( void  )

Definition at line 2925 of file print.c.

References pqsignal(), SIG_IGN, and SIGPIPE.

Referenced by do_copy(), exec_command_write(), ExecQueryUsingCursor(), PageOutput(), PrintQueryTuples(), and ProcessResult().

2926 {
2927 #ifndef WIN32
2929 #endif
2930 }
#define SIGPIPE
Definition: win32_port.h:158
#define SIG_IGN
Definition: win32_port.h:150
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170

◆ get_line_style()

const printTextFormat* get_line_style ( const printTableOpt opt)

Definition at line 3545 of file print.c.

References printTableOpt::line_style, and pg_asciiformat.

Referenced by print_aligned_text(), print_aligned_vertical(), printPsetInfo(), and pset_value_string().

3546 {
3547  /*
3548  * Note: this function mainly exists to preserve the convention that a
3549  * printTableOpt struct can be initialized to zeroes to get default
3550  * behavior.
3551  */
3552  if (opt->line_style != NULL)
3553  return opt->line_style;
3554  else
3555  return &pg_asciiformat;
3556 }
const printTextFormat * line_style
Definition: print.h:115
const printTextFormat pg_asciiformat
Definition: print.c:56

◆ html_escaped_print()

void html_escaped_print ( const char *  in,
FILE *  fout 
)

Definition at line 1838 of file print.c.

Referenced by print_html_text(), print_html_vertical(), and PrintQueryStatus().

1839 {
1840  const char *p;
1841  bool leading_space = true;
1842 
1843  for (p = in; *p; p++)
1844  {
1845  switch (*p)
1846  {
1847  case '&':
1848  fputs("&amp;", fout);
1849  break;
1850  case '<':
1851  fputs("&lt;", fout);
1852  break;
1853  case '>':
1854  fputs("&gt;", fout);
1855  break;
1856  case '\n':
1857  fputs("<br />\n", fout);
1858  break;
1859  case '"':
1860  fputs("&quot;", fout);
1861  break;
1862  case ' ':
1863  /* protect leading space, for EXPLAIN output */
1864  if (leading_space)
1865  fputs("&nbsp;", fout);
1866  else
1867  fputs(" ", fout);
1868  break;
1869  default:
1870  fputc(*p, fout);
1871  }
1872  if (*p != ' ')
1873  leading_space = false;
1874  }
1875 }

◆ PageOutput()

FILE* PageOutput ( int  lines,
const printTableOpt topt 
)

Definition at line 2975 of file print.c.

References DEFAULT_PAGER, disable_sigpipe_trap(), printTableOpt::pager, printTableOpt::pager_min_lines, restore_sigpipe_trap(), and generate_unaccent_rules::stdout.

Referenced by exec_command_sf_sv(), ExecQueryUsingCursor(), helpSQL(), helpVariables(), IsPagerNeeded(), print_aligned_text(), printHistory(), slashUsage(), and usage().

2976 {
2977  /* check whether we need / can / are supposed to use pager */
2978  if (topt && topt->pager && isatty(fileno(stdin)) && isatty(fileno(stdout)))
2979  {
2980 #ifdef TIOCGWINSZ
2981  unsigned short int pager = topt->pager;
2982  int min_lines = topt->pager_min_lines;
2983  int result;
2984  struct winsize screen_size;
2985 
2986  result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
2987 
2988  /* >= accounts for a one-line prompt */
2989  if (result == -1
2990  || (lines >= screen_size.ws_row && lines >= min_lines)
2991  || pager > 1)
2992 #endif
2993  {
2994  const char *pagerprog;
2995  FILE *pagerpipe;
2996 
2997  pagerprog = getenv("PSQL_PAGER");
2998  if (!pagerprog)
2999  pagerprog = getenv("PAGER");
3000  if (!pagerprog)
3001  pagerprog = DEFAULT_PAGER;
3002  else
3003  {
3004  /* if PAGER is empty or all-white-space, don't use pager */
3005  if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
3006  return stdout;
3007  }
3009  pagerpipe = popen(pagerprog, "w");
3010  if (pagerpipe)
3011  return pagerpipe;
3012  /* if popen fails, silently proceed without pager */
3014  }
3015  }
3016 
3017  return stdout;
3018 }
void disable_sigpipe_trap(void)
Definition: print.c:2925
void restore_sigpipe_trap(void)
Definition: print.c:2948
unsigned short int pager
Definition: print.h:106
int pager_min_lines
Definition: print.h:108
#define DEFAULT_PAGER
Definition: print.h:23

◆ printQuery()

void printQuery ( const PGresult result,
const printQueryOpt opt,
FILE *  fout,
bool  is_pager,
FILE *  flog 
)

Definition at line 3417 of file print.c.

References printTableContent::aligns, Assert, cancel_pressed, column_type_alignment(), printQueryOpt::footers, format_numeric_locale(), i, printQueryOpt::n_translate_columns, printTableContent::ncolumns, printTableContent::nrows, printQueryOpt::nullPrint, printTableOpt::numericLocale, PQfname(), PQftype(), PQgetisnull(), PQgetvalue(), PQnfields(), PQntuples(), printTable(), printTableAddCell(), printTableAddFooter(), printTableAddHeader(), printTableCleanup(), printTableInit(), printQueryOpt::title, printQueryOpt::topt, translate(), printQueryOpt::translate_columns, and printQueryOpt::translate_header.

Referenced by describeAccessMethods(), describeAggregates(), describeFunctions(), describeOneTableDetails(), describeOneTSConfig(), describeOneTSParser(), describeOperators(), describeSubscriptions(), describeTablespaces(), describeTypes(), do_lo_list(), ExecQueryUsingCursor(), listAllDbs(), listCasts(), listCollations(), listConversions(), listDbRoleSettings(), listDefaultACLs(), listDomains(), listEventTriggers(), listExtensions(), listForeignDataWrappers(), listForeignServers(), listForeignTables(), listLanguages(), listOneExtensionContents(), listOperatorClasses(), listOperatorFamilies(), listOpFamilyFunctions(), listOpFamilyOperators(), listPartitionedTables(), listPublications(), listSchemas(), listTables(), listTSConfigs(), listTSDictionaries(), listTSParsers(), listTSTemplates(), listUserMappings(), objectDescription(), permissionsList(), PrintQueryTuples(), and PSQLexecWatch().

3419 {
3420  printTableContent cont;
3421  int i,
3422  r,
3423  c;
3424 
3425  if (cancel_pressed)
3426  return;
3427 
3428  printTableInit(&cont, &opt->topt, opt->title,
3429  PQnfields(result), PQntuples(result));
3430 
3431  /* Assert caller supplied enough translate_columns[] entries */
3432  Assert(opt->translate_columns == NULL ||
3433  opt->n_translate_columns >= cont.ncolumns);
3434 
3435  for (i = 0; i < cont.ncolumns; i++)
3436  {
3437  printTableAddHeader(&cont, PQfname(result, i),
3438  opt->translate_header,
3439  column_type_alignment(PQftype(result, i)));
3440  }
3441 
3442  /* set cells */
3443  for (r = 0; r < cont.nrows; r++)
3444  {
3445  for (c = 0; c < cont.ncolumns; c++)
3446  {
3447  char *cell;
3448  bool mustfree = false;
3449  bool translate;
3450 
3451  if (PQgetisnull(result, r, c))
3452  cell = opt->nullPrint ? opt->nullPrint : "";
3453  else
3454  {
3455  cell = PQgetvalue(result, r, c);
3456  if (cont.aligns[c] == 'r' && opt->topt.numericLocale)
3457  {
3458  cell = format_numeric_locale(cell);
3459  mustfree = true;
3460  }
3461  }
3462 
3463  translate = (opt->translate_columns && opt->translate_columns[c]);
3464  printTableAddCell(&cont, cell, translate, mustfree);
3465  }
3466  }
3467 
3468  /* set footers */
3469  if (opt->footers)
3470  {
3471  char **footer;
3472 
3473  for (footer = opt->footers; *footer; footer++)
3474  printTableAddFooter(&cont, *footer);
3475  }
3476 
3477  printTable(&cont, fout, is_pager, flog);
3478  printTableCleanup(&cont);
3479 }
int PQnfields(const PGresult *res)
Definition: fe-exec.c:2777
char * nullPrint
Definition: print.h:170
void printTableCleanup(printTableContent *const content)
Definition: print.c:3225
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3163
char * PQfname(const PGresult *res, int field_num)
Definition: fe-exec.c:2855
printTableOpt topt
Definition: print.h:169
int PQntuples(const PGresult *res)
Definition: fe-exec.c:2769
Datum translate(PG_FUNCTION_ARGS)
void printTableAddHeader(printTableContent *const content, char *header, const bool translate, const char align)
Definition: print.c:3094
char * aligns
Definition: print.h:162
void printTableAddCell(printTableContent *const content, char *cell, const bool translate, const bool mustfree)
Definition: print.c:3134
Oid PQftype(const PGresult *res, int field_num)
Definition: fe-exec.c:3007
char * c
bool translate_header
Definition: print.h:173
void printTable(const printTableContent *cont, FILE *fout, bool is_pager, FILE *flog)
Definition: print.c:3314
char column_type_alignment(Oid ftype)
Definition: print.c:3482
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3182
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
char * title
Definition: print.h:171
char ** footers
Definition: print.h:172
static char * format_numeric_locale(const char *my_str)
Definition: print.c:244
#define Assert(condition)
Definition: c.h:745
int n_translate_columns
Definition: print.h:176
void printTableInit(printTableContent *const content, const printTableOpt *opt, const char *title, const int ncolumns, const int nrows)
Definition: print.c:3057
bool numericLocale
Definition: print.h:119
int i
int PQgetisnull(const PGresult *res, int tup_num, int field_num)
Definition: fe-exec.c:3188
const bool * translate_columns
Definition: print.h:174

◆ printTable()

void printTable ( const printTableContent cont,
FILE *  fout,
bool  is_pager,
FILE *  flog 
)

Definition at line 3314 of file print.c.

References _, cancel_pressed, ClosePager(), EXIT_FAILURE, printTableOpt::expanded, printTableOpt::format, fprintf, IsPagerNeeded(), printTableContent::opt, PRINT_ALIGNED, print_aligned_text(), print_aligned_vertical(), PRINT_ASCIIDOC, print_asciidoc_text(), print_asciidoc_vertical(), PRINT_CSV, print_csv_text(), print_csv_vertical(), PRINT_HTML, print_html_text(), print_html_vertical(), PRINT_LATEX, PRINT_LATEX_LONGTABLE, print_latex_longtable_text(), print_latex_text(), print_latex_vertical(), PRINT_NOTHING, PRINT_TROFF_MS, print_troff_ms_text(), print_troff_ms_vertical(), PRINT_UNALIGNED, print_unaligned_text(), print_unaligned_vertical(), and PRINT_WRAPPED.

Referenced by describeOneTableDetails(), describePublications(), describeRoles(), printCrosstab(), and printQuery().

3316 {
3317  bool is_local_pager = false;
3318 
3319  if (cancel_pressed)
3320  return;
3321 
3322  if (cont->opt->format == PRINT_NOTHING)
3323  return;
3324 
3325  /* print_aligned_*() handle the pager themselves */
3326  if (!is_pager &&
3327  cont->opt->format != PRINT_ALIGNED &&
3328  cont->opt->format != PRINT_WRAPPED)
3329  {
3330  IsPagerNeeded(cont, 0, (cont->opt->expanded == 1), &fout, &is_pager);
3331  is_local_pager = is_pager;
3332  }
3333 
3334  /* print the stuff */
3335 
3336  if (flog)
3337  print_aligned_text(cont, flog, false);
3338 
3339  switch (cont->opt->format)
3340  {
3341  case PRINT_UNALIGNED:
3342  if (cont->opt->expanded == 1)
3343  print_unaligned_vertical(cont, fout);
3344  else
3345  print_unaligned_text(cont, fout);
3346  break;
3347  case PRINT_ALIGNED:
3348  case PRINT_WRAPPED:
3349 
3350  /*
3351  * In expanded-auto mode, force vertical if a pager is passed in;
3352  * else we may make different decisions for different hunks of the
3353  * query result.
3354  */
3355  if (cont->opt->expanded == 1 ||
3356  (cont->opt->expanded == 2 && is_pager))
3357  print_aligned_vertical(cont, fout, is_pager);
3358  else
3359  print_aligned_text(cont, fout, is_pager);
3360  break;
3361  case PRINT_CSV:
3362  if (cont->opt->expanded == 1)
3363  print_csv_vertical(cont, fout);
3364  else
3365  print_csv_text(cont, fout);
3366  break;
3367  case PRINT_HTML:
3368  if (cont->opt->expanded == 1)
3369  print_html_vertical(cont, fout);
3370  else
3371  print_html_text(cont, fout);
3372  break;
3373  case PRINT_ASCIIDOC:
3374  if (cont->opt->expanded == 1)
3375  print_asciidoc_vertical(cont, fout);
3376  else
3377  print_asciidoc_text(cont, fout);
3378  break;
3379  case PRINT_LATEX:
3380  if (cont->opt->expanded == 1)
3381  print_latex_vertical(cont, fout);
3382  else
3383  print_latex_text(cont, fout);
3384  break;
3385  case PRINT_LATEX_LONGTABLE:
3386  if (cont->opt->expanded == 1)
3387  print_latex_vertical(cont, fout);
3388  else
3389  print_latex_longtable_text(cont, fout);
3390  break;
3391  case PRINT_TROFF_MS:
3392  if (cont->opt->expanded == 1)
3393  print_troff_ms_vertical(cont, fout);
3394  else
3395  print_troff_ms_text(cont, fout);
3396  break;
3397  default:
3398  fprintf(stderr, _("invalid output format (internal error): %d"),
3399  cont->opt->format);
3400  exit(EXIT_FAILURE);
3401  }
3402 
3403  if (is_local_pager)
3404  ClosePager(fout);
3405 }
static void print_troff_ms_vertical(const printTableContent *cont, FILE *fout)
Definition: print.c:2805
static void print_unaligned_vertical(const printTableContent *cont, FILE *fout)
Definition: print.c:443
static void print_csv_vertical(const printTableContent *cont, FILE *fout)
Definition: print.c:1806
void ClosePager(FILE *pagerpipe)
Definition: print.c:3026
static void print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
Definition: print.c:565
enum printFormat format
Definition: print.h:101
static void print_aligned_vertical(const printTableContent *cont, FILE *fout, bool is_pager)
Definition: print.c:1211
static void print_troff_ms_text(const printTableContent *cont, FILE *fout)
Definition: print.c:2713
static void print_csv_text(const printTableContent *cont, FILE *fout)
Definition: print.c:1766
const printTableOpt * opt
Definition: print.h:149
#define fprintf
Definition: port.h:197
static void IsPagerNeeded(const printTableContent *cont, int extra_lines, bool expanded, FILE **fout, bool *is_pager)
Definition: print.c:3274
unsigned short int expanded
Definition: print.h:102
static void print_html_text(const printTableContent *cont, FILE *fout)
Definition: print.c:1879
static void print_latex_text(const printTableContent *cont, FILE *fout)
Definition: print.c:2340
static void print_unaligned_text(const printTableContent *cont, FILE *fout)
Definition: print.c:352
static void print_asciidoc_vertical(const printTableContent *cont, FILE *fout)
Definition: print.c:2182
static void print_html_vertical(const printTableContent *cont, FILE *fout)
Definition: print.c:1968
static void print_latex_longtable_text(const printTableContent *cont, FILE *fout)
Definition: print.c:2447
static void print_asciidoc_text(const printTableContent *cont, FILE *fout)
Definition: print.c:2072
volatile sig_atomic_t cancel_pressed
Definition: print.c:43
static void print_latex_vertical(const printTableContent *cont, FILE *fout)
Definition: print.c:2603
#define EXIT_FAILURE
Definition: settings.h:154
#define _(x)
Definition: elog.c:88

◆ printTableAddCell()

void printTableAddCell ( printTableContent *const  content,
char *  cell,
const bool  translate,
const bool  mustfree 
)

Definition at line 3134 of file print.c.

References _, printTableContent::cell, printTableContent::cellmustfree, printTableContent::cellsadded, printTableOpt::encoding, EXIT_FAILURE, fprintf, mbvalidate(), printTableContent::ncolumns, printTableContent::nrows, printTableContent::opt, and pg_malloc0().

Referenced by describeOneTableDetails(), describePublications(), describeRoles(), and printQuery().

3136 {
3137 #ifndef ENABLE_NLS
3138  (void) translate; /* unused parameter */
3139 #endif
3140 
3141  if (content->cellsadded >= content->ncolumns * content->nrows)
3142  {
3143  fprintf(stderr, _("Cannot add cell to table content: "
3144  "total cell count of %d exceeded.\n"),
3145  content->ncolumns * content->nrows);
3146  exit(EXIT_FAILURE);
3147  }
3148 
3149  *content->cell = (char *) mbvalidate((unsigned char *) cell,
3150  content->opt->encoding);
3151 
3152 #ifdef ENABLE_NLS
3153  if (translate)
3154  *content->cell = _(*content->cell);
3155 #endif
3156 
3157  if (mustfree)
3158  {
3159  if (content->cellmustfree == NULL)
3160  content->cellmustfree =
3161  pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool));
3162 
3163  content->cellmustfree[content->cellsadded] = true;
3164  }
3165  content->cell++;
3166  content->cellsadded++;
3167 }
int encoding
Definition: print.h:122
const printTableOpt * opt
Definition: print.h:149
#define fprintf
Definition: port.h:197
Datum translate(PG_FUNCTION_ARGS)
const char ** cell
Definition: print.h:157
bool * cellmustfree
Definition: print.h:159
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
unsigned char * mbvalidate(unsigned char *pwcs, int encoding)
Definition: mbprint.c:392
long cellsadded
Definition: print.h:158
#define EXIT_FAILURE
Definition: settings.h:154
#define _(x)
Definition: elog.c:88

◆ printTableAddFooter()

void printTableAddFooter ( printTableContent *const  content,
const char *  footer 
)

Definition at line 3182 of file print.c.

References printTableFooter::data, printTableContent::footer, printTableContent::footers, printTableFooter::next, pg_malloc0(), and pg_strdup().

Referenced by add_tablespace_footer(), describeOneTableDetails(), describePublications(), printQuery(), and printTableSetFooter().

3183 {
3184  printTableFooter *f;
3185 
3186  f = pg_malloc0(sizeof(*f));
3187  f->data = pg_strdup(footer);
3188 
3189  if (content->footers == NULL)
3190  content->footers = f;
3191  else
3192  content->footer->next = f;
3193 
3194  content->footer = f;
3195 }
struct printTableFooter * next
Definition: print.h:140
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
printTableFooter * footer
Definition: print.h:161
printTableFooter * footers
Definition: print.h:160
char * data
Definition: print.h:139

◆ printTableAddHeader()

void printTableAddHeader ( printTableContent *const  content,
char *  header,
const bool  translate,
const char  align 
)

Definition at line 3094 of file print.c.

References _, printTableContent::align, printTableOpt::encoding, EXIT_FAILURE, fprintf, header(), printTableContent::header, printTableContent::headers, mbvalidate(), printTableContent::ncolumns, and printTableContent::opt.

Referenced by describeOneTableDetails(), describePublications(), describeRoles(), printCrosstab(), and printQuery().

3096 {
3097 #ifndef ENABLE_NLS
3098  (void) translate; /* unused parameter */
3099 #endif
3100 
3101  if (content->header >= content->headers + content->ncolumns)
3102  {
3103  fprintf(stderr, _("Cannot add header to table content: "
3104  "column count of %d exceeded.\n"),
3105  content->ncolumns);
3106  exit(EXIT_FAILURE);
3107  }
3108 
3109  *content->header = (char *) mbvalidate((unsigned char *) header,
3110  content->opt->encoding);
3111 #ifdef ENABLE_NLS
3112  if (translate)
3113  *content->header = _(*content->header);
3114 #endif
3115  content->header++;
3116 
3117  *content->align = align;
3118  content->align++;
3119 }
int encoding
Definition: print.h:122
const printTableOpt * opt
Definition: print.h:149
#define fprintf
Definition: port.h:197
Datum translate(PG_FUNCTION_ARGS)
unsigned char * mbvalidate(unsigned char *pwcs, int encoding)
Definition: mbprint.c:392
const char ** header
Definition: print.h:154
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
#define EXIT_FAILURE
Definition: settings.h:154
char * align
Definition: print.h:164
const char ** headers
Definition: print.h:153
#define _(x)
Definition: elog.c:88

◆ printTableCleanup()

void printTableCleanup ( printTableContent *const  content)

Definition at line 3225 of file print.c.

References printTableContent::align, printTableContent::aligns, printTableContent::cell, printTableContent::cellmustfree, printTableContent::cells, printTableFooter::data, printTableContent::footer, printTableContent::footers, free, printTableContent::header, printTableContent::headers, i, printTableContent::ncolumns, printTableFooter::next, printTableContent::nrows, printTableContent::opt, printTableContent::title, and unconstify.

Referenced by describeOneTableDetails(), describePublications(), describeRoles(), printCrosstab(), and printQuery().

3226 {
3227  if (content->cellmustfree)
3228  {
3229  int i;
3230 
3231  for (i = 0; i < content->nrows * content->ncolumns; i++)
3232  {
3233  if (content->cellmustfree[i])
3234  free(unconstify(char *, content->cells[i]));
3235  }
3236  free(content->cellmustfree);
3237  content->cellmustfree = NULL;
3238  }
3239  free(content->headers);
3240  free(content->cells);
3241  free(content->aligns);
3242 
3243  content->opt = NULL;
3244  content->title = NULL;
3245  content->headers = NULL;
3246  content->cells = NULL;
3247  content->aligns = NULL;
3248  content->header = NULL;
3249  content->cell = NULL;
3250  content->align = NULL;
3251 
3252  if (content->footers)
3253  {
3254  for (content->footer = content->footers; content->footer;)
3255  {
3256  printTableFooter *f;
3257 
3258  f = content->footer;
3259  content->footer = f->next;
3260  free(f->data);
3261  free(f);
3262  }
3263  }
3264  content->footers = NULL;
3265  content->footer = NULL;
3266 }
const char * title
Definition: print.h:150
const printTableOpt * opt
Definition: print.h:149
struct printTableFooter * next
Definition: print.h:140
const char ** cell
Definition: print.h:157
bool * cellmustfree
Definition: print.h:159
char * aligns
Definition: print.h:162
const char ** cells
Definition: print.h:155
const char ** header
Definition: print.h:154
#define unconstify(underlying_type, expr)
Definition: c.h:1183
printTableFooter * footer
Definition: print.h:161
#define free(a)
Definition: header.h:65
printTableFooter * footers
Definition: print.h:160
int i
char * data
Definition: print.h:139
char * align
Definition: print.h:164
const char ** headers
Definition: print.h:153

◆ printTableInit()

void printTableInit ( printTableContent *const  content,
const printTableOpt opt,
const char *  title,
const int  ncolumns,
const int  nrows 
)

Definition at line 3057 of file print.c.

References printTableContent::align, printTableContent::aligns, printTableContent::cell, printTableContent::cellmustfree, printTableContent::cells, printTableContent::cellsadded, printTableContent::footer, printTableContent::footers, printTableContent::header, printTableContent::headers, printTableContent::ncolumns, printTableContent::nrows, printTableContent::opt, pg_malloc0(), and printTableContent::title.

Referenced by describeOneTableDetails(), describePublications(), describeRoles(), printCrosstab(), and printQuery().

3059 {
3060  content->opt = opt;
3061  content->title = title;
3062  content->ncolumns = ncolumns;
3063  content->nrows = nrows;
3064 
3065  content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers));
3066 
3067  content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells));
3068 
3069  content->cellmustfree = NULL;
3070  content->footers = NULL;
3071 
3072  content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align));
3073 
3074  content->header = content->headers;
3075  content->cell = content->cells;
3076  content->footer = content->footers;
3077  content->align = content->aligns;
3078  content->cellsadded = 0;
3079 }
const char * title
Definition: print.h:150
const printTableOpt * opt
Definition: print.h:149
const char ** cell
Definition: print.h:157
bool * cellmustfree
Definition: print.h:159
char * aligns
Definition: print.h:162
void * pg_malloc0(size_t size)
Definition: fe_memutils.c:53
const char ** cells
Definition: print.h:155
long cellsadded
Definition: print.h:158
const char ** header
Definition: print.h:154
printTableFooter * footer
Definition: print.h:161
printTableFooter * footers
Definition: print.h:160
char * align
Definition: print.h:164
const char ** headers
Definition: print.h:153

◆ printTableSetFooter()

void printTableSetFooter ( printTableContent *const  content,
const char *  footer 
)

Definition at line 3207 of file print.c.

References printTableFooter::data, printTableContent::footer, printTableContent::footers, free, pg_strdup(), and printTableAddFooter().

Referenced by add_tablespace_footer().

3208 {
3209  if (content->footers != NULL)
3210  {
3211  free(content->footer->data);
3212  content->footer->data = pg_strdup(footer);
3213  }
3214  else
3215  printTableAddFooter(content, footer);
3216 }
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
void printTableAddFooter(printTableContent *const content, const char *footer)
Definition: print.c:3182
printTableFooter * footer
Definition: print.h:161
#define free(a)
Definition: header.h:65
printTableFooter * footers
Definition: print.h:160
char * data
Definition: print.h:139

◆ refresh_utf8format()

void refresh_utf8format ( const printTableOpt opt)

Definition at line 3559 of file print.c.

References unicodeStyleFormat::border_style, unicodeStyleFormat::column_style, unicodeStyleColumnFormat::down_and_horizontal, unicodeStyleBorderFormat::down_and_left, unicodeStyleBorderFormat::down_and_right, header(), printTextFormat::header_nl_left, unicodeStyleFormat::header_nl_left, printTextFormat::header_nl_right, unicodeStyleFormat::header_nl_right, unicodeStyleRowFormat::horizontal, unicodeStyleBorderFormat::horizontal, printTextLineFormat::hrule, unicodeStyleBorderFormat::left_and_right, printTextLineFormat::leftvrule, printTextFormat::lrule, printTextLineFormat::midvrule, printTextFormat::midvrule_blank, printTextFormat::midvrule_nl, printTextFormat::midvrule_wrap, printTextFormat::name, printTextFormat::nl_left, unicodeStyleFormat::nl_left, printTextFormat::nl_right, unicodeStyleFormat::nl_right, pg_utf8format, PRINT_RULE_BOTTOM, PRINT_RULE_DATA, PRINT_RULE_MIDDLE, PRINT_RULE_TOP, printTextLineFormat::rightvrule, unicodeStyleFormat::row_style, printTableOpt::unicode_border_linestyle, printTableOpt::unicode_column_linestyle, printTableOpt::unicode_header_linestyle, unicodeStyleColumnFormat::up_and_horizontal, unicodeStyleBorderFormat::up_and_right, unicodeStyleColumnFormat::vertical, unicodeStyleBorderFormat::vertical, unicodeStyleColumnFormat::vertical_and_horizontal, unicodeStyleRowFormat::vertical_and_left, unicodeStyleRowFormat::vertical_and_right, printTextFormat::wrap_left, unicodeStyleFormat::wrap_left, printTextFormat::wrap_right, unicodeStyleFormat::wrap_right, printTextFormat::wrap_right_border, and unicodeStyleFormat::wrap_right_border.

Referenced by do_pset(), and main().

3560 {
3561  printTextFormat *popt = &pg_utf8format;
3562 
3563  const unicodeStyleBorderFormat *border;
3565  const unicodeStyleColumnFormat *column;
3566 
3567  popt->name = "unicode";
3568 
3572 
3573  popt->lrule[PRINT_RULE_TOP].hrule = border->horizontal;
3574  popt->lrule[PRINT_RULE_TOP].leftvrule = border->down_and_right;
3576  popt->lrule[PRINT_RULE_TOP].rightvrule = border->down_and_left;
3577 
3578  popt->lrule[PRINT_RULE_MIDDLE].hrule = header->horizontal;
3582 
3583  popt->lrule[PRINT_RULE_BOTTOM].hrule = border->horizontal;
3584  popt->lrule[PRINT_RULE_BOTTOM].leftvrule = border->up_and_right;
3587 
3588  /* N/A */
3589  popt->lrule[PRINT_RULE_DATA].hrule = "";
3590  popt->lrule[PRINT_RULE_DATA].leftvrule = border->vertical;
3591  popt->lrule[PRINT_RULE_DATA].midvrule = column->vertical;
3592  popt->lrule[PRINT_RULE_DATA].rightvrule = border->vertical;
3593 
3594  popt->midvrule_nl = column->vertical;
3595  popt->midvrule_wrap = column->vertical;
3596  popt->midvrule_blank = column->vertical;
3597 
3598  /* Same for all unicode today */
3601  popt->nl_left = unicode_style.nl_left;
3606 }
const char * midvrule
Definition: print.h:48
const char * vertical
Definition: print.c:110
unicodeStyleColumnFormat column_style[2]
Definition: print.c:129
const char * midvrule_wrap
Definition: print.h:75
static const unicodeStyleFormat unicode_style
Definition: print.c:140
const char * header_nl_left
Definition: print.c:131
const char * header_nl_left
Definition: print.h:77
printTextFormat pg_utf8format
Definition: print.c:99
bool wrap_right_border
Definition: print.h:83
const char * wrap_left
Definition: print.c:135
unicode_linestyle unicode_header_linestyle
Definition: print.h:127
printTextLineFormat lrule[4]
Definition: print.h:73
const char * horizontal
Definition: print.c:121
const char * down_and_left
Definition: print.c:122
const char * name
Definition: print.h:72
const char * up_and_right
Definition: print.c:118
const char * midvrule_blank
Definition: print.h:76
unicode_linestyle unicode_border_linestyle
Definition: print.h:125
const char * header_nl_right
Definition: print.c:132
const char * vertical_and_left[2]
Definition: print.c:105
const char * header_nl_right
Definition: print.h:78
unicodeStyleBorderFormat border_style[2]
Definition: print.c:130
const char * down_and_horizontal[2]
Definition: print.c:113
const char * horizontal
Definition: print.c:103
const char * hrule
Definition: print.h:46
const char * vertical_and_horizontal[2]
Definition: print.c:111
unicode_linestyle unicode_column_linestyle
Definition: print.h:126
const char * midvrule_nl
Definition: print.h:74
const char * left_and_right
Definition: print.c:123
const char * wrap_right
Definition: print.c:136
bool wrap_right_border
Definition: print.c:137
const char * nl_left
Definition: print.c:133
const char * leftvrule
Definition: print.h:47
const char * nl_right
Definition: print.c:134
const char * up_and_horizontal[2]
Definition: print.c:112
const char * down_and_right
Definition: print.c:120
const char * nl_right
Definition: print.h:80
const char * vertical
Definition: print.c:119
static void header(const char *fmt,...) pg_attribute_printf(1
Definition: pg_regress.c:210
const char * vertical_and_right[2]
Definition: print.c:104
const char * nl_left
Definition: print.h:79
const char * rightvrule
Definition: print.h:49
const char * wrap_left
Definition: print.h:81
const char * wrap_right
Definition: print.h:82
unicodeStyleRowFormat row_style[2]
Definition: print.c:128

◆ restore_sigpipe_trap()

void restore_sigpipe_trap ( void  )

Definition at line 2948 of file print.c.

References always_ignore_sigpipe, pqsignal(), SIG_DFL, SIG_IGN, and SIGPIPE.

Referenced by ClosePager(), do_copy(), exec_command_write(), ExecQueryUsingCursor(), PageOutput(), PrintQueryTuples(), ProcessResult(), and setQFout().

2949 {
2950 #ifndef WIN32
2952 #endif
2953 }
#define SIGPIPE
Definition: win32_port.h:158
#define SIG_IGN
Definition: win32_port.h:150
static bool always_ignore_sigpipe
Definition: print.c:45
pqsigfunc pqsignal(int signum, pqsigfunc handler)
Definition: signal.c:170
#define SIG_DFL
Definition: win32_port.h:148

◆ set_sigpipe_trap_state()

void set_sigpipe_trap_state ( bool  ignore)

Definition at line 2961 of file print.c.

References always_ignore_sigpipe.

Referenced by setQFout().

2962 {
2963  always_ignore_sigpipe = ignore;
2964 }
static bool always_ignore_sigpipe
Definition: print.c:45

◆ setDecimalLocale()

void setDecimalLocale ( void  )

Definition at line 3509 of file print.c.

References decimal_point, groupdigits, pg_strdup(), and thousands_sep.

Referenced by main().

3510 {
3511  struct lconv *extlconv;
3512 
3513  extlconv = localeconv();
3514 
3515  /* Don't accept an empty decimal_point string */
3516  if (*extlconv->decimal_point)
3517  decimal_point = pg_strdup(extlconv->decimal_point);
3518  else
3519  decimal_point = "."; /* SQL output standard */
3520 
3521  /*
3522  * Although the Open Group standard allows locales to supply more than one
3523  * group width, we consider only the first one, and we ignore any attempt
3524  * to suppress grouping by specifying CHAR_MAX. As in the backend's
3525  * cash.c, we must apply a range check to avoid being fooled by variant
3526  * CHAR_MAX values.
3527  */
3528  groupdigits = *extlconv->grouping;
3529  if (groupdigits <= 0 || groupdigits > 6)
3530  groupdigits = 3; /* most common */
3531 
3532  /* Don't accept an empty thousands_sep string, either */
3533  /* similar code exists in formatting.c */
3534  if (*extlconv->thousands_sep)
3535  thousands_sep = pg_strdup(extlconv->thousands_sep);
3536  /* Make sure thousands separator doesn't match decimal point symbol. */
3537  else if (strcmp(decimal_point, ",") != 0)
3538  thousands_sep = ",";
3539  else
3540  thousands_sep = ".";
3541 }
static int groupdigits
Definition: print.c:49
char * pg_strdup(const char *in)
Definition: fe_memutils.c:85
static char * decimal_point
Definition: print.c:48
static char * thousands_sep
Definition: print.c:50

Variable Documentation

◆ cancel_pressed

◆ pg_asciiformat

const printTextFormat pg_asciiformat

Definition at line 56 of file print.c.

Referenced by do_pset(), and get_line_style().

◆ pg_asciiformat_old

const printTextFormat pg_asciiformat_old

Definition at line 77 of file print.c.

Referenced by do_pset().

◆ pg_utf8format

printTextFormat pg_utf8format

Definition at line 99 of file print.c.

Referenced by do_pset(), and refresh_utf8format().