feature: added completion for ~ and ${}

WIP leaks detected on ${} completion
This commit is contained in:
Tanguy Maze 2019-10-31 14:59:47 +01:00
parent e32efb9312
commit aa5f198c46
5 changed files with 144 additions and 19 deletions

3
.ccls Normal file
View File

@ -0,0 +1,3 @@
clang
-Iincludes
-Ilibft/includes

View File

@ -6,7 +6,7 @@
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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_echo.c \
ms_exec.c \
ms_env.c
ms_env.c \
ms_ext.c
OBJ = $(SRC:.c=.o)

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
char **lsttotab(t_env *env);
char **res_ext(char **argv, t_env *env);
int exec_cmd(char **argv, t_env **env);
#endif

View File

@ -6,7 +6,7 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,27 +57,43 @@ int main(void)
&& (argv = ft_strsplit(cmd, ' ')) != NULL)
{
ft_strdel(&cmd);
if (ft_strequ(argv[0], "exit"))
{
ft_del_words_tables(&argv);
lstdel(&env);
return (0);
}
i = 0;
while (i < S_BIN)
ft_printf("before:\n");
while (argv[i])
{
if (ft_strequ(argv[0], built[i].cmd))
{
built[i].f(argv, &env);
break ;
}
i++;
ft_printf("%s\n", argv[i++]);
}
if (i == S_BIN)
if ((res_ext(argv, env)) != NULL)
{
exec_cmd(argv, &env);
i = 0;
ft_printf("after:\n");
while (argv[i])
{
ft_printf("%s\n", argv[i++]);
}
if (ft_strequ(argv[0], "exit"))
{
ft_del_words_tables(&argv);
lstdel(&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);

103
srcs/ms_ext.c Normal file
View 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);
}