| 1 |
/* rdcolor.c: this program initializes color/printer variables using a |
| 2 |
* rdcolor.dat file if it exists. Otherwise, default values are used. |
| 3 |
* The <rdcolor.dat> file uses the following format: |
| 4 |
* color ...<CR> |
| 5 |
* where the ellipsis is the escape sequence to be used for the "color". |
| 6 |
* The following characters have special meaning when preceded by a |
| 7 |
* backslash: |
| 8 |
* \e => escape character |
| 9 |
* \f => formfeed character |
| 10 |
* \h => backspace character |
| 11 |
* \n => newline character |
| 12 |
* \t => tab character |
| 13 |
* \A - \Z=> Associated control characters |
| 14 |
*/ |
| 15 |
#include <stdio.h> |
| 16 |
#include <ctype.h> |
| 17 |
#include "xtdio.h" |
| 18 |
|
| 19 |
/* |
| 20 |
* #define DEBUG_TEST |
| 21 |
* #define DEBUG |
| 22 |
*/ |
| 23 |
|
| 24 |
/* ------------------------------------------------------------------------- |
| 25 |
* Definitions: |
| 26 |
*/ |
| 27 |
#define LINESIZE 80 |
| 28 |
#define TERMSIZE 20 |
| 29 |
|
| 30 |
/* ------------------------------------------------------------------------- |
| 31 |
* Global Data: |
| 32 |
*/ |
| 33 |
char *BLACK = NULL; |
| 34 |
char *RED = NULL; |
| 35 |
char *GREEN = NULL; |
| 36 |
char *YELLOW = NULL; |
| 37 |
char *BLUE = NULL; |
| 38 |
char *MAGENTA = NULL; |
| 39 |
char *CYAN = NULL; |
| 40 |
char *WHITE = NULL; |
| 41 |
|
| 42 |
/* since some systems just have to use BLACK, ..., WHITE for themselves... */ |
| 43 |
char *RD_BLACK = NULL; |
| 44 |
char *RD_RED = NULL; |
| 45 |
char *RD_GREEN = NULL; |
| 46 |
char *RD_YELLOW = NULL; |
| 47 |
char *RD_BLUE = NULL; |
| 48 |
char *RD_MAGENTA = NULL; |
| 49 |
char *RD_CYAN = NULL; |
| 50 |
char *RD_WHITE = NULL; |
| 51 |
|
| 52 |
char *UBLACK = NULL; |
| 53 |
char *URED = NULL; |
| 54 |
char *UGREEN = NULL; |
| 55 |
char *UYELLOW = NULL; |
| 56 |
char *UBLUE = NULL; |
| 57 |
char *UMAGENTA = NULL; |
| 58 |
char *UCYAN = NULL; |
| 59 |
char *UWHITE = NULL; |
| 60 |
|
| 61 |
char *RVBLACK = NULL; |
| 62 |
char *RVRED = NULL; |
| 63 |
char *RVGREEN = NULL; |
| 64 |
char *RVYELLOW = NULL; |
| 65 |
char *RVBLUE = NULL; |
| 66 |
char *RVMAGENTA = NULL; |
| 67 |
char *RVCYAN = NULL; |
| 68 |
char *RVWHITE = NULL; |
| 69 |
|
| 70 |
char *CLEAR = NULL; |
| 71 |
char *BOLD = NULL; |
| 72 |
char *NRML = NULL; |
| 73 |
|
| 74 |
char *PYELLOW = NULL; |
| 75 |
char *PRED = NULL; |
| 76 |
char *PCYAN = NULL; |
| 77 |
char *PBLACK = NULL; |
| 78 |
|
| 79 |
/* ------------------------------------------------------------------------- */ |
| 80 |
|
| 81 |
static struct colorstr{ /* color data structure */ |
| 82 |
char **color; /* points to a color variable */ |
| 83 |
char *name; /* gives a name to a color */ |
| 84 |
char *defalt; /* default color string: nothing */ |
| 85 |
} colors[]={ |
| 86 |
{&BLACK, "BLACK", "\0"}, |
| 87 |
{&RED, "RED", "\0"}, |
| 88 |
{&GREEN, "GREEN", "\0"}, |
| 89 |
{&YELLOW, "YELLOW", "\0"}, |
| 90 |
{&BLUE, "BLUE", "\0"}, |
| 91 |
{&MAGENTA, "MAGENTA", "\0"}, |
| 92 |
{&CYAN, "CYAN", "\0"}, |
| 93 |
{&WHITE, "WHITE", "\0"}, |
| 94 |
{&UBLACK, "UBLACK", "\0"}, |
| 95 |
{&URED, "URED", "\0"}, |
| 96 |
{&UGREEN, "UGREEN", "\0"}, |
| 97 |
{&UYELLOW, "UYELLOW", "\0"}, |
| 98 |
{&UBLUE, "UBLUE", "\0"}, |
| 99 |
{&UMAGENTA, "UMAGENTA", "\0"}, |
| 100 |
{&UCYAN, "UCYAN", "\0"}, |
| 101 |
{&UWHITE, "UWHITE", "\0"}, |
| 102 |
{&RVBLACK, "RVBLACK", "\0"}, |
| 103 |
{&RVRED, "RVRED", "\0"}, |
| 104 |
{&RVGREEN, "RVGREEN", "\0"}, |
| 105 |
{&RVYELLOW, "RVYELLOW", "\0"}, |
| 106 |
{&RVBLUE, "RVBLUE", "\0"}, |
| 107 |
{&RVMAGENTA, "RVMAGENTA","\0"}, |
| 108 |
{&RVCYAN, "RVCYAN", "\0"}, |
| 109 |
{&RVWHITE, "RVWHITE", "\0"}, |
| 110 |
{&BOLD, "BOLD", "\0"}, |
| 111 |
{&NRML, "NRML", "\0"}, |
| 112 |
{&PYELLOW, "PYELLOW", "\0"}, |
| 113 |
{&PRED, "PRED", "\0"}, |
| 114 |
{&PCYAN, "PCYAN", "\0"}, |
| 115 |
{&PBLACK, "PBLACK", "\0"}, |
| 116 |
{&CLEAR, "CLEAR", "\0"} |
| 117 |
}; |
| 118 |
static int ncolor = sizeof(colors)/sizeof(struct colorstr); |
| 119 |
char *colorfile[] = {"rdcolor.dat","color.dat"}; |
| 120 |
static int ncolorfile = sizeof(colorfile)/sizeof(char *); |
| 121 |
static int rdcolor_done= 0; /* has rdcolor been called? */ |
| 122 |
|
| 123 |
/* ------------------------------------------------------------------------- */ |
| 124 |
|
| 125 |
/* rdcolor: read the <rdcolor.dat> file */ |
| 126 |
#ifdef __PROTOTYPE__ |
| 127 |
void rdcolor(void) |
| 128 |
#else /* __PROTOTYPE__ */ |
| 129 |
void rdcolor() |
| 130 |
#endif /* __PROTOTYPE__ */ |
| 131 |
{ |
| 132 |
FILE *fp = NULL; |
| 133 |
char *cmd = NULL; |
| 134 |
char *new = NULL; |
| 135 |
char *lterm = NULL; |
| 136 |
int icolor; |
| 137 |
int foundterm; |
| 138 |
|
| 139 |
static char color[LINESIZE]; |
| 140 |
static char line[LINESIZE]; |
| 141 |
static char newdef[LINESIZE+LINESIZE]; |
| 142 |
static char *term=NULL; |
| 143 |
|
| 144 |
|
| 145 |
/* indicate that rdcolor() has been called */ |
| 146 |
rdcolor_done= 1; |
| 147 |
|
| 148 |
/* set up default colors */ |
| 149 |
for(icolor= 0; icolor < ncolor; ++icolor) *(colors[icolor].color)= colors[icolor].defalt; |
| 150 |
|
| 151 |
/* attempt to open <rdcolor.dat> file */ |
| 152 |
for(icolor= 0; icolor < ncolorfile; ++icolor) { |
| 153 |
fp= fopenv(colorfile[icolor],"r","RDCOLOR"); |
| 154 |
if(fp) break; |
| 155 |
} |
| 156 |
|
| 157 |
if(!fp) { /* use default values */ |
| 158 |
/* set up RD_... colors */ |
| 159 |
RD_BLACK = BLACK; |
| 160 |
RD_RED = RED; |
| 161 |
RD_GREEN = GREEN; |
| 162 |
RD_YELLOW = YELLOW; |
| 163 |
RD_BLUE = BLUE; |
| 164 |
RD_MAGENTA= MAGENTA; |
| 165 |
RD_CYAN = CYAN; |
| 166 |
RD_WHITE = WHITE; |
| 167 |
return; |
| 168 |
} |
| 169 |
|
| 170 |
/* attempt to locate correct terminal definition */ |
| 171 |
#ifdef unix |
| 172 |
term= getenv("TERM"); |
| 173 |
#endif |
| 174 |
#ifdef LATTICE |
| 175 |
term= "TI"; |
| 176 |
#endif |
| 177 |
#ifdef DESMET |
| 178 |
term= "TI"; |
| 179 |
#endif |
| 180 |
#ifdef AZTEC_C |
| 181 |
term= "Amiga" |
| 182 |
#endif |
| 183 |
#ifdef vms |
| 184 |
term= "vms"; |
| 185 |
#endif |
| 186 |
#ifdef MSDOS |
| 187 |
term= "ibmpc"; |
| 188 |
#endif |
| 189 |
|
| 190 |
if(term) { /* search for terminal entry in <rdcolor.dat> file */ |
| 191 |
foundterm= 0; |
| 192 |
while(fgets(line,LINESIZE,fp)) if(!isspace(line[0])) { |
| 193 |
srmtrblk(line); |
| 194 |
for(lterm= strtok(line,"|:"); lterm; lterm=strtok((char *) NULL,"|:")) { |
| 195 |
lterm= stpblk(lterm); |
| 196 |
srmtrblk(lterm); |
| 197 |
if(!strcmp(lterm,term)) { |
| 198 |
foundterm= 1; |
| 199 |
break; |
| 200 |
} |
| 201 |
} /* term|term|term|term: search */ |
| 202 |
if(foundterm) break; |
| 203 |
} |
| 204 |
|
| 205 |
if(!foundterm) { /* couldn't find terminal entry */ |
| 206 |
fclose(fp); |
| 207 |
return; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
while(fgets(line,LINESIZE,fp)) { /* read color & use cmd sequence */ |
| 212 |
|
| 213 |
/* skip blank lines */ |
| 214 |
for(cmd= line; *cmd; ++cmd) if(!isspace(*cmd)) break; |
| 215 |
if(!*cmd) continue; |
| 216 |
if(cmd == line) break; /* finished reading in terminal def'n */ |
| 217 |
|
| 218 |
/* if first non-blank character is a "#", skip it (comment) */ |
| 219 |
if(*cmd == '#') continue; |
| 220 |
|
| 221 |
/* get the color */ |
| 222 |
sscanf(cmd,"%s",color); |
| 223 |
|
| 224 |
/* skip to first white space character */ |
| 225 |
for(; *cmd && !isspace(*cmd); ++cmd); |
| 226 |
cmd[strlen(cmd) - 1]= '\0'; /* remove newline character */ |
| 227 |
|
| 228 |
/* skip over tabs following colorname */ |
| 229 |
while(*cmd == '\t') ++cmd; |
| 230 |
|
| 231 |
/* identify color (if known) */ |
| 232 |
for(icolor= 0; icolor < ncolor; ++icolor) |
| 233 |
if(!strcmp(colors[icolor].name,color)) break; |
| 234 |
if(icolor >= ncolor) { /* unknown color */ |
| 235 |
error(XTDIO_WARNING, "(rdcolor) ignoring unknown color <%s> in <%s>\n", |
| 236 |
color,colorfile); |
| 237 |
continue; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
/* copy cmd (new color) over to newdef (new definition), making |
| 242 |
* appropriate changes due to backslashes |
| 243 |
*/ |
| 244 |
for(new= newdef; *cmd; ++cmd,++new) { |
| 245 |
if(*cmd == '\\') { |
| 246 |
switch(*++cmd) { |
| 247 |
case 'e' : /* escape character */ |
| 248 |
*new= '\033'; |
| 249 |
break; |
| 250 |
case 'f' : /* formfeed character */ |
| 251 |
*new= '\f'; |
| 252 |
break; |
| 253 |
case 'h' : /* backspace character */ |
| 254 |
*new= (char) 8; |
| 255 |
break; |
| 256 |
case 'n' : /* newline character */ |
| 257 |
*new= '\n'; |
| 258 |
break; |
| 259 |
case 't' : /* tab character */ |
| 260 |
*new= '\t'; |
| 261 |
break; |
| 262 |
default: /* handles A-Z and other */ |
| 263 |
if('A' <= *cmd && *cmd <= 'Z') *new= *cmd - 64; |
| 264 |
else *new= *cmd; |
| 265 |
break; |
| 266 |
} |
| 267 |
} |
| 268 |
else *new= *cmd; |
| 269 |
} |
| 270 |
*new= '\0'; /* terminate new definition with null byte */ |
| 271 |
|
| 272 |
/* allocate memory and then set color to colorfile-defined value */ |
| 273 |
*colors[icolor].color= (void *) calloc((size_t) (strlen(newdef)+1),sizeof(char)); |
| 274 |
if(newdef[0]) strcpy(*colors[icolor].color,newdef); |
| 275 |
else *colors[icolor].color= "\0"; |
| 276 |
|
| 277 |
} |
| 278 |
|
| 279 |
/* close the colorfile */ |
| 280 |
fclose(fp); |
| 281 |
|
| 282 |
/* set up RD_... colors */ |
| 283 |
RD_BLACK = BLACK; |
| 284 |
RD_RED = RED; |
| 285 |
RD_GREEN = GREEN; |
| 286 |
RD_YELLOW = YELLOW; |
| 287 |
RD_BLUE = BLUE; |
| 288 |
RD_MAGENTA= MAGENTA; |
| 289 |
RD_CYAN = CYAN; |
| 290 |
RD_WHITE = WHITE; |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
/* ------------------------------------------------------------------------- */ |
| 295 |
|
| 296 |
/* rdcputs: read color's put-string function. Normally, rdcputs acts exactly |
| 297 |
* like fputs, except that "\255X#" strings are translated to appropriate |
| 298 |
* colors. |
| 299 |
*/ |
| 300 |
#ifdef __PROTOTYPE__ |
| 301 |
void rdcputs( |
| 302 |
char *s, |
| 303 |
FILE *fp) |
| 304 |
#else /* __PROTOTYPE__ */ |
| 305 |
void rdcputs(s,fp) |
| 306 |
char *s; |
| 307 |
FILE *fp; |
| 308 |
#endif /* __PROTOTYPE__ */ |
| 309 |
{ |
| 310 |
int icolor; |
| 311 |
|
| 312 |
if(*s == '\255' && *(s+1) == 'X') { /* translate */ |
| 313 |
if(rdcolor_done) { |
| 314 |
/* only try interpreting this if rdcolor() has been called */ |
| 315 |
sscanf(s+2,"%d",&icolor); |
| 316 |
fputs(*colors[icolor].color,fp); |
| 317 |
} |
| 318 |
} |
| 319 |
else fputs(s,fp); |
| 320 |
} |
| 321 |
|
| 322 |
/* ------------------------------------------------------------------------- */ |