Skip to content

Commit

Permalink
Merge pull request #1 from dimadem/diamdem_backend
Browse files Browse the repository at this point in the history
Diamdem backend
  • Loading branch information
dimadem authored Jun 3, 2024
2 parents 3fadfd6 + a908b0b commit 5cac3ac
Show file tree
Hide file tree
Showing 15 changed files with 505 additions and 17 deletions.
28 changes: 14 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,31 @@ RM = rm -rf
# Structure
LIB_DIR = ./lib
LIBFT = $(LIB_DIR)/libft/libft.a
READLINE = -lreadline
READLINE = -lreadline

SRC_DIR = ./src
APP_DIR = $(SRC_DIR)/app
COMMON_DIR = $(SRC_DIR)/common
BUILTINS_DIR = $(SRC_DIR)/builtins
EXECUTE_DIR = $(SRC_DIR)/execute

INCLUDES = -I./inc \
-I $(LIB_DIR)/libft/inc \
-I $(LIB_DIR)/libft \

MAIN_SOURCE = $(wildcard $(SRC_DIR)/*.c)
APP_SOURCES = $(wildcard $(APP_DIR)/*.c)
COMMON_SOURCES = $(wildcard $(COMMON_DIR)/*.c)
BUILTINS_SOURCES = $(wildcard $(BUILTINS_DIR)/*.c)
EXECUTE_SOURCES = $(wildcard $(EXECUTE_DIR)/*.c)

SOURCES = $(MAIN_SOURCE) \
$(APP_SOURCES) \
$(COMMON_SOURCES)
$(BUILTINS_SOURCES) \
$(EXECUTE_SOURCES)
# Building
BUILD_DIR = ./build
MAIN_OBJECT = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/src/%.o, $(MAIN_SOURCE))
APP_OBJECTS = $(patsubst $(APP_DIR)/%.c, $(BUILD_DIR)/src/app/%.o, $(APP_SOURCES))
COMMON_OBJECTS = $(patsubst $(COMMON_DIR)/%.c, $(BUILD_DIR)/common/app/%.o, $(COMMON_SOURCES))
BUILTINS_OBJECTS = $(patsubst $(BUILTINS_DIR)/%.c, $(BUILD_DIR)/src/builtins/%.o, $(BUILTINS_SOURCES))
EXECUTE_OBJECTS = $(patsubst $(EXECUTE_DIR)/%.c, $(BUILD_DIR)/src/execute/%.o, $(EXECUTE_SOURCES))

OBJECTS = $(MAIN_OBJECT) \
$(APP_OBJECTS) \
$(COMMON_OBJECTS)
$(BUILTINS_OBJECTS) \
$(EXECUTE_OBJECTS)

# Processing
all: $(NAME)
Expand All @@ -71,11 +71,11 @@ $(BUILD_DIR)/src/%.o: $(SRC_DIR)/%.c
@mkdir -p $(@D)
@$(COMPILER) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(BUILD_DIR)/src/app/%.o: $(APP_DIR)/%.c
$(BUILD_DIR)/src/builtins/%.o: $(BUILTINS_DIR)/%.c
@mkdir -p $(@D)
@$(COMPILER) $(CFLAGS) $(INCLUDES) -c $< -o $@

$(BUILD_DIR)/src/common/%.o: $(COMMON_DIR)/%.c
$(BUILD_DIR)/src/execute/%.o: $(EXECUTE_DIR)/%.c
@mkdir -p $(@D)
@$(COMPILER) $(CFLAGS) $(INCLUDES) -c $< -o $@

Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
![Norminette workflow](https://github.com/dimadem/minishell/actions/workflows/norminette.yml/badge.svg)
![Compilation workflow](https://github.com/dimadem/minishell/actions/workflows/ci.yml/badge.svg)



build/: Directory for object files and other intermediate build artifacts.

docs/: Documentation for the project.

inc/: Header files for the project, defining interfaces for different modules.
- builtins.h: Declarations for built-in shell commands.
- execute.h: Declarations for command execution functions.
- parser.h: Declarations for command-line parsing functions.
- pipe.h: Declarations for piping functions.
- redirection.h: Declarations for redirection functions.
- shell.h: General declarations for the shell.

lib/: Directory for third-party libraries, such as libft.

src/: Source files for the project, organized into subdirectories by functionality.
- builtins/: Implementation of built-in shell commands.
- execute/: Implementation of command execution logic.
- parser/: Implementation of command-line parsing.
- pipe/: Implementation of piping between commands.
- redirection/: Implementation of input/output redirection.
- main.c: Entry point of the shell application.
25 changes: 25 additions & 0 deletions inc/builtins.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtins.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:34:46 by dmdemirk #+# #+# */
/* Updated: 2024/06/03 16:34:47 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef BUILTINS_H
# define BUILTINS_H

/* builtin functions */
int builtin_cd(char **args);
int builtin_echo(char **argv);
int builtin_env(char **argv);
int builtin_exit(char **argv);
// int export(char **argv);
// int pwd(char **argv);
// int unset(char **argv);

#endif
18 changes: 18 additions & 0 deletions inc/execute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:34:42 by dmdemirk #+# #+# */
/* Updated: 2024/06/03 16:34:43 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef EXECUTE_H
# define EXECUTE_H

int execute(char **args);

#endif
Binary file added src/builtins/.env.c.swp
Binary file not shown.
33 changes: 33 additions & 0 deletions src/builtins/cd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:31:07 by dmdemirk #+# #+# */
/* Updated: 2024/06/03 16:31:19 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>
#include "libft.h"

int builtin_cd(const char **args)
{
char *target_dir;
char *home_dir;

home_dir = getenv("HOME");
target_dir = (char *)args[1];
if (!target_dir)
target_dir = home_dir;
else if (target_dir[0] == '~')
target_dir = ft_strjoin(home_dir, target_dir + 1);
if (chdir(target_dir) != 0)
{
perror("cd");
return (1);
}
return (0);
}
35 changes: 35 additions & 0 deletions src/builtins/echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:31:33 by dmdemirk #+# #+# */
/* Updated: 2024/06/03 16:38:36 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

int builtin_echo(const char **argv)
{
int newline;
int i;

newline = 1;
if (ft_strcmp(argv[1], "-n") == 0)
{
newline = 0;
argv++;
}
i = -1;
while (*argv++)
{
write(1, argv[i], ft_strlen(argv[i]));
i++;
}
if (newline)
write(1, "\n", 1);
return (0);
}
16 changes: 16 additions & 0 deletions src/builtins/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:32:34 by dmdemirk #+# #+# */
/* Updated: 2024/06/03 16:39:19 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

int builtin_env(void)
{
return (0);
}
16 changes: 16 additions & 0 deletions src/builtins/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/03 16:32:42 by dmdemirk #+# #+# */
/* Updated: 2024/06/03 16:39:06 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

int builtin_exit(void)
{
return (0);
}
Empty file added src/builtins/export.c
Empty file.
Empty file added src/builtins/pwd.c
Empty file.
Empty file added src/builtins/unset.c
Empty file.
64 changes: 64 additions & 0 deletions src/execute/execute.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/31 11:02:00 by dmdemirk #+# #+# */
/* Updated: 2024/06/03 16:33:53 by dmdemirk ### ########.fr */
/* */
/* ************************************************************************** */

#include "builtins.h"
#include "libft.h"
#include <stdio.h>
#include <sys/wait.h>

int new_process(char **args);

int execute(char **args)
{
size_t i;
char *builtin_commands[7];
int (*builtin_functions[7])(char **);

builtin_commands[0] = "cd";
builtin_commands[1] = "echo";
builtin_commands[2] = "env";
builtin_commands[3] = "exit";
builtin_functions[0] = &builtin_cd;
builtin_functions[1] = &builtin_echo;
builtin_functions[2] = &builtin_env;
builtin_functions[3] = &builtin_exit;
if (args[0] == NULL)
return (1);
i = -1;
while (++i < sizeof(builtin_commands) / sizeof(char *))
{
if (ft_strcmp(args[0], builtin_commands[i]) == 0)
return ((*builtin_functions[i])(args));
else
return (new_process(args));
}
return (1);
}

int new_process(char **args)
{
pid_t pid;
int status;

pid = fork();
if (pid == 0)
{
if (execvp(args[0], args) == -1)
perror("minishell");
exit(EXIT_FAILURE);
}
else if (pid < 0)
perror("minishell");
else
waitpid(pid, &status, 0);
return (1);
}
8 changes: 5 additions & 3 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/* ************************************************************************** */

#include "tokens.h"
#include "execute.h"

void handle_args(t_token **tokens, char **argv)
{
Expand Down Expand Up @@ -40,17 +41,18 @@ int main(void)
while (repeat)
{
tokens = NULL;
line = readline("🌴\e[1m dimrom@maxishell> \e[m");
line = readline("🌴\e[1m dimrom@maxishell-> \e[m");
if (!line || ft_strcmp(line, "exit") == 0)
break ;
if (*line)
add_history(line);
write_history(history_file);
parsed_text = ft_split(line, ' ');
printf("\033[31m@maxishell: command not found: %s\033[0m\n", parsed_text[0]);
// printf("\033[31m@maxishell: command not found: %s\033[0m\n", parsed_text[0]);
handle_args(&tokens, parsed_text);
arr = list_to_array(tokens);
print_stack(&tokens);
execute(arr);
// print_stack(&tokens);
free_stack(&tokens);
free(line);
free(arr);
Expand Down
Loading

0 comments on commit 5cac3ac

Please sign in to comment.