2 * psql - the PostgreSQL interactive terminal
4 * Copyright (c) 2000-2016, PostgreSQL Global Development Group
8 #include "postgres_fe.h"
16 #include <sys/ioctl.h> /* for ioctl() */
25 #include "catalog/pg_type.h"
32 * We define the cancel_pressed flag in this file, rather than common.c where
33 * it naturally belongs, because this file is also used by non-psql programs
34 * (see the bin/scripts/ directory). In those programs cancel_pressed will
35 * never become set and will have no effect.
37 * Note: print.c's general strategy for when to check cancel_pressed is to do
38 * so at completion of each row of output.
40 volatile bool cancel_pressed = false;
43 * Likewise, the sigpipe_trap and pager open/close functions are here rather
44 * than in common.c so that this file can be used by non-psql programs.
46 static bool always_ignore_sigpipe = false;
49 /* info for locale-aware numeric formatting; set up by setDecimalLocale() */
50 static char *decimal_point;
51 static int groupdigits;
52 static char *thousands_sep;
54 static char default_footer[100];
55 static printTableFooter default_footer_cell = {default_footer, NULL};
57 /* Line style control structures */
58 const printTextFormat pg_asciiformat =
79 const printTextFormat pg_asciiformat_old =
100 /* Default unicode linestyle format */
101 printTextFormat pg_utf8format;
103 typedef struct unicodeStyleRowFormat
105 const char *horizontal;
106 const char *vertical_and_right[2];
107 const char *vertical_and_left[2];
108 } unicodeStyleRowFormat;
110 typedef struct unicodeStyleColumnFormat
112 const char *vertical;
113 const char *vertical_and_horizontal[2];
114 const char *up_and_horizontal[2];
115 const char *down_and_horizontal[2];
116 } unicodeStyleColumnFormat;
118 typedef struct unicodeStyleBorderFormat
120 const char *up_and_right;
121 const char *vertical;
122 const char *down_and_right;
123 const char *horizontal;
124 const char *down_and_left;
125 const char *left_and_right;
126 } unicodeStyleBorderFormat;
128 typedef struct unicodeStyleFormat
130 unicodeStyleRowFormat row_style[2];
131 unicodeStyleColumnFormat column_style[2];
132 unicodeStyleBorderFormat border_style[2];
133 const char *header_nl_left;
134 const char *header_nl_right;
136 const char *nl_right;
137 const char *wrap_left;
138 const char *wrap_right;
139 bool wrap_right_border;
140 } unicodeStyleFormat;
142 const unicodeStyleFormat unicode_style = {
148 {"\342\224\234", "\342\225\237"},
150 {"\342\224\244", "\342\225\242"},
156 {"\342\225\236", "\342\225\240"},
158 {"\342\225\241", "\342\225\243"},
166 {"\342\224\274", "\342\225\252"},
168 {"\342\224\264", "\342\225\247"},
170 {"\342\224\254", "\342\225\244"},
176 {"\342\225\253", "\342\225\254"},
178 {"\342\225\250", "\342\225\251"},
180 {"\342\225\245", "\342\225\246"},
185 {"\342\224\224", "\342\224\202", "\342\224\214", "\342\224\200", "\342\224\220", "\342\224\230"},
187 {"\342\225\232", "\342\225\221", "\342\225\224", "\342\225\220", "\342\225\227", "\342\225\235"},
190 "\342\206\265", /* ↵ */
192 "\342\206\265", /* ↵ */
193 "\342\200\246", /* … */
194 "\342\200\246", /* … */
199 /* Local functions */
200 static int strlen_max_width(unsigned char *str, int *target_width, int encoding);
201 static void IsPagerNeeded(const printTableContent *cont, int extra_lines, bool expanded,
202 FILE **fout, bool *is_pager);
204 static void print_aligned_vertical(const printTableContent *cont,
205 FILE *fout, bool is_pager);
208 /* Count number of digits in integral part of number */
210 integer_digits(const char *my_str)
212 /* ignoring any sign ... */
213 if (my_str[0] == '-' || my_str[0] == '+')
215 /* ... count initial integral digits */
216 return strspn(my_str, "0123456789");
219 /* Compute additional length required for locale-aware numeric output */
221 additional_numeric_locale_len(const char *my_str)
223 int int_len = integer_digits(my_str),
226 /* Account for added thousands_sep instances */
227 if (int_len > groupdigits)
228 len += ((int_len - 1) / groupdigits) * strlen(thousands_sep);
230 /* Account for possible additional length of decimal_point */
231 if (strchr(my_str, '.') != NULL)
232 len += strlen(decimal_point) - 1;
238 * Format a numeric value per current LC_NUMERIC locale setting
240 * Returns the appropriately formatted string in a new allocated block,
243 * setDecimalLocale() must have been called earlier.
246 format_numeric_locale(const char *my_str)
256 * If the string doesn't look like a number, return it unchanged. This
257 * check is essential to avoid mangling already-localized "money" values.
259 if (strspn(my_str, "0123456789+-.eE") != strlen(my_str))
260 return pg_strdup(my_str);
262 new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
263 new_str = pg_malloc(new_len + 1);
265 int_len = integer_digits(my_str);
267 /* number of digits in first thousands group */
268 leading_digits = int_len % groupdigits;
269 if (leading_digits == 0)
270 leading_digits = groupdigits;
273 if (my_str[0] == '-' || my_str[0] == '+')
275 new_str[new_str_pos++] = my_str[0];
279 /* process integer part of number */
280 for (i = 0; i < int_len; i++)
282 /* Time to insert separator? */
283 if (i > 0 && --leading_digits == 0)
285 strcpy(&new_str[new_str_pos], thousands_sep);
286 new_str_pos += strlen(thousands_sep);
287 leading_digits = groupdigits;
289 new_str[new_str_pos++] = my_str[i];
292 /* handle decimal point if any */
293 if (my_str[i] == '.')
295 strcpy(&new_str[new_str_pos], decimal_point);
296 new_str_pos += strlen(decimal_point);
300 /* copy the rest (fractional digits and/or exponent, and \0 terminator) */
301 strcpy(&new_str[new_str_pos], &my_str[i]);
303 /* assert we didn't underestimate new_len (an overestimate is OK) */
304 Assert(strlen(new_str) <= new_len);
311 * fputnbytes: print exactly N bytes to a file
313 * We avoid using %.*s here because it can misbehave if the data
314 * is not valid in what libc thinks is the prevailing encoding.
317 fputnbytes(FILE *f, const char *str, size_t n)
325 print_separator(struct separator sep, FILE *fout)
327 if (sep.separator_zero)
329 else if (sep.separator)
330 fputs(sep.separator, fout);
335 * Return the list of explicitly-requested footers or, when applicable, the
336 * default "(xx rows)" footer. Always omit the default footer when given
337 * non-default footers, "\pset footer off", or a specific instruction to that
338 * effect from a calling backslash command. Vertical formats number each row,
339 * making the default footer redundant; they do not call this function.
341 * The return value may point to static storage; do not keep it across calls.
343 static printTableFooter *
344 footers_with_default(const printTableContent *cont)
346 if (cont->footers == NULL && cont->opt->default_footer)
348 unsigned long total_records;
350 total_records = cont->opt->prior_records + cont->nrows;
351 snprintf(default_footer, sizeof(default_footer),
352 ngettext("(%lu row)", "(%lu rows)", total_records),
355 return &default_footer_cell;
358 return cont->footers;
362 /*************************/
364 /*************************/
368 print_unaligned_text(const printTableContent *cont, FILE *fout)
370 bool opt_tuples_only = cont->opt->tuples_only;
372 const char *const * ptr;
373 bool need_recordsep = false;
378 if (cont->opt->start_table)
381 if (!opt_tuples_only && cont->title)
383 fputs(cont->title, fout);
384 print_separator(cont->opt->recordSep, fout);
388 if (!opt_tuples_only)
390 for (ptr = cont->headers; *ptr; ptr++)
392 if (ptr != cont->headers)
393 print_separator(cont->opt->fieldSep, fout);
396 need_recordsep = true;
400 /* assume continuing printout */
401 need_recordsep = true;
404 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
408 print_separator(cont->opt->recordSep, fout);
409 need_recordsep = false;
415 if ((i + 1) % cont->ncolumns)
416 print_separator(cont->opt->fieldSep, fout);
418 need_recordsep = true;
422 if (cont->opt->stop_table)
424 printTableFooter *footers = footers_with_default(cont);
426 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
430 for (f = footers; f; f = f->next)
434 print_separator(cont->opt->recordSep, fout);
435 need_recordsep = false;
437 fputs(f->data, fout);
438 need_recordsep = true;
443 * The last record is terminated by a newline, independent of the set
444 * record separator. But when the record separator is a zero byte, we
445 * use that (compatible with find -print0 and xargs).
449 if (cont->opt->recordSep.separator_zero)
450 print_separator(cont->opt->recordSep, fout);
459 print_unaligned_vertical(const printTableContent *cont, FILE *fout)
461 bool opt_tuples_only = cont->opt->tuples_only;
463 const char *const * ptr;
464 bool need_recordsep = false;
469 if (cont->opt->start_table)
472 if (!opt_tuples_only && cont->title)
474 fputs(cont->title, fout);
475 need_recordsep = true;
479 /* assume continuing printout */
480 need_recordsep = true;
483 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
487 /* record separator is 2 occurrences of recordsep in this mode */
488 print_separator(cont->opt->recordSep, fout);
489 print_separator(cont->opt->recordSep, fout);
490 need_recordsep = false;
495 fputs(cont->headers[i % cont->ncolumns], fout);
496 print_separator(cont->opt->fieldSep, fout);
499 if ((i + 1) % cont->ncolumns)
500 print_separator(cont->opt->recordSep, fout);
502 need_recordsep = true;
505 if (cont->opt->stop_table)
508 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
512 print_separator(cont->opt->recordSep, fout);
513 for (f = cont->footers; f; f = f->next)
515 print_separator(cont->opt->recordSep, fout);
516 fputs(f->data, fout);
520 /* see above in print_unaligned_text() */
523 if (cont->opt->recordSep.separator_zero)
524 print_separator(cont->opt->recordSep, fout);
532 /********************/
534 /********************/
539 _print_horizontal_line(const unsigned int ncolumns, const unsigned int *widths,
540 unsigned short border, printTextRule pos,
541 const printTextFormat *format,
544 const printTextLineFormat *lformat = &format->lrule[pos];
549 fputs(lformat->hrule, fout);
550 else if (border == 2)
551 fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
553 for (i = 0; i < ncolumns; i++)
555 for (j = 0; j < widths[i]; j++)
556 fputs(lformat->hrule, fout);
558 if (i < ncolumns - 1)
563 fprintf(fout, "%s%s%s", lformat->hrule,
564 lformat->midvrule, lformat->hrule);
569 fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
570 else if (border == 1)
571 fputs(lformat->hrule, fout);
578 * Print pretty boxes around cells.
581 print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
583 bool opt_tuples_only = cont->opt->tuples_only;
584 int encoding = cont->opt->encoding;
585 unsigned short opt_border = cont->opt->border;
586 const printTextFormat *format = get_line_style(cont->opt);
587 const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
589 unsigned int col_count = 0,
595 unsigned int *width_header,
599 unsigned int *max_nl_lines, /* value split by newlines */
602 unsigned char **format_buf;
603 unsigned int width_total;
604 unsigned int total_header_width;
605 unsigned int extra_row_output_lines = 0;
606 unsigned int extra_output_lines = 0;
608 const char *const * ptr;
610 struct lineptr **col_lineptrs; /* pointers to line pointer per column */
612 bool *header_done; /* Have all header lines been output? */
613 int *bytes_output; /* Bytes output for column value */
614 printTextLineWrap *wrap; /* Wrap status for each column */
615 int output_columns = 0; /* Width of interactive console */
616 bool is_local_pager = false;
624 if (cont->ncolumns > 0)
626 col_count = cont->ncolumns;
627 width_header = pg_malloc0(col_count * sizeof(*width_header));
628 width_average = pg_malloc0(col_count * sizeof(*width_average));
629 max_width = pg_malloc0(col_count * sizeof(*max_width));
630 width_wrap = pg_malloc0(col_count * sizeof(*width_wrap));
631 max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines));
632 curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line));
633 col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs));
634 max_bytes = pg_malloc0(col_count * sizeof(*max_bytes));
635 format_buf = pg_malloc0(col_count * sizeof(*format_buf));
636 header_done = pg_malloc0(col_count * sizeof(*header_done));
637 bytes_output = pg_malloc0(col_count * sizeof(*bytes_output));
638 wrap = pg_malloc0(col_count * sizeof(*wrap));
643 width_average = NULL;
656 /* scan all column headers, find maximum width and max max_nl_lines */
657 for (i = 0; i < col_count; i++)
663 pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
664 encoding, &width, &nl_lines, &bytes_required);
665 if (width > max_width[i])
666 max_width[i] = width;
667 if (nl_lines > max_nl_lines[i])
668 max_nl_lines[i] = nl_lines;
669 if (bytes_required > max_bytes[i])
670 max_bytes[i] = bytes_required;
671 if (nl_lines > extra_row_output_lines)
672 extra_row_output_lines = nl_lines;
674 width_header[i] = width;
676 /* Add height of tallest header column */
677 extra_output_lines += extra_row_output_lines;
678 extra_row_output_lines = 0;
680 /* scan all cells, find maximum width, compute cell_count */
681 for (i = 0, ptr = cont->cells; *ptr; ptr++, i++, cell_count++)
687 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
688 &width, &nl_lines, &bytes_required);
690 if (width > max_width[i % col_count])
691 max_width[i % col_count] = width;
692 if (nl_lines > max_nl_lines[i % col_count])
693 max_nl_lines[i % col_count] = nl_lines;
694 if (bytes_required > max_bytes[i % col_count])
695 max_bytes[i % col_count] = bytes_required;
697 width_average[i % col_count] += width;
700 /* If we have rows, compute average */
701 if (col_count != 0 && cell_count != 0)
703 int rows = cell_count / col_count;
705 for (i = 0; i < col_count; i++)
706 width_average[i] /= rows;
709 /* adjust the total display width based on border style */
711 width_total = col_count;
712 else if (opt_border == 1)
713 width_total = col_count * 3 - ((col_count > 0) ? 1 : 0);
715 width_total = col_count * 3 + 1;
716 total_header_width = width_total;
718 for (i = 0; i < col_count; i++)
720 width_total += max_width[i];
721 total_header_width += width_header[i];
725 * At this point: max_width[] contains the max width of each column,
726 * max_nl_lines[] contains the max number of lines in each column,
727 * max_bytes[] contains the maximum storage space for formatting strings,
728 * width_total contains the giant width sum. Now we allocate some memory
731 for (i = 0; i < col_count; i++)
733 /* Add entry for ptr == NULL array termination */
734 col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) *
735 sizeof(**col_lineptrs));
737 format_buf[i] = pg_malloc(max_bytes[i] + 1);
739 col_lineptrs[i]->ptr = format_buf[i];
742 /* Default word wrap to the full width, i.e. no word wrap */
743 for (i = 0; i < col_count; i++)
744 width_wrap[i] = max_width[i];
747 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
749 if (cont->opt->columns > 0)
750 output_columns = cont->opt->columns;
751 else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
753 if (cont->opt->env_columns > 0)
754 output_columns = cont->opt->env_columns;
758 struct winsize screen_size;
760 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
761 output_columns = screen_size.ws_col;
766 if (cont->opt->format == PRINT_WRAPPED)
769 * Optional optimized word wrap. Shrink columns with a high max/avg
770 * ratio. Slightly bias against wider columns. (Increases chance a
771 * narrow column will fit in its cell.) If available columns is
772 * positive... and greater than the width of the unshrinkable column
775 if (output_columns > 0 && output_columns >= total_header_width)
777 /* While there is still excess width... */
778 while (width_total > output_columns)
780 double max_ratio = 0;
784 * Find column that has the highest ratio of its maximum width
785 * compared to its average width. This tells us which column
786 * will produce the fewest wrapped values if shortened.
787 * width_wrap starts as equal to max_width.
789 for (i = 0; i < col_count; i++)
791 if (width_average[i] && width_wrap[i] > width_header[i])
793 /* Penalize wide columns by 1% of their width */
796 ratio = (double) width_wrap[i] / width_average[i] +
798 if (ratio > max_ratio)
806 /* Exit loop if we can't squeeze any more. */
810 /* Decrease width of target column by one. */
811 width_wrap[worst_col]--;
818 * If in expanded auto mode, we have now calculated the expected width, so
819 * we can now escape to vertical mode if necessary. If the output has
820 * only one column, the expanded format would be wider than the regular
821 * format, so don't use it in that case.
823 if (cont->opt->expanded == 2 && output_columns > 0 && cont->ncolumns > 1 &&
824 (output_columns < total_header_width || output_columns < width_total))
826 print_aligned_vertical(cont, fout, is_pager);
830 /* If we wrapped beyond the display width, use the pager */
831 if (!is_pager && fout == stdout && output_columns > 0 &&
832 (output_columns < total_header_width || output_columns < width_total))
834 fout = PageOutput(INT_MAX, cont->opt); /* force pager */
835 is_pager = is_local_pager = true;
838 /* Check if newlines or our wrapping now need the pager */
839 if (!is_pager && fout == stdout)
841 /* scan all cells, find maximum width, compute cell_count */
842 for (i = 0, ptr = cont->cells; *ptr; ptr++, cell_count++)
848 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
849 &width, &nl_lines, &bytes_required);
852 * A row can have both wrapping and newlines that cause it to
853 * display across multiple lines. We check for both cases below.
855 if (width > 0 && width_wrap[i])
857 unsigned int extra_lines;
859 /* don't count the first line of nl_lines - it's not "extra" */
860 extra_lines = ((width - 1) / width_wrap[i]) + nl_lines - 1;
861 if (extra_lines > extra_row_output_lines)
862 extra_row_output_lines = extra_lines;
865 /* i is the current column number: increment with wrap */
866 if (++i >= col_count)
869 /* At last column of each row, add tallest column height */
870 extra_output_lines += extra_row_output_lines;
871 extra_row_output_lines = 0;
874 IsPagerNeeded(cont, extra_output_lines, false, &fout, &is_pager);
875 is_local_pager = is_pager;
879 if (cont->opt->start_table)
882 if (cont->title && !opt_tuples_only)
887 pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
888 encoding, &width, &height, NULL);
889 if (width >= width_total)
891 fprintf(fout, "%s\n", cont->title);
894 fprintf(fout, "%-*s%s\n", (width_total - width) / 2, "",
899 if (!opt_tuples_only)
901 int more_col_wrapping;
905 _print_horizontal_line(col_count, width_wrap, opt_border,
906 PRINT_RULE_TOP, format, fout);
908 for (i = 0; i < col_count; i++)
909 pg_wcsformat((const unsigned char *) cont->headers[i],
910 strlen(cont->headers[i]), encoding,
911 col_lineptrs[i], max_nl_lines[i]);
913 more_col_wrapping = col_count;
915 memset(header_done, false, col_count * sizeof(bool));
916 while (more_col_wrapping)
919 fputs(dformat->leftvrule, fout);
921 for (i = 0; i < cont->ncolumns; i++)
923 struct lineptr *this_line = col_lineptrs[i] + curr_nl_line;
924 unsigned int nbspace;
926 if (opt_border != 0 ||
927 (!format->wrap_right_border && i > 0))
928 fputs(curr_nl_line ? format->header_nl_left : " ",
933 nbspace = width_wrap[i] - this_line->width;
936 fprintf(fout, "%-*s%s%-*s",
937 nbspace / 2, "", this_line->ptr, (nbspace + 1) / 2, "");
939 if (!(this_line + 1)->ptr)
946 fprintf(fout, "%*s", width_wrap[i], "");
948 if (opt_border != 0 || format->wrap_right_border)
949 fputs(!header_done[i] ? format->header_nl_right : " ",
952 if (opt_border != 0 && col_count > 0 && i < col_count - 1)
953 fputs(dformat->midvrule, fout);
958 fputs(dformat->rightvrule, fout);
962 _print_horizontal_line(col_count, width_wrap, opt_border,
963 PRINT_RULE_MIDDLE, format, fout);
967 /* print cells, one loop per row */
968 for (i = 0, ptr = cont->cells; *ptr; i += col_count, ptr += col_count)
978 for (j = 0; j < col_count; j++)
980 pg_wcsformat((const unsigned char *) ptr[j], strlen(ptr[j]), encoding,
981 col_lineptrs[j], max_nl_lines[j]);
985 memset(bytes_output, 0, col_count * sizeof(int));
988 * Each time through this loop, one display line is output. It can
989 * either be a full value or a partial value if embedded newlines
990 * exist or if 'format=wrapping' mode is enabled.
998 fputs(dformat->leftvrule, fout);
1000 /* for each column */
1001 for (j = 0; j < col_count; j++)
1003 /* We have a valid array element, so index it */
1004 struct lineptr *this_line = &col_lineptrs[j][curr_nl_line[j]];
1005 int bytes_to_output;
1006 int chars_to_output = width_wrap[j];
1007 bool finalspaces = (opt_border == 2 ||
1008 (col_count > 0 && j < col_count - 1));
1010 /* Print left-hand wrap or newline mark */
1011 if (opt_border != 0)
1013 if (wrap[j] == PRINT_LINE_WRAP_WRAP)
1014 fputs(format->wrap_left, fout);
1015 else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1016 fputs(format->nl_left, fout);
1021 if (!this_line->ptr)
1023 /* Past newline lines so just pad for other columns */
1025 fprintf(fout, "%*s", chars_to_output, "");
1029 /* Get strlen() of the characters up to width_wrap */
1031 strlen_max_width(this_line->ptr + bytes_output[j],
1032 &chars_to_output, encoding);
1035 * If we exceeded width_wrap, it means the display width
1036 * of a single character was wider than our target width.
1037 * In that case, we have to pretend we are only printing
1038 * the target display width and make the best of it.
1040 if (chars_to_output > width_wrap[j])
1041 chars_to_output = width_wrap[j];
1043 if (cont->aligns[j] == 'r') /* Right aligned cell */
1046 fprintf(fout, "%*s", width_wrap[j] - chars_to_output, "");
1048 (char *) (this_line->ptr + bytes_output[j]),
1051 else /* Left aligned cell */
1055 (char *) (this_line->ptr + bytes_output[j]),
1059 bytes_output[j] += bytes_to_output;
1061 /* Do we have more text to wrap? */
1062 if (*(this_line->ptr + bytes_output[j]) != '\0')
1066 /* Advance to next newline line */
1068 if (col_lineptrs[j][curr_nl_line[j]].ptr != NULL)
1070 bytes_output[j] = 0;
1074 /* Determine next line's wrap status for this column */
1075 wrap[j] = PRINT_LINE_WRAP_NONE;
1076 if (col_lineptrs[j][curr_nl_line[j]].ptr != NULL)
1078 if (bytes_output[j] != 0)
1079 wrap[j] = PRINT_LINE_WRAP_WRAP;
1080 else if (curr_nl_line[j] != 0)
1081 wrap[j] = PRINT_LINE_WRAP_NEWLINE;
1085 * If left-aligned, pad out remaining space if needed (not
1086 * last column, and/or wrap marks required).
1088 if (cont->aligns[j] != 'r') /* Left aligned cell */
1091 wrap[j] == PRINT_LINE_WRAP_WRAP ||
1092 wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1093 fprintf(fout, "%*s",
1094 width_wrap[j] - chars_to_output, "");
1097 /* Print right-hand wrap or newline mark */
1098 if (wrap[j] == PRINT_LINE_WRAP_WRAP)
1099 fputs(format->wrap_right, fout);
1100 else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1101 fputs(format->nl_right, fout);
1102 else if (opt_border == 2 || (col_count > 0 && j < col_count - 1))
1105 /* Print column divider, if not the last column */
1106 if (opt_border != 0 && (col_count > 0 && j < col_count - 1))
1108 if (wrap[j + 1] == PRINT_LINE_WRAP_WRAP)
1109 fputs(format->midvrule_wrap, fout);
1110 else if (wrap[j + 1] == PRINT_LINE_WRAP_NEWLINE)
1111 fputs(format->midvrule_nl, fout);
1112 else if (col_lineptrs[j + 1][curr_nl_line[j + 1]].ptr == NULL)
1113 fputs(format->midvrule_blank, fout);
1115 fputs(dformat->midvrule, fout);
1119 /* end-of-row border */
1120 if (opt_border == 2)
1121 fputs(dformat->rightvrule, fout);
1124 } while (more_lines);
1127 if (cont->opt->stop_table)
1129 printTableFooter *footers = footers_with_default(cont);
1131 if (opt_border == 2 && !cancel_pressed)
1132 _print_horizontal_line(col_count, width_wrap, opt_border,
1133 PRINT_RULE_BOTTOM, format, fout);
1136 if (footers && !opt_tuples_only && !cancel_pressed)
1138 printTableFooter *f;
1140 for (f = footers; f; f = f->next)
1141 fprintf(fout, "%s\n", f->data);
1149 for (i = 0; i < col_count; i++)
1151 free(col_lineptrs[i]);
1152 free(format_buf[i]);
1155 free(width_average);
1173 print_aligned_vertical_line(const printTextFormat *format,
1174 const unsigned short opt_border,
1175 unsigned long record,
1176 unsigned int hwidth,
1177 unsigned int dwidth,
1181 const printTextLineFormat *lformat = &format->lrule[pos];
1185 if (opt_border == 2)
1186 fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
1187 else if (opt_border == 1)
1188 fputs(lformat->hrule, fout);
1192 if (opt_border == 0)
1193 reclen = fprintf(fout, "* Record %lu", record);
1195 reclen = fprintf(fout, "[ RECORD %lu ]", record);
1197 if (opt_border != 2)
1201 for (i = reclen; i < hwidth; i++)
1202 fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1208 fputs(lformat->hrule, fout);
1210 fputs(lformat->midvrule, fout);
1212 fputs(lformat->hrule, fout);
1221 for (i = reclen; i < dwidth; i++)
1222 fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1223 if (opt_border == 2)
1224 fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
1229 print_aligned_vertical(const printTableContent *cont,
1230 FILE *fout, bool is_pager)
1232 bool opt_tuples_only = cont->opt->tuples_only;
1233 unsigned short opt_border = cont->opt->border;
1234 const printTextFormat *format = get_line_style(cont->opt);
1235 const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
1236 int encoding = cont->opt->encoding;
1237 unsigned long record = cont->opt->prior_records + 1;
1238 const char *const * ptr;
1246 struct lineptr *hlineptr,
1248 bool is_local_pager = false,
1251 int output_columns = 0; /* Width of interactive console */
1259 if (cont->cells[0] == NULL && cont->opt->start_table &&
1260 cont->opt->stop_table)
1262 printTableFooter *footers = footers_with_default(cont);
1264 if (!opt_tuples_only && !cancel_pressed && footers)
1266 printTableFooter *f;
1268 for (f = footers; f; f = f->next)
1269 fprintf(fout, "%s\n", f->data);
1278 * Deal with the pager here instead of in printTable(), because we could
1279 * get here via print_aligned_text() in expanded auto mode, and so we have
1280 * to recalculate the pager requirement based on vertical output.
1284 IsPagerNeeded(cont, 0, true, &fout, &is_pager);
1285 is_local_pager = is_pager;
1288 /* Find the maximum dimensions for the headers */
1289 for (i = 0; i < cont->ncolumns; i++)
1295 pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
1296 encoding, &width, &height, &fs);
1299 if (height > hheight)
1304 if (fs > hformatsize)
1308 /* find longest data cell */
1309 for (i = 0, ptr = cont->cells; *ptr; ptr++, i++)
1315 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
1316 &width, &height, &fs);
1319 if (height > dheight)
1324 if (fs > dformatsize)
1329 * We now have all the information we need to setup the formatting
1332 dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1));
1333 hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1));
1335 dlineptr->ptr = pg_malloc(dformatsize);
1336 hlineptr->ptr = pg_malloc(hformatsize);
1338 if (cont->opt->start_table)
1341 if (!opt_tuples_only && cont->title)
1342 fprintf(fout, "%s\n", cont->title);
1346 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
1348 if (cont->opt->columns > 0)
1349 output_columns = cont->opt->columns;
1350 else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
1352 if (cont->opt->env_columns > 0)
1353 output_columns = cont->opt->env_columns;
1357 struct winsize screen_size;
1359 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
1360 output_columns = screen_size.ws_col;
1366 * Calculate available width for data in wrapped mode
1368 if (cont->opt->format == PRINT_WRAPPED)
1370 unsigned int swidth,
1374 if (opt_border == 0)
1377 * For border = 0, one space in the middle. (If we discover we
1378 * need to wrap, the spacer column will be replaced by a wrap
1379 * marker, and we'll make room below for another wrap marker at
1380 * the end of the line. But for now, assume no wrap is needed.)
1384 /* We might need a column for header newline markers, too */
1388 else if (opt_border == 1)
1391 * For border = 1, two spaces and a vrule in the middle. (As
1392 * above, we might need one more column for a wrap marker.)
1396 /* We might need a column for left header newline markers, too */
1397 if (hmultiline && (format == &pg_asciiformat_old))
1403 * For border = 2, two more for the vrules at the beginning and
1404 * end of the lines, plus spacer columns adjacent to these. (We
1405 * won't need extra columns for wrap/newline markers, we'll just
1406 * repurpose the spacers.)
1411 /* Reserve a column for data newline indicators, too, if needed */
1413 opt_border < 2 && format != &pg_asciiformat_old)
1416 /* Determine width required for record header lines */
1417 if (!opt_tuples_only)
1419 if (cont->nrows > 0)
1420 rwidth = 1 + (int) log10(cont->nrows);
1421 if (opt_border == 0)
1422 rwidth += 9; /* "* RECORD " */
1423 else if (opt_border == 1)
1424 rwidth += 12; /* "-[ RECORD ]" */
1426 rwidth += 15; /* "+-[ RECORD ]-+" */
1429 /* We might need to do the rest of the calculation twice */
1434 /* Total width required to not wrap data */
1435 width = hwidth + swidth + dwidth;
1436 /* ... and not the header lines, either */
1440 if (output_columns > 0)
1442 unsigned int min_width;
1444 /* Minimum acceptable width: room for just 3 columns of data */
1445 min_width = hwidth + swidth + 3;
1446 /* ... but not less than what the record header lines need */
1447 if (min_width < rwidth)
1450 if (output_columns >= width)
1452 /* Plenty of room, use native data width */
1453 /* (but at least enough for the record header lines) */
1454 newdwidth = width - hwidth - swidth;
1456 else if (output_columns < min_width)
1458 /* Set data width to match min_width */
1459 newdwidth = min_width - hwidth - swidth;
1463 /* Set data width to match output_columns */
1464 newdwidth = output_columns - hwidth - swidth;
1469 /* Don't know the wrap limit, so use native data width */
1470 /* (but at least enough for the record header lines) */
1471 newdwidth = width - hwidth - swidth;
1475 * If we will need to wrap data and didn't already allocate a data
1476 * newline/wrap marker column, do so and recompute.
1478 if (newdwidth < dwidth && !dmultiline &&
1479 opt_border < 2 && format != &pg_asciiformat_old)
1492 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1506 pos = PRINT_RULE_TOP;
1508 pos = PRINT_RULE_MIDDLE;
1510 /* Print record header (e.g. "[ RECORD N ]") above each record */
1511 if (i % cont->ncolumns == 0)
1513 unsigned int lhwidth = hwidth;
1515 if ((opt_border < 2) &&
1517 (format == &pg_asciiformat_old))
1518 lhwidth++; /* for newline indicators */
1520 if (!opt_tuples_only)
1521 print_aligned_vertical_line(format, opt_border, record++,
1522 lhwidth, dwidth, pos, fout);
1523 else if (i != 0 || !cont->opt->start_table || opt_border == 2)
1524 print_aligned_vertical_line(format, opt_border, 0, lhwidth,
1528 /* Format the header */
1529 pg_wcsformat((const unsigned char *) cont->headers[i % cont->ncolumns],
1530 strlen(cont->headers[i % cont->ncolumns]),
1531 encoding, hlineptr, hheight);
1532 /* Format the data */
1533 pg_wcsformat((const unsigned char *) *ptr, strlen(*ptr), encoding,
1537 * Loop through header and data in parallel dealing with newlines and
1538 * wrapped lines until they're both exhausted
1541 dcomplete = hcomplete = 0;
1543 chars_to_output = dlineptr[dline].width;
1544 while (!dcomplete || !hcomplete)
1547 if (opt_border == 2)
1548 fprintf(fout, "%s", dformat->leftvrule);
1550 /* Header (never wrapped so just need to deal with newlines) */
1553 int swidth = hwidth,
1554 target_width = hwidth;
1557 * Left spacer or new line indicator
1559 if ((opt_border == 2) ||
1560 (hmultiline && (format == &pg_asciiformat_old)))
1561 fputs(hline ? format->header_nl_left : " ", fout);
1566 strlen_max_width(hlineptr[hline].ptr, &target_width,
1568 fprintf(fout, "%-s", hlineptr[hline].ptr);
1573 swidth -= target_width;
1575 fprintf(fout, "%*s", swidth, " ");
1578 * New line indicator or separator's space
1580 if (hlineptr[hline + 1].ptr)
1582 /* More lines after this one due to a newline */
1583 if ((opt_border > 0) ||
1584 (hmultiline && (format != &pg_asciiformat_old)))
1585 fputs(format->header_nl_right, fout);
1590 /* This was the last line of the header */
1591 if ((opt_border > 0) ||
1592 (hmultiline && (format != &pg_asciiformat_old)))
1599 unsigned int swidth = hwidth + opt_border;
1601 if ((opt_border < 2) &&
1603 (format == &pg_asciiformat_old))
1606 if ((opt_border == 0) &&
1607 (format != &pg_asciiformat_old) &&
1611 fprintf(fout, "%*s", swidth, " ");
1618 fputs(format->midvrule_wrap, fout);
1619 else if (dline == 0)
1620 fputs(dformat->midvrule, fout);
1622 fputs(format->midvrule_nl, fout);
1628 int target_width = dwidth,
1633 * Left spacer or wrap indicator
1635 fputs(offset == 0 ? " " : format->wrap_left, fout);
1640 bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
1641 &target_width, encoding);
1642 fputnbytes(fout, (char *) (dlineptr[dline].ptr + offset),
1645 chars_to_output -= target_width;
1646 offset += bytes_to_output;
1649 swidth -= target_width;
1651 if (chars_to_output)
1653 /* continuing a wrapped column */
1654 if ((opt_border > 1) ||
1655 (dmultiline && (format != &pg_asciiformat_old)))
1658 fprintf(fout, "%*s", swidth, " ");
1659 fputs(format->wrap_right, fout);
1662 else if (dlineptr[dline + 1].ptr)
1664 /* reached a newline in the column */
1665 if ((opt_border > 1) ||
1666 (dmultiline && (format != &pg_asciiformat_old)))
1669 fprintf(fout, "%*s", swidth, " ");
1670 fputs(format->nl_right, fout);
1674 chars_to_output = dlineptr[dline].width;
1678 /* reached the end of the cell */
1682 fprintf(fout, "%*s", swidth, " ");
1689 if (opt_border == 2)
1690 fputs(dformat->rightvrule, fout);
1697 * data exhausted (this can occur if header is longer than the
1698 * data due to newlines in the header)
1703 fprintf(fout, "%*s %s\n", dwidth, "", dformat->rightvrule);
1708 if (cont->opt->stop_table)
1710 if (opt_border == 2 && !cancel_pressed)
1711 print_aligned_vertical_line(format, opt_border, 0, hwidth, dwidth,
1712 PRINT_RULE_BOTTOM, fout);
1715 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
1717 printTableFooter *f;
1721 for (f = cont->footers; f; f = f->next)
1722 fprintf(fout, "%s\n", f->data);
1728 free(hlineptr->ptr);
1729 free(dlineptr->ptr);
1738 /**********************/
1739 /* HTML printing ******/
1740 /**********************/
1744 html_escaped_print(const char *in, FILE *fout)
1747 bool leading_space = true;
1749 for (p = in; *p; p++)
1754 fputs("&", fout);
1757 fputs("<", fout);
1760 fputs(">", fout);
1763 fputs("<br />\n", fout);
1766 fputs(""", fout);
1769 /* protect leading space, for EXPLAIN output */
1771 fputs(" ", fout);
1779 leading_space = false;
1785 print_html_text(const printTableContent *cont, FILE *fout)
1787 bool opt_tuples_only = cont->opt->tuples_only;
1788 unsigned short opt_border = cont->opt->border;
1789 const char *opt_table_attr = cont->opt->tableAttr;
1791 const char *const * ptr;
1796 if (cont->opt->start_table)
1798 fprintf(fout, "<table border=\"%d\"", opt_border);
1800 fprintf(fout, " %s", opt_table_attr);
1804 if (!opt_tuples_only && cont->title)
1806 fputs(" <caption>", fout);
1807 html_escaped_print(cont->title, fout);
1808 fputs("</caption>\n", fout);
1812 if (!opt_tuples_only)
1814 fputs(" <tr>\n", fout);
1815 for (ptr = cont->headers; *ptr; ptr++)
1817 fputs(" <th align=\"center\">", fout);
1818 html_escaped_print(*ptr, fout);
1819 fputs("</th>\n", fout);
1821 fputs(" </tr>\n", fout);
1826 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1828 if (i % cont->ncolumns == 0)
1832 fputs(" <tr valign=\"top\">\n", fout);
1835 fprintf(fout, " <td align=\"%s\">", cont->aligns[(i) % cont->ncolumns] == 'r' ? "right" : "left");
1836 /* is string only whitespace? */
1837 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
1838 fputs(" ", fout);
1840 html_escaped_print(*ptr, fout);
1842 fputs("</td>\n", fout);
1844 if ((i + 1) % cont->ncolumns == 0)
1845 fputs(" </tr>\n", fout);
1848 if (cont->opt->stop_table)
1850 printTableFooter *footers = footers_with_default(cont);
1852 fputs("</table>\n", fout);
1855 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
1857 printTableFooter *f;
1860 for (f = footers; f; f = f->next)
1862 html_escaped_print(f->data, fout);
1863 fputs("<br />\n", fout);
1865 fputs("</p>", fout);
1874 print_html_vertical(const printTableContent *cont, FILE *fout)
1876 bool opt_tuples_only = cont->opt->tuples_only;
1877 unsigned short opt_border = cont->opt->border;
1878 const char *opt_table_attr = cont->opt->tableAttr;
1879 unsigned long record = cont->opt->prior_records + 1;
1881 const char *const * ptr;
1886 if (cont->opt->start_table)
1888 fprintf(fout, "<table border=\"%d\"", opt_border);
1890 fprintf(fout, " %s", opt_table_attr);
1894 if (!opt_tuples_only && cont->title)
1896 fputs(" <caption>", fout);
1897 html_escaped_print(cont->title, fout);
1898 fputs("</caption>\n", fout);
1903 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1905 if (i % cont->ncolumns == 0)
1909 if (!opt_tuples_only)
1911 "\n <tr><td colspan=\"2\" align=\"center\">Record %lu</td></tr>\n",
1914 fputs("\n <tr><td colspan=\"2\"> </td></tr>\n", fout);
1916 fputs(" <tr valign=\"top\">\n"
1918 html_escaped_print(cont->headers[i % cont->ncolumns], fout);
1919 fputs("</th>\n", fout);
1921 fprintf(fout, " <td align=\"%s\">", cont->aligns[i % cont->ncolumns] == 'r' ? "right" : "left");
1922 /* is string only whitespace? */
1923 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
1924 fputs(" ", fout);
1926 html_escaped_print(*ptr, fout);
1928 fputs("</td>\n </tr>\n", fout);
1931 if (cont->opt->stop_table)
1933 fputs("</table>\n", fout);
1936 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
1938 printTableFooter *f;
1941 for (f = cont->footers; f; f = f->next)
1943 html_escaped_print(f->data, fout);
1944 fputs("<br />\n", fout);
1946 fputs("</p>", fout);
1954 /*************************/
1956 /*************************/
1959 asciidoc_escaped_print(const char *in, FILE *fout)
1963 for (p = in; *p; p++)
1977 print_asciidoc_text(const printTableContent *cont, FILE *fout)
1979 bool opt_tuples_only = cont->opt->tuples_only;
1980 unsigned short opt_border = cont->opt->border;
1982 const char *const * ptr;
1987 if (cont->opt->start_table)
1989 /* print table in new paragraph - enforce preliminary new line */
1993 if (!opt_tuples_only && cont->title)
1996 fputs(cont->title, fout);
2000 /* print table [] header definition */
2001 fprintf(fout, "[%scols=\"", !opt_tuples_only ? "options=\"header\"," : "");
2002 for (i = 0; i < cont->ncolumns; i++)
2006 fprintf(fout, "%s", cont->aligns[(i) % cont->ncolumns] == 'r' ? ">l" : "<l");
2012 fputs(",frame=\"none\",grid=\"none\"", fout);
2015 fputs(",frame=\"none\"", fout);
2018 fputs(",frame=\"all\",grid=\"all\"", fout);
2022 fputs("|====\n", fout);
2025 if (!opt_tuples_only)
2027 for (ptr = cont->headers; *ptr; ptr++)
2029 if (ptr != cont->headers)
2032 asciidoc_escaped_print(*ptr, fout);
2039 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2041 if (i % cont->ncolumns == 0)
2047 if (i % cont->ncolumns != 0)
2051 /* protect against needless spaces */
2052 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2054 if ((i + 1) % cont->ncolumns != 0)
2058 asciidoc_escaped_print(*ptr, fout);
2060 if ((i + 1) % cont->ncolumns == 0)
2064 fputs("|====\n", fout);
2066 if (cont->opt->stop_table)
2068 printTableFooter *footers = footers_with_default(cont);
2071 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
2073 printTableFooter *f;
2075 fputs("\n....\n", fout);
2076 for (f = footers; f; f = f->next)
2078 fputs(f->data, fout);
2081 fputs("....\n", fout);
2087 print_asciidoc_vertical(const printTableContent *cont, FILE *fout)
2089 bool opt_tuples_only = cont->opt->tuples_only;
2090 unsigned short opt_border = cont->opt->border;
2091 unsigned long record = cont->opt->prior_records + 1;
2093 const char *const * ptr;
2098 if (cont->opt->start_table)
2100 /* print table in new paragraph - enforce preliminary new line */
2104 if (!opt_tuples_only && cont->title)
2107 fputs(cont->title, fout);
2111 /* print table [] header definition */
2112 fputs("[cols=\"h,l\"", fout);
2116 fputs(",frame=\"none\",grid=\"none\"", fout);
2119 fputs(",frame=\"none\"", fout);
2122 fputs(",frame=\"all\",grid=\"all\"", fout);
2126 fputs("|====\n", fout);
2130 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2132 if (i % cont->ncolumns == 0)
2136 if (!opt_tuples_only)
2141 fputs("2+|\n", fout);
2145 asciidoc_escaped_print(cont->headers[i % cont->ncolumns], fout);
2147 fprintf(fout, " %s|", cont->aligns[i % cont->ncolumns] == 'r' ? ">l" : "<l");
2148 /* is string only whitespace? */
2149 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2152 asciidoc_escaped_print(*ptr, fout);
2156 fputs("|====\n", fout);
2158 if (cont->opt->stop_table)
2161 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
2163 printTableFooter *f;
2165 fputs("\n....\n", fout);
2166 for (f = cont->footers; f; f = f->next)
2168 fputs(f->data, fout);
2171 fputs("....\n", fout);
2176 /*************************/
2178 /*************************/
2182 latex_escaped_print(const char *in, FILE *fout)
2186 for (p = in; *p; p++)
2208 fputs("\\backslash", fout);
2211 fputs("\\\\", fout);
2220 print_latex_text(const printTableContent *cont, FILE *fout)
2222 bool opt_tuples_only = cont->opt->tuples_only;
2223 unsigned short opt_border = cont->opt->border;
2225 const char *const * ptr;
2233 if (cont->opt->start_table)
2236 if (!opt_tuples_only && cont->title)
2238 fputs("\\begin{center}\n", fout);
2239 latex_escaped_print(cont->title, fout);
2240 fputs("\n\\end{center}\n\n", fout);
2243 /* begin environment and set alignments and borders */
2244 fputs("\\begin{tabular}{", fout);
2246 if (opt_border >= 2)
2248 for (i = 0; i < cont->ncolumns; i++)
2250 fputc(*(cont->aligns + i), fout);
2251 if (opt_border != 0 && i < cont->ncolumns - 1)
2254 if (opt_border >= 2)
2259 if (!opt_tuples_only && opt_border >= 2)
2260 fputs("\\hline\n", fout);
2263 if (!opt_tuples_only)
2265 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2269 fputs("\\textit{", fout);
2270 latex_escaped_print(*ptr, fout);
2273 fputs(" \\\\\n", fout);
2274 fputs("\\hline\n", fout);
2279 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2281 latex_escaped_print(*ptr, fout);
2283 if ((i + 1) % cont->ncolumns == 0)
2285 fputs(" \\\\\n", fout);
2286 if (opt_border == 3)
2287 fputs("\\hline\n", fout);
2295 if (cont->opt->stop_table)
2297 printTableFooter *footers = footers_with_default(cont);
2299 if (opt_border == 2)
2300 fputs("\\hline\n", fout);
2302 fputs("\\end{tabular}\n\n\\noindent ", fout);
2305 if (footers && !opt_tuples_only && !cancel_pressed)
2307 printTableFooter *f;
2309 for (f = footers; f; f = f->next)
2311 latex_escaped_print(f->data, fout);
2312 fputs(" \\\\\n", fout);
2322 print_latex_longtable_text(const printTableContent *cont, FILE *fout)
2324 bool opt_tuples_only = cont->opt->tuples_only;
2325 unsigned short opt_border = cont->opt->border;
2327 const char *opt_table_attr = cont->opt->tableAttr;
2328 const char *next_opt_table_attr_char = opt_table_attr;
2329 const char *last_opt_table_attr_char = NULL;
2330 const char *const * ptr;
2338 if (cont->opt->start_table)
2340 /* begin environment and set alignments and borders */
2341 fputs("\\begin{longtable}{", fout);
2343 if (opt_border >= 2)
2346 for (i = 0; i < cont->ncolumns; i++)
2348 /* longtable supports either a width (p) or an alignment (l/r) */
2349 /* Are we left-justified and was a proportional width specified? */
2350 if (*(cont->aligns + i) == 'l' && opt_table_attr)
2352 #define LONGTABLE_WHITESPACE " \t\n"
2354 /* advance over whitespace */
2355 next_opt_table_attr_char += strspn(next_opt_table_attr_char,
2356 LONGTABLE_WHITESPACE);
2357 /* We have a value? */
2358 if (next_opt_table_attr_char[0] != '\0')
2361 fwrite(next_opt_table_attr_char, strcspn(next_opt_table_attr_char,
2362 LONGTABLE_WHITESPACE), 1, fout);
2363 last_opt_table_attr_char = next_opt_table_attr_char;
2364 next_opt_table_attr_char += strcspn(next_opt_table_attr_char,
2365 LONGTABLE_WHITESPACE);
2366 fputs("\\textwidth}", fout);
2368 /* use previous value */
2369 else if (last_opt_table_attr_char != NULL)
2372 fwrite(last_opt_table_attr_char, strcspn(last_opt_table_attr_char,
2373 LONGTABLE_WHITESPACE), 1, fout);
2374 fputs("\\textwidth}", fout);
2380 fputc(*(cont->aligns + i), fout);
2382 if (opt_border != 0 && i < cont->ncolumns - 1)
2386 if (opt_border >= 2)
2392 if (!opt_tuples_only)
2395 if (opt_border >= 2)
2396 fputs("\\toprule\n", fout);
2397 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2401 fputs("\\small\\textbf{\\textit{", fout);
2402 latex_escaped_print(*ptr, fout);
2405 fputs(" \\\\\n", fout);
2406 fputs("\\midrule\n\\endfirsthead\n", fout);
2408 /* secondary heads */
2409 if (opt_border >= 2)
2410 fputs("\\toprule\n", fout);
2411 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2415 fputs("\\small\\textbf{\\textit{", fout);
2416 latex_escaped_print(*ptr, fout);
2419 fputs(" \\\\\n", fout);
2420 /* If the line under the row already appeared, don't do another */
2421 if (opt_border != 3)
2422 fputs("\\midrule\n", fout);
2423 fputs("\\endhead\n", fout);
2425 /* table name, caption? */
2426 if (!opt_tuples_only && cont->title)
2428 /* Don't output if we are printing a line under each row */
2429 if (opt_border == 2)
2430 fputs("\\bottomrule\n", fout);
2431 fputs("\\caption[", fout);
2432 latex_escaped_print(cont->title, fout);
2433 fputs(" (Continued)]{", fout);
2434 latex_escaped_print(cont->title, fout);
2435 fputs("}\n\\endfoot\n", fout);
2436 if (opt_border == 2)
2437 fputs("\\bottomrule\n", fout);
2438 fputs("\\caption[", fout);
2439 latex_escaped_print(cont->title, fout);
2441 latex_escaped_print(cont->title, fout);
2442 fputs("}\n\\endlastfoot\n", fout);
2444 /* output bottom table line? */
2445 else if (opt_border >= 2)
2447 fputs("\\bottomrule\n\\endfoot\n", fout);
2448 fputs("\\bottomrule\n\\endlastfoot\n", fout);
2454 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2456 /* Add a line under each row? */
2457 if (i != 0 && i % cont->ncolumns != 0)
2458 fputs("\n&\n", fout);
2459 fputs("\\raggedright{", fout);
2460 latex_escaped_print(*ptr, fout);
2462 if ((i + 1) % cont->ncolumns == 0)
2464 fputs(" \\tabularnewline\n", fout);
2465 if (opt_border == 3)
2466 fputs(" \\hline\n", fout);
2472 if (cont->opt->stop_table)
2473 fputs("\\end{longtable}\n", fout);
2478 print_latex_vertical(const printTableContent *cont, FILE *fout)
2480 bool opt_tuples_only = cont->opt->tuples_only;
2481 unsigned short opt_border = cont->opt->border;
2482 unsigned long record = cont->opt->prior_records + 1;
2484 const char *const * ptr;
2492 if (cont->opt->start_table)
2495 if (!opt_tuples_only && cont->title)
2497 fputs("\\begin{center}\n", fout);
2498 latex_escaped_print(cont->title, fout);
2499 fputs("\n\\end{center}\n\n", fout);
2502 /* begin environment and set alignments and borders */
2503 fputs("\\begin{tabular}{", fout);
2504 if (opt_border == 0)
2506 else if (opt_border == 1)
2508 else if (opt_border == 2)
2509 fputs("|c|l|", fout);
2514 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2517 if (i % cont->ncolumns == 0)
2521 if (!opt_tuples_only)
2523 if (opt_border == 2)
2525 fputs("\\hline\n", fout);
2526 fprintf(fout, "\\multicolumn{2}{|c|}{\\textit{Record %lu}} \\\\\n", record++);
2529 fprintf(fout, "\\multicolumn{2}{c}{\\textit{Record %lu}} \\\\\n", record++);
2531 if (opt_border >= 1)
2532 fputs("\\hline\n", fout);
2535 latex_escaped_print(cont->headers[i % cont->ncolumns], fout);
2537 latex_escaped_print(*ptr, fout);
2538 fputs(" \\\\\n", fout);
2541 if (cont->opt->stop_table)
2543 if (opt_border == 2)
2544 fputs("\\hline\n", fout);
2546 fputs("\\end{tabular}\n\n\\noindent ", fout);
2549 if (cont->footers && !opt_tuples_only && !cancel_pressed)
2551 printTableFooter *f;
2553 for (f = cont->footers; f; f = f->next)
2555 latex_escaped_print(f->data, fout);
2556 fputs(" \\\\\n", fout);
2565 /*************************/
2567 /*************************/
2571 troff_ms_escaped_print(const char *in, FILE *fout)
2575 for (p = in; *p; p++)
2579 fputs("\\(rs", fout);
2588 print_troff_ms_text(const printTableContent *cont, FILE *fout)
2590 bool opt_tuples_only = cont->opt->tuples_only;
2591 unsigned short opt_border = cont->opt->border;
2593 const char *const * ptr;
2601 if (cont->opt->start_table)
2604 if (!opt_tuples_only && cont->title)
2606 fputs(".LP\n.DS C\n", fout);
2607 troff_ms_escaped_print(cont->title, fout);
2608 fputs("\n.DE\n", fout);
2611 /* begin environment and set alignments and borders */
2612 fputs(".LP\n.TS\n", fout);
2613 if (opt_border == 2)
2614 fputs("center box;\n", fout);
2616 fputs("center;\n", fout);
2618 for (i = 0; i < cont->ncolumns; i++)
2620 fputc(*(cont->aligns + i), fout);
2621 if (opt_border > 0 && i < cont->ncolumns - 1)
2627 if (!opt_tuples_only)
2629 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2633 fputs("\\fI", fout);
2634 troff_ms_escaped_print(*ptr, fout);
2635 fputs("\\fP", fout);
2637 fputs("\n_\n", fout);
2642 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2644 troff_ms_escaped_print(*ptr, fout);
2646 if ((i + 1) % cont->ncolumns == 0)
2656 if (cont->opt->stop_table)
2658 printTableFooter *footers = footers_with_default(cont);
2660 fputs(".TE\n.DS L\n", fout);
2663 if (footers && !opt_tuples_only && !cancel_pressed)
2665 printTableFooter *f;
2667 for (f = footers; f; f = f->next)
2669 troff_ms_escaped_print(f->data, fout);
2674 fputs(".DE\n", fout);
2680 print_troff_ms_vertical(const printTableContent *cont, FILE *fout)
2682 bool opt_tuples_only = cont->opt->tuples_only;
2683 unsigned short opt_border = cont->opt->border;
2684 unsigned long record = cont->opt->prior_records + 1;
2686 const char *const * ptr;
2687 unsigned short current_format = 0; /* 0=none, 1=header, 2=body */
2695 if (cont->opt->start_table)
2698 if (!opt_tuples_only && cont->title)
2700 fputs(".LP\n.DS C\n", fout);
2701 troff_ms_escaped_print(cont->title, fout);
2702 fputs("\n.DE\n", fout);
2705 /* begin environment and set alignments and borders */
2706 fputs(".LP\n.TS\n", fout);
2707 if (opt_border == 2)
2708 fputs("center box;\n", fout);
2710 fputs("center;\n", fout);
2713 if (opt_tuples_only)
2714 fputs("c l;\n", fout);
2717 current_format = 2; /* assume tuples printed already */
2720 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2723 if (i % cont->ncolumns == 0)
2727 if (!opt_tuples_only)
2729 if (current_format != 1)
2731 if (opt_border == 2 && record > 1)
2733 if (current_format != 0)
2734 fputs(".T&\n", fout);
2735 fputs("c s.\n", fout);
2738 fprintf(fout, "\\fIRecord %lu\\fP\n", record++);
2740 if (opt_border >= 1)
2744 if (!opt_tuples_only)
2746 if (current_format != 2)
2748 if (current_format != 0)
2749 fputs(".T&\n", fout);
2750 if (opt_border != 1)
2751 fputs("c l.\n", fout);
2753 fputs("c | l.\n", fout);
2758 troff_ms_escaped_print(cont->headers[i % cont->ncolumns], fout);
2760 troff_ms_escaped_print(*ptr, fout);
2765 if (cont->opt->stop_table)
2767 fputs(".TE\n.DS L\n", fout);
2770 if (cont->footers && !opt_tuples_only && !cancel_pressed)
2772 printTableFooter *f;
2774 for (f = cont->footers; f; f = f->next)
2776 troff_ms_escaped_print(f->data, fout);
2781 fputs(".DE\n", fout);
2786 /********************************/
2787 /* Public functions */
2788 /********************************/
2792 * disable_sigpipe_trap
2794 * Turn off SIGPIPE interrupt --- call this before writing to a temporary
2795 * query output file that is a pipe.
2797 * No-op on Windows, where there's no SIGPIPE interrupts.
2800 disable_sigpipe_trap(void)
2803 pqsignal(SIGPIPE, SIG_IGN);
2808 * restore_sigpipe_trap
2810 * Restore normal SIGPIPE interrupt --- call this when done writing to a
2811 * temporary query output file that was (or might have been) a pipe.
2813 * Note: within psql, we enable SIGPIPE interrupts unless the permanent query
2814 * output file is a pipe, in which case they should be kept off. This
2815 * approach works only because psql is not currently complicated enough to
2816 * have nested usages of short-lived output files. Otherwise we'd probably
2817 * need a genuine save-and-restore-state approach; but for now, that would be
2818 * useless complication. In non-psql programs, this always enables SIGPIPE.
2820 * No-op on Windows, where there's no SIGPIPE interrupts.
2823 restore_sigpipe_trap(void)
2826 pqsignal(SIGPIPE, always_ignore_sigpipe ? SIG_IGN : SIG_DFL);
2831 * set_sigpipe_trap_state
2833 * Set the trap state that restore_sigpipe_trap should restore to.
2836 set_sigpipe_trap_state(bool ignore)
2838 always_ignore_sigpipe = ignore;
2845 * Tests if pager is needed and returns appropriate FILE pointer.
2847 * If the topt argument is NULL no pager is used.
2850 PageOutput(int lines, const printTableOpt *topt)
2852 /* check whether we need / can / are supposed to use pager */
2853 if (topt && topt->pager && isatty(fileno(stdin)) && isatty(fileno(stdout)))
2856 unsigned short int pager = topt->pager;
2857 int min_lines = topt->pager_min_lines;
2859 struct winsize screen_size;
2861 result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
2863 /* >= accounts for a one-line prompt */
2865 || (lines >= screen_size.ws_row && lines >= min_lines)
2869 const char *pagerprog;
2872 pagerprog = getenv("PAGER");
2874 pagerprog = DEFAULT_PAGER;
2875 disable_sigpipe_trap();
2876 pagerpipe = popen(pagerprog, "w");
2888 * Close previously opened pager pipe, if any
2891 ClosePager(FILE *pagerpipe)
2893 if (pagerpipe && pagerpipe != stdout)
2896 * If printing was canceled midstream, warn about it.
2898 * Some pagers like less use Ctrl-C as part of their command set. Even
2899 * so, we abort our processing and warn the user what we did. If the
2900 * pager quit as a result of the SIGINT, this message won't go
2904 fprintf(pagerpipe, _("Interrupted\n"));
2907 restore_sigpipe_trap();
2912 * Initialise a table contents struct.
2913 * Must be called before any other printTable method is used.
2915 * The title is not duplicated; the caller must ensure that the buffer
2916 * is available for the lifetime of the printTableContent struct.
2918 * If you call this, you must call printTableCleanup once you're done with the
2922 printTableInit(printTableContent *const content, const printTableOpt *opt,
2923 const char *title, const int ncolumns, const int nrows)
2926 content->title = title;
2927 content->ncolumns = ncolumns;
2928 content->nrows = nrows;
2930 content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers));
2932 content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells));
2934 content->cellmustfree = NULL;
2935 content->footers = NULL;
2937 content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align));
2939 content->header = content->headers;
2940 content->cell = content->cells;
2941 content->footer = content->footers;
2942 content->align = content->aligns;
2943 content->cellsadded = 0;
2947 * Add a header to the table.
2949 * Headers are not duplicated; you must ensure that the header string is
2950 * available for the lifetime of the printTableContent struct.
2952 * If translate is true, the function will pass the header through gettext.
2953 * Otherwise, the header will not be translated.
2955 * align is either 'l' or 'r', and specifies the alignment for cells in this
2959 printTableAddHeader(printTableContent *const content, char *header,
2960 const bool translate, const char align)
2963 (void) translate; /* unused parameter */
2966 if (content->header >= content->headers + content->ncolumns)
2968 fprintf(stderr, _("Cannot add header to table content: "
2969 "column count of %d exceeded.\n"),
2974 *content->header = (char *) mbvalidate((unsigned char *) header,
2975 content->opt->encoding);
2978 *content->header = _(*content->header);
2982 *content->align = align;
2987 * Add a cell to the table.
2989 * Cells are not duplicated; you must ensure that the cell string is available
2990 * for the lifetime of the printTableContent struct.
2992 * If translate is true, the function will pass the cell through gettext.
2993 * Otherwise, the cell will not be translated.
2995 * If mustfree is true, the cell string is freed by printTableCleanup().
2996 * Note: Automatic freeing of translatable strings is not supported.
2999 printTableAddCell(printTableContent *const content, char *cell,
3000 const bool translate, const bool mustfree)
3003 (void) translate; /* unused parameter */
3006 if (content->cellsadded >= content->ncolumns * content->nrows)
3008 fprintf(stderr, _("Cannot add cell to table content: "
3009 "total cell count of %d exceeded.\n"),
3010 content->ncolumns * content->nrows);
3014 *content->cell = (char *) mbvalidate((unsigned char *) cell,
3015 content->opt->encoding);
3019 *content->cell = _(*content->cell);
3024 if (content->cellmustfree == NULL)
3025 content->cellmustfree =
3026 pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool));
3028 content->cellmustfree[content->cellsadded] = true;
3031 content->cellsadded++;
3035 * Add a footer to the table.
3037 * Footers are added as elements of a singly-linked list, and the content is
3038 * strdup'd, so there is no need to keep the original footer string around.
3040 * Footers are never translated by the function. If you want the footer
3041 * translated you must do so yourself, before calling printTableAddFooter. The
3042 * reason this works differently to headers and cells is that footers tend to
3043 * be made of up individually translated components, rather than being
3044 * translated as a whole.
3047 printTableAddFooter(printTableContent *const content, const char *footer)
3049 printTableFooter *f;
3051 f = pg_malloc0(sizeof(*f));
3052 f->data = pg_strdup(footer);
3054 if (content->footers == NULL)
3055 content->footers = f;
3057 content->footer->next = f;
3059 content->footer = f;
3063 * Change the content of the last-added footer.
3065 * The current contents of the last-added footer are freed, and replaced by the
3066 * content given in *footer. If there was no previous footer, add a new one.
3068 * The content is strdup'd, so there is no need to keep the original string
3072 printTableSetFooter(printTableContent *const content, const char *footer)
3074 if (content->footers != NULL)
3076 free(content->footer->data);
3077 content->footer->data = pg_strdup(footer);
3080 printTableAddFooter(content, footer);
3084 * Free all memory allocated to this struct.
3086 * Once this has been called, the struct is unusable unless you pass it to
3087 * printTableInit() again.
3090 printTableCleanup(printTableContent *const content)
3092 if (content->cellmustfree)
3096 for (i = 0; i < content->nrows * content->ncolumns; i++)
3098 if (content->cellmustfree[i])
3099 free((char *) content->cells[i]);
3101 free(content->cellmustfree);
3102 content->cellmustfree = NULL;
3104 free(content->headers);
3105 free(content->cells);
3106 free(content->aligns);
3108 content->opt = NULL;
3109 content->title = NULL;
3110 content->headers = NULL;
3111 content->cells = NULL;
3112 content->aligns = NULL;
3113 content->header = NULL;
3114 content->cell = NULL;
3115 content->align = NULL;
3117 if (content->footers)
3119 for (content->footer = content->footers; content->footer;)
3121 printTableFooter *f;
3123 f = content->footer;
3124 content->footer = f->next;
3129 content->footers = NULL;
3130 content->footer = NULL;
3136 * Setup pager if required
3139 IsPagerNeeded(const printTableContent *cont, int extra_lines, bool expanded,
3140 FILE **fout, bool *is_pager)
3142 if (*fout == stdout)
3147 lines = (cont->ncolumns + 1) * cont->nrows;
3149 lines = cont->nrows + 1;
3151 if (!cont->opt->tuples_only)
3153 printTableFooter *f;
3156 * FIXME -- this is slightly bogus: it counts the number of
3157 * footers, not the number of lines in them.
3159 for (f = cont->footers; f; f = f->next)
3163 *fout = PageOutput(lines + extra_lines, cont->opt);
3164 *is_pager = (*fout != stdout);
3171 * Use this to print any table in the supported formats.
3173 * cont: table data and formatting options
3174 * fout: where to print to
3175 * is_pager: true if caller has already redirected fout to be a pager pipe
3176 * flog: if not null, also print the table there (for --log-file option)
3179 printTable(const printTableContent *cont,
3180 FILE *fout, bool is_pager, FILE *flog)
3182 bool is_local_pager = false;
3187 if (cont->opt->format == PRINT_NOTHING)
3190 /* print_aligned_*() handle the pager themselves */
3192 cont->opt->format != PRINT_ALIGNED &&
3193 cont->opt->format != PRINT_WRAPPED)
3195 IsPagerNeeded(cont, 0, (cont->opt->expanded == 1), &fout, &is_pager);
3196 is_local_pager = is_pager;
3199 /* print the stuff */
3202 print_aligned_text(cont, flog, false);
3204 switch (cont->opt->format)
3206 case PRINT_UNALIGNED:
3207 if (cont->opt->expanded == 1)
3208 print_unaligned_vertical(cont, fout);
3210 print_unaligned_text(cont, fout);
3216 * In expanded-auto mode, force vertical if a pager is passed in;
3217 * else we may make different decisions for different hunks of the
3220 if (cont->opt->expanded == 1 ||
3221 (cont->opt->expanded == 2 && is_pager))
3222 print_aligned_vertical(cont, fout, is_pager);
3224 print_aligned_text(cont, fout, is_pager);
3227 if (cont->opt->expanded == 1)
3228 print_html_vertical(cont, fout);
3230 print_html_text(cont, fout);
3232 case PRINT_ASCIIDOC:
3233 if (cont->opt->expanded == 1)
3234 print_asciidoc_vertical(cont, fout);
3236 print_asciidoc_text(cont, fout);
3239 if (cont->opt->expanded == 1)
3240 print_latex_vertical(cont, fout);
3242 print_latex_text(cont, fout);
3244 case PRINT_LATEX_LONGTABLE:
3245 if (cont->opt->expanded == 1)
3246 print_latex_vertical(cont, fout);
3248 print_latex_longtable_text(cont, fout);
3250 case PRINT_TROFF_MS:
3251 if (cont->opt->expanded == 1)
3252 print_troff_ms_vertical(cont, fout);
3254 print_troff_ms_text(cont, fout);
3257 fprintf(stderr, _("invalid output format (internal error): %d"),
3267 * Use this to print query results
3269 * result: result of a successful query
3270 * opt: formatting options
3271 * fout: where to print to
3272 * is_pager: true if caller has already redirected fout to be a pager pipe
3273 * flog: if not null, also print the data there (for --log-file option)
3276 printQuery(const PGresult *result, const printQueryOpt *opt,
3277 FILE *fout, bool is_pager, FILE *flog)
3279 printTableContent cont;
3287 printTableInit(&cont, &opt->topt, opt->title,
3288 PQnfields(result), PQntuples(result));
3290 /* Assert caller supplied enough translate_columns[] entries */
3291 Assert(opt->translate_columns == NULL ||
3292 opt->n_translate_columns >= cont.ncolumns);
3294 for (i = 0; i < cont.ncolumns; i++)
3297 Oid ftype = PQftype(result, i);
3318 printTableAddHeader(&cont, PQfname(result, i),
3319 opt->translate_header, align);
3323 for (r = 0; r < cont.nrows; r++)
3325 for (c = 0; c < cont.ncolumns; c++)
3328 bool mustfree = false;
3331 if (PQgetisnull(result, r, c))
3332 cell = opt->nullPrint ? opt->nullPrint : "";
3335 cell = PQgetvalue(result, r, c);
3336 if (cont.aligns[c] == 'r' && opt->topt.numericLocale)
3338 cell = format_numeric_locale(cell);
3343 translate = (opt->translate_columns && opt->translate_columns[c]);
3344 printTableAddCell(&cont, cell, translate, mustfree);
3353 for (footer = opt->footers; *footer; footer++)
3354 printTableAddFooter(&cont, *footer);
3357 printTable(&cont, fout, is_pager, flog);
3358 printTableCleanup(&cont);
3363 setDecimalLocale(void)
3365 struct lconv *extlconv;
3367 extlconv = localeconv();
3369 /* Don't accept an empty decimal_point string */
3370 if (*extlconv->decimal_point)
3371 decimal_point = pg_strdup(extlconv->decimal_point);
3373 decimal_point = "."; /* SQL output standard */
3376 * Although the Open Group standard allows locales to supply more than one
3377 * group width, we consider only the first one, and we ignore any attempt
3378 * to suppress grouping by specifying CHAR_MAX. As in the backend's
3379 * cash.c, we must apply a range check to avoid being fooled by variant
3382 groupdigits = *extlconv->grouping;
3383 if (groupdigits <= 0 || groupdigits > 6)
3384 groupdigits = 3; /* most common */
3386 /* Don't accept an empty thousands_sep string, either */
3387 /* similar code exists in formatting.c */
3388 if (*extlconv->thousands_sep)
3389 thousands_sep = pg_strdup(extlconv->thousands_sep);
3390 /* Make sure thousands separator doesn't match decimal point symbol. */
3391 else if (strcmp(decimal_point, ",") != 0)
3392 thousands_sep = ",";
3394 thousands_sep = ".";
3397 /* get selected or default line style */
3398 const printTextFormat *
3399 get_line_style(const printTableOpt *opt)
3402 * Note: this function mainly exists to preserve the convention that a
3403 * printTableOpt struct can be initialized to zeroes to get default
3406 if (opt->line_style != NULL)
3407 return opt->line_style;
3409 return &pg_asciiformat;
3413 refresh_utf8format(const printTableOpt *opt)
3415 printTextFormat *popt = &pg_utf8format;
3417 const unicodeStyleBorderFormat *border;
3418 const unicodeStyleRowFormat *header;
3419 const unicodeStyleColumnFormat *column;
3421 popt->name = "unicode";
3423 border = &unicode_style.border_style[opt->unicode_border_linestyle];
3424 header = &unicode_style.row_style[opt->unicode_header_linestyle];
3425 column = &unicode_style.column_style[opt->unicode_column_linestyle];
3427 popt->lrule[PRINT_RULE_TOP].hrule = border->horizontal;
3428 popt->lrule[PRINT_RULE_TOP].leftvrule = border->down_and_right;
3429 popt->lrule[PRINT_RULE_TOP].midvrule = column->down_and_horizontal[opt->unicode_border_linestyle];
3430 popt->lrule[PRINT_RULE_TOP].rightvrule = border->down_and_left;
3432 popt->lrule[PRINT_RULE_MIDDLE].hrule = header->horizontal;
3433 popt->lrule[PRINT_RULE_MIDDLE].leftvrule = header->vertical_and_right[opt->unicode_border_linestyle];
3434 popt->lrule[PRINT_RULE_MIDDLE].midvrule = column->vertical_and_horizontal[opt->unicode_header_linestyle];
3435 popt->lrule[PRINT_RULE_MIDDLE].rightvrule = header->vertical_and_left[opt->unicode_border_linestyle];
3437 popt->lrule[PRINT_RULE_BOTTOM].hrule = border->horizontal;
3438 popt->lrule[PRINT_RULE_BOTTOM].leftvrule = border->up_and_right;
3439 popt->lrule[PRINT_RULE_BOTTOM].midvrule = column->up_and_horizontal[opt->unicode_border_linestyle];
3440 popt->lrule[PRINT_RULE_BOTTOM].rightvrule = border->left_and_right;
3443 popt->lrule[PRINT_RULE_DATA].hrule = "";
3444 popt->lrule[PRINT_RULE_DATA].leftvrule = border->vertical;
3445 popt->lrule[PRINT_RULE_DATA].midvrule = column->vertical;
3446 popt->lrule[PRINT_RULE_DATA].rightvrule = border->vertical;
3448 popt->midvrule_nl = column->vertical;
3449 popt->midvrule_wrap = column->vertical;
3450 popt->midvrule_blank = column->vertical;
3452 /* Same for all unicode today */
3453 popt->header_nl_left = unicode_style.header_nl_left;
3454 popt->header_nl_right = unicode_style.header_nl_right;
3455 popt->nl_left = unicode_style.nl_left;
3456 popt->nl_right = unicode_style.nl_right;
3457 popt->wrap_left = unicode_style.wrap_left;
3458 popt->wrap_right = unicode_style.wrap_right;
3459 popt->wrap_right_border = unicode_style.wrap_right_border;
3465 * Compute the byte distance to the end of the string or *target_width
3466 * display character positions, whichever comes first. Update *target_width
3467 * to be the number of display character positions actually filled.
3470 strlen_max_width(unsigned char *str, int *target_width, int encoding)
3472 unsigned char *start = str;
3473 unsigned char *end = str + strlen((char *) str);
3478 int char_width = PQdsplen((char *) str, encoding);
3481 * If the display width of the new character causes the string to
3482 * exceed its target width, skip it and return. However, if this is
3483 * the first character of the string (curr_width == 0), we have to
3486 if (*target_width < curr_width + char_width && curr_width != 0)
3489 curr_width += char_width;
3491 str += PQmblen((char *) str, encoding);
3494 *target_width = curr_width;