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 file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * -------------------------------------------------------------------------- **
33 * -------------------------------------------------------------------------- **
35 * This module performs lexical analysis and initial parsing of a
36 * Windows-like parameter file. It recognizes and handles four token
37 * types: section-name, parameter-name, parameter-value, and
38 * end-of-file. Comments and line continuation are handled
41 * The entry point to the module is function pm_process(). This
42 * function opens the source file, calls the Parse() function to parse
43 * the input, and then closes the file when either the EOF is reached
44 * or a fatal error is encountered.
46 * A sample parameter file might look like this:
49 * parameter one = value string
50 * parameter two = another value
52 * new parameter = some value or t'other
54 * The parameter file is divided into sections by section headers:
55 * section names enclosed in square brackets (eg. [section one]).
56 * Each section contains parameter lines, each of which consist of a
57 * parameter name and value delimited by an equal sign. Roughly, the
60 * <file> :== { <section> } EOF
62 * <section> :== <section header> { <parameter line> }
64 * <section header> :== '[' NAME ']'
66 * <parameter line> :== NAME '=' VALUE '\n'
68 * Blank lines and comment lines are ignored. Comment lines are lines
69 * beginning with either a semicolon (';') or a pound sign ('#').
71 * All whitespace in section names and parameter names is compressed
72 * to single spaces. Leading and trailing whitespace is stipped from
73 * both names and values.
75 * Only the first equals sign in a parameter line is significant.
76 * Parameter values may contain equals signs, square brackets and
77 * semicolons. Internal whitespace is retained in parameter values,
78 * with the exception of the '\r' character, which is stripped for
79 * historic reasons. Parameter names may not start with a left square
80 * bracket, an equal sign, a pound sign, or a semicolon, because these
81 * are used to identify other tokens.
83 * -------------------------------------------------------------------------- **
87 const char *unix_error_string (int error_num
);
89 /* -------------------------------------------------------------------------- **
96 /* -------------------------------------------------------------------------- **
99 * DEBUGLEVEL - The ubiquitous DEBUGLEVEL. This determines which DEBUG()
100 * messages will be produced.
101 * bufr - pointer to a global buffer. This is probably a kludge,
102 * but it was the nicest kludge I could think of (for now).
103 * bSize - The size of the global buffer <bufr>.
106 extern int DEBUGLEVEL
;
108 static char *bufr
= NULL
;
109 static int bSize
= 0;
111 /* -------------------------------------------------------------------------- **
116 EatWhitespace (FILE * InFile
)
117 /* ------------------------------------------------------------------------ **
118 * Scan past whitespace (see ctype(3C)) and return the first non-whitespace
119 * character, or newline, or EOF.
121 * Input: InFile - Input source.
123 * Output: The next non-whitespace character in the input stream.
125 * Notes: Because the config files use a line-oriented grammar, we
126 * explicitly exclude the newline character from the list of
127 * whitespace characters.
128 * - Note that both EOF (-1) and the nul character ('\0') are
129 * considered end-of-file markers.
131 * ------------------------------------------------------------------------ **
136 for (c
= getc (InFile
); isspace (c
) && ('\n' != c
); c
= getc (InFile
))
139 } /* EatWhitespace */
142 EatComment (FILE * InFile
)
143 /* ------------------------------------------------------------------------ **
144 * Scan to the end of a comment.
146 * Input: InFile - Input source.
148 * Output: The character that marks the end of the comment. Normally,
149 * this will be a newline, but it *might* be an EOF.
151 * Notes: Because the config files use a line-oriented grammar, we
152 * explicitly exclude the newline character from the list of
153 * whitespace characters.
154 * - Note that both EOF (-1) and the nul character ('\0') are
155 * considered end-of-file markers.
157 * ------------------------------------------------------------------------ **
162 for (c
= getc (InFile
); ('\n' != c
) && (EOF
!= c
) && (c
> 0); c
= getc (InFile
))
168 Continuation (char *line
, int pos
)
169 /* ------------------------------------------------------------------------ **
170 * Scan backards within a string to discover if the last non-whitespace
171 * character is a line-continuation character ('\\').
173 * Input: line - A pointer to a buffer containing the string to be
175 * pos - This is taken to be the offset of the end of the
176 * string. This position is *not* scanned.
178 * Output: The offset of the '\\' character if it was found, or -1 to
179 * indicate that it was not.
181 * ------------------------------------------------------------------------ **
185 while ((pos
>= 0) && isspace (line
[pos
]))
188 return (((pos
>= 0) && ('\\' == line
[pos
])) ? pos
: -1);
193 Section (FILE * InFile
, BOOL (*sfunc
) (const char *))
194 /* ------------------------------------------------------------------------ **
195 * Scan a section name, and pass the name to function sfunc().
197 * Input: InFile - Input source.
198 * sfunc - Pointer to the function to be called if the section
199 * name is successfully read.
201 * Output: True if the section name was read and True was returned from
202 * <sfunc>. False if <sfunc> failed or if a lexical error was
205 * ------------------------------------------------------------------------ **
211 const char *func
= "params.c:Section() -";
213 i
= 0; /* <i> is the offset of the next free byte in bufr[] and */
214 end
= 0; /* <end> is the current "end of string" offset. In most */
215 /* cases these will be the same, but if the last */
216 /* character written to bufr[] is a space, then <end> */
217 /* will be one less than <i>. */
219 c
= EatWhitespace (InFile
); /* We've already got the '['. Scan */
220 /* past initial white space. */
222 while ((EOF
!= c
) && (c
> 0))
225 /* Check that the buffer is big enough for the next character. */
229 bufr
= Realloc (bufr
, bSize
);
232 DEBUG (0, ("%s Memory re-allocation failure.", func
));
237 /* Handle a single character. */
240 case ']': /* Found the closing bracket. */
242 if (0 == end
) /* Don't allow an empty name. */
244 DEBUG (0, ("%s Empty section name in configuration file.\n", func
));
247 if (!sfunc (bufr
)) /* Got a valid name. Deal with it. */
249 (void) EatComment (InFile
); /* Finish off the line. */
252 case '\n': /* Got newline before closing ']'. */
253 i
= Continuation (bufr
, i
); /* Check for line continuation. */
257 DEBUG (0, ("%s Badly formed line in configuration file: %s\n", func
, bufr
));
260 end
= ((i
> 0) && (' ' == bufr
[i
- 1])) ? (i
- 1) : (i
);
261 c
= getc (InFile
); /* Continue with next line. */
264 default: /* All else are a valid name chars. */
265 if (isspace (c
)) /* One space per whitespace region. */
269 c
= EatWhitespace (InFile
);
271 else /* All others copy verbatim. */
280 /* We arrive here if we've met the EOF before the closing bracket. */
281 DEBUG (0, ("%s Unexpected EOF in the configuration file: %s\n", func
, bufr
));
286 Parameter (FILE * InFile
, BOOL (*pfunc
) (const char *, const char *), int c
)
287 /* ------------------------------------------------------------------------ **
288 * Scan a parameter name and value, and pass these two fields to pfunc().
290 * Input: InFile - The input source.
291 * pfunc - A pointer to the function that will be called to
292 * process the parameter, once it has been scanned.
293 * c - The first character of the parameter name, which
294 * would have been read by Parse(). Unlike a comment
295 * line or a section header, there is no lead-in
296 * character that can be discarded.
298 * Output: True if the parameter name and value were scanned and processed
299 * successfully, else False.
301 * Notes: This function is in two parts. The first loop scans the
302 * parameter name. Internal whitespace is compressed, and an
303 * equal sign (=) terminates the token. Leading and trailing
304 * whitespace is discarded. The second loop scans the parameter
305 * value. When both have been successfully identified, they are
306 * passed to pfunc() for processing.
308 * ------------------------------------------------------------------------ **
311 int i
= 0; /* Position within bufr. */
312 int end
= 0; /* bufr[end] is current end-of-string. */
313 int vstart
= 0; /* Starting position of the parameter value. */
314 const char *func
= "params.c:Parameter() -";
316 /* Read the parameter name. */
317 while (0 == vstart
) /* Loop until we've found the start of the value. */
320 if (i
> (bSize
- 2)) /* Ensure there's space for next char. */
323 bufr
= Realloc (bufr
, bSize
);
326 DEBUG (0, ("%s Memory re-allocation failure.", func
));
333 case '=': /* Equal sign marks end of param name. */
334 if (0 == end
) /* Don't allow an empty name. */
336 DEBUG (0, ("%s Invalid parameter name in config. file.\n", func
));
339 bufr
[end
++] = '\0'; /* Mark end of string & advance. */
340 i
= end
; /* New string starts here. */
341 vstart
= end
; /* New string is parameter value. */
342 bufr
[i
] = '\0'; /* New string is nul, for now. */
345 case '\n': /* Find continuation char, else error. */
346 i
= Continuation (bufr
, i
);
350 DEBUG (1, ("%s Ignoring badly formed line in configuration file: %s\n",
354 end
= ((i
> 0) && (' ' == bufr
[i
- 1])) ? (i
- 1) : (i
);
355 c
= getc (InFile
); /* Read past eoln. */
358 case '\0': /* Shouldn't have EOF within param name. */
361 DEBUG (1, ("%s Unexpected end-of-file at: %s\n", func
, bufr
));
365 if (isspace (c
)) /* One ' ' per whitespace region. */
369 c
= EatWhitespace (InFile
);
371 else /* All others verbatim. */
380 /* Now parse the value. */
381 c
= EatWhitespace (InFile
); /* Again, trim leading whitespace. */
382 while ((EOF
!= c
) && (c
> 0))
385 if (i
> (bSize
- 2)) /* Make sure there's enough room. */
388 bufr
= Realloc (bufr
, bSize
);
391 DEBUG (0, ("%s Memory re-allocation failure.", func
));
398 case '\r': /* Explicitly remove '\r' because the older */
399 c
= getc (InFile
); /* version called fgets_slash() which also */
400 break; /* removes them. */
402 case '\n': /* Marks end of value unless there's a '\'. */
403 i
= Continuation (bufr
, i
);
408 for (end
= i
; (end
>= 0) && isspace (bufr
[end
]); end
--)
414 default: /* All others verbatim. Note that spaces do */
415 bufr
[i
++] = c
; /* not advance <end>. This allows trimming */
416 if (!isspace (c
)) /* of whitespace at the end of the line. */
422 bufr
[end
] = '\0'; /* End of value. */
424 return (pfunc (bufr
, &bufr
[vstart
])); /* Pass name & value to pfunc(). */
428 Parse (FILE * InFile
, BOOL (*sfunc
) (const char *), BOOL (*pfunc
) (const char *, const char *))
429 /* ------------------------------------------------------------------------ **
430 * Scan & parse the input.
432 * Input: InFile - Input source.
433 * sfunc - Function to be called when a section name is scanned.
435 * pfunc - Function to be called when a parameter is scanned.
438 * Output: True if the file was successfully scanned, else False.
440 * Notes: The input can be viewed in terms of 'lines'. There are four
442 * Blank - May contain whitespace, otherwise empty.
443 * Comment - First non-whitespace character is a ';' or '#'.
444 * The remainder of the line is ignored.
445 * Section - First non-whitespace character is a '['.
446 * Parameter - The default case.
448 * ------------------------------------------------------------------------ **
453 c
= EatWhitespace (InFile
);
454 while ((EOF
!= c
) && (c
> 0))
458 case '\n': /* Blank line. */
459 c
= EatWhitespace (InFile
);
462 case ';': /* Comment line. */
464 c
= EatComment (InFile
);
467 case '[': /* Section Header. */
468 if (!Section (InFile
, sfunc
))
470 c
= EatWhitespace (InFile
);
473 case '\\': /* Bogus backslash. */
474 c
= EatWhitespace (InFile
);
477 default: /* Parameter line. */
478 if (!Parameter (InFile
, pfunc
, c
))
480 c
= EatWhitespace (InFile
);
488 OpenConfFile (const char *FileName
)
489 /* ------------------------------------------------------------------------ **
490 * Open a configuration file.
492 * Input: FileName - The pathname of the config file to be opened.
494 * Output: A pointer of type (FILE *) to the opened file, or NULL if the
495 * file could not be opened.
497 * ------------------------------------------------------------------------ **
501 const char *func
= "params.c:OpenConfFile() -";
502 extern BOOL in_client
;
503 int lvl
= in_client
? 1 : 0;
505 if (NULL
== FileName
|| 0 == *FileName
)
507 DEBUG (lvl
, ("%s No configuration filename specified.\n", func
));
511 OpenedFile
= sys_fopen (FileName
, "r");
512 if (NULL
== OpenedFile
)
515 ("%s Unable to open configuration file \"%s\":\n\t%s\n",
516 func
, FileName
, unix_error_string (errno
)));
523 pm_process (const char *FileName
,
524 BOOL (*sfunc
) (const char *), BOOL (*pfunc
) (const char *, const char *))
525 /* ------------------------------------------------------------------------ **
526 * Process the named parameter file.
528 * Input: FileName - The pathname of the parameter file to be opened.
529 * sfunc - A pointer to a function that will be called when
530 * a section name is discovered.
531 * pfunc - A pointer to a function that will be called when
532 * a parameter name and value are discovered.
534 * Output: TRUE if the file was successfully parsed, else FALSE.
536 * ------------------------------------------------------------------------ **
541 const char *func
= "params.c:pm_process() -";
543 InFile
= OpenConfFile (FileName
); /* Open the config file. */
547 DEBUG (3, ("%s Processing configuration file \"%s\"\n", func
, FileName
));
549 if (NULL
!= bufr
) /* If we already have a buffer */
550 result
= Parse (InFile
, sfunc
, pfunc
); /* (recursive call), then just */
553 else /* If we don't have a buffer */
554 { /* allocate one, then parse, */
555 bSize
= BUFR_INC
; /* then free. */
556 bufr
= (char *) malloc (bSize
);
559 DEBUG (0, ("%s memory allocation failure.\n", func
));
563 result
= Parse (InFile
, sfunc
, pfunc
);
571 if (!result
) /* Generic failure. */
573 DEBUG (0, ("%s Failed. Error returned from params.c:parse().\n", func
));
577 return (True
); /* Generic success. */
580 /* -------------------------------------------------------------------------- */