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