fe-print.c 16.3 KB
Newer Older
1 2
/*-------------------------------------------------------------------------
 *
3
 * fe-print.c
4 5 6 7 8 9 10 11
 *	  functions for pretty-printing query results
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 * These functions were formerly part of fe-exec.c, but they
 * didn't really belong there.
 *
 * IDENTIFICATION
12
 *	  $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-print.c,v 1.31 2000/01/19 01:33:16 ishii Exp $
13 14 15
 *
 *-------------------------------------------------------------------------
 */
16 17 18
#include <signal.h>

#include "postgres.h"
19 20 21 22
#include "libpq-fe.h"
#include "libpq-int.h"
#include "pqsignal.h"

Bruce Momjian's avatar
Bruce Momjian committed
23 24
#ifdef WIN32
#include "win32.h"
25
#else
26
#include <unistd.h>
27
#include <sys/ioctl.h>
28 29 30
#endif

#ifdef HAVE_TERMIOS_H
31
#include <termios.h>
32
#else
Bruce Momjian's avatar
Hi!  
Bruce Momjian committed
33
#ifndef WIN32
34
#include <sys/termios.h>
35
#endif
Bruce Momjian's avatar
Hi!  
Bruce Momjian committed
36
#endif
37

38
#ifdef MULTIBYTE
39
#include "miscadmin.h"
Marc G. Fournier's avatar
Marc G. Fournier committed
40
#include "mb/pg_wchar.h"
41
#endif
42 43 44 45 46 47 48 49 50 51 52 53 54 55

#ifdef TIOCGWINSZ
static struct winsize screen_size;

#else
static struct winsize
{
	int			ws_row;
	int			ws_col;
}			screen_size;

#endif


Bruce Momjian's avatar
Bruce Momjian committed
56
static void do_field(const PQprintOpt *po, const PGresult *res,
57
		 const int i, const int j, const int fs_len,
58
		 char **fields,
Bruce Momjian's avatar
Bruce Momjian committed
59
		 const int nFields, const char **fieldNames,
60
		 unsigned char *fieldNotNum, int *fieldMax,
61
		 const int fieldMaxLen, FILE *fout);
Bruce Momjian's avatar
Bruce Momjian committed
62 63 64 65
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 void output_row(FILE *fout, const PQprintOpt *po, const int nFields, char **fields,
66
		   unsigned char *fieldNotNum, int *fieldMax, char *border,
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		   const int row_index);
static void fill(int length, int max, char filler, FILE *fp);


/*
 * PQprint()
 *
 * Format results of a query for printing.
 *
 * PQprintOpt is a typedef (structure) that containes
 * various flags and options. consult libpq-fe.h for
 * details
 *
 * Obsoletes PQprintTuples.
 */

void
PQprint(FILE *fout,
Bruce Momjian's avatar
Bruce Momjian committed
85 86
	const PGresult *res,
	const PQprintOpt *po)
