b8ce74952cd7f5aa17d354f06085a8aa69e982e0
[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 /*
28 * color
29 *
30 * internal dir command
31 */
32 INT CommandColor(LPTSTR rest)
33 {
34 WORD wColor = 0x00;
35
36 /* The user asked for help */
37 if (_tcsncmp(rest, _T("/?"), 2) == 0)
38 {
39 ConOutResPaging(TRUE, STRING_COLOR_HELP1);
40 return 0;
41 }
42
43 /* Let's prepare %ERRORLEVEL% */
44 nErrorLevel = 0;
45
46 /* No parameter: Set the default colors */
47 if (rest[0] == _T('\0'))
48 {
49 ConSetScreenColor(wDefColor, TRUE);
50 return 0;
51 }
52
53 /* The parameter is just one character: Set color text */
54 if (_tcslen(rest) == 1)
55 {
56 if ((rest[0] >= _T('0')) && (rest[0] <= _T('9')))
57 {
58 wColor = (WORD)_ttoi(rest);
59 }
60 else if ((rest[0] >= _T('a')) && (rest[0] <= _T('f')))
61 {
62 wColor = (WORD)(rest[0] + 10 - _T('a'));
63 }
64 else if ((rest[0] >= _T('A')) && (rest[0] <= _T('F')))
65 {
66 wColor = (WORD)(rest[0] + 10 - _T('A'));
67 }
68 else /* Invalid character */
69 {
70 ConOutResPaging(TRUE, STRING_COLOR_HELP1);
71 nErrorLevel = 1;
72 return 1;
73 }
74 }
75 /* Color string: advanced choice: two-digits, "Color ON Color", "Foreground ON Background" */
76 else if (StringToColor(&wColor, &rest) == FALSE)
77 {
78 /* Invalid color string */
79 ConOutResPaging(TRUE, STRING_COLOR_HELP1);
80 nErrorLevel = 1;
81 return 1;
82 }
83
84 TRACE("Color %02x\n", wColor);
85
86 /*
87 * Set the chosen color. Use also the following advanced flag:
88 * /-F to avoid changing already buffered foreground/background.
89 */
90 if (ConSetScreenColor(wColor, !_tcsstr(rest, _T("/-F")) && !_tcsstr(rest, _T("/-f"))) == FALSE)
91 {
92 /* Failed because foreground and background colors were the same */
93 ConErrResPuts(STRING_COLOR_ERROR1);
94 nErrorLevel = 1;
95 return 1;
96 }
97
98 /* Return success */
99 return 0;
100 }
101
102 #endif /* INCLUDE_CMD_COLOR */
103
104 /* EOF */