| 1 | static char *sccsid = "@(#)tk.c 4.1 (Berkeley) %G%"; |
| 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 | |
| 20 | int pl = 66*LINE; |
| 21 | int yyll = -1; |
| 22 | char obuf[BUFSIZ]; |
| 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 | |
| 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("/dev/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 | setbuf(stdout, obuf); |
| 70 | ncol = maxcol; |
| 71 | init(); |
| 72 | while ((i = getchar()) != EOF) { |
| 73 | switch(i) { |
| 74 | |
| 75 | case FF: |
| 76 | yy = 0; |
| 77 | case '\n': |
| 78 | xx = xoff; |
| 79 | yy -= LINE; |
| 80 | alpha = 0; |
| 81 | if (yy < yyll) { |
| 82 | ncol++; |
| 83 | yy = 0; |
| 84 | sendpt(0); |
| 85 | putchar(US); |
| 86 | fflush(stdout); |
| 87 | if (ncol >= maxcol) |
| 88 | kwait(); |
| 89 | init(); |
| 90 | } |
| 91 | continue; |
| 92 | |
| 93 | case CR: |
| 94 | xx = xoff; |
| 95 | alpha = 0; |
| 96 | continue; |
| 97 | |
| 98 | case ' ': |
| 99 | xx += 31; |
| 100 | alpha = 0; |
| 101 | continue; |
| 102 | |
| 103 | case '\t': /*tabstops at 8*31=248*/ |
| 104 | j = ((xx-xoff)/248) + 1; |
| 105 | xx += j*248 - (xx-xoff); |
| 106 | alpha = 0; |
| 107 | continue; |
| 108 | |
| 109 | case '\b': |
| 110 | xx -= 31; |
| 111 | alpha = 0; |
| 112 | continue; |
| 113 | |
| 114 | case ESC: |
| 115 | switch(i = getchar()) { |
| 116 | case '7': |
| 117 | yy += LINE; |
| 118 | alpha = 0; |
| 119 | continue; |
| 120 | case '8': |
| 121 | yy += (LINE + ry)/2; |
| 122 | ry = (LINE + ry)%2; |
| 123 | alpha = 0; |
| 124 | continue; |
| 125 | case '9': |
| 126 | yy -= (LINE - ry)/2; |
| 127 | ry = -(LINE - ry)%2; |
| 128 | alpha = 0; |
| 129 | continue; |
| 130 | default: |
| 131 | continue; |
| 132 | } |
| 133 | |
| 134 | default: |
| 135 | sendpt(alpha); |
| 136 | if (alpha==0) { |
| 137 | putchar(US); |
| 138 | alpha = 1; |
| 139 | } |
| 140 | putchar(i); |
| 141 | if (i>' ') |
| 142 | xx += 31; |
| 143 | continue; |
| 144 | } |
| 145 | } |
| 146 | xx = xoff; |
| 147 | yy = 0; |
| 148 | sendpt(0); |
| 149 | putchar(US); |
| 150 | kwait(); |
| 151 | ex(); |
| 152 | } |
| 153 | |
| 154 | init() |
| 155 | { |
| 156 | ohx = oxb = olx = ohy = oly = -1; |
| 157 | if (ncol >= maxcol) { |
| 158 | ncol = 0; |
| 159 | if (maxcol > 1) |
| 160 | xoff = 0; |
| 161 | else |
| 162 | xoff = XOFF; |
| 163 | } else |
| 164 | xoff += coff; |
| 165 | xx = xoff; |
| 166 | yy = MAXY; |
| 167 | if (ncol==0) |
| 168 | fputs("\033\014\033;", stdout); |
| 169 | sendpt(0); |
| 170 | } |
| 171 | |
| 172 | ex() |
| 173 | { |
| 174 | yy = MAXY; |
| 175 | xx = 0; |
| 176 | fputs("\033;\037", stdout); |
| 177 | sendpt(1); |
| 178 | exit(0); |
| 179 | } |
| 180 | |
| 181 | kwait() |
| 182 | { |
| 183 | register c; |
| 184 | |
| 185 | fflush(stdout); |
| 186 | if (ttyin==NULL) |
| 187 | return; |
| 188 | while ((c=getc(ttyin))!='\n') { |
| 189 | if (c=='!') { |
| 190 | execom(); |
| 191 | printf("!\n"); |
| 192 | fflush(stdout); |
| 193 | continue; |
| 194 | } |
| 195 | if (c==EOF) |
| 196 | ex(); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | execom() |
| 201 | { |
| 202 | int (*si)(), (*sq)(); |
| 203 | |
| 204 | if (fork() != 0) { |
| 205 | si = signal(SIGINT, SIG_IGN); |
| 206 | sq = signal(SIGQUIT, SIG_IGN); |
| 207 | wait((int *)NULL); |
| 208 | signal(SIGINT, si); |
| 209 | signal(SIGQUIT, sq); |
| 210 | return; |
| 211 | } |
| 212 | if (isatty(fileno(stdin)) == 0) { |
| 213 | if (freopen("/dev/tty", "r", stdin)==NULL) |
| 214 | freopen("/dev/null", "r", stdin); |
| 215 | } |
| 216 | execl("/bin/sh", "sh", "-t", 0); |
| 217 | } |
| 218 | |
| 219 | sendpt(a) |
| 220 | { |
| 221 | register zz; |
| 222 | int hy,xb,ly,hx,lx; |
| 223 | |
| 224 | if (a) |
| 225 | return; |
| 226 | if ((zz = yy) < 0) |
| 227 | zz = 0; |
| 228 | hy = ((zz>>7) & 037); |
| 229 | xb = ((xx & 03) + ((zz<<2) & 014) & 017); |
| 230 | ly = ((zz>>2) & 037); |
| 231 | hx = ((xx>>7) & 037); |
| 232 | lx = ((xx>>2) & 037); |
| 233 | putchar(GS); |
| 234 | if (hy != ohy) |
| 235 | putchar(hy | 040); |
| 236 | if (xb != oxb) |
| 237 | putchar(xb | 0140); |
| 238 | if ((ly != oly) || (hx != ohx) || (xb != oxb)) |
| 239 | putchar(ly | 0140); |
| 240 | if (hx != ohx) |
| 241 | putchar(hx | 040); |
| 242 | putchar(lx | 0100); |
| 243 | ohy = hy; |
| 244 | oxb = xb; |
| 245 | oly = ly; |
| 246 | ohx = hx; |
| 247 | olx = lx; |
| 248 | alpha = 0; |
| 249 | } |