added inline environment

This commit is contained in:
Tanguy MAZE 2020-01-30 15:45:47 +01:00
parent cd72d29c63
commit 9a44444070
5 changed files with 51 additions and 21 deletions

View File

@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ # # By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# # # Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
# Updated: 2020/01/26 22:21:26 by tmaze ### ########.fr # # Updated: 2020/01/30 12:00:42 by tmaze ### ########.fr #
# # # #
#******************************************************************************# #******************************************************************************#

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */ /* Created: 2019/09/20 14:33:48 by tmaze #+# #+# */
/* Updated: 2019/12/09 09:59:58 by tmaze ### ########.fr */ /* Updated: 2020/01/30 15:41:08 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,6 +47,24 @@ int env_cleanup(t_env *env_cp, int code)
return (code); return (code);
} }
t_env *inline_env(char **argv, int *i, t_env **env_cp)
{
t_env *new;
while (ft_strchr(argv[*i], '='))
{
if ((new = ft_envnew(argv[*i])) == NULL)
{
ft_printf("env: memory error\n");
ft_envdel(env_cp);
break ;
}
ft_envaddend(env_cp, new);
(*i)++;
}
return (*env_cp);
}
int cmd_env(char **argv, t_env **env) int cmd_env(char **argv, t_env **env)
{ {
int i; int i;
@ -55,8 +73,7 @@ int cmd_env(char **argv, t_env **env)
t_env *env_cp; t_env *env_cp;
i = 1; i = 1;
env_cp = NULL; if (!(env_cp = NULL) && !ft_strequ(argv[i], "-i") && (env != NULL))
if (!ft_strequ(argv[i], "-i") && (env != NULL))
{ {
it = *env; it = *env;
while (it) while (it)
@ -67,8 +84,9 @@ int cmd_env(char **argv, t_env **env)
it = it->next; it = it->next;
} }
} }
if (ft_strequ(argv[i], "-i")) else if (ft_strequ(argv[i], "-i"))
i++; i++;
inline_env(argv, &i, &env_cp);
if (argv[i]) if (argv[i])
exec_cmd(argv + i, &env_cp); exec_cmd(argv + i, &env_cp);
else else

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */ /* Created: 2019/09/19 17:08:46 by tmaze #+# #+# */
/* Updated: 2020/01/25 16:23:50 by tmaze ### ########.fr */ /* Updated: 2020/01/30 13:47:35 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,6 +39,18 @@ t_env *def_env(void)
return (ret); 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 *env2lst(char **env)
{ {
t_env *ret; t_env *ret;
@ -52,7 +64,7 @@ t_env *env2lst(char **env)
if ((new = ft_envnew(env[i])) != NULL) if ((new = ft_envnew(env[i])) != NULL)
{ {
if (ft_strequ(new->key, "SHLVL") && ft_str_is_numeric(new->val)) if (ft_strequ(new->key, "SHLVL") && ft_str_is_numeric(new->val))
new->val = ft_itoa(ft_atoi(new->val) + 1); inc_shlvl(&new->val);
ft_envaddend(&ret, new); ft_envaddend(&ret, new);
} }
else else

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */ /* Created: 2019/09/26 16:25:00 by tmaze #+# #+# */
/* Updated: 2020/01/25 16:46:29 by tmaze ### ########.fr */ /* Updated: 2020/01/30 11:48:25 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -95,6 +95,7 @@ char *check_path_elems(char **path_elems, char *exec)
char *ret; char *ret;
i = -1; i = -1;
ret = NULL;
while (path_elems[++i]) while (path_elems[++i])
{ {
if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL if ((tmp = ft_strjoin(path_elems[i], "/")) == NULL
@ -136,9 +137,10 @@ char *check_path(char *exec, t_env *env)
return (put_error(exec, "could not resolve path")); return (put_error(exec, "could not resolve path"));
i = 0; i = 0;
if ((ret = check_path_elems(path_elems, exec)) == NULL) if ((ret = check_path_elems(path_elems, exec)) == NULL)
return (NULL); {
if (path_elems[i] == NULL) ft_del_words_tables(&path_elems);
put_error(exec, "command not found"); return (put_error(exec, "command not found"));
}
ft_del_words_tables(&path_elems); ft_del_words_tables(&path_elems);
} }
return (ret); return (ret);

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */ /* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
/* Updated: 2020/01/25 16:53:59 by tmaze ### ########.fr */ /* Updated: 2020/01/30 15:42:17 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -57,9 +57,10 @@ char *replace_env(char *src, t_env *elem, int key_size, int i)
int res_len; int res_len;
res_len = ft_strlen(src) - key_size + ft_strlen(elem->val); res_len = ft_strlen(src) - key_size + ft_strlen(elem->val);
ft_printf("size replace: %d\n", res_len);
if ((ret = ft_strnew(res_len)) != NULL) if ((ret = ft_strnew(res_len)) != NULL)
{ {
ft_strlcpy(ret, src, i + 1); ft_strncpy(ret, src, i);
ft_strlcat(ret, elem->val, res_len + 1); ft_strlcat(ret, elem->val, res_len + 1);
ft_strlcat(ret, src + key_size, res_len + 1); ft_strlcat(ret, src + key_size, res_len + 1);
} }
@ -106,10 +107,7 @@ char **res_ext(char **av, t_env *env)
ft_strreplace(&av[i[0]], ret, &i[1], tmp); ft_strreplace(&av[i[0]], ret, &i[1], tmp);
else if (av[i[0]][i[1]] == '~' && (tmp = ft_envgetelem("HOME", env)) else if (av[i[0]][i[1]] == '~' && (tmp = ft_envgetelem("HOME", env))
== NULL) == NULL)
{ return (return_null("Entry HOME not found\n"));
ft_printf("Entry %s not found\n", "HOME");
return (NULL);
}
else if (av[i[0]][i[1]] == '$' else if (av[i[0]][i[1]] == '$'
&& resolve_complete_key(&av[i[0]], &i[1], env) == NULL) && resolve_complete_key(&av[i[0]], &i[1], env) == NULL)
return (NULL); return (NULL);