Skip to content

Commit

Permalink
Merge pull request #10 from facetint/facetint
Browse files Browse the repository at this point in the history
Facetint
  • Loading branch information
facetint authored Mar 3, 2024
2 parents 47484dd + b447ad4 commit 39ccb5c
Show file tree
Hide file tree
Showing 89 changed files with 2,443 additions and 225 deletions.
Binary file added .DS_Store
Binary file not shown.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

32 changes: 25 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ LIBFT_DIR = ./libft
LIBFT_PATH = $(LIBFT_DIR)/libft.a

CC = gcc
FLAGS = -Wall -Wextra -Werror -g

FLAGS = -g -Wall -Wextra -Werror
MEMORY_ALLOCATOR_SOURCES = memory-allocator/aborter.c memory-allocator/allocator.c
SOURCES = expander/expander_nonvariables.c utils.c handler.c lexer.c unquote.c lexer_utils.c parser.c execute.c expander.c splitter.c syntax_analyzer.c $(MEMORY_ALLOCATOR_SOURCES)
SOURCES = execute/find_path.c builtin/exit.c builtin/export.c builtin/echo.c builtin/env.c builtin/pwd.c builtin/builtin.c get_next_line/get_next_line.c expander/expander_nonvariables.c env/env_utils.c utils.c handler.c env/env.c lexer.c unquote.c lexer_utils.c parser.c execute/execute.c expander/expander.c splitter.c syntax_analyzer.c $(MEMORY_ALLOCATOR_SOURCES)

MINISHELL_SOURCES = main.c $(SOURCES)
MINISHELL_OBJECTS = $(MINISHELL_SOURCES:.c=.o)
Expand All @@ -16,24 +15,29 @@ TEST_PATH = tests
TEST_SOURCES = $(wildcard $(TEST_PATH)/*.c)
TEST_OBJECTS = $(TEST_SOURCES:.c=.o)


all: $(NAME)

$(TEST_PATH):
mkdir $(TEST_PATH)
@mkdir $(TEST_PATH)

test: $(TEST_PATH) $(NAME)
$(CC) $(SOURCES:.c=.o) $(LIBFT_PATH) $(TEST_SOURCES) -o $(TEST_PATH)/tests -lcriterion -L/usr/local/lib -I/usr/local/include -lreadline
./$(TEST_PATH)/tests

$(LIBFT_PATH):
make -C $(LIBFT_DIR)
@make -C $(LIBFT_DIR)

$(NAME): $(LIBFT_PATH) $(MINISHELL_OBJECTS)
$(CC) $(FLAGS) -o $(NAME) $(MINISHELL_OBJECTS) $(LIBFT_PATH) -L/usr/local/lib -I/usr/local/include -lreadline
@$(CC) $(FLAGS) -o $(NAME) $(MINISHELL_OBJECTS) $(LIBFT_PATH) -L/usr/local/lib -I/usr/local/include -lreadline
@sleep 0.2
@echo "$(CLEAN_CAR)$(GREEN_COLOR)Minishell Compiled!$(NO_COLOR)"

%.o: %.c
$(CC) $(FLAGS) -c $< -o $@
@printf "$(CLEAN_CAR)$(GREEN_COLOR)[minishell Compiling]$(BLUE_COLOR) : $(PURPLE_COLOR)$<$(NO_COLOR)\n"
@$(CC) $(FLAGS) -c $< -o $@


clean:
rm -f $(MINISHELL_OBJECTS)
make -f libft/Makefile clean
Expand All @@ -42,3 +46,17 @@ fclean: clean
rm -f $(NAME) $(TEST_PATH)/tests $(LIBFT_PATH)

re: fclean all

.PHONY: all clean fclean re

NO_COLOR = \x1b[0m
GREEN_COLOR = \x1b[32;01m
RED_COLOR = \x1b[31;01m
PURPLE_COLOR = \x1b[35m
BLUE_COLOR = \x1b[34;01m
CLEAN_CAR = \033[2K\r


OK_STRING = $(GREEN_COLOR)[OK]$(NO_COLOR)
ERROR_STRING = $(RED_COLOR)[ERRORS]$(NO_COLOR)
WARN_STRING = $(PURPLE_COLOR)[WARNINGS]$(NO_COLOR)
55 changes: 55 additions & 0 deletions builtin/builtin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:04:26 by facetint #+# #+# */
/* Updated: 2024/03/03 13:04:27 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

#include <stdio.h>
#include "../includes/minishell.h"
#include "../libft/libft.h"
#include "../memory-allocator/allocator.h"
#include "../includes/env.h"


int isbuiltin(char *cmd)
{
if (ft_strncmp(cmd, "echo", ft_strlen("echo")) == 0)
return (1);
if (ft_strncmp(cmd, "export", ft_strlen("export")) == 0)
return (1);
if (ft_strncmp(cmd, "env", ft_strlen("env")) == 0)
return(1);
if (ft_strncmp(cmd, "exit", ft_strlen("exit")) == 0)
return (1);
if (ft_strncmp(cmd, "pwd", ft_strlen("pwd")) == 0)
return (1);
if (ft_strncmp(cmd, "cd", ft_strlen("cd")) == 0)
return(1);
if (ft_strncmp(cmd, "unset", ft_strlen("unset")) == 0)
return(1);
return (0);
}

void handle_builtin(t_command *cmd, int fd[2])
{
if (ft_strncmp(cmd->name, "echo", ft_strlen("echo")) == 0)
builtin_echo(cmd, fd);
else if (ft_strncmp(cmd->name, "export", ft_strlen("export")) == 0)
builtin_export(cmd, fd);
else if (ft_strncmp(cmd->name, "env", ft_strlen("env")) == 0)
builtin_env(get_global_env());
else if (ft_strncmp(cmd->name, "exit", ft_strlen("exit")) == 0)
builtin_exit(cmd);
else if (ft_strncmp(cmd->name, "pwd", ft_strlen("pwd")) == 0)
builtin_pwd(cmd);
if (ft_strncmp(cmd->name, "cd", ft_strlen("cd")) == 0)
return;
if (ft_strncmp(cmd->name, "unset", ft_strlen("unset")) == 0)
return;
}
28 changes: 28 additions & 0 deletions builtin/cd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hcoskun <hcoskun@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
<<<<<<< HEAD:builtin/cd.c
/* Created: 2024/03/03 11:48:16 by facetint #+# #+# */
/* Updated: 2024/03/03 11:48:37 by facetint ### ########.fr */
=======
/* Created: 2023/06/24 20:38:03 by hcoskun42 #+# #+# */
/* Updated: 2023/07/08 14:34:36 by hcoskun ### ########.fr */
>>>>>>> main:libft/ft_putchar_fd.c
/* */
/* ************************************************************************** */

#include "../includes/minishell.h"
#include "../libft/libft.h"
#include <stdio.h>

<<<<<<< HEAD:builtin/cd.c
=======
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
}
>>>>>>> main:libft/ft_putchar_fd.c
59 changes: 59 additions & 0 deletions builtin/echo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* echo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 11:48:26 by facetint #+# #+# */
/* Updated: 2024/03/03 11:48:27 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/minishell.h"
#include "../libft/libft.h"
#include <stdio.h>

int is_n_option(char *str)
{
int i;
int len;

if (!str)
return 0;
len = ft_strlen(str);
if (len < 2)
return 0;
if (str[0] != '-')
return 0;
i = 1;
while (str[i] == 'n')
i++;
if (i == len)
return 1;
return 0;
}

void builtin_echo(t_command *cmd, int fd[2])
{
char **args;
int i;
int n_option;

args = cmd->args;
i = 0;
n_option = 0;
while (is_n_option(args[i]))
i++;
if (i > 0)
n_option = 1;
while (args[i])
{
ft_putstr_fd(args[i], fd[1]);
if (args[i + 1] != NULL)
ft_putstr_fd(" ", fd[1]);
i++;
}
if (!n_option)
ft_putstr_fd("\n", fd[1]);
}
28 changes: 28 additions & 0 deletions builtin/env.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/02 20:06:35 by facetint #+# #+# */
/* Updated: 2024/03/02 20:14:21 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/minishell.h"
#include "../libft/libft.h"
#include "../includes/env.h"
#include <stdio.h>

void builtin_env(t_envList *env)
{
t_envList *lst;

lst = env;
while (lst)
{
printf("%s=%s\n", lst->key, lst->value);
lst = lst->next;
}
}
58 changes: 58 additions & 0 deletions builtin/exit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/03 13:04:14 by facetint #+# #+# */
/* Updated: 2024/03/03 14:11:41 by facetint ### ########.fr */
/* */
/* ************************************************************************** */

