123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- /*++
- Copyright (c) 2013 Minoca Corp. All Rights Reserved
- Module Name:
- echo.c
- Abstract:
- This module implements the echo application.
- Author:
- Evan Green 13-Jun-2013
- Environment:
- POSIX
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/lib/types.h>
- #include <stdio.h>
- #include <string.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- BOOL
- EchoIsStringBackslashEscaped (
- PSTR String
- );
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // ------------------------------------------------------------------ Functions
- //
- INT
- EchoMain (
- INT ArgumentCount,
- CHAR **Arguments
- )
- /*++
- Routine Description:
- This routine is the main entry point for the 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 always.
- --*/
- {
- PSTR Argument;
- ULONG ArgumentIndex;
- ULONG ArgumentLength;
- CHAR Character;
- ULONG CharacterIndex;
- CHAR DigitCount;
- BOOL EscapeProcessing;
- BOOL PrintTrailingNewline;
- CHAR Value;
- BOOL WasBackslash;
- EscapeProcessing = FALSE;
- PrintTrailingNewline = TRUE;
- //
- // Loop through processing arguments.
- //
- for (ArgumentIndex = 1; ArgumentIndex < ArgumentCount; ArgumentIndex += 1) {
- Argument = Arguments[ArgumentIndex];
- if (Argument[0] != '-') {
- break;
- }
- 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 {
- 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) ||
- (EchoIsStringBackslashEscaped(Argument) == FALSE)) {
- printf("%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;
- printf("%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;
- printf("%c", Value);
- }
- }
- if (WasBackslash != FALSE) {
- if (Character == 'a') {
- } else if (Character == 'b') {
- printf("\b");
- } else if (Character == 'c') {
- PrintTrailingNewline = FALSE;
- goto MainEnd;
- } else if (Character == 'f') {
- printf("\f");
- } else if (Character == 'n') {
- printf("\n");
- } else if (Character == 'r') {
- printf("\r");
- } else if (Character == 't') {
- printf("\t");
- } else if (Character == '\\') {
- printf("\\");
- } else if (Character == '0') {
- Value = 0;
- DigitCount = 1;
- } else {
- //
- // This backslash escape sequence isn't recognized, so
- // just print it out verbatim.
- //
- printf("\\%c", Character);
- }
- //
- // Just a regular old character, print it out unless it begins
- // an escaped sequence.
- //
- } else if (Character != '\\') {
- printf("%c", Character);
- }
- if (Character == '\\') {
- WasBackslash = !WasBackslash;
- } else {
- WasBackslash = FALSE;
- }
- }
- }
- if (ArgumentIndex != ArgumentCount) {
- printf(" ");
- }
- }
- MainEnd:
- if (PrintTrailingNewline != FALSE) {
- printf("\n");
- }
- return 0;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
- BOOL
- EchoIsStringBackslashEscaped (
- PSTR String
- )
- /*++
- Routine Description:
- This routine determines if the given string has a backslash character in it.
- Arguments:
- String - Supplies a pointer to the null terminated string to check.
- Return Value:
- TRUE if the string contains a backslash.
- FALSE if the string contains no backslashes.
- --*/
- {
- if (strchr(String, '\\') != NULL) {
- return TRUE;
- }
- return FALSE;
- }
|