dacapooptions.c 7.09 KB
Newer Older
1
2
3
4
5
6
7
8
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>

john's avatar
john committed
9
10
11
12
13
14
15
16
#include "dacapooptions.h"

#ifndef FALSE
#define FALSE 0
#endif

struct option_s {
    struct option_s*  next;
17
    char*             option;
john's avatar
john committed
18
    int               length;
19
    char*             argument;
john's avatar
john committed
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
    int               argLength;
};

struct option_s*    optionList = NULL;
struct option_s*    optionTail = NULL;

static struct option_s* findOption(const char* option) {
	struct option_s* check = optionList;

	while (check!=NULL) {
	    if (strncmp(check->option,option,check->length)==0 &&
	        (int)strlen(option)==check->length) {
    		return check;
	    }
	    check = check->next;
	}
	return NULL;
}

39
_Bool isSelected(const char* option, char** argument) {
john's avatar
john committed
40
41
	struct option_s* opt = findOption(option);

42
43
44
45
46
	if (opt != NULL && argument != NULL) {
		if (*argument==NULL)
			*argument = (char*)malloc(opt->argLength+1);
		if (0 < opt->argLength) strncpy(*argument,opt->argument,opt->argLength);
		(*argument)[opt->argLength]='\0';
john's avatar
john committed
47
	}
48

john's avatar
john committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
	return opt!=NULL;
}

_Bool hasArgument(const char* option) {
	struct option_s* opt = findOption(option);

	return opt!=NULL && opt->argument!=NULL && opt->argLength>0;
}

void reportOptionsList() {
	char tmp[10240];

	struct option_s* opt = optionList;

    fprintf(stderr,"reportOptionsList\n");
	while (opt!=NULL) {
	    strncpy(tmp,opt->option,opt->length);
	    tmp[opt->length] = '\0';
		if (opt->argument!=NULL) {
		    tmp[opt->length]=':';
john's avatar
john committed
69
			strncpy(tmp+opt->length+1,opt->argument,opt->argLength);
john's avatar
john committed
70
71
72
73
74
75
76
			tmp[opt->length+1+opt->argLength]='\0';
		}
		fprintf(stderr,"  %s\n",tmp);
		opt = opt->next;
	}
}

