home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 376_01 / os2tool.000 / COLOR.C < prev    next >
C/C++ Source or Header  |  1992-08-08  |  7KB  |  259 lines

  1. /*
  2. * COLOR.C - Sets the clors of the screen using VIO-calls or the
  3. *        ANSI escape sequences.
  4. *
  5. * PROGRAMMER:        Martti Ylikoski
  6. * CREATED:        27.11.1990
  7. */
  8. static char *VERSION = "Version 1.0, Copyright(c) Martti Ylikoski, 1990,1991" ;
  9. /*
  10. */
  11. //#define TOOLCOLOR /* COLOR defined in os2def.h! */
  12. //#define NORMAL
  13. //#define CLEAR
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <ctype.h>
  18. #include <param.h>
  19. #include <paramstd.h>
  20. #define INCL_DOSPROCESS
  21. #define INCL_VIO
  22. #include <os2.h>
  23. #include "vioclr.h"
  24.  
  25. static char *progname ;
  26.  
  27. extern unsigned long pflags ;
  28.  
  29. ParamEntry pentry[12] = {
  30.      "P", &ParamSetPause, 0,
  31.      "F", &ParamSetFold, 0,
  32.      "V", &ParamSetVerbose, 0,
  33.      "R", &ParamSetReport, 0,
  34.      "S", &ParamSetSubDirs, 0,
  35.      "?", &ParamSetHelp, 1,
  36.      "H", &ParamSetHelp, 1,
  37.      "NOD", &ParamSetNoDefault, 0,
  38.      "TEST", &ParamSetTest, 0,
  39.      "Y", &ParamSetYes, 0,
  40.      "N", &ParamSetTest, 0,
  41.      "\0", NULL, 0
  42. } ;
  43.  
  44. ParamBlock params = {
  45.     "/-",   IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
  46.     pentry
  47. } ;
  48.  
  49.  
  50. /* prototypes */
  51.  
  52. int ANSIColor(char *foregc, char *backgc) ;
  53. int VioColor(char *foregc, char *backgc) ;
  54.  
  55. int main(int argc, char *argv[])
  56. {
  57. #ifdef NORMAL
  58. char *bgcol, *fgcol ;
  59. #endif
  60. #ifdef CLEAR
  61. char c ;
  62. VIOMODEINFO viomi ;
  63. int ret ;
  64. #endif
  65.  
  66.    progname = argv[0] ;
  67.  
  68.    ParamHandle(¶ms, &argc, argv) ;
  69. #ifdef CLEAR
  70.    if (( argc != 1 && argc != 4) || pflags & PA_HELP)
  71.    {
  72.       printf("usage: %s [foreground-color ON background-color]\n", progname) ;
  73. #endif
  74. #ifdef TOOLCOLOR
  75.    if (argc != 4 || pflags & PA_HELP)
  76.    {
  77.       printf("usage: %s foreground-color ON background-color\n", progname) ;
  78. #endif
  79. #ifdef NORMAL
  80.    if (argc > 1 || pflags & PA_HELP)
  81.    {
  82.       printf("usage: %s \n", progname) ;
  83.       printf("Sets the default colors (LIGHT_GREY ON BLACK).\n") ;
  84.       printf("Colors can also be given in BGCOLOR\n") ;
  85.       printf("and FGCOLOR environment variables.\n") ;
  86. #endif
  87.       printf("Available colors:\n") ;
  88.       printf("       DARK_BLUE     LIGT_BLUE       BLUE\n") ;
  89.       printf("       DARK_RED      LIGHT_BLUE      RED\n") ;
  90.       printf("       DARK_GREEN    LIGHT_GREEN     GREEN\n") ;
  91.       printf("       DARK_YELLOW   LIGHT_YELLOW    YELLOW\n") ;
  92.       printf("       DARK_MAGENTA  LIGHT_MAGENTA   MAGENTA\n") ;
  93.       printf("       DARK_CYAN     LIGHT_CYAN      CYAN\n") ;
  94.       printf("       BLACK         WHITE           DARK_GREY\n") ;
  95.       printf("       LIGHT_GREY    DEFAULT\n") ;
  96.       printf(" OR an integer specifying the corresponding attribute\n" ) ;
  97.       return( 1 ) ;
  98.    }
  99. #ifdef NORMAL
  100.    bgcol = getenv("BGCOLOR") ;
  101.    fgcol = getenv("FGCOLOR") ;
  102.    if (bgcol == NULL)
  103.       bgcol = "BLACK";
  104.    if (fgcol == NULL)
  105.       fgcol = "LIGHT_GREY";
  106. #endif
  107. #ifdef CLEAR
  108.    if (argc != 1)
  109.       if ( VioColor(argv[1], argv[3])!= 0)
  110. #endif
  111. #ifdef TOOLCOLOR
  112.    if ( VioColor(argv[1], argv[3])!= 0)
  113. #endif
  114. #ifdef NORMAL
  115.    if ( VioColor(fgcol, bgcol)!= 0)
  116. #endif
  117. #if defined(TOOLCOLOR) || defined (CLEAR)
  118.       if ( ANSIColor(argv[1], argv[3]) != 0)
  119. #endif
  120. #ifdef NORMAL
  121.       if ( ANSIColor(fgcol, bgcol) != 0)
  122. #endif
  123.       {
  124.      printf("%s: unable to set the colors\n", progname) ;
  125.      return( 1 ) ;
  126.       }
  127. #ifdef CLEAR
  128.      if ( VioGetMode(&viomi, 0) != 0)
  129.      {
  130.     printf("Error in VioGetMode\n") ;
  131.     return( 1 ) ;
  132.      }
  133.      c = ' ' ;
  134.      VioWrtNChar(&c, viomi.row* viomi.col, 0, 0, 0) ;
  135.      VioSetCurPos(0,0,0) ;
  136. #endif
  137.    return( 0 ) ;
  138. }
  139.  
  140. /* ANSIColor - set the colors of the dislpay using ANSI escape sequences */
  141.  
  142. int ANSIColor(char *foregc, char *backgc)
  143. {
  144. int i, fint, bint ;
  145. USHORT fAnsi, ret ;
  146.    fint = 0 ;
  147.    bint = 0 ;
  148.  
  149.    for (i = 0 ; i < (int) (sizeof(ansi_colors)/sizeof(ANSI_TABLE_ENTRY)) ; i++)
  150.    {
  151.       if (strcmpi(ansi_colors[i].color, foregc) == 0)
  152.      fint = ansi_colors[i].fint ;
  153.       if (strcmpi(ansi_colors[i].color, backgc) == 0)
  154.      bint = ansi_colors[i].bint ;
  155.    }
  156.  
  157.    if (isdigit(foregc[0]) != 0)
  158.       fint = atoi(foregc) ;
  159.  
  160.    if (isdigit(backgc[0]) != 0)
  161.       bint = atoi(backgc) ;
  162.  
  163.    if (fint == 0 || bint == 0 )
  164.    {
  165.       fprintf(stderr,"%s: foreground or backgroung color was not valid\n", progname) ;
  166.       fprintf(stderr,"Run command without arguments to see valid parameters\n") ;
  167.       return( 1 ) ;
  168.    }
  169. /*
  170.    if (( ret = VioGetAnsi(&fAnsi, 0)) != 0)
  171.    {
  172.       printf("%c[%im", ESC, fint) ;
  173.       printf("%c[%im\n", ESC, bint) ;
  174.       fprintf(stderr, "%s: error in VioGetAnsi...\nExiting...\n", progname) ;
  175.       return( 1 ) ;
  176.    }
  177.  
  178.    if (fAnsi == ANSI_OFF)
  179.    {
  180.       VioSetCurPos(23,1,0) ;
  181.       if (( ret = VioSetAnsi(ANSI_ON, 0 )) != 0)
  182.       {
  183.      fprintf(stderr, "%s: error in VioSetAnsi...\nExiting...\n", progname) ;
  184.      return( 1 ) ;
  185.        }
  186.  
  187.    }
  188.  */
  189.       printf("%c[%im", ESC, fint) ;
  190.       printf("%c[%im\n", ESC, bint) ;    /* the newline is important here.
  191.                        It forces the characters out */
  192.       fflush(stdout) ;
  193.    printf("\n") ;
  194.    fclose(stdout) ;
  195.  
  196. //   if (fAnsi == ANSI_OFF)        /* change ANSI flag to original value */
  197. //   {
  198. //    DosSleep(250L) ;
  199. //    VioSetAnsi(ANSI_OFF, 0 ) ;
  200. //   }
  201.  
  202.    return( 0 ) ;
  203. }
  204.  
  205. /* VioColor - Set the colors of the display using Vio-calls */
  206. int VioColor(char *foregc, char *backgc)
  207. {
  208. int i, fint, bint ;
  209. USHORT ret ;
  210. BYTE abState[38] ;
  211. PVIOPALSTATE pviopalstate ;
  212.    fint = -1 ;
  213.    bint = -1 ;
  214.  
  215.    pviopalstate = (PVIOPALSTATE) abState ;
  216.  
  217.    for (i = 0 ; i < (int) (sizeof(colors)/sizeof(TABLE_ENTRY)) ; i++)
  218.    {
  219.       if (strcmpi(colors[i].color, foregc) == 0)
  220.      fint = colors[i].cint ;
  221.       if (strcmpi("DEFAULT", foregc) == 0)
  222.      fint = LIGHT_GREY ;
  223.  
  224.       if (strcmpi(colors[i].color, backgc) == 0)
  225.      bint = colors[i].cint ;
  226.       if (strcmpi("DEFAULT", backgc) == 0)
  227.      bint = BLACK ;
  228.    }
  229.  
  230.    if (isdigit(foregc[0]) != 0)
  231.       fint = atoi(foregc) ;
  232.  
  233.    if (isdigit(backgc[0]) != 0)
  234.       bint = atoi(backgc) ;
  235.  
  236.    if (fint == -1 || bint == -1 )
  237.    {
  238.       fprintf(stderr,"%s: foreground or backgroung color was not valid\n", progname) ;
  239.       fprintf(stderr,"Run command without arguments to see valid parameters\n") ;
  240.       return( 1 ) ;
  241.    }
  242.  
  243.    pviopalstate->cb = sizeof(abState) ;
  244.    pviopalstate->type = 0 ;
  245.    pviopalstate->iFirst = 0 ;
  246.    if (( ret = VioGetState(pviopalstate, 0)) != 0)
  247.       return( ret ) ;
  248.  
  249.    pviopalstate->acolor[0] = bint ;
  250.    pviopalstate->acolor[7] = fint ;
  251.  
  252.    if ((ret = VioSetState(pviopalstate, 0)) != 0)
  253.    {
  254.       printf("Error in VioSetState\nExiting...\n") ;
  255.       return( 1 ) ;
  256.    }
  257.    return( 0 ) ;
  258. }
  259.