This is one of the 42 cursus core curriculum projects. Here, we implemented a shell program like bash
in C
. Our program handles command history, path-based executable searching and launching, redirections and pipes, environment variable expansion, handling of control characters, and several built-in commands like cd, echo, env, exit, export, pwd, unset
.
- You need to install
readline
first to run.brew install readline
- Find installed
readline.h
inhomebrew
directory and add#include <stdio.h>
inside the header file. - Specify the path of readline header file and archive in
Makefile
.
# Makefile
#NAME
NAME = minishell
...
RDLDIR = /opt/homebrew/Cellar/readline/8.2.13 # change this path
...
#FILES
LIBS = $(LFTDIR)/libft.a $(DLLDIR)/libdll.a $(GNLDIR)/libgnl.a -lreadline -L$(RDLDIR)/lib # link archive
INCS = -I $(LFTDIR) -I $(DLLDIR) -I $(GNLDIR) -I $(INCDIR) -I$(RDLDIR)/include # header
SRCS = $(SRCDIR)/main.c \
$(ENVPDIR)/envp.c \
$(ENVPDIR)/envp_util.c \
...
- For OS X Sonoma users, the default path of
ar
would've been set to the one inhomebrew
. Please specify the path ofar
to/usr/bin/ar
in Makefiles oflibraries/dllist
,libraries/libft
,libraries/get_next_line
. Otherwise you'll encountermach -o
errors.
# libraries/dllist/Makefile
NAME = libdll.a
...
AR = /usr/bin/ar # here
ARFLAGS = -rcs
...
git clone https://github.com/Alice-inNovember/42_Minishell.git
cd ./42_Minishell
make
./minishell
We have implemented our parser from this BNF grammar below, which is Recursive Descent Parser
.
program : expression
;
expression : simple_command
| expression '|' simple_command
;
simple_cmd : cmd_prefix cmd_word cmd_suffix
| cmd_prefix cmd_word
| cmd_prefix
| cmd_word cmd_suffix
| cmd_word
;
cmd_word : WORD
;
cmd_prefix : io_redirect
| cmd_prefix io_redirect
;
cmd_suffix : io_redirect
| cmd_suffix io_redirect
| WORD
| cmd_suffix WORD
;
io_redirect : io_file
| io_here
;
io_file : '<' filename
| '>' filename
| '>>' filename
;
filename : WORD
;
io_here : '<<' here_end
;
here_end : WORD
;