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"
25 #include <sys/ioctl.h> /* for ioctl() */
32 #include "catalog/pg_type_d.h"
33 #include "fe_utils/mbprint.h"
34 #include "fe_utils/print.h"
37 * If the calling program doesn't have any mechanism for setting
38 * cancel_pressed, it will have no effect.
40 * Note: print.c's general strategy for when to check cancel_pressed is to do
41 * so at completion of each row of output.
43 volatile sig_atomic_t cancel_pressed
= false;
45 static bool always_ignore_sigpipe
= false;
47 /* info for locale-aware numeric formatting; set up by setDecimalLocale() */
48 static char *decimal_point
;
49 static int groupdigits
;
50 static char *thousands_sep
;
52 static char default_footer
[100];
53 static printTableFooter default_footer_cell
= {default_footer
, NULL
};
55 /* Line style control structures */
56 const printTextFormat pg_asciiformat
=
77 const printTextFormat pg_asciiformat_old
=
98 /* Default unicode linestyle format */
99 printTextFormat pg_utf8format
;
101 typedef struct unicodeStyleRowFormat
103 const char *horizontal
;
104 const char *vertical_and_right
[2];
105 const char *vertical_and_left
[2];
106 } unicodeStyleRowFormat
;
108 typedef struct unicodeStyleColumnFormat
110 const char *vertical
;
111 const char *vertical_and_horizontal
[2];
112 const char *up_and_horizontal
[2];
113 const char *down_and_horizontal
[2];
114 } unicodeStyleColumnFormat
;
116 typedef struct unicodeStyleBorderFormat
118 const char *up_and_right
;
119 const char *vertical
;
120 const char *down_and_right
;
121 const char *horizontal
;
122 const char *down_and_left
;
123 const char *left_and_right
;
124 } unicodeStyleBorderFormat
;
126 typedef struct unicodeStyleFormat
128 unicodeStyleRowFormat row_style
[2];
129 unicodeStyleColumnFormat column_style
[2];
130 unicodeStyleBorderFormat border_style
[2];
131 const char *header_nl_left
;
132 const char *header_nl_right
;
134 const char *nl_right
;
135 const char *wrap_left
;
136 const char *wrap_right
;
137 bool wrap_right_border
;
138 } unicodeStyleFormat
;
140 static const unicodeStyleFormat unicode_style
= {
146 {"\342\224\234", "\342\225\237"},
148 {"\342\224\244", "\342\225\242"},
154 {"\342\225\236", "\342\225\240"},
156 {"\342\225\241", "\342\225\243"},
164 {"\342\224\274", "\342\225\252"},
166 {"\342\224\264", "\342\225\247"},
168 {"\342\224\254", "\342\225\244"},
174 {"\342\225\253", "\342\225\254"},
176 {"\342\225\250", "\342\225\251"},
178 {"\342\225\245", "\342\225\246"},
183 {"\342\224\224", "\342\224\202", "\342\224\214", "\342\224\200", "\342\224\220", "\342\224\230"},
185 {"\342\225\232", "\342\225\221", "\342\225\224", "\342\225\220", "\342\225\227", "\342\225\235"},
188 "\342\206\265", /* ↵ */
190 "\342\206\265", /* ↵ */
191 "\342\200\246", /* … */
192 "\342\200\246", /* … */
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 */
208 integer_digits(const char *my_str
)
210 /* ignoring any sign ... */
211 if (my_str
[0] == '-' || my_str
[0] == '+')
213 /* ... count initial integral digits */
214 return strspn(my_str
, "0123456789");
217 /* Compute additional length required for locale-aware numeric output */
219 additional_numeric_locale_len(const char *my_str
)
221 int int_len
= integer_digits(my_str
),
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;
236 * Format a numeric value per current LC_NUMERIC locale setting
238 * Returns the appropriately formatted string in a new allocated block,
241 * setDecimalLocale() must have been called earlier.
244 format_numeric_locale(const char *my_str
)
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);
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
;
271 if (my_str
[0] == '-' || my_str
[0] == '+')
273 new_str
[new_str_pos
++] = my_str
[0];
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
);
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
);
309 print_separator(struct separator sep
, FILE *fout
)
311 if (sep
.separator_zero
)
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
),
339 return &default_footer_cell
;
342 return cont
->footers
;
346 /*************************/
348 /*************************/
352 print_unaligned_text(const printTableContent
*cont
, FILE *fout
)
354 bool opt_tuples_only
= cont
->opt
->tuples_only
;
356 const char *const *ptr
;
357 bool need_recordsep
= false;
362 if (cont
->opt
->start_table
)
365 if (!opt_tuples_only
&& cont
->title
)
367 fputs(cont
->title
, fout
);
368 print_separator(cont
->opt
->recordSep
, fout
);
372 if (!opt_tuples_only
)
374 for (ptr
= cont
->headers
; *ptr
; ptr
++)
376 if (ptr
!= cont
->headers
)
377 print_separator(cont
->opt
->fieldSep
, fout
);
380 need_recordsep
= true;
384 /* assume continuing printout */
385 need_recordsep
= true;
388 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
392 print_separator(cont
->opt
->recordSep
, fout
);
393 need_recordsep
= false;
399 if ((i
+ 1) % cont
->ncolumns
)
400 print_separator(cont
->opt
->fieldSep
, fout
);
402 need_recordsep
= true;
406 if (cont
->opt
->stop_table
)
408 printTableFooter
*footers
= footers_with_default(cont
);
410 if (!opt_tuples_only
&& footers
!= NULL
&& !cancel_pressed
)
414 for (f
= footers
; f
; f
= f
->next
)
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).
433 if (cont
->opt
->recordSep
.separator_zero
)
434 print_separator(cont
->opt
->recordSep
, fout
);
443 print_unaligned_vertical(const printTableContent
*cont
, FILE *fout
)
445 bool opt_tuples_only
= cont
->opt
->tuples_only
;
447 const char *const *ptr
;
448 bool need_recordsep
= false;
453 if (cont
->opt
->start_table
)
456 if (!opt_tuples_only
&& cont
->title
)
458 fputs(cont
->title
, fout
);
459 need_recordsep
= true;
463 /* assume continuing printout */
464 need_recordsep
= true;
467 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
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;
479 fputs(cont
->headers
[i
% cont
->ncolumns
], fout
);
480 print_separator(cont
->opt
->fieldSep
, fout
);
483 if ((i
+ 1) % cont
->ncolumns
)
484 print_separator(cont
->opt
->recordSep
, fout
);
486 need_recordsep
= true;
489 if (cont
->opt
->stop_table
)
492 if (!opt_tuples_only
&& cont
->footers
!= NULL
&& !cancel_pressed
)
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() */
507 if (cont
->opt
->recordSep
.separator_zero
)
508 print_separator(cont
->opt
->recordSep
, fout
);
516 /********************/
518 /********************/
523 _print_horizontal_line(const unsigned int ncolumns
, const unsigned int *widths
,
524 unsigned short border
, printTextRule pos
,
525 const printTextFormat
*format
,
528 const printTextLineFormat
*lformat
= &format
->lrule
[pos
];
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)
547 fprintf(fout
, "%s%s%s", lformat
->hrule
,
548 lformat
->midvrule
, lformat
->hrule
);
553 fprintf(fout
, "%s%s", lformat
->hrule
, lformat
->rightvrule
);
554 else if (border
== 1)
555 fputs(lformat
->hrule
, fout
);
562 * Print pretty boxes around cells.
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,
579 unsigned int *width_header
,
583 unsigned int *max_nl_lines
, /* value split by newlines */
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;
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
));
627 width_average
= NULL
;
640 /* scan all column headers, find maximum width and max max_nl_lines */
641 for (i
= 0; i
< col_count
; i
++)
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
++)
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 */
695 width_total
= col_count
;
696 else if (opt_border
== 1)
697 width_total
= col_count
* 3 - ((col_count
> 0) ? 1 : 0);
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
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
;
742 struct winsize screen_size
;
744 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &screen_size
) != -1)
745 output_columns
= screen_size
.ws_col
;
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
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;
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 */
780 ratio
= (double) width_wrap
[i
] / width_average
[i
] +
782 if (ratio
> max_ratio
)
790 /* Exit loop if we can't squeeze any more. */
794 /* Decrease width of target column by one. */
795 width_wrap
[worst_col
]--;
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
);
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
++)
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
)
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
;
863 if (cont
->opt
->start_table
)
866 if (cont
->title
&& !opt_tuples_only
)
871 pg_wcssize((const unsigned char *) cont
->title
, strlen(cont
->title
),
872 encoding
, &width
, &height
, NULL
);
873 if (width
>= width_total
)
875 fprintf(fout
, "%s\n", cont
->title
);
878 fprintf(fout
, "%-*s%s\n", (width_total
- width
) / 2, "",
883 if (!opt_tuples_only
)
885 int more_col_wrapping
;
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
;
899 memset(header_done
, false, col_count
* sizeof(bool));
900 while (more_col_wrapping
)
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
: " ",
917 nbspace
= width_wrap
[i
] - this_line
->width
;
920 fprintf(fout
, "%-*s%s%-*s",
921 nbspace
/ 2, "", this_line
->ptr
, (nbspace
+ 1) / 2, "");
923 if (!(this_line
+ 1)->ptr
)
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
: " ",
936 if (opt_border
!= 0 && col_count
> 0 && i
< col_count
- 1)
937 fputs(dformat
->midvrule
, fout
);
942 fputs(dformat
->rightvrule
, 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
)
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
]);
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.
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
]];
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 */
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
);
1005 if (!this_line
->ptr
)
1007 /* Past newline lines so just pad for other columns */
1009 fprintf(fout
, "%*s", chars_to_output
, "");
1013 /* Get strlen() of the characters up to width_wrap */
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 */
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 */
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')
1048 /* Advance to next newline line */
1050 if (col_lineptrs
[j
][curr_nl_line
[j
]].ptr
!= NULL
)
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 */
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))
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
);
1097 fputs(dformat
->midvrule
, fout
);
1101 /* end-of-row border */
1102 if (opt_border
== 2)
1103 fputs(dformat
->rightvrule
, 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
);
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
);
1131 for (i
= 0; i
< col_count
; i
++)
1133 free(col_lineptrs
[i
]);
1134 free(format_buf
[i
]);
1137 free(width_average
);
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
,
1163 const printTextLineFormat
*lformat
= &format
->lrule
[pos
];
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
);
1174 if (opt_border
== 0)
1175 reclen
= fprintf(fout
, "* Record %lu", record
);
1177 reclen
= fprintf(fout
, "[ RECORD %lu ]", record
);
1179 if (opt_border
!= 2)
1183 for (i
= reclen
; i
< hwidth
; i
++)
1184 fputs(opt_border
> 0 ? lformat
->hrule
: " ", fout
);
1190 fputs(lformat
->hrule
, fout
);
1192 fputs(lformat
->midvrule
, fout
);
1194 fputs(lformat
->hrule
, fout
);
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
);
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
;
1228 struct lineptr
*hlineptr
,
1230 bool is_local_pager
= false,
1233 int output_columns
= 0; /* Width of interactive console */
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
);
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.
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
++)
1277 pg_wcssize((const unsigned char *) cont
->headers
[i
], strlen(cont
->headers
[i
]),
1278 encoding
, &width
, &height
, &fs
);
1281 if (height
> hheight
)
1286 if (fs
> hformatsize
)
1290 /* find longest data cell */
1291 for (i
= 0, ptr
= cont
->cells
; *ptr
; ptr
++, i
++)
1297 pg_wcssize((const unsigned char *) *ptr
, strlen(*ptr
), encoding
,
1298 &width
, &height
, &fs
);
1301 if (height
> dheight
)
1306 if (fs
> dformatsize
)
1311 * We now have all the information we need to setup the formatting
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
)
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
;
1339 struct winsize screen_size
;
1341 if (ioctl(fileno(stdout
), TIOCGWINSZ
, &screen_size
) != -1)
1342 output_columns
= screen_size
.ws_col
;
1348 * Calculate available width for data in wrapped mode
1350 if (cont
->opt
->format
== PRINT_WRAPPED
)
1352 unsigned int swidth
,
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.)
1366 /* We might need a column for header newline markers, too */
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.)
1378 /* We might need a column for left header newline markers, too */
1379 if (hmultiline
&& (format
== &pg_asciiformat_old
))
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.)
1393 /* Reserve a column for data newline indicators, too, if needed */
1395 opt_border
< 2 && format
!= &pg_asciiformat_old
)
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 ]" */
1408 rwidth
+= 15; /* "+-[ RECORD ]-+" */
1411 /* We might need to do the rest of the calculation twice */
1416 /* Total width required to not wrap data */
1417 width
= hwidth
+ swidth
+ dwidth
;
1418 /* ... and not the header lines, either */
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
)
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
;
1445 /* Set data width to match output_columns */
1446 newdwidth
= output_columns
- hwidth
- swidth
;
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
)
1474 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
1488 pos
= PRINT_RULE_TOP
;
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) &&
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
,
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
,
1519 * Loop through header and data in parallel dealing with newlines and
1520 * wrapped lines until they're both exhausted
1523 dcomplete
= hcomplete
= 0;
1525 chars_to_output
= dlineptr
[dline
].width
;
1526 while (!dcomplete
|| !hcomplete
)
1529 if (opt_border
== 2)
1530 fprintf(fout
, "%s", dformat
->leftvrule
);
1532 /* Header (never wrapped so just need to deal with newlines) */
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
);
1548 strlen_max_width(hlineptr
[hline
].ptr
, &target_width
,
1550 fprintf(fout
, "%-s", hlineptr
[hline
].ptr
);
1555 swidth
-= target_width
;
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
);
1572 /* This was the last line of the header */
1573 if ((opt_border
> 0) ||
1574 (hmultiline
&& (format
!= &pg_asciiformat_old
)))
1581 unsigned int swidth
= hwidth
+ opt_border
;
1583 if ((opt_border
< 2) &&
1585 (format
== &pg_asciiformat_old
))
1588 if ((opt_border
== 0) &&
1589 (format
!= &pg_asciiformat_old
) &&
1593 fprintf(fout
, "%*s", swidth
, " ");
1600 fputs(format
->midvrule_wrap
, fout
);
1601 else if (dline
== 0)
1602 fputs(dformat
->midvrule
, fout
);
1604 fputs(format
->midvrule_nl
, fout
);
1610 int target_width
= dwidth
,
1615 * Left spacer or wrap indicator
1617 fputs(offset
== 0 ? " " : format
->wrap_left
, fout
);
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
;
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
)))
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
)))
1651 fprintf(fout
, "%*s", swidth
, " ");
1652 fputs(format
->nl_right
, fout
);
1656 chars_to_output
= dlineptr
[dline
].width
;
1660 /* reached the end of the cell */
1664 fprintf(fout
, "%*s", swidth
, " ");
1671 if (opt_border
== 2)
1672 fputs(dformat
->rightvrule
, fout
);
1679 * data exhausted (this can occur if header is longer than the
1680 * data due to newlines in the header)
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
);
1697 if (!opt_tuples_only
&& cont
->footers
!= NULL
&& !cancel_pressed
)
1699 printTableFooter
*f
;
1703 for (f
= cont
->footers
; f
; f
= f
->next
)
1704 fprintf(fout
, "%s\n", f
->data
);
1710 free(hlineptr
->ptr
);
1711 free(dlineptr
->ptr
);
1720 /**********************/
1722 /**********************/
1726 csv_escaped_print(const char *str
, FILE *fout
)
1731 for (p
= str
; *p
; p
++)
1734 fputc('"', fout
); /* double quotes are doubled */
1741 csv_print_field(const char *str
, FILE *fout
, char sep
)
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.
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
);
1766 print_csv_text(const printTableContent
*cont
, FILE *fout
)
1768 const char *const *ptr
;
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
)
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]);
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
);
1806 print_csv_vertical(const printTableContent
*cont
, FILE *fout
)
1808 const char *const *ptr
;
1812 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
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]);
1832 /**********************/
1834 /**********************/
1838 html_escaped_print(const char *in
, FILE *fout
)
1841 bool leading_space
= true;
1843 for (p
= in
; *p
; p
++)
1848 fputs("&", fout
);
1851 fputs("<", fout
);
1854 fputs(">", fout
);
1857 fputs("<br />\n", fout
);
1860 fputs(""", fout
);
1863 /* protect leading space, for EXPLAIN output */
1865 fputs(" ", fout
);
1873 leading_space
= false;
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
;
1885 const char *const *ptr
;
1890 if (cont
->opt
->start_table
)
1892 fprintf(fout
, "<table border=\"%d\"", opt_border
);
1894 fprintf(fout
, " %s", opt_table_attr
);
1898 if (!opt_tuples_only
&& cont
->title
)
1900 fputs(" <caption>", fout
);
1901 html_escaped_print(cont
->title
, fout
);
1902 fputs("</caption>\n", fout
);
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
);
1920 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
1922 if (i
% cont
->ncolumns
== 0)
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(" ", fout
);
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
);
1949 if (!opt_tuples_only
&& footers
!= NULL
&& !cancel_pressed
)
1951 printTableFooter
*f
;
1954 for (f
= footers
; f
; f
= f
->next
)
1956 html_escaped_print(f
->data
, fout
);
1957 fputs("<br />\n", fout
);
1959 fputs("</p>", fout
);
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;
1975 const char *const *ptr
;
1980 if (cont
->opt
->start_table
)
1982 fprintf(fout
, "<table border=\"%d\"", opt_border
);
1984 fprintf(fout
, " %s", opt_table_attr
);
1988 if (!opt_tuples_only
&& cont
->title
)
1990 fputs(" <caption>", fout
);
1991 html_escaped_print(cont
->title
, fout
);
1992 fputs("</caption>\n", fout
);
1997 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
1999 if (i
% cont
->ncolumns
== 0)
2003 if (!opt_tuples_only
)
2005 "\n <tr><td colspan=\"2\" align=\"center\">Record %lu</td></tr>\n",
2008 fputs("\n <tr><td colspan=\"2\"> </td></tr>\n", fout
);
2010 fputs(" <tr valign=\"top\">\n"
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(" ", fout
);
2020 html_escaped_print(*ptr
, fout
);
2022 fputs("</td>\n </tr>\n", fout
);
2025 if (cont
->opt
->stop_table
)
2027 fputs("</table>\n", fout
);
2030 if (!opt_tuples_only
&& cont
->footers
!= NULL
&& !cancel_pressed
)
2032 printTableFooter
*f
;
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
);
2048 /*************************/
2050 /*************************/
2054 asciidoc_escaped_print(const char *in
, FILE *fout
)
2058 for (p
= in
; *p
; p
++)
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
;
2077 const char *const *ptr
;
2082 if (cont
->opt
->start_table
)
2084 /* print table in new paragraph - enforce preliminary new line */
2088 if (!opt_tuples_only
&& cont
->title
)
2091 fputs(cont
->title
, fout
);
2095 /* print table [] header definition */
2096 fprintf(fout
, "[%scols=\"", !opt_tuples_only
? "options=\"header\"," : "");
2097 for (i
= 0; i
< cont
->ncolumns
; i
++)
2101 fprintf(fout
, "%s", cont
->aligns
[(i
) % cont
->ncolumns
] == 'r' ? ">l" : "<l");
2107 fputs(",frame=\"none\",grid=\"none\"", fout
);
2110 fputs(",frame=\"none\"", fout
);
2113 fputs(",frame=\"all\",grid=\"all\"", fout
);
2117 fputs("|====\n", fout
);
2120 if (!opt_tuples_only
)
2122 for (ptr
= cont
->headers
; *ptr
; ptr
++)
2124 if (ptr
!= cont
->headers
)
2127 asciidoc_escaped_print(*ptr
, fout
);
2134 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2136 if (i
% cont
->ncolumns
== 0)
2142 if (i
% cont
->ncolumns
!= 0)
2146 /* protect against needless spaces */
2147 if ((*ptr
)[strspn(*ptr
, " \t")] == '\0')
2149 if ((i
+ 1) % cont
->ncolumns
!= 0)
2153 asciidoc_escaped_print(*ptr
, fout
);
2155 if ((i
+ 1) % cont
->ncolumns
== 0)
2159 fputs("|====\n", fout
);
2161 if (cont
->opt
->stop_table
)
2163 printTableFooter
*footers
= footers_with_default(cont
);
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
);
2176 fputs("....\n", fout
);
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;
2188 const char *const *ptr
;
2193 if (cont
->opt
->start_table
)
2195 /* print table in new paragraph - enforce preliminary new line */
2199 if (!opt_tuples_only
&& cont
->title
)
2202 fputs(cont
->title
, fout
);
2206 /* print table [] header definition */
2207 fputs("[cols=\"h,l\"", fout
);
2211 fputs(",frame=\"none\",grid=\"none\"", fout
);
2214 fputs(",frame=\"none\"", fout
);
2217 fputs(",frame=\"all\",grid=\"all\"", fout
);
2221 fputs("|====\n", fout
);
2225 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2227 if (i
% cont
->ncolumns
== 0)
2231 if (!opt_tuples_only
)
2236 fputs("2+|\n", 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')
2247 asciidoc_escaped_print(*ptr
, fout
);
2251 fputs("|====\n", fout
);
2253 if (cont
->opt
->stop_table
)
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
);
2266 fputs("....\n", fout
);
2272 /*************************/
2274 /*************************/
2278 latex_escaped_print(const char *in
, FILE *fout
)
2282 for (p
= in
; *p
; 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.
2303 fputs("\\textless{}", fout
);
2306 fputs("\\textgreater{}", fout
);
2309 fputs("\\textbackslash{}", fout
);
2312 fputs("\\^{}", fout
);
2321 fputs("\\textbar{}", fout
);
2327 fputs("\\~{}", fout
);
2330 /* This is not right, but doing it right seems too hard */
2331 fputs("\\\\", fout
);
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
;
2345 const char *const *ptr
;
2353 if (cont
->opt
->start_table
)
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)
2368 for (i
= 0; i
< cont
->ncolumns
; i
++)
2370 fputc(*(cont
->aligns
+ i
), fout
);
2371 if (opt_border
!= 0 && i
< cont
->ncolumns
- 1)
2374 if (opt_border
>= 2)
2379 if (!opt_tuples_only
&& opt_border
>= 2)
2380 fputs("\\hline\n", fout
);
2383 if (!opt_tuples_only
)
2385 for (i
= 0, ptr
= cont
->headers
; i
< cont
->ncolumns
; i
++, ptr
++)
2389 fputs("\\textit{", fout
);
2390 latex_escaped_print(*ptr
, fout
);
2393 fputs(" \\\\\n", fout
);
2394 fputs("\\hline\n", fout
);
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
);
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
);
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
);
2441 /*************************/
2442 /* LaTeX longtable */
2443 /*************************/
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
;
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
;
2463 if (cont
->opt
->start_table
)
2465 /* begin environment and set alignments and borders */
2466 fputs("\\begin{longtable}{", fout
);
2468 if (opt_border
>= 2)
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')
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
)
2497 fwrite(last_opt_table_attr_char
, strcspn(last_opt_table_attr_char
,
2498 LONGTABLE_WHITESPACE
), 1, fout
);
2499 fputs("\\textwidth}", fout
);
2505 fputc(*(cont
->aligns
+ i
), fout
);
2507 if (opt_border
!= 0 && i
< cont
->ncolumns
- 1)
2511 if (opt_border
>= 2)
2517 if (!opt_tuples_only
)
2520 if (opt_border
>= 2)
2521 fputs("\\toprule\n", fout
);
2522 for (i
= 0, ptr
= cont
->headers
; i
< cont
->ncolumns
; i
++, ptr
++)
2526 fputs("\\small\\textbf{\\textit{", fout
);
2527 latex_escaped_print(*ptr
, 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
++)
2540 fputs("\\small\\textbf{\\textit{", fout
);
2541 latex_escaped_print(*ptr
, 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
);
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
);
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
);
2587 if ((i
+ 1) % cont
->ncolumns
== 0)
2589 fputs(" \\tabularnewline\n", fout
);
2590 if (opt_border
== 3)
2591 fputs(" \\hline\n", fout
);
2597 if (cont
->opt
->stop_table
)
2598 fputs("\\end{longtable}\n", fout
);
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;
2609 const char *const *ptr
;
2617 if (cont
->opt
->start_table
)
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)
2631 else if (opt_border
== 1)
2633 else if (opt_border
== 2)
2634 fputs("|c|l|", fout
);
2639 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2642 if (i
% cont
->ncolumns
== 0)
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
++);
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
);
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
);
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
);
2690 /*************************/
2692 /*************************/
2696 troff_ms_escaped_print(const char *in
, FILE *fout
)
2700 for (p
= in
; *p
; p
++)
2704 fputs("\\(rs", fout
);
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
;
2718 const char *const *ptr
;
2726 if (cont
->opt
->start_table
)
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
);
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)
2752 if (!opt_tuples_only
)
2754 for (i
= 0, ptr
= cont
->headers
; i
< cont
->ncolumns
; i
++, ptr
++)
2758 fputs("\\fI", fout
);
2759 troff_ms_escaped_print(*ptr
, fout
);
2760 fputs("\\fP", fout
);
2762 fputs("\n_\n", fout
);
2767 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2769 troff_ms_escaped_print(*ptr
, fout
);
2771 if ((i
+ 1) % cont
->ncolumns
== 0)
2781 if (cont
->opt
->stop_table
)
2783 printTableFooter
*footers
= footers_with_default(cont
);
2785 fputs(".TE\n.DS L\n", fout
);
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
);
2799 fputs(".DE\n", fout
);
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;
2811 const char *const *ptr
;
2812 unsigned short current_format
= 0; /* 0=none, 1=header, 2=body */
2820 if (cont
->opt
->start_table
)
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
);
2835 fputs("center;\n", fout
);
2838 if (opt_tuples_only
)
2839 fputs("c l;\n", fout
);
2842 current_format
= 2; /* assume tuples printed already */
2845 for (i
= 0, ptr
= cont
->cells
; *ptr
; i
++, ptr
++)
2848 if (i
% cont
->ncolumns
== 0)
2852 if (!opt_tuples_only
)
2854 if (current_format
!= 1)
2856 if (opt_border
== 2 && record
> 1)
2858 if (current_format
!= 0)
2859 fputs(".T&\n", fout
);
2860 fputs("c s.\n", fout
);
2863 fprintf(fout
, "\\fIRecord %lu\\fP\n", record
++);
2865 if (opt_border
>= 1)
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
);
2878 fputs("c | l.\n", fout
);
2883 troff_ms_escaped_print(cont
->headers
[i
% cont
->ncolumns
], fout
);
2885 troff_ms_escaped_print(*ptr
, fout
);
2890 if (cont
->opt
->stop_table
)
2892 fputs(".TE\n.DS L\n", fout
);
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
);
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.
2925 disable_sigpipe_trap(void)
2928 pqsignal(SIGPIPE
, SIG_IGN
);
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.
2948 restore_sigpipe_trap(void)
2951 pqsignal(SIGPIPE
, always_ignore_sigpipe
? SIG_IGN
: SIG_DFL
);
2956 * set_sigpipe_trap_state
2958 * Set the trap state that restore_sigpipe_trap should restore to.
2961 set_sigpipe_trap_state(bool ignore
)
2963 always_ignore_sigpipe
= ignore
;
2970 * Tests if pager is needed and returns appropriate FILE pointer.
2972 * If the topt argument is NULL no pager is used.
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
)))
2981 unsigned short int pager
= topt
->pager
;
2982 int min_lines
= topt
->pager_min_lines
;
2984 struct winsize screen_size
;
2986 result
= ioctl(fileno(stdout
), TIOCGWINSZ
, &screen_size
);
2988 /* >= accounts for a one-line prompt */
2990 || (lines
>= screen_size
.ws_row
&& lines
>= min_lines
)
2994 const char *pagerprog
;
2997 pagerprog
= getenv("PSQL_PAGER");
2999 pagerprog
= getenv("PAGER");
3001 pagerprog
= DEFAULT_PAGER
;
3004 /* if PAGER is empty or all-white-space, don't use pager */
3005 if (strspn(pagerprog
, " \t\r\n") == strlen(pagerprog
))
3008 disable_sigpipe_trap();
3009 pagerpipe
= popen(pagerprog
, "w");
3012 /* if popen fails, silently proceed without pager */
3013 restore_sigpipe_trap();
3023 * Close previously opened pager pipe, if any
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
3039 fprintf(pagerpipe
, _("Interrupted\n"));
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
3057 printTableInit(printTableContent
*const content
, const printTableOpt
*opt
,
3058 const char *title
, const int ncolumns
, const int nrows
)
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
3094 printTableAddHeader(printTableContent
*const content
, char *header
,
3095 const bool translate
, const char align
)
3098 (void) translate
; /* unused parameter */
3101 if (content
->header
>= content
->headers
+ content
->ncolumns
)
3103 fprintf(stderr
, _("Cannot add header to table content: "
3104 "column count of %d exceeded.\n"),
3109 *content
->header
= (char *) mbvalidate((unsigned char *) header
,
3110 content
->opt
->encoding
);
3113 *content
->header
= _(*content
->header
);
3117 *content
->align
= 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.
3134 printTableAddCell(printTableContent
*const content
, char *cell
,
3135 const bool translate
, const bool mustfree
)
3138 (void) translate
; /* unused parameter */
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
);
3149 *content
->cell
= (char *) mbvalidate((unsigned char *) cell
,
3150 content
->opt
->encoding
);
3154 *content
->cell
= _(*content
->cell
);
3159 if (content
->cellmustfree
== NULL
)
3160 content
->cellmustfree
=
3161 pg_malloc0((content
->ncolumns
* content
->nrows
+ 1) * sizeof(bool));
3163 content
->cellmustfree
[content
->cellsadded
] = true;
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.
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
;
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
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
);
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.
3225 printTableCleanup(printTableContent
*const content
)
3227 if (content
->cellmustfree
)
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
;
3264 content
->footers
= NULL
;
3265 content
->footer
= NULL
;
3271 * Setup pager if required
3274 IsPagerNeeded(const printTableContent
*cont
, int extra_lines
, bool expanded
,
3275 FILE **fout
, bool *is_pager
)
3277 if (*fout
== stdout
)
3282 lines
= (cont
->ncolumns
+ 1) * cont
->nrows
;
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
)
3298 *fout
= PageOutput(lines
+ extra_lines
, cont
->opt
);
3299 *is_pager
= (*fout
!= stdout
);
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)
3314 printTable(const printTableContent
*cont
,
3315 FILE *fout
, bool is_pager
, FILE *flog
)
3317 bool is_local_pager
= false;
3322 if (cont
->opt
->format
== PRINT_NOTHING
)
3325 /* print_aligned_*() handle the pager themselves */
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 */
3337 /* print the stuff */
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
);
3348 print_unaligned_text(cont
, fout
);
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
3358 if (cont
->opt
->expanded
== 1 ||
3359 (cont
->opt
->expanded
== 2 && is_pager
))
3360 print_aligned_vertical(cont
, fout
, is_pager
);
3362 print_aligned_text(cont
, fout
, is_pager
);
3365 if (cont
->opt
->expanded
== 1)
3366 print_csv_vertical(cont
, fout
);
3368 print_csv_text(cont
, fout
);
3371 if (cont
->opt
->expanded
== 1)
3372 print_html_vertical(cont
, fout
);
3374 print_html_text(cont
, fout
);
3376 case PRINT_ASCIIDOC
:
3377 if (cont
->opt
->expanded
== 1)
3378 print_asciidoc_vertical(cont
, fout
);
3380 print_asciidoc_text(cont
, fout
);
3383 if (cont
->opt
->expanded
== 1)
3384 print_latex_vertical(cont
, fout
);
3386 print_latex_text(cont
, fout
);
3388 case PRINT_LATEX_LONGTABLE
:
3389 if (cont
->opt
->expanded
== 1)
3390 print_latex_vertical(cont
, fout
);
3392 print_latex_longtable_text(cont
, fout
);
3394 case PRINT_TROFF_MS
:
3395 if (cont
->opt
->expanded
== 1)
3396 print_troff_ms_vertical(cont
, fout
);
3398 print_troff_ms_text(cont
, fout
);
3401 fprintf(stderr
, _("invalid output format (internal error): %d"),
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)
3420 printQuery(const PGresult
*result
, const printQueryOpt
*opt
,
3421 FILE *fout
, bool is_pager
, FILE *flog
)
3423 printTableContent cont
;
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
)));
3446 for (r
= 0; r
< cont
.nrows
; r
++)
3448 for (c
= 0; c
< cont
.ncolumns
; c
++)
3451 bool mustfree
= false;
3454 if (PQgetisnull(result
, r
, c
))
3455 cell
= opt
->nullPrint
? opt
->nullPrint
: "";
3458 cell
= PQgetvalue(result
, r
, c
);
3459 if (cont
.aligns
[c
] == 'r' && opt
->topt
.numericLocale
)
3461 cell
= format_numeric_locale(cell
);
3466 translate
= (opt
->translate_columns
&& opt
->translate_columns
[c
]);
3467 printTableAddCell(&cont
, cell
, translate
, mustfree
);
3476 for (footer
= opt
->footers
; *footer
; footer
++)
3477 printTableAddFooter(&cont
, *footer
);
3480 printTable(&cont
, fout
, is_pager
, flog
);
3481 printTableCleanup(&cont
);
3485 column_type_alignment(Oid ftype
)
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
);
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
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
= ",";
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
3555 if (opt
->line_style
!= NULL
)
3556 return opt
->line_style
;
3558 return &pg_asciiformat
;
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
;
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.
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
);
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
3633 if (*target_width
< curr_width
+ char_width
&& curr_width
!= 0)
3636 curr_width
+= char_width
;
3638 str
+= PQmblen((char *) str
, encoding
);
3640 if (str
> end
) /* Don't overrun invalid string */
3644 *target_width
= curr_width
;