~vorlon/ubuntu/raring/upstart/lp.1199778

« back to all changes in this revision

Viewing changes to init/parse_conf.c

  • Committer: Scott James Remnant
  • Date: 2007-06-11 16:17:22 UTC
  • Revision ID: scott@netsplit.com-20070611161722-b946aofy2lkkd8dv
* init/parse_conf.c (parse_conf): Parse a configuration file that
defines jobs by name.
(stanza_job): Job stanza, slightly trickier than it would appear to
need to be, to parse the block in-place and keep pos/lineno
consistent.
* init/parse_conf.h: Prototype for external function.
* init/tests/test_parse_conf.c: Test suite for mixed configuration
parsing.
* init/Makefile.am (init_SOURCES): Build and link parse_conf.c and
parse_conf.h
(TESTS): Build and run parse_conf tests
(test_parse_conf_SOURCES, test_parse_conf_LDFLAGS) 
(test_parse_conf_LDADD): Details for parse_conf test suite.
(test_conf_LDADD): Add parse_conf.o and conf.o since this calls
them now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* upstart
 
2
 *
 
3
 * parse_conf.c - general configuration parsing
 
4
 *
 
5
 * Copyright © 2007 Canonical Ltd.
 
6
 * Author: Scott James Remnant <scott@ubuntu.com>.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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
 
21
 */
 
22
 
 
23
#ifdef HAVE_CONFIG_H
 
24
# include <config.h>
 
25
#endif /* HAVE_CONFIG_H */
 
26
 
 
27
 
 
28
#include <nih/macros.h>
 
29
#include <nih/alloc.h>
 
30
#include <nih/list.h>
 
31
#include <nih/config.h>
 
32
#include <nih/logging.h>
 
33
#include <nih/error.h>
 
34
 
 
35
#include "conf.h"
 
36
#include "parse_conf.h"
 
37
#include "parse_job.h"
 
38
 
 
39
 
 
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));
 
45
 
 
46
 
 
47
/**
 
48
 * stanzas:
 
49
 *
 
50
 * This is the table of known configuration stanzas and the functions
 
51
 * that handle parsing them.
 
52
 **/
 
53
static NihConfigStanza stanzas[] = {
 
54
        { "job", (NihConfigHandler)stanza_job },
 
55
 
 
56
        NIH_CONFIG_LAST
 
57
};
 
58
 
 
59
 
 
60
/**
 
61
 * parse_conf:
 
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.
 
67
 *
 
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.
 
71
 *
 
72
 * The necessary configuration item is allocated and attached to the file
 
73
 * automatically.
 
74
 *
 
75
 * Returns: zero on success, negative value on raised error.
 
76
 **/
 
77
int
 
78
parse_conf (ConfFile   *conffile,
 
79
            const char *file,
 
80
            size_t      len,
 
81
            size_t     *pos,
 
82
            size_t     *lineno)
 
83
{
 
84
        nih_assert (conffile != NULL);
 
85
        nih_assert (file != NULL);
 
86
        nih_assert (pos != NULL);
 
87
 
 
88
        if (nih_config_parse_file (file, len, pos, lineno,
 
89
                                   stanzas, conffile) < 0)
 
90
                return -1;
 
91
 
 
92
        return 0;
 
93
}
 
94
 
 
95
 
 
96
/**
 
97
 * stanza_job:
 
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.
 
104
 *
 
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.
 
108
 *
 
109
 * Returns: zero on success, negative value on error.
 
110
 **/
 
111
static int
 
112
stanza_job (ConfFile        *conffile,
 
113
            NihConfigStanza *stanza,
 
114
            const char      *file,
 
115
            size_t           len,
 
116
            size_t          *pos,
 
117
            size_t          *lineno)
 
118
{
 
119
        ConfItem *item;
 
120
        char     *name;
 
121
        size_t    b_pos, b_lineno, b_end;
 
122
        int       ret = 0;
 
123
 
 
124
        nih_assert (conffile != NULL);
 
125
        nih_assert (stanza != NULL);
 
126
        nih_assert (file != NULL);
 
127
        nih_assert (pos != NULL);
 
128
 
 
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);
 
132
        if (! name)
 
133
                return -1;
 
134
 
 
135
        /* Skip over any following comment to the beginning of the block */
 
136
        if (nih_config_skip_comment (file, len, pos, lineno) < 0) {
 
137
                nih_free (name);
 
138
                return -1;
 
139
        }
 
140
 
 
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).
 
145
         *
 
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.
 
149
         */
 
150
        b_pos = *pos;
 
151
        b_lineno = (lineno ? *lineno : 1);
 
152
        b_end = 0;
 
153
 
 
154
        if (nih_config_skip_block (file, len, &b_pos, &b_lineno,
 
155
                                   "job", &b_end) < 0) {
 
156
                ret = -1;
 
157
                goto finish;
 
158
        }
 
159
 
 
160
        /* Now parse the item from the content of the block only.
 
161
         *
 
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.
 
166
         */
 
167
        nih_debug ("Loading %s from %s", name, conffile->path);
 
168
        item = conf_item_new (conffile, CONF_JOB);
 
169
        if (! item) {
 
170
                nih_error_raise_system ();
 
171
                ret = -1;
 
172
                goto finish;
 
173
        }
 
174
 
 
175
        item->job = parse_job (NULL, name, file, b_end, pos, lineno);
 
176
        if (! item->job) {
 
177
                nih_list_free (&item->entry);
 
178
                nih_free (name);
 
179
                return -1;
 
180
        }
 
181
 
 
182
finish:
 
183
        nih_free (name);
 
184
 
 
185
        *pos = b_pos;
 
186
        if (lineno)
 
187
                *lineno = b_lineno;
 
188
 
 
189
        return ret;
 
190
}