diff options
Diffstat (limited to '')
-rw-r--r-- | command/echo.c | 46 |
1 files changed, 39 insertions, 7 deletions
diff --git a/command/echo.c b/command/echo.c index 49008da..396a0f4 100644 --- a/command/echo.c +++ b/command/echo.c @@ -1,31 +1,47 @@ +/** + * file: echo.c + * command: echo + * author: Tyler Murphy + */ + #include "command.h" #include "lslib.h" #include <stdlib.h> +/** + * Flags that are to be used with echo + */ static struct { bool escape_codes; bool newline; } flags; +/** + * Prints a null terminated string to standard out and changes each char based on input flags + * @param str the string to print + */ static void print_with_escape_codes(const char* str) { - size_t index = 0; - char n; + size_t index = 0; /* current index in the string */ + char c; /* current char being read */ + char n; /* next char being read */ while (true) { - char c = str[index]; + c = str[index]; /* read current char */ index++; - if (c == '\0') break; - if (c != '\\') { + if (c == '\0') break; /* if we hit the NUL, break */ + if (c != '\\') { /* if its not a escaped code, print and continue */ putchar(c); continue; } + /* read next character */ n = str[index]; index++; + /* check each char against the valid escape codes */ switch (n) { case '\\': putchar('\\'); @@ -47,13 +63,19 @@ static void print_with_escape_codes(const char* str) { case 'v': putchar('\v'); break; - default: + default: /* if none found print both chars as is */ putchar(c); putchar(n); } } } +/** + * Takes in each argument that has a single - and parses it + * @param c the character after the - + * @param next the next argument in argv that hasnt been parsed + * @reutrn if the next arg was used or if the arg was invalid + */ static int short_arg(char c, char* next) { UNUSED(next); switch (c) { @@ -74,19 +96,26 @@ static int short_arg(char c, char* next) { return ARG_UNUSED; } +/** + * Prints data in each argument to stdout + */ COMMAND(echo_main) { int start, i; + /* if no arguments supplied exit */ if (argc < 1) { - return EXIT_SUCCESS; + return EXIT_FAILURE; } + /* set default flags */ flags.escape_codes = false; flags.newline = true; + /* parse arguments, no help message */ start = parse_args(argc, argv, NULL, short_arg, NULL); + /* for each argument either print as is or parse each character for escape codes */ for (i = start; i < argc; i++) { if (flags.escape_codes) { print_with_escape_codes(argv[i]); @@ -94,14 +123,17 @@ COMMAND(echo_main) { printf("%s", argv[i]); } + /* put a space seperator between arguments */ if (i + 1 != argc) { putchar(' '); } } + /* if set to put a new line at the end (default), do so */ if (flags.newline) { putchar('\n'); } + /* done */ return EXIT_SUCCESS; } |