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.

103 lines
2.4 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/03 10:39:02 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
# include "minishell.h"
t_command *parser(t_token *tok, t_command *prev)
3 years ago
{
int i;
t_command *cmd;
if (!tok)
return (NULL);
3 years ago
cmd = ft_calloc(1, sizeof(t_command));
if (prev)
cmd->prev = prev;
i = 0;
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *));
while(tok && tok->type != PIPE)
3 years ago
{
if (tok->type)
update_redir(cmd, tok);
3 years ago
else
cmd->argv[i++] = tok->value;
if (DEBUG)
printf("token : %s, type: %d\n", tok->value, tok->type);
tok = tok->next;
3 years ago
}
cmd->argv[i] = NULL;
if (tok && tok->type == PIPE)
cmd->next = parser(tok->next, cmd);
return (cmd);
3 years ago
}
t_token *lexer(char *line)
{
t_lexer *lex;
char *tmp;
int tmp_i;
lex = ft_calloc(1, sizeof *lex);
lex->state = ROOT_ST;
3 years ago
tmp = ft_calloc(1024, sizeof *tmp);
tmp_i = 0;
while (*line)
{
if (DEBUG)
printf("%c\n", *line);
if (check_state(lex, &line))
3 years ago
continue;
if (lex->state != S_QUOTE_ST && *line == '$')
tmp_i = replace_var(&line, tmp, tmp_i);
if (check_register(lex, &line, tmp))
{
tmp_i = 0;
3 years ago
ft_bzero(tmp, 1024);
continue ;
}
tmp[tmp_i] = *line;
line++;
tmp_i++;
}
create_token(lex, tmp);
return (lex->tokens);
}
int launcher(t_env env)
{
char *line;
(void)env;
// TODO: handle ret
3 years ago
line = readline("$ ");
if (line == NULL)
halt(EXIT_FAILURE);
if (is_empty(line))
return (EXIT_FAILURE);
parser(lexer(line), NULL);
3 years ago
add_history(line);
return (0);
3 years ago
}
int main(int argc, char **argv, char**envp)
{
if (argc < 2)
(void)argv;
(void)envp;
t_env env;
env.null = 0;
3 years ago
while (1)
launcher(env);
clear_history();
return (EXIT_SUCCESS);
}