Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
# By: ttsubo <[email protected]> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2025/04/03 12:55:20 by ttsubo #+# #+# #
# Updated: 2025/04/24 15:32:30 by ttsubo ### ########.fr #
# Updated: 2025/04/25 18:21:35 by ttsubo ### ########.fr #
# #
# **************************************************************************** #

NAME = minishell
CC = cc

SRC_DIR = src/
PARSER_SRC_DIR = src/parser/
TOKENIZER_SRC_DIR = src/tokenizer/
PERSER_SRC_DIR = src/perser/
INVOKE_CMD_SRC_DIR = src/invoke_cmd/
BUILTIN_SRC_DIR = src/builtin/
INC_DIR = inc/
OBJ_DIR = obj/
Expand All @@ -26,20 +27,24 @@ W_FLG = -Wall -Wextra -Werror
I_FLG = -I$(INC_DIR) -I$(FT_DIR)
L_FLG = -lreadline -lft

SRC = main.c minish_signal.c initialize.c debug.c
SRC = main.c minish_signal.c initialize.c
INVOKE_CMD_SRC = create_envp.c exec_pipeline.c execute_cmd.c \
execute_cmd_helper.c invoke_command.c pipeline_helper.c
TOKENIZER_SRC = tokenizer.c tokenizer_error.c read_token.c \
is_quote_closed.c get_token_capa.c is_redirect_validate.c
PERSER_SRC = allocate_cmds.c perser.c perser_utils.c setup_cmds.c
PARSER_SRC = allocate_cmds.c parser.c parser_utils.c setup_cmds.c
BUILTIN_SRC = cd.c exit.c pwd.c echo.c env.c unset.c \
env_utils.c env_utils_2.c builtin_utils.c

SRCS = $(addprefix $(SRC_DIR), $(SRC))
SRCS += $(addprefix $(PARSER_SRC_DIR), $(PARSER_SRC))
SRCS += $(addprefix $(TOKENIZER_SRC_DIR), $(TOKENIZER_SRC))
SRCS += $(addprefix $(PERSER_SRC_DIR), $(PERSER_SRC))
SRCS += $(addprefix $(INVOKE_CMD_SRC_DIR), $(INVOKE_CMD_SRC))
SRCS += $(addprefix $(BUILTIN_SRC_DIR), $(BUILTIN_SRC))
OBJS = $(addprefix $(OBJ_DIR), $(SRC:.c=.o))
OBJS += $(addprefix $(OBJ_DIR), $(PARSER_SRC:.c=.o))
OBJS += $(addprefix $(OBJ_DIR), $(TOKENIZER_SRC:.c=.o))
OBJS += $(addprefix $(OBJ_DIR), $(PERSER_SRC:.c=.o))
OBJS += $(addprefix $(OBJ_DIR), $(INVOKE_CMD_SRC:.c=.o))
OBJS += $(addprefix $(OBJ_DIR), $(BUILTIN_SRC:.c=.o))

LIBFT=libft.a
Expand All @@ -55,10 +60,13 @@ $(FT_DIR)$(LIBFT):
$(OBJ_DIR)%.o: $(SRC_DIR)%.c
$(CC) $(W_FLG) $(I_FLG) -c $< -o $@

$(OBJ_DIR)%.o: $(PARSER_SRC_DIR)%.c
$(CC) $(W_FLG) $(I_FLG) -c $< -o $@

$(OBJ_DIR)%.o: $(TOKENIZER_SRC_DIR)%.c
$(CC) $(W_FLG) $(I_FLG) -c $< -o $@

$(OBJ_DIR)%.o: $(PERSER_SRC_DIR)%.c
$(OBJ_DIR)%.o: $(INVOKE_CMD_SRC_DIR)%.c
$(CC) $(W_FLG) $(I_FLG) -c $< -o $@

