Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <sys/wait.h>
- #include <string.h>
- #include <fcntl.h>
- #include <sys/stat.h>
- typedef struct {
- char *prog;
- char **args;
- } COMMAND;
- typedef struct {
- COMMAND *commands;
- int numJobs;
- } JOB;
- // parseInput splits command line input based on spaces and calls
- // splitter to divide input that didn't have spaces
- COMMAND parseInput(char* input);
- char** splitter(char *token, int *count);
- // finds position of left carrot within the arguments array
- // returns -1 if not found
- void whereLeftCarrot(COMMAND *comm);
- // finds position of right carrot within the arguments array
- // returns -1 if not found
- void whereRightCarrot(COMMAND *comm);
- // removes special symbol from comm.args by taking in the pos
- // of the special char and shifting from that spot over
- void shiftArgs(COMMAND *comm, int pos);
- int doCommands(COMMAND comm, char *input);
- void printall(COMMAND comm);
- JOB makeJobs(char *input);
- int main(int argc, char *argv[])
- {
- while (1) {
- char input[512]; // store input line
- char inputcpy[512];
- printf("$sshell "); // shell prompt
- //fflush(stdin);
- fgets(input, 512, stdin); // read line into string
- input[strlen(input) - 1] = '\0';
- strcpy(inputcpy, input);
- JOB jobs = makeJobs(input);
- //COMMAND comm = parseInput(input);
- printf("RET FROM MAKE JOBS\n");
- //check if input is empty or whitespace
- if (jobs.commands[0].prog == NULL) {
- continue;
- }
- else
- // check for exit command
- if (strcmp(jobs.commands[0].prog, "exit") == 0) {
- printf("Bye...\n");
- exit(0);
- }
- else
- if (strcmp(jobs.commands[0].prog, "pwd") == 0) {
- char cwd[512];
- getcwd(cwd, sizeof(cwd));
- printf("%s\n", cwd);
- }
- else
- if (strcmp(jobs.commands[0].prog, "cd") == 0) {
- if (chdir(jobs.commands[0].args[1]) == -1) {
- fprintf(stderr, "Error: no such directory.\n");
- }
- }
- else {
- int exits[jobs.numJobs];
- int status;
- for (int i = 0; i < jobs.numJobs; i++) {
- status = doCommands(jobs.commands[i], inputcpy);
- exits[i] = status;
- }
- fprintf(stderr, "+ completed '%s'", input);
- for (int i = 0; i < jobs.numJobs; i++) {
- fprintf(stderr, "[%d]", exits[i]);
- }
- fprintf(stderr, "\n");
- }
- }
- return EXIT_SUCCESS;
- }
- COMMAND parseInput(char *input) {
- COMMAND comm;
- int i = 1;
- char *token;
- token = strtok(input, " \n");
- // printf("Got a token: %s\n", token);
- // is empty or whitespace
- if (token == NULL) {
- comm.prog = malloc(sizeof(NULL));
- comm.prog = NULL;
- return comm;
- }
- comm.prog = (char*)malloc(strlen(token) + 1);
- strcpy(comm.prog, token);
- comm.args = (char**)malloc(sizeof(char*) * 512);
- comm.args[0] = (char*)malloc(strlen(token) + 1);
- strcpy(comm.args[0], token);
- while (1) {
- token = strtok(NULL, " \n");
- if (token == NULL) {
- break;
- }
- if (strpbrk(token, "<>|") != NULL) {
- int count = 0;
- char **split = splitter(token, &count);
- int j;
- for (j = 0; j < count; j++) {
- comm.args[i + j] = malloc(sizeof(split[j] + 1));
- strcpy(comm.args[i + j], split[j]);
- }
- i = i + j;
- }
- else {
- comm.args[i] = (char*)malloc(strlen(token) + 1);
- strcpy(comm.args[i], token);
- i++;
- }
- }
- comm.args[i] = malloc(sizeof((char*)NULL));
- comm.args[i] = NULL;
- /*
- for(int g = 0; g < i; g++) {
- printf("ARGS: %s\n", comm.args[g]);
- }
- */
- return comm;
- }
- char** splitter(char *token, int *count) {
- char **tokens = malloc(sizeof(char*) * 512);
- char temp[512];
- int i = 0;
- int j = 0;
- while (token[i] != '\0') {
- if (token[i] == '<' || token[i] == '>') {
- temp[j] = '\0';
- if (temp[0] != '\0') {
- tokens[*count] = (char*)malloc(strlen(temp) + 1);
- strcpy(tokens[*count], temp);
- ++*count;
- }
- if (token[i] == '<') {
- tokens[*count] = (char*)malloc(strlen("<") + 1);
- strcpy(tokens[*count], "<");
- }
- else if (token[i] == '>') {
- tokens[*count] = (char*)malloc(strlen(">") + 1);
- strcpy(tokens[*count], ">");
- }
- ++*count;
- j = 0;
- i++;
- temp[0] = '\0';
- }
- else {
- temp[j] = token[i];
- i++;
- j++;
- }
- }
- if (temp[0] != '\0') {
- tokens[*count] = malloc(strlen(temp) + 1);
- strcpy(tokens[*count], temp);
- ++*count;
- }
- // printf("# of tokens: %d\n", *count);
- /*
- for(int g = 0; g < *count; g++) {
- printf("Token Stored: %s\n", tokens[g]);
- }
- */
- return tokens;
- }
- void whereLeftCarrot(COMMAND *comm) {
- int pos = 0;
- while (1) {
- //printf("AFTER WHILE\n");
- if ((*comm).args[pos] == NULL) {
- break;
- }
- if (strcmp((*comm).args[pos], "<") == 0) {
- int fd = open((*comm).args[pos + 1], O_RDONLY);
- dup2(fd, STDIN_FILENO);
- close(fd);
- shiftArgs(comm, pos);
- //printf("position %d\n", pos);
- }
- pos++;
- }
- }
- void whereRightCarrot(COMMAND *comm) {
- int pos = 0;
- while (1) {
- //printf("AFTER WHILE\n");
- if ((*comm).args[pos] == NULL) {
- break;
- }
- if (strcmp((*comm).args[pos], ">") == 0) {
- int fd = open((*comm).args[pos + 1], O_RDWR|O_CREAT|O_TRUNC, S_IRWXU);
- dup2(fd, STDOUT_FILENO);
- close(fd);
- shiftArgs(comm, pos);
- (*comm).args[pos] = NULL;
- }
- pos++;
- }
- }
- void shiftArgs(COMMAND *comm, int pos) {
- while ((*comm).args[pos + 1] != NULL) {
- (*comm).args[pos] = realloc((*comm).args[pos], sizeof((*comm).args[pos + 1]));
- strcpy((*comm).args[pos], (*comm).args[pos + 1]);
- pos++;
- }
- (*comm).args[pos] = malloc(sizeof(NULL));
- (*comm).args[pos] = NULL;
- //int i = 0;
- /*while ((*comm).args[i] != NULL) {
- printf("ARGS[i]: %s\n", (*comm).args[i]);
- i++;
- }
- */
- free((*comm).args[pos + 1]);
- return;
- }
- int doCommands(COMMAND comm, char *input) {
- // create new process
- pid_t PID = fork();
- // if child process
- if (PID == 0) {
- int status;
- //printall(comm);
- whereLeftCarrot(&comm);
- whereRightCarrot(&comm);
- //printall(comm);
- status = execvp(comm.prog, comm.args);
- // if exec fails
- if (status != 0) {
- fprintf(stderr, "Error: command not found.\n");
- exit(42);
- // else exit the process
- }
- else {
- exit(0);
- }
- }
- else {
- int status;
- wait(&status);
- return WEXITSTATUS(status);
- }
- }
- void printall(COMMAND comm) {
- int i = 0;
- printf("COMM.PROG: %s\n", comm.prog);
- while (comm.args[i] != NULL) {
- printf("COMM.ARGS[i]: %s\n", comm.args[i]);
- i++;
- }
- }
- JOB makeJobs(char *input) {
- printf("IN MAKE JOBS\n");
- JOB jobs;
- int pos = 1;
- char *token;
- token = strtok(input, "|\n");
- printf("TOKEN: %s\n", token);
- jobs.commands[0] = parseInput(token);
- printf("AFTER PARSE INPUT ON FIRST TOKEN\n");
- printf("RIGHT BEFORE LOOP\n");
- while (1) {
- if (token == NULL) {
- break;
- }
- token = strtok(NULL, "|\n");
- jobs.commands[pos] = parseInput(token);
- pos++;
- }
- for (int i = 0; i < pos; i++) {
- printf("JOB[%d]:", i);
- printall(jobs.commands[i]);
- }
- jobs.numJobs = pos;
- return jobs;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement