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 >
Wrap
C/C++ Source or Header
|
1992-08-08
|
7KB
|
259 lines
/*
* COLOR.C - Sets the clors of the screen using VIO-calls or the
* ANSI escape sequences.
*
* PROGRAMMER: Martti Ylikoski
* CREATED: 27.11.1990
*/
static char *VERSION = "Version 1.0, Copyright(c) Martti Ylikoski, 1990,1991" ;
/*
*/
//#define TOOLCOLOR /* COLOR defined in os2def.h! */
//#define NORMAL
//#define CLEAR
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <param.h>
#include <paramstd.h>
#define INCL_DOSPROCESS
#define INCL_VIO
#include <os2.h>
#include "vioclr.h"
static char *progname ;
extern unsigned long pflags ;
ParamEntry pentry[12] = {
"P", &ParamSetPause, 0,
"F", &ParamSetFold, 0,
"V", &ParamSetVerbose, 0,
"R", &ParamSetReport, 0,
"S", &ParamSetSubDirs, 0,
"?", &ParamSetHelp, 1,
"H", &ParamSetHelp, 1,
"NOD", &ParamSetNoDefault, 0,
"TEST", &ParamSetTest, 0,
"Y", &ParamSetYes, 0,
"N", &ParamSetTest, 0,
"\0", NULL, 0
} ;
ParamBlock params = {
"/-", IGNORECASE | NOPRIORITY | NOTRIGGERSALLOWED ,
pentry
} ;
/* prototypes */
int ANSIColor(char *foregc, char *backgc) ;
int VioColor(char *foregc, char *backgc) ;
int main(int argc, char *argv[])
{
#ifdef NORMAL
char *bgcol, *fgcol ;
#endif
#ifdef CLEAR
char c ;
VIOMODEINFO viomi ;
int ret ;
#endif
progname = argv[0] ;
ParamHandle(¶ms, &argc, argv) ;
#ifdef CLEAR
if (( argc != 1 && argc != 4) || pflags & PA_HELP)
{
printf("usage: %s [foreground-color ON background-color]\n", progname) ;
#endif
#ifdef TOOLCOLOR
if (argc != 4 || pflags & PA_HELP)
{
printf("usage: %s foreground-color ON background-color\n", progname) ;
#endif
#ifdef NORMAL
if (argc > 1 || pflags & PA_HELP)
{
printf("usage: %s \n", progname) ;
printf("Sets the default colors (LIGHT_GREY ON BLACK).\n") ;
printf("Colors can also be given in BGCOLOR\n") ;
printf("and FGCOLOR environment variables.\n") ;
#endif
printf("Available colors:\n") ;
printf(" DARK_BLUE LIGT_BLUE BLUE\n") ;
printf(" DARK_RED LIGHT_BLUE RED\n") ;
printf(" DARK_GREEN LIGHT_GREEN GREEN\n") ;
printf(" DARK_YELLOW LIGHT_YELLOW YELLOW\n") ;
printf(" DARK_MAGENTA LIGHT_MAGENTA MAGENTA\n") ;
printf(" DARK_CYAN LIGHT_CYAN CYAN\n") ;
printf(" BLACK WHITE DARK_GREY\n") ;
printf(" LIGHT_GREY DEFAULT\n") ;
printf(" OR an integer specifying the corresponding attribute\n" ) ;
return( 1 ) ;
}
#ifdef NORMAL
bgcol = getenv("BGCOLOR") ;
fgcol = getenv("FGCOLOR") ;
if (bgcol == NULL)
bgcol = "BLACK";
if (fgcol == NULL)
fgcol = "LIGHT_GREY";
#endif
#ifdef CLEAR
if (argc != 1)
if ( VioColor(argv[1], argv[3])!= 0)
#endif
#ifdef TOOLCOLOR
if ( VioColor(argv[1], argv[3])!= 0)
#endif
#ifdef NORMAL
if ( VioColor(fgcol, bgcol)!= 0)
#endif
#if defined(TOOLCOLOR) || defined (CLEAR)
if ( ANSIColor(argv[1], argv[3]) != 0)
#endif
#ifdef NORMAL
if ( ANSIColor(fgcol, bgcol) != 0)
#endif
{
printf("%s: unable to set the colors\n", progname) ;
return( 1 ) ;
}
#ifdef CLEAR
if ( VioGetMode(&viomi, 0) != 0)
{
printf("Error in VioGetMode\n") ;
return( 1 ) ;
}
c = ' ' ;
VioWrtNChar(&c, viomi.row* viomi.col, 0, 0, 0) ;
VioSetCurPos(0,0,0) ;
#endif
return( 0 ) ;
}
/* ANSIColor - set the colors of the dislpay using ANSI escape sequences */
int ANSIColor(char *foregc, char *backgc)
{
int i, fint, bint ;
USHORT fAnsi, ret ;
fint = 0 ;
bint = 0 ;
for (i = 0 ; i < (int) (sizeof(ansi_colors)/sizeof(ANSI_TABLE_ENTRY)) ; i++)
{
if (strcmpi(ansi_colors[i].color, foregc) == 0)
fint = ansi_colors[i].fint ;
if (strcmpi(ansi_colors[i].color, backgc) == 0)
bint = ansi_colors[i].bint ;
}
if (isdigit(foregc[0]) != 0)
fint = atoi(foregc) ;
if (isdigit(backgc[0]) != 0)
bint = atoi(backgc) ;
if (fint == 0 || bint == 0 )
{
fprintf(stderr,"%s: foreground or backgroung color was not valid\n", progname) ;
fprintf(stderr,"Run command without arguments to see valid parameters\n") ;
return( 1 ) ;
}
/*
if (( ret = VioGetAnsi(&fAnsi, 0)) != 0)
{
printf("%c[%im", ESC, fint) ;
printf("%c[%im\n", ESC, bint) ;
fprintf(stderr, "%s: error in VioGetAnsi...\nExiting...\n", progname) ;
return( 1 ) ;
}
if (fAnsi == ANSI_OFF)
{
VioSetCurPos(23,1,0) ;
if (( ret = VioSetAnsi(ANSI_ON, 0 )) != 0)
{
fprintf(stderr, "%s: error in VioSetAnsi...\nExiting...\n", progname) ;
return( 1 ) ;
}
}
*/
printf("%c[%im", ESC, fint) ;
printf("%c[%im\n", ESC, bint) ; /* the newline is important here.
It forces the characters out */
fflush(stdout) ;
printf("\n") ;
fclose(stdout) ;
// if (fAnsi == ANSI_OFF) /* change ANSI flag to original value */
// {
// DosSleep(250L) ;
// VioSetAnsi(ANSI_OFF, 0 ) ;
// }
return( 0 ) ;
}
/* VioColor - Set the colors of the display using Vio-calls */
int VioColor(char *foregc, char *backgc)
{
int i, fint, bint ;
USHORT ret ;
BYTE abState[38] ;
PVIOPALSTATE pviopalstate ;
fint = -1 ;
bint = -1 ;
pviopalstate = (PVIOPALSTATE) abState ;
for (i = 0 ; i < (int) (sizeof(colors)/sizeof(TABLE_ENTRY)) ; i++)
{
if (strcmpi(colors[i].color, foregc) == 0)
fint = colors[i].cint ;
if (strcmpi("DEFAULT", foregc) == 0)
fint = LIGHT_GREY ;
if (strcmpi(colors[i].color, backgc) == 0)
bint = colors[i].cint ;
if (strcmpi("DEFAULT", backgc) == 0)
bint = BLACK ;
}
if (isdigit(foregc[0]) != 0)
fint = atoi(foregc) ;
if (isdigit(backgc[0]) != 0)
bint = atoi(backgc) ;
if (fint == -1 || bint == -1 )
{
fprintf(stderr,"%s: foreground or backgroung color was not valid\n", progname) ;
fprintf(stderr,"Run command without arguments to see valid parameters\n") ;
return( 1 ) ;
}
pviopalstate->cb = sizeof(abState) ;
pviopalstate->type = 0 ;
pviopalstate->iFirst = 0 ;
if (( ret = VioGetState(pviopalstate, 0)) != 0)
return( ret ) ;
pviopalstate->acolor[0] = bint ;
pviopalstate->acolor[7] = fint ;
if ((ret = VioSetState(pviopalstate, 0)) != 0)
{
printf("Error in VioSetState\nExiting...\n") ;
return( 1 ) ;
}
return( 0 ) ;
}