file reorg, pathnames.h, paths.h
[unix-history] / usr / src / old / tk / tk.c
CommitLineData
7abf8d65 1static char *sccsid = "@(#)tk.c 4.3 (Berkeley) %G%";
9fb37411
BJ
2/*
3 * optimize output for Tek 4014
4 */
5
7abf8d65 6#include <sys/signal.h>
9fb37411 7#include <stdio.h>
7abf8d65 8#include <paths.h>
9fb37411
BJ
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
21int pl = 66*LINE;
22int yyll = -1;
9fb37411
BJ
23int xx = XOFF;
24int xoff = XOFF;
25int coff = 0;
26int ncol = 0;
27int maxcol = 1;
28int yy = MAXY;
29int ohy = -1;
30int ohx = -1;
31int oxb = -1;
32int oly = -1;
33int olx = -1;
34int alpha;
35int ry;
36FILE *ttyin;
37
38main(argc, argv)
39int argc;
40char **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 }
7abf8d65 60 if ((ttyin = fopen(_PATH_TTY, "r")) != NULL)
9fb37411
BJ
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);
9fb37411
BJ
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
153init()
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
171ex()
172{
173 yy = MAXY;
174 xx = 0;
175 fputs("\033;\037", stdout);
176 sendpt(1);
177 exit(0);
178}
179
180kwait()
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
199execom()
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) {
7abf8d65
KB
212 if (freopen(_PATH_TTY, "r", stdin)==NULL)
213 freopen(_PATH_DEVNULL, "r", stdin);
9fb37411 214 }
7abf8d65 215 execl(_PATH_BSHELL, "sh", "-t", 0);
9fb37411
BJ
216}
217
218sendpt(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}