Skip to content

Commit f3fc930

Browse files
committed
Update code with recent changes
1 parent 50f4ea8 commit f3fc930

File tree

9 files changed

+303
-14
lines changed

9 files changed

+303
-14
lines changed

Makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ SRC_MAIN = ./bin/main/main.c \
2121
./bin/utils/free.c \
2222
./bin/utils/path.c \
2323
./bin/utils/split.c \
24-
./bin/utils/utils.c
24+
./bin/utils/utils.c \
25+
./bin/utils/get_next_line.c \
26+
./bin/utils/get_next_line_utils.c \
2527

2628
SRC_BONUS = ./bin/main/main_bonus.c \
2729
./bin/pipex/bonus.c \
@@ -39,7 +41,9 @@ SRC_BONUS = ./bin/main/main_bonus.c \
3941
./bin/utils/free.c \
4042
./bin/utils/path.c \
4143
./bin/utils/split.c \
42-
./bin/utils/utils.c
44+
./bin/utils/utils.c \
45+
./bin/utils/get_next_line.c \
46+
./bin/utils/get_next_line_utils.c \
4347

4448
OBJ_MAIN = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC_MAIN))
4549
OBJ_BONUS = $(patsubst $(SRC_BONUS_DIR)/%.c, $(BUILD_BONUS_DIR)/%.o, $(SRC_BONUS))