87 88 89 90 91 92 93 94 95 96 97 98 99 100
{
	int			nFields;

	nFields = PQnfields(res);

	if (nFields > 0)
	{							/* only print rows with at least 1 field.  */
		int			i,
					j;
		int			nTups;
		int		   *fieldMax = NULL;	/* in case we don't use them */
		unsigned char *fieldNotNum = NULL;
		char	   *border = NULL;
		char	  **fields = NULL;
Bruce Momjian's avatar
Bruce Momjian committed
101
		const char	  **fieldNames;
102 103 104 105 106
		int			fieldMaxLen = 0;
		int			numFieldName;
		int			fs_len = strlen(po->fieldSep);
		int			total_line_length = 0;
		int			usePipe = 0;
107
		pqsigfunc	oldsigpipehandler = NULL;
108 109 110
		char	   *pagerenv;

		nTups = PQntuples(res);
Bruce Momjian's avatar
Bruce Momjian committed
111
		if (!(fieldNames = (const char **) calloc(nFields, sizeof(char *))))
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
		{
			perror("calloc");
			exit(1);
		}
		if (!(fieldNotNum = (unsigned char *) calloc(nFields, 1)))
		{
			perror("calloc");
			exit(1);
		}
		if (!(fieldMax = (int *) calloc(nFields, sizeof(int))))
		{
			perror("calloc");
			exit(1);
		}
		for (numFieldName = 0;
			 po->fieldName && po->fieldName[numFieldName];
			 numFieldName++)
			;
		for (j = 0; j < nFields; j++)
		{
			int			len;
Bruce Momjian's avatar
Bruce Momjian committed
133
			const char	   *s = (j < numFieldName && po->fieldName[j][0]) ?
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
			po->fieldName[j] : PQfname(res, j);

			fieldNames[j] = s;
			len = s ? strlen(s) : 0;
			fieldMax[j] = len;
			len += fs_len;
			if (len > fieldMaxLen)
				fieldMaxLen = len;
			total_line_length += len;
		}

		total_line_length += nFields * strlen(po->fieldSep) + 1;

		if (fout == NULL)
			fout = stdout;
149
		if (po->pager && fout == stdout
Bruce Momjian's avatar
Bruce Momjian committed
150 151
#ifndef WIN32
			&&
152
			isatty(fileno(stdin)) &&
Bruce Momjian's avatar
Bruce Momjian committed
153 154 155
			isatty(fileno(stdout))
#endif
			)
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
		{
			/* try to pipe to the pager program if possible */
#ifdef TIOCGWINSZ
			if (ioctl(fileno(stdout), TIOCGWINSZ, &screen_size) == -1 ||
				screen_size.ws_col == 0 ||
				screen_size.ws_row == 0)
			{
#endif
				screen_size.ws_row = 24;
				screen_size.ws_col = 80;
#ifdef TIOCGWINSZ
			}
#endif
			pagerenv = getenv("PAGER");
			if (pagerenv != NULL &&
				pagerenv[0] != '\0' &&
				!po->html3 &&
				((po->expanded &&
				  nTups * (nFields + 1) >= screen_size.ws_row) ||
				 (!po->expanded &&
				  nTups * (total_line_length / screen_size.ws_col + 1) *
177
				  (1 + (po->standard != 0)) >= screen_size.ws_row -
178 179 180 181 182
				  (po->header != 0) *
				  (total_line_length / screen_size.ws_col + 1) * 2
				  - (po->header != 0) * 2		/* row count and newline */
				  )))
			{
Bruce Momjian's avatar
Bruce Momjian committed
183 184 185
#ifdef WIN32
				fout = _popen(pagerenv, "w");
#else
186
				fout = popen(pagerenv, "w");
Bruce Momjian's avatar
Bruce Momjian committed
187
#endif
188 189 190
				if (fout)
				{
					usePipe = 1;
Bruce Momjian's avatar
Bruce Momjian committed
191
#ifndef WIN32
192
					oldsigpipehandler = pqsignal(SIGPIPE, SIG_IGN);
Bruce Momjian's avatar
Bruce Momjian committed
193
#endif
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
				}
				else
					fout = stdout;
			}
		}

		if (!po->expanded && (po->align || po->html3))
		{
			if (!(fields = (char **) calloc(nFields * (nTups + 1), sizeof(char *))))
			{
				perror("calloc");
				exit(1);
			}
		}
		else if (po->header && !po->html3)
		{
			if (po->expanded)
			{
				if (po->align)
					fprintf(fout, "%-*s%s Value\n",
							fieldMaxLen - fs_len, "Field", po->fieldSep);
				else
					fprintf(fout, "%s%sValue\n", "Field", po->fieldSep);
			}
			else
			{
				int			len = 0;

				for (j = 0; j < nFields; j++)
				{
Bruce Momjian's avatar
Bruce Momjian committed
224
					const char	   *s = fieldNames[j];
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258

					fputs(s, fout);
					len += strlen(s) + fs_len;
					if ((j + 1) < nFields)
						fputs(po->fieldSep, fout);
				}
				fputc('\n', fout);
				for (len -= fs_len; len--; fputc('-', fout));
				fputc('\n', fout);
			}
		}
		if (po->expanded && po->html3)
		{
			if (po->caption)
				fprintf(fout, "<centre><h2>%s</h2></centre>\n", po->caption);
			else
				fprintf(fout,
						"<centre><h2>"
						"Query retrieved %d rows * %d fields"
						"</h2></centre>\n",
						nTups, nFields);
		}
		for (i = 0; i < nTups; i++)
		{
			if (po->expanded)
			{
				if (po->html3)
					fprintf(fout,
						  "<table %s><caption align=high>%d</caption>\n",
							po->tableOpt ? po->tableOpt : "", i);
				else
					fprintf(fout, "-- RECORD %d --\n", i);
			}
			for (j = 0; j < nFields; j++)
259
				do_field(po, res, i, j, fs_len, fields, nFields,
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
						 fieldNames, fieldNotNum,
						 fieldMax, fieldMaxLen, fout);
			if (po->html3 && po->expanded)
				fputs("</table>\n", fout);
		}
		if (!po->expanded && (po->align || po->html3))
		{
			if (po->html3)
			{
				if (po->header)
				{
					if (po->caption)
						fprintf(fout,
						  "<table %s><caption align=high>%s</caption>\n",
								po->tableOpt ? po->tableOpt : "",
								po->caption);
					else
						fprintf(fout,
								"<table %s><caption align=high>"
								"Retrieved %d rows * %d fields"
								"</caption>\n",
						po->tableOpt ? po->tableOpt : "", nTups, nFields);
				}
				else
					fprintf(fout, "<table %s>", po->tableOpt ? po->tableOpt : "");
			}
			if (po->header)
				border = do_header(fout, po, nFields, fieldMax, fieldNames,
								   fieldNotNum, fs_len, res);
			for (i = 0; i < nTups; i++)
				output_row(fout, po, nFields, fields,
						   fieldNotNum, fieldMax, border, i);
			free(fields);
			if (border)
				free(border);
		}
		if (po->header && !po->html3)
			fprintf(fout, "(%d row%s)\n\n", PQntuples(res),
					(PQntuples(res) == 1) ? "" : "s");
		free(fieldMax);
		free(fieldNotNum);
		free(fieldNames);
		if (usePipe)
		{
Bruce Momjian's avatar
Bruce Momjian committed
304 305 306
#ifdef WIN32
			_pclose(fout);
#else
307
			pclose(fout);
308
			pqsignal(SIGPIPE, oldsigpipehandler);
Bruce Momjian's avatar
Bruce Momjian committed
309
#endif
310 311 312 313 314 315 316 317 318 319 320 321 322
		}
		if (po->html3 && !po->expanded)
			fputs("</table>\n", fout);
	}
}


