Skip to content

Commit

Permalink
Added piping and redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
deo002 committed Oct 4, 2021
1 parent db47529 commit be98a43
Show file tree
Hide file tree
Showing 7 changed files with 449 additions and 18 deletions.
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
output: shell.o tokenize.o parse.o builtin.o execute_command.o signal_handlers.o prompt.o ls.o nightswatch.o history.o user_defined_commands.o
gcc -g shell.o tokenize.o parse.o builtin.o execute_command.o signal_handlers.o prompt.o ls.o nightswatch.o history.o user_defined_commands.o -o exe
output: shell.o tokenize.o parse.o builtin.o execute_command.o signal_handlers.o prompt.o ls.o nightswatch.o history.o user_defined_commands.o simple_commands_util.o pipe_redirection.o
gcc -g shell.o tokenize.o parse.o builtin.o execute_command.o signal_handlers.o prompt.o ls.o nightswatch.o history.o user_defined_commands.o simple_commands_util.o pipe_redirection.o -o exe

shell.o: shell.c
gcc -g -c shell.c
Expand All @@ -25,6 +25,12 @@ builtin.o: builtin.c
prompt.o: prompt.c
gcc -g -c prompt.c

pipe_redirection.o: pipe_redirection.c
gcc -g -c pipe_redirection.c

simple_commands_util.o: simple_commands_util.c
gcc -g -c simple_commands_util.c

user_defined_commands.o: user_defined_commands.c
gcc -g -c user_defined_commands.c

Expand Down
16 changes: 16 additions & 0 deletions execute_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,24 @@ int check_builtin_command(char *cmd)
return -1;
}

int check_piping_and_redirection(char **args, int arg_count)
{
for (int i = 0; i < arg_count; ++i)
{
if (strcmp(args[i], ">") == 0 || strcmp(args[i], ">>") == 0 || strcmp(args[i], "<") == 0 || strcmp(args[i], "|") == 0)
return 1;
}

return 0;
}

int execute_command(char **args, int arg_count)
{
if (check_piping_and_redirection(args, arg_count) == 1)
{
return piping_and_redirection(args, arg_count);
}

strcpy(cmd_name, args[0]);

int builtin_command = check_builtin_command(args[0]);
Expand Down
17 changes: 17 additions & 0 deletions headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ static struct background_jobs ongoing_background_jobs[MAX_JOBS];

int cur_jobs;

struct simple_commands
{
int cnt_args;
char *cmd[BUFFER_SIZE];
struct simple_commands *next;
};

struct simple_commands *head, *tail;

struct simple_commands *create_new_command();
void add_new_command(struct simple_commands *node);
void pop_command();
void show_commands();
void clear_commands();

int piping_and_redirection(char **args, int arg_count);

int execute_command(char **args, int);
int check_builtin_command(char *cmd);

Expand Down
32 changes: 16 additions & 16 deletions history.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
20
jobs
fg 1
clear
cat shell.c > d.txt
quit
cat < shell.c
cat < shell.c > d.txt
ls -l | wc > d.txt
sort < shell.c | wc -l >> d.txt
pinfo
pwd
ls
ls -a -l
nightswatch -n 2 newborn
clear
emacs &
jobs
emacs
jobs
bg 2
jobs
fg 1
jobs
kill 1 9
kill 9 1
kjon 1 9
kjob 1 9
jobs
quit
ls | wc
ls | wc -l
quit
ls | wc
cat < tokenize.c > d.txt
rm d.txt
quit
Loading

0 comments on commit be98a43

Please sign in to comment.