param/params.c

説明を見る。
00001 /* -------------------------------------------------------------------------- **
00002  * Microsoft Network Services for Unix, AKA., Andrew Tridgell's SAMBA.
00003  *
00004  * This module Copyright (C) 1990-1998 Karl Auer
00005  *
00006  * Rewritten almost completely by Christopher R. Hertel, 1997.
00007  * This module Copyright (C) 1997-1998 by Christopher R. Hertel
00008  * 
00009  * -------------------------------------------------------------------------- **
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00024  *
00025  * -------------------------------------------------------------------------- **
00026  *
00027  * Module name: params
00028  *
00029  * -------------------------------------------------------------------------- **
00030  *
00031  *  This module performs lexical analysis and initial parsing of a
00032  *  Windows-like parameter file.  It recognizes and handles four token
00033  *  types:  section-name, parameter-name, parameter-value, and
00034  *  end-of-file.  Comments and line continuation are handled
00035  *  internally.
00036  *
00037  *  The entry point to the module is function pm_process().  This
00038  *  function opens the source file, calls the Parse() function to parse
00039  *  the input, and then closes the file when either the EOF is reached
00040  *  or a fatal error is encountered.
00041  *
00042  *  A sample parameter file might look like this:
00043  *
00044  *  [section one]
00045  *  parameter one = value string
00046  *  parameter two = another value
00047  *  [section two]
00048  *  new parameter = some value or t'other
00049  *
00050  *  The parameter file is divided into sections by section headers:
00051  *  section names enclosed in square brackets (eg. [section one]).
00052  *  Each section contains parameter lines, each of which consist of a
00053  *  parameter name and value delimited by an equal sign.  Roughly, the
00054  *  syntax is:
00055  *
00056  *    <file>            :==  { <section> } EOF
00057  *
00058  *    <section>         :==  <section header> { <parameter line> }
00059  *
00060  *    <section header>  :==  '[' NAME ']'
00061  *
00062  *    <parameter line>  :==  NAME '=' VALUE '\n'
00063  *
00064  *  Blank lines and comment lines are ignored.  Comment lines are lines
00065  *  beginning with either a semicolon (';') or a pound sign ('#').
00066  *
00067  *  All whitespace in section names and parameter names is compressed
00068  *  to single spaces.  Leading and trailing whitespace is stipped from
00069  *  both names and values.
00070  *
00071  *  Only the first equals sign in a parameter line is significant.
00072  *  Parameter values may contain equals signs, square brackets and
00073  *  semicolons.  Internal whitespace is retained in parameter values,
00074  *  with the exception of the '\r' character, which is stripped for
00075  *  historic reasons.  Parameter names may not start with a left square
00076  *  bracket, an equal sign, a pound sign, or a semicolon, because these
00077  *  are used to identify other tokens.
00078  *
00079  * -------------------------------------------------------------------------- **
00080  */
00081 
00082 #include "includes.h"
00083 
00084 extern BOOL in_client;
00085 
00086 /* -------------------------------------------------------------------------- **
00087  * Constants...
00088  */
00089 
00090 #define BUFR_INC 1024
00091 
00092 
00093 /* -------------------------------------------------------------------------- **
00094  * Variables...
00095  *
00096  *  DEBUGLEVEL  - The ubiquitous DEBUGLEVEL.  This determines which DEBUG()
00097  *                messages will be produced.
00098  *  bufr        - pointer to a global buffer.  This is probably a kludge,
00099  *                but it was the nicest kludge I could think of (for now).
00100  *  bSize       - The size of the global buffer <bufr>.
00101  */
00102 
00103 static char *bufr  = NULL;
00104 static int   bSize = 0;
00105 
00106 /* we can't use FILE* due to the 256 fd limit - use this cheap hack
00107    instead */
00108 typedef struct {
00109         char *buf;
00110         char *p;
00111         size_t size;
00112         char *end_section_p;
00113 } myFILE;
00114 
00115 static int mygetc(myFILE *f)
00116 {
00117         if (f->p >= f->buf+f->size)
00118                 return EOF;
00119         /* be sure to return chars >127 as positive values */
00120         return (int)( *(f->p++) & 0x00FF );
00121 }
00122 
00123 static void myfile_close(myFILE *f)
00124 {
00125         if (!f)
00126                 return;
00127         SAFE_FREE(f->buf);
00128         SAFE_FREE(f);
00129 }
00130 
00131 /* Find the end of the section. We must use mb functions for this. */
00132 static int FindSectionEnd(myFILE *f)
00133 {
00134         f->end_section_p = strchr_m(f->p, ']');
00135         return f->end_section_p ? 1 : 0;
00136 }
00137 
00138 static int AtSectionEnd(myFILE *f)
00139 {
00140         if (f->p == f->end_section_p + 1) {
00141                 f->end_section_p = NULL;
00142                 return 1;
00143         }
00144         return 0;
00145 }
00146 
00147 /* -------------------------------------------------------------------------- **
00148  * Functions...
00149  */
00150   /* ------------------------------------------------------------------------ **
00151    * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
00152    * character, or newline, or EOF.
00153    *
00154    *  Input:  InFile  - Input source.
00155    *
00156    *  Output: The next non-whitespace character in the input stream.
00157    *
00158    *  Notes:  Because the config files use a line-oriented grammar, we
00159    *          explicitly exclude the newline character from the list of
00160    *          whitespace characters.
00161    *        - Note that both EOF (-1) and the nul character ('\0') are
00162    *          considered end-of-file markers.
00163    *
00164    * ------------------------------------------------------------------------ **
00165    */
00166  
00167 static int EatWhitespace( myFILE *InFile )
00168 {
00169         int c;
00170 
00171         for( c = mygetc( InFile ); isspace( c ) && ('\n' != c); c = mygetc( InFile ) )
00172                 ;
00173         return( c );
00174 }
00175 
00176   /* ------------------------------------------------------------------------ **
00177    * Scan to the end of a comment.
00178    *
00179    *  Input:  InFile  - Input source.
00180    *
00181    *  Output: The character that marks the end of the comment.  Normally,
00182    *          this will be a newline, but it *might* be an EOF.
00183    *
00184    *  Notes:  Because the config files use a line-oriented grammar, we
00185    *          explicitly exclude the newline character from the list of
00186    *          whitespace characters.
00187    *        - Note that both EOF (-1) and the nul character ('\0') are
00188    *          considered end-of-file markers.
00189    *
00190    * ------------------------------------------------------------------------ **
00191    */
00192 
00193 static int EatComment( myFILE *InFile )
00194 {
00195         int c;
00196 
00197         for( c = mygetc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = mygetc( InFile ) )
00198                 ;
00199         return( c );
00200 }
00201 
00202 /*****************************************************************************
00203  * Scan backards within a string to discover if the last non-whitespace
00204  * character is a line-continuation character ('\\').
00205  *
00206  *  Input:  line  - A pointer to a buffer containing the string to be
00207  *                  scanned.
00208  *          pos   - This is taken to be the offset of the end of the
00209  *                  string.  This position is *not* scanned.
00210  *
00211  *  Output: The offset of the '\\' character if it was found, or -1 to
00212  *          indicate that it was not.
00213  *
00214  *****************************************************************************/
00215 
00216 static int Continuation(char *line, int pos )
00217 {
00218         pos--;
00219         while( (pos >= 0) && isspace((int)line[pos]))
00220                 pos--;
00221 
00222         return (((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
00223 }
00224 
00225 /* ------------------------------------------------------------------------ **
00226  * Scan a section name, and pass the name to function sfunc().
00227  *
00228  *  Input:  InFile  - Input source.
00229  *          sfunc   - Pointer to the function to be called if the section
00230  *                    name is successfully read.
00231  *
00232  *  Output: True if the section name was read and True was returned from
00233  *          <sfunc>.  False if <sfunc> failed or if a lexical error was
00234  *          encountered.
00235  *
00236  * ------------------------------------------------------------------------ **
00237  */
00238 
00239 static BOOL Section( myFILE *InFile, BOOL (*sfunc)(const char *) )
00240 {
00241         int   c;
00242         int   i;
00243         int   end;
00244         const char *func  = "params.c:Section() -";
00245 
00246         i = 0;      /* <i> is the offset of the next free byte in bufr[] and  */
00247         end = 0;    /* <end> is the current "end of string" offset.  In most  */
00248                     /* cases these will be the same, but if the last          */
00249                     /* character written to bufr[] is a space, then <end>     */
00250                     /* will be one less than <i>.                             */
00251 
00252 
00253         /* Find the end of the section. We must use mb functions for this. */
00254         if (!FindSectionEnd(InFile)) {
00255                 DEBUG(0, ("%s No terminating ']' character in section.\n", func) );
00256                 return False;
00257         }
00258 
00259         c = EatWhitespace( InFile );    /* We've already got the '['.  Scan */
00260                                         /* past initial white space.        */
00261 
00262         while( (EOF != c) && (c > 0) ) {
00263                 /* Check that the buffer is big enough for the next character. */
00264                 if( i > (bSize - 2) ) {
00265                         char *tb = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR( bufr, bSize +BUFR_INC );
00266                         if(!tb) {
00267                                 DEBUG(0, ("%s Memory re-allocation failure.", func) );
00268                                 return False;
00269                         }
00270                         bufr = tb;
00271                         bSize += BUFR_INC;
00272                 }
00273 
00274                 /* Handle a single character other than section end. */
00275                 switch( c ) {
00276                         case '\n': /* Got newline before closing ']'.    */
00277                                 i = Continuation( bufr, i );    /* Check for line continuation.     */
00278                                 if( i < 0 ) {
00279                                         bufr[end] = '\0';
00280                                         DEBUG(0, ("%s Badly formed line in configuration file: %s\n", func, bufr ));
00281                                         return False;
00282                                 }
00283                                 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
00284                                         c = mygetc( InFile );             /* Continue with next line.         */
00285                                 break;
00286 
00287                         default: /* All else are a valid name chars.   */
00288                                 if(isspace( c )) {
00289                                         /* One space per whitespace region. */
00290                                         bufr[end] = ' ';
00291                                         i = end + 1;
00292                                         c = EatWhitespace( InFile );
00293                                 } else {
00294                                         bufr[i++] = c;
00295                                         end = i;
00296                                         c = mygetc( InFile );
00297                                 }
00298                 }
00299 
00300                 if (AtSectionEnd(InFile)) {
00301                         /* Got to the closing bracket. */
00302                         bufr[end] = '\0';
00303                         if( 0 == end ) {
00304                                 /* Don't allow an empty name.       */
00305                                 DEBUG(0, ("%s Empty section name in configuration file.\n", func ));
00306                                 return False;
00307                         }
00308                         if( !sfunc(bufr) )            /* Got a valid name.  Deal with it. */
00309                                 return False;
00310                         EatComment( InFile );     /* Finish off the line.             */
00311                         return True;
00312                 }
00313 
00314         }
00315 
00316         /* We arrive here if we've met the EOF before the closing bracket. */
00317         DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func, bufr ));
00318         return False;
00319 }
00320 
00321 /* ------------------------------------------------------------------------ **
00322  * Scan a parameter name and value, and pass these two fields to pfunc().
00323  *
00324  *  Input:  InFile  - The input source.
00325  *          pfunc   - A pointer to the function that will be called to
00326  *                    process the parameter, once it has been scanned.
00327  *          c       - The first character of the parameter name, which
00328  *                    would have been read by Parse().  Unlike a comment
00329  *                    line or a section header, there is no lead-in
00330  *                    character that can be discarded.
00331  *
00332  *  Output: True if the parameter name and value were scanned and processed
00333  *          successfully, else False.
00334  *
00335  *  Notes:  This function is in two parts.  The first loop scans the
00336  *          parameter name.  Internal whitespace is compressed, and an
00337  *          equal sign (=) terminates the token.  Leading and trailing
00338  *          whitespace is discarded.  The second loop scans the parameter
00339  *          value.  When both have been successfully identified, they are
00340  *          passed to pfunc() for processing.
00341  *
00342  * ------------------------------------------------------------------------ **
00343  */
00344 
00345 static BOOL Parameter( myFILE *InFile, BOOL (*pfunc)(const char *, const char *), int c )
00346 {
00347         int   i       = 0;    /* Position within bufr. */
00348         int   end     = 0;    /* bufr[end] is current end-of-string. */
00349         int   vstart  = 0;    /* Starting position of the parameter value. */
00350         const char *func    = "params.c:Parameter() -";
00351 
00352         /* Read the parameter name. */
00353         while( 0 == vstart ) {
00354                 /* Loop until we've found the start of the value. */
00355                 if( i > (bSize - 2) ) {
00356                         /* Ensure there's space for next char.    */
00357                         char *tb = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR( bufr, bSize + BUFR_INC );
00358                         if (!tb) {
00359                                 DEBUG(0, ("%s Memory re-allocation failure.", func) );
00360                                 return False;
00361                         }
00362                         bufr = tb;
00363                         bSize += BUFR_INC;
00364                 }
00365 
00366                 switch(c) {
00367                         case '=': /* Equal sign marks end of param name. */
00368                                 if( 0 == end ) {
00369                                         /* Don't allow an empty name.      */
00370                                         DEBUG(0, ("%s Invalid parameter name in config. file.\n", func ));
00371                                         return False;
00372                                 }
00373                                 bufr[end++] = '\0';         /* Mark end of string & advance.   */
00374                                 i       = end;              /* New string starts here.         */
00375                                 vstart  = end;              /* New string is parameter value.  */
00376                                 bufr[i] = '\0';             /* New string is nul, for now.     */
00377                                 break;
00378 
00379                         case '\n': /* Find continuation char, else error. */
00380                                 i = Continuation( bufr, i );
00381                                 if( i < 0 ) {
00382                                         bufr[end] = '\0';
00383                                         DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n", func, bufr ));
00384                                         return True;
00385                                 }
00386                                 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
00387                                 c = mygetc( InFile );       /* Read past eoln.                   */
00388                                 break;
00389 
00390                         case '\0': /* Shouldn't have EOF within param name. */
00391                         case EOF:
00392                                 bufr[i] = '\0';
00393                                 DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, bufr ));
00394                                 return True;
00395 
00396                         default:
00397                                 if(isspace( c )) {
00398                                         /* One ' ' per whitespace region.       */
00399                                         bufr[end] = ' ';
00400                                         i = end + 1;
00401                                         c = EatWhitespace( InFile );
00402                                 } else {
00403                                         bufr[i++] = c;
00404                                         end = i;
00405                                         c = mygetc( InFile );
00406                                 }
00407                 }
00408         }
00409 
00410         /* Now parse the value. */
00411         c = EatWhitespace( InFile );  /* Again, trim leading whitespace. */
00412         while( (EOF !=c) && (c > 0) ) {
00413                 if( i > (bSize - 2) ) {
00414                         /* Make sure there's enough room. */
00415                         char *tb = (char *)SMB_REALLOC_KEEP_OLD_ON_ERROR( bufr, bSize + BUFR_INC );
00416                         if (!tb) {
00417                                 DEBUG(0, ("%s Memory re-allocation failure.", func));
00418                                 return False;
00419                         }
00420                         bufr = tb;
00421                         bSize += BUFR_INC;
00422                 }
00423 
00424                 switch(c) {
00425                         case '\r': /* Explicitly remove '\r' because the older */
00426                                 c = mygetc( InFile );   /* version called fgets_slash() which also  */
00427                                 break;                /* removes them.                            */
00428 
00429                         case '\n': /* Marks end of value unless there's a '\'. */
00430                                 i = Continuation( bufr, i );
00431                                 if( i < 0 ) {
00432                                         c = 0;
00433                                 } else {
00434                                         for( end = i; (end >= 0) && isspace((int)bufr[end]); end-- )
00435                                                 ;
00436                                         c = mygetc( InFile );
00437                                 }
00438                                 break;
00439 
00440                         default: /* All others verbatim.  Note that spaces do not advance <end>.  This allows trimming  */
00441                                 bufr[i++] = c;
00442                                 if( !isspace( c ) )  /* of whitespace at the end of the line.     */
00443                                         end = i;
00444                                 c = mygetc( InFile );
00445                                 break;
00446                 }
00447         }
00448         bufr[end] = '\0';          /* End of value. */
00449 
00450         return( pfunc( bufr, &bufr[vstart] ) );   /* Pass name & value to pfunc().  */
00451 }
00452 
00453 /* ------------------------------------------------------------------------ **
00454  * Scan & parse the input.
00455  *
00456  *  Input:  InFile  - Input source.
00457  *          sfunc   - Function to be called when a section name is scanned.
00458  *                    See Section().
00459  *          pfunc   - Function to be called when a parameter is scanned.
00460  *                    See Parameter().
00461  *
00462  *  Output: True if the file was successfully scanned, else False.
00463  *
00464  *  Notes:  The input can be viewed in terms of 'lines'.  There are four
00465  *          types of lines:
00466  *            Blank      - May contain whitespace, otherwise empty.
00467  *            Comment    - First non-whitespace character is a ';' or '#'.
00468  *                         The remainder of the line is ignored.
00469  *            Section    - First non-whitespace character is a '['.
00470  *            Parameter  - The default case.
00471  * 
00472  * ------------------------------------------------------------------------ **
00473  */
00474 
00475 static BOOL Parse( myFILE *InFile,
00476                    BOOL (*sfunc)(const char *),
00477                    BOOL (*pfunc)(const char *, const char *) )
00478 {
00479         int    c;
00480 
00481         c = EatWhitespace( InFile );
00482         while( (EOF != c) && (c > 0) ) {
00483                 switch( c ) {
00484                         case '\n': /* Blank line. */
00485                                 c = EatWhitespace( InFile );
00486                                 break;
00487 
00488                         case ';': /* Comment line. */
00489                         case '#':
00490                                 c = EatComment( InFile );
00491                                 break;
00492 
00493                         case '[': /* Section Header. */
00494                                 if( !Section( InFile, sfunc ) )
00495                                         return False;
00496                                 c = EatWhitespace( InFile );
00497                                 break;
00498 
00499                         case '\\': /* Bogus backslash. */
00500                                 c = EatWhitespace( InFile );
00501                                 break;
00502 
00503                         default: /* Parameter line. */
00504                                 if( !Parameter( InFile, pfunc, c ) )
00505                                         return False;
00506                                 c = EatWhitespace( InFile );
00507                                 break;
00508                 }
00509         }
00510         return True;
00511 }
00512 
00513 /* ------------------------------------------------------------------------ **
00514  * Open a configuration file.
00515  *
00516  *  Input:  FileName  - The pathname of the config file to be opened.
00517  *
00518  *  Output: A pointer of type (char **) to the lines of the file
00519  *
00520  * ------------------------------------------------------------------------ **
00521  */
00522 
00523 static myFILE *OpenConfFile( const char *FileName )
00524 {
00525         const char *func = "params.c:OpenConfFile() -";
00526         int lvl = in_client?1:0;
00527         myFILE *ret;
00528 
00529         ret = SMB_MALLOC_P(myFILE);
00530         if (!ret)
00531                 return NULL;
00532 
00533         ret->buf = file_load(FileName, &ret->size, 0);
00534         if( NULL == ret->buf ) {
00535                 DEBUG( lvl, ("%s Unable to open configuration file \"%s\":\n\t%s\n",
00536                         func, FileName, strerror(errno)) );
00537                 SAFE_FREE(ret);
00538                 return NULL;
00539         }
00540 
00541         ret->p = ret->buf;
00542         ret->end_section_p = NULL;
00543         return( ret );
00544 }
00545 
00546 /* ------------------------------------------------------------------------ **
00547  * Process the named parameter file.
00548  *
00549  *  Input:  FileName  - The pathname of the parameter file to be opened.
00550  *          sfunc     - A pointer to a function that will be called when
00551  *                      a section name is discovered.
00552  *          pfunc     - A pointer to a function that will be called when
00553  *                      a parameter name and value are discovered.
00554  *
00555  *  Output: TRUE if the file was successfully parsed, else FALSE.
00556  *
00557  * ------------------------------------------------------------------------ **
00558  */
00559 
00560 BOOL pm_process( const char *FileName,
00561                 BOOL (*sfunc)(const char *),
00562                 BOOL (*pfunc)(const char *, const char *) )
00563 {
00564         int   result;
00565         myFILE *InFile;
00566         const char *func = "params.c:pm_process() -";
00567 
00568         InFile = OpenConfFile( FileName );          /* Open the config file. */
00569         if( NULL == InFile )
00570                 return False;
00571 
00572         DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) );
00573 
00574         if( NULL != bufr ) {
00575                 /* If we already have a buffer */
00576                 /* (recursive call), then just */
00577                 /* use it.                     */
00578                 result = Parse( InFile, sfunc, pfunc );
00579         } else {
00580                 bSize = BUFR_INC;
00581                 bufr = (char *)SMB_MALLOC( bSize );
00582                 if( NULL == bufr ) {
00583                         DEBUG(0,("%s memory allocation failure.\n", func));
00584                         myfile_close(InFile);
00585                         return False;
00586                 }
00587 
00588                 result = Parse( InFile, sfunc, pfunc );
00589                 SAFE_FREE( bufr );
00590                 bSize = 0;
00591         }
00592 
00593         myfile_close(InFile);
00594 
00595         if( !result ) {
00596                 DEBUG(0,("%s Failed.  Error returned from params.c:parse().\n", func));
00597                 return False;
00598         }
00599 
00600         return True;
00601 }

Sambaに対してSat Aug 29 21:23:12 2009に生成されました。  doxygen 1.4.7