|
|
@ -12,7 +12,68 @@ |
|
|
|
|
|
|
|
#include "minishell.h" |
|
|
|
|
|
|
|
int create_token(t_lexer *lex, char str[]) |
|
|
|
static int check_state(t_lexer *lex, char **line); |
|
|
|
static int create_token(t_lexer *lex, char str[]); |
|
|
|
static int check_register(t_lexer *lex, char **line, char *tmp); |
|
|
|
|
|
|
|
t_token *lexer(t_datas *datas, char *line) |
|
|
|
{ |
|
|
|
t_lexer *lex; |
|
|
|
t_token *ret; |
|
|
|
char *tmp; |
|
|
|
int tmp_i; |
|
|
|
|
|
|
|
lex = ft_calloc(1, sizeof *lex); |
|
|
|
lex->state = ROOT_ST; |
|
|
|
tmp = ft_calloc(STR_MAX_SIZE, 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); |
|
|
|
else if (check_register(lex, &line, tmp)) |
|
|
|
tmp_i = (ft_bzero(tmp, STR_MAX_SIZE), 0); |
|
|
|
else |
|
|
|
tmp[tmp_i++] = *(line++); |
|
|
|
} |
|
|
|
create_token(lex, tmp); |
|
|
|
free(tmp); |
|
|
|
ret = lex->tokens; |
|
|
|
free(lex); |
|
|
|
return (ret); |
|
|
|
} |
|
|
|
|
|
|
|
static 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); |
|
|
|
} |
|
|
|
|
|
|
|
static int create_token(t_lexer *lex, char str[]) |
|
|
|
{ |
|
|
|
t_token *tok; |
|
|
|
t_token *tmp; |
|
|
@ -39,28 +100,9 @@ int create_token(t_lexer *lex, char str[]) |
|
|
|
return (1); |
|
|
|
} |
|
|
|
|
|
|
|
int set_redir(t_lexer *lex, char **line, char ch) |
|
|
|
{ |
|
|
|
static t_type type_out[2] = {OUT, ADD}; |
|
|
|
static t_type type_in[2] = {IN, HD}; |
|
|
|
t_type *type; |
|
|
|
|
|
|
|
if (ch == '>') |
|
|
|
type = type_out; |
|
|
|
else |
|
|
|
type = type_in; |
|
|
|
if (**line == ch && (*line)++) |
|
|
|
{ |
|
|
|
if (**line == ch && (*line)++) |
|
|
|
lex->next_type = type[1]; |
|
|
|
else |
|
|
|
lex->next_type = type[0]; |
|
|
|
return (1); |
|
|
|
} |
|
|
|
return (0); |
|
|
|
} |
|
|
|
static int set_redir(t_lexer *lex, char **line, char ch); |
|
|
|
|
|
|
|
int check_register(t_lexer *lex, char **line, char *tmp) |
|
|
|
static int check_register(t_lexer *lex, char **line, char *tmp) |
|
|
|
{ |
|
|
|
int spaces; |
|
|
|
|
|
|
@ -88,59 +130,23 @@ int check_register(t_lexer *lex, char **line, char *tmp) |
|
|
|
return (create_token(lex, tmp)); |
|
|
|
} |
|
|
|
|
|
|
|
int check_state(t_lexer *lex, char **line) |
|
|
|
static int set_redir(t_lexer *lex, char **line, char ch) |
|
|
|
{ |
|
|
|
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); |
|
|
|
} |
|
|
|
|
|
|
|
t_token *lexer(t_datas *datas, char *line) |
|
|
|
{ |
|
|
|
t_lexer *lex; |
|
|
|
t_token *ret; |
|
|
|
char *tmp; |
|
|
|
int tmp_i; |
|
|
|
static t_type type_out[2] = {OUT, ADD}; |
|
|
|
static t_type type_in[2] = {IN, HD}; |
|
|
|
t_type *type; |
|
|
|
|
|
|
|
lex = ft_calloc(1, sizeof *lex); |
|
|
|
lex->state = ROOT_ST; |
|
|
|
tmp = ft_calloc(1024, sizeof *tmp); |
|
|
|
tmp_i = 0; |
|
|
|
while (*line) |
|
|
|
if (ch == '>') |
|
|
|
type = type_out; |
|
|
|
else |
|
|
|
type = type_in; |
|
|
|
if (**line == ch && (*line)++) |
|
|
|
{ |
|
|
|
if (check_state(lex, &line)) |
|
|
|
continue; |
|
|
|
if (lex->state != S_QUOTE_ST && *line == '$') |
|
|
|
tmp_i = replace_var(datas, &line, tmp, tmp_i); |
|
|
|
else if (check_register(lex, &line, tmp)) |
|
|
|
tmp_i = (ft_bzero(tmp, 1024), 0); |
|
|
|
if (**line == ch && (*line)++) |
|
|
|
lex->next_type = type[1]; |
|
|
|
else |
|
|
|
tmp[tmp_i++] = *(line++); |
|
|
|
lex->next_type = type[0]; |
|
|
|
return (1); |
|
|
|
} |
|
|
|
create_token(lex, tmp); |
|
|
|
free(tmp); |
|
|
|
ret = lex->tokens; |
|
|
|
free(lex); |
|
|
|
return (ret); |
|
|
|
return (0); |
|
|
|
} |
|
|
|