1 static char *sccsid = "@(#)tk.c 4.3 (Berkeley) 05/11/89";
2 /*
3 * optimize output for Tek 4014
4 */
5
6 #include <sys/signal.h>
7 #include <stdio.h>
8 #include <paths.h>
9
10 #define MAXY 3071
11 #define LINE 47
12 #define XOFF 248
13 #define US 037
14 #define GS 035
15 #define ESC 033
16 #define CR 015
17 #define FF 014
18 #define SO 016
19 #define SI 017
20
21 int pl = 66*LINE;
22 int yyll = -1;
23 int xx = XOFF;
24 int xoff = XOFF;
25 int coff = 0;
26 int ncol = 0;
27 int maxcol = 1;
28 int yy = MAXY;
29 int ohy = -1;
30 int ohx = -1;
31 int oxb = -1;
32 int oly = -1;
33 int olx = -1;
34 int alpha;
35 int ry;
36 FILE *ttyin;
37
main(argc,argv)38 main(argc, argv)
39 int argc;
40 char **argv;
41 {
42 register i, j;
43 extern ex();
44
45 while (--argc > 0 && (++argv)[0][0]=='-')
46 switch(argv[0][1]) {
47 case 'p':
48 if (i = atoi(&argv[0][2]))
49 pl = i;
50 yyll = MAXY + 1 - pl;
51 break;
52 default:
53 if (i = atoi(&argv[0][1])) {
54 maxcol = i;
55 xx = xoff = 0;
56 coff = 4096/i;
57 }
58 break;
59 }
60 if ((ttyin = fopen(_PATH_TTY, "r")) != NULL)
61 setbuf(ttyin, (char *)NULL);
62 if (argc) {
63 if (freopen(argv[0], "r", stdin) == NULL) {
64 fprintf(stderr, "tk: cannot open %s\n", argv[0]);
65 exit(1);
66 }
67 }
68 signal(SIGINT, ex);
69 ncol = maxcol;
70 init();
71 while ((i = getchar()) != EOF) {
72 switch(i) {
73
74 case FF:
75 yy = 0;
76 case '\n':
77 xx = xoff;
78 yy -= LINE;
79 alpha = 0;
80 if (yy < yyll) {
81 ncol++;
82 yy = 0;
83 sendpt(0);
84 putchar(US);
85 fflush(stdout);
86 if (ncol >= maxcol)
87 kwait();
88 init();
89 }
90 continue;
91
92 case CR:
93 xx = xoff;
94 alpha = 0;
95 continue;
96
97 case ' ':
98 xx += 31;
99 alpha = 0;
100 continue;
101
102 case '\t': /*tabstops at 8*31=248*/
103 j = ((xx-xoff)/248) + 1;
104 xx += j*248 - (xx-xoff);
105 alpha = 0;
106 continue;
107
108 case '\b':
109 xx -= 31;
110 alpha = 0;
111 continue;
112
113 case ESC:
114 switch(i = getchar()) {
115 case '7':
116 yy += LINE;
117 alpha = 0;
118 continue;
119 case '8':
120 yy += (LINE + ry)/2;
121 ry = (LINE + ry)%2;
122 alpha = 0;
123 continue;
124 case '9':
125 yy -= (LINE - ry)/2;
126 ry = -(LINE - ry)%2;
127 alpha = 0;
128 continue;
129 default:
130 continue;
131 }
132
133 default:
134 sendpt(alpha);
135 if (alpha==0) {
136 putchar(US);
137 alpha = 1;
138 }
139 putchar(i);
140 if (i>' ')
141 xx += 31;
142 continue;
143 }
144 }
145 xx = xoff;
146 yy = 0;
147 sendpt(0);
148 putchar(US);
149 kwait();
150 ex();
151 }
152
init()153 init()
154 {
155 ohx = oxb = olx = ohy = oly = -1;
156 if (ncol >= maxcol) {
157 ncol = 0;
158 if (maxcol > 1)
159 xoff = 0;
160 else
161 xoff = XOFF;
162 } else
163 xoff += coff;
164 xx = xoff;
165 yy = MAXY;
166 if (ncol==0)
167 fputs("\033\014\033;", stdout);
168 sendpt(0);
169 }
170
ex()171 ex()
172 {
173 yy = MAXY;
174 xx = 0;
175 fputs("\033;\037", stdout);
176 sendpt(1);
177 exit(0);
178 }
179
kwait()180 kwait()
181 {
182 register c;
183
184 fflush(stdout);
185 if (ttyin==NULL)
186 return;
187 while ((c=getc(ttyin))!='\n') {
188 if (c=='!') {
189 execom();
190 printf("!\n");
191 fflush(stdout);
192 continue;
193 }
194 if (c==EOF)
195 ex();
196 }
197 }
198
execom()199 execom()
200 {
201 int (*si)(), (*sq)();
202
203 if (fork() != 0) {
204 si = signal(SIGINT, SIG_IGN);
205 sq = signal(SIGQUIT, SIG_IGN);
206 wait((int *)NULL);
207 signal(SIGINT, si);
208 signal(SIGQUIT, sq);
209 return;
210 }
211 if (isatty(fileno(stdin)) == 0) {
212 if (freopen(_PATH_TTY, "r", stdin)==NULL)
213 freopen(_PATH_DEVNULL, "r", stdin);
214 }
215 execl(_PATH_BSHELL, "sh", "-t", 0);
216 }
217
sendpt(a)218 sendpt(a)
219 {
220 register zz;
221 int hy,xb,ly,hx,lx;
222
223 if (a)
224 return;
225 if ((zz = yy) < 0)
226 zz = 0;
227 hy = ((zz>>7) & 037);
228 xb = ((xx & 03) + ((zz<<2) & 014) & 017);
229 ly = ((zz>>2) & 037);
230 hx = ((xx>>7) & 037);
231 lx = ((xx>>2) & 037);
232 putchar(GS);
233 if (hy != ohy)
234 putchar(hy | 040);
235 if (xb != oxb)
236 putchar(xb | 0140);
237 if ((ly != oly) || (hx != ohx) || (xb != oxb))
238 putchar(ly | 0140);
239 if (hx != ohx)
240 putchar(hx | 040);
241 putchar(lx | 0100);
242 ohy = hy;
243 oxb = xb;
244 oly = ly;
245 ohx = hx;
246 olx = lx;
247 alpha = 0;
248 }
249