Update whitespace and email.
[batch.git] / myecho / myecho.c
1 /*++
2
3   >>> This code is released to the public domain.  No warranty
4        whatsoever is provided, use it at your own risk. <<<
5
6 Module Name:
7
8     myecho.c
9
10 Abstract:
11
12     An "echo" replacement that can do color text.
13
14     Invoke like this:
15
16     myecho.exe [-fg color] [-bg color] "string to echo"
17
18     color can be: blue, red, green, yellow, cyan, violet, pink, black, white
19                   brightblue, brightred, brightgreen, brightyellow,
20                   brightviolet, brightpink, brightblack, gray, grey, or
21                   brightwhite
22
23 Author:
24
25     Scott Gasch ([email protected]) 22 Aug 2002
26
27 Revision History:
28
29 --*/
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <windows.h>
34
35 WORD
36 MakeColor(BOOL fBg, char *sz)
37 /*++
38
39 Routine description:
40
41     Based on a string, return a color WORD.
42
43 Parameters:
44
45     BOOL fBg,
46     char *sz
47
48 Return value:
49
50     WORD
51
52 --*/
53 {
54     WORD w = 0;
55
56     //
57     // bright
58     //
59     if (strstr(sz, "bright"))
60     {
61         w = FOREGROUND_INTENSITY;
62     }
63
64     //
65     // colors
66     //
67     if (strstr(sz, "blue"))
68     {
69         w |= FOREGROUND_BLUE;
70     }
71     if (strstr(sz, "red"))
72     {
73         w |= FOREGROUND_RED;
74     }
75     if (strstr(sz, "green"))
76     {
77         w |= FOREGROUND_GREEN;
78     }
79     if (strstr(sz, "yellow"))
80     {
81         w |= (FOREGROUND_RED | FOREGROUND_GREEN);
82     }
83     if (strstr(sz, "cyan"))
84     {
85         w |= (FOREGROUND_BLUE | FOREGROUND_GREEN);
86     }
87     if ((strstr(sz, "violet")) ||
88         (strstr(sz, "pink")))
89     {
90         w |= (FOREGROUND_BLUE | FOREGROUND_RED);
91     }
92     if (strstr(sz, "white"))
93     {
94         w |= (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
95     }
96     if (strstr(sz, "black"))
97     {
98         w &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
99     }
100     if (strstr(sz, "gray") || strstr(sz, "grey"))
101     {
102         w &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
103         w |= FOREGROUND_INTENSITY;
104     }
105     if (fBg)
106     {
107         w <<= 4;
108     }
109
110     return(w);
111 }
112
113
114 int __cdecl
115 main(int argc, char *argv[])
116 /*++
117
118 Routine description:
119
120     Program entry point; see above for usage.
121
122 Parameters:
123
124     int argc,
125     char *argv[]
126
127 Return value:
128
129     int
130
131 --*/
132 {
133     CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
134     HANDLE hStdOut;
135     WORD wAttr;
136     int x;
137     BOOL fNewline = TRUE;
138
139     //
140     // open stdout
141     //
142     hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
143     if (INVALID_HANDLE_VALUE == hStdOut)
144     {
145         fprintf(stderr, "Failed to open handle to stdout, error=%u.\n",
146                 GetLastError());
147         exit(1);
148     }
149
150     //
151     // Save the current text colors.
152     //
153     if (0 == GetConsoleScreenBufferInfo(hStdOut, &csbiInfo))
154     {
155         fprintf(stderr, "Failed to save current text attr, error=%u\n",
156                 GetLastError());
157         exit(1);
158     }
159
160     //
161     // echo the string(s)
162     //
163     x = 1;
164     while (x < argc)
165     {
166         if (!_strcmpi(argv[x], "-fg"))
167         {
168             x++;
169             if (x < argc)
170             {
171                 SetConsoleTextAttribute(hStdOut, MakeColor(0, argv[x]));
172             }
173             x++;
174         }
175         else if (!_strcmpi(argv[x], "-bg"))
176         {
177             x++;
178             if (x < argc)
179             {
180                 SetConsoleTextAttribute(hStdOut, MakeColor(1, argv[x]));
181             }
182             x++;
183         }
184         else if (!_strcmpi(argv[x], "-n"))
185         {
186             fNewline = FALSE;
187             x++;
188         }
189         else
190         {
191             printf(argv[x]);
192             if (TRUE == fNewline)
193             {
194                 printf("\n");
195             }
196             x++;
197         }
198     }
199
200     //
201     // restore the old text colors
202     //
203     (void)SetConsoleTextAttribute(hStdOut, csbiInfo.wAttributes);
204 }