LCOV - code coverage report
Current view: top level - src/fe_utils - print.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 92.7 % 1643 1523
Test Date: 2026-07-16 07:15:42 Functions: 100.0 % 49 49
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 83.9 % 1315 1103

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

Generated by: LCOV version 2.0-1