Skip to content

Commit

Permalink
Expand (#6)
Browse files Browse the repository at this point in the history
* start expander refactor

* start new expander

* Add expander_split ans expander_join

* Add expander_file

---------

Co-authored-by: Ofacy <[email protected]>
  • Loading branch information
UnRenardQuiDab and Ofacy authored Apr 3, 2024
1 parent 3a95945 commit 929be33
Show file tree
Hide file tree
Showing 16 changed files with 425 additions and 279 deletions.
12 changes: 7 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: lcottet <lcottet@student.42lyon.fr> +#+ +:+ +#+ #
# By: bwisniew <bwisniew@student.42lyon.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/02/14 13:24:31 by bwisniew #+# #+# #
# Updated: 2024/03/29 23:05:54 by lcottet ### ########.fr #
# Updated: 2024/04/03 18:20:54 by bwisniew ### ########.fr #
# #
# **************************************************************************** #

Expand All @@ -18,15 +18,17 @@ SRCS_DIR = srcs

SRCS = main.c env.c prompt.c error.c env_utils.c mshell_utils.c signal.c

PARSER_SRCS = lexer.c expander.c expander_utils.c expander_token.c syntax.c \
token_utils.c expand.c
PARSER_SRCS = lexer.c syntax.c token_utils.c

EXEC_SRCS = path.c exec.c exec_fd.c here_doc.c fork.c exec_utils.c wait.c \
close.c exec_builtins.c

EXPAND_SRCS = expander_token.c expander_len.c expander_split.c expander.c \
expander_join.c expander_file.c

BUILTINS_SRCS = env.c pwd.c unset.c cd.c echo.c builtin_utils.c export.c exit.c

SRCS += $(PARSER_SRCS:%.c=parser/%.c) $(EXEC_SRCS:%.c=exec/%.c) $(BUILTINS_SRCS:%.c=builtins/%.c)
SRCS += $(PARSER_SRCS:%.c=parser/%.c) $(EXEC_SRCS:%.c=exec/%.c) $(BUILTINS_SRCS:%.c=builtins/%.c) $(EXPAND_SRCS:%.c=expander/%.c)

OUTDIR = obj

Expand Down
35 changes: 35 additions & 0 deletions includes/expander.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 19:30:58 by bwisniew #+# #+# */
/* Updated: 2024/04/03 14:21:20 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef EXPANDER_H
# define EXPANDER_H

# include "minishell.h"
# include <stddef.h>

typedef struct s_expand_size
{
size_t exp_size;
size_t str_size;
int error;
} t_expand_size;

int tokens_expand(t_mshell *sh, size_t token_i);
bool need_expand_str(t_token *token, size_t str_i);
size_t expanded_len(t_mshell *sh, t_token *token);
size_t skip_envname(char *str);
size_t expander_split(t_mshell *sh, size_t token_i);
int expander_unseparated(t_vector *lex, size_t i, size_t n);
int token_expand(t_token *token, t_mshell *sh, size_t exp_len);
size_t expander_skip_file(t_vector *lex, size_t i, size_t n);

#endif
14 changes: 3 additions & 11 deletions includes/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lcottet <lcottet@student.42lyon.fr> +#+ +:+ +#+ */
/* By: bwisniew <bwisniew@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/13 17:55:03 by lcottet #+# #+# */
/* Updated: 2024/04/02 09:44:17 by lcottet ### ########.fr */
/* Updated: 2024/04/03 18:21:30 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -73,16 +73,8 @@ typedef struct s_mshell
} t_mshell;

void free_token(t_token *token);

bool should_hide_dollar(t_token *token, size_t i);
size_t expander_skip_arrow(t_vector *lex, size_t i, size_t n);
size_t expended_len(t_token *token, t_mshell *sh);
int replace_env_var(char *token_str, char *exp_str, t_mshell *sh);
int expand_file(t_mshell *sh, size_t token_i);
int expander(t_mshell *sh, size_t start, size_t end);
int expend_file(t_mshell *sh, size_t i);
int get_expended_str(t_token *token, t_mshell *sh);
int expender_token_split(t_vector *tokens, size_t *i);
int skip_env_name(char *str);

t_env *env_get(t_mshell *sh, char *key, bool nullvalue);
int set_env_return(t_mshell *sh, int value);
Expand Down
2 changes: 1 addition & 1 deletion libft
8 changes: 4 additions & 4 deletions srcs/exec/exec_fd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/10 18:29:04 by lcottet #+# #+# */
/* Updated: 2024/03/20 14:32:07 by bwisniew ### ########.fr */
/* Updated: 2024/04/03 14:23:26 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -48,7 +48,7 @@ int exec_set_output(t_execute *exec, t_mshell *sh, size_t i)
{
exec->has_redirect = true;
close_fd(&exec->out);
if (expend_file(sh, i))
if (expand_file(sh, i))
return (1);
exec->out = open(((t_token *)sh->tokens.tab)[i + 1].txt,
O_WRONLY | O_CREAT | O_TRUNC, 0644);
Expand All @@ -63,7 +63,7 @@ int exec_set_output(t_execute *exec, t_mshell *sh, size_t i)
int exec_set_input(t_execute *exec, t_mshell *sh, size_t i)
{
close_fd(&exec->in);
if (expend_file(sh, i))
if (expand_file(sh, i))
return (1);
exec->in = open(((t_token *)sh->tokens.tab)[i + 1].txt, O_RDONLY);
if (exec->in == -1)
Expand All @@ -78,7 +78,7 @@ int exec_set_append(t_execute *exec, t_mshell *sh, size_t i)
{
exec->has_redirect = true;
close_fd(&exec->out);
if (expend_file(sh, i))
if (expand_file(sh, i))
return (1);
exec->out = open(((t_token *)sh->tokens.tab)[i + 1].txt,
O_WRONLY | O_CREAT | O_APPEND, 0644);
Expand Down
5 changes: 3 additions & 2 deletions srcs/exec/here_doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/10 20:41:37 by lcottet #+# #+# */
/* Updated: 2024/03/20 16:28:14 by bwisniew ### ########.fr */
/* Updated: 2024/04/03 18:26:48 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"
#include "expander.h"
#include "libft.h"
#include <readline/readline.h>
#include <unistd.h>
Expand Down Expand Up @@ -61,7 +62,7 @@ char *here_doc_getline(t_mshell *sh, size_t i, t_fd fd, int *err)
line.type = ((t_token *)sh->tokens.tab)[i].old_type;
if (((t_token *)sh->tokens.tab)[i].old_type == UNQUOTED)
{
if (get_expended_str(&line, sh) < 0)
if (token_expand(&line, sh, expanded_len(sh, &line)) != 0)
{
free_token(&line);
return (NULL);
Expand Down
37 changes: 37 additions & 0 deletions srcs/expander/expander.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/21 19:13:27 by bwisniew #+# #+# */
/* Updated: 2024/04/03 18:11:59 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

#include "expander.h"
#include <stdio.h>

int expander(t_mshell *sh, size_t start, size_t end)
{
size_t token_i;
int ret;

token_i = expander_skip_file(&sh->tokens, start, end);
while (token_i < end)
{
if (!is_special(((t_token *)sh->tokens.tab)[token_i].type))
{
ret = tokens_expand(sh, token_i);
if (ret < 0)
return (1);
token_i += ret;
end += ret - 1;
}
else
token_i++;
token_i = expander_skip_file(&sh->tokens, token_i, end);
}
return (expander_unseparated(&sh->tokens, start, token_i));
}
77 changes: 77 additions & 0 deletions srcs/expander/expander_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander_file.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bwisniew <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 13:50:42 by bwisniew #+# #+# */
/* Updated: 2024/04/03 18:26:42 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"
#include "expander.h"
#include "libft.h"

size_t expander_skip_file(t_vector *lex, size_t i, size_t n)
{
if (i < n && is_type_arrow(((t_token *)lex->tab)[i].type))
{
((t_token *)lex->tab)[i + 1].is_file = true;
i += 2;
while (i < n && !((t_token *)lex->tab)[i - 1].is_separated
&& !is_special(((t_token *)lex->tab)[i - 1].type))
{
((t_token *)lex->tab)[i].is_file = true;
i++;
}
if (i < n)
((t_token *)lex->tab)[i].is_file = true;
}
return (i);
}

size_t expand_ambi_file(t_token *token, t_mshell *sh)
{
size_t txt_i;
t_env *env_var;

txt_i = 0;
while (txt_i < token->txt_size)
{
if (need_expand_str(token, txt_i))
{
env_var = env_get(sh, token->txt + txt_i + 1, false);
if (env_var && token->type == UNQUOTED
&& ft_strchr(env_var->value, ' ') != NULL)
{
custom_error(env_var->key, "ambiguous redirect");
return (1);
}
txt_i += skip_envname(token->txt + txt_i + 1) + 1;
}
else
txt_i++;
}
return (0);
}

int expand_file(t_mshell *sh, size_t token_i)
{
size_t i_cp;

i_cp = token_i;
token_i++;
while (token_i < sh->tokens.len
&& ((t_token *)sh->tokens.tab)[token_i].is_file == 1)
{
if (expand_ambi_file(&((t_token *)sh->tokens.tab)[token_i], sh) != 0)
return (1);
token_i++;
}
if (expander(sh, i_cp + 1, expander_skip_file(&sh->tokens,
i_cp, sh->tokens.len) + 1) < 0)
return (1);
return (0);
}
71 changes: 19 additions & 52 deletions srcs/parser/expander_utils.c → srcs/expander/expander_join.c
Original file line number Diff line number Diff line change
@@ -1,30 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander_utils.c :+: :+: :+: */
/* expander_join.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lcottet <lcottet@student.42lyon.fr> +#+ +:+ +#+ */
/* By: bwisniew <bwisniew@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/07 22:26:27 by lcottet #+# #+# */
/* Updated: 2024/03/21 18:55:41 by lcottet ### ########.fr */
/* Created: 2024/04/02 18:51:26 by bwisniew #+# #+# */
/* Updated: 2024/04/02 19:17:30 by bwisniew ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"
#include "expander.h"
#include "libft.h"
#include <stdlib.h>
#include <stdio.h>

int replace_env_var(char *token_str, char *exp_str, t_mshell *sh)
{
t_env *env_var;

env_var = env_get(sh, token_str, false);
if (!env_var)
return (0);
ft_strlcpy(exp_str, env_var->value, env_var->value_size + 1);
return (env_var->value_size);
}

size_t expander_skip_arrow(t_vector *lex, size_t i, size_t n)
{
Expand Down Expand Up @@ -65,48 +53,27 @@ char *expander_join(t_token *t1, t_token *t2)
return (str);
}

int expend_file_ambi(t_token *token, t_mshell *sh)
int expander_unseparated(t_vector *lex, size_t i, size_t n)
{
size_t char_i;
t_env *env_var;
t_token *token;
char *tmp;

char_i = 0;
while (char_i < token->txt_size)
while (i < n)
{
if (token->txt[char_i] == '$' && token->type != SINGLE_QUOTED)
token = ((t_token *)lex->tab) + i;
while (!token->is_separated)
{
env_var = env_get(sh, token->txt + char_i + 1, false);
if (!env_var)
{
char_i++;
continue ;
}
else if (ft_strchr(env_var->value, ' ') != NULL)
{
custom_error(env_var->key, "ambiguous redirect");
tmp = expander_join(token,
((t_token *)lex->tab) + i + 1);
if (!tmp)
return (1);
}
char_i += env_var->key_size + 1;
token->txt = tmp;
vector_remove(lex, i + 1);
}
char_i++;
}
return (0);
}

int expend_file(t_mshell *sh, size_t i)
{
size_t i_cp;

i_cp = i;
i++;
while (i < sh->tokens.len && ((t_token *)sh->tokens.tab)[i].is_file == 1)
{
if (expend_file_ambi(&((t_token *)sh->tokens.tab)[i], sh) != 0)
return (1);
if (((t_token *)lex->tab)[i].type == SINGLE_QUOTED)
((t_token *)lex->tab)[i].type = UNQUOTED;
i = expander_skip_arrow(lex, i, n);
i++;
}
if (expander(sh, i_cp + 1,
expander_skip_arrow(&sh->tokens, i_cp, sh->tokens.len) + 1) < 0)
return (1);
return (0);
}
Loading

0 comments on commit 929be33

Please sign in to comment.