Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file xml.c |
| 3 | * @author Radek Krejci <rkrejci@cesnet.cz> |
| 4 | * @brief XML parser implementation for libyang |
| 5 | * |
| 6 | * Copyright (c) 2015 CESNET, z.s.p.o. |
| 7 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
Michal Vasko | 8de098c | 2016-02-26 10:00:25 +0100 | [diff] [blame] | 11 | * |
Radek Krejci | 54f6fb3 | 2016-02-24 12:56:39 +0100 | [diff] [blame] | 12 | * https://opensource.org/licenses/BSD-3-Clause |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 13 | */ |
| 14 | |
Radek Krejci | 812b10a | 2015-05-28 16:48:25 +0200 | [diff] [blame] | 15 | #include <assert.h> |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 16 | #include <errno.h> |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 17 | #include <ctype.h> |
| 18 | #include <stdint.h> |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 19 | #include <stdio.h> |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 22 | #include <unistd.h> |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 23 | #include <pthread.h> |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 24 | #include <sys/stat.h> |
| 25 | #include <sys/mman.h> |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 26 | #include <sys/syscall.h> |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 27 | #include <fcntl.h> |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 28 | |
Radek Krejci | 06a704e | 2015-04-22 14:50:49 +0200 | [diff] [blame] | 29 | #include "common.h" |
Michal Vasko | 6c81070 | 2018-03-14 16:23:21 +0100 | [diff] [blame] | 30 | #include "hash_table.h" |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 31 | #include "printer.h" |
Radek Krejci | 5449d47 | 2015-10-26 14:35:56 +0100 | [diff] [blame] | 32 | #include "parser.h" |
Michal Vasko | 2d162e1 | 2015-09-24 14:33:29 +0200 | [diff] [blame] | 33 | #include "tree_schema.h" |
Michal Vasko | fc5744d | 2015-10-22 12:09:34 +0200 | [diff] [blame] | 34 | #include "xml_internal.h" |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 35 | |
Radek Krejci | 3045cf3 | 2015-05-28 10:58:52 +0200 | [diff] [blame] | 36 | #define ign_xmlws(p) \ |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 37 | while (is_xmlws(*p)) { \ |
Radek Krejci | 563427e | 2016-02-08 16:26:34 +0100 | [diff] [blame] | 38 | p++; \ |
| 39 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 40 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 41 | static struct lyxml_attr *lyxml_dup_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr); |
| 42 | |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 43 | API const struct lyxml_ns * |
| 44 | lyxml_get_ns(const struct lyxml_elem *elem, const char *prefix) |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 45 | { |
| 46 | struct lyxml_attr *attr; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 47 | |
| 48 | if (!elem) { |
| 49 | return NULL; |
| 50 | } |
| 51 | |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 52 | for (attr = elem->attr; attr; attr = attr->next) { |
| 53 | if (attr->type != LYXML_ATTR_NS) { |
| 54 | continue; |
| 55 | } |
| 56 | if (!attr->name) { |
Radek Krejci | 13f3f15 | 2016-10-03 11:40:13 +0200 | [diff] [blame] | 57 | if (!prefix) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 58 | /* default namespace found */ |
| 59 | if (!attr->value) { |
| 60 | /* empty default namespace -> no default namespace */ |
| 61 | return NULL; |
| 62 | } |
| 63 | return (struct lyxml_ns *)attr; |
| 64 | } |
Radek Krejci | 7d39dae | 2016-10-03 17:33:01 +0200 | [diff] [blame] | 65 | } else if (prefix && !strcmp(attr->name, prefix)) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 66 | /* prefix found */ |
| 67 | return (struct lyxml_ns *)attr; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* go recursively */ |
| 72 | return lyxml_get_ns(elem->parent, prefix); |
| 73 | } |
| 74 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 75 | static void |
| 76 | lyxml_correct_attr_ns(struct ly_ctx *ctx, struct lyxml_attr *attr, struct lyxml_elem *attr_parent, int copy_ns) |
| 77 | { |
| 78 | const struct lyxml_ns *tmp_ns; |
Michal Vasko | f610911 | 2015-12-03 14:00:42 +0100 | [diff] [blame] | 79 | struct lyxml_elem *ns_root, *attr_root; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 80 | |
| 81 | if ((attr->type != LYXML_ATTR_NS) && attr->ns) { |
Michal Vasko | f610911 | 2015-12-03 14:00:42 +0100 | [diff] [blame] | 82 | /* find the root of attr */ |
| 83 | for (attr_root = attr_parent; attr_root->parent; attr_root = attr_root->parent); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 84 | |
| 85 | /* find the root of attr NS */ |
| 86 | for (ns_root = attr->ns->parent; ns_root->parent; ns_root = ns_root->parent); |
| 87 | |
Michal Vasko | f610911 | 2015-12-03 14:00:42 +0100 | [diff] [blame] | 88 | /* attr NS is defined outside attr parent subtree */ |
| 89 | if (ns_root != attr_root) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 90 | if (copy_ns) { |
| 91 | tmp_ns = attr->ns; |
| 92 | /* we may have already copied the NS over? */ |
Radek Krejci | 66aca40 | 2016-05-24 15:23:02 +0200 | [diff] [blame] | 93 | attr->ns = lyxml_get_ns(attr_parent, tmp_ns->prefix); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 94 | |
| 95 | /* we haven't copied it over, copy it now */ |
| 96 | if (!attr->ns) { |
Michal Vasko | f610911 | 2015-12-03 14:00:42 +0100 | [diff] [blame] | 97 | attr->ns = (struct lyxml_ns *)lyxml_dup_attr(ctx, attr_parent, (struct lyxml_attr *)tmp_ns); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 98 | } |
| 99 | } else { |
| 100 | attr->ns = NULL; |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static struct lyxml_attr * |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 107 | lyxml_dup_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr) |
| 108 | { |
| 109 | struct lyxml_attr *result, *a; |
| 110 | |
| 111 | if (!attr || !parent) { |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | if (attr->type == LYXML_ATTR_NS) { |
| 116 | /* this is correct, despite that all attributes seems like a standard |
| 117 | * attributes (struct lyxml_attr), some of them can be namespace |
| 118 | * definitions (and in that case they are struct lyxml_ns). |
| 119 | */ |
| 120 | result = (struct lyxml_attr *)calloc(1, sizeof (struct lyxml_ns)); |
| 121 | } else { |
| 122 | result = calloc(1, sizeof (struct lyxml_attr)); |
| 123 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 124 | LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 125 | |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 126 | result->value = lydict_insert(ctx, attr->value, 0); |
| 127 | result->name = lydict_insert(ctx, attr->name, 0); |
| 128 | result->type = attr->type; |
| 129 | |
| 130 | /* set namespace in case of standard attributes */ |
| 131 | if (result->type == LYXML_ATTR_STD && attr->ns) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 132 | result->ns = attr->ns; |
| 133 | lyxml_correct_attr_ns(ctx, result, parent, 1); |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /* set parent pointer in case of namespace attribute */ |
| 137 | if (result->type == LYXML_ATTR_NS) { |
| 138 | ((struct lyxml_ns *)result)->parent = parent; |
| 139 | } |
| 140 | |
| 141 | /* put attribute into the parent's attributes list */ |
| 142 | if (parent->attr) { |
| 143 | /* go to the end of the list */ |
| 144 | for (a = parent->attr; a->next; a = a->next); |
| 145 | /* and append new attribute */ |
| 146 | a->next = result; |
| 147 | } else { |
| 148 | /* add the first attribute in the list */ |
| 149 | parent->attr = result; |
| 150 | } |
| 151 | |
| 152 | return result; |
| 153 | } |
| 154 | |
Michal Vasko | f748dbc | 2016-04-05 11:27:47 +0200 | [diff] [blame] | 155 | void |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 156 | lyxml_correct_elem_ns(struct ly_ctx *ctx, struct lyxml_elem *elem, int copy_ns, int correct_attrs) |
| 157 | { |
| 158 | const struct lyxml_ns *tmp_ns; |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 159 | struct lyxml_elem *elem_root, *ns_root, *tmp, *iter; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 160 | struct lyxml_attr *attr; |
| 161 | |
| 162 | /* find the root of elem */ |
| 163 | for (elem_root = elem; elem_root->parent; elem_root = elem_root->parent); |
| 164 | |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 165 | LY_TREE_DFS_BEGIN(elem, tmp, iter) { |
| 166 | if (iter->ns) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 167 | /* find the root of elem NS */ |
Radek Krejci | c071c54 | 2016-01-27 14:57:51 +0100 | [diff] [blame] | 168 | for (ns_root = iter->ns->parent; ns_root; ns_root = ns_root->parent); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 169 | |
| 170 | /* elem NS is defined outside elem subtree */ |
| 171 | if (ns_root != elem_root) { |
| 172 | if (copy_ns) { |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 173 | tmp_ns = iter->ns; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 174 | /* we may have already copied the NS over? */ |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 175 | iter->ns = lyxml_get_ns(iter, tmp_ns->prefix); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 176 | |
| 177 | /* we haven't copied it over, copy it now */ |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 178 | if (!iter->ns) { |
| 179 | iter->ns = (struct lyxml_ns *)lyxml_dup_attr(ctx, iter, (struct lyxml_attr *)tmp_ns); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 180 | } |
| 181 | } else { |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 182 | iter->ns = NULL; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | } |
| 186 | if (correct_attrs) { |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 187 | LY_TREE_FOR(iter->attr, attr) { |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 188 | lyxml_correct_attr_ns(ctx, attr, elem_root, copy_ns); |
| 189 | } |
| 190 | } |
Radek Krejci | d5be568 | 2016-01-14 16:23:22 +0100 | [diff] [blame] | 191 | LY_TREE_DFS_END(elem, tmp, iter); |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 195 | struct lyxml_elem * |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 196 | lyxml_dup_elem(struct ly_ctx *ctx, struct lyxml_elem *elem, struct lyxml_elem *parent, int recursive, int with_siblings) |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 197 | { |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 198 | struct lyxml_elem *dup, *result = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 199 | struct lyxml_attr *attr; |
| 200 | |
| 201 | if (!elem) { |
| 202 | return NULL; |
| 203 | } |
| 204 | |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 205 | LY_TREE_FOR(elem, elem) { |
| 206 | dup = calloc(1, sizeof *dup); |
| 207 | LY_CHECK_ERR_RETURN(!dup, LOGMEM(ctx), NULL); |
| 208 | dup->content = lydict_insert(ctx, elem->content, 0); |
| 209 | dup->name = lydict_insert(ctx, elem->name, 0); |
| 210 | dup->flags = elem->flags; |
| 211 | dup->prev = dup; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 212 | |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 213 | if (parent) { |
| 214 | lyxml_add_child(ctx, parent, dup); |
| 215 | } |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 216 | |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 217 | /* keep old namespace for now */ |
| 218 | dup->ns = elem->ns; |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 219 | |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 220 | /* duplicate attributes */ |
| 221 | for (attr = elem->attr; attr; attr = attr->next) { |
| 222 | lyxml_dup_attr(ctx, dup, attr); |
| 223 | } |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 224 | |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 225 | /* correct namespaces */ |
| 226 | lyxml_correct_elem_ns(ctx, dup, 1, 0); |
Michal Vasko | a5c958f | 2018-12-11 08:31:42 +0100 | [diff] [blame] | 227 | |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 228 | if (recursive) { |
| 229 | /* duplicate children */ |
| 230 | lyxml_dup_elem(ctx, elem->child, dup, 1, 1); |
| 231 | } |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 232 | |
Michal Vasko | 85f218c | 2019-03-13 11:29:50 +0100 | [diff] [blame^] | 233 | /* set result (first sibling) */ |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 234 | if (!result) { |
| 235 | result = dup; |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if (!with_siblings) { |
| 239 | break; |
| 240 | } |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | return result; |
| 244 | } |
| 245 | |
Radek Krejci | 6879d95 | 2017-01-09 12:49:19 +0100 | [diff] [blame] | 246 | API struct lyxml_elem * |
| 247 | lyxml_dup(struct ly_ctx *ctx, struct lyxml_elem *root) |
| 248 | { |
Michal Vasko | dbc4058 | 2019-03-12 10:54:24 +0100 | [diff] [blame] | 249 | return lyxml_dup_elem(ctx, root, NULL, 1, 0); |
Radek Krejci | 6879d95 | 2017-01-09 12:49:19 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 252 | void |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 253 | lyxml_unlink_elem(struct ly_ctx *ctx, struct lyxml_elem *elem, int copy_ns) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 254 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 255 | struct lyxml_elem *parent, *first; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 256 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 257 | if (!elem) { |
| 258 | return; |
| 259 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 260 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 261 | /* store pointers to important nodes */ |
| 262 | parent = elem->parent; |
Radek Krejci | e1f1391 | 2015-05-26 15:17:38 +0200 | [diff] [blame] | 263 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 264 | /* unlink from parent */ |
| 265 | if (parent) { |
| 266 | if (parent->child == elem) { |
| 267 | /* we unlink the first child */ |
| 268 | /* update the parent's link */ |
| 269 | parent->child = elem->next; |
| 270 | } |
| 271 | /* forget about the parent */ |
| 272 | elem->parent = NULL; |
| 273 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 274 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 275 | if (copy_ns < 2) { |
| 276 | lyxml_correct_elem_ns(ctx, elem, copy_ns, 1); |
| 277 | } |
| 278 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 279 | /* unlink from siblings */ |
| 280 | if (elem->prev == elem) { |
| 281 | /* there are no more siblings */ |
| 282 | return; |
| 283 | } |
| 284 | if (elem->next) { |
| 285 | elem->next->prev = elem->prev; |
| 286 | } else { |
| 287 | /* unlinking the last element */ |
| 288 | if (parent) { |
| 289 | first = parent->child; |
| 290 | } else { |
| 291 | first = elem; |
Radek Krejci | e4fffcf | 2016-02-23 16:06:25 +0100 | [diff] [blame] | 292 | while (first->prev->next) { |
| 293 | first = first->prev; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | first->prev = elem->prev; |
| 297 | } |
| 298 | if (elem->prev->next) { |
| 299 | elem->prev->next = elem->next; |
| 300 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 301 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 302 | /* clean up the unlinked element */ |
| 303 | elem->next = NULL; |
| 304 | elem->prev = elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 305 | } |
| 306 | |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 307 | API void |
| 308 | lyxml_unlink(struct ly_ctx *ctx, struct lyxml_elem *elem) |
| 309 | { |
| 310 | if (!elem) { |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | lyxml_unlink_elem(ctx, elem, 1); |
| 315 | } |
| 316 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 317 | void |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 318 | lyxml_free_attr(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_attr *attr) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 319 | { |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 320 | struct lyxml_attr *aiter, *aprev; |
| 321 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 322 | if (!attr) { |
| 323 | return; |
| 324 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 325 | |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 326 | if (parent) { |
| 327 | /* unlink attribute from the parent's list of attributes */ |
| 328 | aprev = NULL; |
| 329 | for (aiter = parent->attr; aiter; aiter = aiter->next) { |
| 330 | if (aiter == attr) { |
| 331 | break; |
| 332 | } |
| 333 | aprev = aiter; |
| 334 | } |
| 335 | if (!aiter) { |
| 336 | /* attribute to remove not found */ |
| 337 | return; |
| 338 | } |
| 339 | |
| 340 | if (!aprev) { |
| 341 | /* attribute is first in parent's list of attributes */ |
| 342 | parent->attr = attr->next; |
| 343 | } else { |
| 344 | /* reconnect previous attribute to the next */ |
| 345 | aprev->next = attr->next; |
| 346 | } |
| 347 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 348 | lydict_remove(ctx, attr->name); |
| 349 | lydict_remove(ctx, attr->value); |
| 350 | free(attr); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 353 | void |
| 354 | lyxml_free_attrs(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 355 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 356 | struct lyxml_attr *a, *next; |
| 357 | if (!elem || !elem->attr) { |
| 358 | return; |
| 359 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 360 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 361 | a = elem->attr; |
| 362 | do { |
| 363 | next = a->next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 364 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 365 | lydict_remove(ctx, a->name); |
| 366 | lydict_remove(ctx, a->value); |
| 367 | free(a); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 368 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 369 | a = next; |
| 370 | } while (a); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 371 | } |
| 372 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 373 | static void |
Michal Vasko | 272e42f | 2015-12-02 12:20:37 +0100 | [diff] [blame] | 374 | lyxml_free_elem(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 375 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 376 | struct lyxml_elem *e, *next; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 377 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 378 | if (!elem) { |
| 379 | return; |
| 380 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 381 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 382 | lyxml_free_attrs(ctx, elem); |
| 383 | LY_TREE_FOR_SAFE(elem->child, next, e) { |
Michal Vasko | 272e42f | 2015-12-02 12:20:37 +0100 | [diff] [blame] | 384 | lyxml_free_elem(ctx, e); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 385 | } |
| 386 | lydict_remove(ctx, elem->name); |
| 387 | lydict_remove(ctx, elem->content); |
| 388 | free(elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 389 | } |
| 390 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 391 | API void |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 392 | lyxml_free(struct ly_ctx *ctx, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 393 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 394 | if (!elem) { |
| 395 | return; |
| 396 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 397 | |
Michal Vasko | 61f7ccb | 2015-10-23 10:15:08 +0200 | [diff] [blame] | 398 | lyxml_unlink_elem(ctx, elem, 2); |
Michal Vasko | 272e42f | 2015-12-02 12:20:37 +0100 | [diff] [blame] | 399 | lyxml_free_elem(ctx, elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 400 | } |
| 401 | |
Radek Krejci | 8f8db23 | 2016-05-23 16:48:21 +0200 | [diff] [blame] | 402 | API void |
| 403 | lyxml_free_withsiblings(struct ly_ctx *ctx, struct lyxml_elem *elem) |
| 404 | { |
| 405 | struct lyxml_elem *iter, *aux; |
| 406 | |
| 407 | if (!elem) { |
| 408 | return; |
| 409 | } |
| 410 | |
| 411 | /* optimization - avoid freeing (unlinking) the last node of the siblings list */ |
| 412 | /* so, first, free the node's predecessors to the beginning of the list ... */ |
| 413 | for(iter = elem->prev; iter->next; iter = aux) { |
| 414 | aux = iter->prev; |
| 415 | lyxml_free(ctx, iter); |
| 416 | } |
| 417 | /* ... then, the node is the first in the siblings list, so free them all */ |
| 418 | LY_TREE_FOR_SAFE(elem, aux, iter) { |
| 419 | lyxml_free(ctx, iter); |
| 420 | } |
| 421 | } |
| 422 | |
Michal Vasko | 88c2954 | 2015-11-27 14:57:53 +0100 | [diff] [blame] | 423 | API const char * |
Michal Vasko | 1e62a09 | 2015-12-01 12:27:20 +0100 | [diff] [blame] | 424 | lyxml_get_attr(const struct lyxml_elem *elem, const char *name, const char *ns) |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 425 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 426 | struct lyxml_attr *a; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 427 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 428 | assert(elem); |
| 429 | assert(name); |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 430 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 431 | for (a = elem->attr; a; a = a->next) { |
| 432 | if (a->type != LYXML_ATTR_STD) { |
| 433 | continue; |
| 434 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 435 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 436 | if (!strcmp(name, a->name)) { |
| 437 | if ((!ns && !a->ns) || (ns && a->ns && !strcmp(ns, a->ns->value))) { |
| 438 | return a->value; |
| 439 | } |
| 440 | } |
| 441 | } |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 442 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 443 | return NULL; |
Radek Krejci | da04f4a | 2015-05-21 12:54:09 +0200 | [diff] [blame] | 444 | } |
| 445 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 446 | int |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 447 | lyxml_add_child(struct ly_ctx *ctx, struct lyxml_elem *parent, struct lyxml_elem *elem) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 448 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 449 | struct lyxml_elem *e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 450 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 451 | assert(parent); |
| 452 | assert(elem); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 453 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 454 | /* (re)link element to parent */ |
| 455 | if (elem->parent) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 456 | lyxml_unlink_elem(ctx, elem, 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 457 | } |
| 458 | elem->parent = parent; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 459 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 460 | /* link parent to element */ |
| 461 | if (parent->child) { |
| 462 | e = parent->child; |
| 463 | elem->prev = e->prev; |
| 464 | elem->next = NULL; |
| 465 | elem->prev->next = elem; |
| 466 | e->prev = elem; |
| 467 | } else { |
| 468 | parent->child = elem; |
| 469 | elem->prev = elem; |
| 470 | elem->next = NULL; |
| 471 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 472 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 473 | return EXIT_SUCCESS; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Michal Vasko | 3b85572 | 2015-08-28 16:01:18 +0200 | [diff] [blame] | 476 | int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 477 | lyxml_getutf8(struct ly_ctx *ctx, const char *buf, unsigned int *read) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 478 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 479 | int c, aux; |
| 480 | int i; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 481 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 482 | c = buf[0]; |
| 483 | *read = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 484 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 485 | /* buf is NULL terminated string, so 0 means EOF */ |
| 486 | if (!c) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 487 | LOGVAL(ctx, LYE_EOF, LY_VLOG_NONE, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 488 | return 0; |
| 489 | } |
| 490 | *read = 1; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 491 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 492 | /* process character byte(s) */ |
| 493 | if ((c & 0xf8) == 0xf0) { |
| 494 | /* four bytes character */ |
| 495 | *read = 4; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 496 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 497 | c &= 0x07; |
| 498 | for (i = 1; i <= 3; i++) { |
| 499 | aux = buf[i]; |
| 500 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 501 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 502 | return 0; |
| 503 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 504 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 505 | c = (c << 6) | (aux & 0x3f); |
| 506 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 507 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 508 | if (c < 0x1000 || c > 0x10ffff) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 509 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | } else if ((c & 0xf0) == 0xe0) { |
| 513 | /* three bytes character */ |
| 514 | *read = 3; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 515 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 516 | c &= 0x0f; |
| 517 | for (i = 1; i <= 2; i++) { |
| 518 | aux = buf[i]; |
| 519 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 520 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 521 | return 0; |
| 522 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 523 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 524 | c = (c << 6) | (aux & 0x3f); |
| 525 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 526 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 527 | if (c < 0x800 || (c > 0xd7ff && c < 0xe000) || c > 0xfffd) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 528 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 529 | return 0; |
| 530 | } |
| 531 | } else if ((c & 0xe0) == 0xc0) { |
| 532 | /* two bytes character */ |
| 533 | *read = 2; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 534 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 535 | aux = buf[1]; |
| 536 | if ((aux & 0xc0) != 0x80) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 537 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 538 | return 0; |
| 539 | } |
| 540 | c = ((c & 0x1f) << 6) | (aux & 0x3f); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 541 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 542 | if (c < 0x80) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 543 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | } else if (!(c & 0x80)) { |
| 547 | /* one byte character */ |
| 548 | if (c < 0x20 && c != 0x9 && c != 0xa && c != 0xd) { |
| 549 | /* invalid character */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 550 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 551 | return 0; |
| 552 | } |
| 553 | } else { |
| 554 | /* invalid character */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 555 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "input character"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 556 | return 0; |
| 557 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 558 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 559 | return c; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 562 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 563 | static int |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 564 | parse_ignore(struct ly_ctx *ctx, const char *data, const char *endstr, unsigned int *len) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 565 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 566 | unsigned int slen; |
| 567 | const char *c = data; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 568 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 569 | slen = strlen(endstr); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 570 | |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 571 | while (*c && strncmp(c, endstr, slen)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 572 | c++; |
| 573 | } |
| 574 | if (!*c) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 575 | LOGVAL(ctx, LYE_XML_MISS, LY_VLOG_NONE, NULL, "closing sequence", endstr); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 576 | return EXIT_FAILURE; |
| 577 | } |
| 578 | c += slen; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 579 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 580 | *len = c - data; |
| 581 | return EXIT_SUCCESS; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 582 | } |
| 583 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 584 | /* logs directly, fails when return == NULL and *len == 0 */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 585 | static char * |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 586 | parse_text(struct ly_ctx *ctx, const char *data, char delim, unsigned int *len) |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 587 | { |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 588 | #define BUFSIZE 1024 |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 589 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 590 | char buf[BUFSIZE]; |
| 591 | char *result = NULL, *aux; |
| 592 | unsigned int r; |
| 593 | int o, size = 0; |
| 594 | int cdsect = 0; |
| 595 | int32_t n; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 596 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 597 | for (*len = o = 0; cdsect || data[*len] != delim; o++) { |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 598 | if (!data[*len] || (!cdsect && !strncmp(&data[*len], "]]>", 3))) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 599 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "element content, \"]]>\" found"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 600 | goto error; |
| 601 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 602 | |
Radek Krejci | a4a8406 | 2015-04-16 13:00:10 +0200 | [diff] [blame] | 603 | loop: |
| 604 | |
Radek Krejci | a0802a8 | 2017-02-08 12:41:05 +0100 | [diff] [blame] | 605 | if (o > BUFSIZE - 4) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 606 | /* add buffer into the result */ |
| 607 | if (result) { |
| 608 | size = size + o; |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 609 | result = ly_realloc(result, size + 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 610 | } else { |
| 611 | size = o; |
| 612 | result = malloc((size + 1) * sizeof *result); |
| 613 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 614 | LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 615 | memcpy(&result[size - o], buf, o); |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 616 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 617 | /* write again into the beginning of the buffer */ |
| 618 | o = 0; |
| 619 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 620 | |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 621 | if (cdsect || !strncmp(&data[*len], "<![CDATA[", 9)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 622 | /* CDSect */ |
| 623 | if (!cdsect) { |
| 624 | cdsect = 1; |
| 625 | *len += 9; |
| 626 | } |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 627 | if (data[*len] && !strncmp(&data[*len], "]]>", 3)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 628 | *len += 3; |
| 629 | cdsect = 0; |
| 630 | o--; /* we don't write any data in this iteration */ |
| 631 | } else { |
| 632 | buf[o] = data[*len]; |
| 633 | (*len)++; |
| 634 | } |
| 635 | } else if (data[*len] == '&') { |
| 636 | (*len)++; |
| 637 | if (data[*len] != '#') { |
| 638 | /* entity reference - only predefined refs are supported */ |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 639 | if (!strncmp(&data[*len], "lt;", 3)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 640 | buf[o] = '<'; |
| 641 | *len += 3; |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 642 | } else if (!strncmp(&data[*len], "gt;", 3)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 643 | buf[o] = '>'; |
| 644 | *len += 3; |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 645 | } else if (!strncmp(&data[*len], "amp;", 4)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 646 | buf[o] = '&'; |
| 647 | *len += 4; |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 648 | } else if (!strncmp(&data[*len], "apos;", 5)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 649 | buf[o] = '\''; |
| 650 | *len += 5; |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 651 | } else if (!strncmp(&data[*len], "quot;", 5)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 652 | buf[o] = '\"'; |
| 653 | *len += 5; |
| 654 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 655 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "entity reference (only predefined references are supported)"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 656 | goto error; |
| 657 | } |
| 658 | } else { |
| 659 | /* character reference */ |
| 660 | (*len)++; |
| 661 | if (isdigit(data[*len])) { |
| 662 | for (n = 0; isdigit(data[*len]); (*len)++) { |
| 663 | n = (10 * n) + (data[*len] - '0'); |
| 664 | } |
| 665 | if (data[*len] != ';') { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 666 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character reference, missing semicolon"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 667 | goto error; |
| 668 | } |
| 669 | } else if (data[(*len)++] == 'x' && isxdigit(data[*len])) { |
| 670 | for (n = 0; isxdigit(data[*len]); (*len)++) { |
| 671 | if (isdigit(data[*len])) { |
| 672 | r = (data[*len] - '0'); |
| 673 | } else if (data[*len] > 'F') { |
| 674 | r = 10 + (data[*len] - 'a'); |
| 675 | } else { |
| 676 | r = 10 + (data[*len] - 'A'); |
| 677 | } |
| 678 | n = (16 * n) + r; |
| 679 | } |
| 680 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 681 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character reference"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 682 | goto error; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 683 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 684 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 685 | r = pututf8(ctx, &buf[o], n); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 686 | if (!r) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 687 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "character reference value"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 688 | goto error; |
| 689 | } |
| 690 | o += r - 1; /* o is ++ in for loop */ |
| 691 | (*len)++; |
| 692 | } |
| 693 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 694 | r = copyutf8(ctx, &buf[o], &data[*len]); |
Radek Krejci | deee60e | 2016-09-23 15:21:14 +0200 | [diff] [blame] | 695 | if (!r) { |
| 696 | goto error; |
| 697 | } |
| 698 | |
| 699 | o += r - 1; /* o is ++ in for loop */ |
| 700 | (*len) = (*len) + r; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 701 | } |
| 702 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 703 | |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 704 | if (delim == '<' && !strncmp(&data[*len], "<![CDATA[", 9)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 705 | /* ignore loop's end condition on beginning of CDSect */ |
| 706 | goto loop; |
| 707 | } |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 708 | #undef BUFSIZE |
| 709 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 710 | if (o) { |
| 711 | if (result) { |
| 712 | size = size + o; |
| 713 | aux = realloc(result, size + 1); |
| 714 | result = aux; |
| 715 | } else { |
| 716 | size = o; |
| 717 | result = malloc((size + 1) * sizeof *result); |
| 718 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 719 | LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 720 | memcpy(&result[size - o], buf, o); |
| 721 | } |
| 722 | if (result) { |
| 723 | result[size] = '\0'; |
Radek Krejci | a526964 | 2015-07-20 19:04:11 +0200 | [diff] [blame] | 724 | } else { |
| 725 | size = 0; |
| 726 | result = strdup(""); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 727 | LY_CHECK_ERR_RETURN(!result, LOGMEM(ctx), NULL) |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 728 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 729 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 730 | return result; |
Radek Krejci | 709fee6 | 2015-04-15 13:56:19 +0200 | [diff] [blame] | 731 | |
| 732 | error: |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 733 | *len = 0; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 734 | free(result); |
| 735 | return NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 736 | } |
| 737 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 738 | /* logs directly */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 739 | static struct lyxml_attr * |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 740 | parse_attr(struct ly_ctx *ctx, const char *data, unsigned int *len, struct lyxml_elem *parent) |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 741 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 742 | const char *c = data, *start, *delim; |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 743 | char *prefix = NULL, xml_flag, *str; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 744 | int uc; |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 745 | struct lyxml_attr *attr = NULL, *a; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 746 | unsigned int size; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 747 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 748 | /* check if it is attribute or namespace */ |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 749 | if (!strncmp(c, "xmlns", 5)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 750 | /* namespace */ |
| 751 | attr = calloc(1, sizeof (struct lyxml_ns)); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 752 | LY_CHECK_ERR_RETURN(!attr, LOGMEM(ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 753 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 754 | attr->type = LYXML_ATTR_NS; |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 755 | ((struct lyxml_ns *)attr)->parent = parent; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 756 | c += 5; |
| 757 | if (*c != ':') { |
| 758 | /* default namespace, prefix will be empty */ |
| 759 | goto equal; |
| 760 | } |
| 761 | c++; /* go after ':' to the prefix value */ |
| 762 | } else { |
| 763 | /* attribute */ |
| 764 | attr = calloc(1, sizeof *attr); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 765 | LY_CHECK_ERR_RETURN(!attr, LOGMEM(ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 766 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 767 | attr->type = LYXML_ATTR_STD; |
| 768 | } |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 769 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 770 | /* process name part of the attribute */ |
| 771 | start = c; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 772 | uc = lyxml_getutf8(ctx, c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 773 | if (!is_xmlnamestartchar(uc)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 774 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "NameStartChar of the attribute"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 775 | free(attr); |
| 776 | return NULL; |
| 777 | } |
Michal Vasko | 62d5a6b | 2018-01-03 14:31:39 +0100 | [diff] [blame] | 778 | xml_flag = 4; |
| 779 | if (*c == 'x') { |
| 780 | xml_flag = 1; |
| 781 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 782 | c += size; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 783 | uc = lyxml_getutf8(ctx, c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 784 | while (is_xmlnamechar(uc)) { |
Michal Vasko | 62d5a6b | 2018-01-03 14:31:39 +0100 | [diff] [blame] | 785 | if (attr->type == LYXML_ATTR_STD) { |
| 786 | if ((*c == ':') && (xml_flag != 3)) { |
| 787 | /* attribute in a namespace (but disregard the special "xml" namespace) */ |
| 788 | start = c + 1; |
Radek Krejci | 4ea0838 | 2015-04-21 09:41:40 +0200 | [diff] [blame] | 789 | |
Michal Vasko | 62d5a6b | 2018-01-03 14:31:39 +0100 | [diff] [blame] | 790 | /* look for the prefix in namespaces */ |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 791 | prefix = malloc((c - data + 1) * sizeof *prefix); |
| 792 | LY_CHECK_ERR_GOTO(!prefix, LOGMEM(ctx), error); |
Michal Vasko | 62d5a6b | 2018-01-03 14:31:39 +0100 | [diff] [blame] | 793 | memcpy(prefix, data, c - data); |
| 794 | prefix[c - data] = '\0'; |
| 795 | attr->ns = lyxml_get_ns(parent, prefix); |
| 796 | } else if (((*c == 'm') && (xml_flag == 1)) || |
| 797 | ((*c == 'l') && (xml_flag == 2))) { |
| 798 | ++xml_flag; |
| 799 | } else { |
| 800 | xml_flag = 4; |
| 801 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 802 | } |
| 803 | c += size; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 804 | uc = lyxml_getutf8(ctx, c, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 805 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 806 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 807 | /* store the name */ |
| 808 | size = c - start; |
| 809 | attr->name = lydict_insert(ctx, start, size); |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 810 | |
| 811 | equal: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 812 | /* check Eq mark that can be surrounded by whitespaces */ |
| 813 | ign_xmlws(c); |
| 814 | if (*c != '=') { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 815 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute definition, \"=\" expected"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 816 | goto error; |
| 817 | } |
| 818 | c++; |
| 819 | ign_xmlws(c); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 820 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 821 | /* process value part of the attribute */ |
| 822 | if (!*c || (*c != '"' && *c != '\'')) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 823 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "attribute value, \" or \' expected"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 824 | goto error; |
| 825 | } |
| 826 | delim = c; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 827 | str = parse_text(ctx, ++c, *delim, &size); |
| 828 | if (!str && !size) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 829 | goto error; |
| 830 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 831 | attr->value = lydict_insert_zc(ctx, str); |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 832 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 833 | *len = c + size + 1 - data; /* +1 is delimiter size */ |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 834 | |
| 835 | /* put attribute into the parent's attributes list */ |
| 836 | if (parent->attr) { |
| 837 | /* go to the end of the list */ |
| 838 | for (a = parent->attr; a->next; a = a->next); |
| 839 | /* and append new attribute */ |
| 840 | a->next = attr; |
| 841 | } else { |
| 842 | /* add the first attribute in the list */ |
| 843 | parent->attr = attr; |
| 844 | } |
| 845 | |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 846 | free(prefix); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 847 | return attr; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 848 | |
| 849 | error: |
Radek Krejci | 00249f2 | 2015-07-07 13:43:28 +0200 | [diff] [blame] | 850 | lyxml_free_attr(ctx, NULL, attr); |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 851 | free(prefix); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 852 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 853 | } |
| 854 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 855 | /* logs directly */ |
Radek Krejci | 9a5daea | 2016-03-02 16:49:40 +0100 | [diff] [blame] | 856 | struct lyxml_elem * |
Radek Krejci | e1bacd7 | 2017-03-01 13:18:46 +0100 | [diff] [blame] | 857 | lyxml_parse_elem(struct ly_ctx *ctx, const char *data, unsigned int *len, struct lyxml_elem *parent, int options) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 858 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 859 | const char *c = data, *start, *e; |
| 860 | const char *lws; /* leading white space for handling mixed content */ |
| 861 | int uc; |
| 862 | char *str; |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 863 | char *prefix = NULL; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 864 | unsigned int prefix_len = 0; |
| 865 | struct lyxml_elem *elem = NULL, *child; |
| 866 | struct lyxml_attr *attr; |
| 867 | unsigned int size; |
| 868 | int nons_flag = 0, closed_flag = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 869 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 870 | *len = 0; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 871 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 872 | if (*c != '<') { |
| 873 | return NULL; |
| 874 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 875 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 876 | /* locate element name */ |
| 877 | c++; |
| 878 | e = c; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 879 | |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 880 | uc = lyxml_getutf8(ctx, e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 881 | if (!is_xmlnamestartchar(uc)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 882 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "NameStartChar of the element"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 883 | return NULL; |
| 884 | } |
| 885 | e += size; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 886 | uc = lyxml_getutf8(ctx, e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 887 | while (is_xmlnamechar(uc)) { |
| 888 | if (*e == ':') { |
| 889 | if (prefix_len) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 890 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_NONE, NULL, "element name, multiple colons found"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 891 | goto error; |
| 892 | } |
| 893 | /* element in a namespace */ |
| 894 | start = e + 1; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 895 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 896 | /* look for the prefix in namespaces */ |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 897 | prefix_len = e - c; |
Michal Vasko | 5f5096b | 2018-08-17 10:56:48 +0200 | [diff] [blame] | 898 | LY_CHECK_ERR_GOTO(prefix, LOGVAL(ctx, LYE_XML_INCHAR, LY_VLOG_NONE, NULL, e), error); |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 899 | prefix = malloc((prefix_len + 1) * sizeof *prefix); |
| 900 | LY_CHECK_ERR_GOTO(!prefix, LOGMEM(ctx), error); |
| 901 | memcpy(prefix, c, prefix_len); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 902 | prefix[prefix_len] = '\0'; |
| 903 | c = start; |
| 904 | } |
| 905 | e += size; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 906 | uc = lyxml_getutf8(ctx, e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 907 | } |
| 908 | if (!*e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 909 | LOGVAL(ctx, LYE_EOF, LY_VLOG_NONE, NULL); |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 910 | free(prefix); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 911 | return NULL; |
| 912 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 913 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 914 | /* allocate element structure */ |
| 915 | elem = calloc(1, sizeof *elem); |
Michal Vasko | 64e6cf8 | 2018-08-17 10:32:23 +0200 | [diff] [blame] | 916 | LY_CHECK_ERR_RETURN(!elem, free(prefix); LOGMEM(ctx), NULL); |
Radek Krejci | a8d111f | 2017-05-31 13:57:37 +0200 | [diff] [blame] | 917 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 918 | elem->next = NULL; |
| 919 | elem->prev = elem; |
| 920 | if (parent) { |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 921 | lyxml_add_child(ctx, parent, elem); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 922 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 923 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 924 | /* store the name into the element structure */ |
| 925 | elem->name = lydict_insert(ctx, c, e - c); |
| 926 | c = e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 927 | |
| 928 | process: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 929 | ign_xmlws(c); |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 930 | if (!strncmp("/>", c, 2)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 931 | /* we are done, it was EmptyElemTag */ |
| 932 | c += 2; |
Michal Vasko | 4491384 | 2016-04-13 14:20:41 +0200 | [diff] [blame] | 933 | elem->content = lydict_insert(ctx, "", 0); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 934 | closed_flag = 1; |
| 935 | } else if (*c == '>') { |
| 936 | /* process element content */ |
| 937 | c++; |
| 938 | lws = NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 939 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 940 | while (*c) { |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 941 | if (!strncmp(c, "</", 2)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 942 | if (lws && !elem->child) { |
| 943 | /* leading white spaces were actually content */ |
| 944 | goto store_content; |
| 945 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 946 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 947 | /* Etag */ |
| 948 | c += 2; |
| 949 | /* get name and check it */ |
| 950 | e = c; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 951 | uc = lyxml_getutf8(ctx, e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 952 | if (!is_xmlnamestartchar(uc)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 953 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_XML, elem, "NameStartChar of the element"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 954 | goto error; |
| 955 | } |
| 956 | e += size; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 957 | uc = lyxml_getutf8(ctx, e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 958 | while (is_xmlnamechar(uc)) { |
| 959 | if (*e == ':') { |
| 960 | /* element in a namespace */ |
| 961 | start = e + 1; |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 962 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 963 | /* look for the prefix in namespaces */ |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 964 | if (!prefix || memcmp(prefix, c, e - c)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 965 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_XML, elem, |
Michal Vasko | ff9336a | 2016-05-10 10:48:48 +0200 | [diff] [blame] | 966 | "Invalid (different namespaces) opening (%s) and closing element tags.", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 967 | goto error; |
| 968 | } |
| 969 | c = start; |
| 970 | } |
| 971 | e += size; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 972 | uc = lyxml_getutf8(ctx, e, &size); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 973 | } |
| 974 | if (!*e) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 975 | LOGVAL(ctx, LYE_EOF, LY_VLOG_NONE, NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 976 | goto error; |
| 977 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 978 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 979 | /* check that it corresponds to opening tag */ |
| 980 | size = e - c; |
| 981 | str = malloc((size + 1) * sizeof *str); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 982 | LY_CHECK_ERR_GOTO(!str, LOGMEM(ctx), error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 983 | memcpy(str, c, e - c); |
| 984 | str[e - c] = '\0'; |
| 985 | if (size != strlen(elem->name) || memcmp(str, elem->name, size)) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 986 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_XML, elem, |
Michal Vasko | ff9336a | 2016-05-10 10:48:48 +0200 | [diff] [blame] | 987 | "Invalid (mixed names) opening (%s) and closing (%s) element tags.", elem->name, str); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 988 | free(str); |
| 989 | goto error; |
| 990 | } |
| 991 | free(str); |
| 992 | c = e; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 993 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 994 | ign_xmlws(c); |
| 995 | if (*c != '>') { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 996 | LOGVAL(ctx, LYE_SPEC, LY_VLOG_XML, elem, "Data after closing element tag \"%s\".", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 997 | goto error; |
| 998 | } |
| 999 | c++; |
Michal Vasko | e00b789 | 2016-04-14 10:12:18 +0200 | [diff] [blame] | 1000 | if (!(elem->flags & LYXML_ELEM_MIXED) && !elem->content) { |
| 1001 | /* there was no content, but we don't want NULL (only if mixed content) */ |
| 1002 | elem->content = lydict_insert(ctx, "", 0); |
| 1003 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1004 | closed_flag = 1; |
| 1005 | break; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1006 | |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 1007 | } else if (!strncmp(c, "<?", 2)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1008 | if (lws) { |
| 1009 | /* leading white spaces were only formatting */ |
| 1010 | lws = NULL; |
| 1011 | } |
| 1012 | /* PI - ignore it */ |
| 1013 | c += 2; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1014 | if (parse_ignore(ctx, c, "?>", &size)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1015 | goto error; |
| 1016 | } |
| 1017 | c += size; |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 1018 | } else if (!strncmp(c, "<!--", 4)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1019 | if (lws) { |
| 1020 | /* leading white spaces were only formatting */ |
| 1021 | lws = NULL; |
| 1022 | } |
| 1023 | /* Comment - ignore it */ |
| 1024 | c += 4; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1025 | if (parse_ignore(ctx, c, "-->", &size)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1026 | goto error; |
| 1027 | } |
| 1028 | c += size; |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 1029 | } else if (!strncmp(c, "<![CDATA[", 9)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1030 | /* CDSect */ |
| 1031 | goto store_content; |
| 1032 | } else if (*c == '<') { |
| 1033 | if (lws) { |
| 1034 | if (elem->flags & LYXML_ELEM_MIXED) { |
| 1035 | /* we have a mixed content */ |
| 1036 | goto store_content; |
| 1037 | } else { |
| 1038 | /* leading white spaces were only formatting */ |
| 1039 | lws = NULL; |
| 1040 | } |
| 1041 | } |
| 1042 | if (elem->content) { |
| 1043 | /* we have a mixed content */ |
Radek Krejci | e1bacd7 | 2017-03-01 13:18:46 +0100 | [diff] [blame] | 1044 | if (options & LYXML_PARSE_NOMIXEDCONTENT) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1045 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_XML, elem, "XML element with mixed content"); |
Radek Krejci | e1bacd7 | 2017-03-01 13:18:46 +0100 | [diff] [blame] | 1046 | goto error; |
| 1047 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1048 | child = calloc(1, sizeof *child); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1049 | LY_CHECK_ERR_GOTO(!child, LOGMEM(ctx), error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1050 | child->content = elem->content; |
| 1051 | elem->content = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 1052 | lyxml_add_child(ctx, elem, child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1053 | elem->flags |= LYXML_ELEM_MIXED; |
| 1054 | } |
Radek Krejci | e1bacd7 | 2017-03-01 13:18:46 +0100 | [diff] [blame] | 1055 | child = lyxml_parse_elem(ctx, c, &size, elem, options); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1056 | if (!child) { |
| 1057 | goto error; |
| 1058 | } |
| 1059 | c += size; /* move after processed child element */ |
| 1060 | } else if (is_xmlws(*c)) { |
| 1061 | lws = c; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1062 | ign_xmlws(c); |
| 1063 | } else { |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1064 | store_content: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1065 | /* store text content */ |
| 1066 | if (lws) { |
| 1067 | /* process content including the leading white spaces */ |
| 1068 | c = lws; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1069 | lws = NULL; |
| 1070 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1071 | str = parse_text(ctx, c, '<', &size); |
| 1072 | if (!str && !size) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1073 | goto error; |
| 1074 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1075 | elem->content = lydict_insert_zc(ctx, str); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1076 | c += size; /* move after processed text content */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1077 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1078 | if (elem->child) { |
| 1079 | /* we have a mixed content */ |
Radek Krejci | e1bacd7 | 2017-03-01 13:18:46 +0100 | [diff] [blame] | 1080 | if (options & LYXML_PARSE_NOMIXEDCONTENT) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1081 | LOGVAL(ctx, LYE_XML_INVAL, LY_VLOG_XML, elem, "XML element with mixed content"); |
Radek Krejci | e1bacd7 | 2017-03-01 13:18:46 +0100 | [diff] [blame] | 1082 | goto error; |
| 1083 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1084 | child = calloc(1, sizeof *child); |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1085 | LY_CHECK_ERR_GOTO(!child, LOGMEM(ctx), error); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1086 | child->content = elem->content; |
| 1087 | elem->content = NULL; |
Michal Vasko | f8879c2 | 2015-08-21 09:07:36 +0200 | [diff] [blame] | 1088 | lyxml_add_child(ctx, elem, child); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1089 | elem->flags |= LYXML_ELEM_MIXED; |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | } else { |
| 1094 | /* process attribute */ |
| 1095 | attr = parse_attr(ctx, c, &size, elem); |
| 1096 | if (!attr) { |
| 1097 | goto error; |
| 1098 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1099 | c += size; /* move after processed attribute */ |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1100 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1101 | /* check namespace */ |
| 1102 | if (attr->type == LYXML_ATTR_NS) { |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 1103 | if ((!prefix || !prefix[0]) && !attr->name) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1104 | if (attr->value) { |
| 1105 | /* default prefix */ |
| 1106 | elem->ns = (struct lyxml_ns *)attr; |
| 1107 | } else { |
| 1108 | /* xmlns="" -> no namespace */ |
| 1109 | nons_flag = 1; |
| 1110 | } |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 1111 | } else if (prefix && prefix[0] && attr->name && !strncmp(attr->name, prefix, prefix_len + 1)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1112 | /* matching namespace with prefix */ |
| 1113 | elem->ns = (struct lyxml_ns *)attr; |
| 1114 | } |
| 1115 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1116 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1117 | /* go back to finish element processing */ |
| 1118 | goto process; |
| 1119 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1120 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1121 | *len = c - data; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1122 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1123 | if (!closed_flag) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1124 | LOGVAL(ctx, LYE_XML_MISS, LY_VLOG_XML, elem, "closing element tag", elem->name); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1125 | goto error; |
| 1126 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1127 | |
Radek Krejci | 78a230a | 2015-07-07 17:04:40 +0200 | [diff] [blame] | 1128 | if (!elem->ns && !nons_flag && parent) { |
Radek Krejci | 4476d41 | 2015-07-10 15:35:01 +0200 | [diff] [blame] | 1129 | elem->ns = lyxml_get_ns(parent, prefix_len ? prefix : NULL); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1130 | } |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 1131 | free(prefix); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1132 | return elem; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1133 | |
| 1134 | error: |
Michal Vasko | 345da0a | 2015-12-02 10:35:55 +0100 | [diff] [blame] | 1135 | lyxml_free(ctx, elem); |
aweast | 069a6c0 | 2018-05-30 16:44:18 -0500 | [diff] [blame] | 1136 | free(prefix); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1137 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Michal Vasko | 0d343d1 | 2015-08-24 14:57:36 +0200 | [diff] [blame] | 1140 | /* logs directly */ |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1141 | API struct lyxml_elem * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1142 | lyxml_parse_mem(struct ly_ctx *ctx, const char *data, int options) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1143 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1144 | const char *c = data; |
| 1145 | unsigned int len; |
Radek Krejci | 851ea66 | 2016-01-08 09:30:53 +0100 | [diff] [blame] | 1146 | struct lyxml_elem *root, *first = NULL, *next; |
Radek Krejci | 2342cf6 | 2016-01-29 16:48:23 +0100 | [diff] [blame] | 1147 | |
Radek Krejci | 19b9b25 | 2017-03-17 16:14:09 +0100 | [diff] [blame] | 1148 | if (!ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1149 | LOGARG; |
Radek Krejci | 19b9b25 | 2017-03-17 16:14:09 +0100 | [diff] [blame] | 1150 | return NULL; |
| 1151 | } |
| 1152 | |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1153 | repeat: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1154 | /* process document */ |
Radek Krejci | f8ae23e | 2016-07-26 17:11:17 +0200 | [diff] [blame] | 1155 | while (1) { |
| 1156 | if (!*c) { |
| 1157 | /* eof */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1158 | return first; |
Radek Krejci | f8ae23e | 2016-07-26 17:11:17 +0200 | [diff] [blame] | 1159 | } else if (is_xmlws(*c)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1160 | /* skip whitespaces */ |
| 1161 | ign_xmlws(c); |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 1162 | } else if (!strncmp(c, "<?", 2)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1163 | /* XMLDecl or PI - ignore it */ |
| 1164 | c += 2; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1165 | if (parse_ignore(ctx, c, "?>", &len)) { |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1166 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1167 | } |
| 1168 | c += len; |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 1169 | } else if (!strncmp(c, "<!--", 4)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1170 | /* Comment - ignore it */ |
| 1171 | c += 2; |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1172 | if (parse_ignore(ctx, c, "-->", &len)) { |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1173 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1174 | } |
| 1175 | c += len; |
Radek Krejci | fb78394 | 2016-10-06 09:49:33 +0200 | [diff] [blame] | 1176 | } else if (!strncmp(c, "<!", 2)) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1177 | /* DOCTYPE */ |
| 1178 | /* TODO - standalone ignore counting < and > */ |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1179 | LOGERR(ctx, LY_EINVAL, "DOCTYPE not supported in XML documents."); |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1180 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1181 | } else if (*c == '<') { |
| 1182 | /* element - process it in next loop to strictly follow XML |
| 1183 | * format |
| 1184 | */ |
| 1185 | break; |
Michal Vasko | c2e8056 | 2015-07-27 11:31:41 +0200 | [diff] [blame] | 1186 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1187 | LOGVAL(ctx, LYE_XML_INCHAR, LY_VLOG_NONE, NULL, c); |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1188 | goto error; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1189 | } |
| 1190 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1191 | |
Radek Krejci | e1bacd7 | 2017-03-01 13:18:46 +0100 | [diff] [blame] | 1192 | root = lyxml_parse_elem(ctx, c, &len, NULL, options); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1193 | if (!root) { |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1194 | goto error; |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1195 | } else if (!first) { |
| 1196 | first = root; |
| 1197 | } else { |
| 1198 | first->prev->next = root; |
| 1199 | root->prev = first->prev; |
| 1200 | first->prev = root; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1201 | } |
| 1202 | c += len; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1203 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1204 | /* ignore the rest of document where can be comments, PIs and whitespaces, |
| 1205 | * note that we are not detecting syntax errors in these parts |
| 1206 | */ |
| 1207 | ign_xmlws(c); |
| 1208 | if (*c) { |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1209 | if (options & LYXML_PARSE_MULTIROOT) { |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1210 | goto repeat; |
| 1211 | } else { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1212 | LOGWRN(ctx, "There are some not parsed data:\n%s", c); |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1213 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1214 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1215 | |
Radek Krejci | 120f624 | 2015-12-17 12:32:56 +0100 | [diff] [blame] | 1216 | return first; |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1217 | |
| 1218 | error: |
| 1219 | LY_TREE_FOR_SAFE(first, next, root) { |
| 1220 | lyxml_free(ctx, root); |
| 1221 | } |
Radek Krejci | cf74825 | 2017-09-04 11:11:14 +0200 | [diff] [blame] | 1222 | return NULL; |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1223 | } |
| 1224 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1225 | API struct lyxml_elem * |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1226 | lyxml_parse_path(struct ly_ctx *ctx, const char *filename, int options) |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1227 | { |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1228 | struct lyxml_elem *elem = NULL; |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1229 | size_t length; |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1230 | int fd; |
| 1231 | char *addr; |
| 1232 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1233 | if (!filename || !ctx) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1234 | LOGARG; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1235 | return NULL; |
| 1236 | } |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1237 | |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1238 | fd = open(filename, O_RDONLY); |
| 1239 | if (fd == -1) { |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1240 | LOGERR(ctx, LY_EINVAL,"Opening file \"%s\" failed.", filename); |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1241 | return NULL; |
| 1242 | } |
Michal Vasko | 53b7da0 | 2018-02-13 15:28:42 +0100 | [diff] [blame] | 1243 | if (lyp_mmap(ctx, fd, 0, &length, (void **)&addr)) { |
| 1244 | LOGERR(ctx, LY_ESYS, "Mapping file descriptor into memory failed (%s()).", __func__); |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1245 | goto error; |
Radek Krejci | 10c216a | 2017-02-01 10:36:00 +0100 | [diff] [blame] | 1246 | } else if (!addr) { |
| 1247 | /* empty XML file */ |
| 1248 | goto error; |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1249 | } |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1250 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1251 | elem = lyxml_parse_mem(ctx, addr, options); |
Radek Krejci | 0fb1150 | 2017-01-31 16:45:42 +0100 | [diff] [blame] | 1252 | lyp_munmap(addr, length); |
Radek Krejci | 30793ab | 2015-12-03 13:45:45 +0100 | [diff] [blame] | 1253 | close(fd); |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1254 | |
Pavol Vican | b2570c1 | 2015-11-12 13:50:20 +0100 | [diff] [blame] | 1255 | return elem; |
| 1256 | |
| 1257 | error: |
Radek Krejci | 6b3d926 | 2015-12-03 13:45:27 +0100 | [diff] [blame] | 1258 | if (fd != -1) { |
| 1259 | close(fd); |
| 1260 | } |
| 1261 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1262 | return NULL; |
Radek Krejci | 54ea8de | 2015-04-09 18:02:56 +0200 | [diff] [blame] | 1263 | } |
Radek Krejci | 0211730 | 2015-04-13 16:32:44 +0200 | [diff] [blame] | 1264 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1265 | int |
Radek Krejci | eb827b7 | 2018-02-23 10:05:02 +0100 | [diff] [blame] | 1266 | lyxml_dump_text(struct lyout *out, const char *text, LYXML_DATA_TYPE type) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1267 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1268 | unsigned int i, n; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1269 | |
Michal Vasko | 5db027d | 2015-10-09 14:38:50 +0200 | [diff] [blame] | 1270 | if (!text) { |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1274 | for (i = n = 0; text[i]; i++) { |
| 1275 | switch (text[i]) { |
| 1276 | case '&': |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1277 | n += ly_print(out, "&"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1278 | break; |
| 1279 | case '<': |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1280 | n += ly_print(out, "<"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1281 | break; |
| 1282 | case '>': |
| 1283 | /* not needed, just for readability */ |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1284 | n += ly_print(out, ">"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1285 | break; |
Radek Krejci | 952a725 | 2016-07-16 20:52:43 +0200 | [diff] [blame] | 1286 | case '"': |
Radek Krejci | eb827b7 | 2018-02-23 10:05:02 +0100 | [diff] [blame] | 1287 | if (type == LYXML_DATA_ATTR) { |
| 1288 | n += ly_print(out, """); |
| 1289 | break; |
| 1290 | } |
| 1291 | /* falls through */ |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1292 | default: |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1293 | ly_write(out, &text[i], 1); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1294 | n++; |
| 1295 | } |
| 1296 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1297 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1298 | return n; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1299 | } |
| 1300 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1301 | static int |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1302 | dump_elem(struct lyout *out, const struct lyxml_elem *e, int level, int options, int last_elem) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1303 | { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1304 | int size = 0; |
| 1305 | struct lyxml_attr *a; |
| 1306 | struct lyxml_elem *child; |
| 1307 | const char *delim, *delim_outer; |
| 1308 | int indent; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1309 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1310 | if (!e->name) { |
| 1311 | /* mixed content */ |
| 1312 | if (e->content) { |
Radek Krejci | eb827b7 | 2018-02-23 10:05:02 +0100 | [diff] [blame] | 1313 | return lyxml_dump_text(out, e->content, LYXML_DATA_ELEM); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1314 | } else { |
| 1315 | return 0; |
| 1316 | } |
| 1317 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1318 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1319 | delim = delim_outer = (options & LYXML_PRINT_FORMAT) ? "\n" : ""; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1320 | indent = 2 * level; |
| 1321 | if ((e->flags & LYXML_ELEM_MIXED) || (e->parent && (e->parent->flags & LYXML_ELEM_MIXED))) { |
| 1322 | delim = ""; |
| 1323 | } |
| 1324 | if (e->parent && (e->parent->flags & LYXML_ELEM_MIXED)) { |
| 1325 | delim_outer = ""; |
| 1326 | indent = 0; |
| 1327 | } |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1328 | if (last_elem && (options & LYXML_PRINT_NO_LAST_NEWLINE)) { |
| 1329 | delim_outer = ""; |
| 1330 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1331 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1332 | if (!(options & (LYXML_PRINT_OPEN | LYXML_PRINT_CLOSE | LYXML_PRINT_ATTRS)) || (options & LYXML_PRINT_OPEN)) { |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1333 | /* opening tag */ |
| 1334 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1335 | size += ly_print(out, "%*s<%s:%s", indent, "", e->ns->prefix, e->name); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1336 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1337 | size += ly_print(out, "%*s<%s", indent, "", e->name); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1338 | } |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1339 | } else if (options & LYXML_PRINT_CLOSE) { |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1340 | indent = 0; |
| 1341 | goto close; |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1342 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1343 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1344 | /* attributes */ |
| 1345 | for (a = e->attr; a; a = a->next) { |
| 1346 | if (a->type == LYXML_ATTR_NS) { |
| 1347 | if (a->name) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1348 | size += ly_print(out, " xmlns:%s=\"%s\"", a->name, a->value ? a->value : ""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1349 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1350 | size += ly_print(out, " xmlns=\"%s\"", a->value ? a->value : ""); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1351 | } |
| 1352 | } else if (a->ns && a->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1353 | size += ly_print(out, " %s:%s=\"%s\"", a->ns->prefix, a->name, a->value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1354 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1355 | size += ly_print(out, " %s=\"%s\"", a->name, a->value); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1356 | } |
| 1357 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1358 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1359 | /* apply options */ |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1360 | if ((options & LYXML_PRINT_CLOSE) && (options & LYXML_PRINT_OPEN)) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1361 | size += ly_print(out, "/>%s", delim); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1362 | return size; |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1363 | } else if (options & LYXML_PRINT_OPEN) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1364 | ly_print(out, ">"); |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1365 | return ++size; |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1366 | } else if (options & LYXML_PRINT_ATTRS) { |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1367 | return size; |
| 1368 | } |
| 1369 | |
Michal Vasko | 3a61161 | 2016-04-14 10:12:56 +0200 | [diff] [blame] | 1370 | if (!e->child && (!e->content || !e->content[0])) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1371 | size += ly_print(out, "/>%s", delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1372 | return size; |
Michal Vasko | 3a61161 | 2016-04-14 10:12:56 +0200 | [diff] [blame] | 1373 | } else if (e->content && e->content[0]) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1374 | ly_print(out, ">"); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1375 | size++; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1376 | |
Radek Krejci | eb827b7 | 2018-02-23 10:05:02 +0100 | [diff] [blame] | 1377 | size += lyxml_dump_text(out, e->content, LYXML_DATA_ELEM); |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1378 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1379 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1380 | size += ly_print(out, "</%s:%s>%s", e->ns->prefix, e->name, delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1381 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1382 | size += ly_print(out, "</%s>%s", e->name, delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1383 | } |
| 1384 | return size; |
| 1385 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1386 | size += ly_print(out, ">%s", delim); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1387 | } |
Radek Krejci | 674e1f8 | 2015-04-21 14:12:19 +0200 | [diff] [blame] | 1388 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1389 | /* go recursively */ |
| 1390 | LY_TREE_FOR(e->child, child) { |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1391 | if (options & LYXML_PRINT_FORMAT) { |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1392 | size += dump_elem(out, child, level + 1, LYXML_PRINT_FORMAT, 0); |
Pavol Vican | be7eef5 | 2015-10-22 14:07:48 +0200 | [diff] [blame] | 1393 | } else { |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1394 | size += dump_elem(out, child, level, 0, 0); |
Pavol Vican | be7eef5 | 2015-10-22 14:07:48 +0200 | [diff] [blame] | 1395 | } |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1396 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1397 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1398 | close: |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1399 | /* closing tag */ |
| 1400 | if (e->ns && e->ns->prefix) { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1401 | size += ly_print(out, "%*s</%s:%s>%s", indent, "", e->ns->prefix, e->name, delim_outer); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1402 | } else { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1403 | size += ly_print(out, "%*s</%s>%s", indent, "", e->name, delim_outer); |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1404 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1405 | |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1406 | return size; |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1407 | } |
| 1408 | |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1409 | static int |
| 1410 | dump_siblings(struct lyout *out, const struct lyxml_elem *e, int options) |
| 1411 | { |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1412 | const struct lyxml_elem *start, *iter, *next; |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1413 | int ret = 0; |
| 1414 | |
| 1415 | if (e->parent) { |
| 1416 | start = e->parent->child; |
| 1417 | } else { |
| 1418 | start = e; |
| 1419 | while(start->prev && start->prev->next) { |
| 1420 | start = start->prev; |
| 1421 | } |
| 1422 | } |
| 1423 | |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1424 | LY_TREE_FOR_SAFE(start, next, iter) { |
| 1425 | ret += dump_elem(out, iter, 0, options, (next ? 0 : 1)); |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | return ret; |
| 1429 | } |
| 1430 | |
Radek Krejci | c6704c8 | 2015-10-06 11:12:45 +0200 | [diff] [blame] | 1431 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1432 | lyxml_print_file(FILE *stream, const struct lyxml_elem *elem, int options) |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1433 | { |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1434 | struct lyout out; |
| 1435 | |
| 1436 | if (!stream || !elem) { |
Radek Krejci | 6e4ffbb | 2015-06-16 10:34:41 +0200 | [diff] [blame] | 1437 | return 0; |
| 1438 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1439 | |
Michal Vasko | 002db14 | 2018-07-03 13:52:59 +0200 | [diff] [blame] | 1440 | memset(&out, 0, sizeof out); |
| 1441 | |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1442 | out.type = LYOUT_STREAM; |
| 1443 | out.method.f = stream; |
| 1444 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1445 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1446 | return dump_siblings(&out, elem, options); |
| 1447 | } else { |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1448 | return dump_elem(&out, elem, 0, options, 1); |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1449 | } |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1450 | } |
| 1451 | |
| 1452 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1453 | lyxml_print_fd(int fd, const struct lyxml_elem *elem, int options) |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1454 | { |
| 1455 | struct lyout out; |
| 1456 | |
| 1457 | if (fd < 0 || !elem) { |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
Michal Vasko | 002db14 | 2018-07-03 13:52:59 +0200 | [diff] [blame] | 1461 | memset(&out, 0, sizeof out); |
| 1462 | |
Radek Krejci | 5248f13 | 2015-10-09 10:34:25 +0200 | [diff] [blame] | 1463 | out.type = LYOUT_FD; |
| 1464 | out.method.fd = fd; |
| 1465 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1466 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1467 | return dump_siblings(&out, elem, options); |
| 1468 | } else { |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1469 | return dump_elem(&out, elem, 0, options, 1); |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1470 | } |
Radek Krejci | f0023a9 | 2015-04-20 20:51:39 +0200 | [diff] [blame] | 1471 | } |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1472 | |
| 1473 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1474 | lyxml_print_mem(char **strp, const struct lyxml_elem *elem, int options) |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame] | 1475 | { |
| 1476 | struct lyout out; |
| 1477 | int r; |
| 1478 | |
| 1479 | if (!strp || !elem) { |
| 1480 | return 0; |
| 1481 | } |
| 1482 | |
Michal Vasko | 002db14 | 2018-07-03 13:52:59 +0200 | [diff] [blame] | 1483 | memset(&out, 0, sizeof out); |
| 1484 | |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame] | 1485 | out.type = LYOUT_MEMORY; |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame] | 1486 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1487 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1488 | r = dump_siblings(&out, elem, options); |
| 1489 | } else { |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1490 | r = dump_elem(&out, elem, 0, options, 1); |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1491 | } |
Radek Krejci | 2fa0fc1 | 2015-10-14 18:14:29 +0200 | [diff] [blame] | 1492 | |
| 1493 | *strp = out.method.mem.buf; |
| 1494 | return r; |
| 1495 | } |
| 1496 | |
| 1497 | API int |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1498 | lyxml_print_clb(ssize_t (*writeclb)(void *arg, const void *buf, size_t count), void *arg, const struct lyxml_elem *elem, int options) |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1499 | { |
| 1500 | struct lyout out; |
| 1501 | |
| 1502 | if (!writeclb || !elem) { |
| 1503 | return 0; |
| 1504 | } |
| 1505 | |
Michal Vasko | 002db14 | 2018-07-03 13:52:59 +0200 | [diff] [blame] | 1506 | memset(&out, 0, sizeof out); |
| 1507 | |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1508 | out.type = LYOUT_CALLBACK; |
Radek Krejci | 50929eb | 2015-10-09 18:14:15 +0200 | [diff] [blame] | 1509 | out.method.clb.f = writeclb; |
| 1510 | out.method.clb.arg = arg; |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1511 | |
Radek Krejci | 722b007 | 2016-02-01 17:09:45 +0100 | [diff] [blame] | 1512 | if (options & LYXML_PRINT_SIBLINGS) { |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1513 | return dump_siblings(&out, elem, options); |
| 1514 | } else { |
Michal Vasko | b2f1db7 | 2016-11-16 13:57:35 +0100 | [diff] [blame] | 1515 | return dump_elem(&out, elem, 0, options, 1); |
Radek Krejci | 8c56a5a | 2015-12-16 15:10:28 +0100 | [diff] [blame] | 1516 | } |
Radek Krejci | 6140e4e | 2015-10-09 15:50:55 +0200 | [diff] [blame] | 1517 | } |