LCOV - code coverage report
Current view: top level - src/fe_utils - print.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 92.3 % 1654 1526
Test Date: 2026-11-29 03:16:06 Functions: 100.0 % 49 49
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 83.4 % 1335 1113

             Branch data     Line data    Source code
       1                 :             : /*-------------------------------------------------------------------------
       2                 :             :  *
       3                 :             :  * Query-result printing support for frontend code
       4                 :             :  *
       5                 :             :  * This file used to be part of psql, but now it's separated out to allow
       6                 :             :  * other frontend programs to use it.
       7                 :             :  *
       8                 :             :  *
       9                 :             :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
      10                 :             :  * Portions Copyright (c) 1994, Regents of the University of California
      11                 :             :  *
      12                 :             :  * src/fe_utils/print.c
      13                 :             :  *
      14                 :             :  *-------------------------------------------------------------------------
      15                 :             :  */
      16                 :             : #include "postgres_fe.h"
      17                 :             : 
      18                 :             : #include <limits.h>
      19                 :             : #include <math.h>
      20                 :             : #include <unistd.h>
      21                 :             : 
      22                 :             : #ifndef WIN32
      23                 :             : #include <sys/ioctl.h>            /* for ioctl() */
      24                 :             : #endif
      25                 :             : 
      26                 :             : #ifdef HAVE_TERMIOS_H
      27                 :             : #include <termios.h>
      28                 :             : #endif
      29                 :             : 
      30                 :             : #include "catalog/pg_type_d.h"
      31                 :             : #include "common/logging.h"
      32                 :             : #include "fe_utils/cancel.h"
      33                 :             : #include "fe_utils/mbprint.h"
      34                 :             : #include "fe_utils/print.h"
      35                 :             : 
      36                 :             : /* Presently, count_table_lines() is only used within #ifdef TIOCGWINSZ */
      37                 :             : #ifdef TIOCGWINSZ
      38                 :             : #define NEED_COUNT_TABLE_LINES
      39                 :             : #endif
      40                 :             : 
      41                 :             : /*
      42                 :             :  * Note: print.c's general strategy for when to check CancelRequested is to do
      43                 :             :  * so at completion of each row of output.
      44                 :             :  */
      45                 :             : 
      46                 :             : static bool always_ignore_sigpipe = false;
      47                 :             : 
      48                 :             : /* info for locale-aware numeric formatting; set up by setDecimalLocale() */
      49                 :             : static char *decimal_point;
      50                 :             : static int  groupdigits;
      51                 :             : static char *thousands_sep;
      52                 :             : 
      53                 :             : static char default_footer[100];
      54                 :             : static printTableFooter default_footer_cell = {default_footer, NULL};
      55                 :             : 
      56                 :             : /* Line style control structures */
      57                 :             : const printTextFormat pg_asciiformat =
      58                 :             : {
      59                 :             :     "ascii",
      60                 :             :     {
      61                 :             :         {"-", "+", "+", "+"},
      62                 :             :         {"-", "+", "+", "+"},
      63                 :             :         {"-", "+", "+", "+"},
      64                 :             :         {"", "|", "|", "|"}
      65                 :             :     },
      66                 :             :     "|",
      67                 :             :     "|",
      68                 :             :     "|",
      69                 :             :     " ",
      70                 :             :     "+",
      71                 :             :     " ",
      72                 :             :     "+",
      73                 :             :     ".",
      74                 :             :     ".",
      75                 :             :     true
      76                 :             : };
      77                 :             : 
      78                 :             : const printTextFormat pg_asciiformat_old =
      79                 :             : {
      80                 :             :     "old-ascii",
      81                 :             :     {
      82                 :             :         {"-", "+", "+", "+"},
      83                 :             :         {"-", "+", "+", "+"},
      84                 :             :         {"-", "+", "+", "+"},
      85                 :             :         {"", "|", "|", "|"}
      86                 :             :     },
      87                 :             :     ":",
      88                 :             :     ";",
      89                 :             :     " ",
      90                 :             :     "+",
      91                 :             :     " ",
      92                 :             :     " ",
      93                 :             :     " ",
      94                 :             :     " ",
      95                 :             :     " ",
      96                 :             :     false
      97                 :             : };
      98                 :             : 
      99                 :             : /* Default unicode linestyle format */
     100                 :             : printTextFormat pg_utf8format;
     101                 :             : 
     102                 :             : typedef struct unicodeStyleRowFormat
     103                 :             : {
     104                 :             :     const char *horizontal;
     105                 :             :     const char *vertical_and_right[2];
     106                 :             :     const char *vertical_and_left[2];
     107                 :             : } unicodeStyleRowFormat;
     108                 :             : 
     109                 :             : typedef struct unicodeStyleColumnFormat
     110                 :             : {
     111                 :             :     const char *vertical;
     112                 :             :     const char *vertical_and_horizontal[2];
     113                 :             :     const char *up_and_horizontal[2];
     114                 :             :     const char *down_and_horizontal[2];
     115                 :             : } unicodeStyleColumnFormat;
     116                 :             : 
     117                 :             : typedef struct unicodeStyleBorderFormat
     118                 :             : {
     119                 :             :     const char *up_and_right;
     120                 :             :     const char *vertical;
     121                 :             :     const char *down_and_right;
     122                 :             :     const char *horizontal;
     123                 :             :     const char *down_and_left;
     124                 :             :     const char *left_and_right;
     125                 :             : } unicodeStyleBorderFormat;
     126                 :             : 
     127                 :             : typedef struct unicodeStyleFormat
     128                 :             : {
     129                 :             :     unicodeStyleRowFormat row_style[2];
     130                 :             :     unicodeStyleColumnFormat column_style[2];
     131                 :             :     unicodeStyleBorderFormat border_style[2];
     132                 :             :     const char *header_nl_left;
     133                 :             :     const char *header_nl_right;
     134                 :             :     const char *nl_left;
     135                 :             :     const char *nl_right;
     136                 :             :     const char *wrap_left;
     137                 :             :     const char *wrap_right;
     138                 :             :     bool        wrap_right_border;
     139                 :             : } unicodeStyleFormat;
     140                 :             : 
     141                 :             : static const unicodeStyleFormat unicode_style = {
     142                 :             :     {
     143                 :             :         {
     144                 :             :             /* U+2500 Box Drawings Light Horizontal */
     145                 :             :             "\342\224\200",
     146                 :             : 
     147                 :             :             /*--
     148                 :             :              * U+251C Box Drawings Light Vertical and Right,
     149                 :             :              * U+255F Box Drawings Vertical Double and Right Single
     150                 :             :              *--
     151                 :             :              */
     152                 :             :             {"\342\224\234", "\342\225\237"},
     153                 :             : 
     154                 :             :             /*--
     155                 :             :              * U+2524 Box Drawings Light Vertical and Left,
     156                 :             :              * U+2562 Box Drawings Vertical Double and Left Single
     157                 :             :              *--
     158                 :             :              */
     159                 :             :             {"\342\224\244", "\342\225\242"},
     160                 :             :         },
     161                 :             :         {
     162                 :             :             /* U+2550 Box Drawings Double Horizontal */
     163                 :             :             "\342\225\220",
     164                 :             : 
     165                 :             :             /*--
     166                 :             :              * U+255E Box Drawings Vertical Single and Right Double,
     167                 :             :              * U+2560 Box Drawings Double Vertical and Right
     168                 :             :              *--
     169                 :             :              */
     170                 :             :             {"\342\225\236", "\342\225\240"},
     171                 :             : 
     172                 :             :             /*--
     173                 :             :              * U+2561 Box Drawings Vertical Single and Left Double,
     174                 :             :              * U+2563 Box Drawings Double Vertical and Left
     175                 :             :              *--
     176                 :             :              */
     177                 :             :             {"\342\225\241", "\342\225\243"},
     178                 :             :         },
     179                 :             :     },
     180                 :             :     {
     181                 :             :         {
     182                 :             :             /* U+2502 Box Drawings Light Vertical */
     183                 :             :             "\342\224\202",
     184                 :             : 
     185                 :             :             /*--
     186                 :             :              * U+253C Box Drawings Light Vertical and Horizontal,
     187                 :             :              * U+256A Box Drawings Vertical Single and Horizontal Double
     188                 :             :              *--
     189                 :             :              */
     190                 :             :             {"\342\224\274", "\342\225\252"},
     191                 :             : 
     192                 :             :             /*--
     193                 :             :              * U+2534 Box Drawings Light Up and Horizontal,
     194                 :             :              * U+2567 Box Drawings Up Single and Horizontal Double
     195                 :             :              *--
     196                 :             :              */
     197                 :             :             {"\342\224\264", "\342\225\247"},
     198                 :             : 
     199                 :             :             /*--
     200                 :             :              * U+252C Box Drawings Light Down and Horizontal,
     201                 :             :              * U+2564 Box Drawings Down Single and Horizontal Double
     202                 :             :              *--
     203                 :             :              */
     204                 :             :             {"\342\224\254", "\342\225\244"},
     205                 :             :         },
     206                 :             :         {
     207                 :             :             /* U+2551 Box Drawings Double Vertical */
     208                 :             :             "\342\225\221",
     209                 :             : 
     210                 :             :             /*--
     211                 :             :              * U+256B Box Drawings Vertical Double and Horizontal Single,
     212                 :             :              * U+256C Box Drawings Double Vertical and Horizontal
     213                 :             :              *--
     214                 :             :              */
     215                 :             :             {"\342\225\253", "\342\225\254"},
     216                 :             : 
     217                 :             :             /*--
     218                 :             :              * U+2568 Box Drawings Up Double and Horizontal Single,
     219                 :             :              * U+2569 Box Drawings Double Up and Horizontal
     220                 :             :              *--
     221                 :             :              */
     222                 :             :             {"\342\225\250", "\342\225\251"},
     223                 :             : 
     224                 :             :             /*--
     225                 :             :              * U+2565 Box Drawings Down Double and Horizontal Single,
     226                 :             :              * U+2566 Box Drawings Double Down and Horizontal
     227                 :             :              *--
     228                 :             :              */
     229                 :             :             {"\342\225\245", "\342\225\246"},
     230                 :             :         },
     231                 :             :     },
     232                 :             :     {
     233                 :             :         /*--
     234                 :             :          * U+2514 Box Drawings Light Up and Right,
     235                 :             :          * U+2502 Box Drawings Light Vertical,
     236                 :             :          * U+250C Box Drawings Light Down and Right,
     237                 :             :          * U+2500 Box Drawings Light Horizontal,
     238                 :             :          * U+2510 Box Drawings Light Down and Left,
     239                 :             :          * U+2518 Box Drawings Light Up and Left
     240                 :             :          *--
     241                 :             :          */
     242                 :             :         {"\342\224\224", "\342\224\202", "\342\224\214", "\342\224\200", "\342\224\220", "\342\224\230"},
     243                 :             : 
     244                 :             :         /*--
     245                 :             :          * U+255A Box Drawings Double Up and Right,
     246                 :             :          * U+2551 Box Drawings Double Vertical,
     247                 :             :          * U+2554 Box Drawings Double Down and Right,
     248                 :             :          * U+2550 Box Drawings Double Horizontal,
     249                 :             :          * U+2557 Box Drawings Double Down and Left,
     250                 :             :          * U+255D Box Drawings Double Up and Left
     251                 :             :          *--
     252                 :             :          */
     253                 :             :         {"\342\225\232", "\342\225\221", "\342\225\224", "\342\225\220", "\342\225\227", "\342\225\235"},
     254                 :             :     },
     255                 :             :     " ",
     256                 :             :     /* U+21B5 Downwards Arrow with Corner Leftwards */
     257                 :             :     "\342\206\265",
     258                 :             :     " ",
     259                 :             :     /* U+21B5 Downwards Arrow with Corner Leftwards */
     260                 :             :     "\342\206\265",
     261                 :             :     /* U+2026 Horizontal Ellipsis */
     262                 :             :     "\342\200\246",
     263                 :             :     "\342\200\246",
     264                 :             :     true
     265                 :             : };
     266                 :             : 
     267                 :             : 
     268                 :             : /* Local functions */
     269                 :             : static int  strlen_max_width(unsigned char *str, int *target_width, int encoding);
     270                 :             : static FILE *PageOutputInternal(int lines, const printTableOpt *topt,
     271                 :             :                                 const printTableContent *cont,
     272                 :             :                                 const unsigned int *width_wrap,
     273                 :             :                                 bool vertical);
     274                 :             : static void IsPagerNeeded(const printTableContent *cont,
     275                 :             :                           const unsigned int *width_wrap,
     276                 :             :                           bool vertical,
     277                 :             :                           FILE **fout, bool *is_pager);
     278                 :             : #ifdef NEED_COUNT_TABLE_LINES
     279                 :             : static int  count_table_lines(const printTableContent *cont,
     280                 :             :                               const unsigned int *width_wrap,
     281                 :             :                               bool vertical,
     282                 :             :                               int threshold);
     283                 :             : #endif
     284                 :             : static void print_aligned_vertical(const printTableContent *cont,
     285                 :             :                                    FILE *fout, bool is_pager);
     286                 :             : 
     287                 :             : 
     288                 :             : /* Count number of digits in integral part of number */
     289                 :             : static int
     290                 :         128 : integer_digits(const char *my_str)
     291                 :             : {
     292                 :             :     /* ignoring any sign ... */
     293   [ +  +  -  + ]:         128 :     if (my_str[0] == '-' || my_str[0] == '+')
     294                 :          24 :         my_str++;
     295                 :             :     /* ... count initial integral digits */
     296                 :         128 :     return strspn(my_str, "0123456789");
     297                 :             : }
     298                 :             : 
     299                 :             : /* Compute additional length required for locale-aware numeric output */
     300                 :             : static int
     301                 :          64 : additional_numeric_locale_len(const char *my_str)
     302                 :             : {
     303                 :          64 :     int         int_len = integer_digits(my_str),
     304                 :          64 :                 len = 0;
     305                 :             : 
     306                 :             :     /* Account for added thousands_sep instances */
     307         [ -  + ]:          64 :     if (int_len > groupdigits)
     308                 :           0 :         len += ((int_len - 1) / groupdigits) * strlen(thousands_sep);
     309                 :             : 
     310                 :             :     /* Account for possible additional length of decimal_point */
     311         [ -  + ]:          64 :     if (strchr(my_str, '.') != NULL)
     312                 :           0 :         len += strlen(decimal_point) - 1;
     313                 :             : 
     314                 :          64 :     return len;
     315                 :             : }
     316                 :             : 
     317                 :             : /*
     318                 :             :  * Format a numeric value per current LC_NUMERIC locale setting
     319                 :             :  *
     320                 :             :  * Returns the appropriately formatted string in a new allocated block,
     321                 :             :  * caller must free.
     322                 :             :  *
     323                 :             :  * setDecimalLocale() must have been called earlier.
     324                 :             :  */
     325                 :             : static char *
     326                 :          64 : format_numeric_locale(const char *my_str)
     327                 :             : {
     328                 :             :     char       *new_str;
     329                 :             :     int         new_len,
     330                 :             :                 int_len,
     331                 :             :                 leading_digits,
     332                 :             :                 i,
     333                 :             :                 new_str_pos;
     334                 :             : 
     335                 :             :     /*
     336                 :             :      * If the string doesn't look like a number, return it unchanged.  This
     337                 :             :      * check is essential to avoid mangling already-localized "money" values.
     338                 :             :      */
     339         [ -  + ]:          64 :     if (strspn(my_str, "0123456789+-.eE") != strlen(my_str))
     340                 :           0 :         return pg_strdup(my_str);
     341                 :             : 
     342                 :          64 :     new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
     343                 :          64 :     new_str = pg_malloc_array(char, (new_len + 1));
     344                 :          64 :     new_str_pos = 0;
     345                 :          64 :     int_len = integer_digits(my_str);
     346                 :             : 
     347                 :             :     /* number of digits in first thousands group */
     348                 :          64 :     leading_digits = int_len % groupdigits;
     349         [ +  + ]:          64 :     if (leading_digits == 0)
     350                 :          12 :         leading_digits = groupdigits;
     351                 :             : 
     352                 :             :     /* process sign */
     353   [ +  +  -  + ]:          64 :     if (my_str[0] == '-' || my_str[0] == '+')
     354                 :             :     {
     355                 :          12 :         new_str[new_str_pos++] = my_str[0];
     356                 :          12 :         my_str++;
     357                 :             :     }
     358                 :             : 
     359                 :             :     /* process integer part of number */
     360         [ +  + ]:         152 :     for (i = 0; i < int_len; i++)
     361                 :             :     {
     362                 :             :         /* Time to insert separator? */
     363   [ +  +  -  + ]:          88 :         if (i > 0 && --leading_digits == 0)
     364                 :             :         {
     365                 :           0 :             strcpy(&new_str[new_str_pos], thousands_sep);
     366                 :           0 :             new_str_pos += strlen(thousands_sep);
     367                 :           0 :             leading_digits = groupdigits;
     368                 :             :         }
     369                 :          88 :         new_str[new_str_pos++] = my_str[i];
     370                 :             :     }
     371                 :             : 
     372                 :             :     /* handle decimal point if any */
     373         [ -  + ]:          64 :     if (my_str[i] == '.')
     374                 :             :     {
     375                 :           0 :         strcpy(&new_str[new_str_pos], decimal_point);
     376                 :           0 :         new_str_pos += strlen(decimal_point);
     377                 :           0 :         i++;
     378                 :             :     }
     379                 :             : 
     380                 :             :     /* copy the rest (fractional digits and/or exponent, and \0 terminator) */
     381                 :          64 :     strcpy(&new_str[new_str_pos], &my_str[i]);
     382                 :             : 
     383                 :             :     /* assert we didn't underestimate new_len (an overestimate is OK) */
     384                 :             :     Assert(strlen(new_str) <= new_len);
     385                 :             : 
     386                 :          64 :     return new_str;
     387                 :             : }
     388                 :             : 
     389                 :             : 
     390                 :             : static void
     391                 :     1819160 : print_separator(struct separator sep, FILE *fout)
     392                 :             : {
     393         [ -  + ]:     1819160 :     if (sep.separator_zero)
     394                 :           0 :         fputc('\000', fout);
     395         [ +  - ]:     1819160 :     else if (sep.separator)
     396                 :     1819160 :         fputs(sep.separator, fout);
     397                 :     1819160 : }
     398                 :             : 
     399                 :             : 
     400                 :             : /*
     401                 :             :  * Return the list of explicitly-requested footers or, when applicable, the
     402                 :             :  * default "(xx rows)" footer.  Always omit the default footer when given
     403                 :             :  * non-default footers, "\pset footer off", or a specific instruction to that
     404                 :             :  * effect from a calling backslash command.  Vertical formats number each row,
     405                 :             :  * making the default footer redundant; they do not call this function.
     406                 :             :  *
     407                 :             :  * The return value may point to static storage; do not keep it across calls.
     408                 :             :  */
     409                 :             : static printTableFooter *
     410                 :      101326 : footers_with_default(const printTableContent *cont)
     411                 :             : {
     412   [ +  +  +  + ]:      101326 :     if (cont->footers == NULL && cont->opt->default_footer)
     413                 :             :     {
     414                 :             :         unsigned long total_records;
     415                 :             : 
     416                 :       98370 :         total_records = cont->opt->prior_records + cont->nrows;
     417                 :       98370 :         snprintf(default_footer, sizeof(default_footer),
     418                 :       98370 :                  ngettext("(%lu row)", "(%lu rows)", total_records),
     419                 :             :                  total_records);
     420                 :             : 
     421                 :       98370 :         return &default_footer_cell;
     422                 :             :     }
     423                 :             :     else
     424                 :        2956 :         return cont->footers;
     425                 :             : }
     426                 :             : 
     427                 :             : 
     428                 :             : /*************************/
     429                 :             : /* Unaligned text        */
     430                 :             : /*************************/
     431                 :             : 
     432                 :             : 
     433                 :             : static void
     434                 :       10668 : print_unaligned_text(const printTableContent *cont, FILE *fout)
     435                 :             : {
     436                 :       10668 :     bool        opt_tuples_only = cont->opt->tuples_only;
     437                 :             :     unsigned int i;
     438                 :             :     const char *const *ptr;
     439                 :       10668 :     bool        need_recordsep = false;
     440                 :             : 
     441         [ -  + ]:       10668 :     if (CancelRequested)
     442                 :           0 :         return;
     443                 :             : 
     444         [ +  - ]:       10668 :     if (cont->opt->start_table)
     445                 :             :     {
     446                 :             :         /* print title */
     447   [ +  +  +  + ]:       10668 :         if (!opt_tuples_only && cont->title)
     448                 :             :         {
     449                 :           5 :             fputs(cont->title, fout);
     450                 :           5 :             print_separator(cont->opt->recordSep, fout);
     451                 :             :         }
     452                 :             : 
     453                 :             :         /* print headers */
     454         [ +  + ]:       10668 :         if (!opt_tuples_only)
     455                 :             :         {
     456         [ +  + ]:         282 :             for (ptr = cont->headers; *ptr; ptr++)
     457                 :             :             {
     458         [ +  + ]:         191 :                 if (ptr != cont->headers)
     459                 :         100 :                     print_separator(cont->opt->fieldSep, fout);
     460                 :         191 :                 fputs(*ptr, fout);
     461                 :             :             }
     462                 :          91 :             need_recordsep = true;
     463                 :             :         }
     464                 :             :     }
     465                 :             :     else
     466                 :             :         /* assume continuing printout */
     467                 :           0 :         need_recordsep = true;
     468                 :             : 
     469                 :             :     /* print cells */
     470         [ +  + ]:     1837738 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
     471                 :             :     {
     472         [ +  + ]:     1827070 :         if (need_recordsep)
     473                 :             :         {
     474                 :      878213 :             print_separator(cont->opt->recordSep, fout);
     475                 :      878213 :             need_recordsep = false;
     476         [ -  + ]:      878213 :             if (CancelRequested)
     477                 :           0 :                 break;
     478                 :             :         }
     479                 :     1827070 :         fputs(*ptr, fout);
     480                 :             : 
     481         [ +  + ]:     1827070 :         if ((i + 1) % cont->ncolumns)
     482                 :      938624 :             print_separator(cont->opt->fieldSep, fout);
     483                 :             :         else
     484                 :      888446 :             need_recordsep = true;
     485                 :             :     }
     486                 :             : 
     487                 :             :     /* print footers */
     488         [ +  - ]:       10668 :     if (cont->opt->stop_table)
     489                 :             :     {
     490                 :       10668 :         printTableFooter *footers = footers_with_default(cont);
     491                 :             : 
     492   [ +  +  +  -  :       10668 :         if (!opt_tuples_only && footers != NULL && !CancelRequested)
                   +  - ]
     493                 :             :         {
     494                 :             :             printTableFooter *f;
     495                 :             : 
     496         [ +  + ]:         183 :             for (f = footers; f; f = f->next)
     497                 :             :             {
     498         [ +  - ]:          92 :                 if (need_recordsep)
     499                 :             :                 {
     500                 :          92 :                     print_separator(cont->opt->recordSep, fout);
     501                 :          92 :                     need_recordsep = false;
     502                 :             :                 }
     503                 :          92 :                 fputs(f->data, fout);
     504                 :          92 :                 need_recordsep = true;
     505                 :             :             }
     506                 :             :         }
     507                 :             : 
     508                 :             :         /*
     509                 :             :          * The last record is terminated by a newline, independent of the set
     510                 :             :          * record separator.  But when the record separator is a zero byte, we
     511                 :             :          * use that (compatible with find -print0 and xargs).
     512                 :             :          */
     513         [ +  + ]:       10668 :         if (need_recordsep)
     514                 :             :         {
     515         [ -  + ]:       10324 :             if (cont->opt->recordSep.separator_zero)
     516                 :           0 :                 print_separator(cont->opt->recordSep, fout);
     517                 :             :             else
     518                 :       10324 :                 fputc('\n', fout);
     519                 :             :         }
     520                 :             :     }
     521                 :             : }
     522                 :             : 
     523                 :             : 
     524                 :             : static void
     525                 :          69 : print_unaligned_vertical(const printTableContent *cont, FILE *fout)
     526                 :             : {
     527                 :          69 :     bool        opt_tuples_only = cont->opt->tuples_only;
     528                 :             :     unsigned int i;
     529                 :             :     const char *const *ptr;
     530                 :          69 :     bool        need_recordsep = false;
     531                 :             : 
     532         [ -  + ]:          69 :     if (CancelRequested)
     533                 :           0 :         return;
     534                 :             : 
     535         [ +  - ]:          69 :     if (cont->opt->start_table)
     536                 :             :     {
     537                 :             :         /* print title */
     538   [ +  +  +  + ]:          69 :         if (!opt_tuples_only && cont->title)
     539                 :             :         {
     540                 :           4 :             fputs(cont->title, fout);
     541                 :           4 :             need_recordsep = true;
     542                 :             :         }
     543                 :             :     }
     544                 :             :     else
     545                 :             :         /* assume continuing printout */
     546                 :           0 :         need_recordsep = true;
     547                 :             : 
     548                 :             :     /* print records */
     549         [ +  + ]:         973 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
     550                 :             :     {
     551         [ +  + ]:         904 :         if (need_recordsep)
     552                 :             :         {
     553                 :             :             /* record separator is 2 occurrences of recordsep in this mode */
     554                 :         375 :             print_separator(cont->opt->recordSep, fout);
     555                 :         375 :             print_separator(cont->opt->recordSep, fout);
     556                 :         375 :             need_recordsep = false;
     557         [ -  + ]:         375 :             if (CancelRequested)
     558                 :           0 :                 break;
     559                 :             :         }
     560                 :             : 
     561                 :         904 :         fputs(cont->headers[i % cont->ncolumns], fout);
     562                 :         904 :         print_separator(cont->opt->fieldSep, fout);
     563                 :         904 :         fputs(*ptr, fout);
     564                 :             : 
     565         [ +  + ]:         904 :         if ((i + 1) % cont->ncolumns)
     566                 :         464 :             print_separator(cont->opt->recordSep, fout);
     567                 :             :         else
     568                 :         440 :             need_recordsep = true;
     569                 :             :     }
     570                 :             : 
     571         [ +  - ]:          69 :     if (cont->opt->stop_table)
     572                 :             :     {
     573                 :             :         /* print footers */
     574   [ +  +  +  +  :          69 :         if (!opt_tuples_only && cont->footers != NULL && !CancelRequested)
                   +  - ]
     575                 :             :         {
     576                 :             :             printTableFooter *f;
     577                 :             : 
     578                 :           4 :             print_separator(cont->opt->recordSep, fout);
     579         [ +  + ]:           8 :             for (f = cont->footers; f; f = f->next)
     580                 :             :             {
     581                 :           4 :                 print_separator(cont->opt->recordSep, fout);
     582                 :           4 :                 fputs(f->data, fout);
     583                 :             :             }
     584                 :             :         }
     585                 :             : 
     586                 :             :         /* see above in print_unaligned_text() */
     587         [ +  - ]:          69 :         if (need_recordsep)
     588                 :             :         {
     589         [ -  + ]:          69 :             if (cont->opt->recordSep.separator_zero)
     590                 :           0 :                 print_separator(cont->opt->recordSep, fout);
     591                 :             :             else
     592                 :          69 :                 fputc('\n', fout);
     593                 :             :         }
     594                 :             :     }
     595                 :             : }
     596                 :             : 
     597                 :             : 
     598                 :             : /********************/
     599                 :             : /* Aligned text     */
     600                 :             : /********************/
     601                 :             : 
     602                 :             : 
     603                 :             : /* draw "line" */
     604                 :             : static void
     605                 :       90452 : _print_horizontal_line(unsigned int ncolumns, const unsigned int *widths,
     606                 :             :                        unsigned short border, printTextRule pos,
     607                 :             :                        const printTextFormat *format,
     608                 :             :                        FILE *fout)
     609                 :             : {
     610                 :       90452 :     const printTextLineFormat *lformat = &format->lrule[pos];
     611                 :             :     unsigned int i,
     612                 :             :                 j;
     613                 :             : 
     614         [ +  + ]:       90452 :     if (border == 1)
     615                 :       90324 :         fputs(lformat->hrule, fout);
     616         [ +  + ]:         128 :     else if (border == 2)
     617                 :          96 :         fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
     618                 :             : 
     619         [ +  + ]:      264364 :     for (i = 0; i < ncolumns; i++)
     620                 :             :     {
     621         [ +  + ]:     2807120 :         for (j = 0; j < widths[i]; j++)
     622                 :     2633208 :             fputs(lformat->hrule, fout);
     623                 :             : 
     624         [ +  + ]:      173912 :         if (i < ncolumns - 1)
     625                 :             :         {
     626         [ +  + ]:       83600 :             if (border == 0)
     627                 :          32 :                 fputc(' ', fout);
     628                 :             :             else
     629                 :       83568 :                 fprintf(fout, "%s%s%s", lformat->hrule,
     630                 :       83568 :                         lformat->midvrule, lformat->hrule);
     631                 :             :         }
     632                 :             :     }
     633                 :             : 
     634         [ +  + ]:       90452 :     if (border == 2)
     635                 :          96 :         fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
     636         [ +  + ]:       90356 :     else if (border == 1)
     637                 :       90324 :         fputs(lformat->hrule, fout);
     638                 :             : 
     639                 :       90452 :     fputc('\n', fout);
     640                 :       90452 : }
     641                 :             : 
     642                 :             : 
     643                 :             : /*
     644                 :             :  *  Print pretty boxes around cells.
     645                 :             :  */
     646                 :             : static void
     647                 :       90584 : print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
     648                 :             : {
     649                 :       90584 :     bool        opt_tuples_only = cont->opt->tuples_only;
     650                 :       90584 :     int         encoding = cont->opt->encoding;
     651                 :       90584 :     unsigned short opt_border = cont->opt->border;
     652                 :       90584 :     const printTextFormat *format = get_line_style(cont->opt);
     653                 :       90584 :     const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
     654                 :             : 
     655                 :       90584 :     unsigned int col_count = 0,
     656                 :       90584 :                 cell_count = 0;
     657                 :             : 
     658                 :             :     unsigned int i,
     659                 :             :                 j;
     660                 :             : 
     661                 :             :     unsigned int *width_header,
     662                 :             :                *max_width,
     663                 :             :                *width_wrap,
     664                 :             :                *width_average;
     665                 :             :     unsigned int *max_nl_lines, /* value split by newlines */
     666                 :             :                *curr_nl_lines,
     667                 :             :                *max_bytes;
     668                 :             :     unsigned char **format_buf;
     669                 :             :     unsigned int width_total;
     670                 :             :     unsigned int total_header_width;
     671                 :             : 
     672                 :             :     const char *const *ptr;
     673                 :             : 
     674                 :             :     struct lineptr **col_lineptrs;  /* pointers to line pointer per column */
     675                 :             : 
     676                 :             :     bool       *header_done;    /* Have all header lines been output? */
     677                 :             :     int        *bytes_output;   /* Bytes output for column value */
     678                 :             :     printTextLineWrap *wrap;    /* Wrap status for each column */
     679                 :       90584 :     int         output_columns = 0; /* Width of interactive console */
     680                 :       90584 :     bool        is_local_pager = false;
     681                 :             : 
     682         [ -  + ]:       90584 :     if (CancelRequested)
     683                 :           0 :         return;
     684                 :             : 
     685         [ -  + ]:       90584 :     if (opt_border > 2)
     686                 :           0 :         opt_border = 2;
     687                 :             : 
     688         [ +  + ]:       90584 :     if (cont->ncolumns > 0)
     689                 :             :     {
     690                 :       90444 :         col_count = cont->ncolumns;
     691                 :       90444 :         width_header = pg_malloc0_array(unsigned int, col_count);
     692                 :       90444 :         width_average = pg_malloc0_array(unsigned int, col_count);
     693                 :       90444 :         max_width = pg_malloc0_array(unsigned int, col_count);
     694                 :       90444 :         width_wrap = pg_malloc0_array(unsigned int, col_count);
     695                 :       90444 :         max_nl_lines = pg_malloc0_array(unsigned int, col_count);
     696                 :       90444 :         curr_nl_lines = pg_malloc0_array(unsigned int, col_count);
     697                 :       90444 :         col_lineptrs = pg_malloc0_array(struct lineptr *, col_count);
     698                 :       90444 :         max_bytes = pg_malloc0_array(unsigned int, col_count);
     699                 :       90444 :         format_buf = pg_malloc0_array(unsigned char *, col_count);
     700                 :       90444 :         header_done = pg_malloc0_array(bool, col_count);
     701                 :       90444 :         bytes_output = pg_malloc0_array(int, col_count);
     702                 :       90444 :         wrap = pg_malloc0_array(printTextLineWrap, col_count);
     703                 :             :     }
     704                 :             :     else
     705                 :             :     {
     706                 :         140 :         width_header = NULL;
     707                 :         140 :         width_average = NULL;
     708                 :         140 :         max_width = NULL;
     709                 :         140 :         width_wrap = NULL;
     710                 :         140 :         max_nl_lines = NULL;
     711                 :         140 :         curr_nl_lines = NULL;
     712                 :         140 :         col_lineptrs = NULL;
     713                 :         140 :         max_bytes = NULL;
     714                 :         140 :         format_buf = NULL;
     715                 :         140 :         header_done = NULL;
     716                 :         140 :         bytes_output = NULL;
     717                 :         140 :         wrap = NULL;
     718                 :             :     }
     719                 :             : 
     720                 :             :     /* scan all column headers, find maximum width and max max_nl_lines */
     721         [ +  + ]:      264876 :     for (i = 0; i < col_count; i++)
     722                 :             :     {
     723                 :             :         int         width,
     724                 :             :                     nl_lines,
     725                 :             :                     bytes_required;
     726                 :             : 
     727                 :      174292 :         pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
     728                 :             :                    encoding, &width, &nl_lines, &bytes_required);
     729         [ +  + ]:      174292 :         if (width > max_width[i])
     730                 :      174268 :             max_width[i] = width;
     731         [ +  - ]:      174292 :         if (nl_lines > max_nl_lines[i])
     732                 :      174292 :             max_nl_lines[i] = nl_lines;
     733         [ +  - ]:      174292 :         if (bytes_required > max_bytes[i])
     734                 :      174292 :             max_bytes[i] = bytes_required;
     735                 :             : 
     736                 :      174292 :         width_header[i] = width;
     737                 :             :     }
     738                 :             : 
     739                 :             :     /* scan all cells, find maximum width, compute cell_count */
     740         [ +  + ]:      847454 :     for (i = 0, ptr = cont->cells; *ptr; ptr++, cell_count++)
     741                 :             :     {
     742                 :             :         int         width,
     743                 :             :                     nl_lines,
     744                 :             :                     bytes_required;
     745                 :             : 
     746                 :      756870 :         pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
     747                 :             :                    &width, &nl_lines, &bytes_required);
     748                 :             : 
     749         [ +  + ]:      756870 :         if (width > max_width[i])
     750                 :       98616 :             max_width[i] = width;
     751         [ +  + ]:      756870 :         if (nl_lines > max_nl_lines[i])
     752                 :        1214 :             max_nl_lines[i] = nl_lines;
     753         [ +  + ]:      756870 :         if (bytes_required > max_bytes[i])
     754                 :       98994 :             max_bytes[i] = bytes_required;
     755                 :             : 
     756                 :      756870 :         width_average[i] += width;
     757                 :             : 
     758                 :             :         /* i is the current column number: increment with wrap */
     759         [ +  + ]:      756870 :         if (++i >= col_count)
     760                 :      334670 :             i = 0;
     761                 :             :     }
     762                 :             : 
     763                 :             :     /* If we have rows, compute average */
     764   [ +  +  +  + ]:       90584 :     if (col_count != 0 && cell_count != 0)
     765                 :             :     {
     766                 :       85832 :         int         rows = cell_count / col_count;
     767                 :             : 
     768         [ +  + ]:      248652 :         for (i = 0; i < col_count; i++)
     769                 :      162820 :             width_average[i] /= rows;
     770                 :             :     }
     771                 :             : 
     772                 :             :     /* adjust the total display width based on border style */
     773         [ +  + ]:       90584 :     if (opt_border == 0)
     774                 :          32 :         width_total = col_count;
     775         [ +  + ]:       90552 :     else if (opt_border == 1)
     776         [ +  + ]:       90520 :         width_total = col_count * 3 - ((col_count > 0) ? 1 : 0);
     777                 :             :     else
     778                 :          32 :         width_total = col_count * 3 + 1;
     779                 :       90584 :     total_header_width = width_total;
     780                 :             : 
     781         [ +  + ]:      264876 :     for (i = 0; i < col_count; i++)
     782                 :             :     {
     783                 :      174292 :         width_total += max_width[i];
     784                 :      174292 :         total_header_width += width_header[i];
     785                 :             :     }
     786                 :             : 
     787                 :             :     /*
     788                 :             :      * At this point: max_width[] contains the max width of each column,
     789                 :             :      * max_nl_lines[] contains the max number of lines in each column,
     790                 :             :      * max_bytes[] contains the maximum storage space for formatting strings,
     791                 :             :      * width_total contains the giant width sum.  Now we allocate some memory
     792                 :             :      * for line pointers.
     793                 :             :      */
     794         [ +  + ]:      264876 :     for (i = 0; i < col_count; i++)
     795                 :             :     {
     796                 :             :         /* Add entry for ptr == NULL array termination */
     797                 :      174292 :         col_lineptrs[i] = pg_malloc0_array(struct lineptr,
     798                 :             :                                            (max_nl_lines[i] + 1));
     799                 :             : 
     800                 :      174292 :         format_buf[i] = pg_malloc_array(unsigned char, (max_bytes[i] + 1));
     801                 :             : 
     802                 :      174292 :         col_lineptrs[i]->ptr = format_buf[i];
     803                 :             :     }
     804                 :             : 
     805                 :             :     /* Default word wrap to the full width, i.e. no word wrap */
     806         [ +  + ]:      264876 :     for (i = 0; i < col_count; i++)
     807                 :      174292 :         width_wrap[i] = max_width[i];
     808                 :             : 
     809                 :             :     /*
     810                 :             :      * Choose target output width: \pset columns, or $COLUMNS, or ioctl
     811                 :             :      */
     812         [ +  + ]:       90584 :     if (cont->opt->columns > 0)
     813                 :         900 :         output_columns = cont->opt->columns;
     814   [ +  -  +  -  :       89684 :     else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
                   +  + ]
     815                 :             :     {
     816         [ -  + ]:          68 :         if (cont->opt->env_columns > 0)
     817                 :           0 :             output_columns = cont->opt->env_columns;
     818                 :             : #ifdef TIOCGWINSZ
     819                 :             :         else
     820                 :             :         {
     821                 :             :             struct winsize screen_size;
     822                 :             : 
     823         [ -  + ]:          68 :             if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
     824                 :           0 :                 output_columns = screen_size.ws_col;
     825                 :             :         }
     826                 :             : #endif
     827                 :             :     }
     828                 :             : 
     829         [ +  + ]:       90584 :     if (cont->opt->format == PRINT_WRAPPED)
     830                 :             :     {
     831                 :             :         /*
     832                 :             :          * Optional optimized word wrap. Shrink columns with a high max/avg
     833                 :             :          * ratio.  Slightly bias against wider columns. (Increases chance a
     834                 :             :          * narrow column will fit in its cell.)  If available columns is
     835                 :             :          * positive...  and greater than the width of the unshrinkable column
     836                 :             :          * headers
     837                 :             :          */
     838   [ +  -  +  + ]:          96 :         if (output_columns > 0 && output_columns >= total_header_width)
     839                 :             :         {
     840                 :             :             /* While there is still excess width... */
     841         [ +  + ]:         176 :             while (width_total > output_columns)
     842                 :             :             {
     843                 :         128 :                 double      max_ratio = 0;
     844                 :         128 :                 int         worst_col = -1;
     845                 :             : 
     846                 :             :                 /*
     847                 :             :                  * Find column that has the highest ratio of its maximum width
     848                 :             :                  * compared to its average width.  This tells us which column
     849                 :             :                  * will produce the fewest wrapped values if shortened.
     850                 :             :                  * width_wrap starts as equal to max_width.
     851                 :             :                  */
     852         [ +  + ]:         384 :                 for (i = 0; i < col_count; i++)
     853                 :             :                 {
     854   [ +  -  +  - ]:         256 :                     if (width_average[i] && width_wrap[i] > width_header[i])
     855                 :             :                     {
     856                 :             :                         /* Penalize wide columns by 1% of their width */
     857                 :             :                         double      ratio;
     858                 :             : 
     859                 :         256 :                         ratio = (double) width_wrap[i] / width_average[i] +
     860                 :         256 :                             max_width[i] * 0.01;
     861         [ +  + ]:         256 :                         if (ratio > max_ratio)
     862                 :             :                         {
     863                 :         168 :                             max_ratio = ratio;
     864                 :         168 :                             worst_col = i;
     865                 :             :                         }
     866                 :             :                     }
     867                 :             :                 }
     868                 :             : 
     869                 :             :                 /* Exit loop if we can't squeeze any more. */
     870         [ -  + ]:         128 :                 if (worst_col == -1)
     871                 :           0 :                     break;
     872                 :             : 
     873                 :             :                 /* Decrease width of target column by one. */
     874                 :         128 :                 width_wrap[worst_col]--;
     875                 :         128 :                 width_total--;
     876                 :             :             }
     877                 :             :         }
     878                 :             :     }
     879                 :             : 
     880                 :             :     /*
     881                 :             :      * If in expanded auto mode, we have now calculated the expected width, so
     882                 :             :      * we can now escape to vertical mode if necessary.  If the output has
     883                 :             :      * only one column, the expanded format would be wider than the regular
     884                 :             :      * format, so don't use it in that case.
     885                 :             :      */
     886   [ -  +  -  -  :       90584 :     if (cont->opt->expanded == 2 && output_columns > 0 && cont->ncolumns > 1 &&
                   -  - ]
     887   [ #  #  #  # ]:           0 :         (output_columns < total_header_width || output_columns < width_total))
     888                 :             :     {
     889                 :           0 :         print_aligned_vertical(cont, fout, is_pager);
     890                 :           0 :         goto cleanup;
     891                 :             :     }
     892                 :             : 
     893                 :             :     /* If we wrapped beyond the display width, use the pager */
     894   [ +  +  +  +  :       90584 :     if (!is_pager && fout == stdout && output_columns > 0 &&
                   +  + ]
     895   [ +  +  +  + ]:         852 :         (output_columns < total_header_width || output_columns < width_total))
     896                 :             :     {
     897                 :         408 :         fout = PageOutput(INT_MAX, cont->opt);   /* force pager */
     898                 :         408 :         is_pager = is_local_pager = true;
     899                 :             :     }
     900                 :             : 
     901                 :             :     /* Check if there are enough lines to require the pager */
     902         [ +  + ]:       90584 :     if (!is_pager)
     903                 :             :     {
     904                 :       90092 :         IsPagerNeeded(cont, width_wrap, false, &fout, &is_pager);
     905                 :       90092 :         is_local_pager = is_pager;
     906                 :             :     }
     907                 :             : 
     908                 :             :     /* time to output */
     909         [ +  + ]:       90584 :     if (cont->opt->start_table)
     910                 :             :     {
     911                 :             :         /* print title */
     912   [ +  +  +  + ]:       90536 :         if (cont->title && !opt_tuples_only)
     913                 :             :         {
     914                 :             :             int         width,
     915                 :             :                         height;
     916                 :             : 
     917                 :        3969 :             pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
     918                 :             :                        encoding, &width, &height, NULL);
     919         [ +  + ]:        3969 :             if (width >= width_total)
     920                 :             :                 /* Aligned */
     921                 :         174 :                 fprintf(fout, "%s\n", cont->title);
     922                 :             :             else
     923                 :             :                 /* Centered */
     924                 :        3795 :                 fprintf(fout, "%-*s%s\n", (width_total - width) / 2, "",
     925                 :        3795 :                         cont->title);
     926                 :             :         }
     927                 :             : 
     928                 :             :         /* print headers */
     929         [ +  + ]:       90536 :         if (!opt_tuples_only)
     930                 :             :         {
     931                 :             :             int         more_col_wrapping;
     932                 :             :             int         curr_nl_line;
     933                 :             : 
     934         [ +  + ]:       90388 :             if (opt_border == 2)
     935                 :          32 :                 _print_horizontal_line(col_count, width_wrap, opt_border,
     936                 :             :                                        PRINT_RULE_TOP, format, fout);
     937                 :             : 
     938         [ +  + ]:      264172 :             for (i = 0; i < col_count; i++)
     939                 :      173784 :                 pg_wcsformat((const unsigned char *) cont->headers[i],
     940                 :      173784 :                              strlen(cont->headers[i]), encoding,
     941                 :      173784 :                              col_lineptrs[i], max_nl_lines[i]);
     942                 :             : 
     943                 :       90388 :             more_col_wrapping = col_count;
     944                 :       90388 :             curr_nl_line = 0;
     945         [ +  + ]:       90388 :             if (col_count > 0)
     946                 :       90248 :                 memset(header_done, false, col_count * sizeof(bool));
     947         [ +  + ]:      180732 :             while (more_col_wrapping)
     948                 :             :             {
     949         [ +  + ]:       90344 :                 if (opt_border == 2)
     950                 :          64 :                     fputs(dformat->leftvrule, fout);
     951                 :             : 
     952         [ +  + ]:      264320 :                 for (i = 0; i < cont->ncolumns; i++)
     953                 :             :                 {
     954                 :      173976 :                     struct lineptr *this_line = col_lineptrs[i] + curr_nl_line;
     955                 :             :                     unsigned int nbspace;
     956                 :             : 
     957         [ +  + ]:      173976 :                     if (opt_border != 0 ||
     958   [ +  +  +  + ]:         128 :                         (!format->wrap_right_border && i > 0))
     959         [ +  + ]:      173880 :                         fputs(curr_nl_line ? format->header_nl_left : " ",
     960                 :             :                               fout);
     961                 :             : 
     962         [ +  + ]:      173976 :                     if (!header_done[i])
     963                 :             :                     {
     964                 :      173928 :                         nbspace = width_wrap[i] - this_line->width;
     965                 :             : 
     966                 :             :                         /* centered */
     967                 :      173928 :                         fprintf(fout, "%-*s%s%-*s",
     968                 :      173928 :                                 nbspace / 2, "", this_line->ptr, (nbspace + 1) / 2, "");
     969                 :             : 
     970         [ +  + ]:      173928 :                         if (!(this_line + 1)->ptr)
     971                 :             :                         {
     972                 :      173784 :                             more_col_wrapping--;
     973                 :      173784 :                             header_done[i] = 1;
     974                 :             :                         }
     975                 :             :                     }
     976                 :             :                     else
     977                 :          48 :                         fprintf(fout, "%*s", width_wrap[i], "");
     978                 :             : 
     979   [ +  +  +  + ]:      173976 :                     if (opt_border != 0 || format->wrap_right_border)
     980         [ +  + ]:      173912 :                         fputs(!header_done[i] ? format->header_nl_right : " ",
     981                 :             :                               fout);
     982                 :             : 
     983   [ +  +  +  -  :      173976 :                     if (opt_border != 0 && col_count > 0 && i < col_count - 1)
                   +  + ]
     984                 :       83568 :                         fputs(dformat->midvrule, fout);
     985                 :             :                 }
     986                 :       90344 :                 curr_nl_line++;
     987                 :             : 
     988         [ +  + ]:       90344 :                 if (opt_border == 2)
     989                 :          64 :                     fputs(dformat->rightvrule, fout);
     990                 :       90344 :                 fputc('\n', fout);
     991                 :             :             }
     992                 :             : 
     993                 :       90388 :             _print_horizontal_line(col_count, width_wrap, opt_border,
     994                 :             :                                    PRINT_RULE_MIDDLE, format, fout);
     995                 :             :         }
     996                 :             :     }
     997                 :             : 
     998                 :             :     /* print cells, one loop per row */
     999         [ +  + ]:      425254 :     for (i = 0, ptr = cont->cells; *ptr; i += col_count, ptr += col_count)
    1000                 :             :     {
    1001                 :             :         bool        more_lines;
    1002                 :             : 
    1003         [ -  + ]:      334670 :         if (CancelRequested)
    1004                 :           0 :             break;
    1005                 :             : 
    1006                 :             :         /*
    1007                 :             :          * Format each cell.
    1008                 :             :          */
    1009         [ +  + ]:     1091540 :         for (j = 0; j < col_count; j++)
    1010                 :             :         {
    1011                 :      756870 :             pg_wcsformat((const unsigned char *) ptr[j], strlen(ptr[j]), encoding,
    1012                 :      756870 :                          col_lineptrs[j], max_nl_lines[j]);
    1013                 :      756870 :             curr_nl_lines[j] = 0;
    1014                 :             :         }
    1015                 :             : 
    1016                 :      334670 :         memset(bytes_output, 0, col_count * sizeof(int));
    1017                 :             : 
    1018                 :             :         /*
    1019                 :             :          * Each time through this loop, one display line is output. It can
    1020                 :             :          * either be a full value or a partial value if embedded newlines
    1021                 :             :          * exist or if 'format=wrapping' mode is enabled.
    1022                 :             :          */
    1023                 :             :         do
    1024                 :             :         {
    1025                 :      346555 :             more_lines = false;
    1026                 :             : 
    1027                 :             :             /* left border */
    1028         [ +  + ]:      346555 :             if (opt_border == 2)
    1029                 :         368 :                 fputs(dformat->leftvrule, fout);
    1030                 :             : 
    1031                 :             :             /* for each column */
    1032         [ +  + ]:     1117970 :             for (j = 0; j < col_count; j++)
    1033                 :             :             {
    1034                 :             :                 /* We have a valid array element, so index it */
    1035                 :      771415 :                 struct lineptr *this_line = &col_lineptrs[j][curr_nl_lines[j]];
    1036                 :             :                 int         bytes_to_output;
    1037                 :      771415 :                 int         chars_to_output = width_wrap[j];
    1038   [ +  +  +  - ]:     1542094 :                 bool        finalspaces = (opt_border == 2 ||
    1039         [ +  + ]:      770679 :                                            (col_count > 0 && j < col_count - 1));
    1040                 :             : 
    1041                 :             :                 /* Print left-hand wrap or newline mark */
    1042         [ +  + ]:      771415 :                 if (opt_border != 0)
    1043                 :             :                 {
    1044         [ +  + ]:      770775 :                     if (wrap[j] == PRINT_LINE_WRAP_WRAP)
    1045                 :          80 :                         fputs(format->wrap_left, fout);
    1046         [ +  + ]:      770695 :                     else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
    1047                 :       11937 :                         fputs(format->nl_left, fout);
    1048                 :             :                     else
    1049                 :      758758 :                         fputc(' ', fout);
    1050                 :             :                 }
    1051                 :             : 
    1052         [ +  + ]:      771415 :                 if (!this_line->ptr)
    1053                 :             :                 {
    1054                 :             :                     /* Past newline lines so just pad for other columns */
    1055         [ +  + ]:        2272 :                     if (finalspaces)
    1056                 :        1945 :                         fprintf(fout, "%*s", chars_to_output, "");
    1057                 :             :                 }
    1058                 :             :                 else
    1059                 :             :                 {
    1060                 :             :                     /* Get strlen() of the characters up to width_wrap */
    1061                 :             :                     bytes_to_output =
    1062                 :      769143 :                         strlen_max_width(this_line->ptr + bytes_output[j],
    1063                 :             :                                          &chars_to_output, encoding);
    1064                 :             : 
    1065                 :             :                     /*
    1066                 :             :                      * If we exceeded width_wrap, it means the display width
    1067                 :             :                      * of a single character was wider than our target width.
    1068                 :             :                      * In that case, we have to pretend we are only printing
    1069                 :             :                      * the target display width and make the best of it.
    1070                 :             :                      */
    1071         [ -  + ]:      769143 :                     if (chars_to_output > width_wrap[j])
    1072                 :           0 :                         chars_to_output = width_wrap[j];
    1073                 :             : 
    1074         [ +  + ]:      769143 :                     if (cont->aligns[j] == 'r') /* Right aligned cell */
    1075                 :             :                     {
    1076                 :             :                         /* spaces first */
    1077                 :      301695 :                         fprintf(fout, "%*s", width_wrap[j] - chars_to_output, "");
    1078                 :      301695 :                         fwrite((char *) (this_line->ptr + bytes_output[j]),
    1079                 :             :                                1, bytes_to_output, fout);
    1080                 :             :                     }
    1081                 :             :                     else        /* Left aligned cell */
    1082                 :             :                     {
    1083                 :             :                         /* spaces second */
    1084                 :      467448 :                         fwrite((char *) (this_line->ptr + bytes_output[j]),
    1085                 :             :                                1, bytes_to_output, fout);
    1086                 :             :                     }
    1087                 :             : 
    1088                 :      769143 :                     bytes_output[j] += bytes_to_output;
    1089                 :             : 
    1090                 :             :                     /* Do we have more text to wrap? */
    1091         [ +  + ]:      769143 :                     if (*(this_line->ptr + bytes_output[j]) != '\0')
    1092                 :          80 :                         more_lines = true;
    1093                 :             :                     else
    1094                 :             :                     {
    1095                 :             :                         /* Advance to next newline line */
    1096                 :      769063 :                         curr_nl_lines[j]++;
    1097         [ +  + ]:      769063 :                         if (col_lineptrs[j][curr_nl_lines[j]].ptr != NULL)
    1098                 :       12193 :                             more_lines = true;
    1099                 :      769063 :                         bytes_output[j] = 0;
    1100                 :             :                     }
    1101                 :             :                 }
    1102                 :             : 
    1103                 :             :                 /* Determine next line's wrap status for this column */
    1104                 :      771415 :                 wrap[j] = PRINT_LINE_WRAP_NONE;
    1105         [ +  + ]:      771415 :                 if (col_lineptrs[j][curr_nl_lines[j]].ptr != NULL)
    1106                 :             :                 {
    1107         [ +  + ]:       12273 :                     if (bytes_output[j] != 0)
    1108                 :          80 :                         wrap[j] = PRINT_LINE_WRAP_WRAP;
    1109         [ +  - ]:       12193 :                     else if (curr_nl_lines[j] != 0)
    1110                 :       12193 :                         wrap[j] = PRINT_LINE_WRAP_NEWLINE;
    1111                 :             :                 }
    1112                 :             : 
    1113                 :             :                 /*
    1114                 :             :                  * If left-aligned, pad out remaining space if needed (not
    1115                 :             :                  * last column, and/or wrap marks required).
    1116                 :             :                  */
    1117         [ +  + ]:      771415 :                 if (cont->aligns[j] != 'r') /* Left aligned cell */
    1118                 :             :                 {
    1119         [ +  + ]:      469436 :                     if (finalspaces ||
    1120         [ +  + ]:      250064 :                         wrap[j] == PRINT_LINE_WRAP_WRAP ||
    1121         [ +  + ]:      250056 :                         wrap[j] == PRINT_LINE_WRAP_NEWLINE)
    1122                 :      230754 :                         fprintf(fout, "%*s",
    1123                 :      230754 :                                 width_wrap[j] - chars_to_output, "");
    1124                 :             :                 }
    1125                 :             : 
    1126                 :             :                 /* Print right-hand wrap or newline mark */
    1127         [ +  + ]:      771415 :                 if (wrap[j] == PRINT_LINE_WRAP_WRAP)
    1128                 :          80 :                     fputs(format->wrap_right, fout);
    1129         [ +  + ]:      771335 :                 else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
    1130                 :       12193 :                     fputs(format->nl_right, fout);
    1131   [ +  +  +  -  :      759142 :                 else if (opt_border == 2 || (col_count > 0 && j < col_count - 1))
                   +  + ]
    1132                 :      424337 :                     fputc(' ', fout);
    1133                 :             : 
    1134                 :             :                 /* Print column divider, if not the last column */
    1135   [ +  +  +  -  :      771415 :                 if (opt_border != 0 && (col_count > 0 && j < col_count - 1))
                   +  + ]
    1136                 :             :                 {
    1137         [ +  + ]:      424540 :                     if (wrap[j + 1] == PRINT_LINE_WRAP_WRAP)
    1138                 :          24 :                         fputs(format->midvrule_wrap, fout);
    1139         [ +  + ]:      424516 :                     else if (wrap[j + 1] == PRINT_LINE_WRAP_NEWLINE)
    1140                 :         850 :                         fputs(format->midvrule_nl, fout);
    1141         [ +  + ]:      423666 :                     else if (col_lineptrs[j + 1][curr_nl_lines[j + 1]].ptr == NULL)
    1142                 :        1658 :                         fputs(format->midvrule_blank, fout);
    1143                 :             :                     else
    1144                 :      422008 :                         fputs(dformat->midvrule, fout);
    1145                 :             :                 }
    1146                 :             :             }
    1147                 :             : 
    1148                 :             :             /* end-of-row border */
    1149         [ +  + ]:      346555 :             if (opt_border == 2)
    1150                 :         368 :                 fputs(dformat->rightvrule, fout);
    1151                 :      346555 :             fputc('\n', fout);
    1152         [ +  + ]:      346555 :         } while (more_lines);
    1153                 :             :     }
    1154                 :             : 
    1155         [ +  + ]:       90584 :     if (cont->opt->stop_table)
    1156                 :             :     {
    1157                 :       90532 :         printTableFooter *footers = footers_with_default(cont);
    1158                 :             : 
    1159   [ +  +  +  - ]:       90532 :         if (opt_border == 2 && !CancelRequested)
    1160                 :          32 :             _print_horizontal_line(col_count, width_wrap, opt_border,
    1161                 :             :                                    PRINT_RULE_BOTTOM, format, fout);
    1162                 :             : 
    1163                 :             :         /* print footers */
    1164   [ +  +  +  +  :       90532 :         if (footers && !opt_tuples_only && !CancelRequested)
                   +  - ]
    1165                 :             :         {
    1166                 :             :             printTableFooter *f;
    1167                 :             : 
    1168         [ +  + ]:      186324 :             for (f = footers; f; f = f->next)
    1169                 :       96294 :                 fprintf(fout, "%s\n", f->data);
    1170                 :             :         }
    1171                 :             : 
    1172                 :       90532 :         fputc('\n', fout);
    1173                 :             :     }
    1174                 :             : 
    1175                 :          52 : cleanup:
    1176                 :             :     /* clean up */
    1177         [ +  + ]:      264876 :     for (i = 0; i < col_count; i++)
    1178                 :             :     {
    1179                 :      174292 :         pg_free(col_lineptrs[i]);
    1180                 :      174292 :         pg_free(format_buf[i]);
    1181                 :             :     }
    1182                 :       90584 :     pg_free(width_header);
    1183                 :       90584 :     pg_free(width_average);
    1184                 :       90584 :     pg_free(max_width);
    1185                 :       90584 :     pg_free(width_wrap);
    1186                 :       90584 :     pg_free(max_nl_lines);
    1187                 :       90584 :     pg_free(curr_nl_lines);
    1188                 :       90584 :     pg_free(col_lineptrs);
    1189                 :       90584 :     pg_free(max_bytes);
    1190                 :       90584 :     pg_free(format_buf);
    1191                 :       90584 :     pg_free(header_done);
    1192                 :       90584 :     pg_free(bytes_output);
    1193                 :       90584 :     pg_free(wrap);
    1194                 :             : 
    1195         [ +  + ]:       90584 :     if (is_local_pager)
    1196                 :         408 :         ClosePager(fout);
    1197                 :             : }
    1198                 :             : 
    1199                 :             : 
    1200                 :             : static void
    1201                 :        1162 : print_aligned_vertical_line(const printTableOpt *topt,
    1202                 :             :                             unsigned long record,
    1203                 :             :                             unsigned int hwidth,
    1204                 :             :                             unsigned int dwidth,
    1205                 :             :                             int output_columns,
    1206                 :             :                             printTextRule pos,
    1207                 :             :                             FILE *fout)
    1208                 :             : {
    1209                 :        1162 :     const printTextLineFormat *lformat = &get_line_style(topt)->lrule[pos];
    1210                 :        1162 :     const unsigned short opt_border = topt->border;
    1211                 :             :     unsigned int i;
    1212                 :        1162 :     int         reclen = 0;
    1213                 :             : 
    1214         [ +  + ]:        1162 :     if (opt_border == 2)
    1215                 :         336 :         fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
    1216         [ +  + ]:         826 :     else if (opt_border == 1)
    1217                 :         554 :         fputs(lformat->hrule, fout);
    1218                 :             : 
    1219         [ +  + ]:        1162 :     if (record)
    1220                 :             :     {
    1221         [ +  + ]:        1102 :         if (opt_border == 0)
    1222                 :         272 :             reclen = fprintf(fout, "* Record %lu", record);
    1223                 :             :         else
    1224                 :         830 :             reclen = fprintf(fout, "[ RECORD %lu ]", record);
    1225                 :             :     }
    1226         [ +  + ]:        1162 :     if (opt_border != 2)
    1227                 :         826 :         reclen++;
    1228         [ -  + ]:        1162 :     if (reclen < 0)
    1229                 :           0 :         reclen = 0;
    1230         [ +  + ]:        5765 :     for (i = reclen; i < hwidth; i++)
    1231         [ +  + ]:        4603 :         fputs(opt_border > 0 ? lformat->hrule : " ", fout);
    1232                 :        1162 :     reclen -= hwidth;
    1233                 :             : 
    1234         [ +  + ]:        1162 :     if (opt_border > 0)
    1235                 :             :     {
    1236         [ +  + ]:         890 :         if (reclen-- <= 0)
    1237                 :         733 :             fputs(lformat->hrule, fout);
    1238         [ +  + ]:         890 :         if (reclen-- <= 0)
    1239                 :             :         {
    1240         [ -  + ]:         737 :             if (topt->expanded_header_width_type == PRINT_XHEADER_COLUMN)
    1241                 :             :             {
    1242                 :           0 :                 fputs(lformat->rightvrule, fout);
    1243                 :             :             }
    1244                 :             :             else
    1245                 :             :             {
    1246                 :         737 :                 fputs(lformat->midvrule, fout);
    1247                 :             :             }
    1248                 :             :         }
    1249         [ +  + ]:         890 :         if (reclen-- <= 0
    1250         [ +  - ]:         753 :             && topt->expanded_header_width_type != PRINT_XHEADER_COLUMN)
    1251                 :         753 :             fputs(lformat->hrule, fout);
    1252                 :             :     }
    1253                 :             :     else
    1254                 :             :     {
    1255         [ +  + ]:         272 :         if (reclen-- <= 0)
    1256                 :         240 :             fputc(' ', fout);
    1257                 :             :     }
    1258                 :             : 
    1259         [ +  - ]:        1162 :     if (topt->expanded_header_width_type != PRINT_XHEADER_COLUMN)
    1260                 :             :     {
    1261         [ +  - ]:        1162 :         if (topt->expanded_header_width_type == PRINT_XHEADER_PAGE
    1262         [ -  + ]:        1162 :             || topt->expanded_header_width_type == PRINT_XHEADER_EXACT_WIDTH)
    1263                 :             :         {
    1264         [ #  # ]:           0 :             if (topt->expanded_header_width_type == PRINT_XHEADER_EXACT_WIDTH)
    1265                 :             :             {
    1266                 :           0 :                 output_columns = topt->expanded_header_exact_width;
    1267                 :             :             }
    1268         [ #  # ]:           0 :             if (output_columns > 0)
    1269                 :             :             {
    1270         [ #  # ]:           0 :                 if (opt_border == 0)
    1271                 :           0 :                     dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth)));
    1272         [ #  # ]:           0 :                 if (opt_border == 1)
    1273                 :           0 :                     dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth - 3)));
    1274                 :             : 
    1275                 :             :                 /*
    1276                 :             :                  * Handling the xheader width for border=2 doesn't make much
    1277                 :             :                  * sense because this format has an additional right border,
    1278                 :             :                  * but keep this for consistency.
    1279                 :             :                  */
    1280         [ #  # ]:           0 :                 if (opt_border == 2)
    1281                 :           0 :                     dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth - 7)));
    1282                 :             :             }
    1283                 :             :         }
    1284                 :             : 
    1285         [ +  + ]:        1162 :         if (reclen < 0)
    1286                 :         993 :             reclen = 0;
    1287         [ -  + ]:        1162 :         if (dwidth < reclen)
    1288                 :           0 :             dwidth = reclen;
    1289                 :             : 
    1290         [ +  + ]:       44615 :         for (i = reclen; i < dwidth; i++)
    1291         [ +  + ]:       43453 :             fputs(opt_border > 0 ? lformat->hrule : " ", fout);
    1292         [ +  + ]:        1162 :         if (opt_border == 2)
    1293                 :         336 :             fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
    1294                 :             :     }
    1295                 :             : 
    1296                 :        1162 :     fputc('\n', fout);
    1297                 :        1162 : }
    1298                 :             : 
    1299                 :             : static void
    1300                 :         401 : print_aligned_vertical(const printTableContent *cont,
    1301                 :             :                        FILE *fout, bool is_pager)
    1302                 :             : {
    1303                 :         401 :     bool        opt_tuples_only = cont->opt->tuples_only;
    1304                 :         401 :     unsigned short opt_border = cont->opt->border;
    1305                 :         401 :     const printTextFormat *format = get_line_style(cont->opt);
    1306                 :         401 :     const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
    1307                 :         401 :     int         encoding = cont->opt->encoding;
    1308                 :         401 :     unsigned long record = cont->opt->prior_records + 1;
    1309                 :             :     const char *const *ptr;
    1310                 :             :     unsigned int i,
    1311                 :         401 :                 hwidth = 0,
    1312                 :         401 :                 dwidth = 0,
    1313                 :         401 :                 hheight = 1,
    1314                 :         401 :                 dheight = 1,
    1315                 :         401 :                 hformatsize = 0,
    1316                 :         401 :                 dformatsize = 0;
    1317                 :             :     struct lineptr *hlineptr,
    1318                 :             :                *dlineptr;
    1319                 :         401 :     bool        is_local_pager = false,
    1320                 :         401 :                 hmultiline = false,
    1321                 :         401 :                 dmultiline = false;
    1322                 :         401 :     int         output_columns = 0; /* Width of interactive console */
    1323                 :             : 
    1324         [ -  + ]:         401 :     if (CancelRequested)
    1325                 :           0 :         return;
    1326                 :             : 
    1327         [ -  + ]:         401 :     if (opt_border > 2)
    1328                 :           0 :         opt_border = 2;
    1329                 :             : 
    1330                 :             :     /*
    1331                 :             :      * Kluge for totally empty table: use the default footer even though
    1332                 :             :      * vertical modes normally don't.  Otherwise we'd print nothing at all,
    1333                 :             :      * which isn't terribly friendly.  Assume pager will not be needed.
    1334                 :             :      */
    1335   [ +  +  +  + ]:         401 :     if (cont->cells[0] == NULL && cont->opt->start_table &&
    1336         [ +  - ]:          42 :         cont->opt->stop_table)
    1337                 :             :     {
    1338                 :          42 :         printTableFooter *footers = footers_with_default(cont);
    1339                 :             : 
    1340   [ +  -  +  -  :          42 :         if (!opt_tuples_only && !CancelRequested && footers)
                   +  - ]
    1341                 :             :         {
    1342                 :             :             printTableFooter *f;
    1343                 :             : 
    1344         [ +  + ]:          84 :             for (f = footers; f; f = f->next)
    1345                 :          42 :                 fprintf(fout, "%s\n", f->data);
    1346                 :             :         }
    1347                 :             : 
    1348                 :          42 :         fputc('\n', fout);
    1349                 :             : 
    1350                 :          42 :         return;
    1351                 :             :     }
    1352                 :             : 
    1353                 :             :     /* Find the maximum dimensions for the headers */
    1354         [ +  + ]:        2093 :     for (i = 0; i < cont->ncolumns; i++)
    1355                 :             :     {
    1356                 :             :         int         width,
    1357                 :             :                     height,
    1358                 :             :                     fs;
    1359                 :             : 
    1360                 :        1734 :         pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
    1361                 :             :                    encoding, &width, &height, &fs);
    1362         [ +  + ]:        1734 :         if (width > hwidth)
    1363                 :         640 :             hwidth = width;
    1364         [ +  + ]:        1734 :         if (height > hheight)
    1365                 :             :         {
    1366                 :          48 :             hheight = height;
    1367                 :          48 :             hmultiline = true;
    1368                 :             :         }
    1369         [ +  + ]:        1734 :         if (fs > hformatsize)
    1370                 :         640 :             hformatsize = fs;
    1371                 :             :     }
    1372                 :             : 
    1373                 :             :     /* find longest data cell */
    1374         [ +  + ]:        3922 :     for (ptr = cont->cells; *ptr; ptr++)
    1375                 :             :     {
    1376                 :             :         int         width,
    1377                 :             :                     height,
    1378                 :             :                     fs;
    1379                 :             : 
    1380                 :        3563 :         pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
    1381                 :             :                    &width, &height, &fs);
    1382         [ +  + ]:        3563 :         if (width > dwidth)
    1383                 :         747 :             dwidth = width;
    1384         [ +  + ]:        3563 :         if (height > dheight)
    1385                 :             :         {
    1386                 :          68 :             dheight = height;
    1387                 :          68 :             dmultiline = true;
    1388                 :             :         }
    1389         [ +  + ]:        3563 :         if (fs > dformatsize)
    1390                 :         747 :             dformatsize = fs;
    1391                 :             :     }
    1392                 :             : 
    1393                 :             :     /*
    1394                 :             :      * We now have all the information we need to setup the formatting
    1395                 :             :      * structures
    1396                 :             :      */
    1397                 :         359 :     dlineptr = pg_malloc_array(struct lineptr, (dheight + 1));
    1398                 :         359 :     hlineptr = pg_malloc_array(struct lineptr, (hheight + 1));
    1399                 :             : 
    1400                 :         359 :     dlineptr->ptr = pg_malloc(dformatsize);
    1401                 :         359 :     hlineptr->ptr = pg_malloc(hformatsize);
    1402                 :             : 
    1403                 :             :     /*
    1404                 :             :      * Choose target output width: \pset columns, or $COLUMNS, or ioctl
    1405                 :             :      */
    1406         [ +  + ]:         359 :     if (cont->opt->columns > 0)
    1407                 :         160 :         output_columns = cont->opt->columns;
    1408   [ +  -  +  +  :         199 :     else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
                   +  + ]
    1409                 :             :     {
    1410         [ -  + ]:          17 :         if (cont->opt->env_columns > 0)
    1411                 :           0 :             output_columns = cont->opt->env_columns;
    1412                 :             : #ifdef TIOCGWINSZ
    1413                 :             :         else
    1414                 :             :         {
    1415                 :             :             struct winsize screen_size;
    1416                 :             : 
    1417         [ +  + ]:          17 :             if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
    1418                 :           1 :                 output_columns = screen_size.ws_col;
    1419                 :             :         }
    1420                 :             : #endif
    1421                 :             :     }
    1422                 :             : 
    1423                 :             :     /*
    1424                 :             :      * Determine data column width: fit output width in wrapped mode, or
    1425                 :             :      * ensure alignment with the record header line in aligned mode.
    1426                 :             :      */
    1427   [ +  +  +  - ]:         359 :     if (cont->opt->format == PRINT_WRAPPED || cont->opt->format == PRINT_ALIGNED)
    1428                 :             :     {
    1429                 :             :         unsigned int swidth,
    1430                 :         359 :                     rwidth = 0,
    1431                 :             :                     newdwidth;
    1432                 :             : 
    1433         [ +  + ]:         359 :         if (opt_border == 0)
    1434                 :             :         {
    1435                 :             :             /*
    1436                 :             :              * For border = 0, one space in the middle.  (If we discover we
    1437                 :             :              * need to wrap, the spacer column will be replaced by a wrap
    1438                 :             :              * marker, and we'll make room below for another wrap marker at
    1439                 :             :              * the end of the line.  But for now, assume no wrap is needed.)
    1440                 :             :              */
    1441                 :          40 :             swidth = 1;
    1442                 :             : 
    1443                 :             :             /* We might need a column for header newline markers, too */
    1444         [ +  + ]:          40 :             if (hmultiline)
    1445                 :          16 :                 swidth++;
    1446                 :             :         }
    1447         [ +  + ]:         319 :         else if (opt_border == 1)
    1448                 :             :         {
    1449                 :             :             /*
    1450                 :             :              * For border = 1, two spaces and a vrule in the middle.  (As
    1451                 :             :              * above, we might need one more column for a wrap marker.)
    1452                 :             :              */
    1453                 :         271 :             swidth = 3;
    1454                 :             : 
    1455                 :             :             /* We might need a column for left header newline markers, too */
    1456   [ +  +  +  + ]:         271 :             if (hmultiline && (format == &pg_asciiformat_old))
    1457                 :           8 :                 swidth++;
    1458                 :             :         }
    1459                 :             :         else
    1460                 :             :         {
    1461                 :             :             /*
    1462                 :             :              * For border = 2, two more for the vrules at the beginning and
    1463                 :             :              * end of the lines, plus spacer columns adjacent to these.  (We
    1464                 :             :              * won't need extra columns for wrap/newline markers, we'll just
    1465                 :             :              * repurpose the spacers.)
    1466                 :             :              */
    1467                 :          48 :             swidth = 7;
    1468                 :             :         }
    1469                 :             : 
    1470                 :             :         /* Reserve a column for data newline indicators, too, if needed */
    1471   [ +  +  +  + ]:         359 :         if (dmultiline &&
    1472         [ +  + ]:          48 :             opt_border < 2 && format != &pg_asciiformat_old)
    1473                 :          32 :             swidth++;
    1474                 :             : 
    1475                 :             :         /* Determine width required for record header lines */
    1476         [ +  + ]:         359 :         if (!opt_tuples_only)
    1477                 :             :         {
    1478         [ +  + ]:         347 :             if (cont->nrows > 0)
    1479                 :         339 :                 rwidth = 1 + (int) log10(cont->nrows);
    1480         [ +  + ]:         347 :             if (opt_border == 0)
    1481                 :          40 :                 rwidth += 9;    /* "* RECORD " */
    1482         [ +  + ]:         307 :             else if (opt_border == 1)
    1483                 :         259 :                 rwidth += 12;   /* "-[ RECORD  ]" */
    1484                 :             :             else
    1485                 :          48 :                 rwidth += 15;   /* "+-[ RECORD  ]-+" */
    1486                 :             :         }
    1487                 :             : 
    1488                 :             :         /* We might need to do the rest of the calculation twice */
    1489                 :             :         for (;;)
    1490                 :          17 :         {
    1491                 :             :             unsigned int width;
    1492                 :             : 
    1493                 :             :             /* Total width required to not wrap data */
    1494                 :         376 :             width = hwidth + swidth + dwidth;
    1495                 :             :             /* ... and not the header lines, either */
    1496         [ +  + ]:         376 :             if (width < rwidth)
    1497                 :          37 :                 width = rwidth;
    1498                 :             : 
    1499   [ +  +  +  - ]:         376 :             if (cont->opt->format == PRINT_WRAPPED && output_columns > 0)
    1500                 :          94 :             {
    1501                 :             :                 unsigned int min_width;
    1502                 :             : 
    1503                 :             :                 /* Minimum acceptable width: room for just 3 columns of data */
    1504                 :          94 :                 min_width = hwidth + swidth + 3;
    1505                 :             :                 /* ... but not less than what the record header lines need */
    1506         [ +  + ]:          94 :                 if (min_width < rwidth)
    1507                 :          29 :                     min_width = rwidth;
    1508                 :             : 
    1509         [ +  + ]:          94 :                 if (output_columns >= width)
    1510                 :             :                 {
    1511                 :             :                     /* Plenty of room, use native data width */
    1512                 :             :                     /* (but at least enough for the record header lines) */
    1513                 :          24 :                     newdwidth = width - hwidth - swidth;
    1514                 :             :                 }
    1515         [ +  + ]:          70 :                 else if (output_columns < min_width)
    1516                 :             :                 {
    1517                 :             :                     /* Set data width to match min_width */
    1518                 :          16 :                     newdwidth = min_width - hwidth - swidth;
    1519                 :             :                 }
    1520                 :             :                 else
    1521                 :             :                 {
    1522                 :             :                     /* Set data width to match output_columns */
    1523                 :          54 :                     newdwidth = output_columns - hwidth - swidth;
    1524                 :             :                 }
    1525                 :             :             }
    1526                 :             :             else
    1527                 :             :             {
    1528                 :             :                 /* Don't know the wrap limit, so use native data width */
    1529                 :             :                 /* (but at least enough for the record header lines) */
    1530                 :         282 :                 newdwidth = width - hwidth - swidth;
    1531                 :             :             }
    1532                 :             : 
    1533                 :             :             /*
    1534                 :             :              * If we will need to wrap data and didn't already allocate a data
    1535                 :             :              * newline/wrap marker column, do so and recompute.
    1536                 :             :              */
    1537   [ +  +  +  +  :         376 :             if (newdwidth < dwidth && !dmultiline &&
                   +  + ]
    1538         [ +  - ]:          17 :                 opt_border < 2 && format != &pg_asciiformat_old)
    1539                 :             :             {
    1540                 :          17 :                 dmultiline = true;
    1541                 :          17 :                 swidth++;
    1542                 :             :             }
    1543                 :             :             else
    1544                 :             :                 break;
    1545                 :             :         }
    1546                 :             : 
    1547                 :         359 :         dwidth = newdwidth;
    1548                 :             :     }
    1549                 :             : 
    1550                 :             :     /*
    1551                 :             :      * Deal with the pager here instead of in printTable(), because we could
    1552                 :             :      * get here via print_aligned_text() in expanded auto mode, and so we have
    1553                 :             :      * to recalculate the pager requirement based on vertical output.
    1554                 :             :      */
    1555         [ +  + ]:         359 :     if (!is_pager)
    1556                 :             :     {
    1557                 :         343 :         unsigned int *width_wrap = NULL;
    1558                 :             : 
    1559                 :             :         /*
    1560                 :             :          * Wrapping can add extra output lines, which count_table_lines() can
    1561                 :             :          * only account for if it has wrap widths.  But vertical output uses
    1562                 :             :          * the same data width for every field, so that's easy: use dwidth for
    1563                 :             :          * every column.
    1564                 :             :          */
    1565   [ +  +  +  - ]:         343 :         if (cont->opt->format == PRINT_WRAPPED && cont->ncolumns > 0)
    1566                 :             :         {
    1567                 :          77 :             width_wrap = pg_malloc_array(unsigned int, cont->ncolumns);
    1568         [ +  + ]:         282 :             for (int j = 0; j < cont->ncolumns; j++)
    1569                 :         205 :                 width_wrap[j] = dwidth;
    1570                 :             :         }
    1571                 :             : 
    1572                 :         343 :         IsPagerNeeded(cont, width_wrap, true, &fout, &is_pager);
    1573                 :         343 :         is_local_pager = is_pager;
    1574                 :             : 
    1575                 :         343 :         free(width_wrap);
    1576                 :             :     }
    1577                 :             : 
    1578         [ +  + ]:         359 :     if (cont->opt->start_table)
    1579                 :             :     {
    1580                 :             :         /* print title */
    1581   [ +  +  +  + ]:         351 :         if (!opt_tuples_only && cont->title)
    1582                 :          24 :             fprintf(fout, "%s\n", cont->title);
    1583                 :             :     }
    1584                 :             : 
    1585                 :             :     /* print records */
    1586         [ +  + ]:        3922 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    1587                 :             :     {
    1588                 :             :         printTextRule pos;
    1589                 :             :         int         dline,
    1590                 :             :                     hline,
    1591                 :             :                     dcomplete,
    1592                 :             :                     hcomplete,
    1593                 :             :                     offset,
    1594                 :             :                     chars_to_output;
    1595                 :             : 
    1596         [ -  + ]:        3563 :         if (CancelRequested)
    1597                 :           0 :             break;
    1598                 :             : 
    1599         [ +  + ]:        3563 :         if (i == 0)
    1600                 :         351 :             pos = PRINT_RULE_TOP;
    1601                 :             :         else
    1602                 :        3212 :             pos = PRINT_RULE_MIDDLE;
    1603                 :             : 
    1604                 :             :         /* Print record header (e.g. "[ RECORD N ]") above each record */
    1605         [ +  + ]:        3563 :         if (i % cont->ncolumns == 0)
    1606                 :             :         {
    1607                 :        1126 :             unsigned int lhwidth = hwidth;
    1608                 :             : 
    1609   [ +  +  +  + ]:        1126 :             if ((opt_border < 2) &&
    1610         [ +  + ]:          64 :                 (hmultiline) &&
    1611                 :             :                 (format == &pg_asciiformat_old))
    1612                 :          32 :                 lhwidth++;      /* for newline indicators */
    1613                 :             : 
    1614         [ +  + ]:        1126 :             if (!opt_tuples_only)
    1615                 :        1102 :                 print_aligned_vertical_line(cont->opt, record++,
    1616                 :             :                                             lhwidth, dwidth, output_columns,
    1617                 :             :                                             pos, fout);
    1618   [ +  +  +  -  :          24 :             else if (i != 0 || !cont->opt->start_table || opt_border == 2)
                   -  + ]
    1619                 :          12 :                 print_aligned_vertical_line(cont->opt, 0, lhwidth,
    1620                 :             :                                             dwidth, output_columns, pos, fout);
    1621                 :             :         }
    1622                 :             : 
    1623                 :             :         /* Format the header */
    1624                 :        3563 :         pg_wcsformat((const unsigned char *) cont->headers[i % cont->ncolumns],
    1625                 :        3563 :                      strlen(cont->headers[i % cont->ncolumns]),
    1626                 :             :                      encoding, hlineptr, hheight);
    1627                 :             :         /* Format the data */
    1628                 :        3563 :         pg_wcsformat((const unsigned char *) *ptr, strlen(*ptr), encoding,
    1629                 :             :                      dlineptr, dheight);
    1630                 :             : 
    1631                 :             :         /*
    1632                 :             :          * Loop through header and data in parallel dealing with newlines and
    1633                 :             :          * wrapped lines until they're both exhausted
    1634                 :             :          */
    1635                 :        3563 :         dline = hline = 0;
    1636                 :        3563 :         dcomplete = hcomplete = 0;
    1637                 :        3563 :         offset = 0;
    1638                 :        3563 :         chars_to_output = dlineptr[dline].width;
    1639   [ +  +  +  + ]:        9065 :         while (!dcomplete || !hcomplete)
    1640                 :             :         {
    1641                 :             :             /* Left border */
    1642         [ +  + ]:        5502 :             if (opt_border == 2)
    1643                 :        1244 :                 fprintf(fout, "%s", dformat->leftvrule);
    1644                 :             : 
    1645                 :             :             /* Header (never wrapped so just need to deal with newlines) */
    1646         [ +  + ]:        5502 :             if (!hcomplete)
    1647                 :             :             {
    1648                 :        3851 :                 int         swidth = hwidth,
    1649                 :        3851 :                             target_width = hwidth;
    1650                 :             : 
    1651                 :             :                 /*
    1652                 :             :                  * Left spacer or new line indicator
    1653                 :             :                  */
    1654   [ +  +  +  + ]:        3851 :                 if ((opt_border == 2) ||
    1655         [ +  + ]:         320 :                     (hmultiline && (format == &pg_asciiformat_old)))
    1656         [ +  + ]:         832 :                     fputs(hline ? format->header_nl_left : " ", fout);
    1657                 :             : 
    1658                 :             :                 /*
    1659                 :             :                  * Header text
    1660                 :             :                  */
    1661                 :        3851 :                 strlen_max_width(hlineptr[hline].ptr, &target_width,
    1662                 :             :                                  encoding);
    1663                 :        3851 :                 fprintf(fout, "%-s", hlineptr[hline].ptr);
    1664                 :             : 
    1665                 :             :                 /*
    1666                 :             :                  * Spacer
    1667                 :             :                  */
    1668                 :        3851 :                 swidth -= target_width;
    1669         [ +  + ]:        3851 :                 if (swidth > 0)
    1670                 :        2463 :                     fprintf(fout, "%*s", swidth, " ");
    1671                 :             : 
    1672                 :             :                 /*
    1673                 :             :                  * New line indicator or separator's space
    1674                 :             :                  */
    1675         [ +  + ]:        3851 :                 if (hlineptr[hline + 1].ptr)
    1676                 :             :                 {
    1677                 :             :                     /* More lines after this one due to a newline */
    1678   [ +  +  +  - ]:         288 :                     if ((opt_border > 0) ||
    1679         [ +  + ]:          96 :                         (hmultiline && (format != &pg_asciiformat_old)))
    1680                 :         240 :                         fputs(format->header_nl_right, fout);
    1681                 :         288 :                     hline++;
    1682                 :             :                 }
    1683                 :             :                 else
    1684                 :             :                 {
    1685                 :             :                     /* This was the last line of the header */
    1686   [ +  +  +  + ]:        3563 :                     if ((opt_border > 0) ||
    1687         [ +  + ]:          64 :                         (hmultiline && (format != &pg_asciiformat_old)))
    1688                 :        3051 :                         fputs(" ", fout);
    1689                 :        3563 :                     hcomplete = 1;
    1690                 :             :                 }
    1691                 :             :             }
    1692                 :             :             else
    1693                 :             :             {
    1694                 :        1651 :                 unsigned int swidth = hwidth + opt_border;
    1695                 :             : 
    1696   [ +  +  +  + ]:        1651 :                 if ((opt_border < 2) &&
    1697         [ +  + ]:         472 :                     (hmultiline) &&
    1698                 :             :                     (format == &pg_asciiformat_old))
    1699                 :         232 :                     swidth++;
    1700                 :             : 
    1701   [ +  +  +  + ]:        1651 :                 if ((opt_border == 0) &&
    1702         [ +  + ]:         364 :                     (format != &pg_asciiformat_old) &&
    1703                 :             :                     (hmultiline))
    1704                 :         120 :                     swidth++;
    1705                 :             : 
    1706                 :        1651 :                 fprintf(fout, "%*s", swidth, " ");
    1707                 :             :             }
    1708                 :             : 
    1709                 :             :             /* Separator */
    1710         [ +  + ]:        5502 :             if (opt_border > 0)
    1711                 :             :             {
    1712         [ +  + ]:        4386 :                 if (offset)
    1713                 :         755 :                     fputs(format->midvrule_wrap, fout);
    1714         [ +  + ]:        3631 :                 else if (dline == 0)
    1715                 :        3019 :                     fputs(dformat->midvrule, fout);
    1716                 :             :                 else
    1717                 :         612 :                     fputs(format->midvrule_nl, fout);
    1718                 :             :             }
    1719                 :             : 
    1720                 :             :             /* Data */
    1721         [ +  + ]:        5502 :             if (!dcomplete)
    1722                 :             :             {
    1723                 :        5382 :                 int         target_width = dwidth,
    1724                 :             :                             bytes_to_output,
    1725                 :        5382 :                             swidth = dwidth;
    1726                 :             : 
    1727                 :             :                 /*
    1728                 :             :                  * Left spacer or wrap indicator
    1729                 :             :                  */
    1730         [ +  + ]:        5382 :                 fputs(offset == 0 ? " " : format->wrap_left, fout);
    1731                 :             : 
    1732                 :             :                 /*
    1733                 :             :                  * Data text
    1734                 :             :                  */
    1735                 :        5382 :                 bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
    1736                 :             :                                                    &target_width, encoding);
    1737                 :        5382 :                 fwrite((char *) (dlineptr[dline].ptr + offset),
    1738                 :             :                        1, bytes_to_output, fout);
    1739                 :             : 
    1740                 :        5382 :                 chars_to_output -= target_width;
    1741                 :        5382 :                 offset += bytes_to_output;
    1742                 :             : 
    1743                 :             :                 /* Spacer */
    1744                 :        5382 :                 swidth -= target_width;
    1745                 :             : 
    1746         [ +  + ]:        5382 :                 if (chars_to_output)
    1747                 :             :                 {
    1748                 :             :                     /* continuing a wrapped column */
    1749   [ +  +  +  - ]:         951 :                     if ((opt_border > 1) ||
    1750         [ +  + ]:         579 :                         (dmultiline && (format != &pg_asciiformat_old)))
    1751                 :             :                     {
    1752         [ -  + ]:         919 :                         if (swidth > 0)
    1753                 :           0 :                             fprintf(fout, "%*s", swidth, " ");
    1754                 :         919 :                         fputs(format->wrap_right, fout);
    1755                 :             :                     }
    1756                 :             :                 }
    1757         [ +  + ]:        4431 :                 else if (dlineptr[dline + 1].ptr)
    1758                 :             :                 {
    1759                 :             :                     /* reached a newline in the column */
    1760   [ +  +  +  - ]:         868 :                     if ((opt_border > 1) ||
    1761         [ +  + ]:         612 :                         (dmultiline && (format != &pg_asciiformat_old)))
    1762                 :             :                     {
    1763         [ +  + ]:         612 :                         if (swidth > 0)
    1764                 :         596 :                             fprintf(fout, "%*s", swidth, " ");
    1765                 :         612 :                         fputs(format->nl_right, fout);
    1766                 :             :                     }
    1767                 :         868 :                     dline++;
    1768                 :         868 :                     offset = 0;
    1769                 :         868 :                     chars_to_output = dlineptr[dline].width;
    1770                 :             :                 }
    1771                 :             :                 else
    1772                 :             :                 {
    1773                 :             :                     /* reached the end of the cell */
    1774         [ +  + ]:        3563 :                     if (opt_border > 1)
    1775                 :             :                     {
    1776         [ +  + ]:         576 :                         if (swidth > 0)
    1777                 :         524 :                             fprintf(fout, "%*s", swidth, " ");
    1778                 :         576 :                         fputs(" ", fout);
    1779                 :             :                     }
    1780                 :        3563 :                     dcomplete = 1;
    1781                 :             :                 }
    1782                 :             : 
    1783                 :             :                 /* Right border */
    1784         [ +  + ]:        5382 :                 if (opt_border == 2)
    1785                 :        1204 :                     fputs(dformat->rightvrule, fout);
    1786                 :             : 
    1787                 :        5382 :                 fputs("\n", fout);
    1788                 :             :             }
    1789                 :             :             else
    1790                 :             :             {
    1791                 :             :                 /*
    1792                 :             :                  * data exhausted (this can occur if header is longer than the
    1793                 :             :                  * data due to newlines in the header)
    1794                 :             :                  */
    1795         [ +  + ]:         120 :                 if (opt_border < 2)
    1796                 :          80 :                     fputs("\n", fout);
    1797                 :             :                 else
    1798                 :          40 :                     fprintf(fout, "%*s  %s\n", dwidth, "", dformat->rightvrule);
    1799                 :             :             }
    1800                 :             :         }
    1801                 :             :     }
    1802                 :             : 
    1803         [ +  + ]:         359 :     if (cont->opt->stop_table)
    1804                 :             :     {
    1805   [ +  +  +  - ]:         351 :         if (opt_border == 2 && !CancelRequested)
    1806                 :          48 :             print_aligned_vertical_line(cont->opt, 0, hwidth, dwidth,
    1807                 :             :                                         output_columns, PRINT_RULE_BOTTOM, fout);
    1808                 :             : 
    1809                 :             :         /* print footers */
    1810   [ +  +  +  +  :         351 :         if (!opt_tuples_only && cont->footers != NULL && !CancelRequested)
                   +  - ]
    1811                 :             :         {
    1812                 :             :             printTableFooter *f;
    1813                 :             : 
    1814         [ +  - ]:           8 :             if (opt_border < 2)
    1815                 :           8 :                 fputc('\n', fout);
    1816         [ +  + ]:          16 :             for (f = cont->footers; f; f = f->next)
    1817                 :           8 :                 fprintf(fout, "%s\n", f->data);
    1818                 :             :         }
    1819                 :             : 
    1820                 :         351 :         fputc('\n', fout);
    1821                 :             :     }
    1822                 :             : 
    1823                 :         359 :     pg_free(hlineptr->ptr);
    1824                 :         359 :     pg_free(dlineptr->ptr);
    1825                 :         359 :     pg_free(hlineptr);
    1826                 :         359 :     pg_free(dlineptr);
    1827                 :             : 
    1828         [ +  + ]:         359 :     if (is_local_pager)
    1829                 :           1 :         ClosePager(fout);
    1830                 :             : }
    1831                 :             : 
    1832                 :             : 
    1833                 :             : /**********************/
    1834                 :             : /* CSV format         */
    1835                 :             : /**********************/
    1836                 :             : 
    1837                 :             : 
    1838                 :             : static void
    1839                 :          72 : csv_escaped_print(const char *str, FILE *fout)
    1840                 :             : {
    1841                 :             :     const char *p;
    1842                 :             : 
    1843                 :          72 :     fputc('"', fout);
    1844         [ +  + ]:         616 :     for (p = str; *p; p++)
    1845                 :             :     {
    1846         [ +  + ]:         544 :         if (*p == '"')
    1847                 :          28 :             fputc('"', fout);  /* double quotes are doubled */
    1848                 :         544 :         fputc(*p, fout);
    1849                 :             :     }
    1850                 :          72 :     fputc('"', fout);
    1851                 :          72 : }
    1852                 :             : 
    1853                 :             : static void
    1854                 :         416 : csv_print_field(const char *str, FILE *fout, char sep)
    1855                 :             : {
    1856                 :             :     /*----------------
    1857                 :             :      * Enclose and escape field contents when one of these conditions is met:
    1858                 :             :      * - the field separator is found in the contents.
    1859                 :             :      * - the field contains a CR or LF.
    1860                 :             :      * - the field contains a double quote.
    1861                 :             :      * - the field is exactly "\.".
    1862                 :             :      * - the field separator is either "\" or ".".
    1863                 :             :      * The last two cases prevent producing a line that the server's COPY
    1864                 :             :      * command would interpret as an end-of-data marker.  We only really
    1865                 :             :      * need to ensure that the complete line isn't exactly "\.", but for
    1866                 :             :      * simplicity we apply stronger restrictions here.
    1867                 :             :      *----------------
    1868                 :             :      */
    1869         [ +  + ]:         416 :     if (strchr(str, sep) != NULL ||
    1870         [ +  + ]:         408 :         strcspn(str, "\r\n\"") != strlen(str) ||
    1871   [ +  +  +  - ]:         364 :         strcmp(str, "\\.") == 0 ||
    1872         [ +  + ]:         360 :         sep == '\\' || sep == '.')
    1873                 :          72 :         csv_escaped_print(str, fout);
    1874                 :             :     else
    1875                 :         344 :         fputs(str, fout);
    1876                 :         416 : }
    1877                 :             : 
    1878                 :             : static void
    1879                 :          32 : print_csv_text(const printTableContent *cont, FILE *fout)
    1880                 :             : {
    1881                 :             :     const char *const *ptr;
    1882                 :             :     int         i;
    1883                 :             : 
    1884         [ -  + ]:          32 :     if (CancelRequested)
    1885                 :           0 :         return;
    1886                 :             : 
    1887                 :             :     /*
    1888                 :             :      * The title and footer are never printed in csv format. The header is
    1889                 :             :      * printed if opt_tuples_only is false.
    1890                 :             :      *
    1891                 :             :      * Despite RFC 4180 saying that end of lines are CRLF, terminate lines
    1892                 :             :      * with '\n', which prints out as the system-dependent EOL string in text
    1893                 :             :      * mode (typically LF on Unix and CRLF on Windows).
    1894                 :             :      */
    1895   [ +  -  +  + ]:          32 :     if (cont->opt->start_table && !cont->opt->tuples_only)
    1896                 :             :     {
    1897                 :             :         /* print headers */
    1898         [ +  + ]:         108 :         for (ptr = cont->headers; *ptr; ptr++)
    1899                 :             :         {
    1900         [ +  + ]:          80 :             if (ptr != cont->headers)
    1901                 :          52 :                 fputc(cont->opt->csvFieldSep[0], fout);
    1902                 :          80 :             csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
    1903                 :             :         }
    1904                 :          28 :         fputc('\n', fout);
    1905                 :             :     }
    1906                 :             : 
    1907                 :             :     /* print cells */
    1908         [ +  + ]:         168 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    1909                 :             :     {
    1910                 :         136 :         csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
    1911         [ +  + ]:         136 :         if ((i + 1) % cont->ncolumns)
    1912                 :          96 :             fputc(cont->opt->csvFieldSep[0], fout);
    1913                 :             :         else
    1914                 :          40 :             fputc('\n', fout);
    1915                 :             :     }
    1916                 :             : }
    1917                 :             : 
    1918                 :             : static void
    1919                 :          12 : print_csv_vertical(const printTableContent *cont, FILE *fout)
    1920                 :             : {
    1921                 :             :     const char *const *ptr;
    1922                 :             :     int         i;
    1923                 :             : 
    1924                 :             :     /* print records */
    1925         [ +  + ]:         112 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    1926                 :             :     {
    1927         [ -  + ]:         100 :         if (CancelRequested)
    1928                 :           0 :             return;
    1929                 :             : 
    1930                 :             :         /* print name of column */
    1931                 :         100 :         csv_print_field(cont->headers[i % cont->ncolumns], fout,
    1932                 :         100 :                         cont->opt->csvFieldSep[0]);
    1933                 :             : 
    1934                 :             :         /* print field separator */
    1935                 :         100 :         fputc(cont->opt->csvFieldSep[0], fout);
    1936                 :             : 
    1937                 :             :         /* print field value */
    1938                 :         100 :         csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
    1939                 :             : 
    1940                 :         100 :         fputc('\n', fout);
    1941                 :             :     }
    1942                 :             : }
    1943                 :             : 
    1944                 :             : 
    1945                 :             : /**********************/
    1946                 :             : /* HTML               */
    1947                 :             : /**********************/
    1948                 :             : 
    1949                 :             : 
    1950                 :             : void
    1951                 :         548 : html_escaped_print(const char *in, FILE *fout)
    1952                 :             : {
    1953                 :             :     const char *p;
    1954                 :         548 :     bool        leading_space = true;
    1955                 :             : 
    1956         [ +  + ]:        4600 :     for (p = in; *p; p++)
    1957                 :             :     {
    1958   [ +  +  +  +  :        4052 :         switch (*p)
                +  +  + ]
    1959                 :             :         {
    1960                 :          36 :             case '&':
    1961                 :          36 :                 fputs("&amp;", fout);
    1962                 :          36 :                 break;
    1963                 :          96 :             case '<':
    1964                 :          96 :                 fputs("&lt;", fout);
    1965                 :          96 :                 break;
    1966                 :          96 :             case '>':
    1967                 :          96 :                 fputs("&gt;", fout);
    1968                 :          96 :                 break;
    1969                 :          48 :             case '\n':
    1970                 :          48 :                 fputs("<br />\n", fout);
    1971                 :          48 :                 break;
    1972                 :          64 :             case '"':
    1973                 :          64 :                 fputs("&quot;", fout);
    1974                 :          64 :                 break;
    1975                 :         180 :             case ' ':
    1976                 :             :                 /* protect leading space, for EXPLAIN output */
    1977         [ +  + ]:         180 :                 if (leading_space)
    1978                 :          96 :                     fputs("&nbsp;", fout);
    1979                 :             :                 else
    1980                 :          84 :                     fputs(" ", fout);
    1981                 :         180 :                 break;
    1982                 :        3532 :             default:
    1983                 :        3532 :                 fputc(*p, fout);
    1984                 :             :         }
    1985         [ +  + ]:        4052 :         if (*p != ' ')
    1986                 :        3872 :             leading_space = false;
    1987                 :             :     }
    1988                 :         548 : }
    1989                 :             : 
    1990                 :             : 
    1991                 :             : static void
    1992                 :          20 : print_html_text(const printTableContent *cont, FILE *fout)
    1993                 :             : {
    1994                 :          20 :     bool        opt_tuples_only = cont->opt->tuples_only;
    1995                 :          20 :     unsigned short opt_border = cont->opt->border;
    1996                 :          20 :     const char *opt_table_attr = cont->opt->tableAttr;
    1997                 :             :     unsigned int i;
    1998                 :             :     const char *const *ptr;
    1999                 :             : 
    2000         [ -  + ]:          20 :     if (CancelRequested)
    2001                 :           0 :         return;
    2002                 :             : 
    2003         [ +  - ]:          20 :     if (cont->opt->start_table)
    2004                 :             :     {
    2005                 :          20 :         fprintf(fout, "<table border=\"%d\"", opt_border);
    2006         [ +  + ]:          20 :         if (opt_table_attr)
    2007                 :           4 :             fprintf(fout, " %s", opt_table_attr);
    2008                 :          20 :         fputs(">\n", fout);
    2009                 :             : 
    2010                 :             :         /* print title */
    2011   [ +  +  +  + ]:          20 :         if (!opt_tuples_only && cont->title)
    2012                 :             :         {
    2013                 :           4 :             fputs("  <caption>", fout);
    2014                 :           4 :             html_escaped_print(cont->title, fout);
    2015                 :           4 :             fputs("</caption>\n", fout);
    2016                 :             :         }
    2017                 :             : 
    2018                 :             :         /* print headers */
    2019         [ +  + ]:          20 :         if (!opt_tuples_only)
    2020                 :             :         {
    2021                 :          16 :             fputs("  <tr>\n", fout);
    2022         [ +  + ]:          92 :             for (ptr = cont->headers; *ptr; ptr++)
    2023                 :             :             {
    2024                 :          76 :                 fputs("    <th align=\"center\">", fout);
    2025                 :          76 :                 html_escaped_print(*ptr, fout);
    2026                 :          76 :                 fputs("</th>\n", fout);
    2027                 :             :             }
    2028                 :          16 :             fputs("  </tr>\n", fout);
    2029                 :             :         }
    2030                 :             :     }
    2031                 :             : 
    2032                 :             :     /* print cells */
    2033         [ +  + ]:         184 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2034                 :             :     {
    2035         [ +  + ]:         164 :         if (i % cont->ncolumns == 0)
    2036                 :             :         {
    2037         [ -  + ]:          36 :             if (CancelRequested)
    2038                 :           0 :                 break;
    2039                 :          36 :             fputs("  <tr valign=\"top\">\n", fout);
    2040                 :             :         }
    2041                 :             : 
    2042         [ +  + ]:         164 :         fprintf(fout, "    <td align=\"%s\">", cont->aligns[(i) % cont->ncolumns] == 'r' ? "right" : "left");
    2043                 :             :         /* is string only whitespace? */
    2044         [ +  + ]:         164 :         if ((*ptr)[strspn(*ptr, " \t")] == '\0')
    2045                 :          24 :             fputs("&nbsp; ", fout);
    2046                 :             :         else
    2047                 :         140 :             html_escaped_print(*ptr, fout);
    2048                 :             : 
    2049                 :         164 :         fputs("</td>\n", fout);
    2050                 :             : 
    2051         [ +  + ]:         164 :         if ((i + 1) % cont->ncolumns == 0)
    2052                 :          36 :             fputs("  </tr>\n", fout);
    2053                 :             :     }
    2054                 :             : 
    2055         [ +  - ]:          20 :     if (cont->opt->stop_table)
    2056                 :             :     {
    2057                 :          20 :         printTableFooter *footers = footers_with_default(cont);
    2058                 :             : 
    2059                 :          20 :         fputs("</table>\n", fout);
    2060                 :             : 
    2061                 :             :         /* print footers */
    2062   [ +  +  +  -  :          20 :         if (!opt_tuples_only && footers != NULL && !CancelRequested)
                   +  - ]
    2063                 :             :         {
    2064                 :             :             printTableFooter *f;
    2065                 :             : 
    2066                 :          16 :             fputs("<p>", fout);
    2067         [ +  + ]:          32 :             for (f = footers; f; f = f->next)
    2068                 :             :             {
    2069                 :          16 :                 html_escaped_print(f->data, fout);
    2070                 :          16 :                 fputs("<br />\n", fout);
    2071                 :             :             }
    2072                 :          16 :             fputs("</p>", fout);
    2073                 :             :         }
    2074                 :             : 
    2075                 :          20 :         fputc('\n', fout);
    2076                 :             :     }
    2077                 :             : }
    2078                 :             : 
    2079                 :             : 
    2080                 :             : static void
    2081                 :          20 : print_html_vertical(const printTableContent *cont, FILE *fout)
    2082                 :             : {
    2083                 :          20 :     bool        opt_tuples_only = cont->opt->tuples_only;
    2084                 :          20 :     unsigned short opt_border = cont->opt->border;
    2085                 :          20 :     const char *opt_table_attr = cont->opt->tableAttr;
    2086                 :          20 :     unsigned long record = cont->opt->prior_records + 1;
    2087                 :             :     unsigned int i;
    2088                 :             :     const char *const *ptr;
    2089                 :             : 
    2090         [ -  + ]:          20 :     if (CancelRequested)
    2091                 :           0 :         return;
    2092                 :             : 
    2093         [ +  - ]:          20 :     if (cont->opt->start_table)
    2094                 :             :     {
    2095                 :          20 :         fprintf(fout, "<table border=\"%d\"", opt_border);
    2096         [ +  + ]:          20 :         if (opt_table_attr)
    2097                 :           4 :             fprintf(fout, " %s", opt_table_attr);
    2098                 :          20 :         fputs(">\n", fout);
    2099                 :             : 
    2100                 :             :         /* print title */
    2101   [ +  +  +  + ]:          20 :         if (!opt_tuples_only && cont->title)
    2102                 :             :         {
    2103                 :           4 :             fputs("  <caption>", fout);
    2104                 :           4 :             html_escaped_print(cont->title, fout);
    2105                 :           4 :             fputs("</caption>\n", fout);
    2106                 :             :         }
    2107                 :             :     }
    2108                 :             : 
    2109                 :             :     /* print records */
    2110         [ +  + ]:         184 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2111                 :             :     {
    2112         [ +  + ]:         164 :         if (i % cont->ncolumns == 0)
    2113                 :             :         {
    2114         [ -  + ]:          36 :             if (CancelRequested)
    2115                 :           0 :                 break;
    2116         [ +  + ]:          36 :             if (!opt_tuples_only)
    2117                 :          28 :                 fprintf(fout,
    2118                 :             :                         "\n  <tr><td colspan=\"2\" align=\"center\">Record %lu</td></tr>\n",
    2119                 :             :                         record++);
    2120                 :             :             else
    2121                 :           8 :                 fputs("\n  <tr><td colspan=\"2\">&nbsp;</td></tr>\n", fout);
    2122                 :             :         }
    2123                 :         164 :         fputs("  <tr valign=\"top\">\n"
    2124                 :             :               "    <th>", fout);
    2125                 :         164 :         html_escaped_print(cont->headers[i % cont->ncolumns], fout);
    2126                 :         164 :         fputs("</th>\n", fout);
    2127                 :             : 
    2128         [ +  + ]:         164 :         fprintf(fout, "    <td align=\"%s\">", cont->aligns[i % cont->ncolumns] == 'r' ? "right" : "left");
    2129                 :             :         /* is string only whitespace? */
    2130         [ +  + ]:         164 :         if ((*ptr)[strspn(*ptr, " \t")] == '\0')
    2131                 :          24 :             fputs("&nbsp; ", fout);
    2132                 :             :         else
    2133                 :         140 :             html_escaped_print(*ptr, fout);
    2134                 :             : 
    2135                 :         164 :         fputs("</td>\n  </tr>\n", fout);
    2136                 :             :     }
    2137                 :             : 
    2138         [ +  - ]:          20 :     if (cont->opt->stop_table)
    2139                 :             :     {
    2140                 :          20 :         fputs("</table>\n", fout);
    2141                 :             : 
    2142                 :             :         /* print footers */
    2143   [ +  +  +  +  :          20 :         if (!opt_tuples_only && cont->footers != NULL && !CancelRequested)
                   +  - ]
    2144                 :             :         {
    2145                 :             :             printTableFooter *f;
    2146                 :             : 
    2147                 :           4 :             fputs("<p>", fout);
    2148         [ +  + ]:           8 :             for (f = cont->footers; f; f = f->next)
    2149                 :             :             {
    2150                 :           4 :                 html_escaped_print(f->data, fout);
    2151                 :           4 :                 fputs("<br />\n", fout);
    2152                 :             :             }
    2153                 :           4 :             fputs("</p>", fout);
    2154                 :             :         }
    2155                 :             : 
    2156                 :          20 :         fputc('\n', fout);
    2157                 :             :     }
    2158                 :             : }
    2159                 :             : 
    2160                 :             : 
    2161                 :             : /*************************/
    2162                 :             : /* ASCIIDOC              */
    2163                 :             : /*************************/
    2164                 :             : 
    2165                 :             : 
    2166                 :             : static void
    2167                 :         436 : asciidoc_escaped_print(const char *in, FILE *fout)
    2168                 :             : {
    2169                 :             :     const char *p;
    2170                 :             : 
    2171         [ +  + ]:        3060 :     for (p = in; *p; p++)
    2172                 :             :     {
    2173         [ +  + ]:        2624 :         switch (*p)
    2174                 :             :         {
    2175                 :          84 :             case '|':
    2176                 :          84 :                 fputs("\\|", fout);
    2177                 :          84 :                 break;
    2178                 :        2540 :             default:
    2179                 :        2540 :                 fputc(*p, fout);
    2180                 :             :         }
    2181                 :             :     }
    2182                 :         436 : }
    2183                 :             : 
    2184                 :             : static void
    2185                 :          20 : print_asciidoc_text(const printTableContent *cont, FILE *fout)
    2186                 :             : {
    2187                 :          20 :     bool        opt_tuples_only = cont->opt->tuples_only;
    2188                 :          20 :     unsigned short opt_border = cont->opt->border;
    2189                 :             :     unsigned int i;
    2190                 :             :     const char *const *ptr;
    2191                 :             : 
    2192         [ -  + ]:          20 :     if (CancelRequested)
    2193                 :           0 :         return;
    2194                 :             : 
    2195         [ +  - ]:          20 :     if (cont->opt->start_table)
    2196                 :             :     {
    2197                 :             :         /* print table in new paragraph - enforce preliminary new line */
    2198                 :          20 :         fputs("\n", fout);
    2199                 :             : 
    2200                 :             :         /* print title */
    2201   [ +  +  +  + ]:          20 :         if (!opt_tuples_only && cont->title)
    2202                 :             :         {
    2203                 :           4 :             fputs(".", fout);
    2204                 :           4 :             fputs(cont->title, fout);
    2205                 :           4 :             fputs("\n", fout);
    2206                 :             :         }
    2207                 :             : 
    2208                 :             :         /* print table [] header definition */
    2209         [ +  + ]:          20 :         fprintf(fout, "[%scols=\"", !opt_tuples_only ? "options=\"header\"," : "");
    2210         [ +  + ]:         104 :         for (i = 0; i < cont->ncolumns; i++)
    2211                 :             :         {
    2212         [ +  + ]:          84 :             if (i != 0)
    2213                 :          64 :                 fputs(",", fout);
    2214         [ +  + ]:          84 :             fprintf(fout, "%s", cont->aligns[(i) % cont->ncolumns] == 'r' ? ">l" : "<l");
    2215                 :             :         }
    2216                 :          20 :         fputs("\"", fout);
    2217   [ +  +  +  - ]:          20 :         switch (opt_border)
    2218                 :             :         {
    2219                 :           4 :             case 0:
    2220                 :           4 :                 fputs(",frame=\"none\",grid=\"none\"", fout);
    2221                 :           4 :                 break;
    2222                 :          12 :             case 1:
    2223                 :          12 :                 fputs(",frame=\"none\"", fout);
    2224                 :          12 :                 break;
    2225                 :           4 :             case 2:
    2226                 :           4 :                 fputs(",frame=\"all\",grid=\"all\"", fout);
    2227                 :           4 :                 break;
    2228                 :             :         }
    2229                 :          20 :         fputs("]\n", fout);
    2230                 :          20 :         fputs("|====\n", fout);
    2231                 :             : 
    2232                 :             :         /* print headers */
    2233         [ +  + ]:          20 :         if (!opt_tuples_only)
    2234                 :             :         {
    2235         [ +  + ]:          80 :             for (ptr = cont->headers; *ptr; ptr++)
    2236                 :             :             {
    2237         [ +  + ]:          64 :                 if (ptr != cont->headers)
    2238                 :          48 :                     fputs(" ", fout);
    2239                 :          64 :                 fputs("^l|", fout);
    2240                 :          64 :                 asciidoc_escaped_print(*ptr, fout);
    2241                 :             :             }
    2242                 :          16 :             fputs("\n", fout);
    2243                 :             :         }
    2244                 :             :     }
    2245                 :             : 
    2246                 :             :     /* print cells */
    2247         [ +  + ]:         160 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2248                 :             :     {
    2249         [ +  + ]:         140 :         if (i % cont->ncolumns == 0)
    2250                 :             :         {
    2251         [ -  + ]:          36 :             if (CancelRequested)
    2252                 :           0 :                 break;
    2253                 :             :         }
    2254                 :             : 
    2255         [ +  + ]:         140 :         if (i % cont->ncolumns != 0)
    2256                 :         104 :             fputs(" ", fout);
    2257                 :         140 :         fputs("|", fout);
    2258                 :             : 
    2259                 :             :         /* protect against needless spaces */
    2260         [ +  + ]:         140 :         if ((*ptr)[strspn(*ptr, " \t")] == '\0')
    2261                 :             :         {
    2262         [ +  - ]:          24 :             if ((i + 1) % cont->ncolumns != 0)
    2263                 :          24 :                 fputs(" ", fout);
    2264                 :             :         }
    2265                 :             :         else
    2266                 :         116 :             asciidoc_escaped_print(*ptr, fout);
    2267                 :             : 
    2268         [ +  + ]:         140 :         if ((i + 1) % cont->ncolumns == 0)
    2269                 :          36 :             fputs("\n", fout);
    2270                 :             :     }
    2271                 :             : 
    2272                 :          20 :     fputs("|====\n", fout);
    2273                 :             : 
    2274         [ +  - ]:          20 :     if (cont->opt->stop_table)
    2275                 :             :     {
    2276                 :          20 :         printTableFooter *footers = footers_with_default(cont);
    2277                 :             : 
    2278                 :             :         /* print footers */
    2279   [ +  +  +  -  :          20 :         if (!opt_tuples_only && footers != NULL && !CancelRequested)
                   +  - ]
    2280                 :             :         {
    2281                 :             :             printTableFooter *f;
    2282                 :             : 
    2283                 :          16 :             fputs("\n....\n", fout);
    2284         [ +  + ]:          32 :             for (f = footers; f; f = f->next)
    2285                 :             :             {
    2286                 :          16 :                 fputs(f->data, fout);
    2287                 :          16 :                 fputs("\n", fout);
    2288                 :             :             }
    2289                 :          16 :             fputs("....\n", fout);
    2290                 :             :         }
    2291                 :             :     }
    2292                 :             : }
    2293                 :             : 
    2294                 :             : static void
    2295                 :          20 : print_asciidoc_vertical(const printTableContent *cont, FILE *fout)
    2296                 :             : {
    2297                 :          20 :     bool        opt_tuples_only = cont->opt->tuples_only;
    2298                 :          20 :     unsigned short opt_border = cont->opt->border;
    2299                 :          20 :     unsigned long record = cont->opt->prior_records + 1;
    2300                 :             :     unsigned int i;
    2301                 :             :     const char *const *ptr;
    2302                 :             : 
    2303         [ -  + ]:          20 :     if (CancelRequested)
    2304                 :           0 :         return;
    2305                 :             : 
    2306         [ +  - ]:          20 :     if (cont->opt->start_table)
    2307                 :             :     {
    2308                 :             :         /* print table in new paragraph - enforce preliminary new line */
    2309                 :          20 :         fputs("\n", fout);
    2310                 :             : 
    2311                 :             :         /* print title */
    2312   [ +  +  +  + ]:          20 :         if (!opt_tuples_only && cont->title)
    2313                 :             :         {
    2314                 :           4 :             fputs(".", fout);
    2315                 :           4 :             fputs(cont->title, fout);
    2316                 :           4 :             fputs("\n", fout);
    2317                 :             :         }
    2318                 :             : 
    2319                 :             :         /* print table [] header definition */
    2320                 :          20 :         fputs("[cols=\"h,l\"", fout);
    2321   [ +  +  +  - ]:          20 :         switch (opt_border)
    2322                 :             :         {
    2323                 :           4 :             case 0:
    2324                 :           4 :                 fputs(",frame=\"none\",grid=\"none\"", fout);
    2325                 :           4 :                 break;
    2326                 :          12 :             case 1:
    2327                 :          12 :                 fputs(",frame=\"none\"", fout);
    2328                 :          12 :                 break;
    2329                 :           4 :             case 2:
    2330                 :           4 :                 fputs(",frame=\"all\",grid=\"all\"", fout);
    2331                 :           4 :                 break;
    2332                 :             :         }
    2333                 :          20 :         fputs("]\n", fout);
    2334                 :          20 :         fputs("|====\n", fout);
    2335                 :             :     }
    2336                 :             : 
    2337                 :             :     /* print records */
    2338         [ +  + ]:         160 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2339                 :             :     {
    2340         [ +  + ]:         140 :         if (i % cont->ncolumns == 0)
    2341                 :             :         {
    2342         [ -  + ]:          36 :             if (CancelRequested)
    2343                 :           0 :                 break;
    2344         [ +  + ]:          36 :             if (!opt_tuples_only)
    2345                 :          28 :                 fprintf(fout,
    2346                 :             :                         "2+^|Record %lu\n",
    2347                 :             :                         record++);
    2348                 :             :             else
    2349                 :           8 :                 fputs("2+|\n", fout);
    2350                 :             :         }
    2351                 :             : 
    2352                 :         140 :         fputs("<l|", fout);
    2353                 :         140 :         asciidoc_escaped_print(cont->headers[i % cont->ncolumns], fout);
    2354                 :             : 
    2355         [ +  + ]:         140 :         fprintf(fout, " %s|", cont->aligns[i % cont->ncolumns] == 'r' ? ">l" : "<l");
    2356                 :             :         /* is string only whitespace? */
    2357         [ +  + ]:         140 :         if ((*ptr)[strspn(*ptr, " \t")] == '\0')
    2358                 :          24 :             fputs(" ", fout);
    2359                 :             :         else
    2360                 :         116 :             asciidoc_escaped_print(*ptr, fout);
    2361                 :         140 :         fputs("\n", fout);
    2362                 :             :     }
    2363                 :             : 
    2364                 :          20 :     fputs("|====\n", fout);
    2365                 :             : 
    2366         [ +  - ]:          20 :     if (cont->opt->stop_table)
    2367                 :             :     {
    2368                 :             :         /* print footers */
    2369   [ +  +  +  +  :          20 :         if (!opt_tuples_only && cont->footers != NULL && !CancelRequested)
                   +  - ]
    2370                 :             :         {
    2371                 :             :             printTableFooter *f;
    2372                 :             : 
    2373                 :           4 :             fputs("\n....\n", fout);
    2374         [ +  + ]:           8 :             for (f = cont->footers; f; f = f->next)
    2375                 :             :             {
    2376                 :           4 :                 fputs(f->data, fout);
    2377                 :           4 :                 fputs("\n", fout);
    2378                 :             :             }
    2379                 :           4 :             fputs("....\n", fout);
    2380                 :             :         }
    2381                 :             :     }
    2382                 :             : }
    2383                 :             : 
    2384                 :             : 
    2385                 :             : /*************************/
    2386                 :             : /* LaTeX                 */
    2387                 :             : /*************************/
    2388                 :             : 
    2389                 :             : 
    2390                 :             : static void
    2391                 :        1636 : latex_escaped_print(const char *in, FILE *fout)
    2392                 :             : {
    2393                 :             :     const char *p;
    2394                 :             : 
    2395         [ +  + ]:       14376 :     for (p = in; *p; p++)
    2396   [ +  +  +  +  :       12740 :         switch (*p)
          +  +  +  +  +  
          +  +  +  +  +  
                      + ]
    2397                 :             :         {
    2398                 :             :                 /*
    2399                 :             :                  * We convert ASCII characters per the recommendations in
    2400                 :             :                  * Scott Pakin's "The Comprehensive LATEX Symbol List",
    2401                 :             :                  * available from CTAN.  For non-ASCII, you're on your own.
    2402                 :             :                  */
    2403                 :         144 :             case '#':
    2404                 :         144 :                 fputs("\\#", fout);
    2405                 :         144 :                 break;
    2406                 :         128 :             case '$':
    2407                 :         128 :                 fputs("\\$", fout);
    2408                 :         128 :                 break;
    2409                 :         144 :             case '%':
    2410                 :         144 :                 fputs("\\%", fout);
    2411                 :         144 :                 break;
    2412                 :         144 :             case '&':
    2413                 :         144 :                 fputs("\\&", fout);
    2414                 :         144 :                 break;
    2415                 :         144 :             case '<':
    2416                 :         144 :                 fputs("\\textless{}", fout);
    2417                 :         144 :                 break;
    2418                 :         144 :             case '>':
    2419                 :         144 :                 fputs("\\textgreater{}", fout);
    2420                 :         144 :                 break;
    2421                 :         144 :             case '\\':
    2422                 :         144 :                 fputs("\\textbackslash{}", fout);
    2423                 :         144 :                 break;
    2424                 :         144 :             case '^':
    2425                 :         144 :                 fputs("\\^{}", fout);
    2426                 :         144 :                 break;
    2427                 :         312 :             case '_':
    2428                 :         312 :                 fputs("\\_", fout);
    2429                 :         312 :                 break;
    2430                 :         144 :             case '{':
    2431                 :         144 :                 fputs("\\{", fout);
    2432                 :         144 :                 break;
    2433                 :         144 :             case '|':
    2434                 :         144 :                 fputs("\\textbar{}", fout);
    2435                 :         144 :                 break;
    2436                 :         144 :             case '}':
    2437                 :         144 :                 fputs("\\}", fout);
    2438                 :         144 :                 break;
    2439                 :         144 :             case '~':
    2440                 :         144 :                 fputs("\\~{}", fout);
    2441                 :         144 :                 break;
    2442                 :         144 :             case '\n':
    2443                 :             :                 /* This is not right, but doing it right seems too hard */
    2444                 :         144 :                 fputs("\\\\", fout);
    2445                 :         144 :                 break;
    2446                 :       10572 :             default:
    2447                 :       10572 :                 fputc(*p, fout);
    2448                 :             :         }
    2449                 :        1636 : }
    2450                 :             : 
    2451                 :             : 
    2452                 :             : static void
    2453                 :          24 : print_latex_text(const printTableContent *cont, FILE *fout)
    2454                 :             : {
    2455                 :          24 :     bool        opt_tuples_only = cont->opt->tuples_only;
    2456                 :          24 :     unsigned short opt_border = cont->opt->border;
    2457                 :             :     unsigned int i;
    2458                 :             :     const char *const *ptr;
    2459                 :             : 
    2460         [ -  + ]:          24 :     if (CancelRequested)
    2461                 :           0 :         return;
    2462                 :             : 
    2463         [ -  + ]:          24 :     if (opt_border > 3)
    2464                 :           0 :         opt_border = 3;
    2465                 :             : 
    2466         [ +  - ]:          24 :     if (cont->opt->start_table)
    2467                 :             :     {
    2468                 :             :         /* print title */
    2469   [ +  +  +  + ]:          24 :         if (!opt_tuples_only && cont->title)
    2470                 :             :         {
    2471                 :           4 :             fputs("\\begin{center}\n", fout);
    2472                 :           4 :             latex_escaped_print(cont->title, fout);
    2473                 :           4 :             fputs("\n\\end{center}\n\n", fout);
    2474                 :             :         }
    2475                 :             : 
    2476                 :             :         /* begin environment and set alignments and borders */
    2477                 :          24 :         fputs("\\begin{tabular}{", fout);
    2478                 :             : 
    2479         [ +  + ]:          24 :         if (opt_border >= 2)
    2480                 :           8 :             fputs("| ", fout);
    2481         [ +  + ]:         136 :         for (i = 0; i < cont->ncolumns; i++)
    2482                 :             :         {
    2483                 :         112 :             fputc(*(cont->aligns + i), fout);
    2484   [ +  +  +  + ]:         112 :             if (opt_border != 0 && i < cont->ncolumns - 1)
    2485                 :          76 :                 fputs(" | ", fout);
    2486                 :             :         }
    2487         [ +  + ]:          24 :         if (opt_border >= 2)
    2488                 :           8 :             fputs(" |", fout);
    2489                 :             : 
    2490                 :          24 :         fputs("}\n", fout);
    2491                 :             : 
    2492   [ +  +  +  + ]:          24 :         if (!opt_tuples_only && opt_border >= 2)
    2493                 :           8 :             fputs("\\hline\n", fout);
    2494                 :             : 
    2495                 :             :         /* print headers */
    2496         [ +  + ]:          24 :         if (!opt_tuples_only)
    2497                 :             :         {
    2498         [ +  + ]:         112 :             for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
    2499                 :             :             {
    2500         [ +  + ]:          92 :                 if (i != 0)
    2501                 :          72 :                     fputs(" & ", fout);
    2502                 :          92 :                 fputs("\\textit{", fout);
    2503                 :          92 :                 latex_escaped_print(*ptr, fout);
    2504                 :          92 :                 fputc('}', fout);
    2505                 :             :             }
    2506                 :          20 :             fputs(" \\\\\n", fout);
    2507                 :          20 :             fputs("\\hline\n", fout);
    2508                 :             :         }
    2509                 :             :     }
    2510                 :             : 
    2511                 :             :     /* print cells */
    2512         [ +  + ]:         220 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2513                 :             :     {
    2514                 :         196 :         latex_escaped_print(*ptr, fout);
    2515                 :             : 
    2516         [ +  + ]:         196 :         if ((i + 1) % cont->ncolumns == 0)
    2517                 :             :         {
    2518                 :          44 :             fputs(" \\\\\n", fout);
    2519         [ +  + ]:          44 :             if (opt_border == 3)
    2520                 :           8 :                 fputs("\\hline\n", fout);
    2521         [ -  + ]:          44 :             if (CancelRequested)
    2522                 :           0 :                 break;
    2523                 :             :         }
    2524                 :             :         else
    2525                 :         152 :             fputs(" & ", fout);
    2526                 :             :     }
    2527                 :             : 
    2528         [ +  - ]:          24 :     if (cont->opt->stop_table)
    2529                 :             :     {
    2530                 :          24 :         printTableFooter *footers = footers_with_default(cont);
    2531                 :             : 
    2532         [ +  + ]:          24 :         if (opt_border == 2)
    2533                 :           4 :             fputs("\\hline\n", fout);
    2534                 :             : 
    2535                 :          24 :         fputs("\\end{tabular}\n\n\\noindent ", fout);
    2536                 :             : 
    2537                 :             :         /* print footers */
    2538   [ +  -  +  +  :          24 :         if (footers && !opt_tuples_only && !CancelRequested)
                   +  - ]
    2539                 :             :         {
    2540                 :             :             printTableFooter *f;
    2541                 :             : 
    2542         [ +  + ]:          40 :             for (f = footers; f; f = f->next)
    2543                 :             :             {
    2544                 :          20 :                 latex_escaped_print(f->data, fout);
    2545                 :          20 :                 fputs(" \\\\\n", fout);
    2546                 :             :             }
    2547                 :             :         }
    2548                 :             : 
    2549                 :          24 :         fputc('\n', fout);
    2550                 :             :     }
    2551                 :             : }
    2552                 :             : 
    2553                 :             : 
    2554                 :             : /*************************/
    2555                 :             : /* LaTeX longtable       */
    2556                 :             : /*************************/
    2557                 :             : 
    2558                 :             : 
    2559                 :             : static void
    2560                 :          28 : print_latex_longtable_text(const printTableContent *cont, FILE *fout)
    2561                 :             : {
    2562                 :          28 :     bool        opt_tuples_only = cont->opt->tuples_only;
    2563                 :          28 :     unsigned short opt_border = cont->opt->border;
    2564                 :             :     unsigned int i;
    2565                 :          28 :     const char *opt_table_attr = cont->opt->tableAttr;
    2566                 :          28 :     const char *next_opt_table_attr_char = opt_table_attr;
    2567                 :          28 :     const char *last_opt_table_attr_char = NULL;
    2568                 :             :     const char *const *ptr;
    2569                 :             : 
    2570         [ -  + ]:          28 :     if (CancelRequested)
    2571                 :           0 :         return;
    2572                 :             : 
    2573         [ -  + ]:          28 :     if (opt_border > 3)
    2574                 :           0 :         opt_border = 3;
    2575                 :             : 
    2576         [ +  - ]:          28 :     if (cont->opt->start_table)
    2577                 :             :     {
    2578                 :             :         /* begin environment and set alignments and borders */
    2579                 :          28 :         fputs("\\begin{longtable}{", fout);
    2580                 :             : 
    2581         [ +  + ]:          28 :         if (opt_border >= 2)
    2582                 :          12 :             fputs("| ", fout);
    2583                 :             : 
    2584         [ +  + ]:         156 :         for (i = 0; i < cont->ncolumns; i++)
    2585                 :             :         {
    2586                 :             :             /* longtable supports either a width (p) or an alignment (l/r) */
    2587                 :             :             /* Are we left-justified and was a proportional width specified? */
    2588   [ +  +  +  + ]:         128 :             if (*(cont->aligns + i) == 'l' && opt_table_attr)
    2589                 :             :             {
    2590                 :             : #define LONGTABLE_WHITESPACE    " \t\n"
    2591                 :             : 
    2592                 :             :                 /* advance over whitespace */
    2593                 :          12 :                 next_opt_table_attr_char += strspn(next_opt_table_attr_char,
    2594                 :             :                                                    LONGTABLE_WHITESPACE);
    2595                 :             :                 /* We have a value? */
    2596         [ +  + ]:          12 :                 if (next_opt_table_attr_char[0] != '\0')
    2597                 :             :                 {
    2598                 :           4 :                     fputs("p{", fout);
    2599                 :           4 :                     fwrite(next_opt_table_attr_char, strcspn(next_opt_table_attr_char,
    2600                 :             :                                                              LONGTABLE_WHITESPACE), 1, fout);
    2601                 :           4 :                     last_opt_table_attr_char = next_opt_table_attr_char;
    2602                 :           4 :                     next_opt_table_attr_char += strcspn(next_opt_table_attr_char,
    2603                 :             :                                                         LONGTABLE_WHITESPACE);
    2604                 :           4 :                     fputs("\\textwidth}", fout);
    2605                 :             :                 }
    2606                 :             :                 /* use previous value */
    2607         [ +  - ]:           8 :                 else if (last_opt_table_attr_char != NULL)
    2608                 :             :                 {
    2609                 :           8 :                     fputs("p{", fout);
    2610                 :           8 :                     fwrite(last_opt_table_attr_char, strcspn(last_opt_table_attr_char,
    2611                 :             :                                                              LONGTABLE_WHITESPACE), 1, fout);
    2612                 :           8 :                     fputs("\\textwidth}", fout);
    2613                 :             :                 }
    2614                 :             :                 else
    2615                 :           0 :                     fputc('l', fout);
    2616                 :             :             }
    2617                 :             :             else
    2618                 :         116 :                 fputc(*(cont->aligns + i), fout);
    2619                 :             : 
    2620   [ +  +  +  + ]:         128 :             if (opt_border != 0 && i < cont->ncolumns - 1)
    2621                 :          88 :                 fputs(" | ", fout);
    2622                 :             :         }
    2623                 :             : 
    2624         [ +  + ]:          28 :         if (opt_border >= 2)
    2625                 :          12 :             fputs(" |", fout);
    2626                 :             : 
    2627                 :          28 :         fputs("}\n", fout);
    2628                 :             : 
    2629                 :             :         /* print headers */
    2630         [ +  + ]:          28 :         if (!opt_tuples_only)
    2631                 :             :         {
    2632                 :             :             /* firsthead */
    2633         [ +  + ]:          24 :             if (opt_border >= 2)
    2634                 :          12 :                 fputs("\\toprule\n", fout);
    2635         [ +  + ]:         132 :             for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
    2636                 :             :             {
    2637         [ +  + ]:         108 :                 if (i != 0)
    2638                 :          84 :                     fputs(" & ", fout);
    2639                 :         108 :                 fputs("\\small\\textbf{\\textit{", fout);
    2640                 :         108 :                 latex_escaped_print(*ptr, fout);
    2641                 :         108 :                 fputs("}}", fout);
    2642                 :             :             }
    2643                 :          24 :             fputs(" \\\\\n", fout);
    2644                 :          24 :             fputs("\\midrule\n\\endfirsthead\n", fout);
    2645                 :             : 
    2646                 :             :             /* secondary heads */
    2647         [ +  + ]:          24 :             if (opt_border >= 2)
    2648                 :          12 :                 fputs("\\toprule\n", fout);
    2649         [ +  + ]:         132 :             for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
    2650                 :             :             {
    2651         [ +  + ]:         108 :                 if (i != 0)
    2652                 :          84 :                     fputs(" & ", fout);
    2653                 :         108 :                 fputs("\\small\\textbf{\\textit{", fout);
    2654                 :         108 :                 latex_escaped_print(*ptr, fout);
    2655                 :         108 :                 fputs("}}", fout);
    2656                 :             :             }
    2657                 :          24 :             fputs(" \\\\\n", fout);
    2658                 :             :             /* If the line under the row already appeared, don't do another */
    2659         [ +  + ]:          24 :             if (opt_border != 3)
    2660                 :          16 :                 fputs("\\midrule\n", fout);
    2661                 :          24 :             fputs("\\endhead\n", fout);
    2662                 :             : 
    2663                 :             :             /* table name, caption? */
    2664   [ +  -  +  + ]:          24 :             if (!opt_tuples_only && cont->title)
    2665                 :             :             {
    2666                 :             :                 /* Don't output if we are printing a line under each row */
    2667         [ -  + ]:           4 :                 if (opt_border == 2)
    2668                 :           0 :                     fputs("\\bottomrule\n", fout);
    2669                 :           4 :                 fputs("\\caption[", fout);
    2670                 :           4 :                 latex_escaped_print(cont->title, fout);
    2671                 :           4 :                 fputs(" (Continued)]{", fout);
    2672                 :           4 :                 latex_escaped_print(cont->title, fout);
    2673                 :           4 :                 fputs("}\n\\endfoot\n", fout);
    2674         [ -  + ]:           4 :                 if (opt_border == 2)
    2675                 :           0 :                     fputs("\\bottomrule\n", fout);
    2676                 :           4 :                 fputs("\\caption[", fout);
    2677                 :           4 :                 latex_escaped_print(cont->title, fout);
    2678                 :           4 :                 fputs("]{", fout);
    2679                 :           4 :                 latex_escaped_print(cont->title, fout);
    2680                 :           4 :                 fputs("}\n\\endlastfoot\n", fout);
    2681                 :             :             }
    2682                 :             :             /* output bottom table line? */
    2683         [ +  + ]:          20 :             else if (opt_border >= 2)
    2684                 :             :             {
    2685                 :          12 :                 fputs("\\bottomrule\n\\endfoot\n", fout);
    2686                 :          12 :                 fputs("\\bottomrule\n\\endlastfoot\n", fout);
    2687                 :             :             }
    2688                 :             :         }
    2689                 :             :     }
    2690                 :             : 
    2691                 :             :     /* print cells */
    2692         [ +  + ]:         256 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2693                 :             :     {
    2694                 :             :         /* Add a line under each row? */
    2695   [ +  +  +  + ]:         228 :         if (i != 0 && i % cont->ncolumns != 0)
    2696                 :         176 :             fputs("\n&\n", fout);
    2697                 :         228 :         fputs("\\raggedright{", fout);
    2698                 :         228 :         latex_escaped_print(*ptr, fout);
    2699                 :         228 :         fputc('}', fout);
    2700         [ +  + ]:         228 :         if ((i + 1) % cont->ncolumns == 0)
    2701                 :             :         {
    2702                 :          52 :             fputs(" \\tabularnewline\n", fout);
    2703         [ +  + ]:          52 :             if (opt_border == 3)
    2704                 :          16 :                 fputs(" \\hline\n", fout);
    2705                 :             :         }
    2706         [ -  + ]:         228 :         if (CancelRequested)
    2707                 :           0 :             break;
    2708                 :             :     }
    2709                 :             : 
    2710         [ +  - ]:          28 :     if (cont->opt->stop_table)
    2711                 :          28 :         fputs("\\end{longtable}\n", fout);
    2712                 :             : }
    2713                 :             : 
    2714                 :             : 
    2715                 :             : static void
    2716                 :          52 : print_latex_vertical(const printTableContent *cont, FILE *fout)
    2717                 :             : {
    2718                 :          52 :     bool        opt_tuples_only = cont->opt->tuples_only;
    2719                 :          52 :     unsigned short opt_border = cont->opt->border;
    2720                 :          52 :     unsigned long record = cont->opt->prior_records + 1;
    2721                 :             :     unsigned int i;
    2722                 :             :     const char *const *ptr;
    2723                 :             : 
    2724         [ -  + ]:          52 :     if (CancelRequested)
    2725                 :           0 :         return;
    2726                 :             : 
    2727         [ +  + ]:          52 :     if (opt_border > 2)
    2728                 :          12 :         opt_border = 2;
    2729                 :             : 
    2730         [ +  - ]:          52 :     if (cont->opt->start_table)
    2731                 :             :     {
    2732                 :             :         /* print title */
    2733   [ +  +  +  + ]:          52 :         if (!opt_tuples_only && cont->title)
    2734                 :             :         {
    2735                 :           8 :             fputs("\\begin{center}\n", fout);
    2736                 :           8 :             latex_escaped_print(cont->title, fout);
    2737                 :           8 :             fputs("\n\\end{center}\n\n", fout);
    2738                 :             :         }
    2739                 :             : 
    2740                 :             :         /* begin environment and set alignments and borders */
    2741                 :          52 :         fputs("\\begin{tabular}{", fout);
    2742         [ +  + ]:          52 :         if (opt_border == 0)
    2743                 :           8 :             fputs("cl", fout);
    2744         [ +  + ]:          44 :         else if (opt_border == 1)
    2745                 :          24 :             fputs("c|l", fout);
    2746         [ +  - ]:          20 :         else if (opt_border == 2)
    2747                 :          20 :             fputs("|c|l|", fout);
    2748                 :          52 :         fputs("}\n", fout);
    2749                 :             :     }
    2750                 :             : 
    2751                 :             :     /* print records */
    2752         [ +  + ]:         476 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2753                 :             :     {
    2754                 :             :         /* new record */
    2755         [ +  + ]:         424 :         if (i % cont->ncolumns == 0)
    2756                 :             :         {
    2757         [ -  + ]:          96 :             if (CancelRequested)
    2758                 :           0 :                 break;
    2759         [ +  + ]:          96 :             if (!opt_tuples_only)
    2760                 :             :             {
    2761         [ +  + ]:          80 :                 if (opt_border == 2)
    2762                 :             :                 {
    2763                 :          40 :                     fputs("\\hline\n", fout);
    2764                 :          40 :                     fprintf(fout, "\\multicolumn{2}{|c|}{\\textit{Record %lu}} \\\\\n", record++);
    2765                 :             :                 }
    2766                 :             :                 else
    2767                 :          40 :                     fprintf(fout, "\\multicolumn{2}{c}{\\textit{Record %lu}} \\\\\n", record++);
    2768                 :             :             }
    2769         [ +  + ]:          96 :             if (opt_border >= 1)
    2770                 :          80 :                 fputs("\\hline\n", fout);
    2771                 :             :         }
    2772                 :             : 
    2773                 :         424 :         latex_escaped_print(cont->headers[i % cont->ncolumns], fout);
    2774                 :         424 :         fputs(" & ", fout);
    2775                 :         424 :         latex_escaped_print(*ptr, fout);
    2776                 :         424 :         fputs(" \\\\\n", fout);
    2777                 :             :     }
    2778                 :             : 
    2779         [ +  - ]:          52 :     if (cont->opt->stop_table)
    2780                 :             :     {
    2781         [ +  + ]:          52 :         if (opt_border == 2)
    2782                 :          20 :             fputs("\\hline\n", fout);
    2783                 :             : 
    2784                 :          52 :         fputs("\\end{tabular}\n\n\\noindent ", fout);
    2785                 :             : 
    2786                 :             :         /* print footers */
    2787   [ +  +  +  -  :          52 :         if (cont->footers && !opt_tuples_only && !CancelRequested)
                   +  - ]
    2788                 :             :         {
    2789                 :             :             printTableFooter *f;
    2790                 :             : 
    2791         [ +  + ]:          16 :             for (f = cont->footers; f; f = f->next)
    2792                 :             :             {
    2793                 :           8 :                 latex_escaped_print(f->data, fout);
    2794                 :           8 :                 fputs(" \\\\\n", fout);
    2795                 :             :             }
    2796                 :             :         }
    2797                 :             : 
    2798                 :          52 :         fputc('\n', fout);
    2799                 :             :     }
    2800                 :             : }
    2801                 :             : 
    2802                 :             : 
    2803                 :             : /*************************/
    2804                 :             : /* Troff -ms             */
    2805                 :             : /*************************/
    2806                 :             : 
    2807                 :             : 
    2808                 :             : static void
    2809                 :         596 : troff_ms_escaped_print(const char *in, FILE *fout)
    2810                 :             : {
    2811                 :             :     const char *p;
    2812                 :             : 
    2813         [ +  + ]:        4792 :     for (p = in; *p; p++)
    2814         [ +  + ]:        4196 :         switch (*p)
    2815                 :             :         {
    2816                 :          84 :             case '\\':
    2817                 :          84 :                 fputs("\\(rs", fout);
    2818                 :          84 :                 break;
    2819                 :        4112 :             default:
    2820                 :        4112 :                 fputc(*p, fout);
    2821                 :             :         }
    2822                 :         596 : }
    2823                 :             : 
    2824                 :             : 
    2825                 :             : static void
    2826                 :          20 : print_troff_ms_text(const printTableContent *cont, FILE *fout)
    2827                 :             : {
    2828                 :          20 :     bool        opt_tuples_only = cont->opt->tuples_only;
    2829                 :          20 :     unsigned short opt_border = cont->opt->border;
    2830                 :             :     unsigned int i;
    2831                 :             :     const char *const *ptr;
    2832                 :             : 
    2833         [ -  + ]:          20 :     if (CancelRequested)
    2834                 :           0 :         return;
    2835                 :             : 
    2836         [ -  + ]:          20 :     if (opt_border > 2)
    2837                 :           0 :         opt_border = 2;
    2838                 :             : 
    2839         [ +  - ]:          20 :     if (cont->opt->start_table)
    2840                 :             :     {
    2841                 :             :         /* print title */
    2842   [ +  +  +  + ]:          20 :         if (!opt_tuples_only && cont->title)
    2843                 :             :         {
    2844                 :           4 :             fputs(".LP\n.DS C\n", fout);
    2845                 :           4 :             troff_ms_escaped_print(cont->title, fout);
    2846                 :           4 :             fputs("\n.DE\n", fout);
    2847                 :             :         }
    2848                 :             : 
    2849                 :             :         /* begin environment and set alignments and borders */
    2850                 :          20 :         fputs(".LP\n.TS\n", fout);
    2851         [ +  + ]:          20 :         if (opt_border == 2)
    2852                 :           4 :             fputs("center box;\n", fout);
    2853                 :             :         else
    2854                 :          16 :             fputs("center;\n", fout);
    2855                 :             : 
    2856         [ +  + ]:         116 :         for (i = 0; i < cont->ncolumns; i++)
    2857                 :             :         {
    2858                 :          96 :             fputc(*(cont->aligns + i), fout);
    2859   [ +  +  +  + ]:          96 :             if (opt_border > 0 && i < cont->ncolumns - 1)
    2860                 :          64 :                 fputs(" | ", fout);
    2861                 :             :         }
    2862                 :          20 :         fputs(".\n", fout);
    2863                 :             : 
    2864                 :             :         /* print headers */
    2865         [ +  + ]:          20 :         if (!opt_tuples_only)
    2866                 :             :         {
    2867         [ +  + ]:          92 :             for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
    2868                 :             :             {
    2869         [ +  + ]:          76 :                 if (i != 0)
    2870                 :          60 :                     fputc('\t', fout);
    2871                 :          76 :                 fputs("\\fI", fout);
    2872                 :          76 :                 troff_ms_escaped_print(*ptr, fout);
    2873                 :          76 :                 fputs("\\fP", fout);
    2874                 :             :             }
    2875                 :          16 :             fputs("\n_\n", fout);
    2876                 :             :         }
    2877                 :             :     }
    2878                 :             : 
    2879                 :             :     /* print cells */
    2880         [ +  + ]:         184 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2881                 :             :     {
    2882                 :         164 :         troff_ms_escaped_print(*ptr, fout);
    2883                 :             : 
    2884         [ +  + ]:         164 :         if ((i + 1) % cont->ncolumns == 0)
    2885                 :             :         {
    2886                 :          36 :             fputc('\n', fout);
    2887         [ -  + ]:          36 :             if (CancelRequested)
    2888                 :           0 :                 break;
    2889                 :             :         }
    2890                 :             :         else
    2891                 :         128 :             fputc('\t', fout);
    2892                 :             :     }
    2893                 :             : 
    2894         [ +  - ]:          20 :     if (cont->opt->stop_table)
    2895                 :             :     {
    2896                 :          20 :         printTableFooter *footers = footers_with_default(cont);
    2897                 :             : 
    2898                 :          20 :         fputs(".TE\n.DS L\n", fout);
    2899                 :             : 
    2900                 :             :         /* print footers */
    2901   [ +  -  +  +  :          20 :         if (footers && !opt_tuples_only && !CancelRequested)
                   +  - ]
    2902                 :             :         {
    2903                 :             :             printTableFooter *f;
    2904                 :             : 
    2905         [ +  + ]:          32 :             for (f = footers; f; f = f->next)
    2906                 :             :             {
    2907                 :          16 :                 troff_ms_escaped_print(f->data, fout);
    2908                 :          16 :                 fputc('\n', fout);
    2909                 :             :             }
    2910                 :             :         }
    2911                 :             : 
    2912                 :          20 :         fputs(".DE\n", fout);
    2913                 :             :     }
    2914                 :             : }
    2915                 :             : 
    2916                 :             : 
    2917                 :             : static void
    2918                 :          20 : print_troff_ms_vertical(const printTableContent *cont, FILE *fout)
    2919                 :             : {
    2920                 :          20 :     bool        opt_tuples_only = cont->opt->tuples_only;
    2921                 :          20 :     unsigned short opt_border = cont->opt->border;
    2922                 :          20 :     unsigned long record = cont->opt->prior_records + 1;
    2923                 :             :     unsigned int i;
    2924                 :             :     const char *const *ptr;
    2925                 :          20 :     unsigned short current_format = 0;  /* 0=none, 1=header, 2=body */
    2926                 :             : 
    2927         [ -  + ]:          20 :     if (CancelRequested)
    2928                 :           0 :         return;
    2929                 :             : 
    2930         [ -  + ]:          20 :     if (opt_border > 2)
    2931                 :           0 :         opt_border = 2;
    2932                 :             : 
    2933         [ +  - ]:          20 :     if (cont->opt->start_table)
    2934                 :             :     {
    2935                 :             :         /* print title */
    2936   [ +  +  +  + ]:          20 :         if (!opt_tuples_only && cont->title)
    2937                 :             :         {
    2938                 :           4 :             fputs(".LP\n.DS C\n", fout);
    2939                 :           4 :             troff_ms_escaped_print(cont->title, fout);
    2940                 :           4 :             fputs("\n.DE\n", fout);
    2941                 :             :         }
    2942                 :             : 
    2943                 :             :         /* begin environment and set alignments and borders */
    2944                 :          20 :         fputs(".LP\n.TS\n", fout);
    2945         [ +  + ]:          20 :         if (opt_border == 2)
    2946                 :           4 :             fputs("center box;\n", fout);
    2947                 :             :         else
    2948                 :          16 :             fputs("center;\n", fout);
    2949                 :             : 
    2950                 :             :         /* basic format */
    2951         [ +  + ]:          20 :         if (opt_tuples_only)
    2952                 :           4 :             fputs("c l;\n", fout);
    2953                 :             :     }
    2954                 :             :     else
    2955                 :           0 :         current_format = 2;     /* assume tuples printed already */
    2956                 :             : 
    2957                 :             :     /* print records */
    2958         [ +  + ]:         184 :     for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
    2959                 :             :     {
    2960                 :             :         /* new record */
    2961         [ +  + ]:         164 :         if (i % cont->ncolumns == 0)
    2962                 :             :         {
    2963         [ -  + ]:          36 :             if (CancelRequested)
    2964                 :           0 :                 break;
    2965         [ +  + ]:          36 :             if (!opt_tuples_only)
    2966                 :             :             {
    2967         [ +  - ]:          28 :                 if (current_format != 1)
    2968                 :             :                 {
    2969   [ +  +  +  + ]:          28 :                     if (opt_border == 2 && record > 1)
    2970                 :           4 :                         fputs("_\n", fout);
    2971         [ +  + ]:          28 :                     if (current_format != 0)
    2972                 :          12 :                         fputs(".T&\n", fout);
    2973                 :          28 :                     fputs("c s.\n", fout);
    2974                 :          28 :                     current_format = 1;
    2975                 :             :                 }
    2976                 :          28 :                 fprintf(fout, "\\fIRecord %lu\\fP\n", record++);
    2977                 :             :             }
    2978         [ +  + ]:          36 :             if (opt_border >= 1)
    2979                 :          28 :                 fputs("_\n", fout);
    2980                 :             :         }
    2981                 :             : 
    2982         [ +  + ]:         164 :         if (!opt_tuples_only)
    2983                 :             :         {
    2984         [ +  + ]:         124 :             if (current_format != 2)
    2985                 :             :             {
    2986         [ +  - ]:          28 :                 if (current_format != 0)
    2987                 :          28 :                     fputs(".T&\n", fout);
    2988         [ +  + ]:          28 :                 if (opt_border != 1)
    2989                 :          16 :                     fputs("c l.\n", fout);
    2990                 :             :                 else
    2991                 :          12 :                     fputs("c | l.\n", fout);
    2992                 :          28 :                 current_format = 2;
    2993                 :             :             }
    2994                 :             :         }
    2995                 :             : 
    2996                 :         164 :         troff_ms_escaped_print(cont->headers[i % cont->ncolumns], fout);
    2997                 :         164 :         fputc('\t', fout);
    2998                 :         164 :         troff_ms_escaped_print(*ptr, fout);
    2999                 :             : 
    3000                 :         164 :         fputc('\n', fout);
    3001                 :             :     }
    3002                 :             : 
    3003         [ +  - ]:          20 :     if (cont->opt->stop_table)
    3004                 :             :     {
    3005                 :          20 :         fputs(".TE\n.DS L\n", fout);
    3006                 :             : 
    3007                 :             :         /* print footers */
    3008   [ +  +  +  -  :          20 :         if (cont->footers && !opt_tuples_only && !CancelRequested)
                   +  - ]
    3009                 :             :         {
    3010                 :             :             printTableFooter *f;
    3011                 :             : 
    3012         [ +  + ]:           8 :             for (f = cont->footers; f; f = f->next)
    3013                 :             :             {
    3014                 :           4 :                 troff_ms_escaped_print(f->data, fout);
    3015                 :           4 :                 fputc('\n', fout);
    3016                 :             :             }
    3017                 :             :         }
    3018                 :             : 
    3019                 :          20 :         fputs(".DE\n", fout);
    3020                 :             :     }
    3021                 :             : }
    3022                 :             : 
    3023                 :             : 
    3024                 :             : /********************************/
    3025                 :             : /* Public functions             */
    3026                 :             : /********************************/
    3027                 :             : 
    3028                 :             : 
    3029                 :             : /*
    3030                 :             :  * disable_sigpipe_trap
    3031                 :             :  *
    3032                 :             :  * Turn off SIGPIPE interrupt --- call this before writing to a temporary
    3033                 :             :  * query output file that is a pipe.
    3034                 :             :  *
    3035                 :             :  * No-op on Windows, where there's no SIGPIPE interrupts.
    3036                 :             :  */
    3037                 :             : void
    3038                 :           8 : disable_sigpipe_trap(void)
    3039                 :             : {
    3040                 :             : #ifndef WIN32
    3041                 :           8 :     pqsignal(SIGPIPE, PG_SIG_IGN);
    3042                 :             : #endif
    3043                 :           8 : }
    3044                 :             : 
    3045                 :             : /*
    3046                 :             :  * restore_sigpipe_trap
    3047                 :             :  *
    3048                 :             :  * Restore normal SIGPIPE interrupt --- call this when done writing to a
    3049                 :             :  * temporary query output file that was (or might have been) a pipe.
    3050                 :             :  *
    3051                 :             :  * Note: within psql, we enable SIGPIPE interrupts unless the permanent query
    3052                 :             :  * output file is a pipe, in which case they should be kept off.  This
    3053                 :             :  * approach works only because psql is not currently complicated enough to
    3054                 :             :  * have nested usages of short-lived output files.  Otherwise we'd probably
    3055                 :             :  * need a genuine save-and-restore-state approach; but for now, that would be
    3056                 :             :  * useless complication.  In non-psql programs, this always enables SIGPIPE.
    3057                 :             :  *
    3058                 :             :  * No-op on Windows, where there's no SIGPIPE interrupts.
    3059                 :             :  */
    3060                 :             : void
    3061                 :       11442 : restore_sigpipe_trap(void)
    3062                 :             : {
    3063                 :             : #ifndef WIN32
    3064         [ -  + ]:       11442 :     pqsignal(SIGPIPE, always_ignore_sigpipe ? PG_SIG_IGN : PG_SIG_DFL);
    3065                 :             : #endif
    3066                 :       11442 : }
    3067                 :             : 
    3068                 :             : /*
    3069                 :             :  * set_sigpipe_trap_state
    3070                 :             :  *
    3071                 :             :  * Set the trap state that restore_sigpipe_trap should restore to.
    3072                 :             :  */
    3073                 :             : void
    3074                 :       11434 : set_sigpipe_trap_state(bool ignore)
    3075                 :             : {
    3076                 :       11434 :     always_ignore_sigpipe = ignore;
    3077                 :       11434 : }
    3078                 :             : 
    3079                 :             : 
    3080                 :             : /*
    3081                 :             :  * PageOutput
    3082                 :             :  *
    3083                 :             :  * Tests if pager is needed and returns appropriate FILE pointer
    3084                 :             :  * (either a pipe, or stdout if we don't need the pager).
    3085                 :             :  *
    3086                 :             :  * lines: number of lines that will be printed
    3087                 :             :  * topt: print formatting options
    3088                 :             :  *
    3089                 :             :  * If the topt argument is NULL no pager is used.
    3090                 :             :  */
    3091                 :             : FILE *
    3092                 :         615 : PageOutput(int lines, const printTableOpt *topt)
    3093                 :             : {
    3094                 :         615 :     return PageOutputInternal(lines, topt, NULL, NULL, false);
    3095                 :             : }
    3096                 :             : 
    3097                 :             : /*
    3098                 :             :  * Private version that allows for line-counting to be avoided when
    3099                 :             :  * not needed.  If "cont" is not null then the input value of "lines"
    3100                 :             :  * is ignored and we count lines based on cont + width_wrap + vertical
    3101                 :             :  * (see count_table_lines).
    3102                 :             :  */
    3103                 :             : static FILE *
    3104                 :      102019 : PageOutputInternal(int lines, const printTableOpt *topt,
    3105                 :             :                    const printTableContent *cont,
    3106                 :             :                    const unsigned int *width_wrap,
    3107                 :             :                    bool vertical)
    3108                 :             : {
    3109                 :             :     /* check whether we need / can / are supposed to use pager */
    3110   [ +  +  +  +  :      102019 :     if (topt && topt->pager && isatty(fileno(stdin)) && isatty(fileno(stdout)))
             +  +  +  - ]
    3111                 :             :     {
    3112                 :             :         /* without TIOCGWINSZ, pager == 1 acts the same as pager > 1 */
    3113                 :             : #ifdef TIOCGWINSZ
    3114                 :           5 :         unsigned short int pager = topt->pager;
    3115                 :           5 :         int         min_lines = topt->pager_min_lines;
    3116                 :             : 
    3117         [ +  - ]:           5 :         if (pager == 1)
    3118                 :             :         {
    3119                 :             :             int         result;
    3120                 :             :             struct winsize screen_size;
    3121                 :             : 
    3122                 :           5 :             result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
    3123         [ -  + ]:           5 :             if (result < 0)
    3124                 :           0 :                 pager = 2;      /* force use of pager */
    3125                 :             :             else
    3126                 :             :             {
    3127                 :           5 :                 int         threshold = Max(screen_size.ws_row, min_lines);
    3128                 :             : 
    3129         [ +  - ]:           5 :                 if (cont)       /* caller wants us to calculate lines */
    3130                 :           5 :                     lines = count_table_lines(cont, width_wrap, vertical,
    3131                 :             :                                               threshold);
    3132                 :             :                 /* >= accounts for a one-line prompt */
    3133         [ +  + ]:           5 :                 if (lines >= threshold)
    3134                 :           4 :                     pager = 2;
    3135                 :             :             }
    3136                 :             :         }
    3137                 :             : 
    3138         [ +  + ]:           5 :         if (pager > 1)
    3139                 :             : #endif
    3140                 :             :         {
    3141                 :             :             const char *pagerprog;
    3142                 :             :             FILE       *pagerpipe;
    3143                 :             : 
    3144                 :           4 :             pagerprog = getenv("PSQL_PAGER");
    3145         [ -  + ]:           4 :             if (!pagerprog)
    3146                 :           0 :                 pagerprog = getenv("PAGER");
    3147         [ -  + ]:           4 :             if (!pagerprog)
    3148                 :           0 :                 pagerprog = DEFAULT_PAGER;
    3149                 :             :             else
    3150                 :             :             {
    3151                 :             :                 /* if PAGER is empty or all-white-space, don't use pager */
    3152         [ -  + ]:           4 :                 if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
    3153                 :           0 :                     return stdout;
    3154                 :             :             }
    3155                 :           4 :             fflush(NULL);
    3156                 :           4 :             disable_sigpipe_trap();
    3157                 :           4 :             pagerpipe = popen(pagerprog, "w");
    3158         [ +  - ]:           4 :             if (pagerpipe)
    3159                 :           4 :                 return pagerpipe;
    3160                 :             :             /* if popen fails, silently proceed without pager */
    3161                 :           0 :             restore_sigpipe_trap();
    3162                 :             :         }
    3163                 :             :     }
    3164                 :             : 
    3165                 :      102015 :     return stdout;
    3166                 :             : }
    3167                 :             : 
    3168                 :             : /*
    3169                 :             :  * ClosePager
    3170                 :             :  *
    3171                 :             :  * Close previously opened pager pipe, if any
    3172                 :             :  */
    3173                 :             : void
    3174                 :         619 : ClosePager(FILE *pagerpipe)
    3175                 :             : {
    3176   [ +  -  +  + ]:         619 :     if (pagerpipe && pagerpipe != stdout)
    3177                 :             :     {
    3178                 :             :         /*
    3179                 :             :          * If printing was canceled midstream, warn about it.
    3180                 :             :          *
    3181                 :             :          * Some pagers like less use Ctrl-C as part of their command set. Even
    3182                 :             :          * so, we abort our processing and warn the user what we did.  If the
    3183                 :             :          * pager quit as a result of the SIGINT, this message won't go
    3184                 :             :          * anywhere ...
    3185                 :             :          */
    3186         [ -  + ]:           4 :         if (CancelRequested)
    3187                 :           0 :             fprintf(pagerpipe, _("Interrupted\n"));
    3188                 :             : 
    3189                 :           4 :         pclose(pagerpipe);
    3190                 :           4 :         restore_sigpipe_trap();
    3191                 :             :     }
    3192                 :         619 : }
    3193                 :             : 
    3194                 :             : /*
    3195                 :             :  * Initialise a table contents struct.
    3196                 :             :  *      Must be called before any other printTable method is used.
    3197                 :             :  *
    3198                 :             :  * The title is not duplicated; the caller must ensure that the buffer
    3199                 :             :  * is available for the lifetime of the printTableContent struct.
    3200                 :             :  *
    3201                 :             :  * If you call this, you must call printTableCleanup once you're done with the
    3202                 :             :  * table.
    3203                 :             :  */
    3204                 :             : void
    3205                 :      101998 : printTableInit(printTableContent *content, const printTableOpt *opt,
    3206                 :             :                const char *title, int ncolumns, int nrows)
    3207                 :             : {
    3208                 :             :     uint64      total_cells;
    3209                 :             : 
    3210                 :      101998 :     content->opt = opt;
    3211                 :      101998 :     content->title = title;
    3212                 :      101998 :     content->ncolumns = ncolumns;
    3213                 :      101998 :     content->nrows = nrows;
    3214                 :             : 
    3215                 :      101998 :     content->headers = pg_malloc0_array(const char *, (ncolumns + 1));
    3216                 :      101998 :     content->headersadded = 0;
    3217                 :      101998 :     content->headermustfree = NULL;
    3218                 :             : 
    3219                 :      101998 :     total_cells = (uint64) ncolumns * nrows;
    3220                 :             : 
    3221                 :             :     /* Catch possible overflow.  Using >= here allows adding 1 below */
    3222         [ -  + ]:      101998 :     if (total_cells >= SIZE_MAX / sizeof(*content->cells))
    3223                 :           0 :         pg_fatal("cannot print table contents: number of cells %" PRIu64 " is equal to or exceeds maximum %zu",
    3224                 :             :                  total_cells, SIZE_MAX / sizeof(*content->cells));
    3225                 :             : 
    3226                 :      101998 :     content->cells = pg_malloc0_array(const char *, (total_cells + 1));
    3227                 :      101998 :     content->cellsadded = 0;
    3228                 :      101998 :     content->cellmustfree = NULL;
    3229                 :             : 
    3230                 :      101998 :     content->footers = NULL;
    3231                 :             : 
    3232                 :      101998 :     content->aligns = pg_malloc0_array(char, (ncolumns + 1));
    3233                 :             : 
    3234                 :      101998 :     content->footer = content->footers;
    3235                 :      101998 : }
    3236                 :             : 
    3237                 :             : /*
    3238                 :             :  * Add a header to the table.
    3239                 :             :  *
    3240                 :             :  * Headers are not duplicated; you must ensure that the header string is
    3241                 :             :  * available for the lifetime of the printTableContent struct.
    3242                 :             :  *
    3243                 :             :  * If translate is true, the function will pass the header through gettext.
    3244                 :             :  * Otherwise, the header will not be translated.
    3245                 :             :  *
    3246                 :             :  * align is either 'l' or 'r', and specifies the alignment for cells in this
    3247                 :             :  * column.
    3248                 :             :  */
    3249                 :             : void
    3250                 :      191965 : printTableAddHeader(printTableContent *content, const char *header,
    3251                 :             :                     bool translate, char align)
    3252                 :             : {
    3253                 :      191965 :     bool        mustfree = false;
    3254                 :             : 
    3255         [ -  + ]:      191965 :     if (content->headersadded >= content->ncolumns)
    3256                 :           0 :         pg_fatal("cannot add header to table content: column count of %d exceeded",
    3257                 :             :                  content->ncolumns);
    3258                 :             : 
    3259         [ +  + ]:      191965 :     if (translate)
    3260                 :       26839 :         header = _(header);
    3261                 :             : 
    3262                 :             :     /*
    3263                 :             :      * Note: Translated strings are not checked for encoding validity.  These
    3264                 :             :      * are provided by ourselves, so they had better be ok.  And if they were
    3265                 :             :      * not, running mbvalidate on them could overwrite gettext-owned memory.
    3266                 :             :      */
    3267   [ +  +  -  + ]:      191965 :     if (!translate && !mb_is_valid((const unsigned char *) header, content->opt->encoding))
    3268                 :             :     {
    3269                 :             :         char       *header2;
    3270                 :             : 
    3271                 :           0 :         header2 = pg_strdup(header);
    3272                 :           0 :         header = (char *) mbvalidate((unsigned char *) header2, content->opt->encoding);
    3273                 :           0 :         mustfree = true;
    3274                 :             :     }
    3275                 :             : 
    3276                 :      191965 :     content->headers[content->headersadded] = header;
    3277                 :      191965 :     content->aligns[content->headersadded] = align;
    3278                 :             : 
    3279         [ -  + ]:      191965 :     if (mustfree)
    3280                 :             :     {
    3281         [ #  # ]:           0 :         if (content->headermustfree == NULL)
    3282                 :           0 :             content->headermustfree =
    3283                 :           0 :                 pg_malloc0_array(bool, (content->ncolumns + 1));
    3284                 :             : 
    3285                 :           0 :         content->headermustfree[content->headersadded] = true;
    3286                 :             :     }
    3287                 :             : 
    3288                 :      191965 :     content->headersadded++;
    3289                 :      191965 : }
    3290                 :             : 
    3291                 :             : /*
    3292                 :             :  * Add a cell to the table.
    3293                 :             :  *
    3294                 :             :  * Cells are not duplicated; you must ensure that the cell string is available
    3295                 :             :  * for the lifetime of the printTableContent struct.
    3296                 :             :  *
    3297                 :             :  * If translate is true, the function will pass the cell through gettext.
    3298                 :             :  * Otherwise, the cell will not be translated.
    3299                 :             :  *
    3300                 :             :  * If mustfree is true, the cell string is freed by printTableCleanup().
    3301                 :             :  * Note: Automatic freeing of translatable strings is not supported.
    3302                 :             :  */
    3303                 :             : void
    3304                 :     2589663 : printTableAddCell(printTableContent *content, const char *cell,
    3305                 :             :                   bool translate, bool mustfree)
    3306                 :             : {
    3307                 :             :     uint64      total_cells;
    3308                 :             : 
    3309                 :     2589663 :     total_cells = (uint64) content->ncolumns * content->nrows;
    3310                 :             : 
    3311         [ -  + ]:     2589663 :     if (content->cellsadded >= total_cells)
    3312                 :           0 :         pg_fatal("cannot add cell to table content: total cell count of %" PRIu64 " exceeded",
    3313                 :             :                  total_cells);
    3314                 :             : 
    3315                 :             :     Assert(!(translate && mustfree));
    3316                 :             : 
    3317         [ +  + ]:     2589663 :     if (translate)
    3318                 :        1211 :         cell = _(cell);
    3319                 :             : 
    3320                 :             :     /*
    3321                 :             :      * Note: Translated strings are not checked for encoding validity.  These
    3322                 :             :      * are provided by ourselves, so they had better be ok.  And if they were
    3323                 :             :      * not, running mbvalidate on them could overwrite gettext-owned memory.
    3324                 :             :      */
    3325   [ +  +  +  + ]:     2589663 :     if (!translate && !mb_is_valid((const unsigned char *) cell, content->opt->encoding))
    3326                 :             :     {
    3327                 :             :         char       *cell2;
    3328                 :             : 
    3329                 :             :         /*
    3330                 :             :          * If mustfree is already true, then we own the memory and can have
    3331                 :             :          * mbvalidate() overwrite it directly.
    3332                 :             :          */
    3333         [ -  + ]:          16 :         if (mustfree)
    3334                 :           0 :             cell2 = unconstify(char *, cell);
    3335                 :             :         else
    3336                 :             :         {
    3337                 :          16 :             cell2 = pg_strdup(cell);
    3338                 :          16 :             mustfree = true;
    3339                 :             :         }
    3340                 :          16 :         cell = (char *) mbvalidate((unsigned char *) cell2, content->opt->encoding);
    3341                 :             :     }
    3342                 :             : 
    3343                 :     2589663 :     content->cells[content->cellsadded] = cell;
    3344                 :             : 
    3345         [ +  + ]:     2589663 :     if (mustfree)
    3346                 :             :     {
    3347         [ +  + ]:         366 :         if (content->cellmustfree == NULL)
    3348                 :         278 :             content->cellmustfree =
    3349                 :         278 :                 pg_malloc0_array(bool, (total_cells + 1));
    3350                 :             : 
    3351                 :         366 :         content->cellmustfree[content->cellsadded] = true;
    3352                 :             :     }
    3353                 :             : 
    3354                 :     2589663 :     content->cellsadded++;
    3355                 :     2589663 : }
    3356                 :             : 
    3357                 :             : /*
    3358                 :             :  * Add a footer to the table.
    3359                 :             :  *
    3360                 :             :  * Footers are added as elements of a singly-linked list, and the content is
    3361                 :             :  * strdup'd, so there is no need to keep the original footer string around.
    3362                 :             :  *
    3363                 :             :  * Footers are never translated by the function.  If you want the footer
    3364                 :             :  * translated you must do so yourself, before calling printTableAddFooter.  The
    3365                 :             :  * reason this works differently to headers and cells is that footers tend to
    3366                 :             :  * be made of up individually translated components, rather than being
    3367                 :             :  * translated as a whole.
    3368                 :             :  */
    3369                 :             : void
    3370                 :        8911 : printTableAddFooter(printTableContent *content, const char *footer)
    3371                 :             : {
    3372                 :             :     printTableFooter *f;
    3373                 :             : 
    3374                 :        8911 :     f = pg_malloc0_object(printTableFooter);
    3375                 :        8911 :     f->data = pg_strdup(footer);
    3376                 :             : 
    3377         [ +  + ]:        8911 :     if (content->footers == NULL)
    3378                 :        2646 :         content->footers = f;
    3379                 :             :     else
    3380                 :        6265 :         content->footer->next = f;
    3381                 :             : 
    3382                 :        8911 :     content->footer = f;
    3383                 :        8911 : }
    3384                 :             : 
    3385                 :             : /*
    3386                 :             :  * Change the content of the last-added footer.
    3387                 :             :  *
    3388                 :             :  * The current contents of the last-added footer are freed, and replaced by the
    3389                 :             :  * content given in *footer.  If there was no previous footer, add a new one.
    3390                 :             :  *
    3391                 :             :  * The content is strdup'd, so there is no need to keep the original string
    3392                 :             :  * around.
    3393                 :             :  */
    3394                 :             : void
    3395                 :          20 : printTableSetFooter(printTableContent *content, const char *footer)
    3396                 :             : {
    3397         [ +  - ]:          20 :     if (content->footers != NULL)
    3398                 :             :     {
    3399                 :          20 :         free(content->footer->data);
    3400                 :          20 :         content->footer->data = pg_strdup(footer);
    3401                 :             :     }
    3402                 :             :     else
    3403                 :           0 :         printTableAddFooter(content, footer);
    3404                 :          20 : }
    3405                 :             : 
    3406                 :             : /*
    3407                 :             :  * Free all memory allocated to this struct.
    3408                 :             :  *
    3409                 :             :  * Once this has been called, the struct is unusable unless you pass it to
    3410                 :             :  * printTableInit() again.
    3411                 :             :  */
    3412                 :             : void
    3413                 :      101998 : printTableCleanup(printTableContent *content)
    3414                 :             : {
    3415         [ -  + ]:      101998 :     if (content->headermustfree)
    3416                 :             :     {
    3417         [ #  # ]:           0 :         for (uint64 i = 0; i < content->ncolumns; i++)
    3418                 :             :         {
    3419         [ #  # ]:           0 :             if (content->headermustfree[i])
    3420                 :           0 :                 free(unconstify(char *, content->headers[i]));
    3421                 :             :         }
    3422                 :           0 :         free(content->headermustfree);
    3423                 :           0 :         content->headermustfree = NULL;
    3424                 :             :     }
    3425         [ +  + ]:      101998 :     if (content->cellmustfree)
    3426                 :             :     {
    3427                 :             :         uint64      total_cells;
    3428                 :             : 
    3429                 :         278 :         total_cells = (uint64) content->ncolumns * content->nrows;
    3430         [ +  + ]:        4402 :         for (uint64 i = 0; i < total_cells; i++)
    3431                 :             :         {
    3432         [ +  + ]:        4124 :             if (content->cellmustfree[i])
    3433                 :         366 :                 free(unconstify(char *, content->cells[i]));
    3434                 :             :         }
    3435                 :         278 :         free(content->cellmustfree);
    3436                 :         278 :         content->cellmustfree = NULL;
    3437                 :             :     }
    3438                 :      101998 :     free(content->headers);
    3439                 :      101998 :     free(content->cells);
    3440                 :      101998 :     free(content->aligns);
    3441                 :             : 
    3442                 :      101998 :     content->opt = NULL;
    3443                 :      101998 :     content->title = NULL;
    3444                 :      101998 :     content->headers = NULL;
    3445                 :      101998 :     content->cells = NULL;
    3446                 :      101998 :     content->aligns = NULL;
    3447                 :      101998 :     content->headersadded = 0;
    3448                 :      101998 :     content->cellsadded = 0;
    3449                 :             : 
    3450         [ +  + ]:      101998 :     if (content->footers)
    3451                 :             :     {
    3452         [ +  + ]:       11557 :         for (content->footer = content->footers; content->footer;)
    3453                 :             :         {
    3454                 :             :             printTableFooter *f;
    3455                 :             : 
    3456                 :        8911 :             f = content->footer;
    3457                 :        8911 :             content->footer = f->next;
    3458                 :        8911 :             free(f->data);
    3459                 :        8911 :             free(f);
    3460                 :             :         }
    3461                 :             :     }
    3462                 :      101998 :     content->footers = NULL;
    3463                 :      101998 :     content->footer = NULL;
    3464                 :      101998 : }
    3465                 :             : 
    3466                 :             : /*
    3467                 :             :  * IsPagerNeeded
    3468                 :             :  *
    3469                 :             :  * Setup pager if required
    3470                 :             :  *
    3471                 :             :  * cont: table data to be printed
    3472                 :             :  * width_wrap[]: per-column maximum width, or NULL if caller will not wrap
    3473                 :             :  * vertical: vertical mode?
    3474                 :             :  * fout: where to print to (in/out argument)
    3475                 :             :  * is_pager: output argument
    3476                 :             :  *
    3477                 :             :  * If we decide pager is needed, *fout is modified and *is_pager is set true
    3478                 :             :  */
    3479                 :             : static void
    3480                 :      101440 : IsPagerNeeded(const printTableContent *cont, const unsigned int *width_wrap,
    3481                 :             :               bool vertical,
    3482                 :             :               FILE **fout, bool *is_pager)
    3483                 :             : {
    3484         [ +  + ]:      101440 :     if (*fout == stdout)
    3485                 :             :     {
    3486                 :      101404 :         *fout = PageOutputInternal(0, cont->opt, cont, width_wrap, vertical);
    3487                 :      101404 :         *is_pager = (*fout != stdout);
    3488                 :             :     }
    3489                 :             :     else
    3490                 :          36 :         *is_pager = false;
    3491                 :      101440 : }
    3492                 :             : 
    3493                 :             : /*
    3494                 :             :  * Count the number of lines needed to print the given table.
    3495                 :             :  *
    3496                 :             :  * cont: table data to be printed
    3497                 :             :  * width_wrap[]: per-column maximum width, or NULL if caller will not wrap
    3498                 :             :  * vertical: vertical mode?
    3499                 :             :  * threshold: we can stop counting once we pass this many lines
    3500                 :             :  *
    3501                 :             :  * The result is currently only fully accurate for ALIGNED/WRAPPED and
    3502                 :             :  * UNALIGNED formats; otherwise it's an approximation.
    3503                 :             :  *
    3504                 :             :  * Note: while cont->opt will tell us most formatting details, we need the
    3505                 :             :  * separate "vertical" flag because of the possibility of a dynamic switch
    3506                 :             :  * from aligned_text to aligned_vertical format.
    3507                 :             :  *
    3508                 :             :  * The point of the threshold parameter is that when the table is very long,
    3509                 :             :  * we'll typically be able to stop scanning after not many rows.
    3510                 :             :  */
    3511                 :             : #ifdef NEED_COUNT_TABLE_LINES
    3512                 :             : static int
    3513                 :           5 : count_table_lines(const printTableContent *cont,
    3514                 :             :                   const unsigned int *width_wrap,
    3515                 :             :                   bool vertical,
    3516                 :             :                   int threshold)
    3517                 :             : {
    3518                 :             :     int        *header_height;
    3519                 :           5 :     int         lines = 0,
    3520                 :           5 :                 max_lines = 0,
    3521                 :             :                 nl_lines,
    3522                 :             :                 i;
    3523                 :           5 :     int         encoding = cont->opt->encoding;
    3524                 :             :     const char *const *cell;
    3525                 :             : 
    3526                 :             :     /*
    3527                 :             :      * Scan all column headers and determine their heights.  Cache the values
    3528                 :             :      * since vertical mode repeats the headers for every record.
    3529                 :             :      */
    3530                 :           5 :     header_height = pg_malloc_array(int, cont->ncolumns);
    3531         [ +  + ]:          16 :     for (i = 0; i < cont->ncolumns; i++)
    3532                 :             :     {
    3533                 :          11 :         pg_wcssize((const unsigned char *) cont->headers[i],
    3534                 :          11 :                    strlen(cont->headers[i]), encoding,
    3535                 :          11 :                    NULL, &header_height[i], NULL);
    3536                 :             :     }
    3537                 :             : 
    3538                 :             :     /*
    3539                 :             :      * Account for separator lines (if used), as well as the trailing blank
    3540                 :             :      * line that most formats emit.
    3541                 :             :      */
    3542   [ +  +  -  -  :           5 :     switch (cont->opt->format)
                   -  - ]
    3543                 :             :     {
    3544                 :           1 :         case PRINT_ALIGNED:
    3545                 :             :         case PRINT_WRAPPED:
    3546                 :             : 
    3547                 :             :             /*
    3548                 :             :              * Vertical mode writes one separator line per record.  Normal
    3549                 :             :              * mode writes a single separator line between header and rows.
    3550                 :             :              */
    3551         [ +  - ]:           1 :             lines = vertical ? cont->nrows : 1;
    3552                 :             :             /* Both modes add a blank line at the end */
    3553                 :           1 :             lines++;
    3554                 :           1 :             break;
    3555                 :           4 :         case PRINT_UNALIGNED:
    3556                 :             : 
    3557                 :             :             /*
    3558                 :             :              * Vertical mode writes a separator (here assumed to be a newline)
    3559                 :             :              * between records.  Normal mode writes nothing extra.
    3560                 :             :              */
    3561         [ +  + ]:           4 :             if (vertical)
    3562                 :           1 :                 lines = Max(cont->nrows - 1, 0);
    3563                 :           4 :             break;
    3564                 :           0 :         case PRINT_CSV:
    3565                 :             :             /* Nothing extra is added */
    3566                 :           0 :             break;
    3567                 :           0 :         case PRINT_HTML:
    3568                 :             :         case PRINT_ASCIIDOC:
    3569                 :             :         case PRINT_LATEX:
    3570                 :             :         case PRINT_LATEX_LONGTABLE:
    3571                 :             :         case PRINT_TROFF_MS:
    3572                 :             : 
    3573                 :             :             /*
    3574                 :             :              * These formats aren't really meant for interactive consumption,
    3575                 :             :              * so for now we won't work hard on them.  Treat them like aligned
    3576                 :             :              * mode.
    3577                 :             :              */
    3578         [ #  # ]:           0 :             lines = vertical ? cont->nrows : 1;
    3579                 :           0 :             lines++;
    3580                 :           0 :             break;
    3581                 :           0 :         case PRINT_NOTHING:
    3582                 :             :             /* Shouldn't get here... */
    3583                 :           0 :             break;
    3584                 :             :     }
    3585                 :             : 
    3586                 :             :     /* Scan all cells to count their lines */
    3587         [ +  + ]:         237 :     for (i = 0, cell = cont->cells; *cell; cell++)
    3588                 :             :     {
    3589                 :             :         int         width;
    3590                 :             : 
    3591                 :             :         /* Count the original line breaks */
    3592                 :         235 :         pg_wcssize((const unsigned char *) *cell, strlen(*cell), encoding,
    3593                 :             :                    &width, &nl_lines, NULL);
    3594                 :             : 
    3595                 :             :         /* Count extra lines due to wrapping */
    3596   [ +  +  +  +  :         235 :         if (width > 0 && width_wrap && width_wrap[i])
                   +  - ]
    3597                 :           7 :             nl_lines += (width - 1) / width_wrap[i];
    3598                 :             : 
    3599         [ +  + ]:         235 :         if (vertical)
    3600                 :             :         {
    3601                 :             :             /* Pick the height of the header or cell, whichever is taller */
    3602         [ +  + ]:          13 :             if (nl_lines > header_height[i])
    3603                 :           7 :                 lines += nl_lines;
    3604                 :             :             else
    3605                 :           6 :                 lines += header_height[i];
    3606                 :             :         }
    3607                 :             :         else
    3608                 :             :         {
    3609                 :             :             /* Remember max height in the current row */
    3610         [ +  + ]:         222 :             if (nl_lines > max_lines)
    3611                 :          72 :                 max_lines = nl_lines;
    3612                 :             :         }
    3613                 :             : 
    3614                 :             :         /* i is the current column number: increment with wrap */
    3615         [ +  + ]:         235 :         if (++i >= cont->ncolumns)
    3616                 :             :         {
    3617                 :          85 :             i = 0;
    3618         [ +  + ]:          85 :             if (!vertical)
    3619                 :             :             {
    3620                 :             :                 /* At last column of each row, add tallest column height */
    3621                 :          72 :                 lines += max_lines;
    3622                 :          72 :                 max_lines = 0;
    3623                 :             :             }
    3624                 :             :             /* Stop scanning table body once we pass threshold */
    3625         [ +  + ]:          85 :             if (lines > threshold)
    3626                 :           3 :                 break;
    3627                 :             :         }
    3628                 :             :     }
    3629                 :             : 
    3630                 :             :     /* Account for header and footer decoration */
    3631   [ +  +  -  + ]:           5 :     if (!cont->opt->tuples_only && lines <= threshold)
    3632                 :             :     {
    3633                 :             :         printTableFooter *f;
    3634                 :             : 
    3635         [ #  # ]:           0 :         if (cont->title)
    3636                 :             :         {
    3637                 :             :             /* Add height of title */
    3638                 :           0 :             pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
    3639                 :             :                        encoding, NULL, &nl_lines, NULL);
    3640                 :           0 :             lines += nl_lines;
    3641                 :             :         }
    3642                 :             : 
    3643         [ #  # ]:           0 :         if (!vertical)
    3644                 :             :         {
    3645                 :             :             /* Add height of tallest header column */
    3646                 :           0 :             max_lines = 0;
    3647         [ #  # ]:           0 :             for (i = 0; i < cont->ncolumns; i++)
    3648                 :             :             {
    3649         [ #  # ]:           0 :                 if (header_height[i] > max_lines)
    3650                 :           0 :                     max_lines = header_height[i];
    3651                 :             :             }
    3652                 :           0 :             lines += max_lines;
    3653                 :             :         }
    3654                 :             : 
    3655                 :             :         /*
    3656                 :             :          * Add all footer lines.  Vertical mode does not use the default
    3657                 :             :          * footer, but we must include that in normal mode.
    3658                 :             :          */
    3659         [ #  # ]:           0 :         for (f = (vertical ? cont->footers : footers_with_default(cont));
    3660         [ #  # ]:           0 :              f != NULL; f = f->next)
    3661                 :             :         {
    3662                 :           0 :             pg_wcssize((const unsigned char *) f->data, strlen(f->data),
    3663                 :             :                        encoding, NULL, &nl_lines, NULL);
    3664                 :           0 :             lines += nl_lines;
    3665                 :             :             /* Stop scanning footers once we pass threshold */
    3666         [ #  # ]:           0 :             if (lines > threshold)
    3667                 :           0 :                 break;
    3668                 :             :         }
    3669                 :             :     }
    3670                 :             : 
    3671                 :           5 :     pg_free(header_height);
    3672                 :             : 
    3673                 :           5 :     return lines;
    3674                 :             : }
    3675                 :             : #endif                          /* NEED_COUNT_TABLE_LINES */
    3676                 :             : 
    3677                 :             : /*
    3678                 :             :  * Use this to print any table in the supported formats.
    3679                 :             :  *
    3680                 :             :  * cont: table data and formatting options
    3681                 :             :  * fout: where to print to
    3682                 :             :  * is_pager: true if caller has already redirected fout to be a pager pipe
    3683                 :             :  * flog: if not null, also print the table there (for --log-file option)
    3684                 :             :  */
    3685                 :             : void
    3686                 :      101990 : printTable(const printTableContent *cont,
    3687                 :             :            FILE *fout, bool is_pager, FILE *flog)
    3688                 :             : {
    3689                 :      101990 :     bool        is_local_pager = false;
    3690                 :             : 
    3691         [ -  + ]:      101990 :     if (CancelRequested)
    3692                 :           0 :         return;
    3693                 :             : 
    3694         [ -  + ]:      101990 :     if (cont->opt->format == PRINT_NOTHING)
    3695                 :           0 :         return;
    3696                 :             : 
    3697                 :             :     /* print_aligned_*() handle the pager themselves */
    3698         [ +  + ]:      101990 :     if (!is_pager &&
    3699         [ +  + ]:      101890 :         cont->opt->format != PRINT_ALIGNED &&
    3700         [ +  + ]:       11178 :         cont->opt->format != PRINT_WRAPPED)
    3701                 :             :     {
    3702                 :       11005 :         IsPagerNeeded(cont, NULL, (cont->opt->expanded == 1), &fout, &is_pager);
    3703                 :       11005 :         is_local_pager = is_pager;
    3704                 :             :     }
    3705                 :             : 
    3706                 :             :     /* clear any pre-existing error indication on the output stream */
    3707                 :      101990 :     clearerr(fout);
    3708                 :             : 
    3709                 :             :     /* print the stuff */
    3710   [ +  +  +  +  :      101990 :     switch (cont->opt->format)
             +  +  +  +  
                      - ]
    3711                 :             :     {
    3712                 :       10737 :         case PRINT_UNALIGNED:
    3713         [ +  + ]:       10737 :             if (cont->opt->expanded == 1)
    3714                 :          69 :                 print_unaligned_vertical(cont, fout);
    3715                 :             :             else
    3716                 :       10668 :                 print_unaligned_text(cont, fout);
    3717                 :       10737 :             break;
    3718                 :       90985 :         case PRINT_ALIGNED:
    3719                 :             :         case PRINT_WRAPPED:
    3720                 :             : 
    3721                 :             :             /*
    3722                 :             :              * In expanded-auto mode, force vertical if a pager is passed in;
    3723                 :             :              * else we may make different decisions for different hunks of the
    3724                 :             :              * query result.
    3725                 :             :              */
    3726         [ +  + ]:       90985 :             if (cont->opt->expanded == 1 ||
    3727   [ -  +  -  - ]:       90584 :                 (cont->opt->expanded == 2 && is_pager))
    3728                 :         401 :                 print_aligned_vertical(cont, fout, is_pager);
    3729                 :             :             else
    3730                 :       90584 :                 print_aligned_text(cont, fout, is_pager);
    3731                 :       90985 :             break;
    3732                 :          44 :         case PRINT_CSV:
    3733         [ +  + ]:          44 :             if (cont->opt->expanded == 1)
    3734                 :          12 :                 print_csv_vertical(cont, fout);
    3735                 :             :             else
    3736                 :          32 :                 print_csv_text(cont, fout);
    3737                 :          44 :             break;
    3738                 :          40 :         case PRINT_HTML:
    3739         [ +  + ]:          40 :             if (cont->opt->expanded == 1)
    3740                 :          20 :                 print_html_vertical(cont, fout);
    3741                 :             :             else
    3742                 :          20 :                 print_html_text(cont, fout);
    3743                 :          40 :             break;
    3744                 :          40 :         case PRINT_ASCIIDOC:
    3745         [ +  + ]:          40 :             if (cont->opt->expanded == 1)
    3746                 :          20 :                 print_asciidoc_vertical(cont, fout);
    3747                 :             :             else
    3748                 :          20 :                 print_asciidoc_text(cont, fout);
    3749                 :          40 :             break;
    3750                 :          48 :         case PRINT_LATEX:
    3751         [ +  + ]:          48 :             if (cont->opt->expanded == 1)
    3752                 :          24 :                 print_latex_vertical(cont, fout);
    3753                 :             :             else
    3754                 :          24 :                 print_latex_text(cont, fout);
    3755                 :          48 :             break;
    3756                 :          56 :         case PRINT_LATEX_LONGTABLE:
    3757         [ +  + ]:          56 :             if (cont->opt->expanded == 1)
    3758                 :          28 :                 print_latex_vertical(cont, fout);
    3759                 :             :             else
    3760                 :          28 :                 print_latex_longtable_text(cont, fout);
    3761                 :          56 :             break;
    3762                 :          40 :         case PRINT_TROFF_MS:
    3763         [ +  + ]:          40 :             if (cont->opt->expanded == 1)
    3764                 :          20 :                 print_troff_ms_vertical(cont, fout);
    3765                 :             :             else
    3766                 :          20 :                 print_troff_ms_text(cont, fout);
    3767                 :          40 :             break;
    3768                 :           0 :         default:
    3769                 :           0 :             pg_fatal_internal("invalid output format (internal error): %d",
    3770                 :             :                               cont->opt->format);
    3771                 :             :     }
    3772                 :             : 
    3773         [ +  + ]:      101990 :     if (is_local_pager)
    3774                 :           3 :         ClosePager(fout);
    3775                 :             : 
    3776                 :             :     /* also produce log output if wanted */
    3777         [ -  + ]:      101990 :     if (flog)
    3778                 :           0 :         print_aligned_text(cont, flog, false);
    3779                 :             : }
    3780                 :             : 
    3781                 :             : /*
    3782                 :             :  * Use this to print query results
    3783                 :             :  *
    3784                 :             :  * result: result of a successful query
    3785                 :             :  * opt: formatting options
    3786                 :             :  * fout: where to print to
    3787                 :             :  * is_pager: true if caller has already redirected fout to be a pager pipe
    3788                 :             :  * flog: if not null, also print the data there (for --log-file option)
    3789                 :             :  */
    3790                 :             : void
    3791                 :       99026 : printQuery(const PGresult *result, const printQueryOpt *opt,
    3792                 :             :            FILE *fout, bool is_pager, FILE *flog)
    3793                 :             : {
    3794                 :             :     printTableContent cont;
    3795                 :             :     int         i,
    3796                 :             :                 r,
    3797                 :             :                 c;
    3798                 :             : 
    3799         [ -  + ]:       99026 :     if (CancelRequested)
    3800                 :           0 :         return;
    3801                 :             : 
    3802                 :       99026 :     printTableInit(&cont, &opt->topt, opt->title,
    3803                 :             :                    PQnfields(result), PQntuples(result));
    3804                 :             : 
    3805                 :             :     /* Assert caller supplied enough translate_columns[] entries */
    3806                 :             :     Assert(opt->translate_columns == NULL ||
    3807                 :             :            opt->n_translate_columns >= cont.ncolumns);
    3808                 :             : 
    3809         [ +  + ]:      271656 :     for (i = 0; i < cont.ncolumns; i++)
    3810                 :             :     {
    3811                 :      172630 :         printTableAddHeader(&cont, PQfname(result, i),
    3812                 :      172630 :                             opt->translate_header,
    3813                 :      172630 :                             column_type_alignment(PQftype(result, i)));
    3814                 :             :     }
    3815                 :             : 
    3816                 :             :     /* set cells */
    3817         [ +  + ]:     1317689 :     for (r = 0; r < cont.nrows; r++)
    3818                 :             :     {
    3819         [ +  + ]:     3766304 :         for (c = 0; c < cont.ncolumns; c++)
    3820                 :             :         {
    3821                 :             :             char       *cell;
    3822                 :     2547641 :             bool        mustfree = false;
    3823                 :             :             bool        translate;
    3824                 :             : 
    3825         [ +  + ]:     2547641 :             if (PQgetisnull(result, r, c))
    3826         [ +  + ]:       44784 :                 cell = opt->nullPrint ? opt->nullPrint : "";
    3827         [ +  + ]:     2502857 :             else if (PQftype(result, c) == BOOLOID)
    3828                 :       96980 :                 cell = (PQgetvalue(result, r, c)[0] == 't' ?
    3829   [ +  +  +  + ]:       75750 :                         (opt->truePrint ? opt->truePrint : "t") :
    3830         [ +  + ]:       27260 :                         (opt->falsePrint ? opt->falsePrint : "f"));
    3831                 :             :             else
    3832                 :             :             {
    3833                 :     2454367 :                 cell = PQgetvalue(result, r, c);
    3834   [ +  +  +  + ]:     2454367 :                 if (cont.aligns[c] == 'r' && opt->topt.numericLocale)
    3835                 :             :                 {
    3836                 :          64 :                     cell = format_numeric_locale(cell);
    3837                 :          64 :                     mustfree = true;
    3838                 :             :                 }
    3839                 :             :             }
    3840                 :             : 
    3841   [ +  +  +  + ]:     2547641 :             translate = (opt->translate_columns && opt->translate_columns[c]);
    3842                 :     2547641 :             printTableAddCell(&cont, cell, translate, mustfree);
    3843                 :             :         }
    3844                 :             :     }
    3845                 :             : 
    3846                 :             :     /* set footers */
    3847         [ +  + ]:       99026 :     if (opt->footers)
    3848                 :             :     {
    3849                 :             :         char      **footer;
    3850                 :             : 
    3851         [ +  + ]:         276 :         for (footer = opt->footers; *footer; footer++)
    3852                 :         136 :             printTableAddFooter(&cont, *footer);
    3853                 :             :     }
    3854                 :             : 
    3855                 :       99026 :     printTable(&cont, fout, is_pager, flog);
    3856                 :       99026 :     printTableCleanup(&cont);
    3857                 :             : }
    3858                 :             : 
    3859                 :             : char
    3860                 :      172758 : column_type_alignment(Oid ftype)
    3861                 :             : {
    3862                 :             :     char        align;
    3863                 :             : 
    3864         [ +  + ]:      172758 :     switch (ftype)
    3865                 :             :     {
    3866                 :       67192 :         case INT2OID:
    3867                 :             :         case INT4OID:
    3868                 :             :         case INT8OID:
    3869                 :             :         case FLOAT4OID:
    3870                 :             :         case FLOAT8OID:
    3871                 :             :         case NUMERICOID:
    3872                 :             :         case OIDOID:
    3873                 :             :         case OID8OID:
    3874                 :             :         case XIDOID:
    3875                 :             :         case XID8OID:
    3876                 :             :         case CIDOID:
    3877                 :             :         case MONEYOID:
    3878                 :       67192 :             align = 'r';
    3879                 :       67192 :             break;
    3880                 :      105566 :         default:
    3881                 :      105566 :             align = 'l';
    3882                 :      105566 :             break;
    3883                 :             :     }
    3884                 :      172758 :     return align;
    3885                 :             : }
    3886                 :             : 
    3887                 :             : void
    3888                 :       11629 : setDecimalLocale(void)
    3889                 :             : {
    3890                 :             :     struct lconv *extlconv;
    3891                 :             : 
    3892                 :       11629 :     extlconv = localeconv();
    3893                 :             : 
    3894                 :             :     /* Don't accept an empty decimal_point string */
    3895         [ +  - ]:       11629 :     if (*extlconv->decimal_point)
    3896                 :       11629 :         decimal_point = pg_strdup(extlconv->decimal_point);
    3897                 :             :     else
    3898                 :           0 :         decimal_point = ".";  /* SQL output standard */
    3899                 :             : 
    3900                 :             :     /*
    3901                 :             :      * Although the Open Group standard allows locales to supply more than one
    3902                 :             :      * group width, we consider only the first one, and we ignore any attempt
    3903                 :             :      * to suppress grouping by specifying CHAR_MAX.  As in the backend's
    3904                 :             :      * cash.c, we must apply a range check to avoid being fooled by variant
    3905                 :             :      * CHAR_MAX values.
    3906                 :             :      */
    3907                 :       11629 :     groupdigits = *extlconv->grouping;
    3908   [ +  +  -  + ]:       11629 :     if (groupdigits <= 0 || groupdigits > 6)
    3909                 :       10928 :         groupdigits = 3;        /* most common */
    3910                 :             : 
    3911                 :             :     /* Don't accept an empty thousands_sep string, either */
    3912                 :             :     /* similar code exists in formatting.c */
    3913         [ +  + ]:       11629 :     if (*extlconv->thousands_sep)
    3914                 :         701 :         thousands_sep = pg_strdup(extlconv->thousands_sep);
    3915                 :             :     /* Make sure thousands separator doesn't match decimal point symbol. */
    3916         [ +  - ]:       10928 :     else if (strcmp(decimal_point, ",") != 0)
    3917                 :       10928 :         thousands_sep = ",";
    3918                 :             :     else
    3919                 :           0 :         thousands_sep = ".";
    3920                 :       11629 : }
    3921                 :             : 
    3922                 :             : /* get selected or default line style */
    3923                 :             : const printTextFormat *
    3924                 :       92151 : get_line_style(const printTableOpt *opt)
    3925                 :             : {
    3926                 :             :     /*
    3927                 :             :      * Note: this function mainly exists to preserve the convention that a
    3928                 :             :      * printTableOpt struct can be initialized to zeroes to get default
    3929                 :             :      * behavior.
    3930                 :             :      */
    3931         [ +  + ]:       92151 :     if (opt->line_style != NULL)
    3932                 :        1996 :         return opt->line_style;
    3933                 :             :     else
    3934                 :       90155 :         return &pg_asciiformat;
    3935                 :             : }
    3936                 :             : 
    3937                 :             : void
    3938                 :       11629 : refresh_utf8format(const printTableOpt *opt)
    3939                 :             : {
    3940                 :       11629 :     printTextFormat *popt = &pg_utf8format;
    3941                 :             : 
    3942                 :             :     const unicodeStyleBorderFormat *border;
    3943                 :             :     const unicodeStyleRowFormat *header;
    3944                 :             :     const unicodeStyleColumnFormat *column;
    3945                 :             : 
    3946                 :       11629 :     popt->name = "unicode";
    3947                 :             : 
    3948                 :       11629 :     border = &unicode_style.border_style[opt->unicode_border_linestyle];
    3949                 :       11629 :     header = &unicode_style.row_style[opt->unicode_header_linestyle];
    3950                 :       11629 :     column = &unicode_style.column_style[opt->unicode_column_linestyle];
    3951                 :             : 
    3952                 :       11629 :     popt->lrule[PRINT_RULE_TOP].hrule = border->horizontal;
    3953                 :       11629 :     popt->lrule[PRINT_RULE_TOP].leftvrule = border->down_and_right;
    3954                 :       11629 :     popt->lrule[PRINT_RULE_TOP].midvrule = column->down_and_horizontal[opt->unicode_border_linestyle];
    3955                 :       11629 :     popt->lrule[PRINT_RULE_TOP].rightvrule = border->down_and_left;
    3956                 :             : 
    3957                 :       11629 :     popt->lrule[PRINT_RULE_MIDDLE].hrule = header->horizontal;
    3958                 :       11629 :     popt->lrule[PRINT_RULE_MIDDLE].leftvrule = header->vertical_and_right[opt->unicode_border_linestyle];
    3959                 :       11629 :     popt->lrule[PRINT_RULE_MIDDLE].midvrule = column->vertical_and_horizontal[opt->unicode_header_linestyle];
    3960                 :       11629 :     popt->lrule[PRINT_RULE_MIDDLE].rightvrule = header->vertical_and_left[opt->unicode_border_linestyle];
    3961                 :             : 
    3962                 :       11629 :     popt->lrule[PRINT_RULE_BOTTOM].hrule = border->horizontal;
    3963                 :       11629 :     popt->lrule[PRINT_RULE_BOTTOM].leftvrule = border->up_and_right;
    3964                 :       11629 :     popt->lrule[PRINT_RULE_BOTTOM].midvrule = column->up_and_horizontal[opt->unicode_border_linestyle];
    3965                 :       11629 :     popt->lrule[PRINT_RULE_BOTTOM].rightvrule = border->left_and_right;
    3966                 :             : 
    3967                 :             :     /* N/A */
    3968                 :       11629 :     popt->lrule[PRINT_RULE_DATA].hrule = "";
    3969                 :       11629 :     popt->lrule[PRINT_RULE_DATA].leftvrule = border->vertical;
    3970                 :       11629 :     popt->lrule[PRINT_RULE_DATA].midvrule = column->vertical;
    3971                 :       11629 :     popt->lrule[PRINT_RULE_DATA].rightvrule = border->vertical;
    3972                 :             : 
    3973                 :       11629 :     popt->midvrule_nl = column->vertical;
    3974                 :       11629 :     popt->midvrule_wrap = column->vertical;
    3975                 :       11629 :     popt->midvrule_blank = column->vertical;
    3976                 :             : 
    3977                 :             :     /* Same for all unicode today */
    3978                 :       11629 :     popt->header_nl_left = unicode_style.header_nl_left;
    3979                 :       11629 :     popt->header_nl_right = unicode_style.header_nl_right;
    3980                 :       11629 :     popt->nl_left = unicode_style.nl_left;
    3981                 :       11629 :     popt->nl_right = unicode_style.nl_right;
    3982                 :       11629 :     popt->wrap_left = unicode_style.wrap_left;
    3983                 :       11629 :     popt->wrap_right = unicode_style.wrap_right;
    3984                 :       11629 :     popt->wrap_right_border = unicode_style.wrap_right_border;
    3985                 :       11629 : }
    3986                 :             : 
    3987                 :             : /*
    3988                 :             :  * Compute the byte distance to the end of the string or *target_width
    3989                 :             :  * display character positions, whichever comes first.  Update *target_width
    3990                 :             :  * to be the number of display character positions actually filled.
    3991                 :             :  */
    3992                 :             : static int
    3993                 :      778376 : strlen_max_width(unsigned char *str, int *target_width, int encoding)
    3994                 :             : {
    3995                 :      778376 :     unsigned char *start = str;
    3996                 :      778376 :     unsigned char *end = str + strlen((char *) str);
    3997                 :      778376 :     int         curr_width = 0;
    3998                 :             : 
    3999         [ +  + ]:     9955202 :     while (str < end)
    4000                 :             :     {
    4001                 :     9177857 :         int         char_width = PQdsplen((char *) str, encoding);
    4002                 :             : 
    4003                 :             :         /*
    4004                 :             :          * If the display width of the new character causes the string to
    4005                 :             :          * exceed its target width, skip it and return.  However, if this is
    4006                 :             :          * the first character of the string (curr_width == 0), we have to
    4007                 :             :          * accept it.
    4008                 :             :          */
    4009   [ +  +  +  - ]:     9177857 :         if (*target_width < curr_width + char_width && curr_width != 0)
    4010                 :        1031 :             break;
    4011                 :             : 
    4012                 :     9176826 :         curr_width += char_width;
    4013                 :             : 
    4014                 :     9176826 :         str += PQmblen((char *) str, encoding);
    4015                 :             : 
    4016         [ -  + ]:     9176826 :         if (str > end)           /* Don't overrun invalid string */
    4017                 :           0 :             str = end;
    4018                 :             :     }
    4019                 :             : 
    4020                 :      778376 :     *target_width = curr_width;
    4021                 :             : 
    4022                 :      778376 :     return str - start;
    4023                 :             : }
        

Generated by: LCOV version 2.0-1