Fix indentation in twophase.c
[pgsql.git] / src / fe_utils / print.c
blob7af1ccb6b562bd6e75839d22b3e159c9a59168e8
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"
20 #include <limits.h>
21 #include <math.h>
22 #include <unistd.h>
24 #ifndef WIN32
25 #include <sys/ioctl.h> /* for ioctl() */
26 #endif
28 #ifdef HAVE_TERMIOS_H
29 #include <termios.h>
30 #endif
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 =
58 "ascii",
60 {"-", "+", "+", "+"},
61 {"-", "+", "+", "+"},
62 {"-", "+", "+", "+"},
63 {"", "|", "|", "|"}
65 "|",
66 "|",
67 "|",
68 " ",
69 "+",
70 " ",
71 "+",
72 ".",
73 ".",
74 true
77 const printTextFormat pg_asciiformat_old =
79 "old-ascii",
81 {"-", "+", "+", "+"},
82 {"-", "+", "+", "+"},
83 {"-", "+", "+", "+"},
84 {"", "|", "|", "|"}
86 ":",
87 ";",
88 " ",
89 "+",
90 " ",
91 " ",
92 " ",
93 " ",
94 " ",
95 false
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;
133 const char *nl_left;
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 */
144 "\342\224\200",
146 /*--
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"},
153 /*--
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 */
162 "\342\225\220",
164 /*--
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"},
171 /*--
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 */
182 "\342\224\202",
184 /*--
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"},
191 /*--
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"},
198 /*--
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 */
207 "\342\225\221",
209 /*--
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"},
216 /*--
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"},
223 /*--
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"},
232 /*--
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"},
243 /*--
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"},
254 " ",
255 /* U+21B5 Downwards Arrow with Corner Leftwards */
256 "\342\206\265",
257 " ",
258 /* U+21B5 Downwards Arrow with Corner Leftwards */
259 "\342\206\265",
260 /* U+2026 Horizontal Ellipsis */
261 "\342\200\246",
262 "\342\200\246",
263 true
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 */
277 static int
278 integer_digits(const char *my_str)
280 /* ignoring any sign ... */
281 if (my_str[0] == '-' || my_str[0] == '+')
282 my_str++;
283 /* ... count initial integral digits */
284 return strspn(my_str, "0123456789");
287 /* Compute additional length required for locale-aware numeric output */
288 static int
289 additional_numeric_locale_len(const char *my_str)
291 int int_len = integer_digits(my_str),
292 len = 0;
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;
302 return len;
306 * Format a numeric value per current LC_NUMERIC locale setting
308 * Returns the appropriately formatted string in a new allocated block,
309 * caller must free.
311 * setDecimalLocale() must have been called earlier.
313 static char *
314 format_numeric_locale(const char *my_str)
316 char *new_str;
317 int new_len,
318 int_len,
319 leading_digits,
321 new_str_pos;
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);
332 new_str_pos = 0;
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;
340 /* process sign */
341 if (my_str[0] == '-' || my_str[0] == '+')
343 new_str[new_str_pos++] = my_str[0];
344 my_str++;
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);
365 i++;
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);
374 return new_str;
378 static void
379 print_separator(struct separator sep, FILE *fout)
381 if (sep.separator_zero)
382 fputc('\000', fout);
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),
407 total_records);
409 return &default_footer_cell;
411 else
412 return cont->footers;
416 /*************************/
417 /* Unaligned text */
418 /*************************/
421 static void
422 print_unaligned_text(const printTableContent *cont, FILE *fout)
424 bool opt_tuples_only = cont->opt->tuples_only;
425 unsigned int i;
426 const char *const *ptr;
427 bool need_recordsep = false;
429 if (cancel_pressed)
430 return;
432 if (cont->opt->start_table)
434 /* print title */
435 if (!opt_tuples_only && cont->title)
437 fputs(cont->title, fout);
438 print_separator(cont->opt->recordSep, fout);
441 /* print headers */
442 if (!opt_tuples_only)
444 for (ptr = cont->headers; *ptr; ptr++)
446 if (ptr != cont->headers)
447 print_separator(cont->opt->fieldSep, fout);
448 fputs(*ptr, fout);
450 need_recordsep = true;
453 else
454 /* assume continuing printout */
455 need_recordsep = true;
457 /* print cells */
458 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
460 if (need_recordsep)
462 print_separator(cont->opt->recordSep, fout);
463 need_recordsep = false;
464 if (cancel_pressed)
465 break;
467 fputs(*ptr, fout);
469 if ((i + 1) % cont->ncolumns)
470 print_separator(cont->opt->fieldSep, fout);
471 else
472 need_recordsep = true;
475 /* print footers */
476 if (cont->opt->stop_table)
478 printTableFooter *footers = footers_with_default(cont);
480 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
482 printTableFooter *f;
484 for (f = footers; f; f = f->next)
486 if (need_recordsep)
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).
501 if (need_recordsep)
503 if (cont->opt->recordSep.separator_zero)
504 print_separator(cont->opt->recordSep, fout);
505 else
506 fputc('\n', fout);
512 static void
513 print_unaligned_vertical(const printTableContent *cont, FILE *fout)
515 bool opt_tuples_only = cont->opt->tuples_only;
516 unsigned int i;
517 const char *const *ptr;
518 bool need_recordsep = false;
520 if (cancel_pressed)
521 return;
523 if (cont->opt->start_table)
525 /* print title */
526 if (!opt_tuples_only && cont->title)
528 fputs(cont->title, fout);
529 need_recordsep = true;
532 else
533 /* assume continuing printout */
534 need_recordsep = true;
536 /* print records */
537 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
539 if (need_recordsep)
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;
545 if (cancel_pressed)
546 break;
549 fputs(cont->headers[i % cont->ncolumns], fout);
550 print_separator(cont->opt->fieldSep, fout);
551 fputs(*ptr, fout);
553 if ((i + 1) % cont->ncolumns)
554 print_separator(cont->opt->recordSep, fout);
555 else
556 need_recordsep = true;
559 if (cont->opt->stop_table)
561 /* print footers */
562 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
564 printTableFooter *f;
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() */
575 if (need_recordsep)
577 if (cont->opt->recordSep.separator_zero)
578 print_separator(cont->opt->recordSep, fout);
579 else
580 fputc('\n', fout);
586 /********************/
587 /* Aligned text */
588 /********************/
591 /* draw "line" */
592 static void
593 _print_horizontal_line(const unsigned int ncolumns, const unsigned int *widths,
594 unsigned short border, printTextRule pos,
595 const printTextFormat *format,
596 FILE *fout)
598 const printTextLineFormat *lformat = &format->lrule[pos];
599 unsigned int i,
602 if (border == 1)
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)
614 if (border == 0)
615 fputc(' ', fout);
616 else
617 fprintf(fout, "%s%s%s", lformat->hrule,
618 lformat->midvrule, lformat->hrule);
622 if (border == 2)
623 fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
624 else if (border == 1)
625 fputs(lformat->hrule, fout);
627 fputc('\n', fout);
632 * Print pretty boxes around cells.
634 static void
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,
644 cell_count = 0;
646 unsigned int i,
649 unsigned int *width_header,
650 *max_width,
651 *width_wrap,
652 *width_average;
653 unsigned int *max_nl_lines, /* value split by newlines */
654 *curr_nl_line,
655 *max_bytes;
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;
672 if (cancel_pressed)
673 return;
675 if (opt_border > 2)
676 opt_border = 2;
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));
694 else
696 width_header = NULL;
697 width_average = NULL;
698 max_width = NULL;
699 width_wrap = NULL;
700 max_nl_lines = NULL;
701 curr_nl_line = NULL;
702 col_lineptrs = NULL;
703 max_bytes = NULL;
704 format_buf = NULL;
705 header_done = NULL;
706 bytes_output = NULL;
707 wrap = NULL;
710 /* scan all column headers, find maximum width and max max_nl_lines */
711 for (i = 0; i < col_count; i++)
713 int width,
714 nl_lines,
715 bytes_required;
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++)
737 int width,
738 nl_lines,
739 bytes_required;
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 */
764 if (opt_border == 0)
765 width_total = col_count;
766 else if (opt_border == 1)
767 width_total = col_count * 3 - ((col_count > 0) ? 1 : 0);
768 else
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
783 * for line pointers.
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;
809 #ifdef TIOCGWINSZ
810 else
812 struct winsize screen_size;
814 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
815 output_columns = screen_size.ws_col;
817 #endif
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
827 * headers
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;
835 int worst_col = -1;
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 */
848 double ratio;
850 ratio = (double) width_wrap[i] / width_average[i] +
851 max_width[i] * 0.01;
852 if (ratio > max_ratio)
854 max_ratio = ratio;
855 worst_col = i;
860 /* Exit loop if we can't squeeze any more. */
861 if (worst_col == -1)
862 break;
864 /* Decrease width of target column by one. */
865 width_wrap[worst_col]--;
866 width_total--;
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);
881 goto cleanup;
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++)
898 int width,
899 nl_lines,
900 bytes_required;
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)
922 i = 0;
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;
932 /* time to output */
933 if (cont->opt->start_table)
935 /* print title */
936 if (cont->title && !opt_tuples_only)
938 int width,
939 height;
941 pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
942 encoding, &width, &height, NULL);
943 if (width >= width_total)
944 /* Aligned */
945 fprintf(fout, "%s\n", cont->title);
946 else
947 /* Centered */
948 fprintf(fout, "%-*s%s\n", (width_total - width) / 2, "",
949 cont->title);
952 /* print headers */
953 if (!opt_tuples_only)
955 int more_col_wrapping;
956 int curr_nl_line;
958 if (opt_border == 2)
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;
968 curr_nl_line = 0;
969 if (col_count > 0)
970 memset(header_done, false, col_count * sizeof(bool));
971 while (more_col_wrapping)
973 if (opt_border == 2)
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 : " ",
984 fout);
986 if (!header_done[i])
988 nbspace = width_wrap[i] - this_line->width;
990 /* centered */
991 fprintf(fout, "%-*s%s%-*s",
992 nbspace / 2, "", this_line->ptr, (nbspace + 1) / 2, "");
994 if (!(this_line + 1)->ptr)
996 more_col_wrapping--;
997 header_done[i] = 1;
1000 else
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 : " ",
1005 fout);
1007 if (opt_border != 0 && col_count > 0 && i < col_count - 1)
1008 fputs(dformat->midvrule, fout);
1010 curr_nl_line++;
1012 if (opt_border == 2)
1013 fputs(dformat->rightvrule, fout);
1014 fputc('\n', 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)
1025 bool more_lines;
1027 if (cancel_pressed)
1028 break;
1031 * Format each cell.
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.
1049 more_lines = false;
1051 /* left border */
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);
1072 else
1073 fputc(' ', fout);
1076 if (!this_line->ptr)
1078 /* Past newline lines so just pad for other columns */
1079 if (finalspaces)
1080 fprintf(fout, "%*s", chars_to_output, "");
1082 else
1084 /* Get strlen() of the characters up to width_wrap */
1085 bytes_to_output =
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 */
1100 /* spaces first */
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 */
1107 /* spaces second */
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')
1116 more_lines = true;
1117 else
1119 /* Advance to next newline line */
1120 curr_nl_line[j]++;
1121 if (col_lineptrs[j][curr_nl_line[j]].ptr != NULL)
1122 more_lines = true;
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 */
1143 if (finalspaces ||
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))
1156 fputc(' ', fout);
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);
1167 else
1168 fputs(dformat->midvrule, fout);
1172 /* end-of-row border */
1173 if (opt_border == 2)
1174 fputs(dformat->rightvrule, fout);
1175 fputc('\n', 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);
1187 /* print footers */
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);
1196 fputc('\n', fout);
1199 cleanup:
1200 /* clean up */
1201 for (i = 0; i < col_count; i++)
1203 free(col_lineptrs[i]);
1204 free(format_buf[i]);
1206 free(width_header);
1207 free(width_average);
1208 free(max_width);
1209 free(width_wrap);
1210 free(max_nl_lines);
1211 free(curr_nl_line);
1212 free(col_lineptrs);
1213 free(max_bytes);
1214 free(format_buf);
1215 free(header_done);
1216 free(bytes_output);
1217 free(wrap);
1219 if (is_local_pager)
1220 ClosePager(fout);
1224 static void
1225 print_aligned_vertical_line(const printTableOpt *topt,
1226 unsigned long record,
1227 unsigned int hwidth,
1228 unsigned int dwidth,
1229 int output_columns,
1230 printTextRule pos,
1231 FILE *fout)
1233 const printTextLineFormat *lformat = &get_line_style(topt)->lrule[pos];
1234 const unsigned short opt_border = topt->border;
1235 unsigned int i;
1236 int reclen = 0;
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);
1243 if (record)
1245 if (opt_border == 0)
1246 reclen = fprintf(fout, "* Record %lu", record);
1247 else
1248 reclen = fprintf(fout, "[ RECORD %lu ]", record);
1250 if (opt_border != 2)
1251 reclen++;
1252 if (reclen < 0)
1253 reclen = 0;
1254 for (i = reclen; i < hwidth; i++)
1255 fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1256 reclen -= hwidth;
1258 if (opt_border > 0)
1260 if (reclen-- <= 0)
1261 fputs(lformat->hrule, fout);
1262 if (reclen-- <= 0)
1264 if (topt->expanded_header_width_type == PRINT_XHEADER_COLUMN)
1266 fputs(lformat->rightvrule, fout);
1268 else
1270 fputs(lformat->midvrule, fout);
1273 if (reclen-- <= 0
1274 && topt->expanded_header_width_type != PRINT_XHEADER_COLUMN)
1275 fputs(lformat->hrule, fout);
1277 else
1279 if (reclen-- <= 0)
1280 fputc(' ', 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)));
1309 if (reclen < 0)
1310 reclen = 0;
1311 if (dwidth < reclen)
1312 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);
1320 fputc('\n', fout);
1323 static void
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;
1334 unsigned int i,
1335 hwidth = 0,
1336 dwidth = 0,
1337 hheight = 1,
1338 dheight = 1,
1339 hformatsize = 0,
1340 dformatsize = 0;
1341 struct lineptr *hlineptr,
1342 *dlineptr;
1343 bool is_local_pager = false,
1344 hmultiline = false,
1345 dmultiline = false;
1346 int output_columns = 0; /* Width of interactive console */
1348 if (cancel_pressed)
1349 return;
1351 if (opt_border > 2)
1352 opt_border = 2;
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);
1367 fputc('\n', fout);
1369 return;
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.
1377 if (!is_pager)
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++)
1386 int width,
1387 height,
1390 pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
1391 encoding, &width, &height, &fs);
1392 if (width > hwidth)
1393 hwidth = width;
1394 if (height > hheight)
1396 hheight = height;
1397 hmultiline = true;
1399 if (fs > hformatsize)
1400 hformatsize = fs;
1403 /* find longest data cell */
1404 for (i = 0, ptr = cont->cells; *ptr; ptr++, i++)
1406 int width,
1407 height,
1410 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
1411 &width, &height, &fs);
1412 if (width > dwidth)
1413 dwidth = width;
1414 if (height > dheight)
1416 dheight = height;
1417 dmultiline = true;
1419 if (fs > dformatsize)
1420 dformatsize = fs;
1424 * We now have all the information we need to setup the formatting
1425 * structures
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)
1435 /* print title */
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;
1449 #ifdef TIOCGWINSZ
1450 else
1452 struct winsize screen_size;
1454 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
1455 output_columns = screen_size.ws_col;
1457 #endif
1461 * Calculate available width for data in wrapped mode
1463 if (cont->opt->format == PRINT_WRAPPED)
1465 unsigned int swidth,
1466 rwidth = 0,
1467 newdwidth;
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.)
1477 swidth = 1;
1479 /* We might need a column for header newline markers, too */
1480 if (hmultiline)
1481 swidth++;
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.)
1489 swidth = 3;
1491 /* We might need a column for left header newline markers, too */
1492 if (hmultiline && (format == &pg_asciiformat_old))
1493 swidth++;
1495 else
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.)
1503 swidth = 7;
1506 /* Reserve a column for data newline indicators, too, if needed */
1507 if (dmultiline &&
1508 opt_border < 2 && format != &pg_asciiformat_old)
1509 swidth++;
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 ]" */
1520 else
1521 rwidth += 15; /* "+-[ RECORD ]-+" */
1524 /* We might need to do the rest of the calculation twice */
1525 for (;;)
1527 unsigned int width;
1529 /* Total width required to not wrap data */
1530 width = hwidth + swidth + dwidth;
1531 /* ... and not the header lines, either */
1532 if (width < rwidth)
1533 width = rwidth;
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)
1543 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;
1556 else
1558 /* Set data width to match output_columns */
1559 newdwidth = output_columns - hwidth - swidth;
1562 else
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)
1576 dmultiline = true;
1577 swidth++;
1579 else
1580 break;
1583 dwidth = newdwidth;
1586 /* print records */
1587 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1589 printTextRule pos;
1590 int dline,
1591 hline,
1592 dcomplete,
1593 hcomplete,
1594 offset,
1595 chars_to_output;
1597 if (cancel_pressed)
1598 break;
1600 if (i == 0)
1601 pos = PRINT_RULE_TOP;
1602 else
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) &&
1611 (hmultiline) &&
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,
1618 pos, fout);
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,
1630 dlineptr, dheight);
1633 * Loop through header and data in parallel dealing with newlines and
1634 * wrapped lines until they're both exhausted
1636 dline = hline = 0;
1637 dcomplete = hcomplete = 0;
1638 offset = 0;
1639 chars_to_output = dlineptr[dline].width;
1640 while (!dcomplete || !hcomplete)
1642 /* Left border */
1643 if (opt_border == 2)
1644 fprintf(fout, "%s", dformat->leftvrule);
1646 /* Header (never wrapped so just need to deal with newlines) */
1647 if (!hcomplete)
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);
1660 * Header text
1662 strlen_max_width(hlineptr[hline].ptr, &target_width,
1663 encoding);
1664 fprintf(fout, "%-s", hlineptr[hline].ptr);
1667 * Spacer
1669 swidth -= target_width;
1670 if (swidth > 0)
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);
1682 hline++;
1684 else
1686 /* This was the last line of the header */
1687 if ((opt_border > 0) ||
1688 (hmultiline && (format != &pg_asciiformat_old)))
1689 fputs(" ", fout);
1690 hcomplete = 1;
1693 else
1695 unsigned int swidth = hwidth + opt_border;
1697 if ((opt_border < 2) &&
1698 (hmultiline) &&
1699 (format == &pg_asciiformat_old))
1700 swidth++;
1702 if ((opt_border == 0) &&
1703 (format != &pg_asciiformat_old) &&
1704 (hmultiline))
1705 swidth++;
1707 fprintf(fout, "%*s", swidth, " ");
1710 /* Separator */
1711 if (opt_border > 0)
1713 if (offset)
1714 fputs(format->midvrule_wrap, fout);
1715 else if (dline == 0)
1716 fputs(dformat->midvrule, fout);
1717 else
1718 fputs(format->midvrule_nl, fout);
1721 /* Data */
1722 if (!dcomplete)
1724 int target_width = dwidth,
1725 bytes_to_output,
1726 swidth = dwidth;
1729 * Left spacer or wrap indicator
1731 fputs(offset == 0 ? " " : format->wrap_left, fout);
1734 * Data text
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;
1744 /* Spacer */
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)))
1753 if (swidth > 0)
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)))
1764 if (swidth > 0)
1765 fprintf(fout, "%*s", swidth, " ");
1766 fputs(format->nl_right, fout);
1768 dline++;
1769 offset = 0;
1770 chars_to_output = dlineptr[dline].width;
1772 else
1774 /* reached the end of the cell */
1775 if (opt_border > 1)
1777 if (swidth > 0)
1778 fprintf(fout, "%*s", swidth, " ");
1779 fputs(" ", fout);
1781 dcomplete = 1;
1784 /* Right border */
1785 if (opt_border == 2)
1786 fputs(dformat->rightvrule, fout);
1788 fputs("\n", fout);
1790 else
1793 * data exhausted (this can occur if header is longer than the
1794 * data due to newlines in the header)
1796 if (opt_border < 2)
1797 fputs("\n", fout);
1798 else
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);
1810 /* print footers */
1811 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
1813 printTableFooter *f;
1815 if (opt_border < 2)
1816 fputc('\n', fout);
1817 for (f = cont->footers; f; f = f->next)
1818 fprintf(fout, "%s\n", f->data);
1821 fputc('\n', fout);
1824 free(hlineptr->ptr);
1825 free(dlineptr->ptr);
1826 free(hlineptr);
1827 free(dlineptr);
1829 if (is_local_pager)
1830 ClosePager(fout);
1834 /**********************/
1835 /* CSV format */
1836 /**********************/
1839 static void
1840 csv_escaped_print(const char *str, FILE *fout)
1842 const char *p;
1844 fputc('"', fout);
1845 for (p = str; *p; p++)
1847 if (*p == '"')
1848 fputc('"', fout); /* double quotes are doubled */
1849 fputc(*p, fout);
1851 fputc('"', fout);
1854 static void
1855 csv_print_field(const char *str, FILE *fout, char sep)
1857 /*----------------
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.
1868 *----------------
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);
1875 else
1876 fputs(str, fout);
1879 static void
1880 print_csv_text(const printTableContent *cont, FILE *fout)
1882 const char *const *ptr;
1883 int i;
1885 if (cancel_pressed)
1886 return;
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)
1898 /* print headers */
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]);
1905 fputc('\n', fout);
1908 /* print cells */
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);
1914 else
1915 fputc('\n', fout);
1919 static void
1920 print_csv_vertical(const printTableContent *cont, FILE *fout)
1922 const char *const *ptr;
1923 int i;
1925 /* print records */
1926 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1928 if (cancel_pressed)
1929 return;
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]);
1941 fputc('\n', fout);
1946 /**********************/
1947 /* HTML */
1948 /**********************/
1951 void
1952 html_escaped_print(const char *in, FILE *fout)
1954 const char *p;
1955 bool leading_space = true;
1957 for (p = in; *p; p++)
1959 switch (*p)
1961 case '&':
1962 fputs("&amp;", fout);
1963 break;
1964 case '<':
1965 fputs("&lt;", fout);
1966 break;
1967 case '>':
1968 fputs("&gt;", fout);
1969 break;
1970 case '\n':
1971 fputs("<br />\n", fout);
1972 break;
1973 case '"':
1974 fputs("&quot;", fout);
1975 break;
1976 case ' ':
1977 /* protect leading space, for EXPLAIN output */
1978 if (leading_space)
1979 fputs("&nbsp;", fout);
1980 else
1981 fputs(" ", fout);
1982 break;
1983 default:
1984 fputc(*p, fout);
1986 if (*p != ' ')
1987 leading_space = false;
1992 static void
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;
1998 unsigned int i;
1999 const char *const *ptr;
2001 if (cancel_pressed)
2002 return;
2004 if (cont->opt->start_table)
2006 fprintf(fout, "<table border=\"%d\"", opt_border);
2007 if (opt_table_attr)
2008 fprintf(fout, " %s", opt_table_attr);
2009 fputs(">\n", fout);
2011 /* print title */
2012 if (!opt_tuples_only && cont->title)
2014 fputs(" <caption>", fout);
2015 html_escaped_print(cont->title, fout);
2016 fputs("</caption>\n", fout);
2019 /* print headers */
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);
2033 /* print cells */
2034 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2036 if (i % cont->ncolumns == 0)
2038 if (cancel_pressed)
2039 break;
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("&nbsp; ", fout);
2047 else
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);
2062 /* print footers */
2063 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
2065 printTableFooter *f;
2067 fputs("<p>", fout);
2068 for (f = footers; f; f = f->next)
2070 html_escaped_print(f->data, fout);
2071 fputs("<br />\n", fout);
2073 fputs("</p>", fout);
2076 fputc('\n', fout);
2081 static void
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;
2088 unsigned int i;
2089 const char *const *ptr;
2091 if (cancel_pressed)
2092 return;
2094 if (cont->opt->start_table)
2096 fprintf(fout, "<table border=\"%d\"", opt_border);
2097 if (opt_table_attr)
2098 fprintf(fout, " %s", opt_table_attr);
2099 fputs(">\n", fout);
2101 /* print title */
2102 if (!opt_tuples_only && cont->title)
2104 fputs(" <caption>", fout);
2105 html_escaped_print(cont->title, fout);
2106 fputs("</caption>\n", fout);
2110 /* print records */
2111 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2113 if (i % cont->ncolumns == 0)
2115 if (cancel_pressed)
2116 break;
2117 if (!opt_tuples_only)
2118 fprintf(fout,
2119 "\n <tr><td colspan=\"2\" align=\"center\">Record %lu</td></tr>\n",
2120 record++);
2121 else
2122 fputs("\n <tr><td colspan=\"2\">&nbsp;</td></tr>\n", fout);
2124 fputs(" <tr valign=\"top\">\n"
2125 " <th>", fout);
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("&nbsp; ", fout);
2133 else
2134 html_escaped_print(*ptr, fout);
2136 fputs("</td>\n </tr>\n", fout);
2139 if (cont->opt->stop_table)
2141 fputs("</table>\n", fout);
2143 /* print footers */
2144 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
2146 printTableFooter *f;
2148 fputs("<p>", fout);
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);
2157 fputc('\n', fout);
2162 /*************************/
2163 /* ASCIIDOC */
2164 /*************************/
2167 static void
2168 asciidoc_escaped_print(const char *in, FILE *fout)
2170 const char *p;
2172 for (p = in; *p; p++)
2174 switch (*p)
2176 case '|':
2177 fputs("\\|", fout);
2178 break;
2179 default:
2180 fputc(*p, fout);
2185 static void
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;
2190 unsigned int i;
2191 const char *const *ptr;
2193 if (cancel_pressed)
2194 return;
2196 if (cont->opt->start_table)
2198 /* print table in new paragraph - enforce preliminary new line */
2199 fputs("\n", fout);
2201 /* print title */
2202 if (!opt_tuples_only && cont->title)
2204 fputs(".", fout);
2205 fputs(cont->title, fout);
2206 fputs("\n", fout);
2209 /* print table [] header definition */
2210 fprintf(fout, "[%scols=\"", !opt_tuples_only ? "options=\"header\"," : "");
2211 for (i = 0; i < cont->ncolumns; i++)
2213 if (i != 0)
2214 fputs(",", fout);
2215 fprintf(fout, "%s", cont->aligns[(i) % cont->ncolumns] == 'r' ? ">l" : "<l");
2217 fputs("\"", fout);
2218 switch (opt_border)
2220 case 0:
2221 fputs(",frame=\"none\",grid=\"none\"", fout);
2222 break;
2223 case 1:
2224 fputs(",frame=\"none\"", fout);
2225 break;
2226 case 2:
2227 fputs(",frame=\"all\",grid=\"all\"", fout);
2228 break;
2230 fputs("]\n", fout);
2231 fputs("|====\n", fout);
2233 /* print headers */
2234 if (!opt_tuples_only)
2236 for (ptr = cont->headers; *ptr; ptr++)
2238 if (ptr != cont->headers)
2239 fputs(" ", fout);
2240 fputs("^l|", fout);
2241 asciidoc_escaped_print(*ptr, fout);
2243 fputs("\n", fout);
2247 /* print cells */
2248 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2250 if (i % cont->ncolumns == 0)
2252 if (cancel_pressed)
2253 break;
2256 if (i % cont->ncolumns != 0)
2257 fputs(" ", fout);
2258 fputs("|", fout);
2260 /* protect against needless spaces */
2261 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2263 if ((i + 1) % cont->ncolumns != 0)
2264 fputs(" ", fout);
2266 else
2267 asciidoc_escaped_print(*ptr, fout);
2269 if ((i + 1) % cont->ncolumns == 0)
2270 fputs("\n", fout);
2273 fputs("|====\n", fout);
2275 if (cont->opt->stop_table)
2277 printTableFooter *footers = footers_with_default(cont);
2279 /* print footers */
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);
2288 fputs("\n", fout);
2290 fputs("....\n", fout);
2295 static void
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;
2301 unsigned int i;
2302 const char *const *ptr;
2304 if (cancel_pressed)
2305 return;
2307 if (cont->opt->start_table)
2309 /* print table in new paragraph - enforce preliminary new line */
2310 fputs("\n", fout);
2312 /* print title */
2313 if (!opt_tuples_only && cont->title)
2315 fputs(".", fout);
2316 fputs(cont->title, fout);
2317 fputs("\n", fout);
2320 /* print table [] header definition */
2321 fputs("[cols=\"h,l\"", fout);
2322 switch (opt_border)
2324 case 0:
2325 fputs(",frame=\"none\",grid=\"none\"", fout);
2326 break;
2327 case 1:
2328 fputs(",frame=\"none\"", fout);
2329 break;
2330 case 2:
2331 fputs(",frame=\"all\",grid=\"all\"", fout);
2332 break;
2334 fputs("]\n", fout);
2335 fputs("|====\n", fout);
2338 /* print records */
2339 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2341 if (i % cont->ncolumns == 0)
2343 if (cancel_pressed)
2344 break;
2345 if (!opt_tuples_only)
2346 fprintf(fout,
2347 "2+^|Record %lu\n",
2348 record++);
2349 else
2350 fputs("2+|\n", fout);
2353 fputs("<l|", 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')
2359 fputs(" ", fout);
2360 else
2361 asciidoc_escaped_print(*ptr, fout);
2362 fputs("\n", fout);
2365 fputs("|====\n", fout);
2367 if (cont->opt->stop_table)
2369 /* print footers */
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);
2378 fputs("\n", fout);
2380 fputs("....\n", fout);
2386 /*************************/
2387 /* LaTeX */
2388 /*************************/
2391 static void
2392 latex_escaped_print(const char *in, FILE *fout)
2394 const char *p;
2396 for (p = in; *p; p++)
2397 switch (*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.
2404 case '#':
2405 fputs("\\#", fout);
2406 break;
2407 case '$':
2408 fputs("\\$", fout);
2409 break;
2410 case '%':
2411 fputs("\\%", fout);
2412 break;
2413 case '&':
2414 fputs("\\&", fout);
2415 break;
2416 case '<':
2417 fputs("\\textless{}", fout);
2418 break;
2419 case '>':
2420 fputs("\\textgreater{}", fout);
2421 break;
2422 case '\\':
2423 fputs("\\textbackslash{}", fout);
2424 break;
2425 case '^':
2426 fputs("\\^{}", fout);
2427 break;
2428 case '_':
2429 fputs("\\_", fout);
2430 break;
2431 case '{':
2432 fputs("\\{", fout);
2433 break;
2434 case '|':
2435 fputs("\\textbar{}", fout);
2436 break;
2437 case '}':
2438 fputs("\\}", fout);
2439 break;
2440 case '~':
2441 fputs("\\~{}", fout);
2442 break;
2443 case '\n':
2444 /* This is not right, but doing it right seems too hard */
2445 fputs("\\\\", fout);
2446 break;
2447 default:
2448 fputc(*p, fout);
2453 static void
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;
2458 unsigned int i;
2459 const char *const *ptr;
2461 if (cancel_pressed)
2462 return;
2464 if (opt_border > 3)
2465 opt_border = 3;
2467 if (cont->opt->start_table)
2469 /* print title */
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)
2481 fputs("| ", fout);
2482 for (i = 0; i < cont->ncolumns; i++)
2484 fputc(*(cont->aligns + i), fout);
2485 if (opt_border != 0 && i < cont->ncolumns - 1)
2486 fputs(" | ", fout);
2488 if (opt_border >= 2)
2489 fputs(" |", fout);
2491 fputs("}\n", fout);
2493 if (!opt_tuples_only && opt_border >= 2)
2494 fputs("\\hline\n", fout);
2496 /* print headers */
2497 if (!opt_tuples_only)
2499 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2501 if (i != 0)
2502 fputs(" & ", fout);
2503 fputs("\\textit{", fout);
2504 latex_escaped_print(*ptr, fout);
2505 fputc('}', fout);
2507 fputs(" \\\\\n", fout);
2508 fputs("\\hline\n", fout);
2512 /* print cells */
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);
2522 if (cancel_pressed)
2523 break;
2525 else
2526 fputs(" & ", 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);
2538 /* print footers */
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);
2550 fputc('\n', fout);
2555 /*************************/
2556 /* LaTeX longtable */
2557 /*************************/
2560 static void
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;
2565 unsigned int i;
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;
2571 if (cancel_pressed)
2572 return;
2574 if (opt_border > 3)
2575 opt_border = 3;
2577 if (cont->opt->start_table)
2579 /* begin environment and set alignments and borders */
2580 fputs("\\begin{longtable}{", fout);
2582 if (opt_border >= 2)
2583 fputs("| ", fout);
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')
2599 fputs("p{", fout);
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)
2610 fputs("p{", fout);
2611 fwrite(last_opt_table_attr_char, strcspn(last_opt_table_attr_char,
2612 LONGTABLE_WHITESPACE), 1, fout);
2613 fputs("\\textwidth}", fout);
2615 else
2616 fputc('l', fout);
2618 else
2619 fputc(*(cont->aligns + i), fout);
2621 if (opt_border != 0 && i < cont->ncolumns - 1)
2622 fputs(" | ", fout);
2625 if (opt_border >= 2)
2626 fputs(" |", fout);
2628 fputs("}\n", fout);
2630 /* print headers */
2631 if (!opt_tuples_only)
2633 /* firsthead */
2634 if (opt_border >= 2)
2635 fputs("\\toprule\n", fout);
2636 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2638 if (i != 0)
2639 fputs(" & ", fout);
2640 fputs("\\small\\textbf{\\textit{", fout);
2641 latex_escaped_print(*ptr, fout);
2642 fputs("}}", 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++)
2652 if (i != 0)
2653 fputs(" & ", fout);
2654 fputs("\\small\\textbf{\\textit{", fout);
2655 latex_escaped_print(*ptr, fout);
2656 fputs("}}", 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);
2679 fputs("]{", 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);
2692 /* print cells */
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);
2700 fputc('}', fout);
2701 if ((i + 1) % cont->ncolumns == 0)
2703 fputs(" \\tabularnewline\n", fout);
2704 if (opt_border == 3)
2705 fputs(" \\hline\n", fout);
2707 if (cancel_pressed)
2708 break;
2711 if (cont->opt->stop_table)
2712 fputs("\\end{longtable}\n", fout);
2716 static void
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;
2722 unsigned int i;
2723 const char *const *ptr;
2725 if (cancel_pressed)
2726 return;
2728 if (opt_border > 2)
2729 opt_border = 2;
2731 if (cont->opt->start_table)
2733 /* print title */
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)
2744 fputs("cl", fout);
2745 else if (opt_border == 1)
2746 fputs("c|l", fout);
2747 else if (opt_border == 2)
2748 fputs("|c|l|", fout);
2749 fputs("}\n", fout);
2752 /* print records */
2753 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2755 /* new record */
2756 if (i % cont->ncolumns == 0)
2758 if (cancel_pressed)
2759 break;
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++);
2767 else
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);
2775 fputs(" & ", 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);
2787 /* print footers */
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);
2799 fputc('\n', fout);
2804 /*************************/
2805 /* Troff -ms */
2806 /*************************/
2809 static void
2810 troff_ms_escaped_print(const char *in, FILE *fout)
2812 const char *p;
2814 for (p = in; *p; p++)
2815 switch (*p)
2817 case '\\':
2818 fputs("\\(rs", fout);
2819 break;
2820 default:
2821 fputc(*p, fout);
2826 static void
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;
2831 unsigned int i;
2832 const char *const *ptr;
2834 if (cancel_pressed)
2835 return;
2837 if (opt_border > 2)
2838 opt_border = 2;
2840 if (cont->opt->start_table)
2842 /* print title */
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);
2854 else
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)
2861 fputs(" | ", fout);
2863 fputs(".\n", fout);
2865 /* print headers */
2866 if (!opt_tuples_only)
2868 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2870 if (i != 0)
2871 fputc('\t', fout);
2872 fputs("\\fI", fout);
2873 troff_ms_escaped_print(*ptr, fout);
2874 fputs("\\fP", fout);
2876 fputs("\n_\n", fout);
2880 /* print cells */
2881 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2883 troff_ms_escaped_print(*ptr, fout);
2885 if ((i + 1) % cont->ncolumns == 0)
2887 fputc('\n', fout);
2888 if (cancel_pressed)
2889 break;
2891 else
2892 fputc('\t', fout);
2895 if (cont->opt->stop_table)
2897 printTableFooter *footers = footers_with_default(cont);
2899 fputs(".TE\n.DS L\n", fout);
2901 /* print footers */
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);
2909 fputc('\n', fout);
2913 fputs(".DE\n", fout);
2918 static void
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;
2924 unsigned int i;
2925 const char *const *ptr;
2926 unsigned short current_format = 0; /* 0=none, 1=header, 2=body */
2928 if (cancel_pressed)
2929 return;
2931 if (opt_border > 2)
2932 opt_border = 2;
2934 if (cont->opt->start_table)
2936 /* print title */
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);
2948 else
2949 fputs("center;\n", fout);
2951 /* basic format */
2952 if (opt_tuples_only)
2953 fputs("c l;\n", fout);
2955 else
2956 current_format = 2; /* assume tuples printed already */
2958 /* print records */
2959 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2961 /* new record */
2962 if (i % cont->ncolumns == 0)
2964 if (cancel_pressed)
2965 break;
2966 if (!opt_tuples_only)
2968 if (current_format != 1)
2970 if (opt_border == 2 && record > 1)
2971 fputs("_\n", fout);
2972 if (current_format != 0)
2973 fputs(".T&\n", fout);
2974 fputs("c s.\n", fout);
2975 current_format = 1;
2977 fprintf(fout, "\\fIRecord %lu\\fP\n", record++);
2979 if (opt_border >= 1)
2980 fputs("_\n", fout);
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);
2991 else
2992 fputs("c | l.\n", fout);
2993 current_format = 2;
2997 troff_ms_escaped_print(cont->headers[i % cont->ncolumns], fout);
2998 fputc('\t', fout);
2999 troff_ms_escaped_print(*ptr, fout);
3001 fputc('\n', fout);
3004 if (cont->opt->stop_table)
3006 fputs(".TE\n.DS L\n", fout);
3008 /* print footers */
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);
3016 fputc('\n', 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.
3038 void
3039 disable_sigpipe_trap(void)
3041 #ifndef WIN32
3042 pqsignal(SIGPIPE, SIG_IGN);
3043 #endif
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.
3061 void
3062 restore_sigpipe_trap(void)
3064 #ifndef WIN32
3065 pqsignal(SIGPIPE, always_ignore_sigpipe ? SIG_IGN : SIG_DFL);
3066 #endif
3070 * set_sigpipe_trap_state
3072 * Set the trap state that restore_sigpipe_trap should restore to.
3074 void
3075 set_sigpipe_trap_state(bool ignore)
3077 always_ignore_sigpipe = ignore;
3082 * PageOutput
3084 * Tests if pager is needed and returns appropriate FILE pointer.
3086 * If the topt argument is NULL no pager is used.
3088 FILE *
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)))
3094 #ifdef TIOCGWINSZ
3095 unsigned short int pager = topt->pager;
3096 int min_lines = topt->pager_min_lines;
3097 int result;
3098 struct winsize screen_size;
3100 result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
3102 /* >= accounts for a one-line prompt */
3103 if (result == -1
3104 || (lines >= screen_size.ws_row && lines >= min_lines)
3105 || pager > 1)
3106 #endif
3108 const char *pagerprog;
3109 FILE *pagerpipe;
3111 pagerprog = getenv("PSQL_PAGER");
3112 if (!pagerprog)
3113 pagerprog = getenv("PAGER");
3114 if (!pagerprog)
3115 pagerprog = DEFAULT_PAGER;
3116 else
3118 /* if PAGER is empty or all-white-space, don't use pager */
3119 if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
3120 return stdout;
3122 fflush(NULL);
3123 disable_sigpipe_trap();
3124 pagerpipe = popen(pagerprog, "w");
3125 if (pagerpipe)
3126 return pagerpipe;
3127 /* if popen fails, silently proceed without pager */
3128 restore_sigpipe_trap();
3132 return stdout;
3136 * ClosePager
3138 * Close previously opened pager pipe, if any
3140 void
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
3151 * anywhere ...
3153 if (cancel_pressed)
3154 fprintf(pagerpipe, _("Interrupted\n"));
3156 pclose(pagerpipe);
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
3169 * table.
3171 void
3172 printTableInit(printTableContent *const content, const printTableOpt *opt,
3173 const char *title, const int ncolumns, const int nrows)
3175 content->opt = opt;
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
3206 * column.
3208 void
3209 printTableAddHeader(printTableContent *const content, char *header,
3210 const bool translate, const char align)
3212 #ifndef ENABLE_NLS
3213 (void) translate; /* unused parameter */
3214 #endif
3216 if (content->header >= content->headers + content->ncolumns)
3218 fprintf(stderr, _("Cannot add header to table content: "
3219 "column count of %d exceeded.\n"),
3220 content->ncolumns);
3221 exit(EXIT_FAILURE);
3224 *content->header = (char *) mbvalidate((unsigned char *) header,
3225 content->opt->encoding);
3226 #ifdef ENABLE_NLS
3227 if (translate)
3228 *content->header = _(*content->header);
3229 #endif
3230 content->header++;
3232 *content->align = align;
3233 content->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.
3248 void
3249 printTableAddCell(printTableContent *const content, char *cell,
3250 const bool translate, const bool mustfree)
3252 #ifndef ENABLE_NLS
3253 (void) translate; /* unused parameter */
3254 #endif
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);
3261 exit(EXIT_FAILURE);
3264 *content->cell = (char *) mbvalidate((unsigned char *) cell,
3265 content->opt->encoding);
3267 #ifdef ENABLE_NLS
3268 if (translate)
3269 *content->cell = _(*content->cell);
3270 #endif
3272 if (mustfree)
3274 if (content->cellmustfree == NULL)
3275 content->cellmustfree =
3276 pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool));
3278 content->cellmustfree[content->cellsadded] = true;
3280 content->cell++;
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.
3296 void
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;
3306 else
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
3319 * around.
3321 void
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);
3329 else
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.
3339 void
3340 printTableCleanup(printTableContent *const content)
3342 if (content->cellmustfree)
3344 int i;
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;
3375 free(f->data);
3376 free(f);
3379 content->footers = NULL;
3380 content->footer = NULL;
3384 * IsPagerNeeded
3386 * Setup pager if required
3388 static void
3389 IsPagerNeeded(const printTableContent *cont, int extra_lines, bool expanded,
3390 FILE **fout, bool *is_pager)
3392 if (*fout == stdout)
3394 int lines;
3396 if (expanded)
3397 lines = (cont->ncolumns + 1) * cont->nrows;
3398 else
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)
3410 lines++;
3413 *fout = PageOutput(lines + extra_lines, cont->opt);
3414 *is_pager = (*fout != stdout);
3416 else
3417 *is_pager = false;
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)
3428 void
3429 printTable(const printTableContent *cont,
3430 FILE *fout, bool is_pager, FILE *flog)
3432 bool is_local_pager = false;
3434 if (cancel_pressed)
3435 return;
3437 if (cont->opt->format == PRINT_NOTHING)
3438 return;
3440 /* print_aligned_*() handle the pager themselves */
3441 if (!is_pager &&
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 */
3450 clearerr(fout);
3452 /* print the stuff */
3454 if (flog)
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);
3462 else
3463 print_unaligned_text(cont, fout);
3464 break;
3465 case PRINT_ALIGNED:
3466 case PRINT_WRAPPED:
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
3471 * query result.
3473 if (cont->opt->expanded == 1 ||
3474 (cont->opt->expanded == 2 && is_pager))
3475 print_aligned_vertical(cont, fout, is_pager);
3476 else
3477 print_aligned_text(cont, fout, is_pager);
3478 break;
3479 case PRINT_CSV:
3480 if (cont->opt->expanded == 1)
3481 print_csv_vertical(cont, fout);
3482 else
3483 print_csv_text(cont, fout);
3484 break;
3485 case PRINT_HTML:
3486 if (cont->opt->expanded == 1)
3487 print_html_vertical(cont, fout);
3488 else
3489 print_html_text(cont, fout);
3490 break;
3491 case PRINT_ASCIIDOC:
3492 if (cont->opt->expanded == 1)
3493 print_asciidoc_vertical(cont, fout);
3494 else
3495 print_asciidoc_text(cont, fout);
3496 break;
3497 case PRINT_LATEX:
3498 if (cont->opt->expanded == 1)
3499 print_latex_vertical(cont, fout);
3500 else
3501 print_latex_text(cont, fout);
3502 break;
3503 case PRINT_LATEX_LONGTABLE:
3504 if (cont->opt->expanded == 1)
3505 print_latex_vertical(cont, fout);
3506 else
3507 print_latex_longtable_text(cont, fout);
3508 break;
3509 case PRINT_TROFF_MS:
3510 if (cont->opt->expanded == 1)
3511 print_troff_ms_vertical(cont, fout);
3512 else
3513 print_troff_ms_text(cont, fout);
3514 break;
3515 default:
3516 fprintf(stderr, _("invalid output format (internal error): %d"),
3517 cont->opt->format);
3518 exit(EXIT_FAILURE);
3521 if (is_local_pager)
3522 ClosePager(fout);
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)
3534 void
3535 printQuery(const PGresult *result, const printQueryOpt *opt,
3536 FILE *fout, bool is_pager, FILE *flog)
3538 printTableContent cont;
3539 int i,
3543 if (cancel_pressed)
3544 return;
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)));
3560 /* set cells */
3561 for (r = 0; r < cont.nrows; r++)
3563 for (c = 0; c < cont.ncolumns; c++)
3565 char *cell;
3566 bool mustfree = false;
3567 bool translate;
3569 if (PQgetisnull(result, r, c))
3570 cell = opt->nullPrint ? opt->nullPrint : "";
3571 else
3573 cell = PQgetvalue(result, r, c);
3574 if (cont.aligns[c] == 'r' && opt->topt.numericLocale)
3576 cell = format_numeric_locale(cell);
3577 mustfree = true;
3581 translate = (opt->translate_columns && opt->translate_columns[c]);
3582 printTableAddCell(&cont, cell, translate, mustfree);
3586 /* set footers */
3587 if (opt->footers)
3589 char **footer;
3591 for (footer = opt->footers; *footer; footer++)
3592 printTableAddFooter(&cont, *footer);
3595 printTable(&cont, fout, is_pager, flog);
3596 printTableCleanup(&cont);
3599 char
3600 column_type_alignment(Oid ftype)
3602 char align;
3604 switch (ftype)
3606 case INT2OID:
3607 case INT4OID:
3608 case INT8OID:
3609 case FLOAT4OID:
3610 case FLOAT8OID:
3611 case NUMERICOID:
3612 case OIDOID:
3613 case XIDOID:
3614 case XID8OID:
3615 case CIDOID:
3616 case MONEYOID:
3617 align = 'r';
3618 break;
3619 default:
3620 align = 'l';
3621 break;
3623 return align;
3626 void
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);
3636 else
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
3644 * CHAR_MAX values.
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 = ",";
3657 else
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
3668 * behavior.
3670 if (opt->line_style != NULL)
3671 return opt->line_style;
3672 else
3673 return &pg_asciiformat;
3676 void
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;
3706 /* N/A */
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.
3731 static int
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);
3736 int curr_width = 0;
3738 while (str < end)
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
3746 * accept it.
3748 if (*target_width < curr_width + char_width && curr_width != 0)
3749 break;
3751 curr_width += char_width;
3753 str += PQmblen((char *) str, encoding);
3755 if (str > end) /* Don't overrun invalid string */
3756 str = end;
3759 *target_width = curr_width;
3761 return str - start;