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
|
/*++
>>> This code is released to the public domain. No warranty
whatsoever is provided, use it at your own risk. <<<
Module Name:
myecho.c
Abstract:
An "echo" replacement that can do color text.
Invoke like this:
myecho.exe [-fg color] [-bg color] "string to echo"
color can be: blue, red, green, yellow, cyan, violet, pink, black, white
brightblue, brightred, brightgreen, brightyellow,
brightviolet, brightpink, brightblack, gray, grey, or
brightwhite
Author:
Scott Gasch ([email protected]) 22 Aug 2002
Revision History:
--*/
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
WORD
MakeColor(BOOL fBg, char *sz)
/*++
Routine description:
Based on a string, return a color WORD.
Parameters:
BOOL fBg,
char *sz
Return value:
WORD
--*/
{
WORD w = 0;
//
// bright
//
if (strstr(sz, "bright"))
{
w = FOREGROUND_INTENSITY;
}
//
// colors
//
if (strstr(sz, "blue"))
{
w |= FOREGROUND_BLUE;
}
if (strstr(sz, "red"))
{
w |= FOREGROUND_RED;
}
if (strstr(sz, "green"))
{
w |= FOREGROUND_GREEN;
}
if (strstr(sz, "yellow"))
{
w |= (FOREGROUND_RED | FOREGROUND_GREEN);
}
if (strstr(sz, "cyan"))
{
w |= (FOREGROUND_BLUE | FOREGROUND_GREEN);
}
if ((strstr(sz, "violet")) ||
(strstr(sz, "pink")))
{
w |= (FOREGROUND_BLUE | FOREGROUND_RED);
}
if (strstr(sz, "white"))
{
w |= (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
if (strstr(sz, "black"))
{
w &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
if (strstr(sz, "gray") || strstr(sz, "grey"))
{
w &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
w |= FOREGROUND_INTENSITY;
}
if (fBg)
{
w <<= 4;
}
return(w);
}
int __cdecl
main(int argc, char *argv[])
/*++
Routine description:
Program entry point; see above for usage.
Parameters:
int argc,
char *argv[]
Return value:
int
--*/
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hStdOut;
WORD wAttr;
int x;
BOOL fNewline = TRUE;
//
// open stdout
//
hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (INVALID_HANDLE_VALUE == hStdOut)
{
fprintf(stderr, "Failed to open handle to stdout, error=%u.\n",
GetLastError());
exit(1);
}
//
// Save the current text colors.
//
if (0 == GetConsoleScreenBufferInfo(hStdOut, &csbiInfo))
{
fprintf(stderr, "Failed to save current text attr, error=%u\n",
GetLastError());
exit(1);
}
//
// echo the string(s)
//
x = 1;
while (x < argc)
{
if (!_strcmpi(argv[x], "-fg"))
{
x++;
if (x < argc)
{
SetConsoleTextAttribute(hStdOut, MakeColor(0, argv[x]));
}
x++;
}
else if (!_strcmpi(argv[x], "-bg"))
{
x++;
if (x < argc)
{
SetConsoleTextAttribute(hStdOut, MakeColor(1, argv[x]));
}
x++;
}
else if (!_strcmpi(argv[x], "-n"))
{
fNewline = FALSE;
x++;
}
else
{
printf(argv[x]);
if (TRUE == fNewline)
{
printf("\n");
}
x++;
}
}
//
// restore the old text colors
//
(void)SetConsoleTextAttribute(hStdOut, csbiInfo.wAttributes);
}
|