diff options
| author | Scott Gasch <[email protected]> | 2016-06-01 19:18:42 -0700 |
|---|---|---|
| committer | Scott Gasch <[email protected]> | 2016-06-01 19:18:42 -0700 |
| commit | fe8d423208cbb6e9fc4957824926f289d419aebc (patch) | |
| tree | e021792d0247e59704efd947baf4f2ef29d7759c /myecho | |
Initial checkin.
Diffstat (limited to 'myecho')
| -rw-r--r-- | myecho/myecho.c | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/myecho/myecho.c b/myecho/myecho.c new file mode 100644 index 0000000..c4657a2 --- /dev/null +++ b/myecho/myecho.c @@ -0,0 +1,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 __cdecl + +--*/ +{ + 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); +} |
