123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449 |
- /*++
- Copyright (c) 2014 Minoca Corp. All Rights Reserved
- Module Name:
- cecho.c
- Abstract:
- This module implements the color echo application.
- Author:
- Evan Green 1-May-2014
- Environment:
- POSIX
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/lib/types.h>
- #include "swlib.h"
- #include <string.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define CECHO_USAGE \
- "usage: cecho [-f <color>] [-b <color>] [-neE]\n" \
- "The cecho utility echoes command line parameters in color. Options are:\n"\
- " -f <color> -- Print with the given foreground color.\n" \
- " -b <color> -- Print with the given background color.\n" \
- " -n -- Do not print a newline at the end.\n" \
- " -e -- Enable escape processing.\n" \
- " -E -- Disable escape processing.\n" \
- "Use the -- argument to disable argument processing of all subsequent \n" \
- "parameters. The color type can be one of:\n" \
- " d -- Default color\n" \
- " k -- Black\n" \
- " r -- Dark red\n" \
- " g -- Dark green\n" \
- " y -- Dark yellow\n" \
- " b -- Dark blue\n" \
- " m -- Dark magenta\n" \
- " c -- Dark cyan\n" \
- " a -- Dark gray\n" \
- " D -- Bold default\n" \
- " A -- gray\n" \
- " R -- bright red\n" \
- " G -- bright green\n" \
- " Y -- bright yellow\n" \
- " B -- bright blue\n" \
- " M -- bright magenta\n" \
- " C -- bright cyan\n" \
- " W -- white\n"
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- CONSOLE_COLOR
- ColorEchoConvertToColor (
- CHAR Character
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- INT
- ColorEchoMain (
- INT ArgumentCount,
- CHAR **Arguments
- )
- /*++
- Routine Description:
- This routine is the main entry point for the color echo program.
- Arguments:
- ArgumentCount - Supplies the number of command line arguments the program
- was invoked with.
- Arguments - Supplies a tokenized array of command line arguments.
- Return Value:
- 0 on success.
- Non-zero on failure.
- --*/
- {
- PSTR Argument;
- ULONG ArgumentIndex;
- ULONG ArgumentLength;
- CONSOLE_COLOR BackgroundColor;
- CHAR Character;
- ULONG CharacterIndex;
- CHAR DigitCount;
- BOOL EscapeProcessing;
- CONSOLE_COLOR ForegroundColor;
- BOOL PrintTrailingNewline;
- CHAR Value;
- BOOL WasBackslash;
- EscapeProcessing = FALSE;
- PrintTrailingNewline = TRUE;
- BackgroundColor = ConsoleColorDefault;
- ForegroundColor = ConsoleColorDefault;
- //
- // Loop through processing arguments.
- //
- for (ArgumentIndex = 1; ArgumentIndex < ArgumentCount; ArgumentIndex += 1) {
- Argument = Arguments[ArgumentIndex];
- if (Argument[0] != '-') {
- break;
- }
- if (strcmp(Argument, "--help") == 0) {
- printf(CECHO_USAGE);
- return 1;
- }
- while (TRUE) {
- Argument += 1;
- if (Argument[0] == '\0') {
- break;
- } else if (Argument[0] == 'e') {
- EscapeProcessing = TRUE;
- } else if (Argument[0] == 'E') {
- EscapeProcessing = FALSE;
- } else if (Argument[0] == 'n') {
- PrintTrailingNewline = FALSE;
- } else if (Argument[0] == 'f') {
- ForegroundColor = ColorEchoConvertToColor(Argument[1]);
- if (ForegroundColor == -1) {
- return 1;
- }
- Argument += 1;
- } else if (Argument[0] == 'b') {
- BackgroundColor = ColorEchoConvertToColor(Argument[1]);
- if (BackgroundColor == -1) {
- return 1;
- }
- Argument += 1;
- } else if (Argument[0] == '-') {
- break;
- } else {
- break;
- }
- }
- if (Argument[0] != '\0') {
- break;
- }
- }
- //
- // Echo out the remainder of the arguments.
- //
- while (ArgumentIndex < ArgumentCount) {
- Argument = Arguments[ArgumentIndex];
- ArgumentIndex += 1;
- //
- // If not processing backslashes or there are none, just print it
- // directly and continue.
- //
- if ((EscapeProcessing == FALSE) || (strchr(Argument, '\\') == NULL)) {
- SwPrintInColor(BackgroundColor, ForegroundColor, "%s", Argument);
- //
- // Escape the darn thing by looping through every character in the
- // string.
- //
- } else {
- Value = 0;
- DigitCount = 0;
- WasBackslash = FALSE;
- ArgumentLength = strlen(Argument);
- for (CharacterIndex = 0;
- CharacterIndex < ArgumentLength;
- CharacterIndex += 1) {
- Character = Argument[CharacterIndex];
- //
- // If a \0 was detected, then this is in the process of
- // working through \0NNN, where NNN is one to three octal
- // characters.
- //
- if (DigitCount != 0) {
- if ((Character >= '0') && (Character <= '7')) {
- Value = (Value * 8) + (Character - '0');
- DigitCount += 1;
- if (DigitCount == 4) {
- DigitCount = 0;
- SwPrintInColor(BackgroundColor,
- ForegroundColor,
- "%c",
- Value);
- }
- //
- // This was a digit that counted towards the value, so
- // it shouldn't be considered for further printing.
- //
- continue;
- //
- // The number ended a bit early, so print it out and
- // consider this character for further printing.
- //
- } else {
- DigitCount = 0;
- SwPrintInColor(BackgroundColor,
- ForegroundColor,
- "%c",
- Value);
- }
- }
- if (WasBackslash != FALSE) {
- if (Character == 'a') {
- } else if (Character == 'b') {
- SwPrintInColor(BackgroundColor, ForegroundColor, "\b");
- } else if (Character == 'c') {
- PrintTrailingNewline = FALSE;
- goto MainEnd;
- } else if (Character == 'f') {
- SwPrintInColor(BackgroundColor, ForegroundColor, "\f");
- } else if (Character == 'n') {
- SwPrintInColor(BackgroundColor, ForegroundColor, "\n");
- } else if (Character == 'r') {
- SwPrintInColor(BackgroundColor, ForegroundColor, "\r");
- } else if (Character == 't') {
- SwPrintInColor(BackgroundColor, ForegroundColor, "\t");
- } else if (Character == '\\') {
- SwPrintInColor(BackgroundColor, ForegroundColor, "\\");
- } else if (Character == '0') {
- Value = 0;
- DigitCount = 1;
- } else {
- //
- // This backslash escape sequence isn't recognized, so
- // just print it out verbatim.
- //
- SwPrintInColor(BackgroundColor,
- ForegroundColor,
- "\\%c",
- Character);
- }
- //
- // Just a regular old character, print it out unless it begins
- // an escaped sequence.
- //
- } else if (Character != '\\') {
- SwPrintInColor(BackgroundColor,
- ForegroundColor,
- "%c",
- Character);
- }
- if (Character == '\\') {
- WasBackslash = !WasBackslash;
- } else {
- WasBackslash = FALSE;
- }
- }
- }
- if (ArgumentIndex != ArgumentCount) {
- SwPrintInColor(BackgroundColor, ForegroundColor, " ");
- }
- }
- MainEnd:
- if (PrintTrailingNewline != FALSE) {
- SwPrintInColor(BackgroundColor, ForegroundColor, "\n");
- }
- return 0;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- CONSOLE_COLOR
- ColorEchoConvertToColor (
- CHAR Character
- )
- /*++
- Routine Description:
- This routine converts a color character to a console color enum.
- Arguments:
- Character - Supplies the character to convert.
- Return Value:
- Returns the console color on success.
- -1 if the character is not valid.
- --*/
- {
- CONSOLE_COLOR Color;
- switch (Character) {
- case 'd':
- Color = ConsoleColorDefault;
- break;
- case 'k':
- Color = ConsoleColorBlack;
- break;
- case 'r':
- Color = ConsoleColorDarkRed;
- break;
- case 'g':
- Color = ConsoleColorDarkGreen;
- break;
- case 'y':
- Color = ConsoleColorDarkYellow;
- break;
- case 'b':
- Color = ConsoleColorDarkBlue;
- break;
- case 'm':
- Color = ConsoleColorDarkMagenta;
- break;
- case 'c':
- Color = ConsoleColorDarkCyan;
- break;
- case 'a':
- Color = ConsoleColorDarkGray;
- break;
- case 'D':
- Color = ConsoleColorBoldDefault;
- break;
- case 'A':
- Color = ConsoleColorGray;
- break;
- case 'R':
- Color = ConsoleColorRed;
- break;
- case 'G':
- Color = ConsoleColorGreen;
- break;
- case 'Y':
- Color = ConsoleColorYellow;
- break;
- case 'B':
- Color = ConsoleColorBlue;
- break;
- case 'M':
- Color = ConsoleColorMagenta;
- break;
- case 'C':
- Color = ConsoleColorCyan;
- break;
- case 'W':
- Color = ConsoleColorWhite;
- break;
- default:
- fprintf(stderr, "cecho: Invalid color '%c'.\n", Character);
- Color = -1;
- }
- return Color;
- }
|