]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/xml.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2013 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "string-util.h"
36 static void inc_lines(unsigned *line
, const char *s
, size_t n
) {
45 f
= memchr(p
, '\n', n
);
55 /* We don't actually do real XML here. We only read a simplistic
56 * subset, that is a bit less strict that XML and lacks all the more
57 * complex features, like entities, or namespaces. However, we do
58 * support some HTML5-like simplifications */
60 int xml_tokenize(const char **p
, char **name
, void **state
, unsigned *line
) {
61 const char *c
, *e
, *b
;
70 t
= PTR_TO_INT(*state
);
73 if (t
== STATE_NULL
) {
88 e
= strchrnul(c
, '<');
91 ret
= strndup(c
, e
- c
);
95 inc_lines(line
, c
, e
- c
);
99 *state
= INT_TO_PTR(STATE_TEXT
);
107 if (startswith(b
, "!--")) {
109 e
= strstr(b
+ 3, "-->");
113 inc_lines(line
, b
, e
+ 3 - b
);
120 /* Processing instruction */
122 e
= strstr(b
+ 1, "?>");
126 inc_lines(line
, b
, e
+ 2 - b
);
135 e
= strchr(b
+ 1, '>');
139 inc_lines(line
, b
, e
+ 1 - b
);
152 e
= strpbrk(b
, WHITESPACE
"/>");
156 ret
= strndup(b
, e
- b
);
162 *state
= INT_TO_PTR(STATE_TAG
);
169 b
= c
+ strspn(c
, WHITESPACE
);
173 inc_lines(line
, c
, b
- c
);
175 e
= b
+ strcspn(b
, WHITESPACE
"=/>");
179 ret
= strndup(b
, e
- b
);
185 *state
= INT_TO_PTR(STATE_ATTRIBUTE
);
187 return XML_ATTRIBUTE_NAME
;
190 if (startswith(b
, "/>")) {
193 *name
= NULL
; /* For empty tags we return a NULL name, the caller must be prepared for that */
195 *state
= INT_TO_PTR(STATE_TEXT
);
197 return XML_TAG_CLOSE_EMPTY
;
207 case STATE_ATTRIBUTE
:
212 if (IN_SET(*c
, '\'', '\"')) {
213 /* Tag with a quoted value */
219 inc_lines(line
, c
, e
- c
);
221 ret
= strndup(c
+1, e
- c
- 1);
227 *state
= INT_TO_PTR(STATE_TAG
);
229 return XML_ATTRIBUTE_VALUE
;
233 /* Tag with a value without quotes */
235 b
= strpbrk(c
, WHITESPACE
">");
239 ret
= strndup(c
, b
- c
);
245 *state
= INT_TO_PTR(STATE_TAG
);
246 return XML_ATTRIBUTE_VALUE
;
255 assert_not_reached("Bad state");