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