home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 0 / 0979 / screen.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  6KB  |  319 lines

  1. #include <stdio.h>
  2.  
  3. #include "system.h"
  4.  
  5. #ifdef USG
  6. # ifdef M_XENIX
  7. #  include <sys/types.h>
  8. #  include <sys/ioctl.h>
  9. # endif
  10. # include <termio.h>
  11. struct termio rawbuf;
  12. struct termio cookedbuf;
  13. #else
  14. # include <sgtty.h>                     /* terminal modes for tinit(), tend() */
  15. struct sgttyb sgbuf;                        /* buffer for terminal mode info */
  16. int rawflags, cookflags;          /* flags for raw & cooked tty mode */
  17. #endif
  18. #include <signal.h>
  19.  
  20. #define TERMBUF 1024        /* Size of term buf for termcap */
  21.  
  22. extern int nlines;
  23.  
  24. extern char *tgetstr(), *tgoto();
  25. extern int display_up;
  26. extern int    intrup;                /* Have we been interrupted? */
  27.  
  28. char *tent;                                               /* Pointer to tbuf */
  29. char PC;                                                    /* Pad character */
  30. char *UP, *BC;                                /* Upline, backsapce character */
  31. short ospeed;                                       /* Terminal output speed */
  32. char termbuf[TERMBUF];                   /* Place to put term info */
  33.  
  34. char *cm,                                                   /* Cursor motion */
  35.      *cs,                                         /* Change scrolling region */
  36.      *sf,                                         /*  - scroll forward       */
  37.      *sr,                                         /*  - scroll backwards     */
  38.      *ce,                                            /* Clear to end of line */
  39.      *cl,                                                    /* Clear screen */
  40.      *al,                                                     /* Insert line */
  41.      *dl,                                                    /* delete ditto */
  42.      *so,                                                        /* standout */
  43.      *se,                                                    /* standout end */
  44.      *us,                                                       /* underline */
  45.      *ue,                                                   /* underline end */
  46.      *ti,                            /* Init terminal */
  47.      *te;                           /* Reset terminal */
  48. int  li,                                                  /* lines on screen */
  49.      co;                                                    /* columns ditto */
  50. char xn;        /* Magic cookie kludge */
  51.  
  52. /* Screen manipulation primitives: dumb set for smart terminals */
  53. at(x, y)
  54. int x, y;
  55. {
  56.     outs(tgoto(cm, x, y));
  57. }
  58.  
  59. nl()
  60. {
  61.     outs(ce);
  62.     outc('\n');
  63. }
  64.  
  65. /* Scroll lines in window (from:to) n lines */
  66. scroll(from, to, n)
  67. int from, to, n;
  68. {
  69.     if(cs && sf && sr) {
  70.         outs(tgoto(cs, from, to-1));
  71.         if(n<0) {
  72.             at(0, from);
  73.             while(n++)
  74.                 outs(sr);
  75.         }
  76.         else {
  77.             at(0, to-1);
  78.             while(n--)
  79.                 outs(sf);
  80.         }
  81.         outs(tgoto(cs, 0, li-1));
  82.     } 
  83.     else if(al && dl) {
  84.         if(n<0) {
  85.             int i=n;
  86.             outs(tgoto(cm, 0, to+n));
  87.             while(i++)
  88.                 outs(dl);
  89.             outs(tgoto(cm, 0, from));
  90.             while(n++)
  91.                 outs(al);
  92.         } 
  93.         else {
  94.             int i=n;
  95.             outs(tgoto(cm, 0, from));
  96.             while(i--)
  97.                 outs(dl);
  98.             outs(tgoto(cm, 0, to-n));
  99.             while(n--)
  100.                 outs(al);
  101.         }
  102.     }
  103. }
  104.  
  105. tinit(name)
  106. char *name;
  107. {
  108.     char *termptr;
  109.     char tbuf[TERMBUF], *tmp;
  110.     SIGNAL  intr();
  111.     SIGNAL term();
  112. #ifdef BSD
  113.     SIGNAL  stop();
  114. #endif
  115.  
  116.     termptr = termbuf;
  117.  
  118.     tgetent(tbuf, name);
  119.  
  120.     tmp = tgetstr("pc", &termptr);
  121.     if(tmp) PC = *tmp;
  122.     UP = tgetstr("up", &termptr);
  123.     BC = tgetstr("bc", &termptr);
  124.     cm = tgetstr("cm", &termptr);
  125.     cs = tgetstr("cs", &termptr);
  126.     sf = tgetstr("sf", &termptr);
  127.     sr = tgetstr("sr", &termptr);
  128.     ce = tgetstr("ce", &termptr);
  129.     cl = tgetstr("cl", &termptr);
  130.     al = tgetstr("al", &termptr);
  131.     dl = tgetstr("dl", &termptr);
  132.     us = tgetstr("us", &termptr);
  133.     ue = tgetstr("ue", &termptr);
  134.     so = tgetstr("so", &termptr);
  135.     se = tgetstr("se", &termptr);
  136.     ti = tgetstr("ti", &termptr);
  137.     te = tgetstr("te", &termptr);
  138.     li = tgetnum("li");
  139.     co = tgetnum("co");
  140.     xn = tgetflag("xn");
  141.  
  142.     nlines=li-3;
  143.  
  144. #ifdef USG
  145.     ioctl(1, TCGETA, &rawbuf);
  146.     cookedbuf = rawbuf;
  147.     rawbuf.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
  148.     rawbuf.c_cc[VMIN] = 1;
  149.     rawbuf.c_cc[VTIME] = 0;
  150. #else
  151.     gtty(1, &sgbuf);
  152.     ospeed=sgbuf.sg_ospeed;
  153.     quickmode=ospeed<10;
  154.     cookflags=sgbuf.sg_flags;
  155.     sgbuf.sg_flags = (sgbuf.sg_flags&~ECHO)|CBREAK;
  156.     rawflags=sgbuf.sg_flags;
  157. #endif
  158.     signal(SIGINT, intr);
  159.     signal(SIGTERM, term);
  160. #ifdef BSD
  161.     signal(SIGTSTP, stop);
  162. #endif
  163.     rawtty();
  164. }
  165.  
  166. int tmode=0;
  167.  
  168. entty()
  169. {
  170.     if(!tmode)
  171.         outs(ti);
  172.     tmode=1;
  173. }
  174.  
  175. extty()
  176. {
  177.     if(tmode)
  178.         outs(te);
  179.     tmode=0;
  180. }
  181.  
  182. rawtty()
  183. {
  184. #ifdef USG
  185.     ioctl(1, TCSETA, &rawbuf);
  186. #else
  187.     sgbuf.sg_flags=rawflags;
  188.     stty(1, &sgbuf);
  189. #endif
  190.     entty();
  191. }
  192.  
  193. cooktty()
  194. {
  195. #ifdef USG
  196.     ioctl(1, TCSETA, &cookedbuf);
  197. #else
  198.     sgbuf.sg_flags=cookflags;
  199.     stty(1, &sgbuf);
  200. #endif
  201.     extty();
  202. }
  203.  
  204. SIGNAL intr()
  205. {
  206.     signal(SIGINT, intr);
  207.     intrup=1;
  208. }
  209.  
  210. SIGNAL term()
  211. {
  212.     tend();
  213.     tcl_end();
  214.     exit(0);
  215. }
  216.  
  217. #ifdef BSD
  218. SIGNAL stop()
  219. {
  220.     signal(SIGTSTP, stop);
  221.     intrup=1;
  222.     tend();
  223.     kill(getpid(), SIGSTOP);
  224.     rawtty();
  225.     display_up=0;
  226. }
  227. #endif
  228.  
  229. tend()
  230. {
  231.     end_screenmode();
  232.     cooktty();
  233.     fflush(stdout);
  234. }
  235.  
  236. outs(s)
  237. char *s;
  238. {
  239.     int  outc();
  240.  
  241.     if(s)
  242.         tputs(s, 0, outc);
  243. }
  244.  
  245. outc(c)
  246. char c;
  247. {
  248.     putchar(c);
  249. }
  250.  
  251. int somode = 0;
  252. int ulmode = 0;
  253.  
  254. standout()
  255. {
  256.     if(!somode) {
  257.         outs(so);
  258.         somode = 1;
  259.         if(xn) return 1;
  260.     }
  261.     return 0;
  262. }
  263.  
  264. underline()
  265. {
  266.     if(!ulmode) {
  267.         outs(us);
  268.         ulmode = 1;
  269.         if(xn) return 1;
  270.     }
  271.     return 0;
  272. }
  273.  
  274. standend()
  275. {
  276.     int cnt = 0;
  277.  
  278.     if(somode) {
  279.         outs(se);
  280.         somode = 0;
  281.         if(xn) cnt++;
  282.     }
  283.     if(ulmode) {
  284.         outs(ue);
  285.         ulmode = 0;
  286.         if(xn) cnt++;
  287.     }
  288.     return cnt;
  289. }
  290.  
  291. cmdline()
  292. {
  293.     at(0, li-1);
  294.     outs(ce);
  295. }
  296.  
  297. end_screenmode()
  298. {
  299.     if(display_up) {
  300.         at(0, li-1);
  301.         outc('\n');
  302.         display_up = 0;
  303.     }
  304. }
  305.  
  306. end_linemode()
  307. {
  308.     if(!display_up) {
  309.         redraw();
  310.         display_up = 1;
  311.     }
  312. }
  313.  
  314. clear_screen()
  315. {
  316.     outs(cl);
  317. }
  318.  
  319.