-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.c
398 lines (327 loc) · 10.8 KB
/
config.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "config.h"
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE_SIZE 255
#define INDENT_SIZE 4
#define CLASS_INDENT_LEVEL 0
#define METHOD_INDENT_LEVEL INDENT_SIZE
#define METHOD_PROPS_INDENT_LEVEL (INDENT_SIZE * 2)
// const char *DEFAULT_DISPLAY_METHOD = "toString";
// const char *DEFAULT_DISPLAY_SIG = "()Ljava/lang/String;";
typedef struct ParseState {
ClassConfig *current_class;
MethodConfig *current_method;
} ParseState;
void PrintNameAndSignature(NameAndSignature *nas) {
if (nas == NULL) {
fprintf(stderr, "NULL,");
} else {
fprintf(stderr, "{name = \"%s\", signature = \"%s\"},\n", nas->name, nas->signature);
}
}
void PrintMethodConfig(MethodConfig *methodConfig) {
fprintf(stderr, " {\n");
fprintf(stderr, " method = ");
PrintNameAndSignature(methodConfig->method);
fprintf(stderr, " parameterPosition = %d,\n", methodConfig->parameterPosition);
fprintf(stderr, " displayMethod = ");
PrintNameAndSignature(methodConfig->displayMethod);
fprintf(stderr, " staticDisplayClass = \"%s\",\n", methodConfig->staticDisplayClass);
fprintf(stderr, " showTrace = %d,\n", methodConfig->showTrace);
fprintf(stderr, " showAllParams = %d,\n", methodConfig->showAllParams);
fprintf(stderr, " displayField = ");
PrintNameAndSignature(methodConfig->displayField);
fprintf(stderr, "\n");
fprintf(stderr, " },\n");
}
void PrintClassConfig(ClassConfig *clsConfig) {
fprintf(stderr, " {\n");
fprintf(stderr, " name = \"%s\",\n", clsConfig->name);
fprintf(stderr, " runtime_attached = %d,\n", clsConfig->runtime_attached);
fprintf(stderr, " method_list = [\n");
MethodConfig *methodConfig = clsConfig->method_list;
while (methodConfig != NULL) {
PrintMethodConfig(methodConfig);
methodConfig = methodConfig->next;
}
fprintf(stderr, " ],\n");
fprintf(stderr, " },\n");
}
void PrintConfig(Config *config) {
fprintf(stderr, "{\n");
fprintf(stderr, " classes: [\n");
ClassConfig *clsConfig = config->class_list;
while (clsConfig != NULL) {
PrintClassConfig(clsConfig);
clsConfig = clsConfig->next;
}
fprintf(stderr, " ],\n");
fprintf(stderr, " runtime_all_attached = %d,\n", config->runtime_all_attached);
fprintf(stderr, " defaultDisplayMethodName = \"%s\",\n", config->defaultDisplayMethodName);
fprintf(stderr, " defaultDisplayMethodSig = \"%s\",\n", config->defaultDisplayMethodSig);
fprintf(stderr, "}\n");
}
char *trim(char *text, int *leading_spaces) {
*leading_spaces = 0;
while(isspace(*text)) {
*leading_spaces = *leading_spaces + 1;
text++;
}
if (*text == 0) {
return text;
}
char *end = text + strlen(text) - 1;
while (end > text && isspace(*end)) end--;
end[1] = '\0';
return text;
}
// A name and signature is either:
// someMethod(foo)bar
// someField:bar
bool parseNameAndSignature(char *text, NameAndSignature *nameAndSig) {
// First try breaking up by colon.
char *name;
char *sig;
bool is_method = false;
name = strtok(text, ":");
if (name != NULL) {
sig = strtok(NULL, ":");
if (sig == NULL) {
// Proceed to try as a method.
name = NULL;
}
}
if (name == NULL) {
// Break up by parenthesis
name = strtok(text, "(");
if (name == NULL) {
fprintf(stderr, "Can't find method name in %s\n", text);
return false;
}
sig = strtok(NULL, "(");
if (sig == NULL) {
fprintf(stderr, "Can't find method signature in %s\n", text);
return false;
}
is_method = true;
}
nameAndSig->name = (char *) malloc(sizeof(char) * (strlen(name) + 1));
strcpy(nameAndSig->name, name);
nameAndSig->signature = (char *) malloc(sizeof(char) * (strlen(sig) + (is_method ? 2 : 1)));
char *startOfSig = nameAndSig->signature;
if (is_method) {
nameAndSig->signature[0] = '(';
startOfSig = startOfSig + 1;
}
strcpy(startOfSig, sig);
return true;
}
bool processMethodProp(ParseState *parseState, Config *config, char *line) {
if (parseState->current_method == NULL) {
fprintf(stderr, "Found a method prop, but not in a method!\n");
return false;
}
// swallow the leading "- ". Very fragile.
line += 2;
// Break up the string based on a colon
char *key = strtok(line, ":");
if (key == NULL) {
fprintf(stderr, "Expecting key: value\n");
return false;
}
char *value = strtok(NULL, "");
if (value == NULL) {
fprintf(stderr, "Expecting value\n");
return false;
}
int ignored;
value = trim(value, &ignored);
if (strcmp("displayMethod", key) == 0) {
NameAndSignature *displayMethod = parseState->current_method->displayMethod;
displayMethod->name = (char *) malloc(sizeof(char) * (strlen(value) + 1));
displayMethod->signature = "()Ljava/lang/String;";
strcpy(displayMethod->name, value);
} else if (strcmp("showTrace", key) == 0) {
if (strcmp("true", value) == 0) {
parseState->current_method->showTrace = true;
} else {
parseState->current_method->showTrace = false;
}
} else if (strcmp("showAllParams", key) == 0) {
if (strcmp("true", value) == 0) {
parseState->current_method->showAllParams = true;
} else {
parseState->current_method->showAllParams = false;
}
} else if (strcmp("param", key) == 0) {
int param = atoi(value);
parseState->current_method->parameterPosition = param;
} else if (strcmp("displayMethodStatic", key) == 0) {
NameAndSignature *displayMethod = parseState->current_method->displayMethod;
if (!parseNameAndSignature(value, displayMethod)) {
return false;
}
// Break up the method into its class and method
char *className = strtok(displayMethod->name, ".");
char *methodName = strtok(NULL, ".");
if (methodName == NULL) {
fprintf(stderr, "Expected class.name for static method");
return false;
}
displayMethod->name = methodName;
parseState->current_method->staticDisplayClass = className;
} else if (strcmp("displayField", key) == 0) {
parseState->current_method->displayField = (NameAndSignature *) malloc(sizeof(NameAndSignature));
if (!parseNameAndSignature(value, parseState->current_method->displayField)) {
return false;
}
} else {
fprintf(stderr, "Warning: unrecognized property for method: %s\n", key);
}
return true;
}
bool processMethod(ParseState *parseState, Config *config, char *line) {
if (parseState->current_class == NULL) {
fprintf(stderr, "Found a method, but not in a class!\n");
return false;
}
// Swallow "- "
line += 2;
NameAndSignature *method = (NameAndSignature *) malloc(sizeof(NameAndSignature));
bool result = parseNameAndSignature(line, method);
if (!result) {
free(method);
return false;
}
MethodConfig *methodConfig = (MethodConfig *) malloc(sizeof(MethodConfig));
methodConfig->method = method;
methodConfig->next = parseState->current_class->method_list;
parseState->current_class->method_list = methodConfig;
parseState->current_method = methodConfig;
methodConfig->displayMethod = (NameAndSignature *) malloc(sizeof(NameAndSignature));
methodConfig->displayMethod->name = config->defaultDisplayMethodName;
methodConfig->displayMethod->signature = config->defaultDisplayMethodSig;
methodConfig->staticDisplayClass = NULL;
methodConfig->displayField = NULL;
methodConfig->showTrace = false;
methodConfig->showAllParams = false;
methodConfig->parameterPosition = 1;
return true;
}
bool processClass(ParseState *parseState, Config *config, char *line) {
// Swallow "- "
line += 2;
ClassConfig *clazz = (ClassConfig *) malloc(sizeof(ClassConfig));
clazz->method_list = NULL;
clazz->next = config->class_list;
config->class_list = clazz;
parseState->current_class = clazz;
parseState->current_method = NULL;
clazz->name = (char *) malloc(sizeof(char) * (strlen(line) + 1));
clazz->runtime_attached = false;
strcpy(clazz->name, line);
return true;
}
bool processLine(ParseState *parseState, Config *config, char *line) {
int leadingSpaces;
char *trimmed = trim(line, &leadingSpaces);
// Skip comments and blank lines
if (strlen(trimmed) == 0) {
return true;
}
if (trimmed[0] == '#') {
return true;
}
// Because my parser is lame, all other lines must start with a dash.
if (trimmed[0] != '-') {
fprintf(stderr, "Expecting - at start of line\n");
return false;
}
if (leadingSpaces == METHOD_PROPS_INDENT_LEVEL) {
if (!processMethodProp(parseState, config, trimmed)) {
return false;
}
} else if (leadingSpaces == METHOD_INDENT_LEVEL) {
if (!processMethod(parseState, config, trimmed)) {
return false;
}
} else if (leadingSpaces == CLASS_INDENT_LEVEL) {
if (!processClass(parseState, config, trimmed)) {
return false;
}
}
return true;
}
void ReleaseMethods(Config *mainConfig, MethodConfig *config) {
if (config->next != NULL) {
ReleaseMethods(mainConfig, config->next);
}
if (config->displayMethod != NULL) {
if (config->displayMethod->name != mainConfig->defaultDisplayMethodName) {
free(config->displayMethod->name);
}
if (config->displayMethod->signature != mainConfig->defaultDisplayMethodSig) {
free(config->displayMethod->signature);
}
free(config->displayMethod);
}
if (config->method != NULL) {
free(config->method->name);
free(config->method->signature);
free(config->method);
}
free(config->runtimeData);
free(config->staticDisplayClass);
if (config->displayField != NULL) {
if (config->displayField->name != NULL) {
free(config->displayField->name);
}
if (config->displayField->signature != NULL) {
free(config->displayField->signature);
}
free(config->displayField);
}
free(config);
}
void ReleaseClasses(Config *mainConfig, ClassConfig *config) {
if (config->next != NULL) {
ReleaseClasses(mainConfig, config->next);
}
ReleaseMethods(mainConfig, config->method_list);
free(config->name);
free(config);
}
void ReleaseConfig(Config *config) {
ReleaseClasses(config, config->class_list);
free(config);
}
Config *LoadConfig(const char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
perror("Unable to open config file");
return NULL;
}
Config *config = (Config *) malloc(sizeof(Config));
config->runtime_all_attached = false;
config->class_list = NULL;
config->defaultDisplayMethodName = "toString";
config->defaultDisplayMethodSig = "()Ljava/lang/String;";
ParseState parseState;
char buffer[MAX_LINE_SIZE];
int line = 0;
while(fgets(buffer, MAX_LINE_SIZE, fp)) {
line++;
if (!processLine(&parseState, config, buffer)) {
// We hit a parse error.
fprintf(stderr, "Failed to parse %s at %d\n", filename, line);
ReleaseConfig(config);
fclose(fp);
return NULL;
}
}
fclose(fp);
PrintConfig(config);
return config;
}