Martin gave his approval to use GPLv3 with this code.
[rsync/rsync.git] / params.c
CommitLineData
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
4fd842f9 6 * it under the terms of the GNU General Public License version 3 as
ba2133d6 7 * published by the Free Software Foundation.
0b76cd63
AT
8 *
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.
13 *
e7c67065 14 * You should have received a copy of the GNU General Public License along
4fd842f9 15 * with this program; if not, visit the http://fsf.org website.
0f78b815
WD
16 */
17
18/* -------------------------------------------------------------------------- **
0b76cd63
AT
19 *
20 * Module name: params
21 *
22 * -------------------------------------------------------------------------- **
23 *
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
28 * internally.
29 *
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.
34 *
35 * A sample parameter file might look like this:
36 *
37 * [section one]
38 * parameter one = value string
39 * parameter two = another value
40 * [section two]
41 * new parameter = some value or t'other
42 *
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
47 * syntax is:
48 *
49 * <file> :== { <section> } EOF
50 *
51 * <section> :== <section header> { <parameter line> }
52 *
53 * <section header> :== '[' NAME ']'
54 *
55 * <parameter line> :== NAME '=' VALUE '\n'
56 *
57 * Blank lines and comment lines are ignored. Comment lines are lines
58 * beginning with either a semicolon (';') or a pound sign ('#').
59 *
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.
63 *
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.
71 *
72 * -------------------------------------------------------------------------- **
73 */
74
75#include "rsync.h"
0b76cd63
AT
76
77/* -------------------------------------------------------------------------- **
78 * Constants...
79 */
80
81#define BUFR_INC 1024
82
83
84/* -------------------------------------------------------------------------- **
85 * Variables...
86 *
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>.
90 */
91
92static char *bufr = NULL;
93static int bSize = 0;
94
95/* -------------------------------------------------------------------------- **
96 * Functions...
97 */
98
99static int EatWhitespace( FILE *InFile )
100 /* ------------------------------------------------------------------------ **
101 * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
102 * character, or newline, or EOF.
103 *
104 * Input: InFile - Input source.
105 *
106 * Output: The next non-whitespace character in the input stream.
107 *
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.
113 *
114 * ------------------------------------------------------------------------ **
115 */
116 {
117 int c;
118
119 for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) )
120 ;
121 return( c );
122 } /* EatWhitespace */
123
124static int EatComment( FILE *InFile )
125 /* ------------------------------------------------------------------------ **
126 * Scan to the end of a comment.
127 *
128 * Input: InFile - Input source.
129 *
130 * Output: The character that marks the end of the comment. Normally,
131 * this will be a newline, but it *might* be an EOF.
132 *
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.
138 *
139 * ------------------------------------------------------------------------ **
140 */
141 {
142 int c;
143
144 for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) )
145 ;
146 return( c );
147 } /* EatComment */
148
149static 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 ('\\').
153 *
154 * Input: line - A pointer to a buffer containing the string to be
155 * scanned.
156 * pos - This is taken to be the offset of the end of the
157 * string. This position is *not* scanned.
158 *
159 * Output: The offset of the '\\' character if it was found, or -1 to
160 * indicate that it was not.
161 *
162 * ------------------------------------------------------------------------ **
163 */
164 {
165 pos--;
2dc7b8bd 166 while( pos >= 0 && isSpace(line + pos) )
0b76cd63
AT
167 pos--;
168
169 return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
170 } /* Continuation */
171
172
173static BOOL Section( FILE *InFile, BOOL (*sfunc)(char *) )
174 /* ------------------------------------------------------------------------ **
175 * Scan a section name, and pass the name to function sfunc().
176 *
177 * Input: InFile - Input source.
178 * sfunc - Pointer to the function to be called if the section
179 * name is successfully read.
180 *
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
183 * encountered.
184 *
185 * ------------------------------------------------------------------------ **
186 */
187 {
188 int c;
189 int i;
190 int end;
191 char *func = "params.c:Section() -";
192
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>. */
198
199 c = EatWhitespace( InFile ); /* We've already got the '['. Scan */
200 /* past initial white space. */
201
202 while( (EOF != c) && (c > 0) )
203 {
204
205 /* Check that the buffer is big enough for the next character. */
206 if( i > (bSize - 2) )
207 {
208 bSize += BUFR_INC;
58cadc86 209 bufr = realloc_array( bufr, char, bSize );
0b76cd63
AT
210 if( NULL == bufr )
211 {
212 rprintf(FERROR, "%s Memory re-allocation failure.", func);
213 return( False );
214 }
215 }
216
217 /* Handle a single character. */
218 switch( c )
219 {
220 case ']': /* Found the closing bracket. */
221 bufr[end] = '\0';
222 if( 0 == end ) /* Don't allow an empty name. */
223 {
224 rprintf(FERROR, "%s Empty section name in configuration file.\n", func );
225 return( False );
226 }
227 if( !sfunc( bufr ) ) /* Got a valid name. Deal with it. */
228 return( False );
229 (void)EatComment( InFile ); /* Finish off the line. */
230 return( True );
231
232 case '\n': /* Got newline before closing ']'. */
233 i = Continuation( bufr, i ); /* Check for line continuation. */
234 if( i < 0 )
235 {
236 bufr[end] = '\0';
237 rprintf(FERROR, "%s Badly formed line in configuration file: %s\n",
238 func, bufr );
239 return( False );
240 }
241 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
242 c = getc( InFile ); /* Continue with next line. */
243 break;
244
245 default: /* All else are a valid name chars. */
246 if( isspace( c ) ) /* One space per whitespace region. */
247 {
248 bufr[end] = ' ';
249 i = end + 1;
250 c = EatWhitespace( InFile );
251 }
252 else /* All others copy verbatim. */
253 {
254 bufr[i++] = c;
255 end = i;
256 c = getc( InFile );
257 }
258 }
259 }
260
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 );
263 return( False );
264 } /* Section */
265
266static 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().
269 *
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.
277 *
278 * Output: True if the parameter name and value were scanned and processed
279 * successfully, else False.
280 *
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.
287 *
288 * ------------------------------------------------------------------------ **
289 */
290 {
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() -";
295
296 /* Read the parameter name. */
297 while( 0 == vstart ) /* Loop until we've found the start of the value. */
298 {
299
300 if( i > (bSize - 2) ) /* Ensure there's space for next char. */
301 {
302 bSize += BUFR_INC;
58cadc86 303 bufr = realloc_array( bufr, char, bSize );
0b76cd63
AT
304 if( NULL == bufr )
305 {
306 rprintf(FERROR, "%s Memory re-allocation failure.", func) ;
307 return( False );
308 }
309 }
310
311 switch( c )
312 {
313 case '=': /* Equal sign marks end of param name. */
314 if( 0 == end ) /* Don't allow an empty name. */
315 {
316 rprintf(FERROR, "%s Invalid parameter name in config. file.\n", func );
317 return( False );
318 }
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. */
323 break;
324
325 case '\n': /* Find continuation char, else error. */
326 i = Continuation( bufr, i );
327 if( i < 0 )
328 {
329 bufr[end] = '\0';
330 rprintf(FERROR, "%s Ignoring badly formed line in configuration file: %s\n",
331 func, bufr );
332 return( True );
333 }
334 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
335 c = getc( InFile ); /* Read past eoln. */
336 break;
337
338 case '\0': /* Shouldn't have EOF within param name. */
339 case EOF:
340 bufr[i] = '\0';
341 rprintf(FERROR, "%s Unexpected end-of-file at: %s\n", func, bufr );
342 return( True );
343
344 default:
345 if( isspace( c ) ) /* One ' ' per whitespace region. */
346 {
347 bufr[end] = ' ';
348 i = end + 1;
349 c = EatWhitespace( InFile );
350 }
351 else /* All others verbatim. */
352 {
353 bufr[i++] = c;
354 end = i;
355 c = getc( InFile );
356 }
357 }
358 }
359
360 /* Now parse the value. */
361 c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */
362 while( (EOF !=c) && (c > 0) )
363 {
364
365 if( i > (bSize - 2) ) /* Make sure there's enough room. */
366 {
367 bSize += BUFR_INC;
58cadc86 368 bufr = realloc_array( bufr, char, bSize );
0b76cd63
AT
369 if( NULL == bufr )
370 {
371 rprintf(FERROR, "%s Memory re-allocation failure.", func) ;
372 return( False );
373 }
374 }
375
376 switch( c )
377 {
378 case '\r': /* Explicitly remove '\r' because the older */
379 c = getc( InFile ); /* version called fgets_slash() which also */
380 break; /* removes them. */
381
382 case '\n': /* Marks end of value unless there's a '\'. */
383 i = Continuation( bufr, i );
384 if( i < 0 )
385 c = 0;
386 else
387 {
2dc7b8bd 388 for( end = i; end >= 0 && isSpace(bufr + end); end-- )
0b76cd63
AT
389 ;
390 c = getc( InFile );
391 }
392 break;
393
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. */
397 end = i;
398 c = getc( InFile );
399 break;
400 }
401 }
402 bufr[end] = '\0'; /* End of value. */
403
404 return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */
405 } /* Parameter */
406
407static BOOL Parse( FILE *InFile,
408 BOOL (*sfunc)(char *),
409 BOOL (*pfunc)(char *, char *) )
410 /* ------------------------------------------------------------------------ **
411 * Scan & parse the input.
412 *
413 * Input: InFile - Input source.
414 * sfunc - Function to be called when a section name is scanned.
415 * See Section().
416 * pfunc - Function to be called when a parameter is scanned.
417 * See Parameter().
418 *
419 * Output: True if the file was successfully scanned, else False.
420 *
421 * Notes: The input can be viewed in terms of 'lines'. There are four
422 * types of lines:
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.
428 *
429 * ------------------------------------------------------------------------ **
430 */
431 {
432 int c;
433
434 c = EatWhitespace( InFile );
435 while( (EOF != c) && (c > 0) )
436 {
437 switch( c )
438 {
439 case '\n': /* Blank line. */
440 c = EatWhitespace( InFile );
441 break;
442
443 case ';': /* Comment line. */
444 case '#':
445 c = EatComment( InFile );
446 break;
447
448 case '[': /* Section Header. */
f9e940ef
AT
449 if (!sfunc) return True;
450 if( !Section( InFile, sfunc ) )
451 return( False );
452 c = EatWhitespace( InFile );
453 break;
0b76cd63
AT
454
455 case '\\': /* Bogus backslash. */
456 c = EatWhitespace( InFile );
457 break;
458
459 default: /* Parameter line. */
460 if( !Parameter( InFile, pfunc, c ) )
461 return( False );
462 c = EatWhitespace( InFile );
463 break;
464 }
465 }
466 return( True );
467 } /* Parse */
468
469static FILE *OpenConfFile( char *FileName )
470 /* ------------------------------------------------------------------------ **
471 * Open a configuration file.
472 *
473 * Input: FileName - The pathname of the config file to be opened.
474 *
475 * Output: A pointer of type (FILE *) to the opened file, or NULL if the
476 * file could not be opened.
477 *
478 * ------------------------------------------------------------------------ **
479 */
480 {
481 FILE *OpenedFile;
482 char *func = "params.c:OpenConfFile() -";
483
484 if( NULL == FileName || 0 == *FileName )
485 {
486 rprintf(FERROR,"%s No configuration filename specified.\n", func);
487 return( NULL );
488 }
489
0090cbdb 490 OpenedFile = fopen( FileName, "r" );
0b76cd63
AT
491 if( NULL == OpenedFile )
492 {
415b5983 493 rsyserr(FERROR, errno, "unable to open configuration file \"%s\"",
45c49b52 494 FileName);
0b76cd63
AT
495 }
496
497 return( OpenedFile );
498 } /* OpenConfFile */
499
500BOOL pm_process( char *FileName,
501 BOOL (*sfunc)(char *),
502 BOOL (*pfunc)(char *, char *) )
503 /* ------------------------------------------------------------------------ **
504 * Process the named parameter file.
505 *
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.
511 *
512 * Output: TRUE if the file was successfully parsed, else FALSE.
513 *
514 * ------------------------------------------------------------------------ **
515 */
516 {
517 int result;
518 FILE *InFile;
519 char *func = "params.c:pm_process() -";
520
521 InFile = OpenConfFile( FileName ); /* Open the config file. */
522 if( NULL == InFile )
523 return( False );
524
525 if( NULL != bufr ) /* If we already have a buffer */
526 result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */
527 /* use it. */
528
529 else /* If we don't have a buffer */
530 { /* allocate one, then parse, */
531 bSize = BUFR_INC; /* then free. */
58cadc86 532 bufr = new_array( char, bSize );
0b76cd63
AT
533 if( NULL == bufr )
534 {
535 rprintf(FERROR,"%s memory allocation failure.\n", func);
536 fclose(InFile);
537 return( False );
538 }
539 result = Parse( InFile, sfunc, pfunc );
540 free( bufr );
541 bufr = NULL;
542 bSize = 0;
543 }
544
545 fclose(InFile);
546
547 if( !result ) /* Generic failure. */
548 {
549 rprintf(FERROR,"%s Failed. Error returned from params.c:parse().\n", func);
550 return( False );
551 }
552
553 return( True ); /* Generic success. */
554 } /* pm_process */
555
556/* -------------------------------------------------------------------------- */
f9e940ef 557