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-2016, 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"
27 #include <sys/ioctl.h> /* for ioctl() */
34 #include "fe_utils/print.h"
36 #include "catalog/pg_type.h"
37 #include "fe_utils/mbprint.h"
41 * If the calling program doesn't have any mechanism for setting
42 * cancel_pressed, it will have no effect.
44 * Note: print.c's general strategy for when to check cancel_pressed is to do
45 * so at completion of each row of output.
47 volatile bool cancel_pressed = false;
49 static bool always_ignore_sigpipe = false;
51 /* info for locale-aware numeric formatting; set up by setDecimalLocale() */
52 static char *decimal_point;
53 static int groupdigits;
54 static char *thousands_sep;
56 static char default_footer[100];
57 static printTableFooter default_footer_cell = {default_footer, NULL};
59 /* Line style control structures */
60 const printTextFormat pg_asciiformat =
81 const printTextFormat pg_asciiformat_old =
102 /* Default unicode linestyle format */
103 printTextFormat pg_utf8format;
105 typedef struct unicodeStyleRowFormat
107 const char *horizontal;
108 const char *vertical_and_right[2];
109 const char *vertical_and_left[2];
110 } unicodeStyleRowFormat;
112 typedef struct unicodeStyleColumnFormat
114 const char *vertical;
115 const char *vertical_and_horizontal[2];
116 const char *up_and_horizontal[2];
117 const char *down_and_horizontal[2];
118 } unicodeStyleColumnFormat;
120 typedef struct unicodeStyleBorderFormat
122 const char *up_and_right;
123 const char *vertical;
124 const char *down_and_right;
125 const char *horizontal;
126 const char *down_and_left;
127 const char *left_and_right;
128 } unicodeStyleBorderFormat;
130 typedef struct unicodeStyleFormat
132 unicodeStyleRowFormat row_style[2];
133 unicodeStyleColumnFormat column_style[2];
134 unicodeStyleBorderFormat border_style[2];
135 const char *header_nl_left;
136 const char *header_nl_right;
138 const char *nl_right;
139 const char *wrap_left;
140 const char *wrap_right;
141 bool wrap_right_border;
142 } unicodeStyleFormat;
144 static const unicodeStyleFormat unicode_style = {
150 {"\342\224\234", "\342\225\237"},
152 {"\342\224\244", "\342\225\242"},
158 {"\342\225\236", "\342\225\240"},
160 {"\342\225\241", "\342\225\243"},
168 {"\342\224\274", "\342\225\252"},
170 {"\342\224\264", "\342\225\247"},
172 {"\342\224\254", "\342\225\244"},
178 {"\342\225\253", "\342\225\254"},
180 {"\342\225\250", "\342\225\251"},
182 {"\342\225\245", "\342\225\246"},
187 {"\342\224\224", "\342\224\202", "\342\224\214", "\342\224\200", "\342\224\220", "\342\224\230"},
189 {"\342\225\232", "\342\225\221", "\342\225\224", "\342\225\220", "\342\225\227", "\342\225\235"},
192 "\342\206\265", /* ↵ */
194 "\342\206\265", /* ↵ */
195 "\342\200\246", /* … */
196 "\342\200\246", /* … */
201 /* Local functions */
202 static int strlen_max_width(unsigned char *str, int *target_width, int encoding);
203 static void IsPagerNeeded(const printTableContent *cont, int extra_lines, bool expanded,
204 FILE **fout, bool *is_pager);
206 static void print_aligned_vertical(const printTableContent *cont,
207 FILE *fout, bool is_pager);
210 /* Count number of digits in integral part of number */
212 integer_digits(const char *my_str)
214 /* ignoring any sign ... */
215 if (my_str[0] == '-' || my_str[0] == '+')
217 /* ... count initial integral digits */
218 return strspn(my_str, "0123456789");
221 /* Compute additional length required for locale-aware numeric output */
223 additional_numeric_locale_len(const char *my_str)
225 int int_len = integer_digits(my_str),
228 /* Account for added thousands_sep instances */
229 if (int_len > groupdigits)
230 len += ((int_len - 1) / groupdigits) * strlen(thousands_sep);
232 /* Account for possible additional length of decimal_point */
233 if (strchr(my_str, '.') != NULL)
234 len += strlen(decimal_point) - 1;
240 * Format a numeric value per current LC_NUMERIC locale setting
242 * Returns the appropriately formatted string in a new allocated block,
245 * setDecimalLocale() must have been called earlier.
248 format_numeric_locale(const char *my_str)
258 * If the string doesn't look like a number, return it unchanged. This
259 * check is essential to avoid mangling already-localized "money" values.
261 if (strspn(my_str, "0123456789+-.eE") != strlen(my_str))
262 return pg_strdup(my_str);
264 new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
265 new_str = pg_malloc(new_len + 1);
267 int_len = integer_digits(my_str);
269 /* number of digits in first thousands group */
270 leading_digits = int_len % groupdigits;
271 if (leading_digits == 0)
272 leading_digits = groupdigits;
275 if (my_str[0] == '-' || my_str[0] == '+')
277 new_str[new_str_pos++] = my_str[0];
281 /* process integer part of number */
282 for (i = 0; i < int_len; i++)
284 /* Time to insert separator? */
285 if (i > 0 && --leading_digits == 0)
287 strcpy(&new_str[new_str_pos], thousands_sep);
288 new_str_pos += strlen(thousands_sep);
289 leading_digits = groupdigits;
291 new_str[new_str_pos++] = my_str[i];
294 /* handle decimal point if any */
295 if (my_str[i] == '.')
297 strcpy(&new_str[new_str_pos], decimal_point);
298 new_str_pos += strlen(decimal_point);
302 /* copy the rest (fractional digits and/or exponent, and \0 terminator) */
303 strcpy(&new_str[new_str_pos], &my_str[i]);
305 /* assert we didn't underestimate new_len (an overestimate is OK) */
306 Assert(strlen(new_str) <= new_len);
313 * fputnbytes: print exactly N bytes to a file
315 * We avoid using %.*s here because it can misbehave if the data
316 * is not valid in what libc thinks is the prevailing encoding.
319 fputnbytes(FILE *f, const char *str, size_t n)
327 print_separator(struct separator sep, FILE *fout)
329 if (sep.separator_zero)
331 else if (sep.separator)
332 fputs(sep.separator, fout);
337 * Return the list of explicitly-requested footers or, when applicable, the
338 * default "(xx rows)" footer. Always omit the default footer when given
339 * non-default footers, "\pset footer off", or a specific instruction to that
340 * effect from a calling backslash command. Vertical formats number each row,
341 * making the default footer redundant; they do not call this function.
343 * The return value may point to static storage; do not keep it across calls.
345 static printTableFooter *
346 footers_with_default(const printTableContent *cont)
348 if (cont->footers == NULL && cont->opt->default_footer)
350 unsigned long total_records;
352 total_records = cont->opt->prior_records + cont->nrows;
353 snprintf(default_footer, sizeof(default_footer),
354 ngettext("(%lu row)", "(%lu rows)", total_records),
357 return &default_footer_cell;
360 return cont->footers;
364 /*************************/
366 /*************************/
370 print_unaligned_text(const printTableContent *cont, FILE *fout)
372 bool opt_tuples_only = cont->opt->tuples_only;
374 const char *const * ptr;
375 bool need_recordsep = false;
380 if (cont->opt->start_table)
383 if (!opt_tuples_only && cont->title)
385 fputs(cont->title, fout);
386 print_separator(cont->opt->recordSep, fout);
390 if (!opt_tuples_only)
392 for (ptr = cont->headers; *ptr; ptr++)
394 if (ptr != cont->headers)
395 print_separator(cont->opt->fieldSep, fout);
398 need_recordsep = true;
402 /* assume continuing printout */
403 need_recordsep = true;
406 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
410 print_separator(cont->opt->recordSep, fout);
411 need_recordsep = false;
417 if ((i + 1) % cont->ncolumns)
418 print_separator(cont->opt->fieldSep, fout);
420 need_recordsep = true;
424 if (cont->opt->stop_table)
426 printTableFooter *footers = footers_with_default(cont);
428 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
432 for (f = footers; f; f = f->next)
436 print_separator(cont->opt->recordSep, fout);
437 need_recordsep = false;
439 fputs(f->data, fout);
440 need_recordsep = true;
445 * The last record is terminated by a newline, independent of the set
446 * record separator. But when the record separator is a zero byte, we
447 * use that (compatible with find -print0 and xargs).
451 if (cont->opt->recordSep.separator_zero)
452 print_separator(cont->opt->recordSep, fout);
461 print_unaligned_vertical(const printTableContent *cont, FILE *fout)
463 bool opt_tuples_only = cont->opt->tuples_only;
465 const char *const * ptr;
466 bool need_recordsep = false;
471 if (cont->opt->start_table)
474 if (!opt_tuples_only && cont->title)
476 fputs(cont->title, fout);
477 need_recordsep = true;
481 /* assume continuing printout */
482 need_recordsep = true;
485 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
489 /* record separator is 2 occurrences of recordsep in this mode */
490 print_separator(cont->opt->recordSep, fout);
491 print_separator(cont->opt->recordSep, fout);
492 need_recordsep = false;
497 fputs(cont->headers[i % cont->ncolumns], fout);
498 print_separator(cont->opt->fieldSep, fout);
501 if ((i + 1) % cont->ncolumns)
502 print_separator(cont->opt->recordSep, fout);
504 need_recordsep = true;
507 if (cont->opt->stop_table)
510 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
514 print_separator(cont->opt->recordSep, fout);
515 for (f = cont->footers; f; f = f->next)
517 print_separator(cont->opt->recordSep, fout);
518 fputs(f->data, fout);
522 /* see above in print_unaligned_text() */
525 if (cont->opt->recordSep.separator_zero)
526 print_separator(cont->opt->recordSep, fout);
534 /********************/
536 /********************/
541 _print_horizontal_line(const unsigned int ncolumns, const unsigned int *widths,
542 unsigned short border, printTextRule pos,
543 const printTextFormat *format,
546 const printTextLineFormat *lformat = &format->lrule[pos];
551 fputs(lformat->hrule, fout);
552 else if (border == 2)
553 fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
555 for (i = 0; i < ncolumns; i++)
557 for (j = 0; j < widths[i]; j++)
558 fputs(lformat->hrule, fout);
560 if (i < ncolumns - 1)
565 fprintf(fout, "%s%s%s", lformat->hrule,
566 lformat->midvrule, lformat->hrule);
571 fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
572 else if (border == 1)
573 fputs(lformat->hrule, fout);
580 * Print pretty boxes around cells.
583 print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
585 bool opt_tuples_only = cont->opt->tuples_only;
586 int encoding = cont->opt->encoding;
587 unsigned short opt_border = cont->opt->border;
588 const printTextFormat *format = get_line_style(cont->opt);
589 const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
591 unsigned int col_count = 0,
597 unsigned int *width_header,
601 unsigned int *max_nl_lines, /* value split by newlines */
604 unsigned char **format_buf;
605 unsigned int width_total;
606 unsigned int total_header_width;
607 unsigned int extra_row_output_lines = 0;
608 unsigned int extra_output_lines = 0;
610 const char *const * ptr;
612 struct lineptr **col_lineptrs; /* pointers to line pointer per column */
614 bool *header_done; /* Have all header lines been output? */
615 int *bytes_output; /* Bytes output for column value */
616 printTextLineWrap *wrap; /* Wrap status for each column */
617 int output_columns = 0; /* Width of interactive console */
618 bool is_local_pager = false;
626 if (cont->ncolumns > 0)
628 col_count = cont->ncolumns;
629 width_header = pg_malloc0(col_count * sizeof(*width_header));
630 width_average = pg_malloc0(col_count * sizeof(*width_average));
631 max_width = pg_malloc0(col_count * sizeof(*max_width));
632 width_wrap = pg_malloc0(col_count * sizeof(*width_wrap));
633 max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines));
634 curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line));
635 col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs));
636 max_bytes = pg_malloc0(col_count * sizeof(*max_bytes));
637 format_buf = pg_malloc0(col_count * sizeof(*format_buf));
638 header_done = pg_malloc0(col_count * sizeof(*header_done));
639 bytes_output = pg_malloc0(col_count * sizeof(*bytes_output));
640 wrap = pg_malloc0(col_count * sizeof(*wrap));
645 width_average = NULL;
658 /* scan all column headers, find maximum width and max max_nl_lines */
659 for (i = 0; i < col_count; i++)
665 pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
666 encoding, &width, &nl_lines, &bytes_required);
667 if (width > max_width[i])
668 max_width[i] = width;
669 if (nl_lines > max_nl_lines[i])
670 max_nl_lines[i] = nl_lines;
671 if (bytes_required > max_bytes[i])
672 max_bytes[i] = bytes_required;
673 if (nl_lines > extra_row_output_lines)
674 extra_row_output_lines = nl_lines;
676 width_header[i] = width;
678 /* Add height of tallest header column */
679 extra_output_lines += extra_row_output_lines;
680 extra_row_output_lines = 0;
682 /* scan all cells, find maximum width, compute cell_count */
683 for (i = 0, ptr = cont->cells; *ptr; ptr++, i++, cell_count++)
689 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
690 &width, &nl_lines, &bytes_required);
692 if (width > max_width[i % col_count])
693 max_width[i % col_count] = width;
694 if (nl_lines > max_nl_lines[i % col_count])
695 max_nl_lines[i % col_count] = nl_lines;
696 if (bytes_required > max_bytes[i % col_count])
697 max_bytes[i % col_count] = bytes_required;
699 width_average[i % col_count] += width;
702 /* If we have rows, compute average */
703 if (col_count != 0 && cell_count != 0)
705 int rows = cell_count / col_count;
707 for (i = 0; i < col_count; i++)
708 width_average[i] /= rows;
711 /* adjust the total display width based on border style */
713 width_total = col_count;
714 else if (opt_border == 1)
715 width_total = col_count * 3 - ((col_count > 0) ? 1 : 0);
717 width_total = col_count * 3 + 1;
718 total_header_width = width_total;
720 for (i = 0; i < col_count; i++)
722 width_total += max_width[i];
723 total_header_width += width_header[i];
727 * At this point: max_width[] contains the max width of each column,
728 * max_nl_lines[] contains the max number of lines in each column,
729 * max_bytes[] contains the maximum storage space for formatting strings,
730 * width_total contains the giant width sum. Now we allocate some memory
733 for (i = 0; i < col_count; i++)
735 /* Add entry for ptr == NULL array termination */
736 col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) *
737 sizeof(**col_lineptrs));
739 format_buf[i] = pg_malloc(max_bytes[i] + 1);
741 col_lineptrs[i]->ptr = format_buf[i];
744 /* Default word wrap to the full width, i.e. no word wrap */
745 for (i = 0; i < col_count; i++)
746 width_wrap[i] = max_width[i];
749 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
751 if (cont->opt->columns > 0)
752 output_columns = cont->opt->columns;
753 else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
755 if (cont->opt->env_columns > 0)
756 output_columns = cont->opt->env_columns;
760 struct winsize screen_size;
762 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
763 output_columns = screen_size.ws_col;
768 if (cont->opt->format == PRINT_WRAPPED)
771 * Optional optimized word wrap. Shrink columns with a high max/avg
772 * ratio. Slightly bias against wider columns. (Increases chance a
773 * narrow column will fit in its cell.) If available columns is
774 * positive... and greater than the width of the unshrinkable column
777 if (output_columns > 0 && output_columns >= total_header_width)
779 /* While there is still excess width... */
780 while (width_total > output_columns)
782 double max_ratio = 0;
786 * Find column that has the highest ratio of its maximum width
787 * compared to its average width. This tells us which column
788 * will produce the fewest wrapped values if shortened.
789 * width_wrap starts as equal to max_width.
791 for (i = 0; i < col_count; i++)
793 if (width_average[i] && width_wrap[i] > width_header[i])
795 /* Penalize wide columns by 1% of their width */
798 ratio = (double) width_wrap[i] / width_average[i] +
800 if (ratio > max_ratio)
808 /* Exit loop if we can't squeeze any more. */
812 /* Decrease width of target column by one. */
813 width_wrap[worst_col]--;
820 * If in expanded auto mode, we have now calculated the expected width, so
821 * we can now escape to vertical mode if necessary. If the output has
822 * only one column, the expanded format would be wider than the regular
823 * format, so don't use it in that case.
825 if (cont->opt->expanded == 2 && output_columns > 0 && cont->ncolumns > 1 &&
826 (output_columns < total_header_width || output_columns < width_total))
828 print_aligned_vertical(cont, fout, is_pager);
832 /* If we wrapped beyond the display width, use the pager */
833 if (!is_pager && fout == stdout && output_columns > 0 &&
834 (output_columns < total_header_width || output_columns < width_total))
836 fout = PageOutput(INT_MAX, cont->opt); /* force pager */
837 is_pager = is_local_pager = true;
840 /* Check if newlines or our wrapping now need the pager */
841 if (!is_pager && fout == stdout)
843 /* scan all cells, find maximum width, compute cell_count */
844 for (i = 0, ptr = cont->cells; *ptr; ptr++, cell_count++)
850 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
851 &width, &nl_lines, &bytes_required);
854 * A row can have both wrapping and newlines that cause it to
855 * display across multiple lines. We check for both cases below.
857 if (width > 0 && width_wrap[i])
859 unsigned int extra_lines;
861 /* don't count the first line of nl_lines - it's not "extra" */
862 extra_lines = ((width - 1) / width_wrap[i]) + nl_lines - 1;
863 if (extra_lines > extra_row_output_lines)
864 extra_row_output_lines = extra_lines;
867 /* i is the current column number: increment with wrap */
868 if (++i >= col_count)
871 /* At last column of each row, add tallest column height */
872 extra_output_lines += extra_row_output_lines;
873 extra_row_output_lines = 0;
876 IsPagerNeeded(cont, extra_output_lines, false, &fout, &is_pager);
877 is_local_pager = is_pager;
881 if (cont->opt->start_table)
884 if (cont->title && !opt_tuples_only)
889 pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
890 encoding, &width, &height, NULL);
891 if (width >= width_total)
893 fprintf(fout, "%s\n", cont->title);
896 fprintf(fout, "%-*s%s\n", (width_total - width) / 2, "",
901 if (!opt_tuples_only)
903 int more_col_wrapping;
907 _print_horizontal_line(col_count, width_wrap, opt_border,
908 PRINT_RULE_TOP, format, fout);
910 for (i = 0; i < col_count; i++)
911 pg_wcsformat((const unsigned char *) cont->headers[i],
912 strlen(cont->headers[i]), encoding,
913 col_lineptrs[i], max_nl_lines[i]);
915 more_col_wrapping = col_count;
917 memset(header_done, false, col_count * sizeof(bool));
918 while (more_col_wrapping)
921 fputs(dformat->leftvrule, fout);
923 for (i = 0; i < cont->ncolumns; i++)
925 struct lineptr *this_line = col_lineptrs[i] + curr_nl_line;
926 unsigned int nbspace;
928 if (opt_border != 0 ||
929 (!format->wrap_right_border && i > 0))
930 fputs(curr_nl_line ? format->header_nl_left : " ",
935 nbspace = width_wrap[i] - this_line->width;
938 fprintf(fout, "%-*s%s%-*s",
939 nbspace / 2, "", this_line->ptr, (nbspace + 1) / 2, "");
941 if (!(this_line + 1)->ptr)
948 fprintf(fout, "%*s", width_wrap[i], "");
950 if (opt_border != 0 || format->wrap_right_border)
951 fputs(!header_done[i] ? format->header_nl_right : " ",
954 if (opt_border != 0 && col_count > 0 && i < col_count - 1)
955 fputs(dformat->midvrule, fout);
960 fputs(dformat->rightvrule, fout);
964 _print_horizontal_line(col_count, width_wrap, opt_border,
965 PRINT_RULE_MIDDLE, format, fout);
969 /* print cells, one loop per row */
970 for (i = 0, ptr = cont->cells; *ptr; i += col_count, ptr += col_count)
980 for (j = 0; j < col_count; j++)
982 pg_wcsformat((const unsigned char *) ptr[j], strlen(ptr[j]), encoding,
983 col_lineptrs[j], max_nl_lines[j]);
987 memset(bytes_output, 0, col_count * sizeof(int));
990 * Each time through this loop, one display line is output. It can
991 * either be a full value or a partial value if embedded newlines
992 * exist or if 'format=wrapping' mode is enabled.
1000 fputs(dformat->leftvrule, fout);
1002 /* for each column */
1003 for (j = 0; j < col_count; j++)
1005 /* We have a valid array element, so index it */
1006 struct lineptr *this_line = &col_lineptrs[j][curr_nl_line[j]];
1007 int bytes_to_output;
1008 int chars_to_output = width_wrap[j];
1009 bool finalspaces = (opt_border == 2 ||
1010 (col_count > 0 && j < col_count - 1));
1012 /* Print left-hand wrap or newline mark */
1013 if (opt_border != 0)
1015 if (wrap[j] == PRINT_LINE_WRAP_WRAP)
1016 fputs(format->wrap_left, fout);
1017 else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1018 fputs(format->nl_left, fout);
1023 if (!this_line->ptr)
1025 /* Past newline lines so just pad for other columns */
1027 fprintf(fout, "%*s", chars_to_output, "");
1031 /* Get strlen() of the characters up to width_wrap */
1033 strlen_max_width(this_line->ptr + bytes_output[j],
1034 &chars_to_output, encoding);
1037 * If we exceeded width_wrap, it means the display width
1038 * of a single character was wider than our target width.
1039 * In that case, we have to pretend we are only printing
1040 * the target display width and make the best of it.
1042 if (chars_to_output > width_wrap[j])
1043 chars_to_output = width_wrap[j];
1045 if (cont->aligns[j] == 'r') /* Right aligned cell */
1048 fprintf(fout, "%*s", width_wrap[j] - chars_to_output, "");
1050 (char *) (this_line->ptr + bytes_output[j]),
1053 else /* Left aligned cell */
1057 (char *) (this_line->ptr + bytes_output[j]),
1061 bytes_output[j] += bytes_to_output;
1063 /* Do we have more text to wrap? */
1064 if (*(this_line->ptr + bytes_output[j]) != '\0')
1068 /* Advance to next newline line */
1070 if (col_lineptrs[j][curr_nl_line[j]].ptr != NULL)
1072 bytes_output[j] = 0;
1076 /* Determine next line's wrap status for this column */
1077 wrap[j] = PRINT_LINE_WRAP_NONE;
1078 if (col_lineptrs[j][curr_nl_line[j]].ptr != NULL)
1080 if (bytes_output[j] != 0)
1081 wrap[j] = PRINT_LINE_WRAP_WRAP;
1082 else if (curr_nl_line[j] != 0)
1083 wrap[j] = PRINT_LINE_WRAP_NEWLINE;
1087 * If left-aligned, pad out remaining space if needed (not
1088 * last column, and/or wrap marks required).
1090 if (cont->aligns[j] != 'r') /* Left aligned cell */
1093 wrap[j] == PRINT_LINE_WRAP_WRAP ||
1094 wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1095 fprintf(fout, "%*s",
1096 width_wrap[j] - chars_to_output, "");
1099 /* Print right-hand wrap or newline mark */
1100 if (wrap[j] == PRINT_LINE_WRAP_WRAP)
1101 fputs(format->wrap_right, fout);
1102 else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1103 fputs(format->nl_right, fout);
1104 else if (opt_border == 2 || (col_count > 0 && j < col_count - 1))
1107 /* Print column divider, if not the last column */
1108 if (opt_border != 0 && (col_count > 0 && j < col_count - 1))
1110 if (wrap[j + 1] == PRINT_LINE_WRAP_WRAP)
1111 fputs(format->midvrule_wrap, fout);
1112 else if (wrap[j + 1] == PRINT_LINE_WRAP_NEWLINE)
1113 fputs(format->midvrule_nl, fout);
1114 else if (col_lineptrs[j + 1][curr_nl_line[j + 1]].ptr == NULL)
1115 fputs(format->midvrule_blank, fout);
1117 fputs(dformat->midvrule, fout);
1121 /* end-of-row border */
1122 if (opt_border == 2)
1123 fputs(dformat->rightvrule, fout);
1126 } while (more_lines);
1129 if (cont->opt->stop_table)
1131 printTableFooter *footers = footers_with_default(cont);
1133 if (opt_border == 2 && !cancel_pressed)
1134 _print_horizontal_line(col_count, width_wrap, opt_border,
1135 PRINT_RULE_BOTTOM, format, fout);
1138 if (footers && !opt_tuples_only && !cancel_pressed)
1140 printTableFooter *f;
1142 for (f = footers; f; f = f->next)
1143 fprintf(fout, "%s\n", f->data);
1151 for (i = 0; i < col_count; i++)
1153 free(col_lineptrs[i]);
1154 free(format_buf[i]);
1157 free(width_average);
1175 print_aligned_vertical_line(const printTextFormat *format,
1176 const unsigned short opt_border,
1177 unsigned long record,
1178 unsigned int hwidth,
1179 unsigned int dwidth,
1183 const printTextLineFormat *lformat = &format->lrule[pos];
1187 if (opt_border == 2)
1188 fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
1189 else if (opt_border == 1)
1190 fputs(lformat->hrule, fout);
1194 if (opt_border == 0)
1195 reclen = fprintf(fout, "* Record %lu", record);
1197 reclen = fprintf(fout, "[ RECORD %lu ]", record);
1199 if (opt_border != 2)
1203 for (i = reclen; i < hwidth; i++)
1204 fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1210 fputs(lformat->hrule, fout);
1212 fputs(lformat->midvrule, fout);
1214 fputs(lformat->hrule, fout);
1223 for (i = reclen; i < dwidth; i++)
1224 fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1225 if (opt_border == 2)
1226 fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
1231 print_aligned_vertical(const printTableContent *cont,
1232 FILE *fout, bool is_pager)
1234 bool opt_tuples_only = cont->opt->tuples_only;
1235 unsigned short opt_border = cont->opt->border;
1236 const printTextFormat *format = get_line_style(cont->opt);
1237 const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
1238 int encoding = cont->opt->encoding;
1239 unsigned long record = cont->opt->prior_records + 1;
1240 const char *const * ptr;
1248 struct lineptr *hlineptr,
1250 bool is_local_pager = false,
1253 int output_columns = 0; /* Width of interactive console */
1261 if (cont->cells[0] == NULL && cont->opt->start_table &&
1262 cont->opt->stop_table)
1264 printTableFooter *footers = footers_with_default(cont);
1266 if (!opt_tuples_only && !cancel_pressed && footers)
1268 printTableFooter *f;
1270 for (f = footers; f; f = f->next)
1271 fprintf(fout, "%s\n", f->data);
1280 * Deal with the pager here instead of in printTable(), because we could
1281 * get here via print_aligned_text() in expanded auto mode, and so we have
1282 * to recalculate the pager requirement based on vertical output.
1286 IsPagerNeeded(cont, 0, true, &fout, &is_pager);
1287 is_local_pager = is_pager;
1290 /* Find the maximum dimensions for the headers */
1291 for (i = 0; i < cont->ncolumns; i++)
1297 pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
1298 encoding, &width, &height, &fs);
1301 if (height > hheight)
1306 if (fs > hformatsize)
1310 /* find longest data cell */
1311 for (i = 0, ptr = cont->cells; *ptr; ptr++, i++)
1317 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
1318 &width, &height, &fs);
1321 if (height > dheight)
1326 if (fs > dformatsize)
1331 * We now have all the information we need to setup the formatting
1334 dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1));
1335 hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1));
1337 dlineptr->ptr = pg_malloc(dformatsize);
1338 hlineptr->ptr = pg_malloc(hformatsize);
1340 if (cont->opt->start_table)
1343 if (!opt_tuples_only && cont->title)
1344 fprintf(fout, "%s\n", cont->title);
1348 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
1350 if (cont->opt->columns > 0)
1351 output_columns = cont->opt->columns;
1352 else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
1354 if (cont->opt->env_columns > 0)
1355 output_columns = cont->opt->env_columns;
1359 struct winsize screen_size;
1361 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
1362 output_columns = screen_size.ws_col;
1368 * Calculate available width for data in wrapped mode
1370 if (cont->opt->format == PRINT_WRAPPED)
1372 unsigned int swidth,
1376 if (opt_border == 0)
1379 * For border = 0, one space in the middle. (If we discover we
1380 * need to wrap, the spacer column will be replaced by a wrap
1381 * marker, and we'll make room below for another wrap marker at
1382 * the end of the line. But for now, assume no wrap is needed.)
1386 /* We might need a column for header newline markers, too */
1390 else if (opt_border == 1)
1393 * For border = 1, two spaces and a vrule in the middle. (As
1394 * above, we might need one more column for a wrap marker.)
1398 /* We might need a column for left header newline markers, too */
1399 if (hmultiline && (format == &pg_asciiformat_old))
1405 * For border = 2, two more for the vrules at the beginning and
1406 * end of the lines, plus spacer columns adjacent to these. (We
1407 * won't need extra columns for wrap/newline markers, we'll just
1408 * repurpose the spacers.)
1413 /* Reserve a column for data newline indicators, too, if needed */
1415 opt_border < 2 && format != &pg_asciiformat_old)
1418 /* Determine width required for record header lines */
1419 if (!opt_tuples_only)
1421 if (cont->nrows > 0)
1422 rwidth = 1 + (int) log10(cont->nrows);
1423 if (opt_border == 0)
1424 rwidth += 9; /* "* RECORD " */
1425 else if (opt_border == 1)
1426 rwidth += 12; /* "-[ RECORD ]" */
1428 rwidth += 15; /* "+-[ RECORD ]-+" */
1431 /* We might need to do the rest of the calculation twice */
1436 /* Total width required to not wrap data */
1437 width = hwidth + swidth + dwidth;
1438 /* ... and not the header lines, either */
1442 if (output_columns > 0)
1444 unsigned int min_width;
1446 /* Minimum acceptable width: room for just 3 columns of data */
1447 min_width = hwidth + swidth + 3;
1448 /* ... but not less than what the record header lines need */
1449 if (min_width < rwidth)
1452 if (output_columns >= width)
1454 /* Plenty of room, use native data width */
1455 /* (but at least enough for the record header lines) */
1456 newdwidth = width - hwidth - swidth;
1458 else if (output_columns < min_width)
1460 /* Set data width to match min_width */
1461 newdwidth = min_width - hwidth - swidth;
1465 /* Set data width to match output_columns */
1466 newdwidth = output_columns - hwidth - swidth;
1471 /* Don't know the wrap limit, so use native data width */
1472 /* (but at least enough for the record header lines) */
1473 newdwidth = width - hwidth - swidth;
1477 * If we will need to wrap data and didn't already allocate a data
1478 * newline/wrap marker column, do so and recompute.
1480 if (newdwidth < dwidth && !dmultiline &&
1481 opt_border < 2 && format != &pg_asciiformat_old)
1494 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1508 pos = PRINT_RULE_TOP;
1510 pos = PRINT_RULE_MIDDLE;
1512 /* Print record header (e.g. "[ RECORD N ]") above each record */
1513 if (i % cont->ncolumns == 0)
1515 unsigned int lhwidth = hwidth;
1517 if ((opt_border < 2) &&
1519 (format == &pg_asciiformat_old))
1520 lhwidth++; /* for newline indicators */
1522 if (!opt_tuples_only)
1523 print_aligned_vertical_line(format, opt_border, record++,
1524 lhwidth, dwidth, pos, fout);
1525 else if (i != 0 || !cont->opt->start_table || opt_border == 2)
1526 print_aligned_vertical_line(format, opt_border, 0, lhwidth,
1530 /* Format the header */
1531 pg_wcsformat((const unsigned char *) cont->headers[i % cont->ncolumns],
1532 strlen(cont->headers[i % cont->ncolumns]),
1533 encoding, hlineptr, hheight);
1534 /* Format the data */
1535 pg_wcsformat((const unsigned char *) *ptr, strlen(*ptr), encoding,
1539 * Loop through header and data in parallel dealing with newlines and
1540 * wrapped lines until they're both exhausted
1543 dcomplete = hcomplete = 0;
1545 chars_to_output = dlineptr[dline].width;
1546 while (!dcomplete || !hcomplete)
1549 if (opt_border == 2)
1550 fprintf(fout, "%s", dformat->leftvrule);
1552 /* Header (never wrapped so just need to deal with newlines) */
1555 int swidth = hwidth,
1556 target_width = hwidth;
1559 * Left spacer or new line indicator
1561 if ((opt_border == 2) ||
1562 (hmultiline && (format == &pg_asciiformat_old)))
1563 fputs(hline ? format->header_nl_left : " ", fout);
1568 strlen_max_width(hlineptr[hline].ptr, &target_width,
1570 fprintf(fout, "%-s", hlineptr[hline].ptr);
1575 swidth -= target_width;
1577 fprintf(fout, "%*s", swidth, " ");
1580 * New line indicator or separator's space
1582 if (hlineptr[hline + 1].ptr)
1584 /* More lines after this one due to a newline */
1585 if ((opt_border > 0) ||
1586 (hmultiline && (format != &pg_asciiformat_old)))
1587 fputs(format->header_nl_right, fout);
1592 /* This was the last line of the header */
1593 if ((opt_border > 0) ||
1594 (hmultiline && (format != &pg_asciiformat_old)))
1601 unsigned int swidth = hwidth + opt_border;
1603 if ((opt_border < 2) &&
1605 (format == &pg_asciiformat_old))
1608 if ((opt_border == 0) &&
1609 (format != &pg_asciiformat_old) &&
1613 fprintf(fout, "%*s", swidth, " ");
1620 fputs(format->midvrule_wrap, fout);
1621 else if (dline == 0)
1622 fputs(dformat->midvrule, fout);
1624 fputs(format->midvrule_nl, fout);
1630 int target_width = dwidth,
1635 * Left spacer or wrap indicator
1637 fputs(offset == 0 ? " " : format->wrap_left, fout);
1642 bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
1643 &target_width, encoding);
1644 fputnbytes(fout, (char *) (dlineptr[dline].ptr + offset),
1647 chars_to_output -= target_width;
1648 offset += bytes_to_output;
1651 swidth -= target_width;
1653 if (chars_to_output)
1655 /* continuing a wrapped column */
1656 if ((opt_border > 1) ||
1657 (dmultiline && (format != &pg_asciiformat_old)))
1660 fprintf(fout, "%*s", swidth, " ");
1661 fputs(format->wrap_right, fout);
1664 else if (dlineptr[dline + 1].ptr)
1666 /* reached a newline in the column */
1667 if ((opt_border > 1) ||
1668 (dmultiline && (format != &pg_asciiformat_old)))
1671 fprintf(fout, "%*s", swidth, " ");
1672 fputs(format->nl_right, fout);
1676 chars_to_output = dlineptr[dline].width;
1680 /* reached the end of the cell */
1684 fprintf(fout, "%*s", swidth, " ");
1691 if (opt_border == 2)
1692 fputs(dformat->rightvrule, fout);
1699 * data exhausted (this can occur if header is longer than the
1700 * data due to newlines in the header)
1705 fprintf(fout, "%*s %s\n", dwidth, "", dformat->rightvrule);
1710 if (cont->opt->stop_table)
1712 if (opt_border == 2 && !cancel_pressed)
1713 print_aligned_vertical_line(format, opt_border, 0, hwidth, dwidth,
1714 PRINT_RULE_BOTTOM, fout);
1717 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
1719 printTableFooter *f;
1723 for (f = cont->footers; f; f = f->next)
1724 fprintf(fout, "%s\n", f->data);
1730 free(hlineptr->ptr);
1731 free(dlineptr->ptr);
1740 /**********************/
1741 /* HTML printing ******/
1742 /**********************/
1746 html_escaped_print(const char *in, FILE *fout)
1749 bool leading_space = true;
1751 for (p = in; *p; p++)
1756 fputs("&", fout);
1759 fputs("<", fout);
1762 fputs(">", fout);
1765 fputs("<br />\n", fout);
1768 fputs(""", fout);
1771 /* protect leading space, for EXPLAIN output */
1773 fputs(" ", fout);
1781 leading_space = false;
1787 print_html_text(const printTableContent *cont, FILE *fout)
1789 bool opt_tuples_only = cont->opt->tuples_only;
1790 unsigned short opt_border = cont->opt->border;
1791 const char *opt_table_attr = cont->opt->tableAttr;
1793 const char *const * ptr;
1798 if (cont->opt->start_table)
1800 fprintf(fout, "<table border=\"%d\"", opt_border);
1802 fprintf(fout, " %s", opt_table_attr);
1806 if (!opt_tuples_only && cont->title)
1808 fputs(" <caption>", fout);
1809 html_escaped_print(cont->title, fout);
1810 fputs("</caption>\n", fout);
1814 if (!opt_tuples_only)
1816 fputs(" <tr>\n", fout);
1817 for (ptr = cont->headers; *ptr; ptr++)
1819 fputs(" <th align=\"center\">", fout);
1820 html_escaped_print(*ptr, fout);
1821 fputs("</th>\n", fout);
1823 fputs(" </tr>\n", fout);
1828 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1830 if (i % cont->ncolumns == 0)
1834 fputs(" <tr valign=\"top\">\n", fout);
1837 fprintf(fout, " <td align=\"%s\">", cont->aligns[(i) % cont->ncolumns] == 'r' ? "right" : "left");
1838 /* is string only whitespace? */
1839 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
1840 fputs(" ", fout);
1842 html_escaped_print(*ptr, fout);
1844 fputs("</td>\n", fout);
1846 if ((i + 1) % cont->ncolumns == 0)
1847 fputs(" </tr>\n", fout);
1850 if (cont->opt->stop_table)
1852 printTableFooter *footers = footers_with_default(cont);
1854 fputs("</table>\n", fout);
1857 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
1859 printTableFooter *f;
1862 for (f = footers; f; f = f->next)
1864 html_escaped_print(f->data, fout);
1865 fputs("<br />\n", fout);
1867 fputs("</p>", fout);
1876 print_html_vertical(const printTableContent *cont, FILE *fout)
1878 bool opt_tuples_only = cont->opt->tuples_only;
1879 unsigned short opt_border = cont->opt->border;
1880 const char *opt_table_attr = cont->opt->tableAttr;
1881 unsigned long record = cont->opt->prior_records + 1;
1883 const char *const * ptr;
1888 if (cont->opt->start_table)
1890 fprintf(fout, "<table border=\"%d\"", opt_border);
1892 fprintf(fout, " %s", opt_table_attr);
1896 if (!opt_tuples_only && cont->title)
1898 fputs(" <caption>", fout);
1899 html_escaped_print(cont->title, fout);
1900 fputs("</caption>\n", fout);
1905 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1907 if (i % cont->ncolumns == 0)
1911 if (!opt_tuples_only)
1913 "\n <tr><td colspan=\"2\" align=\"center\">Record %lu</td></tr>\n",
1916 fputs("\n <tr><td colspan=\"2\"> </td></tr>\n", fout);
1918 fputs(" <tr valign=\"top\">\n"
1920 html_escaped_print(cont->headers[i % cont->ncolumns], fout);
1921 fputs("</th>\n", fout);
1923 fprintf(fout, " <td align=\"%s\">", cont->aligns[i % cont->ncolumns] == 'r' ? "right" : "left");
1924 /* is string only whitespace? */
1925 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
1926 fputs(" ", fout);
1928 html_escaped_print(*ptr, fout);
1930 fputs("</td>\n </tr>\n", fout);
1933 if (cont->opt->stop_table)
1935 fputs("</table>\n", fout);
1938 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
1940 printTableFooter *f;
1943 for (f = cont->footers; f; f = f->next)
1945 html_escaped_print(f->data, fout);
1946 fputs("<br />\n", fout);
1948 fputs("</p>", fout);
1956 /*************************/
1958 /*************************/
1961 asciidoc_escaped_print(const char *in, FILE *fout)
1965 for (p = in; *p; p++)
1979 print_asciidoc_text(const printTableContent *cont, FILE *fout)
1981 bool opt_tuples_only = cont->opt->tuples_only;
1982 unsigned short opt_border = cont->opt->border;
1984 const char *const * ptr;
1989 if (cont->opt->start_table)
1991 /* print table in new paragraph - enforce preliminary new line */
1995 if (!opt_tuples_only && cont->title)
1998 fputs(cont->title, fout);
2002 /* print table [] header definition */
2003 fprintf(fout, "[%scols=\"", !opt_tuples_only ? "options=\"header\"," : "");
2004 for (i = 0; i < cont->ncolumns; i++)
2008 fprintf(fout, "%s", cont->aligns[(i) % cont->ncolumns] == 'r' ? ">l" : "<l");
2014 fputs(",frame=\"none\",grid=\"none\"", fout);
2017 fputs(",frame=\"none\"", fout);
2020 fputs(",frame=\"all\",grid=\"all\"", fout);
2024 fputs("|====\n", fout);
2027 if (!opt_tuples_only)
2029 for (ptr = cont->headers; *ptr; ptr++)
2031 if (ptr != cont->headers)
2034 asciidoc_escaped_print(*ptr, fout);
2041 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2043 if (i % cont->ncolumns == 0)
2049 if (i % cont->ncolumns != 0)
2053 /* protect against needless spaces */
2054 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2056 if ((i + 1) % cont->ncolumns != 0)
2060 asciidoc_escaped_print(*ptr, fout);
2062 if ((i + 1) % cont->ncolumns == 0)
2066 fputs("|====\n", fout);
2068 if (cont->opt->stop_table)
2070 printTableFooter *footers = footers_with_default(cont);
2073 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
2075 printTableFooter *f;
2077 fputs("\n....\n", fout);
2078 for (f = footers; f; f = f->next)
2080 fputs(f->data, fout);
2083 fputs("....\n", fout);
2089 print_asciidoc_vertical(const printTableContent *cont, FILE *fout)
2091 bool opt_tuples_only = cont->opt->tuples_only;
2092 unsigned short opt_border = cont->opt->border;
2093 unsigned long record = cont->opt->prior_records + 1;
2095 const char *const * ptr;
2100 if (cont->opt->start_table)
2102 /* print table in new paragraph - enforce preliminary new line */
2106 if (!opt_tuples_only && cont->title)
2109 fputs(cont->title, fout);
2113 /* print table [] header definition */
2114 fputs("[cols=\"h,l\"", fout);
2118 fputs(",frame=\"none\",grid=\"none\"", fout);
2121 fputs(",frame=\"none\"", fout);
2124 fputs(",frame=\"all\",grid=\"all\"", fout);
2128 fputs("|====\n", fout);
2132 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2134 if (i % cont->ncolumns == 0)
2138 if (!opt_tuples_only)
2143 fputs("2+|\n", fout);
2147 asciidoc_escaped_print(cont->headers[i % cont->ncolumns], fout);
2149 fprintf(fout, " %s|", cont->aligns[i % cont->ncolumns] == 'r' ? ">l" : "<l");
2150 /* is string only whitespace? */
2151 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2154 asciidoc_escaped_print(*ptr, fout);
2158 fputs("|====\n", fout);
2160 if (cont->opt->stop_table)
2163 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
2165 printTableFooter *f;
2167 fputs("\n....\n", fout);
2168 for (f = cont->footers; f; f = f->next)
2170 fputs(f->data, fout);
2173 fputs("....\n", fout);
2178 /*************************/
2180 /*************************/
2184 latex_escaped_print(const char *in, FILE *fout)
2188 for (p = in; *p; p++)
2210 fputs("\\backslash", fout);
2213 fputs("\\\\", fout);
2222 print_latex_text(const printTableContent *cont, FILE *fout)
2224 bool opt_tuples_only = cont->opt->tuples_only;
2225 unsigned short opt_border = cont->opt->border;
2227 const char *const * ptr;
2235 if (cont->opt->start_table)
2238 if (!opt_tuples_only && cont->title)
2240 fputs("\\begin{center}\n", fout);
2241 latex_escaped_print(cont->title, fout);
2242 fputs("\n\\end{center}\n\n", fout);
2245 /* begin environment and set alignments and borders */
2246 fputs("\\begin{tabular}{", fout);
2248 if (opt_border >= 2)
2250 for (i = 0; i < cont->ncolumns; i++)
2252 fputc(*(cont->aligns + i), fout);
2253 if (opt_border != 0 && i < cont->ncolumns - 1)
2256 if (opt_border >= 2)
2261 if (!opt_tuples_only && opt_border >= 2)
2262 fputs("\\hline\n", fout);
2265 if (!opt_tuples_only)
2267 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2271 fputs("\\textit{", fout);
2272 latex_escaped_print(*ptr, fout);
2275 fputs(" \\\\\n", fout);
2276 fputs("\\hline\n", fout);
2281 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2283 latex_escaped_print(*ptr, fout);
2285 if ((i + 1) % cont->ncolumns == 0)
2287 fputs(" \\\\\n", fout);
2288 if (opt_border == 3)
2289 fputs("\\hline\n", fout);
2297 if (cont->opt->stop_table)
2299 printTableFooter *footers = footers_with_default(cont);
2301 if (opt_border == 2)
2302 fputs("\\hline\n", fout);
2304 fputs("\\end{tabular}\n\n\\noindent ", fout);
2307 if (footers && !opt_tuples_only && !cancel_pressed)
2309 printTableFooter *f;
2311 for (f = footers; f; f = f->next)
2313 latex_escaped_print(f->data, fout);
2314 fputs(" \\\\\n", fout);
2324 print_latex_longtable_text(const printTableContent *cont, FILE *fout)
2326 bool opt_tuples_only = cont->opt->tuples_only;
2327 unsigned short opt_border = cont->opt->border;
2329 const char *opt_table_attr = cont->opt->tableAttr;
2330 const char *next_opt_table_attr_char = opt_table_attr;
2331 const char *last_opt_table_attr_char = NULL;
2332 const char *const * ptr;
2340 if (cont->opt->start_table)
2342 /* begin environment and set alignments and borders */
2343 fputs("\\begin{longtable}{", fout);
2345 if (opt_border >= 2)
2348 for (i = 0; i < cont->ncolumns; i++)
2350 /* longtable supports either a width (p) or an alignment (l/r) */
2351 /* Are we left-justified and was a proportional width specified? */
2352 if (*(cont->aligns + i) == 'l' && opt_table_attr)
2354 #define LONGTABLE_WHITESPACE " \t\n"
2356 /* advance over whitespace */
2357 next_opt_table_attr_char += strspn(next_opt_table_attr_char,
2358 LONGTABLE_WHITESPACE);
2359 /* We have a value? */
2360 if (next_opt_table_attr_char[0] != '\0')
2363 fwrite(next_opt_table_attr_char, strcspn(next_opt_table_attr_char,
2364 LONGTABLE_WHITESPACE), 1, fout);
2365 last_opt_table_attr_char = next_opt_table_attr_char;
2366 next_opt_table_attr_char += strcspn(next_opt_table_attr_char,
2367 LONGTABLE_WHITESPACE);
2368 fputs("\\textwidth}", fout);
2370 /* use previous value */
2371 else if (last_opt_table_attr_char != NULL)
2374 fwrite(last_opt_table_attr_char, strcspn(last_opt_table_attr_char,
2375 LONGTABLE_WHITESPACE), 1, fout);
2376 fputs("\\textwidth}", fout);
2382 fputc(*(cont->aligns + i), fout);
2384 if (opt_border != 0 && i < cont->ncolumns - 1)
2388 if (opt_border >= 2)
2394 if (!opt_tuples_only)
2397 if (opt_border >= 2)
2398 fputs("\\toprule\n", fout);
2399 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2403 fputs("\\small\\textbf{\\textit{", fout);
2404 latex_escaped_print(*ptr, fout);
2407 fputs(" \\\\\n", fout);
2408 fputs("\\midrule\n\\endfirsthead\n", fout);
2410 /* secondary heads */
2411 if (opt_border >= 2)
2412 fputs("\\toprule\n", fout);
2413 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2417 fputs("\\small\\textbf{\\textit{", fout);
2418 latex_escaped_print(*ptr, fout);
2421 fputs(" \\\\\n", fout);
2422 /* If the line under the row already appeared, don't do another */
2423 if (opt_border != 3)
2424 fputs("\\midrule\n", fout);
2425 fputs("\\endhead\n", fout);
2427 /* table name, caption? */
2428 if (!opt_tuples_only && cont->title)
2430 /* Don't output if we are printing a line under each row */
2431 if (opt_border == 2)
2432 fputs("\\bottomrule\n", fout);
2433 fputs("\\caption[", fout);
2434 latex_escaped_print(cont->title, fout);
2435 fputs(" (Continued)]{", fout);
2436 latex_escaped_print(cont->title, fout);
2437 fputs("}\n\\endfoot\n", fout);
2438 if (opt_border == 2)
2439 fputs("\\bottomrule\n", fout);
2440 fputs("\\caption[", fout);
2441 latex_escaped_print(cont->title, fout);
2443 latex_escaped_print(cont->title, fout);
2444 fputs("}\n\\endlastfoot\n", fout);
2446 /* output bottom table line? */
2447 else if (opt_border >= 2)
2449 fputs("\\bottomrule\n\\endfoot\n", fout);
2450 fputs("\\bottomrule\n\\endlastfoot\n", fout);
2456 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2458 /* Add a line under each row? */
2459 if (i != 0 && i % cont->ncolumns != 0)
2460 fputs("\n&\n", fout);
2461 fputs("\\raggedright{", fout);
2462 latex_escaped_print(*ptr, fout);
2464 if ((i + 1) % cont->ncolumns == 0)
2466 fputs(" \\tabularnewline\n", fout);
2467 if (opt_border == 3)
2468 fputs(" \\hline\n", fout);
2474 if (cont->opt->stop_table)
2475 fputs("\\end{longtable}\n", fout);
2480 print_latex_vertical(const printTableContent *cont, FILE *fout)
2482 bool opt_tuples_only = cont->opt->tuples_only;
2483 unsigned short opt_border = cont->opt->border;
2484 unsigned long record = cont->opt->prior_records + 1;
2486 const char *const * ptr;
2494 if (cont->opt->start_table)
2497 if (!opt_tuples_only && cont->title)
2499 fputs("\\begin{center}\n", fout);
2500 latex_escaped_print(cont->title, fout);
2501 fputs("\n\\end{center}\n\n", fout);
2504 /* begin environment and set alignments and borders */
2505 fputs("\\begin{tabular}{", fout);
2506 if (opt_border == 0)
2508 else if (opt_border == 1)
2510 else if (opt_border == 2)
2511 fputs("|c|l|", fout);
2516 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2519 if (i % cont->ncolumns == 0)
2523 if (!opt_tuples_only)
2525 if (opt_border == 2)
2527 fputs("\\hline\n", fout);
2528 fprintf(fout, "\\multicolumn{2}{|c|}{\\textit{Record %lu}} \\\\\n", record++);
2531 fprintf(fout, "\\multicolumn{2}{c}{\\textit{Record %lu}} \\\\\n", record++);
2533 if (opt_border >= 1)
2534 fputs("\\hline\n", fout);
2537 latex_escaped_print(cont->headers[i % cont->ncolumns], fout);
2539 latex_escaped_print(*ptr, fout);
2540 fputs(" \\\\\n", fout);
2543 if (cont->opt->stop_table)
2545 if (opt_border == 2)
2546 fputs("\\hline\n", fout);
2548 fputs("\\end{tabular}\n\n\\noindent ", fout);
2551 if (cont->footers && !opt_tuples_only && !cancel_pressed)
2553 printTableFooter *f;
2555 for (f = cont->footers; f; f = f->next)
2557 latex_escaped_print(f->data, fout);
2558 fputs(" \\\\\n", fout);
2567 /*************************/
2569 /*************************/
2573 troff_ms_escaped_print(const char *in, FILE *fout)
2577 for (p = in; *p; p++)
2581 fputs("\\(rs", fout);
2590 print_troff_ms_text(const printTableContent *cont, FILE *fout)
2592 bool opt_tuples_only = cont->opt->tuples_only;
2593 unsigned short opt_border = cont->opt->border;
2595 const char *const * ptr;
2603 if (cont->opt->start_table)
2606 if (!opt_tuples_only && cont->title)
2608 fputs(".LP\n.DS C\n", fout);
2609 troff_ms_escaped_print(cont->title, fout);
2610 fputs("\n.DE\n", fout);
2613 /* begin environment and set alignments and borders */
2614 fputs(".LP\n.TS\n", fout);
2615 if (opt_border == 2)
2616 fputs("center box;\n", fout);
2618 fputs("center;\n", fout);
2620 for (i = 0; i < cont->ncolumns; i++)
2622 fputc(*(cont->aligns + i), fout);
2623 if (opt_border > 0 && i < cont->ncolumns - 1)
2629 if (!opt_tuples_only)
2631 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2635 fputs("\\fI", fout);
2636 troff_ms_escaped_print(*ptr, fout);
2637 fputs("\\fP", fout);
2639 fputs("\n_\n", fout);
2644 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2646 troff_ms_escaped_print(*ptr, fout);
2648 if ((i + 1) % cont->ncolumns == 0)
2658 if (cont->opt->stop_table)
2660 printTableFooter *footers = footers_with_default(cont);
2662 fputs(".TE\n.DS L\n", fout);
2665 if (footers && !opt_tuples_only && !cancel_pressed)
2667 printTableFooter *f;
2669 for (f = footers; f; f = f->next)
2671 troff_ms_escaped_print(f->data, fout);
2676 fputs(".DE\n", fout);
2682 print_troff_ms_vertical(const printTableContent *cont, FILE *fout)
2684 bool opt_tuples_only = cont->opt->tuples_only;
2685 unsigned short opt_border = cont->opt->border;
2686 unsigned long record = cont->opt->prior_records + 1;
2688 const char *const * ptr;
2689 unsigned short current_format = 0; /* 0=none, 1=header, 2=body */
2697 if (cont->opt->start_table)
2700 if (!opt_tuples_only && cont->title)
2702 fputs(".LP\n.DS C\n", fout);
2703 troff_ms_escaped_print(cont->title, fout);
2704 fputs("\n.DE\n", fout);
2707 /* begin environment and set alignments and borders */
2708 fputs(".LP\n.TS\n", fout);
2709 if (opt_border == 2)
2710 fputs("center box;\n", fout);
2712 fputs("center;\n", fout);
2715 if (opt_tuples_only)
2716 fputs("c l;\n", fout);
2719 current_format = 2; /* assume tuples printed already */
2722 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2725 if (i % cont->ncolumns == 0)
2729 if (!opt_tuples_only)
2731 if (current_format != 1)
2733 if (opt_border == 2 && record > 1)
2735 if (current_format != 0)
2736 fputs(".T&\n", fout);
2737 fputs("c s.\n", fout);
2740 fprintf(fout, "\\fIRecord %lu\\fP\n", record++);
2742 if (opt_border >= 1)
2746 if (!opt_tuples_only)
2748 if (current_format != 2)
2750 if (current_format != 0)
2751 fputs(".T&\n", fout);
2752 if (opt_border != 1)
2753 fputs("c l.\n", fout);
2755 fputs("c | l.\n", fout);
2760 troff_ms_escaped_print(cont->headers[i % cont->ncolumns], fout);
2762 troff_ms_escaped_print(*ptr, fout);
2767 if (cont->opt->stop_table)
2769 fputs(".TE\n.DS L\n", fout);
2772 if (cont->footers && !opt_tuples_only && !cancel_pressed)
2774 printTableFooter *f;
2776 for (f = cont->footers; f; f = f->next)
2778 troff_ms_escaped_print(f->data, fout);
2783 fputs(".DE\n", fout);
2788 /********************************/
2789 /* Public functions */
2790 /********************************/
2794 * disable_sigpipe_trap
2796 * Turn off SIGPIPE interrupt --- call this before writing to a temporary
2797 * query output file that is a pipe.
2799 * No-op on Windows, where there's no SIGPIPE interrupts.
2802 disable_sigpipe_trap(void)
2805 pqsignal(SIGPIPE, SIG_IGN);
2810 * restore_sigpipe_trap
2812 * Restore normal SIGPIPE interrupt --- call this when done writing to a
2813 * temporary query output file that was (or might have been) a pipe.
2815 * Note: within psql, we enable SIGPIPE interrupts unless the permanent query
2816 * output file is a pipe, in which case they should be kept off. This
2817 * approach works only because psql is not currently complicated enough to
2818 * have nested usages of short-lived output files. Otherwise we'd probably
2819 * need a genuine save-and-restore-state approach; but for now, that would be
2820 * useless complication. In non-psql programs, this always enables SIGPIPE.
2822 * No-op on Windows, where there's no SIGPIPE interrupts.
2825 restore_sigpipe_trap(void)
2828 pqsignal(SIGPIPE, always_ignore_sigpipe ? SIG_IGN : SIG_DFL);
2833 * set_sigpipe_trap_state
2835 * Set the trap state that restore_sigpipe_trap should restore to.
2838 set_sigpipe_trap_state(bool ignore)
2840 always_ignore_sigpipe = ignore;
2847 * Tests if pager is needed and returns appropriate FILE pointer.
2849 * If the topt argument is NULL no pager is used.
2852 PageOutput(int lines, const printTableOpt *topt)
2854 /* check whether we need / can / are supposed to use pager */
2855 if (topt && topt->pager && isatty(fileno(stdin)) && isatty(fileno(stdout)))
2858 unsigned short int pager = topt->pager;
2859 int min_lines = topt->pager_min_lines;
2861 struct winsize screen_size;
2863 result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
2865 /* >= accounts for a one-line prompt */
2867 || (lines >= screen_size.ws_row && lines >= min_lines)
2871 const char *pagerprog;
2874 pagerprog = getenv("PAGER");
2876 pagerprog = DEFAULT_PAGER;
2877 disable_sigpipe_trap();
2878 pagerpipe = popen(pagerprog, "w");
2890 * Close previously opened pager pipe, if any
2893 ClosePager(FILE *pagerpipe)
2895 if (pagerpipe && pagerpipe != stdout)
2898 * If printing was canceled midstream, warn about it.
2900 * Some pagers like less use Ctrl-C as part of their command set. Even
2901 * so, we abort our processing and warn the user what we did. If the
2902 * pager quit as a result of the SIGINT, this message won't go
2906 fprintf(pagerpipe, _("Interrupted\n"));
2909 restore_sigpipe_trap();
2914 * Initialise a table contents struct.
2915 * Must be called before any other printTable method is used.
2917 * The title is not duplicated; the caller must ensure that the buffer
2918 * is available for the lifetime of the printTableContent struct.
2920 * If you call this, you must call printTableCleanup once you're done with the
2924 printTableInit(printTableContent *const content, const printTableOpt *opt,
2925 const char *title, const int ncolumns, const int nrows)
2928 content->title = title;
2929 content->ncolumns = ncolumns;
2930 content->nrows = nrows;
2932 content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers));
2934 content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells));
2936 content->cellmustfree = NULL;
2937 content->footers = NULL;
2939 content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align));
2941 content->header = content->headers;
2942 content->cell = content->cells;
2943 content->footer = content->footers;
2944 content->align = content->aligns;
2945 content->cellsadded = 0;
2949 * Add a header to the table.
2951 * Headers are not duplicated; you must ensure that the header string is
2952 * available for the lifetime of the printTableContent struct.
2954 * If translate is true, the function will pass the header through gettext.
2955 * Otherwise, the header will not be translated.
2957 * align is either 'l' or 'r', and specifies the alignment for cells in this
2961 printTableAddHeader(printTableContent *const content, char *header,
2962 const bool translate, const char align)
2965 (void) translate; /* unused parameter */
2968 if (content->header >= content->headers + content->ncolumns)
2970 fprintf(stderr, _("Cannot add header to table content: "
2971 "column count of %d exceeded.\n"),
2976 *content->header = (char *) mbvalidate((unsigned char *) header,
2977 content->opt->encoding);
2980 *content->header = _(*content->header);
2984 *content->align = align;
2989 * Add a cell to the table.
2991 * Cells are not duplicated; you must ensure that the cell string is available
2992 * for the lifetime of the printTableContent struct.
2994 * If translate is true, the function will pass the cell through gettext.
2995 * Otherwise, the cell will not be translated.
2997 * If mustfree is true, the cell string is freed by printTableCleanup().
2998 * Note: Automatic freeing of translatable strings is not supported.
3001 printTableAddCell(printTableContent *const content, char *cell,
3002 const bool translate, const bool mustfree)
3005 (void) translate; /* unused parameter */
3008 if (content->cellsadded >= content->ncolumns * content->nrows)
3010 fprintf(stderr, _("Cannot add cell to table content: "
3011 "total cell count of %d exceeded.\n"),
3012 content->ncolumns * content->nrows);
3016 *content->cell = (char *) mbvalidate((unsigned char *) cell,
3017 content->opt->encoding);
3021 *content->cell = _(*content->cell);
3026 if (content->cellmustfree == NULL)
3027 content->cellmustfree =
3028 pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool));
3030 content->cellmustfree[content->cellsadded] = true;
3033 content->cellsadded++;
3037 * Add a footer to the table.
3039 * Footers are added as elements of a singly-linked list, and the content is
3040 * strdup'd, so there is no need to keep the original footer string around.
3042 * Footers are never translated by the function. If you want the footer
3043 * translated you must do so yourself, before calling printTableAddFooter. The
3044 * reason this works differently to headers and cells is that footers tend to
3045 * be made of up individually translated components, rather than being
3046 * translated as a whole.
3049 printTableAddFooter(printTableContent *const content, const char *footer)
3051 printTableFooter *f;
3053 f = pg_malloc0(sizeof(*f));
3054 f->data = pg_strdup(footer);
3056 if (content->footers == NULL)
3057 content->footers = f;
3059 content->footer->next = f;
3061 content->footer = f;
3065 * Change the content of the last-added footer.
3067 * The current contents of the last-added footer are freed, and replaced by the
3068 * content given in *footer. If there was no previous footer, add a new one.
3070 * The content is strdup'd, so there is no need to keep the original string
3074 printTableSetFooter(printTableContent *const content, const char *footer)
3076 if (content->footers != NULL)
3078 free(content->footer->data);
3079 content->footer->data = pg_strdup(footer);
3082 printTableAddFooter(content, footer);
3086 * Free all memory allocated to this struct.
3088 * Once this has been called, the struct is unusable unless you pass it to
3089 * printTableInit() again.
3092 printTableCleanup(printTableContent *const content)
3094 if (content->cellmustfree)
3098 for (i = 0; i < content->nrows * content->ncolumns; i++)
3100 if (content->cellmustfree[i])
3101 free((char *) content->cells[i]);
3103 free(content->cellmustfree);
3104 content->cellmustfree = NULL;
3106 free(content->headers);
3107 free(content->cells);
3108 free(content->aligns);
3110 content->opt = NULL;
3111 content->title = NULL;
3112 content->headers = NULL;
3113 content->cells = NULL;
3114 content->aligns = NULL;
3115 content->header = NULL;
3116 content->cell = NULL;
3117 content->align = NULL;
3119 if (content->footers)
3121 for (content->footer = content->footers; content->footer;)
3123 printTableFooter *f;
3125 f = content->footer;
3126 content->footer = f->next;
3131 content->footers = NULL;
3132 content->footer = NULL;
3138 * Setup pager if required
3141 IsPagerNeeded(const printTableContent *cont, int extra_lines, bool expanded,
3142 FILE **fout, bool *is_pager)
3144 if (*fout == stdout)
3149 lines = (cont->ncolumns + 1) * cont->nrows;
3151 lines = cont->nrows + 1;
3153 if (!cont->opt->tuples_only)
3155 printTableFooter *f;
3158 * FIXME -- this is slightly bogus: it counts the number of
3159 * footers, not the number of lines in them.
3161 for (f = cont->footers; f; f = f->next)
3165 *fout = PageOutput(lines + extra_lines, cont->opt);
3166 *is_pager = (*fout != stdout);
3173 * Use this to print any table in the supported formats.
3175 * cont: table data and formatting options
3176 * fout: where to print to
3177 * is_pager: true if caller has already redirected fout to be a pager pipe
3178 * flog: if not null, also print the table there (for --log-file option)
3181 printTable(const printTableContent *cont,
3182 FILE *fout, bool is_pager, FILE *flog)
3184 bool is_local_pager = false;
3189 if (cont->opt->format == PRINT_NOTHING)
3192 /* print_aligned_*() handle the pager themselves */
3194 cont->opt->format != PRINT_ALIGNED &&
3195 cont->opt->format != PRINT_WRAPPED)
3197 IsPagerNeeded(cont, 0, (cont->opt->expanded == 1), &fout, &is_pager);
3198 is_local_pager = is_pager;
3201 /* print the stuff */
3204 print_aligned_text(cont, flog, false);
3206 switch (cont->opt->format)
3208 case PRINT_UNALIGNED:
3209 if (cont->opt->expanded == 1)
3210 print_unaligned_vertical(cont, fout);
3212 print_unaligned_text(cont, fout);
3218 * In expanded-auto mode, force vertical if a pager is passed in;
3219 * else we may make different decisions for different hunks of the
3222 if (cont->opt->expanded == 1 ||
3223 (cont->opt->expanded == 2 && is_pager))
3224 print_aligned_vertical(cont, fout, is_pager);
3226 print_aligned_text(cont, fout, is_pager);
3229 if (cont->opt->expanded == 1)
3230 print_html_vertical(cont, fout);
3232 print_html_text(cont, fout);
3234 case PRINT_ASCIIDOC:
3235 if (cont->opt->expanded == 1)
3236 print_asciidoc_vertical(cont, fout);
3238 print_asciidoc_text(cont, fout);
3241 if (cont->opt->expanded == 1)
3242 print_latex_vertical(cont, fout);
3244 print_latex_text(cont, fout);
3246 case PRINT_LATEX_LONGTABLE:
3247 if (cont->opt->expanded == 1)
3248 print_latex_vertical(cont, fout);
3250 print_latex_longtable_text(cont, fout);
3252 case PRINT_TROFF_MS:
3253 if (cont->opt->expanded == 1)
3254 print_troff_ms_vertical(cont, fout);
3256 print_troff_ms_text(cont, fout);
3259 fprintf(stderr, _("invalid output format (internal error): %d"),
3269 * Use this to print query results
3271 * result: result of a successful query
3272 * opt: formatting options
3273 * fout: where to print to
3274 * is_pager: true if caller has already redirected fout to be a pager pipe
3275 * flog: if not null, also print the data there (for --log-file option)
3278 printQuery(const PGresult *result, const printQueryOpt *opt,
3279 FILE *fout, bool is_pager, FILE *flog)
3281 printTableContent cont;
3289 printTableInit(&cont, &opt->topt, opt->title,
3290 PQnfields(result), PQntuples(result));
3292 /* Assert caller supplied enough translate_columns[] entries */
3293 Assert(opt->translate_columns == NULL ||
3294 opt->n_translate_columns >= cont.ncolumns);
3296 for (i = 0; i < cont.ncolumns; i++)
3299 Oid ftype = PQftype(result, i);
3320 printTableAddHeader(&cont, PQfname(result, i),
3321 opt->translate_header, align);
3325 for (r = 0; r < cont.nrows; r++)
3327 for (c = 0; c < cont.ncolumns; c++)
3330 bool mustfree = false;
3333 if (PQgetisnull(result, r, c))
3334 cell = opt->nullPrint ? opt->nullPrint : "";
3337 cell = PQgetvalue(result, r, c);
3338 if (cont.aligns[c] == 'r' && opt->topt.numericLocale)
3340 cell = format_numeric_locale(cell);
3345 translate = (opt->translate_columns && opt->translate_columns[c]);
3346 printTableAddCell(&cont, cell, translate, mustfree);
3355 for (footer = opt->footers; *footer; footer++)
3356 printTableAddFooter(&cont, *footer);
3359 printTable(&cont, fout, is_pager, flog);
3360 printTableCleanup(&cont);
3365 setDecimalLocale(void)
3367 struct lconv *extlconv;
3369 extlconv = localeconv();
3371 /* Don't accept an empty decimal_point string */
3372 if (*extlconv->decimal_point)
3373 decimal_point = pg_strdup(extlconv->decimal_point);
3375 decimal_point = "."; /* SQL output standard */
3378 * Although the Open Group standard allows locales to supply more than one
3379 * group width, we consider only the first one, and we ignore any attempt
3380 * to suppress grouping by specifying CHAR_MAX. As in the backend's
3381 * cash.c, we must apply a range check to avoid being fooled by variant
3384 groupdigits = *extlconv->grouping;
3385 if (groupdigits <= 0 || groupdigits > 6)
3386 groupdigits = 3; /* most common */
3388 /* Don't accept an empty thousands_sep string, either */
3389 /* similar code exists in formatting.c */
3390 if (*extlconv->thousands_sep)
3391 thousands_sep = pg_strdup(extlconv->thousands_sep);
3392 /* Make sure thousands separator doesn't match decimal point symbol. */
3393 else if (strcmp(decimal_point, ",") != 0)
3394 thousands_sep = ",";
3396 thousands_sep = ".";
3399 /* get selected or default line style */
3400 const printTextFormat *
3401 get_line_style(const printTableOpt *opt)
3404 * Note: this function mainly exists to preserve the convention that a
3405 * printTableOpt struct can be initialized to zeroes to get default
3408 if (opt->line_style != NULL)
3409 return opt->line_style;
3411 return &pg_asciiformat;
3415 refresh_utf8format(const printTableOpt *opt)
3417 printTextFormat *popt = &pg_utf8format;
3419 const unicodeStyleBorderFormat *border;
3420 const unicodeStyleRowFormat *header;
3421 const unicodeStyleColumnFormat *column;
3423 popt->name = "unicode";
3425 border = &unicode_style.border_style[opt->unicode_border_linestyle];
3426 header = &unicode_style.row_style[opt->unicode_header_linestyle];
3427 column = &unicode_style.column_style[opt->unicode_column_linestyle];
3429 popt->lrule[PRINT_RULE_TOP].hrule = border->horizontal;
3430 popt->lrule[PRINT_RULE_TOP].leftvrule = border->down_and_right;
3431 popt->lrule[PRINT_RULE_TOP].midvrule = column->down_and_horizontal[opt->unicode_border_linestyle];
3432 popt->lrule[PRINT_RULE_TOP].rightvrule = border->down_and_left;
3434 popt->lrule[PRINT_RULE_MIDDLE].hrule = header->horizontal;
3435 popt->lrule[PRINT_RULE_MIDDLE].leftvrule = header->vertical_and_right[opt->unicode_border_linestyle];
3436 popt->lrule[PRINT_RULE_MIDDLE].midvrule = column->vertical_and_horizontal[opt->unicode_header_linestyle];
3437 popt->lrule[PRINT_RULE_MIDDLE].rightvrule = header->vertical_and_left[opt->unicode_border_linestyle];
3439 popt->lrule[PRINT_RULE_BOTTOM].hrule = border->horizontal;
3440 popt->lrule[PRINT_RULE_BOTTOM].leftvrule = border->up_and_right;
3441 popt->lrule[PRINT_RULE_BOTTOM].midvrule = column->up_and_horizontal[opt->unicode_border_linestyle];
3442 popt->lrule[PRINT_RULE_BOTTOM].rightvrule = border->left_and_right;
3445 popt->lrule[PRINT_RULE_DATA].hrule = "";
3446 popt->lrule[PRINT_RULE_DATA].leftvrule = border->vertical;
3447 popt->lrule[PRINT_RULE_DATA].midvrule = column->vertical;
3448 popt->lrule[PRINT_RULE_DATA].rightvrule = border->vertical;
3450 popt->midvrule_nl = column->vertical;
3451 popt->midvrule_wrap = column->vertical;
3452 popt->midvrule_blank = column->vertical;
3454 /* Same for all unicode today */
3455 popt->header_nl_left = unicode_style.header_nl_left;
3456 popt->header_nl_right = unicode_style.header_nl_right;
3457 popt->nl_left = unicode_style.nl_left;
3458 popt->nl_right = unicode_style.nl_right;
3459 popt->wrap_left = unicode_style.wrap_left;
3460 popt->wrap_right = unicode_style.wrap_right;
3461 popt->wrap_right_border = unicode_style.wrap_right_border;
3467 * Compute the byte distance to the end of the string or *target_width
3468 * display character positions, whichever comes first. Update *target_width
3469 * to be the number of display character positions actually filled.
3472 strlen_max_width(unsigned char *str, int *target_width, int encoding)
3474 unsigned char *start = str;
3475 unsigned char *end = str + strlen((char *) str);
3480 int char_width = PQdsplen((char *) str, encoding);
3483 * If the display width of the new character causes the string to
3484 * exceed its target width, skip it and return. However, if this is
3485 * the first character of the string (curr_width == 0), we have to
3488 if (*target_width < curr_width + char_width && curr_width != 0)
3491 curr_width += char_width;
3493 str += PQmblen((char *) str, encoding);
3496 *target_width = curr_width;