Annotation of embedaddon/igmpproxy/src/confread.c, revision 1.1.1.1

1.1       misho       1: /*
                      2: **  igmpproxy - IGMP proxy based multicast router 
                      3: **  Copyright (C) 2005 Johnny Egeland <johnny@rlo.org>
                      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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     18: **
                     19: **----------------------------------------------------------------------------
                     20: **
                     21: **  This software is derived work from the following software. The original
                     22: **  source code has been modified from it's original state by the author
                     23: **  of igmpproxy.
                     24: **
                     25: **  smcroute 0.92 - Copyright (C) 2001 Carsten Schill <carsten@cschill.de>
                     26: **  - Licensed under the GNU General Public License, version 2
                     27: **  
                     28: **  mrouted 3.9-beta3 - COPYRIGHT 1989 by The Board of Trustees of 
                     29: **  Leland Stanford Junior University.
                     30: **  - Original license can be found in the Stanford.txt file.
                     31: **
                     32: */
                     33: /**
                     34: *   confread.c
                     35: *
                     36: *   Generic config file reader. Used to open a config file,
                     37: *   and read the tokens from it. The parser is really simple,
                     38: *   and does no backlogging. This means that no form of
                     39: *   text escaping and qouting is currently supported.
                     40: *   '#' chars are read as comments, and the comment lasts until 
                     41: *   a newline or EOF
                     42: *
                     43: */
                     44: 
                     45: #include "igmpproxy.h"
                     46: 
                     47: #define     READ_BUFFER_SIZE    512   // Inputbuffer size...
                     48:         
                     49: #ifndef MAX_TOKEN_LENGTH
                     50:   #define MAX_TOKEN_LENGTH  30     // Default max token length
                     51: #endif
                     52:                                      
                     53: FILE            *confFilePtr;       // File handle pointer
                     54: char            *iBuffer;           // Inputbuffer for reading...
                     55: unsigned int    bufPtr;             // Buffer position pointer.
                     56: unsigned int    readSize;           // Number of bytes in buffer after last read...
                     57: char    cToken[MAX_TOKEN_LENGTH];   // Token buffer...
                     58: short   validToken;
                     59: 
                     60: /**
                     61: *   Opens config file specified by filename.
                     62: */    
                     63: int openConfigFile(char *filename) {
                     64: 
                     65:     // Set the buffer to null initially...
                     66:     iBuffer = NULL;
                     67: 
                     68:     // Open the file for reading...
                     69:     confFilePtr = fopen(filename, "r");
                     70:     
                     71:     // On error, return false
                     72:     if(confFilePtr == NULL) {
                     73:         return 0;
                     74:     }
                     75:     
                     76:     // Allocate memory for inputbuffer...
                     77:     iBuffer = (char*) malloc( sizeof(char) * READ_BUFFER_SIZE );
                     78:     
                     79:     if(iBuffer == NULL) {
                     80:         closeConfigFile();
                     81:         return 0;
                     82:     }
                     83:     
                     84:     // Reset bufferpointer and readsize
                     85:     bufPtr = 0;
                     86:     readSize = 0;
                     87: 
                     88:     return 1;
                     89: }
                     90: 
                     91: /**
                     92: *   Closes the currently open config file.
                     93: */
                     94: void closeConfigFile() {
                     95:     // Close the file.
                     96:     if(confFilePtr!=NULL) {
                     97:         fclose(confFilePtr);
                     98:     }
                     99:     // Free input buffer memory...
                    100:     if(iBuffer != NULL) {
                    101:         free(iBuffer);
                    102:     }
                    103: }
                    104: 
                    105: /**
                    106: *   Returns the next token from the configfile. The function
                    107: *   return NULL if there are no more tokens in the file.    
                    108: */
                    109: char *nextConfigToken() {
                    110: 
                    111:     validToken = 0;
                    112: 
                    113:     // If no file or buffer, return NULL
                    114:     if(confFilePtr == NULL || iBuffer == NULL) {
                    115:         return NULL;
                    116:     }
                    117: 
                    118:     {
                    119:         unsigned int tokenPtr       = 0;
                    120:         unsigned short finished     = 0;
                    121:         unsigned short commentFound = 0;
                    122: 
                    123:         // Outer buffer fill loop...
                    124:         while ( !finished ) {
                    125:             // If readpointer is at the end of the buffer, we should read next chunk...
                    126:             if(bufPtr == readSize) {
                    127:                 // Fill up the buffer...
                    128:                 readSize = fread (iBuffer, sizeof(char), READ_BUFFER_SIZE, confFilePtr);
                    129:                 bufPtr = 0;
                    130: 
                    131:                 // If the readsize is 0, we should just return...
                    132:                 if(readSize == 0) {
                    133:                     return NULL;
                    134:                 }
                    135:             }
                    136: 
                    137:             // Inner char loop...
                    138:             while ( bufPtr < readSize && !finished ) {
                    139: 
                    140:                 //printf("Char %s", iBuffer[bufPtr]);
                    141: 
                    142:                 // Break loop on \0
                    143:                 if(iBuffer[bufPtr] == '\0') {
                    144:                     break;
                    145:                 }
                    146: 
                    147:                 if( commentFound ) {
                    148:                     if( iBuffer[bufPtr] == '\n' ) {
                    149:                         commentFound = 0;
                    150:                     }
                    151:                 } else {
                    152: 
                    153:                     // Check current char...
                    154:                     switch(iBuffer[bufPtr]) {
                    155:                     case '#':
                    156:                         // Found a comment start...
                    157:                         commentFound = 1;
                    158:                         break;
                    159: 
                    160:                     case '\n':
                    161:                     case '\r':
                    162:                     case '\t':
                    163:                     case ' ':
                    164:                         // Newline, CR, Tab and space are end of token, or ignored.
                    165:                         if(tokenPtr > 0) {
                    166:                             cToken[tokenPtr] = '\0';    // EOL
                    167:                             finished = 1;
                    168:                         }
                    169:                         break;
                    170: 
                    171:                     default:
                    172:                         // Append char to token...
                    173:                         cToken[tokenPtr++] = iBuffer[bufPtr];
                    174:                         break;
                    175:                     }
                    176:                 
                    177:                 }
                    178: 
                    179:                 // Check end of token buffer !!!
                    180:                 if(tokenPtr == MAX_TOKEN_LENGTH - 1) {
                    181:                     // Prevent buffer overrun...
                    182:                     cToken[tokenPtr] = '\0';
                    183:                     finished = 1;
                    184:                 }
                    185: 
                    186:                 // Next char...
                    187:                 bufPtr++;
                    188:             }
                    189:             // If the readsize is less than buffersize, we assume EOF.
                    190:             if(readSize < READ_BUFFER_SIZE && bufPtr == readSize) {
                    191:                 if (tokenPtr > 0)
                    192:                     finished = 1;
                    193:                 else
                    194:                     return NULL;
                    195:             }
                    196:         }
                    197:         if(tokenPtr>0) {
                    198:             validToken = 1;
                    199:             return cToken;
                    200:         }
                    201:     }
                    202:     return NULL;
                    203: }
                    204: 
                    205: 
                    206: /**
                    207: *   Returns the currently active token, or null
                    208: *   if no tokens are availible.
                    209: */
                    210: char *getCurrentConfigToken() {
                    211:     return validToken ? cToken : NULL;
                    212: }
                    213: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>