This commit is contained in:
2024-03-27 17:22:11 +01:00
parent a9fde3bad1
commit ffbbd564f9
15 changed files with 133 additions and 142 deletions

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/03/27 14:59:17 by tomoron ### ########.fr */
/* Updated: 2024/03/27 16:25:53 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,7 +32,7 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token)
}
else if (!ft_strcmp(cmd_token, "exit"))
{
exit_bt(msh);
g_return_code = exit_bt(msh);
return (1);
}
else if (!ft_strcmp(cmd_token, "export"))

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */
/* Updated: 2024/03/27 16:20:06 by tomoron ### ########.fr */
/* Updated: 2024/03/27 17:01:49 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -71,10 +71,9 @@ char **get_cmd_args(t_msh *msh)
if (cur_cmd->type == ARG)
{
if (!i)
cmd_args[i] = remove_path(cur_cmd->value);
cmd_args[i++] = remove_path(cur_cmd->value);
else
cmd_args[i] = cur_cmd->value;
i++;
cmd_args[i++] = cur_cmd->value;
}
else
cur_cmd = cur_cmd->next;

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
/* Updated: 2024/03/27 16:21:14 by tomoron ### ########.fr */
/* Updated: 2024/03/27 17:20:40 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,7 +47,7 @@ int echo(t_token *args)
if (!args->value[i])
put_nl = 0;
else
ft_printf("%s ",args->value);
ft_printf("%s ", args->value);
args = args->next;
}
put_args(args);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
/* Updated: 2024/03/27 14:49:53 by tomoron ### ########.fr */
/* Updated: 2024/03/27 16:52:02 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -66,31 +66,17 @@ void exec_command(t_msh *msh, int i, int cmd_count)
remove_command_from_msh(msh);
}
void exec_commands(t_msh *msh)
void end_execution(t_msh *msh, int cmd_count)
{
int cmd_count;
int i;
int status;
i = -1;
if (!msh->cmds)
return ;
cmd_count = get_cmd_count(msh->cmds);
msh->fds = ft_calloc(cmd_count, sizeof(int **));
msh->pids = ft_calloc(cmd_count, sizeof(int *));
if (!msh->pids || !msh->fds)
ft_exit(msh, 1);
while (++i < cmd_count)
exec_command(msh, i, cmd_count);
i = 0;
while (i < cmd_count)
{
waitpid(msh->pids[i], &status, 0);
i++;
}
waitpid(msh->pids[i++], &status, 0);
if (!g_return_code && WIFEXITED(status))
g_return_code = WEXITSTATUS(status);
if(WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT)
if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT)
printf("Quit (core dumped)\n");
i = 0;
while (i < cmd_count)
@ -107,3 +93,21 @@ void exec_commands(t_msh *msh)
signal(SIGQUIT, signal_handler_interactive);
set_echoctl(0);
}
void exec_commands(t_msh *msh)
{
int cmd_count;
int i;
i = -1;
if (!msh->cmds)
return ;
cmd_count = get_cmd_count(msh->cmds);
msh->fds = ft_calloc(cmd_count, sizeof(int **));
msh->pids = ft_calloc(cmd_count, sizeof(int *));
if (!msh->pids || !msh->fds)
ft_exit(msh, 1);
while (++i < cmd_count)
exec_command(msh, i, cmd_count);
end_execution(msh, cmd_count);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
/* Updated: 2024/03/27 15:35:57 by tomoron ### ########.fr */
/* Updated: 2024/03/27 16:24:45 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,7 +33,7 @@ void get_exit_bt_return_code(t_msh *msh, int *exit_code)
*exit_code = g_return_code;
}
void exit_bt(t_msh *msh)
int exit_bt(t_msh *msh)
{
t_token *cur_cmd;
int exit_code;
@ -43,10 +43,7 @@ void exit_bt(t_msh *msh)
ft_printf("exit\n");
if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG
&& ft_strisnbr(cur_cmd->value))
{
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
g_return_code = 1;
}
else
{
get_exit_bt_return_code(msh, &exit_code);
@ -62,4 +59,5 @@ void exit_bt(t_msh *msh)
free_msh(msh);
exit(exit_code);
}
return (1);
}

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/03/27 14:57:44 by tomoron ### ########.fr */
/* Updated: 2024/03/27 16:55:22 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@ void print_env_declare(t_env *env)
{
if (strcmp(env->name, "_"))
{
if(*(env->value))
if (*(env->value))
printf("declare -x %s=\"%s\"\n", env->name, env->value);
else
printf("declare -x %s\n", env->name);
@ -27,22 +27,18 @@ void print_env_declare(t_env *env)
}
}
int check_var_name(char *name)
int export_invalid_identifier(char *arg, char *name)
{
if (ft_isdigit(*name) || !*name)
return (0);
while (*name)
{
if (!ft_isalnum(*name) && *name != '_')
return (0);
name++;
}
ft_putstr_fd("minishell: export: `", 2);
ft_putstr_fd(arg, 2);
ft_putstr_fd("': not a valid identifier\n", 2);
free(name);
return (1);
}
int ft_export(t_msh *msh)
{
t_cmd *cmd;
t_token *cmd;
char *arg;
char *name;
char *value;
@ -60,13 +56,7 @@ int ft_export(t_msh *msh)
len++;
name = ft_substr(arg, 0, len);
if (!name || !check_var_name(name))
{
ft_putstr_fd("minishell: export: `", 2);
ft_putstr_fd(arg, 2);
ft_putstr_fd("': not a valid identifier\n", 2);
free(name);
return (1);
}
return (export_invalid_identifier(arg, name));
if (arg[len])
len++;
value = ft_strdup(arg + len);
@ -102,7 +92,7 @@ void delete_from_env(t_msh *msh, char *name)
int ft_unset(t_msh *msh)
{
t_cmd *cmd;
t_token *cmd;
cmd = msh->cmds;
if (cmd)

View File

@ -6,17 +6,22 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/03/27 15:56:11 by tomoron ### ########.fr */
/* Updated: 2024/03/27 17:18:03 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_cmd *cmd_add_back(t_token *cmd, char *value, t_token_type type)
t_token *cmd_add_back(t_token *cmd, char *value, t_token_type type)
{
t_cmd *res;
t_cmd *current;
t_token *res;
t_token *current;
if (value && !*value)
{
free(value);
return (cmd);
}
res = ft_calloc(1, sizeof(t_token));
if (!res)
return (cmd);
@ -31,7 +36,7 @@ t_cmd *cmd_add_back(t_token *cmd, char *value, t_token_type type)
return (cmd);
}
void free_cmd(t_token *cmd)
int free_cmd(t_token *cmd)
{
if (cmd)
{
@ -48,4 +53,5 @@ void free_cmd(t_token *cmd)
free(cmd);
cmd = 0;
}
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
/* Updated: 2024/03/27 15:05:42 by tomoron ### ########.fr */
/* Updated: 2024/03/27 17:02:16 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -71,39 +71,11 @@ t_env *get_env(char **envp)
return (env);
}
void print_binary(unsigned int num)
{
int size;
int i;
if (num == 0)
{
printf("0");
return ;
}
//Tom qu'est-ce que t'a fait ?????
// Taille d'un unsigned int en bits
size = sizeof(unsigned int) * 8;
// Parcours de chaque bit de droite à gauche
for (i = size - 1; i >= 0; i--)
{
// Vérifie si le bit est 1 ou 0
if (num & (1u << i))
printf("1");
else
printf("0");
}
}
int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
{
struct termios t_p;
*msh = ft_calloc(1, sizeof(t_msh));
if (!*msh)
ft_exit(*msh, 1);
ft_exit(*msh, 1);
(void)argc;
(void)argv;
(*msh)->env = get_env(envp);
@ -111,7 +83,7 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
(*msh)->echoctl = t_p.c_lflag & ECHOCTL;
signal(SIGINT, signal_handler_interactive);
signal(SIGQUIT, signal_handler_interactive);
if(set_echoctl(0))
if (set_echoctl(0))
ft_exit(*msh, 1);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/03/27 15:24:41 by tomoron ### ########.fr */
/* Updated: 2024/03/27 17:20:14 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -52,7 +52,7 @@ typedef struct s_env
typedef struct s_msh
{
struct s_env *env;
struct s_cmd *cmds;
struct s_token *cmds;
int **fds;
int *pids;
enum e_token_type in_type;
@ -66,10 +66,10 @@ typedef struct s_msh
extern int g_return_code;
t_token *cmd_add_back(t_token *res, char *token, t_token_type type);
void free_cmd(t_token *cmd);
int free_cmd(t_token *cmd);
void exec_commands(t_msh *msh);
int echo(t_token *args);
void exit_bt(t_msh *msh);
int exit_bt(t_msh *msh);
t_env *env_add_back(t_env *env, char *name, char *value);
void free_env(t_env *env);
int print_env(t_env *env);
@ -118,5 +118,7 @@ char *get_tmp_file_name(t_msh *msh);
int contains_newline(char *str);
void parse_var(t_msh *msh, char *line);
int set_echoctl(int value);
int add_return_code_to_str(char *res);
int parsing_syntax_error(t_token *res);
#endif

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
/* Updated: 2024/03/24 08:50:11 by marde-vr ### ########.fr */
/* Updated: 2024/03/27 17:20:20 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -47,10 +47,7 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
if (**cmd == '\'' && !*in_dquote)
*in_quote = !*in_quote;
if (**cmd == '$' && !*in_quote)
{
(*cmd)++;
i += add_var_to_str(res + i, cmd, env);
}
else if (**cmd == '~' && !*in_quote && !*in_dquote)
i += add_home_to_str(res + i);
else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote))
@ -86,12 +83,12 @@ t_token_type get_token_type(char **command)
return (res);
}
t_cmd *parse_command(char *command, t_env *env)
t_token *parse_command(char *command, t_env *env)
{
int in_quote;
int in_dquote;
t_cmd *res;
char *token;
t_token *res;
char *value;
t_token_type type;
in_quote = 0;
@ -101,26 +98,16 @@ t_cmd *parse_command(char *command, t_env *env)
{
type = get_token_type(&command);
if (type == ARG)
token = get_token(&command, &in_quote, &in_dquote, env);
value = get_token(&command, &in_quote, &in_dquote, env);
else
token = 0;
if (type == ARG && token == 0)
{
free_cmd(res);
return (0);
}
if (token && !*token)
free(token);
else
res = cmd_add_back(res, token, type);
value = 0;
if (type == ARG && value == 0)
return (free_cmd(res));
res = cmd_add_back(res, value, type);
while (ft_isspace(*command))
command++;
}
if (command && (in_quote || in_dquote))
{
free_cmd(res);
ft_putstr_fd("minishell: syntax error\n", 2);
return (0);
}
return (parsing_syntax_error(res));
return (res);
}

View File

@ -6,10 +6,8 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */
/* Updated: 2024/03/27 15:01:00 by tomoron ### ########.fr */
/* Updated: 2024/03/27 17:20:51 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
/* Updated: 2024/03/05 17:29:06 by marde-vr ### ########.fr */
/* Updated: 2024/03/27 17:09:50 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -72,20 +72,11 @@ int get_token_len(char *command, t_env *env)
return (res);
}
int add_return_code_to_str(char *res)
int invalid_variable_char(char *res, char c)
{
char *var;
int i;
i = 0;
var = ft_itoa(g_return_code);
while (var && var[i])
{
res[i] = var[i];
i++;
}
free(var);
return (i);
res[0] = '$';
res[1] = c;
return (2);
}
int add_var_to_str(char *res, char **command, t_env *env)
@ -95,6 +86,7 @@ int add_var_to_str(char *res, char **command, t_env *env)
int i;
i = 0;
(*command)++;
if (**command == '\'' || **command == '"')
{
*res = '$';
@ -102,21 +94,14 @@ int add_var_to_str(char *res, char **command, t_env *env)
return (1);
}
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
{
res[0] = '$';
res[1] = **command;
return (2);
}
return (invalid_variable_char(res, **command));
if (**command == '?')
return (add_return_code_to_str(res));
var_name = get_var_name(*command);
var = ft_get_env(env, var_name);
free(var_name);
while (var && var[i])
{
res[i] = var[i];
i++;
}
res[i] = var[i++];
*command += get_var_name_len(*command) - 1;
return (i);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/22 14:31:13 by tomoron #+# #+# */
/* Updated: 2024/03/27 16:22:39 by tomoron ### ########.fr */
/* Updated: 2024/03/27 17:21:09 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -76,6 +76,7 @@ ags");
}
return (0);
}
void signal_handler_command(int signum)
{
(void)signum;

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/03/27 15:00:24 by tomoron ### ########.fr */
/* Updated: 2024/03/27 16:55:04 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,6 +31,35 @@ void ft_exit(t_msh *msh, int exit_code)
exit(exit_code);
}
int check_var_name(char *name)
{
if (ft_isdigit(*name) || !*name)
return (0);
while (*name)
{
if (!ft_isalnum(*name) && *name != '_')
return (0);
name++;
}
return (1);
}
int add_return_code_to_str(char *res)
{
char *var;
int i;
i = 0;
var = ft_itoa(g_return_code);
while (var && var[i])
{
res[i] = var[i];
i++;
}
free(var);
return (i);
}
int file_access(t_msh *msh, int *found)
{
int fd;

20
srcs/utils2.c Normal file
View File

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 17:19:27 by tomoron #+# #+# */
/* Updated: 2024/03/27 17:19:35 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int parsing_syntax_error(t_token *res)
{
free_cmd(res);
ft_putstr_fd("minishell: syntax error\n", 2);
return (0);
}