lib: Give base64.c its own .h
[sfrench/samba-autobuild/.git] / lib / util / params.c
1 /* -------------------------------------------------------------------------- **
2  * Microsoft Network Services for Unix, AKA., Andrew Tridgell's SAMBA.
3  *
4  * This module Copyright (C) 1990-1998 Karl Auer
5  *
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  * -------------------------------------------------------------------------- **
10  *
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.
15  *
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.
20  *
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/>.
23  *
24  * -------------------------------------------------------------------------- **
25  *
26  * Module name: params
27  *
28  * -------------------------------------------------------------------------- **
29  *
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.
35  *
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.
40  *
41  *  A sample parameter file might look like this:
42  *
43  *  [section one]
44  *  parameter one = value string
45  *  parameter two = another value
46  *  [section two]
47  *  new parameter = some value or t'other
48  *
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:
54  *
55  *    <file>            :==  { <section> } EOF
56  *
57  *    <section>         :==  <section header> { <parameter line> }
58  *
59  *    <section header>  :==  '[' NAME ']'
60  *
61  *    <parameter line>  :==  NAME '=' VALUE '\n'
62  *
63  *  Blank lines and comment lines are ignored.  Comment lines are lines
64  *  beginning with either a semicolon (';') or a pound sign ('#').
65  *
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.
69  *
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.
77  *
78  * -------------------------------------------------------------------------- **
79  */
80
81 #include "replace.h"
82 #include "lib/util/samba_util.h"
83 #include "tini.h"
84
85 bool pm_process(const char *filename,
86                 bool (*sfunc)(const char *section, void *private_data),
87                 bool (*pfunc)(const char *name, const char *value,
88                               void *private_data),
89                 void *private_data)
90 {
91         FILE *f;
92         bool ret;
93
94         f = fopen(filename, "r");
95         if (f == NULL) {
96                 return false;
97         }
98
99         ret = tini_parse(f, sfunc, pfunc, private_data);
100
101         fclose(f);
102
103         return ret;
104 }