123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572 |
- #include "includes.h"
- const char *unix_error_string (int error_num);
- #define BUFR_INC 1024
- extern int DEBUGLEVEL;
- static char *bufr = NULL;
- static int bSize = 0;
- static int EatWhitespace( FILE *InFile )
-
- {
- int c;
- for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) )
- ;
- return( c );
- }
- static int EatComment( FILE *InFile )
-
- {
- int c;
- for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) )
- ;
- return( c );
- }
- static int Continuation( char *line, int pos )
-
- {
- pos--;
- while( (pos >= 0) && isspace(line[pos]) )
- pos--;
- return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
- }
- static BOOL Section( FILE *InFile, BOOL (*sfunc)(const char *) )
-
- {
- int c;
- int i;
- int end;
- const char *func = "params.c:Section() -";
- i = 0;
- end = 0;
-
-
-
- c = EatWhitespace( InFile );
-
- while( (EOF != c) && (c > 0) )
- {
-
- if( i > (bSize - 2) )
- {
- bSize += BUFR_INC;
- bufr = Realloc( bufr, bSize );
- if( NULL == bufr )
- {
- DEBUG(0, ("%s Memory re-allocation failure.", func) );
- return( False );
- }
- }
-
- switch( c )
- {
- case ']':
- bufr[end] = '\0';
- if( 0 == end )
- {
- DEBUG(0, ("%s Empty section name in configuration file.\n", func ));
- return( False );
- }
- if( !sfunc( bufr ) )
- return( False );
- (void)EatComment( InFile );
- return( True );
- case '\n':
- i = Continuation( bufr, i );
- 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 = getc( InFile );
- break;
- default:
- if( isspace( c ) )
- {
- bufr[end] = ' ';
- i = end + 1;
- c = EatWhitespace( InFile );
- }
- else
- {
- bufr[i++] = c;
- end = i;
- c = getc( InFile );
- }
- }
- }
-
- DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func, bufr ));
- return( False );
- }
- static BOOL Parameter( FILE *InFile, BOOL (*pfunc)(const char *, const char *), int c )
-
- {
- int i = 0;
- int end = 0;
- int vstart = 0;
- const char *func = "params.c:Parameter() -";
-
- while( 0 == vstart )
- {
- if( i > (bSize - 2) )
- {
- bSize += BUFR_INC;
- bufr = Realloc( bufr, bSize );
- if( NULL == bufr )
- {
- DEBUG(0, ("%s Memory re-allocation failure.", func) );
- return( False );
- }
- }
- switch( c )
- {
- case '=':
- if( 0 == end )
- {
- DEBUG(0, ("%s Invalid parameter name in config. file.\n", func ));
- return( False );
- }
- bufr[end++] = '\0';
- i = end;
- vstart = end;
- bufr[i] = '\0';
- break;
- case '\n':
- 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 = getc( InFile );
- break;
- case '\0':
- case EOF:
- bufr[i] = '\0';
- DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, bufr ));
- return( True );
- default:
- if( isspace( c ) )
- {
- bufr[end] = ' ';
- i = end + 1;
- c = EatWhitespace( InFile );
- }
- else
- {
- bufr[i++] = c;
- end = i;
- c = getc( InFile );
- }
- }
- }
-
- c = EatWhitespace( InFile );
- while( (EOF !=c) && (c > 0) )
- {
- if( i > (bSize - 2) )
- {
- bSize += BUFR_INC;
- bufr = Realloc( bufr, bSize );
- if( NULL == bufr )
- {
- DEBUG(0, ("%s Memory re-allocation failure.", func) );
- return( False );
- }
- }
- switch( c )
- {
- case '\r':
- c = getc( InFile );
- break;
- case '\n':
- i = Continuation( bufr, i );
- if( i < 0 )
- c = 0;
- else
- {
- for( end = i; (end >= 0) && isspace(bufr[end]); end-- )
- ;
- c = getc( InFile );
- }
- break;
- default:
- bufr[i++] = c;
- if( !isspace( c ) )
- end = i;
- c = getc( InFile );
- break;
- }
- }
- bufr[end] = '\0';
- return( pfunc( bufr, &bufr[vstart] ) );
- }
- static BOOL Parse( FILE *InFile,
- BOOL (*sfunc)(const char *),
- BOOL (*pfunc)(const char *, const char *) )
-
- {
- int c;
- c = EatWhitespace( InFile );
- while( (EOF != c) && (c > 0) )
- {
- switch( c )
- {
- case '\n':
- c = EatWhitespace( InFile );
- break;
- case ';':
- case '#':
- c = EatComment( InFile );
- break;
- case '[':
- if( !Section( InFile, sfunc ) )
- return( False );
- c = EatWhitespace( InFile );
- break;
- case '\\':
- c = EatWhitespace( InFile );
- break;
- default:
- if( !Parameter( InFile, pfunc, c ) )
- return( False );
- c = EatWhitespace( InFile );
- break;
- }
- }
- return( True );
- }
- static FILE *OpenConfFile( const char *FileName )
-
- {
- FILE *OpenedFile;
- const char *func = "params.c:OpenConfFile() -";
- extern BOOL in_client;
- int lvl = in_client?1:0;
- if( NULL == FileName || 0 == *FileName )
- {
- DEBUG( lvl, ("%s No configuration filename specified.\n", func) );
- return( NULL );
- }
- OpenedFile = sys_fopen( FileName, "r" );
- if( NULL == OpenedFile )
- {
- DEBUG( lvl,
- ("%s Unable to open configuration file \"%s\":\n\t%s\n",
- func, FileName, unix_error_string (errno)) );
- }
- return( OpenedFile );
- }
- BOOL pm_process( const char *FileName,
- BOOL (*sfunc)(const char *),
- BOOL (*pfunc)(const char *, const char *) )
-
- {
- int result;
- FILE *InFile;
- const char *func = "params.c:pm_process() -";
- InFile = OpenConfFile( FileName );
- if( NULL == InFile )
- return( False );
- DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) );
- if( NULL != bufr )
- result = Parse( InFile, sfunc, pfunc );
-
- else
- {
- bSize = BUFR_INC;
- bufr = (char *)malloc( bSize );
- if( NULL == bufr )
- {
- DEBUG(0,("%s memory allocation failure.\n", func));
- fclose(InFile);
- return( False );
- }
- result = Parse( InFile, sfunc, pfunc );
- free( bufr );
- bufr = NULL;
- bSize = 0;
- }
- fclose(InFile);
- if( !result )
- {
- DEBUG(0,("%s Failed. Error returned from params.c:parse().\n", func));
- return( False );
- }
- return( True );
- }
|