home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / program / d / elvis / Source / c / curses < prev    next >
Encoding:
Text File  |  1990-04-15  |  6.6 KB  |  249 lines

  1. /* curses.c */
  2.  
  3. /* Author:
  4.  *      Steve Kirkendall
  5.  *      16820 SW Tallac Way
  6.  *      Beaverton, OR 97006
  7.  *      kirkenda@jove.cs.pdx.edu, or ...uunet!tektronix!psueea!jove!kirkenda
  8.  */
  9.  
  10.  
  11. /* This file contains the functions & variables needed for a tiny subset of
  12.  * curses.  The principle advantage of this version of curses is its
  13.  * extreme speed.  Disadvantages are potentially larger code, few supported
  14.  * functions, limited compatibility with full curses, and only stdscr.
  15.  */
  16.  
  17. #include "curses.h"
  18. #include <signal.h>
  19. #ifdef ARC
  20. extern vdu();
  21. #endif
  22.  
  23. extern char     *malloc();
  24. extern char     *getenv();
  25.  
  26. /* variables, publicly available & used in the macros */
  27. short   ospeed;         /* speed of the tty, eg B2400 */
  28. WINDOW  *stdscr;        /* pointer into kbuf[] */
  29. WINDOW  kbuf[KBSIZ];    /* a very large output buffer */
  30. int     LINES;          /* :li#: number of rows */
  31. int     COLS;           /* :co#: number of columns */
  32. int     AM;             /* :am:  boolean: auto margins? */
  33. int     PT;             /* :pt:  boolean: physical tabs? */
  34. char    *VB="\033VB";   /* :vb=: visible bell */
  35. char    *UP="\033UP";   /* :up=: move cursor up */
  36. char    *SC="";         /* :sc=: save cursor position & char attributes */
  37. char    *RC="";         /* :rc=: resore cursor position & char attributes */
  38. char    *SO="";         /* :so=: standout start */
  39. char    *SE="";         /* :se=: standout end */
  40. char    *US="";         /* :us=: underline start */
  41. char    *UE="";         /* :ue=: underline end */
  42. char    *VB_s="";       /* :VB=: bold start */
  43. char    *VB_e="";       /* :Vb=: bold end */
  44. char    *AS="";         /* :as=: alternate (italic) start */
  45. char    *AE="";         /* :ae=: alternate (italic) end */
  46. char    *CM="\033CM";   /* :cm=: cursor movement */
  47. char    *CE="\033CE";   /* :ce=: clear to end of line */
  48. char    *CL="\033CL";   /* :cl=: home cursor & clear screen */
  49. char    *CD="\033CD";   /* :cd=: clear to end of screen */
  50. char    *AL="\033IL";   /* :al=: add a line */
  51. char    *DL="\033DL";   /* :dl=: delete a line */
  52. char    *SR="\033SR";   /* :sr=: scroll reverse */
  53. char    *KU="\213";     /* :ku=: key sequence sent by up arrow */
  54. char    *KD="\212";     /* :kd=: key sequence sent by down arrow */
  55. char    *KL="\210";     /* :kl=: key sequence sent by left arrow */
  56. char    *KR="\211";     /* :kr=: key sequence sent by right arrow */
  57. char    *IM="";         /* :im=: insert mode start */
  58. char    *IC="\033IC";   /* :ic=: insert the following character */
  59. char    *EI="";         /* :ei=: insert mode end */
  60. char    *IL="\033IL";   /* :il=: insert line */
  61. char    *DC="\033DC";   /* :dc=: delete a character */
  62. char    *aend="";       /* end an attribute -- either UE or VB_e */
  63. char    ERASEKEY=127;   /* backspace key taken from ioctl structure */
  64.  
  65. static char     *capbuf;        /* capability string buffer */
  66.  
  67. initscr()
  68. {
  69.         /* make sure TERM variable is set */
  70.         
  71.         /* set cursor editing off */
  72.         _kernel_osbyte(4,1,0);
  73.         /* disable escape key */
  74.         _kernel_osbyte(229,27,0);
  75.  
  76.     vdu("22,12");
  77.  
  78.     /* set cursor to block */
  79.     vdu("23,0,10,96,0,0,0,0,0,0");
  80.  
  81.     /* set wrap off */
  82.     vdu("23,16,1,254,0,0,0,0,0,0");
  83.  
  84.         /* start termcap stuff */
  85.         starttcap();
  86.  
  87.         /* create stdscr and curscr */
  88.         stdscr = kbuf;
  89.  
  90.         /* change the terminal mode to raw/noecho */
  91.         resume_curses(TRUE);
  92. }
  93.  
  94.  
  95. endwin()
  96. {
  97.         /* change the terminal mode back the way it was */
  98.  
  99.         /* set cusor back to nornal */
  100.         _kernel_osbyte(4,0,0);
  101.         /* enable escape key */
  102.         _kernel_osbyte(229,0,0);
  103.     /* set cursor to line */
  104.     vdu("23,0,10,103,0,0,0,0,0,0");
  105.     /* set wrap on */
  106.     vdu("23,16,0,254,0,0,0,0,0,0");
  107.  
  108.         suspend_curses();
  109. }
  110.  
  111. suspend_curses()
  112. {
  113.         /* change the terminal mode back the way it was */
  114. }
  115.  
  116. resume_curses(quietly)
  117.         int     quietly;
  118. {       
  119.         char    *src, *dest;
  120.  
  121.         /* change the terminal mode to raw/noecho */
  122.  
  123.         if (quietly) return;
  124.  
  125.         /* Wait for a key from the user */
  126.         for (dest = kbuf, src = tgoto(CM, 0, LINES - 1); *src; )
  127.                 *dest++ = *src++;
  128.         for (src = SO; src && *src; )
  129.                 *dest++ = *src++;
  130.         for (src = "[Press <RETURN> to continue]"; *src; )
  131.                 *dest++ = *src++;
  132.         for (src = SE; src && *src; )
  133.                 *dest++ = *src++;
  134.         write(1, kbuf, (int)(dest - kbuf));
  135.         read(0, kbuf, 20); /* in RAW mode, so <20 is very likely */
  136.  
  137.  
  138.         /* !!! special processing of the : key for Elvis' VI mode */
  139.         if (kbuf[0] == ':')
  140.         {
  141.                 ungetkey(':');
  142.         }
  143. }
  144.  
  145. static lacking(s)
  146.         char    *s;
  147. {
  148.         write(2, "This termcap entry lacks the :", 30);
  149.         write(2, s, 2);
  150.         write(2, "=: capability\n", 14);
  151.         exit(1);
  152. }
  153.  
  154. starttcap()
  155. {
  156.         char    *str;
  157.         static char     cbmem[800];
  158.         
  159.         getsize();
  160.  
  161.         /* allocate memory for capbuf */
  162.         capbuf = cbmem;
  163.         
  164. }
  165.  
  166.  
  167. /* This function gets the window size.  It uses the TIOCGWINSZ ioctl call if
  168.  * your system has it, or tgetnum("li") and tgetnum("co") if it doesn't.
  169.  * This function is called once during initialization, and thereafter it is
  170.  * called whenever the SIGWINCH signal is sent to this process.
  171.  */
  172. getsize(signo)
  173.         int     signo;
  174. {
  175.         /* reset the signal vector */
  176.  
  177.         /* get the window size, one way or another. */
  178.         LINES = 32;
  179.         COLS = 80;
  180.  
  181.         /* Make sure we got values that we can live with */
  182.         if (LINES < 2 || COLS < 30)
  183.         {
  184.                 write(2, "Screen too small\n", 17);
  185.                 endwin();
  186.                 exit(2);
  187.         }
  188. }
  189.  
  190.  
  191. /* This is a function version of addch() -- it is used by tputs() */
  192. int faddch(ch)
  193.         int     ch;
  194. {
  195.         addch(ch);
  196. }
  197.  
  198. #ifdef CRUNCH
  199. /* These functions are equivelent to the macros of the same names... */
  200.  
  201. void qaddstr(str)
  202.         char    *str;
  203. {
  204.         register char *s_, *d_;
  205.  
  206.         for (s_=(str), d_=stdscr; *d_++ = *s_++; )
  207.         {
  208.         }
  209.         stdscr = d_ - 1;
  210. }
  211.  
  212. void attrset(a)
  213.         int     a;
  214. {
  215.         tputs(aend, 1, faddch);
  216.         if (a == A_BOLD)
  217.         {
  218.                 tputs(VB_s, 1, faddch);
  219.                 aend = VB_e;
  220.         }
  221.         else if (a == A_UNDERLINE)
  222.         {
  223.                 tputs(US, 1, faddch);
  224.                 aend = UE;
  225.         }
  226.         else if (a == A_ALTCHARSET)
  227.         {
  228.                 tputs(AS, 1, faddch);
  229.                 aend = AE;
  230.         }
  231.         else
  232.         {
  233.                 aend = "";
  234.         }
  235. }
  236.  
  237.  
  238. void insch(ch)
  239.         int     ch;
  240. {
  241. /*        if (IM)
  242.                 tputs(IM, 1, faddch); */
  243.         tputs(IC, 1, faddch);
  244.         qaddch(ch);
  245.         /* if (EI)
  246.                 tputs(EI, 1, faddch); */
  247. }
  248. #endif
  249.