Fix bug 5681: Do not limit the number of network interfaces
[Samba.git] / lib / util / params.c
blob80adc71fca2ed96e0d9b0bcc8df139c4903d0818
1 /* -------------------------------------------------------------------------- **
2 * Microsoft Network Services for Unix, AKA., Andrew Tridgell's SAMBA.
4 * This module Copyright (C) 1990-1998 Karl Auer
6 * Rewritten almost completely by Christopher R. Hertel
7 * at the University of Minnesota, September, 1997.
8 * This module Copyright (C) 1997-1998 by the University of Minnesota
9 * -------------------------------------------------------------------------- **
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, see <http://www.gnu.org/licenses/>.
24 * -------------------------------------------------------------------------- **
26 * Module name: params
28 * -------------------------------------------------------------------------- **
30 * This module performs lexical analysis and initial parsing of a
31 * Windows-like parameter file. It recognizes and handles four token
32 * types: section-name, parameter-name, parameter-value, and
33 * end-of-file. Comments and line continuation are handled
34 * internally.
36 * The entry point to the module is function pm_process(). This
37 * function opens the source file, calls the Parse() function to parse
38 * the input, and then closes the file when either the EOF is reached
39 * or a fatal error is encountered.
41 * A sample parameter file might look like this:
43 * [section one]
44 * parameter one = value string
45 * parameter two = another value
46 * [section two]
47 * new parameter = some value or t'other
49 * The parameter file is divided into sections by section headers:
50 * section names enclosed in square brackets (eg. [section one]).
51 * Each section contains parameter lines, each of which consist of a
52 * parameter name and value delimited by an equal sign. Roughly, the
53 * syntax is:
55 * <file> :== { <section> } EOF
57 * <section> :== <section header> { <parameter line> }
59 * <section header> :== '[' NAME ']'
61 * <parameter line> :== NAME '=' VALUE '\n'
63 * Blank lines and comment lines are ignored. Comment lines are lines
64 * beginning with either a semicolon (';') or a pound sign ('#').
66 * All whitespace in section names and parameter names is compressed
67 * to single spaces. Leading and trailing whitespace is stipped from
68 * both names and values.
70 * Only the first equals sign in a parameter line is significant.
71 * Parameter values may contain equals signs, square brackets and
72 * semicolons. Internal whitespace is retained in parameter values,
73 * with the exception of the '\r' character, which is stripped for
74 * historic reasons. Parameter names may not start with a left square
75 * bracket, an equal sign, a pound sign, or a semicolon, because these
76 * are used to identify other tokens.
78 * -------------------------------------------------------------------------- **
81 #include "includes.h"
82 #include "system/locale.h"
84 /* -------------------------------------------------------------------------- **
85 * Constants...
88 #define BUFR_INC 1024
91 /* we can't use FILE* due to the 256 fd limit - use this cheap hack
92 instead */
93 typedef struct {
94 char *buf;
95 char *p;
96 size_t size;
97 char *bufr;
98 int bSize;
99 } myFILE;
101 static int mygetc(myFILE *f)
103 if (f->p >= f->buf+f->size) return EOF;
104 /* be sure to return chars >127 as positive values */
105 return (int)( *(f->p++) & 0x00FF );
108 /* -------------------------------------------------------------------------- **
109 * Functions...
112 static int EatWhitespace( myFILE *InFile )
113 /* ------------------------------------------------------------------------ **
114 * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
115 * character, or newline, or EOF.
117 * Input: InFile - Input source.
119 * Output: The next non-whitespace character in the input stream.
121 * Notes: Because the config files use a line-oriented grammar, we
122 * explicitly exclude the newline character from the list of
123 * whitespace characters.
124 * - Note that both EOF (-1) and the nul character ('\0') are
125 * considered end-of-file markers.
127 * ------------------------------------------------------------------------ **
130 int c;
132 for( c = mygetc( InFile ); isspace( c ) && ('\n' != c); c = mygetc( InFile ) )
134 return( c );
135 } /* EatWhitespace */
137 static int EatComment( myFILE *InFile )
138 /* ------------------------------------------------------------------------ **
139 * Scan to the end of a comment.
141 * Input: InFile - Input source.
143 * Output: The character that marks the end of the comment. Normally,
144 * this will be a newline, but it *might* be an EOF.
146 * Notes: Because the config files use a line-oriented grammar, we
147 * explicitly exclude the newline character from the list of
148 * whitespace characters.
149 * - Note that both EOF (-1) and the nul character ('\0') are
150 * considered end-of-file markers.
152 * ------------------------------------------------------------------------ **
155 int c;
157 for( c = mygetc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = mygetc( InFile ) )
159 return( c );
160 } /* EatComment */
162 /*****************************************************************************
163 * Scan backards within a string to discover if the last non-whitespace
164 * character is a line-continuation character ('\\').
166 * Input: line - A pointer to a buffer containing the string to be
167 * scanned.
168 * pos - This is taken to be the offset of the end of the
169 * string. This position is *not* scanned.
171 * Output: The offset of the '\\' character if it was found, or -1 to
172 * indicate that it was not.
174 *****************************************************************************/
176 static int Continuation(char *line, int pos )
178 pos--;
179 while( (pos >= 0) && isspace((int)line[pos]))
180 pos--;
182 return (((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
186 static bool Section( myFILE *InFile, bool (*sfunc)(const char *, void *), void *userdata )
187 /* ------------------------------------------------------------------------ **
188 * Scan a section name, and pass the name to function sfunc().
190 * Input: InFile - Input source.
191 * sfunc - Pointer to the function to be called if the section
192 * name is successfully read.
194 * Output: true if the section name was read and true was returned from
195 * <sfunc>. false if <sfunc> failed or if a lexical error was
196 * encountered.
198 * ------------------------------------------------------------------------ **
201 int c;
202 int i;
203 int end;
204 const char *func = "params.c:Section() -";
206 i = 0; /* <i> is the offset of the next free byte in bufr[] and */
207 end = 0; /* <end> is the current "end of string" offset. In most */
208 /* cases these will be the same, but if the last */
209 /* character written to bufr[] is a space, then <end> */
210 /* will be one less than <i>. */
212 c = EatWhitespace( InFile ); /* We've already got the '['. Scan */
213 /* past initial white space. */
215 while( (EOF != c) && (c > 0) )
218 /* Check that the buffer is big enough for the next character. */
219 if( i > (InFile->bSize - 2) )
221 char *tb;
223 tb = talloc_realloc(InFile, InFile->bufr, char, InFile->bSize + BUFR_INC);
224 if( NULL == tb )
226 DEBUG(0, ("%s Memory re-allocation failure.", func) );
227 return( false );
229 InFile->bufr = tb;
230 InFile->bSize += BUFR_INC;
233 /* Handle a single character. */
234 switch( c )
236 case ']': /* Found the closing bracket. */
237 InFile->bufr[end] = '\0';
238 if( 0 == end ) /* Don't allow an empty name. */
240 DEBUG(0, ("%s Empty section name in configuration file.\n", func ));
241 return( false );
243 if( !sfunc(InFile->bufr,userdata) ) /* Got a valid name. Deal with it. */
244 return( false );
245 (void)EatComment( InFile ); /* Finish off the line. */
246 return( true );
248 case '\n': /* Got newline before closing ']'. */
249 i = Continuation( InFile->bufr, i ); /* Check for line continuation. */
250 if( i < 0 )
252 InFile->bufr[end] = '\0';
253 DEBUG(0, ("%s Badly formed line in configuration file: %s\n",
254 func, InFile->bufr ));
255 return( false );
257 end = ( (i > 0) && (' ' == InFile->bufr[i - 1]) ) ? (i - 1) : (i);
258 c = mygetc( InFile ); /* Continue with next line. */
259 break;
261 default: /* All else are a valid name chars. */
262 if( isspace( c ) ) /* One space per whitespace region. */
264 InFile->bufr[end] = ' ';
265 i = end + 1;
266 c = EatWhitespace( InFile );
268 else /* All others copy verbatim. */
270 InFile->bufr[i++] = c;
271 end = i;
272 c = mygetc( InFile );
277 /* We arrive here if we've met the EOF before the closing bracket. */
278 DEBUG(0, ("%s Unexpected EOF in the configuration file\n", func));
279 return( false );
280 } /* Section */
282 static bool Parameter( myFILE *InFile, bool (*pfunc)(const char *, const char *, void *), int c, void *userdata )
283 /* ------------------------------------------------------------------------ **
284 * Scan a parameter name and value, and pass these two fields to pfunc().
286 * Input: InFile - The input source.
287 * pfunc - A pointer to the function that will be called to
288 * process the parameter, once it has been scanned.
289 * c - The first character of the parameter name, which
290 * would have been read by Parse(). Unlike a comment
291 * line or a section header, there is no lead-in
292 * character that can be discarded.
294 * Output: true if the parameter name and value were scanned and processed
295 * successfully, else false.
297 * Notes: This function is in two parts. The first loop scans the
298 * parameter name. Internal whitespace is compressed, and an
299 * equal sign (=) terminates the token. Leading and trailing
300 * whitespace is discarded. The second loop scans the parameter
301 * value. When both have been successfully identified, they are
302 * passed to pfunc() for processing.
304 * ------------------------------------------------------------------------ **
307 int i = 0; /* Position within bufr. */
308 int end = 0; /* bufr[end] is current end-of-string. */
309 int vstart = 0; /* Starting position of the parameter value. */
310 const char *func = "params.c:Parameter() -";
312 /* Read the parameter name. */
313 while( 0 == vstart ) /* Loop until we've found the start of the value. */
316 if( i > (InFile->bSize - 2) ) /* Ensure there's space for next char. */
318 char *tb;
320 tb = talloc_realloc(InFile, InFile->bufr, char, InFile->bSize + BUFR_INC );
321 if( NULL == tb )
323 DEBUG(0, ("%s Memory re-allocation failure.", func) );
324 return( false );
326 InFile->bufr = tb;
327 InFile->bSize += BUFR_INC;
330 switch( c )
332 case '=': /* Equal sign marks end of param name. */
333 if( 0 == end ) /* Don't allow an empty name. */
335 DEBUG(0, ("%s Invalid parameter name in config. file.\n", func ));
336 return( false );
338 InFile->bufr[end++] = '\0'; /* Mark end of string & advance. */
339 i = end; /* New string starts here. */
340 vstart = end; /* New string is parameter value. */
341 InFile->bufr[i] = '\0'; /* New string is nul, for now. */
342 break;
344 case '\n': /* Find continuation char, else error. */
345 i = Continuation( InFile->bufr, i );
346 if( i < 0 )
348 InFile->bufr[end] = '\0';
349 DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n",
350 func, InFile->bufr ));
351 return( true );
353 end = ( (i > 0) && (' ' == InFile->bufr[i - 1]) ) ? (i - 1) : (i);
354 c = mygetc( InFile ); /* Read past eoln. */
355 break;
357 case '\0': /* Shouldn't have EOF within param name. */
358 case EOF:
359 InFile->bufr[i] = '\0';
360 DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, InFile->bufr ));
361 return( true );
363 default:
364 if( isspace( c ) ) /* One ' ' per whitespace region. */
366 InFile->bufr[end] = ' ';
367 i = end + 1;
368 c = EatWhitespace( InFile );
370 else /* All others verbatim. */
372 InFile->bufr[i++] = c;
373 end = i;
374 c = mygetc( InFile );
379 /* Now parse the value. */
380 c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */
381 while( (EOF !=c) && (c > 0) )
384 if( i > (InFile->bSize - 2) ) /* Make sure there's enough room. */
386 char *tb;
388 tb = talloc_realloc(InFile, InFile->bufr, char, InFile->bSize + BUFR_INC );
389 if( NULL == tb )
391 DEBUG(0, ("%s Memory re-allocation failure.", func) );
392 return( false );
394 InFile->bufr = tb;
395 InFile->bSize += BUFR_INC;
398 switch( c )
400 case '\r': /* Explicitly remove '\r' because the older */
401 c = mygetc( InFile ); /* version called fgets_slash() which also */
402 break; /* removes them. */
404 case '\n': /* Marks end of value unless there's a '\'. */
405 i = Continuation( InFile->bufr, i );
406 if( i < 0 )
407 c = 0;
408 else
410 for( end = i; (end >= 0) && isspace((int)InFile->bufr[end]); end-- )
412 c = mygetc( InFile );
414 break;
416 default: /* All others verbatim. Note that spaces do */
417 InFile->bufr[i++] = c; /* not advance <end>. This allows trimming */
418 if( !isspace( c ) ) /* of whitespace at the end of the line. */
419 end = i;
420 c = mygetc( InFile );
421 break;
424 InFile->bufr[end] = '\0'; /* End of value. */
426 return( pfunc( InFile->bufr, &InFile->bufr[vstart], userdata ) ); /* Pass name & value to pfunc(). */
427 } /* Parameter */
429 static bool Parse( myFILE *InFile,
430 bool (*sfunc)(const char *, void *),
431 bool (*pfunc)(const char *, const char *, void *),
432 void *userdata )
433 /* ------------------------------------------------------------------------ **
434 * Scan & parse the input.
436 * Input: InFile - Input source.
437 * sfunc - Function to be called when a section name is scanned.
438 * See Section().
439 * pfunc - Function to be called when a parameter is scanned.
440 * See Parameter().
442 * Output: true if the file was successfully scanned, else false.
444 * Notes: The input can be viewed in terms of 'lines'. There are four
445 * types of lines:
446 * Blank - May contain whitespace, otherwise empty.
447 * Comment - First non-whitespace character is a ';' or '#'.
448 * The remainder of the line is ignored.
449 * Section - First non-whitespace character is a '['.
450 * Parameter - The default case.
452 * ------------------------------------------------------------------------ **
455 int c;
457 c = EatWhitespace( InFile );
458 while( (EOF != c) && (c > 0) )
460 switch( c )
462 case '\n': /* Blank line. */
463 c = EatWhitespace( InFile );
464 break;
466 case ';': /* Comment line. */
467 case '#':
468 c = EatComment( InFile );
469 break;
471 case '[': /* Section Header. */
472 if( !Section( InFile, sfunc, userdata ) )
473 return( false );
474 c = EatWhitespace( InFile );
475 break;
477 case '\\': /* Bogus backslash. */
478 c = EatWhitespace( InFile );
479 break;
481 default: /* Parameter line. */
482 if( !Parameter( InFile, pfunc, c, userdata ) )
483 return( false );
484 c = EatWhitespace( InFile );
485 break;
488 return( true );
489 } /* Parse */
491 static myFILE *OpenConfFile( const char *FileName )
492 /* ------------------------------------------------------------------------ **
493 * Open a configuration file.
495 * Input: FileName - The pathname of the config file to be opened.
497 * Output: A pointer of type (char **) to the lines of the file
499 * ------------------------------------------------------------------------ **
502 const char *func = "params.c:OpenConfFile() -";
503 myFILE *ret;
505 ret = talloc(talloc_autofree_context(), myFILE);
506 if (!ret) return NULL;
508 ret->buf = file_load(FileName, &ret->size, 0, ret);
509 if( NULL == ret->buf )
511 DEBUG( 1,
512 ("%s Unable to open configuration file \"%s\":\n\t%s\n",
513 func, FileName, strerror(errno)) );
514 talloc_free(ret);
515 return NULL;
518 ret->p = ret->buf;
519 ret->bufr = NULL;
520 ret->bSize = 0;
521 return( ret );
522 } /* OpenConfFile */
524 bool pm_process( const char *FileName,
525 bool (*sfunc)(const char *, void *),
526 bool (*pfunc)(const char *, const char *, void *),
527 void *userdata)
528 /* ------------------------------------------------------------------------ **
529 * Process the named parameter file.
531 * Input: FileName - The pathname of the parameter file to be opened.
532 * sfunc - A pointer to a function that will be called when
533 * a section name is discovered.
534 * pfunc - A pointer to a function that will be called when
535 * a parameter name and value are discovered.
537 * Output: TRUE if the file was successfully parsed, else FALSE.
539 * ------------------------------------------------------------------------ **
542 int result;
543 myFILE *InFile;
544 const char *func = "params.c:pm_process() -";
546 InFile = OpenConfFile( FileName ); /* Open the config file. */
547 if( NULL == InFile )
548 return( false );
550 DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) );
552 if( NULL != InFile->bufr ) /* If we already have a buffer */
553 result = Parse( InFile, sfunc, pfunc, userdata ); /* (recursive call), then just */
554 /* use it. */
556 else /* If we don't have a buffer */
557 { /* allocate one, then parse, */
558 InFile->bSize = BUFR_INC; /* then free. */
559 InFile->bufr = talloc_array(InFile, char, InFile->bSize );
560 if( NULL == InFile->bufr )
562 DEBUG(0,("%s memory allocation failure.\n", func));
563 talloc_free(InFile);
564 return( false );
566 result = Parse( InFile, sfunc, pfunc, userdata );
567 InFile->bufr = NULL;
568 InFile->bSize = 0;
571 talloc_free(InFile);
573 if( !result ) /* Generic failure. */
575 DEBUG(3,("%s Failed. Error returned from params.c:parse().\n", func));
576 return( false );
579 return( true ); /* Generic success. */
580 } /* pm_process */
582 /* -------------------------------------------------------------------------- */