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