-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparser.c
234 lines (189 loc) · 4.96 KB
/
parser.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
#include <stdarg.h>
#include <ctype.h>
#include <dlfcn.h>
#include "generator.h"
#include "parser.h"
/** Return address of function referenced in traffic file
* @retval NULL invalid function
*/
static int (*funcname2ptr(const char *funcname))()
{
static void *me = NULL;
static char buf[128];
/* a trick for conversion void* -> int (*fun)() */
static union {
void *ptr;
int (*fun)();
} void2func;
if (!me)
me = dlopen(NULL, RTLD_NOW | RTLD_GLOBAL);
snprintf(buf, sizeof buf, "fun_%s", funcname);
void2func.ptr = dlsym(me, buf);
return void2func.fun;
}
struct mgp_line *mgp_parse_line(mmatic *mm, const char *line, int argmax, char **rest, char **errmsg, ...)
{
struct mgp_line *ret;
struct mgp_arg *arg;
va_list vl; const char *argname;
int argnum, argc, i, j;
bool inq, end = false;
int inp;
char *l, *fname, *fargs;
tlist *mapping;
ret = mmatic_zalloc(mm, sizeof *ret);
ret->args = thash_create_strkey(NULL, mm);
ret->mm = mm;
mapping = tlist_create(NULL, mm);
l = mmatic_strdup(mm, line);
/* read the mapping */
va_start(vl, errmsg);
while ((argname = va_arg(vl, const char *)))
tlist_push(mapping, argname);
va_end(vl);
/* read the line */
i = 0;
argc = 1;
for (argnum = 1; l[i] && (argmax == 0 || argnum <= argmax); argnum++) {
arg = mmatic_zalloc(mm, sizeof *arg);
arg->line = ret;
/* check if the argument has a name= prefix */
inq = false;
inp = 0;
for (j = i; l[j]; j++) {
if (l[j] == '"' && (j == 0 || l[j-1] != '\\')) {
if (inq) { j++; break; }
else { inq = true; continue; }
}
if (inq) continue;
if (l[j] == '(') { inp++; continue; }
else if (inp && l[j] == ')') { inp--; continue; }
if (l[j] == ',') l[j] = ' ';
if (inp) continue;
if (l[j] == '=') break;
if (isspace(l[j])) break;
}
if (isspace(l[j]) || !l[j]) { /* =argument without a name */
/* get key from mapping */
arg->name = tlist_shift(mapping);
if (!arg->name) {
arg->name = mmatic_sprintf(mm, "arg%d", argc++);
}
if (!l[j])
end = true;
/* treat i->j part as value */
if (inq) {
l[j-1] = '\0';
arg->as_string = l + i + 1;
} else {
l[j] = '\0';
arg->as_string = l + i;
}
i = j + 1;
} else if (l[j] == '=') { /* = argument with name */
/* treat i->j part as key */
if (inq) {
l[j-1] = '\0';
arg->name = l + i + 1;
} else {
l[j] = '\0';
arg->name = l + i;
}
i = j + 1;
/* read the value */
inq = false;
inp = 0;
for (j = i; l[j]; j++) {
if (l[j] == '"' && l[j-1] != '\\') {
if (inq) { j++; break; }
else { inq = true; continue; }
}
if (inq) continue;
if (l[j] == '(') { inp++; continue; }
else if (inp && l[j] == ')') { inp--; continue; }
if (l[j] == ',') l[j] = ' ';
if (inp) continue;
if (isspace(l[j])) break;
}
if (!l[j])
end = true;
l[j] = '\0';
arg->as_string = l + i;
i = j + 1;
} else {
if (errmsg)
*errmsg = mmatic_sprintf(mm, "invalid character in argument number %d: '%c'", argnum, l[j]);
return NULL;
}
/* XXX: now argument name is in arg->name and its value in arg->as_string */
/* check if is a function */
if (pjf_match("/^[^ ]+\\(.*\\)$/", arg->as_string) > 0) {
arg->isfunc = true;
/* split into function name and its args */
fname = mmatic_strdup(mm, arg->as_string);
fargs = strchr(fname, '(');
*fargs++ = '\0';
fargs[strlen(fargs) - 1] = '\0';
/* set fptr */
arg->fptr = funcname2ptr(fname);
if (!arg->fptr) {
if (errmsg)
*errmsg = mmatic_sprintf(mm, "invalid function name in argument number %d: '%s'",
argnum, fname);
return NULL;
}
/* parse args recursively */
arg->fargs = mgp_parse_line(mm, fargs, 0, NULL, errmsg, NULL);
} else {
arg->isfunc = false;
}
/* store the argument */
thash_set(ret->args, arg->name, arg);
/* go to next statement */
if (end) break;
while (isspace(l[i])) i++;
}
if (rest)
*rest = l + i;
return ret;
}
/*
* Fetch
*/
/** Fetch given argument, creating it if it does not exist */
static struct mgp_arg *fetch(struct mgp_line *l, const char *name)
{
struct mgp_arg *arg;
arg = thash_get(l->args, name);
if (!arg) {
arg = mmatic_zalloc(l->mm, sizeof *arg);
arg->name = mmatic_strdup(l->mm, name);
thash_set(l->args, name, arg);
}
return arg;
}
struct mgp_arg *mgp_prepare_int(struct mgp_line *l, const char *name, int defval)
{
struct mgp_arg *arg = fetch(l, name);
if (!arg->as_string)
arg->as_int = defval;
else if (!arg->isfunc)
arg->as_int = atoi(arg->as_string);
return arg;
}
struct mgp_arg *mgp_prepare_string(struct mgp_line *l, const char *name, const char *defval)
{
struct mgp_arg *arg = fetch(l, name);
if (!arg->as_string)
arg->as_string = mmatic_strdup(l->mm, defval);
return arg;
}
struct mgp_arg *mgp_prepare_float(struct mgp_line *l, const char *name, float defval)
{
struct mgp_arg *arg = fetch(l, name);
if (!arg->as_string)
arg->as_float = defval;
else
arg->as_float = strtof(arg->as_string, NULL);
return arg;
}