forked from npat-efault/picocom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.c
236 lines (213 loc) · 6.65 KB
/
split.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
/* vi: set sw=4 ts=4:
*
* split.c
*
* Function that splits a string intro arguments with quoting.
*
* by Nick Patavalis ([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 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
*/
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "split.h"
/* Lexer error end-codes */
enum err_codes {
ERR_OK = 0, /* no error, string lexed ok */
ERR_BS_AT_EOS, /* backslash at the end of string */
ERR_SQ_OPEN_AT_EOS, /* single-quote left open */
ERR_DQ_OPEN_AT_EOS /* double-quote left open */
};
/* Lexer states */
enum states {
ST_DELIM,
ST_QUOTE,
ST_ARG,
ST_END
};
/* Special characters */
#define BS '\\'
#define SQ '\''
#define DQ '\"'
#define NL '\n'
#define EOS '\0'
#define is_delim(c) \
( (c) == ' ' || (c) == '\t' || (c) == '\n' )
#define is_dq_escapable(c) \
( (c) == '\\' || (c) == '\"' || (c) == '`' || (c) == '$' )
/* Short-hands used in split_quoted() */
#define push() \
do { \
char *arg; \
if ( *argc < argv_sz ) { \
*ap = '\0'; \
arg = strdup(arg_buff); \
/* !! out of mem !! */ \
if ( ! arg ) return -1; \
argv[*argc] = arg; \
(*argc)++; \
} else { \
flags |= SPLIT_DROP; \
} \
ap = &arg_buff[0]; \
} while(0)
#define save() \
do { \
if (ap != ae) { \
*ap++ = *c; \
} else { \
flags |= SPLIT_TRUNC; \
} \
} while (0)
int
split_quoted (const char *s, int *argc, char *argv[], int argv_sz)
{
char arg_buff[MAX_ARG_LEN]; /* current argument buffer */
char *ap, *ae; /* arg_buff current ptr & end-guard */
const char *c; /* current input charcter ptr */
char qc; /* current quote character */
enum states state; /* current state */
enum err_codes err; /* error end-code */
int flags; /* warning flags */
ap = &arg_buff[0];
ae = &arg_buff[MAX_ARG_LEN - 1];
c = &s[0];
state = ST_DELIM;
err = ERR_OK;
flags = 0;
qc = SQ; /* silence compiler waring */
while ( state != ST_END ) {
switch (state) {
case ST_DELIM:
while ( is_delim(*c) ) c++;
if ( *c == SQ || *c == DQ ) {
qc = *c; c++; state = ST_QUOTE;
break;
}
if ( *c == EOS ) {
state = ST_END;
break;
}
if ( *c == BS ) {
c++;
if ( *c == NL ) {
c++;
break;
}
if ( *c == EOS ) {
state = ST_END; err = ERR_BS_AT_EOS;
break;
}
}
/* All other cases incl. character after BS */
save(); c++; state = ST_ARG;
break;
case ST_QUOTE:
while ( *c != qc && ( *c != BS || qc == SQ ) && *c != EOS ) {
save(); c++;
}
if ( *c == qc ) {
c++; state = ST_ARG;
break;
}
if ( *c == BS ) {
assert (qc == DQ);
c++;
if ( *c == NL) {
c++;
break;
}
if (*c == EOS) {
state = ST_END; err = ERR_BS_AT_EOS;
break;
}
if ( ! is_dq_escapable(*c) ) {
c--; save(); c++;
}
save(); c++;
break;
}
if ( *c == EOS ) {
state = ST_END; err = ERR_SQ_OPEN_AT_EOS;
break;
}
assert(0);
case ST_ARG:
if ( *c == SQ || *c == DQ ) {
qc = *c; c++; state = ST_QUOTE;
break;
}
if ( is_delim(*c) || *c == EOS ) {
push();
state = (*c == EOS) ? ST_END : ST_DELIM;
c++;
break;
}
if ( *c == BS ) {
c++;
if ( *c == NL ) {
c++;
break;
}
if ( *c == EOS ) {
state = ST_END; err = ERR_BS_AT_EOS;
break;
}
}
/* All other cases, incl. character after BS */
save(); c++;
break;
default:
assert(0);
}
}
return ( err != ERR_OK ) ? -1 : flags;
}
/**********************************************************************/
#if 0
int
main (int argc, char *argv[])
{
char *my_argv[12];
int my_argc, i, r;
if ( argc != 2 ) {
printf("Usage is: %s: <string to split>\n", argv[0]);
exit(EXIT_FAILURE);
}
printf("String to split is: [%s]\n", argv[1]);
r = split_quoted(argv[1], &my_argc, my_argv, 12);
if ( r < 0 ) {
printf("Spliting failed!\n");
exit(EXIT_FAILURE);
}
printf("Split ok. SPLIT_DROP is %s, SPLIT_TRUNC is %s\n",
(r & SPLIT_DROP) ? "ON" : "off",
(r & SPLIT_TRUNC) ? "ON" : "off");
for (i = 0; i < my_argc; i++)
printf("%02d : [%s]\n", i, my_argv[i]);
return EXIT_SUCCESS;
}
#endif
/**********************************************************************/
/*
* Local Variables:
* mode:c
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/