/*
 * PQdisplayTuples()
 * kept for backward compatibility
 */

void
Bruce Momjian's avatar
Bruce Momjian committed
323 324 325 326 327 328
PQdisplayTuples(const PGresult *res,
		FILE *fp,		/* where to send the output */
		int fillAlign,	/* pad the fields with spaces */
		const char *fieldSep,	/* field separator */
		int printHeader,/* display headers? */
		int quiet
329 330 331 332 333 334 335 336
)
{
#define DEFAULT_FIELD_SEP " "

	int			i,
				j;
	int			nFields;
	int			nTuples;
337
	int		   *fLength = NULL;
338 339 340 341 342 343 344 345 346 347 348

	if (fieldSep == NULL)
		fieldSep = DEFAULT_FIELD_SEP;

	/* Get some useful info about the results */
	nFields = PQnfields(res);
	nTuples = PQntuples(res);

	if (fp == NULL)
		fp = stdout;

349
	/* Figure the field lengths to align to */
350 351 352
	/* will be somewhat time consuming for very large results */
	if (fillAlign)
	{
353 354
		fLength = (int *) malloc(nFields * sizeof(int));
		for (j = 0; j < nFields; j++)
355
		{
356 357
			fLength[j] = strlen(PQfname(res, j));
			for (i = 0; i < nTuples; i++)
358
			{
359 360 361
				int flen = PQgetlength(res, i, j);
				if (flen > fLength[j])
					fLength[j] = flen;
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
			}
		}
	}

	if (printHeader)
	{
		/* first, print out the attribute names */
		for (i = 0; i < nFields; i++)
		{
			fputs(PQfname(res, i), fp);
			if (fillAlign)
				fill(strlen(PQfname(res, i)), fLength[i], ' ', fp);
			fputs(fieldSep, fp);
		}
		fprintf(fp, "\n");

		/* Underline the attribute names */
		for (i = 0; i < nFields; i++)
		{
			if (fillAlign)
				fill(0, fLength[i], '-', fp);
			fputs(fieldSep, fp);
		}
		fprintf(fp, "\n");
	}

	/* next, print out the instances */
	for (i = 0; i < nTuples; i++)
	{
		for (j = 0; j < nFields; j++)
		{
			fprintf(fp, "%s", PQgetvalue(res, i, j));
			if (fillAlign)
				fill(strlen(PQgetvalue(res, i, j)), fLength[j], ' ', fp);
			fputs(fieldSep, fp);
		}
		fprintf(fp, "\n");
	}

	if (!quiet)
		fprintf(fp, "\nQuery returned %d row%s.\n", PQntuples(res),
				(PQntuples(res) == 1) ? "" : "s");

	fflush(fp);
406 407 408

	if (fLength)
		free(fLength);
409 410 411 412 413 414 415 416 417 418 419
}



