File: rdcolor.c

package info (click to toggle)
robotour 3.1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,596 kB
  • ctags: 2,972
  • sloc: cpp: 17,705; sh: 3,060; ansic: 2,778; makefile: 144
file content (322 lines) | stat: -rwxr-xr-x 8,083 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* rdcolor.c: this program initializes color/printer variables using a
 *   rdcolor.dat file if it exists.  Otherwise, default values are used.
 *   The <rdcolor.dat> file uses the following format:
 *	color ...<CR>
 *   where the ellipsis is the escape sequence to be used for the "color".
 *   The following characters have special meaning when preceded by a
 *   backslash:
 *	     \e     =>      escape character
 *	     \f     =>      formfeed character
 *	     \h     =>      backspace character
 *	     \n     =>      newline character
 *	     \t     =>      tab character
 *	     \A - \Z=>      Associated control characters
 */
#include <stdio.h>
#include <ctype.h>
#include "xtdio.h"

/*
 * #define DEBUG_TEST
 * #define DEBUG
 */

/* -------------------------------------------------------------------------
 * Definitions:
 */
#define LINESIZE 80
#define TERMSIZE 20

/* -------------------------------------------------------------------------
 * Global Data:
 */
char *BLACK   = NULL;
char *RED     = NULL;
char *GREEN   = NULL;
char *YELLOW  = NULL;
char *BLUE    = NULL;
char *MAGENTA = NULL;
char *CYAN    = NULL;
char *WHITE   = NULL;

/* since some systems just have to use BLACK, ..., WHITE for themselves... */
char *RD_BLACK   = NULL;
char *RD_RED     = NULL;
char *RD_GREEN   = NULL;
char *RD_YELLOW  = NULL;
char *RD_BLUE    = NULL;
char *RD_MAGENTA = NULL;
char *RD_CYAN    = NULL;
char *RD_WHITE   = NULL;

char *UBLACK     = NULL;
char *URED       = NULL;
char *UGREEN     = NULL;
char *UYELLOW    = NULL;
char *UBLUE      = NULL;
char *UMAGENTA   = NULL;
char *UCYAN      = NULL;
char *UWHITE     = NULL;

char *RVBLACK    = NULL;
char *RVRED      = NULL;
char *RVGREEN    = NULL;
char *RVYELLOW   = NULL;
char *RVBLUE     = NULL;
char *RVMAGENTA  = NULL;
char *RVCYAN     = NULL;
char *RVWHITE    = NULL;

char *CLEAR      = NULL;
char *BOLD       = NULL;
char *NRML       = NULL;

char *PYELLOW    = NULL;
char *PRED       = NULL;
char *PCYAN      = NULL;
char *PBLACK     = NULL;

/* ------------------------------------------------------------------------- */

static struct colorstr{ /* color data structure			*/
	char **color;	/* points to a color variable			*/
	char *name;		/* gives a name to a color				*/
	char *defalt;	/* default color string: nothing		*/
	} colors[]={
	  {&BLACK,		"BLACK",	"\0"},
	  {&RED,		"RED",		"\0"},
	  {&GREEN,		"GREEN",	"\0"},
	  {&YELLOW,		"YELLOW",	"\0"},
	  {&BLUE,		"BLUE",		"\0"},
	  {&MAGENTA,	"MAGENTA",	"\0"},
	  {&CYAN,		"CYAN",		"\0"},
	  {&WHITE,		"WHITE",	"\0"},
	  {&UBLACK,		"UBLACK",	"\0"},
	  {&URED,		"URED",		"\0"},
	  {&UGREEN,		"UGREEN",	"\0"},
	  {&UYELLOW,	"UYELLOW",	"\0"},
	  {&UBLUE,		"UBLUE",	"\0"},
	  {&UMAGENTA,	"UMAGENTA",	"\0"},
	  {&UCYAN,		"UCYAN",	"\0"},
	  {&UWHITE,		"UWHITE",	"\0"},
	  {&RVBLACK,	"RVBLACK",	"\0"},
	  {&RVRED,		"RVRED",	"\0"},
	  {&RVGREEN,	"RVGREEN",	"\0"},
	  {&RVYELLOW,	"RVYELLOW",	"\0"},
	  {&RVBLUE,		"RVBLUE",	"\0"},
	  {&RVMAGENTA,	"RVMAGENTA","\0"},
	  {&RVCYAN,		"RVCYAN",	"\0"},
	  {&RVWHITE,	"RVWHITE",	"\0"},
	  {&BOLD,		"BOLD",		"\0"},
	  {&NRML,		"NRML",		"\0"},
	  {&PYELLOW,	"PYELLOW",	"\0"},
	  {&PRED,		"PRED",		"\0"},
	  {&PCYAN,		"PCYAN",	"\0"},
	  {&PBLACK,		"PBLACK",	"\0"},
	  {&CLEAR,		"CLEAR",	"\0"}
	  };
static int ncolor      = sizeof(colors)/sizeof(struct colorstr);
char      *colorfile[] = {"rdcolor.dat","color.dat"};
static int ncolorfile  = sizeof(colorfile)/sizeof(char *);
static int rdcolor_done= 0;	/* has rdcolor been called? */

/* ------------------------------------------------------------------------- */

/* rdcolor: read the <rdcolor.dat> file */
#ifdef __PROTOTYPE__
void rdcolor(void)
#else	/* __PROTOTYPE__ */
void rdcolor()
#endif	/* __PROTOTYPE__ */
{
FILE *fp    = NULL;
char *cmd   = NULL;
char *new   = NULL;
char *lterm = NULL;
int   icolor;
int   foundterm;

static char  color[LINESIZE];
static char  line[LINESIZE];
static char  newdef[LINESIZE+LINESIZE];
static char *term=NULL;


/* indicate that rdcolor() has been called */
rdcolor_done= 1;

/* set up default colors */
for(icolor= 0; icolor < ncolor; ++icolor) *(colors[icolor].color)= colors[icolor].defalt;

/* attempt to open <rdcolor.dat> file */
for(icolor= 0; icolor < ncolorfile; ++icolor) {
	fp= fopenv(colorfile[icolor],"r","RDCOLOR");
	if(fp) break;
	}

if(!fp) { /* use default values */
	/* set up RD_... colors */
	RD_BLACK  = BLACK;
	RD_RED    = RED;
	RD_GREEN  = GREEN;
	RD_YELLOW = YELLOW;
	RD_BLUE   = BLUE;
	RD_MAGENTA= MAGENTA;
	RD_CYAN   = CYAN;
	RD_WHITE  = WHITE;
	return;
	}

/* attempt to locate correct terminal definition */
#ifdef unix
term= getenv("TERM");
#endif
#ifdef LATTICE
term= "TI";
#endif
#ifdef DESMET
term= "TI";
#endif
#ifdef AZTEC_C
term= "Amiga"
#endif
#ifdef vms
term= "vms";
#endif
#ifdef MSDOS
term= "ibmpc";
#endif

if(term) {  /* search for terminal entry in <rdcolor.dat> file */
	foundterm= 0;
	while(fgets(line,LINESIZE,fp)) if(!isspace(line[0])) {
		srmtrblk(line);
		for(lterm= strtok(line,"|:"); lterm; lterm=strtok((char *) NULL,"|:")) {
			lterm= stpblk(lterm);
			srmtrblk(lterm);
			if(!strcmp(lterm,term)) {
				foundterm= 1;
				break;
				}
			}	/* term|term|term|term: search */
		if(foundterm) break;
		}

	if(!foundterm) { /* couldn't find terminal entry */
		fclose(fp);
		return;
		}
	}

while(fgets(line,LINESIZE,fp)) { /* read color & use cmd sequence */

	/* skip blank lines */
	for(cmd= line; *cmd; ++cmd) if(!isspace(*cmd)) break;
	if(!*cmd) continue;
	if(cmd == line) break; /* finished reading in terminal def'n */

	/* if first non-blank character is a "#", skip it (comment) */
	if(*cmd == '#') continue;

	/* get the color */
	sscanf(cmd,"%s",color);

	/* skip to first white space character */
	for(; *cmd && !isspace(*cmd); ++cmd);
	cmd[strlen(cmd) - 1]= '\0';	/* remove newline character */

	/* skip over tabs following colorname */
	while(*cmd == '\t') ++cmd;

	/* identify color (if known) */
	for(icolor= 0; icolor < ncolor; ++icolor)
	  if(!strcmp(colors[icolor].name,color)) break;
	if(icolor >= ncolor) { /* unknown color */
		error(XTDIO_WARNING, "(rdcolor) ignoring unknown color <%s> in <%s>\n",
		  color,colorfile);
		continue;
		}


	/* copy cmd (new color) over to newdef (new definition), making
	 *  appropriate changes due to backslashes
	 */
	for(new= newdef; *cmd; ++cmd,++new) {
		if(*cmd == '\\') {
			switch(*++cmd) {
			case 'e' : /* escape character		*/
				*new= '\033';
				break;
			case 'f' : /* formfeed character	*/
				*new= '\f';
				break;
			case 'h' : /* backspace character	*/
				*new= (char) 8;
				break;
			case 'n' : /* newline character		*/
				*new= '\n';
				break;
			case 't' : /* tab character		*/
				*new= '\t';
				break;
			default:   /* handles A-Z and other	*/
				if('A' <= *cmd && *cmd <= 'Z') *new= *cmd - 64;
				else *new= *cmd;
				break;
				}
			}
		else *new= *cmd;
		}
	*new= '\0'; /* terminate new definition with null byte */

	/* allocate memory and then set color to colorfile-defined value */
	*colors[icolor].color= calloc((unsigned) strlen(newdef)+1,sizeof(char));
	if(newdef[0]) strcpy(*colors[icolor].color,newdef);
	else *colors[icolor].color= "\0";

	}

/* close the colorfile */
fclose(fp);

/* set up RD_... colors */
RD_BLACK  = BLACK;
RD_RED    = RED;
RD_GREEN  = GREEN;
RD_YELLOW = YELLOW;
RD_BLUE   = BLUE;
RD_MAGENTA= MAGENTA;
RD_CYAN   = CYAN;
RD_WHITE  = WHITE;

}

/* ------------------------------------------------------------------------- */

/* rdcputs: read color's put-string function.  Normally, rdcputs acts exactly
 *  like fputs, except that "\255X#" strings are translated to appropriate
 *  colors.
 */
#ifdef __PROTOTYPE__
void rdcputs(
  char *s,
  FILE *fp)
#else	/* __PROTOTYPE__ */
void rdcputs(s,fp)
char *s;
FILE *fp;
#endif	/* __PROTOTYPE__ */
{
int icolor;

if(*s == '\255' && *(s+1) == 'X') { /* translate */
	if(rdcolor_done) {
		/* only try interpreting this if rdcolor() has been called */
		sscanf(s+2,"%d",&icolor);
		fputs(*colors[icolor].color,fp);
		}
	}
else fputs(s,fp);
}

/* ------------------------------------------------------------------------- */