Browse Source

norminage et anti-leaks

master
narnaud 3 years ago
parent
commit
3bb4c23b61
  1. 112
      lexer.c
  2. 10
      parser.c

112
lexer.c

@ -23,8 +23,8 @@ int create_token(t_lexer *lex, char str[])
else else
lex->type = WORD; lex->type = WORD;
lex->next_type = WORD; lex->next_type = WORD;
if (!str[0]) //if (!str[0])
return (0); // return (0);
tok = lex->tokens; tok = lex->tokens;
if (!lex->tokens) if (!lex->tokens)
tok = ft_calloc(1, sizeof *tok); tok = ft_calloc(1, sizeof *tok);
@ -36,14 +36,34 @@ int create_token(t_lexer *lex, char str[])
tok = tok->next; tok = tok->next;
} }
tok->type = type; tok->type = type;
tok->value = strdup(str); tok->value = ft_strdup(str);
if (DEBUG)
printf("token value : %s - token type : %d\n", tok->value, type);
if (!lex->tokens) if (!lex->tokens)
lex->tokens = tok; lex->tokens = tok;
ft_bzero(str, 1024);
return (1); return (1);
} }
int set_redir(t_lexer *lex, char **line, char ch)
{
static t_type type_out[2] = {ADD, OUT};
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[0];
else
lex->next_type = type[1];
return (1);
}
return (0);
}
int check_register(t_lexer *lex, char **line, char *tmp) int check_register(t_lexer *lex, char **line, char *tmp)
{ {
int spaces; int spaces;
@ -57,22 +77,9 @@ int check_register(t_lexer *lex, char **line, char *tmp)
(*line)++; (*line)++;
spaces = 1; spaces = 1;
} }
if (**line == '>' && (*line)++) if (set_redir(lex, line, '>')
{ || set_redir(lex, line, '<'))
if (**line == '>' && (*line)++)
lex->next_type = ADD;
else
lex->next_type = OUT;
return (create_token(lex, tmp));
}
else if (**line == '<' && (*line)++)
{
if (**line == '<' && (*line)++)
lex->next_type = HD;
else
lex->next_type = IN;
return (create_token(lex, tmp)); return (create_token(lex, tmp));
}
else if (**line == '|') else if (**line == '|')
{ {
(*line)++; (*line)++;
@ -85,44 +92,54 @@ int check_register(t_lexer *lex, char **line, char *tmp)
return (create_token(lex, tmp)); return (create_token(lex, tmp));
} }
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i) char *get_var_value(t_datas *datas, char **line, int *name_len)
{ {
int name_len; int i;
int i; char **env;
char *var_name; char *name;
char *value; char *value;
char **env;
i = 1; i = 1;
if ((*line)[1] == '?')
{
value = ft_itoa(datas->exit_code);
name_len = 2;
}
else
{
while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_') while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_')
i++; i++;
var_name = ft_substr(*line, 1, i - 1); name = ft_substr(*line, 1, i - 1);
i = 0; i = 0;
while (datas->envp[i]) *name_len = ft_strlen(name);
value = NULL;
while (datas->envp[i] && !value)
{ {
env = ft_split(datas->envp[i], '='); env = ft_split(datas->envp[i], '=');
if (!ft_strncmp(var_name, env[0], ft_strlen(var_name) + 1)) if (!ft_strncmp(name, env[0], *name_len + 1))
break ; value = (free(env[0]), env[1]);
i++; else if (++i)
ft_free_split(env);
} }
if (!datas->envp[i]) if (!datas->envp[i])
value = strdup(""); value = strdup("");
else else
value = env[1]; free(env);
name_len = ft_strlen(var_name); return (value);
}
int replace_var(t_datas *datas, char **line, char *tmp, int tmp_i)
{
int name_len;
int i;
char *value;
if ((*line)[1] == '?')
{
value = ft_itoa(datas->exit_code);
name_len = 2;
} }
else
value = get_var_value(datas, line, &name_len);
i = 0; i = 0;
while (value[i]) while (value[i])
tmp[tmp_i++] = value[i++]; tmp[tmp_i++] = value[i++];
tmp[tmp_i] = 0; tmp[tmp_i] = 0;
*line += name_len + 1; *line += name_len + 1;
free(value);
return (tmp_i); return (tmp_i);
} }
@ -171,17 +188,12 @@ t_token *lexer(t_datas *datas, char *line)
if (lex->state != S_QUOTE_ST && *line == '$') if (lex->state != S_QUOTE_ST && *line == '$')
tmp_i = replace_var(datas, &line, tmp, tmp_i); tmp_i = replace_var(datas, &line, tmp, tmp_i);
if (check_register(lex, &line, tmp)) if (check_register(lex, &line, tmp))
{
tmp_i = 0; tmp_i = 0;
ft_bzero(tmp, 1024); else
continue ; tmp[tmp_i++] = *(line++);
}
tmp[tmp_i] = *line;
line++;
tmp_i++;
} }
create_token(lex, tmp); create_token(lex, tmp);
free(tmp);
free(lex);
return (lex->tokens); return (lex->tokens);
} }

10
parser.c

@ -96,6 +96,7 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev)
{ {
int i; int i;
t_command *cmd; t_command *cmd;
t_token *prev_tok;
if (!tok) if (!tok)
return (NULL); return (NULL);
@ -109,15 +110,22 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev)
if (tok->type) if (tok->type)
update_redir(datas, cmd, tok); update_redir(datas, cmd, tok);
else else
cmd->argv[i++] = tok->value; cmd->argv[i++] = ft_strdup(tok->value);
prev_tok = tok;
tok = tok->next; tok = tok->next;
free(prev_tok->value);
free(prev_tok);
} }
if (DEBUG) if (DEBUG)
printf("######\n->cmd : %s, in_fd : %d, out_fd : %d\n", \ printf("######\n->cmd : %s, in_fd : %d, out_fd : %d\n", \
cmd->argv[0], cmd->fd[0], cmd->fd[1]); cmd->argv[0], cmd->fd[0], cmd->fd[1]);
cmd->argv[i] = NULL; cmd->argv[i] = NULL;
if (tok && tok->type == PIPE) if (tok && tok->type == PIPE)
{
cmd->next = parser(datas, tok->next, cmd); cmd->next = parser(datas, tok->next, cmd);
free(tok->value);
free(tok);
}
return(cmd); return(cmd);
} }

Loading…
Cancel
Save