Consistently use "superuser" instead of "super user"
[pgsql.git] / src / fe_utils / print.c
blobd48fcc4a0328de76bd6b13d6313805e54eca118f
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-2021, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
14 * src/fe_utils/print.c
16 *-------------------------------------------------------------------------
18 #include "postgres_fe.h"
20 #include <limits.h>
21 #include <math.h>
22 #include <unistd.h>
24 #ifndef WIN32
25 #include <sys/ioctl.h> /* for ioctl() */
26 #endif
28 #ifdef HAVE_TERMIOS_H
29 #include <termios.h>
30 #endif
32 #include "catalog/pg_type_d.h"
33 #include "fe_utils/mbprint.h"
34 #include "fe_utils/print.h"
37 * If the calling program doesn't have any mechanism for setting
38 * cancel_pressed, it will have no effect.
40 * Note: print.c's general strategy for when to check cancel_pressed is to do
41 * so at completion of each row of output.
43 volatile sig_atomic_t cancel_pressed = false;
45 static bool always_ignore_sigpipe = false;
47 /* info for locale-aware numeric formatting; set up by setDecimalLocale() */
48 static char *decimal_point;
49 static int groupdigits;
50 static char *thousands_sep;
52 static char default_footer[100];
53 static printTableFooter default_footer_cell = {default_footer, NULL};
55 /* Line style control structures */
56 const printTextFormat pg_asciiformat =
58 "ascii",
60 {"-", "+", "+", "+"},
61 {"-", "+", "+", "+"},
62 {"-", "+", "+", "+"},
63 {"", "|", "|", "|"}
65 "|",
66 "|",
67 "|",
68 " ",
69 "+",
70 " ",
71 "+",
72 ".",
73 ".",
74 true
77 const printTextFormat pg_asciiformat_old =
79 "old-ascii",
81 {"-", "+", "+", "+"},
82 {"-", "+", "+", "+"},
83 {"-", "+", "+", "+"},
84 {"", "|", "|", "|"}
86 ":",
87 ";",
88 " ",
89 "+",
90 " ",
91 " ",
92 " ",
93 " ",
94 " ",
95 false
98 /* Default unicode linestyle format */
99 printTextFormat pg_utf8format;
101 typedef struct unicodeStyleRowFormat
103 const char *horizontal;
104 const char *vertical_and_right[2];
105 const char *vertical_and_left[2];
106 } unicodeStyleRowFormat;
108 typedef struct unicodeStyleColumnFormat
110 const char *vertical;
111 const char *vertical_and_horizontal[2];
112 const char *up_and_horizontal[2];
113 const char *down_and_horizontal[2];
114 } unicodeStyleColumnFormat;
116 typedef struct unicodeStyleBorderFormat
118 const char *up_and_right;
119 const char *vertical;
120 const char *down_and_right;
121 const char *horizontal;
122 const char *down_and_left;
123 const char *left_and_right;
124 } unicodeStyleBorderFormat;
126 typedef struct unicodeStyleFormat
128 unicodeStyleRowFormat row_style[2];
129 unicodeStyleColumnFormat column_style[2];
130 unicodeStyleBorderFormat border_style[2];
131 const char *header_nl_left;
132 const char *header_nl_right;
133 const char *nl_left;
134 const char *nl_right;
135 const char *wrap_left;
136 const char *wrap_right;
137 bool wrap_right_border;
138 } unicodeStyleFormat;
140 static const unicodeStyleFormat unicode_style = {
143 /* ─ */
144 "\342\224\200",
145 /* ├╟ */
146 {"\342\224\234", "\342\225\237"},
147 /* ┤╢ */
148 {"\342\224\244", "\342\225\242"},
151 /* ═ */
152 "\342\225\220",
153 /* ╞╠ */
154 {"\342\225\236", "\342\225\240"},
155 /* ╡╣ */
156 {"\342\225\241", "\342\225\243"},
161 /* │ */
162 "\342\224\202",
163 /* ┼╪ */
164 {"\342\224\274", "\342\225\252"},
165 /* ┴╧ */
166 {"\342\224\264", "\342\225\247"},
167 /* ┬╤ */
168 {"\342\224\254", "\342\225\244"},
171 /* ║ */
172 "\342\225\221",
173 /* ╫╬ */
174 {"\342\225\253", "\342\225\254"},
175 /* ╨╩ */
176 {"\342\225\250", "\342\225\251"},
177 /* ╥╦ */
178 {"\342\225\245", "\342\225\246"},
182 /* └│┌─┐┘ */
183 {"\342\224\224", "\342\224\202", "\342\224\214", "\342\224\200", "\342\224\220", "\342\224\230"},
184 /* ╚║╔═╗╝ */
185 {"\342\225\232", "\342\225\221", "\342\225\224", "\342\225\220", "\342\225\227", "\342\225\235"},
187 " ",
188 "\342\206\265", /* ↵ */
189 " ",
190 "\342\206\265", /* ↵ */
191 "\342\200\246", /* … */
192 "\342\200\246", /* … */
193 true
197 /* Local functions */
198 static int strlen_max_width(unsigned char *str, int *target_width, int encoding);
199 static void IsPagerNeeded(const printTableContent *cont, int extra_lines, bool expanded,
200 FILE **fout, bool *is_pager);
202 static void print_aligned_vertical(const printTableContent *cont,
203 FILE *fout, bool is_pager);
206 /* Count number of digits in integral part of number */
207 static int
208 integer_digits(const char *my_str)
210 /* ignoring any sign ... */
211 if (my_str[0] == '-' || my_str[0] == '+')
212 my_str++;
213 /* ... count initial integral digits */
214 return strspn(my_str, "0123456789");
217 /* Compute additional length required for locale-aware numeric output */
218 static int
219 additional_numeric_locale_len(const char *my_str)
221 int int_len = integer_digits(my_str),
222 len = 0;
224 /* Account for added thousands_sep instances */
225 if (int_len > groupdigits)
226 len += ((int_len - 1) / groupdigits) * strlen(thousands_sep);
228 /* Account for possible additional length of decimal_point */
229 if (strchr(my_str, '.') != NULL)
230 len += strlen(decimal_point) - 1;
232 return len;
236 * Format a numeric value per current LC_NUMERIC locale setting
238 * Returns the appropriately formatted string in a new allocated block,
239 * caller must free.
241 * setDecimalLocale() must have been called earlier.
243 static char *
244 format_numeric_locale(const char *my_str)
246 char *new_str;
247 int new_len,
248 int_len,
249 leading_digits,
251 new_str_pos;
254 * If the string doesn't look like a number, return it unchanged. This
255 * check is essential to avoid mangling already-localized "money" values.
257 if (strspn(my_str, "0123456789+-.eE") != strlen(my_str))
258 return pg_strdup(my_str);
260 new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
261 new_str = pg_malloc(new_len + 1);
262 new_str_pos = 0;
263 int_len = integer_digits(my_str);
265 /* number of digits in first thousands group */
266 leading_digits = int_len % groupdigits;
267 if (leading_digits == 0)
268 leading_digits = groupdigits;
270 /* process sign */
271 if (my_str[0] == '-' || my_str[0] == '+')
273 new_str[new_str_pos++] = my_str[0];
274 my_str++;
277 /* process integer part of number */
278 for (i = 0; i < int_len; i++)
280 /* Time to insert separator? */
281 if (i > 0 && --leading_digits == 0)
283 strcpy(&new_str[new_str_pos], thousands_sep);
284 new_str_pos += strlen(thousands_sep);
285 leading_digits = groupdigits;
287 new_str[new_str_pos++] = my_str[i];
290 /* handle decimal point if any */
291 if (my_str[i] == '.')
293 strcpy(&new_str[new_str_pos], decimal_point);
294 new_str_pos += strlen(decimal_point);
295 i++;
298 /* copy the rest (fractional digits and/or exponent, and \0 terminator) */
299 strcpy(&new_str[new_str_pos], &my_str[i]);
301 /* assert we didn't underestimate new_len (an overestimate is OK) */
302 Assert(strlen(new_str) <= new_len);
304 return new_str;
308 static void
309 print_separator(struct separator sep, FILE *fout)
311 if (sep.separator_zero)
312 fputc('\000', fout);
313 else if (sep.separator)
314 fputs(sep.separator, fout);
319 * Return the list of explicitly-requested footers or, when applicable, the
320 * default "(xx rows)" footer. Always omit the default footer when given
321 * non-default footers, "\pset footer off", or a specific instruction to that
322 * effect from a calling backslash command. Vertical formats number each row,
323 * making the default footer redundant; they do not call this function.
325 * The return value may point to static storage; do not keep it across calls.
327 static printTableFooter *
328 footers_with_default(const printTableContent *cont)
330 if (cont->footers == NULL && cont->opt->default_footer)
332 unsigned long total_records;
334 total_records = cont->opt->prior_records + cont->nrows;
335 snprintf(default_footer, sizeof(default_footer),
336 ngettext("(%lu row)", "(%lu rows)", total_records),
337 total_records);
339 return &default_footer_cell;
341 else
342 return cont->footers;
346 /*************************/
347 /* Unaligned text */
348 /*************************/
351 static void
352 print_unaligned_text(const printTableContent *cont, FILE *fout)
354 bool opt_tuples_only = cont->opt->tuples_only;
355 unsigned int i;
356 const char *const *ptr;
357 bool need_recordsep = false;
359 if (cancel_pressed)
360 return;
362 if (cont->opt->start_table)
364 /* print title */
365 if (!opt_tuples_only && cont->title)
367 fputs(cont->title, fout);
368 print_separator(cont->opt->recordSep, fout);
371 /* print headers */
372 if (!opt_tuples_only)
374 for (ptr = cont->headers; *ptr; ptr++)
376 if (ptr != cont->headers)
377 print_separator(cont->opt->fieldSep, fout);
378 fputs(*ptr, fout);
380 need_recordsep = true;
383 else
384 /* assume continuing printout */
385 need_recordsep = true;
387 /* print cells */
388 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
390 if (need_recordsep)
392 print_separator(cont->opt->recordSep, fout);
393 need_recordsep = false;
394 if (cancel_pressed)
395 break;
397 fputs(*ptr, fout);
399 if ((i + 1) % cont->ncolumns)
400 print_separator(cont->opt->fieldSep, fout);
401 else
402 need_recordsep = true;
405 /* print footers */
406 if (cont->opt->stop_table)
408 printTableFooter *footers = footers_with_default(cont);
410 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
412 printTableFooter *f;
414 for (f = footers; f; f = f->next)
416 if (need_recordsep)
418 print_separator(cont->opt->recordSep, fout);
419 need_recordsep = false;
421 fputs(f->data, fout);
422 need_recordsep = true;
427 * The last record is terminated by a newline, independent of the set
428 * record separator. But when the record separator is a zero byte, we
429 * use that (compatible with find -print0 and xargs).
431 if (need_recordsep)
433 if (cont->opt->recordSep.separator_zero)
434 print_separator(cont->opt->recordSep, fout);
435 else
436 fputc('\n', fout);
442 static void
443 print_unaligned_vertical(const printTableContent *cont, FILE *fout)
445 bool opt_tuples_only = cont->opt->tuples_only;
446 unsigned int i;
447 const char *const *ptr;
448 bool need_recordsep = false;
450 if (cancel_pressed)
451 return;
453 if (cont->opt->start_table)
455 /* print title */
456 if (!opt_tuples_only && cont->title)
458 fputs(cont->title, fout);
459 need_recordsep = true;
462 else
463 /* assume continuing printout */
464 need_recordsep = true;
466 /* print records */
467 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
469 if (need_recordsep)
471 /* record separator is 2 occurrences of recordsep in this mode */
472 print_separator(cont->opt->recordSep, fout);
473 print_separator(cont->opt->recordSep, fout);
474 need_recordsep = false;
475 if (cancel_pressed)
476 break;
479 fputs(cont->headers[i % cont->ncolumns], fout);
480 print_separator(cont->opt->fieldSep, fout);
481 fputs(*ptr, fout);
483 if ((i + 1) % cont->ncolumns)
484 print_separator(cont->opt->recordSep, fout);
485 else
486 need_recordsep = true;
489 if (cont->opt->stop_table)
491 /* print footers */
492 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
494 printTableFooter *f;
496 print_separator(cont->opt->recordSep, fout);
497 for (f = cont->footers; f; f = f->next)
499 print_separator(cont->opt->recordSep, fout);
500 fputs(f->data, fout);
504 /* see above in print_unaligned_text() */
505 if (need_recordsep)
507 if (cont->opt->recordSep.separator_zero)
508 print_separator(cont->opt->recordSep, fout);
509 else
510 fputc('\n', fout);
516 /********************/
517 /* Aligned text */
518 /********************/
521 /* draw "line" */
522 static void
523 _print_horizontal_line(const unsigned int ncolumns, const unsigned int *widths,
524 unsigned short border, printTextRule pos,
525 const printTextFormat *format,
526 FILE *fout)
528 const printTextLineFormat *lformat = &format->lrule[pos];
529 unsigned int i,
532 if (border == 1)
533 fputs(lformat->hrule, fout);
534 else if (border == 2)
535 fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
537 for (i = 0; i < ncolumns; i++)
539 for (j = 0; j < widths[i]; j++)
540 fputs(lformat->hrule, fout);
542 if (i < ncolumns - 1)
544 if (border == 0)
545 fputc(' ', fout);
546 else
547 fprintf(fout, "%s%s%s", lformat->hrule,
548 lformat->midvrule, lformat->hrule);
552 if (border == 2)
553 fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
554 else if (border == 1)
555 fputs(lformat->hrule, fout);
557 fputc('\n', fout);
562 * Print pretty boxes around cells.
564 static void
565 print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
567 bool opt_tuples_only = cont->opt->tuples_only;
568 int encoding = cont->opt->encoding;
569 unsigned short opt_border = cont->opt->border;
570 const printTextFormat *format = get_line_style(cont->opt);
571 const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
573 unsigned int col_count = 0,
574 cell_count = 0;
576 unsigned int i,
579 unsigned int *width_header,
580 *max_width,
581 *width_wrap,
582 *width_average;
583 unsigned int *max_nl_lines, /* value split by newlines */
584 *curr_nl_line,
585 *max_bytes;
586 unsigned char **format_buf;
587 unsigned int width_total;
588 unsigned int total_header_width;
589 unsigned int extra_row_output_lines = 0;
590 unsigned int extra_output_lines = 0;
592 const char *const *ptr;
594 struct lineptr **col_lineptrs; /* pointers to line pointer per column */
596 bool *header_done; /* Have all header lines been output? */
597 int *bytes_output; /* Bytes output for column value */
598 printTextLineWrap *wrap; /* Wrap status for each column */
599 int output_columns = 0; /* Width of interactive console */
600 bool is_local_pager = false;
602 if (cancel_pressed)
603 return;
605 if (opt_border > 2)
606 opt_border = 2;
608 if (cont->ncolumns > 0)
610 col_count = cont->ncolumns;
611 width_header = pg_malloc0(col_count * sizeof(*width_header));
612 width_average = pg_malloc0(col_count * sizeof(*width_average));
613 max_width = pg_malloc0(col_count * sizeof(*max_width));
614 width_wrap = pg_malloc0(col_count * sizeof(*width_wrap));
615 max_nl_lines = pg_malloc0(col_count * sizeof(*max_nl_lines));
616 curr_nl_line = pg_malloc0(col_count * sizeof(*curr_nl_line));
617 col_lineptrs = pg_malloc0(col_count * sizeof(*col_lineptrs));
618 max_bytes = pg_malloc0(col_count * sizeof(*max_bytes));
619 format_buf = pg_malloc0(col_count * sizeof(*format_buf));
620 header_done = pg_malloc0(col_count * sizeof(*header_done));
621 bytes_output = pg_malloc0(col_count * sizeof(*bytes_output));
622 wrap = pg_malloc0(col_count * sizeof(*wrap));
624 else
626 width_header = NULL;
627 width_average = NULL;
628 max_width = NULL;
629 width_wrap = NULL;
630 max_nl_lines = NULL;
631 curr_nl_line = NULL;
632 col_lineptrs = NULL;
633 max_bytes = NULL;
634 format_buf = NULL;
635 header_done = NULL;
636 bytes_output = NULL;
637 wrap = NULL;
640 /* scan all column headers, find maximum width and max max_nl_lines */
641 for (i = 0; i < col_count; i++)
643 int width,
644 nl_lines,
645 bytes_required;
647 pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
648 encoding, &width, &nl_lines, &bytes_required);
649 if (width > max_width[i])
650 max_width[i] = width;
651 if (nl_lines > max_nl_lines[i])
652 max_nl_lines[i] = nl_lines;
653 if (bytes_required > max_bytes[i])
654 max_bytes[i] = bytes_required;
655 if (nl_lines > extra_row_output_lines)
656 extra_row_output_lines = nl_lines;
658 width_header[i] = width;
660 /* Add height of tallest header column */
661 extra_output_lines += extra_row_output_lines;
662 extra_row_output_lines = 0;
664 /* scan all cells, find maximum width, compute cell_count */
665 for (i = 0, ptr = cont->cells; *ptr; ptr++, i++, cell_count++)
667 int width,
668 nl_lines,
669 bytes_required;
671 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
672 &width, &nl_lines, &bytes_required);
674 if (width > max_width[i % col_count])
675 max_width[i % col_count] = width;
676 if (nl_lines > max_nl_lines[i % col_count])
677 max_nl_lines[i % col_count] = nl_lines;
678 if (bytes_required > max_bytes[i % col_count])
679 max_bytes[i % col_count] = bytes_required;
681 width_average[i % col_count] += width;
684 /* If we have rows, compute average */
685 if (col_count != 0 && cell_count != 0)
687 int rows = cell_count / col_count;
689 for (i = 0; i < col_count; i++)
690 width_average[i] /= rows;
693 /* adjust the total display width based on border style */
694 if (opt_border == 0)
695 width_total = col_count;
696 else if (opt_border == 1)
697 width_total = col_count * 3 - ((col_count > 0) ? 1 : 0);
698 else
699 width_total = col_count * 3 + 1;
700 total_header_width = width_total;
702 for (i = 0; i < col_count; i++)
704 width_total += max_width[i];
705 total_header_width += width_header[i];
709 * At this point: max_width[] contains the max width of each column,
710 * max_nl_lines[] contains the max number of lines in each column,
711 * max_bytes[] contains the maximum storage space for formatting strings,
712 * width_total contains the giant width sum. Now we allocate some memory
713 * for line pointers.
715 for (i = 0; i < col_count; i++)
717 /* Add entry for ptr == NULL array termination */
718 col_lineptrs[i] = pg_malloc0((max_nl_lines[i] + 1) *
719 sizeof(**col_lineptrs));
721 format_buf[i] = pg_malloc(max_bytes[i] + 1);
723 col_lineptrs[i]->ptr = format_buf[i];
726 /* Default word wrap to the full width, i.e. no word wrap */
727 for (i = 0; i < col_count; i++)
728 width_wrap[i] = max_width[i];
731 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
733 if (cont->opt->columns > 0)
734 output_columns = cont->opt->columns;
735 else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
737 if (cont->opt->env_columns > 0)
738 output_columns = cont->opt->env_columns;
739 #ifdef TIOCGWINSZ
740 else
742 struct winsize screen_size;
744 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
745 output_columns = screen_size.ws_col;
747 #endif
750 if (cont->opt->format == PRINT_WRAPPED)
753 * Optional optimized word wrap. Shrink columns with a high max/avg
754 * ratio. Slightly bias against wider columns. (Increases chance a
755 * narrow column will fit in its cell.) If available columns is
756 * positive... and greater than the width of the unshrinkable column
757 * headers
759 if (output_columns > 0 && output_columns >= total_header_width)
761 /* While there is still excess width... */
762 while (width_total > output_columns)
764 double max_ratio = 0;
765 int worst_col = -1;
768 * Find column that has the highest ratio of its maximum width
769 * compared to its average width. This tells us which column
770 * will produce the fewest wrapped values if shortened.
771 * width_wrap starts as equal to max_width.
773 for (i = 0; i < col_count; i++)
775 if (width_average[i] && width_wrap[i] > width_header[i])
777 /* Penalize wide columns by 1% of their width */
778 double ratio;
780 ratio = (double) width_wrap[i] / width_average[i] +
781 max_width[i] * 0.01;
782 if (ratio > max_ratio)
784 max_ratio = ratio;
785 worst_col = i;
790 /* Exit loop if we can't squeeze any more. */
791 if (worst_col == -1)
792 break;
794 /* Decrease width of target column by one. */
795 width_wrap[worst_col]--;
796 width_total--;
802 * If in expanded auto mode, we have now calculated the expected width, so
803 * we can now escape to vertical mode if necessary. If the output has
804 * only one column, the expanded format would be wider than the regular
805 * format, so don't use it in that case.
807 if (cont->opt->expanded == 2 && output_columns > 0 && cont->ncolumns > 1 &&
808 (output_columns < total_header_width || output_columns < width_total))
810 print_aligned_vertical(cont, fout, is_pager);
811 goto cleanup;
814 /* If we wrapped beyond the display width, use the pager */
815 if (!is_pager && fout == stdout && output_columns > 0 &&
816 (output_columns < total_header_width || output_columns < width_total))
818 fout = PageOutput(INT_MAX, cont->opt); /* force pager */
819 is_pager = is_local_pager = true;
822 /* Check if newlines or our wrapping now need the pager */
823 if (!is_pager && fout == stdout)
825 /* scan all cells, find maximum width, compute cell_count */
826 for (i = 0, ptr = cont->cells; *ptr; ptr++, cell_count++)
828 int width,
829 nl_lines,
830 bytes_required;
832 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
833 &width, &nl_lines, &bytes_required);
836 * A row can have both wrapping and newlines that cause it to
837 * display across multiple lines. We check for both cases below.
839 if (width > 0 && width_wrap[i])
841 unsigned int extra_lines;
843 /* don't count the first line of nl_lines - it's not "extra" */
844 extra_lines = ((width - 1) / width_wrap[i]) + nl_lines - 1;
845 if (extra_lines > extra_row_output_lines)
846 extra_row_output_lines = extra_lines;
849 /* i is the current column number: increment with wrap */
850 if (++i >= col_count)
852 i = 0;
853 /* At last column of each row, add tallest column height */
854 extra_output_lines += extra_row_output_lines;
855 extra_row_output_lines = 0;
858 IsPagerNeeded(cont, extra_output_lines, false, &fout, &is_pager);
859 is_local_pager = is_pager;
862 /* time to output */
863 if (cont->opt->start_table)
865 /* print title */
866 if (cont->title && !opt_tuples_only)
868 int width,
869 height;
871 pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
872 encoding, &width, &height, NULL);
873 if (width >= width_total)
874 /* Aligned */
875 fprintf(fout, "%s\n", cont->title);
876 else
877 /* Centered */
878 fprintf(fout, "%-*s%s\n", (width_total - width) / 2, "",
879 cont->title);
882 /* print headers */
883 if (!opt_tuples_only)
885 int more_col_wrapping;
886 int curr_nl_line;
888 if (opt_border == 2)
889 _print_horizontal_line(col_count, width_wrap, opt_border,
890 PRINT_RULE_TOP, format, fout);
892 for (i = 0; i < col_count; i++)
893 pg_wcsformat((const unsigned char *) cont->headers[i],
894 strlen(cont->headers[i]), encoding,
895 col_lineptrs[i], max_nl_lines[i]);
897 more_col_wrapping = col_count;
898 curr_nl_line = 0;
899 memset(header_done, false, col_count * sizeof(bool));
900 while (more_col_wrapping)
902 if (opt_border == 2)
903 fputs(dformat->leftvrule, fout);
905 for (i = 0; i < cont->ncolumns; i++)
907 struct lineptr *this_line = col_lineptrs[i] + curr_nl_line;
908 unsigned int nbspace;
910 if (opt_border != 0 ||
911 (!format->wrap_right_border && i > 0))
912 fputs(curr_nl_line ? format->header_nl_left : " ",
913 fout);
915 if (!header_done[i])
917 nbspace = width_wrap[i] - this_line->width;
919 /* centered */
920 fprintf(fout, "%-*s%s%-*s",
921 nbspace / 2, "", this_line->ptr, (nbspace + 1) / 2, "");
923 if (!(this_line + 1)->ptr)
925 more_col_wrapping--;
926 header_done[i] = 1;
929 else
930 fprintf(fout, "%*s", width_wrap[i], "");
932 if (opt_border != 0 || format->wrap_right_border)
933 fputs(!header_done[i] ? format->header_nl_right : " ",
934 fout);
936 if (opt_border != 0 && col_count > 0 && i < col_count - 1)
937 fputs(dformat->midvrule, fout);
939 curr_nl_line++;
941 if (opt_border == 2)
942 fputs(dformat->rightvrule, fout);
943 fputc('\n', fout);
946 _print_horizontal_line(col_count, width_wrap, opt_border,
947 PRINT_RULE_MIDDLE, format, fout);
951 /* print cells, one loop per row */
952 for (i = 0, ptr = cont->cells; *ptr; i += col_count, ptr += col_count)
954 bool more_lines;
956 if (cancel_pressed)
957 break;
960 * Format each cell.
962 for (j = 0; j < col_count; j++)
964 pg_wcsformat((const unsigned char *) ptr[j], strlen(ptr[j]), encoding,
965 col_lineptrs[j], max_nl_lines[j]);
966 curr_nl_line[j] = 0;
969 memset(bytes_output, 0, col_count * sizeof(int));
972 * Each time through this loop, one display line is output. It can
973 * either be a full value or a partial value if embedded newlines
974 * exist or if 'format=wrapping' mode is enabled.
978 more_lines = false;
980 /* left border */
981 if (opt_border == 2)
982 fputs(dformat->leftvrule, fout);
984 /* for each column */
985 for (j = 0; j < col_count; j++)
987 /* We have a valid array element, so index it */
988 struct lineptr *this_line = &col_lineptrs[j][curr_nl_line[j]];
989 int bytes_to_output;
990 int chars_to_output = width_wrap[j];
991 bool finalspaces = (opt_border == 2 ||
992 (col_count > 0 && j < col_count - 1));
994 /* Print left-hand wrap or newline mark */
995 if (opt_border != 0)
997 if (wrap[j] == PRINT_LINE_WRAP_WRAP)
998 fputs(format->wrap_left, fout);
999 else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1000 fputs(format->nl_left, fout);
1001 else
1002 fputc(' ', fout);
1005 if (!this_line->ptr)
1007 /* Past newline lines so just pad for other columns */
1008 if (finalspaces)
1009 fprintf(fout, "%*s", chars_to_output, "");
1011 else
1013 /* Get strlen() of the characters up to width_wrap */
1014 bytes_to_output =
1015 strlen_max_width(this_line->ptr + bytes_output[j],
1016 &chars_to_output, encoding);
1019 * If we exceeded width_wrap, it means the display width
1020 * of a single character was wider than our target width.
1021 * In that case, we have to pretend we are only printing
1022 * the target display width and make the best of it.
1024 if (chars_to_output > width_wrap[j])
1025 chars_to_output = width_wrap[j];
1027 if (cont->aligns[j] == 'r') /* Right aligned cell */
1029 /* spaces first */
1030 fprintf(fout, "%*s", width_wrap[j] - chars_to_output, "");
1031 fwrite((char *) (this_line->ptr + bytes_output[j]),
1032 1, bytes_to_output, fout);
1034 else /* Left aligned cell */
1036 /* spaces second */
1037 fwrite((char *) (this_line->ptr + bytes_output[j]),
1038 1, bytes_to_output, fout);
1041 bytes_output[j] += bytes_to_output;
1043 /* Do we have more text to wrap? */
1044 if (*(this_line->ptr + bytes_output[j]) != '\0')
1045 more_lines = true;
1046 else
1048 /* Advance to next newline line */
1049 curr_nl_line[j]++;
1050 if (col_lineptrs[j][curr_nl_line[j]].ptr != NULL)
1051 more_lines = true;
1052 bytes_output[j] = 0;
1056 /* Determine next line's wrap status for this column */
1057 wrap[j] = PRINT_LINE_WRAP_NONE;
1058 if (col_lineptrs[j][curr_nl_line[j]].ptr != NULL)
1060 if (bytes_output[j] != 0)
1061 wrap[j] = PRINT_LINE_WRAP_WRAP;
1062 else if (curr_nl_line[j] != 0)
1063 wrap[j] = PRINT_LINE_WRAP_NEWLINE;
1067 * If left-aligned, pad out remaining space if needed (not
1068 * last column, and/or wrap marks required).
1070 if (cont->aligns[j] != 'r') /* Left aligned cell */
1072 if (finalspaces ||
1073 wrap[j] == PRINT_LINE_WRAP_WRAP ||
1074 wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1075 fprintf(fout, "%*s",
1076 width_wrap[j] - chars_to_output, "");
1079 /* Print right-hand wrap or newline mark */
1080 if (wrap[j] == PRINT_LINE_WRAP_WRAP)
1081 fputs(format->wrap_right, fout);
1082 else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1083 fputs(format->nl_right, fout);
1084 else if (opt_border == 2 || (col_count > 0 && j < col_count - 1))
1085 fputc(' ', fout);
1087 /* Print column divider, if not the last column */
1088 if (opt_border != 0 && (col_count > 0 && j < col_count - 1))
1090 if (wrap[j + 1] == PRINT_LINE_WRAP_WRAP)
1091 fputs(format->midvrule_wrap, fout);
1092 else if (wrap[j + 1] == PRINT_LINE_WRAP_NEWLINE)
1093 fputs(format->midvrule_nl, fout);
1094 else if (col_lineptrs[j + 1][curr_nl_line[j + 1]].ptr == NULL)
1095 fputs(format->midvrule_blank, fout);
1096 else
1097 fputs(dformat->midvrule, fout);
1101 /* end-of-row border */
1102 if (opt_border == 2)
1103 fputs(dformat->rightvrule, fout);
1104 fputc('\n', fout);
1106 } while (more_lines);
1109 if (cont->opt->stop_table)
1111 printTableFooter *footers = footers_with_default(cont);
1113 if (opt_border == 2 && !cancel_pressed)
1114 _print_horizontal_line(col_count, width_wrap, opt_border,
1115 PRINT_RULE_BOTTOM, format, fout);
1117 /* print footers */
1118 if (footers && !opt_tuples_only && !cancel_pressed)
1120 printTableFooter *f;
1122 for (f = footers; f; f = f->next)
1123 fprintf(fout, "%s\n", f->data);
1126 fputc('\n', fout);
1129 cleanup:
1130 /* clean up */
1131 for (i = 0; i < col_count; i++)
1133 free(col_lineptrs[i]);
1134 free(format_buf[i]);
1136 free(width_header);
1137 free(width_average);
1138 free(max_width);
1139 free(width_wrap);
1140 free(max_nl_lines);
1141 free(curr_nl_line);
1142 free(col_lineptrs);
1143 free(max_bytes);
1144 free(format_buf);
1145 free(header_done);
1146 free(bytes_output);
1147 free(wrap);
1149 if (is_local_pager)
1150 ClosePager(fout);
1154 static void
1155 print_aligned_vertical_line(const printTextFormat *format,
1156 const unsigned short opt_border,
1157 unsigned long record,
1158 unsigned int hwidth,
1159 unsigned int dwidth,
1160 printTextRule pos,
1161 FILE *fout)
1163 const printTextLineFormat *lformat = &format->lrule[pos];
1164 unsigned int i;
1165 int reclen = 0;
1167 if (opt_border == 2)
1168 fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
1169 else if (opt_border == 1)
1170 fputs(lformat->hrule, fout);
1172 if (record)
1174 if (opt_border == 0)
1175 reclen = fprintf(fout, "* Record %lu", record);
1176 else
1177 reclen = fprintf(fout, "[ RECORD %lu ]", record);
1179 if (opt_border != 2)
1180 reclen++;
1181 if (reclen < 0)
1182 reclen = 0;
1183 for (i = reclen; i < hwidth; i++)
1184 fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1185 reclen -= hwidth;
1187 if (opt_border > 0)
1189 if (reclen-- <= 0)
1190 fputs(lformat->hrule, fout);
1191 if (reclen-- <= 0)
1192 fputs(lformat->midvrule, fout);
1193 if (reclen-- <= 0)
1194 fputs(lformat->hrule, fout);
1196 else
1198 if (reclen-- <= 0)
1199 fputc(' ', fout);
1201 if (reclen < 0)
1202 reclen = 0;
1203 for (i = reclen; i < dwidth; i++)
1204 fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1205 if (opt_border == 2)
1206 fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
1207 fputc('\n', fout);
1210 static void
1211 print_aligned_vertical(const printTableContent *cont,
1212 FILE *fout, bool is_pager)
1214 bool opt_tuples_only = cont->opt->tuples_only;
1215 unsigned short opt_border = cont->opt->border;
1216 const printTextFormat *format = get_line_style(cont->opt);
1217 const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
1218 int encoding = cont->opt->encoding;
1219 unsigned long record = cont->opt->prior_records + 1;
1220 const char *const *ptr;
1221 unsigned int i,
1222 hwidth = 0,
1223 dwidth = 0,
1224 hheight = 1,
1225 dheight = 1,
1226 hformatsize = 0,
1227 dformatsize = 0;
1228 struct lineptr *hlineptr,
1229 *dlineptr;
1230 bool is_local_pager = false,
1231 hmultiline = false,
1232 dmultiline = false;
1233 int output_columns = 0; /* Width of interactive console */
1235 if (cancel_pressed)
1236 return;
1238 if (opt_border > 2)
1239 opt_border = 2;
1241 if (cont->cells[0] == NULL && cont->opt->start_table &&
1242 cont->opt->stop_table)
1244 printTableFooter *footers = footers_with_default(cont);
1246 if (!opt_tuples_only && !cancel_pressed && footers)
1248 printTableFooter *f;
1250 for (f = footers; f; f = f->next)
1251 fprintf(fout, "%s\n", f->data);
1254 fputc('\n', fout);
1256 return;
1260 * Deal with the pager here instead of in printTable(), because we could
1261 * get here via print_aligned_text() in expanded auto mode, and so we have
1262 * to recalculate the pager requirement based on vertical output.
1264 if (!is_pager)
1266 IsPagerNeeded(cont, 0, true, &fout, &is_pager);
1267 is_local_pager = is_pager;
1270 /* Find the maximum dimensions for the headers */
1271 for (i = 0; i < cont->ncolumns; i++)
1273 int width,
1274 height,
1277 pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
1278 encoding, &width, &height, &fs);
1279 if (width > hwidth)
1280 hwidth = width;
1281 if (height > hheight)
1283 hheight = height;
1284 hmultiline = true;
1286 if (fs > hformatsize)
1287 hformatsize = fs;
1290 /* find longest data cell */
1291 for (i = 0, ptr = cont->cells; *ptr; ptr++, i++)
1293 int width,
1294 height,
1297 pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
1298 &width, &height, &fs);
1299 if (width > dwidth)
1300 dwidth = width;
1301 if (height > dheight)
1303 dheight = height;
1304 dmultiline = true;
1306 if (fs > dformatsize)
1307 dformatsize = fs;
1311 * We now have all the information we need to setup the formatting
1312 * structures
1314 dlineptr = pg_malloc((sizeof(*dlineptr)) * (dheight + 1));
1315 hlineptr = pg_malloc((sizeof(*hlineptr)) * (hheight + 1));
1317 dlineptr->ptr = pg_malloc(dformatsize);
1318 hlineptr->ptr = pg_malloc(hformatsize);
1320 if (cont->opt->start_table)
1322 /* print title */
1323 if (!opt_tuples_only && cont->title)
1324 fprintf(fout, "%s\n", cont->title);
1328 * Choose target output width: \pset columns, or $COLUMNS, or ioctl
1330 if (cont->opt->columns > 0)
1331 output_columns = cont->opt->columns;
1332 else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
1334 if (cont->opt->env_columns > 0)
1335 output_columns = cont->opt->env_columns;
1336 #ifdef TIOCGWINSZ
1337 else
1339 struct winsize screen_size;
1341 if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
1342 output_columns = screen_size.ws_col;
1344 #endif
1348 * Calculate available width for data in wrapped mode
1350 if (cont->opt->format == PRINT_WRAPPED)
1352 unsigned int swidth,
1353 rwidth = 0,
1354 newdwidth;
1356 if (opt_border == 0)
1359 * For border = 0, one space in the middle. (If we discover we
1360 * need to wrap, the spacer column will be replaced by a wrap
1361 * marker, and we'll make room below for another wrap marker at
1362 * the end of the line. But for now, assume no wrap is needed.)
1364 swidth = 1;
1366 /* We might need a column for header newline markers, too */
1367 if (hmultiline)
1368 swidth++;
1370 else if (opt_border == 1)
1373 * For border = 1, two spaces and a vrule in the middle. (As
1374 * above, we might need one more column for a wrap marker.)
1376 swidth = 3;
1378 /* We might need a column for left header newline markers, too */
1379 if (hmultiline && (format == &pg_asciiformat_old))
1380 swidth++;
1382 else
1385 * For border = 2, two more for the vrules at the beginning and
1386 * end of the lines, plus spacer columns adjacent to these. (We
1387 * won't need extra columns for wrap/newline markers, we'll just
1388 * repurpose the spacers.)
1390 swidth = 7;
1393 /* Reserve a column for data newline indicators, too, if needed */
1394 if (dmultiline &&
1395 opt_border < 2 && format != &pg_asciiformat_old)
1396 swidth++;
1398 /* Determine width required for record header lines */
1399 if (!opt_tuples_only)
1401 if (cont->nrows > 0)
1402 rwidth = 1 + (int) log10(cont->nrows);
1403 if (opt_border == 0)
1404 rwidth += 9; /* "* RECORD " */
1405 else if (opt_border == 1)
1406 rwidth += 12; /* "-[ RECORD ]" */
1407 else
1408 rwidth += 15; /* "+-[ RECORD ]-+" */
1411 /* We might need to do the rest of the calculation twice */
1412 for (;;)
1414 unsigned int width;
1416 /* Total width required to not wrap data */
1417 width = hwidth + swidth + dwidth;
1418 /* ... and not the header lines, either */
1419 if (width < rwidth)
1420 width = rwidth;
1422 if (output_columns > 0)
1424 unsigned int min_width;
1426 /* Minimum acceptable width: room for just 3 columns of data */
1427 min_width = hwidth + swidth + 3;
1428 /* ... but not less than what the record header lines need */
1429 if (min_width < rwidth)
1430 min_width = rwidth;
1432 if (output_columns >= width)
1434 /* Plenty of room, use native data width */
1435 /* (but at least enough for the record header lines) */
1436 newdwidth = width - hwidth - swidth;
1438 else if (output_columns < min_width)
1440 /* Set data width to match min_width */
1441 newdwidth = min_width - hwidth - swidth;
1443 else
1445 /* Set data width to match output_columns */
1446 newdwidth = output_columns - hwidth - swidth;
1449 else
1451 /* Don't know the wrap limit, so use native data width */
1452 /* (but at least enough for the record header lines) */
1453 newdwidth = width - hwidth - swidth;
1457 * If we will need to wrap data and didn't already allocate a data
1458 * newline/wrap marker column, do so and recompute.
1460 if (newdwidth < dwidth && !dmultiline &&
1461 opt_border < 2 && format != &pg_asciiformat_old)
1463 dmultiline = true;
1464 swidth++;
1466 else
1467 break;
1470 dwidth = newdwidth;
1473 /* print records */
1474 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1476 printTextRule pos;
1477 int dline,
1478 hline,
1479 dcomplete,
1480 hcomplete,
1481 offset,
1482 chars_to_output;
1484 if (cancel_pressed)
1485 break;
1487 if (i == 0)
1488 pos = PRINT_RULE_TOP;
1489 else
1490 pos = PRINT_RULE_MIDDLE;
1492 /* Print record header (e.g. "[ RECORD N ]") above each record */
1493 if (i % cont->ncolumns == 0)
1495 unsigned int lhwidth = hwidth;
1497 if ((opt_border < 2) &&
1498 (hmultiline) &&
1499 (format == &pg_asciiformat_old))
1500 lhwidth++; /* for newline indicators */
1502 if (!opt_tuples_only)
1503 print_aligned_vertical_line(format, opt_border, record++,
1504 lhwidth, dwidth, pos, fout);
1505 else if (i != 0 || !cont->opt->start_table || opt_border == 2)
1506 print_aligned_vertical_line(format, opt_border, 0, lhwidth,
1507 dwidth, pos, fout);
1510 /* Format the header */
1511 pg_wcsformat((const unsigned char *) cont->headers[i % cont->ncolumns],
1512 strlen(cont->headers[i % cont->ncolumns]),
1513 encoding, hlineptr, hheight);
1514 /* Format the data */
1515 pg_wcsformat((const unsigned char *) *ptr, strlen(*ptr), encoding,
1516 dlineptr, dheight);
1519 * Loop through header and data in parallel dealing with newlines and
1520 * wrapped lines until they're both exhausted
1522 dline = hline = 0;
1523 dcomplete = hcomplete = 0;
1524 offset = 0;
1525 chars_to_output = dlineptr[dline].width;
1526 while (!dcomplete || !hcomplete)
1528 /* Left border */
1529 if (opt_border == 2)
1530 fprintf(fout, "%s", dformat->leftvrule);
1532 /* Header (never wrapped so just need to deal with newlines) */
1533 if (!hcomplete)
1535 int swidth = hwidth,
1536 target_width = hwidth;
1539 * Left spacer or new line indicator
1541 if ((opt_border == 2) ||
1542 (hmultiline && (format == &pg_asciiformat_old)))
1543 fputs(hline ? format->header_nl_left : " ", fout);
1546 * Header text
1548 strlen_max_width(hlineptr[hline].ptr, &target_width,
1549 encoding);
1550 fprintf(fout, "%-s", hlineptr[hline].ptr);
1553 * Spacer
1555 swidth -= target_width;
1556 if (swidth > 0)
1557 fprintf(fout, "%*s", swidth, " ");
1560 * New line indicator or separator's space
1562 if (hlineptr[hline + 1].ptr)
1564 /* More lines after this one due to a newline */
1565 if ((opt_border > 0) ||
1566 (hmultiline && (format != &pg_asciiformat_old)))
1567 fputs(format->header_nl_right, fout);
1568 hline++;
1570 else
1572 /* This was the last line of the header */
1573 if ((opt_border > 0) ||
1574 (hmultiline && (format != &pg_asciiformat_old)))
1575 fputs(" ", fout);
1576 hcomplete = 1;
1579 else
1581 unsigned int swidth = hwidth + opt_border;
1583 if ((opt_border < 2) &&
1584 (hmultiline) &&
1585 (format == &pg_asciiformat_old))
1586 swidth++;
1588 if ((opt_border == 0) &&
1589 (format != &pg_asciiformat_old) &&
1590 (hmultiline))
1591 swidth++;
1593 fprintf(fout, "%*s", swidth, " ");
1596 /* Separator */
1597 if (opt_border > 0)
1599 if (offset)
1600 fputs(format->midvrule_wrap, fout);
1601 else if (dline == 0)
1602 fputs(dformat->midvrule, fout);
1603 else
1604 fputs(format->midvrule_nl, fout);
1607 /* Data */
1608 if (!dcomplete)
1610 int target_width = dwidth,
1611 bytes_to_output,
1612 swidth = dwidth;
1615 * Left spacer or wrap indicator
1617 fputs(offset == 0 ? " " : format->wrap_left, fout);
1620 * Data text
1622 bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
1623 &target_width, encoding);
1624 fwrite((char *) (dlineptr[dline].ptr + offset),
1625 1, bytes_to_output, fout);
1627 chars_to_output -= target_width;
1628 offset += bytes_to_output;
1630 /* Spacer */
1631 swidth -= target_width;
1633 if (chars_to_output)
1635 /* continuing a wrapped column */
1636 if ((opt_border > 1) ||
1637 (dmultiline && (format != &pg_asciiformat_old)))
1639 if (swidth > 0)
1640 fprintf(fout, "%*s", swidth, " ");
1641 fputs(format->wrap_right, fout);
1644 else if (dlineptr[dline + 1].ptr)
1646 /* reached a newline in the column */
1647 if ((opt_border > 1) ||
1648 (dmultiline && (format != &pg_asciiformat_old)))
1650 if (swidth > 0)
1651 fprintf(fout, "%*s", swidth, " ");
1652 fputs(format->nl_right, fout);
1654 dline++;
1655 offset = 0;
1656 chars_to_output = dlineptr[dline].width;
1658 else
1660 /* reached the end of the cell */
1661 if (opt_border > 1)
1663 if (swidth > 0)
1664 fprintf(fout, "%*s", swidth, " ");
1665 fputs(" ", fout);
1667 dcomplete = 1;
1670 /* Right border */
1671 if (opt_border == 2)
1672 fputs(dformat->rightvrule, fout);
1674 fputs("\n", fout);
1676 else
1679 * data exhausted (this can occur if header is longer than the
1680 * data due to newlines in the header)
1682 if (opt_border < 2)
1683 fputs("\n", fout);
1684 else
1685 fprintf(fout, "%*s %s\n", dwidth, "", dformat->rightvrule);
1690 if (cont->opt->stop_table)
1692 if (opt_border == 2 && !cancel_pressed)
1693 print_aligned_vertical_line(format, opt_border, 0, hwidth, dwidth,
1694 PRINT_RULE_BOTTOM, fout);
1696 /* print footers */
1697 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
1699 printTableFooter *f;
1701 if (opt_border < 2)
1702 fputc('\n', fout);
1703 for (f = cont->footers; f; f = f->next)
1704 fprintf(fout, "%s\n", f->data);
1707 fputc('\n', fout);
1710 free(hlineptr->ptr);
1711 free(dlineptr->ptr);
1712 free(hlineptr);
1713 free(dlineptr);
1715 if (is_local_pager)
1716 ClosePager(fout);
1720 /**********************/
1721 /* CSV format */
1722 /**********************/
1725 static void
1726 csv_escaped_print(const char *str, FILE *fout)
1728 const char *p;
1730 fputc('"', fout);
1731 for (p = str; *p; p++)
1733 if (*p == '"')
1734 fputc('"', fout); /* double quotes are doubled */
1735 fputc(*p, fout);
1737 fputc('"', fout);
1740 static void
1741 csv_print_field(const char *str, FILE *fout, char sep)
1743 /*----------------
1744 * Enclose and escape field contents when one of these conditions is met:
1745 * - the field separator is found in the contents.
1746 * - the field contains a CR or LF.
1747 * - the field contains a double quote.
1748 * - the field is exactly "\.".
1749 * - the field separator is either "\" or ".".
1750 * The last two cases prevent producing a line that the server's COPY
1751 * command would interpret as an end-of-data marker. We only really
1752 * need to ensure that the complete line isn't exactly "\.", but for
1753 * simplicity we apply stronger restrictions here.
1754 *----------------
1756 if (strchr(str, sep) != NULL ||
1757 strcspn(str, "\r\n\"") != strlen(str) ||
1758 strcmp(str, "\\.") == 0 ||
1759 sep == '\\' || sep == '.')
1760 csv_escaped_print(str, fout);
1761 else
1762 fputs(str, fout);
1765 static void
1766 print_csv_text(const printTableContent *cont, FILE *fout)
1768 const char *const *ptr;
1769 int i;
1771 if (cancel_pressed)
1772 return;
1775 * The title and footer are never printed in csv format. The header is
1776 * printed if opt_tuples_only is false.
1778 * Despite RFC 4180 saying that end of lines are CRLF, terminate lines
1779 * with '\n', which prints out as the system-dependent EOL string in text
1780 * mode (typically LF on Unix and CRLF on Windows).
1782 if (cont->opt->start_table && !cont->opt->tuples_only)
1784 /* print headers */
1785 for (ptr = cont->headers; *ptr; ptr++)
1787 if (ptr != cont->headers)
1788 fputc(cont->opt->csvFieldSep[0], fout);
1789 csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
1791 fputc('\n', fout);
1794 /* print cells */
1795 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1797 csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
1798 if ((i + 1) % cont->ncolumns)
1799 fputc(cont->opt->csvFieldSep[0], fout);
1800 else
1801 fputc('\n', fout);
1805 static void
1806 print_csv_vertical(const printTableContent *cont, FILE *fout)
1808 const char *const *ptr;
1809 int i;
1811 /* print records */
1812 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1814 if (cancel_pressed)
1815 return;
1817 /* print name of column */
1818 csv_print_field(cont->headers[i % cont->ncolumns], fout,
1819 cont->opt->csvFieldSep[0]);
1821 /* print field separator */
1822 fputc(cont->opt->csvFieldSep[0], fout);
1824 /* print field value */
1825 csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
1827 fputc('\n', fout);
1832 /**********************/
1833 /* HTML */
1834 /**********************/
1837 void
1838 html_escaped_print(const char *in, FILE *fout)
1840 const char *p;
1841 bool leading_space = true;
1843 for (p = in; *p; p++)
1845 switch (*p)
1847 case '&':
1848 fputs("&amp;", fout);
1849 break;
1850 case '<':
1851 fputs("&lt;", fout);
1852 break;
1853 case '>':
1854 fputs("&gt;", fout);
1855 break;
1856 case '\n':
1857 fputs("<br />\n", fout);
1858 break;
1859 case '"':
1860 fputs("&quot;", fout);
1861 break;
1862 case ' ':
1863 /* protect leading space, for EXPLAIN output */
1864 if (leading_space)
1865 fputs("&nbsp;", fout);
1866 else
1867 fputs(" ", fout);
1868 break;
1869 default:
1870 fputc(*p, fout);
1872 if (*p != ' ')
1873 leading_space = false;
1878 static void
1879 print_html_text(const printTableContent *cont, FILE *fout)
1881 bool opt_tuples_only = cont->opt->tuples_only;
1882 unsigned short opt_border = cont->opt->border;
1883 const char *opt_table_attr = cont->opt->tableAttr;
1884 unsigned int i;
1885 const char *const *ptr;
1887 if (cancel_pressed)
1888 return;
1890 if (cont->opt->start_table)
1892 fprintf(fout, "<table border=\"%d\"", opt_border);
1893 if (opt_table_attr)
1894 fprintf(fout, " %s", opt_table_attr);
1895 fputs(">\n", fout);
1897 /* print title */
1898 if (!opt_tuples_only && cont->title)
1900 fputs(" <caption>", fout);
1901 html_escaped_print(cont->title, fout);
1902 fputs("</caption>\n", fout);
1905 /* print headers */
1906 if (!opt_tuples_only)
1908 fputs(" <tr>\n", fout);
1909 for (ptr = cont->headers; *ptr; ptr++)
1911 fputs(" <th align=\"center\">", fout);
1912 html_escaped_print(*ptr, fout);
1913 fputs("</th>\n", fout);
1915 fputs(" </tr>\n", fout);
1919 /* print cells */
1920 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1922 if (i % cont->ncolumns == 0)
1924 if (cancel_pressed)
1925 break;
1926 fputs(" <tr valign=\"top\">\n", fout);
1929 fprintf(fout, " <td align=\"%s\">", cont->aligns[(i) % cont->ncolumns] == 'r' ? "right" : "left");
1930 /* is string only whitespace? */
1931 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
1932 fputs("&nbsp; ", fout);
1933 else
1934 html_escaped_print(*ptr, fout);
1936 fputs("</td>\n", fout);
1938 if ((i + 1) % cont->ncolumns == 0)
1939 fputs(" </tr>\n", fout);
1942 if (cont->opt->stop_table)
1944 printTableFooter *footers = footers_with_default(cont);
1946 fputs("</table>\n", fout);
1948 /* print footers */
1949 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
1951 printTableFooter *f;
1953 fputs("<p>", fout);
1954 for (f = footers; f; f = f->next)
1956 html_escaped_print(f->data, fout);
1957 fputs("<br />\n", fout);
1959 fputs("</p>", fout);
1962 fputc('\n', fout);
1967 static void
1968 print_html_vertical(const printTableContent *cont, FILE *fout)
1970 bool opt_tuples_only = cont->opt->tuples_only;
1971 unsigned short opt_border = cont->opt->border;
1972 const char *opt_table_attr = cont->opt->tableAttr;
1973 unsigned long record = cont->opt->prior_records + 1;
1974 unsigned int i;
1975 const char *const *ptr;
1977 if (cancel_pressed)
1978 return;
1980 if (cont->opt->start_table)
1982 fprintf(fout, "<table border=\"%d\"", opt_border);
1983 if (opt_table_attr)
1984 fprintf(fout, " %s", opt_table_attr);
1985 fputs(">\n", fout);
1987 /* print title */
1988 if (!opt_tuples_only && cont->title)
1990 fputs(" <caption>", fout);
1991 html_escaped_print(cont->title, fout);
1992 fputs("</caption>\n", fout);
1996 /* print records */
1997 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1999 if (i % cont->ncolumns == 0)
2001 if (cancel_pressed)
2002 break;
2003 if (!opt_tuples_only)
2004 fprintf(fout,
2005 "\n <tr><td colspan=\"2\" align=\"center\">Record %lu</td></tr>\n",
2006 record++);
2007 else
2008 fputs("\n <tr><td colspan=\"2\">&nbsp;</td></tr>\n", fout);
2010 fputs(" <tr valign=\"top\">\n"
2011 " <th>", fout);
2012 html_escaped_print(cont->headers[i % cont->ncolumns], fout);
2013 fputs("</th>\n", fout);
2015 fprintf(fout, " <td align=\"%s\">", cont->aligns[i % cont->ncolumns] == 'r' ? "right" : "left");
2016 /* is string only whitespace? */
2017 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2018 fputs("&nbsp; ", fout);
2019 else
2020 html_escaped_print(*ptr, fout);
2022 fputs("</td>\n </tr>\n", fout);
2025 if (cont->opt->stop_table)
2027 fputs("</table>\n", fout);
2029 /* print footers */
2030 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
2032 printTableFooter *f;
2034 fputs("<p>", fout);
2035 for (f = cont->footers; f; f = f->next)
2037 html_escaped_print(f->data, fout);
2038 fputs("<br />\n", fout);
2040 fputs("</p>", fout);
2043 fputc('\n', fout);
2048 /*************************/
2049 /* ASCIIDOC */
2050 /*************************/
2053 static void
2054 asciidoc_escaped_print(const char *in, FILE *fout)
2056 const char *p;
2058 for (p = in; *p; p++)
2060 switch (*p)
2062 case '|':
2063 fputs("\\|", fout);
2064 break;
2065 default:
2066 fputc(*p, fout);
2071 static void
2072 print_asciidoc_text(const printTableContent *cont, FILE *fout)
2074 bool opt_tuples_only = cont->opt->tuples_only;
2075 unsigned short opt_border = cont->opt->border;
2076 unsigned int i;
2077 const char *const *ptr;
2079 if (cancel_pressed)
2080 return;
2082 if (cont->opt->start_table)
2084 /* print table in new paragraph - enforce preliminary new line */
2085 fputs("\n", fout);
2087 /* print title */
2088 if (!opt_tuples_only && cont->title)
2090 fputs(".", fout);
2091 fputs(cont->title, fout);
2092 fputs("\n", fout);
2095 /* print table [] header definition */
2096 fprintf(fout, "[%scols=\"", !opt_tuples_only ? "options=\"header\"," : "");
2097 for (i = 0; i < cont->ncolumns; i++)
2099 if (i != 0)
2100 fputs(",", fout);
2101 fprintf(fout, "%s", cont->aligns[(i) % cont->ncolumns] == 'r' ? ">l" : "<l");
2103 fputs("\"", fout);
2104 switch (opt_border)
2106 case 0:
2107 fputs(",frame=\"none\",grid=\"none\"", fout);
2108 break;
2109 case 1:
2110 fputs(",frame=\"none\"", fout);
2111 break;
2112 case 2:
2113 fputs(",frame=\"all\",grid=\"all\"", fout);
2114 break;
2116 fputs("]\n", fout);
2117 fputs("|====\n", fout);
2119 /* print headers */
2120 if (!opt_tuples_only)
2122 for (ptr = cont->headers; *ptr; ptr++)
2124 if (ptr != cont->headers)
2125 fputs(" ", fout);
2126 fputs("^l|", fout);
2127 asciidoc_escaped_print(*ptr, fout);
2129 fputs("\n", fout);
2133 /* print cells */
2134 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2136 if (i % cont->ncolumns == 0)
2138 if (cancel_pressed)
2139 break;
2142 if (i % cont->ncolumns != 0)
2143 fputs(" ", fout);
2144 fputs("|", fout);
2146 /* protect against needless spaces */
2147 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2149 if ((i + 1) % cont->ncolumns != 0)
2150 fputs(" ", fout);
2152 else
2153 asciidoc_escaped_print(*ptr, fout);
2155 if ((i + 1) % cont->ncolumns == 0)
2156 fputs("\n", fout);
2159 fputs("|====\n", fout);
2161 if (cont->opt->stop_table)
2163 printTableFooter *footers = footers_with_default(cont);
2165 /* print footers */
2166 if (!opt_tuples_only && footers != NULL && !cancel_pressed)
2168 printTableFooter *f;
2170 fputs("\n....\n", fout);
2171 for (f = footers; f; f = f->next)
2173 fputs(f->data, fout);
2174 fputs("\n", fout);
2176 fputs("....\n", fout);
2181 static void
2182 print_asciidoc_vertical(const printTableContent *cont, FILE *fout)
2184 bool opt_tuples_only = cont->opt->tuples_only;
2185 unsigned short opt_border = cont->opt->border;
2186 unsigned long record = cont->opt->prior_records + 1;
2187 unsigned int i;
2188 const char *const *ptr;
2190 if (cancel_pressed)
2191 return;
2193 if (cont->opt->start_table)
2195 /* print table in new paragraph - enforce preliminary new line */
2196 fputs("\n", fout);
2198 /* print title */
2199 if (!opt_tuples_only && cont->title)
2201 fputs(".", fout);
2202 fputs(cont->title, fout);
2203 fputs("\n", fout);
2206 /* print table [] header definition */
2207 fputs("[cols=\"h,l\"", fout);
2208 switch (opt_border)
2210 case 0:
2211 fputs(",frame=\"none\",grid=\"none\"", fout);
2212 break;
2213 case 1:
2214 fputs(",frame=\"none\"", fout);
2215 break;
2216 case 2:
2217 fputs(",frame=\"all\",grid=\"all\"", fout);
2218 break;
2220 fputs("]\n", fout);
2221 fputs("|====\n", fout);
2224 /* print records */
2225 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2227 if (i % cont->ncolumns == 0)
2229 if (cancel_pressed)
2230 break;
2231 if (!opt_tuples_only)
2232 fprintf(fout,
2233 "2+^|Record %lu\n",
2234 record++);
2235 else
2236 fputs("2+|\n", fout);
2239 fputs("<l|", fout);
2240 asciidoc_escaped_print(cont->headers[i % cont->ncolumns], fout);
2242 fprintf(fout, " %s|", cont->aligns[i % cont->ncolumns] == 'r' ? ">l" : "<l");
2243 /* is string only whitespace? */
2244 if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2245 fputs(" ", fout);
2246 else
2247 asciidoc_escaped_print(*ptr, fout);
2248 fputs("\n", fout);
2251 fputs("|====\n", fout);
2253 if (cont->opt->stop_table)
2255 /* print footers */
2256 if (!opt_tuples_only && cont->footers != NULL && !cancel_pressed)
2258 printTableFooter *f;
2260 fputs("\n....\n", fout);
2261 for (f = cont->footers; f; f = f->next)
2263 fputs(f->data, fout);
2264 fputs("\n", fout);
2266 fputs("....\n", fout);
2272 /*************************/
2273 /* LaTeX */
2274 /*************************/
2277 static void
2278 latex_escaped_print(const char *in, FILE *fout)
2280 const char *p;
2282 for (p = in; *p; p++)
2283 switch (*p)
2286 * We convert ASCII characters per the recommendations in
2287 * Scott Pakin's "The Comprehensive LATEX Symbol List",
2288 * available from CTAN. For non-ASCII, you're on your own.
2290 case '#':
2291 fputs("\\#", fout);
2292 break;
2293 case '$':
2294 fputs("\\$", fout);
2295 break;
2296 case '%':
2297 fputs("\\%", fout);
2298 break;
2299 case '&':
2300 fputs("\\&", fout);
2301 break;
2302 case '<':
2303 fputs("\\textless{}", fout);
2304 break;
2305 case '>':
2306 fputs("\\textgreater{}", fout);
2307 break;
2308 case '\\':
2309 fputs("\\textbackslash{}", fout);
2310 break;
2311 case '^':
2312 fputs("\\^{}", fout);
2313 break;
2314 case '_':
2315 fputs("\\_", fout);
2316 break;
2317 case '{':
2318 fputs("\\{", fout);
2319 break;
2320 case '|':
2321 fputs("\\textbar{}", fout);
2322 break;
2323 case '}':
2324 fputs("\\}", fout);
2325 break;
2326 case '~':
2327 fputs("\\~{}", fout);
2328 break;
2329 case '\n':
2330 /* This is not right, but doing it right seems too hard */
2331 fputs("\\\\", fout);
2332 break;
2333 default:
2334 fputc(*p, fout);
2339 static void
2340 print_latex_text(const printTableContent *cont, FILE *fout)
2342 bool opt_tuples_only = cont->opt->tuples_only;
2343 unsigned short opt_border = cont->opt->border;
2344 unsigned int i;
2345 const char *const *ptr;
2347 if (cancel_pressed)
2348 return;
2350 if (opt_border > 3)
2351 opt_border = 3;
2353 if (cont->opt->start_table)
2355 /* print title */
2356 if (!opt_tuples_only && cont->title)
2358 fputs("\\begin{center}\n", fout);
2359 latex_escaped_print(cont->title, fout);
2360 fputs("\n\\end{center}\n\n", fout);
2363 /* begin environment and set alignments and borders */
2364 fputs("\\begin{tabular}{", fout);
2366 if (opt_border >= 2)
2367 fputs("| ", fout);
2368 for (i = 0; i < cont->ncolumns; i++)
2370 fputc(*(cont->aligns + i), fout);
2371 if (opt_border != 0 && i < cont->ncolumns - 1)
2372 fputs(" | ", fout);
2374 if (opt_border >= 2)
2375 fputs(" |", fout);
2377 fputs("}\n", fout);
2379 if (!opt_tuples_only && opt_border >= 2)
2380 fputs("\\hline\n", fout);
2382 /* print headers */
2383 if (!opt_tuples_only)
2385 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2387 if (i != 0)
2388 fputs(" & ", fout);
2389 fputs("\\textit{", fout);
2390 latex_escaped_print(*ptr, fout);
2391 fputc('}', fout);
2393 fputs(" \\\\\n", fout);
2394 fputs("\\hline\n", fout);
2398 /* print cells */
2399 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2401 latex_escaped_print(*ptr, fout);
2403 if ((i + 1) % cont->ncolumns == 0)
2405 fputs(" \\\\\n", fout);
2406 if (opt_border == 3)
2407 fputs("\\hline\n", fout);
2408 if (cancel_pressed)
2409 break;
2411 else
2412 fputs(" & ", fout);
2415 if (cont->opt->stop_table)
2417 printTableFooter *footers = footers_with_default(cont);
2419 if (opt_border == 2)
2420 fputs("\\hline\n", fout);
2422 fputs("\\end{tabular}\n\n\\noindent ", fout);
2424 /* print footers */
2425 if (footers && !opt_tuples_only && !cancel_pressed)
2427 printTableFooter *f;
2429 for (f = footers; f; f = f->next)
2431 latex_escaped_print(f->data, fout);
2432 fputs(" \\\\\n", fout);
2436 fputc('\n', fout);
2441 /*************************/
2442 /* LaTeX longtable */
2443 /*************************/
2446 static void
2447 print_latex_longtable_text(const printTableContent *cont, FILE *fout)
2449 bool opt_tuples_only = cont->opt->tuples_only;
2450 unsigned short opt_border = cont->opt->border;
2451 unsigned int i;
2452 const char *opt_table_attr = cont->opt->tableAttr;
2453 const char *next_opt_table_attr_char = opt_table_attr;
2454 const char *last_opt_table_attr_char = NULL;
2455 const char *const *ptr;
2457 if (cancel_pressed)
2458 return;
2460 if (opt_border > 3)
2461 opt_border = 3;
2463 if (cont->opt->start_table)
2465 /* begin environment and set alignments and borders */
2466 fputs("\\begin{longtable}{", fout);
2468 if (opt_border >= 2)
2469 fputs("| ", fout);
2471 for (i = 0; i < cont->ncolumns; i++)
2473 /* longtable supports either a width (p) or an alignment (l/r) */
2474 /* Are we left-justified and was a proportional width specified? */
2475 if (*(cont->aligns + i) == 'l' && opt_table_attr)
2477 #define LONGTABLE_WHITESPACE " \t\n"
2479 /* advance over whitespace */
2480 next_opt_table_attr_char += strspn(next_opt_table_attr_char,
2481 LONGTABLE_WHITESPACE);
2482 /* We have a value? */
2483 if (next_opt_table_attr_char[0] != '\0')
2485 fputs("p{", fout);
2486 fwrite(next_opt_table_attr_char, strcspn(next_opt_table_attr_char,
2487 LONGTABLE_WHITESPACE), 1, fout);
2488 last_opt_table_attr_char = next_opt_table_attr_char;
2489 next_opt_table_attr_char += strcspn(next_opt_table_attr_char,
2490 LONGTABLE_WHITESPACE);
2491 fputs("\\textwidth}", fout);
2493 /* use previous value */
2494 else if (last_opt_table_attr_char != NULL)
2496 fputs("p{", fout);
2497 fwrite(last_opt_table_attr_char, strcspn(last_opt_table_attr_char,
2498 LONGTABLE_WHITESPACE), 1, fout);
2499 fputs("\\textwidth}", fout);
2501 else
2502 fputc('l', fout);
2504 else
2505 fputc(*(cont->aligns + i), fout);
2507 if (opt_border != 0 && i < cont->ncolumns - 1)
2508 fputs(" | ", fout);
2511 if (opt_border >= 2)
2512 fputs(" |", fout);
2514 fputs("}\n", fout);
2516 /* print headers */
2517 if (!opt_tuples_only)
2519 /* firsthead */
2520 if (opt_border >= 2)
2521 fputs("\\toprule\n", fout);
2522 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2524 if (i != 0)
2525 fputs(" & ", fout);
2526 fputs("\\small\\textbf{\\textit{", fout);
2527 latex_escaped_print(*ptr, fout);
2528 fputs("}}", fout);
2530 fputs(" \\\\\n", fout);
2531 fputs("\\midrule\n\\endfirsthead\n", fout);
2533 /* secondary heads */
2534 if (opt_border >= 2)
2535 fputs("\\toprule\n", fout);
2536 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2538 if (i != 0)
2539 fputs(" & ", fout);
2540 fputs("\\small\\textbf{\\textit{", fout);
2541 latex_escaped_print(*ptr, fout);
2542 fputs("}}", fout);
2544 fputs(" \\\\\n", fout);
2545 /* If the line under the row already appeared, don't do another */
2546 if (opt_border != 3)
2547 fputs("\\midrule\n", fout);
2548 fputs("\\endhead\n", fout);
2550 /* table name, caption? */
2551 if (!opt_tuples_only && cont->title)
2553 /* Don't output if we are printing a line under each row */
2554 if (opt_border == 2)
2555 fputs("\\bottomrule\n", fout);
2556 fputs("\\caption[", fout);
2557 latex_escaped_print(cont->title, fout);
2558 fputs(" (Continued)]{", fout);
2559 latex_escaped_print(cont->title, fout);
2560 fputs("}\n\\endfoot\n", fout);
2561 if (opt_border == 2)
2562 fputs("\\bottomrule\n", fout);
2563 fputs("\\caption[", fout);
2564 latex_escaped_print(cont->title, fout);
2565 fputs("]{", fout);
2566 latex_escaped_print(cont->title, fout);
2567 fputs("}\n\\endlastfoot\n", fout);
2569 /* output bottom table line? */
2570 else if (opt_border >= 2)
2572 fputs("\\bottomrule\n\\endfoot\n", fout);
2573 fputs("\\bottomrule\n\\endlastfoot\n", fout);
2578 /* print cells */
2579 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2581 /* Add a line under each row? */
2582 if (i != 0 && i % cont->ncolumns != 0)
2583 fputs("\n&\n", fout);
2584 fputs("\\raggedright{", fout);
2585 latex_escaped_print(*ptr, fout);
2586 fputc('}', fout);
2587 if ((i + 1) % cont->ncolumns == 0)
2589 fputs(" \\tabularnewline\n", fout);
2590 if (opt_border == 3)
2591 fputs(" \\hline\n", fout);
2593 if (cancel_pressed)
2594 break;
2597 if (cont->opt->stop_table)
2598 fputs("\\end{longtable}\n", fout);
2602 static void
2603 print_latex_vertical(const printTableContent *cont, FILE *fout)
2605 bool opt_tuples_only = cont->opt->tuples_only;
2606 unsigned short opt_border = cont->opt->border;
2607 unsigned long record = cont->opt->prior_records + 1;
2608 unsigned int i;
2609 const char *const *ptr;
2611 if (cancel_pressed)
2612 return;
2614 if (opt_border > 2)
2615 opt_border = 2;
2617 if (cont->opt->start_table)
2619 /* print title */
2620 if (!opt_tuples_only && cont->title)
2622 fputs("\\begin{center}\n", fout);
2623 latex_escaped_print(cont->title, fout);
2624 fputs("\n\\end{center}\n\n", fout);
2627 /* begin environment and set alignments and borders */
2628 fputs("\\begin{tabular}{", fout);
2629 if (opt_border == 0)
2630 fputs("cl", fout);
2631 else if (opt_border == 1)
2632 fputs("c|l", fout);
2633 else if (opt_border == 2)
2634 fputs("|c|l|", fout);
2635 fputs("}\n", fout);
2638 /* print records */
2639 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2641 /* new record */
2642 if (i % cont->ncolumns == 0)
2644 if (cancel_pressed)
2645 break;
2646 if (!opt_tuples_only)
2648 if (opt_border == 2)
2650 fputs("\\hline\n", fout);
2651 fprintf(fout, "\\multicolumn{2}{|c|}{\\textit{Record %lu}} \\\\\n", record++);
2653 else
2654 fprintf(fout, "\\multicolumn{2}{c}{\\textit{Record %lu}} \\\\\n", record++);
2656 if (opt_border >= 1)
2657 fputs("\\hline\n", fout);
2660 latex_escaped_print(cont->headers[i % cont->ncolumns], fout);
2661 fputs(" & ", fout);
2662 latex_escaped_print(*ptr, fout);
2663 fputs(" \\\\\n", fout);
2666 if (cont->opt->stop_table)
2668 if (opt_border == 2)
2669 fputs("\\hline\n", fout);
2671 fputs("\\end{tabular}\n\n\\noindent ", fout);
2673 /* print footers */
2674 if (cont->footers && !opt_tuples_only && !cancel_pressed)
2676 printTableFooter *f;
2678 for (f = cont->footers; f; f = f->next)
2680 latex_escaped_print(f->data, fout);
2681 fputs(" \\\\\n", fout);
2685 fputc('\n', fout);
2690 /*************************/
2691 /* Troff -ms */
2692 /*************************/
2695 static void
2696 troff_ms_escaped_print(const char *in, FILE *fout)
2698 const char *p;
2700 for (p = in; *p; p++)
2701 switch (*p)
2703 case '\\':
2704 fputs("\\(rs", fout);
2705 break;
2706 default:
2707 fputc(*p, fout);
2712 static void
2713 print_troff_ms_text(const printTableContent *cont, FILE *fout)
2715 bool opt_tuples_only = cont->opt->tuples_only;
2716 unsigned short opt_border = cont->opt->border;
2717 unsigned int i;
2718 const char *const *ptr;
2720 if (cancel_pressed)
2721 return;
2723 if (opt_border > 2)
2724 opt_border = 2;
2726 if (cont->opt->start_table)
2728 /* print title */
2729 if (!opt_tuples_only && cont->title)
2731 fputs(".LP\n.DS C\n", fout);
2732 troff_ms_escaped_print(cont->title, fout);
2733 fputs("\n.DE\n", fout);
2736 /* begin environment and set alignments and borders */
2737 fputs(".LP\n.TS\n", fout);
2738 if (opt_border == 2)
2739 fputs("center box;\n", fout);
2740 else
2741 fputs("center;\n", fout);
2743 for (i = 0; i < cont->ncolumns; i++)
2745 fputc(*(cont->aligns + i), fout);
2746 if (opt_border > 0 && i < cont->ncolumns - 1)
2747 fputs(" | ", fout);
2749 fputs(".\n", fout);
2751 /* print headers */
2752 if (!opt_tuples_only)
2754 for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2756 if (i != 0)
2757 fputc('\t', fout);
2758 fputs("\\fI", fout);
2759 troff_ms_escaped_print(*ptr, fout);
2760 fputs("\\fP", fout);
2762 fputs("\n_\n", fout);
2766 /* print cells */
2767 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2769 troff_ms_escaped_print(*ptr, fout);
2771 if ((i + 1) % cont->ncolumns == 0)
2773 fputc('\n', fout);
2774 if (cancel_pressed)
2775 break;
2777 else
2778 fputc('\t', fout);
2781 if (cont->opt->stop_table)
2783 printTableFooter *footers = footers_with_default(cont);
2785 fputs(".TE\n.DS L\n", fout);
2787 /* print footers */
2788 if (footers && !opt_tuples_only && !cancel_pressed)
2790 printTableFooter *f;
2792 for (f = footers; f; f = f->next)
2794 troff_ms_escaped_print(f->data, fout);
2795 fputc('\n', fout);
2799 fputs(".DE\n", fout);
2804 static void
2805 print_troff_ms_vertical(const printTableContent *cont, FILE *fout)
2807 bool opt_tuples_only = cont->opt->tuples_only;
2808 unsigned short opt_border = cont->opt->border;
2809 unsigned long record = cont->opt->prior_records + 1;
2810 unsigned int i;
2811 const char *const *ptr;
2812 unsigned short current_format = 0; /* 0=none, 1=header, 2=body */
2814 if (cancel_pressed)
2815 return;
2817 if (opt_border > 2)
2818 opt_border = 2;
2820 if (cont->opt->start_table)
2822 /* print title */
2823 if (!opt_tuples_only && cont->title)
2825 fputs(".LP\n.DS C\n", fout);
2826 troff_ms_escaped_print(cont->title, fout);
2827 fputs("\n.DE\n", fout);
2830 /* begin environment and set alignments and borders */
2831 fputs(".LP\n.TS\n", fout);
2832 if (opt_border == 2)
2833 fputs("center box;\n", fout);
2834 else
2835 fputs("center;\n", fout);
2837 /* basic format */
2838 if (opt_tuples_only)
2839 fputs("c l;\n", fout);
2841 else
2842 current_format = 2; /* assume tuples printed already */
2844 /* print records */
2845 for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2847 /* new record */
2848 if (i % cont->ncolumns == 0)
2850 if (cancel_pressed)
2851 break;
2852 if (!opt_tuples_only)
2854 if (current_format != 1)
2856 if (opt_border == 2 && record > 1)
2857 fputs("_\n", fout);
2858 if (current_format != 0)
2859 fputs(".T&\n", fout);
2860 fputs("c s.\n", fout);
2861 current_format = 1;
2863 fprintf(fout, "\\fIRecord %lu\\fP\n", record++);
2865 if (opt_border >= 1)
2866 fputs("_\n", fout);
2869 if (!opt_tuples_only)
2871 if (current_format != 2)
2873 if (current_format != 0)
2874 fputs(".T&\n", fout);
2875 if (opt_border != 1)
2876 fputs("c l.\n", fout);
2877 else
2878 fputs("c | l.\n", fout);
2879 current_format = 2;
2883 troff_ms_escaped_print(cont->headers[i % cont->ncolumns], fout);
2884 fputc('\t', fout);
2885 troff_ms_escaped_print(*ptr, fout);
2887 fputc('\n', fout);
2890 if (cont->opt->stop_table)
2892 fputs(".TE\n.DS L\n", fout);
2894 /* print footers */
2895 if (cont->footers && !opt_tuples_only && !cancel_pressed)
2897 printTableFooter *f;
2899 for (f = cont->footers; f; f = f->next)
2901 troff_ms_escaped_print(f->data, fout);
2902 fputc('\n', fout);
2906 fputs(".DE\n", fout);
2911 /********************************/
2912 /* Public functions */
2913 /********************************/
2917 * disable_sigpipe_trap
2919 * Turn off SIGPIPE interrupt --- call this before writing to a temporary
2920 * query output file that is a pipe.
2922 * No-op on Windows, where there's no SIGPIPE interrupts.
2924 void
2925 disable_sigpipe_trap(void)
2927 #ifndef WIN32
2928 pqsignal(SIGPIPE, SIG_IGN);
2929 #endif
2933 * restore_sigpipe_trap
2935 * Restore normal SIGPIPE interrupt --- call this when done writing to a
2936 * temporary query output file that was (or might have been) a pipe.
2938 * Note: within psql, we enable SIGPIPE interrupts unless the permanent query
2939 * output file is a pipe, in which case they should be kept off. This
2940 * approach works only because psql is not currently complicated enough to
2941 * have nested usages of short-lived output files. Otherwise we'd probably
2942 * need a genuine save-and-restore-state approach; but for now, that would be
2943 * useless complication. In non-psql programs, this always enables SIGPIPE.
2945 * No-op on Windows, where there's no SIGPIPE interrupts.
2947 void
2948 restore_sigpipe_trap(void)
2950 #ifndef WIN32
2951 pqsignal(SIGPIPE, always_ignore_sigpipe ? SIG_IGN : SIG_DFL);
2952 #endif
2956 * set_sigpipe_trap_state
2958 * Set the trap state that restore_sigpipe_trap should restore to.
2960 void
2961 set_sigpipe_trap_state(bool ignore)
2963 always_ignore_sigpipe = ignore;
2968 * PageOutput
2970 * Tests if pager is needed and returns appropriate FILE pointer.
2972 * If the topt argument is NULL no pager is used.
2974 FILE *
2975 PageOutput(int lines, const printTableOpt *topt)
2977 /* check whether we need / can / are supposed to use pager */
2978 if (topt && topt->pager && isatty(fileno(stdin)) && isatty(fileno(stdout)))
2980 #ifdef TIOCGWINSZ
2981 unsigned short int pager = topt->pager;
2982 int min_lines = topt->pager_min_lines;
2983 int result;
2984 struct winsize screen_size;
2986 result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
2988 /* >= accounts for a one-line prompt */
2989 if (result == -1
2990 || (lines >= screen_size.ws_row && lines >= min_lines)
2991 || pager > 1)
2992 #endif
2994 const char *pagerprog;
2995 FILE *pagerpipe;
2997 pagerprog = getenv("PSQL_PAGER");
2998 if (!pagerprog)
2999 pagerprog = getenv("PAGER");
3000 if (!pagerprog)
3001 pagerprog = DEFAULT_PAGER;
3002 else
3004 /* if PAGER is empty or all-white-space, don't use pager */
3005 if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
3006 return stdout;
3008 disable_sigpipe_trap();
3009 pagerpipe = popen(pagerprog, "w");
3010 if (pagerpipe)
3011 return pagerpipe;
3012 /* if popen fails, silently proceed without pager */
3013 restore_sigpipe_trap();
3017 return stdout;
3021 * ClosePager
3023 * Close previously opened pager pipe, if any
3025 void
3026 ClosePager(FILE *pagerpipe)
3028 if (pagerpipe && pagerpipe != stdout)
3031 * If printing was canceled midstream, warn about it.
3033 * Some pagers like less use Ctrl-C as part of their command set. Even
3034 * so, we abort our processing and warn the user what we did. If the
3035 * pager quit as a result of the SIGINT, this message won't go
3036 * anywhere ...
3038 if (cancel_pressed)
3039 fprintf(pagerpipe, _("Interrupted\n"));
3041 pclose(pagerpipe);
3042 restore_sigpipe_trap();
3047 * Initialise a table contents struct.
3048 * Must be called before any other printTable method is used.
3050 * The title is not duplicated; the caller must ensure that the buffer
3051 * is available for the lifetime of the printTableContent struct.
3053 * If you call this, you must call printTableCleanup once you're done with the
3054 * table.
3056 void
3057 printTableInit(printTableContent *const content, const printTableOpt *opt,
3058 const char *title, const int ncolumns, const int nrows)
3060 content->opt = opt;
3061 content->title = title;
3062 content->ncolumns = ncolumns;
3063 content->nrows = nrows;
3065 content->headers = pg_malloc0((ncolumns + 1) * sizeof(*content->headers));
3067 content->cells = pg_malloc0((ncolumns * nrows + 1) * sizeof(*content->cells));
3069 content->cellmustfree = NULL;
3070 content->footers = NULL;
3072 content->aligns = pg_malloc0((ncolumns + 1) * sizeof(*content->align));
3074 content->header = content->headers;
3075 content->cell = content->cells;
3076 content->footer = content->footers;
3077 content->align = content->aligns;
3078 content->cellsadded = 0;
3082 * Add a header to the table.
3084 * Headers are not duplicated; you must ensure that the header string is
3085 * available for the lifetime of the printTableContent struct.
3087 * If translate is true, the function will pass the header through gettext.
3088 * Otherwise, the header will not be translated.
3090 * align is either 'l' or 'r', and specifies the alignment for cells in this
3091 * column.
3093 void
3094 printTableAddHeader(printTableContent *const content, char *header,
3095 const bool translate, const char align)
3097 #ifndef ENABLE_NLS
3098 (void) translate; /* unused parameter */
3099 #endif
3101 if (content->header >= content->headers + content->ncolumns)
3103 fprintf(stderr, _("Cannot add header to table content: "
3104 "column count of %d exceeded.\n"),
3105 content->ncolumns);
3106 exit(EXIT_FAILURE);
3109 *content->header = (char *) mbvalidate((unsigned char *) header,
3110 content->opt->encoding);
3111 #ifdef ENABLE_NLS
3112 if (translate)
3113 *content->header = _(*content->header);
3114 #endif
3115 content->header++;
3117 *content->align = align;
3118 content->align++;
3122 * Add a cell to the table.
3124 * Cells are not duplicated; you must ensure that the cell string is available
3125 * for the lifetime of the printTableContent struct.
3127 * If translate is true, the function will pass the cell through gettext.
3128 * Otherwise, the cell will not be translated.
3130 * If mustfree is true, the cell string is freed by printTableCleanup().
3131 * Note: Automatic freeing of translatable strings is not supported.
3133 void
3134 printTableAddCell(printTableContent *const content, char *cell,
3135 const bool translate, const bool mustfree)
3137 #ifndef ENABLE_NLS
3138 (void) translate; /* unused parameter */
3139 #endif
3141 if (content->cellsadded >= content->ncolumns * content->nrows)
3143 fprintf(stderr, _("Cannot add cell to table content: "
3144 "total cell count of %d exceeded.\n"),
3145 content->ncolumns * content->nrows);
3146 exit(EXIT_FAILURE);
3149 *content->cell = (char *) mbvalidate((unsigned char *) cell,
3150 content->opt->encoding);
3152 #ifdef ENABLE_NLS
3153 if (translate)
3154 *content->cell = _(*content->cell);
3155 #endif
3157 if (mustfree)
3159 if (content->cellmustfree == NULL)
3160 content->cellmustfree =
3161 pg_malloc0((content->ncolumns * content->nrows + 1) * sizeof(bool));
3163 content->cellmustfree[content->cellsadded] = true;
3165 content->cell++;
3166 content->cellsadded++;
3170 * Add a footer to the table.
3172 * Footers are added as elements of a singly-linked list, and the content is
3173 * strdup'd, so there is no need to keep the original footer string around.
3175 * Footers are never translated by the function. If you want the footer
3176 * translated you must do so yourself, before calling printTableAddFooter. The
3177 * reason this works differently to headers and cells is that footers tend to
3178 * be made of up individually translated components, rather than being
3179 * translated as a whole.
3181 void
3182 printTableAddFooter(printTableContent *const content, const char *footer)
3184 printTableFooter *f;
3186 f = pg_malloc0(sizeof(*f));
3187 f->data = pg_strdup(footer);
3189 if (content->footers == NULL)
3190 content->footers = f;
3191 else
3192 content->footer->next = f;
3194 content->footer = f;
3198 * Change the content of the last-added footer.
3200 * The current contents of the last-added footer are freed, and replaced by the
3201 * content given in *footer. If there was no previous footer, add a new one.
3203 * The content is strdup'd, so there is no need to keep the original string
3204 * around.
3206 void
3207 printTableSetFooter(printTableContent *const content, const char *footer)
3209 if (content->footers != NULL)
3211 free(content->footer->data);
3212 content->footer->data = pg_strdup(footer);
3214 else
3215 printTableAddFooter(content, footer);
3219 * Free all memory allocated to this struct.
3221 * Once this has been called, the struct is unusable unless you pass it to
3222 * printTableInit() again.
3224 void
3225 printTableCleanup(printTableContent *const content)
3227 if (content->cellmustfree)
3229 int i;
3231 for (i = 0; i < content->nrows * content->ncolumns; i++)
3233 if (content->cellmustfree[i])
3234 free(unconstify(char *, content->cells[i]));
3236 free(content->cellmustfree);
3237 content->cellmustfree = NULL;
3239 free(content->headers);
3240 free(content->cells);
3241 free(content->aligns);
3243 content->opt = NULL;
3244 content->title = NULL;
3245 content->headers = NULL;
3246 content->cells = NULL;
3247 content->aligns = NULL;
3248 content->header = NULL;
3249 content->cell = NULL;
3250 content->align = NULL;
3252 if (content->footers)
3254 for (content->footer = content->footers; content->footer;)
3256 printTableFooter *f;
3258 f = content->footer;
3259 content->footer = f->next;
3260 free(f->data);
3261 free(f);
3264 content->footers = NULL;
3265 content->footer = NULL;
3269 * IsPagerNeeded
3271 * Setup pager if required
3273 static void
3274 IsPagerNeeded(const printTableContent *cont, int extra_lines, bool expanded,
3275 FILE **fout, bool *is_pager)
3277 if (*fout == stdout)
3279 int lines;
3281 if (expanded)
3282 lines = (cont->ncolumns + 1) * cont->nrows;
3283 else
3284 lines = cont->nrows + 1;
3286 if (!cont->opt->tuples_only)
3288 printTableFooter *f;
3291 * FIXME -- this is slightly bogus: it counts the number of
3292 * footers, not the number of lines in them.
3294 for (f = cont->footers; f; f = f->next)
3295 lines++;
3298 *fout = PageOutput(lines + extra_lines, cont->opt);
3299 *is_pager = (*fout != stdout);
3301 else
3302 *is_pager = false;
3306 * Use this to print any table in the supported formats.
3308 * cont: table data and formatting options
3309 * fout: where to print to
3310 * is_pager: true if caller has already redirected fout to be a pager pipe
3311 * flog: if not null, also print the table there (for --log-file option)
3313 void
3314 printTable(const printTableContent *cont,
3315 FILE *fout, bool is_pager, FILE *flog)
3317 bool is_local_pager = false;
3319 if (cancel_pressed)
3320 return;
3322 if (cont->opt->format == PRINT_NOTHING)
3323 return;
3325 /* print_aligned_*() handle the pager themselves */
3326 if (!is_pager &&
3327 cont->opt->format != PRINT_ALIGNED &&
3328 cont->opt->format != PRINT_WRAPPED)
3330 IsPagerNeeded(cont, 0, (cont->opt->expanded == 1), &fout, &is_pager);
3331 is_local_pager = is_pager;
3334 /* clear any pre-existing error indication on the output stream */
3335 clearerr(fout);
3337 /* print the stuff */
3339 if (flog)
3340 print_aligned_text(cont, flog, false);
3342 switch (cont->opt->format)
3344 case PRINT_UNALIGNED:
3345 if (cont->opt->expanded == 1)
3346 print_unaligned_vertical(cont, fout);
3347 else
3348 print_unaligned_text(cont, fout);
3349 break;
3350 case PRINT_ALIGNED:
3351 case PRINT_WRAPPED:
3354 * In expanded-auto mode, force vertical if a pager is passed in;
3355 * else we may make different decisions for different hunks of the
3356 * query result.
3358 if (cont->opt->expanded == 1 ||
3359 (cont->opt->expanded == 2 && is_pager))
3360 print_aligned_vertical(cont, fout, is_pager);
3361 else
3362 print_aligned_text(cont, fout, is_pager);
3363 break;
3364 case PRINT_CSV:
3365 if (cont->opt->expanded == 1)
3366 print_csv_vertical(cont, fout);
3367 else
3368 print_csv_text(cont, fout);
3369 break;
3370 case PRINT_HTML:
3371 if (cont->opt->expanded == 1)
3372 print_html_vertical(cont, fout);
3373 else
3374 print_html_text(cont, fout);
3375 break;
3376 case PRINT_ASCIIDOC:
3377 if (cont->opt->expanded == 1)
3378 print_asciidoc_vertical(cont, fout);
3379 else
3380 print_asciidoc_text(cont, fout);
3381 break;
3382 case PRINT_LATEX:
3383 if (cont->opt->expanded == 1)
3384 print_latex_vertical(cont, fout);
3385 else
3386 print_latex_text(cont, fout);
3387 break;
3388 case PRINT_LATEX_LONGTABLE:
3389 if (cont->opt->expanded == 1)
3390 print_latex_vertical(cont, fout);
3391 else
3392 print_latex_longtable_text(cont, fout);
3393 break;
3394 case PRINT_TROFF_MS:
3395 if (cont->opt->expanded == 1)
3396 print_troff_ms_vertical(cont, fout);
3397 else
3398 print_troff_ms_text(cont, fout);
3399 break;
3400 default:
3401 fprintf(stderr, _("invalid output format (internal error): %d"),
3402 cont->opt->format);
3403 exit(EXIT_FAILURE);
3406 if (is_local_pager)
3407 ClosePager(fout);
3411 * Use this to print query results
3413 * result: result of a successful query
3414 * opt: formatting options
3415 * fout: where to print to
3416 * is_pager: true if caller has already redirected fout to be a pager pipe
3417 * flog: if not null, also print the data there (for --log-file option)
3419 void
3420 printQuery(const PGresult *result, const printQueryOpt *opt,
3421 FILE *fout, bool is_pager, FILE *flog)
3423 printTableContent cont;
3424 int i,
3428 if (cancel_pressed)
3429 return;
3431 printTableInit(&cont, &opt->topt, opt->title,
3432 PQnfields(result), PQntuples(result));
3434 /* Assert caller supplied enough translate_columns[] entries */
3435 Assert(opt->translate_columns == NULL ||
3436 opt->n_translate_columns >= cont.ncolumns);
3438 for (i = 0; i < cont.ncolumns; i++)
3440 printTableAddHeader(&cont, PQfname(result, i),
3441 opt->translate_header,
3442 column_type_alignment(PQftype(result, i)));
3445 /* set cells */
3446 for (r = 0; r < cont.nrows; r++)
3448 for (c = 0; c < cont.ncolumns; c++)
3450 char *cell;
3451 bool mustfree = false;
3452 bool translate;
3454 if (PQgetisnull(result, r, c))
3455 cell = opt->nullPrint ? opt->nullPrint : "";
3456 else
3458 cell = PQgetvalue(result, r, c);
3459 if (cont.aligns[c] == 'r' && opt->topt.numericLocale)
3461 cell = format_numeric_locale(cell);
3462 mustfree = true;
3466 translate = (opt->translate_columns && opt->translate_columns[c]);
3467 printTableAddCell(&cont, cell, translate, mustfree);
3471 /* set footers */
3472 if (opt->footers)
3474 char **footer;
3476 for (footer = opt->footers; *footer; footer++)
3477 printTableAddFooter(&cont, *footer);
3480 printTable(&cont, fout, is_pager, flog);
3481 printTableCleanup(&cont);
3484 char
3485 column_type_alignment(Oid ftype)
3487 char align;
3489 switch (ftype)
3491 case INT2OID:
3492 case INT4OID:
3493 case INT8OID:
3494 case FLOAT4OID:
3495 case FLOAT8OID:
3496 case NUMERICOID:
3497 case OIDOID:
3498 case XIDOID:
3499 case XID8OID:
3500 case CIDOID:
3501 case MONEYOID:
3502 align = 'r';
3503 break;
3504 default:
3505 align = 'l';
3506 break;
3508 return align;
3511 void
3512 setDecimalLocale(void)
3514 struct lconv *extlconv;
3516 extlconv = localeconv();
3518 /* Don't accept an empty decimal_point string */
3519 if (*extlconv->decimal_point)
3520 decimal_point = pg_strdup(extlconv->decimal_point);
3521 else
3522 decimal_point = "."; /* SQL output standard */
3525 * Although the Open Group standard allows locales to supply more than one
3526 * group width, we consider only the first one, and we ignore any attempt
3527 * to suppress grouping by specifying CHAR_MAX. As in the backend's
3528 * cash.c, we must apply a range check to avoid being fooled by variant
3529 * CHAR_MAX values.
3531 groupdigits = *extlconv->grouping;
3532 if (groupdigits <= 0 || groupdigits > 6)
3533 groupdigits = 3; /* most common */
3535 /* Don't accept an empty thousands_sep string, either */
3536 /* similar code exists in formatting.c */
3537 if (*extlconv->thousands_sep)
3538 thousands_sep = pg_strdup(extlconv->thousands_sep);
3539 /* Make sure thousands separator doesn't match decimal point symbol. */
3540 else if (strcmp(decimal_point, ",") != 0)
3541 thousands_sep = ",";
3542 else
3543 thousands_sep = ".";
3546 /* get selected or default line style */
3547 const printTextFormat *
3548 get_line_style(const printTableOpt *opt)
3551 * Note: this function mainly exists to preserve the convention that a
3552 * printTableOpt struct can be initialized to zeroes to get default
3553 * behavior.
3555 if (opt->line_style != NULL)
3556 return opt->line_style;
3557 else
3558 return &pg_asciiformat;
3561 void
3562 refresh_utf8format(const printTableOpt *opt)
3564 printTextFormat *popt = &pg_utf8format;
3566 const unicodeStyleBorderFormat *border;
3567 const unicodeStyleRowFormat *header;
3568 const unicodeStyleColumnFormat *column;
3570 popt->name = "unicode";
3572 border = &unicode_style.border_style[opt->unicode_border_linestyle];
3573 header = &unicode_style.row_style[opt->unicode_header_linestyle];
3574 column = &unicode_style.column_style[opt->unicode_column_linestyle];
3576 popt->lrule[PRINT_RULE_TOP].hrule = border->horizontal;
3577 popt->lrule[PRINT_RULE_TOP].leftvrule = border->down_and_right;
3578 popt->lrule[PRINT_RULE_TOP].midvrule = column->down_and_horizontal[opt->unicode_border_linestyle];
3579 popt->lrule[PRINT_RULE_TOP].rightvrule = border->down_and_left;
3581 popt->lrule[PRINT_RULE_MIDDLE].hrule = header->horizontal;
3582 popt->lrule[PRINT_RULE_MIDDLE].leftvrule = header->vertical_and_right[opt->unicode_border_linestyle];
3583 popt->lrule[PRINT_RULE_MIDDLE].midvrule = column->vertical_and_horizontal[opt->unicode_header_linestyle];
3584 popt->lrule[PRINT_RULE_MIDDLE].rightvrule = header->vertical_and_left[opt->unicode_border_linestyle];
3586 popt->lrule[PRINT_RULE_BOTTOM].hrule = border->horizontal;
3587 popt->lrule[PRINT_RULE_BOTTOM].leftvrule = border->up_and_right;
3588 popt->lrule[PRINT_RULE_BOTTOM].midvrule = column->up_and_horizontal[opt->unicode_border_linestyle];
3589 popt->lrule[PRINT_RULE_BOTTOM].rightvrule = border->left_and_right;
3591 /* N/A */
3592 popt->lrule[PRINT_RULE_DATA].hrule = "";
3593 popt->lrule[PRINT_RULE_DATA].leftvrule = border->vertical;
3594 popt->lrule[PRINT_RULE_DATA].midvrule = column->vertical;
3595 popt->lrule[PRINT_RULE_DATA].rightvrule = border->vertical;
3597 popt->midvrule_nl = column->vertical;
3598 popt->midvrule_wrap = column->vertical;
3599 popt->midvrule_blank = column->vertical;
3601 /* Same for all unicode today */
3602 popt->header_nl_left = unicode_style.header_nl_left;
3603 popt->header_nl_right = unicode_style.header_nl_right;
3604 popt->nl_left = unicode_style.nl_left;
3605 popt->nl_right = unicode_style.nl_right;
3606 popt->wrap_left = unicode_style.wrap_left;
3607 popt->wrap_right = unicode_style.wrap_right;
3608 popt->wrap_right_border = unicode_style.wrap_right_border;
3612 * Compute the byte distance to the end of the string or *target_width
3613 * display character positions, whichever comes first. Update *target_width
3614 * to be the number of display character positions actually filled.
3616 static int
3617 strlen_max_width(unsigned char *str, int *target_width, int encoding)
3619 unsigned char *start = str;
3620 unsigned char *end = str + strlen((char *) str);
3621 int curr_width = 0;
3623 while (str < end)
3625 int char_width = PQdsplen((char *) str, encoding);
3628 * If the display width of the new character causes the string to
3629 * exceed its target width, skip it and return. However, if this is
3630 * the first character of the string (curr_width == 0), we have to
3631 * accept it.
3633 if (*target_width < curr_width + char_width && curr_width != 0)
3634 break;
3636 curr_width += char_width;
3638 str += PQmblen((char *) str, encoding);
3640 if (str > end) /* Don't overrun invalid string */
3641 str = end;
3644 *target_width = curr_width;
3646 return str - start;