home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / apps / spread / viscalc / curses.c < prev    next >
C/C++ Source or Header  |  1985-11-17  |  8KB  |  381 lines

  1. /**********************************************************************
  2.  *
  3.  * Tiny pseudo "curses" package (runs on U__X, VMS, or MCH_AMIGA)
  4.  *
  5.  *    v1.0    870117    DBW - D. Wecker, initial hack
  6.  *
  7.  **********************************************************************/
  8.  
  9. #ifdef VMS
  10. #include <stsdef.h>
  11. #include <ssdef.h>
  12. #include <descrip.h>
  13. #include <iodef.h>
  14. #include <ttdef.h>
  15.  
  16. #define NIBUF   128        /* Input buffer size */
  17. #define NOBUF   1024        /* MM says bug buffers win! */
  18. #define EFN     0        /* Event flag */
  19.  
  20. char obuf[NOBUF];        /* Output buffer */
  21. int nobuf;            /* # of bytes in above */
  22. char ibuf[NIBUF];        /* Input buffer */
  23. int nibuf;            /* # of bytes in above */
  24. int ibufi;            /* Read index */
  25. int oldmode[2];            /* Old TTY mode bits */
  26. int newmode[2];            /* New TTY mode bits */
  27. short iochan;            /* TTY I/O channel */
  28. struct dsc$descriptor  idsc;
  29. struct dsc$descriptor  odsc;
  30. char oname[40];
  31. int iosb[2];
  32. int term[2];
  33. int status;
  34. #endif
  35.  
  36. #ifdef MCH_AMIGA
  37. extern    char        *Open();
  38. extern    long        Read(),Write();
  39. extern    void        Close();
  40. #define NEW        1006L
  41. #define AMG_MAXBUF    1024
  42. static char        *terminal = 0L;
  43. static char        scrn_tmp[AMG_MAXBUF+1];
  44. static long        scrn_tmp_p = 0L;
  45. #endif
  46.  
  47. #ifdef U__X
  48. #include <sys/ioctl.h>
  49. #include <sgtty.h>
  50. #include <stdio.h>
  51. struct sgttyb old_tty,new_tty;
  52. #endif
  53.  
  54. #ifdef MCH_AMIGA
  55. #define    COLS    79
  56. #define ROWS    23
  57. #else
  58. #define    COLS    80
  59. #define ROWS    24
  60. #endif
  61.  
  62. #ifdef TOS
  63. #include <osbind.h>
  64. #define ST_MAXBUF 512
  65. static char        scrn_tmp[ST_MAXBUF+1];
  66. static long        scrn_tmp_p = 0L;
  67. #endif
  68.  
  69. #define NORMAL    0x00
  70. #define BOLD    0x80
  71.  
  72. char    nscrn[ROWS][COLS],
  73.     cscrn[ROWS][COLS],
  74.     row,
  75.     col,
  76.     mode;
  77. char    str[256];
  78.  
  79. move(y,x)
  80. int y,x;
  81.     {
  82.     row = y;
  83.     col = x;
  84.     }
  85.  
  86. clrtoeol() {
  87.     int i;
  88.  
  89.     for (i = col; i < COLS; i++) nscrn[row][i] = ' ' | mode;
  90.     }
  91.  
  92. printw(fmt,a1,a2,a3,a4,a5)
  93. char    *fmt,*a1,*a2,*a3,*a4,*a5;
  94.     {
  95.     int i,j;
  96.  
  97.     sprintf(str,fmt,a1,a2,a3,a4,a5);
  98.     j = 0;
  99.     for (i = col; i < COLS && str[j] != '\000'; i++)
  100.     nscrn[row][i] = str[j++] | mode;
  101.     col = i;
  102.     }
  103.  
  104. clrtobot() {
  105.     int i,j;
  106.  
  107.     clrtoeol();
  108.     for (i = row+1; i < ROWS; i++)
  109.     for (j = 0; j < COLS; j++)
  110.         nscrn[i][j] = ' ' | mode;
  111.     }
  112.  
  113. standout() {
  114.     mode = BOLD;
  115.     }
  116.  
  117. standend() {
  118.     mode = NORMAL;
  119.     }
  120.  
  121. addstr(s)
  122. char    *s;
  123.     {
  124.     printw("%s",s);
  125.     }
  126.  
  127. initscr() {
  128.     int        i,j;
  129.  
  130. #ifdef MCH_AMIGA
  131.     terminal = Open("RAW:1/1/639/199/DBW_VC (v1.0 870117)",(long)NEW);
  132. #endif
  133. #ifdef VMS
  134.     odsc.dsc$a_pointer = "TT";
  135.     odsc.dsc$w_length = strlen(odsc.dsc$a_pointer);
  136.     odsc.dsc$b_dtype = DSC$K_DTYPE_T;
  137.     odsc.dsc$b_class = DSC$K_CLASS_S;
  138.     idsc.dsc$b_dtype = DSC$K_DTYPE_T;
  139.     idsc.dsc$b_class = DSC$K_CLASS_S;
  140.     do {
  141.     idsc.dsc$a_pointer = odsc.dsc$a_pointer;
  142.     idsc.dsc$w_length = odsc.dsc$w_length;
  143.     odsc.dsc$a_pointer = &oname[0];
  144.     odsc.dsc$w_length = sizeof(oname);
  145.     status = LIB$SYS_TRNLOG(&idsc, &odsc.dsc$w_length, &odsc);
  146.     if (status!=SS$_NORMAL && status!=SS$_NOTRAN) exit(status);
  147.     if (oname[0] == 0x1B) {
  148.         odsc.dsc$a_pointer += 4;
  149.         odsc.dsc$w_length -= 4;
  150.         }
  151.     }
  152.     while (status == SS$_NORMAL);
  153.     status = SYS$ASSIGN(&odsc, &iochan, 0, 0);
  154.     if (status != SS$_NORMAL) exit(status);
  155.     status = SYS$QIOW(EFN, iochan, IO$_SENSEMODE, iosb, 0, 0,
  156.         oldmode, sizeof(oldmode), 0, 0, 0, 0);
  157.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  158.     newmode[0] = oldmode[0];
  159.     newmode[1] = oldmode[1] | TT$M_PASSALL | TT$M_NOECHO;
  160.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  161.         newmode, sizeof(newmode), 0, 0, 0, 0);
  162.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  163. #endif
  164.  
  165. #ifdef U__X
  166.     ioctl(0,TIOCGETP,&old_tty);
  167.     ioctl(0,TIOCGETP,&new_tty);
  168.     new_tty.sg_flags |= RAW;
  169.     new_tty.sg_flags &= ~ECHO;
  170.     ioctl(0,TIOCSETP,&new_tty);
  171. #endif
  172.  
  173.     row        = 0;
  174.     col        = 0;
  175.     mode    = NORMAL;
  176.     for (i = 0; i < ROWS; i++)
  177.     for (j = 0; j < COLS; j++)
  178.         nscrn[i][j] = cscrn[i][j] = ' ';
  179.     ttputs("\033E");
  180.     }
  181.  
  182. clear() {
  183.     row = 0;
  184.     col = 0;
  185.     clrtobot();
  186.     }
  187.  
  188. endwin() {
  189.     move(ROWS-1,0);
  190.     refresh();
  191.  
  192. #ifdef MCH_AMIGA
  193.     amg_flush();
  194.     Close(terminal);
  195. #endif
  196.  
  197. #ifdef VMS
  198.     status = SYS$QIOW(EFN, iochan, IO$_SETMODE, iosb, 0, 0,
  199.         oldmode, sizeof(oldmode), 0, 0, 0, 0);
  200.     if (status!=SS$_NORMAL || (iosb[0]&0xFFFF)!=SS$_NORMAL) exit(status);
  201.     status = SYS$DASSGN(iochan);
  202.     if (status != SS$_NORMAL) exit(status);
  203. #endif
  204.  
  205. #ifdef U__X
  206.     ioctl(0,TIOCSETP,&old_tty);
  207. #endif
  208.  
  209.     }
  210.  
  211. char inch() {
  212.     return(nscrn[row][col] & 0x7F);
  213.     }
  214.  
  215. touchwin() {
  216.     int i,j;
  217.  
  218.     for (i=0; i<ROWS; i++)
  219.     for (j=0; j<COLS; j++)
  220.         cscrn[i][j] = ' ';
  221.     ttputs("\033E");
  222.     }
  223.  
  224. refresh() {
  225.     int    i,j,mode;
  226.  
  227.     mode = NORMAL;
  228.     for (i=0; i < ROWS; i++) {
  229.     for (j = 0; j < COLS; j++) {
  230.         if (nscrn[i][j] != cscrn[i][j]) {
  231.         sprintf(str,"\033Y%c%c",i+32,j+32);
  232.         ttputs(str);
  233.         while (nscrn[i][j] != cscrn[i][j]) {
  234.             if (mode == NORMAL && (nscrn[i][j] & BOLD) == BOLD) {
  235.             ttputs("\033p");
  236.             mode = BOLD;
  237.             }
  238.             else if (mode == BOLD && (nscrn[i][j] & BOLD) == NORMAL) {
  239.             ttputs("\033q");
  240.             mode = NORMAL;
  241.             }
  242.             cscrn[i][j] = nscrn[i][j];
  243.             ttputc(nscrn[i][j] & 0x7F);
  244.             j++;
  245.             }
  246.         }
  247.         }
  248.     }
  249.     sprintf(str,"\033Y%c%c",row+32,col+32);
  250.     ttputs(str);
  251.     if (mode) ttputs("\033q");
  252.     ttflush();
  253.     }
  254.  
  255. ttgetc() {
  256. #ifdef MCH_AMIGA
  257.     unsigned char ch[2];
  258.  
  259.     Read(terminal, ch, 1L);
  260.     return (ch[0] & 0xFF);
  261. #endif
  262.  
  263. #ifdef VMS
  264.     while (ibufi >= nibuf) {
  265.     ibufi = 0;
  266.     term[0] = 0;
  267.     term[1] = 0;
  268.     status = SYS$QIOW(EFN, iochan, IO$_READLBLK|IO$M_TIMED,
  269.     iosb, 0, 0, ibuf, NIBUF, 0, term, 0, 0);
  270.     if (status != SS$_NORMAL) exit(status);
  271.     status = iosb[0] & 0xFFFF;
  272.     if (status!=SS$_NORMAL && status!=SS$_TIMEOUT) exit(status);
  273.     nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  274.     if (nibuf == 0) {
  275.         status = SYS$QIOW(EFN, iochan, IO$_READLBLK,
  276.         iosb, 0, 0, ibuf, 1, 0, term, 0, 0);
  277.         if (status != SS$_NORMAL || (status = (iosb[0]&0xFFFF)) != SS$_NORMAL)
  278.         exit(status);
  279.         nibuf = (iosb[0]>>16) + (iosb[1]>>16);
  280.         }
  281.     }
  282.     return (ibuf[ibufi++] & 0x7F);
  283. #endif
  284.  
  285. #ifdef U__X
  286.     return(getchar() & 0x7F);
  287. #endif
  288.  
  289. #ifdef TOS
  290.     char c, hi, lo;
  291.     long keycode=Crawcin();
  292.     lo=keycode&0xff;
  293.     hi=(keycode>>16)&0xff;
  294.     switch (hi) {
  295.         case 0x62 : {c='?'; break;}
  296.         case 0x48 : {c='\020'; break;}
  297.         case 0x4b : {c='\002'; break;}
  298.         case 0x4d : {c='\006'; break;}
  299.         case 0x50 : {c='\016'; break;}
  300.         default : {c=lo;}
  301.     }
  302.     return (c);
  303.  
  304. #endif
  305.     }
  306.  
  307. ttputc(c)
  308. #ifdef MCH_AMIGA
  309. char c;
  310. #endif
  311.     {
  312. #ifdef MCH_AMIGA
  313.     scrn_tmp[scrn_tmp_p++] = c;
  314.     if(scrn_tmp_p>=AMG_MAXBUF) amg_flush();
  315. #endif
  316.  
  317. #ifdef TOS
  318.     scrn_tmp[scrn_tmp_p++] = c;
  319.     if(scrn_tmp_p>=ST_MAXBUF) st_flush();
  320. #endif
  321.  
  322. #ifdef VMS
  323.     if (nobuf >= NOBUF) ttflush();
  324.     obuf[nobuf++] = c;
  325. #endif
  326.  
  327. #ifdef U__X
  328.     fputc(c, stdout);
  329. #endif
  330.     }
  331.  
  332. #ifdef MCH_AMIGA
  333. amg_flush()
  334.     {
  335.     if(scrn_tmp_p) Write(terminal,scrn_tmp,(long)scrn_tmp_p);
  336.     scrn_tmp_p = 0;
  337.     }
  338. #endif
  339.  
  340. #ifdef TOS
  341. st_flush()
  342.     {
  343.     scrn_tmp[scrn_tmp_p]=0;
  344.     if(scrn_tmp_p) cputs(scrn_tmp);
  345.     scrn_tmp_p = 0;
  346.     }
  347. #endif
  348.  
  349. ttputs(s)
  350. char    *s;
  351.     {
  352.     while (*s) ttputc(*s++);
  353.     }
  354.  
  355. ttflush()
  356.     {
  357. #ifdef MCH_AMIGA
  358.     amg_flush();
  359. #endif
  360.  
  361. #ifdef TOS
  362.     st_flush();
  363. #endif
  364.  
  365. #ifdef VMS
  366.     status = SS$_NORMAL;
  367.     if (nobuf != 0) {
  368.     status = SYS$QIOW(EFN, iochan, IO$_WRITELBLK|IO$M_NOFORMAT,
  369.     iosb, 0, 0, obuf, nobuf, 0, 0, 0, 0);
  370.     if (status == SS$_NORMAL) status = iosb[0] & 0xFFFF;
  371.     nobuf = 0;
  372.     }
  373.     return (status);
  374. #endif
  375.  
  376. #ifdef U__X
  377.     fflush(stdout);
  378. #endif
  379.     }
  380.  
  381.