Compare commits
167 Commits
Author | SHA1 | Date | |
---|---|---|---|
6310a99dcb | |||
0a2ec43f41 | |||
c3a0786da1 | |||
40a5533c5f | |||
390937d195 | |||
6de6904a67 | |||
36f7bfd64d | |||
24f44c27e7 | |||
1366c49e99 | |||
2af75c9497 | |||
df02363087 | |||
b8ceec5d95 | |||
15f8e10829 | |||
fe095d4b1f | |||
bee12290d5 | |||
7f85951e43 | |||
26cf503126 | |||
ae81e8dc45 | |||
6b4a3dd857 | |||
7cb969caba | |||
86653bb3a0 | |||
1cd1e9562f | |||
5810e1c970 | |||
080f05d4f7 | |||
93e4d43283 | |||
52174b8737 | |||
59ce849e26 | |||
2cbb079036 | |||
2f3132a2c3 | |||
3a55a9f6ec | |||
4a901bbdb2 | |||
79e440d6b6 | |||
6a05ea29a2 | |||
285a99b6a9 | |||
6977ae4f71 | |||
2cea45c05d | |||
20037a4609 | |||
ab5190ee8f | |||
8becb3f468 | |||
202c5e8811 | |||
7044aab736 | |||
3c5ad3ad89 | |||
006be64ff5 | |||
529b7b0ef1 | |||
471a417bf9 | |||
e5b705ff64 | |||
2d8624d221 | |||
905bbc659b | |||
b7755111f6 | |||
b2ff5bcf22 | |||
7a032b41b1 | |||
33a37e4711 | |||
3cf92d0495 | |||
3fac53244f | |||
e5de3364e7 | |||
a8fa757432 | |||
67e514d5f9 | |||
e4df4a5655 | |||
642a9ab232 | |||
3cfeeb348a | |||
a56cd5cc81 | |||
4304a4aaad | |||
3bd4113489 | |||
d35e48eb4f | |||
f37b0cf8bc | |||
9f14d1c1b6 | |||
901ff34b7c | |||
30fafc49b0 | |||
a8b65c820b | |||
51934faa06 | |||
a42ddc1955 | |||
e2898b8128 | |||
dfa81cd19c | |||
27d38f5068 | |||
af73eddc64 | |||
a95e4bcbff | |||
ff1406b93e | |||
73e576f109 | |||
7d696f952f | |||
e240f0e1f1 | |||
70a7bde82f | |||
700a63c6c8 | |||
302eb2a17b | |||
4585ef9054 | |||
8e803ae4cf | |||
fb62d4ceed | |||
5da5968deb | |||
9053276342 | |||
be46825893 | |||
6fb0af3d17 | |||
9da2f11ea0 | |||
3dcb4e55d7 | |||
483d6579d6 | |||
dd581557de | |||
5ab0c197ce | |||
c1c97e1666 | |||
95d9c986f2 | |||
2862f6bab5 | |||
6ea24e899f | |||
90e569f231 | |||
51fff7aefd | |||
3a7bbac5cb | |||
2bfd801ba3 | |||
2a4c52f370 | |||
6bb41cb5bb | |||
4373c0da3d | |||
babcc2ebdf | |||
4c9941a5d6 | |||
c9bd04d42c | |||
b4c9665dd3 | |||
0ebdfd7d34 | |||
a1fa344ef0 | |||
d68e163595 | |||
cbbe866ff4 | |||
eb97119a2f | |||
99054a5237 | |||
2473775fa5 | |||
5580852791 | |||
68469542ff | |||
fde70abd2b | |||
dc73084ede | |||
7d1fba69a9 | |||
e05465348d | |||
bd6fba08fe | |||
eda077e34b | |||
43936f2cd4 | |||
7e6f3d8a72 | |||
2a06dc1fac | |||
25a7368c4c | |||
d07a954adf | |||
db934be785 | |||
157bb8a1ac | |||
eae8f96645 | |||
ffbbd564f9 | |||
a9fde3bad1 | |||
7ea57084dd | |||
dad89c923e | |||
ca90a5f692 | |||
1acbb38932 | |||
39d736ac38 | |||
d767ea3f10 | |||
0fb72c0c2c | |||
c5b6f8f53e | |||
50dd393b36 | |||
6ffeebf87b | |||
a7ce98266c | |||
5a3bbfa236 | |||
1e6df9ea62 | |||
a140be4c3e | |||
6e0afd2499 | |||
1f567bd6ce | |||
eb5f2bd32e | |||
b25a87953b | |||
a58366f827 | |||
bedf72b553 | |||
aed056ae87 | |||
19bf6c24ff | |||
0b81c8e3b0 | |||
d4a95a0d76 | |||
568df14b4a | |||
7b6c75b5c3 | |||
2718546387 | |||
d6d99aefba | |||
afbcb6b7ba | |||
5677edfce1 | |||
4011934434 | |||
02cb28b60a |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,7 +1,10 @@
|
|||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
|
.tonotdo
|
||||||
|
|
||||||
|
|
||||||
minishell
|
minishell
|
||||||
|
minishell_bonus
|
||||||
.minishellrc
|
.minishellrc
|
||||||
objs/
|
objs/
|
||||||
.tmp*
|
.tmp*
|
||||||
|
52
Makefile
52
Makefile
@ -6,7 +6,7 @@
|
|||||||
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
||||||
# Updated: 2024/03/05 19:11:56 by marde-vr ### ########.fr #
|
# Updated: 2024/04/26 10:52:53 by marde-vr ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -15,28 +15,38 @@ SRCS_RAW = main.c\
|
|||||||
lst_cmd.c\
|
lst_cmd.c\
|
||||||
cd.c\
|
cd.c\
|
||||||
lst_env.c\
|
lst_env.c\
|
||||||
exec.c\
|
lst_token.c\
|
||||||
exit.c\
|
exit.c\
|
||||||
|
is_fd_open.c\
|
||||||
echo.c\
|
echo.c\
|
||||||
alias.c\
|
|
||||||
unalias.c\
|
|
||||||
pwd.c\
|
pwd.c\
|
||||||
|
env.c\
|
||||||
parsing.c\
|
parsing.c\
|
||||||
debug.c\
|
debug.c\
|
||||||
env_to_char_tab.c\
|
env_utils.c\
|
||||||
parsing_var.c\
|
parsing_var.c\
|
||||||
handle_alias.c\
|
|
||||||
lst_alias.c\
|
|
||||||
minishellrc.c\
|
|
||||||
path.c\
|
path.c\
|
||||||
here_doc.c\
|
here_doc.c\
|
||||||
|
here_doc_utils.c\
|
||||||
export.c\
|
export.c\
|
||||||
input_redirections.c\
|
input_redirections.c\
|
||||||
|
sort_wildcard_list.c\
|
||||||
output_redirections.c\
|
output_redirections.c\
|
||||||
builtins.c\
|
builtins.c\
|
||||||
|
wildcards.c\
|
||||||
commands.c\
|
commands.c\
|
||||||
pipe.c\
|
pipe.c\
|
||||||
utils.c
|
utils.c\
|
||||||
|
utils_bonus.c\
|
||||||
|
signal_handler.c\
|
||||||
|
parsing_bonus.c\
|
||||||
|
exec_bonus.c\
|
||||||
|
exec_utils.c\
|
||||||
|
get_len_bonus.c\
|
||||||
|
check_syntax.c\
|
||||||
|
check_syntax_utils.c\
|
||||||
|
unset.c\
|
||||||
|
free.c
|
||||||
|
|
||||||
OBJS_DIR = objs/
|
OBJS_DIR = objs/
|
||||||
SRCS_DIR = srcs/
|
SRCS_DIR = srcs/
|
||||||
@ -51,20 +61,32 @@ all:
|
|||||||
@$(MAKE) --no-print-directory -j $(NAME)
|
@$(MAKE) --no-print-directory -j $(NAME)
|
||||||
|
|
||||||
$(NAME) : $(LIBFT) $(OBJS)
|
$(NAME) : $(LIBFT) $(OBJS)
|
||||||
$(CC) $(FLAGS) $(OBJS) $(LIBFT) -lreadline -o $(NAME)
|
@$(CC) $(FLAGS) $(OBJS) $(LIBFT) -lreadline -o $(NAME)
|
||||||
|
@echo project ready
|
||||||
|
@if [ ! $(USER) = "marde-vr" ];then\
|
||||||
|
timeout 2 bash -c 'while :; do r=$$(echo "scale=2;($$RANDOM / 16384) + 0.01" | bc -l);g=$$(echo "scale=2;($$RANDOM / 16384) + 0.01" | bc -l); b=$$(echo "scale=2;($$RANDOM / 16384) + 0.01" | bc -l);xrandr --output "HDMI-1-2" --gamma $$r:$$g:$$b;sleep 0.05;done' || true && xrandr --output HDMI-1-2 --brightness 1&\
|
||||||
|
fi
|
||||||
|
|
||||||
$(LIBFT):
|
$(LIBFT):
|
||||||
make --no-print-directory -j -C ./libft
|
@echo compiling libft...
|
||||||
|
@make --no-print-directory -j -C ./libft
|
||||||
|
@echo done
|
||||||
|
|
||||||
$(OBJS_DIR):
|
$(OBJS_DIR):
|
||||||
mkdir -p $(OBJS_DIR)
|
@mkdir -p $(OBJS_DIR)
|
||||||
|
|
||||||
$(OBJS_DIR)%.o : $(SRCS_DIR)%.c | $(OBJS_DIR)
|
$(OBJS_DIR)%.o : $(SRCS_DIR)%.c | $(OBJS_DIR)
|
||||||
$(CC) $(FLAGS) -c $< -o $@
|
@$(CC) $(FLAGS) -c $< -o $@
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf $(OBJS_DIR)
|
@if [ ! $(USER) = "marde-vr" ];then\
|
||||||
make --no-print-directory -C ./libft fclean
|
bash -c 'gamma=1;while (( $$(echo "$$gamma > 0" | bc -l) )); do gamma=$$(echo "$$gamma - 0.1" | bc); xrandr --output "HDMI-1-2" --brightness $$gamma; done;sleep 0.1';\
|
||||||
|
fi
|
||||||
|
@rm -rf $(OBJS_DIR)
|
||||||
|
@make --no-print-directory -C ./libft fclean
|
||||||
|
@if [ ! $(USER) = "marde-vr" ];then\
|
||||||
|
bash -c 'gamma=0;while (( $$(echo "$$gamma < 1" | bc -l) )); do gamma=$$(echo "$$gamma + 0.1" | bc);xrandr --output "HDMI-1-2" --brightness $$gamma;done&';\
|
||||||
|
fi
|
||||||
|
|
||||||
bonus: all
|
bonus: all
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
||||||
# Updated: 2024/02/11 17:41:50 by tomoron ### ########.fr #
|
# Updated: 2024/04/02 16:02:33 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -73,14 +73,14 @@ all: $(NAME)
|
|||||||
|
|
||||||
|
|
||||||
$(NAME): ft_printf $(OBJS)
|
$(NAME): ft_printf $(OBJS)
|
||||||
make --no-print-directory -C ./ft_printf
|
@make --no-print-directory -C ./ft_printf
|
||||||
cp ./ft_printf/libftprintf.a ./gnl/gnl.a
|
@cp ./ft_printf/libftprintf.a ./gnl/gnl.a
|
||||||
make --no-print-directory -C ./gnl
|
@make --no-print-directory -C ./gnl
|
||||||
cp ./gnl/gnl.a ./$(NAME)
|
@cp ./gnl/gnl.a ./$(NAME)
|
||||||
ar rcs $(NAME) $(OBJS)
|
@ar rcs $(NAME) $(OBJS)
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
$(CC) $(FLAGS) -c $< -o $@
|
@$(CC) $(FLAGS) -c $< -o $@
|
||||||
|
|
||||||
bonus: $(OBJS) $(OBJS_BONUS)
|
bonus: $(OBJS) $(OBJS_BONUS)
|
||||||
ar rcs $(NAME) $(OBJS) $(OBJS_BONUS)
|
ar rcs $(NAME) $(OBJS) $(OBJS_BONUS)
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
||||||
# Updated: 2024/02/16 14:22:14 by marde-vr ### ########.fr #
|
# Updated: 2024/04/02 16:02:53 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -34,10 +34,10 @@ OBJS = $(SRCS:.c=.o)
|
|||||||
FLAGS = -Wall -Wextra -Werror
|
FLAGS = -Wall -Wextra -Werror
|
||||||
|
|
||||||
$(NAME): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
ar rcs $(NAME) $(OBJS)
|
@ar rcs $(NAME) $(OBJS)
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
$(CC) $(FLAGS) -c $< -o $@
|
@$(CC) $(FLAGS) -c $< -o $@
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
|
@ -3,10 +3,10 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* ft_strchr.c :+: :+: :+: */
|
/* ft_strchr.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2023/10/30 13:35:44 by tomoron #+# #+# */
|
/* Created: 2023/10/30 13:35:44 by tomoron #+# #+# */
|
||||||
/* Updated: 2023/11/02 11:08:17 by tomoron ### ########.fr */
|
/* Updated: 2024/03/21 16:41:59 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
@ -17,6 +17,8 @@ char *ft_strchr(const char *s, int c)
|
|||||||
char *res;
|
char *res;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
|
if (!s)
|
||||||
|
return (NULL);
|
||||||
res = (char *)s;
|
res = (char *)s;
|
||||||
while (res[i])
|
while (res[i])
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
# By: tomoron <marvin@42.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
# Created: 2023/11/04 08:03:00 by tomoron #+# #+# #
|
||||||
# Updated: 2023/12/04 09:49:24 by tomoron ### ########.fr #
|
# Updated: 2024/04/02 16:03:57 by tomoron ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -22,10 +22,10 @@ OBJS = $(SRCS:.c=.o)
|
|||||||
FLAGS = -Wall -Wextra -Werror
|
FLAGS = -Wall -Wextra -Werror
|
||||||
|
|
||||||
$(NAME): $(OBJS)
|
$(NAME): $(OBJS)
|
||||||
ar rcs $(NAME) $(OBJS)
|
@ar rcs $(NAME) $(OBJS)
|
||||||
|
|
||||||
.c.o:
|
.c.o:
|
||||||
$(CC) $(FLAGS) -c $< -o $@
|
@$(CC) $(FLAGS) -c $< -o $@
|
||||||
|
|
||||||
all: $(NAME)
|
all: $(NAME)
|
||||||
|
|
||||||
|
@ -1,20 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* test2.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/02/07 13:30:04 by tomoron #+# #+# */
|
|
||||||
/* Updated: 2024/02/09 16:42:59 by tomoron ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
|
||||||
{
|
|
||||||
if (argc == 2)
|
|
||||||
return (atoi(argv[1]));
|
|
||||||
return (0);
|
|
||||||
}
|
|
79
srcs/alias.c
79
srcs/alias.c
@ -1,79 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* alias.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/02/16 23:16:07 by marde-vr #+# #+# */
|
|
||||||
/* Updated: 2024/02/21 17:39:40 by marde-vr ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
char *get_alias_name(t_cmd *arg)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *res;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (arg->token[i] && arg->token[i] != '=')
|
|
||||||
i++;
|
|
||||||
i++;
|
|
||||||
res = ft_calloc(i, 1);
|
|
||||||
ft_strlcpy(res, arg->token, i);
|
|
||||||
return (res);
|
|
||||||
}
|
|
||||||
|
|
||||||
char *get_alias_value(t_cmd *arg)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *res;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
while (arg->token[i] != '=')
|
|
||||||
i++;
|
|
||||||
i++;
|
|
||||||
res = ft_strdup(arg->token + i);
|
|
||||||
return (res);
|
|
||||||
}
|
|
||||||
|
|
||||||
void print_aliases(t_alias *aliases)
|
|
||||||
{
|
|
||||||
t_alias *cur_alias;
|
|
||||||
|
|
||||||
cur_alias = aliases;
|
|
||||||
while (cur_alias)
|
|
||||||
{
|
|
||||||
ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value);
|
|
||||||
cur_alias = cur_alias->next;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int alias(t_msh *msh)
|
|
||||||
{
|
|
||||||
char *name;
|
|
||||||
char *value;
|
|
||||||
|
|
||||||
if (!msh->cmds->next || msh->cmds->next->type != ARG)
|
|
||||||
print_aliases(msh->aliases);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
name = get_alias_name(msh->cmds->next);
|
|
||||||
if (ft_strchr(msh->cmds->next->token, '='))
|
|
||||||
{
|
|
||||||
value = get_alias_value(msh->cmds->next);
|
|
||||||
msh->aliases = alias_add_back(msh->aliases, name, value);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (get_alias(msh->aliases, name))
|
|
||||||
ft_printf("alias %s=%s\n", name, get_alias(msh->aliases, name));
|
|
||||||
else
|
|
||||||
ft_printf("minishell: alias %s: not found\n", name);
|
|
||||||
free(name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
81
srcs/builtins.c
Normal file → Executable file
81
srcs/builtins.c
Normal file → Executable file
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */
|
/* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/05 18:51:41 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/23 19:56:00 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,8 +16,8 @@ int cmd_is_forkable_builtin(char *cmd_token)
|
|||||||
{
|
{
|
||||||
if (!ft_strcmp(cmd_token, "echo") || !ft_strcmp(cmd_token, "ret")
|
if (!ft_strcmp(cmd_token, "echo") || !ft_strcmp(cmd_token, "ret")
|
||||||
|| !ft_strcmp(cmd_token, "env") || !ft_strcmp(cmd_token, "exit")
|
|| !ft_strcmp(cmd_token, "env") || !ft_strcmp(cmd_token, "exit")
|
||||||
|| !ft_strcmp(cmd_token, "pwd") || !ft_strcmp(cmd_token, "export")
|
|| !ft_strcmp(cmd_token, "pwd") || !ft_strcmp(cmd_token, "cd")
|
||||||
|| !ft_strcmp(cmd_token, "unset"))
|
|| !ft_strcmp(cmd_token, "export") || !ft_strcmp(cmd_token, "unset"))
|
||||||
return (1);
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
@ -26,54 +26,47 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token)
|
|||||||
{
|
{
|
||||||
if (!cmd_token)
|
if (!cmd_token)
|
||||||
return (0);
|
return (0);
|
||||||
else if (!ft_strcmp(cmd_token, "cd"))
|
else if ((msh->in_type == PIPE || msh->out_type == PIPE)
|
||||||
{
|
&& cmd_is_forkable_builtin(cmd_token) && ft_strcmp(cmd_token, "export"))
|
||||||
cd(msh->cmds);
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
else if (!ft_strcmp(cmd_token, "cd") && msh->out_type != PIPE)
|
||||||
else if (!ft_strcmp(cmd_token, "unalias"))
|
g_return_code = cd(msh->tokens, msh);
|
||||||
{
|
|
||||||
unalias(msh);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
else if (!ft_strcmp(cmd_token, "alias"))
|
|
||||||
{
|
|
||||||
alias(msh);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
else if (!ft_strcmp(cmd_token, "exit"))
|
else if (!ft_strcmp(cmd_token, "exit"))
|
||||||
{
|
g_return_code = exit_bt(msh);
|
||||||
exit_bt(msh);
|
else if (!ft_strcmp(cmd_token, "export") && msh->out_type != PIPE)
|
||||||
return (1);
|
g_return_code = ft_export(msh, msh->tokens, msh->env);
|
||||||
}
|
else if (!ft_strcmp(cmd_token, "unset"))
|
||||||
return (cmd_is_forkable_builtin(cmd_token));
|
g_return_code = ft_unset(msh);
|
||||||
|
else
|
||||||
|
return (cmd_is_forkable_builtin(cmd_token));
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int exec_builtin(t_msh *msh)
|
int exec_builtin(t_msh *msh)
|
||||||
{
|
{
|
||||||
if (!msh->cmds->token)
|
if (!msh->tokens->value)
|
||||||
return (STDIN_FILENO);
|
return (0);
|
||||||
if (!ft_strcmp(msh->cmds->token, "echo"))
|
if (!ft_strcmp(msh->tokens->value, "echo"))
|
||||||
g_return_code = echo(msh->cmds->next);
|
g_return_code = echo(msh->tokens->next);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "ret"))
|
else if (!ft_strcmp(msh->tokens->value, "ret"))
|
||||||
g_return_code = ft_atoi(msh->cmds->next->token);
|
g_return_code = ft_atoi(msh->tokens->next->value);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "env"))
|
else if (!ft_strcmp(msh->tokens->value, "env"))
|
||||||
g_return_code = print_env(msh->env);
|
g_return_code = print_env(msh->env);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "exit"))
|
else if (!ft_strcmp(msh->tokens->value, "exit"))
|
||||||
exit_bt(msh);
|
return (1);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "pwd"))
|
else if (!ft_strcmp(msh->tokens->value, "pwd"))
|
||||||
g_return_code = pwd();
|
g_return_code = pwd();
|
||||||
else if (!ft_strcmp(msh->cmds->token, "cd"))
|
else if (!ft_strcmp(msh->tokens->value, "cd") && msh->out_type == PIPE)
|
||||||
g_return_code = cd(msh->cmds);
|
g_return_code = cd(msh->tokens, msh);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "export"))
|
else if (!ft_strcmp(msh->tokens->value, "cd"))
|
||||||
g_return_code = ft_export(msh);
|
return (1);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "unset"))
|
else if (!ft_strcmp(msh->tokens->value, "export") && msh->out_type == PIPE)
|
||||||
g_return_code = ft_unset(msh);
|
g_return_code = ft_export(msh, msh->tokens, msh->env);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "alias"))
|
else if (!ft_strcmp(msh->tokens->value, "export"))
|
||||||
g_return_code = alias(msh);
|
return (1);
|
||||||
else if (!ft_strcmp(msh->cmds->token, "unalias"))
|
else if (!ft_strcmp(msh->tokens->value, "unset"))
|
||||||
g_return_code = unalias(msh);
|
return (1);
|
||||||
else
|
else
|
||||||
return (STDIN_FILENO);
|
return (0);
|
||||||
return (STDOUT_FILENO);
|
return (1);
|
||||||
}
|
}
|
||||||
|
66
srcs/cd.c
Normal file → Executable file
66
srcs/cd.c
Normal file → Executable file
@ -6,30 +6,78 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
|
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/06 08:43:53 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/24 10:43:52 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int cd(t_cmd *args)
|
void cd_update_pwd(t_msh *msh)
|
||||||
|
{
|
||||||
|
char *pwd;
|
||||||
|
char *new;
|
||||||
|
|
||||||
|
new = getcwd(0, 16382);
|
||||||
|
if (!new)
|
||||||
|
return ;
|
||||||
|
pwd = ft_get_env(msh->env, "PWD");
|
||||||
|
pwd = ft_strdup(pwd);
|
||||||
|
if (!pwd)
|
||||||
|
ft_strdup("");
|
||||||
|
if (!pwd)
|
||||||
|
free(new);
|
||||||
|
if (!pwd)
|
||||||
|
return ;
|
||||||
|
msh->env = export_set_env(msh->env, ft_strdup("OLDPWD"), pwd, 0);
|
||||||
|
if (ft_get_env(msh->env, "PWD"))
|
||||||
|
msh->env = export_set_env(msh->env, ft_strdup("PWD"), new, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_new_wd(t_token *arg, t_msh *msh)
|
||||||
|
{
|
||||||
|
char *nw_wd;
|
||||||
|
|
||||||
|
if (arg)
|
||||||
|
{
|
||||||
|
nw_wd = arg->value;
|
||||||
|
if (!ft_strcmp("-", nw_wd))
|
||||||
|
{
|
||||||
|
nw_wd = ft_get_env(msh->env, "OLDPWD");
|
||||||
|
if (!nw_wd)
|
||||||
|
ft_putstr_fd("minishell: cd: OLDPWD not set\n", 2);
|
||||||
|
if (!nw_wd)
|
||||||
|
return (0);
|
||||||
|
ft_printf_fd((1 * (msh->out_fd == 0)) + msh->out_fd, "%s\n", nw_wd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
nw_wd = ft_get_env(msh->env, "HOME");
|
||||||
|
if (!nw_wd)
|
||||||
|
ft_putstr_fd("minishell: cd: HOME not set\n", 2);
|
||||||
|
if (!nw_wd)
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (nw_wd);
|
||||||
|
}
|
||||||
|
|
||||||
|
int cd(t_token *args, t_msh *msh)
|
||||||
{
|
{
|
||||||
char *new_wd;
|
char *new_wd;
|
||||||
|
|
||||||
if (args->next && args->next->next && args->next->next->type == ARG)
|
if (args->next && args->next->next)
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "minishell: cd: too many arguments\n");
|
ft_printf_fd(2, "minishell: cd: too many arguments\n");
|
||||||
g_return_code = 1;
|
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
if (!args->next || args->next->type != ARG)
|
new_wd = get_new_wd(args->next, msh);
|
||||||
new_wd = getenv("HOME");
|
if (!new_wd)
|
||||||
else
|
return (1);
|
||||||
new_wd = args->next->token;
|
|
||||||
if (chdir(new_wd) == -1)
|
if (chdir(new_wd) == -1)
|
||||||
{
|
{
|
||||||
perror("minishell: cd");
|
perror("minishell: cd");
|
||||||
g_return_code = 1;
|
return (1);
|
||||||
}
|
}
|
||||||
|
cd_update_pwd(msh);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
120
srcs/check_syntax.c
Normal file
120
srcs/check_syntax.c
Normal file
@ -0,0 +1,120 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* check_syntax.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/24 14:50:15 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/25 13:50:46 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int check_parens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
|
||||||
|
{
|
||||||
|
t_cmd *parsed_cmd;
|
||||||
|
t_cmd *tmp;
|
||||||
|
|
||||||
|
if (last && is_cmd_type(last))
|
||||||
|
{
|
||||||
|
ft_putstr_fd("minishell: syntax error\n", 2);
|
||||||
|
g_return_code = 2;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
parsed_cmd = parsing_bonus(cmd->value);
|
||||||
|
if (!parsed_cmd)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("minishell: syntax error\n", 2);
|
||||||
|
g_return_code = 2;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
tmp = check_cmds_syntax(parsed_cmd, env);
|
||||||
|
if (tmp)
|
||||||
|
print_syntax_error_bonus(tmp);
|
||||||
|
free_cmd(parsed_cmd);
|
||||||
|
return (tmp == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_cmd_type_syntax(t_cmd *cmds, t_cmd *last, t_env *env)
|
||||||
|
{
|
||||||
|
if (cmds->cmd_type == CMD)
|
||||||
|
{
|
||||||
|
if (!check_tokens_syntax(cmds, last, env))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else if (cmds->cmd_type == PAREN)
|
||||||
|
{
|
||||||
|
if (!check_parens_syntax(cmds, last, env))
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_cmd_syntax(t_cmd *cmds, t_cmd *last, t_env *env)
|
||||||
|
{
|
||||||
|
if (cmds->cmd_type == ERR)
|
||||||
|
return (1);
|
||||||
|
if (!is_operand_type(cmds) && cmds->cmd_type != PIPE
|
||||||
|
&& cmds->value == 0)
|
||||||
|
return (1);
|
||||||
|
if (is_operand_type(cmds) || cmds->cmd_type == PIPE)
|
||||||
|
if ((!is_cmd_type(last) && !is_output_type(last)
|
||||||
|
&& !is_input_type(last)) || !cmds->next
|
||||||
|
|| (!is_cmd_type(cmds->next) && !is_output_type(cmds->next)
|
||||||
|
&& !is_input_type(cmds->next)))
|
||||||
|
return (1);
|
||||||
|
if (is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, last, env))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env)
|
||||||
|
{
|
||||||
|
t_cmd *last;
|
||||||
|
|
||||||
|
if (!cmds || is_operand_type(cmds) || cmds->cmd_type == PIPE)
|
||||||
|
return (cmds);
|
||||||
|
if (!is_operand_type(cmds) && cmds->cmd_type != PIPE && cmds->value == 0)
|
||||||
|
return (cmds);
|
||||||
|
last = cmds;
|
||||||
|
if (is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, 0, env))
|
||||||
|
return (cmds);
|
||||||
|
cmds = cmds->next;
|
||||||
|
while (cmds)
|
||||||
|
{
|
||||||
|
if (check_cmd_syntax(cmds, last, env))
|
||||||
|
return (cmds);
|
||||||
|
last = cmds;
|
||||||
|
cmds = cmds->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_str_syntax(char *cmd)
|
||||||
|
{
|
||||||
|
int in_quote;
|
||||||
|
int in_dquote;
|
||||||
|
int parenthesis;
|
||||||
|
|
||||||
|
in_quote = 0;
|
||||||
|
in_dquote = 0;
|
||||||
|
parenthesis = 0;
|
||||||
|
while (*cmd)
|
||||||
|
{
|
||||||
|
if (*cmd == '\'' && !in_dquote)
|
||||||
|
in_quote = !in_quote;
|
||||||
|
if (*cmd == '"' && !in_quote)
|
||||||
|
in_dquote = !in_dquote;
|
||||||
|
if ((*cmd == '(' || *cmd == ')') && !in_quote && !in_dquote)
|
||||||
|
parenthesis += 1 - (2 * (*cmd == ')'));
|
||||||
|
cmd++;
|
||||||
|
}
|
||||||
|
if (in_quote || in_dquote || parenthesis)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("minishell: syntax error\n", 2);
|
||||||
|
g_return_code = 2;
|
||||||
|
}
|
||||||
|
return (!(in_quote || in_dquote || parenthesis));
|
||||||
|
}
|
48
srcs/check_syntax_utils.c
Normal file
48
srcs/check_syntax_utils.c
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* check_syntax_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/24 14:54:53 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/25 13:51:03 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void print_syntax_error_bonus(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
if (cmd->cmd_type == CMD || cmd->cmd_type == PAREN)
|
||||||
|
return ;
|
||||||
|
ft_printf_fd(2, "minishell : syntax error near unexpected token `");
|
||||||
|
g_return_code = 2;
|
||||||
|
if (cmd->cmd_type == AND)
|
||||||
|
ft_printf_fd(2, "&&");
|
||||||
|
if (cmd->cmd_type == OR)
|
||||||
|
ft_printf_fd(2, "||");
|
||||||
|
if (cmd->cmd_type == PIPE)
|
||||||
|
ft_printf_fd(2, "|");
|
||||||
|
if (cmd->cmd_type == ERR)
|
||||||
|
ft_printf_fd(2, "&");
|
||||||
|
ft_printf_fd(2, "'\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
|
||||||
|
{
|
||||||
|
t_token *token;
|
||||||
|
|
||||||
|
if (last && is_cmd_type(last))
|
||||||
|
{
|
||||||
|
g_return_code = 2;
|
||||||
|
ft_putstr_fd("minishell : syntax error\n", 2);
|
||||||
|
g_return_code = 2;
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
token = parse_cmds_to_token(cmd, env);
|
||||||
|
if (token == (void *)1)
|
||||||
|
return (0);
|
||||||
|
free_token(token);
|
||||||
|
return (1);
|
||||||
|
}
|
89
srcs/commands.c
Normal file → Executable file
89
srcs/commands.c
Normal file → Executable file
@ -6,49 +6,25 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */
|
/* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/05 18:24:19 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/23 13:46:25 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int get_cmd_count(t_cmd *cmds)
|
int get_args_count(t_token *cmds)
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
t_cmd *cur_cmd;
|
t_token *cur_cmd;
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
cur_cmd = cmds;
|
cur_cmd = cmds;
|
||||||
while (cur_cmd->next != 0)
|
if (cur_cmd)
|
||||||
{
|
|
||||||
if (cur_cmd->type != ARG)
|
|
||||||
if (cur_cmd->type == PIPE)
|
|
||||||
count++;
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
}
|
|
||||||
if (cur_cmd->type == ARG)
|
|
||||||
count++;
|
count++;
|
||||||
return (count);
|
while (cur_cmd && cur_cmd->next)
|
||||||
}
|
|
||||||
|
|
||||||
int get_args_count(t_cmd *cmds)
|
|
||||||
{
|
|
||||||
int count;
|
|
||||||
t_cmd *cur_cmd;
|
|
||||||
|
|
||||||
count = 0;
|
|
||||||
cur_cmd = cmds;
|
|
||||||
if (cur_cmd->type == ARG)
|
|
||||||
count++;
|
|
||||||
while (cur_cmd->next)
|
|
||||||
{
|
{
|
||||||
if (cur_cmd->type == PIPE)
|
|
||||||
break ;
|
|
||||||
cur_cmd = cur_cmd->next;
|
cur_cmd = cur_cmd->next;
|
||||||
if (cur_cmd->type == ARG)
|
count++;
|
||||||
count++;
|
|
||||||
else if (cur_cmd->type != PIPE)
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
}
|
}
|
||||||
return (count);
|
return (count);
|
||||||
}
|
}
|
||||||
@ -56,22 +32,24 @@ int get_args_count(t_cmd *cmds)
|
|||||||
char **get_cmd_args(t_msh *msh)
|
char **get_cmd_args(t_msh *msh)
|
||||||
{
|
{
|
||||||
char **cmd_args;
|
char **cmd_args;
|
||||||
t_cmd *cur_cmd;
|
t_token *cur_cmd;
|
||||||
int args_count;
|
int args_count;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
args_count = get_args_count(msh->cmds);
|
args_count = get_args_count(msh->tokens);
|
||||||
cmd_args = ft_calloc(args_count + 1, sizeof(char *));
|
cmd_args = ft_calloc(args_count + 1, sizeof(char *));
|
||||||
if (!cmd_args || !msh->fds)
|
if (!cmd_args)
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
cur_cmd = msh->cmds;
|
cur_cmd = msh->tokens;
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < args_count)
|
while (i < args_count)
|
||||||
{
|
{
|
||||||
if (cur_cmd->type == ARG)
|
if (cur_cmd)
|
||||||
{
|
{
|
||||||
cmd_args[i] = cur_cmd->token;
|
if (!i)
|
||||||
i++;
|
cmd_args[i++] = remove_path(cur_cmd->value);
|
||||||
|
else
|
||||||
|
cmd_args[i++] = cur_cmd->value;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
cur_cmd = cur_cmd->next;
|
cur_cmd = cur_cmd->next;
|
||||||
@ -82,28 +60,19 @@ char **get_cmd_args(t_msh *msh)
|
|||||||
|
|
||||||
void remove_command_from_msh(t_msh *msh)
|
void remove_command_from_msh(t_msh *msh)
|
||||||
{
|
{
|
||||||
t_cmd *cur_cmd;
|
t_cmd *tmp;
|
||||||
t_cmd *cmd_tmp;
|
|
||||||
|
|
||||||
cur_cmd = msh->cmds;
|
free_token(msh->tokens);
|
||||||
while (cur_cmd && cur_cmd->next)
|
while (msh->cmds && !is_cmd_type(msh->cmds))
|
||||||
{
|
msh->cmds = msh->cmds->next;
|
||||||
if (cur_cmd->type == PIPE)
|
while (msh->cmds && (is_cmd_type(msh->cmds) || is_input_type(msh->cmds)
|
||||||
{
|
|| is_output_type(msh->cmds)))
|
||||||
cmd_tmp = cur_cmd;
|
msh->cmds = msh->cmds->next;
|
||||||
cur_cmd = cur_cmd->next;
|
tmp = msh->cmds;
|
||||||
msh->in_type = cmd_tmp->type;
|
while (tmp && !is_cmd_type(tmp))
|
||||||
free(cmd_tmp->token);
|
tmp = tmp->next;
|
||||||
free(cmd_tmp);
|
if (tmp)
|
||||||
msh->cmds = cur_cmd;
|
msh->tokens = parse_cmds_to_token(tmp, msh->env);
|
||||||
return ;
|
else
|
||||||
}
|
msh->tokens = 0;
|
||||||
cmd_tmp = cur_cmd;
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
msh->in_type = cur_cmd->type;
|
|
||||||
free(cmd_tmp->token);
|
|
||||||
free(cmd_tmp);
|
|
||||||
msh->cmds = cur_cmd;
|
|
||||||
}
|
|
||||||
msh->in_type = msh->cmds->type;
|
|
||||||
}
|
}
|
||||||
|
60
srcs/debug.c
Normal file → Executable file
60
srcs/debug.c
Normal file → Executable file
@ -6,31 +6,61 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/18 15:46:50 by tomoron #+# #+# */
|
/* Created: 2024/02/18 15:46:50 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 12:59:08 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/18 20:48:51 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void print_parsed_token(t_token *token)
|
||||||
|
{
|
||||||
|
while (token)
|
||||||
|
{
|
||||||
|
printf("[ARG : \"%s\"]", token->value);
|
||||||
|
token = token->next;
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_cmd_type(t_cmd_type type, char *value)
|
||||||
|
{
|
||||||
|
if (type == CMD)
|
||||||
|
printf("[CMD : %s] ", value);
|
||||||
|
if (type == PAREN)
|
||||||
|
printf("[PAREN : %s] ", value);
|
||||||
|
if (type == AND)
|
||||||
|
printf("[AND] ");
|
||||||
|
if (type == OR)
|
||||||
|
printf("[OR] ");
|
||||||
|
if (type == PIPE)
|
||||||
|
printf("[PIPE] ");
|
||||||
|
if (type == RED_O_APP)
|
||||||
|
printf("[RED_O_APP : %s] ", value);
|
||||||
|
if (type == RED_O)
|
||||||
|
printf("[RED_O : %s] ", value);
|
||||||
|
if (type == RED_I)
|
||||||
|
printf("[RED_I : %s] ", value);
|
||||||
|
if (type == HERE_DOC)
|
||||||
|
printf("[HERE_DOC : %s] ", value);
|
||||||
|
}
|
||||||
|
|
||||||
void print_parsed_cmd(t_cmd *cmd)
|
void print_parsed_cmd(t_cmd *cmd)
|
||||||
{
|
{
|
||||||
while (cmd)
|
while (cmd)
|
||||||
{
|
{
|
||||||
printf("[");
|
print_cmd_type(cmd->cmd_type, cmd->value);
|
||||||
if (cmd->type == ARG)
|
|
||||||
printf("ARG : \"%s\"", cmd->token);
|
|
||||||
else if (cmd->type == PIPE)
|
|
||||||
printf("PIPE");
|
|
||||||
else if (cmd->type == RED_O)
|
|
||||||
printf("RED_O");
|
|
||||||
else if (cmd->type == RED_O_APP)
|
|
||||||
printf("RED_O_APP");
|
|
||||||
else if (cmd->type == RED_I)
|
|
||||||
printf("RED_I");
|
|
||||||
else if (cmd->type == HERE_DOC)
|
|
||||||
printf("HERE_DOC");
|
|
||||||
printf("] ");
|
|
||||||
cmd = cmd->next;
|
cmd = cmd->next;
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void print_msh_struct(t_msh *msh)
|
||||||
|
{
|
||||||
|
printf("in_fd : %d\n", msh->in_fd);
|
||||||
|
printf("out_fd : %d\n", msh->out_fd);
|
||||||
|
printf("in_type: ");
|
||||||
|
print_cmd_type(msh->in_type, 0);
|
||||||
|
printf("\nout_type: ");
|
||||||
|
print_cmd_type(msh->out_type, 0);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
32
srcs/echo.c
32
srcs/echo.c
@ -6,41 +6,43 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
|
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/05 19:00:53 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/24 14:52:21 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
void put_args(t_cmd *args)
|
void put_args(t_token *args)
|
||||||
{
|
{
|
||||||
int first;
|
int first;
|
||||||
|
|
||||||
first = 1;
|
first = 1;
|
||||||
while (args && args->type != PIPE)
|
while (args)
|
||||||
{
|
{
|
||||||
if (args->type != ARG)
|
if (!first)
|
||||||
args = args->next;
|
ft_putchar_fd(' ', STDOUT_FILENO);
|
||||||
else
|
ft_putstr_fd(args->value, STDOUT_FILENO);
|
||||||
{
|
first = 0;
|
||||||
if (!first)
|
|
||||||
ft_putchar_fd(' ', STDOUT_FILENO);
|
|
||||||
ft_putstr_fd(args->token, STDOUT_FILENO);
|
|
||||||
first = 0;
|
|
||||||
}
|
|
||||||
args = args->next;
|
args = args->next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int echo(t_cmd *args)
|
int echo(t_token *args)
|
||||||
{
|
{
|
||||||
int put_nl;
|
int put_nl;
|
||||||
|
int i;
|
||||||
|
|
||||||
put_nl = 1;
|
put_nl = 1;
|
||||||
while (args && args->token && !strcmp(args->token, "-n"))
|
i = 1;
|
||||||
|
while (args && args->value && args->value[0] == '-')
|
||||||
{
|
{
|
||||||
put_nl = 0;
|
while (args->value[i] == 'n')
|
||||||
|
i++;
|
||||||
|
if (!args->value[i] && i > 1)
|
||||||
|
put_nl = 0;
|
||||||
|
else
|
||||||
|
ft_printf("%s ", args->value);
|
||||||
args = args->next;
|
args = args->next;
|
||||||
}
|
}
|
||||||
put_args(args);
|
put_args(args);
|
||||||
|
104
srcs/env.c
Normal file
104
srcs/env.c
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* env.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/24 10:58:36 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/04/26 10:26:03 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_env *dup_env(t_env *env)
|
||||||
|
{
|
||||||
|
t_env *res;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
while (env)
|
||||||
|
{
|
||||||
|
res = env_add_back(res, env->name, env->value);
|
||||||
|
env = env->next;
|
||||||
|
}
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_env(t_env *env)
|
||||||
|
{
|
||||||
|
t_env *tmp;
|
||||||
|
t_env *start;
|
||||||
|
char *tmp_str;
|
||||||
|
|
||||||
|
tmp = env;
|
||||||
|
start = env;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
env = start;
|
||||||
|
while (env)
|
||||||
|
{
|
||||||
|
if (ft_strcmp(tmp->name, env->name) < 0)
|
||||||
|
{
|
||||||
|
tmp_str = tmp->name;
|
||||||
|
tmp->name = env->name;
|
||||||
|
env->name = tmp_str;
|
||||||
|
tmp_str = tmp->value;
|
||||||
|
tmp->value = env->value;
|
||||||
|
env->value = tmp_str;
|
||||||
|
}
|
||||||
|
env = env->next;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_env_declare(t_msh *msh, t_env *env_orig)
|
||||||
|
{
|
||||||
|
t_env *env;
|
||||||
|
t_env *start;
|
||||||
|
|
||||||
|
env = dup_env(env_orig);
|
||||||
|
sort_env(env);
|
||||||
|
start = env;
|
||||||
|
if (!msh->out_fd)
|
||||||
|
msh->out_fd = 1;
|
||||||
|
while (env)
|
||||||
|
{
|
||||||
|
if (strcmp(env->name, "_"))
|
||||||
|
{
|
||||||
|
if (env->value && *(env->value))
|
||||||
|
ft_printf_fd(msh->out_fd, "declare -x %s=\"%s\"\n", env->name,
|
||||||
|
env->value);
|
||||||
|
else
|
||||||
|
ft_printf_fd(msh->out_fd, "declare -x %s\n", env->name);
|
||||||
|
}
|
||||||
|
env = env->next;
|
||||||
|
}
|
||||||
|
free_env_cpy(start);
|
||||||
|
}
|
||||||
|
|
||||||
|
void delete_from_env(t_msh *msh, char *name)
|
||||||
|
{
|
||||||
|
t_env *tmp_env;
|
||||||
|
t_env *prev;
|
||||||
|
|
||||||
|
tmp_env = msh->env;
|
||||||
|
prev = 0;
|
||||||
|
while (tmp_env)
|
||||||
|
{
|
||||||
|
if (!strcmp(name, tmp_env->name))
|
||||||
|
{
|
||||||
|
free(tmp_env->name);
|
||||||
|
free(tmp_env->value);
|
||||||
|
if (!prev)
|
||||||
|
msh->env = tmp_env->next;
|
||||||
|
else
|
||||||
|
prev->next = tmp_env->next;
|
||||||
|
free(tmp_env);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
prev = tmp_env;
|
||||||
|
tmp_env = tmp_env->next;
|
||||||
|
}
|
||||||
|
}
|
25
srcs/env_to_char_tab.c → srcs/env_utils.c
Normal file → Executable file
25
srcs/env_to_char_tab.c → srcs/env_utils.c
Normal file → Executable file
@ -1,12 +1,12 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* env_to_char_tab.c :+: :+: :+: */
|
/* env_utils.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/16 13:30:18 by tomoron #+# #+# */
|
/* Created: 2024/02/16 13:30:18 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/16 15:08:53 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/18 20:48:52 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -43,3 +43,24 @@ char **env_to_char_tab(t_env *env)
|
|||||||
}
|
}
|
||||||
return (res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char **split_paths_from_env(t_env *env)
|
||||||
|
{
|
||||||
|
t_env *cur_env_var;
|
||||||
|
int path_in_envp;
|
||||||
|
|
||||||
|
path_in_envp = 0;
|
||||||
|
cur_env_var = env;
|
||||||
|
while (cur_env_var && cur_env_var->next != 0)
|
||||||
|
{
|
||||||
|
if (!ft_strcmp(cur_env_var->name, "PATH"))
|
||||||
|
{
|
||||||
|
path_in_envp = 1;
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
cur_env_var = cur_env_var->next;
|
||||||
|
}
|
||||||
|
if (!path_in_envp)
|
||||||
|
return (0);
|
||||||
|
return (ft_split(cur_env_var->value, ':'));
|
||||||
|
}
|
94
srcs/exec.c
94
srcs/exec.c
@ -1,94 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* exec.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
|
||||||
/* Updated: 2024/03/05 19:05:20 by marde-vr ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
|
||||||
{
|
|
||||||
pid_t pid;
|
|
||||||
|
|
||||||
if (i != cmd_count - 1)
|
|
||||||
{
|
|
||||||
if (pipe(msh->fds[i]) == -1)
|
|
||||||
{
|
|
||||||
perror("pipe");
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pid = fork();
|
|
||||||
if (pid == -1)
|
|
||||||
{
|
|
||||||
perror("fork");
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
if (pid == 0)
|
|
||||||
child(msh, cmd_args, i);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
parent(msh, i, cmd_count);
|
|
||||||
msh->pids[i] = pid;
|
|
||||||
free(cmd_args);
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void exec_command(t_msh *msh, int i, int cmd_count)
|
|
||||||
{
|
|
||||||
g_return_code = 0;
|
|
||||||
msh->fds[i] = ft_calloc(2, sizeof(int *));
|
|
||||||
if (!msh->fds[i])
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
if (first_is_in_type(msh))
|
|
||||||
{
|
|
||||||
get_in_type(msh, msh->cmds);
|
|
||||||
if (!g_return_code)
|
|
||||||
get_out_type(msh, msh->cmds);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
get_out_type(msh, msh->cmds);
|
|
||||||
if (!g_return_code)
|
|
||||||
get_in_type(msh, msh->cmds);
|
|
||||||
}
|
|
||||||
if (!cmd_is_builtin(msh, msh->cmds->token))
|
|
||||||
get_cmd_path(msh);
|
|
||||||
exec(msh, get_cmd_args(msh), i, cmd_count);
|
|
||||||
remove_command_from_msh(msh);
|
|
||||||
}
|
|
||||||
|
|
||||||
void exec_commands(t_msh *msh)
|
|
||||||
{
|
|
||||||
int cmd_count;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = -1;
|
|
||||||
if (!msh->cmds)
|
|
||||||
return ;
|
|
||||||
cmd_count = get_cmd_count(msh->cmds);
|
|
||||||
msh->fds = ft_calloc(cmd_count, sizeof(int **));
|
|
||||||
msh->pids = ft_calloc(cmd_count, sizeof(int *));
|
|
||||||
if (!msh->pids || !msh->fds)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
while (++i < cmd_count)
|
|
||||||
exec_command(msh, i, cmd_count);
|
|
||||||
i = cmd_count;
|
|
||||||
while (--i >= 0)
|
|
||||||
waitpid(msh->pids[i], 0, 0);
|
|
||||||
i = 0;
|
|
||||||
while (i < cmd_count)
|
|
||||||
{
|
|
||||||
free(msh->fds[i]);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
free(msh->fds);
|
|
||||||
free(msh->pids);
|
|
||||||
}
|
|
142
srcs/exec_bonus.c
Executable file
142
srcs/exec_bonus.c
Executable file
@ -0,0 +1,142 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* exec_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/26 11:08:13 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void exec_command_bonus(t_msh *msh, char *cmd_str)
|
||||||
|
{
|
||||||
|
t_cmd *cmds;
|
||||||
|
t_cmd *tmp;
|
||||||
|
|
||||||
|
if (!cmd_str)
|
||||||
|
return ;
|
||||||
|
cmds = parsing_bonus(cmd_str);
|
||||||
|
tmp = check_cmds_syntax(cmds, msh->env);
|
||||||
|
if (tmp)
|
||||||
|
{
|
||||||
|
print_syntax_error_bonus(tmp);
|
||||||
|
free_cmd(cmds);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
msh->cmds_head = cmds;
|
||||||
|
while (cmds)
|
||||||
|
{
|
||||||
|
msh->tokens = parse_cmds_to_token(cmds, msh->env);
|
||||||
|
msh->cmds = cmds;
|
||||||
|
exec_commands(msh);
|
||||||
|
msh->in_fd = 0;
|
||||||
|
msh->out_fd = 0;
|
||||||
|
cmds = get_next_command(cmds);
|
||||||
|
}
|
||||||
|
free_cmd(msh->cmds_head);
|
||||||
|
msh->cmds_head = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
||||||
|
{
|
||||||
|
pid_t pid;
|
||||||
|
|
||||||
|
if (i != cmd_count - 1)
|
||||||
|
{
|
||||||
|
if (pipe(msh->fds[i]) == -1)
|
||||||
|
{
|
||||||
|
perror("minishell: pipe");
|
||||||
|
free(cmd_args);
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pid = fork();
|
||||||
|
if (pid == -1)
|
||||||
|
{
|
||||||
|
perror("minishell: fork");
|
||||||
|
free(cmd_args);
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
|
if (pid == 0)
|
||||||
|
child(msh, cmd_args, i);
|
||||||
|
if (pid != 0)
|
||||||
|
msh->pids[i] = pid;
|
||||||
|
if (pid != 0)
|
||||||
|
parent(msh, i, cmd_count, cmd_args);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exec_command(t_msh *msh, int i, int cmd_count)
|
||||||
|
{
|
||||||
|
get_redirections(msh, msh->cmds);
|
||||||
|
g_return_code = 0;
|
||||||
|
if (msh->out_fd != -1 && msh->in_fd != -1)
|
||||||
|
{
|
||||||
|
msh->fds[i] = ft_calloc(2, sizeof(int *));
|
||||||
|
if (!msh->fds[i])
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
if (msh->tokens && !cmd_is_builtin(msh, msh->tokens->value))
|
||||||
|
get_cmd_path(msh);
|
||||||
|
if ((msh->tokens && msh->tokens->value) || is_parenthesis(msh->cmds))
|
||||||
|
exec(msh, get_cmd_args(msh), i, cmd_count);
|
||||||
|
}
|
||||||
|
remove_command_from_msh(msh);
|
||||||
|
}
|
||||||
|
|
||||||
|
void end_execution(t_msh *msh, int cmd_count)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int status;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
status = 0;
|
||||||
|
while (i < cmd_count)
|
||||||
|
waitpid(msh->pids[i++], &status, 0);
|
||||||
|
if (!g_return_code && WIFEXITED(status))
|
||||||
|
g_return_code = WEXITSTATUS(status);
|
||||||
|
if (WIFSIGNALED(status))
|
||||||
|
print_signaled(status);
|
||||||
|
if (msh->here_doc_filename)
|
||||||
|
{
|
||||||
|
unlink(msh->here_doc_filename);
|
||||||
|
free(msh->here_doc_filename);
|
||||||
|
msh->here_doc_filename = 0;
|
||||||
|
}
|
||||||
|
free(msh->pids);
|
||||||
|
free_fds(msh);
|
||||||
|
msh->pids = 0;
|
||||||
|
free(msh->fds);
|
||||||
|
signal(SIGINT, signal_handler_interactive);
|
||||||
|
signal(SIGQUIT, signal_handler_interactive);
|
||||||
|
}
|
||||||
|
|
||||||
|
void exec_commands(t_msh *msh)
|
||||||
|
{
|
||||||
|
int cmd_count;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (!msh->tokens && !is_parenthesis(msh->cmds))
|
||||||
|
{
|
||||||
|
g_return_code = 0;
|
||||||
|
get_redirections(msh, msh->cmds);
|
||||||
|
if (msh->in_fd > 2)
|
||||||
|
close(msh->in_fd);
|
||||||
|
if (msh->out_fd > 2)
|
||||||
|
close(msh->out_fd);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
cmd_count = get_cmd_count(msh->cmds);
|
||||||
|
msh->fds = ft_calloc(cmd_count + 1, sizeof(int **));
|
||||||
|
msh->pids = ft_calloc(cmd_count, sizeof(int *));
|
||||||
|
if (!msh->pids || !msh->fds)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
i = -1;
|
||||||
|
while (++i < cmd_count && msh->in_fd >= 0 && msh->out_fd >= 0)
|
||||||
|
exec_command(msh, i, cmd_count);
|
||||||
|
free_token(msh->tokens);
|
||||||
|
end_execution(msh, cmd_count);
|
||||||
|
}
|
102
srcs/exec_utils.c
Normal file
102
srcs/exec_utils.c
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* exec_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/24 10:46:28 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/04/24 14:05:18 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void get_redirections(t_msh *msh, t_cmd *cmds)
|
||||||
|
{
|
||||||
|
msh->in_type = 0;
|
||||||
|
msh->out_type = 0;
|
||||||
|
if (first_is_in_type(cmds))
|
||||||
|
{
|
||||||
|
if (!get_in_type(msh, cmds, cmds, 1))
|
||||||
|
get_out_type(msh, cmds);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!get_out_type(msh, cmds))
|
||||||
|
get_in_type(msh, cmds, cmds, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
t_cmd *get_next_command(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
while (cmd)
|
||||||
|
{
|
||||||
|
while (cmd && !is_operand_type(cmd))
|
||||||
|
cmd = cmd->next;
|
||||||
|
if (cmd && cmd->cmd_type == AND && !g_return_code)
|
||||||
|
return (cmd->next);
|
||||||
|
if (cmd && cmd->cmd_type == OR && g_return_code)
|
||||||
|
return (cmd->next);
|
||||||
|
if (cmd)
|
||||||
|
cmd = cmd->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_cmd_count(t_cmd *cmds)
|
||||||
|
{
|
||||||
|
int nb;
|
||||||
|
|
||||||
|
nb = 0;
|
||||||
|
while (cmds && !is_operand_type(cmds))
|
||||||
|
{
|
||||||
|
while (cmds && (is_output_type(cmds) || is_input_type(cmds)))
|
||||||
|
cmds = cmds->next;
|
||||||
|
if (is_cmd_type(cmds))
|
||||||
|
nb++;
|
||||||
|
while (cmds && (is_output_type(cmds) || is_input_type(cmds)
|
||||||
|
|| is_cmd_type(cmds)))
|
||||||
|
cmds = cmds->next;
|
||||||
|
if (cmds && cmds->cmd_type == PIPE)
|
||||||
|
cmds = cmds->next;
|
||||||
|
}
|
||||||
|
return (nb);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_parenthesis(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
if (!cmd)
|
||||||
|
return (0);
|
||||||
|
return (cmd->cmd_type == PAREN || (cmd->cmd_type == PIPE
|
||||||
|
&& cmd->next->cmd_type == PAREN));
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_signaled(int status)
|
||||||
|
{
|
||||||
|
int signal;
|
||||||
|
static const char *sigmsg[] = {0, "Hangup", 0, "Quit", "Illegal \
|
||||||
|
instruction", "Trace/breakpoint trap", "Aborted", "Bus error",
|
||||||
|
"Floating point exception", "Killed", "User defined signal 1",
|
||||||
|
"Segmentation fault (skill issue)", "User defined signal 2", 0,
|
||||||
|
"Alarm clock", "Terminated", "Stack fault", 0, 0, "Stopped", "Stopped",
|
||||||
|
"Stopped", "Stopped", 0, "CPU time limit exceeded",
|
||||||
|
"File size limit exceeded", "Virtual time expired",
|
||||||
|
"Profiling timer expired", "I/O possible", "Power failure",
|
||||||
|
"Bad system call"};
|
||||||
|
|
||||||
|
signal = WTERMSIG(status);
|
||||||
|
if (signal < 31 && sigmsg[signal])
|
||||||
|
{
|
||||||
|
ft_putstr_fd((char *)sigmsg[signal], 2);
|
||||||
|
}
|
||||||
|
if (signal >= 34 && signal <= 64)
|
||||||
|
{
|
||||||
|
ft_putstr_fd("Real-time signal ", 2);
|
||||||
|
ft_putnbr_fd(signal - 34, 2);
|
||||||
|
}
|
||||||
|
if (WCOREDUMP(status))
|
||||||
|
ft_putstr_fd(" (core dumped)", 2);
|
||||||
|
ft_putstr_fd("\n", 2);
|
||||||
|
g_return_code = signal + 128;
|
||||||
|
}
|
45
srcs/exit.c
45
srcs/exit.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/06 09:10:11 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/26 10:49:17 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -20,29 +20,42 @@ void numeric_arg_err(char *arg, int *exit_code)
|
|||||||
*exit_code = 2;
|
*exit_code = 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
void exit_bt(t_msh *msh)
|
int is_too_big(char *num_str)
|
||||||
{
|
{
|
||||||
t_cmd *cur_cmd;
|
if ((strlen(num_str) == 19 && strcmp(num_str, "9223372036854775807") > 0)
|
||||||
|
|| (strlen(num_str) == 20 && num_str[0] == '-' && strcmp(num_str,
|
||||||
|
"-9223372036854775808") > 0) || strlen(num_str) > 20)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_exit_bt_return_code(t_msh *msh, int *exit_code)
|
||||||
|
{
|
||||||
|
t_token *cur_cmd;
|
||||||
|
|
||||||
|
cur_cmd = msh->tokens->next;
|
||||||
|
if (cur_cmd && (!ft_strisnbr(cur_cmd->value) || is_too_big(cur_cmd->value)))
|
||||||
|
numeric_arg_err(cur_cmd->value, exit_code);
|
||||||
|
else if (cur_cmd)
|
||||||
|
*exit_code = (unsigned char)ft_atoi(cur_cmd->value);
|
||||||
|
else
|
||||||
|
*exit_code = g_return_code;
|
||||||
|
}
|
||||||
|
|
||||||
|
int exit_bt(t_msh *msh)
|
||||||
|
{
|
||||||
|
t_token *cur_cmd;
|
||||||
int exit_code;
|
int exit_code;
|
||||||
|
|
||||||
cur_cmd = msh->cmds->next;
|
cur_cmd = msh->tokens->next;
|
||||||
ft_printf("exit\n");
|
ft_printf("exit\n");
|
||||||
if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG
|
if (cur_cmd && cur_cmd->next && ft_strisnbr(cur_cmd->value))
|
||||||
&& ft_strisnbr(cur_cmd->token))
|
|
||||||
{
|
|
||||||
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
|
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
|
||||||
g_return_code = 1;
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (cur_cmd && cur_cmd->type == ARG
|
get_exit_bt_return_code(msh, &exit_code);
|
||||||
&& !ft_strisnbr(cur_cmd->token))
|
|
||||||
numeric_arg_err(cur_cmd->token, &exit_code);
|
|
||||||
else if (cur_cmd && cur_cmd->type == ARG)
|
|
||||||
exit_code = (unsigned char)ft_atoi(cur_cmd->token);
|
|
||||||
else
|
|
||||||
exit_code = g_return_code;
|
|
||||||
free_msh(msh);
|
free_msh(msh);
|
||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
}
|
}
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
96
srcs/export.c
Normal file → Executable file
96
srcs/export.c
Normal file → Executable file
@ -6,26 +6,102 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
|
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/28 12:45:59 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/26 10:52:17 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int ft_export(t_msh *msh)
|
int export_invalid_identifier(char *arg, char *name)
|
||||||
{
|
{
|
||||||
t_cmd *cmd;
|
ft_putstr_fd("minishell: export: `", 2);
|
||||||
|
ft_putstr_fd(arg, 2);
|
||||||
|
ft_putstr_fd("': not a valid identifier\n", 2);
|
||||||
|
free(name);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
cmd = msh->cmds;
|
t_env *set_env(t_env *env, char *name, char *value, int append)
|
||||||
(void)cmd;
|
{
|
||||||
|
t_env *tmp;
|
||||||
|
|
||||||
|
tmp = env;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
if (!ft_strcmp(name, tmp->name))
|
||||||
|
{
|
||||||
|
free(name);
|
||||||
|
if (!*value)
|
||||||
|
{
|
||||||
|
free(value);
|
||||||
|
return (env);
|
||||||
|
}
|
||||||
|
if (append)
|
||||||
|
value = ft_strjoin_free(tmp->value, value, 2);
|
||||||
|
if (!value)
|
||||||
|
return (env);
|
||||||
|
free(tmp->value);
|
||||||
|
tmp->value = value;
|
||||||
|
return (env);
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
return (env_add_back(env, name, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
t_env *export_set_env(t_env *env, char *name, char *value, int append)
|
||||||
|
{
|
||||||
|
if (!value || !name)
|
||||||
|
{
|
||||||
|
free(name);
|
||||||
|
free(value);
|
||||||
|
ft_printf_fd(2, "minishell: malloc failed");
|
||||||
|
return (env);
|
||||||
|
}
|
||||||
|
return (set_env(env, name, value, append));
|
||||||
|
}
|
||||||
|
|
||||||
|
int export_var(t_token *cmd, t_env *env)
|
||||||
|
{
|
||||||
|
char *arg;
|
||||||
|
char *name;
|
||||||
|
char *value;
|
||||||
|
int len;
|
||||||
|
int append;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
arg = cmd->value;
|
||||||
|
while (arg[len] && arg[len] != '=' && arg[len] != '+')
|
||||||
|
len++;
|
||||||
|
name = ft_substr(arg, 0, len);
|
||||||
|
append = arg[len] == '+';
|
||||||
|
if (arg[len] == '=' || (arg[len] == '+' && arg[len + 1] == '='))
|
||||||
|
len += 1 + (arg[len] == '+');
|
||||||
|
if (!name || !check_var_name(name) || arg[len] == '+')
|
||||||
|
return (export_invalid_identifier(arg, name));
|
||||||
|
value = ft_strdup(arg + len);
|
||||||
|
env = export_set_env(env, name, value, append);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int ft_unset(t_msh *msh)
|
int ft_export(t_msh *msh, t_token *cmd, t_env *env)
|
||||||
{
|
{
|
||||||
t_cmd *cmd;
|
int error;
|
||||||
|
|
||||||
cmd = msh->cmds;
|
error = 0;
|
||||||
(void)cmd;
|
if (cmd && !cmd->next)
|
||||||
return (0);
|
{
|
||||||
|
print_env_declare(msh, env);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
cmd = cmd->next;
|
||||||
|
while (cmd->next)
|
||||||
|
{
|
||||||
|
if (export_var(cmd, env))
|
||||||
|
error = 1;
|
||||||
|
cmd = cmd->next;
|
||||||
|
}
|
||||||
|
if (export_var(cmd, env))
|
||||||
|
error = 1;
|
||||||
|
return (error);
|
||||||
}
|
}
|
||||||
|
64
srcs/free.c
Normal file
64
srcs/free.c
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* free.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/24 10:51:13 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/04/25 19:01:49 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void free_fds(t_msh *msh)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
if (msh->fds)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (msh->fds[i])
|
||||||
|
{
|
||||||
|
free(msh->fds[i]);
|
||||||
|
msh->fds[i] = 0;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(msh->fds);
|
||||||
|
msh->fds = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_msh(t_msh *msh)
|
||||||
|
{
|
||||||
|
if (msh)
|
||||||
|
{
|
||||||
|
free_env(msh->env);
|
||||||
|
free(msh->pids);
|
||||||
|
free_cmd(msh->cmds_head);
|
||||||
|
free_fds(msh);
|
||||||
|
free(msh->here_doc_filename);
|
||||||
|
free_token(msh->tokens);
|
||||||
|
free(msh);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_env_cpy(t_env *env)
|
||||||
|
{
|
||||||
|
if (env && env->next)
|
||||||
|
free_env_cpy(env->next);
|
||||||
|
free(env);
|
||||||
|
}
|
||||||
|
|
||||||
|
void free_env(t_env *env)
|
||||||
|
{
|
||||||
|
if (env && env->next)
|
||||||
|
free_env(env->next);
|
||||||
|
if (env)
|
||||||
|
{
|
||||||
|
free(env->name);
|
||||||
|
free(env->value);
|
||||||
|
}
|
||||||
|
free(env);
|
||||||
|
}
|
80
srcs/get_len_bonus.c
Normal file
80
srcs/get_len_bonus.c
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* get_len_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/24 14:51:00 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/24 14:59:47 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int get_normal_cmd_len(char *cmd)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
int in_quote;
|
||||||
|
int in_dquote;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
in_quote = 0;
|
||||||
|
in_dquote = 0;
|
||||||
|
while (cmd[len] && (in_quote || in_dquote || (cmd[len] != '|'
|
||||||
|
&& cmd[len] != '&' && cmd[len] != '(' && cmd[len] != ')'
|
||||||
|
&& cmd[len] != '<' && cmd[len] != '>')))
|
||||||
|
{
|
||||||
|
if (cmd[len] == '\'' && !in_dquote)
|
||||||
|
in_quote = !in_quote;
|
||||||
|
if (cmd[len] == '"' && !in_quote)
|
||||||
|
in_dquote = !in_dquote;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_next_arg_len(char *cmd)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
int in_quote;
|
||||||
|
int in_dquote;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
in_quote = 0;
|
||||||
|
in_dquote = 0;
|
||||||
|
while (cmd[len] && ((!ft_isspace(cmd[len]) && cmd[len] != '&'
|
||||||
|
&& cmd[len] != '|' && cmd[len] != '<' && cmd[len] != '>')
|
||||||
|
|| in_quote || in_dquote))
|
||||||
|
{
|
||||||
|
if (cmd[len] == '\'' && !in_dquote)
|
||||||
|
in_quote = !in_quote;
|
||||||
|
if (cmd[len] == '"' && !in_quote)
|
||||||
|
in_dquote = !in_dquote;
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return (len);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_parenthesis_cmd_len(char *cmd)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
int parenthesis;
|
||||||
|
int in_quote;
|
||||||
|
int in_dquote;
|
||||||
|
|
||||||
|
len = 0;
|
||||||
|
parenthesis = 1;
|
||||||
|
in_quote = 0;
|
||||||
|
in_dquote = 0;
|
||||||
|
while (cmd[len] && parenthesis)
|
||||||
|
{
|
||||||
|
if (cmd[len] == '\'' && !in_dquote)
|
||||||
|
in_quote = !in_quote;
|
||||||
|
if (cmd[len] == '"' && !in_quote)
|
||||||
|
in_dquote = !in_dquote;
|
||||||
|
if ((cmd[len] == '(' || cmd[len] == ')') && !in_quote && !in_dquote)
|
||||||
|
parenthesis += 1 * (-(cmd[len] == ')'));
|
||||||
|
len++;
|
||||||
|
}
|
||||||
|
return (len - 1);
|
||||||
|
}
|
@ -1,35 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* handle_alias.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/02/17 02:54:36 by tomoron #+# #+# */
|
|
||||||
/* Updated: 2024/02/21 17:21:27 by marde-vr ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
t_cmd *handle_alias(t_msh *msh)
|
|
||||||
{
|
|
||||||
t_cmd *res;
|
|
||||||
t_cmd *tmp;
|
|
||||||
char *alias_command;
|
|
||||||
|
|
||||||
alias_command = 0;
|
|
||||||
if (!msh->cmds)
|
|
||||||
return (0);
|
|
||||||
if (msh->cmds->type == ARG)
|
|
||||||
alias_command = get_alias(msh->aliases, msh->cmds->token);
|
|
||||||
if (!alias_command)
|
|
||||||
return (msh->cmds);
|
|
||||||
res = parse_command(alias_command, msh->env);
|
|
||||||
tmp = res;
|
|
||||||
while (tmp->next)
|
|
||||||
tmp = tmp->next;
|
|
||||||
tmp->next = msh->cmds->next;
|
|
||||||
free(msh->cmds);
|
|
||||||
return (res);
|
|
||||||
}
|
|
124
srcs/here_doc.c
Normal file → Executable file
124
srcs/here_doc.c
Normal file → Executable file
@ -5,105 +5,93 @@
|
|||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/26 20:20:31 by marde-vr #+# #+# */
|
/* Created: 2024/03/24 17:44:32 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/05 17:45:51 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/26 11:06:25 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
char *get_tmp_file_name(t_msh *msh)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
char *tmp_file_name;
|
|
||||||
char *res;
|
|
||||||
char *i_char;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
tmp_file_name = ".tmp";
|
|
||||||
i_char = ft_itoa(i);
|
|
||||||
res = ft_strjoin(tmp_file_name, i_char);
|
|
||||||
if (!res)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
free(i_char);
|
|
||||||
while (!access(res, F_OK))
|
|
||||||
{
|
|
||||||
free(res);
|
|
||||||
i_char = ft_itoa(i);
|
|
||||||
res = ft_strjoin(tmp_file_name, i_char);
|
|
||||||
if (!res)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
free(i_char);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (res);
|
|
||||||
}
|
|
||||||
|
|
||||||
int contains_newline(char *str)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (!str)
|
|
||||||
return (0);
|
|
||||||
while (str[i])
|
|
||||||
{
|
|
||||||
if (str[i] == '\n')
|
|
||||||
return (1);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_here_doc_input(t_msh *msh, char *eof)
|
void get_here_doc_input(t_msh *msh, char *eof)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
int new_line;
|
|
||||||
|
|
||||||
line = NULL;
|
line = NULL;
|
||||||
new_line = 1;
|
|
||||||
ft_printf_fd(1, "> ");
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
free(line);
|
free(line);
|
||||||
line = get_next_line(0);
|
line = readline("> ");
|
||||||
if (!line && new_line)
|
if (!line)
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "\nminishell: warning: here-document delimited by");
|
ft_printf_fd(2, "\nminishell: warning: here-document delimited by");
|
||||||
ft_printf_fd(2, " end-of-file, wanted %s", eof);
|
ft_printf_fd(2, " end-of-file, (wanted `%s')\n", eof);
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
if (line && new_line && !ft_strncmp(line, eof, ft_strlen(eof)))
|
if (line && !ft_strncmp(line, eof, ft_strlen(eof)))
|
||||||
break ;
|
break ;
|
||||||
new_line = contains_newline(line);
|
parse_var(msh, line);
|
||||||
if (new_line)
|
write(msh->in_fd, "\n", 1);
|
||||||
ft_printf_fd(1, "> ");
|
|
||||||
write(msh->in_fd, line, ft_strlen(line));
|
|
||||||
}
|
}
|
||||||
free(eof);
|
|
||||||
free(line);
|
free(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void here_doc_child(t_msh *msh, char *eof)
|
||||||
|
{
|
||||||
|
rl_catch_signals = 1;
|
||||||
|
here_doc_variables(1, msh);
|
||||||
|
set_echoctl(0);
|
||||||
|
signal(SIGINT, signal_handler_here_doc);
|
||||||
|
signal(SIGQUIT, SIG_IGN);
|
||||||
|
get_here_doc_input(msh, eof);
|
||||||
|
close(msh->in_fd);
|
||||||
|
ft_exit(msh, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void here_doc_signal(t_msh *msh, int child_pid, char *here_doc_file)
|
||||||
|
{
|
||||||
|
int status;
|
||||||
|
|
||||||
|
signal(SIGINT, signal_handler_command);
|
||||||
|
signal(SIGQUIT, signal_handler_here_doc);
|
||||||
|
waitpid(child_pid, &status, 0);
|
||||||
|
set_echoctl(msh->echoctl);
|
||||||
|
signal(SIGINT, signal_handler_interactive);
|
||||||
|
signal(SIGQUIT, signal_handler_interactive);
|
||||||
|
close(msh->in_fd);
|
||||||
|
if (WIFEXITED(status) && WEXITSTATUS(status))
|
||||||
|
{
|
||||||
|
unlink(here_doc_file);
|
||||||
|
msh->in_fd = -1;
|
||||||
|
g_return_code = 130;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
msh->in_fd = open(here_doc_file, O_RDWR, 0644);
|
||||||
|
if (msh->in_fd == -1 && !(WIFEXITED(status) && WEXITSTATUS(status)))
|
||||||
|
perror("open");
|
||||||
|
}
|
||||||
|
|
||||||
void handle_here_doc(t_msh *msh, char *eof)
|
void handle_here_doc(t_msh *msh, char *eof)
|
||||||
{
|
{
|
||||||
char *here_doc_file;
|
char *here_doc_file;
|
||||||
|
int pid;
|
||||||
|
|
||||||
here_doc_file = get_tmp_file_name(msh);
|
here_doc_file = get_tmp_file_name(msh);
|
||||||
|
if (msh->here_doc_filename)
|
||||||
|
{
|
||||||
|
close(msh->in_fd);
|
||||||
|
unlink(msh->here_doc_filename);
|
||||||
|
free(msh->here_doc_filename);
|
||||||
|
}
|
||||||
|
msh->here_doc_filename = here_doc_file;
|
||||||
msh->in_fd = open(here_doc_file, O_CREAT | O_RDWR, 0644);
|
msh->in_fd = open(here_doc_file, O_CREAT | O_RDWR, 0644);
|
||||||
if (msh->in_fd == -1)
|
if (msh->in_fd == -1)
|
||||||
{
|
{
|
||||||
perror("open");
|
perror("open");
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
}
|
}
|
||||||
eof = ft_strjoin_free(eof, "\n", 1);
|
pid = fork();
|
||||||
if (!eof)
|
if (pid == 0)
|
||||||
ft_exit(msh, 1);
|
here_doc_child(msh, eof);
|
||||||
get_here_doc_input(msh, eof);
|
else
|
||||||
close(msh->in_fd);
|
here_doc_signal(msh, pid, here_doc_file);
|
||||||
msh->in_fd = open(here_doc_file, O_RDWR, 0644);
|
|
||||||
if (msh->in_fd == -1)
|
|
||||||
{
|
|
||||||
perror("open");
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
80
srcs/here_doc_utils.c
Executable file
80
srcs/here_doc_utils.c
Executable file
@ -0,0 +1,80 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* here_doc_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/26 09:06:51 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/04/18 20:48:54 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
char *get_tmp_file_name(t_msh *msh)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *tmp_file_name;
|
||||||
|
char *res;
|
||||||
|
char *i_char;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
tmp_file_name = ".tmp";
|
||||||
|
i_char = ft_itoa(i);
|
||||||
|
res = ft_strjoin_free(tmp_file_name, i_char, 2);
|
||||||
|
if (!res)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
while (!access(res, F_OK))
|
||||||
|
{
|
||||||
|
free(res);
|
||||||
|
i_char = ft_itoa(i);
|
||||||
|
res = ft_strjoin_free(tmp_file_name, i_char, 2);
|
||||||
|
if (!res)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
int contains_newline(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!str)
|
||||||
|
return (0);
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
if (str[i] == '\n')
|
||||||
|
return (1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parse_var(t_msh *msh, char *line)
|
||||||
|
{
|
||||||
|
char *var_name;
|
||||||
|
|
||||||
|
while (*line)
|
||||||
|
{
|
||||||
|
if (*line == '$')
|
||||||
|
{
|
||||||
|
var_name = get_var_name(line + 1);
|
||||||
|
if (!var_name)
|
||||||
|
return ;
|
||||||
|
line += ft_strlen(var_name);
|
||||||
|
if (!*var_name)
|
||||||
|
write(msh->in_fd, "$", 1);
|
||||||
|
else if (!ft_strcmp(var_name, "?"))
|
||||||
|
ft_putnbr_fd(g_return_code, msh->in_fd);
|
||||||
|
else
|
||||||
|
ft_putstr_fd(ft_get_env(msh->env, var_name), msh->in_fd);
|
||||||
|
free(var_name);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
write(msh->in_fd, line, 1);
|
||||||
|
line++;
|
||||||
|
}
|
||||||
|
}
|
117
srcs/input_redirections.c
Normal file → Executable file
117
srcs/input_redirections.c
Normal file → Executable file
@ -6,74 +6,105 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
|
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/05 19:19:33 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/25 18:39:48 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void redirect_input(t_msh *msh, int i)
|
void redirect_input(t_msh *msh, int i, char **cmd_args)
|
||||||
{
|
{
|
||||||
if (msh->in_type != PIPE)
|
if (msh->in_type != PIPE)
|
||||||
{
|
{
|
||||||
if (dup2(msh->in_fd, 0) < 0)
|
if (dup2(msh->in_fd, 0) < 0)
|
||||||
|
{
|
||||||
|
free(cmd_args);
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
close(msh->in_fd);
|
close(msh->in_fd);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (dup2(msh->fds[i - 1][0], 0) < 0)
|
if (dup2(msh->fds[i - 1][0], 0) < 0)
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void open_input_file(t_msh *msh, t_cmd **cur_cmd)
|
|
||||||
{
|
|
||||||
if ((*cur_cmd)->type == HERE_DOC)
|
|
||||||
handle_here_doc(msh, (*cur_cmd)->next->token);
|
|
||||||
if ((*cur_cmd)->type == RED_I)
|
|
||||||
{
|
|
||||||
if (msh->in_fd != 0)
|
|
||||||
close(msh->in_fd);
|
|
||||||
msh->in_fd = open((*cur_cmd)->next->token, O_RDONLY | O_CREAT);
|
|
||||||
if (msh->in_fd == -1 && !g_return_code)
|
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "minishell: %s: ", (*cur_cmd)->next->token);
|
free(cmd_args);
|
||||||
perror("");
|
ft_exit(msh, 1);
|
||||||
g_return_code = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_in_type(t_msh *msh, t_cmd *cmds)
|
void ambiguous_redirect(char *str, t_msh *msh)
|
||||||
{
|
{
|
||||||
t_cmd *cur_cmd;
|
ft_printf_fd(2, "minishell: %s: ambiguous redirect\n", str);
|
||||||
|
msh->in_fd = -1;
|
||||||
cur_cmd = cmds;
|
g_return_code = 1;
|
||||||
while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG)
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (cur_cmd->type)
|
|
||||||
{
|
|
||||||
msh->in_type = cur_cmd->type;
|
|
||||||
if (cur_cmd->type == HERE_DOC || cur_cmd->type == RED_I)
|
|
||||||
open_input_file(msh, &cur_cmd);
|
|
||||||
}
|
|
||||||
while (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG)
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (cur_cmd->next && (cur_cmd->next->type == HERE_DOC
|
|
||||||
|| cur_cmd->next->type == RED_I))
|
|
||||||
get_in_type(msh, cur_cmd);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int first_is_in_type(t_msh *msh)
|
int open_input_file(t_msh *msh, t_cmd **cur_token)
|
||||||
{
|
{
|
||||||
t_cmd *cur_cmd;
|
t_token *filename;
|
||||||
|
|
||||||
cur_cmd = msh->cmds;
|
if ((*cur_token)->cmd_type == HERE_DOC)
|
||||||
while (cur_cmd && cur_cmd->type == ARG && cur_cmd->next)
|
handle_here_doc(msh, (*cur_token)->value);
|
||||||
cur_cmd = cur_cmd->next;
|
if ((*cur_token)->cmd_type == RED_I)
|
||||||
if (cur_cmd->type == PIPE || cur_cmd->type == RED_I
|
{
|
||||||
|| cur_cmd->type == HERE_DOC)
|
if (msh->in_fd != 0)
|
||||||
|
close(msh->in_fd);
|
||||||
|
filename = parse_tokens((*cur_token)->value, msh->env);
|
||||||
|
if (!filename)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
if (filename->next)
|
||||||
|
ambiguous_redirect((*cur_token)->value, msh);
|
||||||
|
if (!filename->next)
|
||||||
|
msh->in_fd = open(filename->value, O_RDONLY);
|
||||||
|
if (msh->in_fd == -1)
|
||||||
|
{
|
||||||
|
ft_printf_fd(2, "minishell: %s: ", filename->value);
|
||||||
|
perror("");
|
||||||
|
free_token(filename);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
free_token(filename);
|
||||||
|
}
|
||||||
|
return (msh->in_fd == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc)
|
||||||
|
{
|
||||||
|
t_cmd *cur_token;
|
||||||
|
|
||||||
|
cur_token = tokens;
|
||||||
|
if (cur_token && cur_token->cmd_type == PIPE)
|
||||||
|
{
|
||||||
|
msh->in_type = PIPE;
|
||||||
|
cur_token = cur_token->next;
|
||||||
|
}
|
||||||
|
while (cur_token && (cur_token->cmd_type == CMD
|
||||||
|
|| cur_token->cmd_type == PAREN))
|
||||||
|
cur_token = cur_token->next;
|
||||||
|
if (cur_token && ((cur_token->cmd_type == HERE_DOC && here_doc)
|
||||||
|
|| (cur_token->cmd_type == RED_I && !here_doc)))
|
||||||
|
{
|
||||||
|
msh->in_type = cur_token->cmd_type;
|
||||||
|
if (open_input_file(msh, &cur_token))
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
if (cur_token && cur_token->next && !is_operand_type(cur_token->next)
|
||||||
|
&& cur_token->cmd_type != PIPE && cur_token->next->cmd_type != PIPE)
|
||||||
|
return (get_in_type(msh, t_strt, cur_token->next, here_doc));
|
||||||
|
if (here_doc)
|
||||||
|
return (get_in_type(msh, t_strt, t_strt, 0));
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int first_is_in_type(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
t_cmd *cur_token;
|
||||||
|
|
||||||
|
cur_token = cmd;
|
||||||
|
while (cur_token && cur_token->cmd_type == CMD && cur_token->next)
|
||||||
|
cur_token = cur_token->next;
|
||||||
|
if (is_input_type(cur_token) || cur_token->cmd_type == PIPE)
|
||||||
return (1);
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 17:31:53 by marde-vr #+# #+# */
|
/* Created: 2024/03/05 17:31:53 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/05 17:35:39 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/23 16:41:42 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
@ -6,22 +6,27 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
|
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 23:12:34 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/18 20:48:55 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
t_cmd *cmd_add_back(t_cmd *cmd, char *token, t_token_type type)
|
t_cmd *cmd_add_back(t_cmd *cmd, char *value, t_cmd_type type)
|
||||||
{
|
{
|
||||||
t_cmd *res;
|
t_cmd *res;
|
||||||
t_cmd *current;
|
t_cmd *current;
|
||||||
|
|
||||||
|
if (value && !*value)
|
||||||
|
{
|
||||||
|
free(value);
|
||||||
|
return (cmd);
|
||||||
|
}
|
||||||
res = ft_calloc(1, sizeof(t_cmd));
|
res = ft_calloc(1, sizeof(t_cmd));
|
||||||
if (!res)
|
if (!res)
|
||||||
return (cmd);
|
return (cmd);
|
||||||
res->token = token;
|
res->value = value;
|
||||||
res->type = type;
|
res->cmd_type = type;
|
||||||
if (!cmd)
|
if (!cmd)
|
||||||
return (res);
|
return (res);
|
||||||
current = cmd;
|
current = cmd;
|
||||||
@ -31,14 +36,22 @@ t_cmd *cmd_add_back(t_cmd *cmd, char *token, t_token_type type)
|
|||||||
return (cmd);
|
return (cmd);
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_cmd(t_cmd *cmd)
|
t_cmd *free_cmd(t_cmd *cmd)
|
||||||
{
|
{
|
||||||
if (cmd)
|
if (cmd)
|
||||||
{
|
{
|
||||||
if (cmd && cmd->token)
|
if (cmd && cmd->value)
|
||||||
free(cmd->token);
|
{
|
||||||
|
free(cmd->value);
|
||||||
|
cmd->value = 0;
|
||||||
|
}
|
||||||
if (cmd && cmd->next)
|
if (cmd && cmd->next)
|
||||||
|
{
|
||||||
free_cmd(cmd->next);
|
free_cmd(cmd->next);
|
||||||
|
cmd->next = 0;
|
||||||
|
}
|
||||||
free(cmd);
|
free(cmd);
|
||||||
|
cmd = 0;
|
||||||
}
|
}
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
|
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/17 04:33:17 by tomoron ### ########.fr */
|
/* Updated: 2024/04/25 18:53:51 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -31,18 +31,6 @@ t_env *env_add_back(t_env *env, char *name, char *value)
|
|||||||
return (env);
|
return (env);
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_env(t_env *env)
|
|
||||||
{
|
|
||||||
if (env && env->next)
|
|
||||||
free_env(env->next);
|
|
||||||
if (env)
|
|
||||||
{
|
|
||||||
free(env->name);
|
|
||||||
free(env->value);
|
|
||||||
}
|
|
||||||
free(env);
|
|
||||||
}
|
|
||||||
|
|
||||||
int print_env(t_env *env)
|
int print_env(t_env *env)
|
||||||
{
|
{
|
||||||
while (env)
|
while (env)
|
||||||
|
@ -1,55 +1,56 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* lst_alias.c :+: :+: :+: */
|
/* lst_token.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
|
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 13:09:00 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/24 18:04:15 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
t_alias *alias_add_back(t_alias *alias, char *name, char *value)
|
t_token *token_add_back(t_token *token, char *value, int is_var)
|
||||||
{
|
{
|
||||||
t_alias *res;
|
t_token *res;
|
||||||
t_alias *current;
|
t_token *current;
|
||||||
|
|
||||||
res = ft_calloc(1, sizeof(t_alias));
|
if (value && !*value && is_var)
|
||||||
|
{
|
||||||
|
free(value);
|
||||||
|
return (token);
|
||||||
|
}
|
||||||
|
res = ft_calloc(1, sizeof(t_token));
|
||||||
if (!res)
|
if (!res)
|
||||||
return (alias);
|
return (token);
|
||||||
res->name = name;
|
|
||||||
res->value = value;
|
res->value = value;
|
||||||
if (!alias)
|
if (!token)
|
||||||
return (res);
|
return (res);
|
||||||
current = alias;
|
current = token;
|
||||||
while (current->next)
|
while (current->next)
|
||||||
current = current->next;
|
current = current->next;
|
||||||
current->next = res;
|
current->next = res;
|
||||||
return (alias);
|
return (token);
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_alias(t_alias *alias)
|
t_token *free_token(t_token *token)
|
||||||
{
|
{
|
||||||
if (alias && alias->next)
|
if (token)
|
||||||
free_alias(alias->next);
|
|
||||||
if (alias)
|
|
||||||
{
|
{
|
||||||
free(alias->name);
|
if (token && token->value)
|
||||||
free(alias->value);
|
{
|
||||||
}
|
free(token->value);
|
||||||
free(alias);
|
token->value = 0;
|
||||||
}
|
}
|
||||||
|
if (token && token->next)
|
||||||
char *get_alias(t_alias *alias, char *name)
|
{
|
||||||
{
|
free_token(token->next);
|
||||||
while (alias)
|
token->next = 0;
|
||||||
{
|
}
|
||||||
if (!ft_strcmp(alias->name, name))
|
free(token);
|
||||||
return (alias->value);
|
token = 0;
|
||||||
alias = alias->next;
|
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
58
srcs/main.c
58
srcs/main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/06 08:32:18 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/26 11:08:22 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -22,9 +22,12 @@ char *get_prompt(t_env *env)
|
|||||||
|
|
||||||
res = ft_strjoin_free("\001", ft_get_color(10, 255, 80), 2);
|
res = ft_strjoin_free("\001", ft_get_color(10, 255, 80), 2);
|
||||||
res = ft_strjoin_free(res, "\033[1m\002", 1);
|
res = ft_strjoin_free(res, "\033[1m\002", 1);
|
||||||
res = ft_strjoin_free(res, getenv("USER"), 1);
|
if (getenv("USER"))
|
||||||
res = ft_strjoin_free(res, "@", 1);
|
{
|
||||||
res = ft_strjoin_free(res, "minishell\001\033[0m\002:\001", 1);
|
res = ft_strjoin_free(res, getenv("USER"), 1);
|
||||||
|
res = ft_strjoin_free(res, "@", 1);
|
||||||
|
}
|
||||||
|
res = ft_strjoin_free(res, "hell\001\033[0m\002:\001", 1);
|
||||||
res = ft_strjoin_free(res, ft_get_color(80, 80, 255), 3);
|
res = ft_strjoin_free(res, ft_get_color(80, 80, 255), 3);
|
||||||
res = ft_strjoin_free(res, "\033[1m\002", 1);
|
res = ft_strjoin_free(res, "\033[1m\002", 1);
|
||||||
cwd = getcwd(cwd_buffer, 99);
|
cwd = getcwd(cwd_buffer, 99);
|
||||||
@ -68,15 +71,43 @@ t_env *get_env(char **envp)
|
|||||||
return (env);
|
return (env);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t_env *add_shlvl(t_env *env)
|
||||||
|
{
|
||||||
|
int nb;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
tmp = ft_get_env(env, "SHLVL");
|
||||||
|
if (!tmp)
|
||||||
|
nb = 0;
|
||||||
|
else
|
||||||
|
nb = ft_atoi(tmp);
|
||||||
|
nb++;
|
||||||
|
if (nb < 0)
|
||||||
|
nb = 0;
|
||||||
|
env = export_set_env(env, ft_strdup("SHLVL"), ft_itoa(nb), 0);
|
||||||
|
return (env);
|
||||||
|
}
|
||||||
|
|
||||||
int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
|
int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
|
struct termios t_p;
|
||||||
|
|
||||||
*msh = ft_calloc(1, sizeof(t_msh));
|
*msh = ft_calloc(1, sizeof(t_msh));
|
||||||
if (!*msh)
|
if (!*msh)
|
||||||
ft_exit(*msh, 1);
|
exit(1);
|
||||||
(void)argc;
|
(void)argc;
|
||||||
(void)argv;
|
(void)argv;
|
||||||
|
if (isatty(1))
|
||||||
|
{
|
||||||
|
tcgetattr(1, &t_p);
|
||||||
|
(*msh)->echoctl = t_p.c_lflag & ECHOCTL;
|
||||||
|
}
|
||||||
(*msh)->env = get_env(envp);
|
(*msh)->env = get_env(envp);
|
||||||
(*msh)->aliases = 0;
|
(*msh)->env = add_shlvl((*msh)->env);
|
||||||
|
rl_catch_signals = 0;
|
||||||
|
g_return_code = 0;
|
||||||
|
signal(SIGINT, signal_handler_interactive);
|
||||||
|
signal(SIGQUIT, signal_handler_interactive);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,22 +119,17 @@ int main(int argc, char **argv, char **envp)
|
|||||||
|
|
||||||
commands = (char *)1;
|
commands = (char *)1;
|
||||||
init_minishell(&msh, argc, argv, envp);
|
init_minishell(&msh, argc, argv, envp);
|
||||||
//handle_minishellrc(msh);
|
while (commands)
|
||||||
while (msh->env && commands)
|
|
||||||
{
|
{
|
||||||
prompt = get_prompt(msh->env);
|
prompt = get_prompt(msh->env);
|
||||||
if (!prompt)
|
if (!prompt)
|
||||||
exit(STDIN_FILENO);
|
ft_exit(msh, 1);
|
||||||
commands = readline(prompt);
|
commands = readline(prompt);
|
||||||
free(prompt);
|
free(prompt);
|
||||||
add_history(commands);
|
add_history(commands);
|
||||||
msh->cmds = parse_command(commands, msh->env);
|
exec_command_bonus(msh, commands);
|
||||||
free(commands);
|
free(commands);
|
||||||
msh->cmds = handle_alias(msh);
|
|
||||||
exec_commands(msh);
|
|
||||||
free_cmd(msh->cmds);
|
|
||||||
}
|
}
|
||||||
rl_clear_history();
|
printf("exit\n");
|
||||||
free_msh(msh);
|
ft_exit(msh, g_return_code);
|
||||||
return (g_return_code);
|
|
||||||
}
|
}
|
||||||
|
199
srcs/minishell.h
199
srcs/minishell.h
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/06 10:19:27 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/25 18:54:09 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,29 +16,43 @@
|
|||||||
# include <readline/readline.h>
|
# include <readline/readline.h>
|
||||||
# include <readline/history.h>
|
# include <readline/history.h>
|
||||||
# include <limits.h>
|
# include <limits.h>
|
||||||
# include <stdio.h>//debug
|
# include <stdio.h>
|
||||||
# include <sys/wait.h>
|
# include <sys/wait.h>
|
||||||
# include "../libft/libft.h"
|
# include "../libft/libft.h"
|
||||||
# include "fcntl.h"
|
# include "fcntl.h"
|
||||||
# include <sys/stat.h>
|
# include <sys/stat.h>
|
||||||
|
# include <signal.h>
|
||||||
|
# include <stdint.h>
|
||||||
|
# include <termios.h>
|
||||||
|
# include <dirent.h>
|
||||||
|
|
||||||
typedef enum e_token_type
|
typedef enum e_cmd_type
|
||||||
{
|
{
|
||||||
ARG,
|
CMD,
|
||||||
|
PAREN,
|
||||||
|
AND,
|
||||||
|
OR,
|
||||||
PIPE,
|
PIPE,
|
||||||
RED_O,
|
|
||||||
RED_O_APP,
|
RED_O_APP,
|
||||||
RED_I,
|
|
||||||
HERE_DOC,
|
HERE_DOC,
|
||||||
} t_token_type;
|
RED_O,
|
||||||
|
RED_I,
|
||||||
|
ERR
|
||||||
|
} t_cmd_type;
|
||||||
|
|
||||||
typedef struct s_cmd
|
typedef struct s_cmd
|
||||||
{
|
{
|
||||||
t_token_type type;
|
t_cmd_type cmd_type;
|
||||||
char *token;
|
char *value;
|
||||||
struct s_cmd *next;
|
struct s_cmd *next;
|
||||||
} t_cmd;
|
} t_cmd;
|
||||||
|
|
||||||
|
typedef struct s_token
|
||||||
|
{
|
||||||
|
char *value;
|
||||||
|
struct s_token *next;
|
||||||
|
} t_token;
|
||||||
|
|
||||||
typedef struct s_env
|
typedef struct s_env
|
||||||
{
|
{
|
||||||
char *name;
|
char *name;
|
||||||
@ -46,76 +60,119 @@ typedef struct s_env
|
|||||||
struct s_env *next;
|
struct s_env *next;
|
||||||
} t_env;
|
} t_env;
|
||||||
|
|
||||||
typedef struct s_alias
|
|
||||||
{
|
|
||||||
char *name;
|
|
||||||
char *value;
|
|
||||||
struct s_alias *next;
|
|
||||||
} t_alias;
|
|
||||||
|
|
||||||
typedef struct s_msh
|
typedef struct s_msh
|
||||||
{
|
{
|
||||||
struct s_alias *aliases;
|
t_env *env;
|
||||||
struct s_env *env;
|
t_token *tokens;
|
||||||
struct s_cmd *cmds;
|
t_cmd *cmds;
|
||||||
int **fds;
|
t_cmd *cmds_head;
|
||||||
int *pids;
|
int **fds;
|
||||||
enum e_token_type in_type;
|
int *pids;
|
||||||
enum e_token_type out_type;
|
t_cmd_type in_type;
|
||||||
int in_fd;
|
t_cmd_type out_type;
|
||||||
int out_fd;
|
int in_fd;
|
||||||
|
int echoctl;
|
||||||
|
int out_fd;
|
||||||
|
int locked_return_code;
|
||||||
|
char *here_doc_filename;
|
||||||
} t_msh;
|
} t_msh;
|
||||||
|
|
||||||
extern int g_return_code;
|
extern int g_return_code;
|
||||||
|
|
||||||
t_cmd *cmd_add_back(t_cmd *res, char *token, t_token_type type);
|
t_cmd *cmd_add_back(t_cmd *res, char *cmd, t_cmd_type type);
|
||||||
void free_cmd(t_cmd *cmd);
|
t_env *export_set_env(t_env *env, char *name, char *value, int append);
|
||||||
void exec_commands(t_msh *msh);
|
void *here_doc_variables(int write, void *data);
|
||||||
int echo(t_cmd *args);
|
int set_echoctl(int value);
|
||||||
void exit_bt(t_msh *msh);
|
int add_var_to_str(char *res, char **command, t_env *env, int *is_var);
|
||||||
t_env *env_add_back(t_env *env, char *name, char *value);
|
|
||||||
void free_env(t_env *env);
|
|
||||||
int print_env(t_env *env);
|
|
||||||
t_cmd *parse_command(char *command, t_env *env);
|
|
||||||
int get_token_len(char *cmd, t_env *env);
|
|
||||||
int add_var_to_str(char *res, char **command, t_env *env);
|
|
||||||
int get_var_name_len(char *command);
|
|
||||||
char *ft_get_env(t_env *env, char *var_name);
|
|
||||||
int pwd(void);
|
|
||||||
int is_cmd_char(char c);
|
|
||||||
void print_parsed_cmd(t_cmd *cmd);//debug
|
|
||||||
void ft_exit(t_msh *msh, int error_code);
|
|
||||||
char **env_to_char_tab(t_env *env);
|
|
||||||
void handle_minishellrc(t_msh *msh);
|
|
||||||
t_cmd *handle_alias(t_msh *msh);
|
|
||||||
int cd(t_cmd *args);
|
|
||||||
int alias(t_msh *msh);
|
|
||||||
void free_alias(t_alias *alias);
|
|
||||||
char *get_alias(t_alias *alias, char *var_name);
|
|
||||||
t_alias *alias_add_back(t_alias *alias, char *name, char *value);
|
|
||||||
int unalias(t_msh *msh);
|
|
||||||
int ft_export(t_msh *msh);
|
|
||||||
void free_msh(t_msh *msh);
|
|
||||||
char **split_paths_from_env(t_env *env);
|
|
||||||
void find_cmd_path(t_msh *msh, char **paths, int *found);
|
void find_cmd_path(t_msh *msh, char **paths, int *found);
|
||||||
void get_cmd_path(t_msh *msh);
|
t_env *env_add_back(t_env *env, char *name, char *value);
|
||||||
void handle_here_doc(t_msh *msh, char *eof);
|
t_token *parse_cmds_to_token(t_cmd *command, t_env *env);
|
||||||
int ft_unset(t_msh *msh);
|
void exec_command_bonus(t_msh *msh, char *cmd_str);
|
||||||
void get_in_type(t_msh *msh, t_cmd *cmds);
|
t_token *add_token_back(t_token *res, t_token *next);
|
||||||
void get_out_type(t_msh *msh, t_cmd *cmds);
|
t_token *expand_wildcards(t_token *res, char *value, int is_var);
|
||||||
int first_is_in_type(t_msh *msh);
|
|
||||||
void redirect_input(t_msh *msh, int i);
|
|
||||||
void redirect_output(t_msh *msh, int i);
|
|
||||||
void child(t_msh *msh, char **cmd_args, int i);
|
|
||||||
void parent(t_msh *msh, int i, int cmd_count);
|
|
||||||
void free_msh(t_msh *msh);
|
|
||||||
void ft_exit(t_msh *msh, int exit_code);
|
|
||||||
int cmd_is_builtin(t_msh *msh, char *cmd_token);
|
int cmd_is_builtin(t_msh *msh, char *cmd_token);
|
||||||
int exec_builtin(t_msh *msh);
|
t_token *token_add_back(t_token *res, char *token, int is_var);
|
||||||
int get_cmd_count(t_cmd *cmds);
|
void child(t_msh *msh, char **cmd_args, int i);
|
||||||
int get_args_count(t_cmd *cmds);
|
t_token *parse_tokens(char *command, t_env *env);
|
||||||
char **get_cmd_args(t_msh *msh);
|
void parent(t_msh *msh, int i, int cmd_count, char **cmd_args);
|
||||||
void remove_command_from_msh(t_msh *msh);
|
char *ft_get_env(t_env *env, char *var_name);
|
||||||
|
int is_fd_open(int fd);
|
||||||
|
int get_out_type(t_msh *msh, t_cmd *cmds);
|
||||||
|
void exec_commands(t_msh *msh);
|
||||||
|
void handle_here_doc(t_msh *msh, char *eof);
|
||||||
|
int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc);
|
||||||
|
void signal_handler_interactive(int signum);
|
||||||
|
int get_token_len(char *cmd, t_env *env);
|
||||||
|
void print_syntax_error_bonus(t_cmd *cmd);
|
||||||
|
void signal_handler_here_doc(int signum);
|
||||||
|
t_token *parsing_syntax_error(t_token *res);
|
||||||
int file_access(t_msh *msh, int *found);
|
int file_access(t_msh *msh, int *found);
|
||||||
|
void free_env_cpy(t_env *env);
|
||||||
|
void remove_command_from_msh(t_msh *msh);
|
||||||
|
void get_redirections(t_msh *msh, t_cmd *cmds);
|
||||||
|
t_cmd *get_next_command(t_cmd *cmd);
|
||||||
|
int get_cmd_count(t_cmd *cmds);
|
||||||
|
int is_parenthesis(t_cmd *cmd);
|
||||||
|
void print_signaled(int status);
|
||||||
|
void ft_exit(t_msh *msh, int error_code);
|
||||||
|
void sort_wildcards_token(t_token *list);
|
||||||
|
void redirect_input(t_msh *msh, int i, char **cmd_args);
|
||||||
|
void redirect_output(t_msh *msh, int i, char **cmd_args);
|
||||||
|
void print_cmd_type(t_cmd_type type, char *value);
|
||||||
|
int cmd_is_forkable_builtin(char *cmd_token);
|
||||||
|
void signal_handler_command(int signum);
|
||||||
|
void ft_exit(t_msh *msh, int exit_code);
|
||||||
|
char **split_paths_from_env(t_env *env);
|
||||||
|
int add_return_code_to_str(char *res);
|
||||||
|
void parse_var(t_msh *msh, char *line);
|
||||||
|
void print_parsed_token(t_token *cmd);//debug
|
||||||
|
int get_var_name_len(char *command);
|
||||||
|
void handle_minishellrc(t_msh *msh);
|
||||||
|
t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env);
|
||||||
|
char *get_tmp_file_name(t_msh *msh);
|
||||||
|
int get_args_count(t_token *cmds);
|
||||||
|
char **env_to_char_tab(t_env *env);
|
||||||
|
void print_parsed_cmd(t_cmd *cmd);//debug
|
||||||
|
int first_is_in_type(t_cmd *cmd);
|
||||||
|
void print_msh_struct(t_msh *msh);
|
||||||
|
int contains_newline(char *str);
|
||||||
|
int get_cmd_count(t_cmd *cmds);
|
||||||
|
int check_var_name(char *name);
|
||||||
|
char **get_cmd_args(t_msh *msh);
|
||||||
|
char *remove_path(char *token);
|
||||||
|
t_env *dup_env(t_env *env);
|
||||||
|
void sort_env(t_env *env);
|
||||||
|
void print_env_declare(t_msh *msh, t_env *env_orig);
|
||||||
|
void delete_from_env(t_msh *msh, char *name);
|
||||||
|
t_cmd *parsing_bonus(char *cmd);
|
||||||
|
t_token *free_token(t_token *cmd);
|
||||||
|
void exec_commands(t_msh *msh);
|
||||||
|
char *get_var_name(char *str);
|
||||||
|
int exec_builtin(t_msh *msh);
|
||||||
|
void get_cmd_path(t_msh *msh);
|
||||||
|
int is_operand_type(t_cmd *cmd);
|
||||||
|
int is_output_type(t_cmd *cmd);
|
||||||
|
int print_env(t_env *env);
|
||||||
|
t_cmd *free_cmd(t_cmd *cmd);
|
||||||
|
int ft_export(t_msh *msh, t_token *cmd, t_env *env);
|
||||||
|
int get_parenthesis_cmd_len(char *cmd);
|
||||||
|
int get_normal_cmd_len(char *cmd);
|
||||||
|
int get_next_arg_len(char *cmd);
|
||||||
|
int check_str_syntax(char *cmd);
|
||||||
|
int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env);
|
||||||
|
int is_parenthesis(t_cmd *cmd);
|
||||||
|
int is_input_type(t_cmd *cmd);
|
||||||
|
void free_env(t_env *env);
|
||||||
|
int ft_unset(t_msh *msh);
|
||||||
|
void free_fds(t_msh *msh);
|
||||||
|
void free_msh(t_msh *msh);
|
||||||
|
void free_msh(t_msh *msh);
|
||||||
|
int echo(t_token *args);
|
||||||
|
int exit_bt(t_msh *msh);
|
||||||
|
int is_cmd_type(t_cmd *cmd);
|
||||||
|
int is_cmd_char(char c);
|
||||||
|
void ambiguous_redirect(char *str, t_msh *msh);
|
||||||
|
int cd(t_token *args, t_msh *msh);
|
||||||
|
int pwd(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,55 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* minishellrc.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */
|
|
||||||
/* Updated: 2024/03/05 18:33:30 by marde-vr ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
void exec_rc_file(t_msh *msh, int fd)
|
|
||||||
{
|
|
||||||
char *line;
|
|
||||||
|
|
||||||
line = get_next_line(fd);
|
|
||||||
while (line)
|
|
||||||
{
|
|
||||||
if (line[0] != '#')
|
|
||||||
{
|
|
||||||
msh->cmds = parse_command(line, msh->env);
|
|
||||||
exec_commands(msh);
|
|
||||||
free_cmd(msh->cmds);
|
|
||||||
}
|
|
||||||
free(line);
|
|
||||||
line = get_next_line(fd);
|
|
||||||
}
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
void handle_minishellrc(t_msh *msh)
|
|
||||||
{
|
|
||||||
char *home;
|
|
||||||
char *rc_path;
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
home = ft_get_env(msh->env, "HOME");
|
|
||||||
rc_path = ft_strjoin(home, "/.minishellrc");
|
|
||||||
if (access(rc_path, R_OK) != -1)
|
|
||||||
{
|
|
||||||
fd = open(rc_path, O_RDONLY);
|
|
||||||
if (fd == -1)
|
|
||||||
{
|
|
||||||
free(msh->env);
|
|
||||||
perror("open");
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
exec_rc_file(msh, fd);
|
|
||||||
close(fd);
|
|
||||||
}
|
|
||||||
free(rc_path);
|
|
||||||
}
|
|
94
srcs/output_redirections.c
Normal file → Executable file
94
srcs/output_redirections.c
Normal file → Executable file
@ -3,71 +3,93 @@
|
|||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* output_redirections.c :+: :+: :+: */
|
/* output_redirections.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */
|
/* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/05 19:28:25 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/25 18:39:52 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void redirect_output(t_msh *msh, int i)
|
void redirect_output(t_msh *msh, int i, char **cmd_args)
|
||||||
{
|
{
|
||||||
if (msh->out_type != PIPE)
|
if (msh->out_type != PIPE)
|
||||||
{
|
{
|
||||||
if (dup2(msh->out_fd, 1) < 0)
|
if (dup2(msh->out_fd, 1) < 0)
|
||||||
|
{
|
||||||
|
free(cmd_args);
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (dup2(msh->fds[i][1], 1) < 0)
|
if (dup2(msh->fds[i][1], 1) < 0)
|
||||||
|
{
|
||||||
|
perror("dup2");
|
||||||
|
free(cmd_args);
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void open_out_file(t_msh *msh, t_cmd **cur_cmd)
|
int open_out_file(t_msh *msh, t_cmd **cur_cmd, char *filename)
|
||||||
{
|
{
|
||||||
msh->out_type = (*cur_cmd)->type;
|
|
||||||
if (msh->out_type == RED_O)
|
if (msh->out_type == RED_O)
|
||||||
msh->out_fd = open((*cur_cmd)->next->token,
|
msh->out_fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
||||||
O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
|
||||||
if (msh->out_type == RED_O_APP)
|
if (msh->out_type == RED_O_APP)
|
||||||
msh->out_fd = open((*cur_cmd)->next->token,
|
msh->out_fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||||
O_CREAT | O_RDWR | O_APPEND, 0644);
|
|
||||||
if (msh->out_fd == -1)
|
if (msh->out_fd == -1)
|
||||||
{
|
{
|
||||||
g_return_code = 1;
|
ft_putstr_fd("minishell: ", 2);
|
||||||
perror("open");
|
perror(filename);
|
||||||
return ;
|
return (1);
|
||||||
}
|
}
|
||||||
if ((*cur_cmd)->type != PIPE)
|
if ((*cur_cmd)->cmd_type != PIPE)
|
||||||
{
|
{
|
||||||
while ((*cur_cmd)->next && (*cur_cmd)->next->type == ARG)
|
while ((*cur_cmd)->next && is_cmd_type((*cur_cmd)->next))
|
||||||
*cur_cmd = (*cur_cmd)->next;
|
*cur_cmd = (*cur_cmd)->next;
|
||||||
if ((*cur_cmd)->next && ((*cur_cmd)->next->type == RED_O
|
if ((*cur_cmd)->next && is_output_type((*cur_cmd)->next))
|
||||||
|| (*cur_cmd)->next->type == RED_O_APP))
|
get_out_type(msh, (*cur_cmd)->next);
|
||||||
get_out_type(msh, *cur_cmd);
|
|
||||||
}
|
}
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_out_type(t_msh *msh, t_cmd *cmds)
|
void go_to_next_out_type(t_cmd **cur_cmd)
|
||||||
{
|
{
|
||||||
t_cmd *cur_cmd;
|
while (*cur_cmd && (*cur_cmd)->next && (!is_cmd_type(*cur_cmd)
|
||||||
|
&& !is_output_type(*cur_cmd)))
|
||||||
msh->out_type = ARG;
|
*cur_cmd = (*cur_cmd)->next;
|
||||||
msh->out_fd = 0;
|
while (*cur_cmd && (*cur_cmd)->next && !is_output_type(*cur_cmd)
|
||||||
cur_cmd = cmds;
|
&& !is_operand_type(*cur_cmd) && (*cur_cmd)->cmd_type != PIPE)
|
||||||
if (cmds->type && msh->cmds == cmds)
|
*cur_cmd = (*cur_cmd)->next;
|
||||||
{
|
}
|
||||||
while (msh->cmds->type != ARG && msh->cmds->next->next)
|
|
||||||
msh->cmds = msh->cmds->next->next;
|
int get_out_type(t_msh *msh, t_cmd *cur_cmd)
|
||||||
}
|
{
|
||||||
while (cur_cmd && cur_cmd->next && (cur_cmd->type == ARG
|
t_token *filename;
|
||||||
|| cur_cmd->type > 3))
|
int ret;
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (!cur_cmd->type)
|
msh->out_type = CMD;
|
||||||
msh->out_type = ARG;
|
msh->out_fd = 0;
|
||||||
else
|
ret = 0;
|
||||||
open_out_file(msh, &cur_cmd);
|
go_to_next_out_type(&cur_cmd);
|
||||||
|
if (cur_cmd && (cur_cmd->cmd_type == CMD || cur_cmd->cmd_type == PAREN))
|
||||||
|
msh->out_type = 0;
|
||||||
|
else if (cur_cmd && is_output_type(cur_cmd) && !is_operand_type(cur_cmd)
|
||||||
|
&& cur_cmd->cmd_type != PIPE)
|
||||||
|
{
|
||||||
|
msh->out_type = cur_cmd->cmd_type;
|
||||||
|
filename = parse_tokens(cur_cmd->value, msh->env);
|
||||||
|
if (!filename)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
if (filename->next)
|
||||||
|
ambiguous_redirect(cur_cmd->value, msh);
|
||||||
|
if (!filename->next)
|
||||||
|
ret = open_out_file(msh, &cur_cmd, filename->value);
|
||||||
|
free_token(filename);
|
||||||
|
}
|
||||||
|
else if (cur_cmd && cur_cmd->cmd_type == PIPE)
|
||||||
|
msh->out_type = PIPE;
|
||||||
|
return (ret || msh->in_fd == -1);
|
||||||
}
|
}
|
||||||
|
100
srcs/parsing.c
100
srcs/parsing.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
|
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/28 18:48:27 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/24 20:39:35 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
@ -31,7 +31,7 @@ int add_home_to_str(char *res)
|
|||||||
return (i);
|
return (i);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
|
char *get_token(char **cmd, int *quotes[2], t_env *env, int *is_var)
|
||||||
{
|
{
|
||||||
char *res;
|
char *res;
|
||||||
int i;
|
int i;
|
||||||
@ -40,20 +40,18 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
|
|||||||
while (ft_isspace(**cmd))
|
while (ft_isspace(**cmd))
|
||||||
(*cmd)++;
|
(*cmd)++;
|
||||||
res = ft_calloc(get_token_len(*cmd, env) + 1, 1);
|
res = ft_calloc(get_token_len(*cmd, env) + 1, 1);
|
||||||
while (res && **cmd && (is_cmd_char(**cmd) || *in_quote || *in_dquote))
|
while (res && **cmd && (is_cmd_char(**cmd) || *(quotes[0]) || *(quotes[1])))
|
||||||
{
|
{
|
||||||
if (**cmd == '"' && !*in_quote)
|
if (**cmd == '"' && !*(quotes[0]))
|
||||||
*in_dquote = !*in_dquote;
|
*(quotes[1]) = !*(quotes[1]);
|
||||||
if (**cmd == '\'' && !*in_dquote)
|
if (**cmd == '\'' && !*(quotes[1]))
|
||||||
*in_quote = !*in_quote;
|
*(quotes[0]) = !*(quotes[0]);
|
||||||
if (**cmd == '$' && !*in_quote)
|
if (**cmd == '$' && !*(quotes[0]))
|
||||||
{
|
i += add_var_to_str(res + i, cmd, env, is_var);
|
||||||
(*cmd)++;
|
else if (**cmd == '~' && !*(quotes[0]) && !*(quotes[1]))
|
||||||
i += add_var_to_str(res + i, cmd, env);
|
|
||||||
}
|
|
||||||
else if (**cmd == '~' && !*in_quote && !*in_dquote)
|
|
||||||
i += add_home_to_str(res + i);
|
i += add_home_to_str(res + i);
|
||||||
else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote))
|
else if (((**cmd == '\'' && *(quotes[1]))
|
||||||
|
|| (**cmd == '"' && *(quotes[0])))
|
||||||
|| (**cmd != '\'' && **cmd != '"'))
|
|| (**cmd != '\'' && **cmd != '"'))
|
||||||
res[i++] = **cmd;
|
res[i++] = **cmd;
|
||||||
(*cmd)++;
|
(*cmd)++;
|
||||||
@ -61,58 +59,48 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
|
|||||||
return (res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_token_type get_token_type(char **command)
|
t_token *parse_tokens(char *command, t_env *env)
|
||||||
{
|
{
|
||||||
t_token_type res;
|
int in_quote;
|
||||||
|
int in_dquote;
|
||||||
|
t_token *res;
|
||||||
|
char *value;
|
||||||
|
int is_var;
|
||||||
|
|
||||||
while (ft_isspace(**command))
|
in_quote = 0;
|
||||||
(*command)++;
|
in_dquote = 0;
|
||||||
if ((*command)[0] == '>' && (*command)[1] == '>')
|
|
||||||
res = RED_O_APP;
|
|
||||||
else if ((*command)[0] == '<' && (*command)[1] == '<')
|
|
||||||
res = HERE_DOC;
|
|
||||||
else if ((*command)[0] == '>')
|
|
||||||
res = RED_O;
|
|
||||||
else if ((*command)[0] == '<')
|
|
||||||
res = RED_I;
|
|
||||||
else if ((*command)[0] == '|')
|
|
||||||
res = PIPE;
|
|
||||||
else
|
|
||||||
res = ARG;
|
|
||||||
if (res == RED_O_APP || res == HERE_DOC)
|
|
||||||
(*command) += 2;
|
|
||||||
if (res == RED_O || res == RED_I || res == PIPE)
|
|
||||||
(*command)++;
|
|
||||||
return (res);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_cmd *parse_command(char *command, t_env *env)
|
|
||||||
{
|
|
||||||
int in_quote;
|
|
||||||
int in_dquote;
|
|
||||||
t_cmd *res;
|
|
||||||
char *token;
|
|
||||||
t_token_type type;
|
|
||||||
|
|
||||||
in_quote = EXIT_SUCCESS;
|
|
||||||
in_dquote = STDIN_FILENO;
|
|
||||||
res = 0;
|
res = 0;
|
||||||
|
is_var = 0;
|
||||||
while (command && *command)
|
while (command && *command)
|
||||||
{
|
{
|
||||||
type = get_token_type(&command);
|
value = get_token(&command, (int *[2]){&in_quote, &in_dquote},
|
||||||
if (type == ARG)
|
env, &is_var);
|
||||||
token = get_token(&command, &in_quote, &in_dquote, env);
|
if (!value)
|
||||||
else
|
return (free_token(res));
|
||||||
token = 0;
|
res = expand_wildcards(res, value, is_var);
|
||||||
res = cmd_add_back(res, token, type);
|
|
||||||
while (ft_isspace(*command))
|
while (ft_isspace(*command))
|
||||||
command++;
|
command++;
|
||||||
}
|
}
|
||||||
if (command && (in_quote || in_dquote))
|
if (command && (in_quote || in_dquote))
|
||||||
|
return (parsing_syntax_error(res));
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_token *parse_cmds_to_token(t_cmd *command, t_env *env)
|
||||||
|
{
|
||||||
|
t_token *res;
|
||||||
|
t_token *new;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
while (command && (command->cmd_type == CMD || is_output_type(command)
|
||||||
|
|| is_input_type(command)))
|
||||||
{
|
{
|
||||||
free_cmd(res);
|
if (command->cmd_type == CMD)
|
||||||
ft_putstr_fd("minishell: syntax error\n", 2);
|
{
|
||||||
return (0);
|
new = parse_tokens(command->value, env);
|
||||||
|
res = add_token_back(res, new);
|
||||||
|
}
|
||||||
|
command = command->next;
|
||||||
}
|
}
|
||||||
return (res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
105
srcs/parsing_bonus.c
Executable file
105
srcs/parsing_bonus.c
Executable file
@ -0,0 +1,105 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* parsing_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/24 14:53:19 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_cmd_type str_to_cmd_type(char *cmd)
|
||||||
|
{
|
||||||
|
if (*cmd == '|' && cmd[1] != '|')
|
||||||
|
return (PIPE);
|
||||||
|
else if (*cmd == '|' && cmd[1] == '|')
|
||||||
|
return (OR);
|
||||||
|
else if (*cmd == '&' && cmd[1] == '&')
|
||||||
|
return (AND);
|
||||||
|
else if (*cmd == '>' && cmd[1] == '>')
|
||||||
|
return (RED_O_APP);
|
||||||
|
else if (*cmd == '<' && cmd[1] == '<')
|
||||||
|
return (HERE_DOC);
|
||||||
|
else if (*cmd == '>')
|
||||||
|
return (RED_O);
|
||||||
|
else if (*cmd == '<')
|
||||||
|
return (RED_I);
|
||||||
|
else if (*cmd == '(')
|
||||||
|
return (PAREN);
|
||||||
|
else if (*cmd == '&')
|
||||||
|
return (ERR);
|
||||||
|
return (CMD);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_cmd_type get_cmd_type_bonus(char **cmd)
|
||||||
|
{
|
||||||
|
t_cmd_type res;
|
||||||
|
|
||||||
|
res = str_to_cmd_type(*cmd);
|
||||||
|
if (res != CMD)
|
||||||
|
(*cmd)++;
|
||||||
|
if (res == OR || res == AND || res == RED_O_APP || res == HERE_DOC)
|
||||||
|
(*cmd)++;
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_cmd_value(char **cmd, t_cmd_type type)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
if (type == PAREN)
|
||||||
|
len = get_parenthesis_cmd_len(*cmd);
|
||||||
|
else
|
||||||
|
len = get_normal_cmd_len(*cmd);
|
||||||
|
res = ft_substr(*cmd, 0, len);
|
||||||
|
(*cmd) += len;
|
||||||
|
if (type == PAREN)
|
||||||
|
(*cmd)++;
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
char *get_next_arg(char **cmd)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
char *res;
|
||||||
|
|
||||||
|
while (ft_isspace(**cmd))
|
||||||
|
(*cmd)++;
|
||||||
|
len = get_next_arg_len(*cmd);
|
||||||
|
if (!len)
|
||||||
|
return (0);
|
||||||
|
res = ft_substr(*cmd, 0, len);
|
||||||
|
*cmd += len;
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_cmd *parsing_bonus(char *cmd)
|
||||||
|
{
|
||||||
|
t_cmd *res;
|
||||||
|
t_cmd_type type;
|
||||||
|
char *value;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
if (!check_str_syntax(cmd))
|
||||||
|
return (0);
|
||||||
|
while (*cmd)
|
||||||
|
{
|
||||||
|
while (ft_isspace(*cmd))
|
||||||
|
cmd++;
|
||||||
|
type = get_cmd_type_bonus(&cmd);
|
||||||
|
if (type == CMD || type == PAREN)
|
||||||
|
value = get_cmd_value(&cmd, type);
|
||||||
|
else if (type == RED_O || type == RED_O_APP || type == RED_I
|
||||||
|
|| type == HERE_DOC)
|
||||||
|
value = get_next_arg(&cmd);
|
||||||
|
else
|
||||||
|
value = 0;
|
||||||
|
res = cmd_add_back(res, value, type);
|
||||||
|
}
|
||||||
|
return (res);
|
||||||
|
}
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
|
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/05 17:29:06 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/24 17:59:47 by tomoron ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -28,7 +28,7 @@ int get_var_len(char **command, t_env *env)
|
|||||||
char *env_var;
|
char *env_var;
|
||||||
|
|
||||||
(*command)++;
|
(*command)++;
|
||||||
if (**command == '\'' || **command == '"')
|
if (**command == '\'' || **command == '"' || !**command)
|
||||||
return (1);
|
return (1);
|
||||||
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
||||||
return (2);
|
return (2);
|
||||||
@ -72,51 +72,37 @@ int get_token_len(char *command, t_env *env)
|
|||||||
return (res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
int add_return_code_to_str(char *res)
|
int invalid_variable_char(char *res, char c)
|
||||||
{
|
{
|
||||||
char *var;
|
res[0] = '$';
|
||||||
int i;
|
res[1] = c;
|
||||||
|
return (2);
|
||||||
i = 0;
|
|
||||||
var = ft_itoa(g_return_code);
|
|
||||||
while (var && var[i])
|
|
||||||
{
|
|
||||||
res[i] = var[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
free(var);
|
|
||||||
return (i);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int add_var_to_str(char *res, char **command, t_env *env)
|
int add_var_to_str(char *res, char **command, t_env *env, int *is_var)
|
||||||
{
|
{
|
||||||
char *var_name;
|
char *var_name;
|
||||||
char *var;
|
char *var;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if (**command == '\'' || **command == '"')
|
(*command)++;
|
||||||
|
*is_var = 1;
|
||||||
|
if (**command == '\'' || **command == '"' || !**command)
|
||||||
{
|
{
|
||||||
*res = '$';
|
*res = '$';
|
||||||
(*command)--;
|
(*command)--;
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
||||||
{
|
return (invalid_variable_char(res, **command));
|
||||||
res[0] = '$';
|
|
||||||
res[1] = **command;
|
|
||||||
return (2);
|
|
||||||
}
|
|
||||||
if (**command == '?')
|
if (**command == '?')
|
||||||
return (add_return_code_to_str(res));
|
return (add_return_code_to_str(res));
|
||||||
var_name = get_var_name(*command);
|
var_name = get_var_name(*command);
|
||||||
var = ft_get_env(env, var_name);
|
var = ft_get_env(env, var_name);
|
||||||
free(var_name);
|
free(var_name);
|
||||||
while (var && var[i])
|
while (var && var[i++])
|
||||||
{
|
res[i - 1] = var[i - 1];
|
||||||
res[i] = var[i];
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
*command += get_var_name_len(*command) - 1;
|
*command += get_var_name_len(*command) - 1;
|
||||||
return (i);
|
return (i - 1);
|
||||||
}
|
}
|
||||||
|
62
srcs/path.c
Normal file → Executable file
62
srcs/path.c
Normal file → Executable file
@ -6,37 +6,13 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
|
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/06 10:18:53 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/24 20:37:41 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
char **split_paths_from_env(t_env *env)
|
|
||||||
{
|
|
||||||
t_env *cur_env_var;
|
|
||||||
int path_in_envp;
|
|
||||||
|
|
||||||
path_in_envp = 0;
|
|
||||||
cur_env_var = env;
|
|
||||||
while (cur_env_var->next != 0)
|
|
||||||
{
|
|
||||||
if (!ft_strcmp(cur_env_var->name, "PATH"))
|
|
||||||
{
|
|
||||||
path_in_envp = 1;
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
cur_env_var = cur_env_var->next;
|
|
||||||
}
|
|
||||||
if (!path_in_envp)
|
|
||||||
{
|
|
||||||
ft_printf_fd(2, "minishell: error: PATH not found\n");
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
return (ft_split(cur_env_var->value, ':'));
|
|
||||||
}
|
|
||||||
|
|
||||||
void find_cmd_path(t_msh *msh, char **paths, int *found)
|
void find_cmd_path(t_msh *msh, char **paths, int *found)
|
||||||
{
|
{
|
||||||
char *tmp;
|
char *tmp;
|
||||||
@ -50,15 +26,15 @@ void find_cmd_path(t_msh *msh, char **paths, int *found)
|
|||||||
tmp = ft_strjoin(path, "/");
|
tmp = ft_strjoin(path, "/");
|
||||||
if (!tmp)
|
if (!tmp)
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
path = ft_strjoin(tmp, msh->cmds->token);
|
path = ft_strjoin(tmp, msh->tokens->value);
|
||||||
if (!path)
|
if (!path)
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
if (access(path, X_OK) != -1)
|
if (access(path, X_OK) != -1)
|
||||||
{
|
{
|
||||||
*found = 1;
|
*found = 1;
|
||||||
free(msh->cmds->token);
|
free(msh->tokens->value);
|
||||||
msh->cmds->token = path;
|
msh->tokens->value = path;
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
@ -71,7 +47,7 @@ void free_paths(char **paths)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (paths[i])
|
while (paths && paths[i])
|
||||||
{
|
{
|
||||||
free(paths[i]);
|
free(paths[i]);
|
||||||
i++;
|
i++;
|
||||||
@ -84,32 +60,46 @@ void get_path(t_msh *msh, int *found)
|
|||||||
char **paths;
|
char **paths;
|
||||||
|
|
||||||
paths = split_paths_from_env(msh->env);
|
paths = split_paths_from_env(msh->env);
|
||||||
if (!paths)
|
if (!paths || !*(msh->tokens->value))
|
||||||
{
|
{
|
||||||
free_paths(paths);
|
free_paths(paths);
|
||||||
ft_exit(msh, 1);
|
return ;
|
||||||
}
|
}
|
||||||
find_cmd_path(msh, paths, found);
|
find_cmd_path(msh, paths, found);
|
||||||
free_paths(paths);
|
free_paths(paths);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *remove_path(char *token)
|
||||||
|
{
|
||||||
|
while (ft_strchr(token, '/'))
|
||||||
|
token++;
|
||||||
|
return (token);
|
||||||
|
}
|
||||||
|
|
||||||
void get_cmd_path(t_msh *msh)
|
void get_cmd_path(t_msh *msh)
|
||||||
{
|
{
|
||||||
int found;
|
int found;
|
||||||
|
|
||||||
found = 0;
|
found = 0;
|
||||||
if (ft_strchr(msh->cmds->token, '/'))
|
if (ft_strchr(msh->tokens->value, '/'))
|
||||||
{
|
{
|
||||||
if (!file_access(msh, &found))
|
if (!file_access(msh, &found))
|
||||||
|
{
|
||||||
|
free(msh->tokens->value);
|
||||||
|
msh->tokens->value = 0;
|
||||||
return ;
|
return ;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
get_path(msh, &found);
|
get_path(msh, &found);
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "%s: command not found\n", msh->cmds->token);
|
if (!*(msh->tokens->value))
|
||||||
free(msh->cmds->token);
|
ft_printf_fd(2, "'': command not found\n");
|
||||||
msh->cmds->token = 0;
|
else
|
||||||
|
ft_printf_fd(2, "%s: command not found\n", msh->tokens->value);
|
||||||
|
free(msh->tokens->value);
|
||||||
|
msh->tokens->value = 0;
|
||||||
g_return_code = 127;
|
g_return_code = 127;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
83
srcs/pipe.c
Normal file → Executable file
83
srcs/pipe.c
Normal file → Executable file
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
|
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/06 08:22:46 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/26 10:49:45 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -27,49 +27,82 @@ void close_pipe_fds(t_msh *msh, int i)
|
|||||||
close(msh->fds[i][1]);
|
close(msh->fds[i][1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
void execute_command(t_msh *msh, char **cmd_args, int i)
|
void handle_parenthesis(t_msh *msh)
|
||||||
{
|
{
|
||||||
if (msh->cmds->token && (!ft_strcmp(msh->cmds->token, "cd")
|
char *command;
|
||||||
|| !ft_strcmp(msh->cmds->token, "alias")
|
|
||||||
|| !ft_strcmp(msh->cmds->token, "unalias")
|
command = 0;
|
||||||
|| !ft_strcmp(msh->cmds->token, "exit") || exec_builtin(msh)))
|
if (msh->cmds->cmd_type == PAREN)
|
||||||
|
command = ft_strdup(msh->cmds->value);
|
||||||
|
else if (msh->cmds->cmd_type == PIPE)
|
||||||
|
command = ft_strdup(msh->cmds->next->value);
|
||||||
|
if (!command)
|
||||||
{
|
{
|
||||||
while (i >= 0)
|
ft_printf_fd(2, "an error occured");
|
||||||
{
|
|
||||||
free(msh->fds[i]);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
free(cmd_args);
|
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
}
|
}
|
||||||
if (msh->cmds->token)
|
free(msh->pids);
|
||||||
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
free_cmd(msh->cmds_head);
|
||||||
|
free_fds(msh);
|
||||||
|
msh->in_type = 0;
|
||||||
|
msh->out_type = 0;
|
||||||
|
msh->in_fd = 0;
|
||||||
|
msh->out_fd = 0;
|
||||||
|
msh->locked_return_code = 0;
|
||||||
|
exec_command_bonus(msh, command);
|
||||||
|
free(command);
|
||||||
|
ft_exit(msh, g_return_code);
|
||||||
|
}
|
||||||
|
|
||||||
|
void execute_command(t_msh *msh, char **cmd_args)
|
||||||
|
{
|
||||||
|
char **env;
|
||||||
|
|
||||||
|
if (is_parenthesis(msh->cmds))
|
||||||
|
{
|
||||||
|
free(cmd_args);
|
||||||
|
handle_parenthesis(msh);
|
||||||
|
}
|
||||||
|
if (exec_builtin(msh))
|
||||||
|
{
|
||||||
|
free(cmd_args);
|
||||||
|
ft_exit(msh, g_return_code);
|
||||||
|
}
|
||||||
|
if (msh->tokens->value)
|
||||||
|
{
|
||||||
|
env = env_to_char_tab(msh->env);
|
||||||
|
if (env)
|
||||||
|
{
|
||||||
|
if (execve(msh->tokens->value, cmd_args, env))
|
||||||
|
perror("execve");
|
||||||
|
}
|
||||||
|
ft_free_str_arr(env);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void child(t_msh *msh, char **cmd_args, int i)
|
void child(t_msh *msh, char **cmd_args, int i)
|
||||||
{
|
{
|
||||||
if ((msh->in_type != ARG && msh->in_type != PIPE)
|
if ((msh->in_type != CMD && msh->in_type != PAREN && msh->in_type != AND
|
||||||
|
&& msh->in_type != OR && msh->in_type != PIPE)
|
||||||
|| (msh->in_type == PIPE && i > 0))
|
|| (msh->in_type == PIPE && i > 0))
|
||||||
redirect_input(msh, i);
|
redirect_input(msh, i, cmd_args);
|
||||||
if (msh->out_type == PIPE || msh->out_type == RED_O
|
if (msh->out_type == PIPE || msh->out_type == RED_O
|
||||||
|| msh->out_type == RED_O_APP)
|
|| msh->out_type == RED_O_APP)
|
||||||
redirect_output(msh, i);
|
redirect_output(msh, i, cmd_args);
|
||||||
close_pipe_fds(msh, i);
|
close_pipe_fds(msh, i);
|
||||||
execute_command(msh, cmd_args, i);
|
execute_command(msh, cmd_args);
|
||||||
close(0);
|
close(0);
|
||||||
close(1);
|
close(1);
|
||||||
close(2);
|
close(2);
|
||||||
while (i >= 0)
|
|
||||||
{
|
|
||||||
free(msh->fds[i]);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
free(cmd_args);
|
free(cmd_args);
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, g_return_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
void parent(t_msh *msh, int i, int cmd_count)
|
void parent(t_msh *msh, int i, int cmd_count, char **cmd_args)
|
||||||
{
|
{
|
||||||
|
free(cmd_args);
|
||||||
|
signal(SIGINT, signal_handler_command);
|
||||||
|
signal(SIGQUIT, signal_handler_command);
|
||||||
if (i != 0)
|
if (i != 0)
|
||||||
{
|
{
|
||||||
if (msh->fds[i - 1][0] > 2)
|
if (msh->fds[i - 1][0] > 2)
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/09 18:31:21 by tomoron #+# #+# */
|
/* Created: 2024/02/09 18:31:21 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 12:47:39 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/18 20:48:59 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
75
srcs/signal_handler.c
Executable file
75
srcs/signal_handler.c
Executable file
@ -0,0 +1,75 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* signal_handler.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/22 14:31:13 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/26 11:09:27 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void signal_handler_interactive(int signum)
|
||||||
|
{
|
||||||
|
if (signum == SIGINT)
|
||||||
|
{
|
||||||
|
g_return_code = 130;
|
||||||
|
rl_replace_line("", 0);
|
||||||
|
printf("^C\n");
|
||||||
|
rl_on_new_line();
|
||||||
|
rl_redisplay();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void *here_doc_variables(int write, void *data)
|
||||||
|
{
|
||||||
|
static void *variable;
|
||||||
|
|
||||||
|
if (write)
|
||||||
|
variable = data;
|
||||||
|
else
|
||||||
|
return (variable);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void signal_handler_here_doc(int signum)
|
||||||
|
{
|
||||||
|
t_msh *msh;
|
||||||
|
|
||||||
|
if (signum == SIGINT)
|
||||||
|
{
|
||||||
|
msh = here_doc_variables(0, 0);
|
||||||
|
rl_on_new_line();
|
||||||
|
printf("^C\n");
|
||||||
|
if (msh->in_fd > 2)
|
||||||
|
close(msh->in_fd);
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int set_echoctl(int value)
|
||||||
|
{
|
||||||
|
struct termios t_p;
|
||||||
|
|
||||||
|
if (!isatty(1))
|
||||||
|
return (0);
|
||||||
|
if (tcgetattr(1, &t_p))
|
||||||
|
return (1);
|
||||||
|
if (((t_p.c_lflag & ECHOCTL) != 0) == value)
|
||||||
|
return (0);
|
||||||
|
if (value)
|
||||||
|
t_p.c_lflag = t_p.c_lflag | ECHOCTL;
|
||||||
|
else
|
||||||
|
t_p.c_lflag = t_p.c_lflag & (~ECHOCTL);
|
||||||
|
if (tcsetattr(1, TCSANOW, &t_p))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void signal_handler_command(int signum)
|
||||||
|
{
|
||||||
|
(void)signum;
|
||||||
|
}
|
56
srcs/sort_wildcard_list.c
Normal file
56
srcs/sort_wildcard_list.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* sort_wildcard_list.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/17 09:12:33 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/22 19:30:43 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int wildcard_cmp(char *s1, char *s2, char *order)
|
||||||
|
{
|
||||||
|
while (*s1 && *s2)
|
||||||
|
{
|
||||||
|
while (*s1 && !ft_isalnum(*s1))
|
||||||
|
s1++;
|
||||||
|
while (*s2 && !ft_isalnum(*s2))
|
||||||
|
s2++;
|
||||||
|
if (*s1 != *s2)
|
||||||
|
return ((ft_strchr(order, *s2) - order) - (ft_strchr(order, *s1)
|
||||||
|
- order));
|
||||||
|
s1++;
|
||||||
|
s2++;
|
||||||
|
}
|
||||||
|
return ((ft_strchr(order, *s2) - order) - (ft_strchr(order, *s1) - order));
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_wildcards_token(t_token *list)
|
||||||
|
{
|
||||||
|
t_token *tmp;
|
||||||
|
t_token *start;
|
||||||
|
char *swap;
|
||||||
|
|
||||||
|
tmp = list;
|
||||||
|
start = list;
|
||||||
|
while (tmp)
|
||||||
|
{
|
||||||
|
list = start;
|
||||||
|
while (list->next)
|
||||||
|
{
|
||||||
|
if (wildcard_cmp(list->value, list->next->value, \
|
||||||
|
"zZyYxXwWvVuUtTsSrRqQpPoOnNmMlLkKjJiIhHgGfFeEdDcCbBaA9876543210") > 0)
|
||||||
|
{
|
||||||
|
swap = list->value;
|
||||||
|
list->value = list->next->value;
|
||||||
|
list->next->value = swap;
|
||||||
|
}
|
||||||
|
list = list->next;
|
||||||
|
}
|
||||||
|
tmp = tmp->next;
|
||||||
|
}
|
||||||
|
}
|
@ -1,57 +0,0 @@
|
|||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* unalias.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/02/21 13:11:45 by marde-vr #+# #+# */
|
|
||||||
/* Updated: 2024/02/21 17:44:30 by marde-vr ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
int remove_alias(t_msh *msh)
|
|
||||||
{
|
|
||||||
t_alias *alias;
|
|
||||||
|
|
||||||
alias = msh->aliases;
|
|
||||||
while (alias)
|
|
||||||
{
|
|
||||||
if (alias->next && msh->cmds->next
|
|
||||||
&& !ft_strcmp(alias->next->name, msh->cmds->next->token))
|
|
||||||
{
|
|
||||||
if (alias->next->next)
|
|
||||||
alias->next = alias->next->next;
|
|
||||||
else
|
|
||||||
alias->next = 0;
|
|
||||||
alias->next = 0;
|
|
||||||
free_alias(alias);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
if (alias->next)
|
|
||||||
alias = alias->next;
|
|
||||||
else
|
|
||||||
alias = 0;
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int unalias(t_msh *msh)
|
|
||||||
{
|
|
||||||
if (msh->cmds->next && !ft_strcmp(msh->cmds->next->token, "-a"))
|
|
||||||
{
|
|
||||||
free_alias(msh->aliases);
|
|
||||||
msh->aliases = 0;
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
if (remove_alias(msh))
|
|
||||||
return (1);
|
|
||||||
if (msh->cmds->next && msh->cmds->next->type == ARG)
|
|
||||||
ft_printf("minishell: unalias: %s: not found\n",
|
|
||||||
msh->cmds->next->token);
|
|
||||||
else
|
|
||||||
ft_printf("unalias: usage: unalias name\n");
|
|
||||||
return (1);
|
|
||||||
}
|
|
25
other/test.c → srcs/unset.c
Executable file → Normal file
25
other/test.c → srcs/unset.c
Executable file → Normal file
@ -1,27 +1,28 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* test.c :+: :+: :+: */
|
/* unset.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/04 17:30:08 by tomoron #+# #+# */
|
/* Created: 2024/04/26 10:52:20 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/09 16:41:52 by tomoron ### ########.fr */
|
/* Updated: 2024/04/26 10:52:28 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include "minishell.h"
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int ft_unset(t_msh *msh)
|
||||||
{
|
{
|
||||||
int i;
|
t_token *cmd;
|
||||||
|
|
||||||
i = 0;
|
cmd = msh->tokens;
|
||||||
while (i < argc)
|
if (cmd)
|
||||||
|
cmd = cmd->next;
|
||||||
|
while (cmd)
|
||||||
{
|
{
|
||||||
printf("\"%s\"\n", argv[i]);
|
delete_from_env(msh, cmd->value);
|
||||||
i++;
|
cmd = cmd->next;
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
63
srcs/utils.c
Normal file → Executable file
63
srcs/utils.c
Normal file → Executable file
@ -6,52 +6,67 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
|
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/03/06 10:19:58 by marde-vr ### ########.fr */
|
/* Updated: 2024/04/26 10:53:29 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void free_msh(t_msh *msh)
|
|
||||||
{
|
|
||||||
if (msh)
|
|
||||||
{
|
|
||||||
if (msh->cmds)
|
|
||||||
free_cmd(msh->cmds);
|
|
||||||
if (msh->env)
|
|
||||||
free_env(msh->env);
|
|
||||||
if (msh->aliases)
|
|
||||||
free_alias(msh->aliases);
|
|
||||||
if (msh->pids)
|
|
||||||
free(msh->pids);
|
|
||||||
if (msh->fds)
|
|
||||||
free(msh->fds);
|
|
||||||
free(msh);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ft_exit(t_msh *msh, int exit_code)
|
void ft_exit(t_msh *msh, int exit_code)
|
||||||
{
|
{
|
||||||
free_msh(msh);
|
free_msh(msh);
|
||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int check_var_name(char *name)
|
||||||
|
{
|
||||||
|
if (ft_isdigit(*name) || !*name)
|
||||||
|
return (0);
|
||||||
|
while (*name)
|
||||||
|
{
|
||||||
|
if (!ft_isalnum(*name) && *name != '_')
|
||||||
|
return (0);
|
||||||
|
name++;
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int add_return_code_to_str(char *res)
|
||||||
|
{
|
||||||
|
char *var;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
var = ft_itoa(g_return_code);
|
||||||
|
while (var && var[i])
|
||||||
|
{
|
||||||
|
res[i] = var[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
free(var);
|
||||||
|
return (i);
|
||||||
|
}
|
||||||
|
|
||||||
int file_access(t_msh *msh, int *found)
|
int file_access(t_msh *msh, int *found)
|
||||||
{
|
{
|
||||||
if (open(msh->cmds->token, O_DIRECTORY) != -1)
|
int fd;
|
||||||
|
|
||||||
|
fd = open(msh->tokens->value, O_DIRECTORY);
|
||||||
|
if (fd != -1)
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "minishell: %s: Is a directory\n", msh->cmds->token);
|
close(fd);
|
||||||
|
ft_printf_fd(2, "minishell: %s: Is a directory\n", msh->tokens->value);
|
||||||
g_return_code = 126;
|
g_return_code = 126;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
if (access(msh->cmds->token, X_OK) != -1)
|
if (access(msh->tokens->value, X_OK) != -1)
|
||||||
*found = 1;
|
*found = 1;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "minishell: %s: ", msh->cmds->token);
|
ft_printf_fd(2, "minishell: %s: ", msh->tokens->value);
|
||||||
perror("");
|
perror("");
|
||||||
g_return_code = 127;
|
g_return_code = 127;
|
||||||
if (access(msh->cmds->token, F_OK) != -1)
|
if (access(msh->tokens->value, F_OK) != -1)
|
||||||
g_return_code = 126;
|
g_return_code = 126;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
41
srcs/utils_bonus.c
Executable file
41
srcs/utils_bonus.c
Executable file
@ -0,0 +1,41 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils_bonus.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/27 17:19:27 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/25 13:51:44 by tomoron ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
t_token *parsing_syntax_error(t_token *res)
|
||||||
|
{
|
||||||
|
free_token(res);
|
||||||
|
ft_putstr_fd("minishell: syntax error\n", 2);
|
||||||
|
g_return_code = 2;
|
||||||
|
return ((void *)1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_input_type(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
return (cmd->cmd_type == HERE_DOC || cmd->cmd_type == RED_I);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_output_type(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
return (cmd->cmd_type == RED_O || cmd->cmd_type == RED_O_APP);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_cmd_type(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
return (cmd->cmd_type == CMD || cmd->cmd_type == PAREN);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_operand_type(t_cmd *cmd)
|
||||||
|
{
|
||||||
|
return (cmd->cmd_type == AND || cmd->cmd_type == OR);
|
||||||
|
}
|
94
srcs/wildcards.c
Normal file
94
srcs/wildcards.c
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* wildcards.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/04/15 12:53:29 by tomoron #+# #+# */
|
||||||
|
/* Updated: 2024/04/26 10:49:36 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int filename_corresponds(char *wildcard, char *value)
|
||||||
|
{
|
||||||
|
if (*value == '.' && *wildcard != '.')
|
||||||
|
return (0);
|
||||||
|
while (*wildcard && *value)
|
||||||
|
{
|
||||||
|
if (*wildcard == '*')
|
||||||
|
{
|
||||||
|
while (*wildcard == '*' && wildcard[1] == '*')
|
||||||
|
wildcard++;
|
||||||
|
if (!wildcard[1])
|
||||||
|
return (1);
|
||||||
|
while (*value)
|
||||||
|
if (filename_corresponds(wildcard + 1, value++))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else if (*wildcard == *value)
|
||||||
|
{
|
||||||
|
wildcard++;
|
||||||
|
value++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (!*wildcard && !*value);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_token *get_all_files(DIR *dir, char *wildcard, int is_var)
|
||||||
|
{
|
||||||
|
struct dirent *content;
|
||||||
|
t_token *res;
|
||||||
|
|
||||||
|
res = 0;
|
||||||
|
content = readdir(dir);
|
||||||
|
while (content)
|
||||||
|
{
|
||||||
|
if (filename_corresponds(wildcard, content->d_name))
|
||||||
|
res = token_add_back(res, ft_strdup(content->d_name), is_var);
|
||||||
|
content = readdir(dir);
|
||||||
|
}
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_token *add_token_back(t_token *res, t_token *next)
|
||||||
|
{
|
||||||
|
t_token *start;
|
||||||
|
|
||||||
|
if (!res)
|
||||||
|
return (next);
|
||||||
|
start = res;
|
||||||
|
while (res->next)
|
||||||
|
res = res->next;
|
||||||
|
res->next = next;
|
||||||
|
return (start);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_token *expand_wildcards(t_token *res, char *value, int is_var)
|
||||||
|
{
|
||||||
|
DIR *dir;
|
||||||
|
char *cwd;
|
||||||
|
t_token *new;
|
||||||
|
|
||||||
|
if (!ft_strchr(value, '*'))
|
||||||
|
return (token_add_back(res, value, is_var));
|
||||||
|
cwd = getcwd(NULL, 100000);
|
||||||
|
if (!cwd)
|
||||||
|
return (token_add_back(res, value, is_var));
|
||||||
|
dir = opendir(cwd);
|
||||||
|
free(cwd);
|
||||||
|
if (!dir)
|
||||||
|
return (token_add_back(res, value, is_var));
|
||||||
|
new = get_all_files(dir, value, is_var);
|
||||||
|
closedir(dir);
|
||||||
|
if (!new)
|
||||||
|
return (token_add_back(res, value, is_var));
|
||||||
|
free(value);
|
||||||
|
sort_wildcards_token(new);
|
||||||
|
res = add_token_back(res, new);
|
||||||
|
return (res);
|
||||||
|
}
|
16
todo_list
Normal file
16
todo_list
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
Cat makefile | (echo yes && rev) leaks
|
||||||
|
echo yes && echo yes && cat dhdgdgjsk || echo a leaks
|
||||||
|
echo abc && echo def | cat | rev && echo yes
|
||||||
|
(), ((())), (())
|
||||||
|
export leaks AAAAAAAAAAAAAAAAAAAAAA
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
To test:
|
||||||
|
Open file descriptor with cat << eof | ls open fd and infinte loop
|
||||||
|
|
||||||
|
test signals
|
||||||
|
test and verify all malocs
|
||||||
|
verify forbidden functions
|
||||||
|
check for any hidden or useless files
|
Reference in New Issue
Block a user