3 * Copyright (C) 2002 Florin Malita <mali@go.ro>
5 * This file is part of LUFS, a free userspace filesystem implementation.
6 * See http://lufs.sourceforge.net/ for updates.
8 * LUFS is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * LUFS is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
35 struct list_head list;
40 struct list_head properties;
41 struct list_head list;
52 for(b = 0; (buf[b] == ' ') || (buf[b] == '\t'); b++);
53 for(e = strlen(buf) - 1; (e >= 0) && ((buf[e] == ' ') || (buf[e] == '\t')); e--);
65 find_domain(struct list_head *conf, char *name){
69 list_for_each(p, conf){
70 cls = list_entry(p, struct domain, list);
71 if(!strcmp(name, cls->name)){
72 TRACE("domain found");
81 lu_opt_loadcfg(struct list_head *conf, char *file){
85 static char buf[1024];
87 char *cls, *key, *val;
89 TRACE("loading config from %s", file);
91 if(!(f = fopen(file, "r"))){
92 WARN("could not open file for reading!");
96 while(fgets(buf, 1024, f)){
98 buf[strlen(buf) - 1] = 0;
100 if((i = strchr(buf, '#')))
103 if((i = strchr(buf, '='))){
104 if((j = strstr(buf, "::"))){
116 TRACE("class: #%s#", cls);
117 TRACE("key: #%s#", key);
118 TRACE("val: #%s#", val);
120 if(!(class = find_domain(conf, cls))){
121 TRACE("class not found, creating...");
123 if(!(class = malloc(sizeof(struct domain)))){
128 memset(class, 0, sizeof(struct domain));
130 if(!(class->name = malloc(strlen(cls) + 1))){
136 strcpy(class->name, cls);
137 INIT_LIST_HEAD(&class->properties);
139 list_add(&class->list, conf);
142 if(!(prop = malloc(sizeof(struct option)))){
147 if(!(prop->key = malloc(strlen(key) + 1))){
153 if(!(prop->value = malloc(strlen(val) + 1))){
160 strcpy(prop->key, key);
161 strcpy(prop->value, val);
163 list_add(&prop->list, &class->properties);
177 lu_opt_getchar(struct list_head *conf, char *cls, char *key){
178 struct domain *class;
182 TRACE("retrieving %s::%s", cls, key);
184 if(!(class = find_domain(conf, cls)))
187 list_for_each(p, &class->properties){
188 prop = list_entry(p, struct option, list);
190 if(!strcmp(key, prop->key)){
196 TRACE("key not found");
202 lu_opt_getint(struct list_head *conf, char *domain, char *key, long int *result, int base){
207 if(!(val = lu_opt_getchar(conf, domain, key)))
210 res = strtol(val, &end, base);
220 lu_opt_parse(struct list_head *conf, char *domain, char *opts){
221 struct domain *class;
225 if(!(class = find_domain(conf, domain))){
226 TRACE("domain not found, creating...");
228 if(!(class = malloc(sizeof(struct domain)))){
233 memset(class, 0, sizeof(struct domain));
235 if(!(class->name = malloc(strlen(domain) + 1))){
241 strcpy(class->name, domain);
242 INIT_LIST_HEAD(&class->properties);
244 list_add(&class->list, conf);
247 for(p = strtok(opts, ","); p; p = strtok(NULL, ",")){
248 if(!strstr(p, "password"))
249 TRACE("option: %s", p);
251 if(!(prop = malloc(sizeof(struct option)))){
256 if((sep = strchr(p, '=')))
259 if(!(prop->key = malloc(strlen(p) + 1))){
264 strcpy(prop->key, p);
267 TRACE("option with parameter");
269 if((strlen(sep + 1) >= MAX_LEN) || !(prop->value = malloc(strlen(sep + 1) + 1))){
275 strcpy(prop->value, sep + 1);
277 if(strstr(p, "password")){
278 TRACE("hiding password...");
279 memset(sep + 1, ' ', strlen(sep + 1));
284 if(!(prop->value = malloc(2))){
290 strcpy(prop->value, "");
294 list_add(&prop->list, &class->properties);