1 /*-------------------------------------------------------------------------
3 * Query-result printing support for frontend code
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.
11 * Portions Copyright (c) 1996-2023, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
14 * src/fe_utils/print.c
16 *-------------------------------------------------------------------------
18 #include "postgres_fe.h"
25 #include <sys/ioctl.h> /* for ioctl() */
32 #include "catalog/pg_type_d.h"
33 #include "fe_utils/mbprint.h"
34 #include "fe_utils/print.h"
37 * If the calling program doesn't have any mechanism for setting
38 * cancel_pressed, it will have no effect.
40 * Note: print.c's general strategy for when to check cancel_pressed is to do
41 * so at completion of each row of output.
43 volatile sig_atomic_t cancel_pressed
= false;
45 static bool always_ignore_sigpipe
= false;
47 /* info for locale-aware numeric formatting; set up by setDecimalLocale() */
48 static char *decimal_point
;
49 static int groupdigits
;
50 static char *thousands_sep
;
52 static char default_footer
[100];
53 static printTableFooter default_footer_cell
= {default_footer
, NULL
};
55 /* Line style control structures */
56 const printTextFormat pg_asciiformat
=
77 const printTextFormat pg_asciiformat_old
=
98 /* Default unicode linestyle format */
99 printTextFormat pg_utf8format
;
101 typedef struct unicodeStyleRowFormat
103 const char *horizontal
;
104 const char *vertical_and_right
[2];
105 const char *vertical_and_left
[2];
106 } unicodeStyleRowFormat
;
108 typedef struct unicodeStyleColumnFormat
110 const char *vertical
;
111 const char *vertical_and_horizontal
[2];
112 const char *up_and_horizontal
[2];
113 const char *down_and_horizontal
[2];
114 } unicodeStyleColumnFormat
;
116 typedef struct unicodeStyleBorderFormat
118 const char *up_and_right
;
119 const char *vertical
;
120 const char *down_and_right
;
121 const char *horizontal
;
122 const char *down_and_left
;
123 const char *left_and_right
;
124 } unicodeStyleBorderFormat
;
126 typedef struct unicodeStyleFormat
128 unicodeStyleRowFormat row_style
[2];
129 unicodeStyleColumnFormat column_style
[2];
130 unicodeStyleBorderFormat border_style
[2];
131 const char *header_nl_left
;
132 const char *header_nl_right
;
134 const char *nl_right
;
135 const char *wrap_left
;
136 const char *wrap_right
;
137 bool wrap_right_border
;
138 } unicodeStyleFormat
;
140 static const unicodeStyleFormat unicode_style
= {
143 /* U+2500 Box Drawings Light Horizontal */
147 * U+251C Box Drawings Light Vertical and Right,
148 * U+255F Box Drawings Vertical Double and Right Single
151 {"\342\224\234", "\342\225\237"},
154 * U+2524 Box Drawings Light Vertical and Left,
155 * U+2562 Box Drawings Vertical Double and Left Single
158 {"\342\224\244", "\342\225\242"},
161 /* U+2550 Box Drawings Double Horizontal */
165 * U+255E Box Drawings Vertical Single and Right Double,
166 * U+2560 Box Drawings Double Vertical and Right
169 {"\342\225\236", "\342\225\240"},
172 * U+2561 Box Drawings Vertical Single and Left Double,
173 * U+2563 Box Drawings Double Vertical and Left
176 {"\342\225\241", "\342\225\243"},
181 /* U+2502 Box Drawings Light Vertical */
185 * U+253C Box Drawings Light Vertical and Horizontal,
186 * U+256A Box Drawings Vertical Single and Horizontal Double
189 {"\342\224\274", "\342\225\252"},
192 * U+2534 Box Drawings Light Up and Horizontal,
193 * U+2567 Box Drawings Up Single and Horizontal Double
196 {"\342\224\264", "\342\225\247"},
199 * U+252C Box Drawings Light Down and Horizontal,
200 * U+2564 Box Drawings Down Single and Horizontal Double
203 {"\342\224\254", "\342\225\244"},
206 /* U+2551 Box Drawings Double Vertical */
210 * U+256B Box Drawings Vertical Double and Horizontal Single,
211 * U+256C Box Drawings Double Vertical and Horizontal
214 {"\342\225\253", "\342\225\254"},
217 * U+2568 Box Drawings Up Double and Horizontal Single,
218 * U+2569 Box Drawings Double Up and Horizontal
221 {"\342\225\250", "\342\225\251"},
224 * U+2565 Box Drawings Down Double and Horizontal Single,
225 * U+2566 Box Drawings Double Down and Horizontal
228 {"\342\225\245", "\342\225\246"},
233 * U+2514 Box Drawings Light Up and Right,
234 * U+2502 Box Drawings Light Vertical,
235 * U+250C Box Drawings Light Down and Right,
236 * U+2500 Box Drawings Light Horizontal,
237 * U+2510 Box Drawings Light Down and Left,
238 * U+2518 Box Drawings Light Up and Left
241 {"\342\224\224", "\342\224\202", "\342\224\214", "\342\224\200", "\342\224\220", "\342\224\230"},
244 * U+255A Box Drawings Double Up and Right,
245 * U+2551 Box Drawings Double Vertical,
246 * U+2554 Box Drawings Double Down and Right,
247 * U+2550 Box Drawings Double Horizontal,
248 * U+2557 Box Drawings Double Down and Left,
249 * U+255D Box Drawings Double Up and Left
252 {"\342\225\232", "\342\225\221", "\342\225\224", "\342\225\220", "\342\225\227", "\342\225\235"},
255 /* U+21B5 Downwards Arrow with Corner Leftwards */
258 /* U+21B5 Downwards Arrow with Corner Leftwards */
260 /* U+2026 Horizontal Ellipsis */
267 /* Local functions */
268 static int strlen_max_width(unsigned char *str
, int *target_width
, int encoding
);
269 static void IsPagerNeeded(const printTableContent
*cont
, int extra_lines
, bool expanded
,
270 FILE **fout
, bool *is_pager
);
272 static void print_aligned_vertical(const printTableContent
*cont
,
273 FILE *fout
, bool is_pager
);
276 /* Count number of digits in integral part of number */
278 integer_digits(const char *my_str
)
280 /* ignoring any sign ... */
281 if (my_str
[0] == '-' || my_str
[0] == '+')
283 /* ... count initial integral digits */
284 return strspn(my_str
, "0123456789");
287 /* Compute additional length required for locale-aware numeric output */
289 additional_numeric_locale_len(const char *my_str
)
291 int int_len
= integer_digits(my_str
),
294 /* Account for added thousands_sep instances */
295 if (int_len
> groupdigits
)
296 len
+= ((int_len
- 1) / groupdigits
) * strlen(thousands_sep
);
298 /* Account for possible additional length of decimal_point */
299 if (strchr(my_str
, '.') != NULL
)
300 len
+= strlen(decimal_point
) - 1;
306 * Format a numeric value per current LC_NUMERIC locale setting
308 * Returns the appropriately formatted string in a new allocated block,
311 * setDecimalLocale() must have been called earlier.
314 format_numeric_locale(const char *my_str
)
324 * If the string doesn't look like a number, return it unchanged. This
325 * check is essential to avoid mangling already-localized "money" values.
327 if (strspn(my_str
, "0123456789+-.eE") != strlen(my_str
))
328 return pg_strdup(my_str
);
330 new_len
= strlen(my_str
) + additional_numeric_locale_len(my_str
);
331 new_str
= pg_malloc(new_len
+ 1);
333 int_len
= integer_digits(my_str
);
335 /* number of digits in first thousands group */
336 leading_digits
= int_len
% groupdigits
;
337 if (leading_digits
== 0)
338 leading_digits
= groupdigits
;
341 if (my_str
[0] == '-' || my_str
[0] == '+')
343 new_str
[new_str_pos
++] = my_str
[0];
347 /* process integer part of number */
348 for (i
= 0; i
< int_len
; i
++)
350 /* Time to insert separator? */
351 if (i
> 0 && --leading_digits
== 0)
353 strcpy(&new_str
[new_str_pos
], thousands_sep
);
354 new_str_pos
+= strlen(thousands_sep
);
355 leading_digits
= groupdigits
;
357 new_str
[new_str_pos
++] = my_str
[i
];
360 /* handle decimal point if any */
361 if (my_str
[i
] == '.')
363 strcpy(&new_str
[new_str_pos
], decimal_point
);
364 new_str_pos
+= strlen(decimal_point
);
368 /* copy the rest (fractional digits and/or exponent, and \0 terminator) */
369 strcpy(&new_str
[new_str_pos
], &my_str
[i
]);
371 /* assert we didn't underestimate new_len (an overestimate is OK) */
372 Assert(strlen(new_str
) <= new_len
);
379 print_separator(struct separator sep
, FILE *fout
)
381 if (sep
.separator_zero
)
383 else if (sep
.separator
)
384 fputs(sep
.separator
, fout
);
389 * Return the list of explicitly-requested footers or, when applicable, the
390 * default "(xx rows)" footer. Always omit the default footer when given
391 * non-default footers, "\pset footer off", or a specific instruction to that
392 * effect from a calling backslash command. Vertical formats number each row,
393 * making the default footer redundant; they do not call this function.
395 * The return value may point to static storage; do not keep it across calls.
397 static printTableFooter
*
398 footers_with_default(const printTableContent
*cont
)
400 if (cont
->footers
== NULL
&& cont
->opt
->default_footer
)
402 unsigned long total_records
;
404 total_records
= cont
->opt
->prior_records
+ cont
->nrows
;
405 snprintf(default_footer
, sizeof(default_footer
),
406 ngettext("(%lu row)", "(%lu rows)", total_records
),
409 return &default_footer_cell
;
412 return cont
->footers
;
416 /*************************/
418 /*************************/
422 print_unaligned_text(const printTableContent
*cont
, FILE *fout
)
424 bool opt_tuples_only
= cont
->opt
->tuples_only
;
426 const char *const *ptr
;
427 bool need_recordsep
= false;
432 if (cont
->opt
->start_table
)
435 if (!opt_tuples_only
&& cont
->title
)
437 fputs(cont
->title
, fout
);
438 print_separator(cont
->opt
->recordSep
, fout
);
442 if (!opt_tuples_only
)
444 for (ptr
= cont
->headers
; *ptr
; ptr
++)
446 if (ptr
!= cont
->headers
)
447 print_separator(cont
->opt
->fieldSep
, fout
);
450 need_recordsep
= true;
454 /* assume continuing printout */
455 need_recordsep
= true;
458 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
462 print_separator(cont
->opt
->recordSep
, fout
);
463 need_recordsep
= false;
469 if ((i
+ 1) % cont
->ncolumns
)
470 print_separator(cont
->opt
->fieldSep
, fout
);
472 need_recordsep
= true;
476 if (cont
->opt
->stop_table
)
478 printTableFooter
*footers
= footers_with_default(cont
);
480 if (!opt_tuples_only
&& footers
!= NULL
&& !cancel_pressed
)
484 for (f
= footers
; f
; f
= f
->next
)
488 print_separator(cont
->opt
->recordSep
, fout
);
489 need_recordsep
= false;
491 fputs(f
->data
, fout
);
492 need_recordsep
= true;
497 * The last record is terminated by a newline, independent of the set
498 * record separator. But when the record separator is a zero byte, we
499 * use that (compatible with find -print0 and xargs).
503 if (cont
->opt
->recordSep
.separator_zero
)
504 print_separator(cont
->opt
->recordSep
, fout
);
513 print_unaligned_vertical(const printTableContent
*cont
, FILE *fout
)
515 bool opt_tuples_only
= cont
->opt
->tuples_only
;
517 const char *const *ptr
;
518 bool need_recordsep
= false;
523 if (cont
->opt
->start_table
)
526 if (!opt_tuples_only
&& cont
->title
)
528 fputs(cont
->title
, fout
);
529 need_recordsep
= true;
533 /* assume continuing printout */
534 need_recordsep
= true;
537 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
541 /* record separator is 2 occurrences of recordsep in this mode */
542 print_separator(cont
->opt
->recordSep
, fout
);
543 print_separator(cont
->opt
->recordSep
, fout
);
544 need_recordsep
= false;
549 fputs(cont
->headers
[i
% cont
->ncolumns
], fout
);
550 print_separator(cont
->opt
->fieldSep
, fout
);
553 if ((i
+ 1) % cont
->ncolumns
)
554 print_separator(cont
->opt
->recordSep
, fout
);
556 need_recordsep
= true;
559 if (cont
->opt
->stop_table
)
562 if (!opt_tuples_only
&& cont
->footers
!= NULL
&& !cancel_pressed
)
566 print_separator(cont
->opt
->recordSep
, fout
);
567 for (f
= cont
->footers
; f
; f
= f
->next
)
569 print_separator(cont
->opt
->recordSep
, fout
);
570 fputs(f
->data
, fout
);
574 /* see above in print_unaligned_text() */
577 if (cont
->opt
->recordSep
.separator_zero
)
578 print_separator(cont
->opt
->recordSep
, fout
);
586 /********************/
588 /********************/
593 _print_horizontal_line(const unsigned int ncolumns
, const unsigned int *widths
,
594 unsigned short border
, printTextRule pos
,
595 const printTextFormat
*format
,
598 const printTextLineFormat
*lformat
= &format
->lrule
[pos
];
603 fputs(lformat
->hrule
, fout
);
604 else if (border
== 2)
605 fprintf(fout
, "%s%s", lformat
->leftvrule
, lformat
->hrule
);
607 for (i
= 0; i
< ncolumns
; i
++)
609 for (j
= 0; j
< widths
[i
]; j
++)
610 fputs(lformat
->hrule
, fout
);
612 if (i
< ncolumns
- 1)
617 fprintf(fout
, "%s%s%s", lformat
->hrule
,
618 lformat
->midvrule
, lformat
->hrule
);
623 fprintf(fout
, "%s%s", lformat
->hrule
, lformat
->rightvrule
);
624 else if (border
== 1)
625 fputs(lformat
->hrule
, fout
);
632 * Print pretty boxes around cells.
635 print_aligned_text(const printTableContent
*cont
, FILE *fout
, bool is_pager
)
637 bool opt_tuples_only
= cont
->opt
->tuples_only
;
638 int encoding
= cont
->opt
->encoding
;
639 unsigned short opt_border
= cont
->opt
->border
;
640 const printTextFormat
*format
= get_line_style(cont
->opt
);
641 const printTextLineFormat
*dformat
= &format
->lrule
[PRINT_RULE_DATA
];
643 unsigned int col_count
= 0,
649 unsigned int *width_header
,
653 unsigned int *max_nl_lines
, /* value split by newlines */
656 unsigned char **format_buf
;
657 unsigned int width_total
;
658 unsigned int total_header_width
;
659 unsigned int extra_row_output_lines
= 0;
660 unsigned int extra_output_lines
= 0;
662 const char *const *ptr
;
664 struct lineptr
**col_lineptrs
; /* pointers to line pointer per column */
666 bool *header_done
; /* Have all header lines been output? */
667 int *bytes_output
; /* Bytes output for column value */
668 printTextLineWrap
*wrap
; /* Wrap status for each column */
669 int output_columns
= 0; /* Width of interactive console */
670 bool is_local_pager
= false;
678 if (cont
->ncolumns
> 0)
680 col_count
= cont
->ncolumns
;
681 width_header
= pg_malloc0(col_count
* sizeof(*width_header
));
682 width_average
= pg_malloc0(col_count
* sizeof(*width_average
));
683 max_width
= pg_malloc0(col_count
* sizeof(*max_width
));
684 width_wrap
= pg_malloc0(col_count
* sizeof(*width_wrap
));
685 max_nl_lines
= pg_malloc0(col_count
* sizeof(*max_nl_lines
));
686 curr_nl_line
= pg_malloc0(col_count
* sizeof(*curr_nl_line
));
687 col_lineptrs
= pg_malloc0(col_count
* sizeof(*col_lineptrs
));
688 max_bytes
= pg_malloc0(col_count
* sizeof(*max_bytes
));
689 format_buf
= pg_malloc0(col_count
* sizeof(*format_buf
));
690 header_done
= pg_malloc0(col_count
* sizeof(*header_done
));
691 bytes_output
= pg_malloc0(col_count
* sizeof(*bytes_output
));
692 wrap
= pg_malloc0(col_count
* sizeof(*wrap
));
697 width_average
= NULL
;
710 /* scan all column headers, find maximum width and max max_nl_lines */
711 for (i
= 0; i
< col_count
; i
++)
717 pg_wcssize((const unsigned char *) cont
->headers
[i
], strlen(cont
->headers
[i
]),
718 encoding
, &width
, &nl_lines
, &bytes_required
);
719 if (width
> max_width
[i
])
720 max_width
[i
] = width
;
721 if (nl_lines
> max_nl_lines
[i
])
722 max_nl_lines
[i
] = nl_lines
;
723 if (bytes_required
> max_bytes
[i
])
724 max_bytes
[i
] = bytes_required
;
725 if (nl_lines
> extra_row_output_lines
)
726 extra_row_output_lines
= nl_lines
;
728 width_header
[i
] = width
;
730 /* Add height of tallest header column */
731 extra_output_lines
+= extra_row_output_lines
;
732 extra_row_output_lines
= 0;
734 /* scan all cells, find maximum width, compute cell_count */
735 for (i
= 0, ptr
= cont
->cells
; *ptr
; ptr
++, i
++, cell_count
++)
741 pg_wcssize((const unsigned char *) *ptr
, strlen(*ptr
), encoding
,
742 &width
, &nl_lines
, &bytes_required
);
744 if (width
> max_width
[i
% col_count
])
745 max_width
[i
% col_count
] = width
;
746 if (nl_lines
> max_nl_lines
[i
% col_count
])
747 max_nl_lines
[i
% col_count
] = nl_lines
;
748 if (bytes_required
> max_bytes
[i
% col_count
])
749 max_bytes
[i
% col_count
] = bytes_required
;
751 width_average
[i
% col_count
] += width
;
754 /* If we have rows, compute average */
755 if (col_count
!= 0 && cell_count
!= 0)
757 int rows
= cell_count
/ col_count
;
759 for (i
= 0; i
< col_count
; i
++)
760 width_average
[i
] /= rows
;
763 /* adjust the total display width based on border style */
765 width_total
= col_count
;
766 else if (opt_border
== 1)
767 width_total
= col_count
* 3 - ((col_count
> 0) ? 1 : 0);
769 width_total
= col_count
* 3 + 1;
770 total_header_width
= width_total
;
772 for (i
= 0; i
< col_count
; i
++)
774 width_total
+= max_width
[i
];
775 total_header_width
+= width_header
[i
];
779 * At this point: max_width[] contains the max width of each column,
780 * max_nl_lines[] contains the max number of lines in each column,
781 * max_bytes[] contains the maximum storage space for formatting strings,
782 * width_total contains the giant width sum. Now we allocate some memory
785 for (i
= 0; i
< col_count
; i
++)
787 /* Add entry for ptr == NULL array termination */
788 col_lineptrs
[i
] = pg_malloc0((max_nl_lines
[i
] + 1) *
789 sizeof(**col_lineptrs
));
791 format_buf
[i
] = pg_malloc(max_bytes
[i
] + 1);
793 col_lineptrs
[i
]->ptr
= format_buf
[i
];
796 /* Default word wrap to the full width, i.e. no word wrap */
797 for (i
= 0; i
< col_count
; i
++)
798 width_wrap
[i
] = max_width
[i
];
801 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
803 if (cont
->opt
->columns
> 0)
804 output_columns
= cont
->opt
->columns
;
805 else if ((fout
== stdout
&& isatty(fileno(stdout
))) || is_pager
)
807 if (cont
->opt
->env_columns
> 0)
808 output_columns
= cont
->opt
->env_columns
;
812 struct winsize screen_size
;
814 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &screen_size
) != -1)
815 output_columns
= screen_size
.ws_col
;
820 if (cont
->opt
->format
== PRINT_WRAPPED
)
823 * Optional optimized word wrap. Shrink columns with a high max/avg
824 * ratio. Slightly bias against wider columns. (Increases chance a
825 * narrow column will fit in its cell.) If available columns is
826 * positive... and greater than the width of the unshrinkable column
829 if (output_columns
> 0 && output_columns
>= total_header_width
)
831 /* While there is still excess width... */
832 while (width_total
> output_columns
)
834 double max_ratio
= 0;
838 * Find column that has the highest ratio of its maximum width
839 * compared to its average width. This tells us which column
840 * will produce the fewest wrapped values if shortened.
841 * width_wrap starts as equal to max_width.
843 for (i
= 0; i
< col_count
; i
++)
845 if (width_average
[i
] && width_wrap
[i
] > width_header
[i
])
847 /* Penalize wide columns by 1% of their width */
850 ratio
= (double) width_wrap
[i
] / width_average
[i
] +
852 if (ratio
> max_ratio
)
860 /* Exit loop if we can't squeeze any more. */
864 /* Decrease width of target column by one. */
865 width_wrap
[worst_col
]--;
872 * If in expanded auto mode, we have now calculated the expected width, so
873 * we can now escape to vertical mode if necessary. If the output has
874 * only one column, the expanded format would be wider than the regular
875 * format, so don't use it in that case.
877 if (cont
->opt
->expanded
== 2 && output_columns
> 0 && cont
->ncolumns
> 1 &&
878 (output_columns
< total_header_width
|| output_columns
< width_total
))
880 print_aligned_vertical(cont
, fout
, is_pager
);
884 /* If we wrapped beyond the display width, use the pager */
885 if (!is_pager
&& fout
== stdout
&& output_columns
> 0 &&
886 (output_columns
< total_header_width
|| output_columns
< width_total
))
888 fout
= PageOutput(INT_MAX
, cont
->opt
); /* force pager */
889 is_pager
= is_local_pager
= true;
892 /* Check if newlines or our wrapping now need the pager */
893 if (!is_pager
&& fout
== stdout
)
895 /* scan all cells, find maximum width, compute cell_count */
896 for (i
= 0, ptr
= cont
->cells
; *ptr
; ptr
++, cell_count
++)
902 pg_wcssize((const unsigned char *) *ptr
, strlen(*ptr
), encoding
,
903 &width
, &nl_lines
, &bytes_required
);
906 * A row can have both wrapping and newlines that cause it to
907 * display across multiple lines. We check for both cases below.
909 if (width
> 0 && width_wrap
[i
])
911 unsigned int extra_lines
;
913 /* don't count the first line of nl_lines - it's not "extra" */
914 extra_lines
= ((width
- 1) / width_wrap
[i
]) + nl_lines
- 1;
915 if (extra_lines
> extra_row_output_lines
)
916 extra_row_output_lines
= extra_lines
;
919 /* i is the current column number: increment with wrap */
920 if (++i
>= col_count
)
923 /* At last column of each row, add tallest column height */
924 extra_output_lines
+= extra_row_output_lines
;
925 extra_row_output_lines
= 0;
928 IsPagerNeeded(cont
, extra_output_lines
, false, &fout
, &is_pager
);
929 is_local_pager
= is_pager
;
933 if (cont
->opt
->start_table
)
936 if (cont
->title
&& !opt_tuples_only
)
941 pg_wcssize((const unsigned char *) cont
->title
, strlen(cont
->title
),
942 encoding
, &width
, &height
, NULL
);
943 if (width
>= width_total
)
945 fprintf(fout
, "%s\n", cont
->title
);
948 fprintf(fout
, "%-*s%s\n", (width_total
- width
) / 2, "",
953 if (!opt_tuples_only
)
955 int more_col_wrapping
;
959 _print_horizontal_line(col_count
, width_wrap
, opt_border
,
960 PRINT_RULE_TOP
, format
, fout
);
962 for (i
= 0; i
< col_count
; i
++)
963 pg_wcsformat((const unsigned char *) cont
->headers
[i
],
964 strlen(cont
->headers
[i
]), encoding
,
965 col_lineptrs
[i
], max_nl_lines
[i
]);
967 more_col_wrapping
= col_count
;
970 memset(header_done
, false, col_count
* sizeof(bool));
971 while (more_col_wrapping
)
974 fputs(dformat
->leftvrule
, fout
);
976 for (i
= 0; i
< cont
->ncolumns
; i
++)
978 struct lineptr
*this_line
= col_lineptrs
[i
] + curr_nl_line
;
979 unsigned int nbspace
;
981 if (opt_border
!= 0 ||
982 (!format
->wrap_right_border
&& i
> 0))
983 fputs(curr_nl_line
? format
->header_nl_left
: " ",
988 nbspace
= width_wrap
[i
] - this_line
->width
;
991 fprintf(fout
, "%-*s%s%-*s",
992 nbspace
/ 2, "", this_line
->ptr
, (nbspace
+ 1) / 2, "");
994 if (!(this_line
+ 1)->ptr
)
1001 fprintf(fout
, "%*s", width_wrap
[i
], "");
1003 if (opt_border
!= 0 || format
->wrap_right_border
)
1004 fputs(!header_done
[i
] ? format
->header_nl_right
: " ",
1007 if (opt_border
!= 0 && col_count
> 0 && i
< col_count
- 1)
1008 fputs(dformat
->midvrule
, fout
);
1012 if (opt_border
== 2)
1013 fputs(dformat
->rightvrule
, fout
);
1017 _print_horizontal_line(col_count
, width_wrap
, opt_border
,
1018 PRINT_RULE_MIDDLE
, format
, fout
);
1022 /* print cells, one loop per row */
1023 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
+= col_count
, ptr
+= col_count
)
1033 for (j
= 0; j
< col_count
; j
++)
1035 pg_wcsformat((const unsigned char *) ptr
[j
], strlen(ptr
[j
]), encoding
,
1036 col_lineptrs
[j
], max_nl_lines
[j
]);
1037 curr_nl_line
[j
] = 0;
1040 memset(bytes_output
, 0, col_count
* sizeof(int));
1043 * Each time through this loop, one display line is output. It can
1044 * either be a full value or a partial value if embedded newlines
1045 * exist or if 'format=wrapping' mode is enabled.
1052 if (opt_border
== 2)
1053 fputs(dformat
->leftvrule
, fout
);
1055 /* for each column */
1056 for (j
= 0; j
< col_count
; j
++)
1058 /* We have a valid array element, so index it */
1059 struct lineptr
*this_line
= &col_lineptrs
[j
][curr_nl_line
[j
]];
1060 int bytes_to_output
;
1061 int chars_to_output
= width_wrap
[j
];
1062 bool finalspaces
= (opt_border
== 2 ||
1063 (col_count
> 0 && j
< col_count
- 1));
1065 /* Print left-hand wrap or newline mark */
1066 if (opt_border
!= 0)
1068 if (wrap
[j
] == PRINT_LINE_WRAP_WRAP
)
1069 fputs(format
->wrap_left
, fout
);
1070 else if (wrap
[j
] == PRINT_LINE_WRAP_NEWLINE
)
1071 fputs(format
->nl_left
, fout
);
1076 if (!this_line
->ptr
)
1078 /* Past newline lines so just pad for other columns */
1080 fprintf(fout
, "%*s", chars_to_output
, "");
1084 /* Get strlen() of the characters up to width_wrap */
1086 strlen_max_width(this_line
->ptr
+ bytes_output
[j
],
1087 &chars_to_output
, encoding
);
1090 * If we exceeded width_wrap, it means the display width
1091 * of a single character was wider than our target width.
1092 * In that case, we have to pretend we are only printing
1093 * the target display width and make the best of it.
1095 if (chars_to_output
> width_wrap
[j
])
1096 chars_to_output
= width_wrap
[j
];
1098 if (cont
->aligns
[j
] == 'r') /* Right aligned cell */
1101 fprintf(fout
, "%*s", width_wrap
[j
] - chars_to_output
, "");
1102 fwrite((char *) (this_line
->ptr
+ bytes_output
[j
]),
1103 1, bytes_to_output
, fout
);
1105 else /* Left aligned cell */
1108 fwrite((char *) (this_line
->ptr
+ bytes_output
[j
]),
1109 1, bytes_to_output
, fout
);
1112 bytes_output
[j
] += bytes_to_output
;
1114 /* Do we have more text to wrap? */
1115 if (*(this_line
->ptr
+ bytes_output
[j
]) != '\0')
1119 /* Advance to next newline line */
1121 if (col_lineptrs
[j
][curr_nl_line
[j
]].ptr
!= NULL
)
1123 bytes_output
[j
] = 0;
1127 /* Determine next line's wrap status for this column */
1128 wrap
[j
] = PRINT_LINE_WRAP_NONE
;
1129 if (col_lineptrs
[j
][curr_nl_line
[j
]].ptr
!= NULL
)
1131 if (bytes_output
[j
] != 0)
1132 wrap
[j
] = PRINT_LINE_WRAP_WRAP
;
1133 else if (curr_nl_line
[j
] != 0)
1134 wrap
[j
] = PRINT_LINE_WRAP_NEWLINE
;
1138 * If left-aligned, pad out remaining space if needed (not
1139 * last column, and/or wrap marks required).
1141 if (cont
->aligns
[j
] != 'r') /* Left aligned cell */
1144 wrap
[j
] == PRINT_LINE_WRAP_WRAP
||
1145 wrap
[j
] == PRINT_LINE_WRAP_NEWLINE
)
1146 fprintf(fout
, "%*s",
1147 width_wrap
[j
] - chars_to_output
, "");
1150 /* Print right-hand wrap or newline mark */
1151 if (wrap
[j
] == PRINT_LINE_WRAP_WRAP
)
1152 fputs(format
->wrap_right
, fout
);
1153 else if (wrap
[j
] == PRINT_LINE_WRAP_NEWLINE
)
1154 fputs(format
->nl_right
, fout
);
1155 else if (opt_border
== 2 || (col_count
> 0 && j
< col_count
- 1))
1158 /* Print column divider, if not the last column */
1159 if (opt_border
!= 0 && (col_count
> 0 && j
< col_count
- 1))
1161 if (wrap
[j
+ 1] == PRINT_LINE_WRAP_WRAP
)
1162 fputs(format
->midvrule_wrap
, fout
);
1163 else if (wrap
[j
+ 1] == PRINT_LINE_WRAP_NEWLINE
)
1164 fputs(format
->midvrule_nl
, fout
);
1165 else if (col_lineptrs
[j
+ 1][curr_nl_line
[j
+ 1]].ptr
== NULL
)
1166 fputs(format
->midvrule_blank
, fout
);
1168 fputs(dformat
->midvrule
, fout
);
1172 /* end-of-row border */
1173 if (opt_border
== 2)
1174 fputs(dformat
->rightvrule
, fout
);
1176 } while (more_lines
);
1179 if (cont
->opt
->stop_table
)
1181 printTableFooter
*footers
= footers_with_default(cont
);
1183 if (opt_border
== 2 && !cancel_pressed
)
1184 _print_horizontal_line(col_count
, width_wrap
, opt_border
,
1185 PRINT_RULE_BOTTOM
, format
, fout
);
1188 if (footers
&& !opt_tuples_only
&& !cancel_pressed
)
1190 printTableFooter
*f
;
1192 for (f
= footers
; f
; f
= f
->next
)
1193 fprintf(fout
, "%s\n", f
->data
);
1201 for (i
= 0; i
< col_count
; i
++)
1203 free(col_lineptrs
[i
]);
1204 free(format_buf
[i
]);
1207 free(width_average
);
1225 print_aligned_vertical_line(const printTableOpt
*topt
,
1226 unsigned long record
,
1227 unsigned int hwidth
,
1228 unsigned int dwidth
,
1233 const printTextLineFormat
*lformat
= &get_line_style(topt
)->lrule
[pos
];
1234 const unsigned short opt_border
= topt
->border
;
1238 if (opt_border
== 2)
1239 fprintf(fout
, "%s%s", lformat
->leftvrule
, lformat
->hrule
);
1240 else if (opt_border
== 1)
1241 fputs(lformat
->hrule
, fout
);
1245 if (opt_border
== 0)
1246 reclen
= fprintf(fout
, "* Record %lu", record
);
1248 reclen
= fprintf(fout
, "[ RECORD %lu ]", record
);
1250 if (opt_border
!= 2)
1254 for (i
= reclen
; i
< hwidth
; i
++)
1255 fputs(opt_border
> 0 ? lformat
->hrule
: " ", fout
);
1261 fputs(lformat
->hrule
, fout
);
1264 if (topt
->expanded_header_width_type
== PRINT_XHEADER_COLUMN
)
1266 fputs(lformat
->rightvrule
, fout
);
1270 fputs(lformat
->midvrule
, fout
);
1274 && topt
->expanded_header_width_type
!= PRINT_XHEADER_COLUMN
)
1275 fputs(lformat
->hrule
, fout
);
1283 if (topt
->expanded_header_width_type
!= PRINT_XHEADER_COLUMN
)
1285 if (topt
->expanded_header_width_type
== PRINT_XHEADER_PAGE
1286 || topt
->expanded_header_width_type
== PRINT_XHEADER_EXACT_WIDTH
)
1288 if (topt
->expanded_header_width_type
== PRINT_XHEADER_EXACT_WIDTH
)
1290 output_columns
= topt
->expanded_header_exact_width
;
1292 if (output_columns
> 0)
1294 if (opt_border
== 0)
1295 dwidth
= Min(dwidth
, Max(0, (int) (output_columns
- hwidth
)));
1296 if (opt_border
== 1)
1297 dwidth
= Min(dwidth
, Max(0, (int) (output_columns
- hwidth
- 3)));
1300 * Handling the xheader width for border=2 doesn't make much
1301 * sense because this format has an additional right border,
1302 * but keep this for consistency.
1304 if (opt_border
== 2)
1305 dwidth
= Min(dwidth
, Max(0, (int) (output_columns
- hwidth
- 7)));
1311 if (dwidth
< reclen
)
1314 for (i
= reclen
; i
< dwidth
; i
++)
1315 fputs(opt_border
> 0 ? lformat
->hrule
: " ", fout
);
1316 if (opt_border
== 2)
1317 fprintf(fout
, "%s%s", lformat
->hrule
, lformat
->rightvrule
);
1324 print_aligned_vertical(const printTableContent
*cont
,
1325 FILE *fout
, bool is_pager
)
1327 bool opt_tuples_only
= cont
->opt
->tuples_only
;
1328 unsigned short opt_border
= cont
->opt
->border
;
1329 const printTextFormat
*format
= get_line_style(cont
->opt
);
1330 const printTextLineFormat
*dformat
= &format
->lrule
[PRINT_RULE_DATA
];
1331 int encoding
= cont
->opt
->encoding
;
1332 unsigned long record
= cont
->opt
->prior_records
+ 1;
1333 const char *const *ptr
;
1341 struct lineptr
*hlineptr
,
1343 bool is_local_pager
= false,
1346 int output_columns
= 0; /* Width of interactive console */
1354 if (cont
->cells
[0] == NULL
&& cont
->opt
->start_table
&&
1355 cont
->opt
->stop_table
)
1357 printTableFooter
*footers
= footers_with_default(cont
);
1359 if (!opt_tuples_only
&& !cancel_pressed
&& footers
)
1361 printTableFooter
*f
;
1363 for (f
= footers
; f
; f
= f
->next
)
1364 fprintf(fout
, "%s\n", f
->data
);
1373 * Deal with the pager here instead of in printTable(), because we could
1374 * get here via print_aligned_text() in expanded auto mode, and so we have
1375 * to recalculate the pager requirement based on vertical output.
1379 IsPagerNeeded(cont
, 0, true, &fout
, &is_pager
);
1380 is_local_pager
= is_pager
;
1383 /* Find the maximum dimensions for the headers */
1384 for (i
= 0; i
< cont
->ncolumns
; i
++)
1390 pg_wcssize((const unsigned char *) cont
->headers
[i
], strlen(cont
->headers
[i
]),
1391 encoding
, &width
, &height
, &fs
);
1394 if (height
> hheight
)
1399 if (fs
> hformatsize
)
1403 /* find longest data cell */
1404 for (i
= 0, ptr
= cont
->cells
; *ptr
; ptr
++, i
++)
1410 pg_wcssize((const unsigned char *) *ptr
, strlen(*ptr
), encoding
,
1411 &width
, &height
, &fs
);
1414 if (height
> dheight
)
1419 if (fs
> dformatsize
)
1424 * We now have all the information we need to setup the formatting
1427 dlineptr
= pg_malloc((sizeof(*dlineptr
)) * (dheight
+ 1));
1428 hlineptr
= pg_malloc((sizeof(*hlineptr
)) * (hheight
+ 1));
1430 dlineptr
->ptr
= pg_malloc(dformatsize
);
1431 hlineptr
->ptr
= pg_malloc(hformatsize
);
1433 if (cont
->opt
->start_table
)
1436 if (!opt_tuples_only
&& cont
->title
)
1437 fprintf(fout
, "%s\n", cont
->title
);
1441 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
1443 if (cont
->opt
->columns
> 0)
1444 output_columns
= cont
->opt
->columns
;
1445 else if ((fout
== stdout
&& isatty(fileno(stdout
))) || is_pager
)
1447 if (cont
->opt
->env_columns
> 0)
1448 output_columns
= cont
->opt
->env_columns
;
1452 struct winsize screen_size
;
1454 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &screen_size
) != -1)
1455 output_columns
= screen_size
.ws_col
;
1461 * Calculate available width for data in wrapped mode
1463 if (cont
->opt
->format
== PRINT_WRAPPED
)
1465 unsigned int swidth
,
1469 if (opt_border
== 0)
1472 * For border = 0, one space in the middle. (If we discover we
1473 * need to wrap, the spacer column will be replaced by a wrap
1474 * marker, and we'll make room below for another wrap marker at
1475 * the end of the line. But for now, assume no wrap is needed.)
1479 /* We might need a column for header newline markers, too */
1483 else if (opt_border
== 1)
1486 * For border = 1, two spaces and a vrule in the middle. (As
1487 * above, we might need one more column for a wrap marker.)
1491 /* We might need a column for left header newline markers, too */
1492 if (hmultiline
&& (format
== &pg_asciiformat_old
))
1498 * For border = 2, two more for the vrules at the beginning and
1499 * end of the lines, plus spacer columns adjacent to these. (We
1500 * won't need extra columns for wrap/newline markers, we'll just
1501 * repurpose the spacers.)
1506 /* Reserve a column for data newline indicators, too, if needed */
1508 opt_border
< 2 && format
!= &pg_asciiformat_old
)
1511 /* Determine width required for record header lines */
1512 if (!opt_tuples_only
)
1514 if (cont
->nrows
> 0)
1515 rwidth
= 1 + (int) log10(cont
->nrows
);
1516 if (opt_border
== 0)
1517 rwidth
+= 9; /* "* RECORD " */
1518 else if (opt_border
== 1)
1519 rwidth
+= 12; /* "-[ RECORD ]" */
1521 rwidth
+= 15; /* "+-[ RECORD ]-+" */
1524 /* We might need to do the rest of the calculation twice */
1529 /* Total width required to not wrap data */
1530 width
= hwidth
+ swidth
+ dwidth
;
1531 /* ... and not the header lines, either */
1535 if (output_columns
> 0)
1537 unsigned int min_width
;
1539 /* Minimum acceptable width: room for just 3 columns of data */
1540 min_width
= hwidth
+ swidth
+ 3;
1541 /* ... but not less than what the record header lines need */
1542 if (min_width
< rwidth
)
1545 if (output_columns
>= width
)
1547 /* Plenty of room, use native data width */
1548 /* (but at least enough for the record header lines) */
1549 newdwidth
= width
- hwidth
- swidth
;
1551 else if (output_columns
< min_width
)
1553 /* Set data width to match min_width */
1554 newdwidth
= min_width
- hwidth
- swidth
;
1558 /* Set data width to match output_columns */
1559 newdwidth
= output_columns
- hwidth
- swidth
;
1564 /* Don't know the wrap limit, so use native data width */
1565 /* (but at least enough for the record header lines) */
1566 newdwidth
= width
- hwidth
- swidth
;
1570 * If we will need to wrap data and didn't already allocate a data
1571 * newline/wrap marker column, do so and recompute.
1573 if (newdwidth
< dwidth
&& !dmultiline
&&
1574 opt_border
< 2 && format
!= &pg_asciiformat_old
)
1587 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
1601 pos
= PRINT_RULE_TOP
;
1603 pos
= PRINT_RULE_MIDDLE
;
1605 /* Print record header (e.g. "[ RECORD N ]") above each record */
1606 if (i
% cont
->ncolumns
== 0)
1608 unsigned int lhwidth
= hwidth
;
1610 if ((opt_border
< 2) &&
1612 (format
== &pg_asciiformat_old
))
1613 lhwidth
++; /* for newline indicators */
1615 if (!opt_tuples_only
)
1616 print_aligned_vertical_line(cont
->opt
, record
++,
1617 lhwidth
, dwidth
, output_columns
,
1619 else if (i
!= 0 || !cont
->opt
->start_table
|| opt_border
== 2)
1620 print_aligned_vertical_line(cont
->opt
, 0, lhwidth
,
1621 dwidth
, output_columns
, pos
, fout
);
1624 /* Format the header */
1625 pg_wcsformat((const unsigned char *) cont
->headers
[i
% cont
->ncolumns
],
1626 strlen(cont
->headers
[i
% cont
->ncolumns
]),
1627 encoding
, hlineptr
, hheight
);
1628 /* Format the data */
1629 pg_wcsformat((const unsigned char *) *ptr
, strlen(*ptr
), encoding
,
1633 * Loop through header and data in parallel dealing with newlines and
1634 * wrapped lines until they're both exhausted
1637 dcomplete
= hcomplete
= 0;
1639 chars_to_output
= dlineptr
[dline
].width
;
1640 while (!dcomplete
|| !hcomplete
)
1643 if (opt_border
== 2)
1644 fprintf(fout
, "%s", dformat
->leftvrule
);
1646 /* Header (never wrapped so just need to deal with newlines) */
1649 int swidth
= hwidth
,
1650 target_width
= hwidth
;
1653 * Left spacer or new line indicator
1655 if ((opt_border
== 2) ||
1656 (hmultiline
&& (format
== &pg_asciiformat_old
)))
1657 fputs(hline
? format
->header_nl_left
: " ", fout
);
1662 strlen_max_width(hlineptr
[hline
].ptr
, &target_width
,
1664 fprintf(fout
, "%-s", hlineptr
[hline
].ptr
);
1669 swidth
-= target_width
;
1671 fprintf(fout
, "%*s", swidth
, " ");
1674 * New line indicator or separator's space
1676 if (hlineptr
[hline
+ 1].ptr
)
1678 /* More lines after this one due to a newline */
1679 if ((opt_border
> 0) ||
1680 (hmultiline
&& (format
!= &pg_asciiformat_old
)))
1681 fputs(format
->header_nl_right
, fout
);
1686 /* This was the last line of the header */
1687 if ((opt_border
> 0) ||
1688 (hmultiline
&& (format
!= &pg_asciiformat_old
)))
1695 unsigned int swidth
= hwidth
+ opt_border
;
1697 if ((opt_border
< 2) &&
1699 (format
== &pg_asciiformat_old
))
1702 if ((opt_border
== 0) &&
1703 (format
!= &pg_asciiformat_old
) &&
1707 fprintf(fout
, "%*s", swidth
, " ");
1714 fputs(format
->midvrule_wrap
, fout
);
1715 else if (dline
== 0)
1716 fputs(dformat
->midvrule
, fout
);
1718 fputs(format
->midvrule_nl
, fout
);
1724 int target_width
= dwidth
,
1729 * Left spacer or wrap indicator
1731 fputs(offset
== 0 ? " " : format
->wrap_left
, fout
);
1736 bytes_to_output
= strlen_max_width(dlineptr
[dline
].ptr
+ offset
,
1737 &target_width
, encoding
);
1738 fwrite((char *) (dlineptr
[dline
].ptr
+ offset
),
1739 1, bytes_to_output
, fout
);
1741 chars_to_output
-= target_width
;
1742 offset
+= bytes_to_output
;
1745 swidth
-= target_width
;
1747 if (chars_to_output
)
1749 /* continuing a wrapped column */
1750 if ((opt_border
> 1) ||
1751 (dmultiline
&& (format
!= &pg_asciiformat_old
)))
1754 fprintf(fout
, "%*s", swidth
, " ");
1755 fputs(format
->wrap_right
, fout
);
1758 else if (dlineptr
[dline
+ 1].ptr
)
1760 /* reached a newline in the column */
1761 if ((opt_border
> 1) ||
1762 (dmultiline
&& (format
!= &pg_asciiformat_old
)))
1765 fprintf(fout
, "%*s", swidth
, " ");
1766 fputs(format
->nl_right
, fout
);
1770 chars_to_output
= dlineptr
[dline
].width
;
1774 /* reached the end of the cell */
1778 fprintf(fout
, "%*s", swidth
, " ");
1785 if (opt_border
== 2)
1786 fputs(dformat
->rightvrule
, fout
);
1793 * data exhausted (this can occur if header is longer than the
1794 * data due to newlines in the header)
1799 fprintf(fout
, "%*s %s\n", dwidth
, "", dformat
->rightvrule
);
1804 if (cont
->opt
->stop_table
)
1806 if (opt_border
== 2 && !cancel_pressed
)
1807 print_aligned_vertical_line(cont
->opt
, 0, hwidth
, dwidth
,
1808 output_columns
, PRINT_RULE_BOTTOM
, fout
);
1811 if (!opt_tuples_only
&& cont
->footers
!= NULL
&& !cancel_pressed
)
1813 printTableFooter
*f
;
1817 for (f
= cont
->footers
; f
; f
= f
->next
)
1818 fprintf(fout
, "%s\n", f
->data
);
1824 free(hlineptr
->ptr
);
1825 free(dlineptr
->ptr
);
1834 /**********************/
1836 /**********************/
1840 csv_escaped_print(const char *str
, FILE *fout
)
1845 for (p
= str
; *p
; p
++)
1848 fputc('"', fout
); /* double quotes are doubled */
1855 csv_print_field(const char *str
, FILE *fout
, char sep
)
1858 * Enclose and escape field contents when one of these conditions is met:
1859 * - the field separator is found in the contents.
1860 * - the field contains a CR or LF.
1861 * - the field contains a double quote.
1862 * - the field is exactly "\.".
1863 * - the field separator is either "\" or ".".
1864 * The last two cases prevent producing a line that the server's COPY
1865 * command would interpret as an end-of-data marker. We only really
1866 * need to ensure that the complete line isn't exactly "\.", but for
1867 * simplicity we apply stronger restrictions here.
1870 if (strchr(str
, sep
) != NULL
||
1871 strcspn(str
, "\r\n\"") != strlen(str
) ||
1872 strcmp(str
, "\\.") == 0 ||
1873 sep
== '\\' || sep
== '.')
1874 csv_escaped_print(str
, fout
);
1880 print_csv_text(const printTableContent
*cont
, FILE *fout
)
1882 const char *const *ptr
;
1889 * The title and footer are never printed in csv format. The header is
1890 * printed if opt_tuples_only is false.
1892 * Despite RFC 4180 saying that end of lines are CRLF, terminate lines
1893 * with '\n', which prints out as the system-dependent EOL string in text
1894 * mode (typically LF on Unix and CRLF on Windows).
1896 if (cont
->opt
->start_table
&& !cont
->opt
->tuples_only
)
1899 for (ptr
= cont
->headers
; *ptr
; ptr
++)
1901 if (ptr
!= cont
->headers
)
1902 fputc(cont
->opt
->csvFieldSep
[0], fout
);
1903 csv_print_field(*ptr
, fout
, cont
->opt
->csvFieldSep
[0]);
1909 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
1911 csv_print_field(*ptr
, fout
, cont
->opt
->csvFieldSep
[0]);
1912 if ((i
+ 1) % cont
->ncolumns
)
1913 fputc(cont
->opt
->csvFieldSep
[0], fout
);
1920 print_csv_vertical(const printTableContent
*cont
, FILE *fout
)
1922 const char *const *ptr
;
1926 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
1931 /* print name of column */
1932 csv_print_field(cont
->headers
[i
% cont
->ncolumns
], fout
,
1933 cont
->opt
->csvFieldSep
[0]);
1935 /* print field separator */
1936 fputc(cont
->opt
->csvFieldSep
[0], fout
);
1938 /* print field value */
1939 csv_print_field(*ptr
, fout
, cont
->opt
->csvFieldSep
[0]);
1946 /**********************/
1948 /**********************/
1952 html_escaped_print(const char *in
, FILE *fout
)
1955 bool leading_space
= true;
1957 for (p
= in
; *p
; p
++)
1962 fputs("&", fout
);
1965 fputs("<", fout
);
1968 fputs(">", fout
);
1971 fputs("<br />\n", fout
);
1974 fputs(""", fout
);
1977 /* protect leading space, for EXPLAIN output */
1979 fputs(" ", fout
);
1987 leading_space
= false;
1993 print_html_text(const printTableContent
*cont
, FILE *fout
)
1995 bool opt_tuples_only
= cont
->opt
->tuples_only
;
1996 unsigned short opt_border
= cont
->opt
->border
;
1997 const char *opt_table_attr
= cont
->opt
->tableAttr
;
1999 const char *const *ptr
;
2004 if (cont
->opt
->start_table
)
2006 fprintf(fout
, "<table border=\"%d\"", opt_border
);
2008 fprintf(fout
, " %s", opt_table_attr
);
2012 if (!opt_tuples_only
&& cont
->title
)
2014 fputs(" <caption>", fout
);
2015 html_escaped_print(cont
->title
, fout
);
2016 fputs("</caption>\n", fout
);
2020 if (!opt_tuples_only
)
2022 fputs(" <tr>\n", fout
);
2023 for (ptr
= cont
->headers
; *ptr
; ptr
++)
2025 fputs(" <th align=\"center\">", fout
);
2026 html_escaped_print(*ptr
, fout
);
2027 fputs("</th>\n", fout
);
2029 fputs(" </tr>\n", fout
);
2034 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2036 if (i
% cont
->ncolumns
== 0)
2040 fputs(" <tr valign=\"top\">\n", fout
);
2043 fprintf(fout
, " <td align=\"%s\">", cont
->aligns
[(i
) % cont
->ncolumns
] == 'r' ? "right" : "left");
2044 /* is string only whitespace? */
2045 if ((*ptr
)[strspn(*ptr
, " \t")] == '\0')
2046 fputs(" ", fout
);
2048 html_escaped_print(*ptr
, fout
);
2050 fputs("</td>\n", fout
);
2052 if ((i
+ 1) % cont
->ncolumns
== 0)
2053 fputs(" </tr>\n", fout
);
2056 if (cont
->opt
->stop_table
)
2058 printTableFooter
*footers
= footers_with_default(cont
);
2060 fputs("</table>\n", fout
);
2063 if (!opt_tuples_only
&& footers
!= NULL
&& !cancel_pressed
)
2065 printTableFooter
*f
;
2068 for (f
= footers
; f
; f
= f
->next
)
2070 html_escaped_print(f
->data
, fout
);
2071 fputs("<br />\n", fout
);
2073 fputs("</p>", fout
);
2082 print_html_vertical(const printTableContent
*cont
, FILE *fout
)
2084 bool opt_tuples_only
= cont
->opt
->tuples_only
;
2085 unsigned short opt_border
= cont
->opt
->border
;
2086 const char *opt_table_attr
= cont
->opt
->tableAttr
;
2087 unsigned long record
= cont
->opt
->prior_records
+ 1;
2089 const char *const *ptr
;
2094 if (cont
->opt
->start_table
)
2096 fprintf(fout
, "<table border=\"%d\"", opt_border
);
2098 fprintf(fout
, " %s", opt_table_attr
);
2102 if (!opt_tuples_only
&& cont
->title
)
2104 fputs(" <caption>", fout
);
2105 html_escaped_print(cont
->title
, fout
);
2106 fputs("</caption>\n", fout
);
2111 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2113 if (i
% cont
->ncolumns
== 0)
2117 if (!opt_tuples_only
)
2119 "\n <tr><td colspan=\"2\" align=\"center\">Record %lu</td></tr>\n",
2122 fputs("\n <tr><td colspan=\"2\"> </td></tr>\n", fout
);
2124 fputs(" <tr valign=\"top\">\n"
2126 html_escaped_print(cont
->headers
[i
% cont
->ncolumns
], fout
);
2127 fputs("</th>\n", fout
);
2129 fprintf(fout
, " <td align=\"%s\">", cont
->aligns
[i
% cont
->ncolumns
] == 'r' ? "right" : "left");
2130 /* is string only whitespace? */
2131 if ((*ptr
)[strspn(*ptr
, " \t")] == '\0')
2132 fputs(" ", fout
);
2134 html_escaped_print(*ptr
, fout
);
2136 fputs("</td>\n </tr>\n", fout
);
2139 if (cont
->opt
->stop_table
)
2141 fputs("</table>\n", fout
);
2144 if (!opt_tuples_only
&& cont
->footers
!= NULL
&& !cancel_pressed
)
2146 printTableFooter
*f
;
2149 for (f
= cont
->footers
; f
; f
= f
->next
)
2151 html_escaped_print(f
->data
, fout
);
2152 fputs("<br />\n", fout
);
2154 fputs("</p>", fout
);
2162 /*************************/
2164 /*************************/
2168 asciidoc_escaped_print(const char *in
, FILE *fout
)
2172 for (p
= in
; *p
; p
++)
2186 print_asciidoc_text(const printTableContent
*cont
, FILE *fout
)
2188 bool opt_tuples_only
= cont
->opt
->tuples_only
;
2189 unsigned short opt_border
= cont
->opt
->border
;
2191 const char *const *ptr
;
2196 if (cont
->opt
->start_table
)
2198 /* print table in new paragraph - enforce preliminary new line */
2202 if (!opt_tuples_only
&& cont
->title
)
2205 fputs(cont
->title
, fout
);
2209 /* print table [] header definition */
2210 fprintf(fout
, "[%scols=\"", !opt_tuples_only
? "options=\"header\"," : "");
2211 for (i
= 0; i
< cont
->ncolumns
; i
++)
2215 fprintf(fout
, "%s", cont
->aligns
[(i
) % cont
->ncolumns
] == 'r' ? ">l" : "<l");
2221 fputs(",frame=\"none\",grid=\"none\"", fout
);
2224 fputs(",frame=\"none\"", fout
);
2227 fputs(",frame=\"all\",grid=\"all\"", fout
);
2231 fputs("|====\n", fout
);
2234 if (!opt_tuples_only
)
2236 for (ptr
= cont
->headers
; *ptr
; ptr
++)
2238 if (ptr
!= cont
->headers
)
2241 asciidoc_escaped_print(*ptr
, fout
);
2248 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2250 if (i
% cont
->ncolumns
== 0)
2256 if (i
% cont
->ncolumns
!= 0)
2260 /* protect against needless spaces */
2261 if ((*ptr
)[strspn(*ptr
, " \t")] == '\0')
2263 if ((i
+ 1) % cont
->ncolumns
!= 0)
2267 asciidoc_escaped_print(*ptr
, fout
);
2269 if ((i
+ 1) % cont
->ncolumns
== 0)
2273 fputs("|====\n", fout
);
2275 if (cont
->opt
->stop_table
)
2277 printTableFooter
*footers
= footers_with_default(cont
);
2280 if (!opt_tuples_only
&& footers
!= NULL
&& !cancel_pressed
)
2282 printTableFooter
*f
;
2284 fputs("\n....\n", fout
);
2285 for (f
= footers
; f
; f
= f
->next
)
2287 fputs(f
->data
, fout
);
2290 fputs("....\n", fout
);
2296 print_asciidoc_vertical(const printTableContent
*cont
, FILE *fout
)
2298 bool opt_tuples_only
= cont
->opt
->tuples_only
;
2299 unsigned short opt_border
= cont
->opt
->border
;
2300 unsigned long record
= cont
->opt
->prior_records
+ 1;
2302 const char *const *ptr
;
2307 if (cont
->opt
->start_table
)
2309 /* print table in new paragraph - enforce preliminary new line */
2313 if (!opt_tuples_only
&& cont
->title
)
2316 fputs(cont
->title
, fout
);
2320 /* print table [] header definition */
2321 fputs("[cols=\"h,l\"", fout
);
2325 fputs(",frame=\"none\",grid=\"none\"", fout
);
2328 fputs(",frame=\"none\"", fout
);
2331 fputs(",frame=\"all\",grid=\"all\"", fout
);
2335 fputs("|====\n", fout
);
2339 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2341 if (i
% cont
->ncolumns
== 0)
2345 if (!opt_tuples_only
)
2350 fputs("2+|\n", fout
);
2354 asciidoc_escaped_print(cont
->headers
[i
% cont
->ncolumns
], fout
);
2356 fprintf(fout
, " %s|", cont
->aligns
[i
% cont
->ncolumns
] == 'r' ? ">l" : "<l");
2357 /* is string only whitespace? */
2358 if ((*ptr
)[strspn(*ptr
, " \t")] == '\0')
2361 asciidoc_escaped_print(*ptr
, fout
);
2365 fputs("|====\n", fout
);
2367 if (cont
->opt
->stop_table
)
2370 if (!opt_tuples_only
&& cont
->footers
!= NULL
&& !cancel_pressed
)
2372 printTableFooter
*f
;
2374 fputs("\n....\n", fout
);
2375 for (f
= cont
->footers
; f
; f
= f
->next
)
2377 fputs(f
->data
, fout
);
2380 fputs("....\n", fout
);
2386 /*************************/
2388 /*************************/
2392 latex_escaped_print(const char *in
, FILE *fout
)
2396 for (p
= in
; *p
; p
++)
2400 * We convert ASCII characters per the recommendations in
2401 * Scott Pakin's "The Comprehensive LATEX Symbol List",
2402 * available from CTAN. For non-ASCII, you're on your own.
2417 fputs("\\textless{}", fout
);
2420 fputs("\\textgreater{}", fout
);
2423 fputs("\\textbackslash{}", fout
);
2426 fputs("\\^{}", fout
);
2435 fputs("\\textbar{}", fout
);
2441 fputs("\\~{}", fout
);
2444 /* This is not right, but doing it right seems too hard */
2445 fputs("\\\\", fout
);
2454 print_latex_text(const printTableContent
*cont
, FILE *fout
)
2456 bool opt_tuples_only
= cont
->opt
->tuples_only
;
2457 unsigned short opt_border
= cont
->opt
->border
;
2459 const char *const *ptr
;
2467 if (cont
->opt
->start_table
)
2470 if (!opt_tuples_only
&& cont
->title
)
2472 fputs("\\begin{center}\n", fout
);
2473 latex_escaped_print(cont
->title
, fout
);
2474 fputs("\n\\end{center}\n\n", fout
);
2477 /* begin environment and set alignments and borders */
2478 fputs("\\begin{tabular}{", fout
);
2480 if (opt_border
>= 2)
2482 for (i
= 0; i
< cont
->ncolumns
; i
++)
2484 fputc(*(cont
->aligns
+ i
), fout
);
2485 if (opt_border
!= 0 && i
< cont
->ncolumns
- 1)
2488 if (opt_border
>= 2)
2493 if (!opt_tuples_only
&& opt_border
>= 2)
2494 fputs("\\hline\n", fout
);
2497 if (!opt_tuples_only
)
2499 for (i
= 0, ptr
= cont
->headers
; i
< cont
->ncolumns
; i
++, ptr
++)
2503 fputs("\\textit{", fout
);
2504 latex_escaped_print(*ptr
, fout
);
2507 fputs(" \\\\\n", fout
);
2508 fputs("\\hline\n", fout
);
2513 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2515 latex_escaped_print(*ptr
, fout
);
2517 if ((i
+ 1) % cont
->ncolumns
== 0)
2519 fputs(" \\\\\n", fout
);
2520 if (opt_border
== 3)
2521 fputs("\\hline\n", fout
);
2529 if (cont
->opt
->stop_table
)
2531 printTableFooter
*footers
= footers_with_default(cont
);
2533 if (opt_border
== 2)
2534 fputs("\\hline\n", fout
);
2536 fputs("\\end{tabular}\n\n\\noindent ", fout
);
2539 if (footers
&& !opt_tuples_only
&& !cancel_pressed
)
2541 printTableFooter
*f
;
2543 for (f
= footers
; f
; f
= f
->next
)
2545 latex_escaped_print(f
->data
, fout
);
2546 fputs(" \\\\\n", fout
);
2555 /*************************/
2556 /* LaTeX longtable */
2557 /*************************/
2561 print_latex_longtable_text(const printTableContent
*cont
, FILE *fout
)
2563 bool opt_tuples_only
= cont
->opt
->tuples_only
;
2564 unsigned short opt_border
= cont
->opt
->border
;
2566 const char *opt_table_attr
= cont
->opt
->tableAttr
;
2567 const char *next_opt_table_attr_char
= opt_table_attr
;
2568 const char *last_opt_table_attr_char
= NULL
;
2569 const char *const *ptr
;
2577 if (cont
->opt
->start_table
)
2579 /* begin environment and set alignments and borders */
2580 fputs("\\begin{longtable}{", fout
);
2582 if (opt_border
>= 2)
2585 for (i
= 0; i
< cont
->ncolumns
; i
++)
2587 /* longtable supports either a width (p) or an alignment (l/r) */
2588 /* Are we left-justified and was a proportional width specified? */
2589 if (*(cont
->aligns
+ i
) == 'l' && opt_table_attr
)
2591 #define LONGTABLE_WHITESPACE " \t\n"
2593 /* advance over whitespace */
2594 next_opt_table_attr_char
+= strspn(next_opt_table_attr_char
,
2595 LONGTABLE_WHITESPACE
);
2596 /* We have a value? */
2597 if (next_opt_table_attr_char
[0] != '\0')
2600 fwrite(next_opt_table_attr_char
, strcspn(next_opt_table_attr_char
,
2601 LONGTABLE_WHITESPACE
), 1, fout
);
2602 last_opt_table_attr_char
= next_opt_table_attr_char
;
2603 next_opt_table_attr_char
+= strcspn(next_opt_table_attr_char
,
2604 LONGTABLE_WHITESPACE
);
2605 fputs("\\textwidth}", fout
);
2607 /* use previous value */
2608 else if (last_opt_table_attr_char
!= NULL
)
2611 fwrite(last_opt_table_attr_char
, strcspn(last_opt_table_attr_char
,
2612 LONGTABLE_WHITESPACE
), 1, fout
);
2613 fputs("\\textwidth}", fout
);
2619 fputc(*(cont
->aligns
+ i
), fout
);
2621 if (opt_border
!= 0 && i
< cont
->ncolumns
- 1)
2625 if (opt_border
>= 2)
2631 if (!opt_tuples_only
)
2634 if (opt_border
>= 2)
2635 fputs("\\toprule\n", fout
);
2636 for (i
= 0, ptr
= cont
->headers
; i
< cont
->ncolumns
; i
++, ptr
++)
2640 fputs("\\small\\textbf{\\textit{", fout
);
2641 latex_escaped_print(*ptr
, fout
);
2644 fputs(" \\\\\n", fout
);
2645 fputs("\\midrule\n\\endfirsthead\n", fout
);
2647 /* secondary heads */
2648 if (opt_border
>= 2)
2649 fputs("\\toprule\n", fout
);
2650 for (i
= 0, ptr
= cont
->headers
; i
< cont
->ncolumns
; i
++, ptr
++)
2654 fputs("\\small\\textbf{\\textit{", fout
);
2655 latex_escaped_print(*ptr
, fout
);
2658 fputs(" \\\\\n", fout
);
2659 /* If the line under the row already appeared, don't do another */
2660 if (opt_border
!= 3)
2661 fputs("\\midrule\n", fout
);
2662 fputs("\\endhead\n", fout
);
2664 /* table name, caption? */
2665 if (!opt_tuples_only
&& cont
->title
)
2667 /* Don't output if we are printing a line under each row */
2668 if (opt_border
== 2)
2669 fputs("\\bottomrule\n", fout
);
2670 fputs("\\caption[", fout
);
2671 latex_escaped_print(cont
->title
, fout
);
2672 fputs(" (Continued)]{", fout
);
2673 latex_escaped_print(cont
->title
, fout
);
2674 fputs("}\n\\endfoot\n", fout
);
2675 if (opt_border
== 2)
2676 fputs("\\bottomrule\n", fout
);
2677 fputs("\\caption[", fout
);
2678 latex_escaped_print(cont
->title
, fout
);
2680 latex_escaped_print(cont
->title
, fout
);
2681 fputs("}\n\\endlastfoot\n", fout
);
2683 /* output bottom table line? */
2684 else if (opt_border
>= 2)
2686 fputs("\\bottomrule\n\\endfoot\n", fout
);
2687 fputs("\\bottomrule\n\\endlastfoot\n", fout
);
2693 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2695 /* Add a line under each row? */
2696 if (i
!= 0 && i
% cont
->ncolumns
!= 0)
2697 fputs("\n&\n", fout
);
2698 fputs("\\raggedright{", fout
);
2699 latex_escaped_print(*ptr
, fout
);
2701 if ((i
+ 1) % cont
->ncolumns
== 0)
2703 fputs(" \\tabularnewline\n", fout
);
2704 if (opt_border
== 3)
2705 fputs(" \\hline\n", fout
);
2711 if (cont
->opt
->stop_table
)
2712 fputs("\\end{longtable}\n", fout
);
2717 print_latex_vertical(const printTableContent
*cont
, FILE *fout
)
2719 bool opt_tuples_only
= cont
->opt
->tuples_only
;
2720 unsigned short opt_border
= cont
->opt
->border
;
2721 unsigned long record
= cont
->opt
->prior_records
+ 1;
2723 const char *const *ptr
;
2731 if (cont
->opt
->start_table
)
2734 if (!opt_tuples_only
&& cont
->title
)
2736 fputs("\\begin{center}\n", fout
);
2737 latex_escaped_print(cont
->title
, fout
);
2738 fputs("\n\\end{center}\n\n", fout
);
2741 /* begin environment and set alignments and borders */
2742 fputs("\\begin{tabular}{", fout
);
2743 if (opt_border
== 0)
2745 else if (opt_border
== 1)
2747 else if (opt_border
== 2)
2748 fputs("|c|l|", fout
);
2753 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2756 if (i
% cont
->ncolumns
== 0)
2760 if (!opt_tuples_only
)
2762 if (opt_border
== 2)
2764 fputs("\\hline\n", fout
);
2765 fprintf(fout
, "\\multicolumn{2}{|c|}{\\textit{Record %lu}} \\\\\n", record
++);
2768 fprintf(fout
, "\\multicolumn{2}{c}{\\textit{Record %lu}} \\\\\n", record
++);
2770 if (opt_border
>= 1)
2771 fputs("\\hline\n", fout
);
2774 latex_escaped_print(cont
->headers
[i
% cont
->ncolumns
], fout
);
2776 latex_escaped_print(*ptr
, fout
);
2777 fputs(" \\\\\n", fout
);
2780 if (cont
->opt
->stop_table
)
2782 if (opt_border
== 2)
2783 fputs("\\hline\n", fout
);
2785 fputs("\\end{tabular}\n\n\\noindent ", fout
);
2788 if (cont
->footers
&& !opt_tuples_only
&& !cancel_pressed
)
2790 printTableFooter
*f
;
2792 for (f
= cont
->footers
; f
; f
= f
->next
)
2794 latex_escaped_print(f
->data
, fout
);
2795 fputs(" \\\\\n", fout
);
2804 /*************************/
2806 /*************************/
2810 troff_ms_escaped_print(const char *in
, FILE *fout
)
2814 for (p
= in
; *p
; p
++)
2818 fputs("\\(rs", fout
);
2827 print_troff_ms_text(const printTableContent
*cont
, FILE *fout
)
2829 bool opt_tuples_only
= cont
->opt
->tuples_only
;
2830 unsigned short opt_border
= cont
->opt
->border
;
2832 const char *const *ptr
;
2840 if (cont
->opt
->start_table
)
2843 if (!opt_tuples_only
&& cont
->title
)
2845 fputs(".LP\n.DS C\n", fout
);
2846 troff_ms_escaped_print(cont
->title
, fout
);
2847 fputs("\n.DE\n", fout
);
2850 /* begin environment and set alignments and borders */
2851 fputs(".LP\n.TS\n", fout
);
2852 if (opt_border
== 2)
2853 fputs("center box;\n", fout
);
2855 fputs("center;\n", fout
);
2857 for (i
= 0; i
< cont
->ncolumns
; i
++)
2859 fputc(*(cont
->aligns
+ i
), fout
);
2860 if (opt_border
> 0 && i
< cont
->ncolumns
- 1)
2866 if (!opt_tuples_only
)
2868 for (i
= 0, ptr
= cont
->headers
; i
< cont
->ncolumns
; i
++, ptr
++)
2872 fputs("\\fI", fout
);
2873 troff_ms_escaped_print(*ptr
, fout
);
2874 fputs("\\fP", fout
);
2876 fputs("\n_\n", fout
);
2881 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2883 troff_ms_escaped_print(*ptr
, fout
);
2885 if ((i
+ 1) % cont
->ncolumns
== 0)
2895 if (cont
->opt
->stop_table
)
2897 printTableFooter
*footers
= footers_with_default(cont
);
2899 fputs(".TE\n.DS L\n", fout
);
2902 if (footers
&& !opt_tuples_only
&& !cancel_pressed
)
2904 printTableFooter
*f
;
2906 for (f
= footers
; f
; f
= f
->next
)
2908 troff_ms_escaped_print(f
->data
, fout
);
2913 fputs(".DE\n", fout
);
2919 print_troff_ms_vertical(const printTableContent
*cont
, FILE *fout
)
2921 bool opt_tuples_only
= cont
->opt
->tuples_only
;
2922 unsigned short opt_border
= cont
->opt
->border
;
2923 unsigned long record
= cont
->opt
->prior_records
+ 1;
2925 const char *const *ptr
;
2926 unsigned short current_format
= 0; /* 0=none, 1=header, 2=body */
2934 if (cont
->opt
->start_table
)
2937 if (!opt_tuples_only
&& cont
->title
)
2939 fputs(".LP\n.DS C\n", fout
);
2940 troff_ms_escaped_print(cont
->title
, fout
);
2941 fputs("\n.DE\n", fout
);
2944 /* begin environment and set alignments and borders */
2945 fputs(".LP\n.TS\n", fout
);
2946 if (opt_border
== 2)
2947 fputs("center box;\n", fout
);
2949 fputs("center;\n", fout
);
2952 if (opt_tuples_only
)
2953 fputs("c l;\n", fout
);
2956 current_format
= 2; /* assume tuples printed already */
2959 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2962 if (i
% cont
->ncolumns
== 0)
2966 if (!opt_tuples_only
)
2968 if (current_format
!= 1)
2970 if (opt_border
== 2 && record
> 1)
2972 if (current_format
!= 0)
2973 fputs(".T&\n", fout
);
2974 fputs("c s.\n", fout
);
2977 fprintf(fout
, "\\fIRecord %lu\\fP\n", record
++);
2979 if (opt_border
>= 1)
2983 if (!opt_tuples_only
)
2985 if (current_format
!= 2)
2987 if (current_format
!= 0)
2988 fputs(".T&\n", fout
);
2989 if (opt_border
!= 1)
2990 fputs("c l.\n", fout
);
2992 fputs("c | l.\n", fout
);
2997 troff_ms_escaped_print(cont
->headers
[i
% cont
->ncolumns
], fout
);
2999 troff_ms_escaped_print(*ptr
, fout
);
3004 if (cont
->opt
->stop_table
)
3006 fputs(".TE\n.DS L\n", fout
);
3009 if (cont
->footers
&& !opt_tuples_only
&& !cancel_pressed
)
3011 printTableFooter
*f
;
3013 for (f
= cont
->footers
; f
; f
= f
->next
)
3015 troff_ms_escaped_print(f
->data
, fout
);
3020 fputs(".DE\n", fout
);
3025 /********************************/
3026 /* Public functions */
3027 /********************************/
3031 * disable_sigpipe_trap
3033 * Turn off SIGPIPE interrupt --- call this before writing to a temporary
3034 * query output file that is a pipe.
3036 * No-op on Windows, where there's no SIGPIPE interrupts.
3039 disable_sigpipe_trap(void)
3042 pqsignal(SIGPIPE
, SIG_IGN
);
3047 * restore_sigpipe_trap
3049 * Restore normal SIGPIPE interrupt --- call this when done writing to a
3050 * temporary query output file that was (or might have been) a pipe.
3052 * Note: within psql, we enable SIGPIPE interrupts unless the permanent query
3053 * output file is a pipe, in which case they should be kept off. This
3054 * approach works only because psql is not currently complicated enough to
3055 * have nested usages of short-lived output files. Otherwise we'd probably
3056 * need a genuine save-and-restore-state approach; but for now, that would be
3057 * useless complication. In non-psql programs, this always enables SIGPIPE.
3059 * No-op on Windows, where there's no SIGPIPE interrupts.
3062 restore_sigpipe_trap(void)
3065 pqsignal(SIGPIPE
, always_ignore_sigpipe
? SIG_IGN
: SIG_DFL
);
3070 * set_sigpipe_trap_state
3072 * Set the trap state that restore_sigpipe_trap should restore to.
3075 set_sigpipe_trap_state(bool ignore
)
3077 always_ignore_sigpipe
= ignore
;
3084 * Tests if pager is needed and returns appropriate FILE pointer.
3086 * If the topt argument is NULL no pager is used.
3089 PageOutput(int lines
, const printTableOpt
*topt
)
3091 /* check whether we need / can / are supposed to use pager */
3092 if (topt
&& topt
->pager
&& isatty(fileno(stdin
)) && isatty(fileno(stdout
)))
3095 unsigned short int pager
= topt
->pager
;
3096 int min_lines
= topt
->pager_min_lines
;
3098 struct winsize screen_size
;
3100 result
= ioctl(fileno(stdout
), TIOCGWINSZ
, &screen_size
);
3102 /* >= accounts for a one-line prompt */
3104 || (lines
>= screen_size
.ws_row
&& lines
>= min_lines
)
3108 const char *pagerprog
;
3111 pagerprog
= getenv("PSQL_PAGER");
3113 pagerprog
= getenv("PAGER");
3115 pagerprog
= DEFAULT_PAGER
;
3118 /* if PAGER is empty or all-white-space, don't use pager */
3119 if (strspn(pagerprog
, " \t\r\n") == strlen(pagerprog
))
3123 disable_sigpipe_trap();
3124 pagerpipe
= popen(pagerprog
, "w");
3127 /* if popen fails, silently proceed without pager */
3128 restore_sigpipe_trap();
3138 * Close previously opened pager pipe, if any
3141 ClosePager(FILE *pagerpipe
)
3143 if (pagerpipe
&& pagerpipe
!= stdout
)
3146 * If printing was canceled midstream, warn about it.
3148 * Some pagers like less use Ctrl-C as part of their command set. Even
3149 * so, we abort our processing and warn the user what we did. If the
3150 * pager quit as a result of the SIGINT, this message won't go
3154 fprintf(pagerpipe
, _("Interrupted\n"));
3157 restore_sigpipe_trap();
3162 * Initialise a table contents struct.
3163 * Must be called before any other printTable method is used.
3165 * The title is not duplicated; the caller must ensure that the buffer
3166 * is available for the lifetime of the printTableContent struct.
3168 * If you call this, you must call printTableCleanup once you're done with the
3172 printTableInit(printTableContent
*const content
, const printTableOpt
*opt
,
3173 const char *title
, const int ncolumns
, const int nrows
)
3176 content
->title
= title
;
3177 content
->ncolumns
= ncolumns
;
3178 content
->nrows
= nrows
;
3180 content
->headers
= pg_malloc0((ncolumns
+ 1) * sizeof(*content
->headers
));
3182 content
->cells
= pg_malloc0((ncolumns
* nrows
+ 1) * sizeof(*content
->cells
));
3184 content
->cellmustfree
= NULL
;
3185 content
->footers
= NULL
;
3187 content
->aligns
= pg_malloc0((ncolumns
+ 1) * sizeof(*content
->align
));
3189 content
->header
= content
->headers
;
3190 content
->cell
= content
->cells
;
3191 content
->footer
= content
->footers
;
3192 content
->align
= content
->aligns
;
3193 content
->cellsadded
= 0;
3197 * Add a header to the table.
3199 * Headers are not duplicated; you must ensure that the header string is
3200 * available for the lifetime of the printTableContent struct.
3202 * If translate is true, the function will pass the header through gettext.
3203 * Otherwise, the header will not be translated.
3205 * align is either 'l' or 'r', and specifies the alignment for cells in this
3209 printTableAddHeader(printTableContent
*const content
, char *header
,
3210 const bool translate
, const char align
)
3213 (void) translate
; /* unused parameter */
3216 if (content
->header
>= content
->headers
+ content
->ncolumns
)
3218 fprintf(stderr
, _("Cannot add header to table content: "
3219 "column count of %d exceeded.\n"),
3224 *content
->header
= (char *) mbvalidate((unsigned char *) header
,
3225 content
->opt
->encoding
);
3228 *content
->header
= _(*content
->header
);
3232 *content
->align
= align
;
3237 * Add a cell to the table.
3239 * Cells are not duplicated; you must ensure that the cell string is available
3240 * for the lifetime of the printTableContent struct.
3242 * If translate is true, the function will pass the cell through gettext.
3243 * Otherwise, the cell will not be translated.
3245 * If mustfree is true, the cell string is freed by printTableCleanup().
3246 * Note: Automatic freeing of translatable strings is not supported.
3249 printTableAddCell(printTableContent
*const content
, char *cell
,
3250 const bool translate
, const bool mustfree
)
3253 (void) translate
; /* unused parameter */
3256 if (content
->cellsadded
>= content
->ncolumns
* content
->nrows
)
3258 fprintf(stderr
, _("Cannot add cell to table content: "
3259 "total cell count of %d exceeded.\n"),
3260 content
->ncolumns
* content
->nrows
);
3264 *content
->cell
= (char *) mbvalidate((unsigned char *) cell
,
3265 content
->opt
->encoding
);
3269 *content
->cell
= _(*content
->cell
);
3274 if (content
->cellmustfree
== NULL
)
3275 content
->cellmustfree
=
3276 pg_malloc0((content
->ncolumns
* content
->nrows
+ 1) * sizeof(bool));
3278 content
->cellmustfree
[content
->cellsadded
] = true;
3281 content
->cellsadded
++;
3285 * Add a footer to the table.
3287 * Footers are added as elements of a singly-linked list, and the content is
3288 * strdup'd, so there is no need to keep the original footer string around.
3290 * Footers are never translated by the function. If you want the footer
3291 * translated you must do so yourself, before calling printTableAddFooter. The
3292 * reason this works differently to headers and cells is that footers tend to
3293 * be made of up individually translated components, rather than being
3294 * translated as a whole.
3297 printTableAddFooter(printTableContent
*const content
, const char *footer
)
3299 printTableFooter
*f
;
3301 f
= pg_malloc0(sizeof(*f
));
3302 f
->data
= pg_strdup(footer
);
3304 if (content
->footers
== NULL
)
3305 content
->footers
= f
;
3307 content
->footer
->next
= f
;
3309 content
->footer
= f
;
3313 * Change the content of the last-added footer.
3315 * The current contents of the last-added footer are freed, and replaced by the
3316 * content given in *footer. If there was no previous footer, add a new one.
3318 * The content is strdup'd, so there is no need to keep the original string
3322 printTableSetFooter(printTableContent
*const content
, const char *footer
)
3324 if (content
->footers
!= NULL
)
3326 free(content
->footer
->data
);
3327 content
->footer
->data
= pg_strdup(footer
);
3330 printTableAddFooter(content
, footer
);
3334 * Free all memory allocated to this struct.
3336 * Once this has been called, the struct is unusable unless you pass it to
3337 * printTableInit() again.
3340 printTableCleanup(printTableContent
*const content
)
3342 if (content
->cellmustfree
)
3346 for (i
= 0; i
< content
->nrows
* content
->ncolumns
; i
++)
3348 if (content
->cellmustfree
[i
])
3349 free(unconstify(char *, content
->cells
[i
]));
3351 free(content
->cellmustfree
);
3352 content
->cellmustfree
= NULL
;
3354 free(content
->headers
);
3355 free(content
->cells
);
3356 free(content
->aligns
);
3358 content
->opt
= NULL
;
3359 content
->title
= NULL
;
3360 content
->headers
= NULL
;
3361 content
->cells
= NULL
;
3362 content
->aligns
= NULL
;
3363 content
->header
= NULL
;
3364 content
->cell
= NULL
;
3365 content
->align
= NULL
;
3367 if (content
->footers
)
3369 for (content
->footer
= content
->footers
; content
->footer
;)
3371 printTableFooter
*f
;
3373 f
= content
->footer
;
3374 content
->footer
= f
->next
;
3379 content
->footers
= NULL
;
3380 content
->footer
= NULL
;
3386 * Setup pager if required
3389 IsPagerNeeded(const printTableContent
*cont
, int extra_lines
, bool expanded
,
3390 FILE **fout
, bool *is_pager
)
3392 if (*fout
== stdout
)
3397 lines
= (cont
->ncolumns
+ 1) * cont
->nrows
;
3399 lines
= cont
->nrows
+ 1;
3401 if (!cont
->opt
->tuples_only
)
3403 printTableFooter
*f
;
3406 * FIXME -- this is slightly bogus: it counts the number of
3407 * footers, not the number of lines in them.
3409 for (f
= cont
->footers
; f
; f
= f
->next
)
3413 *fout
= PageOutput(lines
+ extra_lines
, cont
->opt
);
3414 *is_pager
= (*fout
!= stdout
);
3421 * Use this to print any table in the supported formats.
3423 * cont: table data and formatting options
3424 * fout: where to print to
3425 * is_pager: true if caller has already redirected fout to be a pager pipe
3426 * flog: if not null, also print the table there (for --log-file option)
3429 printTable(const printTableContent
*cont
,
3430 FILE *fout
, bool is_pager
, FILE *flog
)
3432 bool is_local_pager
= false;
3437 if (cont
->opt
->format
== PRINT_NOTHING
)
3440 /* print_aligned_*() handle the pager themselves */
3442 cont
->opt
->format
!= PRINT_ALIGNED
&&
3443 cont
->opt
->format
!= PRINT_WRAPPED
)
3445 IsPagerNeeded(cont
, 0, (cont
->opt
->expanded
== 1), &fout
, &is_pager
);
3446 is_local_pager
= is_pager
;
3449 /* clear any pre-existing error indication on the output stream */
3452 /* print the stuff */
3455 print_aligned_text(cont
, flog
, false);
3457 switch (cont
->opt
->format
)
3459 case PRINT_UNALIGNED
:
3460 if (cont
->opt
->expanded
== 1)
3461 print_unaligned_vertical(cont
, fout
);
3463 print_unaligned_text(cont
, fout
);
3469 * In expanded-auto mode, force vertical if a pager is passed in;
3470 * else we may make different decisions for different hunks of the
3473 if (cont
->opt
->expanded
== 1 ||
3474 (cont
->opt
->expanded
== 2 && is_pager
))
3475 print_aligned_vertical(cont
, fout
, is_pager
);
3477 print_aligned_text(cont
, fout
, is_pager
);
3480 if (cont
->opt
->expanded
== 1)
3481 print_csv_vertical(cont
, fout
);
3483 print_csv_text(cont
, fout
);
3486 if (cont
->opt
->expanded
== 1)
3487 print_html_vertical(cont
, fout
);
3489 print_html_text(cont
, fout
);
3491 case PRINT_ASCIIDOC
:
3492 if (cont
->opt
->expanded
== 1)
3493 print_asciidoc_vertical(cont
, fout
);
3495 print_asciidoc_text(cont
, fout
);
3498 if (cont
->opt
->expanded
== 1)
3499 print_latex_vertical(cont
, fout
);
3501 print_latex_text(cont
, fout
);
3503 case PRINT_LATEX_LONGTABLE
:
3504 if (cont
->opt
->expanded
== 1)
3505 print_latex_vertical(cont
, fout
);
3507 print_latex_longtable_text(cont
, fout
);
3509 case PRINT_TROFF_MS
:
3510 if (cont
->opt
->expanded
== 1)
3511 print_troff_ms_vertical(cont
, fout
);
3513 print_troff_ms_text(cont
, fout
);
3516 fprintf(stderr
, _("invalid output format (internal error): %d"),
3526 * Use this to print query results
3528 * result: result of a successful query
3529 * opt: formatting options
3530 * fout: where to print to
3531 * is_pager: true if caller has already redirected fout to be a pager pipe
3532 * flog: if not null, also print the data there (for --log-file option)
3535 printQuery(const PGresult
*result
, const printQueryOpt
*opt
,
3536 FILE *fout
, bool is_pager
, FILE *flog
)
3538 printTableContent cont
;
3546 printTableInit(&cont
, &opt
->topt
, opt
->title
,
3547 PQnfields(result
), PQntuples(result
));
3549 /* Assert caller supplied enough translate_columns[] entries */
3550 Assert(opt
->translate_columns
== NULL
||
3551 opt
->n_translate_columns
>= cont
.ncolumns
);
3553 for (i
= 0; i
< cont
.ncolumns
; i
++)
3555 printTableAddHeader(&cont
, PQfname(result
, i
),
3556 opt
->translate_header
,
3557 column_type_alignment(PQftype(result
, i
)));
3561 for (r
= 0; r
< cont
.nrows
; r
++)
3563 for (c
= 0; c
< cont
.ncolumns
; c
++)
3566 bool mustfree
= false;
3569 if (PQgetisnull(result
, r
, c
))
3570 cell
= opt
->nullPrint
? opt
->nullPrint
: "";
3573 cell
= PQgetvalue(result
, r
, c
);
3574 if (cont
.aligns
[c
] == 'r' && opt
->topt
.numericLocale
)
3576 cell
= format_numeric_locale(cell
);
3581 translate
= (opt
->translate_columns
&& opt
->translate_columns
[c
]);
3582 printTableAddCell(&cont
, cell
, translate
, mustfree
);
3591 for (footer
= opt
->footers
; *footer
; footer
++)
3592 printTableAddFooter(&cont
, *footer
);
3595 printTable(&cont
, fout
, is_pager
, flog
);
3596 printTableCleanup(&cont
);
3600 column_type_alignment(Oid ftype
)
3627 setDecimalLocale(void)
3629 struct lconv
*extlconv
;
3631 extlconv
= localeconv();
3633 /* Don't accept an empty decimal_point string */
3634 if (*extlconv
->decimal_point
)
3635 decimal_point
= pg_strdup(extlconv
->decimal_point
);
3637 decimal_point
= "."; /* SQL output standard */
3640 * Although the Open Group standard allows locales to supply more than one
3641 * group width, we consider only the first one, and we ignore any attempt
3642 * to suppress grouping by specifying CHAR_MAX. As in the backend's
3643 * cash.c, we must apply a range check to avoid being fooled by variant
3646 groupdigits
= *extlconv
->grouping
;
3647 if (groupdigits
<= 0 || groupdigits
> 6)
3648 groupdigits
= 3; /* most common */
3650 /* Don't accept an empty thousands_sep string, either */
3651 /* similar code exists in formatting.c */
3652 if (*extlconv
->thousands_sep
)
3653 thousands_sep
= pg_strdup(extlconv
->thousands_sep
);
3654 /* Make sure thousands separator doesn't match decimal point symbol. */
3655 else if (strcmp(decimal_point
, ",") != 0)
3656 thousands_sep
= ",";
3658 thousands_sep
= ".";
3661 /* get selected or default line style */
3662 const printTextFormat
*
3663 get_line_style(const printTableOpt
*opt
)
3666 * Note: this function mainly exists to preserve the convention that a
3667 * printTableOpt struct can be initialized to zeroes to get default
3670 if (opt
->line_style
!= NULL
)
3671 return opt
->line_style
;
3673 return &pg_asciiformat
;
3677 refresh_utf8format(const printTableOpt
*opt
)
3679 printTextFormat
*popt
= &pg_utf8format
;
3681 const unicodeStyleBorderFormat
*border
;
3682 const unicodeStyleRowFormat
*header
;
3683 const unicodeStyleColumnFormat
*column
;
3685 popt
->name
= "unicode";
3687 border
= &unicode_style
.border_style
[opt
->unicode_border_linestyle
];
3688 header
= &unicode_style
.row_style
[opt
->unicode_header_linestyle
];
3689 column
= &unicode_style
.column_style
[opt
->unicode_column_linestyle
];
3691 popt
->lrule
[PRINT_RULE_TOP
].hrule
= border
->horizontal
;
3692 popt
->lrule
[PRINT_RULE_TOP
].leftvrule
= border
->down_and_right
;
3693 popt
->lrule
[PRINT_RULE_TOP
].midvrule
= column
->down_and_horizontal
[opt
->unicode_border_linestyle
];
3694 popt
->lrule
[PRINT_RULE_TOP
].rightvrule
= border
->down_and_left
;
3696 popt
->lrule
[PRINT_RULE_MIDDLE
].hrule
= header
->horizontal
;
3697 popt
->lrule
[PRINT_RULE_MIDDLE
].leftvrule
= header
->vertical_and_right
[opt
->unicode_border_linestyle
];
3698 popt
->lrule
[PRINT_RULE_MIDDLE
].midvrule
= column
->vertical_and_horizontal
[opt
->unicode_header_linestyle
];
3699 popt
->lrule
[PRINT_RULE_MIDDLE
].rightvrule
= header
->vertical_and_left
[opt
->unicode_border_linestyle
];
3701 popt
->lrule
[PRINT_RULE_BOTTOM
].hrule
= border
->horizontal
;
3702 popt
->lrule
[PRINT_RULE_BOTTOM
].leftvrule
= border
->up_and_right
;
3703 popt
->lrule
[PRINT_RULE_BOTTOM
].midvrule
= column
->up_and_horizontal
[opt
->unicode_border_linestyle
];
3704 popt
->lrule
[PRINT_RULE_BOTTOM
].rightvrule
= border
->left_and_right
;
3707 popt
->lrule
[PRINT_RULE_DATA
].hrule
= "";
3708 popt
->lrule
[PRINT_RULE_DATA
].leftvrule
= border
->vertical
;
3709 popt
->lrule
[PRINT_RULE_DATA
].midvrule
= column
->vertical
;
3710 popt
->lrule
[PRINT_RULE_DATA
].rightvrule
= border
->vertical
;
3712 popt
->midvrule_nl
= column
->vertical
;
3713 popt
->midvrule_wrap
= column
->vertical
;
3714 popt
->midvrule_blank
= column
->vertical
;
3716 /* Same for all unicode today */
3717 popt
->header_nl_left
= unicode_style
.header_nl_left
;
3718 popt
->header_nl_right
= unicode_style
.header_nl_right
;
3719 popt
->nl_left
= unicode_style
.nl_left
;
3720 popt
->nl_right
= unicode_style
.nl_right
;
3721 popt
->wrap_left
= unicode_style
.wrap_left
;
3722 popt
->wrap_right
= unicode_style
.wrap_right
;
3723 popt
->wrap_right_border
= unicode_style
.wrap_right_border
;
3727 * Compute the byte distance to the end of the string or *target_width
3728 * display character positions, whichever comes first. Update *target_width
3729 * to be the number of display character positions actually filled.
3732 strlen_max_width(unsigned char *str
, int *target_width
, int encoding
)
3734 unsigned char *start
= str
;
3735 unsigned char *end
= str
+ strlen((char *) str
);
3740 int char_width
= PQdsplen((char *) str
, encoding
);
3743 * If the display width of the new character causes the string to
3744 * exceed its target width, skip it and return. However, if this is
3745 * the first character of the string (curr_width == 0), we have to
3748 if (*target_width
< curr_width
+ char_width
&& curr_width
!= 0)
3751 curr_width
+= char_width
;
3753 str
+= PQmblen((char *) str
, encoding
);
3755 if (str
> end
) /* Don't overrun invalid string */
3759 *target_width
= curr_width
;