-
Notifications
You must be signed in to change notification settings - Fork 1
/
lexer3.c
98 lines (90 loc) · 1.75 KB
/
lexer3.c
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
#include <stdio.h>
#include "libft/libft.h"
#include "parse.h"
int lex_check_redirection_with_fd(t_parse_buffer *buf, t_token *result)
{
int i;
int ch;
int fd;
i = 0;
while (i < result->length)
{
if (!ft_isdigit(result->text[i]))
return (0);
i++;
}
result->text[i] = '\0';
ch = lex_getc(buf);
if ((ch == '<' || ch == '>') && !is_int_overflow(result->text, 1))
{
fd = ft_atoi(result->text);
lex_get_symbols(buf, result, ch);
result->length = fd;
}
else
lex_ungetc(buf);
return (1);
}
int lex_escaped(t_parse_buffer *buf, t_token *result)
{
char ch;
ch = lex_getc(buf);
if (ch == '\\')
{
ch = lex_getc(buf);
if (ch != '\n'
&& (buf->lex_stat == LEXSTAT_NORMAL
|| (buf->lex_stat == LEXSTAT_DOUBLE_QUOTED
&& ft_strchr("$\"\'\\`", ch))))
{
result->text[0] = ch;
result->length = 1;
result->type = TOKTYPE_NON_EXPANDABLE;
return (1);
}
lex_ungetc(buf);
result->text[0] = '\\';
result->length = 1;
result->type = TOKTYPE_NON_EXPANDABLE;
return (1);
}
lex_ungetc(buf);
return (0);
}
int lex_get_eof(t_token *result, int ch)
{
if (ch == EOF)
{
result->type = TOKTYPE_EOF;
return (1);
}
return (0);
}
int lex_init_token(t_token *result)
{
result->max_length = 1024;
result->text = malloc(result->max_length);
if (!result->text)
{
printf("malloc token buffer failed\n");
exit(1);
}
result->length = 0;
result->type = TOKTYPE_PARSE_ERROR;
return (0);
}
int lex_expand_text_buf(t_token *result)
{
char *old_text;
old_text = result->text;
result->text = malloc(result->max_length * 2);
if (!result->text)
{
printf("expand token buffer failed\n");
exit(1);
}
ft_memcpy(result->text, old_text, result->max_length);
result->max_length = result->max_length * 2;
free(old_text);
return (0);
}