2 * These are very basic config file routines for the main module of CNTLM
4 * CNTLM is free software; you can redistribute it and/or modify it under the
5 * terms of the GNU General Public License as published by the Free Software
6 * Foundation; either version 2 of the License, or (at your option) any later
9 * CNTLM is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
16 * St, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Copyright (c) 2007 David Kubicek
32 static const char *globals[] = {
43 config_t
config_open(const char *fname
) {
46 char *buf
, *tmp
, *key
, *value
;
47 char section
[MINIBUF_SIZE
] = "global";
48 int i
, j
, slen
, len
, quote
;
50 //printf("sizeof = %d\n", sizeof(globals) / sizeof(char *));
52 fp
= fopen(fname
, "r");
57 rc
= (config_t
)new(sizeof(struct config_s
));
62 tmp
= fgets(buf
, BUFSIZE
, fp
);
66 len
= MIN(BUFSIZE
, strlen(buf
));
71 * Find first non-empty character
73 for (i
= j
= 0; j
< len
&& isspace(buf
[j
]); ++j
);
78 if (j
>= len
|| buf
[j
] == '#' || buf
[j
] == ';')
84 for (i
= j
; j
< len
&& isalnum(buf
[j
]); ++j
);
96 for (++j
; j
< len
&& isspace(buf
[j
]); ++j
);
97 for (slen
= j
; j
< len
&& j
-slen
< MINIBUF_SIZE
-1 && buf
[j
] != ']' && !isspace(buf
[j
]); ++j
);
99 strlcpy(section
, buf
+slen
, j
-slen
+1);
107 key
= substr(buf
, i
, j
-i
);
110 * Find next non-empty character
112 for (i
= j
; j
< len
&& isspace(buf
[j
]); ++j
);
113 if (j
>= len
|| buf
[j
] == '#' || buf
[j
] == ';')
121 for (i
= ++j
; j
< len
&& buf
[i
] != '"'; ++i
);
128 * Get value as quoted or until EOL/comment
130 value
= substr(buf
, j
, i
-j
);
132 i
= strcspn(value
, "#");
133 if (i
!= strlen(value
))
139 printf("section: %s, %s = '%s'\n", section
, key
, value
);
140 rc
->options
= hlist_add(rc
->options
, key
, value
, HLIST_NOALLOC
, HLIST_NOALLOC
);
149 void config_set(config_t cf
, char *option
, char *value
) {
150 cf
->options
= hlist_mod(cf
->options
, option
, value
, 1);
153 char *config_pop(config_t cf
, const char *option
) {
156 tmp
= hlist_get(cf
->options
, option
);
159 cf
->options
= hlist_del(cf
->options
, option
);
165 int config_count(config_t cf
) {
166 return hlist_count(cf
->options
);
169 void config_close(config_t cf
) {
173 cf
->options
= hlist_free(cf
->options
);