1 | /* |
---|
2 | * $Id: tfpager.c,v 1.5 1999-02-08 14:47:13 danw Exp $ |
---|
3 | * |
---|
4 | * Copyright (C) 1987 by the Massachusetts Institute of Technology |
---|
5 | * |
---|
6 | * An auto-paging tfile. |
---|
7 | * |
---|
8 | */ |
---|
9 | |
---|
10 | #ifndef lint |
---|
11 | static char *rcsid_tfpager_c = "$Id: tfpager.c,v 1.5 1999-02-08 14:47:13 danw Exp $"; |
---|
12 | #endif /* lint */ |
---|
13 | |
---|
14 | #include <stdio.h> |
---|
15 | #include <errno.h> |
---|
16 | #include <sys/types.h> |
---|
17 | #include <sys/ioctl.h> |
---|
18 | #include <signal.h> |
---|
19 | #include "tfile.h" |
---|
20 | #include <ctype.h> |
---|
21 | #define TABSTOP 8 |
---|
22 | |
---|
23 | #define FLUSHMSG 1 |
---|
24 | #define FLUSHSEQ 2 |
---|
25 | #define NO_TERMCAP_FILE 3 |
---|
26 | #define UNKNOWN_TT 4 |
---|
27 | |
---|
28 | char *clear_eos; |
---|
29 | char *clear_eol; |
---|
30 | int auto_wrap; |
---|
31 | char *standout, *standin, *home_cursor; |
---|
32 | int xsize, ysize; /* X, Y size of terminal */ |
---|
33 | char buf[1024]; |
---|
34 | char *bp; |
---|
35 | |
---|
36 | extern int errno; |
---|
37 | extern char *getenv(); |
---|
38 | extern char *tgetstr(); |
---|
39 | int tgetent(); |
---|
40 | int tgetnum(); |
---|
41 | |
---|
42 | typedef struct tpager_info { |
---|
43 | int curln, curcol; /* current line, current column */ |
---|
44 | int tsize; /* total size of data */ |
---|
45 | int sofar; /* amount seen so far */ |
---|
46 | int errno; |
---|
47 | u_int counting:1; /* are we counting data sent to us? */ |
---|
48 | u_int clear_on_open:1; |
---|
49 | u_int home_on_page:1; |
---|
50 | u_int clear_on_page:1; |
---|
51 | } tpager_info; |
---|
52 | |
---|
53 | void set_no_echo() |
---|
54 | { |
---|
55 | struct sgttyb sgb; |
---|
56 | ioctl(fileno(stdin), TIOCGETP, &sgb); |
---|
57 | sgb.sg_flags |= CBREAK; |
---|
58 | sgb.sg_flags &= ~ECHO; |
---|
59 | ioctl(fileno(stdin), TIOCSETN, &sgb); |
---|
60 | } |
---|
61 | |
---|
62 | void set_echo() |
---|
63 | { |
---|
64 | struct sgttyb sgb; |
---|
65 | ioctl(fileno(stdin), TIOCGETP, &sgb); |
---|
66 | sgb.sg_flags &= ~CBREAK; |
---|
67 | sgb.sg_flags |= ECHO; |
---|
68 | ioctl(fileno(stdin), TIOCSETN, &sgb); |
---|
69 | } |
---|
70 | |
---|
71 | /*ARGSUSED*/ |
---|
72 | tpager (op, pgr_p, info, argp, argn, result) |
---|
73 | int op, argn, *result, *info; |
---|
74 | tpager_info **pgr_p; |
---|
75 | char *argp; |
---|
76 | { |
---|
77 | char *cp; |
---|
78 | int count; |
---|
79 | tpager_info *pgr = *pgr_p; |
---|
80 | *result = 0; |
---|
81 | |
---|
82 | switch(op) { |
---|
83 | case TFOPEN: |
---|
84 | if (pgr->clear_on_open) { |
---|
85 | fputs(home_cursor, stdout); |
---|
86 | fputs(clear_eos, stdout); |
---|
87 | } |
---|
88 | set_no_echo(); |
---|
89 | break; |
---|
90 | |
---|
91 | case TFCLOSE: |
---|
92 | set_echo(); |
---|
93 | break; |
---|
94 | |
---|
95 | case TFREAD: |
---|
96 | *result = EINVAL; |
---|
97 | return(-1); |
---|
98 | break; |
---|
99 | |
---|
100 | case TFWRITE: |
---|
101 | #define checkpwrap(pgr) if (pgr->curln > ysize) { \ |
---|
102 | pgr_prompt(pgr); \ |
---|
103 | if(pgr->errno) break; \ |
---|
104 | } |
---|
105 | |
---|
106 | #define checklwrap(pgr) if (pgr->curcol > xsize) { \ |
---|
107 | pgr->curcol = 0; \ |
---|
108 | if (auto_wrap) \ |
---|
109 | putc('\n', stdout); \ |
---|
110 | if (pgr->home_on_page) \ |
---|
111 | fputs(clear_eol, stdout); \ |
---|
112 | pgr->curln++; \ |
---|
113 | checkpwrap(pgr); \ |
---|
114 | } |
---|
115 | |
---|
116 | for (cp = argp, count=argn; count; cp++, count--) { |
---|
117 | register unsigned char c = toascii(*cp); |
---|
118 | if (pgr->counting) pgr->sofar++; |
---|
119 | if(isprint(c)) { |
---|
120 | putc(c, stdout); |
---|
121 | pgr->curcol++; |
---|
122 | checklwrap(pgr); |
---|
123 | } else if (c == '\n') { |
---|
124 | putc(c, stdout); |
---|
125 | if (pgr->home_on_page) |
---|
126 | fputs(clear_eol, stdout); |
---|
127 | pgr->curcol = 0; |
---|
128 | pgr->curln++; |
---|
129 | checkpwrap(pgr); |
---|
130 | } else if (c == '\t') { |
---|
131 | do { |
---|
132 | putc(' ', stdout); |
---|
133 | pgr->curcol++; |
---|
134 | checklwrap(pgr); |
---|
135 | } while(pgr->curcol % TABSTOP); |
---|
136 | } else { |
---|
137 | putc('^', stdout); |
---|
138 | pgr->curcol++; |
---|
139 | checklwrap(pgr); |
---|
140 | putc(c+'@', stdout); |
---|
141 | pgr->curcol++; |
---|
142 | checklwrap(pgr); |
---|
143 | } |
---|
144 | } |
---|
145 | break; |
---|
146 | } |
---|
147 | *result = pgr->errno; |
---|
148 | } |
---|
149 | |
---|
150 | pgr_prompt(pgr) |
---|
151 | tpager_info *pgr; |
---|
152 | { |
---|
153 | char in_c; |
---|
154 | |
---|
155 | /* this only gets invoked at the start of a line */ |
---|
156 | fputs(standout,stdout); |
---|
157 | fputs("--More--", stdout); |
---|
158 | fprintf(stdout, "(%d%%)", (pgr->sofar * 100) * pgr->tsize); |
---|
159 | fputs(standin,stdout); |
---|
160 | fflush(stdout); |
---|
161 | |
---|
162 | do { |
---|
163 | loop: |
---|
164 | in_c = getchar(); |
---|
165 | switch(in_c) { |
---|
166 | case '\r': |
---|
167 | case '\n': |
---|
168 | pgr->curln--; |
---|
169 | break; |
---|
170 | case ' ': |
---|
171 | pgr->curln=0; |
---|
172 | break; |
---|
173 | case 'n': |
---|
174 | pgr->errno = FLUSHMSG; |
---|
175 | break; |
---|
176 | case 'q': |
---|
177 | pgr->errno = FLUSHSEQ; |
---|
178 | break; |
---|
179 | default: |
---|
180 | putc('\7', stdout); |
---|
181 | fflush(stdout); |
---|
182 | goto loop; |
---|
183 | } |
---|
184 | } while(0); |
---|
185 | |
---|
186 | pgr->curcol=0; |
---|
187 | |
---|
188 | putc('\r',stdout); |
---|
189 | if (clear_eol) |
---|
190 | fputs(clear_eol, stdout); |
---|
191 | else fputs(" \r", stdout); |
---|
192 | if (!pgr->curln) { |
---|
193 | if (pgr->home_on_page || pgr->clear_on_page) |
---|
194 | fputs(home_cursor, stdout); |
---|
195 | if (pgr->clear_on_page) |
---|
196 | fputs(clear_eos, stdout); |
---|
197 | } |
---|
198 | } |
---|
199 | /* |
---|
200 | * Deal with window size change signal (turn the winch... ) |
---|
201 | */ |
---|
202 | |
---|
203 | crank() |
---|
204 | { |
---|
205 | #ifdef TIOCGWINSZ |
---|
206 | struct winsize ws; |
---|
207 | |
---|
208 | if (ioctl(fileno(stdout), TIOCGWINSZ, &ws) < 0) { |
---|
209 | #endif /* TIOCGWINSZ */ |
---|
210 | ysize = tgetnum("li"); |
---|
211 | xsize = tgetnum("co"); |
---|
212 | #ifdef TIOCGWINSZ |
---|
213 | } else { |
---|
214 | if ((ysize = ws.ws_row) == 0) |
---|
215 | ysize = tgetnum("li"); |
---|
216 | if ((xsize = ws.ws_col) == 0) |
---|
217 | xsize = tgetnum("co"); |
---|
218 | } |
---|
219 | #endif /* TIOCGWINSZ */ |
---|
220 | ysize--; |
---|
221 | } |
---|
222 | int init_tfpager() |
---|
223 | { |
---|
224 | char *tt; |
---|
225 | int code; |
---|
226 | |
---|
227 | if (!isatty(fileno(stdout))) { |
---|
228 | punt: |
---|
229 | ysize = 100000; |
---|
230 | xsize = 100000; |
---|
231 | errno = 0; |
---|
232 | return(0); |
---|
233 | } |
---|
234 | |
---|
235 | if (!(tt = getenv("TERM"))) |
---|
236 | goto punt; |
---|
237 | |
---|
238 | code = tgetent(buf, tt); |
---|
239 | if (code == -1) code = NO_TERMCAP_FILE; |
---|
240 | else if (code == 0) code = UNKNOWN_TT; |
---|
241 | else { |
---|
242 | bp = buf; |
---|
243 | clear_eos = tgetstr("cd", &bp); |
---|
244 | clear_eol = tgetstr("ce", &bp); |
---|
245 | standout = tgetstr("so", &bp); |
---|
246 | standin = tgetstr("se", &bp); |
---|
247 | home_cursor = tgetstr("ho", &bp); |
---|
248 | auto_wrap = tgetflag("am"); |
---|
249 | #ifdef SIGWINCH |
---|
250 | signal(SIGWINCH, crank); |
---|
251 | #endif /* SIGWINCH */ |
---|
252 | crank(); |
---|
253 | code = 0; |
---|
254 | } |
---|
255 | return code; |
---|
256 | } |
---|
257 | |
---|
258 | tfile pager_tfile() |
---|
259 | { |
---|
260 | tpager_info *pgr = (tpager_info *)malloc(sizeof(*pgr)); |
---|
261 | memset(pgr, 0, sizeof(*pgr)); |
---|
262 | pgr->counting = 1; |
---|
263 | pgr->clear_on_open = 1; |
---|
264 | pgr->clear_on_page = 1; |
---|
265 | printf("pgr = %x\n", pgr); |
---|
266 | return(tcreate(0, (char *)pgr, 0, tpager)); |
---|
267 | } |
---|