home *** CD-ROM | disk | FTP | other *** search
/ The Devil's Doorknob BBS Capture (1996-2003) / devilsdoorknobbbscapture1996-2003.iso / Dloads / PROGRAMM / SERIAL.ZIP / ANSI.C next >
C/C++ Source or Header  |  1992-01-29  |  12KB  |  593 lines

  1. /********************************************************************
  2. * ansi.c - ANSI Terminal Driver                                     *
  3. *          Copyright (c) 1992 By Mark D. Goodwin                    *
  4. ********************************************************************/
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <conio.h>
  9. #include <stdarg.h>
  10. #ifndef __TURBOC__
  11. #include <graph.h>
  12. #endif
  13. #include "serial.h"
  14.  
  15. static int cnt, crow = 1, ccol = 1;
  16. static unsigned char buffer[256];
  17. int ansi_dsr_flag = FALSE;
  18. int (*ansi_dsr)(unsigned char n);
  19.  
  20. /* check for numeric digit */
  21. static int bindigit(int c)
  22. {
  23.     return c >= '0' && c <= '9';
  24. }
  25.  
  26. /* convert ASCII to binary */
  27. static unsigned char *ascbin(unsigned char *s, int *n)
  28. {
  29.     *n = 0;
  30.     while (TRUE) {
  31.         if (bindigit(*s)) {
  32.             *n *= 10;
  33.             *n += (*s - '0');
  34.             s++;
  35.             continue;
  36.         }
  37.         return s;
  38.     }
  39. }
  40.  
  41. /* convert IBM color attribute to ANSI text string */
  42. char *ibmtoansi(int att, char *s)
  43. {
  44.     s[0] = 0;
  45.     strcat(s, "\x1b[0");
  46.     if (att & 0x80)
  47.         strcat(s, ";5");
  48.     if (att & 0x8)
  49.         strcat(s, ";1");
  50.     switch (att & 7) {
  51.         case 0:
  52.             strcat(s, ";30");
  53.             break;
  54.         case 1:
  55.             strcat(s, ";34");
  56.             break;
  57.         case 2:
  58.             strcat(s, ";32");
  59.             break;
  60.         case 3:
  61.             strcat(s, ";36");
  62.             break;
  63.         case 4:
  64.             strcat(s, ";31");
  65.             break;
  66.         case 5:
  67.             strcat(s, ";35");
  68.             break;
  69.         case 6:
  70.             strcat(s, ";33");
  71.             break;
  72.         case 7:
  73.             strcat(s, ";37");
  74.     }
  75.     switch ((att >> 4) & 7) {
  76.         case 0:
  77.             strcat(s, ";40");
  78.             break;
  79.         case 1:
  80.             strcat(s, ";44");
  81.             break;
  82.         case 2:
  83.             strcat(s, ";42");
  84.             break;
  85.         case 3:
  86.             strcat(s, ";46");
  87.             break;
  88.         case 4:
  89.             strcat(s, ";41");
  90.             break;
  91.         case 5:
  92.             strcat(s, ";45");
  93.             break;
  94.         case 6:
  95.             strcat(s, ";43");
  96.             break;
  97.         case 7:
  98.             strcat(s, ";47");
  99.     }
  100.     strcat(s, "m");
  101.     return s;
  102. }
  103.  
  104. #ifndef __TURBOC__
  105. void clreol(void)
  106. {
  107.     int i, r1, c1, r2, c2;
  108.     struct rccoord cpos;
  109.  
  110.     _gettextwindow(&r1, &c1, &r2, &c2);
  111.     cpos = _gettextposition();
  112.     _settextwindow(cpos.row, cpos.col, cpos.row, c2);
  113.     _clearscreen(_GWINDOW);
  114.     _settextwindow(r1, c1, r2, c2);
  115.     _settextposition(cpos.row, cpos.col);
  116. }
  117. #endif
  118.  
  119. /* ANSI emulation routine */
  120. void ansiout(int c)
  121. {
  122.     int n, row, col;
  123.     #ifndef __TURBOC__
  124.     int r1, c1, r2, c2;
  125.     struct rccoord cpos;
  126.     #endif
  127.     unsigned char *bufptr;
  128.     #ifdef __TURBOC__
  129.     struct text_info tinfo;
  130.     #endif
  131.  
  132.     if (!cnt) {
  133.         if (c != 27)
  134.             switch (c) {
  135.                 case 0:
  136.                     break;
  137.                 case '\n':
  138.                     #ifdef __TURBOC__
  139.                     gettextinfo(&tinfo);
  140.                     if (wherey() + 1 > tinfo.winbottom) {
  141.                         movetext(1, 2, tinfo.winright,
  142.                             tinfo.winbottom, 1, 1);
  143.                         gotoxy(1, tinfo.winbottom);
  144.                         clreol();
  145.                     }
  146.                     else
  147.                         gotoxy(1, wherey() + 1);
  148.                     #else
  149.                     _gettextwindow(&r1, &c1, &r2, &c2);
  150.                     cpos = _gettextposition();
  151.                     if (cpos.row + 1 > r2) {
  152.                         _scrolltextwindow(1);
  153.                         _settextposition(r2, 1);
  154.                         clreol();
  155.                     }
  156.                     else
  157.                         _settextposition(cpos.row + 1, 1);
  158.                     #endif
  159.                     break;
  160.                 case 8:
  161.                 case 127:
  162.                     #ifdef __TURBOC__
  163.                     if (wherex() != 1) {
  164.                         putch(8);
  165.                         putch(' ');
  166.                         putch(8);
  167.                     }
  168.                     #else
  169.                     cpos = _gettextposition();
  170.                     if (cpos.col != 1) {
  171.                         _settextposition(cpos.row, cpos.col - 1);
  172.                         _outmem(" ", 1);
  173.                         _settextposition(cpos.row, cpos.col - 1);
  174.                     }
  175.                     #endif
  176.                     break;
  177.                 case 13:
  178.                     #ifdef __TURBOC__
  179.                     gotoxy(1, wherey());
  180.                     #else
  181.                     cpos = _gettextposition();
  182.                     _settextposition(cpos.row, 1);
  183.                     #endif
  184.                     break;
  185.                 case '\t':
  186.                     do {
  187.                     #ifdef __TURBOC__
  188.                         putch(' ');
  189.                     } while (wherex() % 8 != 1);
  190.                     #else
  191.                         _outmem(" ", 1);
  192.                         cpos = _gettextposition();
  193.                     } while (cpos.row % 8 != 1);
  194.                     #endif
  195.                     break;
  196.                 case 12:
  197.                     #ifdef __TURBOC__
  198.                     clrscr();
  199.                     #else
  200.                     _clearscreen(_GWINDOW);
  201.                     #endif
  202.                     break;
  203.                 default:
  204.                     #ifdef __TURBOC__
  205.                     putch(c);
  206.                     #else
  207.                     _outmem((char *)&c, 1);
  208.                     #endif
  209.             }
  210.         else {
  211.             buffer[cnt] = (unsigned char)c;
  212.             cnt++;
  213.         }
  214.         return;
  215.     }
  216.     if (cnt == 1) {
  217.         if (c == '[') {
  218.             buffer[cnt] = (unsigned char)c;
  219.             cnt++;
  220.         }
  221.         else {
  222.             #ifdef __TURBOC__
  223.             putch(27);
  224.             #else
  225.             _outmem("\x1b", 1);
  226.             #endif
  227.             if (c != 27) {
  228.                 #ifdef __TURBOC__
  229.                 putch(c);
  230.                 #else
  231.                 _outmem((char *)&c, 1);
  232.                 #endif
  233.                 cnt = 0;
  234.             }
  235.         }
  236.         return;
  237.     }
  238.     if (cnt == 2) {
  239.         switch (c) {
  240.             case 's':
  241.                 #ifdef __TURBOC__
  242.                 crow = wherey();
  243.                 ccol = wherex();
  244.                 #else
  245.                 cpos = _gettextposition();
  246.                 crow = cpos.row;
  247.                 ccol = cpos.col;
  248.                 #endif
  249.                 cnt = 0;
  250.                 return;
  251.             case 'u':
  252.                 #ifdef __TURBOC__
  253.                 gotoxy(ccol, crow);
  254.                 #else
  255.                 _settextposition(crow, ccol);
  256.                 #endif
  257.                 cnt = 0;
  258.                 return;
  259.             case 'K':
  260.                 clreol();
  261.                 cnt = 0;
  262.                 return;
  263.             case 'H':
  264.             case 'F':
  265.                 #ifdef __TURBOC__
  266.                 gotoxy(1, 1);
  267.                 #else
  268.                 _settextposition(1, 1);
  269.                 #endif
  270.                 cnt = 0;
  271.                 return;
  272.             case 'A':
  273.                 #ifdef __TURBOC__
  274.                 gotoxy(wherex(), wherey() - 1);
  275.                 #else
  276.                 cpos = _gettextposition();
  277.                 _settextposition(cpos.row - 1, cpos.col);
  278.                 #endif
  279.                 cnt = 0;
  280.                 return;
  281.             case 'B':
  282.                 #ifdef __TURBOC__
  283.                 gotoxy(wherex(), wherey() + 1);
  284.                 #else
  285.                 cpos = _gettextposition();
  286.                 _settextposition(cpos.row + 1, cpos.col);
  287.                 #endif
  288.                 cnt = 0;
  289.                 return;
  290.             case 'C':
  291.                 #ifdef __TURBOC__
  292.                 gotoxy(wherex() + 1, wherey());
  293.                 #else
  294.                 cpos = _gettextposition();
  295.                 _settextposition(cpos.row, cpos.col + 1);
  296.                 #endif
  297.                 cnt = 0;
  298.                 return;
  299.             case 'D':
  300.                 #ifdef __TURBOC__
  301.                 gotoxy(wherex() - 1, wherey());
  302.                 #else
  303.                 cpos = _gettextposition();
  304.                 _settextposition(cpos.row, cpos.col - 1);
  305.                 #endif
  306.                 cnt = 0;
  307.                 return;
  308.             default:
  309.                 if (bindigit(c)) {
  310.                     buffer[cnt] = (unsigned char)c;
  311.                     cnt++;
  312.                     return;
  313.                 }
  314.                 cnt = 0;
  315.                 return;
  316.         }
  317.     }
  318.     if (bindigit(c) || c == ';') {
  319.         buffer[cnt] = (unsigned char)c;
  320.         cnt++;
  321.         if (cnt > 256)
  322.             cnt = 0;
  323.         return;
  324.     }
  325.     bufptr = buffer + 2;
  326.     buffer[cnt] = (unsigned char)c;
  327.     switch (c) {
  328.         case 'H':
  329.         case 'F':
  330.         case 'h':
  331.         case 'f':
  332.             bufptr = ascbin(bufptr, &row);
  333.             if (*bufptr != ';') {
  334.                 #ifdef __TURBOC__
  335.                 gotoxy(1, row);
  336.                 #else
  337.                 _settextposition(row, 1);
  338.                 #endif
  339.                 cnt = 0;
  340.                 return;
  341.             }
  342.             bufptr++;
  343.             if (!bindigit(*bufptr)) {
  344.                 cnt = 0;
  345.                 return;
  346.             }
  347.             ascbin(bufptr, &col);
  348.             #ifdef __TURBOC__
  349.             gotoxy(col, row);
  350.             #else
  351.             _settextposition(row, col);
  352.             #endif
  353.             cnt = 0;
  354.             return;
  355.         case 'A':
  356.             ascbin(bufptr, &n);
  357.             #ifdef __TURBOC__
  358.             gotoxy(wherex(), wherey() - n);
  359.             #else
  360.             cpos = _gettextposition();
  361.             _settextposition(cpos.row - n, cpos.col);
  362.             #endif
  363.             cnt = 0;
  364.             return;
  365.         case 'B':
  366.             ascbin(bufptr, &n);
  367.             #ifdef __TURBOC__
  368.             gotoxy(wherex(), wherey() + n);
  369.             #else
  370.             cpos = _gettextposition();
  371.             _settextposition(cpos.row + n, cpos.col);
  372.             #endif
  373.             cnt = 0;
  374.             return;
  375.         case 'C':
  376.             ascbin(bufptr, &n);
  377.             #ifdef __TURBOC__
  378.             gotoxy(wherex() + n, wherey());
  379.             #else
  380.             cpos = _gettextposition();
  381.             _settextposition(cpos.row, cpos.col + n);
  382.             #endif
  383.             cnt = 0;
  384.             return;
  385.         case 'D':
  386.             ascbin(bufptr, &n);
  387.             #ifdef __TURBOC__
  388.             gotoxy(wherex() - n, wherey());
  389.             #else
  390.             cpos = _gettextposition();
  391.             _settextposition(cpos.row, cpos.col - n);
  392.             #endif
  393.             cnt = 0;
  394.             return;
  395.         case 'n':
  396.             ascbin(bufptr, &n);
  397.             if (n == 6 && ansi_dsr_flag)
  398.                 (*ansi_dsr)('\x1b');
  399.                 (*ansi_dsr)('[');
  400.                 (*ansi_dsr)('u');
  401.             cnt = 0;
  402.             return;
  403.         case 'J':
  404.             ascbin(bufptr, &n);
  405.             if (n == 2)
  406.                 #ifdef __TURBOC__
  407.                 clrscr();
  408.                 #else
  409.                 _clearscreen(_GWINDOW);
  410.                 #endif
  411.             cnt = 0;
  412.             return;
  413.         case 'm':
  414.             while (TRUE) {
  415.                 #ifdef __TURBOC__
  416.                 gettextinfo(&tinfo);
  417.                 #endif
  418.                 bufptr = ascbin(bufptr, &n);
  419.                 switch (n) {
  420.                     case 0:
  421.                         #ifdef __TURBOC__
  422.                         textattr(7);
  423.                         #else
  424.                         _setbkcolor(0L);
  425.                         _settextcolor(7);
  426.                         #endif
  427.                         break;
  428.                     case 1:
  429.                         #ifdef __TURBOC__
  430.                         textattr(tinfo.attribute | 0x08);
  431.                         #else
  432.                         _settextcolor(_gettextcolor() | 0x08);
  433.                         #endif
  434.                         break;
  435.                     case 5:
  436.                         #ifdef __TURBOC__
  437.                         textattr(tinfo.attribute | 0x80);
  438.                         #else
  439.                         _settextcolor(_gettextcolor() | 0x10);
  440.                         #endif
  441.                         break;
  442.                     case 30:
  443.                         #ifdef __TURBOC__
  444.                         textattr(tinfo.attribute & 0xf8);
  445.                         #else
  446.                         _settextcolor(_gettextcolor() & 0xf8);
  447.                         #endif
  448.                         break;
  449.                     case 31:
  450.                         #ifdef __TURBOC__
  451.                         textattr(tinfo.attribute & 0xf8 | 4);
  452.                         #else
  453.                         _settextcolor(_gettextcolor() & 0xf8 | 4);
  454.                         #endif
  455.                         break;
  456.                     case 32:
  457.                         #ifdef __TURBOC__
  458.                         textattr(tinfo.attribute & 0xf8 | 2);
  459.                         #else
  460.                         _settextcolor(_gettextcolor() & 0xf8 | 2);
  461.                         #endif
  462.                         break;
  463.                     case 33:
  464.                         #ifdef __TURBOC__
  465.                         textattr(tinfo.attribute & 0xf8 | 6);
  466.                         #else
  467.                         _settextcolor(_gettextcolor() & 0xf8 | 6);
  468.                         #endif
  469.                         break;
  470.                     case 34:
  471.                         #ifdef __TURBOC__
  472.                         textattr(tinfo.attribute & 0xf8 | 1);
  473.                         #else
  474.                         _settextcolor(_gettextcolor() & 0xf8 | 1);
  475.                         #endif
  476.                         break;
  477.                     case 35:
  478.                         #ifdef __TURBOC__
  479.                         textattr(tinfo.attribute & 0xf8 | 5);
  480.                         #else
  481.                         _settextcolor(_gettextcolor() & 0xf8 | 5);
  482.                         #endif
  483.                         break;
  484.                     case 36:
  485.                         #ifdef __TURBOC__
  486.                         textattr(tinfo.attribute & 0xf8 | 3);
  487.                         #else
  488.                         _settextcolor(_gettextcolor() & 0xf8 | 3);
  489.                         #endif
  490.                         break;
  491.                     case 37:
  492.                         #ifdef __TURBOC__
  493.                         textattr(tinfo.attribute & 0xf8 | 7);
  494.                         #else
  495.                         _settextcolor(_gettextcolor() & 0xf8 | 7);
  496.                         #endif
  497.                         break;
  498.                     case 40:
  499.                         #ifdef __TURBOC__
  500.                         textattr(tinfo.attribute & 0x8f);
  501.                         #else
  502.                         _setbkcolor(0L);
  503.                         #endif
  504.                         break;
  505.                     case 41:
  506.                         #ifdef __TURBOC__
  507.                         textattr(tinfo.attribute & 0x8f | 0x40);
  508.                         #else
  509.                         _setbkcolor(4L);
  510.                         #endif
  511.                         break;
  512.                     case 42:
  513.                         #ifdef __TURBOC__
  514.                         textattr(tinfo.attribute & 0x8f | 0x20);
  515.                         #else
  516.                         _setbkcolor(2L);
  517.                         #endif
  518.                         break;
  519.                     case 43:
  520.                         #ifdef __TURBOC__
  521.                         textattr(tinfo.attribute & 0x8f | 0x60);
  522.                         #else
  523.                         _setbkcolor(6L);
  524.                         #endif
  525.                         break;
  526.                     case 44:
  527.                         #ifdef __TURBOC__
  528.                         textattr(tinfo.attribute & 0x8f | 0x10);
  529.                         #else
  530.                         _setbkcolor(1L);
  531.                         #endif
  532.                         break;
  533.                     case 45:
  534.                         #ifdef __TURBOC__
  535.                         textattr(tinfo.attribute & 0x8f | 0x50);
  536.                         #else
  537.                         _setbkcolor(5L);
  538.                         #endif
  539.                         break;
  540.                     case 46:
  541.                         #ifdef __TURBOC__
  542.                         textattr(tinfo.attribute & 0x8f | 0x30);
  543.                         #else
  544.                         _setbkcolor(3L);
  545.                         #endif
  546.                         break;
  547.                     case 47:
  548.                         #ifdef __TURBOC__
  549.                         textattr(tinfo.attribute & 0x8f | 0x70);
  550.                         #else
  551.                         _setbkcolor(7L);
  552.                         #endif
  553.                         break;
  554.                 }
  555.                 if (*bufptr == ';') {
  556.                     bufptr++;
  557.                     continue;
  558.                 }
  559.                 cnt = 0;
  560.                 return;
  561.             }
  562.         default:
  563.             cnt = 0;
  564.             return;
  565.     }
  566. }
  567.  
  568. void ansistring(char *s)
  569. {
  570.     while (*s)
  571.         ansiout(*s++);
  572. }
  573.  
  574. int ansiprintf(char *f, ...)
  575. {
  576.     int l;
  577.     va_list m;
  578.     char *b, *s;
  579.  
  580.     if ((b = (char *)malloc(1024)) == NULL)
  581.         return -1;
  582.     va_start(m, f);
  583.     l = vsprintf(b, f, m);
  584.     s = b;
  585.     while (*s) {
  586.         ansiout(*s);
  587.         s++;
  588.     }
  589.     va_end(m);
  590.     free(b);
  591.     return l;
  592. }
  593.