blob: 6c6fe943dd12261e367983b934a6173566fca569 (
plain)
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
|
#include <ctype.h>
#include "util.h"
static char *skpspc(char *ptr)
{
while (*ptr == ' ' || *ptr == '\t')
++ptr;
return ptr;
}
static int xdigit(int x)
{
if (isupper(x))
return x - 'A' + 0x0A;
if (islower(x))
return x - 'a' + 0x0A;
return x - '0';
}
static char *parse_str(char *src)
{
char *dst = src;
int c;
for (;;) {
c = *(src++);
switch (c) {
case '\\':
c = *(src++);
switch (c) {
case 'a': c = '\a'; break;
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 't': c = '\t'; break;
case '\\': break;
case '"': break;
case 'x':
c = 0;
if (isxdigit(*src))
c = (c << 4) | xdigit(*(src++));
if (isxdigit(*src))
c = (c << 4) | xdigit(*(src++));
break;
case '0':
c = 0;
if (isdigit(*src) && *src < '8')
c = (c << 3) | (*(src++) - '0');
if (isdigit(*src) && *src < '8')
c = (c << 3) | (*(src++) - '0');
if (isdigit(*src) && *src < '8')
c = (c << 3) | (*(src++) - '0');
break;
default:
return NULL;
}
break;
case '"':
*(dst++) = '\0';
goto out;
}
*(dst++) = c;
}
out:
return src;
}
int splitkv(char *line, char **key, char **value)
{
*key = NULL;
*value = NULL;
line = skpspc(line);
if (*line == '#' || *line == '\0')
return 0;
if (!isalpha(*line))
return -1;
*key = line;
while (isalnum(*line))
++line;
if (*line == ' ' || *line == '\t') {
*(line++) = '\0';
line = skpspc(line);
}
if (*line != '=')
return -1;
*(line++) = '\0';
line = skpspc(line);
if (*line == '"') {
++line;
*value = line;
line = parse_str(line);
} else if (isalnum(*line)) {
*value = line;
while (isalnum(*line) || *line == '.' || *line == '_')
++line;
if (*line != '\0')
*(line++) = '\0';
} else {
return -1;
}
line = skpspc(line);
if (*line != '\0' && *line != '#')
return -1;
return 0;
}
|