Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * Query-result printing support for frontend code
4 : : *
5 : : * This file used to be part of psql, but now it's separated out to allow
6 : : * other frontend programs to use it.
7 : : *
8 : : *
9 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
10 : : * Portions Copyright (c) 1994, Regents of the University of California
11 : : *
12 : : * src/fe_utils/print.c
13 : : *
14 : : *-------------------------------------------------------------------------
15 : : */
16 : : #include "postgres_fe.h"
17 : :
18 : : #include <limits.h>
19 : : #include <math.h>
20 : : #include <unistd.h>
21 : :
22 : : #ifndef WIN32
23 : : #include <sys/ioctl.h> /* for ioctl() */
24 : : #endif
25 : :
26 : : #ifdef HAVE_TERMIOS_H
27 : : #include <termios.h>
28 : : #endif
29 : :
30 : : #include "catalog/pg_type_d.h"
31 : : #include "common/logging.h"
32 : : #include "fe_utils/cancel.h"
33 : : #include "fe_utils/mbprint.h"
34 : : #include "fe_utils/print.h"
35 : :
36 : : /* Presently, count_table_lines() is only used within #ifdef TIOCGWINSZ */
37 : : #ifdef TIOCGWINSZ
38 : : #define NEED_COUNT_TABLE_LINES
39 : : #endif
40 : :
41 : : /*
42 : : * Note: print.c's general strategy for when to check CancelRequested is to do
43 : : * so at completion of each row of output.
44 : : */
45 : :
46 : : static bool always_ignore_sigpipe = false;
47 : :
48 : : /* info for locale-aware numeric formatting; set up by setDecimalLocale() */
49 : : static char *decimal_point;
50 : : static int groupdigits;
51 : : static char *thousands_sep;
52 : :
53 : : static char default_footer[100];
54 : : static printTableFooter default_footer_cell = {default_footer, NULL};
55 : :
56 : : /* Line style control structures */
57 : : const printTextFormat pg_asciiformat =
58 : : {
59 : : "ascii",
60 : : {
61 : : {"-", "+", "+", "+"},
62 : : {"-", "+", "+", "+"},
63 : : {"-", "+", "+", "+"},
64 : : {"", "|", "|", "|"}
65 : : },
66 : : "|",
67 : : "|",
68 : : "|",
69 : : " ",
70 : : "+",
71 : : " ",
72 : : "+",
73 : : ".",
74 : : ".",
75 : : true
76 : : };
77 : :
78 : : const printTextFormat pg_asciiformat_old =
79 : : {
80 : : "old-ascii",
81 : : {
82 : : {"-", "+", "+", "+"},
83 : : {"-", "+", "+", "+"},
84 : : {"-", "+", "+", "+"},
85 : : {"", "|", "|", "|"}
86 : : },
87 : : ":",
88 : : ";",
89 : : " ",
90 : : "+",
91 : : " ",
92 : : " ",
93 : : " ",
94 : : " ",
95 : : " ",
96 : : false
97 : : };
98 : :
99 : : /* Default unicode linestyle format */
100 : : printTextFormat pg_utf8format;
101 : :
102 : : typedef struct unicodeStyleRowFormat
103 : : {
104 : : const char *horizontal;
105 : : const char *vertical_and_right[2];
106 : : const char *vertical_and_left[2];
107 : : } unicodeStyleRowFormat;
108 : :
109 : : typedef struct unicodeStyleColumnFormat
110 : : {
111 : : const char *vertical;
112 : : const char *vertical_and_horizontal[2];
113 : : const char *up_and_horizontal[2];
114 : : const char *down_and_horizontal[2];
115 : : } unicodeStyleColumnFormat;
116 : :
117 : : typedef struct unicodeStyleBorderFormat
118 : : {
119 : : const char *up_and_right;
120 : : const char *vertical;
121 : : const char *down_and_right;
122 : : const char *horizontal;
123 : : const char *down_and_left;
124 : : const char *left_and_right;
125 : : } unicodeStyleBorderFormat;
126 : :
127 : : typedef struct unicodeStyleFormat
128 : : {
129 : : unicodeStyleRowFormat row_style[2];
130 : : unicodeStyleColumnFormat column_style[2];
131 : : unicodeStyleBorderFormat border_style[2];
132 : : const char *header_nl_left;
133 : : const char *header_nl_right;
134 : : const char *nl_left;
135 : : const char *nl_right;
136 : : const char *wrap_left;
137 : : const char *wrap_right;
138 : : bool wrap_right_border;
139 : : } unicodeStyleFormat;
140 : :
141 : : static const unicodeStyleFormat unicode_style = {
142 : : {
143 : : {
144 : : /* U+2500 Box Drawings Light Horizontal */
145 : : "\342\224\200",
146 : :
147 : : /*--
148 : : * U+251C Box Drawings Light Vertical and Right,
149 : : * U+255F Box Drawings Vertical Double and Right Single
150 : : *--
151 : : */
152 : : {"\342\224\234", "\342\225\237"},
153 : :
154 : : /*--
155 : : * U+2524 Box Drawings Light Vertical and Left,
156 : : * U+2562 Box Drawings Vertical Double and Left Single
157 : : *--
158 : : */
159 : : {"\342\224\244", "\342\225\242"},
160 : : },
161 : : {
162 : : /* U+2550 Box Drawings Double Horizontal */
163 : : "\342\225\220",
164 : :
165 : : /*--
166 : : * U+255E Box Drawings Vertical Single and Right Double,
167 : : * U+2560 Box Drawings Double Vertical and Right
168 : : *--
169 : : */
170 : : {"\342\225\236", "\342\225\240"},
171 : :
172 : : /*--
173 : : * U+2561 Box Drawings Vertical Single and Left Double,
174 : : * U+2563 Box Drawings Double Vertical and Left
175 : : *--
176 : : */
177 : : {"\342\225\241", "\342\225\243"},
178 : : },
179 : : },
180 : : {
181 : : {
182 : : /* U+2502 Box Drawings Light Vertical */
183 : : "\342\224\202",
184 : :
185 : : /*--
186 : : * U+253C Box Drawings Light Vertical and Horizontal,
187 : : * U+256A Box Drawings Vertical Single and Horizontal Double
188 : : *--
189 : : */
190 : : {"\342\224\274", "\342\225\252"},
191 : :
192 : : /*--
193 : : * U+2534 Box Drawings Light Up and Horizontal,
194 : : * U+2567 Box Drawings Up Single and Horizontal Double
195 : : *--
196 : : */
197 : : {"\342\224\264", "\342\225\247"},
198 : :
199 : : /*--
200 : : * U+252C Box Drawings Light Down and Horizontal,
201 : : * U+2564 Box Drawings Down Single and Horizontal Double
202 : : *--
203 : : */
204 : : {"\342\224\254", "\342\225\244"},
205 : : },
206 : : {
207 : : /* U+2551 Box Drawings Double Vertical */
208 : : "\342\225\221",
209 : :
210 : : /*--
211 : : * U+256B Box Drawings Vertical Double and Horizontal Single,
212 : : * U+256C Box Drawings Double Vertical and Horizontal
213 : : *--
214 : : */
215 : : {"\342\225\253", "\342\225\254"},
216 : :
217 : : /*--
218 : : * U+2568 Box Drawings Up Double and Horizontal Single,
219 : : * U+2569 Box Drawings Double Up and Horizontal
220 : : *--
221 : : */
222 : : {"\342\225\250", "\342\225\251"},
223 : :
224 : : /*--
225 : : * U+2565 Box Drawings Down Double and Horizontal Single,
226 : : * U+2566 Box Drawings Double Down and Horizontal
227 : : *--
228 : : */
229 : : {"\342\225\245", "\342\225\246"},
230 : : },
231 : : },
232 : : {
233 : : /*--
234 : : * U+2514 Box Drawings Light Up and Right,
235 : : * U+2502 Box Drawings Light Vertical,
236 : : * U+250C Box Drawings Light Down and Right,
237 : : * U+2500 Box Drawings Light Horizontal,
238 : : * U+2510 Box Drawings Light Down and Left,
239 : : * U+2518 Box Drawings Light Up and Left
240 : : *--
241 : : */
242 : : {"\342\224\224", "\342\224\202", "\342\224\214", "\342\224\200", "\342\224\220", "\342\224\230"},
243 : :
244 : : /*--
245 : : * U+255A Box Drawings Double Up and Right,
246 : : * U+2551 Box Drawings Double Vertical,
247 : : * U+2554 Box Drawings Double Down and Right,
248 : : * U+2550 Box Drawings Double Horizontal,
249 : : * U+2557 Box Drawings Double Down and Left,
250 : : * U+255D Box Drawings Double Up and Left
251 : : *--
252 : : */
253 : : {"\342\225\232", "\342\225\221", "\342\225\224", "\342\225\220", "\342\225\227", "\342\225\235"},
254 : : },
255 : : " ",
256 : : /* U+21B5 Downwards Arrow with Corner Leftwards */
257 : : "\342\206\265",
258 : : " ",
259 : : /* U+21B5 Downwards Arrow with Corner Leftwards */
260 : : "\342\206\265",
261 : : /* U+2026 Horizontal Ellipsis */
262 : : "\342\200\246",
263 : : "\342\200\246",
264 : : true
265 : : };
266 : :
267 : :
268 : : /* Local functions */
269 : : static int strlen_max_width(unsigned char *str, int *target_width, int encoding);
270 : : static FILE *PageOutputInternal(int lines, const printTableOpt *topt,
271 : : const printTableContent *cont,
272 : : const unsigned int *width_wrap,
273 : : bool vertical);
274 : : static void IsPagerNeeded(const printTableContent *cont,
275 : : const unsigned int *width_wrap,
276 : : bool vertical,
277 : : FILE **fout, bool *is_pager);
278 : : #ifdef NEED_COUNT_TABLE_LINES
279 : : static int count_table_lines(const printTableContent *cont,
280 : : const unsigned int *width_wrap,
281 : : bool vertical,
282 : : int threshold);
283 : : #endif
284 : : static void print_aligned_vertical(const printTableContent *cont,
285 : : FILE *fout, bool is_pager);
286 : :
287 : :
288 : : /* Count number of digits in integral part of number */
289 : : static int
7807 bruce@momjian.us 290 :CBC 128 : integer_digits(const char *my_str)
291 : : {
292 : : /* ignoring any sign ... */
4083 tgl@sss.pgh.pa.us 293 [ + + - + ]: 128 : if (my_str[0] == '-' || my_str[0] == '+')
7811 bruce@momjian.us 294 : 24 : my_str++;
295 : : /* ... count initial integral digits */
4083 tgl@sss.pgh.pa.us 296 : 128 : return strspn(my_str, "0123456789");
297 : : }
298 : :
299 : : /* Compute additional length required for locale-aware numeric output */
300 : : static int
7803 bruce@momjian.us 301 : 64 : additional_numeric_locale_len(const char *my_str)
302 : : {
7714 303 : 64 : int int_len = integer_digits(my_str),
304 : 64 : len = 0;
305 : :
306 : : /* Account for added thousands_sep instances */
4083 tgl@sss.pgh.pa.us 307 [ - + ]: 64 : if (int_len > groupdigits)
4083 tgl@sss.pgh.pa.us 308 :UBC 0 : len += ((int_len - 1) / groupdigits) * strlen(thousands_sep);
309 : :
310 : : /* Account for possible additional length of decimal_point */
7803 bruce@momjian.us 311 [ - + ]:CBC 64 : if (strchr(my_str, '.') != NULL)
4083 tgl@sss.pgh.pa.us 312 :UBC 0 : len += strlen(decimal_point) - 1;
313 : :
7803 bruce@momjian.us 314 :CBC 64 : return len;
315 : : }
316 : :
317 : : /*
318 : : * Format a numeric value per current LC_NUMERIC locale setting
319 : : *
320 : : * Returns the appropriately formatted string in a new allocated block,
321 : : * caller must free.
322 : : *
323 : : * setDecimalLocale() must have been called earlier.
324 : : */
325 : : static char *
7732 326 : 64 : format_numeric_locale(const char *my_str)
327 : : {
328 : : char *new_str;
329 : : int new_len,
330 : : int_len,
331 : : leading_digits,
332 : : i,
333 : : new_str_pos;
334 : :
335 : : /*
336 : : * If the string doesn't look like a number, return it unchanged. This
337 : : * check is essential to avoid mangling already-localized "money" values.
338 : : */
4082 tgl@sss.pgh.pa.us 339 [ - + ]: 64 : if (strspn(my_str, "0123456789+-.eE") != strlen(my_str))
4082 tgl@sss.pgh.pa.us 340 :UBC 0 : return pg_strdup(my_str);
341 : :
4082 tgl@sss.pgh.pa.us 342 :CBC 64 : new_len = strlen(my_str) + additional_numeric_locale_len(my_str);
277 michael@paquier.xyz 343 : 64 : new_str = pg_malloc_array(char, (new_len + 1));
4082 tgl@sss.pgh.pa.us 344 : 64 : new_str_pos = 0;
345 : 64 : int_len = integer_digits(my_str);
346 : :
347 : : /* number of digits in first thousands group */
4083 348 : 64 : leading_digits = int_len % groupdigits;
349 [ + + ]: 64 : if (leading_digits == 0)
350 : 12 : leading_digits = groupdigits;
351 : :
352 : : /* process sign */
353 [ + + - + ]: 64 : if (my_str[0] == '-' || my_str[0] == '+')
354 : : {
355 : 12 : new_str[new_str_pos++] = my_str[0];
7732 bruce@momjian.us 356 : 12 : my_str++;
357 : : }
358 : :
359 : : /* process integer part of number */
4083 tgl@sss.pgh.pa.us 360 [ + + ]: 152 : for (i = 0; i < int_len; i++)
361 : : {
362 : : /* Time to insert separator? */
363 [ + + - + ]: 88 : if (i > 0 && --leading_digits == 0)
364 : : {
4083 tgl@sss.pgh.pa.us 365 :UBC 0 : strcpy(&new_str[new_str_pos], thousands_sep);
366 : 0 : new_str_pos += strlen(thousands_sep);
367 : 0 : leading_digits = groupdigits;
368 : : }
4083 tgl@sss.pgh.pa.us 369 :CBC 88 : new_str[new_str_pos++] = my_str[i];
370 : : }
371 : :
372 : : /* handle decimal point if any */
373 [ - + ]: 64 : if (my_str[i] == '.')
374 : : {
4083 tgl@sss.pgh.pa.us 375 :UBC 0 : strcpy(&new_str[new_str_pos], decimal_point);
376 : 0 : new_str_pos += strlen(decimal_point);
377 : 0 : i++;
378 : : }
379 : :
380 : : /* copy the rest (fractional digits and/or exponent, and \0 terminator) */
4083 tgl@sss.pgh.pa.us 381 :CBC 64 : strcpy(&new_str[new_str_pos], &my_str[i]);
382 : :
383 : : /* assert we didn't underestimate new_len (an overestimate is OK) */
384 [ - + ]: 64 : Assert(strlen(new_str) <= new_len);
385 : :
7732 bruce@momjian.us 386 : 64 : return new_str;
387 : : }
388 : :
389 : :
390 : : static void
5406 peter_e@gmx.net 391 : 1819156 : print_separator(struct separator sep, FILE *fout)
392 : : {
393 [ - + ]: 1819156 : if (sep.separator_zero)
5406 peter_e@gmx.net 394 :UBC 0 : fputc('\000', fout);
5406 peter_e@gmx.net 395 [ + - ]:CBC 1819156 : else if (sep.separator)
396 : 1819156 : fputs(sep.separator, fout);
397 : 1819156 : }
398 : :
399 : :
400 : : /*
401 : : * Return the list of explicitly-requested footers or, when applicable, the
402 : : * default "(xx rows)" footer. Always omit the default footer when given
403 : : * non-default footers, "\pset footer off", or a specific instruction to that
404 : : * effect from a calling backslash command. Vertical formats number each row,
405 : : * making the default footer redundant; they do not call this function.
406 : : *
407 : : * The return value may point to static storage; do not keep it across calls.
408 : : */
409 : : static printTableFooter *
5324 rhaas@postgresql.org 410 : 101404 : footers_with_default(const printTableContent *cont)
411 : : {
412 [ + + + + ]: 101404 : if (cont->footers == NULL && cont->opt->default_footer)
413 : : {
414 : : unsigned long total_records;
415 : :
416 : 98448 : total_records = cont->opt->prior_records + cont->nrows;
417 : 98448 : snprintf(default_footer, sizeof(default_footer),
418 : 98448 : ngettext("(%lu row)", "(%lu rows)", total_records),
419 : : total_records);
420 : :
421 : 98448 : return &default_footer_cell;
422 : : }
423 : : else
424 : 2956 : return cont->footers;
425 : : }
426 : :
427 : :
428 : : /*************************/
429 : : /* Unaligned text */
430 : : /*************************/
431 : :
432 : :
433 : : static void
6774 alvherre@alvh.no-ip. 434 : 10773 : print_unaligned_text(const printTableContent *cont, FILE *fout)
435 : : {
436 : 10773 : bool opt_tuples_only = cont->opt->tuples_only;
437 : : unsigned int i;
438 : : const char *const *ptr;
9726 bruce@momjian.us 439 : 10773 : bool need_recordsep = false;
440 : :
9 peter@eisentraut.org 441 [ - + ]:GNC 10773 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 442 :UBC 0 : return;
443 : :
6774 alvherre@alvh.no-ip. 444 [ + - ]:CBC 10773 : if (cont->opt->start_table)
445 : : {
446 : : /* print title */
447 [ + + + + ]: 10773 : if (!opt_tuples_only && cont->title)
448 : : {
5406 peter_e@gmx.net 449 : 5 : fputs(cont->title, fout);
450 : 5 : print_separator(cont->opt->recordSep, fout);
451 : : }
452 : :
453 : : /* print headers */
7807 bruce@momjian.us 454 [ + + ]: 10773 : if (!opt_tuples_only)
455 : : {
6774 alvherre@alvh.no-ip. 456 [ + + ]: 282 : for (ptr = cont->headers; *ptr; ptr++)
457 : : {
458 [ + + ]: 191 : if (ptr != cont->headers)
5406 peter_e@gmx.net 459 : 100 : print_separator(cont->opt->fieldSep, fout);
7396 tgl@sss.pgh.pa.us 460 : 191 : fputs(*ptr, fout);
461 : : }
462 : 91 : need_recordsep = true;
463 : : }
464 : : }
465 : : else
466 : : /* assume continuing printout */
9726 bruce@momjian.us 467 :UBC 0 : need_recordsep = true;
468 : :
469 : : /* print cells */
6774 alvherre@alvh.no-ip. 470 [ + + ]:CBC 1837940 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
471 : : {
9726 bruce@momjian.us 472 [ + + ]: 1827167 : if (need_recordsep)
473 : : {
5406 peter_e@gmx.net 474 : 878206 : print_separator(cont->opt->recordSep, fout);
9726 bruce@momjian.us 475 : 878206 : need_recordsep = false;
9 peter@eisentraut.org 476 [ - + ]:GNC 878206 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 477 :UBC 0 : break;
478 : : }
6116 heikki.linnakangas@i 479 :CBC 1827167 : fputs(*ptr, fout);
480 : :
6774 alvherre@alvh.no-ip. 481 [ + + ]: 1827167 : if ((i + 1) % cont->ncolumns)
5406 peter_e@gmx.net 482 : 938627 : print_separator(cont->opt->fieldSep, fout);
483 : : else
9726 bruce@momjian.us 484 : 888540 : need_recordsep = true;
485 : : }
486 : :
487 : : /* print footers */
6774 alvherre@alvh.no-ip. 488 [ + - ]: 10773 : if (cont->opt->stop_table)
489 : : {
5324 rhaas@postgresql.org 490 : 10773 : printTableFooter *footers = footers_with_default(cont);
491 : :
9 peter@eisentraut.org 492 [ + + + - :GNC 10773 : if (!opt_tuples_only && footers != NULL && !CancelRequested)
+ - ]
493 : : {
494 : : printTableFooter *f;
495 : :
5324 rhaas@postgresql.org 496 [ + + ]:CBC 183 : for (f = footers; f; f = f->next)
497 : : {
7396 tgl@sss.pgh.pa.us 498 [ + - ]: 92 : if (need_recordsep)
499 : : {
5406 peter_e@gmx.net 500 : 92 : print_separator(cont->opt->recordSep, fout);
7396 tgl@sss.pgh.pa.us 501 : 92 : need_recordsep = false;
502 : : }
6774 alvherre@alvh.no-ip. 503 : 92 : fputs(f->data, fout);
7396 tgl@sss.pgh.pa.us 504 : 92 : need_recordsep = true;
505 : : }
506 : : }
507 : :
508 : : /*
509 : : * The last record is terminated by a newline, independent of the set
510 : : * record separator. But when the record separator is a zero byte, we
511 : : * use that (compatible with find -print0 and xargs).
512 : : */
513 [ + + ]: 10773 : if (need_recordsep)
514 : : {
5406 peter_e@gmx.net 515 [ - + ]: 10425 : if (cont->opt->recordSep.separator_zero)
5406 peter_e@gmx.net 516 :UBC 0 : print_separator(cont->opt->recordSep, fout);
517 : : else
5406 peter_e@gmx.net 518 :CBC 10425 : fputc('\n', fout);
519 : : }
520 : : }
521 : : }
522 : :
523 : :
524 : : static void
6774 alvherre@alvh.no-ip. 525 : 69 : print_unaligned_vertical(const printTableContent *cont, FILE *fout)
526 : : {
527 : 69 : bool opt_tuples_only = cont->opt->tuples_only;
528 : : unsigned int i;
529 : : const char *const *ptr;
7396 tgl@sss.pgh.pa.us 530 : 69 : bool need_recordsep = false;
531 : :
9 peter@eisentraut.org 532 [ - + ]:GNC 69 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 533 :UBC 0 : return;
534 : :
6774 alvherre@alvh.no-ip. 535 [ + - ]:CBC 69 : if (cont->opt->start_table)
536 : : {
537 : : /* print title */
538 [ + + + + ]: 69 : if (!opt_tuples_only && cont->title)
539 : : {
540 : 4 : fputs(cont->title, fout);
7396 tgl@sss.pgh.pa.us 541 : 4 : need_recordsep = true;
542 : : }
543 : : }
544 : : else
545 : : /* assume continuing printout */
7396 tgl@sss.pgh.pa.us 546 :UBC 0 : need_recordsep = true;
547 : :
548 : : /* print records */
6774 alvherre@alvh.no-ip. 549 [ + + ]:CBC 973 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
550 : : {
7396 tgl@sss.pgh.pa.us 551 [ + + ]: 904 : if (need_recordsep)
552 : : {
553 : : /* record separator is 2 occurrences of recordsep in this mode */
5406 peter_e@gmx.net 554 : 375 : print_separator(cont->opt->recordSep, fout);
555 : 375 : print_separator(cont->opt->recordSep, fout);
7396 tgl@sss.pgh.pa.us 556 : 375 : need_recordsep = false;
9 peter@eisentraut.org 557 [ - + ]:GNC 375 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 558 :UBC 0 : break;
559 : : }
560 : :
6774 alvherre@alvh.no-ip. 561 :CBC 904 : fputs(cont->headers[i % cont->ncolumns], fout);
5406 peter_e@gmx.net 562 : 904 : print_separator(cont->opt->fieldSep, fout);
6116 heikki.linnakangas@i 563 : 904 : fputs(*ptr, fout);
564 : :
6774 alvherre@alvh.no-ip. 565 [ + + ]: 904 : if ((i + 1) % cont->ncolumns)
5406 peter_e@gmx.net 566 : 464 : print_separator(cont->opt->recordSep, fout);
567 : : else
7396 tgl@sss.pgh.pa.us 568 : 440 : need_recordsep = true;
569 : : }
570 : :
6774 alvherre@alvh.no-ip. 571 [ + - ]: 69 : if (cont->opt->stop_table)
572 : : {
573 : : /* print footers */
9 peter@eisentraut.org 574 [ + + + + :GNC 69 : if (!opt_tuples_only && cont->footers != NULL && !CancelRequested)
+ - ]
575 : : {
576 : : printTableFooter *f;
577 : :
5406 peter_e@gmx.net 578 :CBC 4 : print_separator(cont->opt->recordSep, fout);
6774 alvherre@alvh.no-ip. 579 [ + + ]: 8 : for (f = cont->footers; f; f = f->next)
580 : : {
5406 peter_e@gmx.net 581 : 4 : print_separator(cont->opt->recordSep, fout);
6774 alvherre@alvh.no-ip. 582 : 4 : fputs(f->data, fout);
583 : : }
584 : : }
585 : :
586 : : /* see above in print_unaligned_text() */
5040 peter_e@gmx.net 587 [ + - ]: 69 : if (need_recordsep)
588 : : {
589 [ - + ]: 69 : if (cont->opt->recordSep.separator_zero)
5040 peter_e@gmx.net 590 :UBC 0 : print_separator(cont->opt->recordSep, fout);
591 : : else
5040 peter_e@gmx.net 592 :CBC 69 : fputc('\n', fout);
593 : : }
594 : : }
595 : : }
596 : :
597 : :
598 : : /********************/
599 : : /* Aligned text */
600 : : /********************/
601 : :
602 : :
603 : : /* draw "line" */
604 : : static void
109 peter@eisentraut.org 605 :GNC 90425 : _print_horizontal_line(unsigned int ncolumns, const unsigned int *widths,
606 : : unsigned short border, printTextRule pos,
607 : : const printTextFormat *format,
608 : : FILE *fout)
609 : : {
6255 tgl@sss.pgh.pa.us 610 :CBC 90425 : const printTextLineFormat *lformat = &format->lrule[pos];
611 : : unsigned int i,
612 : : j;
613 : :
9886 bruce@momjian.us 614 [ + + ]: 90425 : if (border == 1)
6255 tgl@sss.pgh.pa.us 615 : 90297 : fputs(lformat->hrule, fout);
9886 bruce@momjian.us 616 [ + + ]: 128 : else if (border == 2)
6255 tgl@sss.pgh.pa.us 617 : 96 : fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
618 : :
6774 alvherre@alvh.no-ip. 619 [ + + ]: 264296 : for (i = 0; i < ncolumns; i++)
620 : : {
9886 bruce@momjian.us 621 [ + + ]: 2806669 : for (j = 0; j < widths[i]; j++)
6255 tgl@sss.pgh.pa.us 622 : 2632798 : fputs(lformat->hrule, fout);
623 : :
6774 alvherre@alvh.no-ip. 624 [ + + ]: 173871 : if (i < ncolumns - 1)
625 : : {
9886 bruce@momjian.us 626 [ + + ]: 83586 : if (border == 0)
627 : 32 : fputc(' ', fout);
628 : : else
6255 tgl@sss.pgh.pa.us 629 : 83554 : fprintf(fout, "%s%s%s", lformat->hrule,
630 : 83554 : lformat->midvrule, lformat->hrule);
631 : : }
632 : : }
633 : :
9886 bruce@momjian.us 634 [ + + ]: 90425 : if (border == 2)
6255 tgl@sss.pgh.pa.us 635 : 96 : fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
9886 bruce@momjian.us 636 [ + + ]: 90329 : else if (border == 1)
6255 tgl@sss.pgh.pa.us 637 : 90297 : fputs(lformat->hrule, fout);
638 : :
9886 bruce@momjian.us 639 : 90425 : fputc('\n', fout);
640 : 90425 : }
641 : :
642 : :
643 : : /*
644 : : * Print pretty boxes around cells.
645 : : */
646 : : static void
4014 tgl@sss.pgh.pa.us 647 : 90557 : print_aligned_text(const printTableContent *cont, FILE *fout, bool is_pager)
648 : : {
6774 alvherre@alvh.no-ip. 649 : 90557 : bool opt_tuples_only = cont->opt->tuples_only;
650 : 90557 : int encoding = cont->opt->encoding;
651 : 90557 : unsigned short opt_border = cont->opt->border;
6255 tgl@sss.pgh.pa.us 652 : 90557 : const printTextFormat *format = get_line_style(cont->opt);
653 : 90557 : const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
654 : :
6379 bruce@momjian.us 655 : 90557 : unsigned int col_count = 0,
656 : 90557 : cell_count = 0;
657 : :
658 : : unsigned int i,
659 : : j;
660 : :
661 : : unsigned int *width_header,
662 : : *max_width,
663 : : *width_wrap,
664 : : *width_average;
665 : : unsigned int *max_nl_lines, /* value split by newlines */
666 : : *curr_nl_lines,
667 : : *max_bytes;
668 : : unsigned char **format_buf;
669 : : unsigned int width_total;
670 : : unsigned int total_header_width;
671 : :
672 : : const char *const *ptr;
673 : :
674 : : struct lineptr **col_lineptrs; /* pointers to line pointer per column */
675 : :
676 : : bool *header_done; /* Have all header lines been output? */
677 : : int *bytes_output; /* Bytes output for column value */
678 : : printTextLineWrap *wrap; /* Wrap status for each column */
3447 tgl@sss.pgh.pa.us 679 : 90557 : int output_columns = 0; /* Width of interactive console */
4014 680 : 90557 : bool is_local_pager = false;
681 : :
9 peter@eisentraut.org 682 [ - + ]:GNC 90557 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 683 :UBC 0 : return;
684 : :
7396 tgl@sss.pgh.pa.us 685 [ - + ]:CBC 90557 : if (opt_border > 2)
7396 tgl@sss.pgh.pa.us 686 :UBC 0 : opt_border = 2;
687 : :
6774 alvherre@alvh.no-ip. 688 [ + + ]:CBC 90557 : if (cont->ncolumns > 0)
689 : : {
690 : 90417 : col_count = cont->ncolumns;
277 michael@paquier.xyz 691 : 90417 : width_header = pg_malloc0_array(unsigned int, col_count);
692 : 90417 : width_average = pg_malloc0_array(unsigned int, col_count);
693 : 90417 : max_width = pg_malloc0_array(unsigned int, col_count);
694 : 90417 : width_wrap = pg_malloc0_array(unsigned int, col_count);
695 : 90417 : max_nl_lines = pg_malloc0_array(unsigned int, col_count);
81 peter@eisentraut.org 696 :GNC 90417 : curr_nl_lines = pg_malloc0_array(unsigned int, col_count);
277 michael@paquier.xyz 697 :CBC 90417 : col_lineptrs = pg_malloc0_array(struct lineptr *, col_count);
698 : 90417 : max_bytes = pg_malloc0_array(unsigned int, col_count);
699 : 90417 : format_buf = pg_malloc0_array(unsigned char *, col_count);
700 : 90417 : header_done = pg_malloc0_array(bool, col_count);
701 : 90417 : bytes_output = pg_malloc0_array(int, col_count);
702 : 90417 : wrap = pg_malloc0_array(printTextLineWrap, col_count);
703 : : }
704 : : else
705 : : {
6778 bruce@momjian.us 706 : 140 : width_header = NULL;
707 : 140 : width_average = NULL;
708 : 140 : max_width = NULL;
709 : 140 : width_wrap = NULL;
710 : 140 : max_nl_lines = NULL;
81 peter@eisentraut.org 711 :GNC 140 : curr_nl_lines = NULL;
7596 bruce@momjian.us 712 :CBC 140 : col_lineptrs = NULL;
6778 713 : 140 : max_bytes = NULL;
7596 714 : 140 : format_buf = NULL;
6778 715 : 140 : header_done = NULL;
716 : 140 : bytes_output = NULL;
6215 tgl@sss.pgh.pa.us 717 : 140 : wrap = NULL;
718 : : }
719 : :
720 : : /* scan all column headers, find maximum width and max max_nl_lines */
9165 bruce@momjian.us 721 [ + + ]: 264808 : for (i = 0; i < col_count; i++)
722 : : {
723 : : int width,
724 : : nl_lines,
725 : : bytes_required;
726 : :
5557 peter_e@gmx.net 727 : 174251 : pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
728 : : encoding, &width, &nl_lines, &bytes_required);
6778 bruce@momjian.us 729 [ + + ]: 174251 : if (width > max_width[i])
730 : 174227 : max_width[i] = width;
731 [ + - ]: 174251 : if (nl_lines > max_nl_lines[i])
732 : 174251 : max_nl_lines[i] = nl_lines;
733 [ + - ]: 174251 : if (bytes_required > max_bytes[i])
734 : 174251 : max_bytes[i] = bytes_required;
735 : :
736 : 174251 : width_header[i] = width;
737 : : }
738 : :
739 : : /* scan all cells, find maximum width, compute cell_count */
417 tgl@sss.pgh.pa.us 740 [ + + ]: 847378 : for (i = 0, ptr = cont->cells; *ptr; ptr++, cell_count++)
741 : : {
742 : : int width,
743 : : nl_lines,
744 : : bytes_required;
745 : :
5557 peter_e@gmx.net 746 : 756821 : pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
747 : : &width, &nl_lines, &bytes_required);
748 : :
417 tgl@sss.pgh.pa.us 749 [ + + ]: 756821 : if (width > max_width[i])
750 : 98614 : max_width[i] = width;
751 [ + + ]: 756821 : if (nl_lines > max_nl_lines[i])
752 : 1214 : max_nl_lines[i] = nl_lines;
753 [ + + ]: 756821 : if (bytes_required > max_bytes[i])
754 : 98992 : max_bytes[i] = bytes_required;
755 : :
756 : 756821 : width_average[i] += width;
757 : :
758 : : /* i is the current column number: increment with wrap */
759 [ + + ]: 756821 : if (++i >= col_count)
760 : 334641 : i = 0;
761 : : }
762 : :
763 : : /* If we have rows, compute average */
6778 bruce@momjian.us 764 [ + + + + ]: 90557 : if (col_count != 0 && cell_count != 0)
765 : : {
6379 766 : 85805 : int rows = cell_count / col_count;
767 : :
6778 768 [ + + ]: 248584 : for (i = 0; i < col_count; i++)
6769 tgl@sss.pgh.pa.us 769 : 162779 : width_average[i] /= rows;
770 : : }
771 : :
772 : : /* adjust the total display width based on border style */
9886 bruce@momjian.us 773 [ + + ]: 90557 : if (opt_border == 0)
6215 tgl@sss.pgh.pa.us 774 : 32 : width_total = col_count;
9886 bruce@momjian.us 775 [ + + ]: 90525 : else if (opt_border == 1)
4267 776 [ + + ]: 90493 : width_total = col_count * 3 - ((col_count > 0) ? 1 : 0);
777 : : else
6778 778 : 32 : width_total = col_count * 3 + 1;
779 : 90557 : total_header_width = width_total;
780 : :
9886 781 [ + + ]: 264808 : for (i = 0; i < col_count; i++)
782 : : {
6778 783 : 174251 : width_total += max_width[i];
784 : 174251 : total_header_width += width_header[i];
785 : : }
786 : :
787 : : /*
788 : : * At this point: max_width[] contains the max width of each column,
789 : : * max_nl_lines[] contains the max number of lines in each column,
790 : : * max_bytes[] contains the maximum storage space for formatting strings,
791 : : * width_total contains the giant width sum. Now we allocate some memory
792 : : * for line pointers.
793 : : */
794 [ + + ]: 264808 : for (i = 0; i < col_count; i++)
795 : : {
796 : : /* Add entry for ptr == NULL array termination */
277 michael@paquier.xyz 797 : 174251 : col_lineptrs[i] = pg_malloc0_array(struct lineptr,
798 : : (max_nl_lines[i] + 1));
799 : :
800 : 174251 : format_buf[i] = pg_malloc_array(unsigned char, (max_bytes[i] + 1));
801 : :
6778 bruce@momjian.us 802 : 174251 : col_lineptrs[i]->ptr = format_buf[i];
803 : : }
804 : :
805 : : /* Default word wrap to the full width, i.e. no word wrap */
806 [ + + ]: 264808 : for (i = 0; i < col_count; i++)
807 : 174251 : width_wrap[i] = max_width[i];
808 : :
809 : : /*
810 : : * Choose target output width: \pset columns, or $COLUMNS, or ioctl
811 : : */
6770 812 [ + + ]: 90557 : if (cont->opt->columns > 0)
813 : 900 : output_columns = cont->opt->columns;
814 [ + - + - : 89657 : else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
+ + ]
815 : : {
816 [ - + ]: 68 : if (cont->opt->env_columns > 0)
6770 bruce@momjian.us 817 :UBC 0 : output_columns = cont->opt->env_columns;
818 : : #ifdef TIOCGWINSZ
819 : : else
820 : : {
821 : : struct winsize screen_size;
822 : :
6770 bruce@momjian.us 823 [ - + ]:CBC 68 : if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
6770 bruce@momjian.us 824 :UBC 0 : output_columns = screen_size.ws_col;
825 : : }
826 : : #endif
827 : : }
828 : :
6770 bruce@momjian.us 829 [ + + ]:CBC 90557 : if (cont->opt->format == PRINT_WRAPPED)
830 : : {
831 : : /*
832 : : * Optional optimized word wrap. Shrink columns with a high max/avg
833 : : * ratio. Slightly bias against wider columns. (Increases chance a
834 : : * narrow column will fit in its cell.) If available columns is
835 : : * positive... and greater than the width of the unshrinkable column
836 : : * headers
837 : : */
6778 838 [ + - + + ]: 96 : if (output_columns > 0 && output_columns >= total_header_width)
839 : : {
840 : : /* While there is still excess width... */
841 [ + + ]: 176 : while (width_total > output_columns)
842 : : {
843 : 128 : double max_ratio = 0;
844 : 128 : int worst_col = -1;
845 : :
846 : : /*
847 : : * Find column that has the highest ratio of its maximum width
848 : : * compared to its average width. This tells us which column
849 : : * will produce the fewest wrapped values if shortened.
850 : : * width_wrap starts as equal to max_width.
851 : : */
852 [ + + ]: 384 : for (i = 0; i < col_count; i++)
853 : : {
854 [ + - + - ]: 256 : if (width_average[i] && width_wrap[i] > width_header[i])
855 : : {
856 : : /* Penalize wide columns by 1% of their width */
857 : : double ratio;
858 : :
6776 tgl@sss.pgh.pa.us 859 : 256 : ratio = (double) width_wrap[i] / width_average[i] +
860 : 256 : max_width[i] * 0.01;
6778 bruce@momjian.us 861 [ + + ]: 256 : if (ratio > max_ratio)
862 : : {
863 : 168 : max_ratio = ratio;
864 : 168 : worst_col = i;
865 : : }
866 : : }
867 : : }
868 : :
869 : : /* Exit loop if we can't squeeze any more. */
870 [ - + ]: 128 : if (worst_col == -1)
6778 bruce@momjian.us 871 :UBC 0 : break;
872 : :
873 : : /* Decrease width of target column by one. */
6778 bruce@momjian.us 874 :CBC 128 : width_wrap[worst_col]--;
875 : 128 : width_total--;
876 : : }
877 : : }
878 : : }
879 : :
880 : : /*
881 : : * If in expanded auto mode, we have now calculated the expected width, so
882 : : * we can now escape to vertical mode if necessary. If the output has
883 : : * only one column, the expanded format would be wider than the regular
884 : : * format, so don't use it in that case.
885 : : */
3914 rhaas@postgresql.org 886 [ - + - - : 90557 : if (cont->opt->expanded == 2 && output_columns > 0 && cont->ncolumns > 1 &&
- - ]
5495 peter_e@gmx.net 887 [ # # # # ]:UBC 0 : (output_columns < total_header_width || output_columns < width_total))
888 : : {
4014 tgl@sss.pgh.pa.us 889 : 0 : print_aligned_vertical(cont, fout, is_pager);
5379 peter_e@gmx.net 890 : 0 : goto cleanup;
891 : : }
892 : :
893 : : /* If we wrapped beyond the display width, use the pager */
6769 bruce@momjian.us 894 [ + + + + :CBC 90557 : if (!is_pager && fout == stdout && output_columns > 0 &&
+ + ]
6770 895 [ + + + + ]: 852 : (output_columns < total_header_width || output_columns < width_total))
896 : : {
4263 andrew@dunslane.net 897 : 408 : fout = PageOutput(INT_MAX, cont->opt); /* force pager */
4014 tgl@sss.pgh.pa.us 898 : 408 : is_pager = is_local_pager = true;
899 : : }
900 : :
901 : : /* Check if there are enough lines to require the pager */
417 902 [ + + ]: 90557 : if (!is_pager)
903 : : {
904 : 90065 : IsPagerNeeded(cont, width_wrap, false, &fout, &is_pager);
4014 905 : 90065 : is_local_pager = is_pager;
906 : : }
907 : :
908 : : /* time to output */
6774 alvherre@alvh.no-ip. 909 [ + + ]: 90557 : if (cont->opt->start_table)
910 : : {
911 : : /* print title */
912 [ + + + + ]: 90509 : if (cont->title && !opt_tuples_only)
913 : : {
914 : : int width,
915 : : height;
916 : :
5557 peter_e@gmx.net 917 : 3969 : pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
918 : : encoding, &width, &height, NULL);
6778 bruce@momjian.us 919 [ + + ]: 3969 : if (width >= width_total)
920 : : /* Aligned */
6774 alvherre@alvh.no-ip. 921 : 174 : fprintf(fout, "%s\n", cont->title);
922 : : else
923 : : /* Centered */
924 : 3795 : fprintf(fout, "%-*s%s\n", (width_total - width) / 2, "",
925 : 3795 : cont->title);
926 : : }
927 : :
928 : : /* print headers */
7396 tgl@sss.pgh.pa.us 929 [ + + ]: 90509 : if (!opt_tuples_only)
930 : : {
931 : : int more_col_wrapping;
932 : : int curr_nl_line;
933 : :
7596 bruce@momjian.us 934 [ + + ]: 90361 : if (opt_border == 2)
6255 tgl@sss.pgh.pa.us 935 : 32 : _print_horizontal_line(col_count, width_wrap, opt_border,
936 : : PRINT_RULE_TOP, format, fout);
937 : :
7596 bruce@momjian.us 938 [ + + ]: 264104 : for (i = 0; i < col_count; i++)
5557 peter_e@gmx.net 939 : 173743 : pg_wcsformat((const unsigned char *) cont->headers[i],
6774 alvherre@alvh.no-ip. 940 : 173743 : strlen(cont->headers[i]), encoding,
941 : 173743 : col_lineptrs[i], max_nl_lines[i]);
942 : :
6778 bruce@momjian.us 943 : 90361 : more_col_wrapping = col_count;
944 : 90361 : curr_nl_line = 0;
1731 tgl@sss.pgh.pa.us 945 [ + + ]: 90361 : if (col_count > 0)
946 : 90221 : memset(header_done, false, col_count * sizeof(bool));
6778 bruce@momjian.us 947 [ + + ]: 180678 : while (more_col_wrapping)
948 : : {
7396 tgl@sss.pgh.pa.us 949 [ + + ]: 90317 : if (opt_border == 2)
6215 950 : 64 : fputs(dformat->leftvrule, fout);
951 : :
6774 alvherre@alvh.no-ip. 952 [ + + ]: 264252 : for (i = 0; i < cont->ncolumns; i++)
953 : : {
6778 bruce@momjian.us 954 : 173935 : struct lineptr *this_line = col_lineptrs[i] + curr_nl_line;
955 : : unsigned int nbspace;
956 : :
6215 tgl@sss.pgh.pa.us 957 [ + + ]: 173935 : if (opt_border != 0 ||
5858 rhaas@postgresql.org 958 [ + + + + ]: 128 : (!format->wrap_right_border && i > 0))
6215 tgl@sss.pgh.pa.us 959 [ + + ]: 173839 : fputs(curr_nl_line ? format->header_nl_left : " ",
960 : : fout);
961 : :
6778 bruce@momjian.us 962 [ + + ]: 173935 : if (!header_done[i])
963 : : {
964 : 173887 : nbspace = width_wrap[i] - this_line->width;
965 : :
966 : : /* centered */
7396 tgl@sss.pgh.pa.us 967 : 173887 : fprintf(fout, "%-*s%s%-*s",
968 : 173887 : nbspace / 2, "", this_line->ptr, (nbspace + 1) / 2, "");
969 : :
6778 bruce@momjian.us 970 [ + + ]: 173887 : if (!(this_line + 1)->ptr)
971 : : {
972 : 173743 : more_col_wrapping--;
973 : 173743 : header_done[i] = 1;
974 : : }
975 : : }
976 : : else
977 : 48 : fprintf(fout, "%*s", width_wrap[i], "");
978 : :
5858 rhaas@postgresql.org 979 [ + + + + ]: 173935 : if (opt_border != 0 || format->wrap_right_border)
6215 tgl@sss.pgh.pa.us 980 [ + + ]: 173871 : fputs(!header_done[i] ? format->header_nl_right : " ",
981 : : fout);
982 : :
4267 bruce@momjian.us 983 [ + + + - : 173935 : if (opt_border != 0 && col_count > 0 && i < col_count - 1)
+ + ]
6215 tgl@sss.pgh.pa.us 984 : 83554 : fputs(dformat->midvrule, fout);
985 : : }
6778 bruce@momjian.us 986 : 90317 : curr_nl_line++;
987 : :
7396 tgl@sss.pgh.pa.us 988 [ + + ]: 90317 : if (opt_border == 2)
6215 989 : 64 : fputs(dformat->rightvrule, fout);
7396 990 : 90317 : fputc('\n', fout);
991 : : }
992 : :
6255 993 : 90361 : _print_horizontal_line(col_count, width_wrap, opt_border,
994 : : PRINT_RULE_MIDDLE, format, fout);
995 : : }
996 : : }
997 : :
998 : : /* print cells, one loop per row */
6774 alvherre@alvh.no-ip. 999 [ + + ]: 425198 : for (i = 0, ptr = cont->cells; *ptr; i += col_count, ptr += col_count)
1000 : : {
1001 : : bool more_lines;
1002 : :
9 peter@eisentraut.org 1003 [ - + ]:GNC 334641 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 1004 :UBC 0 : break;
1005 : :
1006 : : /*
1007 : : * Format each cell.
1008 : : */
7596 bruce@momjian.us 1009 [ + + ]:CBC 1091462 : for (j = 0; j < col_count; j++)
1010 : : {
5557 peter_e@gmx.net 1011 : 756821 : pg_wcsformat((const unsigned char *) ptr[j], strlen(ptr[j]), encoding,
6776 tgl@sss.pgh.pa.us 1012 : 756821 : col_lineptrs[j], max_nl_lines[j]);
81 peter@eisentraut.org 1013 :GNC 756821 : curr_nl_lines[j] = 0;
1014 : : }
1015 : :
6778 bruce@momjian.us 1016 :CBC 334641 : memset(bytes_output, 0, col_count * sizeof(int));
1017 : :
1018 : : /*
1019 : : * Each time through this loop, one display line is output. It can
1020 : : * either be a full value or a partial value if embedded newlines
1021 : : * exist or if 'format=wrapping' mode is enabled.
1022 : : */
1023 : : do
1024 : : {
1025 : 346526 : more_lines = false;
1026 : :
1027 : : /* left border */
9886 1028 [ + + ]: 346526 : if (opt_border == 2)
6215 tgl@sss.pgh.pa.us 1029 : 368 : fputs(dformat->leftvrule, fout);
1030 : :
1031 : : /* for each column */
7596 bruce@momjian.us 1032 [ + + ]: 1117892 : for (j = 0; j < col_count; j++)
1033 : : {
1034 : : /* We have a valid array element, so index it */
81 peter@eisentraut.org 1035 :GNC 771366 : struct lineptr *this_line = &col_lineptrs[j][curr_nl_lines[j]];
1036 : : int bytes_to_output;
6379 bruce@momjian.us 1037 :CBC 771366 : int chars_to_output = width_wrap[j];
4267 1038 [ + + + - ]: 1541996 : bool finalspaces = (opt_border == 2 ||
3447 tgl@sss.pgh.pa.us 1039 [ + + ]: 770630 : (col_count > 0 && j < col_count - 1));
1040 : :
1041 : : /* Print left-hand wrap or newline mark */
6215 1042 [ + + ]: 771366 : if (opt_border != 0)
1043 : : {
1044 [ + + ]: 770726 : if (wrap[j] == PRINT_LINE_WRAP_WRAP)
1045 : 80 : fputs(format->wrap_left, fout);
1046 [ + + ]: 770646 : else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1047 : 11937 : fputs(format->nl_left, fout);
1048 : : else
1049 : 758709 : fputc(' ', fout);
1050 : : }
1051 : :
6778 bruce@momjian.us 1052 [ + + ]: 771366 : if (!this_line->ptr)
1053 : : {
1054 : : /* Past newline lines so just pad for other columns */
6776 tgl@sss.pgh.pa.us 1055 [ + + ]: 2272 : if (finalspaces)
1056 : 1945 : fprintf(fout, "%*s", chars_to_output, "");
1057 : : }
1058 : : else
1059 : : {
1060 : : /* Get strlen() of the characters up to width_wrap */
1061 : : bytes_to_output =
1062 : 769094 : strlen_max_width(this_line->ptr + bytes_output[j],
1063 : : &chars_to_output, encoding);
1064 : :
1065 : : /*
1066 : : * If we exceeded width_wrap, it means the display width
1067 : : * of a single character was wider than our target width.
1068 : : * In that case, we have to pretend we are only printing
1069 : : * the target display width and make the best of it.
1070 : : */
6778 bruce@momjian.us 1071 [ - + ]: 769094 : if (chars_to_output > width_wrap[j])
6778 bruce@momjian.us 1072 :UBC 0 : chars_to_output = width_wrap[j];
1073 : :
6379 bruce@momjian.us 1074 [ + + ]:CBC 769094 : if (cont->aligns[j] == 'r') /* Right aligned cell */
1075 : : {
1076 : : /* spaces first */
6778 1077 : 301684 : fprintf(fout, "%*s", width_wrap[j] - chars_to_output, "");
2343 tgl@sss.pgh.pa.us 1078 : 301684 : fwrite((char *) (this_line->ptr + bytes_output[j]),
1079 : : 1, bytes_to_output, fout);
1080 : : }
1081 : : else /* Left aligned cell */
1082 : : {
1083 : : /* spaces second */
1084 : 467410 : fwrite((char *) (this_line->ptr + bytes_output[j]),
1085 : : 1, bytes_to_output, fout);
1086 : : }
1087 : :
6778 bruce@momjian.us 1088 : 769094 : bytes_output[j] += bytes_to_output;
1089 : :
1090 : : /* Do we have more text to wrap? */
6776 tgl@sss.pgh.pa.us 1091 [ + + ]: 769094 : if (*(this_line->ptr + bytes_output[j]) != '\0')
6778 bruce@momjian.us 1092 : 80 : more_lines = true;
1093 : : else
1094 : : {
1095 : : /* Advance to next newline line */
81 peter@eisentraut.org 1096 :GNC 769014 : curr_nl_lines[j]++;
1097 [ + + ]: 769014 : if (col_lineptrs[j][curr_nl_lines[j]].ptr != NULL)
6778 bruce@momjian.us 1098 :CBC 12193 : more_lines = true;
1099 : 769014 : bytes_output[j] = 0;
1100 : : }
1101 : : }
1102 : :
1103 : : /* Determine next line's wrap status for this column */
6215 tgl@sss.pgh.pa.us 1104 : 771366 : wrap[j] = PRINT_LINE_WRAP_NONE;
81 peter@eisentraut.org 1105 [ + + ]:GNC 771366 : if (col_lineptrs[j][curr_nl_lines[j]].ptr != NULL)
1106 : : {
6215 tgl@sss.pgh.pa.us 1107 [ + + ]:CBC 12273 : if (bytes_output[j] != 0)
1108 : 80 : wrap[j] = PRINT_LINE_WRAP_WRAP;
81 peter@eisentraut.org 1109 [ + - ]:GNC 12193 : else if (curr_nl_lines[j] != 0)
6215 tgl@sss.pgh.pa.us 1110 :CBC 12193 : wrap[j] = PRINT_LINE_WRAP_NEWLINE;
1111 : : }
1112 : :
1113 : : /*
1114 : : * If left-aligned, pad out remaining space if needed (not
1115 : : * last column, and/or wrap marks required).
1116 : : */
3447 1117 [ + + ]: 771366 : if (cont->aligns[j] != 'r') /* Left aligned cell */
1118 : : {
6215 1119 [ + + ]: 469398 : if (finalspaces ||
1120 [ + + ]: 250043 : wrap[j] == PRINT_LINE_WRAP_WRAP ||
6119 bruce@momjian.us 1121 [ + + ]: 250035 : wrap[j] == PRINT_LINE_WRAP_NEWLINE)
6215 tgl@sss.pgh.pa.us 1122 : 230737 : fprintf(fout, "%*s",
1123 : 230737 : width_wrap[j] - chars_to_output, "");
1124 : : }
1125 : :
1126 : : /* Print right-hand wrap or newline mark */
1127 [ + + ]: 771366 : if (wrap[j] == PRINT_LINE_WRAP_WRAP)
1128 : 80 : fputs(format->wrap_right, fout);
1129 [ + + ]: 771286 : else if (wrap[j] == PRINT_LINE_WRAP_NEWLINE)
1130 : 12193 : fputs(format->nl_right, fout);
4267 bruce@momjian.us 1131 [ + + + - : 759093 : else if (opt_border == 2 || (col_count > 0 && j < col_count - 1))
+ + ]
6215 tgl@sss.pgh.pa.us 1132 : 424317 : fputc(' ', fout);
1133 : :
1134 : : /* Print column divider, if not the last column */
4267 bruce@momjian.us 1135 [ + + + - : 771366 : if (opt_border != 0 && (col_count > 0 && j < col_count - 1))
+ + ]
1136 : : {
6119 1137 [ + + ]: 424520 : if (wrap[j + 1] == PRINT_LINE_WRAP_WRAP)
6215 tgl@sss.pgh.pa.us 1138 : 24 : fputs(format->midvrule_wrap, fout);
6119 bruce@momjian.us 1139 [ + + ]: 424496 : else if (wrap[j + 1] == PRINT_LINE_WRAP_NEWLINE)
6215 tgl@sss.pgh.pa.us 1140 : 850 : fputs(format->midvrule_nl, fout);
81 peter@eisentraut.org 1141 [ + + ]:GNC 423646 : else if (col_lineptrs[j + 1][curr_nl_lines[j + 1]].ptr == NULL)
6215 tgl@sss.pgh.pa.us 1142 :CBC 1658 : fputs(format->midvrule_blank, fout);
1143 : : else
1144 : 421988 : fputs(dformat->midvrule, fout);
1145 : : }
1146 : : }
1147 : :
1148 : : /* end-of-row border */
9886 bruce@momjian.us 1149 [ + + ]: 346526 : if (opt_border == 2)
6215 tgl@sss.pgh.pa.us 1150 : 368 : fputs(dformat->rightvrule, fout);
9886 bruce@momjian.us 1151 : 346526 : fputc('\n', fout);
6778 1152 [ + + ]: 346526 : } while (more_lines);
1153 : : }
1154 : :
6774 alvherre@alvh.no-ip. 1155 [ + + ]: 90557 : if (cont->opt->stop_table)
1156 : : {
5324 rhaas@postgresql.org 1157 : 90505 : printTableFooter *footers = footers_with_default(cont);
1158 : :
9 peter@eisentraut.org 1159 [ + + + - ]:GNC 90505 : if (opt_border == 2 && !CancelRequested)
6255 tgl@sss.pgh.pa.us 1160 :CBC 32 : _print_horizontal_line(col_count, width_wrap, opt_border,
1161 : : PRINT_RULE_BOTTOM, format, fout);
1162 : :
1163 : : /* print footers */
9 peter@eisentraut.org 1164 [ + + + + :GNC 90505 : if (footers && !opt_tuples_only && !CancelRequested)
+ - ]
1165 : : {
1166 : : printTableFooter *f;
1167 : :
5324 rhaas@postgresql.org 1168 [ + + ]:CBC 186270 : for (f = footers; f; f = f->next)
6774 alvherre@alvh.no-ip. 1169 : 96267 : fprintf(fout, "%s\n", f->data);
1170 : : }
1171 : :
7396 tgl@sss.pgh.pa.us 1172 : 90505 : fputc('\n', fout);
1173 : : }
1174 : :
5379 peter_e@gmx.net 1175 : 52 : cleanup:
1176 : : /* clean up */
6146 tgl@sss.pgh.pa.us 1177 [ + + ]: 264808 : for (i = 0; i < col_count; i++)
1178 : : {
150 peter@eisentraut.org 1179 :GNC 174251 : pg_free(col_lineptrs[i]);
1180 : 174251 : pg_free(format_buf[i]);
1181 : : }
1182 : 90557 : pg_free(width_header);
1183 : 90557 : pg_free(width_average);
1184 : 90557 : pg_free(max_width);
1185 : 90557 : pg_free(width_wrap);
1186 : 90557 : pg_free(max_nl_lines);
81 1187 : 90557 : pg_free(curr_nl_lines);
150 1188 : 90557 : pg_free(col_lineptrs);
1189 : 90557 : pg_free(max_bytes);
1190 : 90557 : pg_free(format_buf);
1191 : 90557 : pg_free(header_done);
1192 : 90557 : pg_free(bytes_output);
1193 : 90557 : pg_free(wrap);
1194 : :
4014 tgl@sss.pgh.pa.us 1195 [ + + ]:CBC 90557 : if (is_local_pager)
6770 bruce@momjian.us 1196 : 408 : ClosePager(fout);
1197 : : }
1198 : :
1199 : :
1200 : : static void
1587 andrew@dunslane.net 1201 : 1162 : print_aligned_vertical_line(const printTableOpt *topt,
1202 : : unsigned long record,
1203 : : unsigned int hwidth,
1204 : : unsigned int dwidth,
1205 : : int output_columns,
1206 : : printTextRule pos,
1207 : : FILE *fout)
1208 : : {
1209 : 1162 : const printTextLineFormat *lformat = &get_line_style(topt)->lrule[pos];
1210 : 1162 : const unsigned short opt_border = topt->border;
1211 : : unsigned int i;
6119 bruce@momjian.us 1212 : 1162 : int reclen = 0;
1213 : :
6255 tgl@sss.pgh.pa.us 1214 [ + + ]: 1162 : if (opt_border == 2)
1215 : 336 : fprintf(fout, "%s%s", lformat->leftvrule, lformat->hrule);
1216 [ + + ]: 826 : else if (opt_border == 1)
1217 : 554 : fputs(lformat->hrule, fout);
1218 : :
1219 [ + + ]: 1162 : if (record)
1220 : : {
1221 [ + + ]: 1102 : if (opt_border == 0)
1222 : 272 : reclen = fprintf(fout, "* Record %lu", record);
1223 : : else
1224 : 830 : reclen = fprintf(fout, "[ RECORD %lu ]", record);
1225 : : }
1226 [ + + ]: 1162 : if (opt_border != 2)
1227 : 826 : reclen++;
1228 [ - + ]: 1162 : if (reclen < 0)
6255 tgl@sss.pgh.pa.us 1229 :UBC 0 : reclen = 0;
6255 tgl@sss.pgh.pa.us 1230 [ + + ]:CBC 5765 : for (i = reclen; i < hwidth; i++)
1231 [ + + ]: 4603 : fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1232 : 1162 : reclen -= hwidth;
1233 : :
1234 [ + + ]: 1162 : if (opt_border > 0)
1235 : : {
1236 [ + + ]: 890 : if (reclen-- <= 0)
1237 : 733 : fputs(lformat->hrule, fout);
1238 [ + + ]: 890 : if (reclen-- <= 0)
1239 : : {
1587 andrew@dunslane.net 1240 [ - + ]: 737 : if (topt->expanded_header_width_type == PRINT_XHEADER_COLUMN)
1241 : : {
1587 andrew@dunslane.net 1242 :UBC 0 : fputs(lformat->rightvrule, fout);
1243 : : }
1244 : : else
1245 : : {
1587 andrew@dunslane.net 1246 :CBC 737 : fputs(lformat->midvrule, fout);
1247 : : }
1248 : : }
1249 [ + + ]: 890 : if (reclen-- <= 0
1250 [ + - ]: 753 : && topt->expanded_header_width_type != PRINT_XHEADER_COLUMN)
6255 tgl@sss.pgh.pa.us 1251 : 753 : fputs(lformat->hrule, fout);
1252 : : }
1253 : : else
1254 : : {
1255 [ + + ]: 272 : if (reclen-- <= 0)
1256 : 240 : fputc(' ', fout);
1257 : : }
1258 : :
1587 andrew@dunslane.net 1259 [ + - ]: 1162 : if (topt->expanded_header_width_type != PRINT_XHEADER_COLUMN)
1260 : : {
1261 [ + - ]: 1162 : if (topt->expanded_header_width_type == PRINT_XHEADER_PAGE
1262 [ - + ]: 1162 : || topt->expanded_header_width_type == PRINT_XHEADER_EXACT_WIDTH)
1263 : : {
1587 andrew@dunslane.net 1264 [ # # ]:UBC 0 : if (topt->expanded_header_width_type == PRINT_XHEADER_EXACT_WIDTH)
1265 : : {
1266 : 0 : output_columns = topt->expanded_header_exact_width;
1267 : : }
1268 [ # # ]: 0 : if (output_columns > 0)
1269 : : {
1270 [ # # ]: 0 : if (opt_border == 0)
1271 : 0 : dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth)));
1272 [ # # ]: 0 : if (opt_border == 1)
1273 : 0 : dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth - 3)));
1274 : :
1275 : : /*
1276 : : * Handling the xheader width for border=2 doesn't make much
1277 : : * sense because this format has an additional right border,
1278 : : * but keep this for consistency.
1279 : : */
1280 [ # # ]: 0 : if (opt_border == 2)
1281 : 0 : dwidth = Min(dwidth, Max(0, (int) (output_columns - hwidth - 7)));
1282 : : }
1283 : : }
1284 : :
1587 andrew@dunslane.net 1285 [ + + ]:CBC 1162 : if (reclen < 0)
1286 : 993 : reclen = 0;
1287 [ - + ]: 1162 : if (dwidth < reclen)
1587 andrew@dunslane.net 1288 :UBC 0 : dwidth = reclen;
1289 : :
1587 andrew@dunslane.net 1290 [ + + ]:CBC 44615 : for (i = reclen; i < dwidth; i++)
1291 [ + + ]: 43453 : fputs(opt_border > 0 ? lformat->hrule : " ", fout);
1292 [ + + ]: 1162 : if (opt_border == 2)
1293 : 336 : fprintf(fout, "%s%s", lformat->hrule, lformat->rightvrule);
1294 : : }
1295 : :
6255 tgl@sss.pgh.pa.us 1296 : 1162 : fputc('\n', fout);
1297 : 1162 : }
1298 : :
1299 : : static void
4014 1300 : 401 : print_aligned_vertical(const printTableContent *cont,
1301 : : FILE *fout, bool is_pager)
1302 : : {
6774 alvherre@alvh.no-ip. 1303 : 401 : bool opt_tuples_only = cont->opt->tuples_only;
1304 : 401 : unsigned short opt_border = cont->opt->border;
6255 tgl@sss.pgh.pa.us 1305 : 401 : const printTextFormat *format = get_line_style(cont->opt);
1306 : 401 : const printTextLineFormat *dformat = &format->lrule[PRINT_RULE_DATA];
6774 alvherre@alvh.no-ip. 1307 : 401 : int encoding = cont->opt->encoding;
1308 : 401 : unsigned long record = cont->opt->prior_records + 1;
1309 : : const char *const *ptr;
1310 : : unsigned int i,
9886 bruce@momjian.us 1311 : 401 : hwidth = 0,
7596 1312 : 401 : dwidth = 0,
1313 : 401 : hheight = 1,
1314 : 401 : dheight = 1,
1315 : 401 : hformatsize = 0,
1316 : 401 : dformatsize = 0;
1317 : : struct lineptr *hlineptr,
1318 : : *dlineptr;
4014 tgl@sss.pgh.pa.us 1319 : 401 : bool is_local_pager = false,
4485 stark@mit.edu 1320 : 401 : hmultiline = false,
1321 : 401 : dmultiline = false;
3447 tgl@sss.pgh.pa.us 1322 : 401 : int output_columns = 0; /* Width of interactive console */
1323 : :
9 peter@eisentraut.org 1324 [ - + ]:GNC 401 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 1325 :UBC 0 : return;
1326 : :
7396 tgl@sss.pgh.pa.us 1327 [ - + ]:CBC 401 : if (opt_border > 2)
7396 tgl@sss.pgh.pa.us 1328 :UBC 0 : opt_border = 2;
1329 : :
1330 : : /*
1331 : : * Kluge for totally empty table: use the default footer even though
1332 : : * vertical modes normally don't. Otherwise we'd print nothing at all,
1333 : : * which isn't terribly friendly. Assume pager will not be needed.
1334 : : */
6774 alvherre@alvh.no-ip. 1335 [ + + + + ]:CBC 401 : if (cont->cells[0] == NULL && cont->opt->start_table &&
1336 [ + - ]: 42 : cont->opt->stop_table)
1337 : : {
4267 bruce@momjian.us 1338 : 42 : printTableFooter *footers = footers_with_default(cont);
1339 : :
9 peter@eisentraut.org 1340 [ + - + - :GNC 42 : if (!opt_tuples_only && !CancelRequested && footers)
+ - ]
1341 : : {
1342 : : printTableFooter *f;
1343 : :
4267 bruce@momjian.us 1344 [ + + ]:CBC 84 : for (f = footers; f; f = f->next)
1345 : 42 : fprintf(fout, "%s\n", f->data);
1346 : : }
1347 : :
1348 : 42 : fputc('\n', fout);
1349 : :
9726 1350 : 42 : return;
1351 : : }
1352 : :
1353 : : /* Find the maximum dimensions for the headers */
6774 alvherre@alvh.no-ip. 1354 [ + + ]: 2093 : for (i = 0; i < cont->ncolumns; i++)
1355 : : {
1356 : : int width,
1357 : : height,
1358 : : fs;
1359 : :
5557 peter_e@gmx.net 1360 : 1734 : pg_wcssize((const unsigned char *) cont->headers[i], strlen(cont->headers[i]),
1361 : : encoding, &width, &height, &fs);
6778 bruce@momjian.us 1362 [ + + ]: 1734 : if (width > hwidth)
1363 : 640 : hwidth = width;
7596 1364 [ + + ]: 1734 : if (height > hheight)
1365 : : {
1366 : 48 : hheight = height;
4485 stark@mit.edu 1367 : 48 : hmultiline = true;
1368 : : }
7596 bruce@momjian.us 1369 [ + + ]: 1734 : if (fs > hformatsize)
1370 : 640 : hformatsize = fs;
1371 : : }
1372 : :
1373 : : /* find longest data cell */
1103 alvherre@alvh.no-ip. 1374 [ + + ]: 3922 : for (ptr = cont->cells; *ptr; ptr++)
1375 : : {
1376 : : int width,
1377 : : height,
1378 : : fs;
1379 : :
5557 peter_e@gmx.net 1380 : 3563 : pg_wcssize((const unsigned char *) *ptr, strlen(*ptr), encoding,
1381 : : &width, &height, &fs);
6778 bruce@momjian.us 1382 [ + + ]: 3563 : if (width > dwidth)
1383 : 747 : dwidth = width;
7596 1384 [ + + ]: 3563 : if (height > dheight)
1385 : : {
1386 : 68 : dheight = height;
4485 stark@mit.edu 1387 : 68 : dmultiline = true;
1388 : : }
7596 bruce@momjian.us 1389 [ + + ]: 3563 : if (fs > dformatsize)
1390 : 747 : dformatsize = fs;
1391 : : }
1392 : :
1393 : : /*
1394 : : * We now have all the information we need to setup the formatting
1395 : : * structures
1396 : : */
277 michael@paquier.xyz 1397 : 359 : dlineptr = pg_malloc_array(struct lineptr, (dheight + 1));
1398 : 359 : hlineptr = pg_malloc_array(struct lineptr, (hheight + 1));
1399 : :
5170 tgl@sss.pgh.pa.us 1400 : 359 : dlineptr->ptr = pg_malloc(dformatsize);
1401 : 359 : hlineptr->ptr = pg_malloc(hformatsize);
1402 : :
1403 : : /*
1404 : : * Choose target output width: \pset columns, or $COLUMNS, or ioctl
1405 : : */
4597 stark@mit.edu 1406 [ + + ]: 359 : if (cont->opt->columns > 0)
1407 : 160 : output_columns = cont->opt->columns;
1408 [ + - + + : 199 : else if ((fout == stdout && isatty(fileno(stdout))) || is_pager)
+ + ]
1409 : : {
1410 [ - + ]: 17 : if (cont->opt->env_columns > 0)
4597 stark@mit.edu 1411 :UBC 0 : output_columns = cont->opt->env_columns;
1412 : : #ifdef TIOCGWINSZ
1413 : : else
1414 : : {
1415 : : struct winsize screen_size;
1416 : :
4597 stark@mit.edu 1417 [ + + ]:CBC 17 : if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) != -1)
1418 : 1 : output_columns = screen_size.ws_col;
1419 : : }
1420 : : #endif
1421 : : }
1422 : :
1423 : : /*
1424 : : * Determine data column width: fit output width in wrapped mode, or
1425 : : * ensure alignment with the record header line in aligned mode.
1426 : : */
173 michael@paquier.xyz 1427 [ + + + - ]: 359 : if (cont->opt->format == PRINT_WRAPPED || cont->opt->format == PRINT_ALIGNED)
1428 : : {
1429 : : unsigned int swidth,
4016 tgl@sss.pgh.pa.us 1430 : 359 : rwidth = 0,
1431 : : newdwidth;
1432 : :
4597 stark@mit.edu 1433 [ + + ]: 359 : if (opt_border == 0)
1434 : : {
1435 : : /*
1436 : : * For border = 0, one space in the middle. (If we discover we
1437 : : * need to wrap, the spacer column will be replaced by a wrap
1438 : : * marker, and we'll make room below for another wrap marker at
1439 : : * the end of the line. But for now, assume no wrap is needed.)
1440 : : */
4485 1441 : 40 : swidth = 1;
1442 : :
1443 : : /* We might need a column for header newline markers, too */
4016 tgl@sss.pgh.pa.us 1444 [ + + ]: 40 : if (hmultiline)
1445 : 16 : swidth++;
1446 : : }
4597 stark@mit.edu 1447 [ + + ]: 319 : else if (opt_border == 1)
1448 : : {
1449 : : /*
1450 : : * For border = 1, two spaces and a vrule in the middle. (As
1451 : : * above, we might need one more column for a wrap marker.)
1452 : : */
4485 1453 : 271 : swidth = 3;
1454 : :
1455 : : /* We might need a column for left header newline markers, too */
4016 tgl@sss.pgh.pa.us 1456 [ + + + + ]: 271 : if (hmultiline && (format == &pg_asciiformat_old))
1457 : 8 : swidth++;
1458 : : }
1459 : : else
1460 : : {
1461 : : /*
1462 : : * For border = 2, two more for the vrules at the beginning and
1463 : : * end of the lines, plus spacer columns adjacent to these. (We
1464 : : * won't need extra columns for wrap/newline markers, we'll just
1465 : : * repurpose the spacers.)
1466 : : */
4485 stark@mit.edu 1467 : 48 : swidth = 7;
1468 : : }
1469 : :
1470 : : /* Reserve a column for data newline indicators, too, if needed */
4016 tgl@sss.pgh.pa.us 1471 [ + + + + ]: 359 : if (dmultiline &&
1472 [ + + ]: 48 : opt_border < 2 && format != &pg_asciiformat_old)
1473 : 32 : swidth++;
1474 : :
1475 : : /* Determine width required for record header lines */
4597 stark@mit.edu 1476 [ + + ]: 359 : if (!opt_tuples_only)
1477 : : {
4015 tgl@sss.pgh.pa.us 1478 [ + + ]: 347 : if (cont->nrows > 0)
1479 : 339 : rwidth = 1 + (int) log10(cont->nrows);
4597 stark@mit.edu 1480 [ + + ]: 347 : if (opt_border == 0)
4485 1481 : 40 : rwidth += 9; /* "* RECORD " */
4597 1482 [ + + ]: 307 : else if (opt_border == 1)
4485 1483 : 259 : rwidth += 12; /* "-[ RECORD ]" */
1484 : : else
1485 : 48 : rwidth += 15; /* "+-[ RECORD ]-+" */
1486 : : }
1487 : :
1488 : : /* We might need to do the rest of the calculation twice */
1489 : : for (;;)
4016 tgl@sss.pgh.pa.us 1490 : 17 : {
1491 : : unsigned int width;
1492 : :
1493 : : /* Total width required to not wrap data */
1494 : 376 : width = hwidth + swidth + dwidth;
1495 : : /* ... and not the header lines, either */
4015 1496 [ + + ]: 376 : if (width < rwidth)
1497 : 37 : width = rwidth;
1498 : :
173 michael@paquier.xyz 1499 [ + + + - ]: 376 : if (cont->opt->format == PRINT_WRAPPED && output_columns > 0)
4016 tgl@sss.pgh.pa.us 1500 : 94 : {
1501 : : unsigned int min_width;
1502 : :
1503 : : /* Minimum acceptable width: room for just 3 columns of data */
4015 1504 : 94 : min_width = hwidth + swidth + 3;
1505 : : /* ... but not less than what the record header lines need */
1506 [ + + ]: 94 : if (min_width < rwidth)
1507 : 29 : min_width = rwidth;
1508 : :
1509 [ + + ]: 94 : if (output_columns >= width)
1510 : : {
1511 : : /* Plenty of room, use native data width */
1512 : : /* (but at least enough for the record header lines) */
1513 : 24 : newdwidth = width - hwidth - swidth;
1514 : : }
1515 [ + + ]: 70 : else if (output_columns < min_width)
1516 : : {
1517 : : /* Set data width to match min_width */
1518 : 16 : newdwidth = min_width - hwidth - swidth;
1519 : : }
1520 : : else
1521 : : {
1522 : : /* Set data width to match output_columns */
1523 : 54 : newdwidth = output_columns - hwidth - swidth;
1524 : : }
1525 : : }
1526 : : else
1527 : : {
1528 : : /* Don't know the wrap limit, so use native data width */
1529 : : /* (but at least enough for the record header lines) */
1530 : 282 : newdwidth = width - hwidth - swidth;
1531 : : }
1532 : :
1533 : : /*
1534 : : * If we will need to wrap data and didn't already allocate a data
1535 : : * newline/wrap marker column, do so and recompute.
1536 : : */
4016 1537 [ + + + + : 376 : if (newdwidth < dwidth && !dmultiline &&
+ + ]
1538 [ + - ]: 17 : opt_border < 2 && format != &pg_asciiformat_old)
1539 : : {
1540 : 17 : dmultiline = true;
1541 : 17 : swidth++;
1542 : : }
1543 : : else
1544 : : break;
1545 : : }
1546 : :
1547 : 359 : dwidth = newdwidth;
1548 : : }
1549 : :
1550 : : /*
1551 : : * Deal with the pager here instead of in printTable(), because we could
1552 : : * get here via print_aligned_text() in expanded auto mode, and so we have
1553 : : * to recalculate the pager requirement based on vertical output.
1554 : : */
146 1555 [ + + ]: 359 : if (!is_pager)
1556 : : {
1557 : 343 : unsigned int *width_wrap = NULL;
1558 : :
1559 : : /*
1560 : : * Wrapping can add extra output lines, which count_table_lines() can
1561 : : * only account for if it has wrap widths. But vertical output uses
1562 : : * the same data width for every field, so that's easy: use dwidth for
1563 : : * every column.
1564 : : */
1565 [ + + + - ]: 343 : if (cont->opt->format == PRINT_WRAPPED && cont->ncolumns > 0)
1566 : : {
1567 : 77 : width_wrap = pg_malloc_array(unsigned int, cont->ncolumns);
140 peter@eisentraut.org 1568 [ + + ]:GNC 282 : for (int j = 0; j < cont->ncolumns; j++)
1569 : 205 : width_wrap[j] = dwidth;
1570 : : }
1571 : :
146 tgl@sss.pgh.pa.us 1572 :CBC 343 : IsPagerNeeded(cont, width_wrap, true, &fout, &is_pager);
1573 : 343 : is_local_pager = is_pager;
1574 : :
1575 : 343 : free(width_wrap);
1576 : : }
1577 : :
1578 [ + + ]: 359 : if (cont->opt->start_table)
1579 : : {
1580 : : /* print title */
1581 [ + + + + ]: 351 : if (!opt_tuples_only && cont->title)
1582 : 24 : fprintf(fout, "%s\n", cont->title);
1583 : : }
1584 : :
1585 : : /* print records */
6774 alvherre@alvh.no-ip. 1586 [ + + ]: 3922 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1587 : : {
1588 : : printTextRule pos;
1589 : : int dline,
1590 : : hline,
1591 : : dcomplete,
1592 : : hcomplete,
1593 : : offset,
1594 : : chars_to_output;
1595 : :
9 peter@eisentraut.org 1596 [ - + ]:GNC 3563 : if (CancelRequested)
6255 tgl@sss.pgh.pa.us 1597 :UBC 0 : break;
1598 : :
6255 tgl@sss.pgh.pa.us 1599 [ + + ]:CBC 3563 : if (i == 0)
6119 bruce@momjian.us 1600 : 351 : pos = PRINT_RULE_TOP;
1601 : : else
6255 tgl@sss.pgh.pa.us 1602 : 3212 : pos = PRINT_RULE_MIDDLE;
1603 : :
1604 : : /* Print record header (e.g. "[ RECORD N ]") above each record */
6774 alvherre@alvh.no-ip. 1605 [ + + ]: 3563 : if (i % cont->ncolumns == 0)
1606 : : {
4485 stark@mit.edu 1607 : 1126 : unsigned int lhwidth = hwidth;
1608 : :
1609 [ + + + + ]: 1126 : if ((opt_border < 2) &&
4482 peter_e@gmx.net 1610 [ + + ]: 64 : (hmultiline) &&
1611 : : (format == &pg_asciiformat_old))
4207 bruce@momjian.us 1612 : 32 : lhwidth++; /* for newline indicators */
1613 : :
7807 1614 [ + + ]: 1126 : if (!opt_tuples_only)
1587 andrew@dunslane.net 1615 : 1102 : print_aligned_vertical_line(cont->opt, record++,
1616 : : lhwidth, dwidth, output_columns,
1617 : : pos, fout);
6774 alvherre@alvh.no-ip. 1618 [ + + + - : 24 : else if (i != 0 || !cont->opt->start_table || opt_border == 2)
- + ]
1587 andrew@dunslane.net 1619 : 12 : print_aligned_vertical_line(cont->opt, 0, lhwidth,
1620 : : dwidth, output_columns, pos, fout);
1621 : : }
1622 : :
1623 : : /* Format the header */
5557 peter_e@gmx.net 1624 : 3563 : pg_wcsformat((const unsigned char *) cont->headers[i % cont->ncolumns],
6774 alvherre@alvh.no-ip. 1625 : 3563 : strlen(cont->headers[i % cont->ncolumns]),
1626 : : encoding, hlineptr, hheight);
1627 : : /* Format the data */
5557 peter_e@gmx.net 1628 : 3563 : pg_wcsformat((const unsigned char *) *ptr, strlen(*ptr), encoding,
1629 : : dlineptr, dheight);
1630 : :
1631 : : /*
1632 : : * Loop through header and data in parallel dealing with newlines and
1633 : : * wrapped lines until they're both exhausted
1634 : : */
4597 stark@mit.edu 1635 : 3563 : dline = hline = 0;
7596 bruce@momjian.us 1636 : 3563 : dcomplete = hcomplete = 0;
4597 stark@mit.edu 1637 : 3563 : offset = 0;
1638 : 3563 : chars_to_output = dlineptr[dline].width;
7596 bruce@momjian.us 1639 [ + + + + ]: 9065 : while (!dcomplete || !hcomplete)
1640 : : {
1641 : : /* Left border */
1642 [ + + ]: 5502 : if (opt_border == 2)
4597 stark@mit.edu 1643 : 1244 : fprintf(fout, "%s", dformat->leftvrule);
1644 : :
1645 : : /* Header (never wrapped so just need to deal with newlines) */
7596 bruce@momjian.us 1646 [ + + ]: 5502 : if (!hcomplete)
1647 : : {
4485 stark@mit.edu 1648 : 3851 : int swidth = hwidth,
1649 : 3851 : target_width = hwidth;
1650 : :
1651 : : /*
1652 : : * Left spacer or new line indicator
1653 : : */
1654 [ + + + + ]: 3851 : if ((opt_border == 2) ||
1655 [ + + ]: 320 : (hmultiline && (format == &pg_asciiformat_old)))
1656 [ + + ]: 832 : fputs(hline ? format->header_nl_left : " ", fout);
1657 : :
1658 : : /*
1659 : : * Header text
1660 : : */
1661 : 3851 : strlen_max_width(hlineptr[hline].ptr, &target_width,
1662 : : encoding);
4597 1663 : 3851 : fprintf(fout, "%-s", hlineptr[hline].ptr);
1664 : :
1665 : : /*
1666 : : * Spacer
1667 : : */
4485 1668 : 3851 : swidth -= target_width;
1669 [ + + ]: 3851 : if (swidth > 0)
4597 1670 : 2463 : fprintf(fout, "%*s", swidth, " ");
1671 : :
1672 : : /*
1673 : : * New line indicator or separator's space
1674 : : */
1675 [ + + ]: 3851 : if (hlineptr[hline + 1].ptr)
1676 : : {
1677 : : /* More lines after this one due to a newline */
4485 1678 [ + + + - ]: 288 : if ((opt_border > 0) ||
1679 [ + + ]: 96 : (hmultiline && (format != &pg_asciiformat_old)))
1680 : 240 : fputs(format->header_nl_right, fout);
4597 1681 : 288 : hline++;
1682 : : }
1683 : : else
1684 : : {
1685 : : /* This was the last line of the header */
4485 1686 [ + + + + ]: 3563 : if ((opt_border > 0) ||
1687 [ + + ]: 64 : (hmultiline && (format != &pg_asciiformat_old)))
1688 : 3051 : fputs(" ", fout);
7596 bruce@momjian.us 1689 : 3563 : hcomplete = 1;
1690 : : }
1691 : : }
1692 : : else
1693 : : {
4485 stark@mit.edu 1694 : 1651 : unsigned int swidth = hwidth + opt_border;
1695 : :
1696 [ + + + + ]: 1651 : if ((opt_border < 2) &&
1697 [ + + ]: 472 : (hmultiline) &&
1698 : : (format == &pg_asciiformat_old))
1699 : 232 : swidth++;
1700 : :
4482 peter_e@gmx.net 1701 [ + + + + ]: 1651 : if ((opt_border == 0) &&
4485 stark@mit.edu 1702 [ + + ]: 364 : (format != &pg_asciiformat_old) &&
1703 : : (hmultiline))
1704 : 120 : swidth++;
1705 : :
1706 : 1651 : fprintf(fout, "%*s", swidth, " ");
1707 : : }
1708 : :
1709 : : /* Separator */
7596 bruce@momjian.us 1710 [ + + ]: 5502 : if (opt_border > 0)
1711 : : {
4597 stark@mit.edu 1712 [ + + ]: 4386 : if (offset)
1713 : 755 : fputs(format->midvrule_wrap, fout);
4016 tgl@sss.pgh.pa.us 1714 [ + + ]: 3631 : else if (dline == 0)
4597 stark@mit.edu 1715 : 3019 : fputs(dformat->midvrule, fout);
1716 : : else
4016 tgl@sss.pgh.pa.us 1717 : 612 : fputs(format->midvrule_nl, fout);
1718 : : }
1719 : :
1720 : : /* Data */
7596 bruce@momjian.us 1721 [ + + ]: 5502 : if (!dcomplete)
1722 : : {
4485 stark@mit.edu 1723 : 5382 : int target_width = dwidth,
1724 : : bytes_to_output,
1725 : 5382 : swidth = dwidth;
1726 : :
1727 : : /*
1728 : : * Left spacer or wrap indicator
1729 : : */
4016 tgl@sss.pgh.pa.us 1730 [ + + ]: 5382 : fputs(offset == 0 ? " " : format->wrap_left, fout);
1731 : :
1732 : : /*
1733 : : * Data text
1734 : : */
4597 stark@mit.edu 1735 : 5382 : bytes_to_output = strlen_max_width(dlineptr[dline].ptr + offset,
1736 : : &target_width, encoding);
2343 tgl@sss.pgh.pa.us 1737 : 5382 : fwrite((char *) (dlineptr[dline].ptr + offset),
1738 : : 1, bytes_to_output, fout);
1739 : :
4597 stark@mit.edu 1740 : 5382 : chars_to_output -= target_width;
1741 : 5382 : offset += bytes_to_output;
1742 : :
1743 : : /* Spacer */
4485 1744 : 5382 : swidth -= target_width;
1745 : :
4597 1746 [ + + ]: 5382 : if (chars_to_output)
1747 : : {
1748 : : /* continuing a wrapped column */
4485 1749 [ + + + - ]: 951 : if ((opt_border > 1) ||
1750 [ + + ]: 579 : (dmultiline && (format != &pg_asciiformat_old)))
1751 : : {
1752 [ - + ]: 919 : if (swidth > 0)
4485 stark@mit.edu 1753 :UBC 0 : fprintf(fout, "%*s", swidth, " ");
4485 stark@mit.edu 1754 :CBC 919 : fputs(format->wrap_right, fout);
1755 : : }
1756 : : }
4597 1757 [ + + ]: 4431 : else if (dlineptr[dline + 1].ptr)
1758 : : {
1759 : : /* reached a newline in the column */
4485 1760 [ + + + - ]: 868 : if ((opt_border > 1) ||
1761 [ + + ]: 612 : (dmultiline && (format != &pg_asciiformat_old)))
1762 : : {
1763 [ + + ]: 612 : if (swidth > 0)
1764 : 596 : fprintf(fout, "%*s", swidth, " ");
1765 : 612 : fputs(format->nl_right, fout);
1766 : : }
4597 1767 : 868 : dline++;
1768 : 868 : offset = 0;
1769 : 868 : chars_to_output = dlineptr[dline].width;
1770 : : }
1771 : : else
1772 : : {
1773 : : /* reached the end of the cell */
4485 1774 [ + + ]: 3563 : if (opt_border > 1)
1775 : : {
1776 [ + + ]: 576 : if (swidth > 0)
1777 : 524 : fprintf(fout, "%*s", swidth, " ");
1778 : 576 : fputs(" ", fout);
1779 : : }
7360 bruce@momjian.us 1780 : 3563 : dcomplete = 1;
1781 : : }
1782 : :
1783 : : /* Right border */
4597 stark@mit.edu 1784 [ + + ]: 5382 : if (opt_border == 2)
1785 : 1204 : fputs(dformat->rightvrule, fout);
1786 : :
1787 : 5382 : fputs("\n", fout);
1788 : : }
1789 : : else
1790 : : {
1791 : : /*
1792 : : * data exhausted (this can occur if header is longer than the
1793 : : * data due to newlines in the header)
1794 : : */
7360 bruce@momjian.us 1795 [ + + ]: 120 : if (opt_border < 2)
4597 stark@mit.edu 1796 : 80 : fputs("\n", fout);
1797 : : else
1798 : 40 : fprintf(fout, "%*s %s\n", dwidth, "", dformat->rightvrule);
1799 : : }
1800 : : }
1801 : : }
1802 : :
6774 alvherre@alvh.no-ip. 1803 [ + + ]: 359 : if (cont->opt->stop_table)
1804 : : {
9 peter@eisentraut.org 1805 [ + + + - ]:GNC 351 : if (opt_border == 2 && !CancelRequested)
1587 andrew@dunslane.net 1806 :CBC 48 : print_aligned_vertical_line(cont->opt, 0, hwidth, dwidth,
1807 : : output_columns, PRINT_RULE_BOTTOM, fout);
1808 : :
1809 : : /* print footers */
9 peter@eisentraut.org 1810 [ + + + + :GNC 351 : if (!opt_tuples_only && cont->footers != NULL && !CancelRequested)
+ - ]
1811 : : {
1812 : : printTableFooter *f;
1813 : :
7396 tgl@sss.pgh.pa.us 1814 [ + - ]:CBC 8 : if (opt_border < 2)
1815 : 8 : fputc('\n', fout);
6774 alvherre@alvh.no-ip. 1816 [ + + ]: 16 : for (f = cont->footers; f; f = f->next)
1817 : 8 : fprintf(fout, "%s\n", f->data);
1818 : : }
1819 : :
7396 tgl@sss.pgh.pa.us 1820 : 351 : fputc('\n', fout);
1821 : : }
1822 : :
150 peter@eisentraut.org 1823 :GNC 359 : pg_free(hlineptr->ptr);
1824 : 359 : pg_free(dlineptr->ptr);
1825 : 359 : pg_free(hlineptr);
1826 : 359 : pg_free(dlineptr);
1827 : :
4014 tgl@sss.pgh.pa.us 1828 [ + + ]:CBC 359 : if (is_local_pager)
5495 peter_e@gmx.net 1829 : 1 : ClosePager(fout);
1830 : : }
1831 : :
1832 : :
1833 : : /**********************/
1834 : : /* CSV format */
1835 : : /**********************/
1836 : :
1837 : :
1838 : : static void
2924 tgl@sss.pgh.pa.us 1839 : 72 : csv_escaped_print(const char *str, FILE *fout)
1840 : : {
1841 : : const char *p;
1842 : :
1843 : 72 : fputc('"', fout);
1844 [ + + ]: 616 : for (p = str; *p; p++)
1845 : : {
1846 [ + + ]: 544 : if (*p == '"')
1847 : 28 : fputc('"', fout); /* double quotes are doubled */
1848 : 544 : fputc(*p, fout);
1849 : : }
1850 : 72 : fputc('"', fout);
1851 : 72 : }
1852 : :
1853 : : static void
1854 : 416 : csv_print_field(const char *str, FILE *fout, char sep)
1855 : : {
1856 : : /*----------------
1857 : : * Enclose and escape field contents when one of these conditions is met:
1858 : : * - the field separator is found in the contents.
1859 : : * - the field contains a CR or LF.
1860 : : * - the field contains a double quote.
1861 : : * - the field is exactly "\.".
1862 : : * - the field separator is either "\" or ".".
1863 : : * The last two cases prevent producing a line that the server's COPY
1864 : : * command would interpret as an end-of-data marker. We only really
1865 : : * need to ensure that the complete line isn't exactly "\.", but for
1866 : : * simplicity we apply stronger restrictions here.
1867 : : *----------------
1868 : : */
1869 [ + + ]: 416 : if (strchr(str, sep) != NULL ||
1870 [ + + ]: 408 : strcspn(str, "\r\n\"") != strlen(str) ||
1871 [ + + + - ]: 364 : strcmp(str, "\\.") == 0 ||
1872 [ + + ]: 360 : sep == '\\' || sep == '.')
1873 : 72 : csv_escaped_print(str, fout);
1874 : : else
1875 : 344 : fputs(str, fout);
1876 : 416 : }
1877 : :
1878 : : static void
1879 : 32 : print_csv_text(const printTableContent *cont, FILE *fout)
1880 : : {
1881 : : const char *const *ptr;
1882 : : int i;
1883 : :
9 peter@eisentraut.org 1884 [ - + ]:GNC 32 : if (CancelRequested)
2924 tgl@sss.pgh.pa.us 1885 :UBC 0 : return;
1886 : :
1887 : : /*
1888 : : * The title and footer are never printed in csv format. The header is
1889 : : * printed if opt_tuples_only is false.
1890 : : *
1891 : : * Despite RFC 4180 saying that end of lines are CRLF, terminate lines
1892 : : * with '\n', which prints out as the system-dependent EOL string in text
1893 : : * mode (typically LF on Unix and CRLF on Windows).
1894 : : */
2924 tgl@sss.pgh.pa.us 1895 [ + - + + ]:CBC 32 : if (cont->opt->start_table && !cont->opt->tuples_only)
1896 : : {
1897 : : /* print headers */
1898 [ + + ]: 108 : for (ptr = cont->headers; *ptr; ptr++)
1899 : : {
1900 [ + + ]: 80 : if (ptr != cont->headers)
1901 : 52 : fputc(cont->opt->csvFieldSep[0], fout);
1902 : 80 : csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
1903 : : }
1904 : 28 : fputc('\n', fout);
1905 : : }
1906 : :
1907 : : /* print cells */
1908 [ + + ]: 168 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1909 : : {
1910 : 136 : csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
1911 [ + + ]: 136 : if ((i + 1) % cont->ncolumns)
1912 : 96 : fputc(cont->opt->csvFieldSep[0], fout);
1913 : : else
1914 : 40 : fputc('\n', fout);
1915 : : }
1916 : : }
1917 : :
1918 : : static void
1919 : 12 : print_csv_vertical(const printTableContent *cont, FILE *fout)
1920 : : {
1921 : : const char *const *ptr;
1922 : : int i;
1923 : :
1924 : : /* print records */
1925 [ + + ]: 112 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
1926 : : {
9 peter@eisentraut.org 1927 [ - + ]:GNC 100 : if (CancelRequested)
2924 tgl@sss.pgh.pa.us 1928 :UBC 0 : return;
1929 : :
1930 : : /* print name of column */
2924 tgl@sss.pgh.pa.us 1931 :CBC 100 : csv_print_field(cont->headers[i % cont->ncolumns], fout,
1932 : 100 : cont->opt->csvFieldSep[0]);
1933 : :
1934 : : /* print field separator */
1935 : 100 : fputc(cont->opt->csvFieldSep[0], fout);
1936 : :
1937 : : /* print field value */
1938 : 100 : csv_print_field(*ptr, fout, cont->opt->csvFieldSep[0]);
1939 : :
1940 : 100 : fputc('\n', fout);
1941 : : }
1942 : : }
1943 : :
1944 : :
1945 : : /**********************/
1946 : : /* HTML */
1947 : : /**********************/
1948 : :
1949 : :
1950 : : void
9886 bruce@momjian.us 1951 : 548 : html_escaped_print(const char *in, FILE *fout)
1952 : : {
1953 : : const char *p;
7714 1954 : 548 : bool leading_space = true;
1955 : :
9886 1956 [ + + ]: 4600 : for (p = in; *p; p++)
1957 : : {
1958 [ + + + + : 4052 : switch (*p)
+ + + ]
1959 : : {
1960 : 36 : case '&':
1961 : 36 : fputs("&", fout);
1962 : 36 : break;
1963 : 96 : case '<':
1964 : 96 : fputs("<", fout);
1965 : 96 : break;
1966 : 96 : case '>':
1967 : 96 : fputs(">", fout);
1968 : 96 : break;
1969 : 48 : case '\n':
8570 1970 : 48 : fputs("<br />\n", fout);
1971 : 48 : break;
1972 : 64 : case '"':
1973 : 64 : fputs(""", fout);
1974 : 64 : break;
7837 1975 : 180 : case ' ':
1976 : : /* protect leading space, for EXPLAIN output */
1977 [ + + ]: 180 : if (leading_space)
1978 : 96 : fputs(" ", fout);
1979 : : else
1980 : 84 : fputs(" ", fout);
1981 : 180 : break;
9886 1982 : 3532 : default:
1983 : 3532 : fputc(*p, fout);
1984 : : }
7837 1985 [ + + ]: 4052 : if (*p != ' ')
1986 : 3872 : leading_space = false;
1987 : : }
9886 1988 : 548 : }
1989 : :
1990 : :
1991 : : static void
6774 alvherre@alvh.no-ip. 1992 : 20 : print_html_text(const printTableContent *cont, FILE *fout)
1993 : : {
1994 : 20 : bool opt_tuples_only = cont->opt->tuples_only;
1995 : 20 : unsigned short opt_border = cont->opt->border;
1996 : 20 : const char *opt_table_attr = cont->opt->tableAttr;
1997 : : unsigned int i;
1998 : : const char *const *ptr;
1999 : :
9 peter@eisentraut.org 2000 [ - + ]:GNC 20 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2001 :UBC 0 : return;
2002 : :
6774 alvherre@alvh.no-ip. 2003 [ + - ]:CBC 20 : if (cont->opt->start_table)
2004 : : {
7396 tgl@sss.pgh.pa.us 2005 : 20 : fprintf(fout, "<table border=\"%d\"", opt_border);
2006 [ + + ]: 20 : if (opt_table_attr)
2007 : 4 : fprintf(fout, " %s", opt_table_attr);
2008 : 20 : fputs(">\n", fout);
2009 : :
2010 : : /* print title */
6774 alvherre@alvh.no-ip. 2011 [ + + + + ]: 20 : if (!opt_tuples_only && cont->title)
2012 : : {
7396 tgl@sss.pgh.pa.us 2013 : 4 : fputs(" <caption>", fout);
6774 alvherre@alvh.no-ip. 2014 : 4 : html_escaped_print(cont->title, fout);
7396 tgl@sss.pgh.pa.us 2015 : 4 : fputs("</caption>\n", fout);
2016 : : }
2017 : :
2018 : : /* print headers */
7807 bruce@momjian.us 2019 [ + + ]: 20 : if (!opt_tuples_only)
2020 : : {
7396 tgl@sss.pgh.pa.us 2021 : 16 : fputs(" <tr>\n", fout);
6774 alvherre@alvh.no-ip. 2022 [ + + ]: 92 : for (ptr = cont->headers; *ptr; ptr++)
2023 : : {
7396 tgl@sss.pgh.pa.us 2024 : 76 : fputs(" <th align=\"center\">", fout);
2025 : 76 : html_escaped_print(*ptr, fout);
2026 : 76 : fputs("</th>\n", fout);
2027 : : }
2028 : 16 : fputs(" </tr>\n", fout);
2029 : : }
2030 : : }
2031 : :
2032 : : /* print cells */
6774 alvherre@alvh.no-ip. 2033 [ + + ]: 184 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2034 : : {
2035 [ + + ]: 164 : if (i % cont->ncolumns == 0)
2036 : : {
9 peter@eisentraut.org 2037 [ - + ]:GNC 36 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2038 :UBC 0 : break;
8570 bruce@momjian.us 2039 :CBC 36 : fputs(" <tr valign=\"top\">\n", fout);
2040 : : }
2041 : :
6774 alvherre@alvh.no-ip. 2042 [ + + ]: 164 : fprintf(fout, " <td align=\"%s\">", cont->aligns[(i) % cont->ncolumns] == 'r' ? "right" : "left");
2043 : : /* is string only whitespace? */
7714 bruce@momjian.us 2044 [ + + ]: 164 : if ((*ptr)[strspn(*ptr, " \t")] == '\0')
8570 2045 : 24 : fputs(" ", fout);
2046 : : else
9886 2047 : 140 : html_escaped_print(*ptr, fout);
2048 : :
2049 : 164 : fputs("</td>\n", fout);
2050 : :
6774 alvherre@alvh.no-ip. 2051 [ + + ]: 164 : if ((i + 1) % cont->ncolumns == 0)
9886 bruce@momjian.us 2052 : 36 : fputs(" </tr>\n", fout);
2053 : : }
2054 : :
6774 alvherre@alvh.no-ip. 2055 [ + - ]: 20 : if (cont->opt->stop_table)
2056 : : {
5324 rhaas@postgresql.org 2057 : 20 : printTableFooter *footers = footers_with_default(cont);
2058 : :
7396 tgl@sss.pgh.pa.us 2059 : 20 : fputs("</table>\n", fout);
2060 : :
2061 : : /* print footers */
9 peter@eisentraut.org 2062 [ + + + - :GNC 20 : if (!opt_tuples_only && footers != NULL && !CancelRequested)
+ - ]
2063 : : {
2064 : : printTableFooter *f;
2065 : :
7396 tgl@sss.pgh.pa.us 2066 :CBC 16 : fputs("<p>", fout);
5324 rhaas@postgresql.org 2067 [ + + ]: 32 : for (f = footers; f; f = f->next)
2068 : : {
6774 alvherre@alvh.no-ip. 2069 : 16 : html_escaped_print(f->data, fout);
7396 tgl@sss.pgh.pa.us 2070 : 16 : fputs("<br />\n", fout);
2071 : : }
2072 : 16 : fputs("</p>", fout);
2073 : : }
2074 : :
2075 : 20 : fputc('\n', fout);
2076 : : }
2077 : : }
2078 : :
2079 : :
2080 : : static void
6774 alvherre@alvh.no-ip. 2081 : 20 : print_html_vertical(const printTableContent *cont, FILE *fout)
2082 : : {
2083 : 20 : bool opt_tuples_only = cont->opt->tuples_only;
2084 : 20 : unsigned short opt_border = cont->opt->border;
2085 : 20 : const char *opt_table_attr = cont->opt->tableAttr;
2086 : 20 : unsigned long record = cont->opt->prior_records + 1;
2087 : : unsigned int i;
2088 : : const char *const *ptr;
2089 : :
9 peter@eisentraut.org 2090 [ - + ]:GNC 20 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2091 :UBC 0 : return;
2092 : :
6774 alvherre@alvh.no-ip. 2093 [ + - ]:CBC 20 : if (cont->opt->start_table)
2094 : : {
7396 tgl@sss.pgh.pa.us 2095 : 20 : fprintf(fout, "<table border=\"%d\"", opt_border);
2096 [ + + ]: 20 : if (opt_table_attr)
2097 : 4 : fprintf(fout, " %s", opt_table_attr);
2098 : 20 : fputs(">\n", fout);
2099 : :
2100 : : /* print title */
6774 alvherre@alvh.no-ip. 2101 [ + + + + ]: 20 : if (!opt_tuples_only && cont->title)
2102 : : {
7396 tgl@sss.pgh.pa.us 2103 : 4 : fputs(" <caption>", fout);
6774 alvherre@alvh.no-ip. 2104 : 4 : html_escaped_print(cont->title, fout);
7396 tgl@sss.pgh.pa.us 2105 : 4 : fputs("</caption>\n", fout);
2106 : : }
2107 : : }
2108 : :
2109 : : /* print records */
6774 alvherre@alvh.no-ip. 2110 [ + + ]: 184 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2111 : : {
2112 [ + + ]: 164 : if (i % cont->ncolumns == 0)
2113 : : {
9 peter@eisentraut.org 2114 [ - + ]:GNC 36 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2115 :UBC 0 : break;
7807 bruce@momjian.us 2116 [ + + ]:CBC 36 : if (!opt_tuples_only)
7396 tgl@sss.pgh.pa.us 2117 : 28 : fprintf(fout,
2118 : : "\n <tr><td colspan=\"2\" align=\"center\">Record %lu</td></tr>\n",
2119 : : record++);
2120 : : else
8570 bruce@momjian.us 2121 : 8 : fputs("\n <tr><td colspan=\"2\"> </td></tr>\n", fout);
2122 : : }
2123 : 164 : fputs(" <tr valign=\"top\">\n"
2124 : : " <th>", fout);
6774 alvherre@alvh.no-ip. 2125 : 164 : html_escaped_print(cont->headers[i % cont->ncolumns], fout);
9886 bruce@momjian.us 2126 : 164 : fputs("</th>\n", fout);
2127 : :
6774 alvherre@alvh.no-ip. 2128 [ + + ]: 164 : fprintf(fout, " <td align=\"%s\">", cont->aligns[i % cont->ncolumns] == 'r' ? "right" : "left");
2129 : : /* is string only whitespace? */
7714 bruce@momjian.us 2130 [ + + ]: 164 : if ((*ptr)[strspn(*ptr, " \t")] == '\0')
8570 2131 : 24 : fputs(" ", fout);
2132 : : else
9886 2133 : 140 : html_escaped_print(*ptr, fout);
2134 : :
2135 : 164 : fputs("</td>\n </tr>\n", fout);
2136 : : }
2137 : :
6774 alvherre@alvh.no-ip. 2138 [ + - ]: 20 : if (cont->opt->stop_table)
2139 : : {
7396 tgl@sss.pgh.pa.us 2140 : 20 : fputs("</table>\n", fout);
2141 : :
2142 : : /* print footers */
9 peter@eisentraut.org 2143 [ + + + + :GNC 20 : if (!opt_tuples_only && cont->footers != NULL && !CancelRequested)
+ - ]
2144 : : {
2145 : : printTableFooter *f;
2146 : :
7396 tgl@sss.pgh.pa.us 2147 :CBC 4 : fputs("<p>", fout);
6774 alvherre@alvh.no-ip. 2148 [ + + ]: 8 : for (f = cont->footers; f; f = f->next)
2149 : : {
2150 : 4 : html_escaped_print(f->data, fout);
7396 tgl@sss.pgh.pa.us 2151 : 4 : fputs("<br />\n", fout);
2152 : : }
2153 : 4 : fputs("</p>", fout);
2154 : : }
2155 : :
2156 : 20 : fputc('\n', fout);
2157 : : }
2158 : : }
2159 : :
2160 : :
2161 : : /*************************/
2162 : : /* ASCIIDOC */
2163 : : /*************************/
2164 : :
2165 : :
2166 : : static void
4260 bruce@momjian.us 2167 : 436 : asciidoc_escaped_print(const char *in, FILE *fout)
2168 : : {
2169 : : const char *p;
2170 : :
2171 [ + + ]: 3060 : for (p = in; *p; p++)
2172 : : {
4207 2173 [ + + ]: 2624 : switch (*p)
2174 : : {
4260 2175 : 84 : case '|':
2176 : 84 : fputs("\\|", fout);
2177 : 84 : break;
2178 : 2540 : default:
2179 : 2540 : fputc(*p, fout);
2180 : : }
2181 : : }
2182 : 436 : }
2183 : :
2184 : : static void
2185 : 20 : print_asciidoc_text(const printTableContent *cont, FILE *fout)
2186 : : {
2187 : 20 : bool opt_tuples_only = cont->opt->tuples_only;
2188 : 20 : unsigned short opt_border = cont->opt->border;
2189 : : unsigned int i;
2190 : : const char *const *ptr;
2191 : :
9 peter@eisentraut.org 2192 [ - + ]:GNC 20 : if (CancelRequested)
4260 bruce@momjian.us 2193 :UBC 0 : return;
2194 : :
4260 bruce@momjian.us 2195 [ + - ]:CBC 20 : if (cont->opt->start_table)
2196 : : {
2197 : : /* print table in new paragraph - enforce preliminary new line */
2198 : 20 : fputs("\n", fout);
2199 : :
2200 : : /* print title */
2201 [ + + + + ]: 20 : if (!opt_tuples_only && cont->title)
2202 : : {
2203 : 4 : fputs(".", fout);
2204 : 4 : fputs(cont->title, fout);
2205 : 4 : fputs("\n", fout);
2206 : : }
2207 : :
2208 : : /* print table [] header definition */
2209 [ + + ]: 20 : fprintf(fout, "[%scols=\"", !opt_tuples_only ? "options=\"header\"," : "");
4207 2210 [ + + ]: 104 : for (i = 0; i < cont->ncolumns; i++)
2211 : : {
4260 2212 [ + + ]: 84 : if (i != 0)
2213 : 64 : fputs(",", fout);
2214 [ + + ]: 84 : fprintf(fout, "%s", cont->aligns[(i) % cont->ncolumns] == 'r' ? ">l" : "<l");
2215 : : }
2216 : 20 : fputs("\"", fout);
2217 [ + + + - ]: 20 : switch (opt_border)
2218 : : {
2219 : 4 : case 0:
2220 : 4 : fputs(",frame=\"none\",grid=\"none\"", fout);
2221 : 4 : break;
2222 : 12 : case 1:
2223 : 12 : fputs(",frame=\"none\"", fout);
2224 : 12 : break;
2225 : 4 : case 2:
2226 : 4 : fputs(",frame=\"all\",grid=\"all\"", fout);
2227 : 4 : break;
2228 : : }
2229 : 20 : fputs("]\n", fout);
2230 : 20 : fputs("|====\n", fout);
2231 : :
2232 : : /* print headers */
2233 [ + + ]: 20 : if (!opt_tuples_only)
2234 : : {
2235 [ + + ]: 80 : for (ptr = cont->headers; *ptr; ptr++)
2236 : : {
2237 [ + + ]: 64 : if (ptr != cont->headers)
2238 : 48 : fputs(" ", fout);
2239 : 64 : fputs("^l|", fout);
2240 : 64 : asciidoc_escaped_print(*ptr, fout);
2241 : : }
2242 : 16 : fputs("\n", fout);
2243 : : }
2244 : : }
2245 : :
2246 : : /* print cells */
2247 [ + + ]: 160 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2248 : : {
2249 [ + + ]: 140 : if (i % cont->ncolumns == 0)
2250 : : {
9 peter@eisentraut.org 2251 [ - + ]:GNC 36 : if (CancelRequested)
4260 bruce@momjian.us 2252 :UBC 0 : break;
2253 : : }
2254 : :
4260 bruce@momjian.us 2255 [ + + ]:CBC 140 : if (i % cont->ncolumns != 0)
2256 : 104 : fputs(" ", fout);
2257 : 140 : fputs("|", fout);
2258 : :
2259 : : /* protect against needless spaces */
2260 [ + + ]: 140 : if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2261 : : {
2262 [ + - ]: 24 : if ((i + 1) % cont->ncolumns != 0)
2263 : 24 : fputs(" ", fout);
2264 : : }
2265 : : else
2266 : 116 : asciidoc_escaped_print(*ptr, fout);
2267 : :
2268 [ + + ]: 140 : if ((i + 1) % cont->ncolumns == 0)
2269 : 36 : fputs("\n", fout);
2270 : : }
2271 : :
2272 : 20 : fputs("|====\n", fout);
2273 : :
2274 [ + - ]: 20 : if (cont->opt->stop_table)
2275 : : {
2276 : 20 : printTableFooter *footers = footers_with_default(cont);
2277 : :
2278 : : /* print footers */
9 peter@eisentraut.org 2279 [ + + + - :GNC 20 : if (!opt_tuples_only && footers != NULL && !CancelRequested)
+ - ]
2280 : : {
2281 : : printTableFooter *f;
2282 : :
4260 bruce@momjian.us 2283 :CBC 16 : fputs("\n....\n", fout);
2284 [ + + ]: 32 : for (f = footers; f; f = f->next)
2285 : : {
2286 : 16 : fputs(f->data, fout);
2287 : 16 : fputs("\n", fout);
2288 : : }
2289 : 16 : fputs("....\n", fout);
2290 : : }
2291 : : }
2292 : : }
2293 : :
2294 : : static void
2295 : 20 : print_asciidoc_vertical(const printTableContent *cont, FILE *fout)
2296 : : {
2297 : 20 : bool opt_tuples_only = cont->opt->tuples_only;
2298 : 20 : unsigned short opt_border = cont->opt->border;
2299 : 20 : unsigned long record = cont->opt->prior_records + 1;
2300 : : unsigned int i;
2301 : : const char *const *ptr;
2302 : :
9 peter@eisentraut.org 2303 [ - + ]:GNC 20 : if (CancelRequested)
4260 bruce@momjian.us 2304 :UBC 0 : return;
2305 : :
4260 bruce@momjian.us 2306 [ + - ]:CBC 20 : if (cont->opt->start_table)
2307 : : {
2308 : : /* print table in new paragraph - enforce preliminary new line */
2309 : 20 : fputs("\n", fout);
2310 : :
2311 : : /* print title */
2312 [ + + + + ]: 20 : if (!opt_tuples_only && cont->title)
2313 : : {
2314 : 4 : fputs(".", fout);
2315 : 4 : fputs(cont->title, fout);
2316 : 4 : fputs("\n", fout);
2317 : : }
2318 : :
2319 : : /* print table [] header definition */
2320 : 20 : fputs("[cols=\"h,l\"", fout);
2321 [ + + + - ]: 20 : switch (opt_border)
2322 : : {
2323 : 4 : case 0:
2324 : 4 : fputs(",frame=\"none\",grid=\"none\"", fout);
2325 : 4 : break;
2326 : 12 : case 1:
2327 : 12 : fputs(",frame=\"none\"", fout);
2328 : 12 : break;
2329 : 4 : case 2:
2330 : 4 : fputs(",frame=\"all\",grid=\"all\"", fout);
4207 2331 : 4 : break;
2332 : : }
4260 2333 : 20 : fputs("]\n", fout);
2334 : 20 : fputs("|====\n", fout);
2335 : : }
2336 : :
2337 : : /* print records */
2338 [ + + ]: 160 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2339 : : {
2340 [ + + ]: 140 : if (i % cont->ncolumns == 0)
2341 : : {
9 peter@eisentraut.org 2342 [ - + ]:GNC 36 : if (CancelRequested)
4260 bruce@momjian.us 2343 :UBC 0 : break;
4260 bruce@momjian.us 2344 [ + + ]:CBC 36 : if (!opt_tuples_only)
2345 : 28 : fprintf(fout,
2346 : : "2+^|Record %lu\n",
2347 : : record++);
2348 : : else
2349 : 8 : fputs("2+|\n", fout);
2350 : : }
2351 : :
2352 : 140 : fputs("<l|", fout);
2353 : 140 : asciidoc_escaped_print(cont->headers[i % cont->ncolumns], fout);
2354 : :
2355 [ + + ]: 140 : fprintf(fout, " %s|", cont->aligns[i % cont->ncolumns] == 'r' ? ">l" : "<l");
2356 : : /* is string only whitespace? */
2357 [ + + ]: 140 : if ((*ptr)[strspn(*ptr, " \t")] == '\0')
2358 : 24 : fputs(" ", fout);
2359 : : else
2360 : 116 : asciidoc_escaped_print(*ptr, fout);
2361 : 140 : fputs("\n", fout);
2362 : : }
2363 : :
2364 : 20 : fputs("|====\n", fout);
2365 : :
2366 [ + - ]: 20 : if (cont->opt->stop_table)
2367 : : {
2368 : : /* print footers */
9 peter@eisentraut.org 2369 [ + + + + :GNC 20 : if (!opt_tuples_only && cont->footers != NULL && !CancelRequested)
+ - ]
2370 : : {
2371 : : printTableFooter *f;
2372 : :
4260 bruce@momjian.us 2373 :CBC 4 : fputs("\n....\n", fout);
2374 [ + + ]: 8 : for (f = cont->footers; f; f = f->next)
2375 : : {
2376 : 4 : fputs(f->data, fout);
2377 : 4 : fputs("\n", fout);
2378 : : }
2379 : 4 : fputs("....\n", fout);
2380 : : }
2381 : : }
2382 : : }
2383 : :
2384 : :
2385 : : /*************************/
2386 : : /* LaTeX */
2387 : : /*************************/
2388 : :
2389 : :
2390 : : static void
9886 2391 : 1636 : latex_escaped_print(const char *in, FILE *fout)
2392 : : {
2393 : : const char *p;
2394 : :
2395 [ + + ]: 14376 : for (p = in; *p; p++)
2396 [ + + + + : 12740 : switch (*p)
+ + + + +
+ + + + +
+ ]
2397 : : {
2398 : : /*
2399 : : * We convert ASCII characters per the recommendations in
2400 : : * Scott Pakin's "The Comprehensive LATEX Symbol List",
2401 : : * available from CTAN. For non-ASCII, you're on your own.
2402 : : */
2924 tgl@sss.pgh.pa.us 2403 : 144 : case '#':
2404 : 144 : fputs("\\#", fout);
2405 : 144 : break;
2406 : 128 : case '$':
2407 : 128 : fputs("\\$", fout);
9886 bruce@momjian.us 2408 : 128 : break;
2409 : 144 : case '%':
2410 : 144 : fputs("\\%", fout);
2411 : 144 : break;
2924 tgl@sss.pgh.pa.us 2412 : 144 : case '&':
2413 : 144 : fputs("\\&", fout);
2414 : 144 : break;
2415 : 144 : case '<':
2416 : 144 : fputs("\\textless{}", fout);
2417 : 144 : break;
2418 : 144 : case '>':
2419 : 144 : fputs("\\textgreater{}", fout);
2420 : 144 : break;
2421 : 144 : case '\\':
2422 : 144 : fputs("\\textbackslash{}", fout);
2423 : 144 : break;
2424 : 144 : case '^':
2425 : 144 : fputs("\\^{}", fout);
9886 bruce@momjian.us 2426 : 144 : break;
8149 2427 : 312 : case '_':
2428 : 312 : fputs("\\_", fout);
2429 : 312 : break;
9886 2430 : 144 : case '{':
2431 : 144 : fputs("\\{", fout);
2432 : 144 : break;
2924 tgl@sss.pgh.pa.us 2433 : 144 : case '|':
2434 : 144 : fputs("\\textbar{}", fout);
2435 : 144 : break;
9886 bruce@momjian.us 2436 : 144 : case '}':
2437 : 144 : fputs("\\}", fout);
2438 : 144 : break;
2924 tgl@sss.pgh.pa.us 2439 : 144 : case '~':
2440 : 144 : fputs("\\~{}", fout);
9886 bruce@momjian.us 2441 : 144 : break;
2442 : 144 : case '\n':
2443 : : /* This is not right, but doing it right seems too hard */
2444 : 144 : fputs("\\\\", fout);
2445 : 144 : break;
2446 : 10572 : default:
2447 : 10572 : fputc(*p, fout);
2448 : : }
2449 : 1636 : }
2450 : :
2451 : :
2452 : : static void
6774 alvherre@alvh.no-ip. 2453 : 24 : print_latex_text(const printTableContent *cont, FILE *fout)
2454 : : {
2455 : 24 : bool opt_tuples_only = cont->opt->tuples_only;
2456 : 24 : unsigned short opt_border = cont->opt->border;
2457 : : unsigned int i;
2458 : : const char *const *ptr;
2459 : :
9 peter@eisentraut.org 2460 [ - + ]:GNC 24 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2461 :UBC 0 : return;
2462 : :
5063 bruce@momjian.us 2463 [ - + ]:CBC 24 : if (opt_border > 3)
5063 bruce@momjian.us 2464 :UBC 0 : opt_border = 3;
2465 : :
6774 alvherre@alvh.no-ip. 2466 [ + - ]:CBC 24 : if (cont->opt->start_table)
2467 : : {
2468 : : /* print title */
2469 [ + + + + ]: 24 : if (!opt_tuples_only && cont->title)
2470 : : {
7396 tgl@sss.pgh.pa.us 2471 : 4 : fputs("\\begin{center}\n", fout);
6774 alvherre@alvh.no-ip. 2472 : 4 : latex_escaped_print(cont->title, fout);
7396 tgl@sss.pgh.pa.us 2473 : 4 : fputs("\n\\end{center}\n\n", fout);
2474 : : }
2475 : :
2476 : : /* begin environment and set alignments and borders */
2477 : 24 : fputs("\\begin{tabular}{", fout);
2478 : :
5063 bruce@momjian.us 2479 [ + + ]: 24 : if (opt_border >= 2)
7396 tgl@sss.pgh.pa.us 2480 : 8 : fputs("| ", fout);
6774 alvherre@alvh.no-ip. 2481 [ + + ]: 136 : for (i = 0; i < cont->ncolumns; i++)
2482 : : {
2483 : 112 : fputc(*(cont->aligns + i), fout);
2484 [ + + + + ]: 112 : if (opt_border != 0 && i < cont->ncolumns - 1)
7396 tgl@sss.pgh.pa.us 2485 : 76 : fputs(" | ", fout);
2486 : : }
5063 bruce@momjian.us 2487 [ + + ]: 24 : if (opt_border >= 2)
7396 tgl@sss.pgh.pa.us 2488 : 8 : fputs(" |", fout);
2489 : :
2490 : 24 : fputs("}\n", fout);
2491 : :
5063 bruce@momjian.us 2492 [ + + + + ]: 24 : if (!opt_tuples_only && opt_border >= 2)
7396 tgl@sss.pgh.pa.us 2493 : 8 : fputs("\\hline\n", fout);
2494 : :
2495 : : /* print headers */
7807 bruce@momjian.us 2496 [ + + ]: 24 : if (!opt_tuples_only)
2497 : : {
6774 alvherre@alvh.no-ip. 2498 [ + + ]: 112 : for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2499 : : {
7396 tgl@sss.pgh.pa.us 2500 [ + + ]: 92 : if (i != 0)
2501 : 72 : fputs(" & ", fout);
2502 : 92 : fputs("\\textit{", fout);
2503 : 92 : latex_escaped_print(*ptr, fout);
2504 : 92 : fputc('}', fout);
2505 : : }
2506 : 20 : fputs(" \\\\\n", fout);
2507 : 20 : fputs("\\hline\n", fout);
2508 : : }
2509 : : }
2510 : :
2511 : : /* print cells */
6774 alvherre@alvh.no-ip. 2512 [ + + ]: 220 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2513 : : {
6116 heikki.linnakangas@i 2514 : 196 : latex_escaped_print(*ptr, fout);
2515 : :
6774 alvherre@alvh.no-ip. 2516 [ + + ]: 196 : if ((i + 1) % cont->ncolumns == 0)
2517 : : {
9886 bruce@momjian.us 2518 : 44 : fputs(" \\\\\n", fout);
5063 2519 [ + + ]: 44 : if (opt_border == 3)
2520 : 8 : fputs("\\hline\n", fout);
9 peter@eisentraut.org 2521 [ - + ]:GNC 44 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2522 :UBC 0 : break;
2523 : : }
2524 : : else
9886 bruce@momjian.us 2525 :CBC 152 : fputs(" & ", fout);
2526 : : }
2527 : :
6774 alvherre@alvh.no-ip. 2528 [ + - ]: 24 : if (cont->opt->stop_table)
2529 : : {
5324 rhaas@postgresql.org 2530 : 24 : printTableFooter *footers = footers_with_default(cont);
2531 : :
5062 bruce@momjian.us 2532 [ + + ]: 24 : if (opt_border == 2)
7396 tgl@sss.pgh.pa.us 2533 : 4 : fputs("\\hline\n", fout);
2534 : :
2535 : 24 : fputs("\\end{tabular}\n\n\\noindent ", fout);
2536 : :
2537 : : /* print footers */
9 peter@eisentraut.org 2538 [ + - + + :GNC 24 : if (footers && !opt_tuples_only && !CancelRequested)
+ - ]
2539 : : {
2540 : : printTableFooter *f;
2541 : :
5324 rhaas@postgresql.org 2542 [ + + ]:CBC 40 : for (f = footers; f; f = f->next)
2543 : : {
6774 alvherre@alvh.no-ip. 2544 : 20 : latex_escaped_print(f->data, fout);
7396 tgl@sss.pgh.pa.us 2545 : 20 : fputs(" \\\\\n", fout);
2546 : : }
2547 : : }
2548 : :
2549 : 24 : fputc('\n', fout);
2550 : : }
2551 : : }
2552 : :
2553 : :
2554 : : /*************************/
2555 : : /* LaTeX longtable */
2556 : : /*************************/
2557 : :
2558 : :
2559 : : static void
5062 bruce@momjian.us 2560 : 28 : print_latex_longtable_text(const printTableContent *cont, FILE *fout)
2561 : : {
5063 2562 : 28 : bool opt_tuples_only = cont->opt->tuples_only;
2563 : 28 : unsigned short opt_border = cont->opt->border;
2564 : : unsigned int i;
2565 : 28 : const char *opt_table_attr = cont->opt->tableAttr;
2566 : 28 : const char *next_opt_table_attr_char = opt_table_attr;
2567 : 28 : const char *last_opt_table_attr_char = NULL;
2568 : : const char *const *ptr;
2569 : :
9 peter@eisentraut.org 2570 [ - + ]:GNC 28 : if (CancelRequested)
5063 bruce@momjian.us 2571 :UBC 0 : return;
2572 : :
5063 bruce@momjian.us 2573 [ - + ]:CBC 28 : if (opt_border > 3)
5063 bruce@momjian.us 2574 :UBC 0 : opt_border = 3;
2575 : :
5063 bruce@momjian.us 2576 [ + - ]:CBC 28 : if (cont->opt->start_table)
2577 : : {
2578 : : /* begin environment and set alignments and borders */
2579 : 28 : fputs("\\begin{longtable}{", fout);
2580 : :
2581 [ + + ]: 28 : if (opt_border >= 2)
2582 : 12 : fputs("| ", fout);
2583 : :
2584 [ + + ]: 156 : for (i = 0; i < cont->ncolumns; i++)
2585 : : {
2586 : : /* longtable supports either a width (p) or an alignment (l/r) */
2587 : : /* Are we left-justified and was a proportional width specified? */
2588 [ + + + + ]: 128 : if (*(cont->aligns + i) == 'l' && opt_table_attr)
2589 : : {
2590 : : #define LONGTABLE_WHITESPACE " \t\n"
2591 : :
2592 : : /* advance over whitespace */
2593 : 12 : next_opt_table_attr_char += strspn(next_opt_table_attr_char,
2594 : : LONGTABLE_WHITESPACE);
2595 : : /* We have a value? */
2596 [ + + ]: 12 : if (next_opt_table_attr_char[0] != '\0')
2597 : : {
2598 : 4 : fputs("p{", fout);
2599 : 4 : fwrite(next_opt_table_attr_char, strcspn(next_opt_table_attr_char,
2600 : : LONGTABLE_WHITESPACE), 1, fout);
2601 : 4 : last_opt_table_attr_char = next_opt_table_attr_char;
2602 : 4 : next_opt_table_attr_char += strcspn(next_opt_table_attr_char,
2603 : : LONGTABLE_WHITESPACE);
2604 : 4 : fputs("\\textwidth}", fout);
2605 : : }
2606 : : /* use previous value */
2607 [ + - ]: 8 : else if (last_opt_table_attr_char != NULL)
2608 : : {
2609 : 8 : fputs("p{", fout);
2610 : 8 : fwrite(last_opt_table_attr_char, strcspn(last_opt_table_attr_char,
2611 : : LONGTABLE_WHITESPACE), 1, fout);
2612 : 8 : fputs("\\textwidth}", fout);
2613 : : }
2614 : : else
5063 bruce@momjian.us 2615 :UBC 0 : fputc('l', fout);
2616 : : }
2617 : : else
5063 bruce@momjian.us 2618 :CBC 116 : fputc(*(cont->aligns + i), fout);
2619 : :
2620 [ + + + + ]: 128 : if (opt_border != 0 && i < cont->ncolumns - 1)
2621 : 88 : fputs(" | ", fout);
2622 : : }
2623 : :
2624 [ + + ]: 28 : if (opt_border >= 2)
2625 : 12 : fputs(" |", fout);
2626 : :
2627 : 28 : fputs("}\n", fout);
2628 : :
2629 : : /* print headers */
2630 [ + + ]: 28 : if (!opt_tuples_only)
2631 : : {
2632 : : /* firsthead */
2633 [ + + ]: 24 : if (opt_border >= 2)
2634 : 12 : fputs("\\toprule\n", fout);
2635 [ + + ]: 132 : for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2636 : : {
2637 [ + + ]: 108 : if (i != 0)
2638 : 84 : fputs(" & ", fout);
2639 : 108 : fputs("\\small\\textbf{\\textit{", fout);
2640 : 108 : latex_escaped_print(*ptr, fout);
2641 : 108 : fputs("}}", fout);
2642 : : }
2643 : 24 : fputs(" \\\\\n", fout);
2644 : 24 : fputs("\\midrule\n\\endfirsthead\n", fout);
2645 : :
2646 : : /* secondary heads */
2647 [ + + ]: 24 : if (opt_border >= 2)
2648 : 12 : fputs("\\toprule\n", fout);
2649 [ + + ]: 132 : for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2650 : : {
2651 [ + + ]: 108 : if (i != 0)
2652 : 84 : fputs(" & ", fout);
2653 : 108 : fputs("\\small\\textbf{\\textit{", fout);
2654 : 108 : latex_escaped_print(*ptr, fout);
2655 : 108 : fputs("}}", fout);
2656 : : }
2657 : 24 : fputs(" \\\\\n", fout);
2658 : : /* If the line under the row already appeared, don't do another */
2659 [ + + ]: 24 : if (opt_border != 3)
2660 : 16 : fputs("\\midrule\n", fout);
2661 : 24 : fputs("\\endhead\n", fout);
2662 : :
2663 : : /* table name, caption? */
2664 [ + - + + ]: 24 : if (!opt_tuples_only && cont->title)
2665 : : {
2666 : : /* Don't output if we are printing a line under each row */
2667 [ - + ]: 4 : if (opt_border == 2)
5063 bruce@momjian.us 2668 :UBC 0 : fputs("\\bottomrule\n", fout);
5063 bruce@momjian.us 2669 :CBC 4 : fputs("\\caption[", fout);
2670 : 4 : latex_escaped_print(cont->title, fout);
2671 : 4 : fputs(" (Continued)]{", fout);
2672 : 4 : latex_escaped_print(cont->title, fout);
2673 : 4 : fputs("}\n\\endfoot\n", fout);
2674 [ - + ]: 4 : if (opt_border == 2)
5063 bruce@momjian.us 2675 :UBC 0 : fputs("\\bottomrule\n", fout);
5063 bruce@momjian.us 2676 :CBC 4 : fputs("\\caption[", fout);
2677 : 4 : latex_escaped_print(cont->title, fout);
2678 : 4 : fputs("]{", fout);
2679 : 4 : latex_escaped_print(cont->title, fout);
2680 : 4 : fputs("}\n\\endlastfoot\n", fout);
2681 : : }
2682 : : /* output bottom table line? */
2683 [ + + ]: 20 : else if (opt_border >= 2)
2684 : : {
2685 : 12 : fputs("\\bottomrule\n\\endfoot\n", fout);
2686 : 12 : fputs("\\bottomrule\n\\endlastfoot\n", fout);
2687 : : }
2688 : : }
2689 : : }
2690 : :
2691 : : /* print cells */
2692 [ + + ]: 256 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2693 : : {
2694 : : /* Add a line under each row? */
2695 [ + + + + ]: 228 : if (i != 0 && i % cont->ncolumns != 0)
2696 : 176 : fputs("\n&\n", fout);
2697 : 228 : fputs("\\raggedright{", fout);
2698 : 228 : latex_escaped_print(*ptr, fout);
2699 : 228 : fputc('}', fout);
2700 [ + + ]: 228 : if ((i + 1) % cont->ncolumns == 0)
2701 : : {
2702 : 52 : fputs(" \\tabularnewline\n", fout);
2703 [ + + ]: 52 : if (opt_border == 3)
2704 : 16 : fputs(" \\hline\n", fout);
2705 : : }
9 peter@eisentraut.org 2706 [ - + ]:GNC 228 : if (CancelRequested)
5063 bruce@momjian.us 2707 :UBC 0 : break;
2708 : : }
2709 : :
5063 bruce@momjian.us 2710 [ + - ]:CBC 28 : if (cont->opt->stop_table)
2711 : 28 : fputs("\\end{longtable}\n", fout);
2712 : : }
2713 : :
2714 : :
2715 : : static void
6774 alvherre@alvh.no-ip. 2716 : 52 : print_latex_vertical(const printTableContent *cont, FILE *fout)
2717 : : {
2718 : 52 : bool opt_tuples_only = cont->opt->tuples_only;
2719 : 52 : unsigned short opt_border = cont->opt->border;
2720 : 52 : unsigned long record = cont->opt->prior_records + 1;
2721 : : unsigned int i;
2722 : : const char *const *ptr;
2723 : :
9 peter@eisentraut.org 2724 [ - + ]:GNC 52 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2725 :UBC 0 : return;
2726 : :
7396 tgl@sss.pgh.pa.us 2727 [ + + ]:CBC 52 : if (opt_border > 2)
2728 : 12 : opt_border = 2;
2729 : :
6774 alvherre@alvh.no-ip. 2730 [ + - ]: 52 : if (cont->opt->start_table)
2731 : : {
2732 : : /* print title */
2733 [ + + + + ]: 52 : if (!opt_tuples_only && cont->title)
2734 : : {
7396 tgl@sss.pgh.pa.us 2735 : 8 : fputs("\\begin{center}\n", fout);
6774 alvherre@alvh.no-ip. 2736 : 8 : latex_escaped_print(cont->title, fout);
7396 tgl@sss.pgh.pa.us 2737 : 8 : fputs("\n\\end{center}\n\n", fout);
2738 : : }
2739 : :
2740 : : /* begin environment and set alignments and borders */
2741 : 52 : fputs("\\begin{tabular}{", fout);
2742 [ + + ]: 52 : if (opt_border == 0)
2743 : 8 : fputs("cl", fout);
2744 [ + + ]: 44 : else if (opt_border == 1)
2745 : 24 : fputs("c|l", fout);
2746 [ + - ]: 20 : else if (opt_border == 2)
2747 : 20 : fputs("|c|l|", fout);
2748 : 52 : fputs("}\n", fout);
2749 : : }
2750 : :
2751 : : /* print records */
6774 alvherre@alvh.no-ip. 2752 [ + + ]: 476 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2753 : : {
2754 : : /* new record */
2755 [ + + ]: 424 : if (i % cont->ncolumns == 0)
2756 : : {
9 peter@eisentraut.org 2757 [ - + ]:GNC 96 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2758 :UBC 0 : break;
7807 bruce@momjian.us 2759 [ + + ]:CBC 96 : if (!opt_tuples_only)
2760 : : {
9886 2761 [ + + ]: 80 : if (opt_border == 2)
2762 : : {
2763 : 40 : fputs("\\hline\n", fout);
7396 tgl@sss.pgh.pa.us 2764 : 40 : fprintf(fout, "\\multicolumn{2}{|c|}{\\textit{Record %lu}} \\\\\n", record++);
2765 : : }
2766 : : else
2767 : 40 : fprintf(fout, "\\multicolumn{2}{c}{\\textit{Record %lu}} \\\\\n", record++);
2768 : : }
9886 bruce@momjian.us 2769 [ + + ]: 96 : if (opt_border >= 1)
2770 : 80 : fputs("\\hline\n", fout);
2771 : : }
2772 : :
6774 alvherre@alvh.no-ip. 2773 : 424 : latex_escaped_print(cont->headers[i % cont->ncolumns], fout);
9886 bruce@momjian.us 2774 : 424 : fputs(" & ", fout);
2775 : 424 : latex_escaped_print(*ptr, fout);
2776 : 424 : fputs(" \\\\\n", fout);
2777 : : }
2778 : :
6774 alvherre@alvh.no-ip. 2779 [ + - ]: 52 : if (cont->opt->stop_table)
2780 : : {
7396 tgl@sss.pgh.pa.us 2781 [ + + ]: 52 : if (opt_border == 2)
2782 : 20 : fputs("\\hline\n", fout);
2783 : :
2784 : 52 : fputs("\\end{tabular}\n\n\\noindent ", fout);
2785 : :
2786 : : /* print footers */
9 peter@eisentraut.org 2787 [ + + + - :GNC 52 : if (cont->footers && !opt_tuples_only && !CancelRequested)
+ - ]
2788 : : {
2789 : : printTableFooter *f;
2790 : :
6774 alvherre@alvh.no-ip. 2791 [ + + ]:CBC 16 : for (f = cont->footers; f; f = f->next)
2792 : : {
6116 heikki.linnakangas@i 2793 : 8 : latex_escaped_print(f->data, fout);
7396 tgl@sss.pgh.pa.us 2794 : 8 : fputs(" \\\\\n", fout);
2795 : : }
2796 : : }
2797 : :
2798 : 52 : fputc('\n', fout);
2799 : : }
2800 : : }
2801 : :
2802 : :
2803 : : /*************************/
2804 : : /* Troff -ms */
2805 : : /*************************/
2806 : :
2807 : :
2808 : : static void
7842 bruce@momjian.us 2809 : 596 : troff_ms_escaped_print(const char *in, FILE *fout)
2810 : : {
2811 : : const char *p;
2812 : :
2813 [ + + ]: 4792 : for (p = in; *p; p++)
2814 [ + + ]: 4196 : switch (*p)
2815 : : {
2816 : 84 : case '\\':
2817 : 84 : fputs("\\(rs", fout);
2818 : 84 : break;
2819 : 4112 : default:
2820 : 4112 : fputc(*p, fout);
2821 : : }
2822 : 596 : }
2823 : :
2824 : :
2825 : : static void
6774 alvherre@alvh.no-ip. 2826 : 20 : print_troff_ms_text(const printTableContent *cont, FILE *fout)
2827 : : {
2828 : 20 : bool opt_tuples_only = cont->opt->tuples_only;
2829 : 20 : unsigned short opt_border = cont->opt->border;
2830 : : unsigned int i;
2831 : : const char *const *ptr;
2832 : :
9 peter@eisentraut.org 2833 [ - + ]:GNC 20 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2834 :UBC 0 : return;
2835 : :
7396 tgl@sss.pgh.pa.us 2836 [ - + ]:CBC 20 : if (opt_border > 2)
7396 tgl@sss.pgh.pa.us 2837 :UBC 0 : opt_border = 2;
2838 : :
6774 alvherre@alvh.no-ip. 2839 [ + - ]:CBC 20 : if (cont->opt->start_table)
2840 : : {
2841 : : /* print title */
2842 [ + + + + ]: 20 : if (!opt_tuples_only && cont->title)
2843 : : {
7396 tgl@sss.pgh.pa.us 2844 : 4 : fputs(".LP\n.DS C\n", fout);
6774 alvherre@alvh.no-ip. 2845 : 4 : troff_ms_escaped_print(cont->title, fout);
7396 tgl@sss.pgh.pa.us 2846 : 4 : fputs("\n.DE\n", fout);
2847 : : }
2848 : :
2849 : : /* begin environment and set alignments and borders */
2850 : 20 : fputs(".LP\n.TS\n", fout);
2851 [ + + ]: 20 : if (opt_border == 2)
2852 : 4 : fputs("center box;\n", fout);
2853 : : else
2854 : 16 : fputs("center;\n", fout);
2855 : :
6774 alvherre@alvh.no-ip. 2856 [ + + ]: 116 : for (i = 0; i < cont->ncolumns; i++)
2857 : : {
2858 : 96 : fputc(*(cont->aligns + i), fout);
2859 [ + + + + ]: 96 : if (opt_border > 0 && i < cont->ncolumns - 1)
7396 tgl@sss.pgh.pa.us 2860 : 64 : fputs(" | ", fout);
2861 : : }
2862 : 20 : fputs(".\n", fout);
2863 : :
2864 : : /* print headers */
7807 bruce@momjian.us 2865 [ + + ]: 20 : if (!opt_tuples_only)
2866 : : {
6774 alvherre@alvh.no-ip. 2867 [ + + ]: 92 : for (i = 0, ptr = cont->headers; i < cont->ncolumns; i++, ptr++)
2868 : : {
7396 tgl@sss.pgh.pa.us 2869 [ + + ]: 76 : if (i != 0)
2870 : 60 : fputc('\t', fout);
2871 : 76 : fputs("\\fI", fout);
2872 : 76 : troff_ms_escaped_print(*ptr, fout);
2873 : 76 : fputs("\\fP", fout);
2874 : : }
2875 : 16 : fputs("\n_\n", fout);
2876 : : }
2877 : : }
2878 : :
2879 : : /* print cells */
6774 alvherre@alvh.no-ip. 2880 [ + + ]: 184 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2881 : : {
6116 heikki.linnakangas@i 2882 : 164 : troff_ms_escaped_print(*ptr, fout);
2883 : :
6774 alvherre@alvh.no-ip. 2884 [ + + ]: 164 : if ((i + 1) % cont->ncolumns == 0)
2885 : : {
7842 bruce@momjian.us 2886 : 36 : fputc('\n', fout);
9 peter@eisentraut.org 2887 [ - + ]:GNC 36 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2888 :UBC 0 : break;
2889 : : }
2890 : : else
7842 bruce@momjian.us 2891 :CBC 128 : fputc('\t', fout);
2892 : : }
2893 : :
6774 alvherre@alvh.no-ip. 2894 [ + - ]: 20 : if (cont->opt->stop_table)
2895 : : {
5324 rhaas@postgresql.org 2896 : 20 : printTableFooter *footers = footers_with_default(cont);
2897 : :
7396 tgl@sss.pgh.pa.us 2898 : 20 : fputs(".TE\n.DS L\n", fout);
2899 : :
2900 : : /* print footers */
9 peter@eisentraut.org 2901 [ + - + + :GNC 20 : if (footers && !opt_tuples_only && !CancelRequested)
+ - ]
2902 : : {
2903 : : printTableFooter *f;
2904 : :
5324 rhaas@postgresql.org 2905 [ + + ]:CBC 32 : for (f = footers; f; f = f->next)
2906 : : {
6774 alvherre@alvh.no-ip. 2907 : 16 : troff_ms_escaped_print(f->data, fout);
7396 tgl@sss.pgh.pa.us 2908 : 16 : fputc('\n', fout);
2909 : : }
2910 : : }
2911 : :
2912 : 20 : fputs(".DE\n", fout);
2913 : : }
2914 : : }
2915 : :
2916 : :
2917 : : static void
6774 alvherre@alvh.no-ip. 2918 : 20 : print_troff_ms_vertical(const printTableContent *cont, FILE *fout)
2919 : : {
2920 : 20 : bool opt_tuples_only = cont->opt->tuples_only;
2921 : 20 : unsigned short opt_border = cont->opt->border;
2922 : 20 : unsigned long record = cont->opt->prior_records + 1;
2923 : : unsigned int i;
2924 : : const char *const *ptr;
7714 bruce@momjian.us 2925 : 20 : unsigned short current_format = 0; /* 0=none, 1=header, 2=body */
2926 : :
9 peter@eisentraut.org 2927 [ - + ]:GNC 20 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2928 :UBC 0 : return;
2929 : :
7396 tgl@sss.pgh.pa.us 2930 [ - + ]:CBC 20 : if (opt_border > 2)
7396 tgl@sss.pgh.pa.us 2931 :UBC 0 : opt_border = 2;
2932 : :
6774 alvherre@alvh.no-ip. 2933 [ + - ]:CBC 20 : if (cont->opt->start_table)
2934 : : {
2935 : : /* print title */
2936 [ + + + + ]: 20 : if (!opt_tuples_only && cont->title)
2937 : : {
7396 tgl@sss.pgh.pa.us 2938 : 4 : fputs(".LP\n.DS C\n", fout);
6774 alvherre@alvh.no-ip. 2939 : 4 : troff_ms_escaped_print(cont->title, fout);
7396 tgl@sss.pgh.pa.us 2940 : 4 : fputs("\n.DE\n", fout);
2941 : : }
2942 : :
2943 : : /* begin environment and set alignments and borders */
2944 : 20 : fputs(".LP\n.TS\n", fout);
2945 [ + + ]: 20 : if (opt_border == 2)
2946 : 4 : fputs("center box;\n", fout);
2947 : : else
2948 : 16 : fputs("center;\n", fout);
2949 : :
2950 : : /* basic format */
2951 [ + + ]: 20 : if (opt_tuples_only)
2952 : 4 : fputs("c l;\n", fout);
2953 : : }
2954 : : else
7396 tgl@sss.pgh.pa.us 2955 :UBC 0 : current_format = 2; /* assume tuples printed already */
2956 : :
2957 : : /* print records */
6774 alvherre@alvh.no-ip. 2958 [ + + ]:CBC 184 : for (i = 0, ptr = cont->cells; *ptr; i++, ptr++)
2959 : : {
2960 : : /* new record */
2961 [ + + ]: 164 : if (i % cont->ncolumns == 0)
2962 : : {
9 peter@eisentraut.org 2963 [ - + ]:GNC 36 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 2964 :UBC 0 : break;
7807 bruce@momjian.us 2965 [ + + ]:CBC 36 : if (!opt_tuples_only)
2966 : : {
7842 2967 [ + - ]: 28 : if (current_format != 1)
2968 : : {
7396 tgl@sss.pgh.pa.us 2969 [ + + + + ]: 28 : if (opt_border == 2 && record > 1)
7842 bruce@momjian.us 2970 : 4 : fputs("_\n", fout);
2971 [ + + ]: 28 : if (current_format != 0)
2972 : 12 : fputs(".T&\n", fout);
2973 : 28 : fputs("c s.\n", fout);
2974 : 28 : current_format = 1;
2975 : : }
7396 tgl@sss.pgh.pa.us 2976 : 28 : fprintf(fout, "\\fIRecord %lu\\fP\n", record++);
2977 : : }
7842 bruce@momjian.us 2978 [ + + ]: 36 : if (opt_border >= 1)
2979 : 28 : fputs("_\n", fout);
2980 : : }
2981 : :
7807 2982 [ + + ]: 164 : if (!opt_tuples_only)
2983 : : {
7842 2984 [ + + ]: 124 : if (current_format != 2)
2985 : : {
2986 [ + - ]: 28 : if (current_format != 0)
2987 : 28 : fputs(".T&\n", fout);
2988 [ + + ]: 28 : if (opt_border != 1)
2989 : 16 : fputs("c l.\n", fout);
2990 : : else
2991 : 12 : fputs("c | l.\n", fout);
2992 : 28 : current_format = 2;
2993 : : }
2994 : : }
2995 : :
6774 alvherre@alvh.no-ip. 2996 : 164 : troff_ms_escaped_print(cont->headers[i % cont->ncolumns], fout);
7842 bruce@momjian.us 2997 : 164 : fputc('\t', fout);
6116 heikki.linnakangas@i 2998 : 164 : troff_ms_escaped_print(*ptr, fout);
2999 : :
7842 bruce@momjian.us 3000 : 164 : fputc('\n', fout);
3001 : : }
3002 : :
6774 alvherre@alvh.no-ip. 3003 [ + - ]: 20 : if (cont->opt->stop_table)
3004 : : {
7396 tgl@sss.pgh.pa.us 3005 : 20 : fputs(".TE\n.DS L\n", fout);
3006 : :
3007 : : /* print footers */
9 peter@eisentraut.org 3008 [ + + + - :GNC 20 : if (cont->footers && !opt_tuples_only && !CancelRequested)
+ - ]
3009 : : {
3010 : : printTableFooter *f;
3011 : :
6774 alvherre@alvh.no-ip. 3012 [ + + ]:CBC 8 : for (f = cont->footers; f; f = f->next)
3013 : : {
3014 : 4 : troff_ms_escaped_print(f->data, fout);
7396 tgl@sss.pgh.pa.us 3015 : 4 : fputc('\n', fout);
3016 : : }
3017 : : }
3018 : :
3019 : 20 : fputs(".DE\n", fout);
3020 : : }
3021 : : }
3022 : :
3023 : :
3024 : : /********************************/
3025 : : /* Public functions */
3026 : : /********************************/
3027 : :
3028 : :
3029 : : /*
3030 : : * disable_sigpipe_trap
3031 : : *
3032 : : * Turn off SIGPIPE interrupt --- call this before writing to a temporary
3033 : : * query output file that is a pipe.
3034 : : *
3035 : : * No-op on Windows, where there's no SIGPIPE interrupts.
3036 : : */
3037 : : void
4013 3038 : 8 : disable_sigpipe_trap(void)
3039 : : {
3040 : : #ifndef WIN32
228 andrew@dunslane.net 3041 : 8 : pqsignal(SIGPIPE, PG_SIG_IGN);
3042 : : #endif
4013 tgl@sss.pgh.pa.us 3043 : 8 : }
3044 : :
3045 : : /*
3046 : : * restore_sigpipe_trap
3047 : : *
3048 : : * Restore normal SIGPIPE interrupt --- call this when done writing to a
3049 : : * temporary query output file that was (or might have been) a pipe.
3050 : : *
3051 : : * Note: within psql, we enable SIGPIPE interrupts unless the permanent query
3052 : : * output file is a pipe, in which case they should be kept off. This
3053 : : * approach works only because psql is not currently complicated enough to
3054 : : * have nested usages of short-lived output files. Otherwise we'd probably
3055 : : * need a genuine save-and-restore-state approach; but for now, that would be
3056 : : * useless complication. In non-psql programs, this always enables SIGPIPE.
3057 : : *
3058 : : * No-op on Windows, where there's no SIGPIPE interrupts.
3059 : : */
3060 : : void
3061 : 11428 : restore_sigpipe_trap(void)
3062 : : {
3063 : : #ifndef WIN32
228 andrew@dunslane.net 3064 [ - + ]: 11428 : pqsignal(SIGPIPE, always_ignore_sigpipe ? PG_SIG_IGN : PG_SIG_DFL);
3065 : : #endif
4013 tgl@sss.pgh.pa.us 3066 : 11428 : }
3067 : :
3068 : : /*
3069 : : * set_sigpipe_trap_state
3070 : : *
3071 : : * Set the trap state that restore_sigpipe_trap should restore to.
3072 : : */
3073 : : void
3074 : 11420 : set_sigpipe_trap_state(bool ignore)
3075 : : {
3076 : 11420 : always_ignore_sigpipe = ignore;
3077 : 11420 : }
3078 : :
3079 : :
3080 : : /*
3081 : : * PageOutput
3082 : : *
3083 : : * Tests if pager is needed and returns appropriate FILE pointer
3084 : : * (either a pipe, or stdout if we don't need the pager).
3085 : : *
3086 : : * lines: number of lines that will be printed
3087 : : * topt: print formatting options
3088 : : *
3089 : : * If the topt argument is NULL no pager is used.
3090 : : */
3091 : : FILE *
4263 andrew@dunslane.net 3092 : 615 : PageOutput(int lines, const printTableOpt *topt)
3093 : : {
417 tgl@sss.pgh.pa.us 3094 : 615 : return PageOutputInternal(lines, topt, NULL, NULL, false);
3095 : : }
3096 : :
3097 : : /*
3098 : : * Private version that allows for line-counting to be avoided when
3099 : : * not needed. If "cont" is not null then the input value of "lines"
3100 : : * is ignored and we count lines based on cont + width_wrap + vertical
3101 : : * (see count_table_lines).
3102 : : */
3103 : : static FILE *
3104 : 102097 : PageOutputInternal(int lines, const printTableOpt *topt,
3105 : : const printTableContent *cont,
3106 : : const unsigned int *width_wrap,
3107 : : bool vertical)
3108 : : {
3109 : : /* check whether we need / can / are supposed to use pager */
4263 andrew@dunslane.net 3110 [ + + + + : 102097 : if (topt && topt->pager && isatty(fileno(stdin)) && isatty(fileno(stdout)))
+ + + - ]
3111 : : {
3112 : : /* without TIOCGWINSZ, pager == 1 acts the same as pager > 1 */
3113 : : #ifdef TIOCGWINSZ
4079 3114 : 5 : unsigned short int pager = topt->pager;
3115 : 5 : int min_lines = topt->pager_min_lines;
3116 : :
417 tgl@sss.pgh.pa.us 3117 [ + - ]: 5 : if (pager == 1)
3118 : : {
3119 : : int result;
3120 : : struct winsize screen_size;
3121 : :
3122 : 5 : result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size);
3123 [ - + ]: 5 : if (result < 0)
417 tgl@sss.pgh.pa.us 3124 :UBC 0 : pager = 2; /* force use of pager */
3125 : : else
3126 : : {
417 tgl@sss.pgh.pa.us 3127 :CBC 5 : int threshold = Max(screen_size.ws_row, min_lines);
3128 : :
3129 [ + - ]: 5 : if (cont) /* caller wants us to calculate lines */
3130 : 5 : lines = count_table_lines(cont, width_wrap, vertical,
3131 : : threshold);
3132 : : /* >= accounts for a one-line prompt */
3133 [ + + ]: 5 : if (lines >= threshold)
3134 : 4 : pager = 2;
3135 : : }
3136 : : }
3137 : :
3138 [ + + ]: 5 : if (pager > 1)
3139 : : #endif
3140 : : {
3141 : : const char *pagerprog;
3142 : : FILE *pagerpipe;
3143 : :
3371 3144 : 4 : pagerprog = getenv("PSQL_PAGER");
3145 [ - + ]: 4 : if (!pagerprog)
3371 tgl@sss.pgh.pa.us 3146 :UBC 0 : pagerprog = getenv("PAGER");
8656 peter_e@gmx.net 3147 [ - + ]:CBC 4 : if (!pagerprog)
8656 peter_e@gmx.net 3148 :UBC 0 : pagerprog = DEFAULT_PAGER;
3149 : : else
3150 : : {
3151 : : /* if PAGER is empty or all-white-space, don't use pager */
3643 tgl@sss.pgh.pa.us 3152 [ - + ]:CBC 4 : if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog))
3643 tgl@sss.pgh.pa.us 3153 :UBC 0 : return stdout;
3154 : : }
1552 tgl@sss.pgh.pa.us 3155 :CBC 4 : fflush(NULL);
4013 3156 : 4 : disable_sigpipe_trap();
7396 3157 : 4 : pagerpipe = popen(pagerprog, "w");
3158 [ + - ]: 4 : if (pagerpipe)
3159 : 4 : return pagerpipe;
3160 : : /* if popen fails, silently proceed without pager */
3643 tgl@sss.pgh.pa.us 3161 :UBC 0 : restore_sigpipe_trap();
3162 : : }
3163 : : }
3164 : :
8656 peter_e@gmx.net 3165 :CBC 102093 : return stdout;
3166 : : }
3167 : :
3168 : : /*
3169 : : * ClosePager
3170 : : *
3171 : : * Close previously opened pager pipe, if any
3172 : : */
3173 : : void
7396 tgl@sss.pgh.pa.us 3174 : 619 : ClosePager(FILE *pagerpipe)
3175 : : {
3176 [ + - + + ]: 619 : if (pagerpipe && pagerpipe != stdout)
3177 : : {
3178 : : /*
3179 : : * If printing was canceled midstream, warn about it.
3180 : : *
3181 : : * Some pagers like less use Ctrl-C as part of their command set. Even
3182 : : * so, we abort our processing and warn the user what we did. If the
3183 : : * pager quit as a result of the SIGINT, this message won't go
3184 : : * anywhere ...
3185 : : */
9 peter@eisentraut.org 3186 [ - + ]:GNC 4 : if (CancelRequested)
7396 tgl@sss.pgh.pa.us 3187 :UBC 0 : fprintf(pagerpipe, _("Interrupted\n"));
3188 : :
7396 tgl@sss.pgh.pa.us 3189 :CBC 4 : pclose(pagerpipe);
4013 3190 : 4 : restore_sigpipe_trap();
3191 : : }
7396 3192 : 619 : }
3193 : :
3194 : : /*
3195 : : * Initialise a table contents struct.
3196 : : * Must be called before any other printTable method is used.
3197 : : *
3198 : : * The title is not duplicated; the caller must ensure that the buffer
3199 : : * is available for the lifetime of the printTableContent struct.
3200 : : *
3201 : : * If you call this, you must call printTableCleanup once you're done with the
3202 : : * table.
3203 : : */
3204 : : void
109 peter@eisentraut.org 3205 :GNC 102076 : printTableInit(printTableContent *content, const printTableOpt *opt,
3206 : : const char *title, int ncolumns, int nrows)
3207 : : {
3208 : : uint64 total_cells;
3209 : :
6774 alvherre@alvh.no-ip. 3210 :CBC 102076 : content->opt = opt;
3211 : 102076 : content->title = title;
3212 : 102076 : content->ncolumns = ncolumns;
3213 : 102076 : content->nrows = nrows;
3214 : :
277 michael@paquier.xyz 3215 : 102076 : content->headers = pg_malloc0_array(const char *, (ncolumns + 1));
109 peter@eisentraut.org 3216 :GNC 102076 : content->headersadded = 0;
3217 : 102076 : content->headermustfree = NULL;
3218 : :
1103 alvherre@alvh.no-ip. 3219 :CBC 102076 : total_cells = (uint64) ncolumns * nrows;
3220 : :
3221 : : /* Catch possible overflow. Using >= here allows adding 1 below */
3222 [ - + ]: 102076 : if (total_cells >= SIZE_MAX / sizeof(*content->cells))
109 peter@eisentraut.org 3223 :UNC 0 : pg_fatal("cannot print table contents: number of cells %" PRIu64 " is equal to or exceeds maximum %zu",
3224 : : total_cells, SIZE_MAX / sizeof(*content->cells));
3225 : :
277 michael@paquier.xyz 3226 :GNC 102076 : content->cells = pg_malloc0_array(const char *, (total_cells + 1));
109 peter@eisentraut.org 3227 : 102076 : content->cellsadded = 0;
6116 heikki.linnakangas@i 3228 :CBC 102076 : content->cellmustfree = NULL;
3229 : :
6774 alvherre@alvh.no-ip. 3230 : 102076 : content->footers = NULL;
3231 : :
277 michael@paquier.xyz 3232 : 102076 : content->aligns = pg_malloc0_array(char, (ncolumns + 1));
3233 : :
6774 alvherre@alvh.no-ip. 3234 : 102076 : content->footer = content->footers;
3235 : 102076 : }
3236 : :
3237 : : /*
3238 : : * Add a header to the table.
3239 : : *
3240 : : * Headers are not duplicated; you must ensure that the header string is
3241 : : * available for the lifetime of the printTableContent struct.
3242 : : *
3243 : : * If translate is true, the function will pass the header through gettext.
3244 : : * Otherwise, the header will not be translated.
3245 : : *
3246 : : * align is either 'l' or 'r', and specifies the alignment for cells in this
3247 : : * column.
3248 : : */
3249 : : void
109 peter@eisentraut.org 3250 :GNC 192031 : printTableAddHeader(printTableContent *content, const char *header,
3251 : : bool translate, char align)
3252 : : {
3253 : 192031 : bool mustfree = false;
3254 : :
3255 [ - + ]: 192031 : if (content->headersadded >= content->ncolumns)
109 peter@eisentraut.org 3256 :UNC 0 : pg_fatal("cannot add header to table content: column count of %d exceeded",
3257 : : content->ncolumns);
3258 : :
6774 alvherre@alvh.no-ip. 3259 [ + + ]:GNC 192031 : if (translate)
109 peter@eisentraut.org 3260 : 26839 : header = _(header);
3261 : :
3262 : : /*
3263 : : * Note: Translated strings are not checked for encoding validity. These
3264 : : * are provided by ourselves, so they had better be ok. And if they were
3265 : : * not, running mbvalidate on them could overwrite gettext-owned memory.
3266 : : */
95 3267 [ + + - + ]: 192031 : if (!translate && !mb_is_valid((const unsigned char *) header, content->opt->encoding))
3268 : : {
3269 : : char *header2;
3270 : :
109 peter@eisentraut.org 3271 :UNC 0 : header2 = pg_strdup(header);
3272 : 0 : header = (char *) mbvalidate((unsigned char *) header2, content->opt->encoding);
3273 : 0 : mustfree = true;
3274 : : }
3275 : :
109 peter@eisentraut.org 3276 :GNC 192031 : content->headers[content->headersadded] = header;
3277 : 192031 : content->aligns[content->headersadded] = align;
3278 : :
3279 [ - + ]: 192031 : if (mustfree)
3280 : : {
109 peter@eisentraut.org 3281 [ # # ]:UNC 0 : if (content->headermustfree == NULL)
3282 : 0 : content->headermustfree =
3283 : 0 : pg_malloc0_array(bool, (content->ncolumns + 1));
3284 : :
3285 : 0 : content->headermustfree[content->headersadded] = true;
3286 : : }
3287 : :
109 peter@eisentraut.org 3288 :GNC 192031 : content->headersadded++;
6774 alvherre@alvh.no-ip. 3289 :CBC 192031 : }
3290 : :
3291 : : /*
3292 : : * Add a cell to the table.
3293 : : *
3294 : : * Cells are not duplicated; you must ensure that the cell string is available
3295 : : * for the lifetime of the printTableContent struct.
3296 : : *
3297 : : * If translate is true, the function will pass the cell through gettext.
3298 : : * Otherwise, the cell will not be translated.
3299 : : *
3300 : : * If mustfree is true, the cell string is freed by printTableCleanup().
3301 : : * Note: Automatic freeing of translatable strings is not supported.
3302 : : */
3303 : : void
109 peter@eisentraut.org 3304 :GNC 2589711 : printTableAddCell(printTableContent *content, const char *cell,
3305 : : bool translate, bool mustfree)
3306 : : {
3307 : : uint64 total_cells;
3308 : :
1103 alvherre@alvh.no-ip. 3309 :CBC 2589711 : total_cells = (uint64) content->ncolumns * content->nrows;
3310 : :
3311 [ - + ]: 2589711 : if (content->cellsadded >= total_cells)
109 peter@eisentraut.org 3312 :UNC 0 : pg_fatal("cannot add cell to table content: total cell count of %" PRIu64 " exceeded",
3313 : : total_cells);
3314 : :
109 peter@eisentraut.org 3315 [ + + - + ]:GNC 2589711 : Assert(!(translate && mustfree));
3316 : :
6774 alvherre@alvh.no-ip. 3317 [ + + ]:CBC 2589711 : if (translate)
109 peter@eisentraut.org 3318 :GNC 1211 : cell = _(cell);
3319 : :
3320 : : /*
3321 : : * Note: Translated strings are not checked for encoding validity. These
3322 : : * are provided by ourselves, so they had better be ok. And if they were
3323 : : * not, running mbvalidate on them could overwrite gettext-owned memory.
3324 : : */
95 3325 [ + + + + ]: 2589711 : if (!translate && !mb_is_valid((const unsigned char *) cell, content->opt->encoding))
3326 : : {
3327 : : char *cell2;
3328 : :
3329 : : /*
3330 : : * If mustfree is already true, then we own the memory and can have
3331 : : * mbvalidate() overwrite it directly.
3332 : : */
109 3333 [ - + ]: 16 : if (mustfree)
109 peter@eisentraut.org 3334 :UNC 0 : cell2 = unconstify(char *, cell);
3335 : : else
3336 : : {
109 peter@eisentraut.org 3337 :GNC 16 : cell2 = pg_strdup(cell);
3338 : 16 : mustfree = true;
3339 : : }
3340 : 16 : cell = (char *) mbvalidate((unsigned char *) cell2, content->opt->encoding);
3341 : : }
3342 : :
3343 : 2589711 : content->cells[content->cellsadded] = cell;
3344 : :
6116 heikki.linnakangas@i 3345 [ + + ]:CBC 2589711 : if (mustfree)
3346 : : {
3347 [ + + ]: 366 : if (content->cellmustfree == NULL)
5170 tgl@sss.pgh.pa.us 3348 : 278 : content->cellmustfree =
277 michael@paquier.xyz 3349 : 278 : pg_malloc0_array(bool, (total_cells + 1));
3350 : :
6116 heikki.linnakangas@i 3351 : 366 : content->cellmustfree[content->cellsadded] = true;
3352 : : }
3353 : :
3354 : 2589711 : content->cellsadded++;
6774 alvherre@alvh.no-ip. 3355 : 2589711 : }
3356 : :
3357 : : /*
3358 : : * Add a footer to the table.
3359 : : *
3360 : : * Footers are added as elements of a singly-linked list, and the content is
3361 : : * strdup'd, so there is no need to keep the original footer string around.
3362 : : *
3363 : : * Footers are never translated by the function. If you want the footer
3364 : : * translated you must do so yourself, before calling printTableAddFooter. The
3365 : : * reason this works differently to headers and cells is that footers tend to
3366 : : * be made of up individually translated components, rather than being
3367 : : * translated as a whole.
3368 : : */
3369 : : void
109 peter@eisentraut.org 3370 :GNC 8911 : printTableAddFooter(printTableContent *content, const char *footer)
3371 : : {
3372 : : printTableFooter *f;
3373 : :
277 michael@paquier.xyz 3374 :CBC 8911 : f = pg_malloc0_object(printTableFooter);
6774 alvherre@alvh.no-ip. 3375 : 8911 : f->data = pg_strdup(footer);
3376 : :
3377 [ + + ]: 8911 : if (content->footers == NULL)
3378 : 2646 : content->footers = f;
3379 : : else
3380 : 6265 : content->footer->next = f;
3381 : :
3382 : 8911 : content->footer = f;
3383 : 8911 : }
3384 : :
3385 : : /*
3386 : : * Change the content of the last-added footer.
3387 : : *
3388 : : * The current contents of the last-added footer are freed, and replaced by the
3389 : : * content given in *footer. If there was no previous footer, add a new one.
3390 : : *
3391 : : * The content is strdup'd, so there is no need to keep the original string
3392 : : * around.
3393 : : */
3394 : : void
109 peter@eisentraut.org 3395 :GNC 20 : printTableSetFooter(printTableContent *content, const char *footer)
3396 : : {
6774 alvherre@alvh.no-ip. 3397 [ + - ]:CBC 20 : if (content->footers != NULL)
3398 : : {
3399 : 20 : free(content->footer->data);
3400 : 20 : content->footer->data = pg_strdup(footer);
3401 : : }
3402 : : else
6774 alvherre@alvh.no-ip. 3403 :UBC 0 : printTableAddFooter(content, footer);
6774 alvherre@alvh.no-ip. 3404 :CBC 20 : }
3405 : :
3406 : : /*
3407 : : * Free all memory allocated to this struct.
3408 : : *
3409 : : * Once this has been called, the struct is unusable unless you pass it to
3410 : : * printTableInit() again.
3411 : : */
3412 : : void
109 peter@eisentraut.org 3413 :GNC 102076 : printTableCleanup(printTableContent *content)
3414 : : {
3415 [ - + ]: 102076 : if (content->headermustfree)
3416 : : {
109 peter@eisentraut.org 3417 [ # # ]:UNC 0 : for (uint64 i = 0; i < content->ncolumns; i++)
3418 : : {
3419 [ # # ]: 0 : if (content->headermustfree[i])
3420 : 0 : free(unconstify(char *, content->headers[i]));
3421 : : }
3422 : 0 : free(content->headermustfree);
3423 : 0 : content->headermustfree = NULL;
3424 : : }
6116 heikki.linnakangas@i 3425 [ + + ]:CBC 102076 : if (content->cellmustfree)
3426 : : {
3427 : : uint64 total_cells;
3428 : :
1103 alvherre@alvh.no-ip. 3429 : 278 : total_cells = (uint64) content->ncolumns * content->nrows;
3430 [ + + ]: 4402 : for (uint64 i = 0; i < total_cells; i++)
3431 : : {
6116 heikki.linnakangas@i 3432 [ + + ]: 4124 : if (content->cellmustfree[i])
2860 peter@eisentraut.org 3433 : 366 : free(unconstify(char *, content->cells[i]));
3434 : : }
6116 heikki.linnakangas@i 3435 : 278 : free(content->cellmustfree);
3436 : 278 : content->cellmustfree = NULL;
3437 : : }
6774 alvherre@alvh.no-ip. 3438 : 102076 : free(content->headers);
3439 : 102076 : free(content->cells);
3440 : 102076 : free(content->aligns);
3441 : :
3442 : 102076 : content->opt = NULL;
3443 : 102076 : content->title = NULL;
3444 : 102076 : content->headers = NULL;
3445 : 102076 : content->cells = NULL;
3446 : 102076 : content->aligns = NULL;
109 peter@eisentraut.org 3447 :GNC 102076 : content->headersadded = 0;
3448 : 102076 : content->cellsadded = 0;
3449 : :
6774 alvherre@alvh.no-ip. 3450 [ + + ]:CBC 102076 : if (content->footers)
3451 : : {
3452 [ + + ]: 11557 : for (content->footer = content->footers; content->footer;)
3453 : : {
3454 : : printTableFooter *f;
3455 : :
3456 : 8911 : f = content->footer;
3457 : 8911 : content->footer = f->next;
3458 : 8911 : free(f->data);
3459 : 8911 : free(f);
3460 : : }
3461 : : }
3462 : 102076 : content->footers = NULL;
3463 : 102076 : content->footer = NULL;
3464 : 102076 : }
3465 : :
3466 : : /*
3467 : : * IsPagerNeeded
3468 : : *
3469 : : * Setup pager if required
3470 : : *
3471 : : * cont: table data to be printed
3472 : : * width_wrap[]: per-column maximum width, or NULL if caller will not wrap
3473 : : * vertical: vertical mode?
3474 : : * fout: where to print to (in/out argument)
3475 : : * is_pager: output argument
3476 : : *
3477 : : * If we decide pager is needed, *fout is modified and *is_pager is set true
3478 : : */
3479 : : static void
417 tgl@sss.pgh.pa.us 3480 : 101518 : IsPagerNeeded(const printTableContent *cont, const unsigned int *width_wrap,
3481 : : bool vertical,
3482 : : FILE **fout, bool *is_pager)
3483 : : {
6770 bruce@momjian.us 3484 [ + + ]: 101518 : if (*fout == stdout)
3485 : : {
417 tgl@sss.pgh.pa.us 3486 : 101482 : *fout = PageOutputInternal(0, cont->opt, cont, width_wrap, vertical);
3487 : 101482 : *is_pager = (*fout != stdout);
3488 : : }
3489 : : else
3490 : 36 : *is_pager = false;
3491 : 101518 : }
3492 : :
3493 : : /*
3494 : : * Count the number of lines needed to print the given table.
3495 : : *
3496 : : * cont: table data to be printed
3497 : : * width_wrap[]: per-column maximum width, or NULL if caller will not wrap
3498 : : * vertical: vertical mode?
3499 : : * threshold: we can stop counting once we pass this many lines
3500 : : *
3501 : : * The result is currently only fully accurate for ALIGNED/WRAPPED and
3502 : : * UNALIGNED formats; otherwise it's an approximation.
3503 : : *
3504 : : * Note: while cont->opt will tell us most formatting details, we need the
3505 : : * separate "vertical" flag because of the possibility of a dynamic switch
3506 : : * from aligned_text to aligned_vertical format.
3507 : : *
3508 : : * The point of the threshold parameter is that when the table is very long,
3509 : : * we'll typically be able to stop scanning after not many rows.
3510 : : */
3511 : : #ifdef NEED_COUNT_TABLE_LINES
3512 : : static int
3513 : 5 : count_table_lines(const printTableContent *cont,
3514 : : const unsigned int *width_wrap,
3515 : : bool vertical,
3516 : : int threshold)
3517 : : {
3518 : : int *header_height;
3519 : 5 : int lines = 0,
3520 : 5 : max_lines = 0,
3521 : : nl_lines,
3522 : : i;
3523 : 5 : int encoding = cont->opt->encoding;
3524 : : const char *const *cell;
3525 : :
3526 : : /*
3527 : : * Scan all column headers and determine their heights. Cache the values
3528 : : * since vertical mode repeats the headers for every record.
3529 : : */
277 michael@paquier.xyz 3530 : 5 : header_height = pg_malloc_array(int, cont->ncolumns);
417 tgl@sss.pgh.pa.us 3531 [ + + ]: 16 : for (i = 0; i < cont->ncolumns; i++)
3532 : : {
3533 : 11 : pg_wcssize((const unsigned char *) cont->headers[i],
3534 : 11 : strlen(cont->headers[i]), encoding,
3535 : 11 : NULL, &header_height[i], NULL);
3536 : : }
3537 : :
3538 : : /*
3539 : : * Account for separator lines (if used), as well as the trailing blank
3540 : : * line that most formats emit.
3541 : : */
3542 [ + + - - : 5 : switch (cont->opt->format)
- - ]
3543 : : {
3544 : 1 : case PRINT_ALIGNED:
3545 : : case PRINT_WRAPPED:
3546 : :
3547 : : /*
3548 : : * Vertical mode writes one separator line per record. Normal
3549 : : * mode writes a single separator line between header and rows.
3550 : : */
3551 [ + - ]: 1 : lines = vertical ? cont->nrows : 1;
3552 : : /* Both modes add a blank line at the end */
3553 : 1 : lines++;
3554 : 1 : break;
3555 : 4 : case PRINT_UNALIGNED:
3556 : :
3557 : : /*
3558 : : * Vertical mode writes a separator (here assumed to be a newline)
3559 : : * between records. Normal mode writes nothing extra.
3560 : : */
3561 [ + + ]: 4 : if (vertical)
3562 : 1 : lines = Max(cont->nrows - 1, 0);
3563 : 4 : break;
417 tgl@sss.pgh.pa.us 3564 :UBC 0 : case PRINT_CSV:
3565 : : /* Nothing extra is added */
3566 : 0 : break;
3567 : 0 : case PRINT_HTML:
3568 : : case PRINT_ASCIIDOC:
3569 : : case PRINT_LATEX:
3570 : : case PRINT_LATEX_LONGTABLE:
3571 : : case PRINT_TROFF_MS:
3572 : :
3573 : : /*
3574 : : * These formats aren't really meant for interactive consumption,
3575 : : * so for now we won't work hard on them. Treat them like aligned
3576 : : * mode.
3577 : : */
3578 [ # # ]: 0 : lines = vertical ? cont->nrows : 1;
3579 : 0 : lines++;
3580 : 0 : break;
3581 : 0 : case PRINT_NOTHING:
3582 : : /* Shouldn't get here... */
3583 : 0 : break;
3584 : : }
3585 : :
3586 : : /* Scan all cells to count their lines */
417 tgl@sss.pgh.pa.us 3587 [ + + ]:CBC 237 : for (i = 0, cell = cont->cells; *cell; cell++)
3588 : : {
3589 : : int width;
3590 : :
3591 : : /* Count the original line breaks */
3592 : 235 : pg_wcssize((const unsigned char *) *cell, strlen(*cell), encoding,
3593 : : &width, &nl_lines, NULL);
3594 : :
3595 : : /* Count extra lines due to wrapping */
3596 [ + + + + : 235 : if (width > 0 && width_wrap && width_wrap[i])
+ - ]
3597 : 7 : nl_lines += (width - 1) / width_wrap[i];
3598 : :
3599 [ + + ]: 235 : if (vertical)
3600 : : {
3601 : : /* Pick the height of the header or cell, whichever is taller */
3602 [ + + ]: 13 : if (nl_lines > header_height[i])
3603 : 7 : lines += nl_lines;
3604 : : else
3605 : 6 : lines += header_height[i];
3606 : : }
3607 : : else
3608 : : {
3609 : : /* Remember max height in the current row */
3610 [ + + ]: 222 : if (nl_lines > max_lines)
3611 : 72 : max_lines = nl_lines;
3612 : : }
3613 : :
3614 : : /* i is the current column number: increment with wrap */
3615 [ + + ]: 235 : if (++i >= cont->ncolumns)
3616 : : {
3617 : 85 : i = 0;
3618 [ + + ]: 85 : if (!vertical)
3619 : : {
3620 : : /* At last column of each row, add tallest column height */
3621 : 72 : lines += max_lines;
3622 : 72 : max_lines = 0;
3623 : : }
3624 : : /* Stop scanning table body once we pass threshold */
3625 [ + + ]: 85 : if (lines > threshold)
3626 : 3 : break;
3627 : : }
3628 : : }
3629 : :
3630 : : /* Account for header and footer decoration */
3631 [ + + - + ]: 5 : if (!cont->opt->tuples_only && lines <= threshold)
3632 : : {
3633 : : printTableFooter *f;
3634 : :
417 tgl@sss.pgh.pa.us 3635 [ # # ]:UBC 0 : if (cont->title)
3636 : : {
3637 : : /* Add height of title */
3638 : 0 : pg_wcssize((const unsigned char *) cont->title, strlen(cont->title),
3639 : : encoding, NULL, &nl_lines, NULL);
3640 : 0 : lines += nl_lines;
3641 : : }
3642 : :
3643 [ # # ]: 0 : if (!vertical)
3644 : : {
3645 : : /* Add height of tallest header column */
3646 : 0 : max_lines = 0;
3647 [ # # ]: 0 : for (i = 0; i < cont->ncolumns; i++)
3648 : : {
3649 [ # # ]: 0 : if (header_height[i] > max_lines)
3650 : 0 : max_lines = header_height[i];
3651 : : }
3652 : 0 : lines += max_lines;
3653 : : }
3654 : :
3655 : : /*
3656 : : * Add all footer lines. Vertical mode does not use the default
3657 : : * footer, but we must include that in normal mode.
3658 : : */
3659 [ # # ]: 0 : for (f = (vertical ? cont->footers : footers_with_default(cont));
3660 [ # # ]: 0 : f != NULL; f = f->next)
3661 : : {
3662 : 0 : pg_wcssize((const unsigned char *) f->data, strlen(f->data),
3663 : : encoding, NULL, &nl_lines, NULL);
3664 : 0 : lines += nl_lines;
3665 : : /* Stop scanning footers once we pass threshold */
3666 [ # # ]: 0 : if (lines > threshold)
3667 : 0 : break;
3668 : : }
3669 : : }
3670 : :
150 peter@eisentraut.org 3671 :GNC 5 : pg_free(header_height);
3672 : :
417 tgl@sss.pgh.pa.us 3673 :CBC 5 : return lines;
3674 : : }
3675 : : #endif /* NEED_COUNT_TABLE_LINES */
3676 : :
3677 : : /*
3678 : : * Use this to print any table in the supported formats.
3679 : : *
3680 : : * cont: table data and formatting options
3681 : : * fout: where to print to
3682 : : * is_pager: true if caller has already redirected fout to be a pager pipe
3683 : : * flog: if not null, also print the table there (for --log-file option)
3684 : : */
3685 : : void
4014 3686 : 102068 : printTable(const printTableContent *cont,
3687 : : FILE *fout, bool is_pager, FILE *flog)
3688 : : {
3689 : 102068 : bool is_local_pager = false;
3690 : :
9 peter@eisentraut.org 3691 [ - + ]:GNC 102068 : if (CancelRequested)
6770 bruce@momjian.us 3692 :UBC 0 : return;
3693 : :
6770 bruce@momjian.us 3694 [ - + ]:CBC 102068 : if (cont->opt->format == PRINT_NOTHING)
6770 bruce@momjian.us 3695 :UBC 0 : return;
3696 : :
3697 : : /* print_aligned_*() handle the pager themselves */
4014 tgl@sss.pgh.pa.us 3698 [ + + ]:CBC 102068 : if (!is_pager &&
3699 [ + + ]: 101968 : cont->opt->format != PRINT_ALIGNED &&
5495 peter_e@gmx.net 3700 [ + + ]: 11283 : cont->opt->format != PRINT_WRAPPED)
3701 : : {
417 tgl@sss.pgh.pa.us 3702 : 11110 : IsPagerNeeded(cont, NULL, (cont->opt->expanded == 1), &fout, &is_pager);
4014 3703 : 11110 : is_local_pager = is_pager;
3704 : : }
3705 : :
3706 : : /* clear any pre-existing error indication on the output stream */
2070 alvherre@alvh.no-ip. 3707 : 102068 : clearerr(fout);
3708 : :
3709 : : /* print the stuff */
6774 3710 [ + + + + : 102068 : switch (cont->opt->format)
+ + + +
- ]
3711 : : {
9886 bruce@momjian.us 3712 : 10842 : case PRINT_UNALIGNED:
5495 peter_e@gmx.net 3713 [ + + ]: 10842 : if (cont->opt->expanded == 1)
6770 bruce@momjian.us 3714 : 69 : print_unaligned_vertical(cont, fout);
3715 : : else
3716 : 10773 : print_unaligned_text(cont, fout);
9886 3717 : 10842 : break;
3718 : 90958 : case PRINT_ALIGNED:
3719 : : case PRINT_WRAPPED:
3720 : :
3721 : : /*
3722 : : * In expanded-auto mode, force vertical if a pager is passed in;
3723 : : * else we may make different decisions for different hunks of the
3724 : : * query result.
3725 : : */
4014 tgl@sss.pgh.pa.us 3726 [ + + ]: 90958 : if (cont->opt->expanded == 1 ||
3727 [ - + - - ]: 90557 : (cont->opt->expanded == 2 && is_pager))
3728 : 401 : print_aligned_vertical(cont, fout, is_pager);
3729 : : else
3730 : 90557 : print_aligned_text(cont, fout, is_pager);
9886 bruce@momjian.us 3731 : 90958 : break;
2924 tgl@sss.pgh.pa.us 3732 : 44 : case PRINT_CSV:
3733 [ + + ]: 44 : if (cont->opt->expanded == 1)
3734 : 12 : print_csv_vertical(cont, fout);
3735 : : else
3736 : 32 : print_csv_text(cont, fout);
3737 : 44 : break;
9886 bruce@momjian.us 3738 : 40 : case PRINT_HTML:
5495 peter_e@gmx.net 3739 [ + + ]: 40 : if (cont->opt->expanded == 1)
6770 bruce@momjian.us 3740 : 20 : print_html_vertical(cont, fout);
3741 : : else
3742 : 20 : print_html_text(cont, fout);
9886 3743 : 40 : break;
4260 3744 : 40 : case PRINT_ASCIIDOC:
3745 [ + + ]: 40 : if (cont->opt->expanded == 1)
3746 : 20 : print_asciidoc_vertical(cont, fout);
3747 : : else
3748 : 20 : print_asciidoc_text(cont, fout);
3749 : 40 : break;
9886 3750 : 48 : case PRINT_LATEX:
5495 peter_e@gmx.net 3751 [ + + ]: 48 : if (cont->opt->expanded == 1)
6770 bruce@momjian.us 3752 : 24 : print_latex_vertical(cont, fout);
3753 : : else
3754 : 24 : print_latex_text(cont, fout);
9886 3755 : 48 : break;
5063 3756 : 56 : case PRINT_LATEX_LONGTABLE:
3757 [ + + ]: 56 : if (cont->opt->expanded == 1)
3758 : 28 : print_latex_vertical(cont, fout);
3759 : : else
5062 3760 : 28 : print_latex_longtable_text(cont, fout);
5063 3761 : 56 : break;
7842 3762 : 40 : case PRINT_TROFF_MS:
5495 peter_e@gmx.net 3763 [ + + ]: 40 : if (cont->opt->expanded == 1)
6770 bruce@momjian.us 3764 : 20 : print_troff_ms_vertical(cont, fout);
3765 : : else
3766 : 20 : print_troff_ms_text(cont, fout);
7842 3767 : 40 : break;
9886 bruce@momjian.us 3768 :UBC 0 : default:
109 peter@eisentraut.org 3769 :UNC 0 : pg_fatal_internal("invalid output format (internal error): %d",
3770 : : cont->opt->format);
3771 : : }
3772 : :
4014 tgl@sss.pgh.pa.us 3773 [ + + ]:CBC 102068 : if (is_local_pager)
6770 bruce@momjian.us 3774 : 3 : ClosePager(fout);
3775 : :
3776 : : /* also produce log output if wanted */
417 tgl@sss.pgh.pa.us 3777 [ - + ]: 102068 : if (flog)
417 tgl@sss.pgh.pa.us 3778 :UBC 0 : print_aligned_text(cont, flog, false);
3779 : : }
3780 : :
3781 : : /*
3782 : : * Use this to print query results
3783 : : *
3784 : : * result: result of a successful query
3785 : : * opt: formatting options
3786 : : * fout: where to print to
3787 : : * is_pager: true if caller has already redirected fout to be a pager pipe
3788 : : * flog: if not null, also print the data there (for --log-file option)
3789 : : */
3790 : : void
4014 tgl@sss.pgh.pa.us 3791 :CBC 99104 : printQuery(const PGresult *result, const printQueryOpt *opt,
3792 : : FILE *fout, bool is_pager, FILE *flog)
3793 : : {
3794 : : printTableContent cont;
3795 : : int i,
3796 : : r,
3797 : : c;
3798 : :
9 peter@eisentraut.org 3799 [ - + ]:GNC 99104 : if (CancelRequested)
7472 tgl@sss.pgh.pa.us 3800 :UBC 0 : return;
3801 : :
6774 alvherre@alvh.no-ip. 3802 :CBC 99104 : printTableInit(&cont, &opt->topt, opt->title,
3803 : : PQnfields(result), PQntuples(result));
3804 : :
3805 : : /* Assert caller supplied enough translate_columns[] entries */
4711 tgl@sss.pgh.pa.us 3806 [ + + - + ]: 99104 : Assert(opt->translate_columns == NULL ||
3807 : : opt->n_translate_columns >= cont.ncolumns);
3808 : :
6774 alvherre@alvh.no-ip. 3809 [ + + ]: 271800 : for (i = 0; i < cont.ncolumns; i++)
3810 : : {
3811 : 172696 : printTableAddHeader(&cont, PQfname(result, i),
3886 3812 : 172696 : opt->translate_header,
3813 : 172696 : column_type_alignment(PQftype(result, i)));
3814 : : }
3815 : :
3816 : : /* set cells */
6774 3817 [ + + ]: 1317832 : for (r = 0; r < cont.nrows; r++)
3818 : : {
3819 [ + + ]: 3766417 : for (c = 0; c < cont.ncolumns; c++)
3820 : : {
3821 : : char *cell;
6116 heikki.linnakangas@i 3822 : 2547689 : bool mustfree = false;
3823 : : bool translate;
3824 : :
6774 alvherre@alvh.no-ip. 3825 [ + + ]: 2547689 : if (PQgetisnull(result, r, c))
3826 [ + + ]: 44790 : cell = opt->nullPrint ? opt->nullPrint : "";
390 alvherre@kurilemu.de 3827 [ + + ]: 2502899 : else if (PQftype(result, c) == BOOLOID)
3828 : 97452 : cell = (PQgetvalue(result, r, c)[0] == 't' ?
3829 [ + + + + ]: 76249 : (opt->truePrint ? opt->truePrint : "t") :
3830 [ + + ]: 27523 : (opt->falsePrint ? opt->falsePrint : "f"));
3831 : : else
3832 : : {
6774 alvherre@alvh.no-ip. 3833 : 2454173 : cell = PQgetvalue(result, r, c);
6116 heikki.linnakangas@i 3834 [ + + + + ]: 2454173 : if (cont.aligns[c] == 'r' && opt->topt.numericLocale)
3835 : : {
3836 : 64 : cell = format_numeric_locale(cell);
3837 : 64 : mustfree = true;
3838 : : }
3839 : : }
3840 : :
6711 bruce@momjian.us 3841 [ + + + + ]: 2547689 : translate = (opt->translate_columns && opt->translate_columns[c]);
6116 heikki.linnakangas@i 3842 : 2547689 : printTableAddCell(&cont, cell, translate, mustfree);
3843 : : }
3844 : : }
3845 : :
3846 : : /* set footers */
6774 alvherre@alvh.no-ip. 3847 [ + + ]: 99104 : if (opt->footers)
3848 : : {
3849 : : char **footer;
3850 : :
3851 [ + + ]: 276 : for (footer = opt->footers; *footer; footer++)
3852 : 136 : printTableAddFooter(&cont, *footer);
3853 : : }
3854 : :
4014 tgl@sss.pgh.pa.us 3855 : 99104 : printTable(&cont, fout, is_pager, flog);
6774 alvherre@alvh.no-ip. 3856 : 99104 : printTableCleanup(&cont);
3857 : : }
3858 : :
3859 : : char
3886 3860 : 172824 : column_type_alignment(Oid ftype)
3861 : : {
3862 : : char align;
3863 : :
3864 [ + + ]: 172824 : switch (ftype)
3865 : : {
3866 : 67035 : case INT2OID:
3867 : : case INT4OID:
3868 : : case INT8OID:
3869 : : case FLOAT4OID:
3870 : : case FLOAT8OID:
3871 : : case NUMERICOID:
3872 : : case OIDOID:
3873 : : case OID8OID:
3874 : : case XIDOID:
3875 : : case XID8OID:
3876 : : case CIDOID:
3877 : : case MONEYOID:
3878 : 67035 : align = 'r';
3879 : 67035 : break;
3880 : 105789 : default:
3881 : 105789 : align = 'l';
3882 : 105789 : break;
3883 : : }
3884 : 172824 : return align;
3885 : : }
3886 : :
3887 : : void
7807 bruce@momjian.us 3888 : 11566 : setDecimalLocale(void)
3889 : : {
3890 : : struct lconv *extlconv;
3891 : :
3892 : 11566 : extlconv = localeconv();
3893 : :
3894 : : /* Don't accept an empty decimal_point string */
3895 [ + - ]: 11566 : if (*extlconv->decimal_point)
6774 alvherre@alvh.no-ip. 3896 : 11566 : decimal_point = pg_strdup(extlconv->decimal_point);
3897 : : else
7807 bruce@momjian.us 3898 :UBC 0 : decimal_point = "."; /* SQL output standard */
3899 : :
3900 : : /*
3901 : : * Although the Open Group standard allows locales to supply more than one
3902 : : * group width, we consider only the first one, and we ignore any attempt
3903 : : * to suppress grouping by specifying CHAR_MAX. As in the backend's
3904 : : * cash.c, we must apply a range check to avoid being fooled by variant
3905 : : * CHAR_MAX values.
3906 : : */
4082 tgl@sss.pgh.pa.us 3907 :CBC 11566 : groupdigits = *extlconv->grouping;
3908 [ + + - + ]: 11566 : if (groupdigits <= 0 || groupdigits > 6)
4083 3909 : 10872 : groupdigits = 3; /* most common */
3910 : :
3911 : : /* Don't accept an empty thousands_sep string, either */
3912 : : /* similar code exists in formatting.c */
7807 bruce@momjian.us 3913 [ + + ]: 11566 : if (*extlconv->thousands_sep)
6774 alvherre@alvh.no-ip. 3914 : 694 : thousands_sep = pg_strdup(extlconv->thousands_sep);
3915 : : /* Make sure thousands separator doesn't match decimal point symbol. */
6947 bruce@momjian.us 3916 [ + - ]: 10872 : else if (strcmp(decimal_point, ",") != 0)
7807 3917 : 10872 : thousands_sep = ",";
3918 : : else
7807 bruce@momjian.us 3919 :UBC 0 : thousands_sep = ".";
7807 bruce@momjian.us 3920 :CBC 11566 : }
3921 : :
3922 : : /* get selected or default line style */
3923 : : const printTextFormat *
6255 tgl@sss.pgh.pa.us 3924 : 92124 : get_line_style(const printTableOpt *opt)
3925 : : {
3926 : : /*
3927 : : * Note: this function mainly exists to preserve the convention that a
3928 : : * printTableOpt struct can be initialized to zeroes to get default
3929 : : * behavior.
3930 : : */
3931 [ + + ]: 92124 : if (opt->line_style != NULL)
3932 : 1996 : return opt->line_style;
3933 : : else
3934 : 90128 : return &pg_asciiformat;
3935 : : }
3936 : :
3937 : : void
4460 sfrost@snowman.net 3938 : 11566 : refresh_utf8format(const printTableOpt *opt)
3939 : : {
3913 tgl@sss.pgh.pa.us 3940 : 11566 : printTextFormat *popt = &pg_utf8format;
3941 : :
3942 : : const unicodeStyleBorderFormat *border;
3943 : : const unicodeStyleRowFormat *header;
3944 : : const unicodeStyleColumnFormat *column;
3945 : :
4460 sfrost@snowman.net 3946 : 11566 : popt->name = "unicode";
3947 : :
3948 : 11566 : border = &unicode_style.border_style[opt->unicode_border_linestyle];
3949 : 11566 : header = &unicode_style.row_style[opt->unicode_header_linestyle];
3950 : 11566 : column = &unicode_style.column_style[opt->unicode_column_linestyle];
3951 : :
3952 : 11566 : popt->lrule[PRINT_RULE_TOP].hrule = border->horizontal;
3953 : 11566 : popt->lrule[PRINT_RULE_TOP].leftvrule = border->down_and_right;
3954 : 11566 : popt->lrule[PRINT_RULE_TOP].midvrule = column->down_and_horizontal[opt->unicode_border_linestyle];
3955 : 11566 : popt->lrule[PRINT_RULE_TOP].rightvrule = border->down_and_left;
3956 : :
3957 : 11566 : popt->lrule[PRINT_RULE_MIDDLE].hrule = header->horizontal;
3958 : 11566 : popt->lrule[PRINT_RULE_MIDDLE].leftvrule = header->vertical_and_right[opt->unicode_border_linestyle];
3959 : 11566 : popt->lrule[PRINT_RULE_MIDDLE].midvrule = column->vertical_and_horizontal[opt->unicode_header_linestyle];
3960 : 11566 : popt->lrule[PRINT_RULE_MIDDLE].rightvrule = header->vertical_and_left[opt->unicode_border_linestyle];
3961 : :
3962 : 11566 : popt->lrule[PRINT_RULE_BOTTOM].hrule = border->horizontal;
3963 : 11566 : popt->lrule[PRINT_RULE_BOTTOM].leftvrule = border->up_and_right;
3964 : 11566 : popt->lrule[PRINT_RULE_BOTTOM].midvrule = column->up_and_horizontal[opt->unicode_border_linestyle];
3965 : 11566 : popt->lrule[PRINT_RULE_BOTTOM].rightvrule = border->left_and_right;
3966 : :
3967 : : /* N/A */
3968 : 11566 : popt->lrule[PRINT_RULE_DATA].hrule = "";
3969 : 11566 : popt->lrule[PRINT_RULE_DATA].leftvrule = border->vertical;
3970 : 11566 : popt->lrule[PRINT_RULE_DATA].midvrule = column->vertical;
3971 : 11566 : popt->lrule[PRINT_RULE_DATA].rightvrule = border->vertical;
3972 : :
3973 : 11566 : popt->midvrule_nl = column->vertical;
3974 : 11566 : popt->midvrule_wrap = column->vertical;
3975 : 11566 : popt->midvrule_blank = column->vertical;
3976 : :
3977 : : /* Same for all unicode today */
3978 : 11566 : popt->header_nl_left = unicode_style.header_nl_left;
3979 : 11566 : popt->header_nl_right = unicode_style.header_nl_right;
3980 : 11566 : popt->nl_left = unicode_style.nl_left;
3981 : 11566 : popt->nl_right = unicode_style.nl_right;
3982 : 11566 : popt->wrap_left = unicode_style.wrap_left;
3983 : 11566 : popt->wrap_right = unicode_style.wrap_right;
3984 : 11566 : popt->wrap_right_border = unicode_style.wrap_right_border;
3985 : 11566 : }
3986 : :
3987 : : /*
3988 : : * Compute the byte distance to the end of the string or *target_width
3989 : : * display character positions, whichever comes first. Update *target_width
3990 : : * to be the number of display character positions actually filled.
3991 : : */
3992 : : static int
6778 bruce@momjian.us 3993 : 778327 : strlen_max_width(unsigned char *str, int *target_width, int encoding)
3994 : : {
3995 : 778327 : unsigned char *start = str;
6776 tgl@sss.pgh.pa.us 3996 : 778327 : unsigned char *end = str + strlen((char *) str);
6379 bruce@momjian.us 3997 : 778327 : int curr_width = 0;
3998 : :
6776 tgl@sss.pgh.pa.us 3999 [ + + ]: 9955024 : while (str < end)
4000 : : {
6379 bruce@momjian.us 4001 : 9177728 : int char_width = PQdsplen((char *) str, encoding);
4002 : :
4003 : : /*
4004 : : * If the display width of the new character causes the string to
4005 : : * exceed its target width, skip it and return. However, if this is
4006 : : * the first character of the string (curr_width == 0), we have to
4007 : : * accept it.
4008 : : */
6776 tgl@sss.pgh.pa.us 4009 [ + + + - ]: 9177728 : if (*target_width < curr_width + char_width && curr_width != 0)
6778 bruce@momjian.us 4010 : 1031 : break;
4011 : :
4012 : 9176697 : curr_width += char_width;
4013 : :
6776 tgl@sss.pgh.pa.us 4014 : 9176697 : str += PQmblen((char *) str, encoding);
4015 : :
2000 4016 [ - + ]: 9176697 : if (str > end) /* Don't overrun invalid string */
2000 tgl@sss.pgh.pa.us 4017 :UBC 0 : str = end;
4018 : : }
4019 : :
6778 bruce@momjian.us 4020 :CBC 778327 : *target_width = curr_width;
4021 : :
4022 : 778327 : return str - start;
4023 : : }
|