90 lines
2.2 KiB
C
90 lines
2.2 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
|
|
/* Updated: 2019/11/10 21:24:36 by tmaze ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "minishell.h"
|
|
|
|
t_env *env2lst(char **env)
|
|
{
|
|
t_env *ret;
|
|
t_env *new;
|
|
int i;
|
|
|
|
i = 0;
|
|
ret = NULL;
|
|
while (env[i])
|
|
{
|
|
if ((new = ft_envnew(env[i])) != NULL)
|
|
ft_envaddend(&ret, new);
|
|
else
|
|
{
|
|
ft_envdelelem(&new);
|
|
ft_envdel(&ret);
|
|
return (NULL);
|
|
}
|
|
i++;
|
|
}
|
|
return (ret);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
extern char **environ;
|
|
static t_builtin built[S_BIN] = {{"env", &cmd_env}, {"cd", &cmd_cd}
|
|
, {"setenv", &cmd_setenv}, {"echo", &cmd_echo}
|
|
, {"unsetenv", &cmd_unsetenv}};
|
|
char **argv;
|
|
t_env *env;
|
|
char *cmd;
|
|
int i;
|
|
|
|
env = NULL;
|
|
if ((env = env2lst(environ)) == NULL)
|
|
return (2);
|
|
while (1)
|
|
{
|
|
cmd = NULL;
|
|
ft_printf("%s$>%s ", FT_COLOR_GREEN, FT_RESET);
|
|
if (ft_getline(&cmd) >= 0 && cmd != NULL
|
|
&& (argv = ft_strsplit(cmd, ' ')) != NULL)
|
|
{
|
|
ft_strdel(&cmd);
|
|
if ((res_ext(argv, env)) != NULL)
|
|
{
|
|
if (ft_strequ(argv[0], "exit"))
|
|
{
|
|
ft_del_words_tables(&argv);
|
|
ft_envdel(&env);
|
|
return (0);
|
|
}
|
|
i = 0;
|
|
while (i < S_BIN)
|
|
{
|
|
if (ft_strequ(argv[0], built[i].cmd))
|
|
{
|
|
built[i].f(argv, &env);
|
|
break ;
|
|
}
|
|
i++;
|
|
}
|
|
if (i == S_BIN)
|
|
exec_cmd(argv, &env);
|
|
}
|
|
}
|
|
ft_strdel(&cmd);
|
|
ft_del_words_tables(&argv);
|
|
}
|
|
ft_del_words_tables(&argv);
|
|
ft_strdel(&cmd);
|
|
ft_envdel(&env);
|
|
return (0);
|
|
}
|