home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 183_01 / ansi.c < prev    next >
Text File  |  1985-11-22  |  8KB  |  258 lines

  1. /*************************************************************************
  2. main function to test some of the ansi device driving functions
  3. which follow below.
  4.  
  5. written by Rex Jaeschke of Rockville, MD. 1983 (301) 251-8987
  6. compiler used DeSmet v2.2
  7.  
  8. ci () is a DeSmet compiler specific function which does direct console
  9. input of 1 character without echoing it. It is used in sgrdsr and the
  10. testing program only. All else should run on any compiler.
  11. *************************************************************************/
  12.  
  13. #define CTRL_C 3
  14. #define BELL 7
  15. #define NULL '\0'
  16. #define LINEMIN 1       /* Minimum screen line # */
  17. #define LINEMAX 25      /* Maximum screen line # */
  18. #define COLMMIN 1       /* Minimum screen column # */
  19. #define COLMMAX 80      /* Maximum screen column # */
  20. #define LARROW 75
  21. #define RARROW 77
  22. #define UARROW 72
  23. #define DARROW 80
  24. #define HOME 71
  25. #define END 79
  26. #define CHOME 119
  27.  
  28. main ()
  29. {
  30.        int c;
  31.  
  32.        scred ();                       /* clear screen */
  33.        scrcup (12,40);         /* move to screen center */
  34.        while ((c = ci()) != CTRLC) {
  35.                if (c == NULL) {        /* do we have extended code? */
  36.                        c = ci();
  37.                        switch (c) {
  38.                        case LARROW:
  39.                                scrcub (1);
  40.                                break;
  41.                        case RARROW:
  42.                                scrcuf (1);
  43.                                break;
  44.                        case UARROW:
  45.                                scrcuu (1);
  46.                                break;
  47.                        case DARROW:
  48.                                scrcud (1);
  49.                                break;
  50.                        case CHOME:
  51.                                scred ();
  52.                                break;
  53.                        case HOME:
  54.                                scrcup (LINEMIN,COLMMIN);
  55.                                break;
  56.  
  57.                        case END:
  58.                                scrcup (LINEMAX,COLMMAX);
  59.                                break;
  60.                        default:
  61.                                putchar (BELL);
  62.                                break;
  63.                        }
  64.                }
  65.                else
  66.                        putchar (BELL);
  67.        }               
  68. }
  69.  
  70.  
  71. #define ESCAPE 27       /* ASCII ESC character definition */
  72.  
  73. /*************************************************************************
  74. SCR_CUB - Cursor Backward.
  75.  
  76. Moves the cursor backward n columns. Current line remains unchanged. If # of
  77. columns exceeds left-of-screen, cursor is left at the left-most column.
  78. *************************************************************************/
  79.  
  80. scrcub (ncolms)
  81. int ncolms;
  82. {
  83.        printf ("%c[%dD",ESCAPE,ncolms);
  84. }
  85.  
  86. /*************************************************************************
  87. SCRCUD - Cursor Down.
  88.  
  89. Moves the cursor down n lines. Current column remains unchanged. If # of lines
  90. to move down exceeds bottom-of-screen, cursor is left at the bottom.
  91. *************************************************************************/
  92.  
  93. scrcud (nlines)
  94. int nlines;
  95. {
  96.        printf ("%c[%dB",ESCAPE,nlines);
  97. }
  98.  
  99. /*************************************************************************
  100. SCRCUF - Cursor Forward.
  101.  
  102. Moves the cursor forward n columns. Current line remains unchanged. If # of
  103. columns exceeds right-of-screen, cursor is left at the right-most column.
  104. *************************************************************************/
  105.  
  106. scrcuf (ncolms)
  107. int ncolms;
  108. {
  109.        printf ("%c[%dC",ESCAPE,ncolms);
  110. }
  111.  
  112.  
  113. /*************************************************************************
  114. SCRCUP - Cursor Position. (same as HVP)
  115.  
  116. Moves the cursor to the specified position line,colm.
  117. *************************************************************************/
  118.  
  119. scrcup (line,colm)
  120. int line,colm;
  121. {
  122.        printf ("%c[%d;%dH",ESCAPE,line,colm);
  123. }
  124.  
  125. /*************************************************************************
  126. SCRCUU - Cursor Up.
  127.  
  128. Moves the cursor up n lines. Current column remains unchanged. If # of lines
  129. to move up exceeds top-of-screen, cursor is left at the top.
  130. *************************************************************************/
  131.  
  132. scrcuu (nlines)
  133. int nlines;
  134. {
  135.        printf ("%c[%dA",ESCAPE,nlines);
  136. }
  137.  
  138. /*************************************************************************
  139. SCRDSR - Device Status Report.
  140.  
  141. Returns the Cursor Position Report (CPR) sequence in the form ESC[line;colmR
  142.  
  143. ci () is a DeSmet compiler specific function which does direct console
  144. input of 1 character without echoing it.
  145. *************************************************************************/
  146.  
  147. scrdsr (line,colm)
  148. int *line,*colm;
  149. {
  150.        int i = 0;
  151.        char cpr[10];
  152.  
  153.        printf ("%c[6n",ESCAPE);
  154.        while ((cpr[i++] = ci ()) != 'R')
  155.                ;
  156.        cpr[i] = '\0';
  157.  
  158. /* format of cpr[] is ESC[rr;ccR row and colm are always two digits */
  159.  
  160.        *line = ((cpr[2]-'0')*10)+cpr[3]-'0';
  161.        *colm = ((cpr[5]-'0')*10)+cpr[6]-'0';
  162. }
  163.  
  164. /*************************************************************************
  165. SCRED - Erase in Display.
  166.  
  167. Erases all of the screen leaving the cursor at home
  168.  
  169. *************************************************************************/
  170.  
  171. scred ()
  172. {
  173.        printf ("%c[2J",ESCAPE);
  174. }
  175.  
  176. /*************************************************************************
  177. SCREL - Erase in Line.
  178.  
  179. Erases from the cursor to the end of the line including the cursor position.
  180. *************************************************************************/
  181.  
  182. screl ()
  183. {
  184.        printf ("%c[2K",ESCAPE);
  185. }
  186.  
  187. /*************************************************************************
  188. SCRHVP - Horizontal and Vertical Position. (same as CUP)
  189.  
  190. Moves the cursor to the specified position line,colm.
  191. *************************************************************************/
  192.  
  193. scrhvp (line,colm)
  194. int line,colm;
  195. {
  196.        printf ("%c[%d;%dH",ESCAPE,line,colm);
  197. }
  198.  
  199. /*************************************************************************
  200. SCRRCP - Restore Cursor Position.
  201.  
  202. Restores the cursor to the value it had when previously saved by scr_scp.
  203. *************************************************************************/
  204.  
  205. scrrcp ()
  206. {
  207.        printf ("%c[u",ESCAPE);
  208. }
  209.  
  210. /*************************************************************************
  211. SCRSCP - Save Cursor Position.
  212.  
  213. Saves the current cursor position for later restoration by scr_rcp.
  214. *************************************************************************/
  215.  
  216. scrscp ()
  217. {
  218.        printf ("%c[s",ESCAPE);
  219. }
  220.  
  221. /*************************************************************************
  222. SCRSGR - Set Graphics Rendition.
  223.  
  224.  
  225. Sets the character attribute specified by the parameter.
  226. Attributes remain in force until reset or changed.
  227. *************************************************************************/
  228.  
  229. scrsgr (attrib)
  230. int attrib;
  231. {
  232.        printf ("%c[%dm",ESCAPE,attrib);
  233. }
  234.  
  235. /*************************************************************************
  236. SCRSM - Set Mode.
  237.  
  238. Sets the screen width or type specified by the parameter.
  239. *************************************************************************/
  240.  
  241. scrsm (param)
  242. int param;
  243. {
  244.        printf ("%c[=%dh",ESCAPE,param);
  245. }
  246.  
  247. /*************************************************************************
  248. SCRRM - Reset Mode.
  249.  
  250. Sets the screen width or type specified by the parameter.
  251. *************************************************************************/
  252.  
  253. scrrm (param)
  254. int param;
  255. {
  256.        printf ("%c[=%dl",ESCAPE,param);
  257. }
  258.