Commit | Line | Data |
---|---|---|
0f78b815 WD |
1 | /* This modules is based on the params.c module from Samba, written by Karl Auer |
2 | and much modifed by Christopher Hertel. */ | |
0b76cd63 | 3 | |
0f78b815 | 4 | /* |
0b76cd63 | 5 | * This program is free software; you can redistribute it and/or modify |
8e41b68e WD |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation; either version 3 of the License, or | |
8 | * (at your option) any later version. | |
0b76cd63 AT |
9 | * |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
e7c67065 | 15 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 16 | * with this program; if not, visit the http://fsf.org website. |
0f78b815 WD |
17 | */ |
18 | ||
19 | /* -------------------------------------------------------------------------- ** | |
0b76cd63 AT |
20 | * |
21 | * Module name: params | |
22 | * | |
23 | * -------------------------------------------------------------------------- ** | |
24 | * | |
25 | * This module performs lexical analysis and initial parsing of a | |
26 | * Windows-like parameter file. It recognizes and handles four token | |
27 | * types: section-name, parameter-name, parameter-value, and | |
28 | * end-of-file. Comments and line continuation are handled | |
29 | * internally. | |
30 | * | |
31 | * The entry point to the module is function pm_process(). This | |
32 | * function opens the source file, calls the Parse() function to parse | |
33 | * the input, and then closes the file when either the EOF is reached | |
34 | * or a fatal error is encountered. | |
35 | * | |
36 | * A sample parameter file might look like this: | |
37 | * | |
38 | * [section one] | |
39 | * parameter one = value string | |
40 | * parameter two = another value | |
41 | * [section two] | |
42 | * new parameter = some value or t'other | |
43 | * | |
44 | * The parameter file is divided into sections by section headers: | |
45 | * section names enclosed in square brackets (eg. [section one]). | |
46 | * Each section contains parameter lines, each of which consist of a | |
47 | * parameter name and value delimited by an equal sign. Roughly, the | |
48 | * syntax is: | |
49 | * | |
50 | * <file> :== { <section> } EOF | |
51 | * | |
52 | * <section> :== <section header> { <parameter line> } | |
53 | * | |
54 | * <section header> :== '[' NAME ']' | |
55 | * | |
56 | * <parameter line> :== NAME '=' VALUE '\n' | |
57 | * | |
58 | * Blank lines and comment lines are ignored. Comment lines are lines | |
59 | * beginning with either a semicolon (';') or a pound sign ('#'). | |
60 | * | |
61 | * All whitespace in section names and parameter names is compressed | |
62 | * to single spaces. Leading and trailing whitespace is stipped from | |
63 | * both names and values. | |
64 | * | |
65 | * Only the first equals sign in a parameter line is significant. | |
66 | * Parameter values may contain equals signs, square brackets and | |
67 | * semicolons. Internal whitespace is retained in parameter values, | |
68 | * with the exception of the '\r' character, which is stripped for | |
69 | * historic reasons. Parameter names may not start with a left square | |
70 | * bracket, an equal sign, a pound sign, or a semicolon, because these | |
71 | * are used to identify other tokens. | |
72 | * | |
73 | * -------------------------------------------------------------------------- ** | |
74 | */ | |
75 | ||
76 | #include "rsync.h" | |
1b42f628 | 77 | #include "ifuncs.h" |
0b76cd63 AT |
78 | |
79 | /* -------------------------------------------------------------------------- ** | |
80 | * Constants... | |
81 | */ | |
82 | ||
83 | #define BUFR_INC 1024 | |
84 | ||
85 | ||
86 | /* -------------------------------------------------------------------------- ** | |
87 | * Variables... | |
88 | * | |
89 | * bufr - pointer to a global buffer. This is probably a kludge, | |
90 | * but it was the nicest kludge I could think of (for now). | |
91 | * bSize - The size of the global buffer <bufr>. | |
92 | */ | |
93 | ||
94 | static char *bufr = NULL; | |
95 | static int bSize = 0; | |
8a3ddcfc WD |
96 | static BOOL (*the_sfunc)(char *); |
97 | static BOOL (*the_pfunc)(char *, char *); | |
0b76cd63 AT |
98 | |
99 | /* -------------------------------------------------------------------------- ** | |
100 | * Functions... | |
101 | */ | |
102 | ||
103 | static int EatWhitespace( FILE *InFile ) | |
104 | /* ------------------------------------------------------------------------ ** | |
105 | * Scan past whitespace (see ctype(3C)) and return the first non-whitespace | |
106 | * character, or newline, or EOF. | |
107 | * | |
108 | * Input: InFile - Input source. | |
109 | * | |
110 | * Output: The next non-whitespace character in the input stream. | |
111 | * | |
112 | * Notes: Because the config files use a line-oriented grammar, we | |
113 | * explicitly exclude the newline character from the list of | |
114 | * whitespace characters. | |
115 | * - Note that both EOF (-1) and the nul character ('\0') are | |
116 | * considered end-of-file markers. | |
117 | * | |
118 | * ------------------------------------------------------------------------ ** | |
119 | */ | |
120 | { | |
121 | int c; | |
122 | ||
123 | for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) ) | |
124 | ; | |
125 | return( c ); | |
126 | } /* EatWhitespace */ | |
127 | ||
128 | static int EatComment( FILE *InFile ) | |
129 | /* ------------------------------------------------------------------------ ** | |
130 | * Scan to the end of a comment. | |
131 | * | |
132 | * Input: InFile - Input source. | |
133 | * | |
134 | * Output: The character that marks the end of the comment. Normally, | |
135 | * this will be a newline, but it *might* be an EOF. | |
136 | * | |
137 | * Notes: Because the config files use a line-oriented grammar, we | |
138 | * explicitly exclude the newline character from the list of | |
139 | * whitespace characters. | |
140 | * - Note that both EOF (-1) and the nul character ('\0') are | |
141 | * considered end-of-file markers. | |
142 | * | |
143 | * ------------------------------------------------------------------------ ** | |
144 | */ | |
145 | { | |
146 | int c; | |
147 | ||
148 | for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) ) | |
149 | ; | |
150 | return( c ); | |
151 | } /* EatComment */ | |
152 | ||
153 | static int Continuation( char *line, int pos ) | |
154 | /* ------------------------------------------------------------------------ ** | |
155 | * Scan backards within a string to discover if the last non-whitespace | |
156 | * character is a line-continuation character ('\\'). | |
157 | * | |
158 | * Input: line - A pointer to a buffer containing the string to be | |
159 | * scanned. | |
160 | * pos - This is taken to be the offset of the end of the | |
161 | * string. This position is *not* scanned. | |
162 | * | |
163 | * Output: The offset of the '\\' character if it was found, or -1 to | |
164 | * indicate that it was not. | |
165 | * | |
166 | * ------------------------------------------------------------------------ ** | |
167 | */ | |
168 | { | |
169 | pos--; | |
2dc7b8bd | 170 | while( pos >= 0 && isSpace(line + pos) ) |
0b76cd63 AT |
171 | pos--; |
172 | ||
173 | return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 ); | |
174 | } /* Continuation */ | |
175 | ||
176 | ||
177 | static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) ) | |
178 | /* ------------------------------------------------------------------------ ** | |
179 | * Scan a section name, and pass the name to function sfunc(). | |
180 | * | |
181 | * Input: InFile - Input source. | |
182 | * sfunc - Pointer to the function to be called if the section | |
183 | * name is successfully read. | |
184 | * | |
185 | * Output: True if the section name was read and True was returned from | |
186 | * <sfunc>. False if <sfunc> failed or if a lexical error was | |
187 | * encountered. | |
188 | * | |
189 | * ------------------------------------------------------------------------ ** | |
190 | */ | |
191 | { | |
192 | int c; | |
193 | int i; | |
194 | int end; | |
195 | char *func = "params.c:Section() -"; | |
196 | ||
197 | i = 0; /* <i> is the offset of the next free byte in bufr[] and */ | |
198 | end = 0; /* <end> is the current "end of string" offset. In most */ | |
199 | /* cases these will be the same, but if the last */ | |
200 | /* character written to bufr[] is a space, then <end> */ | |
201 | /* will be one less than <i>. */ | |
202 | ||
203 | c = EatWhitespace( InFile ); /* We've already got the '['. Scan */ | |
204 | /* past initial white space. */ | |
205 | ||
206 | while( (EOF != c) && (c > 0) ) | |
207 | { | |
208 | ||
209 | /* Check that the buffer is big enough for the next character. */ | |
210 | if( i > (bSize - 2) ) | |
211 | { | |
212 | bSize += BUFR_INC; | |
58cadc86 | 213 | bufr = realloc_array( bufr, char, bSize ); |
0b76cd63 AT |
214 | if( NULL == bufr ) |
215 | { | |
a6c6f8e6 | 216 | rprintf(FLOG, "%s Memory re-allocation failure.", func); |
0b76cd63 AT |
217 | return( False ); |
218 | } | |
219 | } | |
220 | ||
221 | /* Handle a single character. */ | |
222 | switch( c ) | |
223 | { | |
224 | case ']': /* Found the closing bracket. */ | |
225 | bufr[end] = '\0'; | |
226 | if( 0 == end ) /* Don't allow an empty name. */ | |
227 | { | |
582831a4 | 228 | rprintf(FLOG, "%s Empty section name in config file.\n", func ); |
0b76cd63 AT |
229 | return( False ); |
230 | } | |
231 | if( !sfunc( bufr ) ) /* Got a valid name. Deal with it. */ | |
232 | return( False ); | |
233 | (void)EatComment( InFile ); /* Finish off the line. */ | |
234 | return( True ); | |
235 | ||
236 | case '\n': /* Got newline before closing ']'. */ | |
237 | i = Continuation( bufr, i ); /* Check for line continuation. */ | |
238 | if( i < 0 ) | |
239 | { | |
240 | bufr[end] = '\0'; | |
582831a4 | 241 | rprintf(FLOG, "%s Badly formed line in config file: %s\n", |
0b76cd63 AT |
242 | func, bufr ); |
243 | return( False ); | |
244 | } | |
245 | end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); | |
246 | c = getc( InFile ); /* Continue with next line. */ | |
247 | break; | |
248 | ||
249 | default: /* All else are a valid name chars. */ | |
250 | if( isspace( c ) ) /* One space per whitespace region. */ | |
251 | { | |
252 | bufr[end] = ' '; | |
253 | i = end + 1; | |
254 | c = EatWhitespace( InFile ); | |
255 | } | |
256 | else /* All others copy verbatim. */ | |
257 | { | |
258 | bufr[i++] = c; | |
259 | end = i; | |
260 | c = getc( InFile ); | |
261 | } | |
262 | } | |
263 | } | |
264 | ||
265 | /* We arrive here if we've met the EOF before the closing bracket. */ | |
582831a4 | 266 | rprintf(FLOG, "%s Unexpected EOF in the config file: %s\n", func, bufr ); |
0b76cd63 AT |
267 | return( False ); |
268 | } /* Section */ | |
269 | ||
270 | static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c ) | |
271 | /* ------------------------------------------------------------------------ ** | |
272 | * Scan a parameter name and value, and pass these two fields to pfunc(). | |
273 | * | |
274 | * Input: InFile - The input source. | |
275 | * pfunc - A pointer to the function that will be called to | |
276 | * process the parameter, once it has been scanned. | |
277 | * c - The first character of the parameter name, which | |
278 | * would have been read by Parse(). Unlike a comment | |
279 | * line or a section header, there is no lead-in | |
280 | * character that can be discarded. | |
281 | * | |
282 | * Output: True if the parameter name and value were scanned and processed | |
283 | * successfully, else False. | |
284 | * | |
285 | * Notes: This function is in two parts. The first loop scans the | |
286 | * parameter name. Internal whitespace is compressed, and an | |
287 | * equal sign (=) terminates the token. Leading and trailing | |
288 | * whitespace is discarded. The second loop scans the parameter | |
289 | * value. When both have been successfully identified, they are | |
290 | * passed to pfunc() for processing. | |
291 | * | |
292 | * ------------------------------------------------------------------------ ** | |
293 | */ | |
294 | { | |
295 | int i = 0; /* Position within bufr. */ | |
296 | int end = 0; /* bufr[end] is current end-of-string. */ | |
297 | int vstart = 0; /* Starting position of the parameter value. */ | |
298 | char *func = "params.c:Parameter() -"; | |
299 | ||
300 | /* Read the parameter name. */ | |
301 | while( 0 == vstart ) /* Loop until we've found the start of the value. */ | |
302 | { | |
303 | ||
304 | if( i > (bSize - 2) ) /* Ensure there's space for next char. */ | |
305 | { | |
306 | bSize += BUFR_INC; | |
58cadc86 | 307 | bufr = realloc_array( bufr, char, bSize ); |
0b76cd63 AT |
308 | if( NULL == bufr ) |
309 | { | |
a6c6f8e6 | 310 | rprintf(FLOG, "%s Memory re-allocation failure.", func) ; |
0b76cd63 AT |
311 | return( False ); |
312 | } | |
313 | } | |
314 | ||
315 | switch( c ) | |
316 | { | |
317 | case '=': /* Equal sign marks end of param name. */ | |
318 | if( 0 == end ) /* Don't allow an empty name. */ | |
319 | { | |
582831a4 | 320 | rprintf(FLOG, "%s Invalid parameter name in config file.\n", func ); |
0b76cd63 AT |
321 | return( False ); |
322 | } | |
323 | bufr[end++] = '\0'; /* Mark end of string & advance. */ | |
582831a4 WD |
324 | i = vstart = end; /* New string starts here. */ |
325 | c = EatWhitespace(InFile); | |
0b76cd63 AT |
326 | break; |
327 | ||
328 | case '\n': /* Find continuation char, else error. */ | |
329 | i = Continuation( bufr, i ); | |
330 | if( i < 0 ) | |
331 | { | |
332 | bufr[end] = '\0'; | |
582831a4 | 333 | rprintf(FLOG, "%s Ignoring badly formed line in config file: %s\n", |
0b76cd63 AT |
334 | func, bufr ); |
335 | return( True ); | |
336 | } | |
337 | end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); | |
338 | c = getc( InFile ); /* Read past eoln. */ | |
339 | break; | |
340 | ||
341 | case '\0': /* Shouldn't have EOF within param name. */ | |
342 | case EOF: | |
343 | bufr[i] = '\0'; | |
a6c6f8e6 | 344 | rprintf(FLOG, "%s Unexpected end-of-file at: %s\n", func, bufr ); |
0b76cd63 AT |
345 | return( True ); |
346 | ||
582831a4 WD |
347 | case ' ': |
348 | case '\t': | |
349 | /* A directive divides at the first space or tab. */ | |
350 | if (*bufr == '&') { | |
351 | bufr[end++] = '\0'; | |
352 | i = vstart = end; | |
353 | c = EatWhitespace(InFile); | |
354 | if (c == '=') | |
355 | c = EatWhitespace(InFile); | |
356 | break; | |
357 | } | |
358 | /* FALL THROUGH */ | |
359 | ||
0b76cd63 AT |
360 | default: |
361 | if( isspace( c ) ) /* One ' ' per whitespace region. */ | |
362 | { | |
363 | bufr[end] = ' '; | |
364 | i = end + 1; | |
365 | c = EatWhitespace( InFile ); | |
366 | } | |
367 | else /* All others verbatim. */ | |
368 | { | |
369 | bufr[i++] = c; | |
370 | end = i; | |
371 | c = getc( InFile ); | |
372 | } | |
373 | } | |
374 | } | |
375 | ||
376 | /* Now parse the value. */ | |
0b76cd63 AT |
377 | while( (EOF !=c) && (c > 0) ) |
378 | { | |
379 | ||
380 | if( i > (bSize - 2) ) /* Make sure there's enough room. */ | |
381 | { | |
382 | bSize += BUFR_INC; | |
58cadc86 | 383 | bufr = realloc_array( bufr, char, bSize ); |
0b76cd63 AT |
384 | if( NULL == bufr ) |
385 | { | |
a6c6f8e6 | 386 | rprintf(FLOG, "%s Memory re-allocation failure.", func) ; |
0b76cd63 AT |
387 | return( False ); |
388 | } | |
389 | } | |
390 | ||
391 | switch( c ) | |
392 | { | |
393 | case '\r': /* Explicitly remove '\r' because the older */ | |
394 | c = getc( InFile ); /* version called fgets_slash() which also */ | |
395 | break; /* removes them. */ | |
396 | ||
397 | case '\n': /* Marks end of value unless there's a '\'. */ | |
398 | i = Continuation( bufr, i ); | |
399 | if( i < 0 ) | |
400 | c = 0; | |
401 | else | |
402 | { | |
2dc7b8bd | 403 | for( end = i; end >= 0 && isSpace(bufr + end); end-- ) |
0b76cd63 AT |
404 | ; |
405 | c = getc( InFile ); | |
406 | } | |
407 | break; | |
408 | ||
409 | default: /* All others verbatim. Note that spaces do */ | |
410 | bufr[i++] = c; /* not advance <end>. This allows trimming */ | |
411 | if( !isspace( c ) ) /* of whitespace at the end of the line. */ | |
412 | end = i; | |
413 | c = getc( InFile ); | |
414 | break; | |
415 | } | |
416 | } | |
417 | bufr[end] = '\0'; /* End of value. */ | |
418 | ||
419 | return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */ | |
420 | } /* Parameter */ | |
421 | ||
8a3ddcfc WD |
422 | static int name_cmp(const void *n1, const void *n2) |
423 | { | |
424 | return strcmp(*(char * const *)n1, *(char * const *)n2); | |
425 | } | |
426 | ||
427 | static int include_config(char *include, int manage_globals) | |
428 | { | |
312b6841 WD |
429 | STRUCT_STAT sb; |
430 | int ret; | |
8a3ddcfc | 431 | |
582831a4 WD |
432 | if (do_stat(include, &sb) < 0) { |
433 | rsyserr(FLOG, errno, "unable to stat config file \"%s\"", include); | |
312b6841 | 434 | return 0; |
582831a4 | 435 | } |
8a3ddcfc | 436 | |
312b6841 | 437 | if (S_ISREG(sb.st_mode)) { |
8a3ddcfc | 438 | if (manage_globals && the_sfunc) |
312b6841 WD |
439 | the_sfunc("]push"); |
440 | ret = pm_process(include, the_sfunc, the_pfunc); | |
441 | if (manage_globals && the_sfunc) | |
442 | the_sfunc("]pop"); | |
443 | } else if (S_ISDIR(sb.st_mode)) { | |
444 | char buf[MAXPATHLEN], **bpp; | |
445 | item_list conf_list; | |
446 | struct dirent *di; | |
447 | size_t j; | |
448 | DIR *d; | |
449 | ||
582831a4 WD |
450 | if (!(d = opendir(include))) { |
451 | rsyserr(FLOG, errno, "unable to open config dir \"%s\"", include); | |
312b6841 | 452 | return 0; |
582831a4 | 453 | } |
312b6841 WD |
454 | |
455 | memset(&conf_list, 0, sizeof conf_list); | |
456 | ||
457 | while ((di = readdir(d)) != NULL) { | |
458 | char *dname = d_name(di); | |
459 | if (!wildmatch("*.conf", dname)) | |
460 | continue; | |
461 | bpp = EXPAND_ITEM_LIST(&conf_list, char *, 32); | |
462 | pathjoin(buf, sizeof buf, include, dname); | |
463 | *bpp = strdup(buf); | |
464 | } | |
465 | closedir(d); | |
466 | ||
467 | if (!(bpp = conf_list.items)) | |
468 | return 1; | |
469 | ||
470 | if (conf_list.count > 1) | |
471 | qsort(bpp, conf_list.count, sizeof (char *), name_cmp); | |
472 | ||
473 | for (j = 0, ret = 1; j < conf_list.count; j++) { | |
474 | if (manage_globals && the_sfunc) | |
475 | the_sfunc(j == 0 ? "]push" : "]reset"); | |
476 | if ((ret = pm_process(bpp[j], the_sfunc, the_pfunc)) != 1) | |
477 | break; | |
478 | } | |
8a3ddcfc | 479 | |
312b6841 WD |
480 | if (manage_globals && the_sfunc) |
481 | the_sfunc("]pop"); | |
8a3ddcfc | 482 | |
312b6841 WD |
483 | for (j = 0; j < conf_list.count; j++) |
484 | free(bpp[j]); | |
485 | free(bpp); | |
486 | } else | |
487 | ret = 0; | |
8a3ddcfc WD |
488 | |
489 | return ret; | |
490 | } | |
491 | ||
492 | static int parse_directives(char *name, char *val) | |
493 | { | |
582831a4 | 494 | if (strcasecmp(name, "&include") == 0) |
8a3ddcfc | 495 | return include_config(val, 1); |
582831a4 | 496 | if (strcasecmp(name, "&merge") == 0) |
8a3ddcfc | 497 | return include_config(val, 0); |
582831a4 | 498 | rprintf(FLOG, "Unknown directive: %s.\n", name); |
8a3ddcfc WD |
499 | return 0; |
500 | } | |
501 | ||
502 | static int Parse( FILE *InFile, | |
0b76cd63 AT |
503 | BOOL (*sfunc)(char *), |
504 | BOOL (*pfunc)(char *, char *) ) | |
505 | /* ------------------------------------------------------------------------ ** | |
506 | * Scan & parse the input. | |
507 | * | |
508 | * Input: InFile - Input source. | |
509 | * sfunc - Function to be called when a section name is scanned. | |
510 | * See Section(). | |
511 | * pfunc - Function to be called when a parameter is scanned. | |
512 | * See Parameter(). | |
513 | * | |
8a3ddcfc WD |
514 | * Output: 1 if the file was successfully scanned, 2 if the file was |
515 | * scanned until a section header with no section function, else 0. | |
0b76cd63 AT |
516 | * |
517 | * Notes: The input can be viewed in terms of 'lines'. There are four | |
518 | * types of lines: | |
519 | * Blank - May contain whitespace, otherwise empty. | |
520 | * Comment - First non-whitespace character is a ';' or '#'. | |
521 | * The remainder of the line is ignored. | |
522 | * Section - First non-whitespace character is a '['. | |
523 | * Parameter - The default case. | |
8a3ddcfc | 524 | * |
0b76cd63 AT |
525 | * ------------------------------------------------------------------------ ** |
526 | */ | |
527 | { | |
528 | int c; | |
529 | ||
530 | c = EatWhitespace( InFile ); | |
531 | while( (EOF != c) && (c > 0) ) | |
532 | { | |
533 | switch( c ) | |
534 | { | |
535 | case '\n': /* Blank line. */ | |
536 | c = EatWhitespace( InFile ); | |
537 | break; | |
538 | ||
539 | case ';': /* Comment line. */ | |
540 | case '#': | |
541 | c = EatComment( InFile ); | |
542 | break; | |
543 | ||
544 | case '[': /* Section Header. */ | |
8a3ddcfc WD |
545 | if (!sfunc) |
546 | return 2; | |
547 | if( !Section( InFile, sfunc ) ) | |
548 | return 0; | |
549 | c = EatWhitespace( InFile ); | |
550 | break; | |
0b76cd63 AT |
551 | |
552 | case '\\': /* Bogus backslash. */ | |
553 | c = EatWhitespace( InFile ); | |
554 | break; | |
555 | ||
8a3ddcfc WD |
556 | case '&': /* Handle directives */ |
557 | the_sfunc = sfunc; | |
558 | the_pfunc = pfunc; | |
8a3ddcfc WD |
559 | c = Parameter( InFile, parse_directives, c ); |
560 | if (c != 1) | |
561 | return c; | |
562 | c = EatWhitespace( InFile ); | |
563 | break; | |
564 | ||
0b76cd63 AT |
565 | default: /* Parameter line. */ |
566 | if( !Parameter( InFile, pfunc, c ) ) | |
8a3ddcfc | 567 | return 0; |
0b76cd63 AT |
568 | c = EatWhitespace( InFile ); |
569 | break; | |
570 | } | |
571 | } | |
8a3ddcfc | 572 | return 1; |
0b76cd63 AT |
573 | } /* Parse */ |
574 | ||
575 | static FILE *OpenConfFile( char *FileName ) | |
576 | /* ------------------------------------------------------------------------ ** | |
582831a4 | 577 | * Open a config file. |
0b76cd63 AT |
578 | * |
579 | * Input: FileName - The pathname of the config file to be opened. | |
580 | * | |
581 | * Output: A pointer of type (FILE *) to the opened file, or NULL if the | |
582 | * file could not be opened. | |
583 | * | |
584 | * ------------------------------------------------------------------------ ** | |
585 | */ | |
586 | { | |
587 | FILE *OpenedFile; | |
588 | char *func = "params.c:OpenConfFile() -"; | |
589 | ||
590 | if( NULL == FileName || 0 == *FileName ) | |
591 | { | |
582831a4 | 592 | rprintf(FLOG, "%s No config filename specified.\n", func); |
0b76cd63 AT |
593 | return( NULL ); |
594 | } | |
595 | ||
0090cbdb | 596 | OpenedFile = fopen( FileName, "r" ); |
0b76cd63 AT |
597 | if( NULL == OpenedFile ) |
598 | { | |
582831a4 | 599 | rsyserr(FLOG, errno, "unable to open config file \"%s\"", |
45c49b52 | 600 | FileName); |
0b76cd63 AT |
601 | } |
602 | ||
603 | return( OpenedFile ); | |
604 | } /* OpenConfFile */ | |
605 | ||
8a3ddcfc | 606 | int pm_process( char *FileName, |
0b76cd63 AT |
607 | BOOL (*sfunc)(char *), |
608 | BOOL (*pfunc)(char *, char *) ) | |
609 | /* ------------------------------------------------------------------------ ** | |
610 | * Process the named parameter file. | |
611 | * | |
612 | * Input: FileName - The pathname of the parameter file to be opened. | |
613 | * sfunc - A pointer to a function that will be called when | |
614 | * a section name is discovered. | |
615 | * pfunc - A pointer to a function that will be called when | |
616 | * a parameter name and value are discovered. | |
617 | * | |
8a3ddcfc WD |
618 | * Output: 1 if the file was successfully parsed, 2 if parsing ended at a |
619 | * section header w/o a section function, else 0. | |
0b76cd63 AT |
620 | * |
621 | * ------------------------------------------------------------------------ ** | |
622 | */ | |
623 | { | |
624 | int result; | |
625 | FILE *InFile; | |
626 | char *func = "params.c:pm_process() -"; | |
627 | ||
628 | InFile = OpenConfFile( FileName ); /* Open the config file. */ | |
629 | if( NULL == InFile ) | |
630 | return( False ); | |
631 | ||
632 | if( NULL != bufr ) /* If we already have a buffer */ | |
633 | result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */ | |
634 | /* use it. */ | |
635 | ||
636 | else /* If we don't have a buffer */ | |
637 | { /* allocate one, then parse, */ | |
638 | bSize = BUFR_INC; /* then free. */ | |
58cadc86 | 639 | bufr = new_array( char, bSize ); |
0b76cd63 AT |
640 | if( NULL == bufr ) |
641 | { | |
a6c6f8e6 | 642 | rprintf(FLOG, "%s memory allocation failure.\n", func); |
0b76cd63 AT |
643 | fclose(InFile); |
644 | return( False ); | |
645 | } | |
646 | result = Parse( InFile, sfunc, pfunc ); | |
647 | free( bufr ); | |
648 | bufr = NULL; | |
649 | bSize = 0; | |
650 | } | |
651 | ||
652 | fclose(InFile); | |
653 | ||
654 | if( !result ) /* Generic failure. */ | |
655 | { | |
a6c6f8e6 | 656 | rprintf(FLOG, "%s Failed. Error returned from params.c:parse().\n", func); |
8a3ddcfc | 657 | return 0; |
0b76cd63 AT |
658 | } |
659 | ||
8a3ddcfc | 660 | return result; |
0b76cd63 AT |
661 | } /* pm_process */ |
662 | ||
663 | /* -------------------------------------------------------------------------- */ | |
f9e940ef | 664 |