25 #include <sys/ioctl.h>
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,
50 unsigned char *fieldNotNum,
int *fieldMax,
char *border,
52 static 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;
91 #if defined(ENABLE_THREAD_SAFETY) && !defined(WIN32)
93 bool sigpipe_masked =
false;
96 #if !defined(ENABLE_THREAD_SAFETY) && !defined(WIN32)
101 struct winsize screen_size;
111 fieldNames = (
const char **)
calloc(nFields,
sizeof(
char *));
112 fieldNotNum = (
unsigned char *)
calloc(nFields, 1);
113 fieldMax = (
int *)
calloc(nFields,
sizeof(
int));
114 if (!fieldNames || !fieldNotNum || !fieldMax)
119 for (numFieldName = 0;
123 for (
j = 0;
j < nFields;
j++)
126 const char *s = (
j < numFieldName && po->
fieldName[
j][0]) ?
130 len = s ? strlen(s) : 0;
133 if (
len > fieldMaxLen)
135 total_line_length +=
len;
138 total_line_length += nFields * strlen(po->
fieldSep) + 1;
142 if (po->
pager && fout ==
stdout && isatty(fileno(stdin)) &&
150 if (ioctl(fileno(
stdout), TIOCGWINSZ, &screen_size) == -1 ||
151 screen_size.ws_col == 0 ||
152 screen_size.ws_row == 0)
154 screen_size.ws_row = 24;
155 screen_size.ws_col = 80;
158 screen_size.ws_row = 24;
159 screen_size.ws_col = 80;
168 pagerenv = getenv(
"PAGER");
170 if (pagerenv != NULL &&
171 strspn(pagerenv,
" \t\r\n") != strlen(pagerenv) &&
174 nTups * (nFields + 1) >= screen_size.ws_row) ||
176 nTups * (total_line_length / screen_size.ws_col + 1) *
177 (1 + (po->
standard != 0)) >= screen_size.ws_row -
179 (total_line_length / screen_size.ws_col + 1) * 2
184 fout = popen(pagerenv,
"w");
189 #ifdef ENABLE_THREAD_SAFETY
190 if (pq_block_sigpipe(&osigset, &sigpipe_pending) == 0)
191 sigpipe_masked =
true;
204 fields = (
char **)
calloc((
size_t) nTups + 1,
205 nFields *
sizeof(
char *));
226 for (
j = 0;
j < nFields;
j++)
228 const char *s = fieldNames[
j];
231 len += strlen(s) + fs_len;
232 if ((
j + 1) < nFields)
236 for (
len -= fs_len;
len--; fputc(
'-', fout));
247 "Query retrieved %d rows * %d fields"
251 for (
i = 0;
i < nTups;
i++)
257 "<table %s><caption align=\"top\">%d</caption>\n",
262 for (
j = 0;
j < nFields;
j++)
265 fieldNames, fieldNotNum,
266 fieldMax, fieldMaxLen, fout))
270 fputs(
"</table>\n", fout);
280 "<table %s><caption align=\"top\">%s</caption>\n",
285 "<table %s><caption align=\"top\">"
286 "Retrieved %d rows * %d fields"
294 border =
do_header(fout, po, nFields, fieldMax, fieldNames,
295 fieldNotNum, fs_len,
res);
296 for (
i = 0;
i < nTups;
i++)
298 fieldNotNum, fieldMax, border,
i);
304 fputs(
"</table>\n", fout);
313 size_t numfields = ((size_t) nTups + 1) * (size_t) nFields;
315 while (numfields-- > 0)
316 free(fields[numfields]);
327 #ifdef ENABLE_THREAD_SAFETY
330 pq_reset_sigpipe(&osigset, sigpipe_pending,
true);
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(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,
471 for (; n < nFields; n++)
472 tot += fieldMax[n] + fs_len + (po->
standard ? 2 : 0);
474 tot += fs_len * 2 + 2;
489 for (
j = 0;
j < nFields;
j++)
508 for (
j = 0;
j < nFields;
j++)
514 fprintf(fout,
"<th align=\"%s\">%s</th>",
515 fieldNotNum[
j] ?
"left" :
"right", fieldNames[
j]);
525 fieldNotNum[
j] ?
" %-*s " :
" %*s ",
528 fprintf(fout, fieldNotNum[
j] ?
"%-*s" :
"%*s", fieldMax[
j], s);
534 fputs(
"</tr>\n", fout);
536 fprintf(fout,
"\n%s\n", border);
543 unsigned char *fieldNotNum,
int *fieldMax,
char *border,
552 for (field_index = 0; field_index < nFields; field_index++)
554 char *p = fields[row_index * nFields + field_index];
557 fprintf(fout,
"<td align=\"%s\">%s</td>",
558 fieldNotNum[field_index] ?
"left" :
"right", p ? p :
"");
562 fieldNotNum[field_index] ?
563 (po->
standard ?
" %-*s " :
"%-*s") :
565 fieldMax[field_index],
567 if (po->
standard || field_index + 1 < nFields)
572 fputs(
"</tr>", fout);
588 const char *fieldSep,
593 #define DEFAULT_FIELD_SEP " "
601 if (fieldSep == NULL)
615 fLength = (
int *)
malloc(nFields *
sizeof(
int));
622 for (
j = 0;
j < nFields;
j++)
625 for (
i = 0;
i < nTuples;
i++)
629 if (flen > fLength[
j])
638 for (
i = 0;
i < nFields;
i++)
648 for (
i = 0;
i < nFields;
i++)
651 fill(0, fLength[
i],
'-', fp);
658 for (
i = 0;
i < nTuples;
i++)
660 for (
j = 0;
j < nFields;
j++)
693 char formatString[80];
694 char *tborder = NULL;
700 sprintf(formatString,
"%%s %%-%ds", colWidth);
702 sprintf(formatString,
"%%s %%s");
711 width = nFields * 14;
712 tborder = (
char *)
malloc(width + 1);
718 for (
i = 0;
i < width;
i++)
720 tborder[width] =
'\0';
721 fprintf(fout,
"%s\n", tborder);
724 for (
i = 0;
i < nFields;
i++)
729 TerseOutput ?
"" :
"|",
739 fprintf(fout,
"|\n%s\n", tborder);
742 for (
i = 0;
i < nTups;
i++)
744 for (
j = 0;
j < nFields;
j++)
749 TerseOutput ?
"" :
"|",
755 fprintf(fout,
"|\n%s\n", tborder);
766 fill(
int length,
int max,
char filler, FILE *fp)
770 count = max - length;
static void PGresult * res
int PQgetlength(const PGresult *res, int tup_num, int field_num)
int PQntuples(const PGresult *res)
char * PQfname(const PGresult *res, int field_num)
char * PQgetvalue(const PGresult *res, int tup_num, int field_num)
int PQnfields(const PGresult *res)
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 void const char fflush(stdout)
void(* pqsigfunc)(SIGNAL_ARGS)
pqsigfunc pqsignal(int signo, pqsigfunc func)