/*
 * PQprintTuples()
 *
 * kept for backward compatibility
 *
 */
void
Bruce Momjian's avatar
Bruce Momjian committed
420 421 422 423 424 425
PQprintTuples(const PGresult *res,
	      FILE *fout,		/* output stream */
	      int PrintAttNames,/* print attribute names or not */
	      int TerseOutput,	/* delimiter bars or not? */
	      int colWidth		/* width of column, if 0, use variable
					 * width */
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
)
{
	int			nFields;
	int			nTups;
	int			i,
				j;
	char		formatString[80];

	char	   *tborder = NULL;

	nFields = PQnfields(res);
	nTups = PQntuples(res);

	if (colWidth > 0)
		sprintf(formatString, "%%s %%-%ds", colWidth);
	else
		sprintf(formatString, "%%s %%s");

	if (nFields > 0)
	{							/* only print rows with at least 1 field.  */

		if (!TerseOutput)
		{
			int			width;

			width = nFields * 14;
			tborder = malloc(width + 1);
			for (i = 0; i <= width; i++)
				tborder[i] = '-';
			tborder[i] = '\0';
			fprintf(fout, "%s\n", tborder);
		}

		for (i = 0; i < nFields; i++)
		{
			if (PrintAttNames)
			{
				fprintf(fout, formatString,
						TerseOutput ? "" : "|",
						PQfname(res, i));
			}
		}

		if (PrintAttNames)
		{
			if (TerseOutput)
				fprintf(fout, "\n");
			else
				fprintf(fout, "|\n%s\n", tborder);
		}

		for (i = 0; i < nTups; i++)
		{
			for (j = 0; j < nFields; j++)
			{
Bruce Momjian's avatar
Bruce Momjian committed
481
				const char	   *pval = PQgetvalue(res, i, j);
482 483 484 485 486 487 488 489 490 491 492 493 494

				fprintf(fout, formatString,
						TerseOutput ? "" : "|",
						pval ? pval : "");
			}
			if (TerseOutput)
				fprintf(fout, "\n");
			else
				fprintf(fout, "|\n%s\n", tborder);
		}
	}
}

495
#ifdef MULTIBYTE
496 497 498 499 500 501 502
/*
 * returns the byte length of the word beginning s.
 * Client side encoding is determined by the environment variable
 * "PGCLIENTENCODING".
 * if this variable is not defined, the same encoding as
 * the backend is assumed.
 */
503
int
Tatsuo Ishii's avatar
Tatsuo Ishii committed
504 505 506 507 508 509 510 511 512 513
PQmblen(const unsigned char *s, int encoding)
{
	return (pg_encoding_mblen(encoding, s));
}

/*
 * Get encoding id from environment variable PGCLIENTENCODING.
 */
int
PQenv2encoding(void)
514
{
515
	char	   *str;
Tatsuo Ishii's avatar
Tatsuo Ishii committed
516
	int			encoding = SQL_ASCII;
517 518

	str = getenv("PGCLIENTENCODING");
519
	if (str && *str != '\0')
520
		encoding = pg_char_to_encoding(str);
Tatsuo Ishii's avatar
Tatsuo Ishii committed
521
	return(encoding);
522
}
523

