]>
jfr.im git - irc/quakenet/newserv.git/blob - core/config.c
4 * Facilities for handling the config file
10 #include "../lib/sstring.h"
11 #include "../lib/array.h"
17 extern void modulerehash(void);
34 Error("config",ERR_INFO
,"Rehashing config file.");
46 sects
=(configsection
*)(sections
.content
);
47 for (i
=0;i
<sections
.cursi
;i
++) {
48 items
=(configitem
*)(sects
[i
].items
.content
);
49 for (j
=0;j
<sects
[i
].items
.cursi
;j
++) {
50 freesstring(items
[j
].key
);
51 values
=(sstring
**)(items
[j
].values
.content
);
52 for (k
=0;k
<items
[j
].values
.cursi
;k
++) {
53 freesstring(values
[k
]);
55 array_free(&(items
[j
].values
));
57 array_free(&(sects
[i
].items
));
58 freesstring(sects
[i
].modulename
);
60 array_free(§ions
);
63 void initconfig(char *filename
) {
65 configsection
*sects
=NULL
;
66 configitem
*items
=NULL
;
67 sstring
**values
=NULL
;
80 array_init((§ions
),sizeof(configsection
));
82 if ((fp
=fopen(filename
,"r"))==NULL
) {
83 Error("core",ERR_STOP
,"Couldn't load config file.");
90 /* Check we got something */
94 /* Allow some comment chars */
95 if (buf
[0]=='#' || buf
[0]==';' || (buf
[0]=='/' && buf
[1]=='/'))
98 /* Blow away the line ending */
100 if (*cp
=='\n' || *cp
=='\r') {
105 /* Check it's long enough */
110 /* New section (possibly) -- hunt for the ']' */
111 for (cp
=&(buf
[2]);*cp
;cp
++) {
113 si
=array_getfreeslot(§ions
);
114 sects
=(configsection
*)(sections
.content
);
115 array_init(&(sects
[si
].items
),sizeof(configitem
));
116 array_setlim1(&(sects
[si
].items
),10);
118 sects
[si
].modulename
=getsstring(&(buf
[1]),255);
123 /* Ignore if we're not in a valid section */
127 for (cp
=buf
;*cp
;cp
++) {
131 for (ii
=0;ii
<sects
[si
].items
.cursi
;ii
++) {
132 if (!strcmp(items
[ii
].key
->content
,buf
)) {
133 /* Another value for an existing key */
134 j
=array_getfreeslot(&(items
[ii
].values
));
135 values
=(sstring
**)(items
[ii
].values
.content
);
136 values
[j
]=getsstring(cp
+1,512);
143 ii
=array_getfreeslot(&(sects
[si
].items
));
144 items
=(configitem
*)(sects
[si
].items
.content
);
145 items
[ii
].key
=getsstring(buf
,512);
146 array_init(&(items
[ii
].values
),sizeof(sstring
*));
147 array_setlim1(&(items
[ii
].values
),5);
148 j
=array_getfreeslot(&(items
[ii
].values
));
149 values
=(sstring
**)(items
[ii
].values
.content
);
150 values
[j
]=getsstring(cp
+1,512); /* looks nasty but is OK, this char is '='
151 * and we know 'buf' is null-terminated */
164 configsection
*sects
;
168 printf("Dumping complete configuration database.\n");
169 printf("Total sections: %d\n",sections
.cursi
);
171 sects
=(configsection
*)(sections
.content
);
172 for (i
=0;i
<sections
.cursi
;i
++) {
173 printf ("\nSection %02d: [%s] has %d items\n",i
,sects
[i
].modulename
->content
,sects
[i
].items
.cursi
);
174 items
=(configitem
*)(sects
[i
].items
.content
);
175 for(j
=0;j
<sects
[i
].items
.cursi
;j
++) {
176 printf(" Item %02d: [%s] has %d values\n",j
,items
[j
].key
->content
,items
[j
].values
.cursi
);
177 values
=(sstring
**)(items
[j
].values
.content
);
178 for (k
=0;k
<items
[j
].values
.cursi
;k
++) {
179 printf(" Value %2d: [%s]\n",k
,values
[k
]->content
);
184 printf("\n\nEnd of configuration database.\n");
188 * Two routes for extacting config info:
190 * - getconfigitem() is for keys which can only meaningfully have one value.
191 * It returns the last value for that key (so the config file has "last
192 * value overrides" semantics.
193 * - getconfigitems() is for keys which can have multiple values, it returns
194 * a pointer to the array of values.
197 array
*getconfigitems(char *module, char *key
) {
199 configsection
*sects
;
202 sects
=(configsection
*)(sections
.content
);
203 for (i
=0;i
<sections
.cursi
;i
++) {
204 if (!strcmp(module,sects
[i
].modulename
->content
)) {
205 /* Found the module */
206 items
=(configitem
*)(sects
[i
].items
.content
);
207 for (j
=0;j
<sects
[i
].items
.cursi
;j
++) {
208 if (!strcmp(key
,items
[j
].key
->content
)) {
209 return (&items
[j
].values
);
218 sstring
*getconfigitem(char *module, char *key
) {
222 if ((a
=getconfigitems(module,key
))==NULL
) {
226 values
=(sstring
**)(a
->content
);
227 return values
[(a
->cursi
-1)];
230 sstring
*getcopyconfigitem(char *module, char *key
, char *defaultvalue
, int len
) {
233 ss
=getconfigitem(module,key
);
235 return getsstring(ss
->content
,len
);
237 return getsstring(defaultvalue
,len
);