$(OBJ_DIR)%.o: $(BUILTIN_SRC_DIR)%.c
Expand Down
20 changes: 20 additions & 0 deletions inc/create_envp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_envp.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:58:03 by dayano #+# #+# */
/* Updated: 2025/04/24 13:59:24 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef CREATE_ENVP_H
# define CREATE_ENVP_H

# include "struct.h"

char **create_envp(t_minish *minish);

#endif
21 changes: 21 additions & 0 deletions inc/exec_pipeline.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_pipeline.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 13:12:27 by dayano #+# #+# */
/* Updated: 2025/04/24 14:17:43 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef EXEC_PIPELINE_H
# define EXEC_PIPELINE_H

# include "cmd.h"

void exec_pipeline(t_cmd *cmd_head, t_minish *minish);
int wait_pipeline(t_cmd *cmd_head);

#endif
28 changes: 28 additions & 0 deletions inc/execute_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* execute_cmd.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 21:39:18 by dayano #+# #+# */
/* Updated: 2025/04/25 20:46:25 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef EXECUTE_CMD_H
# define EXECUTE_CMD_H

# define PATH_PREFIX_LEN 5
# define EQUAL_LEN 1
# define CMD_NOT_FOUND_EXIT_STATUS 127

void execute_cmd(t_cmd *cmd, t_minish *minish);

// execute_cmd_helper.c
char *join_path(char *dir, char *cmd);
char *get_path_line(char **envp);
void free_str_array(char **str);
void print_cmd_not_found(t_cmd *cmd);

#endif
6 changes: 3 additions & 3 deletions inc/invoke_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* invoke_command.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <ttsubo@student.42.fr> +#+ +:+ +#+ */
/* By: dayano <dayano@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/20 21:21:13 by dayano #+# #+# */
/* Updated: 2025/04/21 13:51:41 by ttsubo ### ########.fr */
/* Updated: 2025/04/21 21:00:48 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -15,6 +15,6 @@

# include "cmd.h"

int invoke_commands(t_cmd *cmd_head);
int invoke_commands(t_cmd *cmd_head, t_minish *minish);

#endif
8 changes: 6 additions & 2 deletions inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 12:50:38 by ttsubo #+# #+# */
/* Updated: 2025/04/24 15:28:00 by ttsubo ### ########.fr */
/* Updated: 2025/04/25 18:16:36 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,15 +16,19 @@
# include "builtin.h"
# include "builtin_utils.h"
# include "cmd.h"
# include "create_envp.h"
# include "env_utils.h"
# include "env_utils_2.h"
# include "exec_pipeline.h"
# include "execute_cmd.h"
# include "initialize.h"
# include "invoke_command.h"
# include "libft.h"
# include "minish_signal.h"
# include "parser.h"
# include "pipeline_helper.h"
# include "struct.h"
# include "tokenizer.h"
# include "perser.h"
# include <readline/history.h>
# include <readline/readline.h>
# include <stdbool.h>
Expand Down
10 changes: 5 additions & 5 deletions inc/perser.h → inc/parser.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* perser.h :+: :+: :+: */
/* parser.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ttsubo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/21 14:14:59 by ttsubo #+# #+# */
/* Updated: 2025/04/24 15:22:00 by ttsubo ### ########.fr */
/* Updated: 2025/04/25 18:16:23 by ttsubo ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PERSER_H
# define PERSER_H
#ifndef PARSER_H
# define PARSER_H

# include "cmd.h"
# include "libft.h"

t_cmd **perser(char **tokens);
t_cmd **parser(char **tokens);
size_t cmds_len(t_cmd **cmds);
int is_separator(char *token);
void free_cmds(t_cmd **cmds, size_t count);
Expand Down
26 changes: 26 additions & 0 deletions inc/pipeline_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipeline_helper.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 14:32:27 by dayano #+# #+# */
/* Updated: 2025/04/24 15:02:17 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef PIPELINE_HELPER_H
# define PIPELINE_HELPER_H

# include "cmd.h"
# include <stdbool.h>

// pipeline_helper.c
void redirect_stdout(t_cmd *cmd);
bool is_redirect(t_cmd *cmd);
bool is_head(t_cmd *cmd);
bool is_tail(t_cmd *cmd);
bool is_builtin(t_cmd *cmd);

#endif
98 changes: 98 additions & 0 deletions src/invoke_cmd/create_envp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_envp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dayano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/24 13:56:59 by dayano #+# #+# */
/* Updated: 2025/04/25 21:22:44 by dayano ### ########.fr */
/* */
/* ************************************************************************** */

#include "main.h"

static void free_envp(char **envp)
{
int i;

i = 0;
if (!envp)
return ;
while (envp[i])
{
if (envp[i])
free(envp[i]);
i++;
}
free(envp);
}

static int _count_value(t_minish *minish)
{
int count;
t_env *current_env;

count = 0;
current_env = minish->env;
while (current_env)
{
if (current_env->value)
count++;
current_env = current_env->next;
}
return (count);
}

static char *_get_envp_key_value(t_env *env)
{
char *result;
int key_len;
int value_len;
int buf_size;

key_len = ft_strlen(env->key);
value_len = ft_strlen(env->value);
buf_size = key_len + EQUAL_LEN + value_len + 1;
result = (char *)ft_calloc(buf_size, sizeof(char));
if (!result)
return (NULL);
ft_strlcpy(result, env->key, buf_size);
ft_strlcat(result, "=", buf_size);
ft_strlcat(result, env->value, buf_size);
return (result);
}

/**
* @brief Create envp array
*
* @param minish
* @return char**
*/
char **create_envp(t_minish *minish)
{
char **envp;
t_env *env;
int i;
int count;

count = _count_value(minish);
envp = (char **)ft_calloc(count, sizeof(char *));
if (!envp)
return (NULL);
env = minish->env;
i = 0;
while (env)
{
if (env->value)
{
envp[i] = _get_envp_key_value(env);
if (!envp[i])
return (free_envp(envp), NULL);
}
env = env->next;
i++;
}
envp[i] = NULL;
return (envp);
}
Loading