|
7 | 7 | *
|
8 | 8 | *
|
9 | 9 | * IDENTIFICATION
|
10 |
| - * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.3 1996/07/18 05:48:56 scrappy Exp $ |
| 10 | + * $Header: /cvsroot/pgsql/src/interfaces/libpq/fe-exec.c,v 1.4 1996/07/19 06:36:38 scrappy Exp $ |
11 | 11 | *
|
12 | 12 | *-------------------------------------------------------------------------
|
13 | 13 | */
|
14 | 14 | #include<stdlib.h>
|
15 | 15 | #include<stdio.h>
|
16 | 16 | #include<string.h>
|
| 17 | +#include<signal.h> |
17 | 18 | #include<errno.h>
|
18 | 19 | #include"postgres.h"
|
19 | 20 | #include"libpq/pqcomm.h"
|
@@ -727,6 +728,10 @@ PQprint(FILE *fout,
|
727 | 728 | char*border=NULL;
|
728 | 729 | intnumFieldName;
|
729 | 730 | intfs_len=strlen(po->fieldSep);
|
| 731 | +inttotal_line_length=0; |
| 732 | +intusePipe=0; |
| 733 | +char*pager; |
| 734 | + |
730 | 735 | nTups=PQntuples(res);
|
731 | 736 | if (!(fieldNames=(char**)calloc(nFields,sizeof (char*))))
|
732 | 737 | {
|
@@ -758,7 +763,37 @@ PQprint(FILE *fout,
|
758 | 763 | len+=fs_len;
|
759 | 764 | if (len>fieldMaxLen)
|
760 | 765 | fieldMaxLen=len;
|
| 766 | +total_line_length+=len; |
761 | 767 | }
|
| 768 | + |
| 769 | +total_line_length+=nFields*strlen(po->fieldSep)+1; |
| 770 | + |
| 771 | +if (fout==NULL) |
| 772 | +fout=stdout; |
| 773 | +if (fout==stdout) { |
| 774 | +/* try to pipe to the pager program if possible */ |
| 775 | +pager=getenv("PAGER"); |
| 776 | +if (pager!=NULL&& |
| 777 | +isatty(fileno(stdout))&& |
| 778 | + !po->html3&& |
| 779 | + ((po->expanded&&nTups* (nFields+1) >=24)|| |
| 780 | + (!po->expanded&&nTups* (total_line_length /80+1)* |
| 781 | +(1+ (po->standard!=0)) >= |
| 782 | +24- |
| 783 | +(po->header!=0)* (total_line_length /80+1)*2 |
| 784 | +/*- 1 *//* newline at end of tuple list */ |
| 785 | +/*- (quiet == 0) |
| 786 | +*/))) |
| 787 | +{ |
| 788 | +fout=popen(pager,"w"); |
| 789 | +if (fout) { |
| 790 | +usePipe=1; |
| 791 | +signal(SIGPIPE,SIG_IGN); |
| 792 | +}else |
| 793 | +fout=stdout; |
| 794 | +} |
| 795 | +} |
| 796 | + |
762 | 797 | if (!po->expanded&& (po->align||po->html3))
|
763 | 798 | {
|
764 | 799 | if (!(fields=(char**)calloc(nFields*(nTups+1),sizeof(char*))))
|
@@ -977,6 +1012,10 @@ PQprint(FILE *fout,
|
977 | 1012 | fputc('\n',fout);
|
978 | 1013 | }
|
979 | 1014 | free(fields);
|
| 1015 | +if (usePipe) { |
| 1016 | +pclose(fout); |
| 1017 | +signal(SIGPIPE,SIG_DFL); |
| 1018 | + } |
980 | 1019 | }
|
981 | 1020 | free(fieldMax);
|
982 | 1021 | free(fieldNotNum);
|
|