aboutsummaryrefslogtreecommitdiffstats
path: root/lufis/options.c
blob: 44f36630b42f97beb6752a5e2d0a2544fb2b5f80 (plain) (blame)
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
/*
 * options.c
 * Copyright (C) 2002 Florin Malita <mali@go.ro>
 *
 * This file is part of LUFS, a free userspace filesystem implementation.
 * See http://lufs.sourceforge.net/ for updates.
 *
 * LUFS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * LUFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <lufs/fs.h>

#include "list.h"

struct option {
    char *key;
    char *value;
    struct list_head list;
};

struct domain {
    char *name;
    struct list_head properties;
    struct list_head list;
};


static void
trim(char *buf){
    int b,e;
    
    if(!buf[0])
	return;

    for(b = 0; (buf[b] == ' ') || (buf[b] == '\t'); b++);
    for(e = strlen(buf) - 1; (e >= 0) && ((buf[e] == ' ') || (buf[e] == '\t')); e--);
    if(e < 0)
	e = strlen(buf) - 1;

    buf[e + 1] = 0;
    
    if(b)
	strcpy(buf, &buf[b]);

}

static struct domain*
find_domain(struct list_head *conf, char *name){
    struct list_head *p;
    struct domain *cls;

    list_for_each(p, conf){
	cls = list_entry(p, struct domain, list);
	if(!strcmp(name, cls->name)){
	    TRACE("domain found");
	    return cls;
	}
    }

    return NULL;
}

int
lu_opt_loadcfg(struct list_head *conf, char *file){
    struct domain *class;
    struct option *prop;
    FILE *f;
    static char buf[1024];
    char *i, *j;
    char *cls, *key, *val;

    TRACE("loading config from %s", file);

    if(!(f = fopen(file, "r"))){
	WARN("could not open file for reading!");
	return -1;
    }

    while(fgets(buf, 1024, f)){
	
	buf[strlen(buf) - 1] = 0;

	if((i = strchr(buf, '#')))
	    *i = 0;
	
	if((i = strchr(buf, '='))){
	    if((j = strstr(buf, "::"))){
		cls = buf;
		key = j + 2;
		val = i + 1;
		
		*i = 0;
		*j = 0;

		trim(cls);
		trim(key);
		trim(val);

		TRACE("class: #%s#", cls);
		TRACE("key: #%s#", key);
		TRACE("val: #%s#", val);

		if(!(class = find_domain(conf, cls))){
		    TRACE("class not found, creating...");

		    if(!(class = malloc(sizeof(struct domain)))){
			WARN("out of mem!");
			break;
		    }

		    memset(class, 0, sizeof(struct domain));

		    if(!(class->name = malloc(strlen(cls) + 1))){
			WARN("out of mem!");
			free(class);
			break;
		    }

		    strcpy(class->name, cls);
		    INIT_LIST_HEAD(&class->properties);

		    list_add(&class->list, conf);
		}

		if(!(prop = malloc(sizeof(struct option)))){
		    WARN("out of mem!");
		    break;
		}

		if(!(prop->key = malloc(strlen(key) + 1))){
		    WARN("out of mem!");
		    free(prop);
		    break;
		}

		if(!(prop->value = malloc(strlen(val) + 1))){
		    WARN("out of mem!");
		    free(prop->key);
		    free(prop);
		    break;
		}

		strcpy(prop->key, key);
		strcpy(prop->value, val);
		
		list_add(&prop->list, &class->properties);
	    }
	}
	
    }


    fclose(f);

    return 0;
}


const char*
lu_opt_getchar(struct list_head *conf, char *cls, char *key){
    struct domain *class;
    struct option *prop;
    struct list_head *p;

    TRACE("retrieving %s::%s", cls, key);
    
    if(!(class = find_domain(conf, cls)))
	return NULL;

    list_for_each(p, &class->properties){
	prop = list_entry(p, struct option, list);

	if(!strcmp(key, prop->key)){
	    TRACE("key found");
	    return prop->value;
	}
    }

    TRACE("key not found");

    return NULL;
}

int
lu_opt_getint(struct list_head *conf, char *domain, char *key, long int *result, int base){
    char *end;
    const char *val;
    long int res;

    if(!(val = lu_opt_getchar(conf, domain, key)))
	return -1;
    
    res = strtol(val, &end, base);
    
    if(*end)
	return -1;
    
    *result = res;
    return 0;
}

int
lu_opt_parse(struct list_head *conf, char *domain, char *opts){
    struct domain *class;
    struct option *prop;
    char *p, *sep;

    if(!(class = find_domain(conf, domain))){
	TRACE("domain not found, creating...");
	
	if(!(class = malloc(sizeof(struct domain)))){
	    WARN("out of mem!");
	    return -1;
	}
	
	memset(class, 0, sizeof(struct domain));
	
	if(!(class->name = malloc(strlen(domain) + 1))){
	    WARN("out of mem!");
	    free(class);
	    return -1;
	}
	
	strcpy(class->name, domain);
	INIT_LIST_HEAD(&class->properties);
	
	list_add(&class->list, conf);
    }
    
    for(p = strtok(opts, ","); p; p = strtok(NULL, ",")){
	if(!strstr(p, "password"))
	    TRACE("option: %s", p);

	if(!(prop = malloc(sizeof(struct option)))){
	    WARN("out of mem!");
	    return -1;
	}

	if((sep = strchr(p, '=')))
	    *sep = 0;

	if(!(prop->key = malloc(strlen(p) + 1))){
	    WARN("out of mem!");
	    free(prop);
	    return -1;
	}
	strcpy(prop->key, p);

	if(sep){
	    TRACE("option with parameter");

	    if((strlen(sep + 1) >= MAX_LEN) || !(prop->value = malloc(strlen(sep + 1) + 1))){
		WARN("out of mem!");
		free(prop->key);
		free(prop);
		return -1;
	    }
	    strcpy(prop->value, sep + 1);
	    
	    if(strstr(p, "password")){
		TRACE("hiding password...");
		memset(sep + 1, ' ', strlen(sep + 1));
	    }
	}else{
	    TRACE("flag");

	    if(!(prop->value = malloc(2))){
		WARN("out of mem!");
		free(prop->key);
		free(prop);
		return -1;
	    }
	    strcpy(prop->value, "");
	    
	}

	list_add(&prop->list, &class->properties);

    }

    return 0;
}