home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 7 Games / 07-Games.zip / VTREK.ZIP / TERMIO.C < prev    next >
C/C++ Source or Header  |  1989-12-26  |  5KB  |  239 lines

  1. /* terminal I/O */
  2.  
  3. #include "vtrek.h"
  4.  
  5. #ifdef CPM    /* OS/2, MSDOS and UNIX users don't need to fiddle */
  6.  
  7. /*============================================================================
  8.  
  9.            TERMINAL PATCH AREA FOR CP/M VERSION
  10.  
  11. To modify the CP/M object code using DDT, ZSID or whatever:-
  12.  
  13.    Use the D(ump) command to look for the string 'CURSES' and patch the
  14.    following bytes.
  15.  
  16.    The clear-screen and clear-to-end-of-line strings are each 16 bytes
  17.    long.  The first byte indicates the number of significant characters
  18.    so 15 bytes are available for each function. */
  19.  
  20. char DDTloc[] = {'C','U','R','S','E','S'};   /* No trailing NUL */
  21.  
  22. char cl[16] = {7,025,027,0177,0177,0177,0177,0177}; /* clear screen */
  23. char ce[16] = {6,026,0177,0177,0177,0177,0177};     /* clear to end of line */
  24.  
  25. /* The following three control strings are used to build the cursor-addressing
  26.    sequence.  The format is similar to the above except that each string is
  27.    eight bytes long, i.e. length byte and up to 7 characters.  */
  28.  
  29. char cpLead[8] = {1,9};       /* Lead-in sequence (before either coordinate) */
  30. char cpMid[8]  = {0};      /* Between coordinates */
  31. char cpTrail[8] = {4,0177,0177,0177,0177}; /* After 2nd co-ordinate */
  32.  
  33. /* The next byte should be ZERO if the terminal expects to receive the
  34.    y-coordinate (line number) first, or NON-ZERO if it expects the x-
  35.    coordinate (column number) first. */
  36.  
  37. char xFirst = 1;
  38.  
  39. /* The next byte should be ZERO if the terminal expects co-ordinates to
  40.    be single-byte binary numbers, or NON-ZERO if the terminal expects
  41.    co-ordinates to be ASCII digits. */
  42.  
  43. char asciiXY = 0;
  44.  
  45. /* Finally, the next two bytes are the 'offsets' to add to the x (column)
  46.    and y (line) coordinates for cursor addressing.  The home position is
  47.    (1,1) so that for most terminals the offset will be 0x1F (31). */
  48.  
  49. char xOffset = 0xFF;
  50. char yOffset = 0xFF;
  51.  
  52. #endif        /* End of CP/M terminal patch area */
  53.  
  54. /*=========================================================================*/
  55.  
  56. #ifdef UNIX
  57. #include <sgtty.h>
  58.  
  59. static int saveflags;
  60. static char cl[5], cm[20], ce[20], bp[1024], name[20];
  61. char PC, BC[5], UP[5];
  62. #endif
  63.  
  64. /* initialize the termimal mode */
  65. terminit()
  66. {
  67. #ifdef UNIX
  68.     struct sgttyb tty;
  69.     char *p, *getenv();
  70.  
  71.     if (p = getenv("TERM"))
  72.         strcpy(name, p);
  73.     else {
  74.         fprintf(stderr, "TERM not set\n");
  75.         exit(1);
  76.     }
  77.     if (tgetent(bp, name) != 1) {
  78.         fprintf(stderr, "Can't get termcap entry\n");
  79.         exit(1);
  80.     }
  81.     p = cm;
  82.     tgetstr("cm", &p);
  83.     p = cl;
  84.     tgetstr("cl", &p);
  85.     p = ce;
  86.     tgetstr("ce", &p);
  87.     p = UP;
  88.     tgetstr("up", &p);
  89.     p = BC;
  90.     tgetstr("bc", &p);
  91.     gtty(0, &tty);
  92.     saveflags = tty.sg_flags;
  93.     tty.sg_flags |= RAW;
  94.     tty.sg_flags &= ~(ECHO | XTABS);
  95.     stty(0, &tty);
  96. #endif
  97. }
  98.  
  99. /* reset the terminal mode */
  100. termreset()
  101. {
  102. #ifdef UNIX
  103.     struct sgttyb tty;
  104.  
  105.     gtty(0, &tty);
  106.     tty.sg_flags = saveflags;
  107.     stty(0, &tty);
  108. #endif
  109. }
  110.  
  111. /* get a character from the terminal */
  112. #ifndef OS2        /* MS-DOS version of Hi-Tech C has this already */
  113.                 /* and so does the Microsoft C library */
  114. getch()
  115. {
  116. #ifdef AZTEC
  117.     int ch;
  118.     while ((ch = CPM(6, 0xff)) == 0)
  119.         ;
  120.     return ch;
  121. #endif
  122.  
  123. #ifdef HI_TECH_C
  124.     int ch;
  125.     while (!(ch = bdos(6,0xFF)));
  126.     return ch;
  127. #endif
  128.  
  129. #ifdef UNIX
  130.     return getchar() & 0177;
  131. #endif
  132. }
  133. #endif
  134.  
  135. /* write a character */
  136.  
  137. #ifndef OS2
  138. putch(ch)
  139. int ch;
  140. {
  141. #ifdef AZTEC
  142.     CPM(6, ch);
  143. #endif
  144. #ifdef HI_TECH_C
  145.     bdos(6,ch);
  146.  
  147. #else /* UNIX */
  148.     putchar(ch);
  149. #endif
  150. }
  151. #endif
  152.  
  153. #ifndef UNIX
  154. /* see if a character is ready to be read */
  155. chready()
  156. {
  157. #ifdef OS2
  158.     return kbhit();
  159. #endif
  160.  
  161. #ifdef CPM
  162. #ifdef HI_TECH_C
  163.     return bdos(11,0);
  164. #else /* AZTEC */
  165.     return CPM(11,0);
  166. #endif
  167. #endif
  168. }
  169. #endif
  170.  
  171. #ifdef CPM
  172. void vputs(s)
  173. unsigned char *s;
  174. {
  175.     unsigned char l;
  176.     for (l=*s++;l--;)
  177.     putch(*s++);
  178. }
  179. void coord(xy)
  180. unsigned char xy;
  181. {
  182.     if (asciiXY)
  183.     printf("%u",xy);
  184.     else
  185.     putch(xy);
  186. }
  187. #endif
  188.  
  189. /* move cursor */
  190. moveyx(ypos,xpos)
  191. int ypos,xpos;
  192. {
  193. #ifdef OS2
  194.     printf("%c[%d;%dH",033,ypos,xpos);
  195. #endif
  196.  
  197. #ifdef UNIX
  198.     fputs(tgoto(cm, xpos - 1, ypos - 1), stdout);
  199. #endif
  200.  
  201. #ifdef CPM
  202.     xpos += xOffset;
  203.     ypos += yOffset;
  204.     vputs(cpLead);
  205.     coord(xFirst?xpos:ypos);
  206.     vputs(cpMid);
  207.     coord(xFirst?ypos:xpos);
  208.     vputs(cpTrail);
  209. #endif
  210. }
  211.  
  212. /* clear screen */
  213. cls()
  214. {
  215. #ifdef UNIX
  216.     fputs(cl, stdout);
  217. #endif
  218. #ifdef OS2
  219.     fputs("\033[2J",stdout);
  220. #endif
  221. #ifdef CPM
  222.     vputs(cl);
  223. #endif
  224. }
  225.  
  226. /* clear end of line */
  227. ceol()
  228. {
  229. #ifdef UNIX
  230.     fputs(ce, stdout);
  231. #endif
  232. #ifdef OS2
  233.     fputs("\033[K",stdout);
  234. #endif
  235. #ifdef CPM
  236.     vputs(ce);
  237. #endif    
  238. }
  239.