2023-06-08 01:33:44 +00:00
|
|
|
/**
|
|
|
|
* file: echo.c
|
|
|
|
* command: echo
|
|
|
|
* author: Tyler Murphy
|
|
|
|
*/
|
|
|
|
|
2023-05-06 04:39:44 +00:00
|
|
|
#include "command.h"
|
|
|
|
#include "lslib.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2023-04-27 23:06:40 +00:00
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/**
|
|
|
|
* Flags that are to be used with echo
|
|
|
|
*/
|
2023-05-01 22:43:32 +00:00
|
|
|
static struct {
|
|
|
|
bool escape_codes;
|
|
|
|
bool newline;
|
|
|
|
} flags;
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/**
|
|
|
|
* Prints a null terminated string to standard out and changes each char based on input flags
|
|
|
|
* @param str the string to print
|
|
|
|
*/
|
2023-04-27 23:06:40 +00:00
|
|
|
static void print_with_escape_codes(const char* str) {
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
size_t index = 0; /* current index in the string */
|
|
|
|
char c; /* current char being read */
|
|
|
|
char n; /* next char being read */
|
2023-05-04 20:10:37 +00:00
|
|
|
|
2023-04-27 23:06:40 +00:00
|
|
|
while (true) {
|
2023-06-08 01:33:44 +00:00
|
|
|
c = str[index]; /* read current char */
|
2023-04-27 23:06:40 +00:00
|
|
|
index++;
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
if (c == '\0') break; /* if we hit the NUL, break */
|
|
|
|
if (c != '\\') { /* if its not a escaped code, print and continue */
|
2023-04-27 23:06:40 +00:00
|
|
|
putchar(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* read next character */
|
2023-05-04 20:10:37 +00:00
|
|
|
n = str[index];
|
2023-04-27 23:06:40 +00:00
|
|
|
index++;
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* check each char against the valid escape codes */
|
2023-04-27 23:06:40 +00:00
|
|
|
switch (n) {
|
|
|
|
case '\\':
|
|
|
|
putchar('\\');
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
putchar('\b');
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
case 'n':
|
|
|
|
putchar('\n');
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
putchar('\r');
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
putchar('\t');
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
putchar('\v');
|
|
|
|
break;
|
2023-06-08 01:33:44 +00:00
|
|
|
default: /* if none found print both chars as is */
|
2023-04-27 23:06:40 +00:00
|
|
|
putchar(c);
|
|
|
|
putchar(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2023-05-01 22:43:32 +00:00
|
|
|
static int short_arg(char c, char* next) {
|
|
|
|
UNUSED(next);
|
|
|
|
switch (c) {
|
|
|
|
case 'e':
|
|
|
|
flags.escape_codes = true;
|
|
|
|
break;
|
|
|
|
case 'E':
|
|
|
|
flags.escape_codes = false;
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
flags.newline = false;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
flags.newline = true;
|
|
|
|
flags.escape_codes = false;
|
|
|
|
return ARG_IGNORE;
|
|
|
|
};
|
|
|
|
return ARG_UNUSED;
|
|
|
|
}
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/**
|
|
|
|
* Prints data in each argument to stdout
|
|
|
|
*/
|
2023-05-15 14:57:33 +00:00
|
|
|
COMMAND(echo_main) {
|
2023-04-27 23:06:40 +00:00
|
|
|
|
2023-05-04 20:10:37 +00:00
|
|
|
int start, i;
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* if no arguments supplied exit */
|
2023-04-27 23:06:40 +00:00
|
|
|
if (argc < 1) {
|
2023-06-08 01:33:44 +00:00
|
|
|
return EXIT_FAILURE;
|
2023-04-27 23:06:40 +00:00
|
|
|
}
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* set default flags */
|
2023-05-01 22:43:32 +00:00
|
|
|
flags.escape_codes = false;
|
|
|
|
flags.newline = true;
|
2023-04-27 23:06:40 +00:00
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* parse arguments, no help message */
|
2023-05-04 20:10:37 +00:00
|
|
|
start = parse_args(argc, argv, NULL, short_arg, NULL);
|
2023-04-27 23:06:40 +00:00
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* for each argument either print as is or parse each character for escape codes */
|
2023-05-04 20:10:37 +00:00
|
|
|
for (i = start; i < argc; i++) {
|
2023-05-01 22:43:32 +00:00
|
|
|
if (flags.escape_codes) {
|
2023-04-27 23:06:40 +00:00
|
|
|
print_with_escape_codes(argv[i]);
|
|
|
|
} else {
|
|
|
|
printf("%s", argv[i]);
|
|
|
|
}
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* put a space seperator between arguments */
|
2023-04-27 23:06:40 +00:00
|
|
|
if (i + 1 != argc) {
|
|
|
|
putchar(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* if set to put a new line at the end (default), do so */
|
2023-05-01 22:43:32 +00:00
|
|
|
if (flags.newline) {
|
2023-04-27 23:06:40 +00:00
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
2023-06-08 01:33:44 +00:00
|
|
|
/* done */
|
2023-04-27 23:06:40 +00:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|