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/>.
33 /* We don't actually do real XML here. We only read a simplistic
34 * subset, that is a bit less strict that XML and lacks all the more
35 * complex features, like entities, or namespaces. However, we do
36 * support some HTML5-like simplifications */
38 int xml_tokenize(const char **p, char **name, void **state) {
39 const char *c, *e, *b;
48 t = PTR_TO_INT(*state);
60 e = strchrnul(c, '<');
63 ret = strndup(c, e - c);
69 *state = INT_TO_PTR(STATE_TEXT);
77 if (startswith(b, "!--")) {
79 e = strstr(b + 3, "-->");
88 /* Processing instruction */
90 e = strstr(b + 1, "?>");
101 e = strchr(b + 1, '>');
116 e = strpbrk(b, WHITESPACE "/>");
120 ret = strndup(b, e - b);
126 *state = INT_TO_PTR(STATE_TAG);
133 b = c + strspn(c, WHITESPACE);
137 e = b + strcspn(b, WHITESPACE "=/>");
141 ret = strndup(b, e - b);
147 *state = INT_TO_PTR(STATE_ATTRIBUTE);
149 return XML_ATTRIBUTE_NAME;
152 if (startswith(b, "/>")) {
155 *name = NULL; /* For empty tags we return a NULL name, the caller must be prepared for that */
157 *state = INT_TO_PTR(STATE_TEXT);
159 return XML_TAG_CLOSE_EMPTY;
169 case STATE_ATTRIBUTE:
174 if (*c == '\'' || *c == '\"') {
175 /* Tag with a quoted value */
181 ret = strndup(c+1, e - c - 1);
187 *state = INT_TO_PTR(STATE_TAG);
189 return XML_ATTRIBUTE_VALUE;
193 /* Tag with a value without quotes */
195 b = strpbrk(c, WHITESPACE ">");
199 ret = strndup(c, b - c);
205 *state = INT_TO_PTR(STATE_TAG);
206 return XML_ATTRIBUTE_VALUE;
215 assert_not_reached("Bad state");