#include "../includes/minishell.h"
#include <stdio.h>
#include "../memory-allocator/allocator.h"
#include <unistd.h>
#include <stdlib.h>
#include "../libft/libft.h"
#include "stdbool.h"

static bool is_numeric(char *s)
{
while ((9 <= *s && *s <= 13) || *s == 32)
s++;
if (*s == 43 || *s == 45)
s++;
while (48 <= *s && *s <= 57)
s++;
while ((9 <= *s && *s <= 13) || *s == 32)
s++;
if (*s != '\0')
return (false);
return (true);
}
void builtin_exit(t_command *cmd)
{
bool status;
int exit_value;

exit_value = 0;
if (!(cmd->args[0]))
exit(EXIT_SUCCESS);
status = is_numeric((cmd->args[0]));
if (status == false)
{
printf("bash: exit: %s: numeric argument required\n",
cmd->args[0]);
exit_value = 255;
}
else if (status == true && !(cmd->args[1]))
exit_value = ft_atoi((cmd->args[0]));
else
{
printf("bash: exit: too many arguments\n");
return ;
}
exit(exit_value);
}
54 changes: 54 additions & 0 deletions builtin/export.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hcoskun <hcoskun@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
<<<<<<< HEAD:builtin/export.c
/* Created: 2024/03/02 14:15:14 by facetint #+# #+# */
/* Updated: 2024/03/03 11:39:15 by facetint ### ########.fr */
=======
/* Created: 2023/07/08 16:18:16 by hcoskun #+# #+# */
/* Updated: 2023/07/11 14:47:49 by hcoskun ### ########.fr */
>>>>>>> main:libft/ft_lstadd_back.c
/* */
/* ************************************************************************** */

#include "../includes/minishell.h"
#include "../includes/env.h"
#include <unistd.h>
#include "../libft/libft.h"

void builtin_export(t_command *cmd, int fd[2])
{
<<<<<<< HEAD:builtin/export.c
(void)cmd;
t_envList *tmp = get_global_env();
while (tmp)
{
write(fd[1], "declare -x ", 12);
write(fd[1], tmp->key, ft_strlen(tmp->key));
write(fd[1], "=", 1);
write(fd[1], tmp->value, ft_strlen(tmp->value));
write(fd[1], "\n", 1);
tmp = tmp->next;
}

}
=======
t_list *last_node;

if (!lst)
return ;
if (!*lst)
{
*lst = new;
return ;
}
last_node = ft_lstlast(*lst);
if (!last_node)
return ;
last_node -> next = new;
}
>>>>>>> main:libft/ft_lstadd_back.c
Loading

0 comments on commit 39ccb5c

Please sign in to comment.