ezxml.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035
  1. /* ezxml.c
  2. *
  3. * Copyright 2004-2006 Aaron Voisine <aaron@voisine.org>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. #define EZXML_NOMMAP
  25. #include <rtthread.h>
  26. #if RT_VER_NUM >= 0x40100
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #else
  32. #include <dfs_posix.h>
  33. #endif /*RT_VER_NUM >= 0x40100*/
  34. #if RT_VER_NUM >= 0x40101
  35. #include <posix/string.h>
  36. #else
  37. #define isspace __isspace_ascii
  38. int __isspace_ascii(int ch)
  39. {
  40. return (unsigned int)(ch - 9) < 5u || ch == ' ';
  41. }
  42. #endif /* RT_VER_NUM >= 0x40101 */
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <stdarg.h>
  46. #include <string.h>
  47. #include <ctype.h>
  48. #ifndef EZXML_NOMMAP
  49. #include <sys/mman.h>
  50. #endif // EZXML_NOMMAP
  51. #include "ezxml.h"
  52. #define EZXML_WS "\t\r\n " // whitespace
  53. #define EZXML_ERRL 128 // maximum error string length
  54. typedef struct ezxml_root *ezxml_root_t;
  55. struct ezxml_root { // additional data for the root tag
  56. struct ezxml xml; // is a super-struct built on top of ezxml struct
  57. ezxml_t cur; // current xml tree insertion point
  58. char *m; // original xml string
  59. size_t len; // length of allocated memory for mmap, -1 for malloc
  60. char *u; // UTF-8 conversion of string if original was UTF-16
  61. char *s; // start of work area
  62. char *e; // end of work area
  63. char **ent; // general entities (ampersand sequences)
  64. char ***attr; // default attributes
  65. char ***pi; // processing instructions
  66. short standalone; // non-zero if <?xml standalone="yes"?>
  67. char err[EZXML_ERRL]; // error string
  68. };
  69. char *EZXML_NIL[] = { NULL }; // empty, null terminated array of strings
  70. // returns the first child tag with the given name or NULL if not found
  71. ezxml_t ezxml_child(ezxml_t xml, const char *name)
  72. {
  73. xml = (xml) ? xml->child : NULL;
  74. while (xml && strcmp(name, xml->name)) xml = xml->sibling;
  75. return xml;
  76. }
  77. // returns the Nth tag with the same name in the same subsection or NULL if not
  78. // found
  79. ezxml_t ezxml_idx(ezxml_t xml, int idx)
  80. {
  81. for (; xml && idx; idx--) xml = xml->next;
  82. return xml;
  83. }
  84. // returns the value of the requested tag attribute or NULL if not found
  85. const char *ezxml_attr(ezxml_t xml, const char *attr)
  86. {
  87. int i = 0, j = 1;
  88. ezxml_root_t root = (ezxml_root_t)xml;
  89. if (! xml || ! xml->attr) return NULL;
  90. while (xml->attr[i] && strcmp(attr, xml->attr[i])) i += 2;
  91. if (xml->attr[i]) return xml->attr[i + 1]; // found attribute
  92. while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
  93. for (i = 0; root->attr[i] && strcmp(xml->name, root->attr[i][0]); i++);
  94. if (! root->attr[i]) return NULL; // no matching default attributes
  95. while (root->attr[i][j] && strcmp(attr, root->attr[i][j])) j += 3;
  96. return (root->attr[i][j]) ? root->attr[i][j + 1] : NULL; // found default
  97. }
  98. // same as ezxml_get but takes an already initialized va_list
  99. ezxml_t ezxml_vget(ezxml_t xml, va_list ap)
  100. {
  101. char *name = va_arg(ap, char *);
  102. int idx = -1;
  103. if (name && *name) {
  104. idx = va_arg(ap, int);
  105. xml = ezxml_child(xml, name);
  106. }
  107. return (idx < 0) ? xml : ezxml_vget(ezxml_idx(xml, idx), ap);
  108. }
  109. // Traverses the xml tree to retrieve a specific subtag. Takes a variable
  110. // length list of tag names and indexes. The argument list must be terminated
  111. // by either an index of -1 or an empty string tag name. Example:
  112. // title = ezxml_get(library, "shelf", 0, "book", 2, "title", -1);
  113. // This retrieves the title of the 3rd book on the 1st shelf of library.
  114. // Returns NULL if not found.
  115. ezxml_t ezxml_get(ezxml_t xml, ...)
  116. {
  117. va_list ap;
  118. ezxml_t r;
  119. va_start(ap, xml);
  120. r = ezxml_vget(xml, ap);
  121. va_end(ap);
  122. return r;
  123. }
  124. // returns a null terminated array of processing instructions for the given
  125. // target
  126. const char **ezxml_pi(ezxml_t xml, const char *target)
  127. {
  128. ezxml_root_t root = (ezxml_root_t)xml;
  129. int i = 0;
  130. if (! root) return (const char **)EZXML_NIL;
  131. while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
  132. while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
  133. return (const char **)((root->pi[i]) ? root->pi[i] + 1 : EZXML_NIL);
  134. }
  135. // set an error string and return root
  136. ezxml_t ezxml_err(ezxml_root_t root, char *s, const char *err, ...)
  137. {
  138. va_list ap;
  139. int line = 1;
  140. char *t, fmt[EZXML_ERRL];
  141. for (t = root->s; t < s; t++) if (*t == '\n') line++;
  142. snprintf(fmt, EZXML_ERRL, "[error near line %d]: %s", line, err);
  143. va_start(ap, err);
  144. vsnprintf(root->err, EZXML_ERRL, fmt, ap);
  145. va_end(ap);
  146. return &root->xml;
  147. }
  148. // Recursively decodes entity and character references and normalizes new lines
  149. // ent is a null terminated array of alternating entity names and values. set t
  150. // to '&' for general entity decoding, '%' for parameter entity decoding, 'c'
  151. // for cdata sections, ' ' for attribute normalization, or '*' for non-cdata
  152. // attribute normalization. Returns s, or if the decoded string is longer than
  153. // s, returns a malloced string that must be freed.
  154. char *ezxml_decode(char *s, char **ent, char t)
  155. {
  156. char *e, *r = s, *m = s;
  157. long b, c, d, l;
  158. for (; *s; s++) { // normalize line endings
  159. while (*s == '\r') {
  160. *(s++) = '\n';
  161. if (*s == '\n') memmove(s, (s + 1), strlen(s));
  162. }
  163. }
  164. for (s = r; ; ) {
  165. while (*s && *s != '&' && (*s != '%' || t != '%') && !isspace(*s)) s++;
  166. if (! *s) break;
  167. else if (t != 'c' && ! strncmp(s, "&#", 2)) { // character reference
  168. if (s[2] == 'x') c = strtol(s + 3, &e, 16); // base 16
  169. else c = strtol(s + 2, &e, 10); // base 10
  170. if (! c || *e != ';') { s++; continue; } // not a character ref
  171. if (c < 0x80) *(s++) = c; // US-ASCII subset
  172. else { // multi-byte UTF-8 sequence
  173. for (b = 0, d = c; d; d /= 2) b++; // number of bits in c
  174. b = (b - 2) / 5; // number of bytes in payload
  175. *(s++) = (0xFF << (7 - b)) | (c >> (6 * b)); // head
  176. while (b) *(s++) = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
  177. }
  178. memmove(s, strchr(s, ';') + 1, strlen(strchr(s, ';')));
  179. }
  180. else if ((*s == '&' && (t == '&' || t == ' ' || t == '*')) ||
  181. (*s == '%' && t == '%')) { // entity reference
  182. for (b = 0; ent[b] && strncmp(s + 1, ent[b], strlen(ent[b]));
  183. b += 2); // find entity in entity list
  184. if (ent[b++]) { // found a match
  185. if ((c = strlen(ent[b])) - 1 > (e = strchr(s, ';')) - s) {
  186. l = (d = (s - r)) + c + strlen(e); // new length
  187. r = (r == m) ? strcpy(malloc(l), r) : realloc(r, l);
  188. e = strchr((s = r + d), ';'); // fix up pointers
  189. }
  190. memmove(s + c, e + 1, strlen(e)); // shift rest of string
  191. strncpy(s, ent[b], c); // copy in replacement text
  192. }
  193. else s++; // not a known entity
  194. }
  195. else if ((t == ' ' || t == '*') && isspace(*s)) *(s++) = ' ';
  196. else s++; // no decoding needed
  197. }
  198. if (t == '*') { // normalize spaces for non-cdata attributes
  199. for (s = r; *s; s++) {
  200. if ((l = strspn(s, " "))) memmove(s, s + l, strlen(s + l) + 1);
  201. while (*s && *s != ' ') s++;
  202. }
  203. if (--s >= r && *s == ' ') *s = '\0'; // trim any trailing space
  204. }
  205. return r;
  206. }
  207. // called when parser finds start of new tag
  208. void ezxml_open_tag(ezxml_root_t root, char *name, char **attr)
  209. {
  210. ezxml_t xml = root->cur;
  211. if (xml->name) xml = ezxml_add_child(xml, name, strlen(xml->txt));
  212. else xml->name = name; // first open tag
  213. xml->attr = attr;
  214. root->cur = xml; // update tag insertion point
  215. }
  216. // called when parser finds character content between open and closing tag
  217. void ezxml_char_content(ezxml_root_t root, char *s, size_t len, char t)
  218. {
  219. ezxml_t xml = root->cur;
  220. char *m = s;
  221. size_t l;
  222. if (! xml || ! xml->name || ! len) return; // sanity check
  223. s[len] = '\0'; // null terminate text (calling functions anticipate this)
  224. len = strlen(s = ezxml_decode(s, root->ent, t)) + 1;
  225. if (! *(xml->txt)) xml->txt = s; // initial character content
  226. else { // allocate our own memory and make a copy
  227. xml->txt = (xml->flags & EZXML_TXTM) // allocate some space
  228. ? realloc(xml->txt, (l = strlen(xml->txt)) + len)
  229. : strcpy(malloc((l = strlen(xml->txt)) + len), xml->txt);
  230. strcpy(xml->txt + l, s); // add new char content
  231. if (s != m) free(s); // free s if it was malloced by ezxml_decode()
  232. }
  233. if (xml->txt != m) ezxml_set_flag(xml, EZXML_TXTM);
  234. }
  235. // called when parser finds closing tag
  236. ezxml_t ezxml_close_tag(ezxml_root_t root, char *name, char *s)
  237. {
  238. if (! root->cur || ! root->cur->name || strcmp(name, root->cur->name))
  239. return ezxml_err(root, s, "unexpected closing tag </%s>", name);
  240. root->cur = root->cur->parent;
  241. return NULL;
  242. }
  243. // checks for circular entity references, returns non-zero if no circular
  244. // references are found, zero otherwise
  245. int ezxml_ent_ok(char *name, char *s, char **ent)
  246. {
  247. int i;
  248. for (; ; s++) {
  249. while (*s && *s != '&') s++; // find next entity reference
  250. if (! *s) return 1;
  251. if (! strncmp(s + 1, name, strlen(name))) return 0; // circular ref.
  252. for (i = 0; ent[i] && strncmp(ent[i], s + 1, strlen(ent[i])); i += 2);
  253. if (ent[i] && ! ezxml_ent_ok(name, ent[i + 1], ent)) return 0;
  254. }
  255. }
  256. // called when the parser finds a processing instruction
  257. void ezxml_proc_inst(ezxml_root_t root, char *s, size_t len)
  258. {
  259. int i = 0, j = 1;
  260. char *target = s;
  261. s[len] = '\0'; // null terminate instruction
  262. if (*(s += strcspn(s, EZXML_WS))) {
  263. *s = '\0'; // null terminate target
  264. s += strspn(s + 1, EZXML_WS) + 1; // skip whitespace after target
  265. }
  266. if (! strcmp(target, "xml")) { // <?xml ... ?>
  267. if ((s = strstr(s, "standalone")) && ! strncmp(s + strspn(s + 10,
  268. EZXML_WS "='\"") + 10, "yes", 3)) root->standalone = 1;
  269. return;
  270. }
  271. if (! root->pi[0]) *(root->pi = malloc(sizeof(char **))) = NULL; //first pi
  272. while (root->pi[i] && strcmp(target, root->pi[i][0])) i++; // find target
  273. if (! root->pi[i]) { // new target
  274. root->pi = realloc(root->pi, sizeof(char **) * (i + 2));
  275. root->pi[i] = malloc(sizeof(char *) * 3);
  276. root->pi[i][0] = target;
  277. root->pi[i][1] = (char *)(root->pi[i + 1] = NULL); // terminate pi list
  278. root->pi[i][2] = strdup(""); // empty document position list
  279. }
  280. while (root->pi[i][j]) j++; // find end of instruction list for this target
  281. root->pi[i] = realloc(root->pi[i], sizeof(char *) * (j + 3));
  282. root->pi[i][j + 2] = realloc(root->pi[i][j + 1], j + 1);
  283. strcpy(root->pi[i][j + 2] + j - 1, (root->xml.name) ? ">" : "<");
  284. root->pi[i][j + 1] = NULL; // null terminate pi list for this target
  285. root->pi[i][j] = s; // set instruction
  286. }
  287. // called when the parser finds an internal doctype subset
  288. short ezxml_internal_dtd(ezxml_root_t root, char *s, size_t len)
  289. {
  290. char q, *c, *t, *n = NULL, *v, **ent, **pe;
  291. int i, j;
  292. pe = memcpy(malloc(sizeof(EZXML_NIL)), EZXML_NIL, sizeof(EZXML_NIL));
  293. for (s[len] = '\0'; s; ) {
  294. while (*s && *s != '<' && *s != '%') s++; // find next declaration
  295. if (! *s) break;
  296. else if (! strncmp(s, "<!ENTITY", 8)) { // parse entity definitions
  297. c = s += strspn(s + 8, EZXML_WS) + 8; // skip white space separator
  298. n = s + strspn(s, EZXML_WS "%"); // find name
  299. *(s = n + strcspn(n, EZXML_WS)) = ';'; // append ; to name
  300. v = s + strspn(s + 1, EZXML_WS) + 1; // find value
  301. if ((q = *(v++)) != '"' && q != '\'') { // skip externals
  302. s = strchr(s, '>');
  303. continue;
  304. }
  305. for (i = 0, ent = (*c == '%') ? pe : root->ent; ent[i]; i++);
  306. ent = realloc(ent, (i + 3) * sizeof(char *)); // space for next ent
  307. if (*c == '%') pe = ent;
  308. else root->ent = ent;
  309. *(++s) = '\0'; // null terminate name
  310. if ((s = strchr(v, q))) *(s++) = '\0'; // null terminate value
  311. ent[i + 1] = ezxml_decode(v, pe, '%'); // set value
  312. ent[i + 2] = NULL; // null terminate entity list
  313. if (! ezxml_ent_ok(n, ent[i + 1], ent)) { // circular reference
  314. if (ent[i + 1] != v) free(ent[i + 1]);
  315. ezxml_err(root, v, "circular entity declaration &%s", n);
  316. break;
  317. }
  318. else ent[i] = n; // set entity name
  319. }
  320. else if (! strncmp(s, "<!ATTLIST", 9)) { // parse default attributes
  321. t = s + strspn(s + 9, EZXML_WS) + 9; // skip whitespace separator
  322. if (! *t) { ezxml_err(root, t, "unclosed <!ATTLIST"); break; }
  323. if (*(s = t + strcspn(t, EZXML_WS ">")) == '>') continue;
  324. else *s = '\0'; // null terminate tag name
  325. for (i = 0; root->attr[i] && strcmp(n, root->attr[i][0]); i++);
  326. while (*(n = ++s + strspn(s, EZXML_WS)) && *n != '>') {
  327. if (*(s = n + strcspn(n, EZXML_WS))) *s = '\0'; // attr name
  328. else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
  329. s += strspn(s + 1, EZXML_WS) + 1; // find next token
  330. c = (strncmp(s, "CDATA", 5)) ? "*" : " "; // is it cdata?
  331. if (! strncmp(s, "NOTATION", 8))
  332. s += strspn(s + 8, EZXML_WS) + 8;
  333. s = (*s == '(') ? strchr(s, ')') : s + strcspn(s, EZXML_WS);
  334. if (! s) { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
  335. s += strspn(s, EZXML_WS ")"); // skip white space separator
  336. if (! strncmp(s, "#FIXED", 6))
  337. s += strspn(s + 6, EZXML_WS) + 6;
  338. if (*s == '#') { // no default value
  339. s += strcspn(s, EZXML_WS ">") - 1;
  340. if (*c == ' ') continue; // cdata is default, nothing to do
  341. v = NULL;
  342. }
  343. else if ((*s == '"' || *s == '\'') && // default value
  344. (s = strchr(v = s + 1, *s))) *s = '\0';
  345. else { ezxml_err(root, t, "malformed <!ATTLIST"); break; }
  346. if (! root->attr[i]) { // new tag name
  347. root->attr = (! i) ? malloc(2 * sizeof(char **))
  348. : realloc(root->attr,
  349. (i + 2) * sizeof(char **));
  350. root->attr[i] = malloc(2 * sizeof(char *));
  351. root->attr[i][0] = t; // set tag name
  352. root->attr[i][1] = (char *)(root->attr[i + 1] = NULL);
  353. }
  354. for (j = 1; root->attr[i][j]; j += 3); // find end of list
  355. root->attr[i] = realloc(root->attr[i],
  356. (j + 4) * sizeof(char *));
  357. root->attr[i][j + 3] = NULL; // null terminate list
  358. root->attr[i][j + 2] = c; // is it cdata?
  359. root->attr[i][j + 1] = (v) ? ezxml_decode(v, root->ent, *c)
  360. : NULL;
  361. root->attr[i][j] = n; // attribute name
  362. }
  363. }
  364. else if (! strncmp(s, "<!--", 4)) s = strstr(s + 4, "-->"); // comments
  365. else if (! strncmp(s, "<?", 2)) { // processing instructions
  366. if ((s = strstr(c = s + 2, "?>")))
  367. ezxml_proc_inst(root, c, s++ - c);
  368. }
  369. else if (*s == '<') s = strchr(s, '>'); // skip other declarations
  370. else if (*(s++) == '%' && ! root->standalone) break;
  371. }
  372. free(pe);
  373. return ! *root->err;
  374. }
  375. // Converts a UTF-16 string to UTF-8. Returns a new string that must be freed
  376. // or NULL if no conversion was needed.
  377. char *ezxml_str2utf8(char **s, size_t *len)
  378. {
  379. char *u;
  380. size_t l = 0, sl, max = *len;
  381. long c, d;
  382. int b, be = (**s == '\xFE') ? 1 : (**s == '\xFF') ? 0 : -1;
  383. if (be == -1) return NULL; // not UTF-16
  384. u = malloc(max);
  385. for (sl = 2; sl < *len - 1; sl += 2) {
  386. c = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF) //UTF-16BE
  387. : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF); //UTF-16LE
  388. if (c >= 0xD800 && c <= 0xDFFF && (sl += 2) < *len - 1) { // high-half
  389. d = (be) ? (((*s)[sl] & 0xFF) << 8) | ((*s)[sl + 1] & 0xFF)
  390. : (((*s)[sl + 1] & 0xFF) << 8) | ((*s)[sl] & 0xFF);
  391. c = (((c & 0x3FF) << 10) | (d & 0x3FF)) + 0x10000;
  392. }
  393. while (l + 6 > max) u = realloc(u, max += EZXML_BUFSIZE);
  394. if (c < 0x80) u[l++] = c; // US-ASCII subset
  395. else { // multi-byte UTF-8 sequence
  396. for (b = 0, d = c; d; d /= 2) b++; // bits in c
  397. b = (b - 2) / 5; // bytes in payload
  398. u[l++] = (0xFF << (7 - b)) | (c >> (6 * b)); // head
  399. while (b) u[l++] = 0x80 | ((c >> (6 * --b)) & 0x3F); // payload
  400. }
  401. }
  402. return *s = realloc(u, *len = l);
  403. }
  404. // frees a tag attribute list
  405. void ezxml_free_attr(char **attr) {
  406. int i = 0;
  407. char *m;
  408. if (! attr || attr == EZXML_NIL) return; // nothing to free
  409. while (attr[i]) i += 2; // find end of attribute list
  410. m = attr[i + 1]; // list of which names and values are malloced
  411. for (i = 0; m[i]; i++) {
  412. if (m[i] & EZXML_NAMEM) free(attr[i * 2]);
  413. if (m[i] & EZXML_TXTM) free(attr[(i * 2) + 1]);
  414. }
  415. free(m);
  416. free(attr);
  417. }
  418. // parse the given xml string and return an ezxml structure
  419. ezxml_t ezxml_parse_str(char *s, size_t len)
  420. {
  421. ezxml_root_t root = (ezxml_root_t)ezxml_new(NULL);
  422. char q, e, *d, **attr, **a = NULL; // initialize a to avoid compile warning
  423. int l, i, j;
  424. root->m = s;
  425. if (! len) return ezxml_err(root, NULL, "root tag missing");
  426. root->u = ezxml_str2utf8(&s, &len); // convert utf-16 to utf-8
  427. root->e = (root->s = s) + len; // record start and end of work area
  428. e = s[len - 1]; // save end char
  429. s[len - 1] = '\0'; // turn end char into null terminator
  430. while (*s && *s != '<') s++; // find first tag
  431. if (! *s) return ezxml_err(root, s, "root tag missing");
  432. for (; ; ) {
  433. attr = (char **)EZXML_NIL;
  434. d = ++s;
  435. if (isalpha(*s) || *s == '_' || *s == ':' || *s < '\0') { // new tag
  436. if (! root->cur)
  437. return ezxml_err(root, d, "markup outside of root element");
  438. s += strcspn(s, EZXML_WS "/>");
  439. while (isspace(*s)) *(s++) = '\0'; // null terminate tag name
  440. if (*s && *s != '/' && *s != '>') // find tag in default attr list
  441. for (i = 0; (a = root->attr[i]) && strcmp(a[0], d); i++);
  442. for (l = 0; *s && *s != '/' && *s != '>'; l += 2) { // new attrib
  443. attr = (l) ? realloc(attr, (l + 4) * sizeof(char *))
  444. : malloc(4 * sizeof(char *)); // allocate space
  445. attr[l + 3] = (l) ? realloc(attr[l + 1], (l / 2) + 2)
  446. : malloc(2); // mem for list of maloced vals
  447. strcpy(attr[l + 3] + (l / 2), " "); // value is not malloced
  448. attr[l + 2] = NULL; // null terminate list
  449. attr[l + 1] = ""; // temporary attribute value
  450. attr[l] = s; // set attribute name
  451. s += strcspn(s, EZXML_WS "=/>");
  452. if (*s == '=' || isspace(*s)) {
  453. *(s++) = '\0'; // null terminate tag attribute name
  454. q = *(s += strspn(s, EZXML_WS "="));
  455. if (q == '"' || q == '\'') { // attribute value
  456. attr[l + 1] = ++s;
  457. while (*s && *s != q) s++;
  458. if (*s) *(s++) = '\0'; // null terminate attribute val
  459. else {
  460. ezxml_free_attr(attr);
  461. return ezxml_err(root, d, "missing %c", q);
  462. }
  463. for (j = 1; a && a[j] && strcmp(a[j], attr[l]); j +=3);
  464. attr[l + 1] = ezxml_decode(attr[l + 1], root->ent, (a
  465. && a[j]) ? *a[j + 2] : ' ');
  466. if (attr[l + 1] < d || attr[l + 1] > s)
  467. attr[l + 3][l / 2] = EZXML_TXTM; // value malloced
  468. }
  469. }
  470. while (isspace(*s)) s++;
  471. }
  472. if (*s == '/') { // self closing tag
  473. *(s++) = '\0';
  474. if ((*s && *s != '>') || (! *s && e != '>')) {
  475. if (l) ezxml_free_attr(attr);
  476. return ezxml_err(root, d, "missing >");
  477. }
  478. ezxml_open_tag(root, d, attr);
  479. ezxml_close_tag(root, d, s);
  480. }
  481. else if ((q = *s) == '>' || (! *s && e == '>')) { // open tag
  482. *s = '\0'; // temporarily null terminate tag name
  483. ezxml_open_tag(root, d, attr);
  484. *s = q;
  485. }
  486. else {
  487. if (l) ezxml_free_attr(attr);
  488. return ezxml_err(root, d, "missing >");
  489. }
  490. }
  491. else if (*s == '/') { // close tag
  492. s += strcspn(d = s + 1, EZXML_WS ">") + 1;
  493. if (! (q = *s) && e != '>') return ezxml_err(root, d, "missing >");
  494. *s = '\0'; // temporarily null terminate tag name
  495. if (ezxml_close_tag(root, d, s)) return &root->xml;
  496. if (isspace(*s = q)) s += strspn(s, EZXML_WS);
  497. }
  498. else if (! strncmp(s, "!--", 3)) { // xml comment
  499. if (! (s = strstr(s + 3, "--")) || (*(s += 2) != '>' && *s) ||
  500. (! *s && e != '>')) return ezxml_err(root, d, "unclosed <!--");
  501. }
  502. else if (! strncmp(s, "![CDATA[", 8)) { // cdata
  503. if ((s = strstr(s, "]]>")))
  504. ezxml_char_content(root, d + 8, (s += 2) - d - 10, 'c');
  505. else return ezxml_err(root, d, "unclosed <![CDATA[");
  506. }
  507. else if (! strncmp(s, "!DOCTYPE", 8)) { // dtd
  508. for (l = 0; *s && ((! l && *s != '>') || (l && (*s != ']' ||
  509. *(s + strspn(s + 1, EZXML_WS) + 1) != '>')));
  510. l = (*s == '[') ? 1 : l) s += strcspn(s + 1, "[]>") + 1;
  511. if (! *s && e != '>')
  512. return ezxml_err(root, d, "unclosed <!DOCTYPE");
  513. d = (l) ? strchr(d, '[') + 1 : d;
  514. if (l && ! ezxml_internal_dtd(root, d, s++ - d)) return &root->xml;
  515. }
  516. else if (*s == '?') { // <?...?> processing instructions
  517. do { s = strchr(s, '?'); } while (s && *(++s) && *s != '>');
  518. if (! s || (! *s && e != '>'))
  519. return ezxml_err(root, d, "unclosed <?");
  520. else ezxml_proc_inst(root, d + 1, s - d - 2);
  521. }
  522. else return ezxml_err(root, d, "unexpected <");
  523. if (! s || ! *s) break;
  524. *s = '\0';
  525. d = ++s;
  526. if (*s && *s != '<') { // tag character content
  527. while (*s && *s != '<') s++;
  528. if (*s) ezxml_char_content(root, d, s - d, '&');
  529. else break;
  530. }
  531. else if (! *s) break;
  532. }
  533. if (! root->cur) return &root->xml;
  534. else if (! root->cur->name) return ezxml_err(root, d, "root tag missing");
  535. else return ezxml_err(root, d, "unclosed tag <%s>", root->cur->name);
  536. }
  537. // Wrapper for ezxml_parse_str() that accepts a file stream. Reads the entire
  538. // stream into memory and then parses it. For xml files, use ezxml_parse_file()
  539. // or ezxml_parse_fd()
  540. ezxml_t ezxml_parse_fp(FILE *fp)
  541. {
  542. ezxml_root_t root;
  543. size_t l, len = 0;
  544. char *s;
  545. if (! (s = malloc(EZXML_BUFSIZE))) return NULL;
  546. do {
  547. len += (l = fread((s + len), 1, EZXML_BUFSIZE, fp));
  548. if (l == EZXML_BUFSIZE) s = realloc(s, len + EZXML_BUFSIZE);
  549. } while (s && l == EZXML_BUFSIZE);
  550. if (! s) return NULL;
  551. root = (ezxml_root_t)ezxml_parse_str(s, len);
  552. root->len = -1; // so we know to free s in ezxml_free()
  553. return &root->xml;
  554. }
  555. // A wrapper for ezxml_parse_str() that accepts a file descriptor. First
  556. // attempts to mem map the file. Failing that, reads the file into memory.
  557. // Returns NULL on failure.
  558. ezxml_t ezxml_parse_fd(int fd)
  559. {
  560. ezxml_root_t root;
  561. struct stat st;
  562. size_t l;
  563. void *m;
  564. if (fd < 0) return NULL;
  565. fstat(fd, &st);
  566. #ifndef EZXML_NOMMAP
  567. l = (st.st_size + sysconf(_SC_PAGESIZE) - 1) & ~(sysconf(_SC_PAGESIZE) -1);
  568. if ((m = mmap(NULL, l, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0)) !=
  569. MAP_FAILED) {
  570. madvise(m, l, MADV_SEQUENTIAL); // optimize for sequential access
  571. root = (ezxml_root_t)ezxml_parse_str(m, st.st_size);
  572. madvise(m, root->len = l, MADV_NORMAL); // put it back to normal
  573. }
  574. else { // mmap failed, read file into memory
  575. #endif // EZXML_NOMMAP
  576. l = read(fd, m = malloc(st.st_size), st.st_size);
  577. root = (ezxml_root_t)ezxml_parse_str(m, l);
  578. root->len = -1; // so we know to free s in ezxml_free()
  579. #ifndef EZXML_NOMMAP
  580. }
  581. #endif // EZXML_NOMMAP
  582. return &root->xml;
  583. }
  584. // a wrapper for ezxml_parse_fd that accepts a file name
  585. ezxml_t ezxml_parse_file(const char *file)
  586. {
  587. int fd = open(file, O_RDONLY, 0);
  588. ezxml_t xml = ezxml_parse_fd(fd);
  589. if (fd >= 0) close(fd);
  590. return xml;
  591. }
  592. // Encodes ampersand sequences appending the results to *dst, reallocating *dst
  593. // if length excedes max. a is non-zero for attribute encoding. Returns *dst
  594. char *ezxml_ampencode(const char *s, size_t len, char **dst, size_t *dlen,
  595. size_t *max, short a)
  596. {
  597. const char *e;
  598. for (e = s + len; s != e; s++) {
  599. while (*dlen + 10 > *max) *dst = realloc(*dst, *max += EZXML_BUFSIZE);
  600. switch (*s) {
  601. case '\0': return *dst;
  602. case '&': *dlen += sprintf(*dst + *dlen, "&amp;"); break;
  603. case '<': *dlen += sprintf(*dst + *dlen, "&lt;"); break;
  604. case '>': *dlen += sprintf(*dst + *dlen, "&gt;"); break;
  605. case '"': *dlen += sprintf(*dst + *dlen, (a) ? "&quot;" : "\""); break;
  606. case '\n': *dlen += sprintf(*dst + *dlen, (a) ? "&#xA;" : "\n"); break;
  607. case '\t': *dlen += sprintf(*dst + *dlen, (a) ? "&#x9;" : "\t"); break;
  608. case '\r': *dlen += sprintf(*dst + *dlen, "&#xD;"); break;
  609. default: (*dst)[(*dlen)++] = *s;
  610. }
  611. }
  612. return *dst;
  613. }
  614. // Recursively converts each tag to xml appending it to *s. Reallocates *s if
  615. // its length excedes max. start is the location of the previous tag in the
  616. // parent tag's character content. Returns *s.
  617. char *ezxml_toxml_r(ezxml_t xml, char **s, size_t *len, size_t *max,
  618. size_t start, char ***attr)
  619. {
  620. int i, j;
  621. char *txt = (xml->parent) ? xml->parent->txt : "";
  622. size_t off = 0;
  623. // parent character content up to this tag
  624. *s = ezxml_ampencode(txt + start, xml->off - start, s, len, max, 0);
  625. while (*len + strlen(xml->name) + 4 > *max) // reallocate s
  626. *s = realloc(*s, *max += EZXML_BUFSIZE);
  627. *len += sprintf(*s + *len, "<%s", xml->name); // open tag
  628. for (i = 0; xml->attr[i]; i += 2) { // tag attributes
  629. if (ezxml_attr(xml, xml->attr[i]) != xml->attr[i + 1]) continue;
  630. while (*len + strlen(xml->attr[i]) + 7 > *max) // reallocate s
  631. *s = realloc(*s, *max += EZXML_BUFSIZE);
  632. *len += sprintf(*s + *len, " %s=\"", xml->attr[i]);
  633. ezxml_ampencode(xml->attr[i + 1], -1, s, len, max, 1);
  634. *len += sprintf(*s + *len, "\"");
  635. }
  636. for (i = 0; attr[i] && strcmp(attr[i][0], xml->name); i++);
  637. for (j = 1; attr[i] && attr[i][j]; j += 3) { // default attributes
  638. if (! attr[i][j + 1] || ezxml_attr(xml, attr[i][j]) != attr[i][j + 1])
  639. continue; // skip duplicates and non-values
  640. while (*len + strlen(attr[i][j]) + 7 > *max) // reallocate s
  641. *s = realloc(*s, *max += EZXML_BUFSIZE);
  642. *len += sprintf(*s + *len, " %s=\"", attr[i][j]);
  643. ezxml_ampencode(attr[i][j + 1], -1, s, len, max, 1);
  644. *len += sprintf(*s + *len, "\"");
  645. }
  646. *len += sprintf(*s + *len, ">");
  647. *s = (xml->child) ? ezxml_toxml_r(xml->child, s, len, max, 0, attr) //child
  648. : ezxml_ampencode(xml->txt, -1, s, len, max, 0); //data
  649. while (*len + strlen(xml->name) + 4 > *max) // reallocate s
  650. *s = realloc(*s, *max += EZXML_BUFSIZE);
  651. *len += sprintf(*s + *len, "</%s>", xml->name); // close tag
  652. while (txt[off] && off < xml->off) off++; // make sure off is within bounds
  653. return (xml->ordered) ? ezxml_toxml_r(xml->ordered, s, len, max, off, attr)
  654. : ezxml_ampencode(txt + off, -1, s, len, max, 0);
  655. }
  656. // Converts an ezxml structure back to xml. Returns a string of xml data that
  657. // must be freed.
  658. char *ezxml_toxml(ezxml_t xml)
  659. {
  660. ezxml_t p = (xml) ? xml->parent : NULL, o = (xml) ? xml->ordered : NULL;
  661. ezxml_root_t root = (ezxml_root_t)xml;
  662. size_t len = 0, max = EZXML_BUFSIZE;
  663. char *s = strcpy(malloc(max), ""), *t, *n;
  664. int i, j, k;
  665. if (! xml || ! xml->name) return realloc(s, len + 1);
  666. while (root->xml.parent) root = (ezxml_root_t)root->xml.parent; // root tag
  667. for (i = 0; ! p && root->pi[i]; i++) { // pre-root processing instructions
  668. for (k = 2; root->pi[i][k - 1]; k++);
  669. for (j = 1; (n = root->pi[i][j]); j++) {
  670. if (root->pi[i][k][j - 1] == '>') continue; // not pre-root
  671. while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
  672. s = realloc(s, max += EZXML_BUFSIZE);
  673. len += sprintf(s + len, "<?%s%s%s?>\n", t, *n ? " " : "", n);
  674. }
  675. }
  676. xml->parent = xml->ordered = NULL;
  677. s = ezxml_toxml_r(xml, &s, &len, &max, 0, root->attr);
  678. xml->parent = p;
  679. xml->ordered = o;
  680. for (i = 0; ! p && root->pi[i]; i++) { // post-root processing instructions
  681. for (k = 2; root->pi[i][k - 1]; k++);
  682. for (j = 1; (n = root->pi[i][j]); j++) {
  683. if (root->pi[i][k][j - 1] == '<') continue; // not post-root
  684. while (len + strlen(t = root->pi[i][0]) + strlen(n) + 7 > max)
  685. s = realloc(s, max += EZXML_BUFSIZE);
  686. len += sprintf(s + len, "\n<?%s%s%s?>", t, *n ? " " : "", n);
  687. }
  688. }
  689. return realloc(s, len + 1);
  690. }
  691. // free the memory allocated for the ezxml structure
  692. void ezxml_free(ezxml_t xml)
  693. {
  694. ezxml_root_t root = (ezxml_root_t)xml;
  695. int i, j;
  696. char **a, *s;
  697. if (! xml) return;
  698. ezxml_free(xml->child);
  699. ezxml_free(xml->ordered);
  700. if (! xml->parent) { // free root tag allocations
  701. for (i = 10; root->ent[i]; i += 2) // 0 - 9 are default entites (<>&"')
  702. if ((s = root->ent[i + 1]) < root->s || s > root->e) free(s);
  703. free(root->ent); // free list of general entities
  704. for (i = 0; (a = root->attr[i]); i++) {
  705. for (j = 1; a[j++]; j += 2) // free malloced attribute values
  706. if (a[j] && (a[j] < root->s || a[j] > root->e)) free(a[j]);
  707. free(a);
  708. }
  709. if (root->attr[0]) free(root->attr); // free default attribute list
  710. for (i = 0; root->pi[i]; i++) {
  711. for (j = 1; root->pi[i][j]; j++);
  712. free(root->pi[i][j + 1]);
  713. free(root->pi[i]);
  714. }
  715. if (root->pi[0]) free(root->pi); // free processing instructions
  716. if (root->len == -1) free(root->m); // malloced xml data
  717. #ifndef EZXML_NOMMAP
  718. else if (root->len) munmap(root->m, root->len); // mem mapped xml data
  719. #endif // EZXML_NOMMAP
  720. if (root->u) free(root->u); // utf8 conversion
  721. }
  722. ezxml_free_attr(xml->attr); // tag attributes
  723. if ((xml->flags & EZXML_TXTM)) free(xml->txt); // character content
  724. if ((xml->flags & EZXML_NAMEM)) free(xml->name); // tag name
  725. free(xml);
  726. }
  727. // return parser error message or empty string if none
  728. const char *ezxml_error(ezxml_t xml)
  729. {
  730. while (xml && xml->parent) xml = xml->parent; // find root tag
  731. return (xml) ? ((ezxml_root_t)xml)->err : "";
  732. }
  733. // returns a new empty ezxml structure with the given root tag name
  734. ezxml_t ezxml_new(const char *name)
  735. {
  736. static char *ent[] = { "lt;", "&#60;", "gt;", "&#62;", "quot;", "&#34;",
  737. "apos;", "&#39;", "amp;", "&#38;", NULL };
  738. ezxml_root_t root = (ezxml_root_t)memset(malloc(sizeof(struct ezxml_root)),
  739. '\0', sizeof(struct ezxml_root));
  740. root->xml.name = (char *)name;
  741. root->cur = &root->xml;
  742. strcpy(root->err, root->xml.txt = "");
  743. root->ent = memcpy(malloc(sizeof(ent)), ent, sizeof(ent));
  744. root->attr = root->pi = (char ***)(root->xml.attr = EZXML_NIL);
  745. return &root->xml;
  746. }
  747. // inserts an existing tag into an ezxml structure
  748. ezxml_t ezxml_insert(ezxml_t xml, ezxml_t dest, size_t off)
  749. {
  750. ezxml_t cur, prev, head;
  751. xml->next = xml->sibling = xml->ordered = NULL;
  752. xml->off = off;
  753. xml->parent = dest;
  754. if ((head = dest->child)) { // already have sub tags
  755. if (head->off <= off) { // not first subtag
  756. for (cur = head; cur->ordered && cur->ordered->off <= off;
  757. cur = cur->ordered);
  758. xml->ordered = cur->ordered;
  759. cur->ordered = xml;
  760. }
  761. else { // first subtag
  762. xml->ordered = head;
  763. dest->child = xml;
  764. }
  765. for (cur = head, prev = NULL; cur && strcmp(cur->name, xml->name);
  766. prev = cur, cur = cur->sibling); // find tag type
  767. if (cur && cur->off <= off) { // not first of type
  768. while (cur->next && cur->next->off <= off) cur = cur->next;
  769. xml->next = cur->next;
  770. cur->next = xml;
  771. }
  772. else { // first tag of this type
  773. if (prev && cur) prev->sibling = cur->sibling; // remove old first
  774. xml->next = cur; // old first tag is now next
  775. for (cur = head, prev = NULL; cur && cur->off <= off;
  776. prev = cur, cur = cur->sibling); // new sibling insert point
  777. xml->sibling = cur;
  778. if (prev) prev->sibling = xml;
  779. }
  780. }
  781. else dest->child = xml; // only sub tag
  782. return xml;
  783. }
  784. // Adds a child tag. off is the offset of the child tag relative to the start
  785. // of the parent tag's character content. Returns the child tag.
  786. ezxml_t ezxml_add_child(ezxml_t xml, const char *name, size_t off)
  787. {
  788. ezxml_t child;
  789. if (! xml) return NULL;
  790. child = (ezxml_t)memset(malloc(sizeof(struct ezxml)), '\0',
  791. sizeof(struct ezxml));
  792. child->name = (char *)name;
  793. child->attr = EZXML_NIL;
  794. child->txt = "";
  795. return ezxml_insert(child, xml, off);
  796. }
  797. // sets the character content for the given tag and returns the tag
  798. ezxml_t ezxml_set_txt(ezxml_t xml, const char *txt)
  799. {
  800. if (! xml) return NULL;
  801. if (xml->flags & EZXML_TXTM) free(xml->txt); // existing txt was malloced
  802. xml->flags &= ~EZXML_TXTM;
  803. xml->txt = (char *)txt;
  804. return xml;
  805. }
  806. // Sets the given tag attribute or adds a new attribute if not found. A value
  807. // of NULL will remove the specified attribute. Returns the tag given.
  808. ezxml_t ezxml_set_attr(ezxml_t xml, const char *name, const char *value)
  809. {
  810. int l = 0, c;
  811. if (! xml) return NULL;
  812. while (xml->attr[l] && strcmp(xml->attr[l], name)) l += 2;
  813. if (! xml->attr[l]) { // not found, add as new attribute
  814. if (! value) return xml; // nothing to do
  815. if (xml->attr == EZXML_NIL) { // first attribute
  816. xml->attr = malloc(4 * sizeof(char *));
  817. xml->attr[1] = strdup(""); // empty list of malloced names/vals
  818. }
  819. else xml->attr = realloc(xml->attr, (l + 4) * sizeof(char *));
  820. xml->attr[l] = (char *)name; // set attribute name
  821. xml->attr[l + 2] = NULL; // null terminate attribute list
  822. xml->attr[l + 3] = realloc(xml->attr[l + 1],
  823. (c = strlen(xml->attr[l + 1])) + 2);
  824. strcpy(xml->attr[l + 3] + c, " "); // set name/value as not malloced
  825. if (xml->flags & EZXML_DUP) xml->attr[l + 3][c] = EZXML_NAMEM;
  826. }
  827. else if (xml->flags & EZXML_DUP) free((char *)name); // name was strduped
  828. for (c = l; xml->attr[c]; c += 2); // find end of attribute list
  829. if (xml->attr[c + 1][l / 2] & EZXML_TXTM) free(xml->attr[l + 1]); //old val
  830. if (xml->flags & EZXML_DUP) xml->attr[c + 1][l / 2] |= EZXML_TXTM;
  831. else xml->attr[c + 1][l / 2] &= ~EZXML_TXTM;
  832. if (value) xml->attr[l + 1] = (char *)value; // set attribute value
  833. else { // remove attribute
  834. if (xml->attr[c + 1][l / 2] & EZXML_NAMEM) free(xml->attr[l]);
  835. memmove(xml->attr + l, xml->attr + l + 2, (c - l + 2) * sizeof(char*));
  836. xml->attr = realloc(xml->attr, (c + 2) * sizeof(char *));
  837. memmove(xml->attr[c + 1] + (l / 2), xml->attr[c + 1] + (l / 2) + 1,
  838. (c / 2) - (l / 2)); // fix list of which name/vals are malloced
  839. }
  840. xml->flags &= ~EZXML_DUP; // clear strdup() flag
  841. return xml;
  842. }
  843. // sets a flag for the given tag and returns the tag
  844. ezxml_t ezxml_set_flag(ezxml_t xml, short flag)
  845. {
  846. if (xml) xml->flags |= flag;
  847. return xml;
  848. }
  849. // removes a tag along with its subtags without freeing its memory
  850. ezxml_t ezxml_cut(ezxml_t xml)
  851. {
  852. ezxml_t cur;
  853. if (! xml) return NULL; // nothing to do
  854. if (xml->next) xml->next->sibling = xml->sibling; // patch sibling list
  855. if (xml->parent) { // not root tag
  856. cur = xml->parent->child; // find head of subtag list
  857. if (cur == xml) xml->parent->child = xml->ordered; // first subtag
  858. else { // not first subtag
  859. while (cur->ordered != xml) cur = cur->ordered;
  860. cur->ordered = cur->ordered->ordered; // patch ordered list
  861. cur = xml->parent->child; // go back to head of subtag list
  862. if (strcmp(cur->name, xml->name)) { // not in first sibling list
  863. while (strcmp(cur->sibling->name, xml->name))
  864. cur = cur->sibling;
  865. if (cur->sibling == xml) { // first of a sibling list
  866. cur->sibling = (xml->next) ? xml->next
  867. : cur->sibling->sibling;
  868. }
  869. else cur = cur->sibling; // not first of a sibling list
  870. }
  871. while (cur->next && cur->next != xml) cur = cur->next;
  872. if (cur->next) cur->next = cur->next->next; // patch next list
  873. }
  874. }
  875. xml->ordered = xml->sibling = xml->next = NULL;
  876. return xml;
  877. }
  878. #ifdef EZXML_TEST // test harness
  879. int main(int argc, char **argv)
  880. {
  881. ezxml_t xml;
  882. char *s;
  883. int i;
  884. if (argc != 2) return fprintf(stderr, "usage: %s xmlfile\n", argv[0]);
  885. xml = ezxml_parse_file(argv[1]);
  886. printf("%s\n", (s = ezxml_toxml(xml)));
  887. free(s);
  888. i = fprintf(stderr, "%s", ezxml_error(xml));
  889. ezxml_free(xml);
  890. return (i) ? 1 : 0;
  891. }
  892. #endif // EZXML_TEST