# 2.11BSD#431 - /usr/src/usr.bin/tk.c

##### Functions

    ex                X
    execom            X
    init              X
    kwait             X
    main              X
    sendpt            X

##### Variables

    alpha             X
    coff              X
    maxcol            X
    ncol              X
    ohx               X
    ohy               X
    olx               X
    oly               X
    oxb               X
    pl                X
    ry                X
    xoff              X
    xx                X
    yy                X
    yyll              X

##### Macros

    CR                X
    ESC               X
    FF                X
    GS                X
    LINE              X
    MAXY              X
    SI                X
    SO                X
    US                X
    XOFF              X

       1: static char *sccsid = "@(#)tk.c	4.2 (Berkeley) 5/15/84";
       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: int xx = XOFF;
      23: int xoff = XOFF;
      24: int coff = 0;
      25: int ncol = 0;
      26: int maxcol = 1;
      27: int yy = MAXY;
      28: int ohy = -1;
      29: int ohx = -1;
      30: int oxb = -1;
      31: int oly = -1;
      32: int olx = -1;
      33: int alpha;
      34: int ry;
      35: FILE    *ttyin;
      36:
      37: main(argc, argv)
      38: int argc;
      39: char **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);
      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:
     152: init()
     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:
     170: ex()
     171: {
     172:     yy = MAXY;
     173:     xx = 0;
     174:     fputs("\033;\037", stdout);
     175:     sendpt(1);
     176:     exit(0);
     177: }
     178:
     179: kwait()
     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:
     198: execom()
     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:
     217: sendpt(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: }

#### Defined functions

ex defined in line 170; used 4 times

- in line 42, 67, 149, 194

execom defined in line 198; used 1 times

- in line 188

init defined in line 152; used 2 times

- in line 69, 87

kwait defined in line 179; used 2 times

- in line 86, 148

main defined in line 37; never used
sendpt defined in line 217; used 5 times

- in line 82, 133, 146, 167, 175

#### Defined variables

alpha defined in line 33; used 12 times

- in line 78, 93-98(2), 104-109(2), 116-126(3), 133-136(3), 246

coff defined in line 24; used 2 times

- in line 55, 162

maxcol defined in line 26; used 5 times

- in line 53, 68, 85, 155-157(2)

ncol defined in line 25; used 6 times

- in line 68, 80-85(2), 155-156(2), 165

ohx defined in line 29; used 4 times

- in line 154, 236-238(2), 244

ohy defined in line 28; used 3 times

- in line 154, 232, 241

olx defined in line 32; used 2 times

- in line 154, 245

oly defined in line 31; used 3 times

- in line 154, 236, 243

oxb defined in line 30; used 4 times

- in line 154, 234-236(2), 242

pl defined in line 20; used 2 times

- in line 48-49(2)

ry defined in line 34; used 6 times

- in line 119-125(6)

sccsid defined in line 1; never used
xoff defined in line 23; used 10 times

- in line 54, 76, 92, 102-103(2), 144, 158-163(4)

xx defined in line 22; used 15 times

- in line 54, 76, 92-108(6), 140-144(2), 163, 173, 227-230(3)

yy defined in line 27; used 11 times

- in line 74-81(4), 115-124(3), 145, 164, 172, 224

yyll defined in line 21; used 2 times

- in line 49, 79

#### Defined macros

CR defined in line 15; never used
ESC defined in line 14; never used
FF defined in line 16; never used
GS defined in line 13; used 1 times

- in line 231

LINE defined in line 10; used 7 times

- in line 20, 77, 115-125(5)

MAXY defined in line 9; used 4 times

- in line 27, 49, 164, 172

SI defined in line 18; never used
SO defined in line 17; never used
US defined in line 12; used 3 times

- in line 83, 135, 147

XOFF defined in line 11; used 3 times

- in line 22-23(2), 160

Last modified: 1987-02-17 Generated: 2016-12-26 | Generated by src2html V0.67 page hit count: 1587 |