-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixtoken.c
291 lines (236 loc) · 6.46 KB
/
fixtoken.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
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
/* fixtoken - extract tokens from a string
Version 1.1
Version 1.2 20090401
remove the safe_strncpy() function; make test program easier
add mkargv(), a simple command line parser
Copyright (C) 1998-2009 Xuming <[email protected]>
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, 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, the file COPYING in this directory, 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, 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <ctype.h>
#include <stdio.h>
#include <string.h>
/* isspace() macro has a problem in Cygwin when compiling it with -mno-cygwin.
* I assume it is caused by minGW because it works fine with cygwin head files.
* The problem is it treats some Chinese characters as space characters.
* A sample is: 0xC5 0xF3 0xD3 0xD1 */
#define IsSpace(c) ((((c) >= 9) && ((c) <= 0xd)) || ((c) == 0x20))
#define IsPipe(c) (((c) == '>') || ((c) == '<') || ((c) == '|'))
static int isdelim(char *delim, int ch)
{
while (*delim) {
if (*delim == (char) ch) {
return 1;
} else if ((*delim == ' ') && IsSpace(ch)) {
return 1;
}
delim++;
}
return 0;
}
/* This function splits the string into tokens. It extracts everything
between delimiter.
sour - the input string
idx - the string array for storing tokens
ids - the maximem number of tokens, the size of "idx"
delim - the delimiter array, each character in the array is a delimiter.
It returns the number of extracted tokens.
For example, fixtoken("#abc wdc:have:::#:debug", idx, 16, "# :") returns
10 tokens "", "abc", "", "wdc", "have", "", "", "", "" and "debug".
NOTE: 'sour' will be changed.
*/
int fixtoken(char *sour, char **idx, int ids, char *delim)
{
int i;
for (i = 0; i < ids; idx[i++] = NULL);
i = 0;
for (idx[i++] = sour; *sour && (i < ids); sour++) {
if (isdelim(delim, *sour)) {
*sour = 0;
idx[i++] = sour + 1;
}
}
return i;
}
/* This function splits the string into tokens. It extracts everything
between delimiter.
Unlike fixtoken(), it treats continous delimiters as one single delimter.
sour - the input string
idx - the string array for storing tokens
ids - the maximem number of tokens, the size of "idx"
delim - the delimiter array, each character in the array is a delimiter.
It returns the number of token extracted.
For example, fixtoken("#abc wdc:have:::#:debug", idx, 16, "# :") returns
4 tokens "abc", "wdc", "have" and "debug".
NOTE: 'sour' will be changed.
*/
int ziptoken(char *sour, char **idx, int ids, char *delim)
{
int i, ss;
for (i = 0; i < ids; idx[i++] = NULL);
for (i = ss = 0; *sour && (i < ids); sour++) {
if (isdelim(delim, *sour)) {
ss = 0;
*sour = 0;
} else if (ss == 0) {
ss = 1;
idx[i++] = sour;
}
}
return i;
}
#define MK_ARG_DELIM 0
#define MK_ARG_TOKEN 1
#define MK_ARG_SGL_QU 2
#define MK_ARG_DBL_QU 3
#define MK_ARG_PIPE 4
/* mkargv - make argument list
This function splits the string into an argument list in the traditional
(int argc, char *argv[]) form. It uses a very simple state machine so you
couldn't expect too much features from here.
sour - the input string
idx - the string array for storing tokens
ids - the maximem number of tokens, the size of "idx"
It returns the number of token extracted.
NOTE: 'sour' will be changed.
*/
int mkargv(char *sour, char **idx, int ids)
{
int i = 0, stat;
stat = MK_ARG_DELIM;
while (*sour) {
switch (stat) {
case MK_ARG_DELIM: /* last char is the delimiter */
if (IsSpace(*sour)) {
*sour = 0;
} else if (*sour == '\'') {
*sour = 0;
idx[i++] = sour + 1;
stat = MK_ARG_SGL_QU;
} else if (*sour == '"') {
*sour = 0;
idx[i++] = sour + 1;
stat = MK_ARG_DBL_QU;
} else if (IsPipe(*sour)) {
*sour = 0;
stat = MK_ARG_PIPE;
} else {
idx[i++] = sour;
stat = MK_ARG_TOKEN;
}
break;
case MK_ARG_TOKEN:
if (IsSpace(*sour)) {
*sour = 0;
stat = MK_ARG_DELIM;
} else if (*sour == '\'') {
*sour = 0;
idx[i++] = sour + 1;
stat = MK_ARG_SGL_QU;
} else if (*sour == '"') {
*sour = 0;
idx[i++] = sour + 1;
stat = MK_ARG_DBL_QU;
} else if (IsPipe(*sour)) {
*sour = 0;
stat = MK_ARG_PIPE;
}
break;
case MK_ARG_SGL_QU:
if (*sour == '\'') {
*sour = 0;
stat = MK_ARG_DELIM;
}
break;
case MK_ARG_DBL_QU:
if (*sour == '"') {
*sour = 0;
stat = MK_ARG_DELIM;
}
break;
}
if (stat == MK_ARG_PIPE) {
break;
}
if (i >= ids) {
i = ids - 1; /* the last argment must be NULL */
break;
}
sour++;
}
idx[i] = NULL;
return i;
}
#ifdef EXECUTE_FIXTOKEN
/* make: gcc -Wall -DEXECUTE_FIXTOKEN -o token fixtoken.c */
struct {
char *delim;
char *content;
} testbl[] = {
{ "# :", "#abc wdc:have:::#:debug" },
{ " ", " abc bcd 'sad str sf ' sdf > asdf" },
{ NULL, NULL }
};
int test_print_token(char *content, char *delim)
{
char buf[256], *argv[32];
int i, argc;
printf("PARSING {%s} by {%s}\n", content, delim);
strcpy(buf, content);
argc = fixtoken(buf, argv, 32, delim);
printf("FIXTOKEN: ");
for (i = 0; i < argc; i++) {
printf("{%s} ", argv[i]);
}
printf("\n");
strcpy(buf, content);
argc = ziptoken(buf, argv, 32, delim);
printf("ZIPTOKEN: ");
for (i = 0; i < argc; i++) {
printf("{%s} ", argv[i]);
}
printf("\n");
strcpy(buf, content);
argc = mkargv(buf, argv, 32);
printf("MKARGV: ");
for (i = 0; i < argc; i++) {
printf("{%s} ", argv[i]);
}
printf("\n\n");
return 0;
}
int main(int argc, char **argv)
{
int i;
char buf[256];
if (argc < 2) {
for (i = 0; testbl[i].delim; i++) {
test_print_token(testbl[i].content, testbl[i].delim);
}
return 0;
}
printf("Press Ctrl-D or 'quit' command to quit.\n");
while (1) {
printf("IN> ");
if (fgets(buf, 256, stdin) == NULL) {
break;
}
buf[strlen(buf) - 1] = 0;
if (!strcmp(buf, "quit") || !strcmp(buf, "exit")) {
break;
}
test_print_token(buf, " ");
}
return 0;
}
#endif