bc2176df50c6cec357e7eb4fee80b00f5c1d1da7
[reactos.git] / reactos / base / shell / cmd / color.c
1 /*
2 * COLOR.C - color internal command.
3 *
4 *
5 * History:
6 *
7 * 13-Dec-1998 (Eric Kohl)
8 * Started.
9 *
10 * 19-Jan-1999 (Eric Kohl)
11 * Unicode ready!
12 *
13 * 20-Jan-1999 (Eric Kohl)
14 * Redirection ready!
15 *
16 * 14-Oct-1999 (Paolo Pantaleo <paolopan@freemail.it>)
17 * 4nt's syntax implemented.
18 *
19 * 03-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
20 * Move all hardcoded strings in En.rc.
21 */
22
23 #include "precomp.h"
24
25 #ifdef INCLUDE_CMD_COLOR
26
27 BOOL SetScreenColor(WORD wColor, BOOL bNoFill)
28 {
29 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
30 DWORD dwWritten;
31 CONSOLE_SCREEN_BUFFER_INFO csbi;
32 COORD coPos;
33
34 /* Foreground and Background colors can't be the same */
35 if ((wColor & 0x0F) == (wColor & 0xF0) >> 4)
36 return FALSE;
37
38 /* Fill the whole background if needed */
39 if (bNoFill != TRUE)
40 {
41 GetConsoleScreenBufferInfo(hConsole, &csbi);
42
43 coPos.X = 0;
44 coPos.Y = 0;
45 FillConsoleOutputAttribute(hConsole,
46 wColor & 0x00FF,
47 csbi.dwSize.X * csbi.dwSize.Y,
48 coPos,
49 &dwWritten);
50 }
51
52 /* Set the text attribute */
53 SetConsoleTextAttribute(hConsole, wColor & 0x00FF);
54 return TRUE;
55 }
56
57 /*
58 * color
59 *
60 * internal dir command
61 */
62 INT CommandColor(LPTSTR rest)
63 {
64 WORD wColor = 0x00;
65
66 /* The user asked for help */
67 if (_tcsncmp(rest, _T("/?"), 2) == 0)
68 {
69 ConOutResPaging(TRUE, STRING_COLOR_HELP1);
70 return 0;
71 }
72
73 /* Let's prepare %ERRORLEVEL% */
74 nErrorLevel = 0;
75
76 /* No parameter: Set the default colors */
77 if (rest[0] == _T('\0'))
78 {
79 SetScreenColor(wDefColor, FALSE);
80 return 0;
81 }
82
83 /* The parameter is just one character: Set color text */
84 if (_tcslen(rest) == 1)
85 {
86 if ((rest[0] >= _T('0')) && (rest[0] <= _T('9')))
87 {
88 wColor = (WORD)_ttoi(rest);
89 }
90 else if ((rest[0] >= _T('a')) && (rest[0] <= _T('f')))
91 {
92 wColor = (WORD)(rest[0] + 10 - _T('a'));
93 }
94 else if ((rest[0] >= _T('A')) && (rest[0] <= _T('F')))
95 {
96 wColor = (WORD)(rest[0] + 10 - _T('A'));
97 }
98 else /* Invalid character */
99 {
100 ConOutResPaging(TRUE, STRING_COLOR_HELP1);
101 nErrorLevel = 1;
102 return 1;
103 }
104 }
105 /* Color string: advanced choice: two-digits, "Color ON Color", "Foreground ON Background" */
106 else if (StringToColor(&wColor, &rest) == FALSE)
107 {
108 /* Invalid color string */
109 ConOutResPaging(TRUE, STRING_COLOR_HELP1);
110 nErrorLevel = 1;
111 return 1;
112 }
113
114 TRACE("Color %02x\n", wColor);
115
116 /*
117 * Set the chosen color. Use also the following advanced flag:
118 * /-F to avoid changing already buffered foreground/background.
119 */
120 if (SetScreenColor(wColor, (_tcsstr(rest, _T("/-F")) || _tcsstr(rest, _T("/-f")))) == FALSE)
121 {
122 /* Failed because foreground and background colors were the same */
123 ConErrResPuts(STRING_COLOR_ERROR1);
124 nErrorLevel = 1;
125 return 1;
126 }
127
128 /* Return success */
129 return 0;
130 }
131
132 #endif /* INCLUDE_CMD_COLOR */
133
134 /* EOF */