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, 1997.
7 * This module Copyright (C) 1997-1998 by Christopher R. Hertel
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 * -------------------------------------------------------------------------- **
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
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:
44 * parameter one = value string
45 * parameter two = another value
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
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 * -------------------------------------------------------------------------- **
83 /* -------------------------------------------------------------------------- **
90 /* -------------------------------------------------------------------------- **
93 * DEBUGLEVEL - The ubiquitous DEBUGLEVEL. This determines which DEBUG()
94 * messages will be produced.
95 * bufr - pointer to a global buffer. This is probably a kludge,
96 * but it was the nicest kludge I could think of (for now).
97 * bSize - The size of the global buffer <bufr>.
100 /* we can't use FILE* due to the 256 fd limit - use this cheap hack
109 static int mygetc(myFILE
*f
)
111 if (f
->p
>= f
->buf
+f
->size
)
113 /* be sure to return chars >127 as positive values */
114 return (int)( *(f
->p
++) & 0x00FF );
117 static void myfile_close(myFILE
*f
)
125 /* Find the end of the section. We must use mb functions for this. */
126 static int FindSectionEnd(myFILE
*f
)
128 f
->end_section_p
= strchr_m(f
->p
, ']');
129 return f
->end_section_p
? 1 : 0;
132 static int AtSectionEnd(myFILE
*f
)
134 if (f
->p
== f
->end_section_p
+ 1) {
135 f
->end_section_p
= NULL
;
141 /* -------------------------------------------------------------------------- **
144 /* ------------------------------------------------------------------------ **
145 * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
146 * character, or newline, or EOF.
148 * Input: InFile - Input source.
150 * Output: The next non-whitespace character in the input stream.
152 * Notes: Because the config files use a line-oriented grammar, we
153 * explicitly exclude the newline character from the list of
154 * whitespace characters.
155 * - Note that both EOF (-1) and the nul character ('\0') are
156 * considered end-of-file markers.
158 * ------------------------------------------------------------------------ **
161 static int EatWhitespace( myFILE
*InFile
)
165 for( c
= mygetc( InFile
); isspace( c
) && ('\n' != c
); c
= mygetc( InFile
) )
170 /* ------------------------------------------------------------------------ **
171 * Scan to the end of a comment.
173 * Input: InFile - Input source.
175 * Output: The character that marks the end of the comment. Normally,
176 * this will be a newline, but it *might* be an EOF.
178 * Notes: Because the config files use a line-oriented grammar, we
179 * explicitly exclude the newline character from the list of
180 * whitespace characters.
181 * - Note that both EOF (-1) and the nul character ('\0') are
182 * considered end-of-file markers.
184 * ------------------------------------------------------------------------ **
187 static int EatComment( myFILE
*InFile
)
191 for( c
= mygetc( InFile
); ('\n'!=c
) && (EOF
!=c
) && (c
>0); c
= mygetc( InFile
) )
196 /*****************************************************************************
197 * Scan backards within a string to discover if the last non-whitespace
198 * character is a line-continuation character ('\\').
200 * Input: line - A pointer to a buffer containing the string to be
202 * pos - This is taken to be the offset of the end of the
203 * string. This position is *not* scanned.
205 * Output: The offset of the '\\' character if it was found, or -1 to
206 * indicate that it was not.
208 *****************************************************************************/
210 static int Continuation(uint8_t *line
, int pos
)
213 while( (pos
>= 0) && isspace((int)line
[pos
]))
216 return (((pos
>= 0) && ('\\' == line
[pos
])) ? pos
: -1 );
219 /* ------------------------------------------------------------------------ **
220 * Scan a section name, and pass the name to function sfunc().
222 * Input: InFile - Input source.
223 * sfunc - Pointer to the function to be called if the section
224 * name is successfully read.
226 * Output: True if the section name was read and True was returned from
227 * <sfunc>. False if <sfunc> failed or if a lexical error was
230 * ------------------------------------------------------------------------ **
233 static bool Section( DATA_BLOB
*buf
, myFILE
*InFile
, bool (*sfunc
)(const char *, void *), void *userdata
)
238 const char *func
= "params.c:Section() -";
240 i
= 0; /* <i> is the offset of the next free byte in bufr[] and */
241 end
= 0; /* <end> is the current "end of string" offset. In most */
242 /* cases these will be the same, but if the last */
243 /* character written to bufr[] is a space, then <end> */
244 /* will be one less than <i>. */
247 /* Find the end of the section. We must use mb functions for this. */
248 if (!FindSectionEnd(InFile
)) {
249 DEBUG(0, ("%s No terminating ']' character in section.\n", func
) );
253 c
= EatWhitespace( InFile
); /* We've already got the '['. Scan */
254 /* past initial white space. */
256 while( (EOF
!= c
) && (c
> 0) ) {
257 /* Check that the buffer is big enough for the next character. */
258 if( i
> (buf
->length
- 2) ) {
259 uint8_t *tb
= (uint8_t *)SMB_REALLOC_KEEP_OLD_ON_ERROR(buf
->data
, buf
->length
+BUFR_INC
);
261 DEBUG(0, ("%s Memory re-allocation failure.", func
) );
265 buf
->length
+= BUFR_INC
;
268 /* Handle a single character other than section end. */
270 case '\n': /* Got newline before closing ']'. */
271 i
= Continuation( buf
->data
, i
); /* Check for line continuation. */
273 buf
->data
[end
] = '\0';
274 DEBUG(0, ("%s Badly formed line in configuration file: %s\n", func
, buf
->data
));
277 end
= ( (i
> 0) && (' ' == buf
->data
[i
- 1]) ) ? (i
- 1) : (i
);
278 c
= mygetc( InFile
); /* Continue with next line. */
281 default: /* All else are a valid name chars. */
283 /* One space per whitespace region. */
284 buf
->data
[end
] = ' ';
286 c
= EatWhitespace( InFile
);
290 c
= mygetc( InFile
);
294 if (AtSectionEnd(InFile
)) {
295 /* Got to the closing bracket. */
296 buf
->data
[end
] = '\0';
298 /* Don't allow an empty name. */
299 DEBUG(0, ("%s Empty section name in configuration file.\n", func
));
302 if( !sfunc((char *)buf
->data
, userdata
) ) /* Got a valid name. Deal with it. */
304 EatComment( InFile
); /* Finish off the line. */
310 /* We arrive here if we've met the EOF before the closing bracket. */
311 DEBUG(0, ("%s Unexpected EOF in the configuration file: %s\n", func
, buf
->data
));
315 /* ------------------------------------------------------------------------ **
316 * Scan a parameter name and value, and pass these two fields to pfunc().
318 * Input: InFile - The input source.
319 * pfunc - A pointer to the function that will be called to
320 * process the parameter, once it has been scanned.
321 * c - The first character of the parameter name, which
322 * would have been read by Parse(). Unlike a comment
323 * line or a section header, there is no lead-in
324 * character that can be discarded.
326 * Output: True if the parameter name and value were scanned and processed
327 * successfully, else False.
329 * Notes: This function is in two parts. The first loop scans the
330 * parameter name. Internal whitespace is compressed, and an
331 * equal sign (=) terminates the token. Leading and trailing
332 * whitespace is discarded. The second loop scans the parameter
333 * value. When both have been successfully identified, they are
334 * passed to pfunc() for processing.
336 * ------------------------------------------------------------------------ **
339 static bool Parameter( DATA_BLOB
*buf
, myFILE
*InFile
, bool (*pfunc
)(const char *, const char *, void *), int c
, void *userdata
)
341 int i
= 0; /* Position within bufr. */
342 int end
= 0; /* bufr[end] is current end-of-string. */
343 int vstart
= 0; /* Starting position of the parameter value. */
344 const char *func
= "params.c:Parameter() -";
346 /* Read the parameter name. */
347 while( 0 == vstart
) {
348 /* Loop until we've found the start of the value. */
349 if( i
> (buf
->length
- 2) ) {
350 /* Ensure there's space for next char. */
351 uint8_t *tb
= (uint8_t *)SMB_REALLOC_KEEP_OLD_ON_ERROR( buf
->data
, buf
->length
+ BUFR_INC
);
353 DEBUG(0, ("%s Memory re-allocation failure.", func
) );
357 buf
->length
+= BUFR_INC
;
361 case '=': /* Equal sign marks end of param name. */
363 /* Don't allow an empty name. */
364 DEBUG(0, ("%s Invalid parameter name in config. file.\n", func
));
367 buf
->data
[end
++] = '\0'; /* Mark end of string & advance. */
368 i
= end
; /* New string starts here. */
369 vstart
= end
; /* New string is parameter value. */
370 buf
->data
[i
] = '\0'; /* New string is nul, for now. */
373 case '\n': /* Find continuation char, else error. */
374 i
= Continuation( buf
->data
, i
);
376 buf
->data
[end
] = '\0';
377 DEBUG(1,("%s Ignoring badly formed line in configuration file: %s\n", func
, buf
->data
));
380 end
= ( (i
> 0) && (' ' == buf
->data
[i
- 1]) ) ? (i
- 1) : (i
);
381 c
= mygetc( InFile
); /* Read past eoln. */
384 case '\0': /* Shouldn't have EOF within param name. */
387 DEBUG(1,("%s Unexpected end-of-file at: %s\n", func
, buf
->data
));
392 /* One ' ' per whitespace region. */
393 buf
->data
[end
] = ' ';
395 c
= EatWhitespace( InFile
);
399 c
= mygetc( InFile
);
404 /* Now parse the value. */
405 c
= EatWhitespace( InFile
); /* Again, trim leading whitespace. */
406 while( (EOF
!=c
) && (c
> 0) ) {
407 if( i
> (buf
->length
- 2) ) {
408 /* Make sure there's enough room. */
409 uint8_t *tb
= (uint8_t *)SMB_REALLOC_KEEP_OLD_ON_ERROR( buf
->data
, buf
->length
+ BUFR_INC
);
411 DEBUG(0, ("%s Memory re-allocation failure.", func
));
415 buf
->length
+= BUFR_INC
;
419 case '\r': /* Explicitly remove '\r' because the older */
420 c
= mygetc( InFile
); /* version called fgets_slash() which also */
421 break; /* removes them. */
423 case '\n': /* Marks end of value unless there's a '\'. */
424 i
= Continuation( buf
->data
, i
);
428 for( end
= i
; (end
>= 0) && isspace((int)buf
->data
[end
]); end
-- )
430 c
= mygetc( InFile
);
434 default: /* All others verbatim. Note that spaces do not advance <end>. This allows trimming */
436 if( !isspace( c
) ) /* of whitespace at the end of the line. */
438 c
= mygetc( InFile
);
442 buf
->data
[end
] = '\0'; /* End of value. */
444 return( pfunc( (char *)buf
->data
, (char *)&buf
->data
[vstart
], userdata
) ); /* Pass name & value to pfunc(). */
447 /* ------------------------------------------------------------------------ **
448 * Scan & parse the input.
450 * Input: InFile - Input source.
451 * sfunc - Function to be called when a section name is scanned.
453 * pfunc - Function to be called when a parameter is scanned.
456 * Output: True if the file was successfully scanned, else False.
458 * Notes: The input can be viewed in terms of 'lines'. There are four
460 * Blank - May contain whitespace, otherwise empty.
461 * Comment - First non-whitespace character is a ';' or '#'.
462 * The remainder of the line is ignored.
463 * Section - First non-whitespace character is a '['.
464 * Parameter - The default case.
466 * ------------------------------------------------------------------------ **
469 static bool Parse( DATA_BLOB
*buf
, myFILE
*InFile
,
470 bool (*sfunc
)(const char *, void *),
471 bool (*pfunc
)(const char *, const char *, void *),
476 c
= EatWhitespace( InFile
);
477 while( (EOF
!= c
) && (c
> 0) ) {
479 case '\n': /* Blank line. */
480 c
= EatWhitespace( InFile
);
483 case ';': /* Comment line. */
485 c
= EatComment( InFile
);
488 case '[': /* Section Header. */
489 if( !Section( buf
, InFile
, sfunc
, userdata
) )
491 c
= EatWhitespace( InFile
);
494 case '\\': /* Bogus backslash. */
495 c
= EatWhitespace( InFile
);
498 default: /* Parameter line. */
499 if( !Parameter( buf
, InFile
, pfunc
, c
, userdata
) )
501 c
= EatWhitespace( InFile
);
508 /* ------------------------------------------------------------------------ **
509 * Open a configuration file.
511 * Input: FileName - The pathname of the config file to be opened.
513 * Output: A pointer of type (char **) to the lines of the file
515 * ------------------------------------------------------------------------ **
518 static myFILE
*OpenConfFile( const char *FileName
)
520 const char *func
= "params.c:OpenConfFile() -";
521 int lvl
= lp_is_in_client() ? 1 : 0;
524 ret
= SMB_MALLOC_P(myFILE
);
528 ret
->buf
= file_load(FileName
, &ret
->size
, 0);
529 if( NULL
== ret
->buf
) {
530 DEBUG( lvl
, ("%s Unable to open configuration file \"%s\":\n\t%s\n",
531 func
, FileName
, strerror(errno
)) );
537 ret
->end_section_p
= NULL
;
541 /* ------------------------------------------------------------------------ **
542 * Process the named parameter file.
544 * Input: FileName - The pathname of the parameter file to be opened.
545 * sfunc - A pointer to a function that will be called when
546 * a section name is discovered.
547 * pfunc - A pointer to a function that will be called when
548 * a parameter name and value are discovered.
550 * Output: TRUE if the file was successfully parsed, else FALSE.
552 * ------------------------------------------------------------------------ **
555 bool pm_process( const char *FileName
,
556 bool (*sfunc
)(const char *, void *),
557 bool (*pfunc
)(const char *, const char *, void *),
562 const char *func
= "params.c:pm_process() -";
565 InFile
= OpenConfFile( FileName
); /* Open the config file. */
569 DEBUG( 3, ("%s Processing configuration file \"%s\"\n", func
, FileName
) );
571 buf
= data_blob(NULL
, 256);
573 if (buf
.data
== NULL
) {
574 DEBUG(0,("%s memory allocation failure.\n", func
));
575 myfile_close(InFile
);
579 result
= Parse( &buf
, InFile
, sfunc
, pfunc
, userdata
);
580 data_blob_free(&buf
);
582 myfile_close(InFile
);
585 DEBUG(0,("%s Failed. Error returned from params.c:parse().\n", func
));