77
void makeOptionList(char* options) {
john's avatar
john committed
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
	if (options != NULL) {
        int start = 0;
		while (options[start]!='\0') {
		    int end = start+(start==0?0:1);

		    while (options[end]!=','&&options[end]!='\0'&&options[end]!='=') end++;

		    int arg = end;

		    if (options[arg]=='=')
		    	while(options[arg]!=','&&options[arg]!='\0') arg++;

		    if (0<(end-start)) {
		        struct option_s *tmp = (struct option_s*)malloc(sizeof(struct option_s));
		        tmp->next = NULL;
		        tmp->option = options+start;
		        tmp->length = end-start;

		        if (arg==end) {
		        	tmp->argument  = NULL;
		        	tmp->argLength = 0;
		        } else {
		        	tmp->argument  = options+end+1;
		        	tmp->argLength = arg-end-1;
		        }

		        if (optionList==NULL) {
		        	optionList = optionTail = tmp;
		        } else {
		            optionTail->next = tmp;
		            optionTail = tmp;
		        }
		    }

		    start = arg + (options[arg]=='\0'?0:1);
		}
	}

john's avatar
john committed
116
117
	if (isSelected(OPT_REPORT_OPTIONS, NULL))
		reportOptionsList();
john's avatar
john committed
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
#define CONFIG_FILE_NAME "config.txt"
#define DEFAULT_DIR "."
#define FILE_SEP_CHAR '/'

#define FILE_TYPE int

#define FILE_IS_CLOSED -1
#define FILE_FLAGS      (O_RDONLY)
#define FILE_MODE       (S_IRUSR)

/*
close(logFile);
logFile = open(f,FILE_FLAGS,FILE_MODE);
logFileSequenceLength += write(logFile,b,(e)*(n));
ssize_t read(int fd, void *buf, size_t count);

*/

enum State {
	UNKNOWN,
	COMMENT,
	KEY,
	KEY_ESCAPE,
	VALUE,
	VALUE_ESCAPE
};

void append(char** buf, int* length, int* maxLength, char c)
{
	if (*length == *maxLength) {
		*maxLength = 2 * *maxLength;
		char* tmp = (char*)malloc(sizeof(char) * *maxLength);

		int i;		
		for(i = 0; i < *length; i++) tmp[i] = (*buf)[i];
		
		free(*buf);
		*buf = tmp;
	}
	
	(*buf)[*length] = c;
	*length = *length + 1;
}
john's avatar
john committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

static void addOption(char* key, int keyLength, char* value, int valueLength) {
	struct option_s* opt = (struct option_s*)malloc(sizeof(struct option_s));
	
	opt->next = NULL;
	
	opt->option = (char*)malloc(sizeof(char)*(keyLength+1));
	opt->length = keyLength;
	strncpy(opt->option,key,sizeof(char)*keyLength);
	opt->option[keyLength] = '\0';
				
	opt->argument = (char*)malloc(sizeof(char)*(valueLength+1));
	opt->argLength = valueLength;
	strncpy(opt->argument,value,sizeof(char)*valueLength);
	opt->argument[valueLength] = '\0';
				
	if (optionList == NULL)
		optionList = optionTail = opt;
	else {
		optionTail->next = opt;
		optionTail = opt;
	}
}
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
	
void makeOptionListFromFile(char* agentDir) 
{
	char* fileName = CONFIG_FILE_NAME;
	char* fullFileName = NULL;
	
	if (agentDir == NULL) agentDir = DEFAULT_DIR; 
	
	int   length = strlen(agentDir);
	
	if (agentDir[length] != FILE_SEP_CHAR) {
		fullFileName = (char*)malloc(sizeof(char)*(length + 1 + strlen(fileName)));
		strcpy(fullFileName, agentDir);
		fullFileName[length] = FILE_SEP_CHAR;
		strcpy(fullFileName + length + 1, fileName);
	} else {
		strcpy(fullFileName, agentDir);
		strcpy(fullFileName + length, fileName);
	}

    FILE_TYPE fh = open(fullFileName,FILE_FLAGS,FILE_MODE);
	
	if (fh == FILE_IS_CLOSED) {
		fprintf(stderr, "unable to read config file %s\n", fullFileName);
		exit(10);
	}

	char c = '\0';
	size_t bytes = read(fh, &c, sizeof(char));
	
	enum State state = UNKNOWN;
	
	int keyLength      = 0;
	int keyMaxLength   = 128;
	char* key          = (char*)malloc(keyMaxLength);

	int valueLength    = 0;
	int valueMaxLength = 1024;
	char* value        = (char*)malloc(valueMaxLength);
		
	while (bytes != 0) {
		if (state == UNKNOWN) {
			switch (c) {
			case '-': case '#': 
				state = COMMENT;
				break;
			case '\n': case '\r':
				break;
			case '\\':
				state = KEY_ESCAPE;
				break;
			default:
				state = KEY;
				append(&key, &keyLength, &keyMaxLength, c);
				break;
			}
		} else if (state == KEY) {
			switch (c) {
			case '\\':
				state = KEY_ESCAPE;
				break;
			case '=':
				state = VALUE;
				break;
john's avatar
john committed
250
251
252
253
254
255
256
257
			case '\r': case '\n':
				state = UNKNOWN;

				addOption(key,keyLength,value,valueLength);

				keyLength   = 0;
				valueLength = 0;
				break;
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
			default:
				append(&key, &keyLength, &keyMaxLength, c);
				break;
			}
		} else if (state == KEY_ESCAPE) {
			switch (c) {
			case 'n':
				append(&key, &keyLength, &keyMaxLength, '\n');
				break;
			case 'r':
				append(&key, &keyLength, &keyMaxLength, '\r');
				break;
			case 't':
				append(&key, &keyLength, &keyMaxLength, '\t');
				break;
			default:
				append(&key, &keyLength, &keyMaxLength, c);
				break;
			}
			state = KEY;
		} else if (state == VALUE) {
			switch (c) {
			case '\\':
				state = VALUE_ESCAPE;
				break;
			case '\r': case '\n':
				state = UNKNOWN;

john's avatar
john committed
286
				addOption(key,keyLength,value,valueLength);
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

				keyLength   = 0;
				valueLength = 0;
				break;
			default:
				append(&value, &valueLength, &valueMaxLength, c);
				break;
			}
		} else if (state == VALUE_ESCAPE) {
			switch (c) {
			case 'n':
				append(&value, &valueLength, &valueMaxLength, '\n');
				break;
			case 'r':
				append(&value, &valueLength, &valueMaxLength, '\r');
				break;
			case 't':
				append(&value, &valueLength, &valueMaxLength, '\t');
				break;
			default:
				append(&value, &valueLength, &valueMaxLength, c);
				break;
			}
			state = VALUE;
		} else if (state == COMMENT) {
			switch (c) {
			case '\r': case '\n':
				state = UNKNOWN;
				break;
			default:
				break;
			}
		} else {
			fprintf(stderr, "we should never reach this state\n");
			exit(10);
		}
	
		bytes = read(fh, &c, sizeof(char));
	}	
	
	close(fh);
	free(fullFileName);
}