3
* parse_conf.c - general configuration parsing
5
* Copyright © 2007 Canonical Ltd.
6
* Author: Scott James Remnant <scott@ubuntu.com>.
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
#endif /* HAVE_CONFIG_H */
28
#include <nih/macros.h>
29
#include <nih/alloc.h>
31
#include <nih/config.h>
32
#include <nih/logging.h>
33
#include <nih/error.h>
36
#include "parse_conf.h"
37
#include "parse_job.h"
40
/* Prototypes for static functions */
41
static int stanza_job (ConfFile *conffile, NihConfigStanza *stanza,
42
const char *file, size_t len,
43
size_t *pos, size_t *lineno)
44
__attribute__ ((warn_unused_result));
50
* This is the table of known configuration stanzas and the functions
51
* that handle parsing them.
53
static NihConfigStanza stanzas[] = {
54
{ "job", (NihConfigHandler)stanza_job },
62
* @conffile: configuration file being parsed,
63
* @file: file or string to parse,
64
* @len: length of @file,
65
* @pos: offset within @file,
66
* @lineno: line number.
68
* This function is used to parse a job definition from @file, taking the
69
* name from the stanza itself. A block is expected containing a sequence
70
* of stanzas is expected, defining the parameters of the job.
72
* The necessary configuration item is allocated and attached to the file
75
* Returns: zero on success, negative value on raised error.
78
parse_conf (ConfFile *conffile,
84
nih_assert (conffile != NULL);
85
nih_assert (file != NULL);
86
nih_assert (pos != NULL);
88
if (nih_config_parse_file (file, len, pos, lineno,
89
stanzas, conffile) < 0)
98
* @conffile: configuration file being parsed,
99
* @stanza: stanza found,
100
* @file: file or string to parse,
101
* @len: length of @file,
102
* @pos: offset within @file,
103
* @lineno: line number.
105
* This function is used to parse the job stanza from @file. A block
106
* terminated with "end job" is expected to follow, containing a sequence
107
* of job definition stanzas.
109
* Returns: zero on success, negative value on error.
112
stanza_job (ConfFile *conffile,
113
NihConfigStanza *stanza,
121
size_t b_pos, b_lineno, b_end;
124
nih_assert (conffile != NULL);
125
nih_assert (stanza != NULL);
126
nih_assert (file != NULL);
127
nih_assert (pos != NULL);
129
/* Expect a single argument containing the name of the new job */
130
name = nih_config_next_token (NULL, file, len, pos, lineno,
131
NIH_CONFIG_CNLWS, FALSE);
135
/* Skip over any following comment to the beginning of the block */
136
if (nih_config_skip_comment (file, len, pos, lineno) < 0) {
141
/* Skip over the block, calculating the length as we go. We store
142
* the end position and line number in separate variables and set
143
* them back over the top after we've successfully parsed the job
144
* (or if we fail to skip the block).
146
* This allows errors raised in the job parsing to have the
147
* appropriate pos and lineno for those errors rather than always
148
* being at the end of the block.
151
b_lineno = (lineno ? *lineno : 1);
154
if (nih_config_skip_block (file, len, &b_pos, &b_lineno,
155
"job", &b_end) < 0) {
160
/* Now parse the item from the content of the block only.
162
* We use the end position of the block as the length (since it's
163
* relative to it), thus we can use the existing pos and lineno
164
* pointers. If this fails, we leave them where they are and don't
165
* copy those of the block end over the top.
167
nih_debug ("Loading %s from %s", name, conffile->path);
168
item = conf_item_new (conffile, CONF_JOB);
170
nih_error_raise_system ();
175
item->job = parse_job (NULL, name, file, b_end, pos, lineno);
177
nih_list_free (&item->entry);