* Restart from scratch branched out on dev removed all previous source files started test on new env2lst function * norm: splitted functions in main into appropriate file Moved lstdelenvelem & env2lst to ms_env.c Normed out files * Feat(WIP): Added possibility to cmd_env to execute commands with altered env. Added dummy function for cd, echo, setenv & unsetenv Started work on env copy feature * feature: switched env from ft_list to specific list * feature: added execution of commands from path * feature: added env, setenv & unsetenv builtins added -i option & command execution to env added setenv & unsetenv builtins * Clean-up: normed out files * Changed comportment on error for check_path functions * feature: added completion for ~ and ${} WIP leaks detected on ${} completion * fix leak on ext need test * feature: added echo cmd changed printf ref to ft_printf * feature: add cd built-in * Simple norm-out * moved env functions to libft * fixed out-of-memory access in extension * Resolved infinite loop in extension revolving WIP norming in cd * Normedout cmd_cd.c * Normed out functions in cmd_cd * Normed out funtions sorting out needed * removed -fsanitize from Makefile * corrected env -i crash & SHLVL * Delete norm.txt * added put_error_cd2 in put_error_cd.c * added inline environment
135 lines
3.1 KiB
C
135 lines
3.1 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* main.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
|
|
/* Updated: 2020/01/30 13:47:35 by tmaze ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "minishell.h"
|
|
|
|
t_env *def_env(void)
|
|
{
|
|
t_env *ret;
|
|
t_env *n;
|
|
char p[PATH_MAX];
|
|
|
|
n = ft_memalloc(sizeof(t_env));
|
|
if (n != NULL && ((n->key = ft_strnew(3)) == NULL || !getcwd(p, PATH_MAX)))
|
|
ft_envdelelem(&n);
|
|
if (n != NULL)
|
|
ft_strcpy(n->key, "PWD");
|
|
if (n != NULL && (n->val = ft_strdup(p)) == NULL)
|
|
ft_envdelelem(&n);
|
|
if (n != NULL)
|
|
ft_envaddend(&ret, n);
|
|
n = ft_memalloc(sizeof(t_env));
|
|
if (n != NULL && (n->key = ft_strnew(5)) == NULL)
|
|
ft_envdelelem(&n);
|
|
if (n != NULL)
|
|
ft_strcpy(n->key, "SHLVL");
|
|
if (n != NULL && (n->val = ft_strdup("1")) == NULL)
|
|
ft_envdelelem(&n);
|
|
if (n != NULL)
|
|
ft_envaddend(&ret, n);
|
|
return (ret);
|
|
}
|
|
|
|
char *inc_shlvl(char **shlvl)
|
|
{
|
|
char *ret;
|
|
|
|
if ((ret = ft_itoa(ft_atoi(*shlvl) + 1)) != NULL)
|
|
{
|
|
ft_strdel(shlvl);
|
|
*shlvl = ret;
|
|
}
|
|
return (ret);
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (ft_strequ(new->key, "SHLVL") && ft_str_is_numeric(new->val))
|
|
inc_shlvl(&new->val);
|
|
ft_envaddend(&ret, new);
|
|
}
|
|
else
|
|
{
|
|
ft_envdelelem(&new);
|
|
ft_envdel(&ret);
|
|
return (NULL);
|
|
}
|
|
i++;
|
|
}
|
|
if (i == 0 && ret == NULL && (ret = def_env()) == NULL)
|
|
ft_printf("Env: Memory Error\n");
|
|
return (ret);
|
|
}
|
|
|
|
void check_exec(char **argv, t_env *env)
|
|
{
|
|
int i;
|
|
static t_builtin built[S_BIN] = {{"env", &cmd_env}, {"cd", &cmd_cd}
|
|
, {"setenv", &cmd_setenv}, {"echo", &cmd_echo}
|
|
, {"unsetenv", &cmd_unsetenv}};
|
|
|
|
i = -1;
|
|
while (++i < S_BIN)
|
|
if (ft_strequ(argv[0], built[i].cmd))
|
|
{
|
|
built[i].f(argv, &env);
|
|
break ;
|
|
}
|
|
if (i == S_BIN)
|
|
exec_cmd(argv, &env);
|
|
}
|
|
|
|
int cleanup(char **argv, t_env *env)
|
|
{
|
|
ft_del_words_tables(&argv);
|
|
ft_envdel(&env);
|
|
return (0);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
extern char **environ;
|
|
char **argv;
|
|
t_env *env;
|
|
char *cmd;
|
|
|
|
if ((env = env2lst(environ)) == NULL)
|
|
return (2);
|
|
while (1)
|
|
{
|
|
ft_printf("%s$>%s ", FT_COLOR_GREEN, FT_RESET);
|
|
if (ft_getline(&cmd) >= 0 && cmd != NULL
|
|
&& (argv = ft_strsplit(cmd, ' ')) != NULL && !ft_strdel_null(&cmd))
|
|
{
|
|
if ((res_ext(argv, env)) != NULL)
|
|
{
|
|
if (ft_strequ(argv[0], "exit"))
|
|
return (cleanup(argv, env));
|
|
check_exec(argv, env);
|
|
}
|
|
}
|
|
ft_strdel(&cmd);
|
|
ft_del_words_tables(&argv);
|
|
}
|
|
return (0);
|
|
}
|