1 /* This modules is based on the params.c module from Samba, written by Karl Auer
2 and much modifed by Christopher Hertel. */
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, visit the http://fsf.org website.
18 /* -------------------------------------------------------------------------- **
22 * -------------------------------------------------------------------------- **
24 * This module performs lexical analysis and initial parsing of a
25 * Windows-like parameter file. It recognizes and handles four token
26 * types: section-name, parameter-name, parameter-value, and
27 * end-of-file. Comments and line continuation are handled
30 * The entry point to the module is function pm_process(). This
31 * function opens the source file, calls the Parse() function to parse
32 * the input, and then closes the file when either the EOF is reached
33 * or a fatal error is encountered.
35 * A sample parameter file might look like this:
38 * parameter one = value string
39 * parameter two = another value
41 * new parameter = some value or t'other
43 * The parameter file is divided into sections by section headers:
44 * section names enclosed in square brackets (eg. [section one]).
45 * Each section contains parameter lines, each of which consist of a
46 * parameter name and value delimited by an equal sign. Roughly, the
49 * <file> :== { <section> } EOF
51 * <section> :== <section header> { <parameter line> }
53 * <section header> :== '[' NAME ']'
55 * <parameter line> :== NAME '=' VALUE '\n'
57 * Blank lines and comment lines are ignored. Comment lines are lines
58 * beginning with either a semicolon (';') or a pound sign ('#').
60 * All whitespace in section names and parameter names is compressed
61 * to single spaces. Leading and trailing whitespace is stipped from
62 * both names and values.
64 * Only the first equals sign in a parameter line is significant.
65 * Parameter values may contain equals signs, square brackets and
66 * semicolons. Internal whitespace is retained in parameter values,
67 * with the exception of the '\r' character, which is stripped for
68 * historic reasons. Parameter names may not start with a left square
69 * bracket, an equal sign, a pound sign, or a semicolon, because these
70 * are used to identify other tokens.
72 * -------------------------------------------------------------------------- **
77 /* -------------------------------------------------------------------------- **
84 /* -------------------------------------------------------------------------- **
87 * bufr - pointer to a global buffer. This is probably a kludge,
88 * but it was the nicest kludge I could think of (for now).
89 * bSize - The size of the global buffer <bufr>.
92 static char *bufr = NULL;
95 /* -------------------------------------------------------------------------- **
99 static int EatWhitespace( FILE *InFile )
100 /* ------------------------------------------------------------------------ **
101 * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
102 * character, or newline, or EOF.
104 * Input: InFile - Input source.
106 * Output: The next non-whitespace character in the input stream.
108 * Notes: Because the config files use a line-oriented grammar, we
109 * explicitly exclude the newline character from the list of
110 * whitespace characters.
111 * - Note that both EOF (-1) and the nul character ('\0') are
112 * considered end-of-file markers.
114 * ------------------------------------------------------------------------ **
119 for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) )
122 } /* EatWhitespace */
124 static int EatComment( FILE *InFile )
125 /* ------------------------------------------------------------------------ **
126 * Scan to the end of a comment.
128 * Input: InFile - Input source.
130 * Output: The character that marks the end of the comment. Normally,
131 * this will be a newline, but it *might* be an EOF.
133 * Notes: Because the config files use a line-oriented grammar, we
134 * explicitly exclude the newline character from the list of
135 * whitespace characters.
136 * - Note that both EOF (-1) and the nul character ('\0') are
137 * considered end-of-file markers.
139 * ------------------------------------------------------------------------ **
144 for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) )
149 static int Continuation( char *line, int pos )
150 /* ------------------------------------------------------------------------ **
151 * Scan backards within a string to discover if the last non-whitespace
152 * character is a line-continuation character ('\\').
154 * Input: line - A pointer to a buffer containing the string to be
156 * pos - This is taken to be the offset of the end of the
157 * string. This position is *not* scanned.
159 * Output: The offset of the '\\' character if it was found, or -1 to
160 * indicate that it was not.
162 * ------------------------------------------------------------------------ **
166 while( pos >= 0 && isSpace(line + pos) )
169 return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
173 static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) )
174 /* ------------------------------------------------------------------------ **
175 * Scan a section name, and pass the name to function sfunc().
177 * Input: InFile - Input source.
178 * sfunc - Pointer to the function to be called if the section
179 * name is successfully read.
181 * Output: True if the section name was read and True was returned from
182 * <sfunc>. False if <sfunc> failed or if a lexical error was
185 * ------------------------------------------------------------------------ **
191 char *func = "params.c:Section() -";
193 i = 0; /* <i> is the offset of the next free byte in bufr[] and */
194 end = 0; /* <end> is the current "end of string" offset. In most */
195 /* cases these will be the same, but if the last */
196 /* character written to bufr[] is a space, then <end> */
197 /* will be one less than <i>. */
199 c = EatWhitespace( InFile ); /* We've already got the '['. Scan */
200 /* past initial white space. */
202 while( (EOF != c) && (c > 0) )
205 /* Check that the buffer is big enough for the next character. */
206 if( i > (bSize - 2) )
209 bufr = realloc_array( bufr, char, bSize );
212 rprintf(FERROR, "%s Memory re-allocation failure.", func);
217 /* Handle a single character. */
220 case ']': /* Found the closing bracket. */
222 if( 0 == end ) /* Don't allow an empty name. */
224 rprintf(FERROR, "%s Empty section name in configuration file.\n", func );
227 if( !sfunc( bufr ) ) /* Got a valid name. Deal with it. */
229 (void)EatComment( InFile ); /* Finish off the line. */
232 case '\n': /* Got newline before closing ']'. */
233 i = Continuation( bufr, i ); /* Check for line continuation. */
237 rprintf(FERROR, "%s Badly formed line in configuration file: %s\n",
241 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
242 c = getc( InFile ); /* Continue with next line. */
245 default: /* All else are a valid name chars. */
246 if( isspace( c ) ) /* One space per whitespace region. */
250 c = EatWhitespace( InFile );
252 else /* All others copy verbatim. */
261 /* We arrive here if we've met the EOF before the closing bracket. */
262 rprintf(FERROR, "%s Unexpected EOF in the configuration file: %s\n", func, bufr );
266 static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
267 /* ------------------------------------------------------------------------ **
268 * Scan a parameter name and value, and pass these two fields to pfunc().
270 * Input: InFile - The input source.
271 * pfunc - A pointer to the function that will be called to
272 * process the parameter, once it has been scanned.
273 * c - The first character of the parameter name, which
274 * would have been read by Parse(). Unlike a comment
275 * line or a section header, there is no lead-in
276 * character that can be discarded.
278 * Output: True if the parameter name and value were scanned and processed
279 * successfully, else False.
281 * Notes: This function is in two parts. The first loop scans the
282 * parameter name. Internal whitespace is compressed, and an
283 * equal sign (=) terminates the token. Leading and trailing
284 * whitespace is discarded. The second loop scans the parameter
285 * value. When both have been successfully identified, they are
286 * passed to pfunc() for processing.
288 * ------------------------------------------------------------------------ **
291 int i = 0; /* Position within bufr. */
292 int end = 0; /* bufr[end] is current end-of-string. */
293 int vstart = 0; /* Starting position of the parameter value. */
294 char *func = "params.c:Parameter() -";
296 /* Read the parameter name. */
297 while( 0 == vstart ) /* Loop until we've found the start of the value. */
300 if( i > (bSize - 2) ) /* Ensure there's space for next char. */
303 bufr = realloc_array( bufr, char, bSize );
306 rprintf(FERROR, "%s Memory re-allocation failure.", func) ;
313 case '=': /* Equal sign marks end of param name. */
314 if( 0 == end ) /* Don't allow an empty name. */
316 rprintf(FERROR, "%s Invalid parameter name in config. file.\n", func );
319 bufr[end++] = '\0'; /* Mark end of string & advance. */
320 i = end; /* New string starts here. */
321 vstart = end; /* New string is parameter value. */
322 bufr[i] = '\0'; /* New string is nul, for now. */
325 case '\n': /* Find continuation char, else error. */
326 i = Continuation( bufr, i );
330 rprintf(FERROR, "%s Ignoring badly formed line in configuration file: %s\n",
334 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
335 c = getc( InFile ); /* Read past eoln. */
338 case '\0': /* Shouldn't have EOF within param name. */
341 rprintf(FERROR, "%s Unexpected end-of-file at: %s\n", func, bufr );
345 if( isspace( c ) ) /* One ' ' per whitespace region. */
349 c = EatWhitespace( InFile );
351 else /* All others verbatim. */
360 /* Now parse the value. */
361 c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */
362 while( (EOF !=c) && (c > 0) )
365 if( i > (bSize - 2) ) /* Make sure there's enough room. */
368 bufr = realloc_array( bufr, char, bSize );
371 rprintf(FERROR, "%s Memory re-allocation failure.", func) ;
378 case '\r': /* Explicitly remove '\r' because the older */
379 c = getc( InFile ); /* version called fgets_slash() which also */
380 break; /* removes them. */
382 case '\n': /* Marks end of value unless there's a '\'. */
383 i = Continuation( bufr, i );
388 for( end = i; end >= 0 && isSpace(bufr + end); end-- )
394 default: /* All others verbatim. Note that spaces do */
395 bufr[i++] = c; /* not advance <end>. This allows trimming */
396 if( !isspace( c ) ) /* of whitespace at the end of the line. */
402 bufr[end] = '\0'; /* End of value. */
404 return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */
407 static BOOL Parse( FILE *InFile,
408 BOOL (*sfunc)(char *),
409 BOOL (*pfunc)(char *, char *) )
410 /* ------------------------------------------------------------------------ **
411 * Scan & parse the input.
413 * Input: InFile - Input source.
414 * sfunc - Function to be called when a section name is scanned.
416 * pfunc - Function to be called when a parameter is scanned.
419 * Output: True if the file was successfully scanned, else False.
421 * Notes: The input can be viewed in terms of 'lines'. There are four
423 * Blank - May contain whitespace, otherwise empty.
424 * Comment - First non-whitespace character is a ';' or '#'.
425 * The remainder of the line is ignored.
426 * Section - First non-whitespace character is a '['.
427 * Parameter - The default case.
429 * ------------------------------------------------------------------------ **
434 c = EatWhitespace( InFile );
435 while( (EOF != c) && (c > 0) )
439 case '\n': /* Blank line. */
440 c = EatWhitespace( InFile );
443 case ';': /* Comment line. */
445 c = EatComment( InFile );
448 case '[': /* Section Header. */
449 if (!sfunc) return True;
450 if( !Section( InFile, sfunc ) )
452 c = EatWhitespace( InFile );
455 case '\\': /* Bogus backslash. */
456 c = EatWhitespace( InFile );
459 default: /* Parameter line. */
460 if( !Parameter( InFile, pfunc, c ) )
462 c = EatWhitespace( InFile );
469 static FILE *OpenConfFile( char *FileName )
470 /* ------------------------------------------------------------------------ **
471 * Open a configuration file.
473 * Input: FileName - The pathname of the config file to be opened.
475 * Output: A pointer of type (FILE *) to the opened file, or NULL if the
476 * file could not be opened.
478 * ------------------------------------------------------------------------ **
482 char *func = "params.c:OpenConfFile() -";
484 if( NULL == FileName || 0 == *FileName )
486 rprintf(FERROR,"%s No configuration filename specified.\n", func);
490 OpenedFile = fopen( FileName, "r" );
491 if( NULL == OpenedFile )
493 rsyserr(FERROR, errno, "unable to open configuration file \"%s\"",
497 return( OpenedFile );
500 BOOL pm_process( char *FileName,
501 BOOL (*sfunc)(char *),
502 BOOL (*pfunc)(char *, char *) )
503 /* ------------------------------------------------------------------------ **
504 * Process the named parameter file.
506 * Input: FileName - The pathname of the parameter file to be opened.
507 * sfunc - A pointer to a function that will be called when
508 * a section name is discovered.
509 * pfunc - A pointer to a function that will be called when
510 * a parameter name and value are discovered.
512 * Output: TRUE if the file was successfully parsed, else FALSE.
514 * ------------------------------------------------------------------------ **
519 char *func = "params.c:pm_process() -";
521 InFile = OpenConfFile( FileName ); /* Open the config file. */
525 if( NULL != bufr ) /* If we already have a buffer */
526 result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */
529 else /* If we don't have a buffer */
530 { /* allocate one, then parse, */
531 bSize = BUFR_INC; /* then free. */
532 bufr = new_array( char, bSize );
535 rprintf(FERROR,"%s memory allocation failure.\n", func);
539 result = Parse( InFile, sfunc, pfunc );
547 if( !result ) /* Generic failure. */
549 rprintf(FERROR,"%s Failed. Error returned from params.c:parse().\n", func);
553 return( True ); /* Generic success. */
556 /* -------------------------------------------------------------------------- */