exec pt1
This commit is contained in:
@@ -3,17 +3,17 @@
|
||||
/* ::: :::::::: */
|
||||
/* ast.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 08:23:27 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/24 08:23:27 by nalebrun ### ########.fr */
|
||||
/* Created: 2025/01/24 08:23:27 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/27 14:15:24 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef AST_H
|
||||
# define AST_H
|
||||
|
||||
# include "../../includes/env.h"
|
||||
# include "env.h"
|
||||
|
||||
typedef enum e_state
|
||||
{
|
||||
25
includes/exec.h
Normal file
25
includes/exec.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exec.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/27 11:21:23 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/27 15:02:20 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef EXEC_H
|
||||
#define EXEC_H
|
||||
|
||||
# include <stdio.h>
|
||||
# include <sys/wait.h>
|
||||
# include <sys/types.h>
|
||||
# include <stdlib.h>
|
||||
# include <unistd.h>
|
||||
# include "ast.h"
|
||||
|
||||
int execute_command(t_ast_n *node);
|
||||
|
||||
#endif
|
||||
@@ -1,3 +1,15 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* minishell.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/27 11:25:25 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/27 14:15:38 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef MINISHELL_H
|
||||
# define MINISHELL_H
|
||||
|
||||
@@ -11,6 +23,8 @@
|
||||
# include "../lib/libft/libft.h"
|
||||
# include "builtins.h"
|
||||
# include "env.h"
|
||||
# include "exec.h"
|
||||
# include "ast.h"
|
||||
|
||||
# define POW1 "\033[1;38;2;21;22;26;48;2;92;106;178m"
|
||||
# define POW2 "\033[1;38;2;92;106;178;48;2;54;54;54m"
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
/* ::: :::::::: */
|
||||
/* ast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 08:22:16 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/24 08:22:16 by nalebrun ### ########.fr */
|
||||
/* Created: 2025/01/24 08:22:16 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/27 15:40:58 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -48,18 +48,21 @@ t_ast_n *return_hardcode_ast(char **envp)
|
||||
{
|
||||
t_ast_n *head;
|
||||
|
||||
head = created_ast_n(_AND, NULL, NULL);
|
||||
head = created_ast_n(_CMD, NULL, NULL);
|
||||
head->head = head;
|
||||
setup_cmd(head, "ls", "ls -l");
|
||||
// head = created_ast_n(_AND, NULL, NULL);
|
||||
head->env = init_env(envp);
|
||||
head->left = created_ast_n(_CMD, head, head);
|
||||
setup_cmd(head->left, "echo", "echo coucou");
|
||||
head->right = created_ast_n(_PLINE, head, head);
|
||||
head->right->pline = malloc(sizeof(t_ast_n *) * 4);
|
||||
head->right->pline[0] = created_ast_n(_CMD, head->right, head);
|
||||
setup_cmd(head->right->pline[0], "ls", "ls");
|
||||
head->right->pline[1] = created_ast_n(_CMD, head->right, head);
|
||||
setup_cmd(head->right->pline[1], "cat", "cat -e");
|
||||
head->right->pline[2] = created_ast_n(_CMD, head->right, head);
|
||||
setup_cmd(head->right->pline[2], "wc", "wc -l");
|
||||
head->right->pline[3] = NULL;
|
||||
// head->left = created_ast_n(_CMD, head, head);
|
||||
// setup_cmd(head->left, "echo", "echo coucou");
|
||||
// head->right = created_ast_n(_PLINE, head, head);
|
||||
// head->right->pline = malloc(sizeof(t_ast_n *) * 4);
|
||||
// head->right->pline[0] = created_ast_n(_CMD, head->right, head);
|
||||
// setup_cmd(head->right->pline[0], "ls", "ls");
|
||||
// head->right->pline[1] = created_ast_n(_CMD, head->right, head);
|
||||
// setup_cmd(head->right->pline[1], "cat", "cat -e");
|
||||
// head->right->pline[2] = created_ast_n(_CMD, head->right, head);
|
||||
// setup_cmd(head->right->pline[2], "wc", "wc -l");
|
||||
// head->right->pline[3] = NULL;
|
||||
return (head);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 14:32:13 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/24 14:32:13 by lderidde ### ########.fr */
|
||||
/* Updated: 2025/01/27 11:19:17 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -112,12 +112,12 @@ void builtin_exit(char *str, bool depth)
|
||||
ft_putendl_fd(": numeric argument required", 2);
|
||||
exit(2);
|
||||
}
|
||||
exit(ft_atoi(arg) % 256);
|
||||
exit(ft_atol(arg) % 256);
|
||||
}
|
||||
if (count_arg(str) >= 2)
|
||||
bash_exiterrorcount();
|
||||
else if (ft_isnumeric(arg))
|
||||
bash_exit(ft_atoi(arg) % 256);
|
||||
bash_exit(ft_atol(arg) % 256);
|
||||
else
|
||||
bash_exit_errornum(arg);
|
||||
}
|
||||
|
||||
212
srcs/execution/exec.c
Normal file
212
srcs/execution/exec.c
Normal file
@@ -0,0 +1,212 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* exec.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/27 15:43:24 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
// int is_builtin(char *str)
|
||||
// {
|
||||
// if (ft_strncmp(str, "exit", 4) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "pwd", 3) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "echo", 4) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "env", 3) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "unset", 5) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "cd", 2) == 0)
|
||||
// return (1);
|
||||
// if (ft_strncmp(str, "export", 6) == 0)
|
||||
// return (1);
|
||||
// }
|
||||
|
||||
// int exec_builtin(t_ast_n *node)
|
||||
// {
|
||||
// if (ft_strncmp(node->cmd, "exit", 4) == 0)
|
||||
// return (builtin_exit(node->cmd, true));
|
||||
// if (ft_strncmp(node->cmd, "pwd", 3) == 0)
|
||||
// return (builtin_pwd(node->cmd));
|
||||
// if (ft_strncmp(node->cmd, "echo", 4) == 0)
|
||||
// return (builtin_echo(ft_split(node->arg, " "), node->head->env));
|
||||
// if (ft_strncmp(node->cmd, "env", 3) == 0)
|
||||
// return (builtin_env(node->cmd, node->head->env));
|
||||
// if (ft_strncmp(node->cmd, "unset", 5) == 0)
|
||||
// return (builtin_unset(ft_split(node->arg, " "), node->head));
|
||||
// if (ft_strncmp(node->cmd, "cd", 2) == 0)
|
||||
// return (builtin_cd(ft_split(node->arg, " "), node->head));
|
||||
// if (ft_strncmp(node->cmd, "export", 6) == 0)
|
||||
// return (builtin_export(ft_split(node->arg, " "), node->head));
|
||||
// }
|
||||
|
||||
// void free_tab(char **tab)
|
||||
// {
|
||||
// int i;
|
||||
//
|
||||
// i = -1;
|
||||
// while (tab[++i])
|
||||
// free(tab[i]);
|
||||
// free(tab);
|
||||
// }
|
||||
|
||||
char *find_path(char *cmd, char **env)
|
||||
{
|
||||
char *tmp;
|
||||
char *path;
|
||||
char **paths;
|
||||
int i;
|
||||
|
||||
// if (access(cmd, F_OK) == 0)
|
||||
// return (cmd);
|
||||
i = 0;
|
||||
ft_printf("test2\n");
|
||||
while (ft_strnstr(env[i], "PATH=", 5) == NULL)
|
||||
i++;
|
||||
paths = ft_split(env[i] + 5, ":");
|
||||
i = -1;
|
||||
while (paths[++i])
|
||||
{
|
||||
tmp = ft_strjoin(paths[i], "/");
|
||||
path = ft_strjoin(tmp, cmd);
|
||||
printf("path: %s\n", path);
|
||||
free(tmp);
|
||||
if (access(path, F_OK) == 0)
|
||||
return (free_tab(paths), path);
|
||||
free(path);
|
||||
}
|
||||
free_tab(paths);
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
void return_error(char *arg, char *msg, int code)
|
||||
{
|
||||
ft_fprintf(2, "%s: %s\n", arg, msg);
|
||||
exit(code);
|
||||
}
|
||||
|
||||
void handle_file(t_ast_n *node, int check)
|
||||
{
|
||||
int fd;
|
||||
|
||||
if (check == 1)
|
||||
fd = open(node->infile, O_RDONLY);
|
||||
else if (check == 2)
|
||||
fd = open(node->outfile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||
else if (check == 3)
|
||||
fd = open(node->outfile, O_WRONLY | O_CREAT | O_APPEND, 0666);
|
||||
if (fd == -1)
|
||||
{
|
||||
perror("open");
|
||||
exit(1);
|
||||
}
|
||||
if (check == 1)
|
||||
node->_stdin = fd;
|
||||
else if (check == 2 || check == 3)
|
||||
node->_stdout = fd;
|
||||
}
|
||||
|
||||
void handle_redir(t_ast_n *node)
|
||||
{
|
||||
if (node->redir == _NR)
|
||||
return ;
|
||||
else if (node->redir == _RED_L)
|
||||
handle_file(node, 1);
|
||||
else if (node->redir == _RED_R)
|
||||
handle_file(node , 2);
|
||||
else if (node->redir == _RED_DR)
|
||||
handle_file(node, 3);
|
||||
if (node->redir == _RED_L)
|
||||
{
|
||||
dup2(node->_stdin, STDIN_FILENO);
|
||||
close(node->_stdin);
|
||||
}
|
||||
else if (node->redir == _RED_R || node->redir == _RED_DR)
|
||||
{
|
||||
dup2(node->_stdout, STDOUT_FILENO);
|
||||
close(node->_stdout);
|
||||
}
|
||||
}
|
||||
|
||||
int exec(t_ast_n *node)
|
||||
{
|
||||
char *path;
|
||||
|
||||
handle_redir(node);
|
||||
ft_printf("test1\n");
|
||||
path = find_path(node->cmd, node->head->env);
|
||||
ft_printf("test3\n");
|
||||
printf("path: %s\n", path);
|
||||
if (!path)
|
||||
return_error(node->cmd, "command not found", 127);
|
||||
if (access(path, X_OK) != 0)
|
||||
{
|
||||
free(path);
|
||||
return_error(path, "Permission denied", 126);
|
||||
}
|
||||
execve(path, node->args, NULL);
|
||||
free(path);
|
||||
perror("execve");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int exec_scmd(t_ast_n *node)
|
||||
{
|
||||
pid_t pid;
|
||||
int status;
|
||||
|
||||
// if (is_builtin(node->cmd))
|
||||
// return (exec_builtin(node));
|
||||
// else
|
||||
// {
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
exec(node);
|
||||
}
|
||||
else
|
||||
{
|
||||
wait(NULL);
|
||||
return (0);
|
||||
if (WIFEXITED(status))
|
||||
return (WEXITSTATUS(status));
|
||||
else
|
||||
return (-1);
|
||||
}
|
||||
return (0);
|
||||
// }
|
||||
}
|
||||
|
||||
int execute_command(t_ast_n *node)
|
||||
{
|
||||
int status;
|
||||
|
||||
printf("test100\n");
|
||||
if (node->state == _CMD)
|
||||
return (exec_scmd(node));
|
||||
else if (node->state == _AND)
|
||||
{
|
||||
status = execute_command(node->left);
|
||||
if (WEXITSTATUS(status) == 0)
|
||||
return (execute_command(node->right));
|
||||
return (status);
|
||||
}
|
||||
else if (node->state == _OR)
|
||||
{
|
||||
status = execute_command(node->left);
|
||||
if (WEXITSTATUS(status) != 0)
|
||||
return (execute_command(node->right));
|
||||
return (status);
|
||||
}
|
||||
return (0);
|
||||
// else if (node->state == _PLINE)
|
||||
// return (exec_pipe(node));
|
||||
}
|
||||
73
srcs/main.c
73
srcs/main.c
@@ -1,3 +1,15 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/27 15:13:16 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/minishell.h"
|
||||
|
||||
char *powerline(void)
|
||||
@@ -40,43 +52,44 @@ char **ft_setnewenv(void)
|
||||
return (envp);
|
||||
}
|
||||
|
||||
static t_data *init_data(char **envp)
|
||||
{
|
||||
t_data *data;
|
||||
|
||||
data = malloc (sizeof(t_data));
|
||||
data->env = init_env(envp);
|
||||
return (data);
|
||||
}
|
||||
// static t_data *init_data(char **envp)
|
||||
// {
|
||||
// t_data *data;
|
||||
//
|
||||
// data = malloc (sizeof(t_data));
|
||||
// data->env = init_env(envp);
|
||||
// return (data);
|
||||
// }
|
||||
|
||||
int main(int ac, char **av, char **envp)
|
||||
{
|
||||
char *input;
|
||||
t_data *data;
|
||||
// char *input;
|
||||
t_ast_n *node;
|
||||
|
||||
(void)ac;
|
||||
(void)av;
|
||||
// if (!envp[0])
|
||||
// env = ft_setnewenv();
|
||||
data = init_data(envp);
|
||||
while (1)
|
||||
{
|
||||
input = powerline();
|
||||
if (ft_strncmp(input, "exit", 4) == 0)
|
||||
builtin_exit(input, true);
|
||||
if (ft_strncmp(input, "pwd", 3) == 0)
|
||||
builtin_pwd(input);
|
||||
if (ft_strncmp(input, "echo", 4) == 0)
|
||||
builtin_echo(ft_split(input, " "), data->env);
|
||||
if (ft_strncmp(input, "env", 3) == 0)
|
||||
builtin_env(input, data->env);
|
||||
if (ft_strncmp(input, "unset", 5) == 0)
|
||||
builtin_unset(ft_split(input, " "), data);
|
||||
if (ft_strncmp(input, "cd", 2) == 0)
|
||||
builtin_cd(ft_split(input, " "), data);
|
||||
if (ft_strncmp(input, "export", 6) == 0)
|
||||
builtin_export(ft_split(input, " "), data);
|
||||
free(input);
|
||||
}
|
||||
node = return_hardcode_ast(envp);
|
||||
execute_command(node);
|
||||
// while (1)
|
||||
// {
|
||||
// input = powerline();
|
||||
// if (ft_strncmp(input, "exit", 4) == 0)
|
||||
// builtin_exit(input, true);
|
||||
// if (ft_strncmp(input, "pwd", 3) == 0)
|
||||
// builtin_pwd(input);
|
||||
// if (ft_strncmp(input, "echo", 4) == 0)
|
||||
// builtin_echo(ft_split(input, " "), data->env);
|
||||
// if (ft_strncmp(input, "env", 3) == 0)
|
||||
// builtin_env(input, data->env);
|
||||
// if (ft_strncmp(input, "unset", 5) == 0)
|
||||
// builtin_unset(ft_split(input, " "), data);
|
||||
// if (ft_strncmp(input, "cd", 2) == 0)
|
||||
// builtin_cd(ft_split(input, " "), data);
|
||||
// if (ft_strncmp(input, "export", 6) == 0)
|
||||
// builtin_export(ft_split(input, " "), data);
|
||||
// free(input);
|
||||
// }
|
||||
return (0);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user