70 Commits
bonus ... main

Author SHA1 Message Date
12dc548a4e Update README.md 2024-10-17 07:34:10 +02:00
bd18752fdc Create README.md 2024-10-17 07:33:46 +02:00
ded1b802b2 kill me 2024-05-22 13:53:49 +02:00
1731f820ee Merge branch 'main' of github.com:mdev9/minishell 2024-05-16 13:22:35 +02:00
b8fade86a0 the most important thing to fix 2024-05-16 13:22:28 +02:00
6d40826c51 fixed big error 2024-05-16 13:21:04 +02:00
f5532679bc fixed minishell 2024-05-16 13:14:30 +02:00
2c8feb6c18 removed useless file 2024-05-15 17:21:53 +02:00
956aa0e1e7 norm 2024-05-08 12:32:20 +02:00
d1681dafe3 fix is_var 2024-05-08 11:57:49 +02:00
e986d8692b fix des trucs 2024-05-07 17:43:00 +02:00
01496f0587 fix cat < missing | cat mais ça a probablement cassé autre chose 2024-05-07 12:37:36 +02:00
e1831b18fa fix error code 2024-05-06 16:41:37 +02:00
0af303d895 fix infinite loop 2024-05-06 16:29:27 +02:00
3d2adaf520 fix norme 2024-05-06 16:17:54 +02:00
923da71a49 AAAAAAAAAAA 2024-05-06 15:23:30 +02:00
444b84c541 fixed hell 2024-05-06 14:29:15 +02:00
855ba3f6f2 fixed stuff 2024-05-06 14:18:40 +02:00
60d6e50ab9 fix un truc 2024-05-06 13:42:47 +02:00
231f1a8002 updated tood 2024-05-06 13:20:23 +02:00
398e4b5928 fix too long path 2024-05-06 13:19:17 +02:00
b65c07d430 fix infinit loop when redirection to ( 2024-05-06 11:15:44 +02:00
ab5ef04911 c'est mieux quand ça compile 2024-05-06 11:00:51 +02:00
6cfc2beff4 norme 2024-05-06 10:58:42 +02:00
57ad83c2aa fix "$" 2024-05-06 10:56:13 +02:00
8513bd3bdd merge avec moi meme 2024-05-06 10:29:41 +02:00
d3e5d459cb fix fds ouverts 2024-05-06 10:28:30 +02:00
28efa75b39 fix autre invalid read 2024-05-05 20:38:05 +02:00
43e4d054b5 fix exit when variable on input redirection and invalid read 2024-05-05 20:26:52 +02:00
b45a04e622 fix exit quand output ambiguous redirect 2024-05-05 20:07:53 +02:00
257e4829ba j'ai remis le is_var, je sais pas a quoi il sert mais mtn il marche (peut-être) 2024-05-04 18:32:37 +02:00
ef3915c640 AAAAAAAAAAAAAAAAAAAAAAAAAAA j'ai pas les variables d'environnement 2024-05-04 14:33:30 +02:00
fb0ebc2398 fix unset all -> export a 2024-05-03 15:06:27 +02:00
b7658d02ec fix export a= ->a="" 2024-05-03 14:30:50 +02:00
bdfff1360a fixed some errors 2024-05-03 14:14:12 +02:00
032bfb976e fix des trucs 2024-05-03 13:03:41 +02:00
2d5449ed3d fix $""coucou 2024-05-03 12:56:07 +02:00
6e93081fcb j'aime pas minishell 2024-05-03 11:18:49 +02:00
2d11d1493a Merge branch 'main' of github.com:mdev9/minishell 2024-05-03 08:49:16 +02:00
b1c3e53509 fix leak 2024-05-03 08:49:04 +02:00
9e2f926ca8 fixed command not found stopping execution 2024-05-03 08:42:53 +02:00
e75099562e fix leak when (()) 2024-05-03 08:14:33 +02:00
f661232c77 fixed all problems 2024-04-30 14:50:58 +02:00
f67d334503 fixed closing all pipe fds 2024-04-30 14:05:25 +02:00
df6bef58dc todo 2024-04-29 22:17:20 +02:00
992f02cded fix quelques trucs 2024-04-29 22:12:46 +02:00
0bad8c3cdb fix echo 2024-04-29 21:43:50 +02:00
245b7b2526 todo 2024-04-29 19:41:15 +02:00
efd8838ee0 free trop tot quand il y a une syntax error 2024-04-29 13:22:18 +02:00
03e108b625 rl_clear_history() 2024-04-29 13:01:14 +02:00
d05e304475 added libft 2024-04-29 12:38:11 +02:00
e9f9ede1f1 fixed wildcards and makefile 2024-04-29 11:15:54 +02:00
bb0a62342e ambigous redirect stops execution 2024-04-28 14:54:32 +02:00
97d5c24662 yes 2024-04-26 19:00:57 +02:00
56ff115753 fixed wildcards 2024-04-26 17:41:04 +02:00
a7f8b264b7 rm bonus 2024-04-26 16:08:33 +02:00
6c58f1d26c fix export ="" 2024-04-26 16:07:25 +02:00
ee49511eaf fixed header 2024-04-26 15:50:46 +02:00
344c40df46 fixed 2024-04-26 15:47:34 +02:00
2290d638b6 fixed wildcards 2024-04-26 15:45:28 +02:00
6012037886 wildcards 2024-04-26 15:34:05 +02:00
5eff48d14b invalid free 2024-04-26 15:28:08 +02:00
8a6378aa9c val.supp 2024-04-26 15:24:19 +02:00
1c8694e688 Merge branch 'main' of github.com:mdev9/minishell 2024-04-26 15:08:01 +02:00
69467e0446 tres important 2024-04-26 15:07:50 +02:00
e7ab260a3b norme 2024-04-26 14:52:59 +02:00
744ab7728d AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA 2024-04-26 14:46:08 +02:00
f3d01b28f0 Merge branch 'main' of github.com:mdev9/minishell 2024-04-26 13:47:34 +02:00
517f94fe48 () (()) 2024-04-26 13:47:08 +02:00
7b5df4fa3a fixed some minor issues 2024-04-26 13:27:27 +02:00
38 changed files with 531 additions and 406 deletions

62
.gitignore vendored
View File

@ -1,62 +0,0 @@
# Prerequisites
*.d
.tonotdo
minishell
minishell_bonus
.minishellrc
objs/
.tmp*
out
minishell_tester/
# Object files
*.o
*.ko
*.obj
*.elf
# Linker output
*.ilk
*.map
*.exp
# Precompiled Headers
*.gch
*.pch
# Libraries
*.lib
*.a
*.la
*.lo
# Shared objects (inc. Windows DLLs)
*.dll
*.so
*.so.*
*.dylib
# Executables
*.exe
*.out
*.app
*.i*86
*.x86_64
*.hex
# Debug files
*.dSYM/
*.su
*.idb
*.pdb
# Kernel Module Compile Results
*.mod*
*.cmd
.tmp_versions/
modules.order
Module.symvers
Mkfile.old
dkms.conf

View File

@ -6,7 +6,7 @@
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/04/26 10:52:53 by marde-vr ### ########.fr #
# Updated: 2024/05/22 13:50:22 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -22,7 +22,6 @@ SRCS_RAW = main.c\
pwd.c\
env.c\
parsing.c\
debug.c\
env_utils.c\
parsing_var.c\
path.c\
@ -30,9 +29,11 @@ SRCS_RAW = main.c\
here_doc_utils.c\
export.c\
input_redirections.c\
redirection_utils.c\
sort_wildcard_list.c\
output_redirections.c\
builtins.c\
variable_expantion.c\
wildcards.c\
commands.c\
pipe.c\
@ -45,6 +46,7 @@ SRCS_RAW = main.c\
get_len_bonus.c\
check_syntax.c\
check_syntax_utils.c\
parsing_utils.c\
unset.c\
free.c
@ -89,13 +91,14 @@ clean:
fi
bonus: all
@$(CC) $(FLAGS) $(OBJS) $(LIBFT) -lreadline -o $(NAME)_bonus
install: $(NAME)
cp $(NAME) ~/.local/bin/msh
cp $(NAME) ~/.local/bin/minishell
fclean: clean
rm -f $(NAME)
rm -f $(NAME) $(NAME)_bonus
re: fclean all

44
README.md Normal file
View File

@ -0,0 +1,44 @@
# 🐚 minishell Custom UNIX Shell 💻
Welcome to **minishell**, a project that implements a **custom UNIX shell** from the ground up! 🌟 This shell allows for command execution, pipe management, and process handling. 🌐
## 📝 Project Overview
In this project, we create a fully functional **UNIX shell** that supports various commands, piping between processes, and robust error handling. The shell is designed to handle signals 📡 and fork processes efficiently 🍴, simulating the behavior of bash and other traditional UNIX shells. 🚀
### Features:
- 🔹 **Command execution**: Run shell commands with ease.
- 🔹 **Pipe management**: Link commands together for streamlined output processing.
- 🔹 **Signal handling**: Manage signals effectively within the shell environment.
- 🔹 **Process forking**: Create child processes to execute commands.
- 🔹 **Error handling**: Robust mechanisms to handle errors gracefully.
## 🛠️ Installation & Setup
To get started, follow these steps to set up and run the project:
1. **Clone the repository**:
```bash
git clone https://github.com/mdev9/minishell.git
cd minishell
make
```
2. **Run the shell**:
```bash
./minishell
```
## 🔍 Technologies Used
- C 💻: Primary language used to implement the shell.
- System Calls 📞: Leveraging UNIX system calls for process management and execution.
- Pipes 🚰: Implementing inter-process communication through pipes.
- Signals ⚡: Handling OS signals for effective process management.
## 🎯 Goals of the Project
- Create a functional UNIX shell that mimics common shell behaviors.
- Understand key operating system concepts such as process management and system calls.
- Enhance programming skills in C through practical application.
Thank you for checking out minishell! 🌟 Happy coding! 💻

View File

@ -6,7 +6,7 @@
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/04/02 16:02:33 by tomoron ### ########.fr #
# Updated: 2024/04/29 11:13:55 by marde-vr ### ########.fr #
# #
# **************************************************************************** #
@ -52,7 +52,8 @@ SRCS = ft_atoi.c\
ft_split_set.c\
ft_free_str_arr.c\
ft_set_color.c\
ft_isspace.c
ft_isspace.c\
ft_str_is_only_char.c\
SRCS_BONUS = ft_lstnew.c\
ft_lstadd_front.c\

View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_str_is_only_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 11:11:31 by marde-vr #+# #+# */
/* Updated: 2024/04/29 11:11:57 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
int ft_str_is_only_char(char *str, char c)
{
while (c && *str == c)
str++;
if (!*str && c)
return (1);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 16:55:48 by tomoron #+# #+# */
/* Updated: 2024/02/11 17:41:32 by tomoron ### ########.fr */
/* Updated: 2024/04/29 11:12:16 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -46,6 +46,7 @@ void ft_lstadd_front(t_list **lst, t_list *new);
char **ft_split(char const *str, char charset);
void ft_lstadd_back(t_list **lst, t_list *new);
char **ft_split_set(char *str, char *charset);
int ft_str_is_only_char(char *str, char c);
void *ft_memset(void *s, int c, size_t n);
char *ft_get_color(int r, int g, int b);
void *ft_calloc(size_t nb, size_t size);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */
/* Updated: 2024/04/23 19:56:00 by tomoron ### ########.fr */
/* Updated: 2024/04/29 21:47:25 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,10 +14,10 @@
int cmd_is_forkable_builtin(char *cmd_token)
{
if (!ft_strcmp(cmd_token, "echo") || !ft_strcmp(cmd_token, "ret")
|| !ft_strcmp(cmd_token, "env") || !ft_strcmp(cmd_token, "exit")
|| !ft_strcmp(cmd_token, "pwd") || !ft_strcmp(cmd_token, "cd")
|| !ft_strcmp(cmd_token, "export") || !ft_strcmp(cmd_token, "unset"))
if (!ft_strcmp(cmd_token, "echo") || !ft_strcmp(cmd_token, "env")
|| !ft_strcmp(cmd_token, "exit") || !ft_strcmp(cmd_token, "pwd")
|| !ft_strcmp(cmd_token, "cd") || !ft_strcmp(cmd_token, "export")
|| !ft_strcmp(cmd_token, "unset"))
return (1);
return (0);
}
@ -48,8 +48,6 @@ int exec_builtin(t_msh *msh)
return (0);
if (!ft_strcmp(msh->tokens->value, "echo"))
g_return_code = echo(msh->tokens->next);
else if (!ft_strcmp(msh->tokens->value, "ret"))
g_return_code = ft_atoi(msh->tokens->next->value);
else if (!ft_strcmp(msh->tokens->value, "env"))
g_return_code = print_env(msh->env);
else if (!ft_strcmp(msh->tokens->value, "exit"))

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
/* Updated: 2024/04/24 10:43:52 by marde-vr ### ########.fr */
/* Updated: 2024/05/03 08:39:33 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,13 +23,12 @@ void cd_update_pwd(t_msh *msh)
pwd = ft_get_env(msh->env, "PWD");
pwd = ft_strdup(pwd);
if (!pwd)
ft_strdup("");
pwd = ft_strdup("");
if (!pwd)
free(new);
if (!pwd)
return ;
msh->env = export_set_env(msh->env, ft_strdup("OLDPWD"), pwd, 0);
if (ft_get_env(msh->env, "PWD"))
msh->env = export_set_env(msh->env, ft_strdup("PWD"), new, 0);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 14:50:15 by tomoron #+# #+# */
/* Updated: 2024/04/25 13:50:46 by tomoron ### ########.fr */
/* Updated: 2024/04/26 14:39:48 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,7 +32,7 @@ int check_parens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
}
tmp = check_cmds_syntax(parsed_cmd, env);
if (tmp)
print_syntax_error_bonus(tmp);
print_syntax_error_bonus(tmp, 0);
free_cmd(parsed_cmd);
return (tmp == 0);
}

View File

@ -6,16 +6,19 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 14:54:53 by tomoron #+# #+# */
/* Updated: 2024/04/25 13:51:03 by tomoron ### ########.fr */
/* Updated: 2024/05/03 08:13:45 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void print_syntax_error_bonus(t_cmd *cmd)
void print_syntax_error_bonus(t_cmd *cmd, t_cmd *cmds)
{
if (cmd->cmd_type == CMD || cmd->cmd_type == PAREN)
{
free_cmd(cmds);
return ;
}
ft_printf_fd(2, "minishell : syntax error near unexpected token `");
g_return_code = 2;
if (cmd->cmd_type == AND)
@ -27,6 +30,7 @@ void print_syntax_error_bonus(t_cmd *cmd)
if (cmd->cmd_type == ERR)
ft_printf_fd(2, "&");
ft_printf_fd(2, "'\n");
free_cmd(cmds);
}
int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)

View File

@ -1,66 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* debug.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 15:46:50 by tomoron #+# #+# */
/* Updated: 2024/04/18 20:48:51 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void print_parsed_token(t_token *token)
{
while (token)
{
printf("[ARG : \"%s\"]", token->value);
token = token->next;
}
printf("\n");
}
void print_cmd_type(t_cmd_type type, char *value)
{
if (type == CMD)
printf("[CMD : %s] ", value);
if (type == PAREN)
printf("[PAREN : %s] ", value);
if (type == AND)
printf("[AND] ");
if (type == OR)
printf("[OR] ");
if (type == PIPE)
printf("[PIPE] ");
if (type == RED_O_APP)
printf("[RED_O_APP : %s] ", value);
if (type == RED_O)
printf("[RED_O : %s] ", value);
if (type == RED_I)
printf("[RED_I : %s] ", value);
if (type == HERE_DOC)
printf("[HERE_DOC : %s] ", value);
}
void print_parsed_cmd(t_cmd *cmd)
{
while (cmd)
{
print_cmd_type(cmd->cmd_type, cmd->value);
cmd = cmd->next;
}
printf("\n");
}
void print_msh_struct(t_msh *msh)
{
printf("in_fd : %d\n", msh->in_fd);
printf("out_fd : %d\n", msh->out_fd);
printf("in_type: ");
print_cmd_type(msh->in_type, 0);
printf("\nout_type: ");
print_cmd_type(msh->out_type, 0);
printf("\n");
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
/* Updated: 2024/04/24 14:52:21 by marde-vr ### ########.fr */
/* Updated: 2024/04/29 22:11:06 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -34,15 +34,15 @@ int echo(t_token *args)
int i;
put_nl = 1;
i = 1;
while (args && args->value && args->value[0] == '-')
{
i = 1;
while (args->value[i] == 'n')
i++;
if (!args->value[i] && i > 1)
put_nl = 0;
else
ft_printf("%s ", args->value);
break ;
args = args->next;
}
put_args(args);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 10:58:36 by marde-vr #+# #+# */
/* Updated: 2024/04/26 10:26:03 by tomoron ### ########.fr */
/* Updated: 2024/05/03 14:25:03 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,7 +19,7 @@ t_env *dup_env(t_env *env)
res = 0;
while (env)
{
res = env_add_back(res, env->name, env->value);
res = env_add_back(res, env->name, env->value, 0);
env = env->next;
}
return (res);
@ -67,7 +67,7 @@ void print_env_declare(t_msh *msh, t_env *env_orig)
{
if (strcmp(env->name, "_"))
{
if (env->value && *(env->value))
if (env->value)
ft_printf_fd(msh->out_fd, "declare -x %s=\"%s\"\n", env->name,
env->value);
else

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 13:30:18 by tomoron #+# #+# */
/* Updated: 2024/04/18 20:48:52 by marde-vr ### ########.fr */
/* Updated: 2024/05/16 13:18:46 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -51,7 +51,7 @@ char **split_paths_from_env(t_env *env)
path_in_envp = 0;
cur_env_var = env;
while (cur_env_var && cur_env_var->next != 0)
while (cur_env_var)
{
if (!ft_strcmp(cur_env_var->name, "PATH"))
{

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/26 11:08:13 by tomoron ### ########.fr */
/* Updated: 2024/05/07 17:26:49 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,11 +20,11 @@ void exec_command_bonus(t_msh *msh, char *cmd_str)
if (!cmd_str)
return ;
cmds = parsing_bonus(cmd_str);
free(cmd_str);
tmp = check_cmds_syntax(cmds, msh->env);
if (tmp)
{
print_syntax_error_bonus(tmp);
free_cmd(cmds);
print_syntax_error_bonus(tmp, cmds);
return ;
}
msh->cmds_head = cmds;
@ -73,16 +73,26 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
void exec_command(t_msh *msh, int i, int cmd_count)
{
get_redirections(msh, msh->cmds);
g_return_code = 0;
if (msh->out_fd != -1 && msh->in_fd != -1)
if (msh->in_fd != -2)
{
msh->last_return_code = g_return_code;
g_return_code = 0;
msh->fds[i] = ft_calloc(2, sizeof(int *));
if (!msh->fds[i])
ft_exit(msh, 1);
if (msh->tokens && !cmd_is_builtin(msh, msh->tokens->value))
get_cmd_path(msh);
if ((msh->tokens && msh->tokens->value) || is_parenthesis(msh->cmds))
if (((msh->tokens && msh->tokens->value) || is_parenthesis(msh->cmds)))
exec(msh, get_cmd_args(msh), i, cmd_count);
else
{
if (msh->in_fd > 2)
close(msh->in_fd);
if (msh->out_fd > 2)
close(msh->out_fd);
if (!g_return_code)
g_return_code = 1;
}
}
remove_command_from_msh(msh);
}
@ -96,6 +106,7 @@ void end_execution(t_msh *msh, int cmd_count)
status = 0;
while (i < cmd_count)
waitpid(msh->pids[i++], &status, 0);
close_all_pipes(msh);
if (!g_return_code && WIFEXITED(status))
g_return_code = WEXITSTATUS(status);
if (WIFSIGNALED(status))
@ -121,8 +132,9 @@ void exec_commands(t_msh *msh)
if (!msh->tokens && !is_parenthesis(msh->cmds))
{
g_return_code = 0;
get_redirections(msh, msh->cmds);
g_return_code = (msh->in_fd < 0 || msh->out_fd < 0);
remove_here_doc_file(msh);
if (msh->in_fd > 2)
close(msh->in_fd);
if (msh->out_fd > 2)
@ -135,8 +147,9 @@ void exec_commands(t_msh *msh)
if (!msh->pids || !msh->fds)
ft_exit(msh, 1);
i = -1;
while (++i < cmd_count && msh->in_fd >= 0 && msh->out_fd >= 0)
while (++i < cmd_count && msh->in_fd != -2)
exec_command(msh, i, cmd_count);
free_token(msh->tokens);
msh->tokens = 0;
end_execution(msh, cmd_count);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 10:46:28 by marde-vr #+# #+# */
/* Updated: 2024/04/24 14:05:18 by tomoron ### ########.fr */
/* Updated: 2024/05/06 10:56:52 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,8 @@ void get_redirections(t_msh *msh, t_cmd *cmds)
{
msh->in_type = 0;
msh->out_type = 0;
msh->in_fd = 0;
msh->out_fd = 0;
if (first_is_in_type(cmds))
{
if (!get_in_type(msh, cmds, cmds, 1))
@ -48,17 +50,13 @@ int get_cmd_count(t_cmd *cmds)
{
int nb;
nb = 0;
nb = 1;
while (cmds && !is_operand_type(cmds))
{
while (cmds && (is_output_type(cmds) || is_input_type(cmds)))
cmds = cmds->next;
if (is_cmd_type(cmds))
nb++;
while (cmds && (is_output_type(cmds) || is_input_type(cmds)
|| is_cmd_type(cmds)))
cmds = cmds->next;
if (cmds && cmds->cmd_type == PIPE)
{
nb++;
}
cmds = cmds->next;
}
return (nb);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
/* Updated: 2024/04/26 10:49:17 by marde-vr ### ########.fr */
/* Updated: 2024/05/16 12:49:41 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,7 +24,7 @@ int is_too_big(char *num_str)
{
if ((strlen(num_str) == 19 && strcmp(num_str, "9223372036854775807") > 0)
|| (strlen(num_str) == 20 && num_str[0] == '-' && strcmp(num_str,
"-9223372036854775808") > 0) || strlen(num_str) > 20)
"-9223372036854775808") > 0) || strlen(num_str) >= 20)
return (1);
return (0);
}
@ -39,7 +39,7 @@ void get_exit_bt_return_code(t_msh *msh, int *exit_code)
else if (cur_cmd)
*exit_code = (unsigned char)ft_atoi(cur_cmd->value);
else
*exit_code = g_return_code;
*exit_code = msh->last_return_code;
}
int exit_bt(t_msh *msh)

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
/* Updated: 2024/04/26 10:52:17 by marde-vr ### ########.fr */
/* Updated: 2024/05/03 14:50:57 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@ int export_invalid_identifier(char *arg, char *name)
return (1);
}
t_env *set_env(t_env *env, char *name, char *value, int append)
t_env *set_env(t_env *env, char *name, char *value, int flags)
{
t_env *tmp;
@ -31,12 +31,12 @@ t_env *set_env(t_env *env, char *name, char *value, int append)
if (!ft_strcmp(name, tmp->name))
{
free(name);
if (!*value)
if (!*value && !(flags & 0b10))
{
free(value);
return (env);
}
if (append)
if ((flags & 0b1) && tmp->value)
value = ft_strjoin_free(tmp->value, value, 2);
if (!value)
return (env);
@ -46,10 +46,10 @@ t_env *set_env(t_env *env, char *name, char *value, int append)
}
tmp = tmp->next;
}
return (env_add_back(env, name, value));
return (env_add_back(env, name, value, !(flags & 0b10)));
}
t_env *export_set_env(t_env *env, char *name, char *value, int append)
t_env *export_set_env(t_env *env, char *name, char *value, int flags)
{
if (!value || !name)
{
@ -58,29 +58,34 @@ t_env *export_set_env(t_env *env, char *name, char *value, int append)
ft_printf_fd(2, "minishell: malloc failed");
return (env);
}
return (set_env(env, name, value, append));
return (set_env(env, name, value, flags));
}
int export_var(t_token *cmd, t_env *env)
int export_var(t_token *cmd, t_env **env)
{
char *arg;
char *name;
char *value;
int len;
int append;
int flags;
len = 0;
arg = cmd->value;
while (arg[len] && arg[len] != '=' && arg[len] != '+')
len++;
name = ft_substr(arg, 0, len);
append = arg[len] == '+';
if (arg[len] == '=' || (arg[len] == '+' && arg[len + 1] == '='))
len += 1 + (arg[len] == '+');
flags = arg[len] == '+';
if (arg[len] == '+' && arg[len + 1] == '=')
len++;
if (arg[len] == '=')
{
flags += 0b10;
len++;
}
if (!name || !check_var_name(name) || arg[len] == '+')
return (export_invalid_identifier(arg, name));
value = ft_strdup(arg + len);
env = export_set_env(env, name, value, append);
*env = export_set_env(*env, name, value, flags);
return (0);
}
@ -97,11 +102,12 @@ int ft_export(t_msh *msh, t_token *cmd, t_env *env)
cmd = cmd->next;
while (cmd->next)
{
if (export_var(cmd, env))
if (export_var(cmd, &env))
error = 1;
cmd = cmd->next;
}
if (export_var(cmd, env))
if (export_var(cmd, &env))
error = 1;
msh->env = env;
return (error);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 14:51:00 by tomoron #+# #+# */
/* Updated: 2024/04/24 14:59:47 by tomoron ### ########.fr */
/* Updated: 2024/05/06 11:14:51 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -43,7 +43,8 @@ int get_next_arg_len(char *cmd)
in_quote = 0;
in_dquote = 0;
while (cmd[len] && ((!ft_isspace(cmd[len]) && cmd[len] != '&'
&& cmd[len] != '|' && cmd[len] != '<' && cmd[len] != '>')
&& cmd[len] != '|' && cmd[len] != '<' && cmd[len] != '>'
&& cmd[len] != '(' && cmd[len] != ')')
|| in_quote || in_dquote))
{
if (cmd[len] == '\'' && !in_dquote)
@ -73,7 +74,7 @@ int get_parenthesis_cmd_len(char *cmd)
if (cmd[len] == '"' && !in_quote)
in_dquote = !in_dquote;
if ((cmd[len] == '(' || cmd[len] == ')') && !in_quote && !in_dquote)
parenthesis += 1 * (-(cmd[len] == ')'));
parenthesis += 1 - ((cmd[len] == ')') * 2);
len++;
}
return (len - 1);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/24 17:44:32 by marde-vr #+# #+# */
/* Updated: 2024/04/26 11:06:25 by tomoron ### ########.fr */
/* Updated: 2024/05/22 13:48:39 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ void get_here_doc_input(t_msh *msh, char *eof)
ft_printf_fd(2, " end-of-file, (wanted `%s')\n", eof);
break ;
}
if (line && !ft_strncmp(line, eof, ft_strlen(eof)))
if (line && !ft_strcmp(line, eof))
break ;
parse_var(msh, line);
write(msh->in_fd, "\n", 1);
@ -44,6 +44,7 @@ void here_doc_child(t_msh *msh, char *eof)
signal(SIGQUIT, SIG_IGN);
get_here_doc_input(msh, eof);
close(msh->in_fd);
close_all_pipes(msh);
ft_exit(msh, 0);
}
@ -57,11 +58,12 @@ void here_doc_signal(t_msh *msh, int child_pid, char *here_doc_file)
set_echoctl(msh->echoctl);
signal(SIGINT, signal_handler_interactive);
signal(SIGQUIT, signal_handler_interactive);
if (msh->in_fd > 2)
close(msh->in_fd);
if (WIFEXITED(status) && WEXITSTATUS(status))
{
unlink(here_doc_file);
msh->in_fd = -1;
msh->in_fd = -2;
g_return_code = 130;
return ;
}
@ -78,6 +80,7 @@ void handle_here_doc(t_msh *msh, char *eof)
here_doc_file = get_tmp_file_name(msh);
if (msh->here_doc_filename)
{
if (msh->in_fd)
close(msh->in_fd);
unlink(msh->here_doc_filename);
free(msh->here_doc_filename);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/26 09:06:51 by marde-vr #+# #+# */
/* Updated: 2024/04/18 20:48:54 by marde-vr ### ########.fr */
/* Updated: 2024/05/08 12:31:54 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -78,3 +78,13 @@ void parse_var(t_msh *msh, char *line)
line++;
}
}
void remove_here_doc_file(t_msh *msh)
{
if (msh->here_doc_filename)
{
unlink(msh->here_doc_filename);
free(msh->here_doc_filename);
msh->here_doc_filename = 0;
}
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
/* Updated: 2024/04/25 18:39:48 by tomoron ### ########.fr */
/* Updated: 2024/05/06 14:43:48 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -19,6 +19,7 @@ void redirect_input(t_msh *msh, int i, char **cmd_args)
if (dup2(msh->in_fd, 0) < 0)
{
free(cmd_args);
close_pipe_fds(msh, i);
ft_exit(msh, 1);
}
close(msh->in_fd);
@ -28,28 +29,23 @@ void redirect_input(t_msh *msh, int i, char **cmd_args)
if (dup2(msh->fds[i - 1][0], 0) < 0)
{
free(cmd_args);
close_pipe_fds(msh, i);
ft_exit(msh, 1);
}
}
}
void ambiguous_redirect(char *str, t_msh *msh)
{
ft_printf_fd(2, "minishell: %s: ambiguous redirect\n", str);
msh->in_fd = -1;
g_return_code = 1;
}
int open_input_file(t_msh *msh, t_cmd **cur_token)
int open_input_redirection_file(t_msh *msh, t_cmd **cur_token)
{
t_token *filename;
if ((*cur_token)->cmd_type == HERE_DOC)
handle_here_doc(msh, (*cur_token)->value);
if ((*cur_token)->cmd_type == RED_I)
{
if (msh->in_fd != 0)
if (msh->in_fd != 0 && msh->in_type != PIPE)
close(msh->in_fd);
if (ft_strchr((*cur_token)->value, '$'))
{
ambiguous_redirect((*cur_token)->value, msh);
return (1);
}
filename = parse_tokens((*cur_token)->value, msh->env);
if (!filename)
ft_exit(msh, 1);
@ -57,7 +53,7 @@ int open_input_file(t_msh *msh, t_cmd **cur_token)
ambiguous_redirect((*cur_token)->value, msh);
if (!filename->next)
msh->in_fd = open(filename->value, O_RDONLY);
if (msh->in_fd == -1)
if (msh->in_fd == -1 && !filename->next)
{
ft_printf_fd(2, "minishell: %s: ", filename->value);
perror("");
@ -65,8 +61,16 @@ int open_input_file(t_msh *msh, t_cmd **cur_token)
return (1);
}
free_token(filename);
}
return (msh->in_fd == -1);
return (msh->in_fd == -2);
}
int open_input_file(t_msh *msh, t_cmd **cur_token)
{
if ((*cur_token)->cmd_type == HERE_DOC)
handle_here_doc(msh, (*cur_token)->value);
if ((*cur_token)->cmd_type == RED_I)
return (open_input_redirection_file(msh, cur_token));
return (msh->in_fd == -2);
}
int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc)
@ -89,22 +93,11 @@ int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc)
if (open_input_file(msh, &cur_token))
return (1);
}
if (cur_token && cur_token->next && !is_operand_type(cur_token->next)
if (cur_token && cur_token->next
&& !is_operand_type(cur_token) && !is_operand_type(cur_token->next)
&& cur_token->cmd_type != PIPE && cur_token->next->cmd_type != PIPE)
return (get_in_type(msh, t_strt, cur_token->next, here_doc));
if (here_doc)
return (get_in_type(msh, t_strt, t_strt, 0));
return (0);
}
int first_is_in_type(t_cmd *cmd)
{
t_cmd *cur_token;
cur_token = cmd;
while (cur_token && cur_token->cmd_type == CMD && cur_token->next)
cur_token = cur_token->next;
if (is_input_type(cur_token) || cur_token->cmd_type == PIPE)
return (1);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/04/18 20:48:55 by marde-vr ### ########.fr */
/* Updated: 2024/04/26 13:45:08 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,7 +17,7 @@ t_cmd *cmd_add_back(t_cmd *cmd, char *value, t_cmd_type type)
t_cmd *res;
t_cmd *current;
if (value && !*value)
if (value && !*value && type != PAREN)
{
free(value);
return (cmd);

View File

@ -6,21 +6,24 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/04/25 18:53:51 by marde-vr ### ########.fr */
/* Updated: 2024/05/06 10:56:39 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_env *env_add_back(t_env *env, char *name, char *value)
t_env *env_add_back(t_env *env, char *name, char *value, int empty)
{
t_env *res;
t_env *current;
if (empty)
free(value);
res = ft_calloc(1, sizeof(t_env));
if (!res)
return (env);
res->name = name;
if (!empty)
res->value = value;
if (!env)
return (res);
@ -35,6 +38,7 @@ int print_env(t_env *env)
{
while (env)
{
if (env->value)
ft_printf("%s=%s\n", env->name, env->value);
env = env->next;
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
/* Updated: 2024/04/26 11:08:22 by tomoron ### ########.fr */
/* Updated: 2024/05/06 14:25:27 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,12 +31,13 @@ char *get_prompt(t_env *env)
res = ft_strjoin_free(res, ft_get_color(80, 80, 255), 3);
res = ft_strjoin_free(res, "\033[1m\002", 1);
cwd = getcwd(cwd_buffer, 99);
if (ft_get_env(env, "HOME") && !ft_strncmp(cwd_buffer, ft_get_env(env,
"HOME"), ft_strlen(ft_get_env(env, "HOME"))))
if (cwd && ft_get_env(env, "HOME") && !ft_strncmp(cwd_buffer,
ft_get_env(env, "HOME"), ft_strlen(ft_get_env(env, "HOME"))))
{
cwd += ft_strlen(getenv("HOME")) - 1;
cwd[0] = '~';
}
if (cwd)
res = ft_strjoin_free(res, cwd, 1);
res = ft_strjoin_free(res, "\001\033[0m\002$ ", 1);
return (res);
@ -61,7 +62,7 @@ t_env *get_env(char **envp)
j++;
name = ft_substr(*envp, 0, i);
value = ft_substr(*envp, i + 1, j);
env = env_add_back(env, name, value);
env = env_add_back(env, name, value, 0);
if (!name || !value)
free_env(env);
if (!name || !value)
@ -128,7 +129,6 @@ int main(int argc, char **argv, char **envp)
free(prompt);
add_history(commands);
exec_command_bonus(msh, commands);
free(commands);
}
printf("exit\n");
ft_exit(msh, g_return_code);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/04/25 18:54:09 by marde-vr ### ########.fr */
/* Updated: 2024/05/22 13:49:58 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -71,108 +71,115 @@ typedef struct s_msh
t_cmd_type in_type;
t_cmd_type out_type;
int in_fd;
int echoctl;
int out_fd;
int echoctl;
int last_return_code;
int locked_return_code;
char *here_doc_filename;
} t_msh;
extern int g_return_code;
t_cmd *cmd_add_back(t_cmd *res, char *cmd, t_cmd_type type);
t_env *export_set_env(t_env *env, char *name, char *value, int append);
void *here_doc_variables(int write, void *data);
int set_echoctl(int value);
int add_var_to_str(char *res, char **command, t_env *env, int *is_var);
void find_cmd_path(t_msh *msh, char **paths, int *found);
t_env *env_add_back(t_env *env, char *name, char *value);
t_token *parse_cmds_to_token(t_cmd *command, t_env *env);
void exec_command_bonus(t_msh *msh, char *cmd_str);
t_token *add_token_back(t_token *res, t_token *next);
t_token *expand_wildcards(t_token *res, char *value, int is_var);
int cmd_is_builtin(t_msh *msh, char *cmd_token);
t_token *token_add_back(t_token *res, char *token, int is_var);
void child(t_msh *msh, char **cmd_args, int i);
t_token *parse_tokens(char *command, t_env *env);
void parent(t_msh *msh, int i, int cmd_count, char **cmd_args);
char *ft_get_env(t_env *env, char *var_name);
int is_fd_open(int fd);
int get_out_type(t_msh *msh, t_cmd *cmds);
void exec_commands(t_msh *msh);
void handle_here_doc(t_msh *msh, char *eof);
int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc);
t_env *export_set_env(t_env *env, char *name, char *value, int append);
t_env *env_add_back(t_env *env, char *name, char *value, int empty);
int add_var_to_str(char *res, char **cmd, t_env *env, int quote);
void parent(t_msh *msh, int i, int cmd_count, char **cmd_args);
char *expand_variables(char *command, t_env *env, int *is_var);
t_token *expand_wildcards(t_token *res, char *value, int is_var);
int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env);
t_token *token_add_back(t_token *res, char *token, int is_var);
int recursive_filename_check(char *wildcard, char **value);
int get_variable_expantion_len(char *command, t_env *env);
t_cmd *cmd_add_back(t_cmd *res, char *cmd, t_cmd_type type);
int get_var_len(char **command, t_env *env, int dquote);
void find_cmd_path(t_msh *msh, char **paths, int *found);
void redirect_output(t_msh *msh, int i, char **cmd_args);
void redirect_input(t_msh *msh, int i, char **cmd_args);
void print_syntax_error_bonus(t_cmd *cmd, t_cmd *cmds);
int filename_corresponds(char *wildcard, char *value);
t_token *parse_cmds_to_token(t_cmd *command, t_env *env);
int ft_export(t_msh *msh, t_token *cmd, t_env *env);
void print_env_declare(t_msh *msh, t_env *env_orig);
void exec_command_bonus(t_msh *msh, char *cmd_str);
void print_cmd_type(t_cmd_type type, char *value);
t_token *add_token_back(t_token *res, t_token *next);
t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env);
int cmd_is_builtin(t_msh *msh, char *cmd_token);
void *here_doc_variables(int write, void *data);
void get_redirections(t_msh *msh, t_cmd *cmds);
void ambiguous_redirect(char *str, t_msh *msh);
void child(t_msh *msh, char **cmd_args, int i);
int cmd_is_forkable_builtin(char *cmd_token);
t_token *parse_tokens(char *command, t_env *env);
void delete_from_env(t_msh *msh, char *name);
char *ft_get_env(t_env *env, char *var_name);
void handle_here_doc(t_msh *msh, char *eof);
void signal_handler_interactive(int signum);
int get_token_len(char *cmd, t_env *env);
void print_syntax_error_bonus(t_cmd *cmd);
int get_out_type(t_msh *msh, t_cmd *cmds);
void signal_handler_here_doc(int signum);
t_token *parsing_syntax_error(t_token *res);
int file_access(t_msh *msh, int *found);
void free_env_cpy(t_env *env);
void remove_command_from_msh(t_msh *msh);
void get_redirections(t_msh *msh, t_cmd *cmds);
t_cmd *get_next_command(t_cmd *cmd);
int get_cmd_count(t_cmd *cmds);
int is_parenthesis(t_cmd *cmd);
void print_signaled(int status);
void ft_exit(t_msh *msh, int error_code);
void sort_wildcards_token(t_token *list);
void redirect_input(t_msh *msh, int i, char **cmd_args);
void redirect_output(t_msh *msh, int i, char **cmd_args);
void print_cmd_type(t_cmd_type type, char *value);
int cmd_is_forkable_builtin(char *cmd_token);
void signal_handler_command(int signum);
void ft_exit(t_msh *msh, int exit_code);
int get_parenthesis_cmd_len(char *cmd);
char **split_paths_from_env(t_env *env);
int add_return_code_to_str(char *res);
void close_pipe_fds(t_msh *msh, int i);
void parse_var(t_msh *msh, char *line);
void print_parsed_token(t_token *cmd);//debug
void remove_here_doc_file(t_msh *msh);
int get_var_name_len(char *command);
void handle_minishellrc(t_msh *msh);
t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env);
char *get_tmp_file_name(t_msh *msh);
int cd(t_token *args, t_msh *msh);
int get_normal_cmd_len(char *cmd);
t_cmd *get_next_command(t_cmd *cmd);
int get_args_count(t_token *cmds);
char **env_to_char_tab(t_env *env);
void print_parsed_cmd(t_cmd *cmd);//debug
int get_token_len(char *command);
int first_is_in_type(t_cmd *cmd);
void print_msh_struct(t_msh *msh);
int get_next_arg_len(char *cmd);
int check_str_syntax(char *cmd);
void close_all_pipes(t_msh *msh);
int is_operand_type(t_cmd *cmd);
int contains_newline(char *str);
t_token *free_token(t_token *token);
int get_cmd_count(t_cmd *cmds);
int check_var_name(char *name);
char **get_cmd_args(t_msh *msh);
char *remove_path(char *token);
t_env *dup_env(t_env *env);
void sort_env(t_env *env);
void print_env_declare(t_msh *msh, t_env *env_orig);
void delete_from_env(t_msh *msh, char *name);
t_cmd *parsing_bonus(char *cmd);
t_token *free_token(t_token *cmd);
int get_cmd_count(t_cmd *cmds);
int is_parenthesis(t_cmd *cmd);
int is_output_type(t_cmd *cmd);
int is_parenthesis(t_cmd *cmd);
void print_signaled(int status);
void exec_commands(t_msh *msh);
int is_input_type(t_cmd *cmd);
char *remove_path(char *token);
t_cmd *parsing_bonus(char *cmd);
void free_env_cpy(t_env *env);
char *get_var_name(char *str);
int exec_builtin(t_msh *msh);
void get_cmd_path(t_msh *msh);
int is_operand_type(t_cmd *cmd);
int is_output_type(t_cmd *cmd);
int is_cmd_type(t_cmd *cmd);
int get_home_var_len(void);
int set_echoctl(int value);
int print_env(t_env *env);
t_cmd *free_cmd(t_cmd *cmd);
int ft_export(t_msh *msh, t_token *cmd, t_env *env);
int get_parenthesis_cmd_len(char *cmd);
int get_normal_cmd_len(char *cmd);
int get_next_arg_len(char *cmd);
int check_str_syntax(char *cmd);
int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env);
int is_parenthesis(t_cmd *cmd);
int is_input_type(t_cmd *cmd);
void free_env(t_env *env);
int ft_unset(t_msh *msh);
void free_fds(t_msh *msh);
t_env *dup_env(t_env *env);
void sort_env(t_env *env);
void free_msh(t_msh *msh);
void free_msh(t_msh *msh);
int echo(t_token *args);
int exit_bt(t_msh *msh);
int is_cmd_type(t_cmd *cmd);
int is_cmd_char(char c);
void ambiguous_redirect(char *str, t_msh *msh);
int cd(t_token *args, t_msh *msh);
int is_fd_open(int fd);
int pwd(void);
#endif

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */
/* Updated: 2024/04/25 18:39:52 by tomoron ### ########.fr */
/* Updated: 2024/05/06 14:43:45 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -65,12 +65,33 @@ void go_to_next_out_type(t_cmd **cur_cmd)
*cur_cmd = (*cur_cmd)->next;
}
int get_out_type(t_msh *msh, t_cmd *cur_cmd)
void get_out_file(t_msh *msh, t_cmd *cur_cmd, int *ret)
{
t_token *filename;
msh->out_type = cur_cmd->cmd_type;
if (ft_strchr(cur_cmd->value, '$'))
{
ambiguous_redirect(cur_cmd->value, msh);
return ;
}
filename = parse_tokens(cur_cmd->value, msh->env);
if (!filename)
ft_exit(msh, 1);
if (filename->next)
ambiguous_redirect(cur_cmd->value, msh);
if (!filename->next)
*ret = open_out_file(msh, &cur_cmd, filename->value);
free_token(filename);
}
int get_out_type(t_msh *msh, t_cmd *cur_cmd)
{
int ret;
msh->out_type = CMD;
if (msh->out_fd > 2)
close(msh->out_fd);
msh->out_fd = 0;
ret = 0;
go_to_next_out_type(&cur_cmd);
@ -78,18 +99,8 @@ int get_out_type(t_msh *msh, t_cmd *cur_cmd)
msh->out_type = 0;
else if (cur_cmd && is_output_type(cur_cmd) && !is_operand_type(cur_cmd)
&& cur_cmd->cmd_type != PIPE)
{
msh->out_type = cur_cmd->cmd_type;
filename = parse_tokens(cur_cmd->value, msh->env);
if (!filename)
ft_exit(msh, 1);
if (filename->next)
ambiguous_redirect(cur_cmd->value, msh);
if (!filename->next)
ret = open_out_file(msh, &cur_cmd, filename->value);
free_token(filename);
}
get_out_file(msh, cur_cmd, &ret);
else if (cur_cmd && cur_cmd->cmd_type == PIPE)
msh->out_type = PIPE;
return (ret || msh->in_fd == -1);
return (ret || msh->in_fd == -2);
}

View File

@ -6,16 +6,11 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
/* Updated: 2024/04/24 20:39:35 by marde-vr ### ########.fr */
/* Updated: 2024/05/22 13:52:40 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int is_cmd_char(char c)
{
return (!ft_isspace(c) && c != '|' && c != '&' && c != '<' && c != '>');
}
int add_home_to_str(char *res)
{
int i;
@ -23,15 +18,20 @@ int add_home_to_str(char *res)
i = 0;
str = getenv("HOME");
while (str[i])
while (str && str[i])
{
res[i] = str[i];
i++;
}
if (!str)
{
res[i] = '~';
return (1);
}
return (i);
}
char *get_token(char **cmd, int *quotes[2], t_env *env, int *is_var)
char *get_token(char **cmd, int quotes[2])
{
char *res;
int i;
@ -39,19 +39,16 @@ char *get_token(char **cmd, int *quotes[2], t_env *env, int *is_var)
i = 0;
while (ft_isspace(**cmd))
(*cmd)++;
res = ft_calloc(get_token_len(*cmd, env) + 1, 1);
while (res && **cmd && (is_cmd_char(**cmd) || *(quotes[0]) || *(quotes[1])))
res = ft_calloc(get_token_len(*cmd) + 1, 1);
while (res && **cmd && (!ft_isspace(**cmd) || quotes[0] || quotes[1]))
{
if (**cmd == '"' && !*(quotes[0]))
*(quotes[1]) = !*(quotes[1]);
if (**cmd == '\'' && !*(quotes[1]))
*(quotes[0]) = !*(quotes[0]);
if (**cmd == '$' && !*(quotes[0]))
i += add_var_to_str(res + i, cmd, env, is_var);
else if (**cmd == '~' && !*(quotes[0]) && !*(quotes[1]))
if (**cmd == '"' && !quotes[0])
quotes[1] = !quotes[1];
if (**cmd == '\'' && !quotes[1])
quotes[0] = !quotes[0];
else if (**cmd == '~' && !quotes[0] && !quotes[1])
i += add_home_to_str(res + i);
else if (((**cmd == '\'' && *(quotes[1]))
|| (**cmd == '"' && *(quotes[0])))
else if (((**cmd == '\'' && quotes[1]) || (**cmd == '"' && quotes[0]))
|| (**cmd != '\'' && **cmd != '"'))
res[i++] = **cmd;
(*cmd)++;
@ -61,27 +58,29 @@ char *get_token(char **cmd, int *quotes[2], t_env *env, int *is_var)
t_token *parse_tokens(char *command, t_env *env)
{
int in_quote;
int in_dquote;
int quotes[2];
char *tmp;
t_token *res;
char *value;
int is_var;
in_quote = 0;
in_dquote = 0;
quotes[0] = 0;
quotes[1] = 0;
res = 0;
is_var = 0;
command = expand_variables(command, env, &is_var);
tmp = command;
while (command && *command)
{
value = get_token(&command, (int *[2]){&in_quote, &in_dquote},
env, &is_var);
value = get_token(&command, quotes);
if (!value)
return (free_token(res));
res = expand_wildcards(res, value, is_var);
while (ft_isspace(*command))
command++;
}
if (command && (in_quote || in_dquote))
free(tmp);
if (tmp && (quotes[0] || quotes[1]))
return (parsing_syntax_error(res));
return (res);
}

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */
/* Updated: 2024/04/24 14:53:19 by tomoron ### ########.fr */
/* Updated: 2024/04/26 14:32:51 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */

24
srcs/parsing_utils.c Normal file
View File

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/22 13:49:17 by tomoron #+# #+# */
/* Updated: 2024/05/22 13:50:31 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_home_var_len(void)
{
char *str;
str = getenv("HOME");
if (!str)
return (1);
else
return (ft_strlen("HOME"));
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
/* Updated: 2024/04/24 17:59:47 by tomoron ### ########.fr */
/* Updated: 2024/05/22 13:52:29 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,14 +22,22 @@ char *get_var_name(char *command)
return (res);
}
int get_var_len(char **command, t_env *env)
int get_var_len(char **command, t_env *env, int in_dquote)
{
char *var_name;
char *env_var;
(*command)++;
if (**command == '\'' || **command == '"' || !**command)
if ((**command == '\'' || **command == '"') && !in_dquote)
{
(*command)--;
return (0);
}
if (!**command || (**command == '"' && in_dquote))
{
(*command)--;
return (1);
}
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
return (2);
if (**command == '?')
@ -43,7 +51,7 @@ int get_var_len(char **command, t_env *env)
return (ft_strlen(env_var));
}
int get_token_len(char *command, t_env *env)
int get_token_len(char *command)
{
int in_quote;
int in_dquote;
@ -52,16 +60,14 @@ int get_token_len(char *command, t_env *env)
in_quote = 0;
in_dquote = 0;
res = 0;
while (*command && (is_cmd_char(*command) || in_quote || in_dquote))
while (*command && (!ft_isspace(*command) || in_quote || in_dquote))
{
if (*command == '"' && !in_quote)
in_dquote = !in_dquote;
if (*command == '\'' && !in_dquote)
in_quote = !in_quote;
if (*command == '$' && !in_quote)
res += get_var_len(&command, env);
else if (*command == '~' && !in_quote && !in_dquote)
res += ft_strlen(getenv("HOME"));
res += get_home_var_len();
else if (*command != '\'' && *command != '"')
res++;
else if ((*command == '\'' && in_dquote) || (*command == '"'
@ -79,7 +85,7 @@ int invalid_variable_char(char *res, char c)
return (2);
}
int add_var_to_str(char *res, char **command, t_env *env, int *is_var)
int add_var_to_str(char *res, char **command, t_env *env, int dquote)
{
char *var_name;
char *var;
@ -87,12 +93,12 @@ int add_var_to_str(char *res, char **command, t_env *env, int *is_var)
i = 0;
(*command)++;
*is_var = 1;
if (**command == '\'' || **command == '"' || !**command)
{
if ((**command != '\'' && **command != '"') || dquote)
*res = '$';
(*command)--;
return (1);
return ((*(*command + 1) != '\'' && *(*command + 1) != '"') || dquote);
}
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
return (invalid_variable_char(res, **command));
@ -104,5 +110,5 @@ int add_var_to_str(char *res, char **command, t_env *env, int *is_var)
while (var && var[i++])
res[i - 1] = var[i - 1];
*command += get_var_name_len(*command) - 1;
return (i - 1);
return (i - (1 - (i == 0)));
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
/* Updated: 2024/04/24 20:37:41 by marde-vr ### ########.fr */
/* Updated: 2024/05/07 16:08:17 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -60,7 +60,8 @@ void get_path(t_msh *msh, int *found)
char **paths;
paths = split_paths_from_env(msh->env);
if (!paths || !*(msh->tokens->value))
if (!paths || !*(msh->tokens->value)
|| ft_str_is_only_char(msh->tokens->value, '.'))
{
free_paths(paths);
return ;
@ -98,8 +99,7 @@ void get_cmd_path(t_msh *msh)
ft_printf_fd(2, "'': command not found\n");
else
ft_printf_fd(2, "%s: command not found\n", msh->tokens->value);
free(msh->tokens->value);
msh->tokens->value = 0;
close_all_pipes(msh);
g_return_code = 127;
}
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
/* Updated: 2024/04/26 10:49:45 by marde-vr ### ########.fr */
/* Updated: 2024/05/06 14:06:33 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,17 +14,20 @@
void close_pipe_fds(t_msh *msh, int i)
{
if (msh->fds)
{
if (i != 0)
{
if (msh->fds[i - 1][0] > 2)
if (msh->fds[i - 1] && msh->fds[i - 1][0] > 2)
close(msh->fds[i - 1][0]);
if (msh->fds[i - 1][1] > 2)
if (msh->fds[i - 1] && msh->fds[i - 1][1] > 2)
close(msh->fds[i - 1][1]);
}
if (msh->fds[i][0] > 2)
if (msh->fds[i] && msh->fds[i][0] > 2)
close(msh->fds[i][0]);
if (msh->fds[i][1] > 2)
if (msh->fds[i] && msh->fds[i][1] > 2)
close(msh->fds[i][1]);
}
}
void handle_parenthesis(t_msh *msh)
@ -50,7 +53,6 @@ void handle_parenthesis(t_msh *msh)
msh->out_fd = 0;
msh->locked_return_code = 0;
exec_command_bonus(msh, command);
free(command);
ft_exit(msh, g_return_code);
}
@ -72,10 +74,7 @@ void execute_command(t_msh *msh, char **cmd_args)
{
env = env_to_char_tab(msh->env);
if (env)
{
if (execve(msh->tokens->value, cmd_args, env))
perror("execve");
}
execve(msh->tokens->value, cmd_args, env);
ft_free_str_arr(env);
}
}
@ -90,6 +89,10 @@ void child(t_msh *msh, char **cmd_args, int i)
|| msh->out_type == RED_O_APP)
redirect_output(msh, i, cmd_args);
close_pipe_fds(msh, i);
if (msh->in_fd > 2)
close(msh->in_fd);
if (msh->out_fd > 2)
close(msh->out_fd);
execute_command(msh, cmd_args);
close(0);
close(1);

32
srcs/redirection_utils.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirection_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/03 14:11:22 by marde-vr #+# #+# */
/* Updated: 2024/05/03 14:11:50 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ambiguous_redirect(char *str, t_msh *msh)
{
ft_printf_fd(2, "minishell: %s: ambiguous redirect\n", str);
msh->in_fd = -2;
g_return_code = 1;
}
int first_is_in_type(t_cmd *cmd)
{
t_cmd *cur_token;
cur_token = cmd;
while (cur_token && cur_token->cmd_type == CMD && cur_token->next)
cur_token = cur_token->next;
if (is_input_type(cur_token) || cur_token->cmd_type == PIPE)
return (1);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
/* Updated: 2024/04/26 10:53:29 by marde-vr ### ########.fr */
/* Updated: 2024/05/07 17:07:16 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
void ft_exit(t_msh *msh, int exit_code)
{
free_msh(msh);
rl_clear_history();
exit(exit_code);
}
@ -72,3 +73,15 @@ int file_access(t_msh *msh, int *found)
}
return (1);
}
void close_all_pipes(t_msh *msh)
{
int i;
i = 0;
while (msh->fds && msh->fds[i])
{
close_pipe_fds(msh, i);
i++;
}
}

64
srcs/variable_expantion.c Normal file
View File

@ -0,0 +1,64 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* variable_expantion.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/06 10:57:33 by tomoron #+# #+# */
/* Updated: 2024/05/08 12:29:17 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_variable_expantion_len(char *command, t_env *env)
{
int in_quote;
int in_dquote;
int i;
i = 0;
in_dquote = 0;
in_quote = 0;
while (*command)
{
if (*command == '\'' && !in_dquote)
in_quote = !in_quote;
if (*command == '"' && !in_quote)
in_dquote = !in_dquote;
if (*command == '$' && !in_quote)
i += get_var_len(&command, env, in_dquote);
else
i++;
command++;
}
return (i);
}
char *expand_variables(char *command, t_env *env, int *is_var)
{
char *res;
int i;
int in_dquote;
int in_quote;
if (!command)
return (0);
res = ft_calloc(get_variable_expantion_len(command, env) + 1, 1);
in_quote = 0;
in_dquote = 0;
i = 0;
while (res && *command)
{
if (*command == '\'' && !in_dquote)
in_quote = !in_quote;
if (*command == '"' && !in_quote)
in_dquote = !in_dquote;
if (*command == '$' && !in_quote && ++(*is_var))
i += add_var_to_str(res + i, &command, env, in_dquote);
else
res[i++] = *command;
command++;
}
return (res);
}

View File

@ -6,11 +6,22 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/15 12:53:29 by tomoron #+# #+# */
/* Updated: 2024/04/26 10:49:36 by marde-vr ### ########.fr */
/* Updated: 2024/04/29 11:12:40 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int recursive_filename_check(char *wildcard, char **value)
{
while (**value)
{
if (filename_corresponds(wildcard + 1, *value))
return (1);
(*value)++;
}
return (0);
}
int filename_corresponds(char *wildcard, char *value)
{
if (*value == '.' && *wildcard != '.')
@ -23,8 +34,7 @@ int filename_corresponds(char *wildcard, char *value)
wildcard++;
if (!wildcard[1])
return (1);
while (*value)
if (filename_corresponds(wildcard + 1, value++))
if (recursive_filename_check(wildcard, &value))
return (1);
return (0);
}
@ -32,6 +42,8 @@ int filename_corresponds(char *wildcard, char *value)
{
wildcard++;
value++;
if (ft_str_is_only_char(wildcard, '*') && !*value)
return (1);
}
else
return (0);

View File

@ -1,16 +0,0 @@
Cat makefile | (echo yes && rev) leaks
echo yes && echo yes && cat dhdgdgjsk || echo a leaks
echo abc && echo def | cat | rev && echo yes
(), ((())), (())
export leaks AAAAAAAAAAAAAAAAAAAAAA
To test:
Open file descriptor with cat << eof | ls open fd and infinte loop
test signals
test and verify all malocs
verify forbidden functions
check for any hidden or useless files