feature: added completion for ~ and ${}
WIP leaks detected on ${} completion
This commit is contained in:
parent
e32efb9312
commit
aa5f198c46
5
Makefile
5
Makefile
@ -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: 2019/09/27 16:13:51 by tmaze ### ########.fr #
|
# Updated: 2019/10/31 14:05:15 by tmaze ### ########.fr #
|
||||||
# #
|
# #
|
||||||
#******************************************************************************#
|
#******************************************************************************#
|
||||||
|
|
||||||
@ -43,7 +43,8 @@ SRC = main.c \
|
|||||||
cmd_cd.c \
|
cmd_cd.c \
|
||||||
cmd_echo.c \
|
cmd_echo.c \
|
||||||
ms_exec.c \
|
ms_exec.c \
|
||||||
ms_env.c
|
ms_env.c \
|
||||||
|
ms_ext.c
|
||||||
|
|
||||||
OBJ = $(SRC:.c=.o)
|
OBJ = $(SRC:.c=.o)
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
|
/* Created: 2018/11/18 13:12:34 by tmaze #+# #+# */
|
||||||
/* Updated: 2019/09/27 16:09:09 by tmaze ### ########.fr */
|
/* Updated: 2019/10/31 14:38:16 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -45,6 +45,8 @@ t_env *lstaddend(t_env **alst, t_env *new);
|
|||||||
t_env *lstgetelem(char *key, t_env *env);
|
t_env *lstgetelem(char *key, t_env *env);
|
||||||
char **lsttotab(t_env *env);
|
char **lsttotab(t_env *env);
|
||||||
|
|
||||||
|
char **res_ext(char **argv, t_env *env);
|
||||||
|
|
||||||
int exec_cmd(char **argv, t_env **env);
|
int exec_cmd(char **argv, t_env **env);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
18
srcs/main.c
18
srcs/main.c
@ -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: 2019/09/27 16:12:10 by tmaze ### ########.fr */
|
/* Updated: 2019/10/31 14:36:15 by tmaze ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -57,6 +57,20 @@ int main(void)
|
|||||||
&& (argv = ft_strsplit(cmd, ' ')) != NULL)
|
&& (argv = ft_strsplit(cmd, ' ')) != NULL)
|
||||||
{
|
{
|
||||||
ft_strdel(&cmd);
|
ft_strdel(&cmd);
|
||||||
|
i = 0;
|
||||||
|
ft_printf("before:\n");
|
||||||
|
while (argv[i])
|
||||||
|
{
|
||||||
|
ft_printf("%s\n", argv[i++]);
|
||||||
|
}
|
||||||
|
if ((res_ext(argv, env)) != NULL)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
ft_printf("after:\n");
|
||||||
|
while (argv[i])
|
||||||
|
{
|
||||||
|
ft_printf("%s\n", argv[i++]);
|
||||||
|
}
|
||||||
if (ft_strequ(argv[0], "exit"))
|
if (ft_strequ(argv[0], "exit"))
|
||||||
{
|
{
|
||||||
ft_del_words_tables(&argv);
|
ft_del_words_tables(&argv);
|
||||||
@ -78,6 +92,8 @@ int main(void)
|
|||||||
exec_cmd(argv, &env);
|
exec_cmd(argv, &env);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
ft_strdel(&cmd);
|
||||||
ft_del_words_tables(&argv);
|
ft_del_words_tables(&argv);
|
||||||
}
|
}
|
||||||
ft_del_words_tables(&argv);
|
ft_del_words_tables(&argv);
|
||||||
|
103
srcs/ms_ext.c
Normal file
103
srcs/ms_ext.c
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ms_ext.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2019/10/25 18:21:47 by tmaze #+# #+# */
|
||||||
|
/* Updated: 2019/10/31 14:49:51 by tmaze ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_env *search_key(t_env *env, char *cmd, int index)
|
||||||
|
{
|
||||||
|
t_env *it;
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
it = env;
|
||||||
|
while (it && cmd[index + 2])
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
j = index;
|
||||||
|
while (it->key[i] && cmd[j] && cmd[j] != '}' && it->key[i] == cmd[j])
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
if (!it->key[i] && cmd[j] == '}')
|
||||||
|
return (it);
|
||||||
|
it = it->next;
|
||||||
|
}
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **res_ext(char **argv, t_env *env)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int j;
|
||||||
|
int k;
|
||||||
|
int res_len;
|
||||||
|
t_env *elem;
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
k = 0;
|
||||||
|
while (argv && argv[k])
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (argv[k] && argv[k][i]) {
|
||||||
|
if (i == 0 && argv[k][i] == '~' && lstgetelem("HOME", env)) {
|
||||||
|
res_len =
|
||||||
|
ft_strlen(argv[k]) - 1 + ft_strlen(lstgetelem("HOME", env)->val);
|
||||||
|
if ((ret = ft_strnew(res_len)) != NULL) {
|
||||||
|
ft_strcpy(ret, lstgetelem("HOME", env)->val);
|
||||||
|
if (ret[ft_strlen(ret) - 1] != '/')
|
||||||
|
ft_strcat(ret, "/");
|
||||||
|
ft_strcat(ret, argv[k] + 1);
|
||||||
|
argv[k] = ret;
|
||||||
|
i = ft_strlen(lstgetelem("HOME", env)->val);
|
||||||
|
} else {
|
||||||
|
ft_printf("minishell: memory error\n");
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
} else if (argv[k][i] == '$' && argv[k][i + 1] && argv[k][i + 1] == '{') {
|
||||||
|
j = i + 1;
|
||||||
|
while (argv[k][j] && argv[k][j] != '}')
|
||||||
|
j++;
|
||||||
|
if (!argv[k][j])
|
||||||
|
{
|
||||||
|
ft_printf("minishell: missing }\n");
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
else if ((elem = search_key(env, argv[k], i + 2)) != NULL)
|
||||||
|
{
|
||||||
|
if ((ret = ft_strnew(i + ft_strlen(argv[k]) - j +
|
||||||
|
ft_strlen(elem->val))) != NULL)
|
||||||
|
{
|
||||||
|
ft_strncpy(ret, argv[k], i);
|
||||||
|
ft_strcat(ret, elem->val);
|
||||||
|
ft_strcat(ret, argv[k] + j + 1);
|
||||||
|
argv[k] = ret;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_printf("minishell: memory error\n");
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ft_printf("minishell: variable %*s not found\n", j - i - 1,
|
||||||
|
argv[k] + i + 2);
|
||||||
|
return (NULL);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
k++;
|
||||||
|
}
|
||||||
|
return (argv);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user