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