You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
3.9 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */
3 years ago
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/05/06 11:19:26 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#include "minishell.h"
int create_token(t_lexer *lex, char str[])
{
t_token *tok;
t_type type;
type = lex->type;
if (lex->next_type)
lex->type = lex->next_type;
else
lex->type = WORD;
3 years ago
lex->next_type = WORD;
if (!str[0])
return (0);
tok = lex->tokens;
if (!lex->tokens)
tok = ft_calloc(1, sizeof *tok);
else
{
while (tok->next)
tok = tok->next;
tok->next = ft_calloc(1, sizeof *tok);
tok = tok->next;
}
tok->type = type;
tok->value = strdup(str);
if (DEBUG)
printf("token value : %s - token type : %d\n", tok->value, type);
3 years ago
if (!lex->tokens)
lex->tokens = tok;
return (1);
}
int check_register(t_lexer *lex, char **line, char *tmp)
{
int spaces;
if (lex->state != ROOT_ST)
return (0);
spaces = 0;
3 years ago
if (ft_isspace(**line))
3 years ago
{
while (ft_isspace(**line))
3 years ago
(*line)++;
spaces = 1;
}
if (**line == '>' && (*line)++)
{
if (**line == '>' && (*line)++)
lex->next_type = ADD;
else
lex->next_type = OUT;
return (create_token(lex, tmp));
3 years ago
}
else if (**line == '<' && (*line)++)
{
if (**line == '<' && (*line)++)
lex->next_type = HD;
else
lex->next_type = IN;
return (create_token(lex, tmp));
3 years ago
}
else if (**line == '|')
{
(*line)++;
lex->next_type = PIPE;
create_token(lex, tmp);
return (create_token(lex, "|"));
3 years ago
}
if (!spaces || !*tmp)
3 years ago
return (0);
return (create_token(lex, tmp));
3 years ago
}
3 years ago
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{
int name_len;
3 years ago
int i;
char *var_name;
char *value;
char **env;
3 years ago
3 years ago
i = 1;
3 years ago
if ((*line)[1] == '?')
{
value = ft_itoa(datas->exit_code);
name_len = 2;
}
else
{
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
i++;
var_name = ft_substr(*line, 1, i - 1);
i = 0;
while (datas->envp[i])
{
env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(var_name, env[0], ft_strlen(var_name) + 1))
break ;
i++;
}
if (!datas->envp[i])
value = strdup("");
else
value = env[1];
name_len = ft_strlen(var_name);
3 years ago
}
3 years ago
i = 0;
3 years ago
while (value[i])
tmp[tmp_i++] = value[i++];
3 years ago
tmp[tmp_i] = 0;
*line += name_len + 1;
3 years ago
return (tmp_i);
}
int check_state(t_lexer *lex, char **line)
{
t_state new;
new = OLD_ST;
if (**line == '"')
{
if (lex->state == D_QUOTE_ST)
new = ROOT_ST;
else if (lex->state == ROOT_ST)
new = D_QUOTE_ST;
}
else if (**line == '\'')
{
if (lex->state == S_QUOTE_ST)
new = ROOT_ST;
else if (lex->state == ROOT_ST)
new = S_QUOTE_ST;
}
if (new)
{
(*line)++;
lex->state = new;
return (1);
}
return (0);
}
3 years ago
t_token *lexer(t_datas *datas, char *line)
{
t_lexer *lex;
char *tmp;
int tmp_i;
lex = ft_calloc(1, sizeof *lex);
lex->state = ROOT_ST;
tmp = ft_calloc(1024, sizeof *tmp);
tmp_i = 0;
while (*line)
{
if (check_state(lex, &line))
continue;
if (lex->state != S_QUOTE_ST && *line == '$')
tmp_i = replace_var(datas, &line, tmp, tmp_i);
if (check_register(lex, &line, tmp))
{
tmp_i = 0;
ft_bzero(tmp, 1024);
continue ;
}
tmp[tmp_i] = *line;
line++;
tmp_i++;
}
create_token(lex, tmp);
return (lex->tokens);
}