-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlexer.c
265 lines (227 loc) · 4.7 KB
/
lexer.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#include <glob.h>
#include <string.h>
#include <stdlib.h>
#include "lexer.h"
int getchartype(char c)
{
switch(c)
{
case '\'':
return CHAR_QOUTE;
break;
case '\"':
return CHAR_DQUOTE;
break;
case '|':
return CHAR_PIPE;
break;
case '&':
return CHAR_AMPERSAND;
break;
case ' ':
return CHAR_WHITESPACE;
break;
case ';':
return CHAR_SEMICOLON;
break;
case '\\':
return CHAR_ESCAPESEQUENCE;
break;
case '\t':
return CHAR_TAB;
break;
case '\n':
return CHAR_NEWLINE;
break;
case '>':
return CHAR_GREATER;
break;
case '<':
return CHAR_LESSER;
break;
case 0:
return CHAR_NULL;
break;
default:
return CHAR_GENERAL;
};
return CHAR_GENERAL;
}
void strip_quotes(char* src, char* dest)
{
int n = strlen(src);
if (n <= 1) {
strcpy(dest, src);
return;
}
int i;
char lastquote = 0;
int j = 0;
for (i=0; i < n; i++)
{
char c = src[i];
if ((c == '\'' || c == '\"') && lastquote == 0)
lastquote = c;
else if (c == lastquote)
lastquote = 0;
else
dest[j++] = c;
}
dest[j] = 0;
}
void tok_init(tok_t* tok, int datasize)
{
tok->data = malloc(datasize + 1); // 1 for null terminator
tok->data[0] = 0;
tok->type = CHAR_NULL;
tok->next = NULL;
}
void tok_destroy(tok_t* tok) {
if (tok != NULL) {
free(tok->data);
tok_destroy(tok->next);
free(tok);
}
}
int lexer_build(char* input, int size, lexer_t* lexerbuf)
{
if (lexerbuf == NULL)
return -1;
if (size == 0) {
lexerbuf->ntoks = 0;
return 0;
}
lexerbuf->llisttok = malloc(sizeof(tok_t));
// allocate the first token
tok_t* token = lexerbuf->llisttok;
tok_init(token, size);
int i = 0;
int j = 0, ntemptok = 0;
char c;
int state = STATE_GENERAL;
do
{
c = input[i];
int chtype = getchartype(c);
if (state == STATE_GENERAL)
{
switch (chtype)
{
case CHAR_QOUTE:
state = STATE_IN_QUOTE;
token->data[j++] = CHAR_QOUTE;
token->type = TOKEN;
break;
case CHAR_DQUOTE:
state = STATE_IN_DQUOTE;
token->data[j++] = CHAR_DQUOTE;
token->type = TOKEN;
break;
case CHAR_ESCAPESEQUENCE:
token->data[j++] = input[++i];
token->type = TOKEN;
break;
case CHAR_GENERAL:
token->data[j++] = c;
token->type = TOKEN;
break;
case CHAR_WHITESPACE:
if (j > 0) {
token->data[j] = 0;
token->next = malloc(sizeof(tok_t));
token = token->next;
tok_init(token, size - i);
j = 0;
}
break;
case CHAR_SEMICOLON:
case CHAR_GREATER:
case CHAR_LESSER:
case CHAR_AMPERSAND:
case CHAR_PIPE:
// end the token that was being read before
if (j > 0) {
token->data[j] = 0;
token->next = malloc(sizeof(tok_t));
token = token->next;
tok_init(token, size - i);
j = 0;
}
// next token
token->data[0] = chtype;
token->data[1] = 0;
token->type = chtype;
token->next = malloc(sizeof(tok_t));
token = token->next;
tok_init(token, size - i);
break;
}
}
else if (state == STATE_IN_DQUOTE) {
token->data[j++] = c;
if (chtype == CHAR_DQUOTE)
state = STATE_GENERAL;
}
else if (state == STATE_IN_QUOTE) {
token->data[j++] = c;
if (chtype == CHAR_QOUTE)
state = STATE_GENERAL;
}
if (chtype == CHAR_NULL) {
if (j > 0) {
token->data[j] = 0;
ntemptok++;
j = 0;
}
}
i++;
} while (c != '\0');
token = lexerbuf->llisttok;
int k = 0;
while (token != NULL)
{
if (token->type == TOKEN)
{
glob_t globbuf;
glob(token->data, GLOB_TILDE, NULL, &globbuf);
if (globbuf.gl_pathc > 0)
{
k += globbuf.gl_pathc;
// save the next token so we can attach it later
tok_t* saved = token->next;
// replace the current token with the first one
free(token->data);
token->data = malloc(strlen(globbuf.gl_pathv[0]) + 1);
strcpy(token->data, globbuf.gl_pathv[0]);
int i;
for (i = 1; i < globbuf.gl_pathc; i++)
{
token->next = malloc(sizeof(tok_t));
tok_init(token->next, strlen(globbuf.gl_pathv[i]));
token = token->next;
token->type = TOKEN;
strcpy(token->data, globbuf.gl_pathv[i]);
}
token->next = saved;
}
else {
// token from the user might be inside quotation to escape special characters
// hence strip the quotation symbol
char* stripped = malloc(strlen(token->data) + 1);
strip_quotes(token->data, stripped);
free(token->data);
token->data = stripped;
k++;
}
}
token = token->next;
}
lexerbuf->ntoks = k;
return k;
}
void lexer_destroy(lexer_t* lexerbuf)
{
if (lexerbuf == NULL)
return;
tok_destroy(lexerbuf->llisttok);
}