Skip to content

Commit

Permalink
[Add] Redirecting and other fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gsc2001 committed Oct 3, 2020
1 parent 6547a5c commit 15d6a44
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 16 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,8 @@ $(BUILD)/kjob.o: $(SRC)/kjob.c $(INC)/kjob.h $(INC)/errorHandler.h $(INC)/utils.
$(BUILD)/piping.o: $(SRC)/piping.c $(INC)/piping.h $(INC)/errorHandler.h $(INC)/globals.h
$(COMMAND) -o $(BUILD)/piping.o -c $(SRC)/piping.c

$(BUILD)/redirecting.o: $(SRC)/redirecting.c $(INC)/redirecting.h $(INC)/errorHandler.h $(INC)/globals.h
$(COMMAND) -o $(BUILD)/redirecting.o -c $(SRC)/redirecting.c

clean:
rm $(BUILD)/* gSH
7 changes: 7 additions & 0 deletions include/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>

#ifndef __GLOBAL_H
#define __GLOBAL_H
Expand Down Expand Up @@ -38,6 +39,9 @@ typedef struct Command
// for inp and out redirection
char *inp;
char *out;

// is out for appending ?
int append;
} Command;

// piped commands
Expand All @@ -64,4 +68,7 @@ typedef struct Process

char *HOME;

// global stdin and stdout save
int stdoutSaveGlobal;

#endif // __GLOBAL_H
11 changes: 11 additions & 0 deletions include/redirecting.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/****************************************************
author: gsc2001
brief: redirection handling
*****************************************************/
#include "globals.h"
#ifndef __REDIRECT_H
#define __REDIRECT_H
int stdinSave, stdoutSave, rfd, wfd;
void redirectBegin(Command c);
void redirectRestore();
#endif // __REDIRECT_H
34 changes: 30 additions & 4 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Command parseCommand(char *command_raw)

Command command;
command.args = NULL;
command.inp = NULL;
command.out = NULL;
command.append = 0;
command.bg = 0;
command.cmd = strtok(command_, " ");
if (command.cmd == NULL)
Expand All @@ -20,12 +23,35 @@ Command parseCommand(char *command_raw)
arg = strtok(NULL, " ");

