32#include <sys/termios.h>
42 const int i,
const int j,
const int fs_len,
44 const int nFields,
const char **fieldNames,
45 unsigned char *fieldNotNum,
int *fieldMax,
46 const int fieldMaxLen, FILE *fout);
48 int *fieldMax,
const char **fieldNames,
unsigned char *fieldNotNum,
49 const int fs_len,
const PGresult *res);
51 unsigned char *fieldNotNum,
int *fieldMax,
char *border,
53static void fill(
int length,
int max,
char filler, FILE *fp);
81 unsigned char *fieldNotNum = NULL;
84 const char **fieldNames = NULL;
88 int total_line_length = 0;
94 bool sigpipe_masked =
false;
99 struct winsize screen_size;
119 fieldNames = (
const char **)
calloc(nFields,
sizeof(
char *));
120 fieldNotNum = (
unsigned char *)
calloc(nFields, 1);
121 fieldMax = (
int *)
calloc(nFields,
sizeof(
int));
122 if (!fieldNames || !fieldNotNum || !fieldMax)
127 for (numFieldName = 0;
131 for (
j = 0;
j < nFields;
j++)
134 const char *s = (
j < numFieldName && po->
fieldName[
j][0]) ?
138 len = s ? strlen(s) : 0;
141 if (
len > fieldMaxLen)
143 total_line_length +=
len;
146 total_line_length += nFields * strlen(po->
fieldSep) + 1;
150 if (po->
pager && fout ==
stdout && isatty(fileno(stdin)) &&
158 if (ioctl(fileno(
stdout), TIOCGWINSZ, &screen_size) == -1 ||
159 screen_size.ws_col == 0 ||
160 screen_size.ws_row == 0)
162 screen_size.ws_row = 24;
163 screen_size.ws_col = 80;
166 screen_size.ws_row = 24;
167 screen_size.ws_col = 80;
176 pagerenv = getenv(
"PAGER");
178 if (pagerenv != NULL &&
179 strspn(pagerenv,
" \t\r\n") != strlen(pagerenv) &&
182 nTups * (nFields + 1) >= screen_size.ws_row) ||
184 nTups * (total_line_length / screen_size.ws_col + 1) *
185 (1 + (po->
standard != 0)) >= screen_size.ws_row -
187 (total_line_length / screen_size.ws_col + 1) * 2
192 fout = popen(pagerenv,
"w");
198 sigpipe_masked =
true;
208 fields = (
char **)
calloc((
size_t) nTups + 1,
209 nFields *
sizeof(
char *));
230 for (
j = 0;
j < nFields;
j++)
232 const char *s = fieldNames[
j];
235 len += strlen(s) + fs_len;
236 if ((
j + 1) < nFields)
240 for (
len -= fs_len;
len--; fputc(
'-', fout));
251 "Query retrieved %d rows * %d fields"
255 for (
i = 0;
i < nTups;
i++)
261 "<table %s><caption align=\"top\">%d</caption>\n",
266 for (
j = 0;
j < nFields;
j++)
268 if (!
do_field(po, res,
i,
j, fs_len, fields, nFields,
269 fieldNames, fieldNotNum,
270 fieldMax, fieldMaxLen, fout))
274 fputs(
"</table>\n", fout);
284 "<table %s><caption align=\"top\">%s</caption>\n",
289 "<table %s><caption align=\"top\">"
290 "Retrieved %d rows * %d fields"
298 border =
do_header(fout, po, nFields, fieldMax, fieldNames,
299 fieldNotNum, fs_len, res);
300 for (
i = 0;
i < nTups;
i++)
302 fieldNotNum, fieldMax, border,
i);
308 fputs(
"</table>\n", fout);
317 size_t numfields = ((size_t) nTups + 1) * (size_t) nFields;
319 while (numfields-- > 0)
320 free(fields[numfields]);
342 const int i,
const int j,
const int fs_len,
344 const int nFields,
char const **fieldNames,
345 unsigned char *fieldNotNum,
int *fieldMax,
346 const int fieldMaxLen, FILE *fout)
356 if (plen < 1 || !pval || !*pval)
371 if (po->
align && !fieldNotNum[
j])
379 if (!((ch >=
'0' && ch <=
'9') ||
396 if (*pval ==
'E' || *pval ==
'e' ||
397 !(ch >=
'0' && ch <=
'9'))
403 if (plen > fieldMax[
j])
405 if (!(fields[
i * nFields +
j] = (
char *)
malloc((
size_t) plen + 1)))
410 strcpy(fields[
i * nFields +
j], pval);
418 "<tr><td align=\"left\"><b>%s</b></td>"
419 "<td align=\"%s\">%s</td></tr>\n",
421 fieldNotNum[
j] ?
"left" :
"right",
428 fieldMaxLen - fs_len, fieldNames[
j],
443 if ((
j + 1) < nFields)
457 const char **fieldNames,
unsigned char *fieldNotNum,
458 const int fs_len,
const PGresult *res)
472 for (; n < nFields; n++)
505 for (
j = 0;
j < nFields;
j++)
524 for (
j = 0;
j < nFields;
j++)
530 fprintf(fout,
"<th align=\"%s\">%s</th>",
531 fieldNotNum[
j] ?
"left" :
"right", fieldNames[
j]);
541 fieldNotNum[
j] ?
" %-*s " :
" %*s ",
544 fprintf(fout, fieldNotNum[
j] ?
"%-*s" :
"%*s", fieldMax[
j], s);
550 fputs(
"</tr>\n", fout);
552 fprintf(fout,
"\n%s\n", border);
563 unsigned char *fieldNotNum,
int *fieldMax,
char *border,
572 for (field_index = 0; field_index < nFields; field_index++)
574 char *p = fields[row_index * nFields + field_index];
577 fprintf(fout,
"<td align=\"%s\">%s</td>",
578 fieldNotNum[field_index] ?
"left" :
"right", p ? p :
"");
582 fieldNotNum[field_index] ?
583 (po->
standard ?
" %-*s " :
"%-*s") :
585 fieldMax[field_index],
587 if (po->
standard || field_index + 1 < nFields)
592 fputs(
"</tr>", fout);
608 const char *fieldSep,
613#define DEFAULT_FIELD_SEP " "
621 if (fieldSep == NULL)
635 fLength = (
int *)
malloc(nFields *
sizeof(
int));
642 for (
j = 0;
j < nFields;
j++)
645 for (
i = 0;
i < nTuples;
i++)
649 if (flen > fLength[
j])
658 for (
i = 0;
i < nFields;
i++)
668 for (
i = 0;
i < nFields;
i++)
671 fill(0, fLength[
i],
'-', fp);
678 for (
i = 0;
i < nTuples;
i++)
680 for (
j = 0;
j < nFields;
j++)
713 char formatString[80];
714 char *tborder = NULL;
720 sprintf(formatString,
"%%s %%-%ds", colWidth);
722 sprintf(formatString,
"%%s %%s");
731 width = nFields * 14;
732 tborder = (
char *)
malloc(width + 1);
738 for (
i = 0;
i < width;
i++)
740 tborder[width] =
'\0';
741 fprintf(fout,
"%s\n", tborder);
744 for (
i = 0;
i < nFields;
i++)
749 TerseOutput ?
"" :
"|",
759 fprintf(fout,
"|\n%s\n", tborder);
762 for (
i = 0;
i < nTups;
i++)
764 for (
j = 0;
j < nFields;
j++)
769 TerseOutput ?
"" :
"|",
775 fprintf(fout,
"|\n%s\n", tborder);
786fill(
int length,
int max,
char filler, FILE *fp)
790 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 pg_add_size_overflow(size_t a, size_t b, size_t *result)
void pq_reset_sigpipe(sigset_t *osigset, bool sigpipe_pending, bool got_epipe)
int pq_block_sigpipe(sigset_t *osigset, bool *sigpipe_pending)