1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <strings.h>
4 #include <string.h>
5 #include "configfile.h"
6 #include "machine_type.h"
7 #include "ncurses.h"
8 #include "output.h"
9 #include "input.h"
10 
11 typedef	struct _tColorName
12 {
13 	char name[16];
14 	short len;
15 	short val;
16 	int attrs;
17 } tColorName;
18 
19 typedef struct _tColorGroup
20 {
21 	char 	configname[16];
22 	short	 len;
23 	uicolors uicol;
24 } tColorGroup;
25 
26 #define	NUMCOLORGROUPS	14
27 const tColorGroup colorGroups[NUMCOLORGROUPS]={
28 				{"BRACKETS:",		 9,COLOR_BRACKETS},
29 				{"HEXFIELD:",		 9,COLOR_HEXFIELD},
30 				{"INPUT:",		 6,COLOR_INPUT},
31 				{"CURSOR:",		 7,COLOR_CURSOR},
32 				{"TEXT:",		 5,COLOR_TEXT},
33 				{"MENU_NORMAL:",	12,COLOR_MENUNORMAL},
34 				{"MENU_HIGHLIGHT:",	15,COLOR_MENUACTIVE},
35 				{"MENU_HOTKEY:",	12,COLOR_MENUHOTKEY},
36 				{"MENU_HOTKEY_HI:",	15,COLOR_MENUHOTKEYACTIVE},
37 				{"NORMAL_DIFF:",	12,COLOR_DIFF},
38 				{"CURSOR_DIFF:",	12,COLOR_CURSORDIFF},
39 				{"HEADLINE:",		 9,COLOR_HEADLINE},
40 				{"HEADER:",		 7,COLOR_HEADER},
41 				{"FRAME:",		 6,COLOR_FRAME}
42 				};
43 #define	NUMCOLORNAMES	20
44 const tColorName colorNames[NUMCOLORNAMES]={
45 					{"BLACK",	5,COLOR_BLACK,0},
46 					{"RED",		3,COLOR_RED,0},
47 					{"GREEN",	5,COLOR_GREEN,0},
48 					{"BLUE",	4,COLOR_BLUE,0},
49 					{"PURPLE",	6,COLOR_MAGENTA,0},
50 					{"MAGENTA",	7,COLOR_MAGENTA,0},
51 					{"CYAN",	4,COLOR_CYAN,0},
52 					{"WHITE",	5,COLOR_WHITE,0},
53 					{"YELLOW",	6,COLOR_YELLOW,A_BOLD},
54 					{"BROWN",	5,COLOR_YELLOW,0},
55 
56 					{"LIGHTBLACK",	10,COLOR_BLACK,A_BOLD},
57 					{"LIGHTRED",	 8,COLOR_RED,A_BOLD},
58 					{"LIGHTGREEN",	10,COLOR_GREEN,A_BOLD},
59 					{"LIGHTBLUE",	 9,COLOR_BLUE,A_BOLD},
60 					{"LIGHTPURPLE",	11,COLOR_MAGENTA,A_BOLD},
61 					{"LIGHTMAGENTA",12,COLOR_MAGENTA,A_BOLD},
62 					{"LIGHTCYAN",	 9,COLOR_CYAN,A_BOLD},
63 					{"LIGHTWHITE",	10,COLOR_WHITE,A_BOLD},
64 					{"LIGHTYELLOW",	11,COLOR_YELLOW,A_BOLD},
65 					{"LIGHTBROWN",	10,COLOR_YELLOW,A_BOLD}
66 				};
67 
68 #define	NUMATTRNAMES	5
69 const tColorName attrNames[NUMATTRNAMES]={
70 					{"UNDERLINE",	 9,0,A_UNDERLINE},
71 					{"REVERSE",	 7,0,A_REVERSE},
72 					{"BLINK",	 5,0,A_BLINK},
73 					{"DIM",		 3,0,A_DIM},
74 					{"BOLD",	 4,0,A_BOLD}
75 				};
76 
getcolors(tOutput * output,char * line)77 int getcolors(tOutput* output,char* line)
78 {
79 	int i;
80 	int l;
81 	int state=0;	// 0: find :, 1=find FG=, 2=find, , 3=find BG=, 4=find , ,5=find END, 6=nothing
82 	char token[6][64];
83 	int tokenidx=0;
84 	char c;
85 	short col1;
86 	short col2;
87 	int attrs1;
88 	int attrs2;
89 	int attrsret;
90 	short fg=7,bg=0;
91 
92 	l=strlen(line);
93 	memset(token,0,sizeof(token));
94 	for (i=0;i<l;i++)
95 	{
96 		c=line[i];
97 		if (c=='#') state=6;
98 		if (c==' ' || c==9) c=0;	// ignore all the white spaces
99 		else {
100 			if (c>='a' && c<='z') c-=32;	// make the letters lower case
101 			if (state==0 && c==':') {
102 				token[state][tokenidx++]=c;	//apend the last char
103 				token[state][tokenidx]=0;
104 				state=1;
105 				tokenidx=0;
106 			}
107 			else if (state==1 && c=='=') {state=2;tokenidx=0;}
108 			else if (state==2 && c==',') {state=3;tokenidx=0;}
109 			else if (state==3 && c=='=') {state=4;tokenidx=0;}
110 			else if (state==4 && c==',') {state=5;tokenidx=0;}
111 			else if (state==5 && c<32) {state=6;tokenidx=0;}
112 			else if (state!=6 && tokenidx<63)
113 			{
114 				token[state][tokenidx++]=c;
115 				token[state][tokenidx]=0;
116 			}
117 		}
118 	}
119 	//printf("%s;%s;%s;%s;%s;%s\n",token[0],token[1],token[2],token[3],token[4],token[5]);
120 	// token0: contains the color group
121 	// token1: contains fg or bg
122 	// token2: contains the color
123 	// token3: contains fg or bg
124 	// token4: contains the color black, red, green, blue, purple, cyan, brown, yellow, white, lightblack, lightred, lightgreen, lightblue, lightpurple,lightcyan, lightbrown, lightyellow
125 	// token5: contains something light BOLD, reverse, blink, underline, dim
126 
127 	col1=0;col2=0;
128 	attrs1=0;attrs2=0;
129 	attrsret=0;
130 	for (i=0;i<NUMCOLORNAMES;i++)
131 	{
132 		//printf("   colorNames[i].name=%s,token[2]=%s\n",colorNames[i].name,token[2]);
133 		if (strncmp(token[2],colorNames[i].name,colorNames[i].len)==0) {col1=colorNames[i].val;attrs1=colorNames[i].attrs;}
134 		if (strncmp(token[4],colorNames[i].name,colorNames[i].len)==0) {col2=colorNames[i].val;attrs2=colorNames[i].attrs;}
135 	}
136 	if (strncmp(token[1],"FG",2)==0)
137 	{
138 		fg=col1;
139 		attrsret|=attrs1;
140 	}
141 	if (strncmp(token[1],"BG",2)==0)
142 	{
143 		bg=col1;
144 		attrsret|=(attrs1==A_BOLD)?A_BLINK:attrs1;
145 	}
146 
147 	if (strncmp(token[3],"FG",2)==0)
148 	{
149 		fg=col2;
150 		attrsret|=attrs2;
151 	}
152 	if (strncmp(token[3],"BG",2)==0)
153 	{
154 		bg=col2;
155 		attrsret|=(attrs2==A_BOLD)?A_BLINK:attrs2;
156 	}
157 	for (i=0;i<NUMATTRNAMES;i++)
158 	{
159 		if (strncmp(attrNames[i].name,token[5],attrNames[i].len)==0) attrsret|=attrNames[i].attrs;
160 	}
161 
162 	for (i=0;i<NUMCOLORGROUPS;i++)
163 	{
164 		if (strncmp(colorGroups[i].configname,token[0],colorGroups[i].len)==0)
165 		{
166 			colorpair(output,colorGroups[i].uicol,fg,bg,attrsret);
167 		}
168 	}
169 	return	RETOK;
170 }
readconfigfile(tOutput * output,char * filename)171 int readconfigfile(tOutput* output,char* filename)
172 {
173 	tFptr f=fopen(filename,"rb");
174 	char line[512];
175 	unsigned char c;
176 	int lineidx=0;
177 	int	keyboardcnt;
178 	int n;
179 	if (!f) return 1;
180 	keyboardcnt=0;
181 	while (!feof(f))
182 	{
183 		n=fread(&c,sizeof(char),1,f);
184 		if (n && !feof(f))
185 		{
186 			if (c>='a' && c<='z') c-=32;	// make everything uppercase
187 			if (c==9) c=32;			// and whitespaces are all the same
188 			if (lineidx<511 && c!=32)
189 			{
190 				line[lineidx++]=c;
191 				line[lineidx]=0;
192 				if (c<32)
193 				{
194 					getcolors(output,line);
195 					keyboardcnt+=(configkeytab((tKeyTab*)output->pKeyTab,line)==0);
196 					lineidx=0;
197 					line[0]=0;
198 				}
199 			}
200 		}
201 	}
202 	fclose(f);
203 	if (keyboardcnt!=NUM_SPECIALKEYS) return 2;	// not every spcial key was configured in the config file
204 	return 0;
205 }
206