// loop over args
int isInp = 0, isOut = 0;
while (arg)
{
if (!strlen(arg))
continue;

args_[command.argc++] = arg;
if (strlen(arg))
{
if (!strcmp(arg, "<") && isInp == 0)
isInp = 1;
else if (!strcmp(arg, ">") && isOut == 0)
isOut = 1;
else if (!strcmp(arg, ">>") && isOut == 0)
{
isOut = 1;
command.append = 1;
}
else if (isInp == 1)
{
isInp++;
command.inp = (char *)malloc(strlen(arg) + 1);
strcpy(command.inp, arg);
}
else if (isOut == 1)
{
isOut++;
command.out = (char *)malloc(strlen(arg) + 1);
strcpy(command.out, arg);
}
else
args_[command.argc++] = arg;
}
arg = strtok(NULL, " ");
}
if (command.argc > 0 && !strcmp(args_[command.argc - 1], "&"))
Expand Down
2 changes: 1 addition & 1 deletion src/piping.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ void execPiped(PipedCommands piped)
{
// inp from pipes[n-2][0] and out to STDOUT
dup2(pipes[i - 1][0], STDIN_FILENO);

dup2(stdoutSave, STDOUT_FILENO);
execCommand(piped.commands[i]);
handleSyscallint(close(pipes[i - 1][0]), "Closing pipe read end");
}
else
{
// inp from pipes[i-1][0] and out to pipes[i][1]
dup2(pipes[i - 1][0], STDIN_FILENO);
dup2(pipes[i][1], STDOUT_FILENO);
execCommand(piped.commands[i]);
Expand Down
47 changes: 47 additions & 0 deletions src/redirecting.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "redirecting.h"
#include "errorHandler.h"

void redirectBegin(Command c)
{
stdinSave = -1;
stdoutSave = -1;

if (c.inp)
{
rfd = handleSyscallint(open(c.inp, O_RDONLY), "Error opening inp file");
if (rfd >= 0)
{
stdinSave = dup(STDIN_FILENO);
dup2(rfd, STDIN_FILENO);
}
}
if (c.out)
{
if (c.append)
wfd = handleSyscallint(open(c.out, O_CREAT | O_WRONLY | O_APPEND, 0644), "Error opening output file");
else
wfd = handleSyscallint(open(c.out, O_CREAT | O_WRONLY | O_TRUNC, 0644), "Error opening output file");

if (wfd >= 0)
{
stdoutSave = dup(STDOUT_FILENO);
dup2(wfd, STDOUT_FILENO);
}
}
}

void redirectRestore()
{
if (stdinSave >= 0)
{
dup2(stdinSave, STDIN_FILENO);
close(rfd);
close(stdinSave);
}
if (stdoutSave >= 0)
{
dup2(stdoutSave, STDOUT_FILENO);
close(wfd);
close(stdoutSave);
}
}
32 changes: 24 additions & 8 deletions src/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "env.h"
#include "kjob.h"
#include "piping.h"
#include "redirecting.h"

void init()
{
Expand All @@ -26,10 +27,13 @@ void init()

// signal handlers
signal(SIGCHLD, sigchldHandler);
// signal(SIGINT, sigintHandler);
signal(SIGINT, sigintHandler);
signal(SIGTSTP, sigtstpHandler);

// saving stdout and stdin
stdoutSaveGlobal = dup(STDOUT_FILENO);
}
const int builtInN = 12;
const int builtInN = 13;

const char *builtInComs[] = {
"pwd",
Expand All @@ -44,6 +48,7 @@ const char *builtInComs[] = {
"setenv",
"unsetenv",
"kjob",
"exit",
};

// built in command functions
Expand All @@ -65,7 +70,7 @@ void (*builtInComExec[])(Command c) = {
void execCommand(Command c)
{
int commandIndex = -1;

redirectBegin(c);
for (int i = 0; i < builtInN; i++)
{
if (!strcmp(c.cmd, builtInComs[i]))
Expand All @@ -76,26 +81,35 @@ void execCommand(Command c)
}

if (commandIndex != -1)
{
if (commandIndex == 12)
{
// exit
byebye();
exit(0);
}

builtInComExec[commandIndex](c);
}
else
{
execSys(c);
}
redirectRestore();
}

void repl()
{
size_t inpsize;
char *inp = 0;
char *inp = NULL;
size_t bufsize = 0;

while (1)
{
char *prompt = get_prompt();
printf("%s", prompt);
fflush(stdout);
inpsize = getline(&inp, &bufsize, stdin);
if (inpsize < 0)
int len = getline(&inp, &bufsize, stdin);
if (len < 0)
break;
inp[strlen(inp) - 1] = '\0';

Expand All @@ -120,7 +134,8 @@ void repl()
for (int j = 0; j < piped.n; j++)
{
Command com = piped.commands[j];
fprintf(stderr, "cmd=%s, argc=%d, backround=%d\n", com.cmd, com.argc, com.bg);
fprintf(stderr, "cmd=%s, argc=%d, backround=%d, inp=%s, out=%s, append=%d\n",
com.cmd, com.argc, com.bg, com.inp ? com.inp : "STDIN", com.out ? com.out : "STDOUT", com.append);
fprintf(stderr, "args->");
for (int k = 0; k < com.argc; k++)
fprintf(stderr, "%s,", com.args[k]);
Expand All @@ -141,5 +156,6 @@ void repl()

void byebye()
{
close(stdoutSaveGlobal);
saveHistory();
}
6 changes: 6 additions & 0 deletions src/sysCommand.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ void execSys(Command c)
// fork
pid_t forkReturn = handleSyscallint(fork(), "Error forking");

if (forkReturn < 0)
return;

if (forkReturn == 0)
{
// child process
Expand All @@ -27,6 +30,9 @@ void execSys(Command c)
int status;
if (c.bg)
{
// to print the pid to shell
dup2(stdoutSaveGlobal, STDOUT_FILENO);

Process p;
initProcess(&p, forkReturn, argv[0]);
insertProcess(p);
Expand Down
21 changes: 18 additions & 3 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,31 @@ void initProcess(Process *loc, pid_t pid, char *name)
strcpy(loc->name, name);
}

int checkEmpty(char *token)
{
int l = strlen(token);
for (int i = 0; i < l; i++)
{
if (token[i] != ' ')
return 0;
}

return 1;
}

int splitString(char **strs, char *str, char *delim)
{
int n = 0;
char *token = strtok(str, delim);
while (token)
{
strs[n] = (char *)malloc(strlen(token) + 1);
strcpy(strs[n++], token);
if (!checkEmpty(token))
{
strs[n] = (char *)malloc(strlen(token) + 1);
strcpy(strs[n++], token);
}
token = strtok(NULL, delim);
}
free(token);
return n;
}
}

0 comments on commit 15d6a44

Please sign in to comment.