Changes to update Tomato RAF.
[tomato.git] / release / src / router / igmpproxy / src / confread.c
blob6e267dc65c0f56bdc614544544cee8a2e3b8545f
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.
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
19 **----------------------------------------------------------------------------
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.
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.
33 /**
34 * confread.c
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
45 #include "igmpproxy.h"
47 #define READ_BUFFER_SIZE 512 // Inputbuffer size...
49 #ifndef MAX_TOKEN_LENGTH
50 #define MAX_TOKEN_LENGTH 30 // Default max token length
51 #endif
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;
60 /**
61 * Opens config file specified by filename.
62 */
63 int openConfigFile(char *filename) {
65 // Set the buffer to null initially...
66 iBuffer = NULL;
68 // Open the file for reading...
69 confFilePtr = fopen(filename, "r");
71 // On error, return false
72 if(confFilePtr == NULL) {
73 return 0;
76 // Allocate memory for inputbuffer...
77 iBuffer = (char*) malloc( sizeof(char) * READ_BUFFER_SIZE );
79 if(iBuffer == NULL) {
80 closeConfigFile();
81 return 0;
84 // Reset bufferpointer and readsize
85 bufPtr = 0;
86 readSize = 0;
88 return 1;
91 /**
92 * Closes the currently open config file.
94 void closeConfigFile() {
95 // Close the file.
96 if(confFilePtr!=NULL) {
97 fclose(confFilePtr);
99 // Free input buffer memory...
100 if(iBuffer != NULL) {
101 free(iBuffer);
106 * Returns the next token from the configfile. The function
107 * return NULL if there are no more tokens in the file.
109 char *nextConfigToken() {
111 validToken = 0;
113 // If no file or buffer, return NULL
114 if(confFilePtr == NULL || iBuffer == NULL) {
115 return NULL;
119 unsigned int tokenPtr = 0;
120 unsigned short finished = 0;
121 unsigned short commentFound = 0;
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;
131 // If the readsize is 0, we should just return...
132 if(readSize == 0) {
133 return NULL;
137 // Inner char loop...
138 while ( bufPtr < readSize && !finished ) {
140 //printf("Char %s", iBuffer[bufPtr]);
142 // Break loop on \0
143 if(iBuffer[bufPtr] == '\0') {
144 break;
147 if( commentFound ) {
148 if( iBuffer[bufPtr] == '\n' ) {
149 commentFound = 0;
151 } else {
153 // Check current char...
154 switch(iBuffer[bufPtr]) {
155 case '#':
156 // Found a comment start...
157 commentFound = 1;
158 break;
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;
169 break;
171 default:
172 // Append char to token...
173 cToken[tokenPtr++] = iBuffer[bufPtr];
174 break;
179 // Check end of token buffer !!!
180 if(tokenPtr == MAX_TOKEN_LENGTH - 1) {
181 // Prevent buffer overrun...
182 cToken[tokenPtr] = '\0';
183 finished = 1;
186 // Next char...
187 bufPtr++;
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;
197 if(tokenPtr>0) {
198 validToken = 1;
199 return cToken;
202 return NULL;
207 * Returns the currently active token, or null
208 * if no tokens are availible.
210 char *getCurrentConfigToken() {
211 return validToken ? cToken : NULL;