Crtl-C gives a new line when reading input
[reactos.git] / reactos / subsys / system / cmd / strtoclr.c
1 /*
2 * STRTOCLR.C - read color (for color command and other)
3 *
4 *
5 * History:
6 *
7 * 07-Oct-1999 (Paolo Pantaleo)
8 * Started.
9 *
10 *
11 */
12
13 /*only
14 BOOL StringToColor(LPWORD lpColor, LPTSTR*str)
15 is to be called
16 other are internal service functions*/
17
18
19 #include <precomp.h>
20
21 #define _B FOREGROUND_BLUE
22 #define _G FOREGROUND_GREEN
23 #define _R FOREGROUND_RED
24 #define _I FOREGROUND_INTENSITY
25
26
27 /*return values for chop_blank*/
28 #define CP_OK 0
29 #define CP_BLANK_NOT_FOUND 1
30 #define CP_END_OF_STRING 2
31
32 #define SC_HEX 0x0100
33 #define SC_TXT 0x0200
34
35
36
37 typedef struct _CLRTABLE
38 {
39 LPTSTR name;
40 WORD val;
41 } CLRTABLE;
42
43
44 CLRTABLE clrtable[] =
45 {
46 {_T("bla") ,0 },
47 {_T("blu") ,_B },
48 {_T("gre") ,_G },
49 {_T("cya") ,_B|_G },
50 {_T("red") ,_R },
51 {_T("mag") ,_B|_R },
52 {_T("yel") ,_R|_G },
53 {_T("whi") ,_R|_G|_B },
54 {_T("gra") ,_I },
55
56
57 {_T("0") ,0 },
58 {_T("2") ,_G },
59 {_T("3") ,_B|_G },
60 {_T("4") ,_R },
61 {_T("5") ,_B|_R },
62 {_T("6") ,_R|_G },
63 {_T("7") ,_R|_G|_B },
64
65 {_T("8") ,_I },
66 {_T("9") ,_I|_B },
67 {_T("10") ,_I|_G },
68 {_T("11") ,_I|_B|_G },
69 {_T("12") ,_I|_R },
70 {_T("13") ,_I|_B|_R },
71 {_T("14") ,_I|_R|_G },
72 {_T("15") ,_I|_R|_G|_B },
73
74
75 /* note that 1 is at the end of list
76 to avoid to confuse it with 10-15*/
77 {_T("1") ,_B },
78
79 /*cyan synonimous*/
80 {_T("aqu") ,_B|_G },
81 /*magenta synonimous*/
82 {_T("pur") ,_B|_R },
83
84
85 {_T("") ,0},
86 };
87
88
89
90 /*
91 move string pointer to next word (skip all spaces)
92 on erro retunr nonzero value
93 */
94 static
95 INT chop_blank(LPTSTR *arg_str)
96 {
97
98 LPTSTR str;
99 str = _tcschr(*arg_str,_T(' '));
100 if(!str)
101 {
102 str = _tcschr (*arg_str, _T('\0'));
103 if(str != NULL)
104 *arg_str=str;
105 return CP_BLANK_NOT_FOUND;
106 }
107
108
109
110 while(_istspace(*str))
111 str++;
112
113 if (*str == _T('\0'))
114 {
115 *arg_str=str;
116 return CP_END_OF_STRING;
117 }
118
119 *arg_str = str;
120
121 return CP_OK;
122 }
123
124
125
126 /*
127 read a color value in hex (like win nt's cmd syntax)
128 if an error occurs return -1
129 */
130 static
131 WORD hex_clr(LPTSTR str)
132 {
133 WORD ret= (WORD)-1;
134 TCHAR ch;
135
136 ch = str[1];
137
138 if(_istdigit(ch))
139 ret = ch-_T('0');
140 else
141 {
142 ch=_totupper(ch);
143
144 if( ch >= _T('A') && ch <= _T('F') )
145 ret = ch-_T('A')+10;
146 else
147 return (WORD)-1;
148 }
149
150
151 ch = str[0];
152
153 if(_istdigit(ch))
154 ret |= (ch-_T('0')) << 4;
155 else
156 {
157 ch=_totupper(ch);
158
159 if( ch >= _T('A') && ch <= _T('F') )
160 ret |= (ch-_T('A')+10) <<4;
161 else
162 return (WORD)-1;
163 }
164
165 return ret;
166 }
167
168
169 /*
170 read a color value from a string (like 4nt's syntax)
171 if an error occurs return -1
172 */
173 static
174 WORD txt_clr(LPTSTR str)
175 {
176 INT i;
177
178 for(i = 0; *(clrtable[i].name); i++)
179 if (_tcsnicmp(str, clrtable[i].name, _tcslen(clrtable[i].name)) == 0)
180 return clrtable[i].val;
181
182 return (WORD)-1;
183 }
184
185
186
187 /*search for x on y*/
188 static
189 WORD str_to_color(LPTSTR* arg_str)
190 {
191 LPTSTR str;
192 BOOL bBri;
193
194 WORD tmp_clr,ret_clr;
195
196 str = *arg_str;
197
198 if (!(*str))
199 return (WORD)-1;
200
201
202 /*foreground*/
203 bBri = FALSE;
204
205 if (_tcsnicmp(str,_T("bri"),3) == 0)
206 {
207 bBri = TRUE;
208
209 if (chop_blank(&str))
210 return (WORD)-1;
211 }
212
213 if ((tmp_clr = txt_clr(str)) == (WORD)-1)
214 {
215 return (WORD)-1;
216 }
217
218 /*skip spaces and "on"*/
219 if (chop_blank(&str) || chop_blank(&str))
220 return (WORD)-1;
221
222 ret_clr = tmp_clr | (bBri << 3);
223
224 /*background*/
225 bBri = FALSE;
226
227 if(_tcsnicmp(str,_T("bri"),3) == 0 )
228 {
229 bBri = TRUE;
230
231 if(chop_blank(&str))
232 return (WORD)-1;
233 }
234
235
236 if( (tmp_clr = txt_clr(str)) == (WORD)-1 )
237 return (WORD)-1;
238
239 chop_blank(&str);
240
241 *arg_str = str;
242
243 return SC_HEX | ret_clr | tmp_clr << 4 | bBri << 7;
244 }
245
246
247
248 /****main function****/
249 /*
250 the only parameter is arg_str, a pointer to a string.
251 the string is modified so it will begin to first word after
252 color specification
253 (only the char* is moved, no chars in the string are modfied)
254
255
256 it returns the color in the l.o. byte, plus two flags in the
257 h.o. byte, they are:
258 SC_HEX win nt's cmd syntax (for exampl a0)
259 SC_TXT 4nt's syntax ( "bri gre on bla" or "10 on 0")
260
261 if succedes also move the LPTSTR to end of
262 string that specify color
263 */
264
265
266 BOOL StringToColor(LPWORD lpColor, LPTSTR*str)
267 {
268 WORD wRet;
269
270 wRet = str_to_color (str);
271 if (wRet == (WORD)-1)
272 {
273 wRet=hex_clr (*str);
274 chop_blank (str);
275 if (wRet == (WORD)-1)
276 return FALSE;
277 }
278
279 *lpColor = wRet;
280
281 return TRUE;
282 }
283
284 /* EOF */