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