🎉」 init: Merged branch successfully

This commit is contained in:
2024-06-20 14:30:25 +02:00
15 changed files with 288 additions and 63 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */ /* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */
/* Updated: 2024/06/04 13:15:24 by adjoly ### ########.fr */ /* Updated: 2024/06/18 12:58:53 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,6 +15,7 @@
# include "libft.h" # include "libft.h"
# include "tokenizer.h" # include "tokenizer.h"
# include "env.h"
typedef struct s_cmd typedef struct s_cmd
{ {
@ -28,15 +29,19 @@ typedef enum s_quote
{ {
NOT_CLOSED = -1, NOT_CLOSED = -1,
FALSE, FALSE,
SINGLE, SINGLE = 39,
DOUBLE DOUBLE = 34
} t_quote; } t_quote;
void check_syntax(char *readline, char **argv); bool check_syntax(char *readline, char **argv);
void send_error(char *msg, char **argv); void send_error(char *msg, char **argv);
void check_redir(t_list *redir, char **argv); void check_redir(t_list *redir, char **argv);
t_cmd *get_redir_fd(void *content); t_cmd *get_redir_fd(void *content, t_env *env);
t_list *get_cmd_list(t_list *list); t_list *get_cmd_list(t_list *list, t_env *env);
char **split_argv(char *readline);
char *env_var_replace(char *readline, t_env *env);
size_t get_size_with_env(char *readline, t_env *env);
size_t strlen_till_char(char *s, int c);
/** /**
* @brief Take the argv of a command a split the argv and the * @brief Take the argv of a command a split the argv and the
* command it self * command it self

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 19:35:01 by mmoussou #+# #+# */ /* Created: 2024/01/03 19:35:01 by mmoussou #+# #+# */
/* Updated: 2024/05/23 20:01:55 by adjoly ### ########.fr */ /* Updated: 2024/06/06 14:37:07 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/01 22:03:09 by mmoussou #+# #+# */ /* Created: 2023/11/01 22:03:09 by mmoussou #+# #+# */
/* Updated: 2023/11/11 13:19:18 by mmoussou ### ########.fr */ /* Updated: 2024/06/10 17:13:04 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,5 +23,5 @@ int ft_strncmp(const char *s1, const char *s2, unsigned int n)
return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
i++; i++;
} }
return (0); return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]);
} }

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */ /* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/06/04 21:42:55 by mmoussou ### ########.fr */ /* Updated: 2024/06/20 14:00:59 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -87,18 +87,18 @@ int main(int ac, char **av, char **env)
if (is_str(test, "exit")) if (is_str(test, "exit"))
break ; break ;
piped = tokenizer(test); piped = tokenizer(test);
check_redir(((t_token *)(piped->content))->redirection, av); //check_redir(((t_token *)(piped->content))->redirection, av);
cmd_list = get_cmd_list(piped); cmd_list = get_cmd_list(piped, &env_l);
exec_split_cmd(cmd_list, &env_l); //exec_split_cmd(cmd_list, &env_l);
/*while (cmd_list) /*while (cmd_list)
{ {
cmd = cmd_list->content; cmd = cmd_list->content;
cmd_list = cmd_list->next; cmd_list = cmd_list->next;
} }*/
print_cmd(cmd);*/ print_cmd(cmd_list->content);
free(test); //free(test);
ft_lstclear(&piped, free_token); //ft_lstclear(&piped, free_token);
ft_free("a", &lll); //ft_free("a", &lll);
} }
ft_free("a", &lll); ft_free("a", &lll);
return (0); return (0);
@ -110,6 +110,6 @@ int main(int ac, char **av, char **env)
t_quote d; t_quote d;
d = is_inquote(ll, 6); d = is_inquote(ll, 6);
ft_printf("%c\n", *(ll+6)); ft_printf("%c\n", *(ll+6))
print_quote_type(d); print_quote_type(d);
}*/ }*/

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 17:40:13 by adjoly #+# #+# */ /* Created: 2024/05/28 17:40:13 by adjoly #+# #+# */
/* Updated: 2024/05/29 11:37:06 by adjoly ### ########.fr */ /* Updated: 2024/06/04 16:43:53 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,7 +31,24 @@ bool check_triple(char *chevron)
return (false); return (false);
} }
void check_syntax(char *readline, char **argv) bool check_if_file(char *readline)
{
char *tmp;
tmp = readline;
if (is_chevron(*tmp))
{
while (*tmp && is_chevron(*tmp))
tmp++;
while (*tmp && !ft_isalnum(*tmp))
tmp++;
if (!*tmp)
return (true);
}
return (false);
}
bool check_syntax(char *readline, char **argv)
{ {
char *tmp; char *tmp;
@ -39,7 +56,16 @@ void check_syntax(char *readline, char **argv)
while (*tmp) while (*tmp)
{ {
if (check_triple(tmp)) if (check_triple(tmp))
{
send_error(ERROR_SYNTAX, argv); send_error(ERROR_SYNTAX, argv);
return (true);
}
if (check_if_file(tmp))
{
send_error(ERROR_SYNTAX, argv);
return (true);
}
tmp++; tmp++;
} }
return (false);
} }

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 18:09:49 by adjoly #+# #+# */ /* Created: 2024/05/28 18:09:49 by adjoly #+# #+# */
/* Updated: 2024/05/28 18:15:09 by adjoly ### ########.fr */ /* Updated: 2024/06/04 15:39:24 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,5 +16,4 @@ void send_error(char *msg, char **argv)
{ {
ft_putstr_fd(argv[0], STDERR_FILENO); ft_putstr_fd(argv[0], STDERR_FILENO);
ft_putendl_fd(msg, STDERR_FILENO); ft_putendl_fd(msg, STDERR_FILENO);
exit(EXIT_FAILURE);
} }

View File

@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_var_replace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 21:14:04 by adjoly #+# #+# */
/* Updated: 2024/06/20 14:14:57 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdio.h>
#include "parsing.h"
#include "env.h"
size_t strlen_till_notalnum(char *s)
{
char *tmp;
tmp = s;
while (*tmp && ft_isalnum(*tmp))
tmp++;
return (tmp - s);
}
char *env_var_replace(char *readline, t_env *env)
{
char *tmp;
char *rl_dollared;
size_t dollar_size;
char *dollar;
rl_dollared = ft_calloc(get_size_with_env(readline, env) + 1, sizeof(char));
if (rl_dollared == NULL)
return (NULL);
tmp = readline;
while (*tmp)
{
if (*tmp == '$' && is_inquote(readline, tmp - readline) != SINGLE)
{
tmp++;
dollar_size = strlen_till_notalnum(tmp);
printf("%zu\n", dollar_size);
dollar = env_getn_value(tmp, env, dollar_size - 1);
if (!dollar)
{
tmp += dollar_size;
continue ;
}
ft_strlcat(rl_dollared, dollar, ft_strlen(dollar) + ft_strlen(rl_dollared) + 1);
tmp += dollar_size;
}
else
{
ft_strlcat(rl_dollared, tmp, ft_strlen(rl_dollared) + 2);
tmp++;
}
}
return (rl_dollared);
}

View File

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_size_with_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/06 15:01:19 by adjoly #+# #+# */
/* Updated: 2024/06/10 17:11:16 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "env.h"
#include "parsing.h"
#include "libft.h"
size_t get_size_with_env(char *readline, t_env *env)
{
size_t size;
char *tmp;
char *dollar;
tmp = readline;
size = ft_strlen(readline);
while (*tmp)
{
if (*tmp == '$' && is_inquote(tmp, tmp - readline) != SINGLE && is_inquote(tmp, tmp - readline) != NOT_CLOSED)
{
tmp++;
size -= strlen_till_char(tmp, ' ');
dollar = env_getn_value(tmp, env, strlen_till_char(tmp, ' '));
if (!dollar)
continue ;
size += ft_strlen(dollar);
}
tmp++;
}
return (size);
}

View File

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strlen_till_char.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 01:09:40 by adjoly #+# #+# */
/* Updated: 2024/06/09 14:02:35 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
size_t strlen_till_char(char *s, int c)
{
char *tmp;
size_t size;
tmp = s;
size = 0;
while (*tmp && *tmp != c)
{
tmp++;
size++;
}
return (size);
}

View File

@ -6,14 +6,14 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/31 12:47:13 by adjoly #+# #+# */ /* Created: 2024/05/31 12:47:13 by adjoly #+# #+# */
/* Updated: 2024/05/31 13:29:46 by adjoly ### ########.fr */ /* Updated: 2024/06/18 12:58:36 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
#include "parsing.h" #include "parsing.h"
t_list *get_cmd_list(t_list *list) t_list *get_cmd_list(t_list *list, t_env *env)
{ {
t_list *tmp; t_list *tmp;
t_list *cmd_list; t_list *cmd_list;
@ -22,7 +22,7 @@ t_list *get_cmd_list(t_list *list)
cmd_list = NULL; cmd_list = NULL;
while (tmp) while (tmp)
{ {
ft_lstadd_back(&cmd_list, ft_lstnew(get_redir_fd(tmp->content))); ft_lstadd_back(&cmd_list, ft_lstnew(get_redir_fd(tmp->content, env)));
tmp = tmp->next; tmp = tmp->next;
} }
return (cmd_list); return (cmd_list);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/30 10:48:41 by adjoly #+# #+# */ /* Created: 2024/05/30 10:48:41 by adjoly #+# #+# */
/* Updated: 2024/05/31 19:16:41 by adjoly ### ########.fr */ /* Updated: 2024/06/20 12:30:56 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,53 +14,62 @@
#include "tokenizer.h" #include "tokenizer.h"
#include "execution.h" #include "execution.h"
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h>
#include "libft.h" #include "libft.h"
t_cmd *get_redir_fd(void *content) t_cmd *get_redir_fd(void *content, t_env *env)
{ {
t_token *token; t_token *token;
t_list *tmp; t_list *tmp;
t_redirection *tmp_redir; t_redirection *tmp_redir;
t_redirection out; t_redirection_sign out;
t_redirection in; t_redirection_sign in;
t_cmd *cmd; t_cmd *cmd;
token = (t_token *)content; token = (t_token *)content;
tmp = token->redirection; tmp = token->redirection;
cmd = NULL; cmd = NULL;
out.sign = INFILE; out = INFILE;
in.sign = OUTFILE; in = OUTFILE;
cmd = ft_calloc(sizeof(t_cmd), 1); cmd = ft_calloc(sizeof(t_cmd), 1);
while (tmp) while (tmp)
{ {
tmp_redir = (t_redirection *)tmp->content; tmp_redir = (t_redirection *)tmp->content;
if (tmp_redir->sign == (t_redirection_sign)HEREDOC) if (tmp_redir->sign == (t_redirection_sign)HEREDOC)
{ {
in.file_name = NULL; if (cmd->infile != 0)
in.sign = HEREDOC; close(cmd->infile);
close(cmd->infile); in = HEREDOC;
cmd->infile = ft_heredoc(tmp_redir->file_name); cmd->infile = ft_heredoc(tmp_redir->file_name);
} }
else if (tmp_redir->sign == INFILE) else if (tmp_redir->sign == INFILE)
{ {
in.sign = INFILE; if (cmd->infile != 0)
in.file_name = tmp_redir->file_name; close(cmd->infile);
cmd->infile = open(tmp_redir->file_name, O_RDONLY);
in = INFILE;
} }
else if (tmp_redir->sign == OUTFILE) else if (tmp_redir->sign == OUTFILE)
{
if (cmd->infile != 0)
close(cmd->outfile);
out = OUTFILE;
cmd->outfile = open(tmp_redir->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644); cmd->outfile = open(tmp_redir->file_name, O_CREAT | O_TRUNC | O_WRONLY, 0644);
}
else if (tmp_redir->sign == OUT_APPEND) else if (tmp_redir->sign == OUT_APPEND)
{
if (cmd->infile != 0)
close(cmd->outfile);
out = OUT_APPEND;
cmd->outfile = open(tmp_redir->file_name, O_CREAT | O_APPEND | O_WRONLY, 0644); cmd->outfile = open(tmp_redir->file_name, O_CREAT | O_APPEND | O_WRONLY, 0644);
}
tmp = tmp->next; tmp = tmp->next;
} }
if (in.sign == OUTFILE) if (in == OUTFILE)
cmd->infile = STDIN_FILENO; cmd->infile = STDIN_FILENO;
else if (in.sign == INFILE) if (out == INFILE)
{
cmd->infile = open(in.file_name, O_RDONLY);
ft_putendl_fd(in.file_name, STDOUT_FILENO);
}
if (out.sign == INFILE)
cmd->outfile = STDOUT_FILENO; cmd->outfile = STDOUT_FILENO;
cmd = split_cmd(token->argv, cmd); char *ll = env_var_replace(token->argv, env);
cmd = split_cmd(ll, cmd);
return (cmd); return (cmd);
} }

View File

@ -6,18 +6,19 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 20:06:13 by adjoly #+# #+# */ /* Created: 2024/05/20 20:06:13 by adjoly #+# #+# */
/* Updated: 2024/05/21 20:34:14 by adjoly ### ########.fr */ /* Updated: 2024/06/20 11:07:56 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "parsing.h" #include "parsing.h"
#include "libft.h" #include "libft.h"
#include <stdio.h>
t_quote __is_quote(char c) t_quote __is_quote(char c)
{ {
if (c == 39) if (c == SINGLE)
return (SINGLE); return (SINGLE);
if (c == 34) if (c == DOUBLE)
return (DOUBLE); return (DOUBLE);
return (FALSE); return (FALSE);
} }
@ -27,12 +28,8 @@ char *search_for_next_quote(char *s, t_quote quote_type)
char *tmp; char *tmp;
tmp = s; tmp = s;
while (*tmp) while (*tmp && __is_quote(*tmp) != quote_type)
{
if (__is_quote(*tmp) == quote_type)
break ;
tmp++; tmp++;
}
return (tmp); return (tmp);
} }
@ -43,19 +40,20 @@ t_quote is_inquote(char *s, size_t i)
t_quote quote_type; t_quote quote_type;
start_quote = 0; start_quote = 0;
printf("%zu", i);
tmp = s; tmp = s;
quote_type = FALSE; quote_type = FALSE;
while (*tmp) while (*tmp)
{ {
if ((size_t)(tmp - s) > i) if ((size_t)(tmp - s + 1) > i)
break ; break ;
if (__is_quote(*tmp) != FALSE) if (__is_quote(*tmp) != FALSE)
{ {
start_quote = tmp - s; start_quote = tmp - s;
quote_type = __is_quote(*tmp); quote_type = __is_quote(*tmp);
tmp = search_for_next_quote(tmp, quote_type);
tmp++; tmp++;
if (*tmp && (start_quote < i && (size_t)(tmp - s) > i)) tmp = search_for_next_quote(tmp, quote_type);
if ((start_quote < i && (size_t)(tmp - s) > i))
return (quote_type); return (quote_type);
else if (!*tmp) else if (!*tmp)
return (NOT_CLOSED); return (NOT_CLOSED);

57
src/parsing/split_argv.c Normal file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* split_argv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/05 13:48:57 by adjoly #+# #+# */
/* Updated: 2024/06/10 16:29:12 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "parsing.h"
/*char *end_of_arg(char *readline)
{
char *tmp;
tmp = readline;
while (*tmp)
{
if (*tmp == 32 && is_inquote(readline, tmp - readline))
break ;
tmp++;
}
return (tmp);
}
size_t count_args(char *readline)
{
char *tmp;
tmp = readline;
while (*tmp)
{
tmp++;
}
}
char **split_argv(char *readline)
{
char *tmp;
char **argv;
tmp = readline;
ft_putnbr_fd(count_args(readline), STDOUT_FILENO);
argv = ft_calloc(count_args(readline), sizof(char *));
while (*tmp)
{
tmp++;
}
return (argv);
}
*/

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 15:00:32 by adjoly #+# #+# */ /* Created: 2024/05/04 15:00:32 by adjoly #+# #+# */
/* Updated: 2024/06/08 18:31:42 by mmoussou ### ########.fr */ /* Updated: 2024/06/18 12:56:43 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */ /* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/02 14:42:00 by adjoly #+# #+# */ /* Created: 2024/05/02 14:42:00 by adjoly #+# #+# */
/* Updated: 2024/05/27 19:00:47 by adjoly ### ########.fr */ /* Updated: 2024/06/11 16:04:42 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,7 +25,7 @@ char *get_pwd(t_env env)
home = env_get_value("HOME", &env);//getenv("HOME"); home = env_get_value("HOME", &env);//getenv("HOME");
if (!pwd) if (!pwd)
return (NULL); return (NULL);
if (!ft_strncmp(pwd, home, ft_strlen(home))) if (!ft_strncmp(pwd, home, ft_strlen(home) - 1))
{ {
pwd += ft_strlen(home); pwd += ft_strlen(home);
pwd = ft_strjoin("~", pwd); pwd = ft_strjoin("~", pwd);