home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / unixen.tar / unixen / PDP-11 / Trees / V7 / usr / src / cmd / tk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1979-01-10  |  3.5 KB  |  249 lines

  1. /*
  2.  * optimize output for Tek 4014
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <signal.h>
  7.  
  8. #define MAXY 3071
  9. #define LINE 47
  10. #define XOFF 248
  11. #define US 037
  12. #define GS 035
  13. #define ESC 033
  14. #define CR 015
  15. #define FF 014
  16. #define SO 016
  17. #define SI 017
  18.  
  19. int    pl    = 66*LINE;
  20. int    yyll    = -1;
  21. char    obuf[BUFSIZ];
  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.     setbuf(stdout, obuf);
  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.  
  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.  
  171. ex()
  172. {
  173.     yy = MAXY;
  174.     xx = 0;
  175.     fputs("\033;\037", stdout);
  176.     sendpt(1);
  177.     exit(0);
  178. }
  179.  
  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.  
  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("/dev/tty", "r", stdin)==NULL)
  213.             freopen("/dev/null", "r", stdin);
  214.     }
  215.     execl("/bin/sh", "sh", "-t", 0);
  216. }
  217.  
  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.