This commit is contained in:
PrStein 2022-04-09 05:14:30 +02:00
commit 2c2a293581
15 changed files with 65 additions and 383 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
/* Updated: 2022/04/07 16:47:50 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:59:53 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,7 +21,7 @@
# include <readline/history.h>
# include <sys/types.h>
# include <sys/stat.h>
#include <sys/wait.h>
# include <sys/wait.h>
# include <fcntl.h>
# include <stdio.h>
# include <errno.h>
@ -32,7 +32,7 @@
// Describes a simple command and arguments
typedef struct s_simple {
char **env;
struct s_command *big_cmd;
int fd[2];
int pipe[2];
int last;
@ -50,6 +50,7 @@ typedef struct s_simple {
// Describes a complete command with the multiple pipes if any
// and input/output redirection if any.
typedef struct s_command {
char **env;
int nb_s_cmd;
struct s_simple **s_cmds;
//char *outfile;
@ -72,11 +73,9 @@ t_cmd *set_cmd(char *input, char **path);
char **get_path(char **env);
char *get_command(char **exec, char **env);
//free_cmd
void free_cmd(t_cmd *cmd);
void exit_shell(t_cmd *cmd);
void exit_shell(t_cmd *cmd, char **env);
//redirection.c set redirection and input good
char *set_redirection(t_s_cmd *cmd, char *line, int index);

View File

@ -1,190 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */
/* Updated: 2022/04/05 18:30:30 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
void ft_ls(char *input)
{
int i;
DIR *dir;
struct dirent *sd;
i = ft_strlen("ls");
// if (input[i] == ' ')
// {
printf("ici\n");
while (input[i] && input[i] != ' ')
i++;
// }
// printf("[%c]\n", input[i]);
// printf("[%c]\n", input[i+1]);
dir = opendir(".");
if (dir == NULL)
{
printf("stop\n");
return ;
}
// printf("dir --> %s\n", dir[0]);
while ((sd=readdir(dir)) != NULL)
{
printf(" >> %s\n", sd->d_name);
}
closedir(dir);
}
void del_one(t_s_cmd *cmd)
{
int i;
char *r;
i = ft_strlen(cmd->env[find_pwd(cmd)]);
// printf("%s\n", cmd->args[6]);
// while (cmd->env[find_pwd(cmd)][i])
// i++;
while (cmd->env[find_pwd(cmd)][i] != '/')
i--;
r = ft_substr(cmd->env[find_pwd(cmd)], 0, i);
// printf ("-->%s\n", r);
// free(cmd->env[find_pwd(cmd)]);
if (r)
cmd->env[find_pwd(cmd)] = ft_strdup(r);
else
cmd->env[find_pwd(cmd)] = ft_strdup("PWD=/");
if (r)
free(r);
}
void add_one(t_s_cmd *cmd, char *str)
{
char *r;
r = ft_strjoin(cmd->env[find_pwd(cmd)], "/");
r = ft_strjoin(r, str);
//free(cmd->env[find_pwd(cmd)]);
cmd->env[find_pwd(cmd)] = ft_strdup(r);
free(r);
}
void change_path(t_s_cmd *cmd)
{
char **tab;
int i;
i = 0;
tab = ft_split(cmd->args[1], '/');
while (cmd->env[i])
{
if (ft_strncmp(cmd->env[i], "OLDPWD=", 7) == 0)
break ;
i++;
}
// printf("%d\n", i);
/*if (cmd->env[i])
free(cmd->env[i]);*/
cmd->env[i] = ft_strjoin("OLD", cmd->env[find_pwd(cmd)]);
i = 0;
while (tab[i])
{
// printf("%d -> %s\n", tab_len(tab), tab[i]);
// if (tab_len(tab) < 2)
// break ;
if (ft_strcmp(tab[i], "..") == 0)
del_one(cmd);
else
add_one(cmd, tab[i]);
i++;
}
}
void reboot_pwd(t_s_cmd *cmd, int i)
{
char *str;
while (i > 3)
{
if (chdir("..") == 0)
del_one(cmd);
i--;
}
str = ft_substr(cmd->args[1], 2, ft_strlen(cmd->args[1]));
cmd->args[1] = ft_strdup(str);
free(str);
}
int find_it(char **str, char *s)
{
int i;
i = 0;
while (str[i] && ft_strncmp(str[i], s, ft_strlen(s)))
i++;
return (i);
}
void open_directory(t_s_cmd *cmd)
{
char **str;
int j;
str = ft_split(cmd->env[find_pwd(cmd)], '/');
j = tab_len(str);
if (!cmd->args[1])
{
// reboot_pwd(cmd, j);
if (tab_len(str) > 3)
{
while (j-- > 3)
if (chdir("..") == 0)
del_one(cmd);
}
else
{
char *p = ft_substr(cmd->env[find_it(cmd->env, "HOME=")], 6, ft_strlen(cmd->env[find_it(cmd->env, "HOME=")]));
if (chdir(p) == 0)
{
cmd->env[find_it(cmd->env, "OLDPWD=")] = ft_strjoin("OLD", cmd->env[find_pwd(cmd)]);
cmd->env[find_pwd(cmd)] = ft_strjoin("PWD=/", p);
}
free(p);
// printf("%s\n", p);
}
}
if (tab_len(cmd->args) == 2)
{
if (cmd->args[1][0] == '~')
reboot_pwd(cmd, j);
if (chdir(cmd->args[1]) == 0)
change_path(cmd);
}
}
void ft_pwd(t_s_cmd *cmd)
{
int i;
int j;
j = 0;
i = 0;
while (cmd->env[find_pwd(cmd)][j] != '=')
j++;
j += 1;
while (cmd->env[find_pwd(cmd)][j])
{
write (1, &cmd->env[find_pwd(cmd)][j], 1);
j++;
}
write(1, "\n", 1);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/28 17:12:52 by sadjigui #+# #+# */
/* Updated: 2022/04/05 18:22:08 by apommier ### ########.fr */
/* Updated: 2022/04/09 05:00:15 by apommier ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 20:52:56 by sadjigui #+# #+# */
/* Updated: 2022/04/05 17:30:24 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:55:48 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -84,19 +84,19 @@ void find_for_print(t_s_cmd *cmd, char *input)
str = ft_strjoin(input, "=");
x = ft_strlen(str);
// printf("%d\n", x);
while (cmd->env[i])
while (cmd->big_cmd->env[i])
{
if (ft_strncmp(cmd->env[i], str, x) == 0)
if (ft_strncmp(cmd->big_cmd->env[i], str, x) == 0)
{
// printf("%s\n", cmd->env[i]);
// printf("%s\n", cmd->big_cmd->env[i]);
break ;
}
i++;
}
if (cmd->env[i] != NULL)
if (cmd->big_cmd->env[i] != NULL)
{
print_value(cmd->env[i], input);
print_value(cmd->big_cmd->env[i], input);
// print_rest(input, tmp, j);
}
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/28 23:26:59 by sadjigui #+# #+# */
/* Updated: 2022/04/05 03:54:10 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:59:38 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -74,9 +74,9 @@ void ft_export_variable(t_s_cmd *cmd, char *variable)
// printf("jojo\n");
dest = check_value(variable);
unset = ft_substr(dest, 0, find_len(dest, 0, '='));
while (cmd->env[i] != NULL)
while (cmd->big_cmd->env[i] != NULL)
{
if (ft_strncmp(unset, cmd->env[i], ft_strlen(unset)) == 0)
if (ft_strncmp(unset, cmd->big_cmd->env[i], ft_strlen(unset)) == 0)
{
find_variable(unset, cmd);
break ;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/07 17:58:25 by sadjigui #+# #+# */
/* Updated: 2022/04/05 03:47:53 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:55:48 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -67,7 +67,7 @@ void lone_export(t_s_cmd *cmd)
int i;
i = 0;
tmp = cmd->env;
tmp = cmd->big_cmd->env;
sort_tab(tmp);
while (tmp[i])
{

View File

@ -1,139 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/28 17:17:01 by sadjigui #+# #+# */
/* Updated: 2022/04/05 17:16:17 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
// void join_variable(t_s_cmd *cmd, char **v_v, int size, char **tab)
// {
// char *str;
// char *tmp;
//
// while (tab[size])
// {
// cmd->env[size] = ft_strdup(tab[size]);
// // printf("tmp----%s\n", tmp[size]);
// // printf("envy---%s\n", cmd->env[size]);
// size++;
// }
// str = ft_strjoin(v_v[0], "=");
// tmp = ft_strdup(str);
// free(str);
// str = ft_strjoin(tmp,v_v[1]);
// cmd->env[size] = ft_strdup(str);
// printf("ici\n");
// free(str);
// free(tmp);
// cmd->env[size + 1] = NULL;
// }
void register_env(t_s_cmd *cmd, char *variable)
{
char **tmp = NULL;
int i;
i = 0;
// tmp = NULL;
// printf("TABLEN %d", tab_len(cmd->env));
tmp = (char **)malloc(sizeof(char *) * (tab_len(cmd->env) + 2));
if(!tmp)
printf("no tmp\n");
while (cmd->env[i])
{
tmp[i] = ft_strdup(cmd->env[i]);
// printf("ici\n");
// printf("%s\n", "let me in");
i++;
}
// printf("i: %d %s\n",i,tmp[i]);
// printf("%s\n", "brrrrrrr");
tmp[i] = ft_strdup(variable);
tmp[i + 1] = NULL;
// printf("debut %p\n", tmp[2]);
// printf("1--------------------\n");
free_double(cmd->env);
// printf("1--------------------\n");
cmd->env = (char **)malloc(sizeof(char *) * (tab_len(tmp) + 1));
if (!cmd->env)
printf("no env\n");
i = 0;
while (tmp[i])
{
cmd->env[i] = ft_strdup(tmp[i]);
// if (tmp[size])
// free(tmp[size]);
// printf("tmp----%s\n", tmp[size]);
// printf("envy---%s\n", cmd->env[size]);
i++;
}
cmd->env[i] = NULL;
// printf("2--------------------\n");
if (tmp)
free_double(tmp);
// printf("2--------------------\n");
// int i = 0;
// while (tmp[i])
// {
// printf("nano");
// free(tmp[i]);
// i++;
// }
// if(tmp)
// free(tmp);
// join_variable(cmd, variable, size, tmp);
}
void ft_env(t_s_cmd *cmd, char **env)
{
int i;
i = 0;
while (env[i])
i++;
cmd->env = ft_calloc(sizeof(char *), i + 1);
i = 0;
while (env[i])
{
cmd->env[i] = ft_strdup(env[i]);
i++;
}
cmd->env[i] = NULL;
}
int find_pwd(t_s_cmd *cmd)
{
int i;
i = 0;
while (cmd->env[i])
{
if (!ft_strncmp(cmd->env[i], "PWD=", 4))
return (i);
i++;
}
return (0);
// printf("%s\n", cmd->env[i]);
}
void init_s_cmd(t_s_cmd *cmd, char **env)
{
ft_env(cmd, env);
}
int tab_len(char **tab)
{
int i;
i = 0;
while (tab[i])
i++;
return (i);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 18:26:29 by sadjigui #+# #+# */
/* Updated: 2022/04/05 03:54:57 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:59:24 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,36 +20,36 @@ void unset_variable(t_s_cmd *cmd, int i)
a = 0;
b = 0;
tmp = malloc(sizeof(char *) * tab_len(cmd->env));
tmp = malloc(sizeof(char *) * tab_len(cmd->big_cmd->env));
if (!tmp)
printf("no tmp\n");
//ft_exit(cmd);
while (cmd->env[a])
while (cmd->big_cmd->env[a])
{
// if (a == i)
// a++;
printf("%s\n", cmd->env[a]);
printf("%s\n", cmd->big_cmd->env[a]);
if (a != i)
{
tmp[b] = ft_substr(cmd->env[a], 0, ft_strlen(cmd->env[a]));
tmp[b] = ft_substr(cmd->big_cmd->env[a], 0, ft_strlen(cmd->big_cmd->env[a]));
b++;
}
a++;
}
tmp[b] = NULL;
free_double(cmd->env);
free_double(cmd->big_cmd->env);
a = 0;
cmd->env = malloc(sizeof(char *) * (tab_len(tmp) + 1));
if (!cmd->env)
cmd->big_cmd->env = malloc(sizeof(char *) * (tab_len(tmp) + 1));
if (!cmd->big_cmd->env)
printf("no env\n");
//ft_exit(cmd);
while (tmp[a])
{
cmd->env[a] = ft_strdup(tmp[a]);
// printf("%s\n", cmd->env[a]);
cmd->big_cmd->env[a] = ft_strdup(tmp[a]);
// printf("%s\n", cmd->big_cmd->env[a]);
a++;
}
cmd->env[a] = NULL;
cmd->big_cmd->env[a] = NULL;
if (tmp)
free_double(tmp);
printf("2-------------\n");
@ -68,9 +68,9 @@ void find_variable(char *variable, t_s_cmd *cmd)
// printf("variable == %s\n", variable);
while (str[j])
j++;
while (cmd->env[i] && !(ft_strncmp(cmd->env[i], str, j) == 0))
while (cmd->big_cmd->env[i] && !(ft_strncmp(cmd->big_cmd->env[i], str, j) == 0))
i++;
if (i == tab_len(cmd->env))
if (i == tab_len(cmd->big_cmd->env))
return ;
unset_variable(cmd, i);
free(str);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
/* Updated: 2022/04/07 19:29:27 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:47:37 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
void crtl_c(int num)
{
// close(0);
printf("\n");
rl_replace_line("", 0);
rl_on_new_line();
@ -41,12 +42,12 @@ void print_prompt(char **path)
i = 0;
input = readline("\033[1;31m~$ \033[0m");
if (!input)
exit_shell(cmd);
exit_shell(cmd, path);
add_history(input);
if (!ft_strcmp("exit", input) && input)
{
rl_clear_history();
exit_shell(cmd);
free(input);
exit_shell(cmd, path);
}
if (ft_strlen(input) && next_space(input, 0) && input)
{
@ -55,7 +56,8 @@ void print_prompt(char **path)
{
cmd->err_var = 0;
execute(cmd, path);
cmd = 0;
path = cmd->env;
free_cmd(cmd);
}
}
free(input);
@ -74,7 +76,7 @@ char **ft_dup_double(char **env)
i = 0;
while (env[i])
{
new_tab[i] = ft_strdup(env[i]);
new_tab[i] = ft_strjoin(env[i], 0);
i++;
}
new_tab[i] = NULL;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
/* Updated: 2022/04/07 19:08:54 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:29:40 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -99,9 +99,9 @@ void execute(t_cmd *cmd, char **env)
{
int fdpipe[2];
int fdout;
int fdin;
int tmpin;
int tmpout;
int fdin;
int i;
i = 0;
@ -134,7 +134,7 @@ void execute(t_cmd *cmd, char **env)
fdout=dup(tmpout);
cmd->current_s_cmd->fd[0] = fdin;
cmd->current_s_cmd->fd[1] = fdout;
if (is_builtin(cmd->current_s_cmd->cmd))
if (i == 0 && is_builtin(cmd->current_s_cmd->cmd))
call_builtin(cmd, env);
else
exec_cmd(cmd, env, 0);
@ -163,8 +163,8 @@ void execute(t_cmd *cmd, char **env)
}
close_pipe(cmd);
wait_exit(cmd);
dup2(tmpin,0);
dup2(tmpout,1);
dup2(tmpin, 0);
dup2(tmpout, 1);
close(tmpin);
close(tmpout);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 16:58:40 by apommier #+# #+# */
/* Updated: 2022/03/11 16:57:52 by apommier ### ########.fr */
/* Updated: 2022/04/08 01:03:54 by apommier ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,17 +6,23 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
/* Updated: 2022/04/07 14:00:58 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:48:22 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
void exit_shell(t_cmd *cmd)
void exit_shell(t_cmd *cmd, char **env)
{
int i;
i = 0;
free_double(cmd->env);
clear_history();
//print_double_fd(cmd->s_cmds[0]->env, 1);
ft_putstr_fd("exit\n", 1);
//if (cmd->path)
//free_double(cmd->path);
cmd = 0;
exit(1);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
/* Updated: 2022/04/07 17:26:11 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:53:02 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -68,7 +68,8 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds, char **env)
free_cmd(cmd);
return (0);
}
cmd->s_cmds[i]->env = env;
//cmd->s_cmds[i]->env = env;
cmd->s_cmds[i]->big_cmd = cmd;
i++;
}
cmd->s_cmds[i] = 0;
@ -89,9 +90,13 @@ t_cmd *set_cmd(char *input, char **env)
cmd->s_cmds = malloc(sizeof(t_s_cmd) * double_size(cmds) + 1);
if (!cmd->s_cmds)
return (0);
cmd->path = 0;
cmd->s_cmds[double_size(cmds)] = 0;
cmd->path = get_path(env);
cmd->err_var = 0;
cmd->env = env;
//set_quote_and_var;
//cmd->err_var = 0;
//cmd->outfile = 0;
//cmd->infile = 0;
cmd->nb_s_cmd = double_size(cmds);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 16:35:37 by apommier #+# #+# */
/* Updated: 2022/04/03 20:20:14 by apommier ### ########.fr */
/* Updated: 2022/04/09 04:34:47 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,7 +47,6 @@ void print_double_fd(char **tab, int fd)
while (tab[i])
{
ft_putstr_fd(tab[i], fd);
if (tab[i + 1])
ft_putstr_fd("\n", fd);
i++;
}