From 26d81332de11fd115b081cb438f0c698b81b9bb1 Mon Sep 17 00:00:00 2001 From: Jeremy Allison Date: Thu, 24 Feb 2005 02:19:17 +0000 Subject: r5535: Reformat so I can actually maintain this code and eventually convert to MB safe. Jeremy. (This used to be commit 0285754c1408b4155e115a5f76b1f5ff481c9d7e) --- source3/param/params.c | 778 ++++++++++++++++++++++++------------------------- 1 file changed, 377 insertions(+), 401 deletions(-) (limited to 'source3/param/params.c') diff --git a/source3/param/params.c b/source3/param/params.c index 2d6fe56716..192223605a 100644 --- a/source3/param/params.c +++ b/source3/param/params.c @@ -111,14 +111,16 @@ typedef struct { static int mygetc(myFILE *f) { - if (f->p >= f->buf+f->size) return EOF; + if (f->p >= f->buf+f->size) + return EOF; /* be sure to return chars >127 as positive values */ return (int)( *(f->p++) & 0x00FF ); } static void myfile_close(myFILE *f) { - if (!f) return; + if (!f) + return; SAFE_FREE(f->buf); SAFE_FREE(f); } @@ -126,8 +128,6 @@ static void myfile_close(myFILE *f) /* -------------------------------------------------------------------------- ** * Functions... */ - -static int EatWhitespace( myFILE *InFile ) /* ------------------------------------------------------------------------ ** * Scan past whitespace (see ctype(3C)) and return the first non-whitespace * character, or newline, or EOF. @@ -144,15 +144,16 @@ static int EatWhitespace( myFILE *InFile ) * * ------------------------------------------------------------------------ ** */ - { - int c; + +static int EatWhitespace( myFILE *InFile ) +{ + int c; - for( c = mygetc( InFile ); isspace( c ) && ('\n' != c); c = mygetc( InFile ) ) - ; - return( c ); - } /* EatWhitespace */ + for( c = mygetc( InFile ); isspace( c ) && ('\n' != c); c = mygetc( InFile ) ) + ; + return( c ); +} -static int EatComment( myFILE *InFile ) /* ------------------------------------------------------------------------ ** * Scan to the end of a comment. * @@ -169,13 +170,15 @@ static int EatComment( myFILE *InFile ) * * ------------------------------------------------------------------------ ** */ - { - int c; - for( c = mygetc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = mygetc( InFile ) ) - ; - return( c ); - } /* EatComment */ +static int EatComment( myFILE *InFile ) +{ + int c; + + for( c = mygetc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = mygetc( InFile ) ) + ; + return( c ); +} /***************************************************************************** * Scan backards within a string to discover if the last non-whitespace @@ -200,400 +203,373 @@ static int Continuation(char *line, int pos ) return (((pos >= 0) && ('\\' == line[pos])) ? pos : -1 ); } +/* ------------------------------------------------------------------------ ** + * Scan a section name, and pass the name to function sfunc(). + * + * Input: InFile - Input source. + * sfunc - Pointer to the function to be called if the section + * name is successfully read. + * + * Output: True if the section name was read and True was returned from + * . False if failed or if a lexical error was + * encountered. + * + * ------------------------------------------------------------------------ ** + */ static BOOL Section( myFILE *InFile, BOOL (*sfunc)(const char *) ) - /* ------------------------------------------------------------------------ ** - * Scan a section name, and pass the name to function sfunc(). - * - * Input: InFile - Input source. - * sfunc - Pointer to the function to be called if the section - * name is successfully read. - * - * Output: True if the section name was read and True was returned from - * . False if failed or if a lexical error was - * encountered. - * - * ------------------------------------------------------------------------ ** - */ - { - int c; - int i; - int end; - const char *func = "params.c:Section() -"; - - i = 0; /* is the offset of the next free byte in bufr[] and */ - end = 0; /* is the current "end of string" offset. In most */ - /* cases these will be the same, but if the last */ - /* character written to bufr[] is a space, then */ - /* will be one less than . */ - - c = EatWhitespace( InFile ); /* We've already got the '['. Scan */ - /* past initial white space. */ - - while( (EOF != c) && (c > 0) ) - { - - /* Check that the buffer is big enough for the next character. */ - if( i > (bSize - 2) ) - { - char *tb; +{ + int c; + int i; + int end; + const char *func = "params.c:Section() -"; + + i = 0; /* is the offset of the next free byte in bufr[] and */ + end = 0; /* is the current "end of string" offset. In most */ + /* cases these will be the same, but if the last */ + /* character written to bufr[] is a space, then */ + /* will be one less than . */ + + c = EatWhitespace( InFile ); /* We've already got the '['. Scan */ + /* past initial white space. */ + + while( (EOF != c) && (c > 0) ) { + /* Check that the buffer is big enough for the next character. */ + if( i > (bSize - 2) ) { + char *tb; - tb = SMB_REALLOC( bufr, bSize +BUFR_INC ); - if( NULL == tb ) - { - DEBUG(0, ("%s Memory re-allocation failure.", func) ); - return( False ); - } - bufr = tb; - bSize += BUFR_INC; - } - - /* Handle a single character. */ - switch( c ) - { - case ']': /* Found the closing bracket. */ - bufr[end] = '\0'; - if( 0 == end ) /* Don't allow an empty name. */ - { - DEBUG(0, ("%s Empty section name in configuration file.\n", func )); - return( False ); - } - if( !sfunc(bufr) ) /* Got a valid name. Deal with it. */ - return( False ); - (void)EatComment( InFile ); /* Finish off the line. */ - return( True ); - - case '\n': /* Got newline before closing ']'. */ - i = Continuation( bufr, i ); /* Check for line continuation. */ - if( i < 0 ) - { - bufr[end] = '\0'; - DEBUG(0, ("%s Badly formed line in configuration file: %s\n", - func, bufr )); - return( False ); - } - end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); - c = mygetc( InFile ); /* Continue with next line. */ - break; - - default: /* All else are a valid name chars. */ - if( isspace( c ) ) /* One space per whitespace region. */ - { - bufr[end] = ' '; - i = end + 1; - c = EatWhitespace( InFile ); - } - else /* All others copy verbatim. */ - { - bufr[i++] = c; - end = i; - c = mygetc( InFile ); - } - } - } - - /* We arrive here if we've met the EOF before the closing bracket. */ - DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func, bufr )); - return( False ); - } /* Section */ + tb = SMB_REALLOC( bufr, bSize +BUFR_INC ); + if( NULL == tb ) { + DEBUG(0, ("%s Memory re-allocation failure.", func) ); + return False; + } + bufr = tb; + bSize += BUFR_INC; + } + + /* Handle a single character. */ + switch( c ) { + case ']': /* Found the closing bracket. */ + bufr[end] = '\0'; + if( 0 == end ) { + /* Don't allow an empty name. */ + DEBUG(0, ("%s Empty section name in configuration file.\n", func )); + return False; + } + if( !sfunc(bufr) ) /* Got a valid name. Deal with it. */ + return False; + EatComment( InFile ); /* Finish off the line. */ + return True; + + case '\n': /* Got newline before closing ']'. */ + i = Continuation( bufr, i ); /* Check for line continuation. */ + if( i < 0 ) { + bufr[end] = '\0'; + DEBUG(0, ("%s Badly formed line in configuration file: %s\n", func, bufr )); + return False; + } + end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); + c = mygetc( InFile ); /* Continue with next line. */ + break; + + default: /* All else are a valid name chars. */ + if(isspace( c )) { + /* One space per whitespace region. */ + bufr[end] = ' '; + i = end + 1; + c = EatWhitespace( InFile ); + } else { + bufr[i++] = c; + end = i; + c = mygetc( InFile ); + } + } + } + + /* We arrive here if we've met the EOF before the closing bracket. */ + DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func, bufr )); + return False; +} + +/* ------------------------------------------------------------------------ ** + * Scan a parameter name and value, and pass these two fields to pfunc(). + * + * Input: InFile - The input source. + * pfunc - A pointer to the function that will be called to + * process the parameter, once it has been scanned. + * c - The first character of the parameter name, which + * would have been read by Parse(). Unlike a comment + * line or a section header, there is no lead-in + * character that can be discarded. + * + * Output: True if the parameter name and value were scanned and processed + * successfully, else False. + * + * Notes: This function is in two parts. The first loop scans the + * parameter name. Internal whitespace is compressed, and an + * equal sign (=) terminates the token. Leading and trailing + * whitespace is discarded. The second loop scans the parameter + * value. When both have been successfully identified, they are + * passed to pfunc() for processing. + * + * ------------------------------------------------------------------------ ** + */ static BOOL Parameter( myFILE *InFile, BOOL (*pfunc)(const char *, const char *), int c ) - /* ------------------------------------------------------------------------ ** - * Scan a parameter name and value, and pass these two fields to pfunc(). - * - * Input: InFile - The input source. - * pfunc - A pointer to the function that will be called to - * process the parameter, once it has been scanned. - * c - The first character of the parameter name, which - * would have been read by Parse(). Unlike a comment - * line or a section header, there is no lead-in - * character that can be discarded. - * - * Output: True if the parameter name and value were scanned and processed - * successfully, else False. - * - * Notes: This function is in two parts. The first loop scans the - * parameter name. Internal whitespace is compressed, and an - * equal sign (=) terminates the token. Leading and trailing - * whitespace is discarded. The second loop scans the parameter - * value. When both have been successfully identified, they are - * passed to pfunc() for processing. - * - * ------------------------------------------------------------------------ ** - */ - { - int i = 0; /* Position within bufr. */ - int end = 0; /* bufr[end] is current end-of-string. */ - int vstart = 0; /* Starting position of the parameter value. */ - const char *func = "params.c:Parameter() -"; - - /* Read the parameter name. */ - while( 0 == vstart ) /* Loop until we've found the start of the value. */ - { - - if( i > (bSize - 2) ) /* Ensure there's space for next char. */ - { - char *tb; - - tb = SMB_REALLOC( bufr, bSize + BUFR_INC ); - if( NULL == tb ) - { - DEBUG(0, ("%s Memory re-allocation failure.", func) ); - return( False ); - } - bufr = tb; - bSize += BUFR_INC; - } - - switch( c ) - { - case '=': /* Equal sign marks end of param name. */ - if( 0 == end ) /* Don't allow an empty name. */ - { - DEBUG(0, ("%s Invalid parameter name in config. file.\n", func )); - return( False ); - } - bufr[end++] = '\0'; /* Mark end of string & advance. */ - i = end; /* New string starts here. */ - vstart = end; /* New string is parameter value. */ - bufr[i] = '\0'; /* New string is nul, for now. */ - break; - - case '\n': /* Find continuation char, else error. */ - i = Continuation( bufr, i ); - if( i < 0 ) - { - bufr[end] = '\0'; - DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n", - func, bufr )); - return( True ); - } - end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); - c = mygetc( InFile ); /* Read past eoln. */ - break; - - case '\0': /* Shouldn't have EOF within param name. */ - case EOF: - bufr[i] = '\0'; - DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, bufr )); - return( True ); - - default: - if( isspace( c ) ) /* One ' ' per whitespace region. */ - { - bufr[end] = ' '; - i = end + 1; - c = EatWhitespace( InFile ); - } - else /* All others verbatim. */ - { - bufr[i++] = c; - end = i; - c = mygetc( InFile ); - } - } - } - - /* Now parse the value. */ - c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */ - while( (EOF !=c) && (c > 0) ) - { - - if( i > (bSize - 2) ) /* Make sure there's enough room. */ - { - char *tb; - - tb = SMB_REALLOC( bufr, bSize + BUFR_INC ); - if( NULL == tb ) - { - DEBUG(0, ("%s Memory re-allocation failure.", func) ); - return( False ); - } - bufr = tb; - bSize += BUFR_INC; - } - - switch( c ) - { - case '\r': /* Explicitly remove '\r' because the older */ - c = mygetc( InFile ); /* version called fgets_slash() which also */ - break; /* removes them. */ - - case '\n': /* Marks end of value unless there's a '\'. */ - i = Continuation( bufr, i ); - if( i < 0 ) - c = 0; - else - { - for( end = i; (end >= 0) && isspace((int)bufr[end]); end-- ) - ; - c = mygetc( InFile ); - } - break; - - default: /* All others verbatim. Note that spaces do */ - bufr[i++] = c; /* not advance . This allows trimming */ - if( !isspace( c ) ) /* of whitespace at the end of the line. */ - end = i; - c = mygetc( InFile ); - break; - } - } - bufr[end] = '\0'; /* End of value. */ - - return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */ - } /* Parameter */ +{ + int i = 0; /* Position within bufr. */ + int end = 0; /* bufr[end] is current end-of-string. */ + int vstart = 0; /* Starting position of the parameter value. */ + const char *func = "params.c:Parameter() -"; + + /* Read the parameter name. */ + while( 0 == vstart ) { + /* Loop until we've found the start of the value. */ + if( i > (bSize - 2) ) { + /* Ensure there's space for next char. */ + char *tb = SMB_REALLOC( bufr, bSize + BUFR_INC ); + if( NULL == tb ) { + DEBUG(0, ("%s Memory re-allocation failure.", func) ); + return False; + } + bufr = tb; + bSize += BUFR_INC; + } + + switch(c) { + case '=': /* Equal sign marks end of param name. */ + if( 0 == end ) { + /* Don't allow an empty name. */ + DEBUG(0, ("%s Invalid parameter name in config. file.\n", func )); + return False; + } + bufr[end++] = '\0'; /* Mark end of string & advance. */ + i = end; /* New string starts here. */ + vstart = end; /* New string is parameter value. */ + bufr[i] = '\0'; /* New string is nul, for now. */ + break; + + case '\n': /* Find continuation char, else error. */ + i = Continuation( bufr, i ); + if( i < 0 ) { + bufr[end] = '\0'; + DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n", func, bufr )); + return True; + } + end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); + c = mygetc( InFile ); /* Read past eoln. */ + break; + + case '\0': /* Shouldn't have EOF within param name. */ + case EOF: + bufr[i] = '\0'; + DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, bufr )); + return True; + + default: + if(isspace( c )) { + /* One ' ' per whitespace region. */ + bufr[end] = ' '; + i = end + 1; + c = EatWhitespace( InFile ); + } else { + bufr[i++] = c; + end = i; + c = mygetc( InFile ); + } + } + } + + /* Now parse the value. */ + c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */ + while( (EOF !=c) && (c > 0) ) { + if( i > (bSize - 2) ) { + /* Make sure there's enough room. */ + char *tb = SMB_REALLOC( bufr, bSize + BUFR_INC ); + if( NULL == tb ) { + DEBUG(0, ("%s Memory re-allocation failure.", func)); + return False; + } + bufr = tb; + bSize += BUFR_INC; + } + + switch(c) { + case '\r': /* Explicitly remove '\r' because the older */ + c = mygetc( InFile ); /* version called fgets_slash() which also */ + break; /* removes them. */ + + case '\n': /* Marks end of value unless there's a '\'. */ + i = Continuation( bufr, i ); + if( i < 0 ) { + c = 0; + } else { + for( end = i; (end >= 0) && isspace((int)bufr[end]); end-- ) + ; + c = mygetc( InFile ); + } + break; + + default: /* All others verbatim. Note that spaces do not advance . This allows trimming */ + bufr[i++] = c; + if( !isspace( c ) ) /* of whitespace at the end of the line. */ + end = i; + c = mygetc( InFile ); + break; + } + } + bufr[end] = '\0'; /* End of value. */ + + return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */ +} + +/* ------------------------------------------------------------------------ ** + * Scan & parse the input. + * + * Input: InFile - Input source. + * sfunc - Function to be called when a section name is scanned. + * See Section(). + * pfunc - Function to be called when a parameter is scanned. + * See Parameter(). + * + * Output: True if the file was successfully scanned, else False. + * + * Notes: The input can be viewed in terms of 'lines'. There are four + * types of lines: + * Blank - May contain whitespace, otherwise empty. + * Comment - First non-whitespace character is a ';' or '#'. + * The remainder of the line is ignored. + * Section - First non-whitespace character is a '['. + * Parameter - The default case. + * + * ------------------------------------------------------------------------ ** + */ static BOOL Parse( myFILE *InFile, BOOL (*sfunc)(const char *), BOOL (*pfunc)(const char *, const char *) ) - /* ------------------------------------------------------------------------ ** - * Scan & parse the input. - * - * Input: InFile - Input source. - * sfunc - Function to be called when a section name is scanned. - * See Section(). - * pfunc - Function to be called when a parameter is scanned. - * See Parameter(). - * - * Output: True if the file was successfully scanned, else False. - * - * Notes: The input can be viewed in terms of 'lines'. There are four - * types of lines: - * Blank - May contain whitespace, otherwise empty. - * Comment - First non-whitespace character is a ';' or '#'. - * The remainder of the line is ignored. - * Section - First non-whitespace character is a '['. - * Parameter - The default case. - * - * ------------------------------------------------------------------------ ** - */ - { - int c; - - c = EatWhitespace( InFile ); - while( (EOF != c) && (c > 0) ) - { - switch( c ) - { - case '\n': /* Blank line. */ - c = EatWhitespace( InFile ); - break; - - case ';': /* Comment line. */ - case '#': - c = EatComment( InFile ); - break; - - case '[': /* Section Header. */ - if( !Section( InFile, sfunc ) ) - return( False ); - c = EatWhitespace( InFile ); - break; - - case '\\': /* Bogus backslash. */ - c = EatWhitespace( InFile ); - break; - - default: /* Parameter line. */ - if( !Parameter( InFile, pfunc, c ) ) - return( False ); - c = EatWhitespace( InFile ); - break; - } - } - return( True ); - } /* Parse */ +{ + int c; + + c = EatWhitespace( InFile ); + while( (EOF != c) && (c > 0) ) { + switch( c ) { + case '\n': /* Blank line. */ + c = EatWhitespace( InFile ); + break; + + case ';': /* Comment line. */ + case '#': + c = EatComment( InFile ); + break; + + case '[': /* Section Header. */ + if( !Section( InFile, sfunc ) ) + return False; + c = EatWhitespace( InFile ); + break; + + case '\\': /* Bogus backslash. */ + c = EatWhitespace( InFile ); + break; + + default: /* Parameter line. */ + if( !Parameter( InFile, pfunc, c ) ) + return False; + c = EatWhitespace( InFile ); + break; + } + } + return True; +} + +/* ------------------------------------------------------------------------ ** + * Open a configuration file. + * + * Input: FileName - The pathname of the config file to be opened. + * + * Output: A pointer of type (char **) to the lines of the file + * + * ------------------------------------------------------------------------ ** + */ static myFILE *OpenConfFile( const char *FileName ) - /* ------------------------------------------------------------------------ ** - * Open a configuration file. - * - * Input: FileName - The pathname of the config file to be opened. - * - * Output: A pointer of type (char **) to the lines of the file - * - * ------------------------------------------------------------------------ ** - */ - { - const char *func = "params.c:OpenConfFile() -"; - extern BOOL in_client; - int lvl = in_client?1:0; - myFILE *ret; - - ret = SMB_MALLOC_P(myFILE); - if (!ret) return NULL; - - ret->buf = file_load(FileName, &ret->size); - if( NULL == ret->buf ) - { - DEBUG( lvl, - ("%s Unable to open configuration file \"%s\":\n\t%s\n", - func, FileName, strerror(errno)) ); - SAFE_FREE(ret); - return NULL; - } - - ret->p = ret->buf; - return( ret ); - } /* OpenConfFile */ +{ + const char *func = "params.c:OpenConfFile() -"; + extern BOOL in_client; + int lvl = in_client?1:0; + myFILE *ret; + + ret = SMB_MALLOC_P(myFILE); + if (!ret) + return NULL; + + ret->buf = file_load(FileName, &ret->size); + if( NULL == ret->buf ) { + DEBUG( lvl, ("%s Unable to open configuration file \"%s\":\n\t%s\n", + func, FileName, strerror(errno)) ); + SAFE_FREE(ret); + return NULL; + } + + ret->p = ret->buf; + return( ret ); +} + +/* ------------------------------------------------------------------------ ** + * Process the named parameter file. + * + * Input: FileName - The pathname of the parameter file to be opened. + * sfunc - A pointer to a function that will be called when + * a section name is discovered. + * pfunc - A pointer to a function that will be called when + * a parameter name and value are discovered. + * + * Output: TRUE if the file was successfully parsed, else FALSE. + * + * ------------------------------------------------------------------------ ** + */ BOOL pm_process( const char *FileName, - BOOL (*sfunc)(const char *), - BOOL (*pfunc)(const char *, const char *) ) - /* ------------------------------------------------------------------------ ** - * Process the named parameter file. - * - * Input: FileName - The pathname of the parameter file to be opened. - * sfunc - A pointer to a function that will be called when - * a section name is discovered. - * pfunc - A pointer to a function that will be called when - * a parameter name and value are discovered. - * - * Output: TRUE if the file was successfully parsed, else FALSE. - * - * ------------------------------------------------------------------------ ** - */ - { - int result; - myFILE *InFile; - const char *func = "params.c:pm_process() -"; - - InFile = OpenConfFile( FileName ); /* Open the config file. */ - if( NULL == InFile ) - return( False ); - - DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) ); - - if( NULL != bufr ) /* If we already have a buffer */ - result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */ - /* use it. */ - - else /* If we don't have a buffer */ - { /* allocate one, then parse, */ - bSize = BUFR_INC; /* then free. */ - bufr = (char *)SMB_MALLOC( bSize ); - if( NULL == bufr ) - { - DEBUG(0,("%s memory allocation failure.\n", func)); - myfile_close(InFile); - return( False ); - } - result = Parse( InFile, sfunc, pfunc ); - SAFE_FREE( bufr ); - bufr = NULL; - bSize = 0; - } - - myfile_close(InFile); - - if( !result ) /* Generic failure. */ - { - DEBUG(0,("%s Failed. Error returned from params.c:parse().\n", func)); - return( False ); - } - - return( True ); /* Generic success. */ - } /* pm_process */ - -/* -------------------------------------------------------------------------- */ + BOOL (*sfunc)(const char *), + BOOL (*pfunc)(const char *, const char *) ) +{ + int result; + myFILE *InFile; + const char *func = "params.c:pm_process() -"; + + InFile = OpenConfFile( FileName ); /* Open the config file. */ + if( NULL == InFile ) + return False; + + DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) ); + + if( NULL != bufr ) { + /* If we already have a buffer */ + /* (recursive call), then just */ + /* use it. */ + result = Parse( InFile, sfunc, pfunc ); + } else { + bSize = BUFR_INC; + bufr = (char *)SMB_MALLOC( bSize ); + if( NULL == bufr ) { + DEBUG(0,("%s memory allocation failure.\n", func)); + myfile_close(InFile); + return False; + } + + result = Parse( InFile, sfunc, pfunc ); + SAFE_FREE( bufr ); + bufr = NULL; + bSize = 0; + } + + myfile_close(InFile); + + if( !result ) { + DEBUG(0,("%s Failed. Error returned from params.c:parse().\n", func)); + return False; + } + + return True; +} -- cgit