"Fossies" - the Fresh Open Source Software Archive

Member "vfu-4.18/vslib/ansiterm.cpp" (23 Aug 2015, 4528 Bytes) of package /linux/privat/vfu-4.18.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ansiterm.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.16_vs_4.18.

    1 /****************************************************************************
    2  *
    3  * (c) Vladi Belperchinov-Shabanski "Cade" 1996-2015
    4  * http://cade.datamax.bg/  <cade@biscom.net> <cade@bis.bg> <cade@datamax.bg>
    5  *
    6  * SEE `README',`LICENSE' OR `COPYING' FILE FOR LICENSE AND OTHER DETAILS!
    7  *
    8  ****************************************************************************/
    9 
   10 #include <stdio.h>
   11 #include <stdlib.h>
   12 #include <string.h>
   13 #include "ansiterm.h"
   14 
   15   int Ansi_MAXX = 80;
   16   int Ansi_MAXY = 25;
   17   int Ansi_X    =  1;
   18   int Ansi_Y    =  1;
   19 
   20   int a_fg;
   21   int a_bg;
   22   int a_ta;
   23 
   24   int ansi_o_ta;
   25 
   26   int ANSI = 0;
   27 
   28   static int colortab( int color ) // convert normal colors to ANSI ones
   29   {
   30     switch(color)
   31       {
   32       case cBLACK   : return 0;
   33       case cBLUE    : return 4;
   34       case cGREEN   : return 2;
   35       case cCYAN    : return 6;
   36       case cRED     : return 1;
   37       case cMAGENTA : return 5;
   38       case cYELLOW  : return 3;
   39       case cWHITE   : return 7;
   40       }
   41     return 7;
   42   }
   43 
   44   int AnsiInit( int pANSI )
   45   {
   46     if ( pANSI != -1)
   47       ANSI = pANSI;
   48     else
   49       {
   50       ANSI = 0;
   51       char *buf = getenv( "TERM" );
   52       if ( buf && strcasecmp( buf, "ANSI"  ) == 0 ) ANSI = 1;
   53       if ( buf && strcasecmp( buf, "vt100" ) == 0 ) ANSI = 1;
   54       }
   55     if (getenv( "TERMX" )) Ansi_MAXX = atoi( getenv( "TERMX" ) );
   56     if (getenv( "TERMY" )) Ansi_MAXY = atoi( getenv( "TERMY" ) );
   57     if ( Ansi_MAXX == 0 ) Ansi_MAXX = 80;
   58     if ( Ansi_MAXY == 0 ) Ansi_MAXY = 25;
   59     Ansi_X = 1;
   60     Ansi_Y = 1;
   61     AnsiTA( cNORMAL );
   62 
   63     return 0;
   64   }
   65 
   66   void AnsiDone()
   67   {
   68     if (!ANSI) return;
   69     AnsiTA( cNORMAL );
   70   }
   71 
   72 
   73   void AnsiSuspend() // suspends console (before system() for example)
   74   {
   75     if (!ANSI) return;
   76     ansi_o_ta = a_ta;
   77     AnsiTA( cNORMAL );
   78   }
   79 
   80   void AnsiRestore() // restores console after suspend
   81   {
   82     if (!ANSI) return;
   83     AnsiTA( ansi_o_ta );
   84   }
   85 
   86   void AnsiCE( int attr ) // clear to end-of-line
   87   {
   88     if (!ANSI) return;
   89     if ( attr != -1 )
   90       {
   91       int save_ta = a_ta;
   92       AnsiTA( attr );
   93       printf( "\033[K" );
   94       AnsiTA( save_ta );
   95       }
   96     else
   97       {
   98       printf( "\033[K" );
   99       }
  100   }
  101 
  102   void AnsiCS( int attr ) // clear screen
  103   {
  104     if (!ANSI) return;
  105     if ( attr != -1 )
  106       {
  107       int save_ta = a_ta;
  108       AnsiTA( attr );
  109       printf( "\033[2J" );
  110       AnsiTA( save_ta );
  111       }
  112     else
  113       {
  114       printf( "\033[2J" );
  115       }
  116   }
  117 
  118 
  119   void AnsiOut( int x, int y, const char *s )
  120   {
  121     AnsiXY( x, y );
  122     AnsiPuts( s );
  123   }
  124 
  125   void AnsiOut( int x, int y, const char *s, int attr )
  126   {
  127     AnsiXY( x, y );
  128     AnsiPuts( s, attr );
  129   }
  130 
  131   void AnsiPuts( const char *s )
  132   {
  133     puts( s );
  134   }
  135 
  136   void AnsiPuts( const char *s, int attr )
  137   {
  138     int _ta = a_ta;
  139     AnsiTA( attr );
  140     puts( s );
  141     AnsiTA( _ta  );
  142   }
  143 
  144   void AnsiCHide() { return; } // cursor hide
  145   void AnsiCShow() { return; } // cursor show
  146 
  147   int AnsiMaxX() { return Ansi_MAXX; }
  148   int AnsiMaxY() { return Ansi_MAXY; }
  149   int AnsiX() { return -1; }
  150   int AnsiY() { return -1; }
  151 
  152   void AnsiFG( int color )
  153   {
  154     AnsiTA( CONCOLOR(color, a_bg) );
  155   }
  156 
  157   void AnsiBG( int color )
  158   {
  159     AnsiTA( CONCOLOR( a_fg, color ) );
  160   }
  161 
  162   void AnsiTA( int attr )
  163   {
  164     if (!ANSI) return;
  165     a_ta = attr;
  166     a_fg = COLORFG( a_ta );
  167     a_bg = COLORBG( a_ta );
  168 
  169     int _l = (a_bg>7)?5:0;    // blink
  170     int _h = (a_fg>7)?1:0;    // hi
  171     int _f = a_fg;
  172     int _b = a_bg;
  173     if ( _f > 7 ) _f -= 8;
  174     if ( _b > 7 ) _b -= 8;
  175     _f = colortab(_f)+30;  // fore
  176     _b = colortab(_b)+40;  // back
  177 
  178     // hi,blink,fg,bg
  179     printf( "\033[0;%s%s%d;%dm", _h?"1;":"", _l?"5;":"", _f, _b );
  180   }
  181 
  182   void AnsiXY( int x, int y ) // go to x,y
  183   {
  184     if (!ANSI) return;
  185     if ( x < 1 || y < 1 || x > Ansi_MAXX || y > Ansi_MAXY ) return;
  186     printf( "\033[%d;%dH", y, x );
  187   }
  188 
  189   int AnsiKbHit() { return 1; }
  190   int AnsiGetch() { return fgetc(stdin); }
  191   void AnsiBeep()  { printf( "\007" ); }
  192 
  193 
  194 #ifdef TEST3
  195 int main()
  196 {
  197   AnsiInit();
  198   AnsiCS();
  199 
  200   AnsiOut( 1, 1, "<-" );
  201   AnsiXY( 10, 10 );
  202   AnsiTA( 79 );
  203   AnsiPuts( "  ANSI terminal Test  " );
  204   AnsiXY( 1, 11 );
  205   int z;
  206   for ( z = 0; z < 256; z++ )
  207     {
  208     char tmp[16];
  209     sprintf( tmp, " %3d ", z );
  210     AnsiTA(z); AnsiPuts( tmp );
  211     }
  212   AnsiTA( cNORMAL );
  213 
  214   for ( z = 1; z <= AnsiMaxX(); z++ )
  215     AnsiOut( z, 1, "*", z );
  216   for ( z = 1; z <= AnsiMaxX(); z++ )
  217     AnsiOut( z, AnsiMaxY()-1, "*", z );
  218   int c = AnsiGetch();
  219   if (c) c = c; // :) test
  220 
  221 
  222   AnsiDone();
  223 }
  224 #endif
  225 
  226  // eof ansiterm.cpp