-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse_step-1_mask.c
More file actions
94 lines (85 loc) · 2.45 KB
/
parse_step-1_mask.c
File metadata and controls
94 lines (85 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_step-1_mask.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hyeyeom <hyeyeom@42student.gyeongsan.kr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/23 17:18:32 by yuhyoon #+# #+# */
/* Updated: 2025/04/14 00:38:35 by hyeyeom ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int handle_putchar(t_char_state *char_state, int fd)
{
int size;
int quote_fd;
size = set_char_state(char_state->current, char_state);
if (size < 0)
{
unlink("mask_fd");
close(fd);
}
else if (size == 0)
size = putchar_quote_state_zero(char_state->current, fd);
else if (size > 0)
{
quote_fd = store_quote_seq(char_state, size);
read_store_fd(fd, quote_fd);
}
return (size);
}
int write_replace_spaces(char *s, int replace_fd)
{
t_char_state char_state;
int i;
init_char_state(&char_state, s);
i = 0;
while (*(char_state.current) != '\0')
{
i = handle_putchar(&char_state, replace_fd);
if (i > 0)
char_state.current += i;
else
break ;
}
if (*(char_state.current) == '\0')
ft_putchar_fd('\n', replace_fd);
return (i);
}
char *read_maskfd(int mask_fd, int *len)
{
struct stat st;
char *mask;
fstat(mask_fd, &st);
*len = (int)(st.st_size);
if (*len == 0)
return (NULL);
mask = malloc(sizeof(char) * ((*len) + 1));
ft_memset(mask, 0, (*len) + 1);
read(mask_fd, mask, *len);
return (mask);
}
char *generate_mask(t_minish *sh)
{
int mask_fd;
char *mask;
int syntax_result;
int len;
mask_fd = open("mask_fd", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (mask_fd == -1 || write_replace_spaces(sh->src, mask_fd) < 0)
{
err_syntax(2);
return (NULL);
}
close(mask_fd);
mask_fd = open("mask_fd", O_RDONLY);
mask = read_maskfd(mask_fd, &len);
syntax_result = syntax_analysis(mask, &len);
sh->mask = syntax_result2(syntax_result, mask);
if (sh->mask == NULL)
mask = NULL;
close(mask_fd);
unlink("mask_fd");
return (mask);
}