Skip to content

Commit 63542a9

Browse files
authored
Merge pull request #43 from rick-morty-kh/bugs
heredoc and signal bug fix
2 parents 9bedbc8 + 8e8765f commit 63542a9

File tree

9 files changed

+107
-12
lines changed

9 files changed

+107
-12
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ USERS = $(USER)
66
COMP = -L/goinfre/$(USERS)/.brew/opt/readline/lib
77
LINK = -I/goinfre/$(USERS)/.brew/opt/readline/include
88

9-
SRCS = srcs/ft_close.c srcs/ft_dups.c srcs/ft_execve.c srcs/ft_fork.c srcs/ft_open.c srcs/ft_pipe.c srcs/ft_waits.c builtins/cd.c builtins/echo.c builtins/env.c builtins/export.c builtins/helpers.c builtins/pwd.c builtins/shell_exit.c builtins/unset.c builtins/utils.c execution/environment.c execution/execute.c execution/heredoc.c execution/misc.c execution/paths.c execution/shell.c execution/signals.c parsing/commands.c parsing/commands_list.c parsing/env.c parsing/expand.c parsing/expand_env.c parsing/free.c parsing/heredoc.c parsing/lexer.c parsing/operator.c parsing/parser.c parsing/parsing_utils.c parsing/token_list.c parsing/word_spliting.c utils/alpha.c utils/contains.c utils/fatal.c utils/ft_atoi.c utils/ft_calloc.c utils/ft_exit.c utils/ft_free.c utils/ft_itoa.c utils/ft_memcpy.c utils/ft_split.c utils/ft_strcmp.c utils/ft_strdup.c utils/ft_strjoins.c utils/ft_strlen.c utils/ft_substr.c utils/prints.c main.c
9+
SRCS = srcs/ft_close.c srcs/ft_dups.c srcs/ft_execve.c srcs/ft_fork.c srcs/ft_open.c srcs/ft_pipe.c srcs/ft_waits.c builtins/cd.c builtins/echo.c builtins/env.c builtins/export.c builtins/helpers.c builtins/pwd.c builtins/shell_exit.c builtins/unset.c builtins/utils.c execution/environment.c execution/execute.c execution/heredoc.c execution/misc.c execution/paths.c execution/shell.c execution/signals.c parsing/commands.c parsing/commands_list.c parsing/env.c parsing/expand.c parsing/expand_env.c parsing/free.c parsing/heredoc.c parsing/lexer.c parsing/operator.c parsing/parser.c parsing/heredoc_expansion.c parsing/parsing_utils.c parsing/token_list.c parsing/word_spliting.c utils/alpha.c utils/contains.c utils/fatal.c utils/ft_atoi.c utils/ft_calloc.c utils/ft_exit.c utils/ft_free.c utils/ft_itoa.c utils/ft_memcpy.c utils/ft_split.c utils/ft_strcmp.c utils/ft_strdup.c utils/ft_strjoins.c utils/ft_strlen.c utils/ft_substr.c utils/prints.c main.c
1010
SRCS_DIRS = $(dir $(SRCS))
1111

1212
BIN_DIR = bin/
@@ -22,11 +22,11 @@ NAME = minishell
2222
all: $(NAME)
2323

2424
$(NAME): $(OBJS)
25-
@$(CC) -L/goinfre/$(USERS)/.brew/opt/readline/lib -I/goinfre/$(USERS)/.brew/opt/readline/include -lreadline $(OBJS) -o $(NAME)
25+
@$(CC) -g -L/goinfre/$(USERS)/.brew/opt/readline/lib -I/goinfre/$(USERS)/.brew/opt/readline/include -lreadline $(OBJS) -o $(NAME)
2626

2727
$(BIN_DIR)%.o: %.c $(INCLUDES)
2828
@$(MKDIR) $(OBJS_DIRS)
29-
$(CC) $(LINK) $(CFLAGS) -c $< -o $@
29+
$(CC) $(LINK) $(CFLAGS) -c $< -g -o $@
3030

3131
clean:
3232
@$(RM) $(BIN_DIR)

