Go to the source code of this file.
Functions | |
int | EatWhitespace (FILE *InFile) |
int | EatComment (FILE *InFile) |
int | Continuation (char *line, int pos) |
BOOL | Section (FILE *InFile, BOOL(*sfunc)(char *)) |
BOOL | Parameter (FILE *InFile, BOOL(*pfunc)(char *, char *), int c) |
BOOL | Parse (FILE *InFile, BOOL(*sfunc)(char *), BOOL(*pfunc)(char *, char *)) |
FILE * | OpenConfFile (char *FileName) |
BOOL | pm_process (char *FileName, BOOL(*sfunc)(char *), BOOL(*pfunc)(char *, char *)) |
Variables | |
char * | bufr = NULL |
int | bSize = 0 |
|
Definition at line 100 of file params.c. Referenced by Parameter(), Parse(), and Section().
00105 : InFile - Input source. 00106 * 00107 * Output: The next non-whitespace character in the input stream. 00108 * 00109 * Notes: Because the config files use a line-oriented grammar, we 00110 * explicitly exclude the newline character from the list of 00111 * whitespace characters. 00112 * - Note that both EOF (-1) and the nul character ('\0') are 00113 * considered end-of-file markers. 00114 * 00115 * ------------------------------------------------------------------------ ** 00116 */ 00117 { 00118 int c; 00119 00120 for( c = getc( InFile ); isspace( c ) && ('\n' != c); c = getc( InFile ) ) 00121 ; 00122 return( c ); 00123 } /* EatWhitespace */ |
|
Definition at line 125 of file params.c. Referenced by Parse(), and Section().
00129 : InFile - Input source. 00130 * 00131 * Output: The character that marks the end of the comment. Normally, 00132 * this will be a newline, but it *might* be an EOF. 00133 * 00134 * Notes: Because the config files use a line-oriented grammar, we 00135 * explicitly exclude the newline character from the list of 00136 * whitespace characters. 00137 * - Note that both EOF (-1) and the nul character ('\0') are 00138 * considered end-of-file markers. 00139 * 00140 * ------------------------------------------------------------------------ ** 00141 */ 00142 { 00143 int c; 00144 00145 for( c = getc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = getc( InFile ) ) 00146 ; 00147 return( c ); 00148 } /* EatComment */ |
|
Definition at line 150 of file params.c. Referenced by Parameter(), and Section().
00155 : line - A pointer to a buffer containing the string to be 00156 * scanned. 00157 * pos - This is taken to be the offset of the end of the 00158 * string. This position is *not* scanned. 00159 * 00160 * Output: The offset of the '\\' character if it was found, or -1 to 00161 * indicate that it was not. 00162 * 00163 * ------------------------------------------------------------------------ ** 00164 */ 00165 { 00166 pos--; 00167 while( (pos >= 0) && isspace(((unsigned char *)line)[pos]) ) 00168 pos--; 00169 00170 return( ((pos >= 0) && ('\\' == line[pos])) ? pos : -1 ); 00171 } /* Continuation */ |
|
Definition at line 174 of file params.c. References bSize, bufr, Continuation(), EatComment(), EatWhitespace(), FERROR, Realloc(), and rprintf(). Referenced by Parse().
00178 : InFile - Input source. 00179 * sfunc - Pointer to the function to be called if the section 00180 * name is successfully read. 00181 * 00182 * Output: True if the section name was read and True was returned from 00183 * <sfunc>. False if <sfunc> failed or if a lexical error was 00184 * encountered. 00185 * 00186 * ------------------------------------------------------------------------ ** 00187 */ 00188 { 00189 int c; 00190 int i; 00191 int end; 00192 char *func = "params.c:Section() -"; 00193 00194 i = 0; /* <i> is the offset of the next free byte in bufr[] and */ 00195 end = 0; /* <end> is the current "end of string" offset. In most */ 00196 /* cases these will be the same, but if the last */ 00197 /* character written to bufr[] is a space, then <end> */ 00198 /* will be one less than <i>. */ 00199 00200 c = EatWhitespace( InFile ); /* We've already got the '['. Scan */ 00201 /* past initial white space. */ 00202 00203 while( (EOF != c) && (c > 0) ) 00204 { 00205 00206 /* Check that the buffer is big enough for the next character. */ 00207 if( i > (bSize - 2) ) 00208 { 00209 bSize += BUFR_INC; 00210 bufr = Realloc( bufr, bSize ); 00211 if( NULL == bufr ) 00212 { 00213 rprintf(FERROR, "%s Memory re-allocation failure.", func); 00214 return( False ); 00215 } 00216 } 00217 00218 /* Handle a single character. */ 00219 switch( c ) 00220 { 00221 case ']': /* Found the closing bracket. */ 00222 bufr[end] = '\0'; 00223 if( 0 == end ) /* Don't allow an empty name. */ 00224 { 00225 rprintf(FERROR, "%s Empty section name in configuration file.\n", func ); 00226 return( False ); 00227 } 00228 if( !sfunc( bufr ) ) /* Got a valid name. Deal with it. */ 00229 return( False ); 00230 (void)EatComment( InFile ); /* Finish off the line. */ 00231 return( True ); 00232 00233 case '\n': /* Got newline before closing ']'. */ 00234 i = Continuation( bufr, i ); /* Check for line continuation. */ 00235 if( i < 0 ) 00236 { 00237 bufr[end] = '\0'; 00238 rprintf(FERROR, "%s Badly formed line in configuration file: %s\n", 00239 func, bufr ); 00240 return( False ); 00241 } 00242 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); 00243 c = getc( InFile ); /* Continue with next line. */ 00244 break; 00245 00246 default: /* All else are a valid name chars. */ 00247 if( isspace( c ) ) /* One space per whitespace region. */ 00248 { 00249 bufr[end] = ' '; 00250 i = end + 1; 00251 c = EatWhitespace( InFile ); 00252 } 00253 else /* All others copy verbatim. */ 00254 { 00255 bufr[i++] = c; 00256 end = i; 00257 c = getc( InFile ); 00258 } 00259 } 00260 } 00261 00262 /* We arrive here if we've met the EOF before the closing bracket. */ 00263 rprintf(FERROR, "%s Unexpected EOF in the configuration file: %s\n", func, bufr ); 00264 return( False ); 00265 } /* Section */ |
|
Definition at line 267 of file params.c. References bSize, bufr, Continuation(), EatWhitespace(), FERROR, Realloc(), and rprintf(). Referenced by Parse().
00271 : InFile - The input source. 00272 * pfunc - A pointer to the function that will be called to 00273 * process the parameter, once it has been scanned. 00274 * c - The first character of the parameter name, which 00275 * would have been read by Parse(). Unlike a comment 00276 * line or a section header, there is no lead-in 00277 * character that can be discarded. 00278 * 00279 * Output: True if the parameter name and value were scanned and processed 00280 * successfully, else False. 00281 * 00282 * Notes: This function is in two parts. The first loop scans the 00283 * parameter name. Internal whitespace is compressed, and an 00284 * equal sign (=) terminates the token. Leading and trailing 00285 * whitespace is discarded. The second loop scans the parameter 00286 * value. When both have been successfully identified, they are 00287 * passed to pfunc() for processing. 00288 * 00289 * ------------------------------------------------------------------------ ** 00290 */ 00291 { 00292 int i = 0; /* Position within bufr. */ 00293 int end = 0; /* bufr[end] is current end-of-string. */ 00294 int vstart = 0; /* Starting position of the parameter value. */ 00295 char *func = "params.c:Parameter() -"; 00296 00297 /* Read the parameter name. */ 00298 while( 0 == vstart ) /* Loop until we've found the start of the value. */ 00299 { 00300 00301 if( i > (bSize - 2) ) /* Ensure there's space for next char. */ 00302 { 00303 bSize += BUFR_INC; 00304 bufr = Realloc( bufr, bSize ); 00305 if( NULL == bufr ) 00306 { 00307 rprintf(FERROR, "%s Memory re-allocation failure.", func) ; 00308 return( False ); 00309 } 00310 } 00311 00312 switch( c ) 00313 { 00314 case '=': /* Equal sign marks end of param name. */ 00315 if( 0 == end ) /* Don't allow an empty name. */ 00316 { 00317 rprintf(FERROR, "%s Invalid parameter name in config. file.\n", func ); 00318 return( False ); 00319 } 00320 bufr[end++] = '\0'; /* Mark end of string & advance. */ 00321 i = end; /* New string starts here. */ 00322 vstart = end; /* New string is parameter value. */ 00323 bufr[i] = '\0'; /* New string is nul, for now. */ 00324 break; 00325 00326 case '\n': /* Find continuation char, else error. */ 00327 i = Continuation( bufr, i ); 00328 if( i < 0 ) 00329 { 00330 bufr[end] = '\0'; 00331 rprintf(FERROR, "%s Ignoring badly formed line in configuration file: %s\n", 00332 func, bufr ); 00333 return( True ); 00334 } 00335 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i); 00336 c = getc( InFile ); /* Read past eoln. */ 00337 break; 00338 00339 case '\0': /* Shouldn't have EOF within param name. */ 00340 case EOF: 00341 bufr[i] = '\0'; 00342 rprintf(FERROR, "%s Unexpected end-of-file at: %s\n", func, bufr ); 00343 return( True ); 00344 00345 default: 00346 if( isspace( c ) ) /* One ' ' per whitespace region. */ 00347 { 00348 bufr[end] = ' '; 00349 i = end + 1; 00350 c = EatWhitespace( InFile ); 00351 } 00352 else /* All others verbatim. */ 00353 { 00354 bufr[i++] = c; 00355 end = i; 00356 c = getc( InFile ); 00357 } 00358 } 00359 } 00360 00361 /* Now parse the value. */ 00362 c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */ 00363 while( (EOF !=c) && (c > 0) ) 00364 { 00365 00366 if( i > (bSize - 2) ) /* Make sure there's enough room. */ 00367 { 00368 bSize += BUFR_INC; 00369 bufr = Realloc( bufr, bSize ); 00370 if( NULL == bufr ) 00371 { 00372 rprintf(FERROR, "%s Memory re-allocation failure.", func) ; 00373 return( False ); 00374 } 00375 } 00376 00377 switch( c ) 00378 { 00379 case '\r': /* Explicitly remove '\r' because the older */ 00380 c = getc( InFile ); /* version called fgets_slash() which also */ 00381 break; /* removes them. */ 00382 00383 case '\n': /* Marks end of value unless there's a '\'. */ 00384 i = Continuation( bufr, i ); 00385 if( i < 0 ) 00386 c = 0; 00387 else 00388 { 00389 for( end = i; (end >= 0) && isspace(((unsigned char *) bufr)[end]); end-- ) 00390 ; 00391 c = getc( InFile ); 00392 } 00393 break; 00394 00395 default: /* All others verbatim. Note that spaces do */ 00396 bufr[i++] = c; /* not advance <end>. This allows trimming */ 00397 if( !isspace( c ) ) /* of whitespace at the end of the line. */ 00398 end = i; 00399 c = getc( InFile ); 00400 break; 00401 } 00402 } 00403 bufr[end] = '\0'; /* End of value. */ 00404 00405 return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */ 00406 } /* Parameter */ |
|
Definition at line 408 of file params.c. References EatComment(), EatWhitespace(), Parameter(), and Section(). Referenced by pm_process().
00414 : InFile - Input source. 00415 * sfunc - Function to be called when a section name is scanned. 00416 * See Section(). 00417 * pfunc - Function to be called when a parameter is scanned. 00418 * See Parameter(). 00419 * 00420 * Output: True if the file was successfully scanned, else False. 00421 * 00422 * Notes: The input can be viewed in terms of 'lines'. There are four 00423 * types of lines: 00424 * Blank - May contain whitespace, otherwise empty. 00425 * Comment - First non-whitespace character is a ';' or '#'. 00426 * The remainder of the line is ignored. 00427 * Section - First non-whitespace character is a '['. 00428 * Parameter - The default case. 00429 * 00430 * ------------------------------------------------------------------------ ** 00431 */ 00432 { 00433 int c; 00434 00435 c = EatWhitespace( InFile ); 00436 while( (EOF != c) && (c > 0) ) 00437 { 00438 switch( c ) 00439 { 00440 case '\n': /* Blank line. */ 00441 c = EatWhitespace( InFile ); 00442 break; 00443 00444 case ';': /* Comment line. */ 00445 case '#': 00446 c = EatComment( InFile ); 00447 break; 00448 00449 case '[': /* Section Header. */ 00450 if (!sfunc) return True; 00451 if( !Section( InFile, sfunc ) ) 00452 return( False ); 00453 c = EatWhitespace( InFile ); 00454 break; 00455 00456 case '\\': /* Bogus backslash. */ 00457 c = EatWhitespace( InFile ); 00458 break; 00459 00460 default: /* Parameter line. */ 00461 if( !Parameter( InFile, pfunc, c ) ) 00462 return( False ); 00463 c = EatWhitespace( InFile ); 00464 break; 00465 } 00466 } 00467 return( True ); 00468 } /* Parse */ |
|
Definition at line 470 of file params.c. References FERROR, and rprintf(). Referenced by pm_process().
00474 : FileName - The pathname of the config file to be opened. 00475 * 00476 * Output: A pointer of type (FILE *) to the opened file, or NULL if the 00477 * file could not be opened. 00478 * 00479 * ------------------------------------------------------------------------ ** 00480 */ 00481 { 00482 FILE *OpenedFile; 00483 char *func = "params.c:OpenConfFile() -"; 00484 00485 if( NULL == FileName || 0 == *FileName ) 00486 { 00487 rprintf(FERROR,"%s No configuration filename specified.\n", func); 00488 return( NULL ); 00489 } 00490 00491 OpenedFile = fopen( FileName, "r" ); 00492 if( NULL == OpenedFile ) 00493 { 00494 rprintf(FERROR,"%s Unable to open configuration file \"%s\":\n\t%s\n", 00495 func, FileName, strerror(errno)); 00496 } 00497 00498 return( OpenedFile ); 00499 } /* OpenConfFile */ |
|
Definition at line 501 of file params.c. References bSize, bufr, FERROR, OpenConfFile(), Parse(), and rprintf(). Referenced by lp_load().
00507 : FileName - The pathname of the parameter file to be opened. 00508 * sfunc - A pointer to a function that will be called when 00509 * a section name is discovered. 00510 * pfunc - A pointer to a function that will be called when 00511 * a parameter name and value are discovered. 00512 * 00513 * Output: TRUE if the file was successfully parsed, else FALSE. 00514 * 00515 * ------------------------------------------------------------------------ ** 00516 */ 00517 { 00518 int result; 00519 FILE *InFile; 00520 char *func = "params.c:pm_process() -"; 00521 00522 InFile = OpenConfFile( FileName ); /* Open the config file. */ 00523 if( NULL == InFile ) 00524 return( False ); 00525 00526 if( NULL != bufr ) /* If we already have a buffer */ 00527 result = Parse( InFile, sfunc, pfunc ); /* (recursive call), then just */ 00528 /* use it. */ 00529 00530 else /* If we don't have a buffer */ 00531 { /* allocate one, then parse, */ 00532 bSize = BUFR_INC; /* then free. */ 00533 bufr = (char *)malloc( bSize ); 00534 if( NULL == bufr ) 00535 { 00536 rprintf(FERROR,"%s memory allocation failure.\n", func); 00537 fclose(InFile); 00538 return( False ); 00539 } 00540 result = Parse( InFile, sfunc, pfunc ); 00541 free( bufr ); 00542 bufr = NULL; 00543 bSize = 0; 00544 } 00545 00546 fclose(InFile); 00547 00548 if( !result ) /* Generic failure. */ 00549 { 00550 rprintf(FERROR,"%s Failed. Error returned from params.c:parse().\n", func); 00551 return( False ); 00552 } 00553 00554 return( True ); /* Generic success. */ 00555 } /* pm_process */ |
|
Definition at line 93 of file params.c. Referenced by Parameter(), pm_process(), and Section(). |
|
Definition at line 94 of file params.c. Referenced by Parameter(), pm_process(), and Section(). |