Start work on exec
This commit is contained in:
parent
1b2f6e4225
commit
24d122dc54
3 changed files with 59 additions and 79 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */
|
/* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/04/29 15:07:54 by rparodi ### ########.fr */
|
/* Updated: 2024/04/30 15:42:51 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -14,6 +14,7 @@
|
||||||
#define MINISHELL_H
|
#define MINISHELL_H
|
||||||
|
|
||||||
# include "me/types.h"
|
# include "me/types.h"
|
||||||
|
# include "app/node.h"
|
||||||
|
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <fcntl.h>
|
# include <fcntl.h>
|
||||||
|
|
@ -29,6 +30,11 @@
|
||||||
|
|
||||||
# define PATH_FILES "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
|
# define PATH_FILES "/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games"
|
||||||
|
|
||||||
|
typedef struct s_myparser
|
||||||
|
{
|
||||||
|
t_parser *parser;
|
||||||
|
} t_myparser;
|
||||||
|
|
||||||
typedef struct s_utils
|
typedef struct s_utils
|
||||||
{
|
{
|
||||||
t_str name_shell;
|
t_str name_shell;
|
||||||
|
|
@ -36,6 +42,8 @@ typedef struct s_utils
|
||||||
t_str *strs_input;
|
t_str *strs_input;
|
||||||
t_str *path;
|
t_str *path;
|
||||||
t_str *envp;
|
t_str *envp;
|
||||||
|
t_myparser parser;
|
||||||
|
t_node current_node;
|
||||||
} t_utils;
|
} t_utils;
|
||||||
|
|
||||||
t_i32 main(t_i32 argc, t_str argv[], t_str arge[]);
|
t_i32 main(t_i32 argc, t_str argv[], t_str arge[]);
|
||||||
|
|
|
||||||
|
|
@ -6,39 +6,12 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/04/13 20:26:13 by rparodi #+# #+# */
|
/* Created: 2024/04/13 20:26:13 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/04/13 20:37:13 by rparodi ### ########.fr */
|
/* Updated: 2024/04/30 15:30:52 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "../includes/minishell.h"
|
#include "../includes/minishell.h"
|
||||||
|
|
||||||
char *ft_check_cmds(t_utils *utils, char *command)
|
|
||||||
{
|
|
||||||
size_t i;
|
|
||||||
char *str;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
str = NULL;
|
|
||||||
if (access(command, X_OK) != -1)
|
|
||||||
return (command);
|
|
||||||
else if (ft_strlen(command) != 1)
|
|
||||||
{
|
|
||||||
while (utils->path[i] != NULL)
|
|
||||||
{
|
|
||||||
if (str)
|
|
||||||
ft_free(str);
|
|
||||||
str = ft_strjoin(utils->path[i], command);
|
|
||||||
if (str == NULL)
|
|
||||||
ft_exit(utils, 1);
|
|
||||||
else if (access(str, F_OK) == -1 || access(str, X_OK) == -1)
|
|
||||||
i++;
|
|
||||||
else
|
|
||||||
return (str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (ft_exit(utils, 1), NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[])
|
void ft_exec_cmd(t_utils *utils, t_str cmd, t_str cmd_args[])
|
||||||
{
|
{
|
||||||
if (execve(cmd, cmd_args, utils->envp) == -1)
|
if (execve(cmd, cmd_args, utils->envp) == -1)
|
||||||
|
|
@ -56,7 +29,7 @@ void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i)
|
||||||
t_str *args;
|
t_str *args;
|
||||||
t_usize k;
|
t_usize k;
|
||||||
t_usize tmp;
|
t_usize tmp;
|
||||||
t_str cmd;
|
// t_str cmd;
|
||||||
|
|
||||||
k = prev_i;
|
k = prev_i;
|
||||||
tmp = prev_i;
|
tmp = prev_i;
|
||||||
|
|
@ -70,12 +43,12 @@ void ft_other_cmd(t_utils *shcat, t_usize i, t_usize prev_i)
|
||||||
k++;
|
k++;
|
||||||
}
|
}
|
||||||
args[k] = NULL;
|
args[k] = NULL;
|
||||||
cmd = ft_check_cmds(shcat, ft_strjoin("/", args[tmp]));
|
// cmd = ft_check_cmds(shcat, args[tmp]);
|
||||||
options = 0;
|
options = 0;
|
||||||
pid = fork();
|
pid = fork();
|
||||||
if (pid == -1)
|
if (pid == -1)
|
||||||
ft_exit(shcat, 1);
|
ft_exit(shcat, 1);
|
||||||
if (pid == 0)
|
// if (pid == 0)
|
||||||
ft_exec_cmd(shcat, cmd, args);
|
// ft_exec_cmd(shcat, cmd, args);
|
||||||
waitpid(pid, NULL, options);
|
waitpid(pid, NULL, options);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
|
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
|
||||||
/* Updated: 2024/04/30 13:02:39 by maiboyer ### ########.fr */
|
/* Updated: 2024/04/30 15:46:55 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -16,6 +16,36 @@
|
||||||
#include "parser/parser.h"
|
#include "parser/parser.h"
|
||||||
#include "../includes/minishell.h"
|
#include "../includes/minishell.h"
|
||||||
|
|
||||||
|
void print_node_data(t_node *t, t_usize depth)
|
||||||
|
{
|
||||||
|
t_usize idx;
|
||||||
|
|
||||||
|
idx = 0;
|
||||||
|
while (idx++ < depth)
|
||||||
|
printf("\t");
|
||||||
|
idx = 0;
|
||||||
|
printf("%s = %s\n", t->kind_str, node_getstr(t));
|
||||||
|
while (idx < t->childs_count)
|
||||||
|
print_node_data(&t->childs[idx++], depth + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_node parse_to_nodes(t_parser *parser, t_const_str input)
|
||||||
|
{
|
||||||
|
t_parse_tree *tree;
|
||||||
|
t_parse_node node;
|
||||||
|
t_node ret;
|
||||||
|
|
||||||
|
tree = ts_parser_parse_string(parser, NULL, input, str_len(input));
|
||||||
|
node = ts_tree_root_node(tree);
|
||||||
|
ret = build_node(node, input);
|
||||||
|
ts_tree_delete(tree);
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
t_node parse_str(t_myparser *parser, t_const_str input)
|
||||||
|
{
|
||||||
|
return (parse_to_nodes(parser->parser, input));
|
||||||
|
}
|
||||||
|
|
||||||
void ft_check(t_utils *shcat, char **input) {
|
void ft_check(t_utils *shcat, char **input) {
|
||||||
t_usize i;
|
t_usize i;
|
||||||
t_usize prev_i;
|
t_usize prev_i;
|
||||||
|
|
@ -34,6 +64,11 @@ void ft_check(t_utils *shcat, char **input) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void exec_shcat(t_utils *shcat)
|
||||||
|
{
|
||||||
|
print_node_data(&shcat->current_node, 0);
|
||||||
|
}
|
||||||
|
|
||||||
void ft_take_args(t_utils *shcat)
|
void ft_take_args(t_utils *shcat)
|
||||||
{
|
{
|
||||||
t_i32 i;
|
t_i32 i;
|
||||||
|
|
@ -43,10 +78,8 @@ void ft_take_args(t_utils *shcat)
|
||||||
shcat->str_input = readline((t_const_str)shcat->name_shell);
|
shcat->str_input = readline((t_const_str)shcat->name_shell);
|
||||||
if (!shcat->str_input)
|
if (!shcat->str_input)
|
||||||
ft_exit(shcat, 0);
|
ft_exit(shcat, 0);
|
||||||
shcat->strs_input = ft_split(shcat->str_input, ' ');
|
shcat->current_node = parse_str(&shcat->parser, shcat->str_input);
|
||||||
if (!shcat->strs_input)
|
exec_shcat(shcat);
|
||||||
exit(1);
|
|
||||||
ft_check(shcat, shcat->strs_input);
|
|
||||||
add_history(shcat->str_input);
|
add_history(shcat->str_input);
|
||||||
ft_free_strs(shcat->strs_input);
|
ft_free_strs(shcat->strs_input);
|
||||||
free(shcat->str_input);
|
free(shcat->str_input);
|
||||||
|
|
@ -75,36 +108,8 @@ void ft_find_path(t_str arge[], t_utils *utils)
|
||||||
|
|
||||||
t_language *tree_sitter_bash(void);
|
t_language *tree_sitter_bash(void);
|
||||||
|
|
||||||
t_node parse_to_nodes(t_parser *parser, t_const_str input)
|
|
||||||
{
|
|
||||||
t_parse_tree *tree;
|
|
||||||
t_parse_node node;
|
|
||||||
t_node ret;
|
|
||||||
|
|
||||||
tree = ts_parser_parse_string(parser, NULL, input, str_len(input));
|
|
||||||
node = ts_tree_root_node(tree);
|
|
||||||
ret = build_node(node, input);
|
|
||||||
ts_tree_delete(tree);
|
|
||||||
return (ret);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_node_data(t_node *t, t_usize depth)
|
|
||||||
{
|
|
||||||
t_usize idx;
|
|
||||||
|
|
||||||
idx = 0;
|
|
||||||
while (idx++ < depth)
|
|
||||||
printf("\t");
|
|
||||||
idx = 0;
|
|
||||||
printf("%s = %s\n", t->kind_str, node_getstr(t));
|
|
||||||
while (idx < t->childs_count)
|
|
||||||
print_node_data(&t->childs[idx++], depth + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct s_myparser
|
|
||||||
{
|
|
||||||
t_parser *parser;
|
|
||||||
} t_myparser;
|
|
||||||
|
|
||||||
t_myparser create_myparser(void)
|
t_myparser create_myparser(void)
|
||||||
{
|
{
|
||||||
|
|
@ -122,23 +127,17 @@ void free_myparser(t_myparser self)
|
||||||
ts_parser_delete(self.parser);
|
ts_parser_delete(self.parser);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_node parse_string(t_myparser *parser, t_const_str input)
|
|
||||||
{
|
|
||||||
return (parse_to_nodes(parser->parser, input));
|
|
||||||
}
|
|
||||||
|
|
||||||
t_i32 main(t_i32 argc, t_str argv[], t_str arge[])
|
t_i32 main(t_i32 argc, t_str argv[], t_str arge[])
|
||||||
{
|
{
|
||||||
// t_myparser parser;
|
|
||||||
// t_node node;
|
|
||||||
t_utils utils;
|
t_utils utils;
|
||||||
|
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
utils.parser = create_myparser();
|
||||||
ft_find_path(arge, &utils);
|
ft_find_path(arge, &utils);
|
||||||
utils.name_shell = "42sh > ";
|
utils.name_shell = "42sh > ";
|
||||||
ft_take_args(&utils);
|
ft_take_args(&utils);
|
||||||
// parser = create_myparser();
|
|
||||||
// node = parse_string(&parser, "banane \"$VAR\"'truc'");
|
// node = parse_string(&parser, "banane \"$VAR\"'truc'");
|
||||||
// print_node_data(&node, 0);
|
// print_node_data(&node, 0);
|
||||||
// free_node(node);
|
// free_node(node);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue