-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsyntax_check.c
More file actions
113 lines (105 loc) · 3.36 KB
/
Copy pathsyntax_check.c
File metadata and controls
113 lines (105 loc) · 3.36 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* syntax_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ojamal <ojamal@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/27 19:17:20 by ojamal #+# #+# */
/* Updated: 2023/07/18 18:54:52 by ojamal ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int check_start(t_tokens *lexer)
{
if (lexer->e_types != 1)
{
if (lexer->e_types == 0)
return (g_helper.exit_status = 258,
msg_er("syntax error near unexpected token `|'"));
}
return (0);
}
int token_check(t_tokens *lexer)
{
if (check_start(lexer))
return (1);
while (lexer)
{
if (lexer->e_types == 0 && lexer->next && (lexer->next->e_types == 6
|| lexer->next->e_types == 0))
return (g_helper.exit_status = 258,
msg_er("syntax error near unexpected token `|'"));
if (lexer->e_types == 2 && lexer->next && lexer->next->e_types != 1)
return (g_helper.exit_status = 258,
msg_er("syntax error near unexpected token `newline'"));
if (lexer->e_types == 3 && lexer->next && lexer->next->e_types != 1)
return (g_helper.exit_status = 258,
msg_er("syntax error near unexpected token `newline'"));
if (lexer->e_types == 4 && lexer->next && lexer->next->e_types != 1)
return (g_helper.exit_status = 258,
msg_er("syntax error near unexpected token `newline'"));
if (lexer->e_types == 5 && lexer->next && lexer->next->e_types != 1)
return (g_helper.exit_status = 258,
msg_er("syntax error near unexpected token `newline'"));
lexer = lexer->next;
}
return (0);
}
void process_character(char c, t_quote **stack_double,
t_quote **stack_single)
{
if (c == '\"')
{
if (*stack_single == NULL || (*stack_single)->quote != '\'')
{
if (*stack_double == NULL || (*stack_double)->quote == '\'')
push_quote(stack_double, '\"');
else
pop_quote(stack_double);
}
}
else if (c == '\'')
{
if (*stack_double == NULL || (*stack_double)->quote != '\"')
{
if (*stack_single == NULL || (*stack_single)->quote == '\"')
push_quote(stack_single, '\'');
else
pop_quote(stack_single);
}
}
}
void process_string(char *str, t_quote **stack_double,
t_quote **stack_single)
{
int i;
i = 0;
while (str[i] != '\0')
{
process_character(str[i], stack_double, stack_single);
i++;
}
}
int syntax_check(t_tokens *lexer)
{
t_quote *stack_single;
t_quote *stack_double;
char *str;
stack_single = NULL;
stack_double = NULL;
while (lexer)
{
str = (char *)lexer->val;
if (str != NULL)
process_string(str, &stack_double, &stack_single);
lexer = lexer->next;
}
if (stack_single != NULL)
return (g_helper.exit_status = 258, free(stack_single),
msg_er("Unclosed single quote: '"));
if (stack_double != NULL)
return (g_helper.exit_status = 258, free(stack_double),
msg_er("Unclosed double quote: \""));
return (0);
}