524 525
#else

526
/* Provide a default definition in case someone calls it anyway */
527
int
Tatsuo Ishii's avatar
Tatsuo Ishii committed
528
PQmblen(const unsigned char *s, int encoding)
529
{
530
	return 1;
531
}
Tatsuo Ishii's avatar
Tatsuo Ishii committed
532 533 534 535 536
int
PQenv2encoding(void)
{
	return 0;
}
537

Bruce Momjian's avatar
Bruce Momjian committed
538
#endif	 /* MULTIBYTE */
539 540

static void
Bruce Momjian's avatar
Bruce Momjian committed
541
do_field(const PQprintOpt *po, const PGresult *res,
542
		 const int i, const int j, const int fs_len,
543
		 char **fields,
Bruce Momjian's avatar
Bruce Momjian committed
544
		 const int nFields, char const **fieldNames,
545
		 unsigned char *fieldNotNum, int *fieldMax,
546 547 548
		 const int fieldMaxLen, FILE *fout)
{

Bruce Momjian's avatar
Bruce Momjian committed
549
	const char	   *pval,
550
			   *p;
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
	int			plen;
	bool		skipit;

	plen = PQgetlength(res, i, j);
	pval = PQgetvalue(res, i, j);

	if (plen < 1 || !pval || !*pval)
	{
		if (po->align || po->expanded)
			skipit = true;
		else
		{
			skipit = false;
			goto efield;
		}
	}
	else
		skipit = false;

	if (!skipit)
	{
572 573 574 575
		if (po->align && ! fieldNotNum[j])
		{
			/* Detect whether field contains non-numeric data */
			char		ch = '0';
Bruce Momjian's avatar
Bruce Momjian committed
576

577
#ifdef MULTIBYTE
Tatsuo Ishii's avatar
Tatsuo Ishii committed
578
			for (p = pval; *p; p += PQmblen(p, PQclientencoding(res->conn)))
579
#else
580
			for (p = pval; *p; p++)
581
#endif
582 583 584 585 586 587 588 589 590 591 592 593 594
			{
				ch = *p;
				if (! ((ch >= '0' && ch <= '9') ||
					   ch == '.' ||
					   ch == 'E' ||
					   ch == 'e' ||
					   ch == ' ' ||
					   ch == '-'))
				{
					fieldNotNum[j] = 1;
					break;
				}
			}
595
			/*
596 597 598
			 * Above loop will believe E in first column is numeric; also, we
			 * insist on a digit in the last column for a numeric.	This test
			 * is still not bulletproof but it handles most cases.
599
			 */
600 601
			if (*pval == 'E' || *pval == 'e' ||
				!(ch >= '0' && ch <= '9'))
602 603
				fieldNotNum[j] = 1;
		}
604

605 606
		if (!po->expanded && (po->align || po->html3))
		{
607 608 609
			if (plen > fieldMax[j])
				fieldMax[j] = plen;
			if (!(fields[i * nFields + j] = (char *) malloc(plen + 1)))
610 611 612 613
			{
				perror("malloc");
				exit(1);
			}
614
			strcpy(fields[i * nFields + j], pval);
615 616 617 618 619 620 621 622 623 624 625
		}
		else
		{
			if (po->expanded)
			{
				if (po->html3)
					fprintf(fout,
							"<tr><td align=left><b>%s</b></td>"
							"<td align=%s>%s</td></tr>\n",
							fieldNames[j],
							fieldNotNum[j] ? "left" : "right",
626
							pval);
627 628 629 630 631
				else
				{
					if (po->align)
						fprintf(fout,
								"%-*s%s %s\n",
632 633 634
								fieldMaxLen - fs_len, fieldNames[j],
								po->fieldSep,
								pval);
635
					else
636 637 638
						fprintf(fout,
								"%s%s%s\n",
								fieldNames[j], po->fieldSep, pval);
639 640 641 642 643 644
				}
			}
			else
			{
				if (!po->html3)
				{
645
					fputs(pval, fout);
646 647 648 649 650 651 652 653 654 655 656 657 658
			efield:
					if ((j + 1) < nFields)
						fputs(po->fieldSep, fout);
					else
						fputc('\n', fout);
				}
			}
		}
	}
}


