* PageOutput * * Tests if pager is needed and returns appropriate FILE pointer. * * If the topt argument is NULL no pager is used. */
| 2973 | * If the topt argument is NULL no pager is used. |
| 2974 | */ |
| 2975 | FILE * |
| 2976 | PageOutput(int lines, const printTableOpt *topt) |
| 2977 | { |
| 2978 | /* check whether we need / can / are supposed to use pager */ |
| 2979 | if (topt && topt->pager && isatty(fileno(stdin)) && isatty(fileno(stdout))) |
| 2980 | { |
| 2981 | #ifdef TIOCGWINSZ |
| 2982 | unsigned short int pager = topt->pager; |
| 2983 | int min_lines = topt->pager_min_lines; |
| 2984 | int result; |
| 2985 | struct winsize screen_size; |
| 2986 | |
| 2987 | result = ioctl(fileno(stdout), TIOCGWINSZ, &screen_size); |
| 2988 | |
| 2989 | /* >= accounts for a one-line prompt */ |
| 2990 | if (result == -1 |
| 2991 | || (lines >= screen_size.ws_row && lines >= min_lines) |
| 2992 | || pager > 1) |
| 2993 | #endif |
| 2994 | { |
| 2995 | const char *pagerprog; |
| 2996 | FILE *pagerpipe; |
| 2997 | |
| 2998 | pagerprog = getenv("PSQL_PAGER"); |
| 2999 | if (!pagerprog) |
| 3000 | pagerprog = getenv("PAGER"); |
| 3001 | if (!pagerprog) |
| 3002 | pagerprog = DEFAULT_PAGER; |
| 3003 | else |
| 3004 | { |
| 3005 | /* if PAGER is empty or all-white-space, don't use pager */ |
| 3006 | if (strspn(pagerprog, " \t\r\n") == strlen(pagerprog)) |
| 3007 | return stdout; |
| 3008 | } |
| 3009 | disable_sigpipe_trap(); |
| 3010 | pagerpipe = popen(pagerprog, "w"); |
| 3011 | if (pagerpipe) |
| 3012 | return pagerpipe; |
| 3013 | /* if popen fails, silently proceed without pager */ |
| 3014 | restore_sigpipe_trap(); |
| 3015 | } |
| 3016 | } |
| 3017 | |
| 3018 | return stdout; |
| 3019 | } |
| 3020 | |
| 3021 | /* |
| 3022 | * ClosePager |
no test coverage detected