builtins/echo.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void echo(char **args, int fd)
2727
ft_putstr_fd("\n", fd);
2828
return ;
2929
}
30-
if (is_flag_n(args[i]))
30+
while (args[i] && is_flag_n(args[i]))
3131
{
3232
new_line = FALSE;
3333
i++;

builtins/unset.c

+3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ int unset(char **args, int fd)
2727
return (1);
2828
}
2929
if (!ft_strcmp(*args, "_"))
30+
{
31+
args++;
3032
continue ;
33+
}
3134
pop_env(*args);
3235
args++;
3336
}

execution/shell.c

-6
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,3 @@ void shell_loop(void)
3838
free(line);
3939
}
4040
}
41-
42-
void set_exit_status(int sig)
43-
{
44-
(void)sig;
45-
ft_putchar_fd('\n', 1);
46-
}

execution/signals.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void ignore_signals(void)
5353
struct sigaction sa_sigint;
5454
struct sigaction sa_sigquit;
5555

56-
sa_sigint.sa_handler = &set_exit_status;
56+
sa_sigint.sa_handler = SIG_IGN;
5757
sa_sigquit.sa_handler = SIG_IGN;
5858
sigaction(SIGINT, &sa_sigint, NULL);
5959
sigaction(SIGQUIT, &sa_sigquit, NULL);

includes/parsing.h

+2
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,7 @@ void free_token_word(t_token *token, char *word);
8080
void check_and_redirect(int *inf_out, int fd);
8181
void free_all(t_token *tokens);
8282
int handle_heredocs(t_cmd *cmd, t_token *tokens);
83+
int get_name_len(char *token, int i);
84+
char *heredoc_expansion(char *line);
8385

8486
#endif // PARSING_H

parsing/heredoc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ int handle_heredoc(t_cmd *cmd, char *limiter, char *file)
6868
while (line && ft_strcmp(line, limiter))
6969
{
7070
if (expand_mode && *line)
71-
line = parameter_expansion(line);
71+
line = heredoc_expansion(line);
7272
joined_line = ft_strjoin(line, "\n");
7373
write(fd, joined_line, ft_strlen(joined_line));
7474
free(line);

parsing/heredoc_expansion.c

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* heredoc_expansion.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mtagemou <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/02/06 12:59:34 by mtagemou #+# #+# */
9+
/* Updated: 2023/02/06 12:59:36 by mtagemou ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "minishell.h"
14+
15+
char *get_name_here(char *token)
16+
{
17+
int i;
18+
int name_len;
19+
20+
i = 0;
21+
name_len = -1;
22+
while (token[i])
23+
{
24+
name_len = -1;
25+
if (token[i] == '$')
26+
name_len = get_name_len(token, i);
27+
if (name_len == 0)
28+
token[i] = -1;
29+
if (name_len > 0)
30+
break ;
31+
i++;
32+
}
33+
if (name_len == 0 || name_len == -1)
34+
return (NULL);
35+
return (ft_substr(token, i, i + name_len));
36+
}
37+
38+
int replace_before_name_here(char *new_token, char *token)
39+
{
40+
int i;
41+
42+
i = 0;
43+
while (token[i] != '$')
44+
{
45+
new_token[i] = token[i];
46+
i++;
47+
}
48+
return (i);
49+
}
50+
51+
char *replace_name_value_here(char *token, char *name, char *value)
52+
{
53+
char *new_token;
54+
int i;
55+
int j;
56+
int k;
57+
58+
i = 0;
59+
j = 0;
60+
new_token = ft_calloc(ft_strlen(token)
61+
+ (ft_strlen(value) - ft_strlen(name)) + 1);
62+
i = replace_before_name_here(new_token, token);
63+
k = i;
64+
j = 0;
65+
while (value[j])
66+
{
67+
if (value[j] == '$')
68+
value[j] = -1;
69+
new_token[i++] = value[j++];
70+
}
71+
k += ft_strlen(name);
72+
while (token[k])
73+
new_token[i++] = token[k++];
74+
free(token);
75+
return (new_token);
76+
}
77+
78+
char *heredoc_expansion(char *line)
79+
{
80+
char *name;
81+
char *value;
82+
char *new_line;
83+
84+
name = get_name_here(line);
85+
if (!name)
86+
{
87+
expands_dollars_dollars(line);
88+
return (line);
89+
}
90+
value = get_env_value(name + 1);
91+
new_line = replace_name_value_here(line, name, value);
92+
if (*(name + 1) == '?')
93+
free(value);
94+
free(name);
95+
return (heredoc_expansion(new_line));
96+
}

path/ls

37.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)