32#include <sys/termios.h>
41 const int i,
const int j,
const int fs_len,
43 const int nFields,
const char **fieldNames,
44 unsigned char *fieldNotNum,
int *fieldMax,
45 const int fieldMaxLen, FILE *fout);
47 int *fieldMax,
const char **fieldNames,
unsigned char *fieldNotNum,
48 const int fs_len,
const PGresult *res);
50 unsigned char *fieldNotNum,
int *fieldMax,
char *border,
52static void fill(
int length,
int max,
char filler, FILE *fp);
80 unsigned char *fieldNotNum = NULL;
83 const char **fieldNames = NULL;
87 int total_line_length = 0;
93 bool sigpipe_masked =
false;
98 struct winsize screen_size;
118 fieldNames = (
const char **)
calloc(nFields,
sizeof(
char *));
119 fieldNotNum = (
unsigned char *)
calloc(nFields, 1);
120 fieldMax = (
int *)
calloc(nFields,
sizeof(
int));
121 if (!fieldNames || !fieldNotNum || !fieldMax)
126 for (numFieldName = 0;
130 for (
j = 0;
j < nFields;
j++)
133 const char *s = (
j < numFieldName && po->
fieldName[
j][0]) ?
137 len = s ? strlen(s) : 0;
140 if (
len > fieldMaxLen)
142 total_line_length +=
len;
145 total_line_length += nFields * strlen(po->
fieldSep) + 1;
149 if (po->
pager && fout ==
stdout && isatty(fileno(stdin)) &&
157 if (ioctl(fileno(
stdout), TIOCGWINSZ, &screen_size) == -1 ||
158 screen_size.ws_col == 0 ||
159 screen_size.ws_row == 0)
161 screen_size.ws_row = 24;
162 screen_size.ws_col = 80;
165 screen_size.ws_row = 24;
166 screen_size.ws_col = 80;
175 pagerenv = getenv(
"PAGER");
177 if (pagerenv != NULL &&
178 strspn(pagerenv,
" \t\r\n") != strlen(pagerenv) &&
181 nTups * (nFields + 1) >= screen_size.ws_row) ||
183 nTups * (total_line_length / screen_size.ws_col + 1) *
184 (1 + (po->
standard != 0)) >= screen_size.ws_row -
186 (total_line_length / screen_size.ws_col + 1) * 2
191 fout = popen(pagerenv,
"w");
197 sigpipe_masked =
true;
207 fields = (
char **)
calloc((
size_t) nTups + 1,
208 nFields *
sizeof(
char *));
229 for (
j = 0;
j < nFields;
j++)
231 const char *s = fieldNames[
j];
234 len += strlen(s) + fs_len;
235 if ((
j + 1) < nFields)
239 for (
len -= fs_len;
len--; fputc(
'-', fout));
250 "Query retrieved %d rows * %d fields"
254 for (
i = 0;
i < nTups;
i++)
260 "<table %s><caption align=\"top\">%d</caption>\n",
265 for (
j = 0;
j < nFields;
j++)
267 if (!
do_field(po, res,
i,
j, fs_len, fields, nFields,
268 fieldNames, fieldNotNum,
269 fieldMax, fieldMaxLen, fout))
273 fputs(
"</table>\n", fout);
283 "<table %s><caption align=\"top\">%s</caption>\n",
288 "<table %s><caption align=\"top\">"
289 "Retrieved %d rows * %d fields"
297 border =
do_header(fout, po, nFields, fieldMax, fieldNames,
298 fieldNotNum, fs_len, res);
299 for (
i = 0;
i < nTups;
i++)
301 fieldNotNum, fieldMax, border,
i);
307 fputs(
"</table>\n", fout);
316 size_t numfields = ((size_t) nTups + 1) * (size_t) nFields;
318 while (numfields-- > 0)
319 free(fields[numfields]);
341 const int i,
const int j,
const int fs_len,
343 const int nFields,
char const **fieldNames,
344 unsigned char *fieldNotNum,
int *fieldMax,
345 const int fieldMaxLen, FILE *fout)
355 if (plen < 1 || !pval || !*pval)
370 if (po->
align && !fieldNotNum[
j])
378 if (!((ch >=
'0' && ch <=
'9') ||
395 if (*pval ==
'E' || *pval ==
'e' ||
396 !(ch >=
'0' && ch <=
'9'))
402 if (plen > fieldMax[
j])
404 if (!(fields[
i * nFields +
j] = (
char *)
malloc((
size_t) plen + 1)))
409 strcpy(fields[
i * nFields +
j], pval);
417 "<tr><td align=\"left\"><b>%s</b></td>"
418 "<td align=\"%s\">%s</td></tr>\n",
420 fieldNotNum[
j] ?
"left" :
"right",
427 fieldMaxLen - fs_len, fieldNames[
j],
442 if ((
j + 1) < nFields)
467 if (result <
s1 || result <
s2)
477 const char **fieldNames,
unsigned char *fieldNotNum,
478 const int fs_len,
const PGresult *res)
492 for (; n < nFields; n++)
525 for (
j = 0;
j < nFields;
j++)
544 for (
j = 0;
j < nFields;
j++)
550 fprintf(fout,
"<th align=\"%s\">%s</th>",
551 fieldNotNum[
j] ?
"left" :
"right", fieldNames[
j]);
561 fieldNotNum[
j] ?
" %-*s " :
" %*s ",
564 fprintf(fout, fieldNotNum[
j] ?
"%-*s" :
"%*s", fieldMax[
j], s);
570 fputs(
"</tr>\n", fout);
572 fprintf(fout,
"\n%s\n", border);
583 unsigned char *fieldNotNum,
int *fieldMax,
char *border,
592 for (field_index = 0; field_index < nFields; field_index++)
594 char *p = fields[row_index * nFields + field_index];
597 fprintf(fout,
"<td align=\"%s\">%s</td>",
598 fieldNotNum[field_index] ?
"left" :
"right", p ? p :
"");
602 fieldNotNum[field_index] ?
603 (po->
standard ?
" %-*s " :
"%-*s") :
605 fieldMax[field_index],
607 if (po->
standard || field_index + 1 < nFields)
612 fputs(
"</tr>", fout);
628 const char *fieldSep,
633#define DEFAULT_FIELD_SEP " "
641 if (fieldSep == NULL)
655 fLength = (
int *)
malloc(nFields *
sizeof(
int));
662 for (
j = 0;
j < nFields;
j++)
665 for (
i = 0;
i < nTuples;
i++)
669 if (flen > fLength[
j])
678 for (
i = 0;
i < nFields;
i++)
688 for (
i = 0;
i < nFields;
i++)
691 fill(0, fLength[
i],
'-', fp);
698 for (
i = 0;
i < nTuples;
i++)
700 for (
j = 0;
j < nFields;
j++)
733 char formatString[80];
734 char *tborder = NULL;
740 sprintf(formatString,
"%%s %%-%ds", colWidth);
742 sprintf(formatString,
"%%s %%s");
751 width = nFields * 14;
752 tborder = (
char *)
malloc(width + 1);
758 for (
i = 0;
i < width;
i++)
760 tborder[width] =
'\0';
761 fprintf(fout,
"%s\n", tborder);
764 for (
i = 0;
i < nFields;
i++)
769 TerseOutput ?
"" :
"|",
779 fprintf(fout,
"|\n%s\n", tborder);
782 for (
i = 0;
i < nTups;
i++)
784 for (
j = 0;
j < nFields;
j++)
789 TerseOutput ?
"" :
"|",
795 fprintf(fout,
"|\n%s\n", tborder);
806fill(
int length,
int max,
char filler, FILE *fp)
810 count = max - length;
#define fprintf(file, fmt, msg)
int PQmblenBounded(const char *s, int encoding)
void PQdisplayTuples(const PGresult *res, FILE *fp, int fillAlign, const char *fieldSep, int printHeader, int quiet)
static void fill(int length, int max, char filler, FILE *fp)
#define DEFAULT_FIELD_SEP
void PQprintTuples(const PGresult *res, FILE *fout, int PrintAttNames, int TerseOutput, int colWidth)
void PQprint(FILE *fout, const PGresult *res, const PQprintOpt *po)
static char * do_header(FILE *fout, const PQprintOpt *po, const int nFields, int *fieldMax, const char **fieldNames, unsigned char *fieldNotNum, const int fs_len, const PGresult *res)
static bool do_field(const PQprintOpt *po, const PGresult *res, const int i, const int j, const int fs_len, char **fields, const int nFields, const char **fieldNames, unsigned char *fieldNotNum, int *fieldMax, const int fieldMaxLen, FILE *fout)
static void output_row(FILE *fout, const PQprintOpt *po, const int nFields, char **fields, unsigned char *fieldNotNum, int *fieldMax, char *border, const int row_index)
static bool add_size_overflow(size_t s1, size_t s2, size_t *dst)
void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)