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