Giter VIP home page Giter VIP logo

tinyshell's Introduction

tinyshell

a tiny shell made with c standard library, which handles multiple pipes and redirections

features

  • interactive mode
  • commandline mode
  • background execution
  • exit on EOF
  • multiple pipes
  • multiple redirections
  • any combinations
  • semicolon separation (dropped after major refactoring)

overview

this program consists of three major parts:

  • execute.c
  • pipe.c
  • redirect.c

know that there are three global variables used for the sake of simplicity,
but you can easily changed them to pointer variables.

extern int I;
extern int pipe_count;
extern bool is_background;

execute.c

bool fork_pipe_redirect(char **, io [2]) is like the core of the core.
pretty much everything is happening here.

well, except for the execvp(3) of course.

static bool fork_pipe_redirect(char **command, io pipes[2])
{
	if (fork() == CHILD)
		if (redirect_in(command) && connect(pipes) && redirect_out(command))
			return (true);
	return (false);
}

void execute(char **command)
{
	static io pipes[2];
	bool is_child_process;

	if (pipe_count && pipe(pipes[CURRENT]) == ERROR)
		exit_error("pipe");
	is_child_process = fork_pipe_redirect(command, pipes);
	if (is_child_process)
	{
		execvp(command[0], command);
		exit_error("execvp");
	}
	if (!is_background)
		while (wait(NULL) >= 0);
	close_(pipes);
	alternate((int **)pipes);
}

pipe.c

the pipe part is where my BRILLIANCE shines the most.
especially in void alternate(int **).
this function literally alternate pipes.

io type a.k.a int [2] get casted to int ** and handled correctly.

void alternate(int **pipes)
{
	int	*pipe_current;

	pipe_current = pipes[CURRENT];
	pipes[CURRENT] = pipes[PREVIOUS];
	pipes[PREVIOUS] = pipe_current;
}

thus making connecting and closing pipes get extremely easy and straightforward.

#define is_last_command (I == pipe_count)

bool connect(io pipes[2])
{
	if (pipe_count)
	{
		if (is_last_command || I != 0)
			dup2(pipes[PREVIOUS][READ], STDIN_FILENO);
		if (I == 0 || !is_last_command)
			dup2(pipes[CURRENT][WRITE], STDOUT_FILENO);
	}
	return (true);
}

void close_(io pipes[2])
{
	if (pipe_count)
	{
		if (is_last_command || I != 0)
			close(pipes[PREVIOUS][READ]);
		if (I == 0 || !is_last_command)
			close(pipes[CURRENT][WRITE]);
	}
}

redirect.c

redirection is the most boring part.
this itself is explanatory,
but a slight variance here and there makes it difficult to shorten the code.

static bool next_redirect_out(char ***arguments)
{
	while (**arguments)
	{
		if (is_same(**arguments, ">") || is_same(**arguments, ">>"))
			return (true);
		(*arguments)++;
	}
	return (false);
}

static void	redirect_out_to(const char *filename, int option)
{
	static int output;

	if (output)
		close(output);
	if (option == TRUNCATE)
		output = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0777);
	else
		output = open(filename, O_WRONLY | O_CREAT | O_APPEND, 0777);
	if (output == ERROR)
		exit_error("open");
	dup2(output, STDOUT_FILENO);
	close(output);
}

bool redirect_out(char **arguments)
{
	char *filename;

	while (next_redirect_out(&arguments))
	{
		filename = strdup(arguments[1]);
		if (is_same(arguments[0], ">"))
			redirect_out_to(filename, TRUNCATE);
		else if (is_same(arguments[0], ">>"))
			redirect_out_to(filename, APPEND);
		erase_from(arguments, 2);
		free(filename);
	}
	return (true);
}

redirect in is almost the same as above, but simpler due to the lack of append operator.
(it's been quite long, but handling heredoc was not pleasant, though that is not shown here)

main.c

after that it's just combining them.

int pipe_count;
int I;
bool is_background;

static void run(char ***commands)
{
	I = -1;
	while (commands[++I])
		if (*commands[I])
			execute(commands[I]);
	while (I-- > 0)
		free_strings(commands[I]);
	free(commands);
}

...

static void parse_and_run(char *line)
{
	char ***commands;

	is_background = line[strlen(line) - 1] == '&';
	if ((commands = parse(line)))
		run(commands);
}

int	main(int count, char **arguments)
{
	char *line;
	char mode;

	check(&mode, count, arguments);
	if (mode == INTERACTIVE)
		while (read_(&line, "tinyshell$ "))
			parse_and_run(line);
	else
		parse_and_run(strdup(arguments[2]));
}

usage

cc *.c
./a.out

commands you can test:

tinyshell$ ls -la | rev > test
tinyshell$ cat test
tinyshell$ cat test | rev | rev | rev > test_rev
tinyshell$ cat test_rev

tinyshell's People

Contributors

eastriverlee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.