-
Notifications
You must be signed in to change notification settings - Fork 2
/
cmdparser.h
303 lines (291 loc) · 6.61 KB
/
cmdparser.h
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/***************************************************************************
* cmdparser.h
*
* Copyright 2005 Dimitur Kirov
* dkirov@gmail.com
****************************************************************************/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef _CMDPARSER_H
#define _CMDPARSER_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdio.h>
#include <stddef.h>
#include "defines.h"
enum {
CMD_USER,
CMD_PASS,
CMD_ACCT,
CMD_CWD,
CMD_CDUP,
CMD_SMNT,
CMD_QUIT,
CMD_REIN,
CMD_PORT,
CMD_PASV,
CMD_TYPE,
CMD_STRU,
CMD_MODE,
CMD_RETR,
CMD_STOR,
CMD_STOU,
CMD_APPE,
CMD_ALLO,
CMD_REST,
CMD_RNFR,
CMD_RNTO,
CMD_ABOR,
CMD_DELE,
CMD_RMD,
CMD_MKD,
CMD_PWD,
CMD_LIST,
CMD_NLST,
CMD_SITE,
CMD_SYST,
CMD_STAT,
CMD_HELP,
CMD_NOOP,
CMD_UNKNOWN,
CMD_EMPTY,
CMD_CLOSE
};
/*
Statichni funkcii za optimalna obrabotka na vhodnie komandi
*/
static void set_data(char *src,char *dest) {
//dest[0] = '\0';
int i=0,y=0;
bool hasbegun = FALSE;
for(;;i++) {
if(hasbegun==TRUE) {
dest[y]=src[i];
if(src[i]=='\0' || src[i]=='\n' || src[i]=='\r')
break;
y++;
} else {
if(src[i]==' ' || src[i]==' ')
continue;
else {
dest[0]=src[i];
y=1;
hasbegun=TRUE;
}
}
}
dest[y]='\0';
}
static bool cmp2(char c1,char c2,char d1,char d2) {
if(c1==d1 && c2 == d2)
return TRUE;
return FALSE;
}
static bool cmp3(char c1,char c2,char c3,char d1,char d2,char d3) {
if(cmp2(c1,c2,d1,d2) && c3==d3)
return TRUE;
return FALSE;
}
static bool _cmp2(char c1,char c2,char *d,char *d1) {
if(d[0]=='\0')
return FALSE;
if(d[1]=='\0')
return FALSE;
if(!cmp2(c1,c2,d[0],d[1]))
return FALSE;
set_data(d+2,d1);
return TRUE;
}
static bool _cmp1(char c1,char *d,char *d1) {
if(c1 != d[0])
return FALSE;
set_data(d+1,d1);
return TRUE;
}
static bool _cmp3(char c1,char c2,char c3,char *d,char *d1) {
if(d[0]=='\0')
return FALSE;
if(d[1]=='\0')
return FALSE;
if(d[2]=='\0')
return FALSE;
if(!cmp3(c1,c2,c3,d[0],d[1],d[2]))
return FALSE;
set_data(d+3,d1);
return TRUE;
}
static int parse_input(char *input_buff,char *data_buff) {
if(input_buff==NULL)
return CMD_EMPTY;
//To avoid Warning "not used"
parse_input(NULL,NULL);
int len = strlen(input_buff);
if(len<3)
return CMD_UNKNOWN;
switch(input_buff[0]) {
case 'A':
switch(input_buff[1]) {
case 'B':
if(_cmp2('O','R',input_buff+2,data_buff))
return CMD_ABOR;
case 'C':
if(_cmp2('C','T',input_buff+2,data_buff))
return CMD_ACCT;
case 'L':
if(_cmp2('L','O',input_buff+2,data_buff))
return CMD_ALLO;
case 'P':
if(_cmp2('P','E',input_buff+2,data_buff))
return CMD_APPE;
return CMD_UNKNOWN;
}
case 'C':
if(_cmp2('W','D',input_buff+1,data_buff))
return CMD_CWD;
else if(_cmp3('D','U','P',input_buff+1,data_buff))
return CMD_CDUP;
return CMD_UNKNOWN;
case 'D':
if(_cmp3('E','L','E',input_buff+1,data_buff))
return CMD_DELE;
return CMD_UNKNOWN;
case 'H':
if(_cmp3('E','L','P',input_buff+1,data_buff))
return CMD_HELP;
return CMD_UNKNOWN;
case 'L':
if(_cmp3('I','S','T',input_buff+1,data_buff))
return CMD_LIST;
return CMD_UNKNOWN;
case 'M':
switch(input_buff[1]) {
case 'K':
if(_cmp1('D',input_buff+2,data_buff))
return CMD_MKD;
case 'O':
if(_cmp2('D','E',input_buff+2,data_buff))
return CMD_MODE;
return CMD_UNKNOWN;
}
case 'N':
switch(input_buff[1]) {
case 'L':
if(_cmp2('S','T',input_buff+2,data_buff))
return CMD_NLST;
case 'O':
if(_cmp2('O','P',input_buff+2,data_buff))
return CMD_NOOP;
return CMD_UNKNOWN;
}
case 'P':
switch(input_buff[1]) {
case 'A':
if(_cmp1('S',input_buff+2,data_buff)) {
if(_cmp1('S',input_buff+3,data_buff))
return CMD_PASS;
if(_cmp1('V',input_buff+3,data_buff))
return CMD_PASV;
return CMD_UNKNOWN;
}
case 'O':
if(_cmp2('R','T',input_buff+2,data_buff))
return CMD_PORT;
case 'W':
if(_cmp1('D',input_buff+2,data_buff))
return CMD_PWD;
return CMD_UNKNOWN;
}
case 'Q':
if(_cmp3('U','I','T',input_buff+1,data_buff))
return CMD_QUIT;
return CMD_UNKNOWN;
case 'R':
switch(input_buff[1]) {
case 'E':
switch(input_buff[2]) {
case 'I':
if(_cmp1('N',input_buff+3,data_buff))
return CMD_REIN;
case 'S':
if(_cmp1('T',input_buff+3,data_buff))
return CMD_REST;
case 'T':
if(_cmp1('R',input_buff+3,data_buff))
return CMD_RETR;
return CMD_UNKNOWN;
}
return CMD_UNKNOWN;
case 'M':
if(_cmp1('D',input_buff+2,data_buff))
return CMD_RMD;
case 'N':
switch(input_buff[2]) {
case 'F':
if(_cmp1('R',input_buff+3,data_buff))
return CMD_RNFR;
case 'T':
if(_cmp1('O',input_buff+3,data_buff))
return CMD_RNTO;
return CMD_UNKNOWN;
}
return CMD_UNKNOWN;
}
case 'S':
switch(input_buff[1]) {
case 'I':
if(_cmp2('T','E',input_buff+2,data_buff))
return CMD_SITE;
case 'M':
if(_cmp2('N','T',input_buff+2,data_buff))
return CMD_SMNT;
case 'T':
switch(input_buff[2]) {
case 'A':
if(_cmp1('T',input_buff+3,data_buff))
return CMD_STAT;
case 'O':
if(_cmp1('R',input_buff+3,data_buff))
return CMD_STOR;
if(_cmp1('U',input_buff+3,data_buff))
return CMD_STOU;
case 'R':
if(_cmp1('U',input_buff+3,data_buff))
return CMD_STRU;
}
return CMD_UNKNOWN;
case 'Y':
if(_cmp2('S','T',input_buff+2,data_buff))
return CMD_SYST;
return CMD_UNKNOWN;
}
case 'T':
if(_cmp3('Y','P','E',input_buff+1,data_buff))
return CMD_TYPE;
return CMD_UNKNOWN;
case 'U':
if(_cmp3('S','E','R',input_buff+1,data_buff))
return CMD_USER;
return CMD_UNKNOWN;
return CMD_UNKNOWN;
}
return CMD_UNKNOWN;
}
#ifdef __cplusplus
}
#endif
#endif /* _CMDPARSER_H */