websh/websh.c

312 lines
6.4 KiB
C

/**
* @file websh.c
* @date 2018-04-16
*
* @brief Formats program output for the web.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <sys/wait.h>
#define MAX_LEN 256
#define MAX_LINES 1024
/**
* @details Global variable for the program name.
*/
static const char *pname;
/**
* @details Global variable for the e-Flag.
*/
static int eFlag = 0;
/**
* @details Global variable for the h-Flag.
*/
static int hFlag = 0;
/**
* @details Global variable for the s-Flag.
*/
static int sFlag = 0;
/**
* @details Global variable for the supplied word.
*/
static const char *word;
/**
* @details Global variable for the supplied tag.
*/
static const char *tag;
/**
* @details Global array which saves the supplied linux commands.
*/
static char *lines[MAX_LINES] = {NULL};
/**
* @details Variable which gets set on signal received.
*/
volatile sig_atomic_t quit = 0;
/**
* @details Signal handler.
* @param signal The number of the received signal.
* @return This function has no return value.
*/
static void handle_signal(int signal)
{
quit = 1;
}
/**
* @details Prints the synopsis.
* @param void This function takes no parameters.
* @return Exit code of the program. Always non-zero.
*/
static void usage(void)
{
fprintf(stderr, "Usage: websh [-e] [-h] [-s WORD:TAG]\n");
exit(EXIT_FAILURE);
}
/**
* @details Parses the supplied -s commandline argument (splits it into WORD and TAG).
* @param sArg The supplied string after the -s flag.
* @return This function has no return value.
*/
static void parsesArg(char *sArg)
{
char *token;
int i = 0;
while ((token = strsep(&sArg, ":"))) {
if (strcmp(token, "") == 0)
usage();
if (i == 2)
usage();
if (i == 0) {
word = token;
} else {
tag = token;
}
i++;
}
}
/**
* @details Parses the commandline arguments and sets the global variables.
* @param argc The number of commandline arguments.
* @param argv The array of commandline arguments.
* @return This function has no return value
*/
static void parse(int argc, char *argv[])
{
int optInd = 0;
char *sArg;
while ((optInd = getopt(argc, argv, "ehs:")) != -1) {
switch (optInd) {
case 'e':
if (eFlag != 0) {
usage();
} else {
eFlag = 1;
}
break;
case 'h':
if (hFlag != 0) {
usage();
} else {
hFlag = 1;
}
break;
case 's':
if (sFlag != 0) {
usage();
} else {
sFlag = 1;
sArg = optarg;
parsesArg(sArg);
}
break;
case '?':
usage();
default:
assert(0);
}
}
pname = argv[0];
}
/**
* @details This function gets called by the second child which gets stdout from
* child1 through a pipe and writes the output to stdout.
* @param fd The file descriptor for the pipe.
* @param line The linux command supplied via stdin.
* @return This function has no return value.
*/
static void child2(int fd[], char *line)
{
char readbuffer[MAX_LEN];
close(fd[1]);
int nbytes = read(fd[0], readbuffer, MAX_LEN);
if (nbytes == -1) {
perror(pname);
exit(EXIT_FAILURE);
}
readbuffer[nbytes-1] = '\0';
if (eFlag && (strcmp(line, lines[0]) == 0))
printf("<html><head></head><body>\n");
if (hFlag) {
/* remove \n from input string */
char *pch = strstr(line, "\n");
if (pch != NULL)
strncpy(pch, "\0", 1);
printf("<h1>%s</h1>\n", line);
}
if (sFlag) {
/* try to find word in command and set in tag */
if (strstr(readbuffer, word) != NULL) {
printf("<%s>%s</%s><br />\n", tag, readbuffer, tag);
} else {
printf("%s<br />\n", readbuffer);
}
} else {
printf("%s<br />\n", readbuffer);
}
int i = 0;
while (lines[i] != NULL) {
i++;
}
/* if last line, print closing tags */
if (eFlag && (strcmp(line, lines[i-1]) == 0)) {
printf("</body></html>\n");
}
return;
}
/**
* @details This function gets called by the first child. It executes a bash shell
* which in turn calls the specified linux command. Stdout is redirected
* to the write end of the pipe.
* @param fd The file descriptor of the pipe.
* @param line The linux command specified via stdin in the calling process.
* @return This function has no return value.
*/
static void child1(int fd[], char *line)
{
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
execl("/bin/bash", "/bin/bash", "-c", line, (char *) NULL);
fprintf(stderr, "[%s] exec failed\n", pname);
free(line);
exit(EXIT_FAILURE);
return;
}
/**
* The main entry point of the program.
* @details Calls the parsing of commandline arguments, sets the signal handler
* and forks the process which execute the functions child1 and child2.
* @param argc The number of supplied commandline arguments.
* @param argv The array of supplied commandline arguments.
* @return 0 on success, non-zero on failure.
*/
int main(int argc, char *argv[])
{
parse(argc, argv);
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = handle_signal;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
while (quit == 0) {
char *line = NULL;
size_t len = 0;
ssize_t nread;
int status;
int w;
int fd[2];
pid_t childpid1;
pid_t childpid2;
pipe(fd);
int count = 0;
while ((nread = getline(&line, &len, stdin)) > 0) {
lines[count] = (char *) malloc (len);
strcpy(lines[count], line);
count++;
/* only read a maximum of 1024 lines */
if (count == 1024)
break;
}
for (int i = 0; i < count; i++) {
switch (childpid1 = fork()) {
case -1:
perror(pname);
free(line);
exit(EXIT_FAILURE);
case 0:
/* CHILD ONE */
child1(fd, lines[i]);
exit(EXIT_SUCCESS);
break;
default:
switch (childpid2 = fork()) {
case -1:
perror(pname);
free(line);
exit(EXIT_FAILURE);
case 0:
/* CHILD TWO */
child2(fd, lines[i]);
exit(EXIT_SUCCESS);
break;
default:
/* PARENT */
do {
w = waitpid(-1, &status, WUNTRACED | WCONTINUED);
if (w == -1) {
perror(pname);
exit(EXIT_FAILURE);
}
if (WIFSIGNALED(status)) {
printf("Killed by signal %d\n", WTERMSIG(status));
} else if (WIFSTOPPED(status)) {
printf("Stopped by signal %d\n", WSTOPSIG(status));
} else if (WIFCONTINUED(status)) {
printf("Continued\n");
}
} while (!WIFEXITED(status) && !WIFSIGNALED(status));
break;
}
}
}
free(line);
break;
}
return 0;
}