static char *
Bruce Momjian's avatar
Bruce Momjian committed
659 660 661
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)
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
{

	int			j;				/* for loop index */
	char	   *border = NULL;

	if (po->html3)
		fputs("<tr>", fout);
	else
	{
		int			j;			/* for loop index */
		int			tot = 0;
		int			n = 0;
		char	   *p = NULL;

		for (; n < nFields; n++)
			tot += fieldMax[n] + fs_len + (po->standard ? 2 : 0);
		if (po->standard)
			tot += fs_len * 2 + 2;
		border = malloc(tot + 1);
		if (!border)
		{
			perror("malloc");
			exit(1);
		}
		p = border;
		if (po->standard)
		{
689
			char	   *fs = po->fieldSep;
690 691 692 693 694 695 696 697 698 699 700

			while (*fs++)
				*p++ = '+';
		}
		for (j = 0; j < nFields; j++)
		{
			int			len;

			for (len = fieldMax[j] + (po->standard ? 2 : 0); len--; *p++ = '-');
			if (po->standard || (j + 1) < nFields)
			{
701
				char	   *fs = po->fieldSep;
702 703 704 705 706 707 708 709 710 711 712 713 714

				while (*fs++)
					*p++ = '+';
			}
		}
		*p = '\0';
		if (po->standard)
			fprintf(fout, "%s\n", border);
	}
	if (po->standard)
		fputs(po->fieldSep, fout);
	for (j = 0; j < nFields; j++)
	{
Bruce Momjian's avatar
Bruce Momjian committed
715
		const char	   *s = PQfname(res, j);
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746

		if (po->html3)
		{
			fprintf(fout, "<th align=%s>%s</th>",
					fieldNotNum[j] ? "left" : "right", fieldNames[j]);
		}
		else
		{
			int			n = strlen(s);

			if (n > fieldMax[j])
				fieldMax[j] = n;
			if (po->standard)
				fprintf(fout,
						fieldNotNum[j] ? " %-*s " : " %*s ",
						fieldMax[j], s);
			else
				fprintf(fout, fieldNotNum[j] ? "%-*s" : "%*s", fieldMax[j], s);
			if (po->standard || (j + 1) < nFields)
				fputs(po->fieldSep, fout);
		}
	}
	if (po->html3)
		fputs("</tr>\n", fout);
	else
		fprintf(fout, "\n%s\n", border);
	return border;
}


static void
Bruce Momjian's avatar
Bruce Momjian committed
747
output_row(FILE *fout, const PQprintOpt *po, const int nFields, char **fields,
748
		   unsigned char *fieldNotNum, int *fieldMax, char *border,
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
		   const int row_index)
{

	int			field_index;	/* for loop index */

	if (po->html3)
		fputs("<tr>", fout);
	else if (po->standard)
		fputs(po->fieldSep, fout);
	for (field_index = 0; field_index < nFields; field_index++)
	{
		char	   *p = fields[row_index * nFields + field_index];

		if (po->html3)
			fprintf(fout, "<td align=%s>%s</td>",
				fieldNotNum[field_index] ? "left" : "right", p ? p : "");
		else
		{
			fprintf(fout,
					fieldNotNum[field_index] ?
					(po->standard ? " %-*s " : "%-*s") :
					(po->standard ? " %*s " : "%*s"),
					fieldMax[field_index],
					p ? p : "");
			if (po->standard || field_index + 1 < nFields)
				fputs(po->fieldSep, fout);
		}
		if (p)
			free(p);
	}
	if (po->html3)
		fputs("</tr>", fout);
	else if (po->standard)
		fprintf(fout, "\n%s", border);
	fputc('\n', fout);
}


/* simply send out max-length number of filler characters to fp */

static void
fill(int length, int max, char filler, FILE *fp)
{
	int			count;

	count = max - length;
	while (count-- >= 0)
		putc(filler, fp);
}