-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathutil.c
95 lines (88 loc) · 3.02 KB
/
util.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
#include "util.h"
int parse_line(char *line, /* modified */
char *(*tokens)[],
size_t *count)
{
enum {
s_token_pending, /* waiting for command to begin */
s_parse_plain_token, /* parsing plain token, such as foo... */
s_parse_quoted_token, /* parsing quoted token, such as "foo... */
s_parse_aposd_token /* parsing quoted token, such as 'foo... */
} state = s_token_pending;
*count = 0;
char *start = line;
int term = 0;
while (!term) {
const char ch = *line;
switch (state) {
case s_token_pending:
switch (ch) {
case ' ':
case '\t':
case '\r':
case '\n':
case '\0':
break; /* skip white-space */
case '\"':
state = s_parse_quoted_token;
start = line + 1;
break;
case '\'':
state = s_parse_aposd_token;
start = line + 1;
break;
default:
state = s_parse_plain_token;
start = line;
break;
}
break;
case s_parse_plain_token:
switch (ch) {
case ' ':
case '\t':
case '\r':
case '\n':
case '\0':
/* end of plain token */
*line = '\0';
(*tokens)[(*count)++] = start;
state = s_token_pending;
break;
case '\'':
case '\"':
return (-1); /* quotes are not allowed in the middle of token */
}
break;
case s_parse_quoted_token:
switch (ch) {
case '\0':
return (-1); /* missing closing quotes */
case '\"':
/* end of quoted token */
*line = '\0';
(*tokens)[(*count)++] = start;
state = s_token_pending;
break;
}
break;
case s_parse_aposd_token:
switch (ch) {
case '\0':
return (-1); /* missing closing quotes */
case '\'':
/* end of quoted token */
*line = '\0';
(*tokens)[(*count)++] = start;
state = s_token_pending;
break;
}
break;
}
++line;
term = (ch == '\0' || ch == '\r' || ch == '\n');
}
if (state != s_token_pending)
return (-1);
return (0);
}