File: pager.c

package info (click to toggle)
dico 2.9-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 16,908 kB
  • sloc: ansic: 85,886; sh: 50,207; lex: 2,624; yacc: 1,437; tcl: 1,434; python: 1,281; makefile: 1,266; perl: 881; lisp: 489; awk: 157; pascal: 125; cpp: 44; fortran: 25; asm: 19; sed: 16; xml: 8
file content (116 lines) | stat: -rw-r--r-- 2,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* This file is part of GNU Dico. 
   Copyright (C) 1998-2019 Sergey Poznyakoff

   GNU Dico is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Dico is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Dico.  If not, see <http://www.gnu.org/licenses/>. */

#include "dico-priv.h"
#include <sys/ioctl.h>

static char *pager;

static char *
get_pager_name(void)
{
    if (pager) {
	if (strcmp(pager, "-") == 0)
	    return NULL;
	return pager;
    }
    return getenv("PAGER");
}

void
ds_pager(int argc, char **argv)
{
    if (argc == 1) {
	char *p = get_pager_name();
	if (p) {
	    printf("%s\n", p);
	    if (!pager)
		printf("%s\n", _("(Pager set from environment)"));
	} 
    } else
	xdico_assign_string(&pager, argv[1]);
}


dico_stream_t
create_output_stream(void)
{
    return dico_fd_stream_create(fileno(stdout), DICO_STREAM_WRITE, 1);
}

static int
get_screen_lines(void)
{
    struct winsize ws;

    ws.ws_col = ws.ws_row = 0;
    if ((ioctl(1, TIOCGWINSZ, (char *) &ws) < 0) || ws.ws_row == 0) {
	const char *lines = getenv("LINES");
	if (lines)
	    ws.ws_row = strtol(lines, NULL, 10);
    }
    return ws.ws_row;
}

struct pfile_stream {
    FILE *fp;
};

static int
fp_write(void *data, const char *buf, size_t size, size_t *pret)
{
    struct pfile_stream *p = data;
    *pret = fwrite(buf, 1, size, p->fp);
    return ferror(p->fp);
}

static int
fp_close(void *data)
{
    struct pfile_stream *p = data;
    pclose(p->fp);
    return 0;
}

static dico_stream_t
create_pfile_stream(FILE *fp)
{
    dico_stream_t str;
    struct pfile_stream *s;

    s = xmalloc(sizeof(*s));
    s->fp = fp;
    if (dico_stream_create(&str,  DICO_STREAM_WRITE, s))
	xalloc_die();
    dico_stream_set_write(str, fp_write);
    dico_stream_set_close(str, fp_close);
    return str;
}
    
dico_stream_t
create_pager_stream(size_t nlines)
{
    char *pager = get_pager_name();
    FILE *fp;

    if (!pager || !pager[0]
	|| !isatty(fileno(stdout)) || nlines < get_screen_lines())
	return create_output_stream();
    fp = popen(pager, "w");
    if (!fp)
	return create_output_stream();
    return create_pfile_stream(fp);
}