r6369: update release notes
[Samba.git] / source / param / params.c
blob3b736113befc7bcf383210819ed642fcc1feb7e3
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 2 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, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * -------------------------------------------------------------------------- **
27 * Module name: params
29 * -------------------------------------------------------------------------- **
31 * This module performs lexical analysis and initial parsing of a
32 * Windows-like parameter file. It recognizes and handles four token
33 * types: section-name, parameter-name, parameter-value, and
34 * end-of-file. Comments and line continuation are handled
35 * internally.
37 * The entry point to the module is function pm_process(). This
38 * function opens the source file, calls the Parse() function to parse
39 * the input, and then closes the file when either the EOF is reached
40 * or a fatal error is encountered.
42 * A sample parameter file might look like this:
44 * [section one]
45 * parameter one = value string
46 * parameter two = another value
47 * [section two]
48 * new parameter = some value or t'other
50 * The parameter file is divided into sections by section headers:
51 * section names enclosed in square brackets (eg. [section one]).
52 * Each section contains parameter lines, each of which consist of a
53 * parameter name and value delimited by an equal sign. Roughly, the
54 * syntax is:
56 * <file> :== { <section> } EOF
58 * <section> :== <section header> { <parameter line> }
60 * <section header> :== '[' NAME ']'
62 * <parameter line> :== NAME '=' VALUE '\n'
64 * Blank lines and comment lines are ignored. Comment lines are lines
65 * beginning with either a semicolon (';') or a pound sign ('#').
67 * All whitespace in section names and parameter names is compressed
68 * to single spaces. Leading and trailing whitespace is stipped from
69 * both names and values.
71 * Only the first equals sign in a parameter line is significant.
72 * Parameter values may contain equals signs, square brackets and
73 * semicolons. Internal whitespace is retained in parameter values,
74 * with the exception of the '\r' character, which is stripped for
75 * historic reasons. Parameter names may not start with a left square
76 * bracket, an equal sign, a pound sign, or a semicolon, because these
77 * are used to identify other tokens.
79 * -------------------------------------------------------------------------- **
82 #include "includes.h"
84 extern BOOL in_client;
86 /* -------------------------------------------------------------------------- **
87 * Constants...
90 #define BUFR_INC 1024
93 /* -------------------------------------------------------------------------- **
94 * Variables...
96 * DEBUGLEVEL - The ubiquitous DEBUGLEVEL. This determines which DEBUG()
97 * messages will be produced.
98 * bufr - pointer to a global buffer. This is probably a kludge,
99 * but it was the nicest kludge I could think of (for now).
100 * bSize - The size of the global buffer <bufr>.
103 static char *bufr = NULL;
104 static int bSize = 0;
106 /* we can't use FILE* due to the 256 fd limit - use this cheap hack
107 instead */
108 typedef struct {
109 char *buf;
110 char *p;
111 size_t size;
112 char *end_section_p;
113 } myFILE;
115 static int mygetc(myFILE *f)
117 if (f->p >= f->buf+f->size)
118 return EOF;
119 /* be sure to return chars >127 as positive values */
120 return (int)( *(f->p++) & 0x00FF );
123 static void myfile_close(myFILE *f)
125 if (!f)
126 return;
127 SAFE_FREE(f->buf);
128 SAFE_FREE(f);
131 /* Find the end of the section. We must use mb functions for this. */
132 static int FindSectionEnd(myFILE *f)
134 f->end_section_p = strchr_m(f->p, ']');
135 return f->end_section_p ? 1 : 0;
138 static int AtSectionEnd(myFILE *f)
140 if (f->p == f->end_section_p + 1) {
141 f->end_section_p = NULL;
142 return 1;
144 return 0;
147 /* -------------------------------------------------------------------------- **
148 * Functions...
150 /* ------------------------------------------------------------------------ **
151 * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
152 * character, or newline, or EOF.
154 * Input: InFile - Input source.
156 * Output: The next non-whitespace character in the input stream.
158 * Notes: Because the config files use a line-oriented grammar, we
159 * explicitly exclude the newline character from the list of
160 * whitespace characters.
161 * - Note that both EOF (-1) and the nul character ('\0') are
162 * considered end-of-file markers.
164 * ------------------------------------------------------------------------ **
167 static int EatWhitespace( myFILE *InFile )
169 int c;
171 for( c = mygetc( InFile ); isspace( c ) && ('\n' != c); c = mygetc( InFile ) )
173 return( c );
176 /* ------------------------------------------------------------------------ **
177 * Scan to the end of a comment.
179 * Input: InFile - Input source.
181 * Output: The character that marks the end of the comment. Normally,
182 * this will be a newline, but it *might* be an EOF.
184 * Notes: Because the config files use a line-oriented grammar, we
185 * explicitly exclude the newline character from the list of
186 * whitespace characters.
187 * - Note that both EOF (-1) and the nul character ('\0') are
188 * considered end-of-file markers.
190 * ------------------------------------------------------------------------ **
193 static int EatComment( myFILE *InFile )
195 int c;
197 for( c = mygetc( InFile ); ('\n'!=c) && (EOF!=c) && (c>0); c = mygetc( InFile ) )
199 return( c );
202 /*****************************************************************************
203 * Scan backards within a string to discover if the last non-whitespace
204 * character is a line-continuation character ('\\').
206 * Input: line - A pointer to a buffer containing the string to be
207 * scanned.
208 * pos - This is taken to be the offset of the end of the
209 * string. This position is *not* scanned.
211 * Output: The offset of the '\\' character if it was found, or -1 to
212 * indicate that it was not.
214 *****************************************************************************/
216 static int Continuation(char *line, int pos )
218 pos--;
219 while( (pos >= 0) && isspace((int)line[pos]))
220 pos--;
222 return (((pos >= 0) && ('\\' == line[pos])) ? pos : -1 );
225 /* ------------------------------------------------------------------------ **
226 * Scan a section name, and pass the name to function sfunc().
228 * Input: InFile - Input source.
229 * sfunc - Pointer to the function to be called if the section
230 * name is successfully read.
232 * Output: True if the section name was read and True was returned from
233 * <sfunc>. False if <sfunc> failed or if a lexical error was
234 * encountered.
236 * ------------------------------------------------------------------------ **
239 static BOOL Section( myFILE *InFile, BOOL (*sfunc)(const char *) )
241 int c;
242 int i;
243 int end;
244 const char *func = "params.c:Section() -";
246 i = 0; /* <i> is the offset of the next free byte in bufr[] and */
247 end = 0; /* <end> is the current "end of string" offset. In most */
248 /* cases these will be the same, but if the last */
249 /* character written to bufr[] is a space, then <end> */
250 /* will be one less than <i>. */
253 /* Find the end of the section. We must use mb functions for this. */
254 if (!FindSectionEnd(InFile)) {
255 DEBUG(0, ("%s No terminating ']' character in section.\n", func) );
256 return False;
259 c = EatWhitespace( InFile ); /* We've already got the '['. Scan */
260 /* past initial white space. */
262 while( (EOF != c) && (c > 0) ) {
263 /* Check that the buffer is big enough for the next character. */
264 if( i > (bSize - 2) ) {
265 char *tb;
267 tb = SMB_REALLOC( bufr, bSize +BUFR_INC );
268 if( NULL == tb ) {
269 DEBUG(0, ("%s Memory re-allocation failure.", func) );
270 return False;
272 bufr = tb;
273 bSize += BUFR_INC;
276 /* Handle a single character other than section end. */
277 switch( c ) {
278 case '\n': /* Got newline before closing ']'. */
279 i = Continuation( bufr, i ); /* Check for line continuation. */
280 if( i < 0 ) {
281 bufr[end] = '\0';
282 DEBUG(0, ("%s Badly formed line in configuration file: %s\n", func, bufr ));
283 return False;
285 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
286 c = mygetc( InFile ); /* Continue with next line. */
287 break;
289 default: /* All else are a valid name chars. */
290 if(isspace( c )) {
291 /* One space per whitespace region. */
292 bufr[end] = ' ';
293 i = end + 1;
294 c = EatWhitespace( InFile );
295 } else {
296 bufr[i++] = c;
297 end = i;
298 c = mygetc( InFile );
302 if (AtSectionEnd(InFile)) {
303 /* Got to the closing bracket. */
304 bufr[end] = '\0';
305 if( 0 == end ) {
306 /* Don't allow an empty name. */
307 DEBUG(0, ("%s Empty section name in configuration file.\n", func ));
308 return False;
310 if( !sfunc(bufr) ) /* Got a valid name. Deal with it. */
311 return False;
312 EatComment( InFile ); /* Finish off the line. */
313 return True;
318 /* We arrive here if we've met the EOF before the closing bracket. */
319 DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func, bufr ));
320 return False;
323 /* ------------------------------------------------------------------------ **
324 * Scan a parameter name and value, and pass these two fields to pfunc().
326 * Input: InFile - The input source.
327 * pfunc - A pointer to the function that will be called to
328 * process the parameter, once it has been scanned.
329 * c - The first character of the parameter name, which
330 * would have been read by Parse(). Unlike a comment
331 * line or a section header, there is no lead-in
332 * character that can be discarded.
334 * Output: True if the parameter name and value were scanned and processed
335 * successfully, else False.
337 * Notes: This function is in two parts. The first loop scans the
338 * parameter name. Internal whitespace is compressed, and an
339 * equal sign (=) terminates the token. Leading and trailing
340 * whitespace is discarded. The second loop scans the parameter
341 * value. When both have been successfully identified, they are
342 * passed to pfunc() for processing.
344 * ------------------------------------------------------------------------ **
347 static BOOL Parameter( myFILE *InFile, BOOL (*pfunc)(const char *, const char *), int c )
349 int i = 0; /* Position within bufr. */
350 int end = 0; /* bufr[end] is current end-of-string. */
351 int vstart = 0; /* Starting position of the parameter value. */
352 const char *func = "params.c:Parameter() -";
354 /* Read the parameter name. */
355 while( 0 == vstart ) {
356 /* Loop until we've found the start of the value. */
357 if( i > (bSize - 2) ) {
358 /* Ensure there's space for next char. */
359 char *tb = SMB_REALLOC( bufr, bSize + BUFR_INC );
360 if( NULL == tb ) {
361 DEBUG(0, ("%s Memory re-allocation failure.", func) );
362 return False;
364 bufr = tb;
365 bSize += BUFR_INC;
368 switch(c) {
369 case '=': /* Equal sign marks end of param name. */
370 if( 0 == end ) {
371 /* Don't allow an empty name. */
372 DEBUG(0, ("%s Invalid parameter name in config. file.\n", func ));
373 return False;
375 bufr[end++] = '\0'; /* Mark end of string & advance. */
376 i = end; /* New string starts here. */
377 vstart = end; /* New string is parameter value. */
378 bufr[i] = '\0'; /* New string is nul, for now. */
379 break;
381 case '\n': /* Find continuation char, else error. */
382 i = Continuation( bufr, i );
383 if( i < 0 ) {
384 bufr[end] = '\0';
385 DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n", func, bufr ));
386 return True;
388 end = ( (i > 0) && (' ' == bufr[i - 1]) ) ? (i - 1) : (i);
389 c = mygetc( InFile ); /* Read past eoln. */
390 break;
392 case '\0': /* Shouldn't have EOF within param name. */
393 case EOF:
394 bufr[i] = '\0';
395 DEBUG(1,("%s Unexpected end-of-file at: %s\n", func, bufr ));
396 return True;
398 default:
399 if(isspace( c )) {
400 /* One ' ' per whitespace region. */
401 bufr[end] = ' ';
402 i = end + 1;
403 c = EatWhitespace( InFile );
404 } else {
405 bufr[i++] = c;
406 end = i;
407 c = mygetc( InFile );
412 /* Now parse the value. */
413 c = EatWhitespace( InFile ); /* Again, trim leading whitespace. */
414 while( (EOF !=c) && (c > 0) ) {
415 if( i > (bSize - 2) ) {
416 /* Make sure there's enough room. */
417 char *tb = SMB_REALLOC( bufr, bSize + BUFR_INC );
418 if( NULL == tb ) {
419 DEBUG(0, ("%s Memory re-allocation failure.", func));
420 return False;
422 bufr = tb;
423 bSize += BUFR_INC;
426 switch(c) {
427 case '\r': /* Explicitly remove '\r' because the older */
428 c = mygetc( InFile ); /* version called fgets_slash() which also */
429 break; /* removes them. */
431 case '\n': /* Marks end of value unless there's a '\'. */
432 i = Continuation( bufr, i );
433 if( i < 0 ) {
434 c = 0;
435 } else {
436 for( end = i; (end >= 0) && isspace((int)bufr[end]); end-- )
438 c = mygetc( InFile );
440 break;
442 default: /* All others verbatim. Note that spaces do not advance <end>. This allows trimming */
443 bufr[i++] = c;
444 if( !isspace( c ) ) /* of whitespace at the end of the line. */
445 end = i;
446 c = mygetc( InFile );
447 break;
450 bufr[end] = '\0'; /* End of value. */
452 return( pfunc( bufr, &bufr[vstart] ) ); /* Pass name & value to pfunc(). */
455 /* ------------------------------------------------------------------------ **
456 * Scan & parse the input.
458 * Input: InFile - Input source.
459 * sfunc - Function to be called when a section name is scanned.
460 * See Section().
461 * pfunc - Function to be called when a parameter is scanned.
462 * See Parameter().
464 * Output: True if the file was successfully scanned, else False.
466 * Notes: The input can be viewed in terms of 'lines'. There are four
467 * types of lines:
468 * Blank - May contain whitespace, otherwise empty.
469 * Comment - First non-whitespace character is a ';' or '#'.
470 * The remainder of the line is ignored.
471 * Section - First non-whitespace character is a '['.
472 * Parameter - The default case.
474 * ------------------------------------------------------------------------ **
477 static BOOL Parse( myFILE *InFile,
478 BOOL (*sfunc)(const char *),
479 BOOL (*pfunc)(const char *, const char *) )
481 int c;
483 c = EatWhitespace( InFile );
484 while( (EOF != c) && (c > 0) ) {
485 switch( c ) {
486 case '\n': /* Blank line. */
487 c = EatWhitespace( InFile );
488 break;
490 case ';': /* Comment line. */
491 case '#':
492 c = EatComment( InFile );
493 break;
495 case '[': /* Section Header. */
496 if( !Section( InFile, sfunc ) )
497 return False;
498 c = EatWhitespace( InFile );
499 break;
501 case '\\': /* Bogus backslash. */
502 c = EatWhitespace( InFile );
503 break;
505 default: /* Parameter line. */
506 if( !Parameter( InFile, pfunc, c ) )
507 return False;
508 c = EatWhitespace( InFile );
509 break;
512 return True;
515 /* ------------------------------------------------------------------------ **
516 * Open a configuration file.
518 * Input: FileName - The pathname of the config file to be opened.
520 * Output: A pointer of type (char **) to the lines of the file
522 * ------------------------------------------------------------------------ **
525 static myFILE *OpenConfFile( const char *FileName )
527 const char *func = "params.c:OpenConfFile() -";
528 int lvl = in_client?1:0;
529 myFILE *ret;
531 ret = SMB_MALLOC_P(myFILE);
532 if (!ret)
533 return NULL;
535 ret->buf = file_load(FileName, &ret->size);
536 if( NULL == ret->buf ) {
537 DEBUG( lvl, ("%s Unable to open configuration file \"%s\":\n\t%s\n",
538 func, FileName, strerror(errno)) );
539 SAFE_FREE(ret);
540 return NULL;
543 ret->p = ret->buf;
544 ret->end_section_p = NULL;
545 return( ret );
548 /* ------------------------------------------------------------------------ **
549 * Process the named parameter file.
551 * Input: FileName - The pathname of the parameter file to be opened.
552 * sfunc - A pointer to a function that will be called when
553 * a section name is discovered.
554 * pfunc - A pointer to a function that will be called when
555 * a parameter name and value are discovered.
557 * Output: TRUE if the file was successfully parsed, else FALSE.
559 * ------------------------------------------------------------------------ **
562 BOOL pm_process( const char *FileName,
563 BOOL (*sfunc)(const char *),
564 BOOL (*pfunc)(const char *, const char *) )
566 int result;
567 myFILE *InFile;
568 const char *func = "params.c:pm_process() -";
570 InFile = OpenConfFile( FileName ); /* Open the config file. */
571 if( NULL == InFile )
572 return False;
574 DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func, FileName) );
576 if( NULL != bufr ) {
577 /* If we already have a buffer */
578 /* (recursive call), then just */
579 /* use it. */
580 result = Parse( InFile, sfunc, pfunc );
581 } else {
582 bSize = BUFR_INC;
583 bufr = (char *)SMB_MALLOC( bSize );
584 if( NULL == bufr ) {
585 DEBUG(0,("%s memory allocation failure.\n", func));
586 myfile_close(InFile);
587 return False;
590 result = Parse( InFile, sfunc, pfunc );
591 SAFE_FREE( bufr );
592 bufr = NULL;
593 bSize = 0;
596 myfile_close(InFile);
598 if( !result ) {
599 DEBUG(0,("%s Failed. Error returned from params.c:parse().\n", func));
600 return False;
603 return True;