bin/print/print.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/28 22:28:03 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 15:24:43 by tauer ### ########.fr */
9+
/* Updated: 2024/04/23 11:45:06 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -37,14 +37,17 @@ void print_command(t_arg *current, bool isError)
3737
ft_printf("\n");
3838
}
3939

40-
void print_fd(t_arg *current, bool isError)
40+
void print_fd(t_arg *current, bool isError, bool isHeredoc)
4141
{
4242
if (!current->pos)
4343
write(1, "pos type fd/path name \n", 33);
4444
write_line();
4545
if (isError)
4646
ft_printf("[%d] | [\033[38;5;196m%s\033[0m] | [%d] [%s]\n",
4747
current->pos, current->type, current->fd, current->name[0]);
48+
else if (isHeredoc)
49+
ft_printf("[%d] | [\033[38;5;154m%s\033[0m] | [%d] [%s]\n",
50+
current->pos, current->type, current->fd, current->name[0]);
4851
else
4952
ft_printf("[%d] | [\033[38;5;154m%s\033[0m] | [%d] [%s]\n",
5053
current->pos, current->type, current->fd, current->name[0]);
@@ -64,10 +67,10 @@ void print_arg(t_data *data)
6467
|| current->pos != data->env.argc - 1)))
6568
print_command(current, ft_strncmp(current->type, "ERR", 2));
6669
else if (ft_strncmp(current->type, "IFD", 2)
67-
|| ft_strncmp(current->type, "OFD", 2) || (ft_strncmp(current->type,
70+
|| ft_strncmp(current->type, "OFD", 2) || ft_strncmp(current->type, "HDC", 2) || (ft_strncmp(current->type,
6871
"ERR", 2) && (current->pos == 0
6972
|| current->pos == data->env.argc - 1)))
70-
print_fd(current, ft_strncmp(current->type, "ERR", 2));
73+
print_fd(current, ft_strncmp(current->type, "ERR", 2), ft_strncmp(current->type, "HDC", 2));
7174
current = current->next;
7275
}
7376
}

bin/setup/args.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/28 22:36:00 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 15:32:43 by tauer ### ########.fr */
9+
/* Updated: 2024/04/23 11:00:17 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

bin/utils/bool.c

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/04/22 15:27:18 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 15:37:39 by tauer ### ########.fr */
9+
/* Updated: 2024/04/23 12:12:35 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -46,6 +46,42 @@ bool is_nopath(t_data *data, t_arg *arg)
4646
return (false);
4747
}
4848

49+
void print_tab(char **tab)
50+
{
51+
for (int i = 0; tab[i]; i++)
52+
printf("%d - %s\n", i, tab[i]);
53+
}
54+
55+
bool is_heredoc(t_data *data)
56+
{
57+
int tmp_stdin;
58+
char *line;
59+
60+
line = "";
61+
if (ft_strncmp(data->env.argv[data->env.argc - 1], "here_doc", 8))
62+
{
63+
ft_printf("\t[HEREDOC << %s]\n\n", data->env.argv[data->env.argc - 2]);
64+
data->pip.in_fd = open(".heredoc.tmp", O_CREAT | O_WRONLY | O_TRUNC,
65+
0644);
66+
tmp_stdin = dup(STDIN_FILENO);
67+
write(1, "\there_doc > ", 13);
68+
while (1)
69+
{
70+
line = get_next_line(tmp_stdin);
71+
if (!line)
72+
break ;
73+
if (ft_strncmp(line, data->env.argv[data->env.argc - 2],
74+
ft_strlen(data->env.argv[data->env.argc - 2])))
75+
close(tmp_stdin);
76+
else
77+
write(1, "\there_doc > ", 13);
78+
free(line);
79+
}
80+
return (write(1, "\n", 2), true);
81+
}
82+
return (false);
83+
}
84+
4985
bool is_fd(t_data *data, t_arg *arg)
5086
{
5187
int fd;
@@ -55,6 +91,9 @@ bool is_fd(t_data *data, t_arg *arg)
5591
data->pip.safety_fd = open("/dev/null", O_RDONLY);
5692
if (data->pip.safety_fd < 0)
5793
return (false);
94+
if (is_heredoc(data))
95+
return (arg->fd = data->pip.in_fd, arg->type = "HDC",
96+
arg->path = NULL, true);
5897
fd = open(arg->name[0], O_RDONLY);
5998
if (fd > 0)
6099
return (data->pip.in_fd = fd, arg->fd = fd, arg->type = "IFD",

bin/utils/get_next_line.c

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: tauer <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/11/27 22:38:23 by marvin #+# #+# */
9+
/* Updated: 2024/04/23 10:52:23 by tauer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include <all.h>
14+
15+
void extract_line(t_list *stash, char **line)
16+
{
17+
int i;
18+
int j;
19+
20+
if (stash == NULL)
21+
return ;
22+
generate_line(line, stash);
23+
if (*line == NULL)
24+
return ;
25+
j = 0;
26+
while (stash)
27+
{
28+
i = 0;
29+
while (stash->content[i])
30+
{
31+
if (stash->content[i] == '\n')
32+
{
33+
(*line)[j++] = stash->content[i];
34+
break ;
35+
}
36+
(*line)[j++] = stash->content[i++];
37+
}
38+
stash = stash->next;
39+
}
40+
(*line)[j] = '\0';
41+
}
42+
43+
void free_stash(t_list *stash)
44+
{
45+
t_list *current;
46+
t_list *next;
47+
48+
current = stash;
49+
while (current)
50+
{
51+
free(current->content);
52+
next = current->next;
53+
free(current);
54+
current = next;
55+
}
56+
}
57+
58+
void clear_stash(t_list **stash)
59+
{
60+
t_list *last;
61+
t_list *clean_node;
62+
int i;
63+
int j;
64+
65+
clean_node = malloc(sizeof(t_list));
66+
if (stash == NULL || clean_node == NULL)
67+
return ;
68+
clean_node->next = NULL;
69+
last = ft_lst_get_last(*stash);
70+
i = 0;
71+
while (last->content[i] && last->content[i] != '\n')
72+
i++;
73+
if (last->content && last->content[i] == '\n')
74+
i++;
75+
clean_node->content = malloc(sizeof(char) * ((ft_strlen(last->content) - i)
76+
+ 1));
77+
if (clean_node == NULL)
78+
return ;
79+
j = 0;
80+
while (last->content[i])
81+
clean_node->content[j++] = last->content[i++];
82+
clean_node->content[j] = '\0';
83+
free_stash(*stash);
84+
*stash = clean_node;
85+
}
86+
87+
char *get_next_line(int fd)
88+
{
89+
static t_list *stash = NULL;
90+
char *line;
91+
int readed;
92+
93+
if (fd < 0 || BUFFER_SIZE < 0 || read(fd, &line, 0) < 0)
94+
return (NULL);
95+
readed = 1;
96+
line = NULL;
97+
// read from fd and add to linker list
98+
read_and_stash(&stash, &readed, fd);
99+
if (stash == NULL)
100+
return (NULL);
101+
// extract from stash to line
102+
extract_line(stash, &line);
103+
// cleanup la stash
104+
clear_stash(&stash);
105+
if (line[0] == '\0')
106+
{
107+
free_stash(stash);
108+
stash = NULL;
109+
free(line);
110+
return (NULL);
111+
}
112+
return (line);
113+
}

bin/utils/get_next_line_utils.c

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* get_next_line_utils.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: tauer <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/11/28 13:01:50 by marvin #+# #+# */
9+
/* Updated: 2024/04/23 10:39:16 by tauer ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include <all.h>
14+
15+
t_list *ft_lst_get_last(t_list *stash)
16+
{
17+
t_list *current;
18+
19+
current = stash;
20+
while (current && current->next)
21+
current = current->next;
22+
return (current);
23+
}
24+
25+
int find_newline(t_list *stash)
26+
{
27+
int i;
28+
t_list *current;
29+
30+
if (stash == NULL)
31+
return (0);
32+
current = ft_lst_get_last(stash);
33+
i = 0;
34+
while (current->content[i])
35+
{
36+
if (current->content[i] == '\n')
37+
return (1);
38+
i++;
39+
}
40+
return (0);
41+
}
42+
43+
void add_to_stash(t_list **stash, char *buff, int readed)
44+
{
45+
int i;
46+
t_list *last;
47+
t_list *new_node;
48+
49+
new_node = malloc(sizeof(t_list));
50+
if (new_node == NULL)
51+
return ;
52+
new_node->next = NULL;
53+
new_node->content = malloc(sizeof(char) * (readed + 1));
54+
if (new_node->content == NULL)
55+
return ;
56+
i = 0;
57+
while (buff[i] && i < readed)
58+
{
59+
new_node->content[i] = buff[i];
60+
i++;
61+
}
62+
new_node->content[i] = '\0';
63+
if (*stash == NULL)
64+
{
65+
*stash = new_node;
66+
return ;
67+
}
68+
last = ft_lst_get_last(*stash);
69+
last->next = new_node;
70+
}
71+
72+
void read_and_stash(t_list **stash, int *readed_ptr, int fd)
73+
{
74+
char *buff;
75+
76+
while (!find_newline(*stash) && *readed_ptr != 0)
77+
{
78+
buff = malloc(sizeof(char) * (BUFFER_SIZE + 1));
79+
if (buff == NULL)
80+
return ;
81+
*readed_ptr = (int)read(fd, buff, BUFFER_SIZE);
82+
if ((*stash == NULL && *readed_ptr == 0) || *readed_ptr == -1)
83+
{
84+
free(buff);
85+
return ;
86+
}
87+
buff[*readed_ptr] = '\0';
88+
add_to_stash(stash, buff, *readed_ptr);
89+
free(buff);
90+
}
91+
}
92+
93+
void generate_line(char **line, t_list *stash)
94+
{
95+
int i;
96+
int len;
97+
98+
len = 0;
99+
while (stash)
100+
{
101+
i = 0;
102+
while (stash->content[i])
103+
{
104+
if (stash->content[i] == '\n')
105+
{
106+
len++;
107+
break ;
108+
}
109+
len++;
110+
i++;
111+
}
112+
stash = stash->next;
113+
}
114+
*line = malloc(sizeof(char) * (len + 1));
115+
}

includes/def.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: tauer <[email protected]> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2024/03/27 23:13:45 by tauer #+# #+# */
9-
/* Updated: 2024/04/22 15:35:06 by tauer ### ########.fr */
9+
/* Updated: 2024/04/23 10:38:05 by tauer ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -70,4 +70,11 @@ bool create_list(t_data *data);
7070

7171
extern int ft_printf(const char *str, ...);
7272

73+
t_list *ft_lst_get_last(t_list *stash);
74+
int find_newline(t_list *stash);
75+
void add_to_stash(t_list **stash, char *buff, int readed);
76+
void read_and_stash(t_list **stash, int *readed_ptr, int fd);
77+
void generate_line(char **line, t_list *stash);
78+
char *get_next_line(int fd);
79+
7380
#endif

0 commit comments

Comments
 (0)