ezxml.c 39 KB

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