Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facetint #14

Merged
merged 7 commits into from
Mar 5, 2024
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
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ a.out
idea/
.idea/
.vscode/
.DS_Store
10 changes: 5 additions & 5 deletions builtin/builtin.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ int isbuiltin(char *cmd)
void handle_builtin(t_command *cmd, int fd[2])
{
if (ft_strncmp(cmd->name, "echo", ft_strlen("echo")) == 0)
builtin_echo(cmd, fd);
return builtin_echo(cmd, fd);
else if (ft_strncmp(cmd->name, "export", ft_strlen("export")) == 0)
builtin_export(cmd, fd);
return builtin_export(cmd, fd);
else if (ft_strncmp(cmd->name, "env", ft_strlen("env")) == 0)
builtin_env(get_global_env());
return builtin_env(get_global_env());
else if (ft_strncmp(cmd->name, "exit", ft_strlen("exit")) == 0)
builtin_exit(cmd);
return builtin_exit(cmd);
else if (ft_strncmp(cmd->name, "pwd", ft_strlen("pwd")) == 0)
builtin_pwd(cmd);
return builtin_pwd(cmd);
if (ft_strncmp(cmd->name, "cd", ft_strlen("cd")) == 0)
return;
if (ft_strncmp(cmd->name, "unset", ft_strlen("unset")) == 0)
Expand Down
10 changes: 4 additions & 6 deletions env/env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/27 19:05:06 by facetint #+# #+# */
/* Updated: 2024/03/03 18:11:18 by facetint ### ########.fr */
/* Updated: 2024/03/03 23:25:13 by hcoskun42 ### ########.tr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -48,7 +48,7 @@ t_envList *push_list(char *key, char *value, t_envList *begin)
if (begin)
return (add_list(key, value, begin));
else
return (create_list(key, value));
return (create_list(key, value));
}
t_envList *make_list(char **env)
{
Expand All @@ -58,6 +58,7 @@ t_envList *make_list(char **env)
char *value;

i = 0;
lst = NULL;
while (env[i])
{
key = strdup_n(env[i], '=');
Expand Down Expand Up @@ -91,12 +92,9 @@ char **make_arr(t_envList *lst)
}
void print_list(t_envList *lst)
{
t_envList *last = NULL;
while (lst)
while (lst)
{
printf("%s=%s\n", lst->key, lst->value);
last = lst;
lst = lst->next;
}
}

118 changes: 55 additions & 63 deletions execute/execute.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: facetint <facetint@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/17 17:34:07 by facetint #+# #+# */
/* Updated: 2024/03/03 11:35:26 by facetint ### ########.fr */
/* Updated: 2024/03/03 23:31:12 by hcoskun42 ### ########.tr */
/* */
/* ************************************************************************** */

Expand All @@ -22,26 +22,22 @@

t_envList *get_global_env()
{
static t_envList env;

static t_envList env = (t_envList){0};
return (&env);
}
t_command *find_before_cmd(t_command *cmds, t_command *beginCmds)
{
if (cmds == beginCmds)
return (NULL);
while (beginCmds->next != cmds)
beginCmds = beginCmds->next;
return (beginCmds);
}

void read_and_print(int fd) {
void print_and_close(int fd) {
char *line;
while ((line = get_next_line(fd)) != NULL)

while (1)
{
write(1, line, ft_strlen(line));
line = get_next_line(fd);
if (line == NULL)
break;
ft_putstr_fd(line, 1);
safe_free(line);
}
close(fd);
}

char **new_arr(char *new, char **arr)
Expand All @@ -53,68 +49,64 @@ char **new_arr(char *new, char **arr)
while (arr[counter])
counter++;
counter += 2;
ret_val = safe_malloc(sizeof(char *) * (counter));
ret_val[counter] = NULL;
ret_val = safe_malloc(sizeof(char *) * (counter));
ret_val[counter - 1] = NULL;
while (--counter > 0)
ret_val[counter] = arr[counter - 1];
ret_val[counter] = arr[counter - 1];
ret_val[0] = new;
return (ret_val);
}

void execute(t_command *cmds, t_command *beginCmds)
void execute_command(t_command *cmd, t_command *before, int fd[2])
{
int fd[2];
int pid;
char *path_cmd;
char **arg;
t_command *before;
int builtin;
int pid;

builtin = isbuiltin(cmds->name);
before = find_before_cmd(cmds, beginCmds);

if (pipe(fd) == -1)
return ft_putstr_fd("Pipe error!", 2);
pid = fork();
if (pid == -1)
return ft_putstr_fd("Fork error!", 2); //todo exit
if (pid > 0)
return;

if (!builtin)
path_cmd = find_path(cmd->name);
if (!path_cmd)
return ft_putstr_fd("Command not found.\n", 2);

if (before)
{
pid = fork();
if (pid == -1)
return ft_putstr_fd("Fork error!", 2);

path_cmd = find_path(cmds->name);
if (!path_cmd)
return ft_putstr_fd("command not found: ", 2);
arg = new_arr(cmds->name, cmds->args);

if (pid == 0) //child
{
if (before)
{
dup2(before->fd, STDIN_FILENO);
close(before->fd);
}
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
execve(path_cmd, arg, NULL);
}
//waitpid(pid, NULL, 0);
safe_free(arg);
dup2(before->fd, STDIN_FILENO);
close(before->fd);
}
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
execve(path_cmd, new_arr(cmd->name, cmd->args), make_arr(get_global_env()));
}

void execute(t_command *cmds)
{
handle_command(NULL, cmds, cmds);
}

void handle_command(t_command *before, t_command *cmd, t_command *first_cmd)
{
int fd[2];

if (pipe(fd) == -1)
return ft_putstr_fd("Pipe error!", 2); //todo exit

if (isbuiltin(cmd->name))
handle_builtin(cmd, fd);
else
handle_builtin(cmds, fd);
execute_command(cmd, before, fd);

close(fd[1]);
if (before)
close(before->fd);
if (cmds->next)
{
cmds->fd = fd[0];
execute(cmds->next, beginCmds);
}
else
{
read_and_print(fd[0]);
close(fd[0]);
}

if (!cmd->next)
return print_and_close(fd[0]);

cmd->fd = fd[0];
handle_command(cmd, cmd->next, first_cmd);
}
4 changes: 2 additions & 2 deletions handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ void handle_input(char *input)
parser_data = parse(lexer_data);
handle_heredocs(parser_data);

debug(lexer_data, parser_data);
execute(parser_data, parser_data);
//debug(lexer_data, parser_data);
execute(parser_data);
wait(NULL);
uninit_tokens(lexer_data);
}
Expand Down
4 changes: 2 additions & 2 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ void debug(t_token *token, t_command *cmd);
char *ft_str_arr_join(char **str_list, unsigned int str_count);

// executer
void execute(t_command *cmds, t_command *beginCmds);
void execute(t_command *cmds);
void handle_command(t_command *before, t_command *cmds, t_command *beginCmds);
char *find_path(char *cmd);

// builtin
Expand All @@ -113,5 +114,4 @@ void builtin_unset(t_command *cmd);
void builtin_cd(t_command *cmd);
void handle_builtin(t_command *cmd, int fd[2]);


#endif
Binary file removed libft/libft.a
Binary file not shown.
3 changes: 0 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
int main(int ac, char **av, char **envp) {

*get_global_env() = *make_list(envp);
while (*envp)
printf("%s\n", *envp),
envp++;
(void)ac;
(void)av;

Expand Down
Empty file removed test
Empty file.
Loading