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