1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2013 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
34 static void inc_lines(unsigned *line, const char *s, size_t n) {
43 f = memchr(p, '\n', n);
53 /* We don't actually do real XML here. We only read a simplistic
54 * subset, that is a bit less strict that XML and lacks all the more
55 * complex features, like entities, or namespaces. However, we do
56 * support some HTML5-like simplifications */
58 int xml_tokenize(const char **p, char **name, void **state, unsigned *line) {
59 const char *c, *e, *b;
68 t = PTR_TO_INT(*state);
71 if (t == STATE_NULL) {
86 e = strchrnul(c, '<');
89 ret = strndup(c, e - c);
93 inc_lines(line, c, e - c);
97 *state = INT_TO_PTR(STATE_TEXT);
105 if (startswith(b, "!--")) {
107 e = strstr(b + 3, "-->");
111 inc_lines(line, b, e + 3 - b);
118 /* Processing instruction */
120 e = strstr(b + 1, "?>");
124 inc_lines(line, b, e + 2 - b);
133 e = strchr(b + 1, '>');
137 inc_lines(line, b, e + 1 - b);
150 e = strpbrk(b, WHITESPACE "/>");
154 ret = strndup(b, e - b);
160 *state = INT_TO_PTR(STATE_TAG);
167 b = c + strspn(c, WHITESPACE);
171 inc_lines(line, c, b - c);
173 e = b + strcspn(b, WHITESPACE "=/>");
177 ret = strndup(b, e - b);
183 *state = INT_TO_PTR(STATE_ATTRIBUTE);
185 return XML_ATTRIBUTE_NAME;
188 if (startswith(b, "/>")) {
191 *name = NULL; /* For empty tags we return a NULL name, the caller must be prepared for that */
193 *state = INT_TO_PTR(STATE_TEXT);
195 return XML_TAG_CLOSE_EMPTY;
205 case STATE_ATTRIBUTE:
210 if (*c == '\'' || *c == '\"') {
211 /* Tag with a quoted value */
217 inc_lines(line, c, e - c);
219 ret = strndup(c+1, e - c - 1);
225 *state = INT_TO_PTR(STATE_TAG);
227 return XML_ATTRIBUTE_VALUE;
231 /* Tag with a value without quotes */
233 b = strpbrk(c, WHITESPACE ">");
237 ret = strndup(c, b - c);
243 *state = INT_TO_PTR(STATE_TAG);
244 return XML_ATTRIBUTE_VALUE;
253 assert_not_reached("Bad state");