Tweaked some whitespace to match the latest version from autoconf.
[rsync/rsync.git] / params.c
CommitLineData
0b76cd63
AT
1/*
2 This modules is based on the params.c module from Samba, written by Karl Auer
3 and much modifed by Christopher Hertel.
4
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
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 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 *
19 * -------------------------------------------------------------------------- **
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"
0b76cd63
AT
77
78/* -------------------------------------------------------------------------- **
79 * Constants...
80 */
81
82#define BUFR_INC 1024
83
84
85/* -------------------------------------------------------------------------- **
86 * Variables...
87 *
88 * bufr - pointer to a global buffer. This is probably a kludge,
89 * but it was the nicest kludge I could think of (for now).
90 * bSize - The size of the global buffer <bufr>.
91 */
92
93static char *bufr = NULL;
94static int bSize = 0;
95
96/* -------------------------------------------------------------------------- **
97 * Functions...
98 */
99
100static int EatWhitespace( FILE *InFile )
101 /* ------------------------------------------------------------------------ **
102 * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
103 * character, or newline, or EOF.
104 *
105 * Input: InFile - Input source.
106 *
107 * Output: The next non-whitespace character in the input stream.
108 *
109 * Notes: Because the config files use a line-oriented grammar, we
110 * explicitly exclude the newline character from the list of
111 * whitespace characters.
112 * - Note that both EOF (-1) and the nul character ('\0') are
113 * considered end-of-file markers.
114 *
115 * ------------------------------------------------------------------------ **
116 */
117 {
118 int c;
119
120 for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) )
121 ;
122 return( c );
123 } /* EatWhitespace */
124
125static int EatComment( FILE *InFile )
126 /* ------------------------------------------------------------------------ **
127 * Scan to the end of a comment.
128 *
129 * Input: InFile - Input source.
130 *
131 * Output: The character that marks the end of the comment. Normally,
132 * this will be a newline, but it *might* be an EOF.
133 *
134 * Notes: Because the config files use a line-oriented grammar, we
135 * explicitly exclude the newline character from the list of
136 * whitespace characters.
137 * - Note that both EOF (-1) and the nul character ('\0') are
138 * considered end-of-file markers.
139 *
140 * ------------------------------------------------------------------------ **
141 */
142 {
143 int c;
144
145 for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) )
146 ;
147 return( c );
148 } /* EatComment */
149
150static int Continuation( char *line, int pos )
151 /* ------------------------------------------------------------------------ **
152 * Scan backards within a string to discover if the last non-whitespace
153 * character is a line-continuation character ('\\').
154 *
155 * Input: line - A pointer to a buffer containing the string to be
156 * scanned.
157 * pos - This is taken to be the offset of the end of the
158 * string. This position is *not* scanned.
159 *
160 * Output: The offset of the '\\' character if it was found, or -1 to
161 * indicate that it was not.
162 *
163 * ------------------------------------------------------------------------ **
164 */
165 {
166 pos--;
32f76175 167 while( (pos >= 0) && isspace(((unsigned char *)line)[pos]) )
0b76cd63
AT
168 pos--;
169
170 return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
171 } /* Continuation */
172
173
174static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) )
175 /* ------------------------------------------------------------------------ **
176 * Scan a section name, and pass the name to function sfunc().
177 *
178 * Input: InFile - Input source.
179 * sfunc - Pointer to the function to be called if the section
180 * name is successfully read.
181 *
182 * Output: True if the section name was read and True was returned from
183 * <sfunc>. False if <sfunc> failed or if a lexical error was
184 * encountered.
185 *
186 * ------------------------------------------------------------------------ **
187 */
188 {
189 int c;
190 int i;
191 int end;
192 char *func = "params.c:Section() -";
193
194 i = 0; /* <i> is the offset of the next free byte in bufr[] and */
195 end = 0; /* <end> is the current "end of string" offset. In most */
196 /* cases these will be the same, but if the last */
197 /* character written to bufr[] is a space, then <end> */
198 /* will be one less than <i>. */
199
200 c = EatWhitespace( InFile ); /* We've already got the '['. Scan */
201 /* past initial white space. */
202
203 while( (EOF != c) && (c > 0) )
204 {
205
206 /* Check that the buffer is big enough for the next character. */
207 if( i > (bSize - 2) )
208 {
209 bSize += BUFR_INC;
58cadc86 210 bufr = realloc_array( bufr, char, bSize );
0b76cd63
AT
211 if( NULL == bufr )
212 {
213 rprintf(FERROR, "%s Memory re-allocation failure.", func);
214 return( False );
215 }
216 }
217
218 /* Handle a single character. */
219 switch( c )
220 {
221 case ']': /* Found the closing bracket. */
222 bufr[end] = '\0';
223 if( 0 == end ) /* Don't allow an empty name. */
224 {
225 rprintf(FERROR, "%s Empty section name in configuration file.\n", func );
226 return( False );
227 }
228 if( !sfunc( bufr ) ) /* Got a valid name. Deal with it. */
229 return( False );
230 (void)EatComment( InFile ); /* Finish off the line. */
231 return( True );
232
233 case '\n': /* Got newline before closing ']'. */
234 i = Continuation( bufr, i ); /* Check for line continuation. */
235 if( i < 0 )
236 {
237 bufr[end] = '\0';
238 rprintf(FERROR, "%s Badly formed line in configuration file: %s\n",
239 func, bufr );
240 return( False );
241 }
242 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
243 c = getc( InFile ); /* Continue with next line. */
244 break;
245
246 default: /* All else are a valid name chars. */
247 if( isspace( c ) ) /* One space per whitespace region. */
248 {
249 bufr[end] = ' ';
250 i = end + 1;
251 c = EatWhitespace( InFile );
252 }
253 else /* All others copy verbatim. */
254 {
255 bufr[i++] = c;
256 end = i;
257 c = getc( InFile );
258 }
259 }
260 }
261
262 /* We arrive here if we've met the EOF before the closing bracket. */
263 rprintf(FERROR, "%s Unexpected EOF in the configuration file: %s\n", func, bufr );
264 return( False );
265 } /* Section */
266
267static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(char *, char *), int c )
268 /* ------------------------------------------------------------------------ **
269 * Scan a parameter name and value, and pass these two fields to pfunc().
270 *
271 * Input: InFile - The input source.
272 * pfunc - A pointer to the function that will be called to
273 * process the parameter, once it has been scanned.
274 * c - The first character of the parameter name, which
275 * would have been read by Parse(). Unlike a comment
276 * line or a section header, there is no lead-in
277 * character that can be discarded.
278 *
279 * Output: True if the parameter name and value were scanned and processed
280 * successfully, else False.
281 *
282 * Notes: This function is in two parts. The first loop scans the
283 * parameter name. Internal whitespace is compressed, and an
284 * equal sign (=) terminates the token. Leading and trailing
285 * whitespace is discarded. The second loop scans the parameter
286 * value. When both have been successfully identified, they are
287 * passed to pfunc() for processing.
288 *
289 * ------------------------------------------------------------------------ **
290 */
291 {
292 int i = 0; /* Position within bufr. */
293 int end = 0; /* bufr[end] is current end-of-string. */
294 int vstart = 0; /* Starting position of the parameter value. */
295 char *func = "params.c:Parameter() -";
296
297 /* Read the parameter name. */
298 while( 0 == vstart ) /* Loop until we've found the start of the value. */
299 {
300
301 if( i > (bSize - 2) ) /* Ensure there's space for next char. */
302 {
303 bSize += BUFR_INC;
58cadc86 304 bufr = realloc_array( bufr, char, bSize );
0b76cd63
AT
305 if( NULL == bufr )
306 {
307 rprintf(FERROR, "%s Memory re-allocation failure.", func) ;
308 return( False );
309 }
310 }
311
312 switch( c )
313 {
314 case '=': /* Equal sign marks end of param name. */
315 if( 0 == end ) /* Don't allow an empty name. */
316 {
317 rprintf(FERROR, "%s Invalid parameter name in config. file.\n", func );
318 return( False );
319 }
320 bufr[end++] = '\0'; /* Mark end of string & advance. */
321 i = end; /* New string starts here. */
322 vstart = end; /* New string is parameter value. */
323 bufr[i] = '\0'; /* New string is nul, for now. */
324 break;
325
326 case '\n': /* Find continuation char, else error. */
327 i = Continuation( bufr, i );
328 if( i < 0 )
329 {
330 bufr[end] = '\0';
331 rprintf(FERROR, "%s Ignoring badly formed line in configuration file: %s\n",
332 func, bufr );
333 return( True );
334 }
335 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
336 c = getc( InFile ); /* Read past eoln. */
337 break;
338
339 case '\0': /* Shouldn't have EOF within param name. */
340 case EOF:
341 bufr[i] = '\0';
342 rprintf(FERROR, "%s Unexpected end-of-file at: %s\n", func, bufr );
343 return( True );
344
345 default:
346 if( isspace( c ) ) /* One ' ' per whitespace region. */
347 {
348 bufr[end] = ' ';
349 i = end + 1;
350 c = EatWhitespace( InFile );
351 }
352 else /* All others verbatim. */
353 {
354 bufr[i++] = c;
355 end = i;
356 c = getc( InFile );
357 }
358 }
359 }
360
361 /* Now parse the value. */
362 c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */
363 while( (EOF !=c) && (c > 0) )
364 {
365
366 if( i > (bSize - 2) ) /* Make sure there's enough room. */
367 {
368 bSize += BUFR_INC;
58cadc86 369 bufr = realloc_array( bufr, char, bSize );
0b76cd63
AT
370 if( NULL == bufr )
371 {
372 rprintf(FERROR, "%s Memory re-allocation failure.", func) ;
373 return( False );
374 }
375 }
376
377 switch( c )
378 {
379 case '\r': /* Explicitly remove '\r' because the older */
380 c = getc( InFile ); /* version called fgets_slash() which also */
381 break; /* removes them. */
382
383 case '\n': /* Marks end of value unless there's a '\'. */
384 i = Continuation( bufr, i );
385 if( i < 0 )
386 c = 0;
387 else
388 {
32f76175 389 for( end = i; (end >= 0) && isspace(((unsigned char *) bufr)[end]); end-- )
0b76cd63
AT
390 ;
391 c = getc( InFile );
392 }
393 break;
394
395 default: /* All others verbatim. Note that spaces do */
396 bufr[i++] = c; /* not advance <end>. This allows trimming */
397 if( !isspace( c ) ) /* of whitespace at the end of the line. */
398 end = i;
399 c = getc( InFile );
400 break;
401 }
402 }
403 bufr[end] = '\0'; /* End of value. */
404
405 return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */
406 } /* Parameter */
407
408static BOOL Parse( FILE *InFile,
409 BOOL (*sfunc)(char *),
410 BOOL (*pfunc)(char *, char *) )
411 /* ------------------------------------------------------------------------ **
412 * Scan & parse the input.
413 *
414 * Input: InFile - Input source.
415 * sfunc - Function to be called when a section name is scanned.
416 * See Section().
417 * pfunc - Function to be called when a parameter is scanned.
418 * See Parameter().
419 *
420 * Output: True if the file was successfully scanned, else False.
421 *
422 * Notes: The input can be viewed in terms of 'lines'. There are four
423 * types of lines:
424 * Blank - May contain whitespace, otherwise empty.
425 * Comment - First non-whitespace character is a ';' or '#'.
426 * The remainder of the line is ignored.
427 * Section - First non-whitespace character is a '['.
428 * Parameter - The default case.
429 *
430 * ------------------------------------------------------------------------ **
431 */
432 {
433 int c;
434
435 c = EatWhitespace( InFile );
436 while( (EOF != c) && (c > 0) )
437 {
438 switch( c )
439 {
440 case '\n': /* Blank line. */
441 c = EatWhitespace( InFile );
442 break;
443
444 case ';': /* Comment line. */
445 case '#':
446 c = EatComment( InFile );
447 break;
448
449 case '[': /* Section Header. */
f9e940ef
AT
450 if (!sfunc) return True;
451 if( !Section( InFile, sfunc ) )
452 return( False );
453 c = EatWhitespace( InFile );
454 break;
0b76cd63
AT
455
456 case '\\': /* Bogus backslash. */
457 c = EatWhitespace( InFile );
458 break;
459
460 default: /* Parameter line. */
461 if( !Parameter( InFile, pfunc, c ) )
462 return( False );
463 c = EatWhitespace( InFile );
464 break;
465 }
466 }
467 return( True );
468 } /* Parse */
469
470static FILE *OpenConfFile( char *FileName )
471 /* ------------------------------------------------------------------------ **
472 * Open a configuration file.
473 *
474 * Input: FileName - The pathname of the config file to be opened.
475 *
476 * Output: A pointer of type (FILE *) to the opened file, or NULL if the
477 * file could not be opened.
478 *
479 * ------------------------------------------------------------------------ **
480 */
481 {
482 FILE *OpenedFile;
483 char *func = "params.c:OpenConfFile() -";
484
485 if( NULL == FileName || 0 == *FileName )
486 {
487 rprintf(FERROR,"%s No configuration filename specified.\n", func);
488 return( NULL );
489 }
490
0090cbdb 491 OpenedFile = fopen( FileName, "r" );
0b76cd63
AT
492 if( NULL == OpenedFile )
493 {
415b5983 494 rsyserr(FERROR, errno, "unable to open configuration file \"%s\"",
45c49b52 495 FileName);
0b76cd63
AT
496 }
497
498 return( OpenedFile );
499 } /* OpenConfFile */
500
501BOOL pm_process( char *FileName,
502 BOOL (*sfunc)(char *),
503 BOOL (*pfunc)(char *, char *) )
504 /* ------------------------------------------------------------------------ **
505 * Process the named parameter file.
506 *
507 * Input: FileName - The pathname of the parameter file to be opened.
508 * sfunc - A pointer to a function that will be called when
509 * a section name is discovered.
510 * pfunc - A pointer to a function that will be called when
511 * a parameter name and value are discovered.
512 *
513 * Output: TRUE if the file was successfully parsed, else FALSE.
514 *
515 * ------------------------------------------------------------------------ **
516 */
517 {
518 int result;
519 FILE *InFile;
520 char *func = "params.c:pm_process() -";
521
522 InFile = OpenConfFile( FileName ); /* Open the config file. */
523 if( NULL == InFile )
524 return( False );
525
526 if( NULL != bufr ) /* If we already have a buffer */
527 result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */
528 /* use it. */
529
530 else /* If we don't have a buffer */
531 { /* allocate one, then parse, */
532 bSize = BUFR_INC; /* then free. */
58cadc86 533 bufr = new_array( char, bSize );
0b76cd63
AT
534 if( NULL == bufr )
535 {
536 rprintf(FERROR,"%s memory allocation failure.\n", func);
537 fclose(InFile);
538 return( False );
539 }
540 result = Parse( InFile, sfunc, pfunc );
541 free( bufr );
542 bufr = NULL;
543 bSize = 0;
544 }
545
546 fclose(InFile);
547
548 if( !result ) /* Generic failure. */
549 {
550 rprintf(FERROR,"%s Failed. Error returned from params.c:parse().\n", func);
551 return( False );
552 }
553
554 return( True ); /* Generic success. */
555 } /* pm_process */
556
557/* -------------------------------------------------------------------------- */
f9e940ef 558