-
Notifications
You must be signed in to change notification settings - Fork 7
/
nargv.c
259 lines (217 loc) · 7.56 KB
/
nargv.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
//============================================================================
// Name : nargv.c
// Author : Triston J. Taylor (pc.wiz.tt@gmail.com)
// Version : 2.0
// Copyright : (C) Triston J. Taylor 2012. All Rights Reserved
//============================================================================
// Description : New Argument Vectors
//
// Parse a shell style string into an argument vector table.
//
//============================================================================
// Public Calls : nargv_free, nargv_parse, nargv_ifs
// Public Type Structs: NARGV
// Public Data: None
//============================================================================
#include <stddef.h>
#include <stdlib.h>
const char *NARGV_IFS = " \t\n";
typedef struct NARGV {
char **argv, *data, *error_message;
int argc, data_length, error_index, error_code;
} NARGV;
void nargv_free(NARGV* props) {
free(props->data); free(props->argv);
free(props);
}
void nargv_ifs(const char *nifs) {
if (! nifs) {
NARGV_IFS = " \t\n";
} else {
NARGV_IFS = nifs;
}
}
int nargv_field_seperator(char seperator) {
const char *list = NARGV_IFS;
if (seperator) {
while (*list) {
if (*list++ == seperator) return 1;
}
return 0;
}
return 1; // null is always a field seperator
}
NARGV *nargv_parse(const char *input) {
NARGV *nvp = calloc(1, sizeof(NARGV));
if (! input) {
nvp->error_code = 1;
nvp->error_message = "cannot parse null pointer";
return nvp;
}
/* Get the input length */
long input_length = -1;
test_next_input_char:
if (input[++input_length]) goto test_next_input_char;
if (! input_length) {
nvp->error_code = 2;
nvp->error_message = "cannot parse empty input";
return nvp;
}
int composing_argument = 0;
long quote = 0;
long index;
char look_ahead;
// FIRST PASS
// discover how many elements we have, and discover how large a data buffer we need
for (index = 0; index <= input_length; index++) {
if (nargv_field_seperator(input[index])) {
if (composing_argument) {
// close the argument
composing_argument = 0;
nvp->data_length++;
}
continue;
}
if (! composing_argument) {
nvp->argc++;
composing_argument = 1;
}
switch (input[index]) {
/* back slash */
case '\\':
// If the sequence is not \' or \" or seperator copy the back slash, and
// the data
look_ahead = *(input+index+1);
if (look_ahead == '"' || look_ahead == '\'' || nargv_field_seperator(look_ahead)) {
index++;
} else {
index++;
nvp->data_length++;
}
break;
/* double quote */
case '"':
quote = index;
while (input[++index] != '"') {
switch (input[index]) {
case '\0':
nvp->error_index = quote + 1;
nvp->error_code = 3;
nvp->error_message = "unterminated double quote";
return nvp;
break;
case '\\':
look_ahead = *(input + index + 1);
if (look_ahead == '"') {
index++;
} else {
index++;
nvp->data_length++;
}
break;
}
nvp->data_length++;
}
continue;
break;
/* single quote */
case '\'':
/* copy single quoted data */
quote = index; // QT used as temp here...
while (input[++index] != '\'') {
if (! input[index]) {
// unterminated double quote @ input
nvp->error_index = quote + 1;
nvp->error_code = 4;
nvp->error_message = "unterminated single quote";
return nvp;
}
nvp->data_length++;
}
continue;
break;
}
// "record" the data
nvp->data_length++;
}
// +1 for extra NULL pointer required by execv() and friends
nvp->argv = calloc(nvp->argc + 1, sizeof(char *));
nvp->data = calloc(nvp->data_length, 1);
// SECOND PASS
composing_argument = 0;
quote = 0;
int data_index = 0;
int arg_index = 0;
for (index = 0; index <= input_length; index++) {
if (nargv_field_seperator(input[index])) {
if (composing_argument) {
composing_argument = 0;
nvp->data[data_index++] = '\0';
}
continue;
}
if (! composing_argument) {
nvp->argv[arg_index++] = (nvp->data + data_index);
composing_argument = 1;
}
switch (input[index]) {
/* back slash */
case '\\':
// If the sequence is not \' or \" or field seperator copy the backslash
look_ahead = *(input+index+1);
if (look_ahead == '"' || look_ahead == '\'' || nargv_field_seperator(look_ahead)) {
index++;
} else {
nvp->data[data_index++] = input[index++];
}
break;
/* double quote */
case '"':
while (input[++index] != '"') {
if (input[index] == '\\') {
look_ahead = *(input + index + 1);
if (look_ahead == '"') {
index++;
} else {
nvp->data[data_index++] = input[index++];
}
}
nvp->data[data_index++] = input[index];
}
continue;
break;
/* single quote */
case '\'':
/* copy single quoted data */
while (input[++index] != '\'') {
nvp->data[data_index++] = input[index];
}
continue;
break;
}
// "record" the data
nvp->data[data_index++] = input[index];
}
return nvp;
}
#ifdef NARGVTEST
#include <stdio.h>
int main(int argc, char *argv[]) {
int arg;
char line_in[4096];
while (fgets(line_in, 4096, stdin) == line_in) {
NARGV *nargv = nargv_parse(line_in);
if (nargv->error_code) {
printf("\nnargv parse error: %i: %s: at input column %i\n",
nargv->error_code, nargv->error_message, nargv->error_index);
} else {
printf("\nNARGV Argument Count: %i\n", nargv->argc);
printf("NARGV Data Length: %i\n\n", nargv->data_length);
for (arg = 0; arg < nargv->argc; arg++) {
printf("argument %i: %s\n", arg, nargv->argv[arg]);
}
}
nargv_free(nargv);
}
}
#endif